blob: 8a2b13ff0aff2a069f667f1db7e6215e20063fd3 [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;
388 call_data.ptc_g_data = p->u.ptc_g_data;
389
390 for (i = 0; i < KVM_MAX_VCPUS; i++) {
391 if (!kvm->vcpus[i] || kvm->vcpus[i]->arch.mp_state ==
Avi Kivitya4535292008-04-13 17:54:35 +0300392 KVM_MP_STATE_UNINITIALIZED ||
Xiantao Zhangb024b792008-04-01 15:29:29 +0800393 vcpu == kvm->vcpus[i])
394 continue;
395
396 if (waitqueue_active(&kvm->vcpus[i]->wq))
397 wake_up_interruptible(&kvm->vcpus[i]->wq);
398
399 if (kvm->vcpus[i]->cpu != -1) {
400 call_data.vcpu = kvm->vcpus[i];
401 smp_call_function_single(kvm->vcpus[i]->cpu,
Takashi Iwai2f73cca2008-07-17 18:09:12 +0200402 vcpu_global_purge, &call_data, 1);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800403 } else
404 printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n");
405
406 }
407 return 1;
408}
409
410static int handle_switch_rr6(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
411{
412 return 1;
413}
414
415int kvm_emulate_halt(struct kvm_vcpu *vcpu)
416{
417
418 ktime_t kt;
419 long itc_diff;
420 unsigned long vcpu_now_itc;
421
422 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
427 vcpu_now_itc = ia64_getreg(_IA64_REG_AR_ITC) + vcpu->arch.itc_offset;
428
429 if (time_after(vcpu_now_itc, vpd->itm)) {
430 vcpu->arch.timer_check = 1;
431 return 1;
432 }
433 itc_diff = vpd->itm - vcpu_now_itc;
434 if (itc_diff < 0)
435 itc_diff = -itc_diff;
436
Roman Zippel6f6d6a12008-05-01 04:34:28 -0700437 expires = div64_u64(itc_diff, cyc_per_usec);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800438 kt = ktime_set(0, 1000 * expires);
439 vcpu->arch.ht_active = 1;
440 hrtimer_start(p_ht, kt, HRTIMER_MODE_ABS);
441
442 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +0300443 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800444 kvm_vcpu_block(vcpu);
445 hrtimer_cancel(p_ht);
446 vcpu->arch.ht_active = 0;
447
Avi Kivitya4535292008-04-13 17:54:35 +0300448 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800449 return -EINTR;
450 return 1;
451 } else {
452 printk(KERN_ERR"kvm: Unsupported userspace halt!");
453 return 0;
454 }
455}
456
457static int handle_vm_shutdown(struct kvm_vcpu *vcpu,
458 struct kvm_run *kvm_run)
459{
460 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
461 return 0;
462}
463
464static int handle_external_interrupt(struct kvm_vcpu *vcpu,
465 struct kvm_run *kvm_run)
466{
467 return 1;
468}
469
470static int (*kvm_vti_exit_handlers[])(struct kvm_vcpu *vcpu,
471 struct kvm_run *kvm_run) = {
472 [EXIT_REASON_VM_PANIC] = handle_vm_error,
473 [EXIT_REASON_MMIO_INSTRUCTION] = handle_mmio,
474 [EXIT_REASON_PAL_CALL] = handle_pal_call,
475 [EXIT_REASON_SAL_CALL] = handle_sal_call,
476 [EXIT_REASON_SWITCH_RR6] = handle_switch_rr6,
477 [EXIT_REASON_VM_DESTROY] = handle_vm_shutdown,
478 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
479 [EXIT_REASON_IPI] = handle_ipi,
480 [EXIT_REASON_PTC_G] = handle_global_purge,
481
482};
483
484static const int kvm_vti_max_exit_handlers =
485 sizeof(kvm_vti_exit_handlers)/sizeof(*kvm_vti_exit_handlers);
486
487static void kvm_prepare_guest_switch(struct kvm_vcpu *vcpu)
488{
489}
490
491static uint32_t kvm_get_exit_reason(struct kvm_vcpu *vcpu)
492{
493 struct exit_ctl_data *p_exit_data;
494
495 p_exit_data = kvm_get_exit_data(vcpu);
496 return p_exit_data->exit_reason;
497}
498
499/*
500 * The guest has exited. See if we can fix it or if we need userspace
501 * assistance.
502 */
503static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
504{
505 u32 exit_reason = kvm_get_exit_reason(vcpu);
506 vcpu->arch.last_exit = exit_reason;
507
508 if (exit_reason < kvm_vti_max_exit_handlers
509 && kvm_vti_exit_handlers[exit_reason])
510 return kvm_vti_exit_handlers[exit_reason](vcpu, kvm_run);
511 else {
512 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
513 kvm_run->hw.hardware_exit_reason = exit_reason;
514 }
515 return 0;
516}
517
518static inline void vti_set_rr6(unsigned long rr6)
519{
520 ia64_set_rr(RR6, rr6);
521 ia64_srlz_i();
522}
523
524static int kvm_insert_vmm_mapping(struct kvm_vcpu *vcpu)
525{
526 unsigned long pte;
527 struct kvm *kvm = vcpu->kvm;
528 int r;
529
530 /*Insert a pair of tr to map vmm*/
531 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
532 r = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
533 if (r < 0)
534 goto out;
535 vcpu->arch.vmm_tr_slot = r;
536 /*Insert a pairt of tr to map data of vm*/
537 pte = pte_val(mk_pte_phys(__pa(kvm->arch.vm_base), PAGE_KERNEL));
538 r = ia64_itr_entry(0x3, KVM_VM_DATA_BASE,
539 pte, KVM_VM_DATA_SHIFT);
540 if (r < 0)
541 goto out;
542 vcpu->arch.vm_tr_slot = r;
543 r = 0;
544out:
545 return r;
546
547}
548
549static void kvm_purge_vmm_mapping(struct kvm_vcpu *vcpu)
550{
551
552 ia64_ptr_entry(0x3, vcpu->arch.vmm_tr_slot);
553 ia64_ptr_entry(0x3, vcpu->arch.vm_tr_slot);
554
555}
556
557static int kvm_vcpu_pre_transition(struct kvm_vcpu *vcpu)
558{
559 int cpu = smp_processor_id();
560
561 if (vcpu->arch.last_run_cpu != cpu ||
562 per_cpu(last_vcpu, cpu) != vcpu) {
563 per_cpu(last_vcpu, cpu) = vcpu;
564 vcpu->arch.last_run_cpu = cpu;
565 kvm_flush_tlb_all();
566 }
567
568 vcpu->arch.host_rr6 = ia64_get_rr(RR6);
569 vti_set_rr6(vcpu->arch.vmm_rr);
570 return kvm_insert_vmm_mapping(vcpu);
571}
572static void kvm_vcpu_post_transition(struct kvm_vcpu *vcpu)
573{
574 kvm_purge_vmm_mapping(vcpu);
575 vti_set_rr6(vcpu->arch.host_rr6);
576}
577
578static int vti_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
579{
580 union context *host_ctx, *guest_ctx;
581 int r;
582
583 /*Get host and guest context with guest address space.*/
584 host_ctx = kvm_get_host_context(vcpu);
585 guest_ctx = kvm_get_guest_context(vcpu);
586
587 r = kvm_vcpu_pre_transition(vcpu);
588 if (r < 0)
589 goto out;
590 kvm_vmm_info->tramp_entry(host_ctx, guest_ctx);
591 kvm_vcpu_post_transition(vcpu);
592 r = 0;
593out:
594 return r;
595}
596
597static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
598{
599 int r;
600
601again:
602 preempt_disable();
603
604 kvm_prepare_guest_switch(vcpu);
605 local_irq_disable();
606
607 if (signal_pending(current)) {
608 local_irq_enable();
609 preempt_enable();
610 r = -EINTR;
611 kvm_run->exit_reason = KVM_EXIT_INTR;
612 goto out;
613 }
614
615 vcpu->guest_mode = 1;
616 kvm_guest_enter();
617
618 r = vti_vcpu_run(vcpu, kvm_run);
619 if (r < 0) {
620 local_irq_enable();
621 preempt_enable();
622 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
623 goto out;
624 }
625
626 vcpu->arch.launched = 1;
627 vcpu->guest_mode = 0;
628 local_irq_enable();
629
630 /*
631 * We must have an instruction between local_irq_enable() and
632 * kvm_guest_exit(), so the timer interrupt isn't delayed by
633 * the interrupt shadow. The stat.exits increment will do nicely.
634 * But we need to prevent reordering, hence this barrier():
635 */
636 barrier();
637
638 kvm_guest_exit();
639
640 preempt_enable();
641
642 r = kvm_handle_exit(kvm_run, vcpu);
643
644 if (r > 0) {
645 if (!need_resched())
646 goto again;
647 }
648
649out:
650 if (r > 0) {
651 kvm_resched(vcpu);
652 goto again;
653 }
654
655 return r;
656}
657
658static void kvm_set_mmio_data(struct kvm_vcpu *vcpu)
659{
660 struct kvm_mmio_req *p = kvm_get_vcpu_ioreq(vcpu);
661
662 if (!vcpu->mmio_is_write)
663 memcpy(&p->data, vcpu->mmio_data, 8);
664 p->state = STATE_IORESP_READY;
665}
666
667int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
668{
669 int r;
670 sigset_t sigsaved;
671
672 vcpu_load(vcpu);
673
Avi Kivitya4535292008-04-13 17:54:35 +0300674 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800675 kvm_vcpu_block(vcpu);
676 vcpu_put(vcpu);
677 return -EAGAIN;
678 }
679
680 if (vcpu->sigset_active)
681 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
682
683 if (vcpu->mmio_needed) {
684 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
685 kvm_set_mmio_data(vcpu);
686 vcpu->mmio_read_completed = 1;
687 vcpu->mmio_needed = 0;
688 }
689 r = __vcpu_run(vcpu, kvm_run);
690
691 if (vcpu->sigset_active)
692 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
693
694 vcpu_put(vcpu);
695 return r;
696}
697
698/*
699 * Allocate 16M memory for every vm to hold its specific data.
700 * Its memory map is defined in kvm_host.h.
701 */
702static struct kvm *kvm_alloc_kvm(void)
703{
704
705 struct kvm *kvm;
706 uint64_t vm_base;
707
708 vm_base = __get_free_pages(GFP_KERNEL, get_order(KVM_VM_DATA_SIZE));
709
710 if (!vm_base)
711 return ERR_PTR(-ENOMEM);
712 printk(KERN_DEBUG"kvm: VM data's base Address:0x%lx\n", vm_base);
713
714 /* Zero all pages before use! */
715 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
716
717 kvm = (struct kvm *)(vm_base + KVM_VM_OFS);
718 kvm->arch.vm_base = vm_base;
719
720 return kvm;
721}
722
723struct kvm_io_range {
724 unsigned long start;
725 unsigned long size;
726 unsigned long type;
727};
728
729static const struct kvm_io_range io_ranges[] = {
730 {VGA_IO_START, VGA_IO_SIZE, GPFN_FRAME_BUFFER},
731 {MMIO_START, MMIO_SIZE, GPFN_LOW_MMIO},
732 {LEGACY_IO_START, LEGACY_IO_SIZE, GPFN_LEGACY_IO},
733 {IO_SAPIC_START, IO_SAPIC_SIZE, GPFN_IOSAPIC},
734 {PIB_START, PIB_SIZE, GPFN_PIB},
735};
736
737static void kvm_build_io_pmt(struct kvm *kvm)
738{
739 unsigned long i, j;
740
741 /* Mark I/O ranges */
742 for (i = 0; i < (sizeof(io_ranges) / sizeof(struct kvm_io_range));
743 i++) {
744 for (j = io_ranges[i].start;
745 j < io_ranges[i].start + io_ranges[i].size;
746 j += PAGE_SIZE)
747 kvm_set_pmt_entry(kvm, j >> PAGE_SHIFT,
748 io_ranges[i].type, 0);
749 }
750
751}
752
753/*Use unused rids to virtualize guest rid.*/
754#define GUEST_PHYSICAL_RR0 0x1739
755#define GUEST_PHYSICAL_RR4 0x2739
756#define VMM_INIT_RR 0x1660
757
758static void kvm_init_vm(struct kvm *kvm)
759{
760 long vm_base;
761
762 BUG_ON(!kvm);
763
764 kvm->arch.metaphysical_rr0 = GUEST_PHYSICAL_RR0;
765 kvm->arch.metaphysical_rr4 = GUEST_PHYSICAL_RR4;
766 kvm->arch.vmm_init_rr = VMM_INIT_RR;
767
768 vm_base = kvm->arch.vm_base;
769 if (vm_base) {
770 kvm->arch.vhpt_base = vm_base + KVM_VHPT_OFS;
771 kvm->arch.vtlb_base = vm_base + KVM_VTLB_OFS;
772 kvm->arch.vpd_base = vm_base + KVM_VPD_OFS;
773 }
774
775 /*
776 *Fill P2M entries for MMIO/IO ranges
777 */
778 kvm_build_io_pmt(kvm);
779
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800780 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Sheng Yang5550af42008-10-15 20:15:06 +0800781
782 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
783 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800784}
785
786struct kvm *kvm_arch_create_vm(void)
787{
788 struct kvm *kvm = kvm_alloc_kvm();
789
790 if (IS_ERR(kvm))
791 return ERR_PTR(-ENOMEM);
792 kvm_init_vm(kvm);
793
794 return kvm;
795
796}
797
798static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm,
799 struct kvm_irqchip *chip)
800{
801 int r;
802
803 r = 0;
804 switch (chip->chip_id) {
805 case KVM_IRQCHIP_IOAPIC:
806 memcpy(&chip->chip.ioapic, ioapic_irqchip(kvm),
807 sizeof(struct kvm_ioapic_state));
808 break;
809 default:
810 r = -EINVAL;
811 break;
812 }
813 return r;
814}
815
816static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
817{
818 int r;
819
820 r = 0;
821 switch (chip->chip_id) {
822 case KVM_IRQCHIP_IOAPIC:
823 memcpy(ioapic_irqchip(kvm),
824 &chip->chip.ioapic,
825 sizeof(struct kvm_ioapic_state));
826 break;
827 default:
828 r = -EINVAL;
829 break;
830 }
831 return r;
832}
833
834#define RESTORE_REGS(_x) vcpu->arch._x = regs->_x
835
836int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
837{
838 int i;
839 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
840 int r;
841
842 vcpu_load(vcpu);
843
844 for (i = 0; i < 16; i++) {
845 vpd->vgr[i] = regs->vpd.vgr[i];
846 vpd->vbgr[i] = regs->vpd.vbgr[i];
847 }
848 for (i = 0; i < 128; i++)
849 vpd->vcr[i] = regs->vpd.vcr[i];
850 vpd->vhpi = regs->vpd.vhpi;
851 vpd->vnat = regs->vpd.vnat;
852 vpd->vbnat = regs->vpd.vbnat;
853 vpd->vpsr = regs->vpd.vpsr;
854
855 vpd->vpr = regs->vpd.vpr;
856
857 r = -EFAULT;
858 r = copy_from_user(&vcpu->arch.guest, regs->saved_guest,
859 sizeof(union context));
860 if (r)
861 goto out;
862 r = copy_from_user(vcpu + 1, regs->saved_stack +
863 sizeof(struct kvm_vcpu),
864 IA64_STK_OFFSET - sizeof(struct kvm_vcpu));
865 if (r)
866 goto out;
867 vcpu->arch.exit_data =
868 ((struct kvm_vcpu *)(regs->saved_stack))->arch.exit_data;
869
870 RESTORE_REGS(mp_state);
871 RESTORE_REGS(vmm_rr);
872 memcpy(vcpu->arch.itrs, regs->itrs, sizeof(struct thash_data) * NITRS);
873 memcpy(vcpu->arch.dtrs, regs->dtrs, sizeof(struct thash_data) * NDTRS);
874 RESTORE_REGS(itr_regions);
875 RESTORE_REGS(dtr_regions);
876 RESTORE_REGS(tc_regions);
877 RESTORE_REGS(irq_check);
878 RESTORE_REGS(itc_check);
879 RESTORE_REGS(timer_check);
880 RESTORE_REGS(timer_pending);
881 RESTORE_REGS(last_itc);
882 for (i = 0; i < 8; i++) {
883 vcpu->arch.vrr[i] = regs->vrr[i];
884 vcpu->arch.ibr[i] = regs->ibr[i];
885 vcpu->arch.dbr[i] = regs->dbr[i];
886 }
887 for (i = 0; i < 4; i++)
888 vcpu->arch.insvc[i] = regs->insvc[i];
889 RESTORE_REGS(xtp);
890 RESTORE_REGS(metaphysical_rr0);
891 RESTORE_REGS(metaphysical_rr4);
892 RESTORE_REGS(metaphysical_saved_rr0);
893 RESTORE_REGS(metaphysical_saved_rr4);
894 RESTORE_REGS(fp_psr);
895 RESTORE_REGS(saved_gp);
896
897 vcpu->arch.irq_new_pending = 1;
898 vcpu->arch.itc_offset = regs->saved_itc - ia64_getreg(_IA64_REG_AR_ITC);
899 set_bit(KVM_REQ_RESUME, &vcpu->requests);
900
901 vcpu_put(vcpu);
902 r = 0;
903out:
904 return r;
905}
906
907long kvm_arch_vm_ioctl(struct file *filp,
908 unsigned int ioctl, unsigned long arg)
909{
910 struct kvm *kvm = filp->private_data;
911 void __user *argp = (void __user *)arg;
912 int r = -EINVAL;
913
914 switch (ioctl) {
915 case KVM_SET_MEMORY_REGION: {
916 struct kvm_memory_region kvm_mem;
917 struct kvm_userspace_memory_region kvm_userspace_mem;
918
919 r = -EFAULT;
920 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
921 goto out;
922 kvm_userspace_mem.slot = kvm_mem.slot;
923 kvm_userspace_mem.flags = kvm_mem.flags;
924 kvm_userspace_mem.guest_phys_addr =
925 kvm_mem.guest_phys_addr;
926 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
927 r = kvm_vm_ioctl_set_memory_region(kvm,
928 &kvm_userspace_mem, 0);
929 if (r)
930 goto out;
931 break;
932 }
933 case KVM_CREATE_IRQCHIP:
934 r = -EFAULT;
935 r = kvm_ioapic_init(kvm);
936 if (r)
937 goto out;
938 break;
939 case KVM_IRQ_LINE: {
940 struct kvm_irq_level irq_event;
941
942 r = -EFAULT;
943 if (copy_from_user(&irq_event, argp, sizeof irq_event))
944 goto out;
945 if (irqchip_in_kernel(kvm)) {
946 mutex_lock(&kvm->lock);
Sheng Yang5550af42008-10-15 20:15:06 +0800947 kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
948 irq_event.irq, irq_event.level);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800949 mutex_unlock(&kvm->lock);
950 r = 0;
951 }
952 break;
953 }
954 case KVM_GET_IRQCHIP: {
955 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
956 struct kvm_irqchip chip;
957
958 r = -EFAULT;
959 if (copy_from_user(&chip, argp, sizeof chip))
960 goto out;
961 r = -ENXIO;
962 if (!irqchip_in_kernel(kvm))
963 goto out;
964 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
965 if (r)
966 goto out;
967 r = -EFAULT;
968 if (copy_to_user(argp, &chip, sizeof chip))
969 goto out;
970 r = 0;
971 break;
972 }
973 case KVM_SET_IRQCHIP: {
974 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
975 struct kvm_irqchip chip;
976
977 r = -EFAULT;
978 if (copy_from_user(&chip, argp, sizeof chip))
979 goto out;
980 r = -ENXIO;
981 if (!irqchip_in_kernel(kvm))
982 goto out;
983 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
984 if (r)
985 goto out;
986 r = 0;
987 break;
988 }
989 default:
990 ;
991 }
992out:
993 return r;
994}
995
996int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
997 struct kvm_sregs *sregs)
998{
999 return -EINVAL;
1000}
1001
1002int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1003 struct kvm_sregs *sregs)
1004{
1005 return -EINVAL;
1006
1007}
1008int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1009 struct kvm_translation *tr)
1010{
1011
1012 return -EINVAL;
1013}
1014
1015static int kvm_alloc_vmm_area(void)
1016{
1017 if (!kvm_vmm_base && (kvm_vm_buffer_size < KVM_VM_BUFFER_SIZE)) {
1018 kvm_vmm_base = __get_free_pages(GFP_KERNEL,
1019 get_order(KVM_VMM_SIZE));
1020 if (!kvm_vmm_base)
1021 return -ENOMEM;
1022
1023 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1024 kvm_vm_buffer = kvm_vmm_base + VMM_SIZE;
1025
1026 printk(KERN_DEBUG"kvm:VMM's Base Addr:0x%lx, vm_buffer:0x%lx\n",
1027 kvm_vmm_base, kvm_vm_buffer);
1028 }
1029
1030 return 0;
1031}
1032
1033static void kvm_free_vmm_area(void)
1034{
1035 if (kvm_vmm_base) {
1036 /*Zero this area before free to avoid bits leak!!*/
1037 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1038 free_pages(kvm_vmm_base, get_order(KVM_VMM_SIZE));
1039 kvm_vmm_base = 0;
1040 kvm_vm_buffer = 0;
1041 kvm_vsa_base = 0;
1042 }
1043}
1044
Xiantao Zhangb024b792008-04-01 15:29:29 +08001045static void vti_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1046{
1047}
1048
1049static int vti_init_vpd(struct kvm_vcpu *vcpu)
1050{
1051 int i;
1052 union cpuid3_t cpuid3;
1053 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1054
1055 if (IS_ERR(vpd))
1056 return PTR_ERR(vpd);
1057
1058 /* CPUID init */
1059 for (i = 0; i < 5; i++)
1060 vpd->vcpuid[i] = ia64_get_cpuid(i);
1061
1062 /* Limit the CPUID number to 5 */
1063 cpuid3.value = vpd->vcpuid[3];
1064 cpuid3.number = 4; /* 5 - 1 */
1065 vpd->vcpuid[3] = cpuid3.value;
1066
1067 /*Set vac and vdc fields*/
1068 vpd->vac.a_from_int_cr = 1;
1069 vpd->vac.a_to_int_cr = 1;
1070 vpd->vac.a_from_psr = 1;
1071 vpd->vac.a_from_cpuid = 1;
1072 vpd->vac.a_cover = 1;
1073 vpd->vac.a_bsw = 1;
1074 vpd->vac.a_int = 1;
1075 vpd->vdc.d_vmsw = 1;
1076
1077 /*Set virtual buffer*/
1078 vpd->virt_env_vaddr = KVM_VM_BUFFER_BASE;
1079
1080 return 0;
1081}
1082
1083static int vti_create_vp(struct kvm_vcpu *vcpu)
1084{
1085 long ret;
1086 struct vpd *vpd = vcpu->arch.vpd;
1087 unsigned long vmm_ivt;
1088
1089 vmm_ivt = kvm_vmm_info->vmm_ivt;
1090
1091 printk(KERN_DEBUG "kvm: vcpu:%p,ivt: 0x%lx\n", vcpu, vmm_ivt);
1092
1093 ret = ia64_pal_vp_create((u64 *)vpd, (u64 *)vmm_ivt, 0);
1094
1095 if (ret) {
1096 printk(KERN_ERR"kvm: ia64_pal_vp_create failed!\n");
1097 return -EINVAL;
1098 }
1099 return 0;
1100}
1101
1102static void init_ptce_info(struct kvm_vcpu *vcpu)
1103{
1104 ia64_ptce_info_t ptce = {0};
1105
1106 ia64_get_ptce(&ptce);
1107 vcpu->arch.ptce_base = ptce.base;
1108 vcpu->arch.ptce_count[0] = ptce.count[0];
1109 vcpu->arch.ptce_count[1] = ptce.count[1];
1110 vcpu->arch.ptce_stride[0] = ptce.stride[0];
1111 vcpu->arch.ptce_stride[1] = ptce.stride[1];
1112}
1113
1114static void kvm_migrate_hlt_timer(struct kvm_vcpu *vcpu)
1115{
1116 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
1117
1118 if (hrtimer_cancel(p_ht))
Arjan van de Ven18dd36a2008-09-01 15:19:11 -07001119 hrtimer_start_expires(p_ht, HRTIMER_MODE_ABS);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001120}
1121
1122static enum hrtimer_restart hlt_timer_fn(struct hrtimer *data)
1123{
1124 struct kvm_vcpu *vcpu;
1125 wait_queue_head_t *q;
1126
1127 vcpu = container_of(data, struct kvm_vcpu, arch.hlt_timer);
Avi Kivitya4535292008-04-13 17:54:35 +03001128 if (vcpu->arch.mp_state != KVM_MP_STATE_HALTED)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001129 goto out;
1130
1131 q = &vcpu->wq;
1132 if (waitqueue_active(q)) {
Avi Kivitya4535292008-04-13 17:54:35 +03001133 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001134 wake_up_interruptible(q);
1135 }
1136out:
1137 vcpu->arch.timer_check = 1;
1138 return HRTIMER_NORESTART;
1139}
1140
1141#define PALE_RESET_ENTRY 0x80000000ffffffb0UL
1142
1143int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1144{
1145 struct kvm_vcpu *v;
1146 int r;
1147 int i;
1148 long itc_offset;
1149 struct kvm *kvm = vcpu->kvm;
1150 struct kvm_pt_regs *regs = vcpu_regs(vcpu);
1151
1152 union context *p_ctx = &vcpu->arch.guest;
1153 struct kvm_vcpu *vmm_vcpu = to_guest(vcpu->kvm, vcpu);
1154
1155 /*Init vcpu context for first run.*/
1156 if (IS_ERR(vmm_vcpu))
1157 return PTR_ERR(vmm_vcpu);
1158
1159 if (vcpu->vcpu_id == 0) {
Avi Kivitya4535292008-04-13 17:54:35 +03001160 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001161
1162 /*Set entry address for first run.*/
1163 regs->cr_iip = PALE_RESET_ENTRY;
1164
1165 /*Initilize itc offset for vcpus*/
1166 itc_offset = 0UL - ia64_getreg(_IA64_REG_AR_ITC);
1167 for (i = 0; i < MAX_VCPU_NUM; i++) {
1168 v = (struct kvm_vcpu *)((char *)vcpu + VCPU_SIZE * i);
1169 v->arch.itc_offset = itc_offset;
1170 v->arch.last_itc = 0;
1171 }
1172 } else
Avi Kivitya4535292008-04-13 17:54:35 +03001173 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001174
1175 r = -ENOMEM;
1176 vcpu->arch.apic = kzalloc(sizeof(struct kvm_lapic), GFP_KERNEL);
1177 if (!vcpu->arch.apic)
1178 goto out;
1179 vcpu->arch.apic->vcpu = vcpu;
1180
1181 p_ctx->gr[1] = 0;
1182 p_ctx->gr[12] = (unsigned long)((char *)vmm_vcpu + IA64_STK_OFFSET);
1183 p_ctx->gr[13] = (unsigned long)vmm_vcpu;
1184 p_ctx->psr = 0x1008522000UL;
1185 p_ctx->ar[40] = FPSR_DEFAULT; /*fpsr*/
1186 p_ctx->caller_unat = 0;
1187 p_ctx->pr = 0x0;
1188 p_ctx->ar[36] = 0x0; /*unat*/
1189 p_ctx->ar[19] = 0x0; /*rnat*/
1190 p_ctx->ar[18] = (unsigned long)vmm_vcpu +
1191 ((sizeof(struct kvm_vcpu)+15) & ~15);
1192 p_ctx->ar[64] = 0x0; /*pfs*/
1193 p_ctx->cr[0] = 0x7e04UL;
1194 p_ctx->cr[2] = (unsigned long)kvm_vmm_info->vmm_ivt;
1195 p_ctx->cr[8] = 0x3c;
1196
1197 /*Initilize region register*/
1198 p_ctx->rr[0] = 0x30;
1199 p_ctx->rr[1] = 0x30;
1200 p_ctx->rr[2] = 0x30;
1201 p_ctx->rr[3] = 0x30;
1202 p_ctx->rr[4] = 0x30;
1203 p_ctx->rr[5] = 0x30;
1204 p_ctx->rr[7] = 0x30;
1205
1206 /*Initilize branch register 0*/
1207 p_ctx->br[0] = *(unsigned long *)kvm_vmm_info->vmm_entry;
1208
1209 vcpu->arch.vmm_rr = kvm->arch.vmm_init_rr;
1210 vcpu->arch.metaphysical_rr0 = kvm->arch.metaphysical_rr0;
1211 vcpu->arch.metaphysical_rr4 = kvm->arch.metaphysical_rr4;
1212
1213 hrtimer_init(&vcpu->arch.hlt_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1214 vcpu->arch.hlt_timer.function = hlt_timer_fn;
1215
1216 vcpu->arch.last_run_cpu = -1;
1217 vcpu->arch.vpd = (struct vpd *)VPD_ADDR(vcpu->vcpu_id);
1218 vcpu->arch.vsa_base = kvm_vsa_base;
1219 vcpu->arch.__gp = kvm_vmm_gp;
1220 vcpu->arch.dirty_log_lock_pa = __pa(&kvm->arch.dirty_log_lock);
1221 vcpu->arch.vhpt.hash = (struct thash_data *)VHPT_ADDR(vcpu->vcpu_id);
1222 vcpu->arch.vtlb.hash = (struct thash_data *)VTLB_ADDR(vcpu->vcpu_id);
1223 init_ptce_info(vcpu);
1224
1225 r = 0;
1226out:
1227 return r;
1228}
1229
1230static int vti_vcpu_setup(struct kvm_vcpu *vcpu, int id)
1231{
1232 unsigned long psr;
1233 int r;
1234
1235 local_irq_save(psr);
1236 r = kvm_insert_vmm_mapping(vcpu);
1237 if (r)
1238 goto fail;
1239 r = kvm_vcpu_init(vcpu, vcpu->kvm, id);
1240 if (r)
1241 goto fail;
1242
1243 r = vti_init_vpd(vcpu);
1244 if (r) {
1245 printk(KERN_DEBUG"kvm: vpd init error!!\n");
1246 goto uninit;
1247 }
1248
1249 r = vti_create_vp(vcpu);
1250 if (r)
1251 goto uninit;
1252
1253 kvm_purge_vmm_mapping(vcpu);
1254 local_irq_restore(psr);
1255
1256 return 0;
1257uninit:
1258 kvm_vcpu_uninit(vcpu);
1259fail:
Julia Lawallcab7a1e2008-07-22 21:38:18 +02001260 local_irq_restore(psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001261 return r;
1262}
1263
1264struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1265 unsigned int id)
1266{
1267 struct kvm_vcpu *vcpu;
1268 unsigned long vm_base = kvm->arch.vm_base;
1269 int r;
1270 int cpu;
1271
1272 r = -ENOMEM;
1273 if (!vm_base) {
1274 printk(KERN_ERR"kvm: Create vcpu[%d] error!\n", id);
1275 goto fail;
1276 }
1277 vcpu = (struct kvm_vcpu *)(vm_base + KVM_VCPU_OFS + VCPU_SIZE * id);
1278 vcpu->kvm = kvm;
1279
1280 cpu = get_cpu();
1281 vti_vcpu_load(vcpu, cpu);
1282 r = vti_vcpu_setup(vcpu, id);
1283 put_cpu();
1284
1285 if (r) {
1286 printk(KERN_DEBUG"kvm: vcpu_setup error!!\n");
1287 goto fail;
1288 }
1289
1290 return vcpu;
1291fail:
1292 return ERR_PTR(r);
1293}
1294
1295int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1296{
1297 return 0;
1298}
1299
1300int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1301{
1302 return -EINVAL;
1303}
1304
1305int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1306{
1307 return -EINVAL;
1308}
1309
1310int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
1311 struct kvm_debug_guest *dbg)
1312{
1313 return -EINVAL;
1314}
1315
1316static void free_kvm(struct kvm *kvm)
1317{
1318 unsigned long vm_base = kvm->arch.vm_base;
1319
1320 if (vm_base) {
1321 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
1322 free_pages(vm_base, get_order(KVM_VM_DATA_SIZE));
1323 }
1324
1325}
1326
1327static void kvm_release_vm_pages(struct kvm *kvm)
1328{
1329 struct kvm_memory_slot *memslot;
1330 int i, j;
1331 unsigned long base_gfn;
1332
1333 for (i = 0; i < kvm->nmemslots; i++) {
1334 memslot = &kvm->memslots[i];
1335 base_gfn = memslot->base_gfn;
1336
1337 for (j = 0; j < memslot->npages; j++) {
1338 if (memslot->rmap[j])
1339 put_page((struct page *)memslot->rmap[j]);
1340 }
1341 }
1342}
1343
1344void kvm_arch_destroy_vm(struct kvm *kvm)
1345{
Xiantao Zhang2381ad22008-10-08 08:29:33 +08001346 kvm_iommu_unmap_guest(kvm);
1347#ifdef KVM_CAP_DEVICE_ASSIGNMENT
1348 kvm_free_all_assigned_devices(kvm);
1349#endif
Xiantao Zhangb024b792008-04-01 15:29:29 +08001350 kfree(kvm->arch.vioapic);
1351 kvm_release_vm_pages(kvm);
1352 kvm_free_physmem(kvm);
1353 free_kvm(kvm);
1354}
1355
1356void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1357{
1358}
1359
1360void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1361{
1362 if (cpu != vcpu->cpu) {
1363 vcpu->cpu = cpu;
1364 if (vcpu->arch.ht_active)
1365 kvm_migrate_hlt_timer(vcpu);
1366 }
1367}
1368
1369#define SAVE_REGS(_x) regs->_x = vcpu->arch._x
1370
1371int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1372{
1373 int i;
1374 int r;
1375 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1376 vcpu_load(vcpu);
1377
1378 for (i = 0; i < 16; i++) {
1379 regs->vpd.vgr[i] = vpd->vgr[i];
1380 regs->vpd.vbgr[i] = vpd->vbgr[i];
1381 }
1382 for (i = 0; i < 128; i++)
1383 regs->vpd.vcr[i] = vpd->vcr[i];
1384 regs->vpd.vhpi = vpd->vhpi;
1385 regs->vpd.vnat = vpd->vnat;
1386 regs->vpd.vbnat = vpd->vbnat;
1387 regs->vpd.vpsr = vpd->vpsr;
1388 regs->vpd.vpr = vpd->vpr;
1389
1390 r = -EFAULT;
1391 r = copy_to_user(regs->saved_guest, &vcpu->arch.guest,
1392 sizeof(union context));
1393 if (r)
1394 goto out;
1395 r = copy_to_user(regs->saved_stack, (void *)vcpu, IA64_STK_OFFSET);
1396 if (r)
1397 goto out;
1398 SAVE_REGS(mp_state);
1399 SAVE_REGS(vmm_rr);
1400 memcpy(regs->itrs, vcpu->arch.itrs, sizeof(struct thash_data) * NITRS);
1401 memcpy(regs->dtrs, vcpu->arch.dtrs, sizeof(struct thash_data) * NDTRS);
1402 SAVE_REGS(itr_regions);
1403 SAVE_REGS(dtr_regions);
1404 SAVE_REGS(tc_regions);
1405 SAVE_REGS(irq_check);
1406 SAVE_REGS(itc_check);
1407 SAVE_REGS(timer_check);
1408 SAVE_REGS(timer_pending);
1409 SAVE_REGS(last_itc);
1410 for (i = 0; i < 8; i++) {
1411 regs->vrr[i] = vcpu->arch.vrr[i];
1412 regs->ibr[i] = vcpu->arch.ibr[i];
1413 regs->dbr[i] = vcpu->arch.dbr[i];
1414 }
1415 for (i = 0; i < 4; i++)
1416 regs->insvc[i] = vcpu->arch.insvc[i];
1417 regs->saved_itc = vcpu->arch.itc_offset + ia64_getreg(_IA64_REG_AR_ITC);
1418 SAVE_REGS(xtp);
1419 SAVE_REGS(metaphysical_rr0);
1420 SAVE_REGS(metaphysical_rr4);
1421 SAVE_REGS(metaphysical_saved_rr0);
1422 SAVE_REGS(metaphysical_saved_rr4);
1423 SAVE_REGS(fp_psr);
1424 SAVE_REGS(saved_gp);
1425 vcpu_put(vcpu);
1426 r = 0;
1427out:
1428 return r;
1429}
1430
1431void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1432{
1433
1434 hrtimer_cancel(&vcpu->arch.hlt_timer);
1435 kfree(vcpu->arch.apic);
1436}
1437
1438
1439long kvm_arch_vcpu_ioctl(struct file *filp,
1440 unsigned int ioctl, unsigned long arg)
1441{
1442 return -EINVAL;
1443}
1444
1445int kvm_arch_set_memory_region(struct kvm *kvm,
1446 struct kvm_userspace_memory_region *mem,
1447 struct kvm_memory_slot old,
1448 int user_alloc)
1449{
1450 unsigned long i;
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001451 unsigned long pfn;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001452 int npages = mem->memory_size >> PAGE_SHIFT;
1453 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
1454 unsigned long base_gfn = memslot->base_gfn;
1455
1456 for (i = 0; i < npages; i++) {
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001457 pfn = gfn_to_pfn(kvm, base_gfn + i);
1458 if (!kvm_is_mmio_pfn(pfn)) {
1459 kvm_set_pmt_entry(kvm, base_gfn + i,
1460 pfn << PAGE_SHIFT,
Xiantao Zhangb010eb52008-09-28 01:39:46 -07001461 _PAGE_AR_RWX | _PAGE_MA_WB);
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001462 memslot->rmap[i] = (unsigned long)pfn_to_page(pfn);
1463 } else {
1464 kvm_set_pmt_entry(kvm, base_gfn + i,
Xiantao Zhangb010eb52008-09-28 01:39:46 -07001465 GPFN_PHYS_MMIO | (pfn << PAGE_SHIFT),
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001466 _PAGE_MA_UC);
1467 memslot->rmap[i] = 0;
1468 }
Xiantao Zhangb024b792008-04-01 15:29:29 +08001469 }
1470
1471 return 0;
1472}
1473
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001474void kvm_arch_flush_shadow(struct kvm *kvm)
1475{
1476}
Xiantao Zhangb024b792008-04-01 15:29:29 +08001477
1478long kvm_arch_dev_ioctl(struct file *filp,
1479 unsigned int ioctl, unsigned long arg)
1480{
1481 return -EINVAL;
1482}
1483
1484void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1485{
1486 kvm_vcpu_uninit(vcpu);
1487}
1488
1489static int vti_cpu_has_kvm_support(void)
1490{
1491 long avail = 1, status = 1, control = 1;
1492 long ret;
1493
1494 ret = ia64_pal_proc_get_features(&avail, &status, &control, 0);
1495 if (ret)
1496 goto out;
1497
1498 if (!(avail & PAL_PROC_VM_BIT))
1499 goto out;
1500
1501 printk(KERN_DEBUG"kvm: Hardware Supports VT\n");
1502
1503 ret = ia64_pal_vp_env_info(&kvm_vm_buffer_size, &vp_env_info);
1504 if (ret)
1505 goto out;
1506 printk(KERN_DEBUG"kvm: VM Buffer Size:0x%lx\n", kvm_vm_buffer_size);
1507
1508 if (!(vp_env_info & VP_OPCODE)) {
1509 printk(KERN_WARNING"kvm: No opcode ability on hardware, "
1510 "vm_env_info:0x%lx\n", vp_env_info);
1511 }
1512
1513 return 1;
1514out:
1515 return 0;
1516}
1517
1518static int kvm_relocate_vmm(struct kvm_vmm_info *vmm_info,
1519 struct module *module)
1520{
1521 unsigned long module_base;
1522 unsigned long vmm_size;
1523
1524 unsigned long vmm_offset, func_offset, fdesc_offset;
1525 struct fdesc *p_fdesc;
1526
1527 BUG_ON(!module);
1528
1529 if (!kvm_vmm_base) {
1530 printk("kvm: kvm area hasn't been initilized yet!!\n");
1531 return -EFAULT;
1532 }
1533
1534 /*Calculate new position of relocated vmm module.*/
1535 module_base = (unsigned long)module->module_core;
1536 vmm_size = module->core_size;
1537 if (unlikely(vmm_size > KVM_VMM_SIZE))
1538 return -EFAULT;
1539
1540 memcpy((void *)kvm_vmm_base, (void *)module_base, vmm_size);
1541 kvm_flush_icache(kvm_vmm_base, vmm_size);
1542
1543 /*Recalculate kvm_vmm_info based on new VMM*/
1544 vmm_offset = vmm_info->vmm_ivt - module_base;
1545 kvm_vmm_info->vmm_ivt = KVM_VMM_BASE + vmm_offset;
1546 printk(KERN_DEBUG"kvm: Relocated VMM's IVT Base Addr:%lx\n",
1547 kvm_vmm_info->vmm_ivt);
1548
1549 fdesc_offset = (unsigned long)vmm_info->vmm_entry - module_base;
1550 kvm_vmm_info->vmm_entry = (kvm_vmm_entry *)(KVM_VMM_BASE +
1551 fdesc_offset);
1552 func_offset = *(unsigned long *)vmm_info->vmm_entry - module_base;
1553 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1554 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1555 p_fdesc->gp = KVM_VMM_BASE+(p_fdesc->gp - module_base);
1556
1557 printk(KERN_DEBUG"kvm: Relocated VMM's Init Entry Addr:%lx\n",
1558 KVM_VMM_BASE+func_offset);
1559
1560 fdesc_offset = (unsigned long)vmm_info->tramp_entry - module_base;
1561 kvm_vmm_info->tramp_entry = (kvm_tramp_entry *)(KVM_VMM_BASE +
1562 fdesc_offset);
1563 func_offset = *(unsigned long *)vmm_info->tramp_entry - module_base;
1564 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1565 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1566 p_fdesc->gp = KVM_VMM_BASE + (p_fdesc->gp - module_base);
1567
1568 kvm_vmm_gp = p_fdesc->gp;
1569
1570 printk(KERN_DEBUG"kvm: Relocated VMM's Entry IP:%p\n",
1571 kvm_vmm_info->vmm_entry);
1572 printk(KERN_DEBUG"kvm: Relocated VMM's Trampoline Entry IP:0x%lx\n",
1573 KVM_VMM_BASE + func_offset);
1574
1575 return 0;
1576}
1577
1578int kvm_arch_init(void *opaque)
1579{
1580 int r;
1581 struct kvm_vmm_info *vmm_info = (struct kvm_vmm_info *)opaque;
1582
1583 if (!vti_cpu_has_kvm_support()) {
1584 printk(KERN_ERR "kvm: No Hardware Virtualization Support!\n");
1585 r = -EOPNOTSUPP;
1586 goto out;
1587 }
1588
1589 if (kvm_vmm_info) {
1590 printk(KERN_ERR "kvm: Already loaded VMM module!\n");
1591 r = -EEXIST;
1592 goto out;
1593 }
1594
1595 r = -ENOMEM;
1596 kvm_vmm_info = kzalloc(sizeof(struct kvm_vmm_info), GFP_KERNEL);
1597 if (!kvm_vmm_info)
1598 goto out;
1599
1600 if (kvm_alloc_vmm_area())
1601 goto out_free0;
1602
1603 r = kvm_relocate_vmm(vmm_info, vmm_info->module);
1604 if (r)
1605 goto out_free1;
1606
1607 return 0;
1608
1609out_free1:
1610 kvm_free_vmm_area();
1611out_free0:
1612 kfree(kvm_vmm_info);
1613out:
1614 return r;
1615}
1616
1617void kvm_arch_exit(void)
1618{
1619 kvm_free_vmm_area();
1620 kfree(kvm_vmm_info);
1621 kvm_vmm_info = NULL;
1622}
1623
1624static int kvm_ia64_sync_dirty_log(struct kvm *kvm,
1625 struct kvm_dirty_log *log)
1626{
1627 struct kvm_memory_slot *memslot;
1628 int r, i;
1629 long n, base;
1630 unsigned long *dirty_bitmap = (unsigned long *)((void *)kvm - KVM_VM_OFS
1631 + KVM_MEM_DIRTY_LOG_OFS);
1632
1633 r = -EINVAL;
1634 if (log->slot >= KVM_MEMORY_SLOTS)
1635 goto out;
1636
1637 memslot = &kvm->memslots[log->slot];
1638 r = -ENOENT;
1639 if (!memslot->dirty_bitmap)
1640 goto out;
1641
1642 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1643 base = memslot->base_gfn / BITS_PER_LONG;
1644
1645 for (i = 0; i < n/sizeof(long); ++i) {
1646 memslot->dirty_bitmap[i] = dirty_bitmap[base + i];
1647 dirty_bitmap[base + i] = 0;
1648 }
1649 r = 0;
1650out:
1651 return r;
1652}
1653
1654int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1655 struct kvm_dirty_log *log)
1656{
1657 int r;
1658 int n;
1659 struct kvm_memory_slot *memslot;
1660 int is_dirty = 0;
1661
1662 spin_lock(&kvm->arch.dirty_log_lock);
1663
1664 r = kvm_ia64_sync_dirty_log(kvm, log);
1665 if (r)
1666 goto out;
1667
1668 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1669 if (r)
1670 goto out;
1671
1672 /* If nothing is dirty, don't bother messing with page tables. */
1673 if (is_dirty) {
1674 kvm_flush_remote_tlbs(kvm);
1675 memslot = &kvm->memslots[log->slot];
1676 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1677 memset(memslot->dirty_bitmap, 0, n);
1678 }
1679 r = 0;
1680out:
1681 spin_unlock(&kvm->arch.dirty_log_lock);
1682 return r;
1683}
1684
1685int kvm_arch_hardware_setup(void)
1686{
1687 return 0;
1688}
1689
1690void kvm_arch_hardware_unsetup(void)
1691{
1692}
1693
1694static void vcpu_kick_intr(void *info)
1695{
1696#ifdef DEBUG
1697 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
1698 printk(KERN_DEBUG"vcpu_kick_intr %p \n", vcpu);
1699#endif
1700}
1701
1702void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1703{
1704 int ipi_pcpu = vcpu->cpu;
1705
1706 if (waitqueue_active(&vcpu->wq))
1707 wake_up_interruptible(&vcpu->wq);
1708
1709 if (vcpu->guest_mode)
Takashi Iwai2f73cca2008-07-17 18:09:12 +02001710 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001711}
1712
1713int kvm_apic_set_irq(struct kvm_vcpu *vcpu, u8 vec, u8 trig)
1714{
1715
1716 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1717
1718 if (!test_and_set_bit(vec, &vpd->irr[0])) {
1719 vcpu->arch.irq_new_pending = 1;
Avi Kivitya4535292008-04-13 17:54:35 +03001720 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001721 kvm_vcpu_kick(vcpu);
Avi Kivitya4535292008-04-13 17:54:35 +03001722 else if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) {
1723 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001724 if (waitqueue_active(&vcpu->wq))
1725 wake_up_interruptible(&vcpu->wq);
1726 }
1727 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{
1796 return 0;
1797}
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}