KVM: ia64: preserve int status through call to kvm_insert_vmm_mapping
[deliverable/linux.git] / arch / ia64 / kvm / kvm-ia64.c
1 /*
2 * kvm_ia64.c: Basic KVM suppport On Itanium series processors
3 *
4 *
5 * Copyright (C) 2007, Intel Corporation.
6 * Xiantao Zhang (xiantao.zhang@intel.com)
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place - Suite 330, Boston, MA 02111-1307 USA.
20 *
21 */
22
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/percpu.h>
26 #include <linux/gfp.h>
27 #include <linux/fs.h>
28 #include <linux/smp.h>
29 #include <linux/kvm_host.h>
30 #include <linux/kvm.h>
31 #include <linux/bitops.h>
32 #include <linux/hrtimer.h>
33 #include <linux/uaccess.h>
34 #include <linux/iommu.h>
35 #include <linux/intel-iommu.h>
36
37 #include <asm/pgtable.h>
38 #include <asm/gcc_intrin.h>
39 #include <asm/pal.h>
40 #include <asm/cacheflush.h>
41 #include <asm/div64.h>
42 #include <asm/tlb.h>
43 #include <asm/elf.h>
44 #include <asm/sn/addrs.h>
45 #include <asm/sn/clksupport.h>
46 #include <asm/sn/shub_mmr.h>
47
48 #include "misc.h"
49 #include "vti.h"
50 #include "iodev.h"
51 #include "ioapic.h"
52 #include "lapic.h"
53 #include "irq.h"
54
55 static unsigned long kvm_vmm_base;
56 static unsigned long kvm_vsa_base;
57 static unsigned long kvm_vm_buffer;
58 static unsigned long kvm_vm_buffer_size;
59 unsigned long kvm_vmm_gp;
60
61 static long vp_env_info;
62
63 static struct kvm_vmm_info *kvm_vmm_info;
64
65 static DEFINE_PER_CPU(struct kvm_vcpu *, last_vcpu);
66
67 struct kvm_stats_debugfs_item debugfs_entries[] = {
68 { NULL }
69 };
70
71 static unsigned long kvm_get_itc(struct kvm_vcpu *vcpu)
72 {
73 #if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
74 if (vcpu->kvm->arch.is_sn2)
75 return rtc_time();
76 else
77 #endif
78 return ia64_getreg(_IA64_REG_AR_ITC);
79 }
80
81 static void kvm_flush_icache(unsigned long start, unsigned long len)
82 {
83 int l;
84
85 for (l = 0; l < (len + 32); l += 32)
86 ia64_fc((void *)(start + l));
87
88 ia64_sync_i();
89 ia64_srlz_i();
90 }
91
92 static void kvm_flush_tlb_all(void)
93 {
94 unsigned long i, j, count0, count1, stride0, stride1, addr;
95 long flags;
96
97 addr = local_cpu_data->ptce_base;
98 count0 = local_cpu_data->ptce_count[0];
99 count1 = local_cpu_data->ptce_count[1];
100 stride0 = local_cpu_data->ptce_stride[0];
101 stride1 = local_cpu_data->ptce_stride[1];
102
103 local_irq_save(flags);
104 for (i = 0; i < count0; ++i) {
105 for (j = 0; j < count1; ++j) {
106 ia64_ptce(addr);
107 addr += stride1;
108 }
109 addr += stride0;
110 }
111 local_irq_restore(flags);
112 ia64_srlz_i(); /* srlz.i implies srlz.d */
113 }
114
115 long ia64_pal_vp_create(u64 *vpd, u64 *host_iva, u64 *opt_handler)
116 {
117 struct ia64_pal_retval iprv;
118
119 PAL_CALL_STK(iprv, PAL_VP_CREATE, (u64)vpd, (u64)host_iva,
120 (u64)opt_handler);
121
122 return iprv.status;
123 }
124
125 static DEFINE_SPINLOCK(vp_lock);
126
127 void kvm_arch_hardware_enable(void *garbage)
128 {
129 long status;
130 long tmp_base;
131 unsigned long pte;
132 unsigned long saved_psr;
133 int slot;
134
135 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
136 local_irq_save(saved_psr);
137 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
138 local_irq_restore(saved_psr);
139 if (slot < 0)
140 return;
141
142 spin_lock(&vp_lock);
143 status = ia64_pal_vp_init_env(kvm_vsa_base ?
144 VP_INIT_ENV : VP_INIT_ENV_INITALIZE,
145 __pa(kvm_vm_buffer), KVM_VM_BUFFER_BASE, &tmp_base);
146 if (status != 0) {
147 printk(KERN_WARNING"kvm: Failed to Enable VT Support!!!!\n");
148 return ;
149 }
150
151 if (!kvm_vsa_base) {
152 kvm_vsa_base = tmp_base;
153 printk(KERN_INFO"kvm: kvm_vsa_base:0x%lx\n", kvm_vsa_base);
154 }
155 spin_unlock(&vp_lock);
156 ia64_ptr_entry(0x3, slot);
157 }
158
159 void kvm_arch_hardware_disable(void *garbage)
160 {
161
162 long status;
163 int slot;
164 unsigned long pte;
165 unsigned long saved_psr;
166 unsigned long host_iva = ia64_getreg(_IA64_REG_CR_IVA);
167
168 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base),
169 PAGE_KERNEL));
170
171 local_irq_save(saved_psr);
172 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
173 local_irq_restore(saved_psr);
174 if (slot < 0)
175 return;
176
177 status = ia64_pal_vp_exit_env(host_iva);
178 if (status)
179 printk(KERN_DEBUG"kvm: Failed to disable VT support! :%ld\n",
180 status);
181 ia64_ptr_entry(0x3, slot);
182 }
183
184 void kvm_arch_check_processor_compat(void *rtn)
185 {
186 *(int *)rtn = 0;
187 }
188
189 int kvm_dev_ioctl_check_extension(long ext)
190 {
191
192 int r;
193
194 switch (ext) {
195 case KVM_CAP_IRQCHIP:
196 case KVM_CAP_MP_STATE:
197 case KVM_CAP_IRQ_INJECT_STATUS:
198 r = 1;
199 break;
200 case KVM_CAP_COALESCED_MMIO:
201 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
202 break;
203 case KVM_CAP_IOMMU:
204 r = iommu_found();
205 break;
206 default:
207 r = 0;
208 }
209 return r;
210
211 }
212
213 static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
214 gpa_t addr, int len, int is_write)
215 {
216 struct kvm_io_device *dev;
217
218 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len, is_write);
219
220 return dev;
221 }
222
223 static int handle_vm_error(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
224 {
225 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
226 kvm_run->hw.hardware_exit_reason = 1;
227 return 0;
228 }
229
230 static int handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
231 {
232 struct kvm_mmio_req *p;
233 struct kvm_io_device *mmio_dev;
234
235 p = kvm_get_vcpu_ioreq(vcpu);
236
237 if ((p->addr & PAGE_MASK) == IOAPIC_DEFAULT_BASE_ADDRESS)
238 goto mmio;
239 vcpu->mmio_needed = 1;
240 vcpu->mmio_phys_addr = kvm_run->mmio.phys_addr = p->addr;
241 vcpu->mmio_size = kvm_run->mmio.len = p->size;
242 vcpu->mmio_is_write = kvm_run->mmio.is_write = !p->dir;
243
244 if (vcpu->mmio_is_write)
245 memcpy(vcpu->mmio_data, &p->data, p->size);
246 memcpy(kvm_run->mmio.data, &p->data, p->size);
247 kvm_run->exit_reason = KVM_EXIT_MMIO;
248 return 0;
249 mmio:
250 mmio_dev = vcpu_find_mmio_dev(vcpu, p->addr, p->size, !p->dir);
251 if (mmio_dev) {
252 if (!p->dir)
253 kvm_iodevice_write(mmio_dev, p->addr, p->size,
254 &p->data);
255 else
256 kvm_iodevice_read(mmio_dev, p->addr, p->size,
257 &p->data);
258
259 } else
260 printk(KERN_ERR"kvm: No iodevice found! addr:%lx\n", p->addr);
261 p->state = STATE_IORESP_READY;
262
263 return 1;
264 }
265
266 static int handle_pal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
267 {
268 struct exit_ctl_data *p;
269
270 p = kvm_get_exit_data(vcpu);
271
272 if (p->exit_reason == EXIT_REASON_PAL_CALL)
273 return kvm_pal_emul(vcpu, kvm_run);
274 else {
275 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
276 kvm_run->hw.hardware_exit_reason = 2;
277 return 0;
278 }
279 }
280
281 static int handle_sal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
282 {
283 struct exit_ctl_data *p;
284
285 p = kvm_get_exit_data(vcpu);
286
287 if (p->exit_reason == EXIT_REASON_SAL_CALL) {
288 kvm_sal_emul(vcpu);
289 return 1;
290 } else {
291 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
292 kvm_run->hw.hardware_exit_reason = 3;
293 return 0;
294 }
295
296 }
297
298 static int __apic_accept_irq(struct kvm_vcpu *vcpu, uint64_t vector)
299 {
300 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
301
302 if (!test_and_set_bit(vector, &vpd->irr[0])) {
303 vcpu->arch.irq_new_pending = 1;
304 kvm_vcpu_kick(vcpu);
305 return 1;
306 }
307 return 0;
308 }
309
310 /*
311 * offset: address offset to IPI space.
312 * value: deliver value.
313 */
314 static void vcpu_deliver_ipi(struct kvm_vcpu *vcpu, uint64_t dm,
315 uint64_t vector)
316 {
317 switch (dm) {
318 case SAPIC_FIXED:
319 break;
320 case SAPIC_NMI:
321 vector = 2;
322 break;
323 case SAPIC_EXTINT:
324 vector = 0;
325 break;
326 case SAPIC_INIT:
327 case SAPIC_PMI:
328 default:
329 printk(KERN_ERR"kvm: Unimplemented Deliver reserved IPI!\n");
330 return;
331 }
332 __apic_accept_irq(vcpu, vector);
333 }
334
335 static struct kvm_vcpu *lid_to_vcpu(struct kvm *kvm, unsigned long id,
336 unsigned long eid)
337 {
338 union ia64_lid lid;
339 int i;
340
341 for (i = 0; i < kvm->arch.online_vcpus; i++) {
342 if (kvm->vcpus[i]) {
343 lid.val = VCPU_LID(kvm->vcpus[i]);
344 if (lid.id == id && lid.eid == eid)
345 return kvm->vcpus[i];
346 }
347 }
348
349 return NULL;
350 }
351
352 static int handle_ipi(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
353 {
354 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
355 struct kvm_vcpu *target_vcpu;
356 struct kvm_pt_regs *regs;
357 union ia64_ipi_a addr = p->u.ipi_data.addr;
358 union ia64_ipi_d data = p->u.ipi_data.data;
359
360 target_vcpu = lid_to_vcpu(vcpu->kvm, addr.id, addr.eid);
361 if (!target_vcpu)
362 return handle_vm_error(vcpu, kvm_run);
363
364 if (!target_vcpu->arch.launched) {
365 regs = vcpu_regs(target_vcpu);
366
367 regs->cr_iip = vcpu->kvm->arch.rdv_sal_data.boot_ip;
368 regs->r1 = vcpu->kvm->arch.rdv_sal_data.boot_gp;
369
370 target_vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
371 if (waitqueue_active(&target_vcpu->wq))
372 wake_up_interruptible(&target_vcpu->wq);
373 } else {
374 vcpu_deliver_ipi(target_vcpu, data.dm, data.vector);
375 if (target_vcpu != vcpu)
376 kvm_vcpu_kick(target_vcpu);
377 }
378
379 return 1;
380 }
381
382 struct call_data {
383 struct kvm_ptc_g ptc_g_data;
384 struct kvm_vcpu *vcpu;
385 };
386
387 static void vcpu_global_purge(void *info)
388 {
389 struct call_data *p = (struct call_data *)info;
390 struct kvm_vcpu *vcpu = p->vcpu;
391
392 if (test_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
393 return;
394
395 set_bit(KVM_REQ_PTC_G, &vcpu->requests);
396 if (vcpu->arch.ptc_g_count < MAX_PTC_G_NUM) {
397 vcpu->arch.ptc_g_data[vcpu->arch.ptc_g_count++] =
398 p->ptc_g_data;
399 } else {
400 clear_bit(KVM_REQ_PTC_G, &vcpu->requests);
401 vcpu->arch.ptc_g_count = 0;
402 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
403 }
404 }
405
406 static int handle_global_purge(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
407 {
408 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
409 struct kvm *kvm = vcpu->kvm;
410 struct call_data call_data;
411 int i;
412
413 call_data.ptc_g_data = p->u.ptc_g_data;
414
415 for (i = 0; i < kvm->arch.online_vcpus; i++) {
416 if (!kvm->vcpus[i] || kvm->vcpus[i]->arch.mp_state ==
417 KVM_MP_STATE_UNINITIALIZED ||
418 vcpu == kvm->vcpus[i])
419 continue;
420
421 if (waitqueue_active(&kvm->vcpus[i]->wq))
422 wake_up_interruptible(&kvm->vcpus[i]->wq);
423
424 if (kvm->vcpus[i]->cpu != -1) {
425 call_data.vcpu = kvm->vcpus[i];
426 smp_call_function_single(kvm->vcpus[i]->cpu,
427 vcpu_global_purge, &call_data, 1);
428 } else
429 printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n");
430
431 }
432 return 1;
433 }
434
435 static int handle_switch_rr6(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
436 {
437 return 1;
438 }
439
440 static int kvm_sn2_setup_mappings(struct kvm_vcpu *vcpu)
441 {
442 unsigned long pte, rtc_phys_addr, map_addr;
443 int slot;
444
445 map_addr = KVM_VMM_BASE + (1UL << KVM_VMM_SHIFT);
446 rtc_phys_addr = LOCAL_MMR_OFFSET | SH_RTC;
447 pte = pte_val(mk_pte_phys(rtc_phys_addr, PAGE_KERNEL_UC));
448 slot = ia64_itr_entry(0x3, map_addr, pte, PAGE_SHIFT);
449 vcpu->arch.sn_rtc_tr_slot = slot;
450 if (slot < 0) {
451 printk(KERN_ERR "Mayday mayday! RTC mapping failed!\n");
452 slot = 0;
453 }
454 return slot;
455 }
456
457 int kvm_emulate_halt(struct kvm_vcpu *vcpu)
458 {
459
460 ktime_t kt;
461 long itc_diff;
462 unsigned long vcpu_now_itc;
463 unsigned long expires;
464 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
465 unsigned long cyc_per_usec = local_cpu_data->cyc_per_usec;
466 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
467
468 if (irqchip_in_kernel(vcpu->kvm)) {
469
470 vcpu_now_itc = kvm_get_itc(vcpu) + vcpu->arch.itc_offset;
471
472 if (time_after(vcpu_now_itc, vpd->itm)) {
473 vcpu->arch.timer_check = 1;
474 return 1;
475 }
476 itc_diff = vpd->itm - vcpu_now_itc;
477 if (itc_diff < 0)
478 itc_diff = -itc_diff;
479
480 expires = div64_u64(itc_diff, cyc_per_usec);
481 kt = ktime_set(0, 1000 * expires);
482
483 vcpu->arch.ht_active = 1;
484 hrtimer_start(p_ht, kt, HRTIMER_MODE_ABS);
485
486 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
487 kvm_vcpu_block(vcpu);
488 hrtimer_cancel(p_ht);
489 vcpu->arch.ht_active = 0;
490
491 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests) ||
492 kvm_cpu_has_pending_timer(vcpu))
493 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
494 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
495
496 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
497 return -EINTR;
498 return 1;
499 } else {
500 printk(KERN_ERR"kvm: Unsupported userspace halt!");
501 return 0;
502 }
503 }
504
505 static int handle_vm_shutdown(struct kvm_vcpu *vcpu,
506 struct kvm_run *kvm_run)
507 {
508 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
509 return 0;
510 }
511
512 static int handle_external_interrupt(struct kvm_vcpu *vcpu,
513 struct kvm_run *kvm_run)
514 {
515 return 1;
516 }
517
518 static int handle_vcpu_debug(struct kvm_vcpu *vcpu,
519 struct kvm_run *kvm_run)
520 {
521 printk("VMM: %s", vcpu->arch.log_buf);
522 return 1;
523 }
524
525 static int (*kvm_vti_exit_handlers[])(struct kvm_vcpu *vcpu,
526 struct kvm_run *kvm_run) = {
527 [EXIT_REASON_VM_PANIC] = handle_vm_error,
528 [EXIT_REASON_MMIO_INSTRUCTION] = handle_mmio,
529 [EXIT_REASON_PAL_CALL] = handle_pal_call,
530 [EXIT_REASON_SAL_CALL] = handle_sal_call,
531 [EXIT_REASON_SWITCH_RR6] = handle_switch_rr6,
532 [EXIT_REASON_VM_DESTROY] = handle_vm_shutdown,
533 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
534 [EXIT_REASON_IPI] = handle_ipi,
535 [EXIT_REASON_PTC_G] = handle_global_purge,
536 [EXIT_REASON_DEBUG] = handle_vcpu_debug,
537
538 };
539
540 static const int kvm_vti_max_exit_handlers =
541 sizeof(kvm_vti_exit_handlers)/sizeof(*kvm_vti_exit_handlers);
542
543 static uint32_t kvm_get_exit_reason(struct kvm_vcpu *vcpu)
544 {
545 struct exit_ctl_data *p_exit_data;
546
547 p_exit_data = kvm_get_exit_data(vcpu);
548 return p_exit_data->exit_reason;
549 }
550
551 /*
552 * The guest has exited. See if we can fix it or if we need userspace
553 * assistance.
554 */
555 static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
556 {
557 u32 exit_reason = kvm_get_exit_reason(vcpu);
558 vcpu->arch.last_exit = exit_reason;
559
560 if (exit_reason < kvm_vti_max_exit_handlers
561 && kvm_vti_exit_handlers[exit_reason])
562 return kvm_vti_exit_handlers[exit_reason](vcpu, kvm_run);
563 else {
564 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
565 kvm_run->hw.hardware_exit_reason = exit_reason;
566 }
567 return 0;
568 }
569
570 static inline void vti_set_rr6(unsigned long rr6)
571 {
572 ia64_set_rr(RR6, rr6);
573 ia64_srlz_i();
574 }
575
576 static int kvm_insert_vmm_mapping(struct kvm_vcpu *vcpu)
577 {
578 unsigned long pte;
579 struct kvm *kvm = vcpu->kvm;
580 int r;
581
582 /*Insert a pair of tr to map vmm*/
583 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
584 r = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
585 if (r < 0)
586 goto out;
587 vcpu->arch.vmm_tr_slot = r;
588 /*Insert a pairt of tr to map data of vm*/
589 pte = pte_val(mk_pte_phys(__pa(kvm->arch.vm_base), PAGE_KERNEL));
590 r = ia64_itr_entry(0x3, KVM_VM_DATA_BASE,
591 pte, KVM_VM_DATA_SHIFT);
592 if (r < 0)
593 goto out;
594 vcpu->arch.vm_tr_slot = r;
595
596 #if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
597 if (kvm->arch.is_sn2) {
598 r = kvm_sn2_setup_mappings(vcpu);
599 if (r < 0)
600 goto out;
601 }
602 #endif
603
604 r = 0;
605 out:
606 return r;
607 }
608
609 static void kvm_purge_vmm_mapping(struct kvm_vcpu *vcpu)
610 {
611 struct kvm *kvm = vcpu->kvm;
612 ia64_ptr_entry(0x3, vcpu->arch.vmm_tr_slot);
613 ia64_ptr_entry(0x3, vcpu->arch.vm_tr_slot);
614 #if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
615 if (kvm->arch.is_sn2)
616 ia64_ptr_entry(0x3, vcpu->arch.sn_rtc_tr_slot);
617 #endif
618 }
619
620 static int kvm_vcpu_pre_transition(struct kvm_vcpu *vcpu)
621 {
622 unsigned long psr;
623 int r;
624 int cpu = smp_processor_id();
625
626 if (vcpu->arch.last_run_cpu != cpu ||
627 per_cpu(last_vcpu, cpu) != vcpu) {
628 per_cpu(last_vcpu, cpu) = vcpu;
629 vcpu->arch.last_run_cpu = cpu;
630 kvm_flush_tlb_all();
631 }
632
633 vcpu->arch.host_rr6 = ia64_get_rr(RR6);
634 vti_set_rr6(vcpu->arch.vmm_rr);
635 local_irq_save(psr);
636 r = kvm_insert_vmm_mapping(vcpu);
637 local_irq_restore(psr);
638 return r;
639 }
640
641 static void kvm_vcpu_post_transition(struct kvm_vcpu *vcpu)
642 {
643 kvm_purge_vmm_mapping(vcpu);
644 vti_set_rr6(vcpu->arch.host_rr6);
645 }
646
647 static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
648 {
649 union context *host_ctx, *guest_ctx;
650 int r;
651
652 /*
653 * down_read() may sleep and return with interrupts enabled
654 */
655 down_read(&vcpu->kvm->slots_lock);
656
657 again:
658 if (signal_pending(current)) {
659 r = -EINTR;
660 kvm_run->exit_reason = KVM_EXIT_INTR;
661 goto out;
662 }
663
664 preempt_disable();
665 local_irq_disable();
666
667 /*Get host and guest context with guest address space.*/
668 host_ctx = kvm_get_host_context(vcpu);
669 guest_ctx = kvm_get_guest_context(vcpu);
670
671 vcpu->guest_mode = 1;
672
673 r = kvm_vcpu_pre_transition(vcpu);
674 if (r < 0)
675 goto vcpu_run_fail;
676
677 up_read(&vcpu->kvm->slots_lock);
678 kvm_guest_enter();
679
680 /*
681 * Transition to the guest
682 */
683 kvm_vmm_info->tramp_entry(host_ctx, guest_ctx);
684
685 kvm_vcpu_post_transition(vcpu);
686
687 vcpu->arch.launched = 1;
688 vcpu->guest_mode = 0;
689 local_irq_enable();
690
691 /*
692 * We must have an instruction between local_irq_enable() and
693 * kvm_guest_exit(), so the timer interrupt isn't delayed by
694 * the interrupt shadow. The stat.exits increment will do nicely.
695 * But we need to prevent reordering, hence this barrier():
696 */
697 barrier();
698 kvm_guest_exit();
699 preempt_enable();
700
701 down_read(&vcpu->kvm->slots_lock);
702
703 r = kvm_handle_exit(kvm_run, vcpu);
704
705 if (r > 0) {
706 if (!need_resched())
707 goto again;
708 }
709
710 out:
711 up_read(&vcpu->kvm->slots_lock);
712 if (r > 0) {
713 kvm_resched(vcpu);
714 down_read(&vcpu->kvm->slots_lock);
715 goto again;
716 }
717
718 return r;
719
720 vcpu_run_fail:
721 local_irq_enable();
722 preempt_enable();
723 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
724 goto out;
725 }
726
727 static void kvm_set_mmio_data(struct kvm_vcpu *vcpu)
728 {
729 struct kvm_mmio_req *p = kvm_get_vcpu_ioreq(vcpu);
730
731 if (!vcpu->mmio_is_write)
732 memcpy(&p->data, vcpu->mmio_data, 8);
733 p->state = STATE_IORESP_READY;
734 }
735
736 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
737 {
738 int r;
739 sigset_t sigsaved;
740
741 vcpu_load(vcpu);
742
743 if (vcpu->sigset_active)
744 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
745
746 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
747 kvm_vcpu_block(vcpu);
748 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
749 r = -EAGAIN;
750 goto out;
751 }
752
753 if (vcpu->mmio_needed) {
754 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
755 kvm_set_mmio_data(vcpu);
756 vcpu->mmio_read_completed = 1;
757 vcpu->mmio_needed = 0;
758 }
759 r = __vcpu_run(vcpu, kvm_run);
760 out:
761 if (vcpu->sigset_active)
762 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
763
764 vcpu_put(vcpu);
765 return r;
766 }
767
768 static struct kvm *kvm_alloc_kvm(void)
769 {
770
771 struct kvm *kvm;
772 uint64_t vm_base;
773
774 BUG_ON(sizeof(struct kvm) > KVM_VM_STRUCT_SIZE);
775
776 vm_base = __get_free_pages(GFP_KERNEL, get_order(KVM_VM_DATA_SIZE));
777
778 if (!vm_base)
779 return ERR_PTR(-ENOMEM);
780
781 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
782 kvm = (struct kvm *)(vm_base +
783 offsetof(struct kvm_vm_data, kvm_vm_struct));
784 kvm->arch.vm_base = vm_base;
785 printk(KERN_DEBUG"kvm: vm's data area:0x%lx\n", vm_base);
786
787 return kvm;
788 }
789
790 struct kvm_io_range {
791 unsigned long start;
792 unsigned long size;
793 unsigned long type;
794 };
795
796 static const struct kvm_io_range io_ranges[] = {
797 {VGA_IO_START, VGA_IO_SIZE, GPFN_FRAME_BUFFER},
798 {MMIO_START, MMIO_SIZE, GPFN_LOW_MMIO},
799 {LEGACY_IO_START, LEGACY_IO_SIZE, GPFN_LEGACY_IO},
800 {IO_SAPIC_START, IO_SAPIC_SIZE, GPFN_IOSAPIC},
801 {PIB_START, PIB_SIZE, GPFN_PIB},
802 };
803
804 static void kvm_build_io_pmt(struct kvm *kvm)
805 {
806 unsigned long i, j;
807
808 /* Mark I/O ranges */
809 for (i = 0; i < (sizeof(io_ranges) / sizeof(struct kvm_io_range));
810 i++) {
811 for (j = io_ranges[i].start;
812 j < io_ranges[i].start + io_ranges[i].size;
813 j += PAGE_SIZE)
814 kvm_set_pmt_entry(kvm, j >> PAGE_SHIFT,
815 io_ranges[i].type, 0);
816 }
817
818 }
819
820 /*Use unused rids to virtualize guest rid.*/
821 #define GUEST_PHYSICAL_RR0 0x1739
822 #define GUEST_PHYSICAL_RR4 0x2739
823 #define VMM_INIT_RR 0x1660
824
825 static void kvm_init_vm(struct kvm *kvm)
826 {
827 BUG_ON(!kvm);
828
829 kvm->arch.metaphysical_rr0 = GUEST_PHYSICAL_RR0;
830 kvm->arch.metaphysical_rr4 = GUEST_PHYSICAL_RR4;
831 kvm->arch.vmm_init_rr = VMM_INIT_RR;
832
833 /*
834 *Fill P2M entries for MMIO/IO ranges
835 */
836 kvm_build_io_pmt(kvm);
837
838 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
839
840 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
841 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
842 }
843
844 struct kvm *kvm_arch_create_vm(void)
845 {
846 struct kvm *kvm = kvm_alloc_kvm();
847
848 if (IS_ERR(kvm))
849 return ERR_PTR(-ENOMEM);
850
851 kvm->arch.is_sn2 = ia64_platform_is("sn2");
852
853 kvm_init_vm(kvm);
854
855 kvm->arch.online_vcpus = 0;
856
857 return kvm;
858
859 }
860
861 static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm,
862 struct kvm_irqchip *chip)
863 {
864 int r;
865
866 r = 0;
867 switch (chip->chip_id) {
868 case KVM_IRQCHIP_IOAPIC:
869 memcpy(&chip->chip.ioapic, ioapic_irqchip(kvm),
870 sizeof(struct kvm_ioapic_state));
871 break;
872 default:
873 r = -EINVAL;
874 break;
875 }
876 return r;
877 }
878
879 static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
880 {
881 int r;
882
883 r = 0;
884 switch (chip->chip_id) {
885 case KVM_IRQCHIP_IOAPIC:
886 memcpy(ioapic_irqchip(kvm),
887 &chip->chip.ioapic,
888 sizeof(struct kvm_ioapic_state));
889 break;
890 default:
891 r = -EINVAL;
892 break;
893 }
894 return r;
895 }
896
897 #define RESTORE_REGS(_x) vcpu->arch._x = regs->_x
898
899 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
900 {
901 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
902 int i;
903
904 vcpu_load(vcpu);
905
906 for (i = 0; i < 16; i++) {
907 vpd->vgr[i] = regs->vpd.vgr[i];
908 vpd->vbgr[i] = regs->vpd.vbgr[i];
909 }
910 for (i = 0; i < 128; i++)
911 vpd->vcr[i] = regs->vpd.vcr[i];
912 vpd->vhpi = regs->vpd.vhpi;
913 vpd->vnat = regs->vpd.vnat;
914 vpd->vbnat = regs->vpd.vbnat;
915 vpd->vpsr = regs->vpd.vpsr;
916
917 vpd->vpr = regs->vpd.vpr;
918
919 memcpy(&vcpu->arch.guest, &regs->saved_guest, sizeof(union context));
920
921 RESTORE_REGS(mp_state);
922 RESTORE_REGS(vmm_rr);
923 memcpy(vcpu->arch.itrs, regs->itrs, sizeof(struct thash_data) * NITRS);
924 memcpy(vcpu->arch.dtrs, regs->dtrs, sizeof(struct thash_data) * NDTRS);
925 RESTORE_REGS(itr_regions);
926 RESTORE_REGS(dtr_regions);
927 RESTORE_REGS(tc_regions);
928 RESTORE_REGS(irq_check);
929 RESTORE_REGS(itc_check);
930 RESTORE_REGS(timer_check);
931 RESTORE_REGS(timer_pending);
932 RESTORE_REGS(last_itc);
933 for (i = 0; i < 8; i++) {
934 vcpu->arch.vrr[i] = regs->vrr[i];
935 vcpu->arch.ibr[i] = regs->ibr[i];
936 vcpu->arch.dbr[i] = regs->dbr[i];
937 }
938 for (i = 0; i < 4; i++)
939 vcpu->arch.insvc[i] = regs->insvc[i];
940 RESTORE_REGS(xtp);
941 RESTORE_REGS(metaphysical_rr0);
942 RESTORE_REGS(metaphysical_rr4);
943 RESTORE_REGS(metaphysical_saved_rr0);
944 RESTORE_REGS(metaphysical_saved_rr4);
945 RESTORE_REGS(fp_psr);
946 RESTORE_REGS(saved_gp);
947
948 vcpu->arch.irq_new_pending = 1;
949 vcpu->arch.itc_offset = regs->saved_itc - kvm_get_itc(vcpu);
950 set_bit(KVM_REQ_RESUME, &vcpu->requests);
951
952 vcpu_put(vcpu);
953
954 return 0;
955 }
956
957 long kvm_arch_vm_ioctl(struct file *filp,
958 unsigned int ioctl, unsigned long arg)
959 {
960 struct kvm *kvm = filp->private_data;
961 void __user *argp = (void __user *)arg;
962 int r = -EINVAL;
963
964 switch (ioctl) {
965 case KVM_SET_MEMORY_REGION: {
966 struct kvm_memory_region kvm_mem;
967 struct kvm_userspace_memory_region kvm_userspace_mem;
968
969 r = -EFAULT;
970 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
971 goto out;
972 kvm_userspace_mem.slot = kvm_mem.slot;
973 kvm_userspace_mem.flags = kvm_mem.flags;
974 kvm_userspace_mem.guest_phys_addr =
975 kvm_mem.guest_phys_addr;
976 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
977 r = kvm_vm_ioctl_set_memory_region(kvm,
978 &kvm_userspace_mem, 0);
979 if (r)
980 goto out;
981 break;
982 }
983 case KVM_CREATE_IRQCHIP:
984 r = -EFAULT;
985 r = kvm_ioapic_init(kvm);
986 if (r)
987 goto out;
988 r = kvm_setup_default_irq_routing(kvm);
989 if (r) {
990 kfree(kvm->arch.vioapic);
991 goto out;
992 }
993 break;
994 case KVM_IRQ_LINE_STATUS:
995 case KVM_IRQ_LINE: {
996 struct kvm_irq_level irq_event;
997
998 r = -EFAULT;
999 if (copy_from_user(&irq_event, argp, sizeof irq_event))
1000 goto out;
1001 if (irqchip_in_kernel(kvm)) {
1002 __s32 status;
1003 mutex_lock(&kvm->lock);
1004 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
1005 irq_event.irq, irq_event.level);
1006 mutex_unlock(&kvm->lock);
1007 if (ioctl == KVM_IRQ_LINE_STATUS) {
1008 irq_event.status = status;
1009 if (copy_to_user(argp, &irq_event,
1010 sizeof irq_event))
1011 goto out;
1012 }
1013 r = 0;
1014 }
1015 break;
1016 }
1017 case KVM_GET_IRQCHIP: {
1018 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1019 struct kvm_irqchip chip;
1020
1021 r = -EFAULT;
1022 if (copy_from_user(&chip, argp, sizeof chip))
1023 goto out;
1024 r = -ENXIO;
1025 if (!irqchip_in_kernel(kvm))
1026 goto out;
1027 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
1028 if (r)
1029 goto out;
1030 r = -EFAULT;
1031 if (copy_to_user(argp, &chip, sizeof chip))
1032 goto out;
1033 r = 0;
1034 break;
1035 }
1036 case KVM_SET_IRQCHIP: {
1037 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1038 struct kvm_irqchip chip;
1039
1040 r = -EFAULT;
1041 if (copy_from_user(&chip, argp, sizeof chip))
1042 goto out;
1043 r = -ENXIO;
1044 if (!irqchip_in_kernel(kvm))
1045 goto out;
1046 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1047 if (r)
1048 goto out;
1049 r = 0;
1050 break;
1051 }
1052 default:
1053 ;
1054 }
1055 out:
1056 return r;
1057 }
1058
1059 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1060 struct kvm_sregs *sregs)
1061 {
1062 return -EINVAL;
1063 }
1064
1065 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1066 struct kvm_sregs *sregs)
1067 {
1068 return -EINVAL;
1069
1070 }
1071 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1072 struct kvm_translation *tr)
1073 {
1074
1075 return -EINVAL;
1076 }
1077
1078 static int kvm_alloc_vmm_area(void)
1079 {
1080 if (!kvm_vmm_base && (kvm_vm_buffer_size < KVM_VM_BUFFER_SIZE)) {
1081 kvm_vmm_base = __get_free_pages(GFP_KERNEL,
1082 get_order(KVM_VMM_SIZE));
1083 if (!kvm_vmm_base)
1084 return -ENOMEM;
1085
1086 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1087 kvm_vm_buffer = kvm_vmm_base + VMM_SIZE;
1088
1089 printk(KERN_DEBUG"kvm:VMM's Base Addr:0x%lx, vm_buffer:0x%lx\n",
1090 kvm_vmm_base, kvm_vm_buffer);
1091 }
1092
1093 return 0;
1094 }
1095
1096 static void kvm_free_vmm_area(void)
1097 {
1098 if (kvm_vmm_base) {
1099 /*Zero this area before free to avoid bits leak!!*/
1100 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1101 free_pages(kvm_vmm_base, get_order(KVM_VMM_SIZE));
1102 kvm_vmm_base = 0;
1103 kvm_vm_buffer = 0;
1104 kvm_vsa_base = 0;
1105 }
1106 }
1107
1108 static int vti_init_vpd(struct kvm_vcpu *vcpu)
1109 {
1110 int i;
1111 union cpuid3_t cpuid3;
1112 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1113
1114 if (IS_ERR(vpd))
1115 return PTR_ERR(vpd);
1116
1117 /* CPUID init */
1118 for (i = 0; i < 5; i++)
1119 vpd->vcpuid[i] = ia64_get_cpuid(i);
1120
1121 /* Limit the CPUID number to 5 */
1122 cpuid3.value = vpd->vcpuid[3];
1123 cpuid3.number = 4; /* 5 - 1 */
1124 vpd->vcpuid[3] = cpuid3.value;
1125
1126 /*Set vac and vdc fields*/
1127 vpd->vac.a_from_int_cr = 1;
1128 vpd->vac.a_to_int_cr = 1;
1129 vpd->vac.a_from_psr = 1;
1130 vpd->vac.a_from_cpuid = 1;
1131 vpd->vac.a_cover = 1;
1132 vpd->vac.a_bsw = 1;
1133 vpd->vac.a_int = 1;
1134 vpd->vdc.d_vmsw = 1;
1135
1136 /*Set virtual buffer*/
1137 vpd->virt_env_vaddr = KVM_VM_BUFFER_BASE;
1138
1139 return 0;
1140 }
1141
1142 static int vti_create_vp(struct kvm_vcpu *vcpu)
1143 {
1144 long ret;
1145 struct vpd *vpd = vcpu->arch.vpd;
1146 unsigned long vmm_ivt;
1147
1148 vmm_ivt = kvm_vmm_info->vmm_ivt;
1149
1150 printk(KERN_DEBUG "kvm: vcpu:%p,ivt: 0x%lx\n", vcpu, vmm_ivt);
1151
1152 ret = ia64_pal_vp_create((u64 *)vpd, (u64 *)vmm_ivt, 0);
1153
1154 if (ret) {
1155 printk(KERN_ERR"kvm: ia64_pal_vp_create failed!\n");
1156 return -EINVAL;
1157 }
1158 return 0;
1159 }
1160
1161 static void init_ptce_info(struct kvm_vcpu *vcpu)
1162 {
1163 ia64_ptce_info_t ptce = {0};
1164
1165 ia64_get_ptce(&ptce);
1166 vcpu->arch.ptce_base = ptce.base;
1167 vcpu->arch.ptce_count[0] = ptce.count[0];
1168 vcpu->arch.ptce_count[1] = ptce.count[1];
1169 vcpu->arch.ptce_stride[0] = ptce.stride[0];
1170 vcpu->arch.ptce_stride[1] = ptce.stride[1];
1171 }
1172
1173 static void kvm_migrate_hlt_timer(struct kvm_vcpu *vcpu)
1174 {
1175 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
1176
1177 if (hrtimer_cancel(p_ht))
1178 hrtimer_start_expires(p_ht, HRTIMER_MODE_ABS);
1179 }
1180
1181 static enum hrtimer_restart hlt_timer_fn(struct hrtimer *data)
1182 {
1183 struct kvm_vcpu *vcpu;
1184 wait_queue_head_t *q;
1185
1186 vcpu = container_of(data, struct kvm_vcpu, arch.hlt_timer);
1187 q = &vcpu->wq;
1188
1189 if (vcpu->arch.mp_state != KVM_MP_STATE_HALTED)
1190 goto out;
1191
1192 if (waitqueue_active(q))
1193 wake_up_interruptible(q);
1194
1195 out:
1196 vcpu->arch.timer_fired = 1;
1197 vcpu->arch.timer_check = 1;
1198 return HRTIMER_NORESTART;
1199 }
1200
1201 #define PALE_RESET_ENTRY 0x80000000ffffffb0UL
1202
1203 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1204 {
1205 struct kvm_vcpu *v;
1206 int r;
1207 int i;
1208 long itc_offset;
1209 struct kvm *kvm = vcpu->kvm;
1210 struct kvm_pt_regs *regs = vcpu_regs(vcpu);
1211
1212 union context *p_ctx = &vcpu->arch.guest;
1213 struct kvm_vcpu *vmm_vcpu = to_guest(vcpu->kvm, vcpu);
1214
1215 /*Init vcpu context for first run.*/
1216 if (IS_ERR(vmm_vcpu))
1217 return PTR_ERR(vmm_vcpu);
1218
1219 if (vcpu->vcpu_id == 0) {
1220 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
1221
1222 /*Set entry address for first run.*/
1223 regs->cr_iip = PALE_RESET_ENTRY;
1224
1225 /*Initialize itc offset for vcpus*/
1226 itc_offset = 0UL - kvm_get_itc(vcpu);
1227 for (i = 0; i < kvm->arch.online_vcpus; i++) {
1228 v = (struct kvm_vcpu *)((char *)vcpu +
1229 sizeof(struct kvm_vcpu_data) * i);
1230 v->arch.itc_offset = itc_offset;
1231 v->arch.last_itc = 0;
1232 }
1233 } else
1234 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
1235
1236 r = -ENOMEM;
1237 vcpu->arch.apic = kzalloc(sizeof(struct kvm_lapic), GFP_KERNEL);
1238 if (!vcpu->arch.apic)
1239 goto out;
1240 vcpu->arch.apic->vcpu = vcpu;
1241
1242 p_ctx->gr[1] = 0;
1243 p_ctx->gr[12] = (unsigned long)((char *)vmm_vcpu + KVM_STK_OFFSET);
1244 p_ctx->gr[13] = (unsigned long)vmm_vcpu;
1245 p_ctx->psr = 0x1008522000UL;
1246 p_ctx->ar[40] = FPSR_DEFAULT; /*fpsr*/
1247 p_ctx->caller_unat = 0;
1248 p_ctx->pr = 0x0;
1249 p_ctx->ar[36] = 0x0; /*unat*/
1250 p_ctx->ar[19] = 0x0; /*rnat*/
1251 p_ctx->ar[18] = (unsigned long)vmm_vcpu +
1252 ((sizeof(struct kvm_vcpu)+15) & ~15);
1253 p_ctx->ar[64] = 0x0; /*pfs*/
1254 p_ctx->cr[0] = 0x7e04UL;
1255 p_ctx->cr[2] = (unsigned long)kvm_vmm_info->vmm_ivt;
1256 p_ctx->cr[8] = 0x3c;
1257
1258 /*Initilize region register*/
1259 p_ctx->rr[0] = 0x30;
1260 p_ctx->rr[1] = 0x30;
1261 p_ctx->rr[2] = 0x30;
1262 p_ctx->rr[3] = 0x30;
1263 p_ctx->rr[4] = 0x30;
1264 p_ctx->rr[5] = 0x30;
1265 p_ctx->rr[7] = 0x30;
1266
1267 /*Initilize branch register 0*/
1268 p_ctx->br[0] = *(unsigned long *)kvm_vmm_info->vmm_entry;
1269
1270 vcpu->arch.vmm_rr = kvm->arch.vmm_init_rr;
1271 vcpu->arch.metaphysical_rr0 = kvm->arch.metaphysical_rr0;
1272 vcpu->arch.metaphysical_rr4 = kvm->arch.metaphysical_rr4;
1273
1274 hrtimer_init(&vcpu->arch.hlt_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1275 vcpu->arch.hlt_timer.function = hlt_timer_fn;
1276
1277 vcpu->arch.last_run_cpu = -1;
1278 vcpu->arch.vpd = (struct vpd *)VPD_BASE(vcpu->vcpu_id);
1279 vcpu->arch.vsa_base = kvm_vsa_base;
1280 vcpu->arch.__gp = kvm_vmm_gp;
1281 vcpu->arch.dirty_log_lock_pa = __pa(&kvm->arch.dirty_log_lock);
1282 vcpu->arch.vhpt.hash = (struct thash_data *)VHPT_BASE(vcpu->vcpu_id);
1283 vcpu->arch.vtlb.hash = (struct thash_data *)VTLB_BASE(vcpu->vcpu_id);
1284 init_ptce_info(vcpu);
1285
1286 r = 0;
1287 out:
1288 return r;
1289 }
1290
1291 static int vti_vcpu_setup(struct kvm_vcpu *vcpu, int id)
1292 {
1293 unsigned long psr;
1294 int r;
1295
1296 local_irq_save(psr);
1297 r = kvm_insert_vmm_mapping(vcpu);
1298 local_irq_restore(psr);
1299 if (r)
1300 goto fail;
1301 r = kvm_vcpu_init(vcpu, vcpu->kvm, id);
1302 if (r)
1303 goto fail;
1304
1305 r = vti_init_vpd(vcpu);
1306 if (r) {
1307 printk(KERN_DEBUG"kvm: vpd init error!!\n");
1308 goto uninit;
1309 }
1310
1311 r = vti_create_vp(vcpu);
1312 if (r)
1313 goto uninit;
1314
1315 kvm_purge_vmm_mapping(vcpu);
1316
1317 return 0;
1318 uninit:
1319 kvm_vcpu_uninit(vcpu);
1320 fail:
1321 return r;
1322 }
1323
1324 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1325 unsigned int id)
1326 {
1327 struct kvm_vcpu *vcpu;
1328 unsigned long vm_base = kvm->arch.vm_base;
1329 int r;
1330 int cpu;
1331
1332 BUG_ON(sizeof(struct kvm_vcpu) > VCPU_STRUCT_SIZE/2);
1333
1334 r = -EINVAL;
1335 if (id >= KVM_MAX_VCPUS) {
1336 printk(KERN_ERR"kvm: Can't configure vcpus > %ld",
1337 KVM_MAX_VCPUS);
1338 goto fail;
1339 }
1340
1341 r = -ENOMEM;
1342 if (!vm_base) {
1343 printk(KERN_ERR"kvm: Create vcpu[%d] error!\n", id);
1344 goto fail;
1345 }
1346 vcpu = (struct kvm_vcpu *)(vm_base + offsetof(struct kvm_vm_data,
1347 vcpu_data[id].vcpu_struct));
1348 vcpu->kvm = kvm;
1349
1350 cpu = get_cpu();
1351 r = vti_vcpu_setup(vcpu, id);
1352 put_cpu();
1353
1354 if (r) {
1355 printk(KERN_DEBUG"kvm: vcpu_setup error!!\n");
1356 goto fail;
1357 }
1358
1359 kvm->arch.online_vcpus++;
1360
1361 return vcpu;
1362 fail:
1363 return ERR_PTR(r);
1364 }
1365
1366 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1367 {
1368 return 0;
1369 }
1370
1371 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1372 {
1373 return -EINVAL;
1374 }
1375
1376 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1377 {
1378 return -EINVAL;
1379 }
1380
1381 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1382 struct kvm_guest_debug *dbg)
1383 {
1384 return -EINVAL;
1385 }
1386
1387 static void free_kvm(struct kvm *kvm)
1388 {
1389 unsigned long vm_base = kvm->arch.vm_base;
1390
1391 if (vm_base) {
1392 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
1393 free_pages(vm_base, get_order(KVM_VM_DATA_SIZE));
1394 }
1395
1396 }
1397
1398 static void kvm_release_vm_pages(struct kvm *kvm)
1399 {
1400 struct kvm_memory_slot *memslot;
1401 int i, j;
1402 unsigned long base_gfn;
1403
1404 for (i = 0; i < kvm->nmemslots; i++) {
1405 memslot = &kvm->memslots[i];
1406 base_gfn = memslot->base_gfn;
1407
1408 for (j = 0; j < memslot->npages; j++) {
1409 if (memslot->rmap[j])
1410 put_page((struct page *)memslot->rmap[j]);
1411 }
1412 }
1413 }
1414
1415 void kvm_arch_sync_events(struct kvm *kvm)
1416 {
1417 }
1418
1419 void kvm_arch_destroy_vm(struct kvm *kvm)
1420 {
1421 kvm_iommu_unmap_guest(kvm);
1422 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
1423 kvm_free_all_assigned_devices(kvm);
1424 #endif
1425 kfree(kvm->arch.vioapic);
1426 kvm_release_vm_pages(kvm);
1427 kvm_free_physmem(kvm);
1428 free_kvm(kvm);
1429 }
1430
1431 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1432 {
1433 }
1434
1435 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1436 {
1437 if (cpu != vcpu->cpu) {
1438 vcpu->cpu = cpu;
1439 if (vcpu->arch.ht_active)
1440 kvm_migrate_hlt_timer(vcpu);
1441 }
1442 }
1443
1444 #define SAVE_REGS(_x) regs->_x = vcpu->arch._x
1445
1446 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1447 {
1448 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1449 int i;
1450
1451 vcpu_load(vcpu);
1452
1453 for (i = 0; i < 16; i++) {
1454 regs->vpd.vgr[i] = vpd->vgr[i];
1455 regs->vpd.vbgr[i] = vpd->vbgr[i];
1456 }
1457 for (i = 0; i < 128; i++)
1458 regs->vpd.vcr[i] = vpd->vcr[i];
1459 regs->vpd.vhpi = vpd->vhpi;
1460 regs->vpd.vnat = vpd->vnat;
1461 regs->vpd.vbnat = vpd->vbnat;
1462 regs->vpd.vpsr = vpd->vpsr;
1463 regs->vpd.vpr = vpd->vpr;
1464
1465 memcpy(&regs->saved_guest, &vcpu->arch.guest, sizeof(union context));
1466
1467 SAVE_REGS(mp_state);
1468 SAVE_REGS(vmm_rr);
1469 memcpy(regs->itrs, vcpu->arch.itrs, sizeof(struct thash_data) * NITRS);
1470 memcpy(regs->dtrs, vcpu->arch.dtrs, sizeof(struct thash_data) * NDTRS);
1471 SAVE_REGS(itr_regions);
1472 SAVE_REGS(dtr_regions);
1473 SAVE_REGS(tc_regions);
1474 SAVE_REGS(irq_check);
1475 SAVE_REGS(itc_check);
1476 SAVE_REGS(timer_check);
1477 SAVE_REGS(timer_pending);
1478 SAVE_REGS(last_itc);
1479 for (i = 0; i < 8; i++) {
1480 regs->vrr[i] = vcpu->arch.vrr[i];
1481 regs->ibr[i] = vcpu->arch.ibr[i];
1482 regs->dbr[i] = vcpu->arch.dbr[i];
1483 }
1484 for (i = 0; i < 4; i++)
1485 regs->insvc[i] = vcpu->arch.insvc[i];
1486 regs->saved_itc = vcpu->arch.itc_offset + kvm_get_itc(vcpu);
1487 SAVE_REGS(xtp);
1488 SAVE_REGS(metaphysical_rr0);
1489 SAVE_REGS(metaphysical_rr4);
1490 SAVE_REGS(metaphysical_saved_rr0);
1491 SAVE_REGS(metaphysical_saved_rr4);
1492 SAVE_REGS(fp_psr);
1493 SAVE_REGS(saved_gp);
1494
1495 vcpu_put(vcpu);
1496 return 0;
1497 }
1498
1499 int kvm_arch_vcpu_ioctl_get_stack(struct kvm_vcpu *vcpu,
1500 struct kvm_ia64_vcpu_stack *stack)
1501 {
1502 memcpy(stack, vcpu, sizeof(struct kvm_ia64_vcpu_stack));
1503 return 0;
1504 }
1505
1506 int kvm_arch_vcpu_ioctl_set_stack(struct kvm_vcpu *vcpu,
1507 struct kvm_ia64_vcpu_stack *stack)
1508 {
1509 memcpy(vcpu + 1, &stack->stack[0] + sizeof(struct kvm_vcpu),
1510 sizeof(struct kvm_ia64_vcpu_stack) - sizeof(struct kvm_vcpu));
1511
1512 vcpu->arch.exit_data = ((struct kvm_vcpu *)stack)->arch.exit_data;
1513 return 0;
1514 }
1515
1516 void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1517 {
1518
1519 hrtimer_cancel(&vcpu->arch.hlt_timer);
1520 kfree(vcpu->arch.apic);
1521 }
1522
1523
1524 long kvm_arch_vcpu_ioctl(struct file *filp,
1525 unsigned int ioctl, unsigned long arg)
1526 {
1527 struct kvm_vcpu *vcpu = filp->private_data;
1528 void __user *argp = (void __user *)arg;
1529 struct kvm_ia64_vcpu_stack *stack = NULL;
1530 long r;
1531
1532 switch (ioctl) {
1533 case KVM_IA64_VCPU_GET_STACK: {
1534 struct kvm_ia64_vcpu_stack __user *user_stack;
1535 void __user *first_p = argp;
1536
1537 r = -EFAULT;
1538 if (copy_from_user(&user_stack, first_p, sizeof(void *)))
1539 goto out;
1540
1541 if (!access_ok(VERIFY_WRITE, user_stack,
1542 sizeof(struct kvm_ia64_vcpu_stack))) {
1543 printk(KERN_INFO "KVM_IA64_VCPU_GET_STACK: "
1544 "Illegal user destination address for stack\n");
1545 goto out;
1546 }
1547 stack = kzalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL);
1548 if (!stack) {
1549 r = -ENOMEM;
1550 goto out;
1551 }
1552
1553 r = kvm_arch_vcpu_ioctl_get_stack(vcpu, stack);
1554 if (r)
1555 goto out;
1556
1557 if (copy_to_user(user_stack, stack,
1558 sizeof(struct kvm_ia64_vcpu_stack)))
1559 goto out;
1560
1561 break;
1562 }
1563 case KVM_IA64_VCPU_SET_STACK: {
1564 struct kvm_ia64_vcpu_stack __user *user_stack;
1565 void __user *first_p = argp;
1566
1567 r = -EFAULT;
1568 if (copy_from_user(&user_stack, first_p, sizeof(void *)))
1569 goto out;
1570
1571 if (!access_ok(VERIFY_READ, user_stack,
1572 sizeof(struct kvm_ia64_vcpu_stack))) {
1573 printk(KERN_INFO "KVM_IA64_VCPU_SET_STACK: "
1574 "Illegal user address for stack\n");
1575 goto out;
1576 }
1577 stack = kmalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL);
1578 if (!stack) {
1579 r = -ENOMEM;
1580 goto out;
1581 }
1582 if (copy_from_user(stack, user_stack,
1583 sizeof(struct kvm_ia64_vcpu_stack)))
1584 goto out;
1585
1586 r = kvm_arch_vcpu_ioctl_set_stack(vcpu, stack);
1587 break;
1588 }
1589
1590 default:
1591 r = -EINVAL;
1592 }
1593
1594 out:
1595 kfree(stack);
1596 return r;
1597 }
1598
1599 int kvm_arch_set_memory_region(struct kvm *kvm,
1600 struct kvm_userspace_memory_region *mem,
1601 struct kvm_memory_slot old,
1602 int user_alloc)
1603 {
1604 unsigned long i;
1605 unsigned long pfn;
1606 int npages = mem->memory_size >> PAGE_SHIFT;
1607 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
1608 unsigned long base_gfn = memslot->base_gfn;
1609
1610 if (base_gfn + npages > (KVM_MAX_MEM_SIZE >> PAGE_SHIFT))
1611 return -ENOMEM;
1612
1613 for (i = 0; i < npages; i++) {
1614 pfn = gfn_to_pfn(kvm, base_gfn + i);
1615 if (!kvm_is_mmio_pfn(pfn)) {
1616 kvm_set_pmt_entry(kvm, base_gfn + i,
1617 pfn << PAGE_SHIFT,
1618 _PAGE_AR_RWX | _PAGE_MA_WB);
1619 memslot->rmap[i] = (unsigned long)pfn_to_page(pfn);
1620 } else {
1621 kvm_set_pmt_entry(kvm, base_gfn + i,
1622 GPFN_PHYS_MMIO | (pfn << PAGE_SHIFT),
1623 _PAGE_MA_UC);
1624 memslot->rmap[i] = 0;
1625 }
1626 }
1627
1628 return 0;
1629 }
1630
1631 void kvm_arch_flush_shadow(struct kvm *kvm)
1632 {
1633 kvm_flush_remote_tlbs(kvm);
1634 }
1635
1636 long kvm_arch_dev_ioctl(struct file *filp,
1637 unsigned int ioctl, unsigned long arg)
1638 {
1639 return -EINVAL;
1640 }
1641
1642 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1643 {
1644 kvm_vcpu_uninit(vcpu);
1645 }
1646
1647 static int vti_cpu_has_kvm_support(void)
1648 {
1649 long avail = 1, status = 1, control = 1;
1650 long ret;
1651
1652 ret = ia64_pal_proc_get_features(&avail, &status, &control, 0);
1653 if (ret)
1654 goto out;
1655
1656 if (!(avail & PAL_PROC_VM_BIT))
1657 goto out;
1658
1659 printk(KERN_DEBUG"kvm: Hardware Supports VT\n");
1660
1661 ret = ia64_pal_vp_env_info(&kvm_vm_buffer_size, &vp_env_info);
1662 if (ret)
1663 goto out;
1664 printk(KERN_DEBUG"kvm: VM Buffer Size:0x%lx\n", kvm_vm_buffer_size);
1665
1666 if (!(vp_env_info & VP_OPCODE)) {
1667 printk(KERN_WARNING"kvm: No opcode ability on hardware, "
1668 "vm_env_info:0x%lx\n", vp_env_info);
1669 }
1670
1671 return 1;
1672 out:
1673 return 0;
1674 }
1675
1676
1677 /*
1678 * On SN2, the ITC isn't stable, so copy in fast path code to use the
1679 * SN2 RTC, replacing the ITC based default verion.
1680 */
1681 static void kvm_patch_vmm(struct kvm_vmm_info *vmm_info,
1682 struct module *module)
1683 {
1684 unsigned long new_ar, new_ar_sn2;
1685 unsigned long module_base;
1686
1687 if (!ia64_platform_is("sn2"))
1688 return;
1689
1690 module_base = (unsigned long)module->module_core;
1691
1692 new_ar = kvm_vmm_base + vmm_info->patch_mov_ar - module_base;
1693 new_ar_sn2 = kvm_vmm_base + vmm_info->patch_mov_ar_sn2 - module_base;
1694
1695 printk(KERN_INFO "kvm: Patching ITC emulation to use SGI SN2 RTC "
1696 "as source\n");
1697
1698 /*
1699 * Copy the SN2 version of mov_ar into place. They are both
1700 * the same size, so 6 bundles is sufficient (6 * 0x10).
1701 */
1702 memcpy((void *)new_ar, (void *)new_ar_sn2, 0x60);
1703 }
1704
1705 static int kvm_relocate_vmm(struct kvm_vmm_info *vmm_info,
1706 struct module *module)
1707 {
1708 unsigned long module_base;
1709 unsigned long vmm_size;
1710
1711 unsigned long vmm_offset, func_offset, fdesc_offset;
1712 struct fdesc *p_fdesc;
1713
1714 BUG_ON(!module);
1715
1716 if (!kvm_vmm_base) {
1717 printk("kvm: kvm area hasn't been initilized yet!!\n");
1718 return -EFAULT;
1719 }
1720
1721 /*Calculate new position of relocated vmm module.*/
1722 module_base = (unsigned long)module->module_core;
1723 vmm_size = module->core_size;
1724 if (unlikely(vmm_size > KVM_VMM_SIZE))
1725 return -EFAULT;
1726
1727 memcpy((void *)kvm_vmm_base, (void *)module_base, vmm_size);
1728 kvm_patch_vmm(vmm_info, module);
1729 kvm_flush_icache(kvm_vmm_base, vmm_size);
1730
1731 /*Recalculate kvm_vmm_info based on new VMM*/
1732 vmm_offset = vmm_info->vmm_ivt - module_base;
1733 kvm_vmm_info->vmm_ivt = KVM_VMM_BASE + vmm_offset;
1734 printk(KERN_DEBUG"kvm: Relocated VMM's IVT Base Addr:%lx\n",
1735 kvm_vmm_info->vmm_ivt);
1736
1737 fdesc_offset = (unsigned long)vmm_info->vmm_entry - module_base;
1738 kvm_vmm_info->vmm_entry = (kvm_vmm_entry *)(KVM_VMM_BASE +
1739 fdesc_offset);
1740 func_offset = *(unsigned long *)vmm_info->vmm_entry - module_base;
1741 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1742 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1743 p_fdesc->gp = KVM_VMM_BASE+(p_fdesc->gp - module_base);
1744
1745 printk(KERN_DEBUG"kvm: Relocated VMM's Init Entry Addr:%lx\n",
1746 KVM_VMM_BASE+func_offset);
1747
1748 fdesc_offset = (unsigned long)vmm_info->tramp_entry - module_base;
1749 kvm_vmm_info->tramp_entry = (kvm_tramp_entry *)(KVM_VMM_BASE +
1750 fdesc_offset);
1751 func_offset = *(unsigned long *)vmm_info->tramp_entry - module_base;
1752 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1753 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1754 p_fdesc->gp = KVM_VMM_BASE + (p_fdesc->gp - module_base);
1755
1756 kvm_vmm_gp = p_fdesc->gp;
1757
1758 printk(KERN_DEBUG"kvm: Relocated VMM's Entry IP:%p\n",
1759 kvm_vmm_info->vmm_entry);
1760 printk(KERN_DEBUG"kvm: Relocated VMM's Trampoline Entry IP:0x%lx\n",
1761 KVM_VMM_BASE + func_offset);
1762
1763 return 0;
1764 }
1765
1766 int kvm_arch_init(void *opaque)
1767 {
1768 int r;
1769 struct kvm_vmm_info *vmm_info = (struct kvm_vmm_info *)opaque;
1770
1771 if (!vti_cpu_has_kvm_support()) {
1772 printk(KERN_ERR "kvm: No Hardware Virtualization Support!\n");
1773 r = -EOPNOTSUPP;
1774 goto out;
1775 }
1776
1777 if (kvm_vmm_info) {
1778 printk(KERN_ERR "kvm: Already loaded VMM module!\n");
1779 r = -EEXIST;
1780 goto out;
1781 }
1782
1783 r = -ENOMEM;
1784 kvm_vmm_info = kzalloc(sizeof(struct kvm_vmm_info), GFP_KERNEL);
1785 if (!kvm_vmm_info)
1786 goto out;
1787
1788 if (kvm_alloc_vmm_area())
1789 goto out_free0;
1790
1791 r = kvm_relocate_vmm(vmm_info, vmm_info->module);
1792 if (r)
1793 goto out_free1;
1794
1795 return 0;
1796
1797 out_free1:
1798 kvm_free_vmm_area();
1799 out_free0:
1800 kfree(kvm_vmm_info);
1801 out:
1802 return r;
1803 }
1804
1805 void kvm_arch_exit(void)
1806 {
1807 kvm_free_vmm_area();
1808 kfree(kvm_vmm_info);
1809 kvm_vmm_info = NULL;
1810 }
1811
1812 static int kvm_ia64_sync_dirty_log(struct kvm *kvm,
1813 struct kvm_dirty_log *log)
1814 {
1815 struct kvm_memory_slot *memslot;
1816 int r, i;
1817 long n, base;
1818 unsigned long *dirty_bitmap = (unsigned long *)(kvm->arch.vm_base +
1819 offsetof(struct kvm_vm_data, kvm_mem_dirty_log));
1820
1821 r = -EINVAL;
1822 if (log->slot >= KVM_MEMORY_SLOTS)
1823 goto out;
1824
1825 memslot = &kvm->memslots[log->slot];
1826 r = -ENOENT;
1827 if (!memslot->dirty_bitmap)
1828 goto out;
1829
1830 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1831 base = memslot->base_gfn / BITS_PER_LONG;
1832
1833 for (i = 0; i < n/sizeof(long); ++i) {
1834 memslot->dirty_bitmap[i] = dirty_bitmap[base + i];
1835 dirty_bitmap[base + i] = 0;
1836 }
1837 r = 0;
1838 out:
1839 return r;
1840 }
1841
1842 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1843 struct kvm_dirty_log *log)
1844 {
1845 int r;
1846 int n;
1847 struct kvm_memory_slot *memslot;
1848 int is_dirty = 0;
1849
1850 spin_lock(&kvm->arch.dirty_log_lock);
1851
1852 r = kvm_ia64_sync_dirty_log(kvm, log);
1853 if (r)
1854 goto out;
1855
1856 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1857 if (r)
1858 goto out;
1859
1860 /* If nothing is dirty, don't bother messing with page tables. */
1861 if (is_dirty) {
1862 kvm_flush_remote_tlbs(kvm);
1863 memslot = &kvm->memslots[log->slot];
1864 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1865 memset(memslot->dirty_bitmap, 0, n);
1866 }
1867 r = 0;
1868 out:
1869 spin_unlock(&kvm->arch.dirty_log_lock);
1870 return r;
1871 }
1872
1873 int kvm_arch_hardware_setup(void)
1874 {
1875 return 0;
1876 }
1877
1878 void kvm_arch_hardware_unsetup(void)
1879 {
1880 }
1881
1882 static void vcpu_kick_intr(void *info)
1883 {
1884 #ifdef DEBUG
1885 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
1886 printk(KERN_DEBUG"vcpu_kick_intr %p \n", vcpu);
1887 #endif
1888 }
1889
1890 void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1891 {
1892 int ipi_pcpu = vcpu->cpu;
1893 int cpu = get_cpu();
1894
1895 if (waitqueue_active(&vcpu->wq))
1896 wake_up_interruptible(&vcpu->wq);
1897
1898 if (vcpu->guest_mode && cpu != ipi_pcpu)
1899 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
1900 put_cpu();
1901 }
1902
1903 int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
1904 {
1905 return __apic_accept_irq(vcpu, irq->vector);
1906 }
1907
1908 int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
1909 {
1910 return apic->vcpu->vcpu_id == dest;
1911 }
1912
1913 int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
1914 {
1915 return 0;
1916 }
1917
1918 int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
1919 {
1920 return vcpu1->arch.xtp - vcpu2->arch.xtp;
1921 }
1922
1923 int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
1924 int short_hand, int dest, int dest_mode)
1925 {
1926 struct kvm_lapic *target = vcpu->arch.apic;
1927 return (dest_mode == 0) ?
1928 kvm_apic_match_physical_addr(target, dest) :
1929 kvm_apic_match_logical_addr(target, dest);
1930 }
1931
1932 static int find_highest_bits(int *dat)
1933 {
1934 u32 bits, bitnum;
1935 int i;
1936
1937 /* loop for all 256 bits */
1938 for (i = 7; i >= 0 ; i--) {
1939 bits = dat[i];
1940 if (bits) {
1941 bitnum = fls(bits);
1942 return i * 32 + bitnum - 1;
1943 }
1944 }
1945
1946 return -1;
1947 }
1948
1949 int kvm_highest_pending_irq(struct kvm_vcpu *vcpu)
1950 {
1951 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1952
1953 if (vpd->irr[0] & (1UL << NMI_VECTOR))
1954 return NMI_VECTOR;
1955 if (vpd->irr[0] & (1UL << ExtINT_VECTOR))
1956 return ExtINT_VECTOR;
1957
1958 return find_highest_bits((int *)&vpd->irr[0]);
1959 }
1960
1961 int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
1962 {
1963 if (kvm_highest_pending_irq(vcpu) != -1)
1964 return 1;
1965 return 0;
1966 }
1967
1968 int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
1969 {
1970 /* do real check here */
1971 return 1;
1972 }
1973
1974 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1975 {
1976 return vcpu->arch.timer_fired;
1977 }
1978
1979 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1980 {
1981 return gfn;
1982 }
1983
1984 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1985 {
1986 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE;
1987 }
1988
1989 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1990 struct kvm_mp_state *mp_state)
1991 {
1992 vcpu_load(vcpu);
1993 mp_state->mp_state = vcpu->arch.mp_state;
1994 vcpu_put(vcpu);
1995 return 0;
1996 }
1997
1998 static int vcpu_reset(struct kvm_vcpu *vcpu)
1999 {
2000 int r;
2001 long psr;
2002 local_irq_save(psr);
2003 r = kvm_insert_vmm_mapping(vcpu);
2004 if (r)
2005 goto fail;
2006
2007 vcpu->arch.launched = 0;
2008 kvm_arch_vcpu_uninit(vcpu);
2009 r = kvm_arch_vcpu_init(vcpu);
2010 if (r)
2011 goto fail;
2012
2013 kvm_purge_vmm_mapping(vcpu);
2014 r = 0;
2015 fail:
2016 local_irq_restore(psr);
2017 return r;
2018 }
2019
2020 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
2021 struct kvm_mp_state *mp_state)
2022 {
2023 int r = 0;
2024
2025 vcpu_load(vcpu);
2026 vcpu->arch.mp_state = mp_state->mp_state;
2027 if (vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)
2028 r = vcpu_reset(vcpu);
2029 vcpu_put(vcpu);
2030 return r;
2031 }
This page took 0.135593 seconds and 6 git commands to generate.