2 * s390host.c -- hosting zSeries kernel virtual machines
4 * Copyright IBM Corp. 2008
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.
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
12 * Heiko Carstens <heiko.carstens@de.ibm.com>
15 #include <linux/compiler.h>
16 #include <linux/err.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>
28 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
30 struct kvm_stats_debugfs_item debugfs_entries[] = {
31 { "userspace_handled", VCPU_STAT(exit_userspace) },
36 /* Section: not file related */
37 void kvm_arch_hardware_enable(void *garbage)
39 /* every s390 is virtualization enabled ;-) */
42 void kvm_arch_hardware_disable(void *garbage)
46 void decache_vcpus_on_cpu(int cpu)
50 int kvm_arch_hardware_setup(void)
55 void kvm_arch_hardware_unsetup(void)
59 void kvm_arch_check_processor_compat(void *rtn)
63 int kvm_arch_init(void *opaque)
68 void kvm_arch_exit(void)
72 /* Section: device related */
73 long kvm_arch_dev_ioctl(struct file *filp,
74 unsigned int ioctl, unsigned long arg)
76 if (ioctl == KVM_S390_ENABLE_SIE)
77 return s390_enable_sie();
81 int kvm_dev_ioctl_check_extension(long ext)
86 /* Section: vm related */
88 * Get (and clear) the dirty memory log for a memory slot.
90 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
91 struct kvm_dirty_log *log)
96 long kvm_arch_vm_ioctl(struct file *filp,
97 unsigned int ioctl, unsigned long arg)
99 struct kvm *kvm = filp->private_data;
100 void __user *argp = (void __user *)arg;
111 struct kvm *kvm_arch_create_vm(void)
117 rc = s390_enable_sie();
122 kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
126 kvm->arch.sca = (struct sca_block *) get_zeroed_page(GFP_KERNEL);
130 sprintf(debug_name, "kvm-%u", current->pid);
132 kvm->arch.dbf = debug_register(debug_name, 8, 2, 8 * sizeof(long));
136 debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
137 VM_EVENT(kvm, 3, "%s", "vm created");
139 try_module_get(THIS_MODULE);
143 free_page((unsigned long)(kvm->arch.sca));
150 void kvm_arch_destroy_vm(struct kvm *kvm)
152 debug_unregister(kvm->arch.dbf);
153 free_page((unsigned long)(kvm->arch.sca));
155 module_put(THIS_MODULE);
158 /* Section: vcpu related */
159 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
164 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
166 /* kvm common code refers to this, but does'nt call it */
170 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
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);
178 if (signal_pending(current))
179 atomic_set_mask(CPUSTAT_STOP_INT,
180 &vcpu->arch.sie_block->cpuflags);
183 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
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);
191 static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
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;
209 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
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;
220 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
223 struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
229 vcpu->arch.sie_block = (struct sie_block *) get_zeroed_page(GFP_KERNEL);
231 if (!vcpu->arch.sie_block)
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;
241 rc = kvm_vcpu_init(vcpu, kvm, id);
244 VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu,
245 vcpu->arch.sie_block);
247 try_module_get(THIS_MODULE);
256 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
258 VCPU_EVENT(vcpu, 3, "%s", "destroy cpu");
259 free_page((unsigned long)(vcpu->arch.sie_block));
261 module_put(THIS_MODULE);
264 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
266 /* kvm common code refers to this, but never calls it */
271 static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
274 kvm_s390_vcpu_initial_reset(vcpu);
279 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
282 memcpy(&vcpu->arch.guest_gprs, ®s->gprs, sizeof(regs->gprs));
287 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
290 memcpy(®s->gprs, &vcpu->arch.guest_gprs, sizeof(regs->gprs));
295 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
296 struct kvm_sregs *sregs)
299 memcpy(&vcpu->arch.guest_acrs, &sregs->acrs, sizeof(sregs->acrs));
300 memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
305 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
306 struct kvm_sregs *sregs)
309 memcpy(&sregs->acrs, &vcpu->arch.guest_acrs, sizeof(sregs->acrs));
310 memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
315 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
318 memcpy(&vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs));
319 vcpu->arch.guest_fpregs.fpc = fpu->fpc;
324 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
327 memcpy(&fpu->fprs, &vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs));
328 fpu->fpc = vcpu->arch.guest_fpregs.fpc;
333 static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
338 if (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_RUNNING)
341 vcpu->arch.sie_block->gpsw = psw;
346 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
347 struct kvm_translation *tr)
349 return -EINVAL; /* not implemented yet */
352 int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
353 struct kvm_debug_guest *dbg)
355 return -EINVAL; /* not implemented yet */
358 static void __vcpu_run(struct kvm_vcpu *vcpu)
360 memcpy(&vcpu->arch.sie_block->gg14, &vcpu->arch.guest_gprs[14], 16);
365 vcpu->arch.sie_block->icptcode = 0;
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);
378 memcpy(&vcpu->arch.guest_gprs[14], &vcpu->arch.sie_block->gg14, 16);
381 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
387 if (vcpu->sigset_active)
388 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
390 atomic_set_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
394 if (vcpu->sigset_active)
395 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
399 vcpu->stat.exit_userspace++;
403 static int __guestcopy(struct kvm_vcpu *vcpu, u64 guestdest, const void *from,
404 unsigned long n, int prefix)
407 return copy_to_guest(vcpu, guestdest, from, n);
409 return copy_to_guest_absolute(vcpu, guestdest, from, n);
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
418 int __kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
420 const unsigned char archmode = 1;
423 if (addr == KVM_S390_STORE_STATUS_NOADDR) {
424 if (copy_to_guest_absolute(vcpu, 163ul, &archmode, 1))
426 addr = SAVE_AREA_BASE;
428 } else if (addr == KVM_S390_STORE_STATUS_PREFIXED) {
429 if (copy_to_guest(vcpu, 163ul, &archmode, 1))
431 addr = SAVE_AREA_BASE;
436 if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, fp_regs),
437 vcpu->arch.guest_fpregs.fprs, 128, prefix))
440 if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, gp_regs),
441 vcpu->arch.guest_gprs, 128, prefix))
444 if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, psw),
445 &vcpu->arch.sie_block->gpsw, 16, prefix))
448 if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, pref_reg),
449 &vcpu->arch.sie_block->prefix, 4, prefix))
452 if (__guestcopy(vcpu,
453 addr + offsetof(struct save_area_s390x, fp_ctrl_reg),
454 &vcpu->arch.guest_fpregs.fpc, 4, prefix))
457 if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, tod_reg),
458 &vcpu->arch.sie_block->todpr, 4, prefix))
461 if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, timer),
462 &vcpu->arch.sie_block->cputm, 8, prefix))
465 if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, clk_cmp),
466 &vcpu->arch.sie_block->ckc, 8, prefix))
469 if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, acc_regs),
470 &vcpu->arch.guest_acrs, 64, prefix))
473 if (__guestcopy(vcpu,
474 addr + offsetof(struct save_area_s390x, ctrl_regs),
475 &vcpu->arch.sie_block->gcr, 128, prefix))
480 static int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
485 rc = __kvm_s390_vcpu_store_status(vcpu, addr);
490 long kvm_arch_vcpu_ioctl(struct file *filp,
491 unsigned int ioctl, unsigned long arg)
493 struct kvm_vcpu *vcpu = filp->private_data;
494 void __user *argp = (void __user *)arg;
497 case KVM_S390_STORE_STATUS:
498 return kvm_s390_vcpu_store_status(vcpu, arg);
499 case KVM_S390_SET_INITIAL_PSW: {
502 if (copy_from_user(&psw, argp, sizeof(psw)))
504 return kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
506 case KVM_S390_INITIAL_RESET:
507 return kvm_arch_vcpu_ioctl_initial_reset(vcpu);
514 /* Section: memory related */
515 int kvm_arch_set_memory_region(struct kvm *kvm,
516 struct kvm_userspace_memory_region *mem,
517 struct kvm_memory_slot old,
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 */
530 if (mem->guest_phys_addr)
533 if (mem->userspace_addr & (PAGE_SIZE - 1))
536 if (mem->memory_size & (PAGE_SIZE - 1))
539 kvm->arch.guest_origin = mem->userspace_addr;
540 kvm->arch.guest_memsize = mem->memory_size;
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. */
550 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
555 static int __init kvm_s390_init(void)
557 return kvm_init(NULL, sizeof(struct kvm_vcpu), THIS_MODULE);
560 static void __exit kvm_s390_exit(void)
565 module_init(kvm_s390_init);
566 module_exit(kvm_s390_exit);