blob: af1464f7a6ad270ae6de3783f7d6096aba726294 [file] [log] [blame]
Xiantao Zhangb024b792008-04-01 15:29:29 +08001/*
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>
Xiantao Zhang2381ad22008-10-08 08:29:33 +080034#include <linux/intel-iommu.h>
Xiantao Zhangb024b792008-04-01 15:29:29 +080035
36#include <asm/pgtable.h>
37#include <asm/gcc_intrin.h>
38#include <asm/pal.h>
39#include <asm/cacheflush.h>
40#include <asm/div64.h>
41#include <asm/tlb.h>
Jes Sorensen9f726322008-09-12 14:12:08 +020042#include <asm/elf.h>
Xiantao Zhangb024b792008-04-01 15:29:29 +080043
44#include "misc.h"
45#include "vti.h"
46#include "iodev.h"
47#include "ioapic.h"
48#include "lapic.h"
Xiantao Zhang2f749772008-09-27 11:46:36 +080049#include "irq.h"
Xiantao Zhangb024b792008-04-01 15:29:29 +080050
51static unsigned long kvm_vmm_base;
52static unsigned long kvm_vsa_base;
53static unsigned long kvm_vm_buffer;
54static unsigned long kvm_vm_buffer_size;
55unsigned long kvm_vmm_gp;
56
57static long vp_env_info;
58
59static struct kvm_vmm_info *kvm_vmm_info;
60
61static DEFINE_PER_CPU(struct kvm_vcpu *, last_vcpu);
62
63struct kvm_stats_debugfs_item debugfs_entries[] = {
64 { NULL }
65};
66
Xiantao Zhangb024b792008-04-01 15:29:29 +080067static void kvm_flush_icache(unsigned long start, unsigned long len)
68{
69 int l;
70
71 for (l = 0; l < (len + 32); l += 32)
72 ia64_fc(start + l);
73
74 ia64_sync_i();
75 ia64_srlz_i();
76}
77
78static void kvm_flush_tlb_all(void)
79{
80 unsigned long i, j, count0, count1, stride0, stride1, addr;
81 long flags;
82
83 addr = local_cpu_data->ptce_base;
84 count0 = local_cpu_data->ptce_count[0];
85 count1 = local_cpu_data->ptce_count[1];
86 stride0 = local_cpu_data->ptce_stride[0];
87 stride1 = local_cpu_data->ptce_stride[1];
88
89 local_irq_save(flags);
90 for (i = 0; i < count0; ++i) {
91 for (j = 0; j < count1; ++j) {
92 ia64_ptce(addr);
93 addr += stride1;
94 }
95 addr += stride0;
96 }
97 local_irq_restore(flags);
98 ia64_srlz_i(); /* srlz.i implies srlz.d */
99}
100
101long ia64_pal_vp_create(u64 *vpd, u64 *host_iva, u64 *opt_handler)
102{
103 struct ia64_pal_retval iprv;
104
105 PAL_CALL_STK(iprv, PAL_VP_CREATE, (u64)vpd, (u64)host_iva,
106 (u64)opt_handler);
107
108 return iprv.status;
109}
110
111static DEFINE_SPINLOCK(vp_lock);
112
113void kvm_arch_hardware_enable(void *garbage)
114{
115 long status;
116 long tmp_base;
117 unsigned long pte;
118 unsigned long saved_psr;
119 int slot;
120
121 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base),
122 PAGE_KERNEL));
123 local_irq_save(saved_psr);
124 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
Julia Lawallcab7a1e2008-07-22 21:38:18 +0200125 local_irq_restore(saved_psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800126 if (slot < 0)
127 return;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800128
129 spin_lock(&vp_lock);
130 status = ia64_pal_vp_init_env(kvm_vsa_base ?
131 VP_INIT_ENV : VP_INIT_ENV_INITALIZE,
132 __pa(kvm_vm_buffer), KVM_VM_BUFFER_BASE, &tmp_base);
133 if (status != 0) {
134 printk(KERN_WARNING"kvm: Failed to Enable VT Support!!!!\n");
135 return ;
136 }
137
138 if (!kvm_vsa_base) {
139 kvm_vsa_base = tmp_base;
140 printk(KERN_INFO"kvm: kvm_vsa_base:0x%lx\n", kvm_vsa_base);
141 }
142 spin_unlock(&vp_lock);
143 ia64_ptr_entry(0x3, slot);
144}
145
146void kvm_arch_hardware_disable(void *garbage)
147{
148
149 long status;
150 int slot;
151 unsigned long pte;
152 unsigned long saved_psr;
153 unsigned long host_iva = ia64_getreg(_IA64_REG_CR_IVA);
154
155 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base),
156 PAGE_KERNEL));
157
158 local_irq_save(saved_psr);
159 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
Julia Lawallcab7a1e2008-07-22 21:38:18 +0200160 local_irq_restore(saved_psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800161 if (slot < 0)
162 return;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800163
164 status = ia64_pal_vp_exit_env(host_iva);
165 if (status)
166 printk(KERN_DEBUG"kvm: Failed to disable VT support! :%ld\n",
167 status);
168 ia64_ptr_entry(0x3, slot);
169}
170
171void kvm_arch_check_processor_compat(void *rtn)
172{
173 *(int *)rtn = 0;
174}
175
176int kvm_dev_ioctl_check_extension(long ext)
177{
178
179 int r;
180
181 switch (ext) {
182 case KVM_CAP_IRQCHIP:
183 case KVM_CAP_USER_MEMORY:
Xiantao Zhang8c4b5372008-08-28 09:34:08 +0800184 case KVM_CAP_MP_STATE:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800185
186 r = 1;
187 break;
Laurent Vivier7f39f8a2008-05-30 16:05:57 +0200188 case KVM_CAP_COALESCED_MMIO:
189 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
190 break;
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800191 case KVM_CAP_IOMMU:
192 r = intel_iommu_found();
193 break;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800194 default:
195 r = 0;
196 }
197 return r;
198
199}
200
201static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +0200202 gpa_t addr, int len, int is_write)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800203{
204 struct kvm_io_device *dev;
205
Laurent Vivier92760492008-05-30 16:05:53 +0200206 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len, is_write);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800207
208 return dev;
209}
210
211static int handle_vm_error(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
212{
213 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
214 kvm_run->hw.hardware_exit_reason = 1;
215 return 0;
216}
217
218static int handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
219{
220 struct kvm_mmio_req *p;
221 struct kvm_io_device *mmio_dev;
222
223 p = kvm_get_vcpu_ioreq(vcpu);
224
225 if ((p->addr & PAGE_MASK) == IOAPIC_DEFAULT_BASE_ADDRESS)
226 goto mmio;
227 vcpu->mmio_needed = 1;
228 vcpu->mmio_phys_addr = kvm_run->mmio.phys_addr = p->addr;
229 vcpu->mmio_size = kvm_run->mmio.len = p->size;
230 vcpu->mmio_is_write = kvm_run->mmio.is_write = !p->dir;
231
232 if (vcpu->mmio_is_write)
233 memcpy(vcpu->mmio_data, &p->data, p->size);
234 memcpy(kvm_run->mmio.data, &p->data, p->size);
235 kvm_run->exit_reason = KVM_EXIT_MMIO;
236 return 0;
237mmio:
Laurent Vivier92760492008-05-30 16:05:53 +0200238 mmio_dev = vcpu_find_mmio_dev(vcpu, p->addr, p->size, !p->dir);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800239 if (mmio_dev) {
240 if (!p->dir)
241 kvm_iodevice_write(mmio_dev, p->addr, p->size,
242 &p->data);
243 else
244 kvm_iodevice_read(mmio_dev, p->addr, p->size,
245 &p->data);
246
247 } else
248 printk(KERN_ERR"kvm: No iodevice found! addr:%lx\n", p->addr);
249 p->state = STATE_IORESP_READY;
250
251 return 1;
252}
253
254static int handle_pal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
255{
256 struct exit_ctl_data *p;
257
258 p = kvm_get_exit_data(vcpu);
259
260 if (p->exit_reason == EXIT_REASON_PAL_CALL)
261 return kvm_pal_emul(vcpu, kvm_run);
262 else {
263 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
264 kvm_run->hw.hardware_exit_reason = 2;
265 return 0;
266 }
267}
268
269static int handle_sal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
270{
271 struct exit_ctl_data *p;
272
273 p = kvm_get_exit_data(vcpu);
274
275 if (p->exit_reason == EXIT_REASON_SAL_CALL) {
276 kvm_sal_emul(vcpu);
277 return 1;
278 } else {
279 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
280 kvm_run->hw.hardware_exit_reason = 3;
281 return 0;
282 }
283
284}
285
286/*
287 * offset: address offset to IPI space.
288 * value: deliver value.
289 */
290static void vcpu_deliver_ipi(struct kvm_vcpu *vcpu, uint64_t dm,
291 uint64_t vector)
292{
293 switch (dm) {
294 case SAPIC_FIXED:
295 kvm_apic_set_irq(vcpu, vector, 0);
296 break;
297 case SAPIC_NMI:
298 kvm_apic_set_irq(vcpu, 2, 0);
299 break;
300 case SAPIC_EXTINT:
301 kvm_apic_set_irq(vcpu, 0, 0);
302 break;
303 case SAPIC_INIT:
304 case SAPIC_PMI:
305 default:
306 printk(KERN_ERR"kvm: Unimplemented Deliver reserved IPI!\n");
307 break;
308 }
309}
310
311static struct kvm_vcpu *lid_to_vcpu(struct kvm *kvm, unsigned long id,
312 unsigned long eid)
313{
314 union ia64_lid lid;
315 int i;
316
317 for (i = 0; i < KVM_MAX_VCPUS; i++) {
318 if (kvm->vcpus[i]) {
319 lid.val = VCPU_LID(kvm->vcpus[i]);
320 if (lid.id == id && lid.eid == eid)
321 return kvm->vcpus[i];
322 }
323 }
324
325 return NULL;
326}
327
328static int handle_ipi(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
329{
330 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
331 struct kvm_vcpu *target_vcpu;
332 struct kvm_pt_regs *regs;
333 union ia64_ipi_a addr = p->u.ipi_data.addr;
334 union ia64_ipi_d data = p->u.ipi_data.data;
335
336 target_vcpu = lid_to_vcpu(vcpu->kvm, addr.id, addr.eid);
337 if (!target_vcpu)
338 return handle_vm_error(vcpu, kvm_run);
339
340 if (!target_vcpu->arch.launched) {
341 regs = vcpu_regs(target_vcpu);
342
343 regs->cr_iip = vcpu->kvm->arch.rdv_sal_data.boot_ip;
344 regs->r1 = vcpu->kvm->arch.rdv_sal_data.boot_gp;
345
Avi Kivitya4535292008-04-13 17:54:35 +0300346 target_vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800347 if (waitqueue_active(&target_vcpu->wq))
348 wake_up_interruptible(&target_vcpu->wq);
349 } else {
350 vcpu_deliver_ipi(target_vcpu, data.dm, data.vector);
351 if (target_vcpu != vcpu)
352 kvm_vcpu_kick(target_vcpu);
353 }
354
355 return 1;
356}
357
358struct call_data {
359 struct kvm_ptc_g ptc_g_data;
360 struct kvm_vcpu *vcpu;
361};
362
363static void vcpu_global_purge(void *info)
364{
365 struct call_data *p = (struct call_data *)info;
366 struct kvm_vcpu *vcpu = p->vcpu;
367
368 if (test_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
369 return;
370
371 set_bit(KVM_REQ_PTC_G, &vcpu->requests);
372 if (vcpu->arch.ptc_g_count < MAX_PTC_G_NUM) {
373 vcpu->arch.ptc_g_data[vcpu->arch.ptc_g_count++] =
374 p->ptc_g_data;
375 } else {
376 clear_bit(KVM_REQ_PTC_G, &vcpu->requests);
377 vcpu->arch.ptc_g_count = 0;
378 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
379 }
380}
381
382static int handle_global_purge(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
383{
384 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
385 struct kvm *kvm = vcpu->kvm;
386 struct call_data call_data;
387 int i;
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800388
Xiantao Zhangb024b792008-04-01 15:29:29 +0800389 call_data.ptc_g_data = p->u.ptc_g_data;
390
391 for (i = 0; i < KVM_MAX_VCPUS; i++) {
392 if (!kvm->vcpus[i] || kvm->vcpus[i]->arch.mp_state ==
Avi Kivitya4535292008-04-13 17:54:35 +0300393 KVM_MP_STATE_UNINITIALIZED ||
Xiantao Zhangb024b792008-04-01 15:29:29 +0800394 vcpu == kvm->vcpus[i])
395 continue;
396
397 if (waitqueue_active(&kvm->vcpus[i]->wq))
398 wake_up_interruptible(&kvm->vcpus[i]->wq);
399
400 if (kvm->vcpus[i]->cpu != -1) {
401 call_data.vcpu = kvm->vcpus[i];
402 smp_call_function_single(kvm->vcpus[i]->cpu,
Takashi Iwai2f73cca2008-07-17 18:09:12 +0200403 vcpu_global_purge, &call_data, 1);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800404 } else
405 printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n");
406
407 }
408 return 1;
409}
410
411static int handle_switch_rr6(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
412{
413 return 1;
414}
415
416int kvm_emulate_halt(struct kvm_vcpu *vcpu)
417{
418
419 ktime_t kt;
420 long itc_diff;
421 unsigned long vcpu_now_itc;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800422 unsigned long expires;
423 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
424 unsigned long cyc_per_usec = local_cpu_data->cyc_per_usec;
425 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
426
Xiantao Zhangb024b792008-04-01 15:29:29 +0800427 if (irqchip_in_kernel(vcpu->kvm)) {
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800428
429 vcpu_now_itc = ia64_getreg(_IA64_REG_AR_ITC) + vcpu->arch.itc_offset;
430
431 if (time_after(vcpu_now_itc, vpd->itm)) {
432 vcpu->arch.timer_check = 1;
433 return 1;
434 }
435 itc_diff = vpd->itm - vcpu_now_itc;
436 if (itc_diff < 0)
437 itc_diff = -itc_diff;
438
439 expires = div64_u64(itc_diff, cyc_per_usec);
440 kt = ktime_set(0, 1000 * expires);
441
442 down_read(&vcpu->kvm->slots_lock);
443 vcpu->arch.ht_active = 1;
444 hrtimer_start(p_ht, kt, HRTIMER_MODE_ABS);
445
Avi Kivitya4535292008-04-13 17:54:35 +0300446 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800447 kvm_vcpu_block(vcpu);
448 hrtimer_cancel(p_ht);
449 vcpu->arch.ht_active = 0;
450
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800451 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
452 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
453 vcpu->arch.mp_state =
454 KVM_MP_STATE_RUNNABLE;
455 up_read(&vcpu->kvm->slots_lock);
456
Avi Kivitya4535292008-04-13 17:54:35 +0300457 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800458 return -EINTR;
459 return 1;
460 } else {
461 printk(KERN_ERR"kvm: Unsupported userspace halt!");
462 return 0;
463 }
464}
465
466static int handle_vm_shutdown(struct kvm_vcpu *vcpu,
467 struct kvm_run *kvm_run)
468{
469 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
470 return 0;
471}
472
473static int handle_external_interrupt(struct kvm_vcpu *vcpu,
474 struct kvm_run *kvm_run)
475{
476 return 1;
477}
478
479static int (*kvm_vti_exit_handlers[])(struct kvm_vcpu *vcpu,
480 struct kvm_run *kvm_run) = {
481 [EXIT_REASON_VM_PANIC] = handle_vm_error,
482 [EXIT_REASON_MMIO_INSTRUCTION] = handle_mmio,
483 [EXIT_REASON_PAL_CALL] = handle_pal_call,
484 [EXIT_REASON_SAL_CALL] = handle_sal_call,
485 [EXIT_REASON_SWITCH_RR6] = handle_switch_rr6,
486 [EXIT_REASON_VM_DESTROY] = handle_vm_shutdown,
487 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
488 [EXIT_REASON_IPI] = handle_ipi,
489 [EXIT_REASON_PTC_G] = handle_global_purge,
490
491};
492
493static const int kvm_vti_max_exit_handlers =
494 sizeof(kvm_vti_exit_handlers)/sizeof(*kvm_vti_exit_handlers);
495
Xiantao Zhangb024b792008-04-01 15:29:29 +0800496static uint32_t kvm_get_exit_reason(struct kvm_vcpu *vcpu)
497{
498 struct exit_ctl_data *p_exit_data;
499
500 p_exit_data = kvm_get_exit_data(vcpu);
501 return p_exit_data->exit_reason;
502}
503
504/*
505 * The guest has exited. See if we can fix it or if we need userspace
506 * assistance.
507 */
508static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
509{
510 u32 exit_reason = kvm_get_exit_reason(vcpu);
511 vcpu->arch.last_exit = exit_reason;
512
513 if (exit_reason < kvm_vti_max_exit_handlers
514 && kvm_vti_exit_handlers[exit_reason])
515 return kvm_vti_exit_handlers[exit_reason](vcpu, kvm_run);
516 else {
517 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
518 kvm_run->hw.hardware_exit_reason = exit_reason;
519 }
520 return 0;
521}
522
523static inline void vti_set_rr6(unsigned long rr6)
524{
525 ia64_set_rr(RR6, rr6);
526 ia64_srlz_i();
527}
528
529static int kvm_insert_vmm_mapping(struct kvm_vcpu *vcpu)
530{
531 unsigned long pte;
532 struct kvm *kvm = vcpu->kvm;
533 int r;
534
535 /*Insert a pair of tr to map vmm*/
536 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
537 r = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
538 if (r < 0)
539 goto out;
540 vcpu->arch.vmm_tr_slot = r;
541 /*Insert a pairt of tr to map data of vm*/
542 pte = pte_val(mk_pte_phys(__pa(kvm->arch.vm_base), PAGE_KERNEL));
543 r = ia64_itr_entry(0x3, KVM_VM_DATA_BASE,
544 pte, KVM_VM_DATA_SHIFT);
545 if (r < 0)
546 goto out;
547 vcpu->arch.vm_tr_slot = r;
548 r = 0;
549out:
550 return r;
551
552}
553
554static void kvm_purge_vmm_mapping(struct kvm_vcpu *vcpu)
555{
556
557 ia64_ptr_entry(0x3, vcpu->arch.vmm_tr_slot);
558 ia64_ptr_entry(0x3, vcpu->arch.vm_tr_slot);
559
560}
561
562static int kvm_vcpu_pre_transition(struct kvm_vcpu *vcpu)
563{
564 int cpu = smp_processor_id();
565
566 if (vcpu->arch.last_run_cpu != cpu ||
567 per_cpu(last_vcpu, cpu) != vcpu) {
568 per_cpu(last_vcpu, cpu) = vcpu;
569 vcpu->arch.last_run_cpu = cpu;
570 kvm_flush_tlb_all();
571 }
572
573 vcpu->arch.host_rr6 = ia64_get_rr(RR6);
574 vti_set_rr6(vcpu->arch.vmm_rr);
575 return kvm_insert_vmm_mapping(vcpu);
576}
577static void kvm_vcpu_post_transition(struct kvm_vcpu *vcpu)
578{
579 kvm_purge_vmm_mapping(vcpu);
580 vti_set_rr6(vcpu->arch.host_rr6);
581}
582
583static int vti_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
584{
585 union context *host_ctx, *guest_ctx;
586 int r;
587
588 /*Get host and guest context with guest address space.*/
589 host_ctx = kvm_get_host_context(vcpu);
590 guest_ctx = kvm_get_guest_context(vcpu);
591
592 r = kvm_vcpu_pre_transition(vcpu);
593 if (r < 0)
594 goto out;
595 kvm_vmm_info->tramp_entry(host_ctx, guest_ctx);
596 kvm_vcpu_post_transition(vcpu);
597 r = 0;
598out:
599 return r;
600}
601
602static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
603{
604 int r;
605
606again:
607 preempt_disable();
Xiantao Zhangb024b792008-04-01 15:29:29 +0800608 local_irq_disable();
609
610 if (signal_pending(current)) {
611 local_irq_enable();
612 preempt_enable();
613 r = -EINTR;
614 kvm_run->exit_reason = KVM_EXIT_INTR;
615 goto out;
616 }
617
618 vcpu->guest_mode = 1;
619 kvm_guest_enter();
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800620 down_read(&vcpu->kvm->slots_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800621 r = vti_vcpu_run(vcpu, kvm_run);
622 if (r < 0) {
623 local_irq_enable();
624 preempt_enable();
625 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
626 goto out;
627 }
628
629 vcpu->arch.launched = 1;
630 vcpu->guest_mode = 0;
631 local_irq_enable();
632
633 /*
634 * We must have an instruction between local_irq_enable() and
635 * kvm_guest_exit(), so the timer interrupt isn't delayed by
636 * the interrupt shadow. The stat.exits increment will do nicely.
637 * But we need to prevent reordering, hence this barrier():
638 */
639 barrier();
Xiantao Zhangb024b792008-04-01 15:29:29 +0800640 kvm_guest_exit();
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800641 up_read(&vcpu->kvm->slots_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800642 preempt_enable();
643
644 r = kvm_handle_exit(kvm_run, vcpu);
645
646 if (r > 0) {
647 if (!need_resched())
648 goto again;
649 }
650
651out:
652 if (r > 0) {
653 kvm_resched(vcpu);
654 goto again;
655 }
656
657 return r;
658}
659
660static void kvm_set_mmio_data(struct kvm_vcpu *vcpu)
661{
662 struct kvm_mmio_req *p = kvm_get_vcpu_ioreq(vcpu);
663
664 if (!vcpu->mmio_is_write)
665 memcpy(&p->data, vcpu->mmio_data, 8);
666 p->state = STATE_IORESP_READY;
667}
668
669int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
670{
671 int r;
672 sigset_t sigsaved;
673
674 vcpu_load(vcpu);
675
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800676 if (vcpu->sigset_active)
677 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
678
Avi Kivitya4535292008-04-13 17:54:35 +0300679 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800680 kvm_vcpu_block(vcpu);
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800681 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800682 r = -EAGAIN;
683 goto out;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800684 }
685
Xiantao Zhangb024b792008-04-01 15:29:29 +0800686 if (vcpu->mmio_needed) {
687 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
688 kvm_set_mmio_data(vcpu);
689 vcpu->mmio_read_completed = 1;
690 vcpu->mmio_needed = 0;
691 }
692 r = __vcpu_run(vcpu, kvm_run);
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800693out:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800694 if (vcpu->sigset_active)
695 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
696
697 vcpu_put(vcpu);
698 return r;
699}
700
701/*
702 * Allocate 16M memory for every vm to hold its specific data.
703 * Its memory map is defined in kvm_host.h.
704 */
705static struct kvm *kvm_alloc_kvm(void)
706{
707
708 struct kvm *kvm;
709 uint64_t vm_base;
710
711 vm_base = __get_free_pages(GFP_KERNEL, get_order(KVM_VM_DATA_SIZE));
712
713 if (!vm_base)
714 return ERR_PTR(-ENOMEM);
715 printk(KERN_DEBUG"kvm: VM data's base Address:0x%lx\n", vm_base);
716
717 /* Zero all pages before use! */
718 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
719
720 kvm = (struct kvm *)(vm_base + KVM_VM_OFS);
721 kvm->arch.vm_base = vm_base;
722
723 return kvm;
724}
725
726struct kvm_io_range {
727 unsigned long start;
728 unsigned long size;
729 unsigned long type;
730};
731
732static const struct kvm_io_range io_ranges[] = {
733 {VGA_IO_START, VGA_IO_SIZE, GPFN_FRAME_BUFFER},
734 {MMIO_START, MMIO_SIZE, GPFN_LOW_MMIO},
735 {LEGACY_IO_START, LEGACY_IO_SIZE, GPFN_LEGACY_IO},
736 {IO_SAPIC_START, IO_SAPIC_SIZE, GPFN_IOSAPIC},
737 {PIB_START, PIB_SIZE, GPFN_PIB},
738};
739
740static void kvm_build_io_pmt(struct kvm *kvm)
741{
742 unsigned long i, j;
743
744 /* Mark I/O ranges */
745 for (i = 0; i < (sizeof(io_ranges) / sizeof(struct kvm_io_range));
746 i++) {
747 for (j = io_ranges[i].start;
748 j < io_ranges[i].start + io_ranges[i].size;
749 j += PAGE_SIZE)
750 kvm_set_pmt_entry(kvm, j >> PAGE_SHIFT,
751 io_ranges[i].type, 0);
752 }
753
754}
755
756/*Use unused rids to virtualize guest rid.*/
757#define GUEST_PHYSICAL_RR0 0x1739
758#define GUEST_PHYSICAL_RR4 0x2739
759#define VMM_INIT_RR 0x1660
760
761static void kvm_init_vm(struct kvm *kvm)
762{
763 long vm_base;
764
765 BUG_ON(!kvm);
766
767 kvm->arch.metaphysical_rr0 = GUEST_PHYSICAL_RR0;
768 kvm->arch.metaphysical_rr4 = GUEST_PHYSICAL_RR4;
769 kvm->arch.vmm_init_rr = VMM_INIT_RR;
770
771 vm_base = kvm->arch.vm_base;
772 if (vm_base) {
773 kvm->arch.vhpt_base = vm_base + KVM_VHPT_OFS;
774 kvm->arch.vtlb_base = vm_base + KVM_VTLB_OFS;
775 kvm->arch.vpd_base = vm_base + KVM_VPD_OFS;
776 }
777
778 /*
779 *Fill P2M entries for MMIO/IO ranges
780 */
781 kvm_build_io_pmt(kvm);
782
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800783 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Sheng Yang5550af42008-10-15 20:15:06 +0800784
785 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
786 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800787}
788
789struct kvm *kvm_arch_create_vm(void)
790{
791 struct kvm *kvm = kvm_alloc_kvm();
792
793 if (IS_ERR(kvm))
794 return ERR_PTR(-ENOMEM);
795 kvm_init_vm(kvm);
796
797 return kvm;
798
799}
800
801static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm,
802 struct kvm_irqchip *chip)
803{
804 int r;
805
806 r = 0;
807 switch (chip->chip_id) {
808 case KVM_IRQCHIP_IOAPIC:
809 memcpy(&chip->chip.ioapic, ioapic_irqchip(kvm),
810 sizeof(struct kvm_ioapic_state));
811 break;
812 default:
813 r = -EINVAL;
814 break;
815 }
816 return r;
817}
818
819static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
820{
821 int r;
822
823 r = 0;
824 switch (chip->chip_id) {
825 case KVM_IRQCHIP_IOAPIC:
826 memcpy(ioapic_irqchip(kvm),
827 &chip->chip.ioapic,
828 sizeof(struct kvm_ioapic_state));
829 break;
830 default:
831 r = -EINVAL;
832 break;
833 }
834 return r;
835}
836
837#define RESTORE_REGS(_x) vcpu->arch._x = regs->_x
838
839int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
840{
841 int i;
842 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
843 int r;
844
845 vcpu_load(vcpu);
846
847 for (i = 0; i < 16; i++) {
848 vpd->vgr[i] = regs->vpd.vgr[i];
849 vpd->vbgr[i] = regs->vpd.vbgr[i];
850 }
851 for (i = 0; i < 128; i++)
852 vpd->vcr[i] = regs->vpd.vcr[i];
853 vpd->vhpi = regs->vpd.vhpi;
854 vpd->vnat = regs->vpd.vnat;
855 vpd->vbnat = regs->vpd.vbnat;
856 vpd->vpsr = regs->vpd.vpsr;
857
858 vpd->vpr = regs->vpd.vpr;
859
860 r = -EFAULT;
861 r = copy_from_user(&vcpu->arch.guest, regs->saved_guest,
862 sizeof(union context));
863 if (r)
864 goto out;
865 r = copy_from_user(vcpu + 1, regs->saved_stack +
866 sizeof(struct kvm_vcpu),
867 IA64_STK_OFFSET - sizeof(struct kvm_vcpu));
868 if (r)
869 goto out;
870 vcpu->arch.exit_data =
871 ((struct kvm_vcpu *)(regs->saved_stack))->arch.exit_data;
872
873 RESTORE_REGS(mp_state);
874 RESTORE_REGS(vmm_rr);
875 memcpy(vcpu->arch.itrs, regs->itrs, sizeof(struct thash_data) * NITRS);
876 memcpy(vcpu->arch.dtrs, regs->dtrs, sizeof(struct thash_data) * NDTRS);
877 RESTORE_REGS(itr_regions);
878 RESTORE_REGS(dtr_regions);
879 RESTORE_REGS(tc_regions);
880 RESTORE_REGS(irq_check);
881 RESTORE_REGS(itc_check);
882 RESTORE_REGS(timer_check);
883 RESTORE_REGS(timer_pending);
884 RESTORE_REGS(last_itc);
885 for (i = 0; i < 8; i++) {
886 vcpu->arch.vrr[i] = regs->vrr[i];
887 vcpu->arch.ibr[i] = regs->ibr[i];
888 vcpu->arch.dbr[i] = regs->dbr[i];
889 }
890 for (i = 0; i < 4; i++)
891 vcpu->arch.insvc[i] = regs->insvc[i];
892 RESTORE_REGS(xtp);
893 RESTORE_REGS(metaphysical_rr0);
894 RESTORE_REGS(metaphysical_rr4);
895 RESTORE_REGS(metaphysical_saved_rr0);
896 RESTORE_REGS(metaphysical_saved_rr4);
897 RESTORE_REGS(fp_psr);
898 RESTORE_REGS(saved_gp);
899
900 vcpu->arch.irq_new_pending = 1;
901 vcpu->arch.itc_offset = regs->saved_itc - ia64_getreg(_IA64_REG_AR_ITC);
902 set_bit(KVM_REQ_RESUME, &vcpu->requests);
903
904 vcpu_put(vcpu);
905 r = 0;
906out:
907 return r;
908}
909
910long kvm_arch_vm_ioctl(struct file *filp,
911 unsigned int ioctl, unsigned long arg)
912{
913 struct kvm *kvm = filp->private_data;
914 void __user *argp = (void __user *)arg;
915 int r = -EINVAL;
916
917 switch (ioctl) {
918 case KVM_SET_MEMORY_REGION: {
919 struct kvm_memory_region kvm_mem;
920 struct kvm_userspace_memory_region kvm_userspace_mem;
921
922 r = -EFAULT;
923 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
924 goto out;
925 kvm_userspace_mem.slot = kvm_mem.slot;
926 kvm_userspace_mem.flags = kvm_mem.flags;
927 kvm_userspace_mem.guest_phys_addr =
928 kvm_mem.guest_phys_addr;
929 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
930 r = kvm_vm_ioctl_set_memory_region(kvm,
931 &kvm_userspace_mem, 0);
932 if (r)
933 goto out;
934 break;
935 }
936 case KVM_CREATE_IRQCHIP:
937 r = -EFAULT;
938 r = kvm_ioapic_init(kvm);
939 if (r)
940 goto out;
941 break;
942 case KVM_IRQ_LINE: {
943 struct kvm_irq_level irq_event;
944
945 r = -EFAULT;
946 if (copy_from_user(&irq_event, argp, sizeof irq_event))
947 goto out;
948 if (irqchip_in_kernel(kvm)) {
949 mutex_lock(&kvm->lock);
Sheng Yang5550af42008-10-15 20:15:06 +0800950 kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
951 irq_event.irq, irq_event.level);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800952 mutex_unlock(&kvm->lock);
953 r = 0;
954 }
955 break;
956 }
957 case KVM_GET_IRQCHIP: {
958 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
959 struct kvm_irqchip chip;
960
961 r = -EFAULT;
962 if (copy_from_user(&chip, argp, sizeof chip))
963 goto out;
964 r = -ENXIO;
965 if (!irqchip_in_kernel(kvm))
966 goto out;
967 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
968 if (r)
969 goto out;
970 r = -EFAULT;
971 if (copy_to_user(argp, &chip, sizeof chip))
972 goto out;
973 r = 0;
974 break;
975 }
976 case KVM_SET_IRQCHIP: {
977 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
978 struct kvm_irqchip chip;
979
980 r = -EFAULT;
981 if (copy_from_user(&chip, argp, sizeof chip))
982 goto out;
983 r = -ENXIO;
984 if (!irqchip_in_kernel(kvm))
985 goto out;
986 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
987 if (r)
988 goto out;
989 r = 0;
990 break;
991 }
992 default:
993 ;
994 }
995out:
996 return r;
997}
998
999int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1000 struct kvm_sregs *sregs)
1001{
1002 return -EINVAL;
1003}
1004
1005int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1006 struct kvm_sregs *sregs)
1007{
1008 return -EINVAL;
1009
1010}
1011int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1012 struct kvm_translation *tr)
1013{
1014
1015 return -EINVAL;
1016}
1017
1018static int kvm_alloc_vmm_area(void)
1019{
1020 if (!kvm_vmm_base && (kvm_vm_buffer_size < KVM_VM_BUFFER_SIZE)) {
1021 kvm_vmm_base = __get_free_pages(GFP_KERNEL,
1022 get_order(KVM_VMM_SIZE));
1023 if (!kvm_vmm_base)
1024 return -ENOMEM;
1025
1026 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1027 kvm_vm_buffer = kvm_vmm_base + VMM_SIZE;
1028
1029 printk(KERN_DEBUG"kvm:VMM's Base Addr:0x%lx, vm_buffer:0x%lx\n",
1030 kvm_vmm_base, kvm_vm_buffer);
1031 }
1032
1033 return 0;
1034}
1035
1036static void kvm_free_vmm_area(void)
1037{
1038 if (kvm_vmm_base) {
1039 /*Zero this area before free to avoid bits leak!!*/
1040 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1041 free_pages(kvm_vmm_base, get_order(KVM_VMM_SIZE));
1042 kvm_vmm_base = 0;
1043 kvm_vm_buffer = 0;
1044 kvm_vsa_base = 0;
1045 }
1046}
1047
Xiantao Zhangb024b792008-04-01 15:29:29 +08001048static void vti_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1049{
1050}
1051
1052static int vti_init_vpd(struct kvm_vcpu *vcpu)
1053{
1054 int i;
1055 union cpuid3_t cpuid3;
1056 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1057
1058 if (IS_ERR(vpd))
1059 return PTR_ERR(vpd);
1060
1061 /* CPUID init */
1062 for (i = 0; i < 5; i++)
1063 vpd->vcpuid[i] = ia64_get_cpuid(i);
1064
1065 /* Limit the CPUID number to 5 */
1066 cpuid3.value = vpd->vcpuid[3];
1067 cpuid3.number = 4; /* 5 - 1 */
1068 vpd->vcpuid[3] = cpuid3.value;
1069
1070 /*Set vac and vdc fields*/
1071 vpd->vac.a_from_int_cr = 1;
1072 vpd->vac.a_to_int_cr = 1;
1073 vpd->vac.a_from_psr = 1;
1074 vpd->vac.a_from_cpuid = 1;
1075 vpd->vac.a_cover = 1;
1076 vpd->vac.a_bsw = 1;
1077 vpd->vac.a_int = 1;
1078 vpd->vdc.d_vmsw = 1;
1079
1080 /*Set virtual buffer*/
1081 vpd->virt_env_vaddr = KVM_VM_BUFFER_BASE;
1082
1083 return 0;
1084}
1085
1086static int vti_create_vp(struct kvm_vcpu *vcpu)
1087{
1088 long ret;
1089 struct vpd *vpd = vcpu->arch.vpd;
1090 unsigned long vmm_ivt;
1091
1092 vmm_ivt = kvm_vmm_info->vmm_ivt;
1093
1094 printk(KERN_DEBUG "kvm: vcpu:%p,ivt: 0x%lx\n", vcpu, vmm_ivt);
1095
1096 ret = ia64_pal_vp_create((u64 *)vpd, (u64 *)vmm_ivt, 0);
1097
1098 if (ret) {
1099 printk(KERN_ERR"kvm: ia64_pal_vp_create failed!\n");
1100 return -EINVAL;
1101 }
1102 return 0;
1103}
1104
1105static void init_ptce_info(struct kvm_vcpu *vcpu)
1106{
1107 ia64_ptce_info_t ptce = {0};
1108
1109 ia64_get_ptce(&ptce);
1110 vcpu->arch.ptce_base = ptce.base;
1111 vcpu->arch.ptce_count[0] = ptce.count[0];
1112 vcpu->arch.ptce_count[1] = ptce.count[1];
1113 vcpu->arch.ptce_stride[0] = ptce.stride[0];
1114 vcpu->arch.ptce_stride[1] = ptce.stride[1];
1115}
1116
1117static void kvm_migrate_hlt_timer(struct kvm_vcpu *vcpu)
1118{
1119 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
1120
1121 if (hrtimer_cancel(p_ht))
Arjan van de Ven18dd36a2008-09-01 15:19:11 -07001122 hrtimer_start_expires(p_ht, HRTIMER_MODE_ABS);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001123}
1124
1125static enum hrtimer_restart hlt_timer_fn(struct hrtimer *data)
1126{
1127 struct kvm_vcpu *vcpu;
1128 wait_queue_head_t *q;
1129
1130 vcpu = container_of(data, struct kvm_vcpu, arch.hlt_timer);
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001131 q = &vcpu->wq;
1132
Avi Kivitya4535292008-04-13 17:54:35 +03001133 if (vcpu->arch.mp_state != KVM_MP_STATE_HALTED)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001134 goto out;
1135
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001136 if (waitqueue_active(q))
Xiantao Zhangb024b792008-04-01 15:29:29 +08001137 wake_up_interruptible(q);
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001138
Xiantao Zhangb024b792008-04-01 15:29:29 +08001139out:
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001140 vcpu->arch.timer_fired = 1;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001141 vcpu->arch.timer_check = 1;
1142 return HRTIMER_NORESTART;
1143}
1144
1145#define PALE_RESET_ENTRY 0x80000000ffffffb0UL
1146
1147int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1148{
1149 struct kvm_vcpu *v;
1150 int r;
1151 int i;
1152 long itc_offset;
1153 struct kvm *kvm = vcpu->kvm;
1154 struct kvm_pt_regs *regs = vcpu_regs(vcpu);
1155
1156 union context *p_ctx = &vcpu->arch.guest;
1157 struct kvm_vcpu *vmm_vcpu = to_guest(vcpu->kvm, vcpu);
1158
1159 /*Init vcpu context for first run.*/
1160 if (IS_ERR(vmm_vcpu))
1161 return PTR_ERR(vmm_vcpu);
1162
1163 if (vcpu->vcpu_id == 0) {
Avi Kivitya4535292008-04-13 17:54:35 +03001164 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001165
1166 /*Set entry address for first run.*/
1167 regs->cr_iip = PALE_RESET_ENTRY;
1168
1169 /*Initilize itc offset for vcpus*/
1170 itc_offset = 0UL - ia64_getreg(_IA64_REG_AR_ITC);
1171 for (i = 0; i < MAX_VCPU_NUM; i++) {
1172 v = (struct kvm_vcpu *)((char *)vcpu + VCPU_SIZE * i);
1173 v->arch.itc_offset = itc_offset;
1174 v->arch.last_itc = 0;
1175 }
1176 } else
Avi Kivitya4535292008-04-13 17:54:35 +03001177 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001178
1179 r = -ENOMEM;
1180 vcpu->arch.apic = kzalloc(sizeof(struct kvm_lapic), GFP_KERNEL);
1181 if (!vcpu->arch.apic)
1182 goto out;
1183 vcpu->arch.apic->vcpu = vcpu;
1184
1185 p_ctx->gr[1] = 0;
1186 p_ctx->gr[12] = (unsigned long)((char *)vmm_vcpu + IA64_STK_OFFSET);
1187 p_ctx->gr[13] = (unsigned long)vmm_vcpu;
1188 p_ctx->psr = 0x1008522000UL;
1189 p_ctx->ar[40] = FPSR_DEFAULT; /*fpsr*/
1190 p_ctx->caller_unat = 0;
1191 p_ctx->pr = 0x0;
1192 p_ctx->ar[36] = 0x0; /*unat*/
1193 p_ctx->ar[19] = 0x0; /*rnat*/
1194 p_ctx->ar[18] = (unsigned long)vmm_vcpu +
1195 ((sizeof(struct kvm_vcpu)+15) & ~15);
1196 p_ctx->ar[64] = 0x0; /*pfs*/
1197 p_ctx->cr[0] = 0x7e04UL;
1198 p_ctx->cr[2] = (unsigned long)kvm_vmm_info->vmm_ivt;
1199 p_ctx->cr[8] = 0x3c;
1200
1201 /*Initilize region register*/
1202 p_ctx->rr[0] = 0x30;
1203 p_ctx->rr[1] = 0x30;
1204 p_ctx->rr[2] = 0x30;
1205 p_ctx->rr[3] = 0x30;
1206 p_ctx->rr[4] = 0x30;
1207 p_ctx->rr[5] = 0x30;
1208 p_ctx->rr[7] = 0x30;
1209
1210 /*Initilize branch register 0*/
1211 p_ctx->br[0] = *(unsigned long *)kvm_vmm_info->vmm_entry;
1212
1213 vcpu->arch.vmm_rr = kvm->arch.vmm_init_rr;
1214 vcpu->arch.metaphysical_rr0 = kvm->arch.metaphysical_rr0;
1215 vcpu->arch.metaphysical_rr4 = kvm->arch.metaphysical_rr4;
1216
1217 hrtimer_init(&vcpu->arch.hlt_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1218 vcpu->arch.hlt_timer.function = hlt_timer_fn;
1219
1220 vcpu->arch.last_run_cpu = -1;
1221 vcpu->arch.vpd = (struct vpd *)VPD_ADDR(vcpu->vcpu_id);
1222 vcpu->arch.vsa_base = kvm_vsa_base;
1223 vcpu->arch.__gp = kvm_vmm_gp;
1224 vcpu->arch.dirty_log_lock_pa = __pa(&kvm->arch.dirty_log_lock);
1225 vcpu->arch.vhpt.hash = (struct thash_data *)VHPT_ADDR(vcpu->vcpu_id);
1226 vcpu->arch.vtlb.hash = (struct thash_data *)VTLB_ADDR(vcpu->vcpu_id);
1227 init_ptce_info(vcpu);
1228
1229 r = 0;
1230out:
1231 return r;
1232}
1233
1234static int vti_vcpu_setup(struct kvm_vcpu *vcpu, int id)
1235{
1236 unsigned long psr;
1237 int r;
1238
1239 local_irq_save(psr);
1240 r = kvm_insert_vmm_mapping(vcpu);
1241 if (r)
1242 goto fail;
1243 r = kvm_vcpu_init(vcpu, vcpu->kvm, id);
1244 if (r)
1245 goto fail;
1246
1247 r = vti_init_vpd(vcpu);
1248 if (r) {
1249 printk(KERN_DEBUG"kvm: vpd init error!!\n");
1250 goto uninit;
1251 }
1252
1253 r = vti_create_vp(vcpu);
1254 if (r)
1255 goto uninit;
1256
1257 kvm_purge_vmm_mapping(vcpu);
1258 local_irq_restore(psr);
1259
1260 return 0;
1261uninit:
1262 kvm_vcpu_uninit(vcpu);
1263fail:
Julia Lawallcab7a1e2008-07-22 21:38:18 +02001264 local_irq_restore(psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001265 return r;
1266}
1267
1268struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1269 unsigned int id)
1270{
1271 struct kvm_vcpu *vcpu;
1272 unsigned long vm_base = kvm->arch.vm_base;
1273 int r;
1274 int cpu;
1275
1276 r = -ENOMEM;
1277 if (!vm_base) {
1278 printk(KERN_ERR"kvm: Create vcpu[%d] error!\n", id);
1279 goto fail;
1280 }
1281 vcpu = (struct kvm_vcpu *)(vm_base + KVM_VCPU_OFS + VCPU_SIZE * id);
1282 vcpu->kvm = kvm;
1283
1284 cpu = get_cpu();
1285 vti_vcpu_load(vcpu, cpu);
1286 r = vti_vcpu_setup(vcpu, id);
1287 put_cpu();
1288
1289 if (r) {
1290 printk(KERN_DEBUG"kvm: vcpu_setup error!!\n");
1291 goto fail;
1292 }
1293
1294 return vcpu;
1295fail:
1296 return ERR_PTR(r);
1297}
1298
1299int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1300{
1301 return 0;
1302}
1303
1304int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1305{
1306 return -EINVAL;
1307}
1308
1309int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1310{
1311 return -EINVAL;
1312}
1313
1314int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
1315 struct kvm_debug_guest *dbg)
1316{
1317 return -EINVAL;
1318}
1319
1320static void free_kvm(struct kvm *kvm)
1321{
1322 unsigned long vm_base = kvm->arch.vm_base;
1323
1324 if (vm_base) {
1325 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
1326 free_pages(vm_base, get_order(KVM_VM_DATA_SIZE));
1327 }
1328
1329}
1330
1331static void kvm_release_vm_pages(struct kvm *kvm)
1332{
1333 struct kvm_memory_slot *memslot;
1334 int i, j;
1335 unsigned long base_gfn;
1336
1337 for (i = 0; i < kvm->nmemslots; i++) {
1338 memslot = &kvm->memslots[i];
1339 base_gfn = memslot->base_gfn;
1340
1341 for (j = 0; j < memslot->npages; j++) {
1342 if (memslot->rmap[j])
1343 put_page((struct page *)memslot->rmap[j]);
1344 }
1345 }
1346}
1347
1348void kvm_arch_destroy_vm(struct kvm *kvm)
1349{
Xiantao Zhang2381ad22008-10-08 08:29:33 +08001350 kvm_iommu_unmap_guest(kvm);
1351#ifdef KVM_CAP_DEVICE_ASSIGNMENT
1352 kvm_free_all_assigned_devices(kvm);
1353#endif
Xiantao Zhangb024b792008-04-01 15:29:29 +08001354 kfree(kvm->arch.vioapic);
1355 kvm_release_vm_pages(kvm);
1356 kvm_free_physmem(kvm);
1357 free_kvm(kvm);
1358}
1359
1360void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1361{
1362}
1363
1364void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1365{
1366 if (cpu != vcpu->cpu) {
1367 vcpu->cpu = cpu;
1368 if (vcpu->arch.ht_active)
1369 kvm_migrate_hlt_timer(vcpu);
1370 }
1371}
1372
1373#define SAVE_REGS(_x) regs->_x = vcpu->arch._x
1374
1375int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1376{
1377 int i;
1378 int r;
1379 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1380 vcpu_load(vcpu);
1381
1382 for (i = 0; i < 16; i++) {
1383 regs->vpd.vgr[i] = vpd->vgr[i];
1384 regs->vpd.vbgr[i] = vpd->vbgr[i];
1385 }
1386 for (i = 0; i < 128; i++)
1387 regs->vpd.vcr[i] = vpd->vcr[i];
1388 regs->vpd.vhpi = vpd->vhpi;
1389 regs->vpd.vnat = vpd->vnat;
1390 regs->vpd.vbnat = vpd->vbnat;
1391 regs->vpd.vpsr = vpd->vpsr;
1392 regs->vpd.vpr = vpd->vpr;
1393
1394 r = -EFAULT;
1395 r = copy_to_user(regs->saved_guest, &vcpu->arch.guest,
1396 sizeof(union context));
1397 if (r)
1398 goto out;
1399 r = copy_to_user(regs->saved_stack, (void *)vcpu, IA64_STK_OFFSET);
1400 if (r)
1401 goto out;
1402 SAVE_REGS(mp_state);
1403 SAVE_REGS(vmm_rr);
1404 memcpy(regs->itrs, vcpu->arch.itrs, sizeof(struct thash_data) * NITRS);
1405 memcpy(regs->dtrs, vcpu->arch.dtrs, sizeof(struct thash_data) * NDTRS);
1406 SAVE_REGS(itr_regions);
1407 SAVE_REGS(dtr_regions);
1408 SAVE_REGS(tc_regions);
1409 SAVE_REGS(irq_check);
1410 SAVE_REGS(itc_check);
1411 SAVE_REGS(timer_check);
1412 SAVE_REGS(timer_pending);
1413 SAVE_REGS(last_itc);
1414 for (i = 0; i < 8; i++) {
1415 regs->vrr[i] = vcpu->arch.vrr[i];
1416 regs->ibr[i] = vcpu->arch.ibr[i];
1417 regs->dbr[i] = vcpu->arch.dbr[i];
1418 }
1419 for (i = 0; i < 4; i++)
1420 regs->insvc[i] = vcpu->arch.insvc[i];
1421 regs->saved_itc = vcpu->arch.itc_offset + ia64_getreg(_IA64_REG_AR_ITC);
1422 SAVE_REGS(xtp);
1423 SAVE_REGS(metaphysical_rr0);
1424 SAVE_REGS(metaphysical_rr4);
1425 SAVE_REGS(metaphysical_saved_rr0);
1426 SAVE_REGS(metaphysical_saved_rr4);
1427 SAVE_REGS(fp_psr);
1428 SAVE_REGS(saved_gp);
1429 vcpu_put(vcpu);
1430 r = 0;
1431out:
1432 return r;
1433}
1434
1435void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1436{
1437
1438 hrtimer_cancel(&vcpu->arch.hlt_timer);
1439 kfree(vcpu->arch.apic);
1440}
1441
1442
1443long kvm_arch_vcpu_ioctl(struct file *filp,
1444 unsigned int ioctl, unsigned long arg)
1445{
1446 return -EINVAL;
1447}
1448
1449int kvm_arch_set_memory_region(struct kvm *kvm,
1450 struct kvm_userspace_memory_region *mem,
1451 struct kvm_memory_slot old,
1452 int user_alloc)
1453{
1454 unsigned long i;
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001455 unsigned long pfn;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001456 int npages = mem->memory_size >> PAGE_SHIFT;
1457 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
1458 unsigned long base_gfn = memslot->base_gfn;
1459
1460 for (i = 0; i < npages; i++) {
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001461 pfn = gfn_to_pfn(kvm, base_gfn + i);
1462 if (!kvm_is_mmio_pfn(pfn)) {
1463 kvm_set_pmt_entry(kvm, base_gfn + i,
1464 pfn << PAGE_SHIFT,
Xiantao Zhangb010eb52008-09-28 01:39:46 -07001465 _PAGE_AR_RWX | _PAGE_MA_WB);
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001466 memslot->rmap[i] = (unsigned long)pfn_to_page(pfn);
1467 } else {
1468 kvm_set_pmt_entry(kvm, base_gfn + i,
Xiantao Zhangb010eb52008-09-28 01:39:46 -07001469 GPFN_PHYS_MMIO | (pfn << PAGE_SHIFT),
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001470 _PAGE_MA_UC);
1471 memslot->rmap[i] = 0;
1472 }
Xiantao Zhangb024b792008-04-01 15:29:29 +08001473 }
1474
1475 return 0;
1476}
1477
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001478void kvm_arch_flush_shadow(struct kvm *kvm)
1479{
1480}
Xiantao Zhangb024b792008-04-01 15:29:29 +08001481
1482long kvm_arch_dev_ioctl(struct file *filp,
1483 unsigned int ioctl, unsigned long arg)
1484{
1485 return -EINVAL;
1486}
1487
1488void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1489{
1490 kvm_vcpu_uninit(vcpu);
1491}
1492
1493static int vti_cpu_has_kvm_support(void)
1494{
1495 long avail = 1, status = 1, control = 1;
1496 long ret;
1497
1498 ret = ia64_pal_proc_get_features(&avail, &status, &control, 0);
1499 if (ret)
1500 goto out;
1501
1502 if (!(avail & PAL_PROC_VM_BIT))
1503 goto out;
1504
1505 printk(KERN_DEBUG"kvm: Hardware Supports VT\n");
1506
1507 ret = ia64_pal_vp_env_info(&kvm_vm_buffer_size, &vp_env_info);
1508 if (ret)
1509 goto out;
1510 printk(KERN_DEBUG"kvm: VM Buffer Size:0x%lx\n", kvm_vm_buffer_size);
1511
1512 if (!(vp_env_info & VP_OPCODE)) {
1513 printk(KERN_WARNING"kvm: No opcode ability on hardware, "
1514 "vm_env_info:0x%lx\n", vp_env_info);
1515 }
1516
1517 return 1;
1518out:
1519 return 0;
1520}
1521
1522static int kvm_relocate_vmm(struct kvm_vmm_info *vmm_info,
1523 struct module *module)
1524{
1525 unsigned long module_base;
1526 unsigned long vmm_size;
1527
1528 unsigned long vmm_offset, func_offset, fdesc_offset;
1529 struct fdesc *p_fdesc;
1530
1531 BUG_ON(!module);
1532
1533 if (!kvm_vmm_base) {
1534 printk("kvm: kvm area hasn't been initilized yet!!\n");
1535 return -EFAULT;
1536 }
1537
1538 /*Calculate new position of relocated vmm module.*/
1539 module_base = (unsigned long)module->module_core;
1540 vmm_size = module->core_size;
1541 if (unlikely(vmm_size > KVM_VMM_SIZE))
1542 return -EFAULT;
1543
1544 memcpy((void *)kvm_vmm_base, (void *)module_base, vmm_size);
1545 kvm_flush_icache(kvm_vmm_base, vmm_size);
1546
1547 /*Recalculate kvm_vmm_info based on new VMM*/
1548 vmm_offset = vmm_info->vmm_ivt - module_base;
1549 kvm_vmm_info->vmm_ivt = KVM_VMM_BASE + vmm_offset;
1550 printk(KERN_DEBUG"kvm: Relocated VMM's IVT Base Addr:%lx\n",
1551 kvm_vmm_info->vmm_ivt);
1552
1553 fdesc_offset = (unsigned long)vmm_info->vmm_entry - module_base;
1554 kvm_vmm_info->vmm_entry = (kvm_vmm_entry *)(KVM_VMM_BASE +
1555 fdesc_offset);
1556 func_offset = *(unsigned long *)vmm_info->vmm_entry - module_base;
1557 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1558 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1559 p_fdesc->gp = KVM_VMM_BASE+(p_fdesc->gp - module_base);
1560
1561 printk(KERN_DEBUG"kvm: Relocated VMM's Init Entry Addr:%lx\n",
1562 KVM_VMM_BASE+func_offset);
1563
1564 fdesc_offset = (unsigned long)vmm_info->tramp_entry - module_base;
1565 kvm_vmm_info->tramp_entry = (kvm_tramp_entry *)(KVM_VMM_BASE +
1566 fdesc_offset);
1567 func_offset = *(unsigned long *)vmm_info->tramp_entry - module_base;
1568 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1569 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1570 p_fdesc->gp = KVM_VMM_BASE + (p_fdesc->gp - module_base);
1571
1572 kvm_vmm_gp = p_fdesc->gp;
1573
1574 printk(KERN_DEBUG"kvm: Relocated VMM's Entry IP:%p\n",
1575 kvm_vmm_info->vmm_entry);
1576 printk(KERN_DEBUG"kvm: Relocated VMM's Trampoline Entry IP:0x%lx\n",
1577 KVM_VMM_BASE + func_offset);
1578
1579 return 0;
1580}
1581
1582int kvm_arch_init(void *opaque)
1583{
1584 int r;
1585 struct kvm_vmm_info *vmm_info = (struct kvm_vmm_info *)opaque;
1586
1587 if (!vti_cpu_has_kvm_support()) {
1588 printk(KERN_ERR "kvm: No Hardware Virtualization Support!\n");
1589 r = -EOPNOTSUPP;
1590 goto out;
1591 }
1592
1593 if (kvm_vmm_info) {
1594 printk(KERN_ERR "kvm: Already loaded VMM module!\n");
1595 r = -EEXIST;
1596 goto out;
1597 }
1598
1599 r = -ENOMEM;
1600 kvm_vmm_info = kzalloc(sizeof(struct kvm_vmm_info), GFP_KERNEL);
1601 if (!kvm_vmm_info)
1602 goto out;
1603
1604 if (kvm_alloc_vmm_area())
1605 goto out_free0;
1606
1607 r = kvm_relocate_vmm(vmm_info, vmm_info->module);
1608 if (r)
1609 goto out_free1;
1610
1611 return 0;
1612
1613out_free1:
1614 kvm_free_vmm_area();
1615out_free0:
1616 kfree(kvm_vmm_info);
1617out:
1618 return r;
1619}
1620
1621void kvm_arch_exit(void)
1622{
1623 kvm_free_vmm_area();
1624 kfree(kvm_vmm_info);
1625 kvm_vmm_info = NULL;
1626}
1627
1628static int kvm_ia64_sync_dirty_log(struct kvm *kvm,
1629 struct kvm_dirty_log *log)
1630{
1631 struct kvm_memory_slot *memslot;
1632 int r, i;
1633 long n, base;
1634 unsigned long *dirty_bitmap = (unsigned long *)((void *)kvm - KVM_VM_OFS
1635 + KVM_MEM_DIRTY_LOG_OFS);
1636
1637 r = -EINVAL;
1638 if (log->slot >= KVM_MEMORY_SLOTS)
1639 goto out;
1640
1641 memslot = &kvm->memslots[log->slot];
1642 r = -ENOENT;
1643 if (!memslot->dirty_bitmap)
1644 goto out;
1645
1646 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1647 base = memslot->base_gfn / BITS_PER_LONG;
1648
1649 for (i = 0; i < n/sizeof(long); ++i) {
1650 memslot->dirty_bitmap[i] = dirty_bitmap[base + i];
1651 dirty_bitmap[base + i] = 0;
1652 }
1653 r = 0;
1654out:
1655 return r;
1656}
1657
1658int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1659 struct kvm_dirty_log *log)
1660{
1661 int r;
1662 int n;
1663 struct kvm_memory_slot *memslot;
1664 int is_dirty = 0;
1665
1666 spin_lock(&kvm->arch.dirty_log_lock);
1667
1668 r = kvm_ia64_sync_dirty_log(kvm, log);
1669 if (r)
1670 goto out;
1671
1672 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1673 if (r)
1674 goto out;
1675
1676 /* If nothing is dirty, don't bother messing with page tables. */
1677 if (is_dirty) {
1678 kvm_flush_remote_tlbs(kvm);
1679 memslot = &kvm->memslots[log->slot];
1680 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1681 memset(memslot->dirty_bitmap, 0, n);
1682 }
1683 r = 0;
1684out:
1685 spin_unlock(&kvm->arch.dirty_log_lock);
1686 return r;
1687}
1688
1689int kvm_arch_hardware_setup(void)
1690{
1691 return 0;
1692}
1693
1694void kvm_arch_hardware_unsetup(void)
1695{
1696}
1697
1698static void vcpu_kick_intr(void *info)
1699{
1700#ifdef DEBUG
1701 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
1702 printk(KERN_DEBUG"vcpu_kick_intr %p \n", vcpu);
1703#endif
1704}
1705
1706void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1707{
1708 int ipi_pcpu = vcpu->cpu;
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001709 int cpu = get_cpu();
Xiantao Zhangb024b792008-04-01 15:29:29 +08001710
1711 if (waitqueue_active(&vcpu->wq))
1712 wake_up_interruptible(&vcpu->wq);
1713
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001714 if (vcpu->guest_mode && cpu != ipi_pcpu)
Takashi Iwai2f73cca2008-07-17 18:09:12 +02001715 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001716 put_cpu();
Xiantao Zhangb024b792008-04-01 15:29:29 +08001717}
1718
1719int kvm_apic_set_irq(struct kvm_vcpu *vcpu, u8 vec, u8 trig)
1720{
1721
1722 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1723
1724 if (!test_and_set_bit(vec, &vpd->irr[0])) {
1725 vcpu->arch.irq_new_pending = 1;
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001726 kvm_vcpu_kick(vcpu);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001727 return 1;
1728 }
1729 return 0;
1730}
1731
1732int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
1733{
1734 return apic->vcpu->vcpu_id == dest;
1735}
1736
1737int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
1738{
1739 return 0;
1740}
1741
1742struct kvm_vcpu *kvm_get_lowest_prio_vcpu(struct kvm *kvm, u8 vector,
1743 unsigned long bitmap)
1744{
1745 struct kvm_vcpu *lvcpu = kvm->vcpus[0];
1746 int i;
1747
1748 for (i = 1; i < KVM_MAX_VCPUS; i++) {
1749 if (!kvm->vcpus[i])
1750 continue;
1751 if (lvcpu->arch.xtp > kvm->vcpus[i]->arch.xtp)
1752 lvcpu = kvm->vcpus[i];
1753 }
1754
1755 return lvcpu;
1756}
1757
1758static int find_highest_bits(int *dat)
1759{
1760 u32 bits, bitnum;
1761 int i;
1762
1763 /* loop for all 256 bits */
1764 for (i = 7; i >= 0 ; i--) {
1765 bits = dat[i];
1766 if (bits) {
1767 bitnum = fls(bits);
1768 return i * 32 + bitnum - 1;
1769 }
1770 }
1771
1772 return -1;
1773}
1774
1775int kvm_highest_pending_irq(struct kvm_vcpu *vcpu)
1776{
1777 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1778
1779 if (vpd->irr[0] & (1UL << NMI_VECTOR))
1780 return NMI_VECTOR;
1781 if (vpd->irr[0] & (1UL << ExtINT_VECTOR))
1782 return ExtINT_VECTOR;
1783
1784 return find_highest_bits((int *)&vpd->irr[0]);
1785}
1786
1787int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
1788{
1789 if (kvm_highest_pending_irq(vcpu) != -1)
1790 return 1;
1791 return 0;
1792}
1793
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001794int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1795{
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001796 return vcpu->arch.timer_fired;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001797}
1798
Xiantao Zhangb024b792008-04-01 15:29:29 +08001799gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1800{
1801 return gfn;
1802}
1803
1804int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1805{
Avi Kivitya4535292008-04-13 17:54:35 +03001806 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001807}
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001808
1809int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1810 struct kvm_mp_state *mp_state)
1811{
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001812 vcpu_load(vcpu);
1813 mp_state->mp_state = vcpu->arch.mp_state;
1814 vcpu_put(vcpu);
1815 return 0;
1816}
1817
1818static int vcpu_reset(struct kvm_vcpu *vcpu)
1819{
1820 int r;
1821 long psr;
1822 local_irq_save(psr);
1823 r = kvm_insert_vmm_mapping(vcpu);
1824 if (r)
1825 goto fail;
1826
1827 vcpu->arch.launched = 0;
1828 kvm_arch_vcpu_uninit(vcpu);
1829 r = kvm_arch_vcpu_init(vcpu);
1830 if (r)
1831 goto fail;
1832
1833 kvm_purge_vmm_mapping(vcpu);
1834 r = 0;
1835fail:
1836 local_irq_restore(psr);
1837 return r;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001838}
1839
1840int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1841 struct kvm_mp_state *mp_state)
1842{
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001843 int r = 0;
1844
1845 vcpu_load(vcpu);
1846 vcpu->arch.mp_state = mp_state->mp_state;
1847 if (vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)
1848 r = vcpu_reset(vcpu);
1849 vcpu_put(vcpu);
1850 return r;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001851}