blob: 8213efe1998c16eade6966acd1cf1b5b22d489a8 [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>
Xiantao Zhangb024b792008-04-01 15:29:29 +080026#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Xiantao Zhangb024b792008-04-01 15:29:29 +080028#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>
Joerg Roedel19de40a2008-12-03 14:43:34 +010034#include <linux/iommu.h>
Xiantao Zhang2381ad22008-10-08 08:29:33 +080035#include <linux/intel-iommu.h>
Xiantao Zhangb024b792008-04-01 15:29:29 +080036
37#include <asm/pgtable.h>
38#include <asm/gcc_intrin.h>
39#include <asm/pal.h>
40#include <asm/cacheflush.h>
41#include <asm/div64.h>
42#include <asm/tlb.h>
Jes Sorensen9f726322008-09-12 14:12:08 +020043#include <asm/elf.h>
Jes Sorensen0c72ea72009-02-25 10:38:52 -060044#include <asm/sn/addrs.h>
45#include <asm/sn/clksupport.h>
46#include <asm/sn/shub_mmr.h>
Xiantao Zhangb024b792008-04-01 15:29:29 +080047
48#include "misc.h"
49#include "vti.h"
50#include "iodev.h"
51#include "ioapic.h"
52#include "lapic.h"
Xiantao Zhang2f749772008-09-27 11:46:36 +080053#include "irq.h"
Xiantao Zhangb024b792008-04-01 15:29:29 +080054
55static unsigned long kvm_vmm_base;
56static unsigned long kvm_vsa_base;
57static unsigned long kvm_vm_buffer;
58static unsigned long kvm_vm_buffer_size;
59unsigned long kvm_vmm_gp;
60
61static long vp_env_info;
62
63static struct kvm_vmm_info *kvm_vmm_info;
64
65static DEFINE_PER_CPU(struct kvm_vcpu *, last_vcpu);
66
67struct kvm_stats_debugfs_item debugfs_entries[] = {
68 { NULL }
69};
70
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -060071static unsigned long kvm_get_itc(struct kvm_vcpu *vcpu)
72{
73#if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
74 if (vcpu->kvm->arch.is_sn2)
75 return rtc_time();
76 else
77#endif
78 return ia64_getreg(_IA64_REG_AR_ITC);
79}
80
Xiantao Zhangb024b792008-04-01 15:29:29 +080081static void kvm_flush_icache(unsigned long start, unsigned long len)
82{
83 int l;
84
85 for (l = 0; l < (len + 32); l += 32)
Isaku Yamahata71205692009-03-27 15:11:57 +090086 ia64_fc((void *)(start + l));
Xiantao Zhangb024b792008-04-01 15:29:29 +080087
88 ia64_sync_i();
89 ia64_srlz_i();
90}
91
92static void kvm_flush_tlb_all(void)
93{
94 unsigned long i, j, count0, count1, stride0, stride1, addr;
95 long flags;
96
97 addr = local_cpu_data->ptce_base;
98 count0 = local_cpu_data->ptce_count[0];
99 count1 = local_cpu_data->ptce_count[1];
100 stride0 = local_cpu_data->ptce_stride[0];
101 stride1 = local_cpu_data->ptce_stride[1];
102
103 local_irq_save(flags);
104 for (i = 0; i < count0; ++i) {
105 for (j = 0; j < count1; ++j) {
106 ia64_ptce(addr);
107 addr += stride1;
108 }
109 addr += stride0;
110 }
111 local_irq_restore(flags);
112 ia64_srlz_i(); /* srlz.i implies srlz.d */
113}
114
115long ia64_pal_vp_create(u64 *vpd, u64 *host_iva, u64 *opt_handler)
116{
117 struct ia64_pal_retval iprv;
118
119 PAL_CALL_STK(iprv, PAL_VP_CREATE, (u64)vpd, (u64)host_iva,
120 (u64)opt_handler);
121
122 return iprv.status;
123}
124
125static DEFINE_SPINLOCK(vp_lock);
126
Alexander Graf10474ae2009-09-15 11:37:46 +0200127int kvm_arch_hardware_enable(void *garbage)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800128{
129 long status;
130 long tmp_base;
131 unsigned long pte;
132 unsigned long saved_psr;
133 int slot;
134
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600135 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
Xiantao Zhangb024b792008-04-01 15:29:29 +0800136 local_irq_save(saved_psr);
137 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
Julia Lawallcab7a1e2008-07-22 21:38:18 +0200138 local_irq_restore(saved_psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800139 if (slot < 0)
Alexander Graf10474ae2009-09-15 11:37:46 +0200140 return -EINVAL;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800141
142 spin_lock(&vp_lock);
143 status = ia64_pal_vp_init_env(kvm_vsa_base ?
144 VP_INIT_ENV : VP_INIT_ENV_INITALIZE,
145 __pa(kvm_vm_buffer), KVM_VM_BUFFER_BASE, &tmp_base);
146 if (status != 0) {
Julia Lawall3499f4d2010-05-26 17:57:05 +0200147 spin_unlock(&vp_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800148 printk(KERN_WARNING"kvm: Failed to Enable VT Support!!!!\n");
Alexander Graf10474ae2009-09-15 11:37:46 +0200149 return -EINVAL;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800150 }
151
152 if (!kvm_vsa_base) {
153 kvm_vsa_base = tmp_base;
154 printk(KERN_INFO"kvm: kvm_vsa_base:0x%lx\n", kvm_vsa_base);
155 }
156 spin_unlock(&vp_lock);
157 ia64_ptr_entry(0x3, slot);
Alexander Graf10474ae2009-09-15 11:37:46 +0200158
159 return 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800160}
161
162void kvm_arch_hardware_disable(void *garbage)
163{
164
165 long status;
166 int slot;
167 unsigned long pte;
168 unsigned long saved_psr;
169 unsigned long host_iva = ia64_getreg(_IA64_REG_CR_IVA);
170
171 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base),
172 PAGE_KERNEL));
173
174 local_irq_save(saved_psr);
175 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
Julia Lawallcab7a1e2008-07-22 21:38:18 +0200176 local_irq_restore(saved_psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800177 if (slot < 0)
178 return;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800179
180 status = ia64_pal_vp_exit_env(host_iva);
181 if (status)
182 printk(KERN_DEBUG"kvm: Failed to disable VT support! :%ld\n",
183 status);
184 ia64_ptr_entry(0x3, slot);
185}
186
187void kvm_arch_check_processor_compat(void *rtn)
188{
189 *(int *)rtn = 0;
190}
191
192int kvm_dev_ioctl_check_extension(long ext)
193{
194
195 int r;
196
197 switch (ext) {
198 case KVM_CAP_IRQCHIP:
Xiantao Zhang8c4b5372008-08-28 09:34:08 +0800199 case KVM_CAP_MP_STATE:
Gleb Natapov49256632009-02-04 17:28:14 +0200200 case KVM_CAP_IRQ_INJECT_STATUS:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800201 r = 1;
202 break;
Laurent Vivier7f39f8a2008-05-30 16:05:57 +0200203 case KVM_CAP_COALESCED_MMIO:
204 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
205 break;
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800206 case KVM_CAP_IOMMU:
Joerg Roedel19de40a2008-12-03 14:43:34 +0100207 r = iommu_found();
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800208 break;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800209 default:
210 r = 0;
211 }
212 return r;
213
214}
215
Xiantao Zhangb024b792008-04-01 15:29:29 +0800216static int handle_vm_error(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
217{
218 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
219 kvm_run->hw.hardware_exit_reason = 1;
220 return 0;
221}
222
223static int handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
224{
225 struct kvm_mmio_req *p;
226 struct kvm_io_device *mmio_dev;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300227 int r;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800228
229 p = kvm_get_vcpu_ioreq(vcpu);
230
231 if ((p->addr & PAGE_MASK) == IOAPIC_DEFAULT_BASE_ADDRESS)
232 goto mmio;
233 vcpu->mmio_needed = 1;
234 vcpu->mmio_phys_addr = kvm_run->mmio.phys_addr = p->addr;
235 vcpu->mmio_size = kvm_run->mmio.len = p->size;
236 vcpu->mmio_is_write = kvm_run->mmio.is_write = !p->dir;
237
238 if (vcpu->mmio_is_write)
239 memcpy(vcpu->mmio_data, &p->data, p->size);
240 memcpy(kvm_run->mmio.data, &p->data, p->size);
241 kvm_run->exit_reason = KVM_EXIT_MMIO;
242 return 0;
243mmio:
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300244 if (p->dir)
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200245 r = kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, p->addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300246 p->size, &p->data);
247 else
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200248 r = kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, p->addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300249 p->size, &p->data);
250 if (r)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800251 printk(KERN_ERR"kvm: No iodevice found! addr:%lx\n", p->addr);
252 p->state = STATE_IORESP_READY;
253
254 return 1;
255}
256
257static int handle_pal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
258{
259 struct exit_ctl_data *p;
260
261 p = kvm_get_exit_data(vcpu);
262
263 if (p->exit_reason == EXIT_REASON_PAL_CALL)
264 return kvm_pal_emul(vcpu, kvm_run);
265 else {
266 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
267 kvm_run->hw.hardware_exit_reason = 2;
268 return 0;
269 }
270}
271
272static int handle_sal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
273{
274 struct exit_ctl_data *p;
275
276 p = kvm_get_exit_data(vcpu);
277
278 if (p->exit_reason == EXIT_REASON_SAL_CALL) {
279 kvm_sal_emul(vcpu);
280 return 1;
281 } else {
282 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
283 kvm_run->hw.hardware_exit_reason = 3;
284 return 0;
285 }
286
287}
288
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200289static int __apic_accept_irq(struct kvm_vcpu *vcpu, uint64_t vector)
290{
291 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
292
293 if (!test_and_set_bit(vector, &vpd->irr[0])) {
294 vcpu->arch.irq_new_pending = 1;
295 kvm_vcpu_kick(vcpu);
296 return 1;
297 }
298 return 0;
299}
300
Xiantao Zhangb024b792008-04-01 15:29:29 +0800301/*
302 * offset: address offset to IPI space.
303 * value: deliver value.
304 */
305static void vcpu_deliver_ipi(struct kvm_vcpu *vcpu, uint64_t dm,
306 uint64_t vector)
307{
308 switch (dm) {
309 case SAPIC_FIXED:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800310 break;
311 case SAPIC_NMI:
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200312 vector = 2;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800313 break;
314 case SAPIC_EXTINT:
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200315 vector = 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800316 break;
317 case SAPIC_INIT:
318 case SAPIC_PMI:
319 default:
320 printk(KERN_ERR"kvm: Unimplemented Deliver reserved IPI!\n");
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200321 return;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800322 }
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200323 __apic_accept_irq(vcpu, vector);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800324}
325
326static struct kvm_vcpu *lid_to_vcpu(struct kvm *kvm, unsigned long id,
327 unsigned long eid)
328{
329 union ia64_lid lid;
330 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300331 struct kvm_vcpu *vcpu;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800332
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300333 kvm_for_each_vcpu(i, vcpu, kvm) {
334 lid.val = VCPU_LID(vcpu);
335 if (lid.id == id && lid.eid == eid)
336 return vcpu;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800337 }
338
339 return NULL;
340}
341
342static int handle_ipi(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
343{
344 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
345 struct kvm_vcpu *target_vcpu;
346 struct kvm_pt_regs *regs;
347 union ia64_ipi_a addr = p->u.ipi_data.addr;
348 union ia64_ipi_d data = p->u.ipi_data.data;
349
350 target_vcpu = lid_to_vcpu(vcpu->kvm, addr.id, addr.eid);
351 if (!target_vcpu)
352 return handle_vm_error(vcpu, kvm_run);
353
354 if (!target_vcpu->arch.launched) {
355 regs = vcpu_regs(target_vcpu);
356
357 regs->cr_iip = vcpu->kvm->arch.rdv_sal_data.boot_ip;
358 regs->r1 = vcpu->kvm->arch.rdv_sal_data.boot_gp;
359
Avi Kivitya4535292008-04-13 17:54:35 +0300360 target_vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800361 if (waitqueue_active(&target_vcpu->wq))
362 wake_up_interruptible(&target_vcpu->wq);
363 } else {
364 vcpu_deliver_ipi(target_vcpu, data.dm, data.vector);
365 if (target_vcpu != vcpu)
366 kvm_vcpu_kick(target_vcpu);
367 }
368
369 return 1;
370}
371
372struct call_data {
373 struct kvm_ptc_g ptc_g_data;
374 struct kvm_vcpu *vcpu;
375};
376
377static void vcpu_global_purge(void *info)
378{
379 struct call_data *p = (struct call_data *)info;
380 struct kvm_vcpu *vcpu = p->vcpu;
381
382 if (test_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
383 return;
384
385 set_bit(KVM_REQ_PTC_G, &vcpu->requests);
386 if (vcpu->arch.ptc_g_count < MAX_PTC_G_NUM) {
387 vcpu->arch.ptc_g_data[vcpu->arch.ptc_g_count++] =
388 p->ptc_g_data;
389 } else {
390 clear_bit(KVM_REQ_PTC_G, &vcpu->requests);
391 vcpu->arch.ptc_g_count = 0;
392 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
393 }
394}
395
396static int handle_global_purge(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
397{
398 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
399 struct kvm *kvm = vcpu->kvm;
400 struct call_data call_data;
401 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300402 struct kvm_vcpu *vcpui;
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800403
Xiantao Zhangb024b792008-04-01 15:29:29 +0800404 call_data.ptc_g_data = p->u.ptc_g_data;
405
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300406 kvm_for_each_vcpu(i, vcpui, kvm) {
407 if (vcpui->arch.mp_state == KVM_MP_STATE_UNINITIALIZED ||
408 vcpu == vcpui)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800409 continue;
410
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300411 if (waitqueue_active(&vcpui->wq))
412 wake_up_interruptible(&vcpui->wq);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800413
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300414 if (vcpui->cpu != -1) {
415 call_data.vcpu = vcpui;
416 smp_call_function_single(vcpui->cpu,
Takashi Iwai2f73cca2008-07-17 18:09:12 +0200417 vcpu_global_purge, &call_data, 1);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800418 } else
419 printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n");
420
421 }
422 return 1;
423}
424
425static int handle_switch_rr6(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
426{
427 return 1;
428}
429
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600430static int kvm_sn2_setup_mappings(struct kvm_vcpu *vcpu)
431{
432 unsigned long pte, rtc_phys_addr, map_addr;
433 int slot;
434
435 map_addr = KVM_VMM_BASE + (1UL << KVM_VMM_SHIFT);
436 rtc_phys_addr = LOCAL_MMR_OFFSET | SH_RTC;
437 pte = pte_val(mk_pte_phys(rtc_phys_addr, PAGE_KERNEL_UC));
438 slot = ia64_itr_entry(0x3, map_addr, pte, PAGE_SHIFT);
439 vcpu->arch.sn_rtc_tr_slot = slot;
440 if (slot < 0) {
441 printk(KERN_ERR "Mayday mayday! RTC mapping failed!\n");
442 slot = 0;
443 }
444 return slot;
445}
446
Xiantao Zhangb024b792008-04-01 15:29:29 +0800447int kvm_emulate_halt(struct kvm_vcpu *vcpu)
448{
449
450 ktime_t kt;
451 long itc_diff;
452 unsigned long vcpu_now_itc;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800453 unsigned long expires;
454 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
455 unsigned long cyc_per_usec = local_cpu_data->cyc_per_usec;
456 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
457
Xiantao Zhangb024b792008-04-01 15:29:29 +0800458 if (irqchip_in_kernel(vcpu->kvm)) {
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800459
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -0600460 vcpu_now_itc = kvm_get_itc(vcpu) + vcpu->arch.itc_offset;
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800461
462 if (time_after(vcpu_now_itc, vpd->itm)) {
463 vcpu->arch.timer_check = 1;
464 return 1;
465 }
466 itc_diff = vpd->itm - vcpu_now_itc;
467 if (itc_diff < 0)
468 itc_diff = -itc_diff;
469
470 expires = div64_u64(itc_diff, cyc_per_usec);
471 kt = ktime_set(0, 1000 * expires);
472
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800473 vcpu->arch.ht_active = 1;
474 hrtimer_start(p_ht, kt, HRTIMER_MODE_ABS);
475
Avi Kivitya4535292008-04-13 17:54:35 +0300476 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800477 kvm_vcpu_block(vcpu);
478 hrtimer_cancel(p_ht);
479 vcpu->arch.ht_active = 0;
480
Gleb Natapov09cec752009-03-23 15:11:44 +0200481 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests) ||
482 kvm_cpu_has_pending_timer(vcpu))
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800483 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
Gleb Natapov09cec752009-03-23 15:11:44 +0200484 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800485
Avi Kivitya4535292008-04-13 17:54:35 +0300486 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800487 return -EINTR;
488 return 1;
489 } else {
490 printk(KERN_ERR"kvm: Unsupported userspace halt!");
491 return 0;
492 }
493}
494
495static int handle_vm_shutdown(struct kvm_vcpu *vcpu,
496 struct kvm_run *kvm_run)
497{
498 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
499 return 0;
500}
501
502static int handle_external_interrupt(struct kvm_vcpu *vcpu,
503 struct kvm_run *kvm_run)
504{
505 return 1;
506}
507
Xiantao Zhang7d637972008-11-21 20:58:11 +0800508static int handle_vcpu_debug(struct kvm_vcpu *vcpu,
509 struct kvm_run *kvm_run)
510{
511 printk("VMM: %s", vcpu->arch.log_buf);
512 return 1;
513}
514
Xiantao Zhangb024b792008-04-01 15:29:29 +0800515static int (*kvm_vti_exit_handlers[])(struct kvm_vcpu *vcpu,
516 struct kvm_run *kvm_run) = {
517 [EXIT_REASON_VM_PANIC] = handle_vm_error,
518 [EXIT_REASON_MMIO_INSTRUCTION] = handle_mmio,
519 [EXIT_REASON_PAL_CALL] = handle_pal_call,
520 [EXIT_REASON_SAL_CALL] = handle_sal_call,
521 [EXIT_REASON_SWITCH_RR6] = handle_switch_rr6,
522 [EXIT_REASON_VM_DESTROY] = handle_vm_shutdown,
523 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
524 [EXIT_REASON_IPI] = handle_ipi,
525 [EXIT_REASON_PTC_G] = handle_global_purge,
Xiantao Zhang7d637972008-11-21 20:58:11 +0800526 [EXIT_REASON_DEBUG] = handle_vcpu_debug,
Xiantao Zhangb024b792008-04-01 15:29:29 +0800527
528};
529
530static const int kvm_vti_max_exit_handlers =
531 sizeof(kvm_vti_exit_handlers)/sizeof(*kvm_vti_exit_handlers);
532
Xiantao Zhangb024b792008-04-01 15:29:29 +0800533static uint32_t kvm_get_exit_reason(struct kvm_vcpu *vcpu)
534{
535 struct exit_ctl_data *p_exit_data;
536
537 p_exit_data = kvm_get_exit_data(vcpu);
538 return p_exit_data->exit_reason;
539}
540
541/*
542 * The guest has exited. See if we can fix it or if we need userspace
543 * assistance.
544 */
545static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
546{
547 u32 exit_reason = kvm_get_exit_reason(vcpu);
548 vcpu->arch.last_exit = exit_reason;
549
550 if (exit_reason < kvm_vti_max_exit_handlers
551 && kvm_vti_exit_handlers[exit_reason])
552 return kvm_vti_exit_handlers[exit_reason](vcpu, kvm_run);
553 else {
554 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
555 kvm_run->hw.hardware_exit_reason = exit_reason;
556 }
557 return 0;
558}
559
560static inline void vti_set_rr6(unsigned long rr6)
561{
562 ia64_set_rr(RR6, rr6);
563 ia64_srlz_i();
564}
565
566static int kvm_insert_vmm_mapping(struct kvm_vcpu *vcpu)
567{
568 unsigned long pte;
569 struct kvm *kvm = vcpu->kvm;
570 int r;
571
572 /*Insert a pair of tr to map vmm*/
573 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
574 r = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
575 if (r < 0)
576 goto out;
577 vcpu->arch.vmm_tr_slot = r;
578 /*Insert a pairt of tr to map data of vm*/
579 pte = pte_val(mk_pte_phys(__pa(kvm->arch.vm_base), PAGE_KERNEL));
580 r = ia64_itr_entry(0x3, KVM_VM_DATA_BASE,
581 pte, KVM_VM_DATA_SHIFT);
582 if (r < 0)
583 goto out;
584 vcpu->arch.vm_tr_slot = r;
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600585
586#if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
587 if (kvm->arch.is_sn2) {
588 r = kvm_sn2_setup_mappings(vcpu);
589 if (r < 0)
590 goto out;
591 }
592#endif
593
Xiantao Zhangb024b792008-04-01 15:29:29 +0800594 r = 0;
595out:
596 return r;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800597}
598
599static void kvm_purge_vmm_mapping(struct kvm_vcpu *vcpu)
600{
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600601 struct kvm *kvm = vcpu->kvm;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800602 ia64_ptr_entry(0x3, vcpu->arch.vmm_tr_slot);
603 ia64_ptr_entry(0x3, vcpu->arch.vm_tr_slot);
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600604#if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
605 if (kvm->arch.is_sn2)
606 ia64_ptr_entry(0x3, vcpu->arch.sn_rtc_tr_slot);
607#endif
Xiantao Zhangb024b792008-04-01 15:29:29 +0800608}
609
610static int kvm_vcpu_pre_transition(struct kvm_vcpu *vcpu)
611{
Jes Sorensen4d13c3b2009-04-16 16:53:13 +0200612 unsigned long psr;
613 int r;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800614 int cpu = smp_processor_id();
615
616 if (vcpu->arch.last_run_cpu != cpu ||
617 per_cpu(last_vcpu, cpu) != vcpu) {
618 per_cpu(last_vcpu, cpu) = vcpu;
619 vcpu->arch.last_run_cpu = cpu;
620 kvm_flush_tlb_all();
621 }
622
623 vcpu->arch.host_rr6 = ia64_get_rr(RR6);
624 vti_set_rr6(vcpu->arch.vmm_rr);
Jes Sorensen4d13c3b2009-04-16 16:53:13 +0200625 local_irq_save(psr);
626 r = kvm_insert_vmm_mapping(vcpu);
627 local_irq_restore(psr);
628 return r;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800629}
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200630
Xiantao Zhangb024b792008-04-01 15:29:29 +0800631static void kvm_vcpu_post_transition(struct kvm_vcpu *vcpu)
632{
633 kvm_purge_vmm_mapping(vcpu);
634 vti_set_rr6(vcpu->arch.host_rr6);
635}
636
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200637static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800638{
639 union context *host_ctx, *guest_ctx;
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200640 int r, idx;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800641
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200642 idx = srcu_read_lock(&vcpu->kvm->srcu);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800643
644again:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800645 if (signal_pending(current)) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800646 r = -EINTR;
647 kvm_run->exit_reason = KVM_EXIT_INTR;
648 goto out;
649 }
650
Jes Sorensend24d2c12009-04-09 16:38:14 +0200651 preempt_disable();
652 local_irq_disable();
653
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200654 /*Get host and guest context with guest address space.*/
655 host_ctx = kvm_get_host_context(vcpu);
656 guest_ctx = kvm_get_guest_context(vcpu);
657
Marcelo Tosatti32f88402009-05-07 17:55:12 -0300658 clear_bit(KVM_REQ_KICK, &vcpu->requests);
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200659
660 r = kvm_vcpu_pre_transition(vcpu);
661 if (r < 0)
662 goto vcpu_run_fail;
663
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200664 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +0800665 vcpu->mode = IN_GUEST_MODE;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800666 kvm_guest_enter();
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200667
668 /*
669 * Transition to the guest
670 */
671 kvm_vmm_info->tramp_entry(host_ctx, guest_ctx);
672
673 kvm_vcpu_post_transition(vcpu);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800674
675 vcpu->arch.launched = 1;
Marcelo Tosatti32f88402009-05-07 17:55:12 -0300676 set_bit(KVM_REQ_KICK, &vcpu->requests);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800677 local_irq_enable();
678
679 /*
680 * We must have an instruction between local_irq_enable() and
681 * kvm_guest_exit(), so the timer interrupt isn't delayed by
682 * the interrupt shadow. The stat.exits increment will do nicely.
683 * But we need to prevent reordering, hence this barrier():
684 */
685 barrier();
Xiantao Zhangb024b792008-04-01 15:29:29 +0800686 kvm_guest_exit();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +0800687 vcpu->mode = OUTSIDE_GUEST_MODE;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800688 preempt_enable();
689
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200690 idx = srcu_read_lock(&vcpu->kvm->srcu);
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200691
Xiantao Zhangb024b792008-04-01 15:29:29 +0800692 r = kvm_handle_exit(kvm_run, vcpu);
693
694 if (r > 0) {
695 if (!need_resched())
696 goto again;
697 }
698
699out:
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200700 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800701 if (r > 0) {
702 kvm_resched(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200703 idx = srcu_read_lock(&vcpu->kvm->srcu);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800704 goto again;
705 }
706
707 return r;
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200708
709vcpu_run_fail:
710 local_irq_enable();
711 preempt_enable();
712 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
713 goto out;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800714}
715
716static void kvm_set_mmio_data(struct kvm_vcpu *vcpu)
717{
718 struct kvm_mmio_req *p = kvm_get_vcpu_ioreq(vcpu);
719
720 if (!vcpu->mmio_is_write)
721 memcpy(&p->data, vcpu->mmio_data, 8);
722 p->state = STATE_IORESP_READY;
723}
724
725int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
726{
727 int r;
728 sigset_t sigsaved;
729
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800730 if (vcpu->sigset_active)
731 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
732
Avi Kivitya4535292008-04-13 17:54:35 +0300733 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800734 kvm_vcpu_block(vcpu);
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800735 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800736 r = -EAGAIN;
737 goto out;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800738 }
739
Xiantao Zhangb024b792008-04-01 15:29:29 +0800740 if (vcpu->mmio_needed) {
741 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
742 kvm_set_mmio_data(vcpu);
743 vcpu->mmio_read_completed = 1;
744 vcpu->mmio_needed = 0;
745 }
746 r = __vcpu_run(vcpu, kvm_run);
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800747out:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800748 if (vcpu->sigset_active)
749 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
750
Xiantao Zhangb024b792008-04-01 15:29:29 +0800751 return r;
752}
753
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100754struct kvm *kvm_arch_alloc_vm(void)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800755{
756
757 struct kvm *kvm;
758 uint64_t vm_base;
759
Xiantao Zhanga917f7af32008-10-23 14:56:44 +0800760 BUG_ON(sizeof(struct kvm) > KVM_VM_STRUCT_SIZE);
761
Xiantao Zhangb024b792008-04-01 15:29:29 +0800762 vm_base = __get_free_pages(GFP_KERNEL, get_order(KVM_VM_DATA_SIZE));
763
764 if (!vm_base)
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100765 return NULL;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800766
Xiantao Zhangb024b792008-04-01 15:29:29 +0800767 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
Xiantao Zhanga917f7af32008-10-23 14:56:44 +0800768 kvm = (struct kvm *)(vm_base +
769 offsetof(struct kvm_vm_data, kvm_vm_struct));
Xiantao Zhangb024b792008-04-01 15:29:29 +0800770 kvm->arch.vm_base = vm_base;
Xiantao Zhanga917f7af32008-10-23 14:56:44 +0800771 printk(KERN_DEBUG"kvm: vm's data area:0x%lx\n", vm_base);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800772
773 return kvm;
774}
775
776struct kvm_io_range {
777 unsigned long start;
778 unsigned long size;
779 unsigned long type;
780};
781
782static const struct kvm_io_range io_ranges[] = {
783 {VGA_IO_START, VGA_IO_SIZE, GPFN_FRAME_BUFFER},
784 {MMIO_START, MMIO_SIZE, GPFN_LOW_MMIO},
785 {LEGACY_IO_START, LEGACY_IO_SIZE, GPFN_LEGACY_IO},
786 {IO_SAPIC_START, IO_SAPIC_SIZE, GPFN_IOSAPIC},
787 {PIB_START, PIB_SIZE, GPFN_PIB},
788};
789
790static void kvm_build_io_pmt(struct kvm *kvm)
791{
792 unsigned long i, j;
793
794 /* Mark I/O ranges */
795 for (i = 0; i < (sizeof(io_ranges) / sizeof(struct kvm_io_range));
796 i++) {
797 for (j = io_ranges[i].start;
798 j < io_ranges[i].start + io_ranges[i].size;
799 j += PAGE_SIZE)
800 kvm_set_pmt_entry(kvm, j >> PAGE_SHIFT,
801 io_ranges[i].type, 0);
802 }
803
804}
805
806/*Use unused rids to virtualize guest rid.*/
807#define GUEST_PHYSICAL_RR0 0x1739
808#define GUEST_PHYSICAL_RR4 0x2739
809#define VMM_INIT_RR 0x1660
810
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100811int kvm_arch_init_vm(struct kvm *kvm)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800812{
Xiantao Zhangb024b792008-04-01 15:29:29 +0800813 BUG_ON(!kvm);
814
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100815 kvm->arch.is_sn2 = ia64_platform_is("sn2");
816
Xiantao Zhangb024b792008-04-01 15:29:29 +0800817 kvm->arch.metaphysical_rr0 = GUEST_PHYSICAL_RR0;
818 kvm->arch.metaphysical_rr4 = GUEST_PHYSICAL_RR4;
819 kvm->arch.vmm_init_rr = VMM_INIT_RR;
820
Xiantao Zhangb024b792008-04-01 15:29:29 +0800821 /*
822 *Fill P2M entries for MMIO/IO ranges
823 */
824 kvm_build_io_pmt(kvm);
825
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800826 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Sheng Yang5550af42008-10-15 20:15:06 +0800827
828 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
829 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800830
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100831 return 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800832}
833
834static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm,
835 struct kvm_irqchip *chip)
836{
837 int r;
838
839 r = 0;
840 switch (chip->chip_id) {
841 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +0300842 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800843 break;
844 default:
845 r = -EINVAL;
846 break;
847 }
848 return r;
849}
850
851static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
852{
853 int r;
854
855 r = 0;
856 switch (chip->chip_id) {
857 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +0300858 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800859 break;
860 default:
861 r = -EINVAL;
862 break;
863 }
864 return r;
865}
866
867#define RESTORE_REGS(_x) vcpu->arch._x = regs->_x
868
869int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
870{
Xiantao Zhangb024b792008-04-01 15:29:29 +0800871 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
Jes Sorensen042b26e2008-12-16 16:45:47 +0100872 int i;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800873
Xiantao Zhangb024b792008-04-01 15:29:29 +0800874 for (i = 0; i < 16; i++) {
875 vpd->vgr[i] = regs->vpd.vgr[i];
876 vpd->vbgr[i] = regs->vpd.vbgr[i];
877 }
878 for (i = 0; i < 128; i++)
879 vpd->vcr[i] = regs->vpd.vcr[i];
880 vpd->vhpi = regs->vpd.vhpi;
881 vpd->vnat = regs->vpd.vnat;
882 vpd->vbnat = regs->vpd.vbnat;
883 vpd->vpsr = regs->vpd.vpsr;
884
885 vpd->vpr = regs->vpd.vpr;
886
Jes Sorensen042b26e2008-12-16 16:45:47 +0100887 memcpy(&vcpu->arch.guest, &regs->saved_guest, sizeof(union context));
Xiantao Zhangb024b792008-04-01 15:29:29 +0800888
889 RESTORE_REGS(mp_state);
890 RESTORE_REGS(vmm_rr);
891 memcpy(vcpu->arch.itrs, regs->itrs, sizeof(struct thash_data) * NITRS);
892 memcpy(vcpu->arch.dtrs, regs->dtrs, sizeof(struct thash_data) * NDTRS);
893 RESTORE_REGS(itr_regions);
894 RESTORE_REGS(dtr_regions);
895 RESTORE_REGS(tc_regions);
896 RESTORE_REGS(irq_check);
897 RESTORE_REGS(itc_check);
898 RESTORE_REGS(timer_check);
899 RESTORE_REGS(timer_pending);
900 RESTORE_REGS(last_itc);
901 for (i = 0; i < 8; i++) {
902 vcpu->arch.vrr[i] = regs->vrr[i];
903 vcpu->arch.ibr[i] = regs->ibr[i];
904 vcpu->arch.dbr[i] = regs->dbr[i];
905 }
906 for (i = 0; i < 4; i++)
907 vcpu->arch.insvc[i] = regs->insvc[i];
908 RESTORE_REGS(xtp);
909 RESTORE_REGS(metaphysical_rr0);
910 RESTORE_REGS(metaphysical_rr4);
911 RESTORE_REGS(metaphysical_saved_rr0);
912 RESTORE_REGS(metaphysical_saved_rr4);
913 RESTORE_REGS(fp_psr);
914 RESTORE_REGS(saved_gp);
915
916 vcpu->arch.irq_new_pending = 1;
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -0600917 vcpu->arch.itc_offset = regs->saved_itc - kvm_get_itc(vcpu);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800918 set_bit(KVM_REQ_RESUME, &vcpu->requests);
919
Jes Sorensen042b26e2008-12-16 16:45:47 +0100920 return 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800921}
922
923long kvm_arch_vm_ioctl(struct file *filp,
924 unsigned int ioctl, unsigned long arg)
925{
926 struct kvm *kvm = filp->private_data;
927 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +0300928 int r = -ENOTTY;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800929
930 switch (ioctl) {
931 case KVM_SET_MEMORY_REGION: {
932 struct kvm_memory_region kvm_mem;
933 struct kvm_userspace_memory_region kvm_userspace_mem;
934
935 r = -EFAULT;
936 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
937 goto out;
938 kvm_userspace_mem.slot = kvm_mem.slot;
939 kvm_userspace_mem.flags = kvm_mem.flags;
940 kvm_userspace_mem.guest_phys_addr =
941 kvm_mem.guest_phys_addr;
942 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
943 r = kvm_vm_ioctl_set_memory_region(kvm,
944 &kvm_userspace_mem, 0);
945 if (r)
946 goto out;
947 break;
948 }
949 case KVM_CREATE_IRQCHIP:
950 r = -EFAULT;
951 r = kvm_ioapic_init(kvm);
952 if (r)
953 goto out;
Avi Kivity399ec802008-11-19 13:58:46 +0200954 r = kvm_setup_default_irq_routing(kvm);
955 if (r) {
Takuya Yoshikawa175504c2010-12-16 01:41:37 +0900956 mutex_lock(&kvm->slots_lock);
Wei Yongjun4b7bb922010-02-09 10:41:56 +0800957 kvm_ioapic_destroy(kvm);
Takuya Yoshikawa175504c2010-12-16 01:41:37 +0900958 mutex_unlock(&kvm->slots_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200959 goto out;
960 }
Xiantao Zhangb024b792008-04-01 15:29:29 +0800961 break;
Gleb Natapov49256632009-02-04 17:28:14 +0200962 case KVM_IRQ_LINE_STATUS:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800963 case KVM_IRQ_LINE: {
964 struct kvm_irq_level irq_event;
965
966 r = -EFAULT;
967 if (copy_from_user(&irq_event, argp, sizeof irq_event))
968 goto out;
Wei Yongjun600f1ec2010-03-12 10:11:15 +0800969 r = -ENXIO;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800970 if (irqchip_in_kernel(kvm)) {
Gleb Natapov49256632009-02-04 17:28:14 +0200971 __s32 status;
Gleb Natapov49256632009-02-04 17:28:14 +0200972 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Sheng Yang5550af42008-10-15 20:15:06 +0800973 irq_event.irq, irq_event.level);
Gleb Natapov49256632009-02-04 17:28:14 +0200974 if (ioctl == KVM_IRQ_LINE_STATUS) {
Wei Yongjun600f1ec2010-03-12 10:11:15 +0800975 r = -EFAULT;
Gleb Natapov49256632009-02-04 17:28:14 +0200976 irq_event.status = status;
977 if (copy_to_user(argp, &irq_event,
978 sizeof irq_event))
979 goto out;
980 }
Xiantao Zhangb024b792008-04-01 15:29:29 +0800981 r = 0;
982 }
983 break;
984 }
985 case KVM_GET_IRQCHIP: {
986 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
987 struct kvm_irqchip chip;
988
989 r = -EFAULT;
990 if (copy_from_user(&chip, argp, sizeof chip))
991 goto out;
992 r = -ENXIO;
993 if (!irqchip_in_kernel(kvm))
994 goto out;
995 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
996 if (r)
997 goto out;
998 r = -EFAULT;
999 if (copy_to_user(argp, &chip, sizeof chip))
1000 goto out;
1001 r = 0;
1002 break;
1003 }
1004 case KVM_SET_IRQCHIP: {
1005 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1006 struct kvm_irqchip chip;
1007
1008 r = -EFAULT;
1009 if (copy_from_user(&chip, argp, sizeof chip))
1010 goto out;
1011 r = -ENXIO;
1012 if (!irqchip_in_kernel(kvm))
1013 goto out;
1014 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1015 if (r)
1016 goto out;
1017 r = 0;
1018 break;
1019 }
1020 default:
1021 ;
1022 }
1023out:
1024 return r;
1025}
1026
1027int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1028 struct kvm_sregs *sregs)
1029{
1030 return -EINVAL;
1031}
1032
1033int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1034 struct kvm_sregs *sregs)
1035{
1036 return -EINVAL;
1037
1038}
1039int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1040 struct kvm_translation *tr)
1041{
1042
1043 return -EINVAL;
1044}
1045
1046static int kvm_alloc_vmm_area(void)
1047{
1048 if (!kvm_vmm_base && (kvm_vm_buffer_size < KVM_VM_BUFFER_SIZE)) {
1049 kvm_vmm_base = __get_free_pages(GFP_KERNEL,
1050 get_order(KVM_VMM_SIZE));
1051 if (!kvm_vmm_base)
1052 return -ENOMEM;
1053
1054 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1055 kvm_vm_buffer = kvm_vmm_base + VMM_SIZE;
1056
1057 printk(KERN_DEBUG"kvm:VMM's Base Addr:0x%lx, vm_buffer:0x%lx\n",
1058 kvm_vmm_base, kvm_vm_buffer);
1059 }
1060
1061 return 0;
1062}
1063
1064static void kvm_free_vmm_area(void)
1065{
1066 if (kvm_vmm_base) {
1067 /*Zero this area before free to avoid bits leak!!*/
1068 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1069 free_pages(kvm_vmm_base, get_order(KVM_VMM_SIZE));
1070 kvm_vmm_base = 0;
1071 kvm_vm_buffer = 0;
1072 kvm_vsa_base = 0;
1073 }
1074}
1075
Xiantao Zhangb024b792008-04-01 15:29:29 +08001076static int vti_init_vpd(struct kvm_vcpu *vcpu)
1077{
1078 int i;
1079 union cpuid3_t cpuid3;
1080 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1081
1082 if (IS_ERR(vpd))
1083 return PTR_ERR(vpd);
1084
1085 /* CPUID init */
1086 for (i = 0; i < 5; i++)
1087 vpd->vcpuid[i] = ia64_get_cpuid(i);
1088
1089 /* Limit the CPUID number to 5 */
1090 cpuid3.value = vpd->vcpuid[3];
1091 cpuid3.number = 4; /* 5 - 1 */
1092 vpd->vcpuid[3] = cpuid3.value;
1093
1094 /*Set vac and vdc fields*/
1095 vpd->vac.a_from_int_cr = 1;
1096 vpd->vac.a_to_int_cr = 1;
1097 vpd->vac.a_from_psr = 1;
1098 vpd->vac.a_from_cpuid = 1;
1099 vpd->vac.a_cover = 1;
1100 vpd->vac.a_bsw = 1;
1101 vpd->vac.a_int = 1;
1102 vpd->vdc.d_vmsw = 1;
1103
1104 /*Set virtual buffer*/
1105 vpd->virt_env_vaddr = KVM_VM_BUFFER_BASE;
1106
1107 return 0;
1108}
1109
1110static int vti_create_vp(struct kvm_vcpu *vcpu)
1111{
1112 long ret;
1113 struct vpd *vpd = vcpu->arch.vpd;
1114 unsigned long vmm_ivt;
1115
1116 vmm_ivt = kvm_vmm_info->vmm_ivt;
1117
1118 printk(KERN_DEBUG "kvm: vcpu:%p,ivt: 0x%lx\n", vcpu, vmm_ivt);
1119
1120 ret = ia64_pal_vp_create((u64 *)vpd, (u64 *)vmm_ivt, 0);
1121
1122 if (ret) {
1123 printk(KERN_ERR"kvm: ia64_pal_vp_create failed!\n");
1124 return -EINVAL;
1125 }
1126 return 0;
1127}
1128
1129static void init_ptce_info(struct kvm_vcpu *vcpu)
1130{
1131 ia64_ptce_info_t ptce = {0};
1132
1133 ia64_get_ptce(&ptce);
1134 vcpu->arch.ptce_base = ptce.base;
1135 vcpu->arch.ptce_count[0] = ptce.count[0];
1136 vcpu->arch.ptce_count[1] = ptce.count[1];
1137 vcpu->arch.ptce_stride[0] = ptce.stride[0];
1138 vcpu->arch.ptce_stride[1] = ptce.stride[1];
1139}
1140
1141static void kvm_migrate_hlt_timer(struct kvm_vcpu *vcpu)
1142{
1143 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
1144
1145 if (hrtimer_cancel(p_ht))
Arjan van de Ven18dd36a2008-09-01 15:19:11 -07001146 hrtimer_start_expires(p_ht, HRTIMER_MODE_ABS);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001147}
1148
1149static enum hrtimer_restart hlt_timer_fn(struct hrtimer *data)
1150{
1151 struct kvm_vcpu *vcpu;
1152 wait_queue_head_t *q;
1153
1154 vcpu = container_of(data, struct kvm_vcpu, arch.hlt_timer);
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001155 q = &vcpu->wq;
1156
Avi Kivitya4535292008-04-13 17:54:35 +03001157 if (vcpu->arch.mp_state != KVM_MP_STATE_HALTED)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001158 goto out;
1159
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001160 if (waitqueue_active(q))
Xiantao Zhangb024b792008-04-01 15:29:29 +08001161 wake_up_interruptible(q);
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001162
Xiantao Zhangb024b792008-04-01 15:29:29 +08001163out:
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001164 vcpu->arch.timer_fired = 1;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001165 vcpu->arch.timer_check = 1;
1166 return HRTIMER_NORESTART;
1167}
1168
1169#define PALE_RESET_ENTRY 0x80000000ffffffb0UL
1170
1171int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1172{
1173 struct kvm_vcpu *v;
1174 int r;
1175 int i;
1176 long itc_offset;
1177 struct kvm *kvm = vcpu->kvm;
1178 struct kvm_pt_regs *regs = vcpu_regs(vcpu);
1179
1180 union context *p_ctx = &vcpu->arch.guest;
1181 struct kvm_vcpu *vmm_vcpu = to_guest(vcpu->kvm, vcpu);
1182
1183 /*Init vcpu context for first run.*/
1184 if (IS_ERR(vmm_vcpu))
1185 return PTR_ERR(vmm_vcpu);
1186
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001187 if (kvm_vcpu_is_bsp(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03001188 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001189
1190 /*Set entry address for first run.*/
1191 regs->cr_iip = PALE_RESET_ENTRY;
1192
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001193 /*Initialize itc offset for vcpus*/
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -06001194 itc_offset = 0UL - kvm_get_itc(vcpu);
Jes Sorensen3032b922009-05-25 10:22:17 +02001195 for (i = 0; i < KVM_MAX_VCPUS; i++) {
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001196 v = (struct kvm_vcpu *)((char *)vcpu +
1197 sizeof(struct kvm_vcpu_data) * i);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001198 v->arch.itc_offset = itc_offset;
1199 v->arch.last_itc = 0;
1200 }
1201 } else
Avi Kivitya4535292008-04-13 17:54:35 +03001202 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001203
1204 r = -ENOMEM;
1205 vcpu->arch.apic = kzalloc(sizeof(struct kvm_lapic), GFP_KERNEL);
1206 if (!vcpu->arch.apic)
1207 goto out;
1208 vcpu->arch.apic->vcpu = vcpu;
1209
1210 p_ctx->gr[1] = 0;
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001211 p_ctx->gr[12] = (unsigned long)((char *)vmm_vcpu + KVM_STK_OFFSET);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001212 p_ctx->gr[13] = (unsigned long)vmm_vcpu;
1213 p_ctx->psr = 0x1008522000UL;
1214 p_ctx->ar[40] = FPSR_DEFAULT; /*fpsr*/
1215 p_ctx->caller_unat = 0;
1216 p_ctx->pr = 0x0;
1217 p_ctx->ar[36] = 0x0; /*unat*/
1218 p_ctx->ar[19] = 0x0; /*rnat*/
1219 p_ctx->ar[18] = (unsigned long)vmm_vcpu +
1220 ((sizeof(struct kvm_vcpu)+15) & ~15);
1221 p_ctx->ar[64] = 0x0; /*pfs*/
1222 p_ctx->cr[0] = 0x7e04UL;
1223 p_ctx->cr[2] = (unsigned long)kvm_vmm_info->vmm_ivt;
1224 p_ctx->cr[8] = 0x3c;
1225
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001226 /*Initialize region register*/
Xiantao Zhangb024b792008-04-01 15:29:29 +08001227 p_ctx->rr[0] = 0x30;
1228 p_ctx->rr[1] = 0x30;
1229 p_ctx->rr[2] = 0x30;
1230 p_ctx->rr[3] = 0x30;
1231 p_ctx->rr[4] = 0x30;
1232 p_ctx->rr[5] = 0x30;
1233 p_ctx->rr[7] = 0x30;
1234
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001235 /*Initialize branch register 0*/
Xiantao Zhangb024b792008-04-01 15:29:29 +08001236 p_ctx->br[0] = *(unsigned long *)kvm_vmm_info->vmm_entry;
1237
1238 vcpu->arch.vmm_rr = kvm->arch.vmm_init_rr;
1239 vcpu->arch.metaphysical_rr0 = kvm->arch.metaphysical_rr0;
1240 vcpu->arch.metaphysical_rr4 = kvm->arch.metaphysical_rr4;
1241
1242 hrtimer_init(&vcpu->arch.hlt_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1243 vcpu->arch.hlt_timer.function = hlt_timer_fn;
1244
1245 vcpu->arch.last_run_cpu = -1;
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001246 vcpu->arch.vpd = (struct vpd *)VPD_BASE(vcpu->vcpu_id);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001247 vcpu->arch.vsa_base = kvm_vsa_base;
1248 vcpu->arch.__gp = kvm_vmm_gp;
1249 vcpu->arch.dirty_log_lock_pa = __pa(&kvm->arch.dirty_log_lock);
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001250 vcpu->arch.vhpt.hash = (struct thash_data *)VHPT_BASE(vcpu->vcpu_id);
1251 vcpu->arch.vtlb.hash = (struct thash_data *)VTLB_BASE(vcpu->vcpu_id);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001252 init_ptce_info(vcpu);
1253
1254 r = 0;
1255out:
1256 return r;
1257}
1258
1259static int vti_vcpu_setup(struct kvm_vcpu *vcpu, int id)
1260{
1261 unsigned long psr;
1262 int r;
1263
1264 local_irq_save(psr);
1265 r = kvm_insert_vmm_mapping(vcpu);
Jes Sorensen457459c2009-04-16 16:08:29 +02001266 local_irq_restore(psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001267 if (r)
1268 goto fail;
1269 r = kvm_vcpu_init(vcpu, vcpu->kvm, id);
1270 if (r)
1271 goto fail;
1272
1273 r = vti_init_vpd(vcpu);
1274 if (r) {
1275 printk(KERN_DEBUG"kvm: vpd init error!!\n");
1276 goto uninit;
1277 }
1278
1279 r = vti_create_vp(vcpu);
1280 if (r)
1281 goto uninit;
1282
1283 kvm_purge_vmm_mapping(vcpu);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001284
1285 return 0;
1286uninit:
1287 kvm_vcpu_uninit(vcpu);
1288fail:
1289 return r;
1290}
1291
1292struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1293 unsigned int id)
1294{
1295 struct kvm_vcpu *vcpu;
1296 unsigned long vm_base = kvm->arch.vm_base;
1297 int r;
1298 int cpu;
1299
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001300 BUG_ON(sizeof(struct kvm_vcpu) > VCPU_STRUCT_SIZE/2);
1301
1302 r = -EINVAL;
1303 if (id >= KVM_MAX_VCPUS) {
1304 printk(KERN_ERR"kvm: Can't configure vcpus > %ld",
1305 KVM_MAX_VCPUS);
1306 goto fail;
1307 }
1308
Xiantao Zhangb024b792008-04-01 15:29:29 +08001309 r = -ENOMEM;
1310 if (!vm_base) {
1311 printk(KERN_ERR"kvm: Create vcpu[%d] error!\n", id);
1312 goto fail;
1313 }
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001314 vcpu = (struct kvm_vcpu *)(vm_base + offsetof(struct kvm_vm_data,
1315 vcpu_data[id].vcpu_struct));
Xiantao Zhangb024b792008-04-01 15:29:29 +08001316 vcpu->kvm = kvm;
1317
1318 cpu = get_cpu();
Xiantao Zhangb024b792008-04-01 15:29:29 +08001319 r = vti_vcpu_setup(vcpu, id);
1320 put_cpu();
1321
1322 if (r) {
1323 printk(KERN_DEBUG"kvm: vcpu_setup error!!\n");
1324 goto fail;
1325 }
1326
1327 return vcpu;
1328fail:
1329 return ERR_PTR(r);
1330}
1331
1332int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1333{
1334 return 0;
1335}
1336
1337int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1338{
1339 return -EINVAL;
1340}
1341
1342int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1343{
1344 return -EINVAL;
1345}
1346
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001347int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1348 struct kvm_guest_debug *dbg)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001349{
1350 return -EINVAL;
1351}
1352
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001353void kvm_arch_free_vm(struct kvm *kvm)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001354{
1355 unsigned long vm_base = kvm->arch.vm_base;
1356
1357 if (vm_base) {
1358 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
1359 free_pages(vm_base, get_order(KVM_VM_DATA_SIZE));
1360 }
1361
1362}
1363
1364static void kvm_release_vm_pages(struct kvm *kvm)
1365{
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001366 struct kvm_memslots *slots;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001367 struct kvm_memory_slot *memslot;
1368 int i, j;
1369 unsigned long base_gfn;
1370
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08001371 slots = kvm_memslots(kvm);
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001372 for (i = 0; i < slots->nmemslots; i++) {
1373 memslot = &slots->memslots[i];
Xiantao Zhangb024b792008-04-01 15:29:29 +08001374 base_gfn = memslot->base_gfn;
1375
1376 for (j = 0; j < memslot->npages; j++) {
1377 if (memslot->rmap[j])
1378 put_page((struct page *)memslot->rmap[j]);
1379 }
1380 }
1381}
1382
Sheng Yangad8ba2c2009-01-06 10:03:02 +08001383void kvm_arch_sync_events(struct kvm *kvm)
1384{
1385}
1386
Xiantao Zhangb024b792008-04-01 15:29:29 +08001387void kvm_arch_destroy_vm(struct kvm *kvm)
1388{
Xiantao Zhang2381ad22008-10-08 08:29:33 +08001389 kvm_iommu_unmap_guest(kvm);
1390#ifdef KVM_CAP_DEVICE_ASSIGNMENT
1391 kvm_free_all_assigned_devices(kvm);
1392#endif
Xiantao Zhangb024b792008-04-01 15:29:29 +08001393 kfree(kvm->arch.vioapic);
1394 kvm_release_vm_pages(kvm);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001395}
1396
1397void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1398{
1399}
1400
1401void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1402{
1403 if (cpu != vcpu->cpu) {
1404 vcpu->cpu = cpu;
1405 if (vcpu->arch.ht_active)
1406 kvm_migrate_hlt_timer(vcpu);
1407 }
1408}
1409
1410#define SAVE_REGS(_x) regs->_x = vcpu->arch._x
1411
1412int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1413{
Xiantao Zhangb024b792008-04-01 15:29:29 +08001414 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
Jes Sorensen042b26e2008-12-16 16:45:47 +01001415 int i;
1416
Xiantao Zhangb024b792008-04-01 15:29:29 +08001417 vcpu_load(vcpu);
1418
1419 for (i = 0; i < 16; i++) {
1420 regs->vpd.vgr[i] = vpd->vgr[i];
1421 regs->vpd.vbgr[i] = vpd->vbgr[i];
1422 }
1423 for (i = 0; i < 128; i++)
1424 regs->vpd.vcr[i] = vpd->vcr[i];
1425 regs->vpd.vhpi = vpd->vhpi;
1426 regs->vpd.vnat = vpd->vnat;
1427 regs->vpd.vbnat = vpd->vbnat;
1428 regs->vpd.vpsr = vpd->vpsr;
1429 regs->vpd.vpr = vpd->vpr;
1430
Jes Sorensen042b26e2008-12-16 16:45:47 +01001431 memcpy(&regs->saved_guest, &vcpu->arch.guest, sizeof(union context));
1432
Xiantao Zhangb024b792008-04-01 15:29:29 +08001433 SAVE_REGS(mp_state);
1434 SAVE_REGS(vmm_rr);
1435 memcpy(regs->itrs, vcpu->arch.itrs, sizeof(struct thash_data) * NITRS);
1436 memcpy(regs->dtrs, vcpu->arch.dtrs, sizeof(struct thash_data) * NDTRS);
1437 SAVE_REGS(itr_regions);
1438 SAVE_REGS(dtr_regions);
1439 SAVE_REGS(tc_regions);
1440 SAVE_REGS(irq_check);
1441 SAVE_REGS(itc_check);
1442 SAVE_REGS(timer_check);
1443 SAVE_REGS(timer_pending);
1444 SAVE_REGS(last_itc);
1445 for (i = 0; i < 8; i++) {
1446 regs->vrr[i] = vcpu->arch.vrr[i];
1447 regs->ibr[i] = vcpu->arch.ibr[i];
1448 regs->dbr[i] = vcpu->arch.dbr[i];
1449 }
1450 for (i = 0; i < 4; i++)
1451 regs->insvc[i] = vcpu->arch.insvc[i];
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -06001452 regs->saved_itc = vcpu->arch.itc_offset + kvm_get_itc(vcpu);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001453 SAVE_REGS(xtp);
1454 SAVE_REGS(metaphysical_rr0);
1455 SAVE_REGS(metaphysical_rr4);
1456 SAVE_REGS(metaphysical_saved_rr0);
1457 SAVE_REGS(metaphysical_saved_rr4);
1458 SAVE_REGS(fp_psr);
1459 SAVE_REGS(saved_gp);
Jes Sorensen042b26e2008-12-16 16:45:47 +01001460
Xiantao Zhangb024b792008-04-01 15:29:29 +08001461 vcpu_put(vcpu);
Jes Sorensen042b26e2008-12-16 16:45:47 +01001462 return 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001463}
1464
Jes Sorensene9a999f2008-12-18 12:17:51 +01001465int kvm_arch_vcpu_ioctl_get_stack(struct kvm_vcpu *vcpu,
1466 struct kvm_ia64_vcpu_stack *stack)
1467{
1468 memcpy(stack, vcpu, sizeof(struct kvm_ia64_vcpu_stack));
1469 return 0;
1470}
1471
1472int kvm_arch_vcpu_ioctl_set_stack(struct kvm_vcpu *vcpu,
1473 struct kvm_ia64_vcpu_stack *stack)
1474{
1475 memcpy(vcpu + 1, &stack->stack[0] + sizeof(struct kvm_vcpu),
1476 sizeof(struct kvm_ia64_vcpu_stack) - sizeof(struct kvm_vcpu));
1477
1478 vcpu->arch.exit_data = ((struct kvm_vcpu *)stack)->arch.exit_data;
1479 return 0;
1480}
1481
Xiantao Zhangb024b792008-04-01 15:29:29 +08001482void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1483{
1484
1485 hrtimer_cancel(&vcpu->arch.hlt_timer);
1486 kfree(vcpu->arch.apic);
1487}
1488
1489
1490long kvm_arch_vcpu_ioctl(struct file *filp,
Jes Sorensene9a999f2008-12-18 12:17:51 +01001491 unsigned int ioctl, unsigned long arg)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001492{
Jes Sorensene9a999f2008-12-18 12:17:51 +01001493 struct kvm_vcpu *vcpu = filp->private_data;
1494 void __user *argp = (void __user *)arg;
1495 struct kvm_ia64_vcpu_stack *stack = NULL;
1496 long r;
1497
1498 switch (ioctl) {
1499 case KVM_IA64_VCPU_GET_STACK: {
1500 struct kvm_ia64_vcpu_stack __user *user_stack;
1501 void __user *first_p = argp;
1502
1503 r = -EFAULT;
1504 if (copy_from_user(&user_stack, first_p, sizeof(void *)))
1505 goto out;
1506
1507 if (!access_ok(VERIFY_WRITE, user_stack,
1508 sizeof(struct kvm_ia64_vcpu_stack))) {
1509 printk(KERN_INFO "KVM_IA64_VCPU_GET_STACK: "
1510 "Illegal user destination address for stack\n");
1511 goto out;
1512 }
1513 stack = kzalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL);
1514 if (!stack) {
1515 r = -ENOMEM;
1516 goto out;
1517 }
1518
1519 r = kvm_arch_vcpu_ioctl_get_stack(vcpu, stack);
1520 if (r)
1521 goto out;
1522
1523 if (copy_to_user(user_stack, stack,
Wei Yongjun5c0d0922010-03-12 08:45:39 +08001524 sizeof(struct kvm_ia64_vcpu_stack))) {
1525 r = -EFAULT;
Jes Sorensene9a999f2008-12-18 12:17:51 +01001526 goto out;
Wei Yongjun5c0d0922010-03-12 08:45:39 +08001527 }
Jes Sorensene9a999f2008-12-18 12:17:51 +01001528
1529 break;
1530 }
1531 case KVM_IA64_VCPU_SET_STACK: {
1532 struct kvm_ia64_vcpu_stack __user *user_stack;
1533 void __user *first_p = argp;
1534
1535 r = -EFAULT;
1536 if (copy_from_user(&user_stack, first_p, sizeof(void *)))
1537 goto out;
1538
1539 if (!access_ok(VERIFY_READ, user_stack,
1540 sizeof(struct kvm_ia64_vcpu_stack))) {
1541 printk(KERN_INFO "KVM_IA64_VCPU_SET_STACK: "
1542 "Illegal user address for stack\n");
1543 goto out;
1544 }
1545 stack = kmalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL);
1546 if (!stack) {
1547 r = -ENOMEM;
1548 goto out;
1549 }
1550 if (copy_from_user(stack, user_stack,
1551 sizeof(struct kvm_ia64_vcpu_stack)))
1552 goto out;
1553
1554 r = kvm_arch_vcpu_ioctl_set_stack(vcpu, stack);
1555 break;
1556 }
1557
1558 default:
1559 r = -EINVAL;
1560 }
1561
1562out:
1563 kfree(stack);
1564 return r;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001565}
1566
Marcelo Tosattif7784b82009-12-23 14:35:18 -02001567int kvm_arch_prepare_memory_region(struct kvm *kvm,
1568 struct kvm_memory_slot *memslot,
Xiantao Zhangb024b792008-04-01 15:29:29 +08001569 struct kvm_memory_slot old,
Marcelo Tosattif7784b82009-12-23 14:35:18 -02001570 struct kvm_userspace_memory_region *mem,
Xiantao Zhangb024b792008-04-01 15:29:29 +08001571 int user_alloc)
1572{
1573 unsigned long i;
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001574 unsigned long pfn;
Marcelo Tosattif7784b82009-12-23 14:35:18 -02001575 int npages = memslot->npages;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001576 unsigned long base_gfn = memslot->base_gfn;
1577
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001578 if (base_gfn + npages > (KVM_MAX_MEM_SIZE >> PAGE_SHIFT))
1579 return -ENOMEM;
1580
Xiantao Zhangb024b792008-04-01 15:29:29 +08001581 for (i = 0; i < npages; i++) {
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001582 pfn = gfn_to_pfn(kvm, base_gfn + i);
1583 if (!kvm_is_mmio_pfn(pfn)) {
1584 kvm_set_pmt_entry(kvm, base_gfn + i,
1585 pfn << PAGE_SHIFT,
Xiantao Zhangb010eb52008-09-28 01:39:46 -07001586 _PAGE_AR_RWX | _PAGE_MA_WB);
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001587 memslot->rmap[i] = (unsigned long)pfn_to_page(pfn);
1588 } else {
1589 kvm_set_pmt_entry(kvm, base_gfn + i,
Xiantao Zhangb010eb52008-09-28 01:39:46 -07001590 GPFN_PHYS_MMIO | (pfn << PAGE_SHIFT),
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001591 _PAGE_MA_UC);
1592 memslot->rmap[i] = 0;
1593 }
Xiantao Zhangb024b792008-04-01 15:29:29 +08001594 }
1595
1596 return 0;
1597}
1598
Marcelo Tosattif7784b82009-12-23 14:35:18 -02001599void kvm_arch_commit_memory_region(struct kvm *kvm,
1600 struct kvm_userspace_memory_region *mem,
1601 struct kvm_memory_slot old,
1602 int user_alloc)
1603{
1604 return;
1605}
1606
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001607void kvm_arch_flush_shadow(struct kvm *kvm)
1608{
Xiantao Zhang64f6afb2009-04-16 17:59:16 +08001609 kvm_flush_remote_tlbs(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001610}
Xiantao Zhangb024b792008-04-01 15:29:29 +08001611
1612long kvm_arch_dev_ioctl(struct file *filp,
Jes Sorensene9a999f2008-12-18 12:17:51 +01001613 unsigned int ioctl, unsigned long arg)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001614{
1615 return -EINVAL;
1616}
1617
1618void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1619{
1620 kvm_vcpu_uninit(vcpu);
1621}
1622
1623static int vti_cpu_has_kvm_support(void)
1624{
1625 long avail = 1, status = 1, control = 1;
1626 long ret;
1627
1628 ret = ia64_pal_proc_get_features(&avail, &status, &control, 0);
1629 if (ret)
1630 goto out;
1631
1632 if (!(avail & PAL_PROC_VM_BIT))
1633 goto out;
1634
1635 printk(KERN_DEBUG"kvm: Hardware Supports VT\n");
1636
1637 ret = ia64_pal_vp_env_info(&kvm_vm_buffer_size, &vp_env_info);
1638 if (ret)
1639 goto out;
1640 printk(KERN_DEBUG"kvm: VM Buffer Size:0x%lx\n", kvm_vm_buffer_size);
1641
1642 if (!(vp_env_info & VP_OPCODE)) {
1643 printk(KERN_WARNING"kvm: No opcode ability on hardware, "
1644 "vm_env_info:0x%lx\n", vp_env_info);
1645 }
1646
1647 return 1;
1648out:
1649 return 0;
1650}
1651
Jes Sorensen0b5d7a22009-02-25 10:38:55 -06001652
1653/*
1654 * On SN2, the ITC isn't stable, so copy in fast path code to use the
1655 * SN2 RTC, replacing the ITC based default verion.
1656 */
1657static void kvm_patch_vmm(struct kvm_vmm_info *vmm_info,
1658 struct module *module)
1659{
1660 unsigned long new_ar, new_ar_sn2;
1661 unsigned long module_base;
1662
1663 if (!ia64_platform_is("sn2"))
1664 return;
1665
1666 module_base = (unsigned long)module->module_core;
1667
1668 new_ar = kvm_vmm_base + vmm_info->patch_mov_ar - module_base;
1669 new_ar_sn2 = kvm_vmm_base + vmm_info->patch_mov_ar_sn2 - module_base;
1670
1671 printk(KERN_INFO "kvm: Patching ITC emulation to use SGI SN2 RTC "
1672 "as source\n");
1673
1674 /*
1675 * Copy the SN2 version of mov_ar into place. They are both
1676 * the same size, so 6 bundles is sufficient (6 * 0x10).
1677 */
1678 memcpy((void *)new_ar, (void *)new_ar_sn2, 0x60);
1679}
1680
Xiantao Zhangb024b792008-04-01 15:29:29 +08001681static int kvm_relocate_vmm(struct kvm_vmm_info *vmm_info,
Jes Sorensen0b5d7a22009-02-25 10:38:55 -06001682 struct module *module)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001683{
1684 unsigned long module_base;
1685 unsigned long vmm_size;
1686
1687 unsigned long vmm_offset, func_offset, fdesc_offset;
1688 struct fdesc *p_fdesc;
1689
1690 BUG_ON(!module);
1691
1692 if (!kvm_vmm_base) {
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001693 printk("kvm: kvm area hasn't been initialized yet!!\n");
Xiantao Zhangb024b792008-04-01 15:29:29 +08001694 return -EFAULT;
1695 }
1696
1697 /*Calculate new position of relocated vmm module.*/
1698 module_base = (unsigned long)module->module_core;
1699 vmm_size = module->core_size;
1700 if (unlikely(vmm_size > KVM_VMM_SIZE))
1701 return -EFAULT;
1702
1703 memcpy((void *)kvm_vmm_base, (void *)module_base, vmm_size);
Jes Sorensen0b5d7a22009-02-25 10:38:55 -06001704 kvm_patch_vmm(vmm_info, module);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001705 kvm_flush_icache(kvm_vmm_base, vmm_size);
1706
1707 /*Recalculate kvm_vmm_info based on new VMM*/
1708 vmm_offset = vmm_info->vmm_ivt - module_base;
1709 kvm_vmm_info->vmm_ivt = KVM_VMM_BASE + vmm_offset;
1710 printk(KERN_DEBUG"kvm: Relocated VMM's IVT Base Addr:%lx\n",
1711 kvm_vmm_info->vmm_ivt);
1712
1713 fdesc_offset = (unsigned long)vmm_info->vmm_entry - module_base;
1714 kvm_vmm_info->vmm_entry = (kvm_vmm_entry *)(KVM_VMM_BASE +
1715 fdesc_offset);
1716 func_offset = *(unsigned long *)vmm_info->vmm_entry - module_base;
1717 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1718 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1719 p_fdesc->gp = KVM_VMM_BASE+(p_fdesc->gp - module_base);
1720
1721 printk(KERN_DEBUG"kvm: Relocated VMM's Init Entry Addr:%lx\n",
1722 KVM_VMM_BASE+func_offset);
1723
1724 fdesc_offset = (unsigned long)vmm_info->tramp_entry - module_base;
1725 kvm_vmm_info->tramp_entry = (kvm_tramp_entry *)(KVM_VMM_BASE +
1726 fdesc_offset);
1727 func_offset = *(unsigned long *)vmm_info->tramp_entry - module_base;
1728 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1729 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1730 p_fdesc->gp = KVM_VMM_BASE + (p_fdesc->gp - module_base);
1731
1732 kvm_vmm_gp = p_fdesc->gp;
1733
1734 printk(KERN_DEBUG"kvm: Relocated VMM's Entry IP:%p\n",
1735 kvm_vmm_info->vmm_entry);
1736 printk(KERN_DEBUG"kvm: Relocated VMM's Trampoline Entry IP:0x%lx\n",
1737 KVM_VMM_BASE + func_offset);
1738
1739 return 0;
1740}
1741
1742int kvm_arch_init(void *opaque)
1743{
1744 int r;
1745 struct kvm_vmm_info *vmm_info = (struct kvm_vmm_info *)opaque;
1746
1747 if (!vti_cpu_has_kvm_support()) {
1748 printk(KERN_ERR "kvm: No Hardware Virtualization Support!\n");
1749 r = -EOPNOTSUPP;
1750 goto out;
1751 }
1752
1753 if (kvm_vmm_info) {
1754 printk(KERN_ERR "kvm: Already loaded VMM module!\n");
1755 r = -EEXIST;
1756 goto out;
1757 }
1758
1759 r = -ENOMEM;
1760 kvm_vmm_info = kzalloc(sizeof(struct kvm_vmm_info), GFP_KERNEL);
1761 if (!kvm_vmm_info)
1762 goto out;
1763
1764 if (kvm_alloc_vmm_area())
1765 goto out_free0;
1766
1767 r = kvm_relocate_vmm(vmm_info, vmm_info->module);
1768 if (r)
1769 goto out_free1;
1770
1771 return 0;
1772
1773out_free1:
1774 kvm_free_vmm_area();
1775out_free0:
1776 kfree(kvm_vmm_info);
1777out:
1778 return r;
1779}
1780
1781void kvm_arch_exit(void)
1782{
1783 kvm_free_vmm_area();
1784 kfree(kvm_vmm_info);
1785 kvm_vmm_info = NULL;
1786}
1787
Takuya Yoshikawa979586e2010-06-23 15:00:29 +09001788static void kvm_ia64_sync_dirty_log(struct kvm *kvm,
1789 struct kvm_memory_slot *memslot)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001790{
Takuya Yoshikawa979586e2010-06-23 15:00:29 +09001791 int i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001792 long base;
1793 unsigned long n;
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001794 unsigned long *dirty_bitmap = (unsigned long *)(kvm->arch.vm_base +
1795 offsetof(struct kvm_vm_data, kvm_mem_dirty_log));
Xiantao Zhangb024b792008-04-01 15:29:29 +08001796
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001797 n = kvm_dirty_bitmap_bytes(memslot);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001798 base = memslot->base_gfn / BITS_PER_LONG;
1799
Takuya Yoshikawa4482b062010-06-23 14:59:29 +09001800 spin_lock(&kvm->arch.dirty_log_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001801 for (i = 0; i < n/sizeof(long); ++i) {
1802 memslot->dirty_bitmap[i] = dirty_bitmap[base + i];
1803 dirty_bitmap[base + i] = 0;
1804 }
Takuya Yoshikawa4482b062010-06-23 14:59:29 +09001805 spin_unlock(&kvm->arch.dirty_log_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001806}
1807
1808int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1809 struct kvm_dirty_log *log)
1810{
1811 int r;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001812 unsigned long n;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001813 struct kvm_memory_slot *memslot;
1814 int is_dirty = 0;
1815
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001816 mutex_lock(&kvm->slots_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001817
Takuya Yoshikawa979586e2010-06-23 15:00:29 +09001818 r = -EINVAL;
1819 if (log->slot >= KVM_MEMORY_SLOTS)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001820 goto out;
1821
Takuya Yoshikawa979586e2010-06-23 15:00:29 +09001822 memslot = &kvm->memslots->memslots[log->slot];
1823 r = -ENOENT;
1824 if (!memslot->dirty_bitmap)
1825 goto out;
1826
1827 kvm_ia64_sync_dirty_log(kvm, memslot);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001828 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1829 if (r)
1830 goto out;
1831
1832 /* If nothing is dirty, don't bother messing with page tables. */
1833 if (is_dirty) {
1834 kvm_flush_remote_tlbs(kvm);
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001835 n = kvm_dirty_bitmap_bytes(memslot);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001836 memset(memslot->dirty_bitmap, 0, n);
1837 }
1838 r = 0;
1839out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001840 mutex_unlock(&kvm->slots_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001841 return r;
1842}
1843
1844int kvm_arch_hardware_setup(void)
1845{
1846 return 0;
1847}
1848
1849void kvm_arch_hardware_unsetup(void)
1850{
1851}
1852
Xiantao Zhangb024b792008-04-01 15:29:29 +08001853void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1854{
Marcelo Tosatti32f88402009-05-07 17:55:12 -03001855 int me;
1856 int cpu = vcpu->cpu;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001857
1858 if (waitqueue_active(&vcpu->wq))
1859 wake_up_interruptible(&vcpu->wq);
1860
Marcelo Tosatti32f88402009-05-07 17:55:12 -03001861 me = get_cpu();
1862 if (cpu != me && (unsigned) cpu < nr_cpu_ids && cpu_online(cpu))
1863 if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
1864 smp_send_reschedule(cpu);
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001865 put_cpu();
Xiantao Zhangb024b792008-04-01 15:29:29 +08001866}
1867
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001868int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001869{
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001870 return __apic_accept_irq(vcpu, irq->vector);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001871}
1872
1873int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
1874{
1875 return apic->vcpu->vcpu_id == dest;
1876}
1877
1878int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
1879{
1880 return 0;
1881}
1882
Gleb Natapove1035712009-03-05 16:34:59 +02001883int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001884{
Gleb Natapove1035712009-03-05 16:34:59 +02001885 return vcpu1->arch.xtp - vcpu2->arch.xtp;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001886}
1887
Gleb Natapov343f94f2009-03-05 16:34:54 +02001888int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
1889 int short_hand, int dest, int dest_mode)
1890{
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001891 struct kvm_lapic *target = vcpu->arch.apic;
Gleb Natapov343f94f2009-03-05 16:34:54 +02001892 return (dest_mode == 0) ?
1893 kvm_apic_match_physical_addr(target, dest) :
1894 kvm_apic_match_logical_addr(target, dest);
1895}
1896
Xiantao Zhangb024b792008-04-01 15:29:29 +08001897static int find_highest_bits(int *dat)
1898{
1899 u32 bits, bitnum;
1900 int i;
1901
1902 /* loop for all 256 bits */
1903 for (i = 7; i >= 0 ; i--) {
1904 bits = dat[i];
1905 if (bits) {
1906 bitnum = fls(bits);
1907 return i * 32 + bitnum - 1;
1908 }
1909 }
1910
1911 return -1;
1912}
1913
1914int kvm_highest_pending_irq(struct kvm_vcpu *vcpu)
1915{
1916 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1917
1918 if (vpd->irr[0] & (1UL << NMI_VECTOR))
1919 return NMI_VECTOR;
1920 if (vpd->irr[0] & (1UL << ExtINT_VECTOR))
1921 return ExtINT_VECTOR;
1922
1923 return find_highest_bits((int *)&vpd->irr[0]);
1924}
1925
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001926int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1927{
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001928 return vcpu->arch.timer_fired;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001929}
1930
Xiantao Zhangb024b792008-04-01 15:29:29 +08001931int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1932{
Gleb Natapova1b37102009-07-09 15:33:52 +03001933 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE) ||
1934 (kvm_highest_pending_irq(vcpu) != -1);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001935}
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001936
1937int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1938 struct kvm_mp_state *mp_state)
1939{
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001940 mp_state->mp_state = vcpu->arch.mp_state;
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001941 return 0;
1942}
1943
1944static int vcpu_reset(struct kvm_vcpu *vcpu)
1945{
1946 int r;
1947 long psr;
1948 local_irq_save(psr);
1949 r = kvm_insert_vmm_mapping(vcpu);
Jes Sorensen43890ae2009-04-17 16:43:27 +02001950 local_irq_restore(psr);
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001951 if (r)
1952 goto fail;
1953
1954 vcpu->arch.launched = 0;
1955 kvm_arch_vcpu_uninit(vcpu);
1956 r = kvm_arch_vcpu_init(vcpu);
1957 if (r)
1958 goto fail;
1959
1960 kvm_purge_vmm_mapping(vcpu);
1961 r = 0;
1962fail:
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001963 return r;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001964}
1965
1966int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1967 struct kvm_mp_state *mp_state)
1968{
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001969 int r = 0;
1970
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001971 vcpu->arch.mp_state = mp_state->mp_state;
1972 if (vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)
1973 r = vcpu_reset(vcpu);
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001974 return r;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001975}