blob: 6a4309bb821af9882d5169eeacee8638e5376bbb [file] [log] [blame]
Xiantao Zhangb024b792008-04-01 15:29:29 +08001/*
Masanari Iidad7558142012-08-22 18:40:21 +09002 * kvm_ia64.c: Basic KVM support On Itanium series processors
Xiantao Zhangb024b792008-04-01 15:29:29 +08003 *
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>
Joerg Roedela1b60c12011-09-06 18:46:34 +020036#include <linux/pci.h>
Xiantao Zhangb024b792008-04-01 15:29:29 +080037
38#include <asm/pgtable.h>
39#include <asm/gcc_intrin.h>
40#include <asm/pal.h>
41#include <asm/cacheflush.h>
42#include <asm/div64.h>
43#include <asm/tlb.h>
Jes Sorensen9f726322008-09-12 14:12:08 +020044#include <asm/elf.h>
Jes Sorensen0c72ea72009-02-25 10:38:52 -060045#include <asm/sn/addrs.h>
46#include <asm/sn/clksupport.h>
47#include <asm/sn/shub_mmr.h>
Xiantao Zhangb024b792008-04-01 15:29:29 +080048
49#include "misc.h"
50#include "vti.h"
51#include "iodev.h"
52#include "ioapic.h"
53#include "lapic.h"
Xiantao Zhang2f749772008-09-27 11:46:36 +080054#include "irq.h"
Xiantao Zhangb024b792008-04-01 15:29:29 +080055
56static unsigned long kvm_vmm_base;
57static unsigned long kvm_vsa_base;
58static unsigned long kvm_vm_buffer;
59static unsigned long kvm_vm_buffer_size;
60unsigned long kvm_vmm_gp;
61
62static long vp_env_info;
63
64static struct kvm_vmm_info *kvm_vmm_info;
65
66static DEFINE_PER_CPU(struct kvm_vcpu *, last_vcpu);
67
68struct kvm_stats_debugfs_item debugfs_entries[] = {
69 { NULL }
70};
71
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -060072static unsigned long kvm_get_itc(struct kvm_vcpu *vcpu)
73{
74#if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
75 if (vcpu->kvm->arch.is_sn2)
76 return rtc_time();
77 else
78#endif
79 return ia64_getreg(_IA64_REG_AR_ITC);
80}
81
Xiantao Zhangb024b792008-04-01 15:29:29 +080082static void kvm_flush_icache(unsigned long start, unsigned long len)
83{
84 int l;
85
86 for (l = 0; l < (len + 32); l += 32)
Isaku Yamahata71205692009-03-27 15:11:57 +090087 ia64_fc((void *)(start + l));
Xiantao Zhangb024b792008-04-01 15:29:29 +080088
89 ia64_sync_i();
90 ia64_srlz_i();
91}
92
93static void kvm_flush_tlb_all(void)
94{
95 unsigned long i, j, count0, count1, stride0, stride1, addr;
96 long flags;
97
98 addr = local_cpu_data->ptce_base;
99 count0 = local_cpu_data->ptce_count[0];
100 count1 = local_cpu_data->ptce_count[1];
101 stride0 = local_cpu_data->ptce_stride[0];
102 stride1 = local_cpu_data->ptce_stride[1];
103
104 local_irq_save(flags);
105 for (i = 0; i < count0; ++i) {
106 for (j = 0; j < count1; ++j) {
107 ia64_ptce(addr);
108 addr += stride1;
109 }
110 addr += stride0;
111 }
112 local_irq_restore(flags);
113 ia64_srlz_i(); /* srlz.i implies srlz.d */
114}
115
116long ia64_pal_vp_create(u64 *vpd, u64 *host_iva, u64 *opt_handler)
117{
118 struct ia64_pal_retval iprv;
119
120 PAL_CALL_STK(iprv, PAL_VP_CREATE, (u64)vpd, (u64)host_iva,
121 (u64)opt_handler);
122
123 return iprv.status;
124}
125
126static DEFINE_SPINLOCK(vp_lock);
127
Alexander Graf10474ae2009-09-15 11:37:46 +0200128int kvm_arch_hardware_enable(void *garbage)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800129{
130 long status;
131 long tmp_base;
132 unsigned long pte;
133 unsigned long saved_psr;
134 int slot;
135
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600136 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
Xiantao Zhangb024b792008-04-01 15:29:29 +0800137 local_irq_save(saved_psr);
138 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
Julia Lawallcab7a1e2008-07-22 21:38:18 +0200139 local_irq_restore(saved_psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800140 if (slot < 0)
Alexander Graf10474ae2009-09-15 11:37:46 +0200141 return -EINVAL;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800142
143 spin_lock(&vp_lock);
144 status = ia64_pal_vp_init_env(kvm_vsa_base ?
145 VP_INIT_ENV : VP_INIT_ENV_INITALIZE,
146 __pa(kvm_vm_buffer), KVM_VM_BUFFER_BASE, &tmp_base);
147 if (status != 0) {
Julia Lawall3499f4d2010-05-26 17:57:05 +0200148 spin_unlock(&vp_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800149 printk(KERN_WARNING"kvm: Failed to Enable VT Support!!!!\n");
Alexander Graf10474ae2009-09-15 11:37:46 +0200150 return -EINVAL;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800151 }
152
153 if (!kvm_vsa_base) {
154 kvm_vsa_base = tmp_base;
155 printk(KERN_INFO"kvm: kvm_vsa_base:0x%lx\n", kvm_vsa_base);
156 }
157 spin_unlock(&vp_lock);
158 ia64_ptr_entry(0x3, slot);
Alexander Graf10474ae2009-09-15 11:37:46 +0200159
160 return 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800161}
162
163void kvm_arch_hardware_disable(void *garbage)
164{
165
166 long status;
167 int slot;
168 unsigned long pte;
169 unsigned long saved_psr;
170 unsigned long host_iva = ia64_getreg(_IA64_REG_CR_IVA);
171
172 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base),
173 PAGE_KERNEL));
174
175 local_irq_save(saved_psr);
176 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
Julia Lawallcab7a1e2008-07-22 21:38:18 +0200177 local_irq_restore(saved_psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800178 if (slot < 0)
179 return;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800180
181 status = ia64_pal_vp_exit_env(host_iva);
182 if (status)
183 printk(KERN_DEBUG"kvm: Failed to disable VT support! :%ld\n",
184 status);
185 ia64_ptr_entry(0x3, slot);
186}
187
188void kvm_arch_check_processor_compat(void *rtn)
189{
190 *(int *)rtn = 0;
191}
192
193int kvm_dev_ioctl_check_extension(long ext)
194{
195
196 int r;
197
198 switch (ext) {
199 case KVM_CAP_IRQCHIP:
Xiantao Zhang8c4b5372008-08-28 09:34:08 +0800200 case KVM_CAP_MP_STATE:
Gleb Natapov49256632009-02-04 17:28:14 +0200201 case KVM_CAP_IRQ_INJECT_STATUS:
Gabriel L. Somlo100943c2014-02-27 23:06:17 -0500202 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800203 r = 1;
204 break;
Laurent Vivier7f39f8a2008-05-30 16:05:57 +0200205 case KVM_CAP_COALESCED_MMIO:
206 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
207 break;
Alex Williamson4cee4b72013-04-29 10:54:08 -0600208#ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800209 case KVM_CAP_IOMMU:
Joerg Roedela1b60c12011-09-06 18:46:34 +0200210 r = iommu_present(&pci_bus_type);
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800211 break;
Alex Williamson4cee4b72013-04-29 10:54:08 -0600212#endif
Xiantao Zhangb024b792008-04-01 15:29:29 +0800213 default:
214 r = 0;
215 }
216 return r;
217
218}
219
Xiantao Zhangb024b792008-04-01 15:29:29 +0800220static int handle_vm_error(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
221{
222 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
223 kvm_run->hw.hardware_exit_reason = 1;
224 return 0;
225}
226
227static int handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
228{
229 struct kvm_mmio_req *p;
230 struct kvm_io_device *mmio_dev;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300231 int r;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800232
233 p = kvm_get_vcpu_ioreq(vcpu);
234
235 if ((p->addr & PAGE_MASK) == IOAPIC_DEFAULT_BASE_ADDRESS)
236 goto mmio;
237 vcpu->mmio_needed = 1;
Avi Kivityf78146b2012-04-18 19:22:47 +0300238 vcpu->mmio_fragments[0].gpa = kvm_run->mmio.phys_addr = p->addr;
239 vcpu->mmio_fragments[0].len = kvm_run->mmio.len = p->size;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800240 vcpu->mmio_is_write = kvm_run->mmio.is_write = !p->dir;
241
242 if (vcpu->mmio_is_write)
Avi Kivityf78146b2012-04-18 19:22:47 +0300243 memcpy(vcpu->arch.mmio_data, &p->data, p->size);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800244 memcpy(kvm_run->mmio.data, &p->data, p->size);
245 kvm_run->exit_reason = KVM_EXIT_MMIO;
246 return 0;
247mmio:
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300248 if (p->dir)
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200249 r = kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, p->addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300250 p->size, &p->data);
251 else
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200252 r = kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, p->addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300253 p->size, &p->data);
254 if (r)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800255 printk(KERN_ERR"kvm: No iodevice found! addr:%lx\n", p->addr);
256 p->state = STATE_IORESP_READY;
257
258 return 1;
259}
260
261static int handle_pal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
262{
263 struct exit_ctl_data *p;
264
265 p = kvm_get_exit_data(vcpu);
266
267 if (p->exit_reason == EXIT_REASON_PAL_CALL)
268 return kvm_pal_emul(vcpu, kvm_run);
269 else {
270 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
271 kvm_run->hw.hardware_exit_reason = 2;
272 return 0;
273 }
274}
275
276static int handle_sal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
277{
278 struct exit_ctl_data *p;
279
280 p = kvm_get_exit_data(vcpu);
281
282 if (p->exit_reason == EXIT_REASON_SAL_CALL) {
283 kvm_sal_emul(vcpu);
284 return 1;
285 } else {
286 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
287 kvm_run->hw.hardware_exit_reason = 3;
288 return 0;
289 }
290
291}
292
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200293static int __apic_accept_irq(struct kvm_vcpu *vcpu, uint64_t vector)
294{
295 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
296
297 if (!test_and_set_bit(vector, &vpd->irr[0])) {
298 vcpu->arch.irq_new_pending = 1;
299 kvm_vcpu_kick(vcpu);
300 return 1;
301 }
302 return 0;
303}
304
Xiantao Zhangb024b792008-04-01 15:29:29 +0800305/*
306 * offset: address offset to IPI space.
307 * value: deliver value.
308 */
309static void vcpu_deliver_ipi(struct kvm_vcpu *vcpu, uint64_t dm,
310 uint64_t vector)
311{
312 switch (dm) {
313 case SAPIC_FIXED:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800314 break;
315 case SAPIC_NMI:
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200316 vector = 2;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800317 break;
318 case SAPIC_EXTINT:
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200319 vector = 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800320 break;
321 case SAPIC_INIT:
322 case SAPIC_PMI:
323 default:
324 printk(KERN_ERR"kvm: Unimplemented Deliver reserved IPI!\n");
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200325 return;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800326 }
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200327 __apic_accept_irq(vcpu, vector);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800328}
329
330static struct kvm_vcpu *lid_to_vcpu(struct kvm *kvm, unsigned long id,
331 unsigned long eid)
332{
333 union ia64_lid lid;
334 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300335 struct kvm_vcpu *vcpu;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800336
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300337 kvm_for_each_vcpu(i, vcpu, kvm) {
338 lid.val = VCPU_LID(vcpu);
339 if (lid.id == id && lid.eid == eid)
340 return vcpu;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800341 }
342
343 return NULL;
344}
345
346static int handle_ipi(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
347{
348 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
349 struct kvm_vcpu *target_vcpu;
350 struct kvm_pt_regs *regs;
351 union ia64_ipi_a addr = p->u.ipi_data.addr;
352 union ia64_ipi_d data = p->u.ipi_data.data;
353
354 target_vcpu = lid_to_vcpu(vcpu->kvm, addr.id, addr.eid);
355 if (!target_vcpu)
356 return handle_vm_error(vcpu, kvm_run);
357
358 if (!target_vcpu->arch.launched) {
359 regs = vcpu_regs(target_vcpu);
360
361 regs->cr_iip = vcpu->kvm->arch.rdv_sal_data.boot_ip;
362 regs->r1 = vcpu->kvm->arch.rdv_sal_data.boot_gp;
363
Avi Kivitya4535292008-04-13 17:54:35 +0300364 target_vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800365 if (waitqueue_active(&target_vcpu->wq))
366 wake_up_interruptible(&target_vcpu->wq);
367 } else {
368 vcpu_deliver_ipi(target_vcpu, data.dm, data.vector);
369 if (target_vcpu != vcpu)
370 kvm_vcpu_kick(target_vcpu);
371 }
372
373 return 1;
374}
375
376struct call_data {
377 struct kvm_ptc_g ptc_g_data;
378 struct kvm_vcpu *vcpu;
379};
380
381static void vcpu_global_purge(void *info)
382{
383 struct call_data *p = (struct call_data *)info;
384 struct kvm_vcpu *vcpu = p->vcpu;
385
386 if (test_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
387 return;
388
389 set_bit(KVM_REQ_PTC_G, &vcpu->requests);
390 if (vcpu->arch.ptc_g_count < MAX_PTC_G_NUM) {
391 vcpu->arch.ptc_g_data[vcpu->arch.ptc_g_count++] =
392 p->ptc_g_data;
393 } else {
394 clear_bit(KVM_REQ_PTC_G, &vcpu->requests);
395 vcpu->arch.ptc_g_count = 0;
396 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
397 }
398}
399
400static int handle_global_purge(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
401{
402 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
403 struct kvm *kvm = vcpu->kvm;
404 struct call_data call_data;
405 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300406 struct kvm_vcpu *vcpui;
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800407
Xiantao Zhangb024b792008-04-01 15:29:29 +0800408 call_data.ptc_g_data = p->u.ptc_g_data;
409
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300410 kvm_for_each_vcpu(i, vcpui, kvm) {
411 if (vcpui->arch.mp_state == KVM_MP_STATE_UNINITIALIZED ||
412 vcpu == vcpui)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800413 continue;
414
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300415 if (waitqueue_active(&vcpui->wq))
416 wake_up_interruptible(&vcpui->wq);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800417
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300418 if (vcpui->cpu != -1) {
419 call_data.vcpu = vcpui;
420 smp_call_function_single(vcpui->cpu,
Takashi Iwai2f73cca2008-07-17 18:09:12 +0200421 vcpu_global_purge, &call_data, 1);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800422 } else
423 printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n");
424
425 }
426 return 1;
427}
428
429static int handle_switch_rr6(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
430{
431 return 1;
432}
433
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600434static int kvm_sn2_setup_mappings(struct kvm_vcpu *vcpu)
435{
436 unsigned long pte, rtc_phys_addr, map_addr;
437 int slot;
438
439 map_addr = KVM_VMM_BASE + (1UL << KVM_VMM_SHIFT);
440 rtc_phys_addr = LOCAL_MMR_OFFSET | SH_RTC;
441 pte = pte_val(mk_pte_phys(rtc_phys_addr, PAGE_KERNEL_UC));
442 slot = ia64_itr_entry(0x3, map_addr, pte, PAGE_SHIFT);
443 vcpu->arch.sn_rtc_tr_slot = slot;
444 if (slot < 0) {
445 printk(KERN_ERR "Mayday mayday! RTC mapping failed!\n");
446 slot = 0;
447 }
448 return slot;
449}
450
Xiantao Zhangb024b792008-04-01 15:29:29 +0800451int kvm_emulate_halt(struct kvm_vcpu *vcpu)
452{
453
454 ktime_t kt;
455 long itc_diff;
456 unsigned long vcpu_now_itc;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800457 unsigned long expires;
458 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
459 unsigned long cyc_per_usec = local_cpu_data->cyc_per_usec;
460 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
461
Xiantao Zhangb024b792008-04-01 15:29:29 +0800462 if (irqchip_in_kernel(vcpu->kvm)) {
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800463
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -0600464 vcpu_now_itc = kvm_get_itc(vcpu) + vcpu->arch.itc_offset;
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800465
466 if (time_after(vcpu_now_itc, vpd->itm)) {
467 vcpu->arch.timer_check = 1;
468 return 1;
469 }
470 itc_diff = vpd->itm - vcpu_now_itc;
471 if (itc_diff < 0)
472 itc_diff = -itc_diff;
473
474 expires = div64_u64(itc_diff, cyc_per_usec);
475 kt = ktime_set(0, 1000 * expires);
476
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800477 vcpu->arch.ht_active = 1;
478 hrtimer_start(p_ht, kt, HRTIMER_MODE_ABS);
479
Avi Kivitya4535292008-04-13 17:54:35 +0300480 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800481 kvm_vcpu_block(vcpu);
482 hrtimer_cancel(p_ht);
483 vcpu->arch.ht_active = 0;
484
Gleb Natapov09cec752009-03-23 15:11:44 +0200485 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests) ||
486 kvm_cpu_has_pending_timer(vcpu))
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800487 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
Gleb Natapov09cec752009-03-23 15:11:44 +0200488 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800489
Avi Kivitya4535292008-04-13 17:54:35 +0300490 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800491 return -EINTR;
492 return 1;
493 } else {
494 printk(KERN_ERR"kvm: Unsupported userspace halt!");
495 return 0;
496 }
497}
498
499static int handle_vm_shutdown(struct kvm_vcpu *vcpu,
500 struct kvm_run *kvm_run)
501{
502 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
503 return 0;
504}
505
506static int handle_external_interrupt(struct kvm_vcpu *vcpu,
507 struct kvm_run *kvm_run)
508{
509 return 1;
510}
511
Xiantao Zhang7d637972008-11-21 20:58:11 +0800512static int handle_vcpu_debug(struct kvm_vcpu *vcpu,
513 struct kvm_run *kvm_run)
514{
515 printk("VMM: %s", vcpu->arch.log_buf);
516 return 1;
517}
518
Xiantao Zhangb024b792008-04-01 15:29:29 +0800519static int (*kvm_vti_exit_handlers[])(struct kvm_vcpu *vcpu,
520 struct kvm_run *kvm_run) = {
521 [EXIT_REASON_VM_PANIC] = handle_vm_error,
522 [EXIT_REASON_MMIO_INSTRUCTION] = handle_mmio,
523 [EXIT_REASON_PAL_CALL] = handle_pal_call,
524 [EXIT_REASON_SAL_CALL] = handle_sal_call,
525 [EXIT_REASON_SWITCH_RR6] = handle_switch_rr6,
526 [EXIT_REASON_VM_DESTROY] = handle_vm_shutdown,
527 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
528 [EXIT_REASON_IPI] = handle_ipi,
529 [EXIT_REASON_PTC_G] = handle_global_purge,
Xiantao Zhang7d637972008-11-21 20:58:11 +0800530 [EXIT_REASON_DEBUG] = handle_vcpu_debug,
Xiantao Zhangb024b792008-04-01 15:29:29 +0800531
532};
533
534static const int kvm_vti_max_exit_handlers =
535 sizeof(kvm_vti_exit_handlers)/sizeof(*kvm_vti_exit_handlers);
536
Xiantao Zhangb024b792008-04-01 15:29:29 +0800537static uint32_t kvm_get_exit_reason(struct kvm_vcpu *vcpu)
538{
539 struct exit_ctl_data *p_exit_data;
540
541 p_exit_data = kvm_get_exit_data(vcpu);
542 return p_exit_data->exit_reason;
543}
544
545/*
546 * The guest has exited. See if we can fix it or if we need userspace
547 * assistance.
548 */
549static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
550{
551 u32 exit_reason = kvm_get_exit_reason(vcpu);
552 vcpu->arch.last_exit = exit_reason;
553
554 if (exit_reason < kvm_vti_max_exit_handlers
555 && kvm_vti_exit_handlers[exit_reason])
556 return kvm_vti_exit_handlers[exit_reason](vcpu, kvm_run);
557 else {
558 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
559 kvm_run->hw.hardware_exit_reason = exit_reason;
560 }
561 return 0;
562}
563
564static inline void vti_set_rr6(unsigned long rr6)
565{
566 ia64_set_rr(RR6, rr6);
567 ia64_srlz_i();
568}
569
570static int kvm_insert_vmm_mapping(struct kvm_vcpu *vcpu)
571{
572 unsigned long pte;
573 struct kvm *kvm = vcpu->kvm;
574 int r;
575
576 /*Insert a pair of tr to map vmm*/
577 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
578 r = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
579 if (r < 0)
580 goto out;
581 vcpu->arch.vmm_tr_slot = r;
582 /*Insert a pairt of tr to map data of vm*/
583 pte = pte_val(mk_pte_phys(__pa(kvm->arch.vm_base), PAGE_KERNEL));
584 r = ia64_itr_entry(0x3, KVM_VM_DATA_BASE,
585 pte, KVM_VM_DATA_SHIFT);
586 if (r < 0)
587 goto out;
588 vcpu->arch.vm_tr_slot = r;
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600589
590#if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
591 if (kvm->arch.is_sn2) {
592 r = kvm_sn2_setup_mappings(vcpu);
593 if (r < 0)
594 goto out;
595 }
596#endif
597
Xiantao Zhangb024b792008-04-01 15:29:29 +0800598 r = 0;
599out:
600 return r;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800601}
602
603static void kvm_purge_vmm_mapping(struct kvm_vcpu *vcpu)
604{
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600605 struct kvm *kvm = vcpu->kvm;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800606 ia64_ptr_entry(0x3, vcpu->arch.vmm_tr_slot);
607 ia64_ptr_entry(0x3, vcpu->arch.vm_tr_slot);
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600608#if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
609 if (kvm->arch.is_sn2)
610 ia64_ptr_entry(0x3, vcpu->arch.sn_rtc_tr_slot);
611#endif
Xiantao Zhangb024b792008-04-01 15:29:29 +0800612}
613
614static int kvm_vcpu_pre_transition(struct kvm_vcpu *vcpu)
615{
Jes Sorensen4d13c3b2009-04-16 16:53:13 +0200616 unsigned long psr;
617 int r;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800618 int cpu = smp_processor_id();
619
620 if (vcpu->arch.last_run_cpu != cpu ||
621 per_cpu(last_vcpu, cpu) != vcpu) {
622 per_cpu(last_vcpu, cpu) = vcpu;
623 vcpu->arch.last_run_cpu = cpu;
624 kvm_flush_tlb_all();
625 }
626
627 vcpu->arch.host_rr6 = ia64_get_rr(RR6);
628 vti_set_rr6(vcpu->arch.vmm_rr);
Jes Sorensen4d13c3b2009-04-16 16:53:13 +0200629 local_irq_save(psr);
630 r = kvm_insert_vmm_mapping(vcpu);
631 local_irq_restore(psr);
632 return r;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800633}
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200634
Xiantao Zhangb024b792008-04-01 15:29:29 +0800635static void kvm_vcpu_post_transition(struct kvm_vcpu *vcpu)
636{
637 kvm_purge_vmm_mapping(vcpu);
638 vti_set_rr6(vcpu->arch.host_rr6);
639}
640
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200641static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800642{
643 union context *host_ctx, *guest_ctx;
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200644 int r, idx;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800645
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200646 idx = srcu_read_lock(&vcpu->kvm->srcu);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800647
648again:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800649 if (signal_pending(current)) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800650 r = -EINTR;
651 kvm_run->exit_reason = KVM_EXIT_INTR;
652 goto out;
653 }
654
Jes Sorensend24d2c12009-04-09 16:38:14 +0200655 preempt_disable();
656 local_irq_disable();
657
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200658 /*Get host and guest context with guest address space.*/
659 host_ctx = kvm_get_host_context(vcpu);
660 guest_ctx = kvm_get_guest_context(vcpu);
661
Marcelo Tosatti32f88402009-05-07 17:55:12 -0300662 clear_bit(KVM_REQ_KICK, &vcpu->requests);
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200663
664 r = kvm_vcpu_pre_transition(vcpu);
665 if (r < 0)
666 goto vcpu_run_fail;
667
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200668 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +0800669 vcpu->mode = IN_GUEST_MODE;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800670 kvm_guest_enter();
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200671
672 /*
673 * Transition to the guest
674 */
675 kvm_vmm_info->tramp_entry(host_ctx, guest_ctx);
676
677 kvm_vcpu_post_transition(vcpu);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800678
679 vcpu->arch.launched = 1;
Marcelo Tosatti32f88402009-05-07 17:55:12 -0300680 set_bit(KVM_REQ_KICK, &vcpu->requests);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800681 local_irq_enable();
682
683 /*
684 * We must have an instruction between local_irq_enable() and
685 * kvm_guest_exit(), so the timer interrupt isn't delayed by
686 * the interrupt shadow. The stat.exits increment will do nicely.
687 * But we need to prevent reordering, hence this barrier():
688 */
689 barrier();
Xiantao Zhangb024b792008-04-01 15:29:29 +0800690 kvm_guest_exit();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +0800691 vcpu->mode = OUTSIDE_GUEST_MODE;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800692 preempt_enable();
693
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200694 idx = srcu_read_lock(&vcpu->kvm->srcu);
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200695
Xiantao Zhangb024b792008-04-01 15:29:29 +0800696 r = kvm_handle_exit(kvm_run, vcpu);
697
698 if (r > 0) {
699 if (!need_resched())
700 goto again;
701 }
702
703out:
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200704 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800705 if (r > 0) {
Takuya Yoshikawac08ac062013-12-13 15:07:21 +0900706 cond_resched();
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200707 idx = srcu_read_lock(&vcpu->kvm->srcu);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800708 goto again;
709 }
710
711 return r;
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200712
713vcpu_run_fail:
714 local_irq_enable();
715 preempt_enable();
716 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
717 goto out;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800718}
719
720static void kvm_set_mmio_data(struct kvm_vcpu *vcpu)
721{
722 struct kvm_mmio_req *p = kvm_get_vcpu_ioreq(vcpu);
723
724 if (!vcpu->mmio_is_write)
Avi Kivityf78146b2012-04-18 19:22:47 +0300725 memcpy(&p->data, vcpu->arch.mmio_data, 8);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800726 p->state = STATE_IORESP_READY;
727}
728
729int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
730{
731 int r;
732 sigset_t sigsaved;
733
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800734 if (vcpu->sigset_active)
735 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
736
Avi Kivitya4535292008-04-13 17:54:35 +0300737 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800738 kvm_vcpu_block(vcpu);
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800739 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800740 r = -EAGAIN;
741 goto out;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800742 }
743
Xiantao Zhangb024b792008-04-01 15:29:29 +0800744 if (vcpu->mmio_needed) {
Avi Kivityf78146b2012-04-18 19:22:47 +0300745 memcpy(vcpu->arch.mmio_data, kvm_run->mmio.data, 8);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800746 kvm_set_mmio_data(vcpu);
747 vcpu->mmio_read_completed = 1;
748 vcpu->mmio_needed = 0;
749 }
750 r = __vcpu_run(vcpu, kvm_run);
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800751out:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800752 if (vcpu->sigset_active)
753 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
754
Xiantao Zhangb024b792008-04-01 15:29:29 +0800755 return r;
756}
757
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100758struct kvm *kvm_arch_alloc_vm(void)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800759{
760
761 struct kvm *kvm;
762 uint64_t vm_base;
763
Xiantao Zhanga917f7af32008-10-23 14:56:44 +0800764 BUG_ON(sizeof(struct kvm) > KVM_VM_STRUCT_SIZE);
765
Xiantao Zhangb024b792008-04-01 15:29:29 +0800766 vm_base = __get_free_pages(GFP_KERNEL, get_order(KVM_VM_DATA_SIZE));
767
768 if (!vm_base)
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100769 return NULL;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800770
Xiantao Zhangb024b792008-04-01 15:29:29 +0800771 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
Xiantao Zhanga917f7af32008-10-23 14:56:44 +0800772 kvm = (struct kvm *)(vm_base +
773 offsetof(struct kvm_vm_data, kvm_vm_struct));
Xiantao Zhangb024b792008-04-01 15:29:29 +0800774 kvm->arch.vm_base = vm_base;
Xiantao Zhanga917f7af32008-10-23 14:56:44 +0800775 printk(KERN_DEBUG"kvm: vm's data area:0x%lx\n", vm_base);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800776
777 return kvm;
778}
779
Xiao Guangrong60f9a9e2011-11-24 18:09:11 +0800780struct kvm_ia64_io_range {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800781 unsigned long start;
782 unsigned long size;
783 unsigned long type;
784};
785
Xiao Guangrong60f9a9e2011-11-24 18:09:11 +0800786static const struct kvm_ia64_io_range io_ranges[] = {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800787 {VGA_IO_START, VGA_IO_SIZE, GPFN_FRAME_BUFFER},
788 {MMIO_START, MMIO_SIZE, GPFN_LOW_MMIO},
789 {LEGACY_IO_START, LEGACY_IO_SIZE, GPFN_LEGACY_IO},
790 {IO_SAPIC_START, IO_SAPIC_SIZE, GPFN_IOSAPIC},
791 {PIB_START, PIB_SIZE, GPFN_PIB},
792};
793
794static void kvm_build_io_pmt(struct kvm *kvm)
795{
796 unsigned long i, j;
797
798 /* Mark I/O ranges */
799 for (i = 0; i < (sizeof(io_ranges) / sizeof(struct kvm_io_range));
800 i++) {
801 for (j = io_ranges[i].start;
802 j < io_ranges[i].start + io_ranges[i].size;
803 j += PAGE_SIZE)
804 kvm_set_pmt_entry(kvm, j >> PAGE_SHIFT,
805 io_ranges[i].type, 0);
806 }
807
808}
809
810/*Use unused rids to virtualize guest rid.*/
811#define GUEST_PHYSICAL_RR0 0x1739
812#define GUEST_PHYSICAL_RR4 0x2739
813#define VMM_INIT_RR 0x1660
814
Carsten Ottee08b9632012-01-04 10:25:20 +0100815int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800816{
Xiantao Zhangb024b792008-04-01 15:29:29 +0800817 BUG_ON(!kvm);
818
Carsten Ottee08b9632012-01-04 10:25:20 +0100819 if (type)
820 return -EINVAL;
821
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100822 kvm->arch.is_sn2 = ia64_platform_is("sn2");
823
Xiantao Zhangb024b792008-04-01 15:29:29 +0800824 kvm->arch.metaphysical_rr0 = GUEST_PHYSICAL_RR0;
825 kvm->arch.metaphysical_rr4 = GUEST_PHYSICAL_RR4;
826 kvm->arch.vmm_init_rr = VMM_INIT_RR;
827
Xiantao Zhangb024b792008-04-01 15:29:29 +0800828 /*
829 *Fill P2M entries for MMIO/IO ranges
830 */
831 kvm_build_io_pmt(kvm);
832
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800833 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Sheng Yang5550af42008-10-15 20:15:06 +0800834
835 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
836 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800837
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100838 return 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800839}
840
841static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm,
842 struct kvm_irqchip *chip)
843{
844 int r;
845
846 r = 0;
847 switch (chip->chip_id) {
848 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +0300849 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800850 break;
851 default:
852 r = -EINVAL;
853 break;
854 }
855 return r;
856}
857
858static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
859{
860 int r;
861
862 r = 0;
863 switch (chip->chip_id) {
864 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +0300865 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800866 break;
867 default:
868 r = -EINVAL;
869 break;
870 }
871 return r;
872}
873
874#define RESTORE_REGS(_x) vcpu->arch._x = regs->_x
875
876int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
877{
Xiantao Zhangb024b792008-04-01 15:29:29 +0800878 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
Jes Sorensen042b26e2008-12-16 16:45:47 +0100879 int i;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800880
Xiantao Zhangb024b792008-04-01 15:29:29 +0800881 for (i = 0; i < 16; i++) {
882 vpd->vgr[i] = regs->vpd.vgr[i];
883 vpd->vbgr[i] = regs->vpd.vbgr[i];
884 }
885 for (i = 0; i < 128; i++)
886 vpd->vcr[i] = regs->vpd.vcr[i];
887 vpd->vhpi = regs->vpd.vhpi;
888 vpd->vnat = regs->vpd.vnat;
889 vpd->vbnat = regs->vpd.vbnat;
890 vpd->vpsr = regs->vpd.vpsr;
891
892 vpd->vpr = regs->vpd.vpr;
893
Jes Sorensen042b26e2008-12-16 16:45:47 +0100894 memcpy(&vcpu->arch.guest, &regs->saved_guest, sizeof(union context));
Xiantao Zhangb024b792008-04-01 15:29:29 +0800895
896 RESTORE_REGS(mp_state);
897 RESTORE_REGS(vmm_rr);
898 memcpy(vcpu->arch.itrs, regs->itrs, sizeof(struct thash_data) * NITRS);
899 memcpy(vcpu->arch.dtrs, regs->dtrs, sizeof(struct thash_data) * NDTRS);
900 RESTORE_REGS(itr_regions);
901 RESTORE_REGS(dtr_regions);
902 RESTORE_REGS(tc_regions);
903 RESTORE_REGS(irq_check);
904 RESTORE_REGS(itc_check);
905 RESTORE_REGS(timer_check);
906 RESTORE_REGS(timer_pending);
907 RESTORE_REGS(last_itc);
908 for (i = 0; i < 8; i++) {
909 vcpu->arch.vrr[i] = regs->vrr[i];
910 vcpu->arch.ibr[i] = regs->ibr[i];
911 vcpu->arch.dbr[i] = regs->dbr[i];
912 }
913 for (i = 0; i < 4; i++)
914 vcpu->arch.insvc[i] = regs->insvc[i];
915 RESTORE_REGS(xtp);
916 RESTORE_REGS(metaphysical_rr0);
917 RESTORE_REGS(metaphysical_rr4);
918 RESTORE_REGS(metaphysical_saved_rr0);
919 RESTORE_REGS(metaphysical_saved_rr4);
920 RESTORE_REGS(fp_psr);
921 RESTORE_REGS(saved_gp);
922
923 vcpu->arch.irq_new_pending = 1;
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -0600924 vcpu->arch.itc_offset = regs->saved_itc - kvm_get_itc(vcpu);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800925 set_bit(KVM_REQ_RESUME, &vcpu->requests);
926
Jes Sorensen042b26e2008-12-16 16:45:47 +0100927 return 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800928}
929
Yang Zhange6062642013-04-17 08:46:41 +0800930int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
931 bool line_status)
Christoffer Dall23d43cf2012-07-24 08:51:20 -0400932{
933 if (!irqchip_in_kernel(kvm))
934 return -ENXIO;
935
936 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Yang Zhange6062642013-04-17 08:46:41 +0800937 irq_event->irq, irq_event->level,
938 line_status);
Christoffer Dall23d43cf2012-07-24 08:51:20 -0400939 return 0;
940}
941
Xiantao Zhangb024b792008-04-01 15:29:29 +0800942long kvm_arch_vm_ioctl(struct file *filp,
943 unsigned int ioctl, unsigned long arg)
944{
945 struct kvm *kvm = filp->private_data;
946 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +0300947 int r = -ENOTTY;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800948
949 switch (ioctl) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800950 case KVM_CREATE_IRQCHIP:
951 r = -EFAULT;
952 r = kvm_ioapic_init(kvm);
953 if (r)
954 goto out;
Avi Kivity399ec802008-11-19 13:58:46 +0200955 r = kvm_setup_default_irq_routing(kvm);
956 if (r) {
Takuya Yoshikawa175504c2010-12-16 01:41:37 +0900957 mutex_lock(&kvm->slots_lock);
Wei Yongjun4b7bb922010-02-09 10:41:56 +0800958 kvm_ioapic_destroy(kvm);
Takuya Yoshikawa175504c2010-12-16 01:41:37 +0900959 mutex_unlock(&kvm->slots_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200960 goto out;
961 }
Xiantao Zhangb024b792008-04-01 15:29:29 +0800962 break;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800963 case KVM_GET_IRQCHIP: {
964 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
965 struct kvm_irqchip chip;
966
967 r = -EFAULT;
968 if (copy_from_user(&chip, argp, sizeof chip))
969 goto out;
970 r = -ENXIO;
971 if (!irqchip_in_kernel(kvm))
972 goto out;
973 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
974 if (r)
975 goto out;
976 r = -EFAULT;
977 if (copy_to_user(argp, &chip, sizeof chip))
978 goto out;
979 r = 0;
980 break;
981 }
982 case KVM_SET_IRQCHIP: {
983 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
984 struct kvm_irqchip chip;
985
986 r = -EFAULT;
987 if (copy_from_user(&chip, argp, sizeof chip))
988 goto out;
989 r = -ENXIO;
990 if (!irqchip_in_kernel(kvm))
991 goto out;
992 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
993 if (r)
994 goto out;
995 r = 0;
996 break;
997 }
998 default:
999 ;
1000 }
1001out:
1002 return r;
1003}
1004
1005int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1006 struct kvm_sregs *sregs)
1007{
1008 return -EINVAL;
1009}
1010
1011int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1012 struct kvm_sregs *sregs)
1013{
1014 return -EINVAL;
1015
1016}
1017int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1018 struct kvm_translation *tr)
1019{
1020
1021 return -EINVAL;
1022}
1023
1024static int kvm_alloc_vmm_area(void)
1025{
1026 if (!kvm_vmm_base && (kvm_vm_buffer_size < KVM_VM_BUFFER_SIZE)) {
1027 kvm_vmm_base = __get_free_pages(GFP_KERNEL,
1028 get_order(KVM_VMM_SIZE));
1029 if (!kvm_vmm_base)
1030 return -ENOMEM;
1031
1032 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1033 kvm_vm_buffer = kvm_vmm_base + VMM_SIZE;
1034
1035 printk(KERN_DEBUG"kvm:VMM's Base Addr:0x%lx, vm_buffer:0x%lx\n",
1036 kvm_vmm_base, kvm_vm_buffer);
1037 }
1038
1039 return 0;
1040}
1041
1042static void kvm_free_vmm_area(void)
1043{
1044 if (kvm_vmm_base) {
1045 /*Zero this area before free to avoid bits leak!!*/
1046 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1047 free_pages(kvm_vmm_base, get_order(KVM_VMM_SIZE));
1048 kvm_vmm_base = 0;
1049 kvm_vm_buffer = 0;
1050 kvm_vsa_base = 0;
1051 }
1052}
1053
Xiantao Zhangb024b792008-04-01 15:29:29 +08001054static int vti_init_vpd(struct kvm_vcpu *vcpu)
1055{
1056 int i;
1057 union cpuid3_t cpuid3;
1058 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1059
1060 if (IS_ERR(vpd))
1061 return PTR_ERR(vpd);
1062
1063 /* CPUID init */
1064 for (i = 0; i < 5; i++)
1065 vpd->vcpuid[i] = ia64_get_cpuid(i);
1066
1067 /* Limit the CPUID number to 5 */
1068 cpuid3.value = vpd->vcpuid[3];
1069 cpuid3.number = 4; /* 5 - 1 */
1070 vpd->vcpuid[3] = cpuid3.value;
1071
1072 /*Set vac and vdc fields*/
1073 vpd->vac.a_from_int_cr = 1;
1074 vpd->vac.a_to_int_cr = 1;
1075 vpd->vac.a_from_psr = 1;
1076 vpd->vac.a_from_cpuid = 1;
1077 vpd->vac.a_cover = 1;
1078 vpd->vac.a_bsw = 1;
1079 vpd->vac.a_int = 1;
1080 vpd->vdc.d_vmsw = 1;
1081
1082 /*Set virtual buffer*/
1083 vpd->virt_env_vaddr = KVM_VM_BUFFER_BASE;
1084
1085 return 0;
1086}
1087
1088static int vti_create_vp(struct kvm_vcpu *vcpu)
1089{
1090 long ret;
1091 struct vpd *vpd = vcpu->arch.vpd;
1092 unsigned long vmm_ivt;
1093
1094 vmm_ivt = kvm_vmm_info->vmm_ivt;
1095
1096 printk(KERN_DEBUG "kvm: vcpu:%p,ivt: 0x%lx\n", vcpu, vmm_ivt);
1097
1098 ret = ia64_pal_vp_create((u64 *)vpd, (u64 *)vmm_ivt, 0);
1099
1100 if (ret) {
1101 printk(KERN_ERR"kvm: ia64_pal_vp_create failed!\n");
1102 return -EINVAL;
1103 }
1104 return 0;
1105}
1106
1107static void init_ptce_info(struct kvm_vcpu *vcpu)
1108{
1109 ia64_ptce_info_t ptce = {0};
1110
1111 ia64_get_ptce(&ptce);
1112 vcpu->arch.ptce_base = ptce.base;
1113 vcpu->arch.ptce_count[0] = ptce.count[0];
1114 vcpu->arch.ptce_count[1] = ptce.count[1];
1115 vcpu->arch.ptce_stride[0] = ptce.stride[0];
1116 vcpu->arch.ptce_stride[1] = ptce.stride[1];
1117}
1118
1119static void kvm_migrate_hlt_timer(struct kvm_vcpu *vcpu)
1120{
1121 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
1122
1123 if (hrtimer_cancel(p_ht))
Arjan van de Ven18dd36a2008-09-01 15:19:11 -07001124 hrtimer_start_expires(p_ht, HRTIMER_MODE_ABS);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001125}
1126
1127static enum hrtimer_restart hlt_timer_fn(struct hrtimer *data)
1128{
1129 struct kvm_vcpu *vcpu;
1130 wait_queue_head_t *q;
1131
1132 vcpu = container_of(data, struct kvm_vcpu, arch.hlt_timer);
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001133 q = &vcpu->wq;
1134
Avi Kivitya4535292008-04-13 17:54:35 +03001135 if (vcpu->arch.mp_state != KVM_MP_STATE_HALTED)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001136 goto out;
1137
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001138 if (waitqueue_active(q))
Xiantao Zhangb024b792008-04-01 15:29:29 +08001139 wake_up_interruptible(q);
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001140
Xiantao Zhangb024b792008-04-01 15:29:29 +08001141out:
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001142 vcpu->arch.timer_fired = 1;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001143 vcpu->arch.timer_check = 1;
1144 return HRTIMER_NORESTART;
1145}
1146
1147#define PALE_RESET_ENTRY 0x80000000ffffffb0UL
1148
Avi Kivity3e515702012-03-05 14:23:29 +02001149bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu)
1150{
Avi Kivity82817152012-04-18 19:23:50 +03001151 return irqchip_in_kernel(vcpu->kvm) == (vcpu->arch.apic != NULL);
Avi Kivity3e515702012-03-05 14:23:29 +02001152}
1153
Xiantao Zhangb024b792008-04-01 15:29:29 +08001154int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1155{
1156 struct kvm_vcpu *v;
1157 int r;
1158 int i;
1159 long itc_offset;
1160 struct kvm *kvm = vcpu->kvm;
1161 struct kvm_pt_regs *regs = vcpu_regs(vcpu);
1162
1163 union context *p_ctx = &vcpu->arch.guest;
1164 struct kvm_vcpu *vmm_vcpu = to_guest(vcpu->kvm, vcpu);
1165
1166 /*Init vcpu context for first run.*/
1167 if (IS_ERR(vmm_vcpu))
1168 return PTR_ERR(vmm_vcpu);
1169
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001170 if (kvm_vcpu_is_bsp(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03001171 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001172
1173 /*Set entry address for first run.*/
1174 regs->cr_iip = PALE_RESET_ENTRY;
1175
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001176 /*Initialize itc offset for vcpus*/
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -06001177 itc_offset = 0UL - kvm_get_itc(vcpu);
Jes Sorensen3032b922009-05-25 10:22:17 +02001178 for (i = 0; i < KVM_MAX_VCPUS; i++) {
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001179 v = (struct kvm_vcpu *)((char *)vcpu +
1180 sizeof(struct kvm_vcpu_data) * i);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001181 v->arch.itc_offset = itc_offset;
1182 v->arch.last_itc = 0;
1183 }
1184 } else
Avi Kivitya4535292008-04-13 17:54:35 +03001185 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001186
1187 r = -ENOMEM;
1188 vcpu->arch.apic = kzalloc(sizeof(struct kvm_lapic), GFP_KERNEL);
1189 if (!vcpu->arch.apic)
1190 goto out;
1191 vcpu->arch.apic->vcpu = vcpu;
1192
1193 p_ctx->gr[1] = 0;
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001194 p_ctx->gr[12] = (unsigned long)((char *)vmm_vcpu + KVM_STK_OFFSET);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001195 p_ctx->gr[13] = (unsigned long)vmm_vcpu;
1196 p_ctx->psr = 0x1008522000UL;
1197 p_ctx->ar[40] = FPSR_DEFAULT; /*fpsr*/
1198 p_ctx->caller_unat = 0;
1199 p_ctx->pr = 0x0;
1200 p_ctx->ar[36] = 0x0; /*unat*/
1201 p_ctx->ar[19] = 0x0; /*rnat*/
1202 p_ctx->ar[18] = (unsigned long)vmm_vcpu +
1203 ((sizeof(struct kvm_vcpu)+15) & ~15);
1204 p_ctx->ar[64] = 0x0; /*pfs*/
1205 p_ctx->cr[0] = 0x7e04UL;
1206 p_ctx->cr[2] = (unsigned long)kvm_vmm_info->vmm_ivt;
1207 p_ctx->cr[8] = 0x3c;
1208
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001209 /*Initialize region register*/
Xiantao Zhangb024b792008-04-01 15:29:29 +08001210 p_ctx->rr[0] = 0x30;
1211 p_ctx->rr[1] = 0x30;
1212 p_ctx->rr[2] = 0x30;
1213 p_ctx->rr[3] = 0x30;
1214 p_ctx->rr[4] = 0x30;
1215 p_ctx->rr[5] = 0x30;
1216 p_ctx->rr[7] = 0x30;
1217
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001218 /*Initialize branch register 0*/
Xiantao Zhangb024b792008-04-01 15:29:29 +08001219 p_ctx->br[0] = *(unsigned long *)kvm_vmm_info->vmm_entry;
1220
1221 vcpu->arch.vmm_rr = kvm->arch.vmm_init_rr;
1222 vcpu->arch.metaphysical_rr0 = kvm->arch.metaphysical_rr0;
1223 vcpu->arch.metaphysical_rr4 = kvm->arch.metaphysical_rr4;
1224
1225 hrtimer_init(&vcpu->arch.hlt_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1226 vcpu->arch.hlt_timer.function = hlt_timer_fn;
1227
1228 vcpu->arch.last_run_cpu = -1;
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001229 vcpu->arch.vpd = (struct vpd *)VPD_BASE(vcpu->vcpu_id);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001230 vcpu->arch.vsa_base = kvm_vsa_base;
1231 vcpu->arch.__gp = kvm_vmm_gp;
1232 vcpu->arch.dirty_log_lock_pa = __pa(&kvm->arch.dirty_log_lock);
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001233 vcpu->arch.vhpt.hash = (struct thash_data *)VHPT_BASE(vcpu->vcpu_id);
1234 vcpu->arch.vtlb.hash = (struct thash_data *)VTLB_BASE(vcpu->vcpu_id);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001235 init_ptce_info(vcpu);
1236
1237 r = 0;
1238out:
1239 return r;
1240}
1241
1242static int vti_vcpu_setup(struct kvm_vcpu *vcpu, int id)
1243{
1244 unsigned long psr;
1245 int r;
1246
1247 local_irq_save(psr);
1248 r = kvm_insert_vmm_mapping(vcpu);
Jes Sorensen457459c2009-04-16 16:08:29 +02001249 local_irq_restore(psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001250 if (r)
1251 goto fail;
1252 r = kvm_vcpu_init(vcpu, vcpu->kvm, id);
1253 if (r)
1254 goto fail;
1255
1256 r = vti_init_vpd(vcpu);
1257 if (r) {
1258 printk(KERN_DEBUG"kvm: vpd init error!!\n");
1259 goto uninit;
1260 }
1261
1262 r = vti_create_vp(vcpu);
1263 if (r)
1264 goto uninit;
1265
1266 kvm_purge_vmm_mapping(vcpu);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001267
1268 return 0;
1269uninit:
1270 kvm_vcpu_uninit(vcpu);
1271fail:
1272 return r;
1273}
1274
1275struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1276 unsigned int id)
1277{
1278 struct kvm_vcpu *vcpu;
1279 unsigned long vm_base = kvm->arch.vm_base;
1280 int r;
1281 int cpu;
1282
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001283 BUG_ON(sizeof(struct kvm_vcpu) > VCPU_STRUCT_SIZE/2);
1284
1285 r = -EINVAL;
1286 if (id >= KVM_MAX_VCPUS) {
1287 printk(KERN_ERR"kvm: Can't configure vcpus > %ld",
1288 KVM_MAX_VCPUS);
1289 goto fail;
1290 }
1291
Xiantao Zhangb024b792008-04-01 15:29:29 +08001292 r = -ENOMEM;
1293 if (!vm_base) {
1294 printk(KERN_ERR"kvm: Create vcpu[%d] error!\n", id);
1295 goto fail;
1296 }
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001297 vcpu = (struct kvm_vcpu *)(vm_base + offsetof(struct kvm_vm_data,
1298 vcpu_data[id].vcpu_struct));
Xiantao Zhangb024b792008-04-01 15:29:29 +08001299 vcpu->kvm = kvm;
1300
1301 cpu = get_cpu();
Xiantao Zhangb024b792008-04-01 15:29:29 +08001302 r = vti_vcpu_setup(vcpu, id);
1303 put_cpu();
1304
1305 if (r) {
1306 printk(KERN_DEBUG"kvm: vcpu_setup error!!\n");
1307 goto fail;
1308 }
1309
1310 return vcpu;
1311fail:
1312 return ERR_PTR(r);
1313}
1314
1315int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1316{
1317 return 0;
1318}
1319
Marcelo Tosatti42897d82012-11-27 23:29:02 -02001320int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
1321{
1322 return 0;
1323}
1324
Xiantao Zhangb024b792008-04-01 15:29:29 +08001325int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1326{
1327 return -EINVAL;
1328}
1329
1330int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1331{
1332 return -EINVAL;
1333}
1334
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001335int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1336 struct kvm_guest_debug *dbg)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001337{
1338 return -EINVAL;
1339}
1340
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001341void kvm_arch_free_vm(struct kvm *kvm)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001342{
1343 unsigned long vm_base = kvm->arch.vm_base;
1344
1345 if (vm_base) {
1346 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
1347 free_pages(vm_base, get_order(KVM_VM_DATA_SIZE));
1348 }
1349
1350}
1351
1352static void kvm_release_vm_pages(struct kvm *kvm)
1353{
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001354 struct kvm_memslots *slots;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001355 struct kvm_memory_slot *memslot;
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +08001356 int j;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001357
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08001358 slots = kvm_memslots(kvm);
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +08001359 kvm_for_each_memslot(memslot, slots) {
Xiantao Zhangb024b792008-04-01 15:29:29 +08001360 for (j = 0; j < memslot->npages; j++) {
1361 if (memslot->rmap[j])
1362 put_page((struct page *)memslot->rmap[j]);
1363 }
1364 }
1365}
1366
Sheng Yangad8ba2c2009-01-06 10:03:02 +08001367void kvm_arch_sync_events(struct kvm *kvm)
1368{
1369}
1370
Xiantao Zhangb024b792008-04-01 15:29:29 +08001371void kvm_arch_destroy_vm(struct kvm *kvm)
1372{
Xiantao Zhang2381ad22008-10-08 08:29:33 +08001373 kvm_iommu_unmap_guest(kvm);
Xiantao Zhang2381ad22008-10-08 08:29:33 +08001374 kvm_free_all_assigned_devices(kvm);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001375 kfree(kvm->arch.vioapic);
1376 kvm_release_vm_pages(kvm);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001377}
1378
1379void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1380{
1381}
1382
1383void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1384{
1385 if (cpu != vcpu->cpu) {
1386 vcpu->cpu = cpu;
1387 if (vcpu->arch.ht_active)
1388 kvm_migrate_hlt_timer(vcpu);
1389 }
1390}
1391
1392#define SAVE_REGS(_x) regs->_x = vcpu->arch._x
1393
1394int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1395{
Xiantao Zhangb024b792008-04-01 15:29:29 +08001396 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
Jes Sorensen042b26e2008-12-16 16:45:47 +01001397 int i;
1398
Xiantao Zhangb024b792008-04-01 15:29:29 +08001399 vcpu_load(vcpu);
1400
1401 for (i = 0; i < 16; i++) {
1402 regs->vpd.vgr[i] = vpd->vgr[i];
1403 regs->vpd.vbgr[i] = vpd->vbgr[i];
1404 }
1405 for (i = 0; i < 128; i++)
1406 regs->vpd.vcr[i] = vpd->vcr[i];
1407 regs->vpd.vhpi = vpd->vhpi;
1408 regs->vpd.vnat = vpd->vnat;
1409 regs->vpd.vbnat = vpd->vbnat;
1410 regs->vpd.vpsr = vpd->vpsr;
1411 regs->vpd.vpr = vpd->vpr;
1412
Jes Sorensen042b26e2008-12-16 16:45:47 +01001413 memcpy(&regs->saved_guest, &vcpu->arch.guest, sizeof(union context));
1414
Xiantao Zhangb024b792008-04-01 15:29:29 +08001415 SAVE_REGS(mp_state);
1416 SAVE_REGS(vmm_rr);
1417 memcpy(regs->itrs, vcpu->arch.itrs, sizeof(struct thash_data) * NITRS);
1418 memcpy(regs->dtrs, vcpu->arch.dtrs, sizeof(struct thash_data) * NDTRS);
1419 SAVE_REGS(itr_regions);
1420 SAVE_REGS(dtr_regions);
1421 SAVE_REGS(tc_regions);
1422 SAVE_REGS(irq_check);
1423 SAVE_REGS(itc_check);
1424 SAVE_REGS(timer_check);
1425 SAVE_REGS(timer_pending);
1426 SAVE_REGS(last_itc);
1427 for (i = 0; i < 8; i++) {
1428 regs->vrr[i] = vcpu->arch.vrr[i];
1429 regs->ibr[i] = vcpu->arch.ibr[i];
1430 regs->dbr[i] = vcpu->arch.dbr[i];
1431 }
1432 for (i = 0; i < 4; i++)
1433 regs->insvc[i] = vcpu->arch.insvc[i];
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -06001434 regs->saved_itc = vcpu->arch.itc_offset + kvm_get_itc(vcpu);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001435 SAVE_REGS(xtp);
1436 SAVE_REGS(metaphysical_rr0);
1437 SAVE_REGS(metaphysical_rr4);
1438 SAVE_REGS(metaphysical_saved_rr0);
1439 SAVE_REGS(metaphysical_saved_rr4);
1440 SAVE_REGS(fp_psr);
1441 SAVE_REGS(saved_gp);
Jes Sorensen042b26e2008-12-16 16:45:47 +01001442
Xiantao Zhangb024b792008-04-01 15:29:29 +08001443 vcpu_put(vcpu);
Jes Sorensen042b26e2008-12-16 16:45:47 +01001444 return 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001445}
1446
Jes Sorensene9a999f2008-12-18 12:17:51 +01001447int kvm_arch_vcpu_ioctl_get_stack(struct kvm_vcpu *vcpu,
1448 struct kvm_ia64_vcpu_stack *stack)
1449{
1450 memcpy(stack, vcpu, sizeof(struct kvm_ia64_vcpu_stack));
1451 return 0;
1452}
1453
1454int kvm_arch_vcpu_ioctl_set_stack(struct kvm_vcpu *vcpu,
1455 struct kvm_ia64_vcpu_stack *stack)
1456{
1457 memcpy(vcpu + 1, &stack->stack[0] + sizeof(struct kvm_vcpu),
1458 sizeof(struct kvm_ia64_vcpu_stack) - sizeof(struct kvm_vcpu));
1459
1460 vcpu->arch.exit_data = ((struct kvm_vcpu *)stack)->arch.exit_data;
1461 return 0;
1462}
1463
Xiantao Zhangb024b792008-04-01 15:29:29 +08001464void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1465{
1466
1467 hrtimer_cancel(&vcpu->arch.hlt_timer);
1468 kfree(vcpu->arch.apic);
1469}
1470
1471
1472long kvm_arch_vcpu_ioctl(struct file *filp,
Jes Sorensene9a999f2008-12-18 12:17:51 +01001473 unsigned int ioctl, unsigned long arg)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001474{
Jes Sorensene9a999f2008-12-18 12:17:51 +01001475 struct kvm_vcpu *vcpu = filp->private_data;
1476 void __user *argp = (void __user *)arg;
1477 struct kvm_ia64_vcpu_stack *stack = NULL;
1478 long r;
1479
1480 switch (ioctl) {
1481 case KVM_IA64_VCPU_GET_STACK: {
1482 struct kvm_ia64_vcpu_stack __user *user_stack;
1483 void __user *first_p = argp;
1484
1485 r = -EFAULT;
1486 if (copy_from_user(&user_stack, first_p, sizeof(void *)))
1487 goto out;
1488
1489 if (!access_ok(VERIFY_WRITE, user_stack,
1490 sizeof(struct kvm_ia64_vcpu_stack))) {
1491 printk(KERN_INFO "KVM_IA64_VCPU_GET_STACK: "
1492 "Illegal user destination address for stack\n");
1493 goto out;
1494 }
1495 stack = kzalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL);
1496 if (!stack) {
1497 r = -ENOMEM;
1498 goto out;
1499 }
1500
1501 r = kvm_arch_vcpu_ioctl_get_stack(vcpu, stack);
1502 if (r)
1503 goto out;
1504
1505 if (copy_to_user(user_stack, stack,
Wei Yongjun5c0d0922010-03-12 08:45:39 +08001506 sizeof(struct kvm_ia64_vcpu_stack))) {
1507 r = -EFAULT;
Jes Sorensene9a999f2008-12-18 12:17:51 +01001508 goto out;
Wei Yongjun5c0d0922010-03-12 08:45:39 +08001509 }
Jes Sorensene9a999f2008-12-18 12:17:51 +01001510
1511 break;
1512 }
1513 case KVM_IA64_VCPU_SET_STACK: {
1514 struct kvm_ia64_vcpu_stack __user *user_stack;
1515 void __user *first_p = argp;
1516
1517 r = -EFAULT;
1518 if (copy_from_user(&user_stack, first_p, sizeof(void *)))
1519 goto out;
1520
1521 if (!access_ok(VERIFY_READ, user_stack,
1522 sizeof(struct kvm_ia64_vcpu_stack))) {
1523 printk(KERN_INFO "KVM_IA64_VCPU_SET_STACK: "
1524 "Illegal user address for stack\n");
1525 goto out;
1526 }
1527 stack = kmalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL);
1528 if (!stack) {
1529 r = -ENOMEM;
1530 goto out;
1531 }
1532 if (copy_from_user(stack, user_stack,
1533 sizeof(struct kvm_ia64_vcpu_stack)))
1534 goto out;
1535
1536 r = kvm_arch_vcpu_ioctl_set_stack(vcpu, stack);
1537 break;
1538 }
1539
1540 default:
1541 r = -EINVAL;
1542 }
1543
1544out:
1545 kfree(stack);
1546 return r;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001547}
1548
Carsten Otte5b1c1492012-01-04 10:25:23 +01001549int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1550{
1551 return VM_FAULT_SIGBUS;
1552}
1553
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05301554void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09001555 struct kvm_memory_slot *dont)
1556{
1557}
1558
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05301559int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
1560 unsigned long npages)
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09001561{
1562 return 0;
1563}
1564
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09001565void kvm_arch_memslots_updated(struct kvm *kvm)
1566{
1567}
1568
Marcelo Tosattif7784b82009-12-23 14:35:18 -02001569int kvm_arch_prepare_memory_region(struct kvm *kvm,
1570 struct kvm_memory_slot *memslot,
Takuya Yoshikawa7b6195a2013-02-27 19:44:34 +09001571 struct kvm_userspace_memory_region *mem,
1572 enum kvm_mr_change change)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001573{
1574 unsigned long i;
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001575 unsigned long pfn;
Marcelo Tosattif7784b82009-12-23 14:35:18 -02001576 int npages = memslot->npages;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001577 unsigned long base_gfn = memslot->base_gfn;
1578
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001579 if (base_gfn + npages > (KVM_MAX_MEM_SIZE >> PAGE_SHIFT))
1580 return -ENOMEM;
1581
Xiantao Zhangb024b792008-04-01 15:29:29 +08001582 for (i = 0; i < npages; i++) {
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001583 pfn = gfn_to_pfn(kvm, base_gfn + i);
1584 if (!kvm_is_mmio_pfn(pfn)) {
1585 kvm_set_pmt_entry(kvm, base_gfn + i,
1586 pfn << PAGE_SHIFT,
Xiantao Zhangb010eb52008-09-28 01:39:46 -07001587 _PAGE_AR_RWX | _PAGE_MA_WB);
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001588 memslot->rmap[i] = (unsigned long)pfn_to_page(pfn);
1589 } else {
1590 kvm_set_pmt_entry(kvm, base_gfn + i,
Xiantao Zhangb010eb52008-09-28 01:39:46 -07001591 GPFN_PHYS_MMIO | (pfn << PAGE_SHIFT),
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001592 _PAGE_MA_UC);
1593 memslot->rmap[i] = 0;
1594 }
Xiantao Zhangb024b792008-04-01 15:29:29 +08001595 }
1596
1597 return 0;
1598}
1599
Marcelo Tosattif7784b82009-12-23 14:35:18 -02001600void kvm_arch_commit_memory_region(struct kvm *kvm,
1601 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001602 const struct kvm_memory_slot *old,
1603 enum kvm_mr_change change)
Marcelo Tosattif7784b82009-12-23 14:35:18 -02001604{
1605 return;
1606}
1607
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03001608void kvm_arch_flush_shadow_all(struct kvm *kvm)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001609{
Xiantao Zhang64f6afb2009-04-16 17:59:16 +08001610 kvm_flush_remote_tlbs(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001611}
Xiantao Zhangb024b792008-04-01 15:29:29 +08001612
Marcelo Tosatti2df72e92012-08-24 15:54:57 -03001613void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
1614 struct kvm_memory_slot *slot)
1615{
1616 kvm_arch_flush_shadow_all();
1617}
1618
Xiantao Zhangb024b792008-04-01 15:29:29 +08001619long kvm_arch_dev_ioctl(struct file *filp,
Jes Sorensene9a999f2008-12-18 12:17:51 +01001620 unsigned int ioctl, unsigned long arg)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001621{
1622 return -EINVAL;
1623}
1624
1625void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1626{
1627 kvm_vcpu_uninit(vcpu);
1628}
1629
1630static int vti_cpu_has_kvm_support(void)
1631{
1632 long avail = 1, status = 1, control = 1;
1633 long ret;
1634
1635 ret = ia64_pal_proc_get_features(&avail, &status, &control, 0);
1636 if (ret)
1637 goto out;
1638
1639 if (!(avail & PAL_PROC_VM_BIT))
1640 goto out;
1641
1642 printk(KERN_DEBUG"kvm: Hardware Supports VT\n");
1643
1644 ret = ia64_pal_vp_env_info(&kvm_vm_buffer_size, &vp_env_info);
1645 if (ret)
1646 goto out;
1647 printk(KERN_DEBUG"kvm: VM Buffer Size:0x%lx\n", kvm_vm_buffer_size);
1648
1649 if (!(vp_env_info & VP_OPCODE)) {
1650 printk(KERN_WARNING"kvm: No opcode ability on hardware, "
1651 "vm_env_info:0x%lx\n", vp_env_info);
1652 }
1653
1654 return 1;
1655out:
1656 return 0;
1657}
1658
Jes Sorensen0b5d7a22009-02-25 10:38:55 -06001659
1660/*
1661 * On SN2, the ITC isn't stable, so copy in fast path code to use the
1662 * SN2 RTC, replacing the ITC based default verion.
1663 */
1664static void kvm_patch_vmm(struct kvm_vmm_info *vmm_info,
1665 struct module *module)
1666{
1667 unsigned long new_ar, new_ar_sn2;
1668 unsigned long module_base;
1669
1670 if (!ia64_platform_is("sn2"))
1671 return;
1672
1673 module_base = (unsigned long)module->module_core;
1674
1675 new_ar = kvm_vmm_base + vmm_info->patch_mov_ar - module_base;
1676 new_ar_sn2 = kvm_vmm_base + vmm_info->patch_mov_ar_sn2 - module_base;
1677
1678 printk(KERN_INFO "kvm: Patching ITC emulation to use SGI SN2 RTC "
1679 "as source\n");
1680
1681 /*
1682 * Copy the SN2 version of mov_ar into place. They are both
1683 * the same size, so 6 bundles is sufficient (6 * 0x10).
1684 */
1685 memcpy((void *)new_ar, (void *)new_ar_sn2, 0x60);
1686}
1687
Xiantao Zhangb024b792008-04-01 15:29:29 +08001688static int kvm_relocate_vmm(struct kvm_vmm_info *vmm_info,
Jes Sorensen0b5d7a22009-02-25 10:38:55 -06001689 struct module *module)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001690{
1691 unsigned long module_base;
1692 unsigned long vmm_size;
1693
1694 unsigned long vmm_offset, func_offset, fdesc_offset;
1695 struct fdesc *p_fdesc;
1696
1697 BUG_ON(!module);
1698
1699 if (!kvm_vmm_base) {
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001700 printk("kvm: kvm area hasn't been initialized yet!!\n");
Xiantao Zhangb024b792008-04-01 15:29:29 +08001701 return -EFAULT;
1702 }
1703
1704 /*Calculate new position of relocated vmm module.*/
1705 module_base = (unsigned long)module->module_core;
1706 vmm_size = module->core_size;
1707 if (unlikely(vmm_size > KVM_VMM_SIZE))
1708 return -EFAULT;
1709
1710 memcpy((void *)kvm_vmm_base, (void *)module_base, vmm_size);
Jes Sorensen0b5d7a22009-02-25 10:38:55 -06001711 kvm_patch_vmm(vmm_info, module);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001712 kvm_flush_icache(kvm_vmm_base, vmm_size);
1713
1714 /*Recalculate kvm_vmm_info based on new VMM*/
1715 vmm_offset = vmm_info->vmm_ivt - module_base;
1716 kvm_vmm_info->vmm_ivt = KVM_VMM_BASE + vmm_offset;
1717 printk(KERN_DEBUG"kvm: Relocated VMM's IVT Base Addr:%lx\n",
1718 kvm_vmm_info->vmm_ivt);
1719
1720 fdesc_offset = (unsigned long)vmm_info->vmm_entry - module_base;
1721 kvm_vmm_info->vmm_entry = (kvm_vmm_entry *)(KVM_VMM_BASE +
1722 fdesc_offset);
1723 func_offset = *(unsigned long *)vmm_info->vmm_entry - module_base;
1724 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1725 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1726 p_fdesc->gp = KVM_VMM_BASE+(p_fdesc->gp - module_base);
1727
1728 printk(KERN_DEBUG"kvm: Relocated VMM's Init Entry Addr:%lx\n",
1729 KVM_VMM_BASE+func_offset);
1730
1731 fdesc_offset = (unsigned long)vmm_info->tramp_entry - module_base;
1732 kvm_vmm_info->tramp_entry = (kvm_tramp_entry *)(KVM_VMM_BASE +
1733 fdesc_offset);
1734 func_offset = *(unsigned long *)vmm_info->tramp_entry - module_base;
1735 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1736 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1737 p_fdesc->gp = KVM_VMM_BASE + (p_fdesc->gp - module_base);
1738
1739 kvm_vmm_gp = p_fdesc->gp;
1740
1741 printk(KERN_DEBUG"kvm: Relocated VMM's Entry IP:%p\n",
1742 kvm_vmm_info->vmm_entry);
1743 printk(KERN_DEBUG"kvm: Relocated VMM's Trampoline Entry IP:0x%lx\n",
1744 KVM_VMM_BASE + func_offset);
1745
1746 return 0;
1747}
1748
1749int kvm_arch_init(void *opaque)
1750{
1751 int r;
1752 struct kvm_vmm_info *vmm_info = (struct kvm_vmm_info *)opaque;
1753
1754 if (!vti_cpu_has_kvm_support()) {
1755 printk(KERN_ERR "kvm: No Hardware Virtualization Support!\n");
1756 r = -EOPNOTSUPP;
1757 goto out;
1758 }
1759
1760 if (kvm_vmm_info) {
1761 printk(KERN_ERR "kvm: Already loaded VMM module!\n");
1762 r = -EEXIST;
1763 goto out;
1764 }
1765
1766 r = -ENOMEM;
1767 kvm_vmm_info = kzalloc(sizeof(struct kvm_vmm_info), GFP_KERNEL);
1768 if (!kvm_vmm_info)
1769 goto out;
1770
1771 if (kvm_alloc_vmm_area())
1772 goto out_free0;
1773
1774 r = kvm_relocate_vmm(vmm_info, vmm_info->module);
1775 if (r)
1776 goto out_free1;
1777
1778 return 0;
1779
1780out_free1:
1781 kvm_free_vmm_area();
1782out_free0:
1783 kfree(kvm_vmm_info);
1784out:
1785 return r;
1786}
1787
1788void kvm_arch_exit(void)
1789{
1790 kvm_free_vmm_area();
1791 kfree(kvm_vmm_info);
1792 kvm_vmm_info = NULL;
1793}
1794
Takuya Yoshikawa979586e2010-06-23 15:00:29 +09001795static void kvm_ia64_sync_dirty_log(struct kvm *kvm,
1796 struct kvm_memory_slot *memslot)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001797{
Takuya Yoshikawa979586e2010-06-23 15:00:29 +09001798 int i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001799 long base;
1800 unsigned long n;
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001801 unsigned long *dirty_bitmap = (unsigned long *)(kvm->arch.vm_base +
1802 offsetof(struct kvm_vm_data, kvm_mem_dirty_log));
Xiantao Zhangb024b792008-04-01 15:29:29 +08001803
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001804 n = kvm_dirty_bitmap_bytes(memslot);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001805 base = memslot->base_gfn / BITS_PER_LONG;
1806
Takuya Yoshikawa4482b062010-06-23 14:59:29 +09001807 spin_lock(&kvm->arch.dirty_log_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001808 for (i = 0; i < n/sizeof(long); ++i) {
1809 memslot->dirty_bitmap[i] = dirty_bitmap[base + i];
1810 dirty_bitmap[base + i] = 0;
1811 }
Takuya Yoshikawa4482b062010-06-23 14:59:29 +09001812 spin_unlock(&kvm->arch.dirty_log_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001813}
1814
1815int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1816 struct kvm_dirty_log *log)
1817{
1818 int r;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001819 unsigned long n;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001820 struct kvm_memory_slot *memslot;
1821 int is_dirty = 0;
1822
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001823 mutex_lock(&kvm->slots_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001824
Takuya Yoshikawa979586e2010-06-23 15:00:29 +09001825 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07001826 if (log->slot >= KVM_USER_MEM_SLOTS)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001827 goto out;
1828
Xiao Guangrong28a37542011-11-24 19:04:35 +08001829 memslot = id_to_memslot(kvm->memslots, log->slot);
Takuya Yoshikawa979586e2010-06-23 15:00:29 +09001830 r = -ENOENT;
1831 if (!memslot->dirty_bitmap)
1832 goto out;
1833
1834 kvm_ia64_sync_dirty_log(kvm, memslot);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001835 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1836 if (r)
1837 goto out;
1838
1839 /* If nothing is dirty, don't bother messing with page tables. */
1840 if (is_dirty) {
1841 kvm_flush_remote_tlbs(kvm);
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001842 n = kvm_dirty_bitmap_bytes(memslot);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001843 memset(memslot->dirty_bitmap, 0, n);
1844 }
1845 r = 0;
1846out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001847 mutex_unlock(&kvm->slots_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001848 return r;
1849}
1850
1851int kvm_arch_hardware_setup(void)
1852{
1853 return 0;
1854}
1855
1856void kvm_arch_hardware_unsetup(void)
1857{
1858}
1859
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001860int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001861{
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001862 return __apic_accept_irq(vcpu, irq->vector);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001863}
1864
1865int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
1866{
1867 return apic->vcpu->vcpu_id == dest;
1868}
1869
1870int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
1871{
1872 return 0;
1873}
1874
Gleb Natapove1035712009-03-05 16:34:59 +02001875int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001876{
Gleb Natapove1035712009-03-05 16:34:59 +02001877 return vcpu1->arch.xtp - vcpu2->arch.xtp;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001878}
1879
Gleb Natapov343f94f2009-03-05 16:34:54 +02001880int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
1881 int short_hand, int dest, int dest_mode)
1882{
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001883 struct kvm_lapic *target = vcpu->arch.apic;
Gleb Natapov343f94f2009-03-05 16:34:54 +02001884 return (dest_mode == 0) ?
1885 kvm_apic_match_physical_addr(target, dest) :
1886 kvm_apic_match_logical_addr(target, dest);
1887}
1888
Xiantao Zhangb024b792008-04-01 15:29:29 +08001889static int find_highest_bits(int *dat)
1890{
1891 u32 bits, bitnum;
1892 int i;
1893
1894 /* loop for all 256 bits */
1895 for (i = 7; i >= 0 ; i--) {
1896 bits = dat[i];
1897 if (bits) {
1898 bitnum = fls(bits);
1899 return i * 32 + bitnum - 1;
1900 }
1901 }
1902
1903 return -1;
1904}
1905
1906int kvm_highest_pending_irq(struct kvm_vcpu *vcpu)
1907{
1908 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1909
1910 if (vpd->irr[0] & (1UL << NMI_VECTOR))
1911 return NMI_VECTOR;
1912 if (vpd->irr[0] & (1UL << ExtINT_VECTOR))
1913 return ExtINT_VECTOR;
1914
1915 return find_highest_bits((int *)&vpd->irr[0]);
1916}
1917
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001918int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1919{
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001920 return vcpu->arch.timer_fired;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001921}
1922
Xiantao Zhangb024b792008-04-01 15:29:29 +08001923int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1924{
Gleb Natapova1b37102009-07-09 15:33:52 +03001925 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE) ||
1926 (kvm_highest_pending_irq(vcpu) != -1);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001927}
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001928
Christoffer Dallb6d33832012-03-08 16:44:24 -05001929int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
1930{
1931 return (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests));
1932}
1933
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001934int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1935 struct kvm_mp_state *mp_state)
1936{
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001937 mp_state->mp_state = vcpu->arch.mp_state;
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001938 return 0;
1939}
1940
1941static int vcpu_reset(struct kvm_vcpu *vcpu)
1942{
1943 int r;
1944 long psr;
1945 local_irq_save(psr);
1946 r = kvm_insert_vmm_mapping(vcpu);
Jes Sorensen43890ae2009-04-17 16:43:27 +02001947 local_irq_restore(psr);
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001948 if (r)
1949 goto fail;
1950
1951 vcpu->arch.launched = 0;
1952 kvm_arch_vcpu_uninit(vcpu);
1953 r = kvm_arch_vcpu_init(vcpu);
1954 if (r)
1955 goto fail;
1956
1957 kvm_purge_vmm_mapping(vcpu);
1958 r = 0;
1959fail:
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001960 return r;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001961}
1962
1963int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1964 struct kvm_mp_state *mp_state)
1965{
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001966 int r = 0;
1967
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001968 vcpu->arch.mp_state = mp_state->mp_state;
1969 if (vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)
1970 r = vcpu_reset(vcpu);
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001971 return r;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001972}