aboutsummaryrefslogtreecommitdiffstats
path: root/arch/s390/kvm/kvm-s390.c
diff options
context:
space:
mode:
authorHeiko Carstens <heiko.carstens@de.ibm.com>2008-03-25 13:47:20 -0400
committerAvi Kivity <avi@qumranet.com>2008-04-27 05:00:42 -0400
commitb0c632db637d68ad39d9f97f452ce176253f5f4e (patch)
tree770d68c17cbcabc1543d1e9125669130fcf4fee4 /arch/s390/kvm/kvm-s390.c
parent8a88ac6183975c73c65b45f365f6f3b875c1348b (diff)
KVM: s390: arch backend for the kvm kernel module
This patch contains the port of Qumranet's kvm kernel module to IBM zSeries (aka s390x, mainframe) architecture. It uses the mainframe's virtualization instruction SIE to run virtual machines with up to 64 virtual CPUs each. This port is only usable on 64bit host kernels, and can only run 64bit guest kernels. However, running 31bit applications in guest userspace is possible. The following source files are introduced by this patch arch/s390/kvm/kvm-s390.c similar to arch/x86/kvm/x86.c, this implements all arch callbacks for kvm. __vcpu_run calls back into sie64a to enter the guest machine context arch/s390/kvm/sie64a.S assembler function sie64a, which enters guest context via SIE, and switches world before and after that include/asm-s390/kvm_host.h contains all vital data structures needed to run virtual machines on the mainframe include/asm-s390/kvm.h defines kvm_regs and friends for user access to guest register content arch/s390/kvm/gaccess.h functions similar to uaccess to access guest memory arch/s390/kvm/kvm-s390.h header file for kvm-s390 internals, extended by later patches Acked-by: Martin Schwidefsky <schwidefsky@de.ibm.com> Signed-off-by: Christian Borntraeger <borntraeger@de.ibm.com> Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com> Signed-off-by: Carsten Otte <cotte@de.ibm.com> Signed-off-by: Avi Kivity <avi@qumranet.com>
Diffstat (limited to 'arch/s390/kvm/kvm-s390.c')
-rw-r--r--arch/s390/kvm/kvm-s390.c566
1 files changed, 566 insertions, 0 deletions
diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c
new file mode 100644
index 000000000000..6e1e1d39ae15
--- /dev/null
+++ b/arch/s390/kvm/kvm-s390.c
@@ -0,0 +1,566 @@
1/*
2 * s390host.c -- hosting zSeries kernel virtual machines
3 *
4 * Copyright IBM Corp. 2008
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
9 *
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
12 * Heiko Carstens <heiko.carstens@de.ibm.com>
13 */
14
15#include <linux/compiler.h>
16#include <linux/err.h>
17#include <linux/fs.h>
18#include <linux/init.h>
19#include <linux/kvm.h>
20#include <linux/kvm_host.h>
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <asm/lowcore.h>
24#include <asm/pgtable.h>
25
26#include "gaccess.h"
27
28#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
29
30struct kvm_stats_debugfs_item debugfs_entries[] = {
31 { "userspace_handled", VCPU_STAT(exit_userspace) },
32 { NULL }
33};
34
35
36/* Section: not file related */
37void kvm_arch_hardware_enable(void *garbage)
38{
39 /* every s390 is virtualization enabled ;-) */
40}
41
42void kvm_arch_hardware_disable(void *garbage)
43{
44}
45
46void decache_vcpus_on_cpu(int cpu)
47{
48}
49
50int kvm_arch_hardware_setup(void)
51{
52 return 0;
53}
54
55void kvm_arch_hardware_unsetup(void)
56{
57}
58
59void kvm_arch_check_processor_compat(void *rtn)
60{
61}
62
63int kvm_arch_init(void *opaque)
64{
65 return 0;
66}
67
68void kvm_arch_exit(void)
69{
70}
71
72/* Section: device related */
73long kvm_arch_dev_ioctl(struct file *filp,
74 unsigned int ioctl, unsigned long arg)
75{
76 if (ioctl == KVM_S390_ENABLE_SIE)
77 return s390_enable_sie();
78 return -EINVAL;
79}
80
81int kvm_dev_ioctl_check_extension(long ext)
82{
83 return 0;
84}
85
86/* Section: vm related */
87/*
88 * Get (and clear) the dirty memory log for a memory slot.
89 */
90int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
91 struct kvm_dirty_log *log)
92{
93 return 0;
94}
95
96long kvm_arch_vm_ioctl(struct file *filp,
97 unsigned int ioctl, unsigned long arg)
98{
99 struct kvm *kvm = filp->private_data;
100 void __user *argp = (void __user *)arg;
101 int r;
102
103 switch (ioctl) {
104 default:
105 r = -EINVAL;
106 }
107
108 return r;
109}
110
111struct kvm *kvm_arch_create_vm(void)
112{
113 struct kvm *kvm;
114 int rc;
115 char debug_name[16];
116
117 rc = s390_enable_sie();
118 if (rc)
119 goto out_nokvm;
120
121 rc = -ENOMEM;
122 kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
123 if (!kvm)
124 goto out_nokvm;
125
126 kvm->arch.sca = (struct sca_block *) get_zeroed_page(GFP_KERNEL);
127 if (!kvm->arch.sca)
128 goto out_nosca;
129
130 sprintf(debug_name, "kvm-%u", current->pid);
131
132 kvm->arch.dbf = debug_register(debug_name, 8, 2, 8 * sizeof(long));
133 if (!kvm->arch.dbf)
134 goto out_nodbf;
135
136 debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
137 VM_EVENT(kvm, 3, "%s", "vm created");
138
139 try_module_get(THIS_MODULE);
140
141 return kvm;
142out_nodbf:
143 free_page((unsigned long)(kvm->arch.sca));
144out_nosca:
145 kfree(kvm);
146out_nokvm:
147 return ERR_PTR(rc);
148}
149
150void kvm_arch_destroy_vm(struct kvm *kvm)
151{
152 debug_unregister(kvm->arch.dbf);
153 free_page((unsigned long)(kvm->arch.sca));
154 kfree(kvm);
155 module_put(THIS_MODULE);
156}
157
158/* Section: vcpu related */
159int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
160{
161 return 0;
162}
163
164void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
165{
166 /* kvm common code refers to this, but does'nt call it */
167 BUG();
168}
169
170void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
171{
172 save_fp_regs(&vcpu->arch.host_fpregs);
173 save_access_regs(vcpu->arch.host_acrs);
174 vcpu->arch.guest_fpregs.fpc &= FPC_VALID_MASK;
175 restore_fp_regs(&vcpu->arch.guest_fpregs);
176 restore_access_regs(vcpu->arch.guest_acrs);
177
178 if (signal_pending(current))
179 atomic_set_mask(CPUSTAT_STOP_INT,
180 &vcpu->arch.sie_block->cpuflags);
181}
182
183void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
184{
185 save_fp_regs(&vcpu->arch.guest_fpregs);
186 save_access_regs(vcpu->arch.guest_acrs);
187 restore_fp_regs(&vcpu->arch.host_fpregs);
188 restore_access_regs(vcpu->arch.host_acrs);
189}
190
191static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
192{
193 /* this equals initial cpu reset in pop, but we don't switch to ESA */
194 vcpu->arch.sie_block->gpsw.mask = 0UL;
195 vcpu->arch.sie_block->gpsw.addr = 0UL;
196 vcpu->arch.sie_block->prefix = 0UL;
197 vcpu->arch.sie_block->ihcpu = 0xffff;
198 vcpu->arch.sie_block->cputm = 0UL;
199 vcpu->arch.sie_block->ckc = 0UL;
200 vcpu->arch.sie_block->todpr = 0;
201 memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
202 vcpu->arch.sie_block->gcr[0] = 0xE0UL;
203 vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
204 vcpu->arch.guest_fpregs.fpc = 0;
205 asm volatile("lfpc %0" : : "Q" (vcpu->arch.guest_fpregs.fpc));
206 vcpu->arch.sie_block->gbea = 1;
207}
208
209int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
210{
211 atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH);
212 vcpu->arch.sie_block->gmslm = 0xffffffffffUL;
213 vcpu->arch.sie_block->gmsor = 0x000000000000;
214 vcpu->arch.sie_block->ecb = 2;
215 vcpu->arch.sie_block->eca = 0xC1002001U;
216
217 return 0;
218}
219
220struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
221 unsigned int id)
222{
223 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
224 int rc = -ENOMEM;
225
226 if (!vcpu)
227 goto out_nomem;
228
229 vcpu->arch.sie_block = (struct sie_block *) get_zeroed_page(GFP_KERNEL);
230
231 if (!vcpu->arch.sie_block)
232 goto out_free_cpu;
233
234 vcpu->arch.sie_block->icpua = id;
235 BUG_ON(!kvm->arch.sca);
236 BUG_ON(kvm->arch.sca->cpu[id].sda);
237 kvm->arch.sca->cpu[id].sda = (__u64) vcpu->arch.sie_block;
238 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)kvm->arch.sca) >> 32);
239 vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca;
240
241 rc = kvm_vcpu_init(vcpu, kvm, id);
242 if (rc)
243 goto out_free_cpu;
244 VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu,
245 vcpu->arch.sie_block);
246
247 try_module_get(THIS_MODULE);
248
249 return vcpu;
250out_free_cpu:
251 kfree(vcpu);
252out_nomem:
253 return ERR_PTR(rc);
254}
255
256void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
257{
258 VCPU_EVENT(vcpu, 3, "%s", "destroy cpu");
259 free_page((unsigned long)(vcpu->arch.sie_block));
260 kfree(vcpu);
261 module_put(THIS_MODULE);
262}
263
264int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
265{
266 /* kvm common code refers to this, but never calls it */
267 BUG();
268 return 0;
269}
270
271static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
272{
273 vcpu_load(vcpu);
274 kvm_s390_vcpu_initial_reset(vcpu);
275 vcpu_put(vcpu);
276 return 0;
277}
278
279int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
280{
281 vcpu_load(vcpu);
282 memcpy(&vcpu->arch.guest_gprs, &regs->gprs, sizeof(regs->gprs));
283 vcpu_put(vcpu);
284 return 0;
285}
286
287int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
288{
289 vcpu_load(vcpu);
290 memcpy(&regs->gprs, &vcpu->arch.guest_gprs, sizeof(regs->gprs));
291 vcpu_put(vcpu);
292 return 0;
293}
294
295int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
296 struct kvm_sregs *sregs)
297{
298 vcpu_load(vcpu);
299 memcpy(&vcpu->arch.guest_acrs, &sregs->acrs, sizeof(sregs->acrs));
300 memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
301 vcpu_put(vcpu);
302 return 0;
303}
304
305int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
306 struct kvm_sregs *sregs)
307{
308 vcpu_load(vcpu);
309 memcpy(&sregs->acrs, &vcpu->arch.guest_acrs, sizeof(sregs->acrs));
310 memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
311 vcpu_put(vcpu);
312 return 0;
313}
314
315int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
316{
317 vcpu_load(vcpu);
318 memcpy(&vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs));
319 vcpu->arch.guest_fpregs.fpc = fpu->fpc;
320 vcpu_put(vcpu);
321 return 0;
322}
323
324int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
325{
326 vcpu_load(vcpu);
327 memcpy(&fpu->fprs, &vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs));
328 fpu->fpc = vcpu->arch.guest_fpregs.fpc;
329 vcpu_put(vcpu);
330 return 0;
331}
332
333static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
334{
335 int rc = 0;
336
337 vcpu_load(vcpu);
338 if (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_RUNNING)
339 rc = -EBUSY;
340 else
341 vcpu->arch.sie_block->gpsw = psw;
342 vcpu_put(vcpu);
343 return rc;
344}
345
346int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
347 struct kvm_translation *tr)
348{
349 return -EINVAL; /* not implemented yet */
350}
351
352int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
353 struct kvm_debug_guest *dbg)
354{
355 return -EINVAL; /* not implemented yet */
356}
357
358static void __vcpu_run(struct kvm_vcpu *vcpu)
359{
360 memcpy(&vcpu->arch.sie_block->gg14, &vcpu->arch.guest_gprs[14], 16);
361
362 if (need_resched())
363 schedule();
364
365 vcpu->arch.sie_block->icptcode = 0;
366 local_irq_disable();
367 kvm_guest_enter();
368 local_irq_enable();
369 VCPU_EVENT(vcpu, 6, "entering sie flags %x",
370 atomic_read(&vcpu->arch.sie_block->cpuflags));
371 sie64a(vcpu->arch.sie_block, vcpu->arch.guest_gprs);
372 VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
373 vcpu->arch.sie_block->icptcode);
374 local_irq_disable();
375 kvm_guest_exit();
376 local_irq_enable();
377
378 memcpy(&vcpu->arch.guest_gprs[14], &vcpu->arch.sie_block->gg14, 16);
379}
380
381int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
382{
383 sigset_t sigsaved;
384
385 vcpu_load(vcpu);
386
387 if (vcpu->sigset_active)
388 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
389
390 atomic_set_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
391
392 __vcpu_run(vcpu);
393
394 if (vcpu->sigset_active)
395 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
396
397 vcpu_put(vcpu);
398
399 vcpu->stat.exit_userspace++;
400 return 0;
401}
402
403static int __guestcopy(struct kvm_vcpu *vcpu, u64 guestdest, const void *from,
404 unsigned long n, int prefix)
405{
406 if (prefix)
407 return copy_to_guest(vcpu, guestdest, from, n);
408 else
409 return copy_to_guest_absolute(vcpu, guestdest, from, n);
410}
411
412/*
413 * store status at address
414 * we use have two special cases:
415 * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
416 * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
417 */
418int __kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
419{
420 const unsigned char archmode = 1;
421 int prefix;
422
423 if (addr == KVM_S390_STORE_STATUS_NOADDR) {
424 if (copy_to_guest_absolute(vcpu, 163ul, &archmode, 1))
425 return -EFAULT;
426 addr = SAVE_AREA_BASE;
427 prefix = 0;
428 } else if (addr == KVM_S390_STORE_STATUS_PREFIXED) {
429 if (copy_to_guest(vcpu, 163ul, &archmode, 1))
430 return -EFAULT;
431 addr = SAVE_AREA_BASE;
432 prefix = 1;
433 } else
434 prefix = 0;
435
436 if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, fp_regs),
437 vcpu->arch.guest_fpregs.fprs, 128, prefix))
438 return -EFAULT;
439
440 if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, gp_regs),
441 vcpu->arch.guest_gprs, 128, prefix))
442 return -EFAULT;
443
444 if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, psw),
445 &vcpu->arch.sie_block->gpsw, 16, prefix))
446 return -EFAULT;
447
448 if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, pref_reg),
449 &vcpu->arch.sie_block->prefix, 4, prefix))
450 return -EFAULT;
451
452 if (__guestcopy(vcpu,
453 addr + offsetof(struct save_area_s390x, fp_ctrl_reg),
454 &vcpu->arch.guest_fpregs.fpc, 4, prefix))
455 return -EFAULT;
456
457 if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, tod_reg),
458 &vcpu->arch.sie_block->todpr, 4, prefix))
459 return -EFAULT;
460
461 if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, timer),
462 &vcpu->arch.sie_block->cputm, 8, prefix))
463 return -EFAULT;
464
465 if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, clk_cmp),
466 &vcpu->arch.sie_block->ckc, 8, prefix))
467 return -EFAULT;
468
469 if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, acc_regs),
470 &vcpu->arch.guest_acrs, 64, prefix))
471 return -EFAULT;
472
473 if (__guestcopy(vcpu,
474 addr + offsetof(struct save_area_s390x, ctrl_regs),
475 &vcpu->arch.sie_block->gcr, 128, prefix))
476 return -EFAULT;
477 return 0;
478}
479
480static int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
481{
482 int rc;
483
484 vcpu_load(vcpu);
485 rc = __kvm_s390_vcpu_store_status(vcpu, addr);
486 vcpu_put(vcpu);
487 return rc;
488}
489
490long kvm_arch_vcpu_ioctl(struct file *filp,
491 unsigned int ioctl, unsigned long arg)
492{
493 struct kvm_vcpu *vcpu = filp->private_data;
494 void __user *argp = (void __user *)arg;
495
496 switch (ioctl) {
497 case KVM_S390_STORE_STATUS:
498 return kvm_s390_vcpu_store_status(vcpu, arg);
499 case KVM_S390_SET_INITIAL_PSW: {
500 psw_t psw;
501
502 if (copy_from_user(&psw, argp, sizeof(psw)))
503 return -EFAULT;
504 return kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
505 }
506 case KVM_S390_INITIAL_RESET:
507 return kvm_arch_vcpu_ioctl_initial_reset(vcpu);
508 default:
509 ;
510 }
511 return -EINVAL;
512}
513
514/* Section: memory related */
515int kvm_arch_set_memory_region(struct kvm *kvm,
516 struct kvm_userspace_memory_region *mem,
517 struct kvm_memory_slot old,
518 int user_alloc)
519{
520 /* A few sanity checks. We can have exactly one memory slot which has
521 to start at guest virtual zero and which has to be located at a
522 page boundary in userland and which has to end at a page boundary.
523 The memory in userland is ok to be fragmented into various different
524 vmas. It is okay to mmap() and munmap() stuff in this slot after
525 doing this call at any time */
526
527 if (mem->slot)
528 return -EINVAL;
529
530 if (mem->guest_phys_addr)
531 return -EINVAL;
532
533 if (mem->userspace_addr & (PAGE_SIZE - 1))
534 return -EINVAL;
535
536 if (mem->memory_size & (PAGE_SIZE - 1))
537 return -EINVAL;
538
539 kvm->arch.guest_origin = mem->userspace_addr;
540 kvm->arch.guest_memsize = mem->memory_size;
541
542 /* FIXME: we do want to interrupt running CPUs and update their memory
543 configuration now to avoid race conditions. But hey, changing the
544 memory layout while virtual CPUs are running is usually bad
545 programming practice. */
546
547 return 0;
548}
549
550gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
551{
552 return gfn;
553}
554
555static int __init kvm_s390_init(void)
556{
557 return kvm_init(NULL, sizeof(struct kvm_vcpu), THIS_MODULE);
558}
559
560static void __exit kvm_s390_exit(void)
561{
562 kvm_exit();
563}
564
565module_init(kvm_s390_init);
566module_exit(kvm_s390_exit);