blob: 463fb3bbe11ee13dbd903f65359864bd8f45b452 [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>
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:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800202 r = 1;
203 break;
Laurent Vivier7f39f8a2008-05-30 16:05:57 +0200204 case KVM_CAP_COALESCED_MMIO:
205 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
206 break;
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800207 case KVM_CAP_IOMMU:
Joerg Roedela1b60c12011-09-06 18:46:34 +0200208 r = iommu_present(&pci_bus_type);
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800209 break;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800210 default:
211 r = 0;
212 }
213 return r;
214
215}
216
Xiantao Zhangb024b792008-04-01 15:29:29 +0800217static int handle_vm_error(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
218{
219 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
220 kvm_run->hw.hardware_exit_reason = 1;
221 return 0;
222}
223
224static int handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
225{
226 struct kvm_mmio_req *p;
227 struct kvm_io_device *mmio_dev;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300228 int r;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800229
230 p = kvm_get_vcpu_ioreq(vcpu);
231
232 if ((p->addr & PAGE_MASK) == IOAPIC_DEFAULT_BASE_ADDRESS)
233 goto mmio;
234 vcpu->mmio_needed = 1;
235 vcpu->mmio_phys_addr = kvm_run->mmio.phys_addr = p->addr;
236 vcpu->mmio_size = kvm_run->mmio.len = p->size;
237 vcpu->mmio_is_write = kvm_run->mmio.is_write = !p->dir;
238
239 if (vcpu->mmio_is_write)
240 memcpy(vcpu->mmio_data, &p->data, p->size);
241 memcpy(kvm_run->mmio.data, &p->data, p->size);
242 kvm_run->exit_reason = KVM_EXIT_MMIO;
243 return 0;
244mmio:
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300245 if (p->dir)
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200246 r = kvm_io_bus_read(vcpu->kvm, KVM_MMIO_BUS, p->addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300247 p->size, &p->data);
248 else
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200249 r = kvm_io_bus_write(vcpu->kvm, KVM_MMIO_BUS, p->addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +0300250 p->size, &p->data);
251 if (r)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800252 printk(KERN_ERR"kvm: No iodevice found! addr:%lx\n", p->addr);
253 p->state = STATE_IORESP_READY;
254
255 return 1;
256}
257
258static int handle_pal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
259{
260 struct exit_ctl_data *p;
261
262 p = kvm_get_exit_data(vcpu);
263
264 if (p->exit_reason == EXIT_REASON_PAL_CALL)
265 return kvm_pal_emul(vcpu, kvm_run);
266 else {
267 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
268 kvm_run->hw.hardware_exit_reason = 2;
269 return 0;
270 }
271}
272
273static int handle_sal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
274{
275 struct exit_ctl_data *p;
276
277 p = kvm_get_exit_data(vcpu);
278
279 if (p->exit_reason == EXIT_REASON_SAL_CALL) {
280 kvm_sal_emul(vcpu);
281 return 1;
282 } else {
283 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
284 kvm_run->hw.hardware_exit_reason = 3;
285 return 0;
286 }
287
288}
289
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200290static int __apic_accept_irq(struct kvm_vcpu *vcpu, uint64_t vector)
291{
292 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
293
294 if (!test_and_set_bit(vector, &vpd->irr[0])) {
295 vcpu->arch.irq_new_pending = 1;
296 kvm_vcpu_kick(vcpu);
297 return 1;
298 }
299 return 0;
300}
301
Xiantao Zhangb024b792008-04-01 15:29:29 +0800302/*
303 * offset: address offset to IPI space.
304 * value: deliver value.
305 */
306static void vcpu_deliver_ipi(struct kvm_vcpu *vcpu, uint64_t dm,
307 uint64_t vector)
308{
309 switch (dm) {
310 case SAPIC_FIXED:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800311 break;
312 case SAPIC_NMI:
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200313 vector = 2;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800314 break;
315 case SAPIC_EXTINT:
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200316 vector = 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800317 break;
318 case SAPIC_INIT:
319 case SAPIC_PMI:
320 default:
321 printk(KERN_ERR"kvm: Unimplemented Deliver reserved IPI!\n");
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200322 return;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800323 }
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200324 __apic_accept_irq(vcpu, vector);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800325}
326
327static struct kvm_vcpu *lid_to_vcpu(struct kvm *kvm, unsigned long id,
328 unsigned long eid)
329{
330 union ia64_lid lid;
331 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300332 struct kvm_vcpu *vcpu;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800333
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300334 kvm_for_each_vcpu(i, vcpu, kvm) {
335 lid.val = VCPU_LID(vcpu);
336 if (lid.id == id && lid.eid == eid)
337 return vcpu;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800338 }
339
340 return NULL;
341}
342
343static int handle_ipi(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
344{
345 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
346 struct kvm_vcpu *target_vcpu;
347 struct kvm_pt_regs *regs;
348 union ia64_ipi_a addr = p->u.ipi_data.addr;
349 union ia64_ipi_d data = p->u.ipi_data.data;
350
351 target_vcpu = lid_to_vcpu(vcpu->kvm, addr.id, addr.eid);
352 if (!target_vcpu)
353 return handle_vm_error(vcpu, kvm_run);
354
355 if (!target_vcpu->arch.launched) {
356 regs = vcpu_regs(target_vcpu);
357
358 regs->cr_iip = vcpu->kvm->arch.rdv_sal_data.boot_ip;
359 regs->r1 = vcpu->kvm->arch.rdv_sal_data.boot_gp;
360
Avi Kivitya4535292008-04-13 17:54:35 +0300361 target_vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800362 if (waitqueue_active(&target_vcpu->wq))
363 wake_up_interruptible(&target_vcpu->wq);
364 } else {
365 vcpu_deliver_ipi(target_vcpu, data.dm, data.vector);
366 if (target_vcpu != vcpu)
367 kvm_vcpu_kick(target_vcpu);
368 }
369
370 return 1;
371}
372
373struct call_data {
374 struct kvm_ptc_g ptc_g_data;
375 struct kvm_vcpu *vcpu;
376};
377
378static void vcpu_global_purge(void *info)
379{
380 struct call_data *p = (struct call_data *)info;
381 struct kvm_vcpu *vcpu = p->vcpu;
382
383 if (test_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
384 return;
385
386 set_bit(KVM_REQ_PTC_G, &vcpu->requests);
387 if (vcpu->arch.ptc_g_count < MAX_PTC_G_NUM) {
388 vcpu->arch.ptc_g_data[vcpu->arch.ptc_g_count++] =
389 p->ptc_g_data;
390 } else {
391 clear_bit(KVM_REQ_PTC_G, &vcpu->requests);
392 vcpu->arch.ptc_g_count = 0;
393 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
394 }
395}
396
397static int handle_global_purge(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
398{
399 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
400 struct kvm *kvm = vcpu->kvm;
401 struct call_data call_data;
402 int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300403 struct kvm_vcpu *vcpui;
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800404
Xiantao Zhangb024b792008-04-01 15:29:29 +0800405 call_data.ptc_g_data = p->u.ptc_g_data;
406
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300407 kvm_for_each_vcpu(i, vcpui, kvm) {
408 if (vcpui->arch.mp_state == KVM_MP_STATE_UNINITIALIZED ||
409 vcpu == vcpui)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800410 continue;
411
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300412 if (waitqueue_active(&vcpui->wq))
413 wake_up_interruptible(&vcpui->wq);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800414
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300415 if (vcpui->cpu != -1) {
416 call_data.vcpu = vcpui;
417 smp_call_function_single(vcpui->cpu,
Takashi Iwai2f73cca2008-07-17 18:09:12 +0200418 vcpu_global_purge, &call_data, 1);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800419 } else
420 printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n");
421
422 }
423 return 1;
424}
425
426static int handle_switch_rr6(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
427{
428 return 1;
429}
430
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600431static int kvm_sn2_setup_mappings(struct kvm_vcpu *vcpu)
432{
433 unsigned long pte, rtc_phys_addr, map_addr;
434 int slot;
435
436 map_addr = KVM_VMM_BASE + (1UL << KVM_VMM_SHIFT);
437 rtc_phys_addr = LOCAL_MMR_OFFSET | SH_RTC;
438 pte = pte_val(mk_pte_phys(rtc_phys_addr, PAGE_KERNEL_UC));
439 slot = ia64_itr_entry(0x3, map_addr, pte, PAGE_SHIFT);
440 vcpu->arch.sn_rtc_tr_slot = slot;
441 if (slot < 0) {
442 printk(KERN_ERR "Mayday mayday! RTC mapping failed!\n");
443 slot = 0;
444 }
445 return slot;
446}
447
Xiantao Zhangb024b792008-04-01 15:29:29 +0800448int kvm_emulate_halt(struct kvm_vcpu *vcpu)
449{
450
451 ktime_t kt;
452 long itc_diff;
453 unsigned long vcpu_now_itc;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800454 unsigned long expires;
455 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
456 unsigned long cyc_per_usec = local_cpu_data->cyc_per_usec;
457 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
458
Xiantao Zhangb024b792008-04-01 15:29:29 +0800459 if (irqchip_in_kernel(vcpu->kvm)) {
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800460
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -0600461 vcpu_now_itc = kvm_get_itc(vcpu) + vcpu->arch.itc_offset;
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800462
463 if (time_after(vcpu_now_itc, vpd->itm)) {
464 vcpu->arch.timer_check = 1;
465 return 1;
466 }
467 itc_diff = vpd->itm - vcpu_now_itc;
468 if (itc_diff < 0)
469 itc_diff = -itc_diff;
470
471 expires = div64_u64(itc_diff, cyc_per_usec);
472 kt = ktime_set(0, 1000 * expires);
473
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800474 vcpu->arch.ht_active = 1;
475 hrtimer_start(p_ht, kt, HRTIMER_MODE_ABS);
476
Avi Kivitya4535292008-04-13 17:54:35 +0300477 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800478 kvm_vcpu_block(vcpu);
479 hrtimer_cancel(p_ht);
480 vcpu->arch.ht_active = 0;
481
Gleb Natapov09cec752009-03-23 15:11:44 +0200482 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests) ||
483 kvm_cpu_has_pending_timer(vcpu))
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800484 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
Gleb Natapov09cec752009-03-23 15:11:44 +0200485 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800486
Avi Kivitya4535292008-04-13 17:54:35 +0300487 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800488 return -EINTR;
489 return 1;
490 } else {
491 printk(KERN_ERR"kvm: Unsupported userspace halt!");
492 return 0;
493 }
494}
495
496static int handle_vm_shutdown(struct kvm_vcpu *vcpu,
497 struct kvm_run *kvm_run)
498{
499 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
500 return 0;
501}
502
503static int handle_external_interrupt(struct kvm_vcpu *vcpu,
504 struct kvm_run *kvm_run)
505{
506 return 1;
507}
508
Xiantao Zhang7d637972008-11-21 20:58:11 +0800509static int handle_vcpu_debug(struct kvm_vcpu *vcpu,
510 struct kvm_run *kvm_run)
511{
512 printk("VMM: %s", vcpu->arch.log_buf);
513 return 1;
514}
515
Xiantao Zhangb024b792008-04-01 15:29:29 +0800516static int (*kvm_vti_exit_handlers[])(struct kvm_vcpu *vcpu,
517 struct kvm_run *kvm_run) = {
518 [EXIT_REASON_VM_PANIC] = handle_vm_error,
519 [EXIT_REASON_MMIO_INSTRUCTION] = handle_mmio,
520 [EXIT_REASON_PAL_CALL] = handle_pal_call,
521 [EXIT_REASON_SAL_CALL] = handle_sal_call,
522 [EXIT_REASON_SWITCH_RR6] = handle_switch_rr6,
523 [EXIT_REASON_VM_DESTROY] = handle_vm_shutdown,
524 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
525 [EXIT_REASON_IPI] = handle_ipi,
526 [EXIT_REASON_PTC_G] = handle_global_purge,
Xiantao Zhang7d637972008-11-21 20:58:11 +0800527 [EXIT_REASON_DEBUG] = handle_vcpu_debug,
Xiantao Zhangb024b792008-04-01 15:29:29 +0800528
529};
530
531static const int kvm_vti_max_exit_handlers =
532 sizeof(kvm_vti_exit_handlers)/sizeof(*kvm_vti_exit_handlers);
533
Xiantao Zhangb024b792008-04-01 15:29:29 +0800534static uint32_t kvm_get_exit_reason(struct kvm_vcpu *vcpu)
535{
536 struct exit_ctl_data *p_exit_data;
537
538 p_exit_data = kvm_get_exit_data(vcpu);
539 return p_exit_data->exit_reason;
540}
541
542/*
543 * The guest has exited. See if we can fix it or if we need userspace
544 * assistance.
545 */
546static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
547{
548 u32 exit_reason = kvm_get_exit_reason(vcpu);
549 vcpu->arch.last_exit = exit_reason;
550
551 if (exit_reason < kvm_vti_max_exit_handlers
552 && kvm_vti_exit_handlers[exit_reason])
553 return kvm_vti_exit_handlers[exit_reason](vcpu, kvm_run);
554 else {
555 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
556 kvm_run->hw.hardware_exit_reason = exit_reason;
557 }
558 return 0;
559}
560
561static inline void vti_set_rr6(unsigned long rr6)
562{
563 ia64_set_rr(RR6, rr6);
564 ia64_srlz_i();
565}
566
567static int kvm_insert_vmm_mapping(struct kvm_vcpu *vcpu)
568{
569 unsigned long pte;
570 struct kvm *kvm = vcpu->kvm;
571 int r;
572
573 /*Insert a pair of tr to map vmm*/
574 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
575 r = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
576 if (r < 0)
577 goto out;
578 vcpu->arch.vmm_tr_slot = r;
579 /*Insert a pairt of tr to map data of vm*/
580 pte = pte_val(mk_pte_phys(__pa(kvm->arch.vm_base), PAGE_KERNEL));
581 r = ia64_itr_entry(0x3, KVM_VM_DATA_BASE,
582 pte, KVM_VM_DATA_SHIFT);
583 if (r < 0)
584 goto out;
585 vcpu->arch.vm_tr_slot = r;
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600586
587#if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
588 if (kvm->arch.is_sn2) {
589 r = kvm_sn2_setup_mappings(vcpu);
590 if (r < 0)
591 goto out;
592 }
593#endif
594
Xiantao Zhangb024b792008-04-01 15:29:29 +0800595 r = 0;
596out:
597 return r;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800598}
599
600static void kvm_purge_vmm_mapping(struct kvm_vcpu *vcpu)
601{
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600602 struct kvm *kvm = vcpu->kvm;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800603 ia64_ptr_entry(0x3, vcpu->arch.vmm_tr_slot);
604 ia64_ptr_entry(0x3, vcpu->arch.vm_tr_slot);
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600605#if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
606 if (kvm->arch.is_sn2)
607 ia64_ptr_entry(0x3, vcpu->arch.sn_rtc_tr_slot);
608#endif
Xiantao Zhangb024b792008-04-01 15:29:29 +0800609}
610
611static int kvm_vcpu_pre_transition(struct kvm_vcpu *vcpu)
612{
Jes Sorensen4d13c3b2009-04-16 16:53:13 +0200613 unsigned long psr;
614 int r;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800615 int cpu = smp_processor_id();
616
617 if (vcpu->arch.last_run_cpu != cpu ||
618 per_cpu(last_vcpu, cpu) != vcpu) {
619 per_cpu(last_vcpu, cpu) = vcpu;
620 vcpu->arch.last_run_cpu = cpu;
621 kvm_flush_tlb_all();
622 }
623
624 vcpu->arch.host_rr6 = ia64_get_rr(RR6);
625 vti_set_rr6(vcpu->arch.vmm_rr);
Jes Sorensen4d13c3b2009-04-16 16:53:13 +0200626 local_irq_save(psr);
627 r = kvm_insert_vmm_mapping(vcpu);
628 local_irq_restore(psr);
629 return r;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800630}
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200631
Xiantao Zhangb024b792008-04-01 15:29:29 +0800632static void kvm_vcpu_post_transition(struct kvm_vcpu *vcpu)
633{
634 kvm_purge_vmm_mapping(vcpu);
635 vti_set_rr6(vcpu->arch.host_rr6);
636}
637
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200638static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800639{
640 union context *host_ctx, *guest_ctx;
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200641 int r, idx;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800642
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200643 idx = srcu_read_lock(&vcpu->kvm->srcu);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800644
645again:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800646 if (signal_pending(current)) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800647 r = -EINTR;
648 kvm_run->exit_reason = KVM_EXIT_INTR;
649 goto out;
650 }
651
Jes Sorensend24d2c12009-04-09 16:38:14 +0200652 preempt_disable();
653 local_irq_disable();
654
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200655 /*Get host and guest context with guest address space.*/
656 host_ctx = kvm_get_host_context(vcpu);
657 guest_ctx = kvm_get_guest_context(vcpu);
658
Marcelo Tosatti32f88402009-05-07 17:55:12 -0300659 clear_bit(KVM_REQ_KICK, &vcpu->requests);
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200660
661 r = kvm_vcpu_pre_transition(vcpu);
662 if (r < 0)
663 goto vcpu_run_fail;
664
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200665 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +0800666 vcpu->mode = IN_GUEST_MODE;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800667 kvm_guest_enter();
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200668
669 /*
670 * Transition to the guest
671 */
672 kvm_vmm_info->tramp_entry(host_ctx, guest_ctx);
673
674 kvm_vcpu_post_transition(vcpu);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800675
676 vcpu->arch.launched = 1;
Marcelo Tosatti32f88402009-05-07 17:55:12 -0300677 set_bit(KVM_REQ_KICK, &vcpu->requests);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800678 local_irq_enable();
679
680 /*
681 * We must have an instruction between local_irq_enable() and
682 * kvm_guest_exit(), so the timer interrupt isn't delayed by
683 * the interrupt shadow. The stat.exits increment will do nicely.
684 * But we need to prevent reordering, hence this barrier():
685 */
686 barrier();
Xiantao Zhangb024b792008-04-01 15:29:29 +0800687 kvm_guest_exit();
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +0800688 vcpu->mode = OUTSIDE_GUEST_MODE;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800689 preempt_enable();
690
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200691 idx = srcu_read_lock(&vcpu->kvm->srcu);
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200692
Xiantao Zhangb024b792008-04-01 15:29:29 +0800693 r = kvm_handle_exit(kvm_run, vcpu);
694
695 if (r > 0) {
696 if (!need_resched())
697 goto again;
698 }
699
700out:
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200701 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800702 if (r > 0) {
703 kvm_resched(vcpu);
Marcelo Tosattif656ce02009-12-23 14:35:25 -0200704 idx = srcu_read_lock(&vcpu->kvm->srcu);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800705 goto again;
706 }
707
708 return r;
Jes Sorensenc6b60c62009-04-16 10:43:48 +0200709
710vcpu_run_fail:
711 local_irq_enable();
712 preempt_enable();
713 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
714 goto out;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800715}
716
717static void kvm_set_mmio_data(struct kvm_vcpu *vcpu)
718{
719 struct kvm_mmio_req *p = kvm_get_vcpu_ioreq(vcpu);
720
721 if (!vcpu->mmio_is_write)
722 memcpy(&p->data, vcpu->mmio_data, 8);
723 p->state = STATE_IORESP_READY;
724}
725
726int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
727{
728 int r;
729 sigset_t sigsaved;
730
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800731 if (vcpu->sigset_active)
732 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
733
Avi Kivitya4535292008-04-13 17:54:35 +0300734 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800735 kvm_vcpu_block(vcpu);
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800736 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800737 r = -EAGAIN;
738 goto out;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800739 }
740
Xiantao Zhangb024b792008-04-01 15:29:29 +0800741 if (vcpu->mmio_needed) {
742 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
743 kvm_set_mmio_data(vcpu);
744 vcpu->mmio_read_completed = 1;
745 vcpu->mmio_needed = 0;
746 }
747 r = __vcpu_run(vcpu, kvm_run);
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800748out:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800749 if (vcpu->sigset_active)
750 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
751
Xiantao Zhangb024b792008-04-01 15:29:29 +0800752 return r;
753}
754
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100755struct kvm *kvm_arch_alloc_vm(void)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800756{
757
758 struct kvm *kvm;
759 uint64_t vm_base;
760
Xiantao Zhanga917f7a2008-10-23 14:56:44 +0800761 BUG_ON(sizeof(struct kvm) > KVM_VM_STRUCT_SIZE);
762
Xiantao Zhangb024b792008-04-01 15:29:29 +0800763 vm_base = __get_free_pages(GFP_KERNEL, get_order(KVM_VM_DATA_SIZE));
764
765 if (!vm_base)
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100766 return NULL;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800767
Xiantao Zhangb024b792008-04-01 15:29:29 +0800768 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
Xiantao Zhanga917f7a2008-10-23 14:56:44 +0800769 kvm = (struct kvm *)(vm_base +
770 offsetof(struct kvm_vm_data, kvm_vm_struct));
Xiantao Zhangb024b792008-04-01 15:29:29 +0800771 kvm->arch.vm_base = vm_base;
Xiantao Zhanga917f7a2008-10-23 14:56:44 +0800772 printk(KERN_DEBUG"kvm: vm's data area:0x%lx\n", vm_base);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800773
774 return kvm;
775}
776
Xiao Guangrong60f9a9e2011-11-24 18:09:11 +0800777struct kvm_ia64_io_range {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800778 unsigned long start;
779 unsigned long size;
780 unsigned long type;
781};
782
Xiao Guangrong60f9a9e2011-11-24 18:09:11 +0800783static const struct kvm_ia64_io_range io_ranges[] = {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800784 {VGA_IO_START, VGA_IO_SIZE, GPFN_FRAME_BUFFER},
785 {MMIO_START, MMIO_SIZE, GPFN_LOW_MMIO},
786 {LEGACY_IO_START, LEGACY_IO_SIZE, GPFN_LEGACY_IO},
787 {IO_SAPIC_START, IO_SAPIC_SIZE, GPFN_IOSAPIC},
788 {PIB_START, PIB_SIZE, GPFN_PIB},
789};
790
791static void kvm_build_io_pmt(struct kvm *kvm)
792{
793 unsigned long i, j;
794
795 /* Mark I/O ranges */
796 for (i = 0; i < (sizeof(io_ranges) / sizeof(struct kvm_io_range));
797 i++) {
798 for (j = io_ranges[i].start;
799 j < io_ranges[i].start + io_ranges[i].size;
800 j += PAGE_SIZE)
801 kvm_set_pmt_entry(kvm, j >> PAGE_SHIFT,
802 io_ranges[i].type, 0);
803 }
804
805}
806
807/*Use unused rids to virtualize guest rid.*/
808#define GUEST_PHYSICAL_RR0 0x1739
809#define GUEST_PHYSICAL_RR4 0x2739
810#define VMM_INIT_RR 0x1660
811
Carsten Ottee08b9632012-01-04 10:25:20 +0100812int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800813{
Xiantao Zhangb024b792008-04-01 15:29:29 +0800814 BUG_ON(!kvm);
815
Carsten Ottee08b9632012-01-04 10:25:20 +0100816 if (type)
817 return -EINVAL;
818
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100819 kvm->arch.is_sn2 = ia64_platform_is("sn2");
820
Xiantao Zhangb024b792008-04-01 15:29:29 +0800821 kvm->arch.metaphysical_rr0 = GUEST_PHYSICAL_RR0;
822 kvm->arch.metaphysical_rr4 = GUEST_PHYSICAL_RR4;
823 kvm->arch.vmm_init_rr = VMM_INIT_RR;
824
Xiantao Zhangb024b792008-04-01 15:29:29 +0800825 /*
826 *Fill P2M entries for MMIO/IO ranges
827 */
828 kvm_build_io_pmt(kvm);
829
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800830 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Sheng Yang5550af42008-10-15 20:15:06 +0800831
832 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
833 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800834
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100835 return 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800836}
837
838static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm,
839 struct kvm_irqchip *chip)
840{
841 int r;
842
843 r = 0;
844 switch (chip->chip_id) {
845 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +0300846 r = kvm_get_ioapic(kvm, &chip->chip.ioapic);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800847 break;
848 default:
849 r = -EINVAL;
850 break;
851 }
852 return r;
853}
854
855static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
856{
857 int r;
858
859 r = 0;
860 switch (chip->chip_id) {
861 case KVM_IRQCHIP_IOAPIC:
Gleb Natapoveba02262009-08-24 11:54:25 +0300862 r = kvm_set_ioapic(kvm, &chip->chip.ioapic);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800863 break;
864 default:
865 r = -EINVAL;
866 break;
867 }
868 return r;
869}
870
871#define RESTORE_REGS(_x) vcpu->arch._x = regs->_x
872
873int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
874{
Xiantao Zhangb024b792008-04-01 15:29:29 +0800875 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
Jes Sorensen042b26e2008-12-16 16:45:47 +0100876 int i;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800877
Xiantao Zhangb024b792008-04-01 15:29:29 +0800878 for (i = 0; i < 16; i++) {
879 vpd->vgr[i] = regs->vpd.vgr[i];
880 vpd->vbgr[i] = regs->vpd.vbgr[i];
881 }
882 for (i = 0; i < 128; i++)
883 vpd->vcr[i] = regs->vpd.vcr[i];
884 vpd->vhpi = regs->vpd.vhpi;
885 vpd->vnat = regs->vpd.vnat;
886 vpd->vbnat = regs->vpd.vbnat;
887 vpd->vpsr = regs->vpd.vpsr;
888
889 vpd->vpr = regs->vpd.vpr;
890
Jes Sorensen042b26e2008-12-16 16:45:47 +0100891 memcpy(&vcpu->arch.guest, &regs->saved_guest, sizeof(union context));
Xiantao Zhangb024b792008-04-01 15:29:29 +0800892
893 RESTORE_REGS(mp_state);
894 RESTORE_REGS(vmm_rr);
895 memcpy(vcpu->arch.itrs, regs->itrs, sizeof(struct thash_data) * NITRS);
896 memcpy(vcpu->arch.dtrs, regs->dtrs, sizeof(struct thash_data) * NDTRS);
897 RESTORE_REGS(itr_regions);
898 RESTORE_REGS(dtr_regions);
899 RESTORE_REGS(tc_regions);
900 RESTORE_REGS(irq_check);
901 RESTORE_REGS(itc_check);
902 RESTORE_REGS(timer_check);
903 RESTORE_REGS(timer_pending);
904 RESTORE_REGS(last_itc);
905 for (i = 0; i < 8; i++) {
906 vcpu->arch.vrr[i] = regs->vrr[i];
907 vcpu->arch.ibr[i] = regs->ibr[i];
908 vcpu->arch.dbr[i] = regs->dbr[i];
909 }
910 for (i = 0; i < 4; i++)
911 vcpu->arch.insvc[i] = regs->insvc[i];
912 RESTORE_REGS(xtp);
913 RESTORE_REGS(metaphysical_rr0);
914 RESTORE_REGS(metaphysical_rr4);
915 RESTORE_REGS(metaphysical_saved_rr0);
916 RESTORE_REGS(metaphysical_saved_rr4);
917 RESTORE_REGS(fp_psr);
918 RESTORE_REGS(saved_gp);
919
920 vcpu->arch.irq_new_pending = 1;
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -0600921 vcpu->arch.itc_offset = regs->saved_itc - kvm_get_itc(vcpu);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800922 set_bit(KVM_REQ_RESUME, &vcpu->requests);
923
Jes Sorensen042b26e2008-12-16 16:45:47 +0100924 return 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800925}
926
927long kvm_arch_vm_ioctl(struct file *filp,
928 unsigned int ioctl, unsigned long arg)
929{
930 struct kvm *kvm = filp->private_data;
931 void __user *argp = (void __user *)arg;
Avi Kivity367e1312009-08-26 14:57:07 +0300932 int r = -ENOTTY;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800933
934 switch (ioctl) {
935 case KVM_SET_MEMORY_REGION: {
936 struct kvm_memory_region kvm_mem;
937 struct kvm_userspace_memory_region kvm_userspace_mem;
938
939 r = -EFAULT;
940 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
941 goto out;
942 kvm_userspace_mem.slot = kvm_mem.slot;
943 kvm_userspace_mem.flags = kvm_mem.flags;
944 kvm_userspace_mem.guest_phys_addr =
945 kvm_mem.guest_phys_addr;
946 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
947 r = kvm_vm_ioctl_set_memory_region(kvm,
948 &kvm_userspace_mem, 0);
949 if (r)
950 goto out;
951 break;
952 }
953 case KVM_CREATE_IRQCHIP:
954 r = -EFAULT;
955 r = kvm_ioapic_init(kvm);
956 if (r)
957 goto out;
Avi Kivity399ec802008-11-19 13:58:46 +0200958 r = kvm_setup_default_irq_routing(kvm);
959 if (r) {
Takuya Yoshikawa175504c2010-12-16 01:41:37 +0900960 mutex_lock(&kvm->slots_lock);
Wei Yongjun4b7bb922010-02-09 10:41:56 +0800961 kvm_ioapic_destroy(kvm);
Takuya Yoshikawa175504c2010-12-16 01:41:37 +0900962 mutex_unlock(&kvm->slots_lock);
Avi Kivity399ec802008-11-19 13:58:46 +0200963 goto out;
964 }
Xiantao Zhangb024b792008-04-01 15:29:29 +0800965 break;
Gleb Natapov49256632009-02-04 17:28:14 +0200966 case KVM_IRQ_LINE_STATUS:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800967 case KVM_IRQ_LINE: {
968 struct kvm_irq_level irq_event;
969
970 r = -EFAULT;
971 if (copy_from_user(&irq_event, argp, sizeof irq_event))
972 goto out;
Wei Yongjun600f1ec2010-03-12 10:11:15 +0800973 r = -ENXIO;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800974 if (irqchip_in_kernel(kvm)) {
Gleb Natapov49256632009-02-04 17:28:14 +0200975 __s32 status;
Gleb Natapov49256632009-02-04 17:28:14 +0200976 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Sheng Yang5550af42008-10-15 20:15:06 +0800977 irq_event.irq, irq_event.level);
Gleb Natapov49256632009-02-04 17:28:14 +0200978 if (ioctl == KVM_IRQ_LINE_STATUS) {
Wei Yongjun600f1ec2010-03-12 10:11:15 +0800979 r = -EFAULT;
Gleb Natapov49256632009-02-04 17:28:14 +0200980 irq_event.status = status;
981 if (copy_to_user(argp, &irq_event,
982 sizeof irq_event))
983 goto out;
984 }
Xiantao Zhangb024b792008-04-01 15:29:29 +0800985 r = 0;
986 }
987 break;
988 }
989 case KVM_GET_IRQCHIP: {
990 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
991 struct kvm_irqchip chip;
992
993 r = -EFAULT;
994 if (copy_from_user(&chip, argp, sizeof chip))
995 goto out;
996 r = -ENXIO;
997 if (!irqchip_in_kernel(kvm))
998 goto out;
999 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
1000 if (r)
1001 goto out;
1002 r = -EFAULT;
1003 if (copy_to_user(argp, &chip, sizeof chip))
1004 goto out;
1005 r = 0;
1006 break;
1007 }
1008 case KVM_SET_IRQCHIP: {
1009 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1010 struct kvm_irqchip chip;
1011
1012 r = -EFAULT;
1013 if (copy_from_user(&chip, argp, sizeof chip))
1014 goto out;
1015 r = -ENXIO;
1016 if (!irqchip_in_kernel(kvm))
1017 goto out;
1018 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1019 if (r)
1020 goto out;
1021 r = 0;
1022 break;
1023 }
1024 default:
1025 ;
1026 }
1027out:
1028 return r;
1029}
1030
1031int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1032 struct kvm_sregs *sregs)
1033{
1034 return -EINVAL;
1035}
1036
1037int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1038 struct kvm_sregs *sregs)
1039{
1040 return -EINVAL;
1041
1042}
1043int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1044 struct kvm_translation *tr)
1045{
1046
1047 return -EINVAL;
1048}
1049
1050static int kvm_alloc_vmm_area(void)
1051{
1052 if (!kvm_vmm_base && (kvm_vm_buffer_size < KVM_VM_BUFFER_SIZE)) {
1053 kvm_vmm_base = __get_free_pages(GFP_KERNEL,
1054 get_order(KVM_VMM_SIZE));
1055 if (!kvm_vmm_base)
1056 return -ENOMEM;
1057
1058 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1059 kvm_vm_buffer = kvm_vmm_base + VMM_SIZE;
1060
1061 printk(KERN_DEBUG"kvm:VMM's Base Addr:0x%lx, vm_buffer:0x%lx\n",
1062 kvm_vmm_base, kvm_vm_buffer);
1063 }
1064
1065 return 0;
1066}
1067
1068static void kvm_free_vmm_area(void)
1069{
1070 if (kvm_vmm_base) {
1071 /*Zero this area before free to avoid bits leak!!*/
1072 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1073 free_pages(kvm_vmm_base, get_order(KVM_VMM_SIZE));
1074 kvm_vmm_base = 0;
1075 kvm_vm_buffer = 0;
1076 kvm_vsa_base = 0;
1077 }
1078}
1079
Xiantao Zhangb024b792008-04-01 15:29:29 +08001080static int vti_init_vpd(struct kvm_vcpu *vcpu)
1081{
1082 int i;
1083 union cpuid3_t cpuid3;
1084 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1085
1086 if (IS_ERR(vpd))
1087 return PTR_ERR(vpd);
1088
1089 /* CPUID init */
1090 for (i = 0; i < 5; i++)
1091 vpd->vcpuid[i] = ia64_get_cpuid(i);
1092
1093 /* Limit the CPUID number to 5 */
1094 cpuid3.value = vpd->vcpuid[3];
1095 cpuid3.number = 4; /* 5 - 1 */
1096 vpd->vcpuid[3] = cpuid3.value;
1097
1098 /*Set vac and vdc fields*/
1099 vpd->vac.a_from_int_cr = 1;
1100 vpd->vac.a_to_int_cr = 1;
1101 vpd->vac.a_from_psr = 1;
1102 vpd->vac.a_from_cpuid = 1;
1103 vpd->vac.a_cover = 1;
1104 vpd->vac.a_bsw = 1;
1105 vpd->vac.a_int = 1;
1106 vpd->vdc.d_vmsw = 1;
1107
1108 /*Set virtual buffer*/
1109 vpd->virt_env_vaddr = KVM_VM_BUFFER_BASE;
1110
1111 return 0;
1112}
1113
1114static int vti_create_vp(struct kvm_vcpu *vcpu)
1115{
1116 long ret;
1117 struct vpd *vpd = vcpu->arch.vpd;
1118 unsigned long vmm_ivt;
1119
1120 vmm_ivt = kvm_vmm_info->vmm_ivt;
1121
1122 printk(KERN_DEBUG "kvm: vcpu:%p,ivt: 0x%lx\n", vcpu, vmm_ivt);
1123
1124 ret = ia64_pal_vp_create((u64 *)vpd, (u64 *)vmm_ivt, 0);
1125
1126 if (ret) {
1127 printk(KERN_ERR"kvm: ia64_pal_vp_create failed!\n");
1128 return -EINVAL;
1129 }
1130 return 0;
1131}
1132
1133static void init_ptce_info(struct kvm_vcpu *vcpu)
1134{
1135 ia64_ptce_info_t ptce = {0};
1136
1137 ia64_get_ptce(&ptce);
1138 vcpu->arch.ptce_base = ptce.base;
1139 vcpu->arch.ptce_count[0] = ptce.count[0];
1140 vcpu->arch.ptce_count[1] = ptce.count[1];
1141 vcpu->arch.ptce_stride[0] = ptce.stride[0];
1142 vcpu->arch.ptce_stride[1] = ptce.stride[1];
1143}
1144
1145static void kvm_migrate_hlt_timer(struct kvm_vcpu *vcpu)
1146{
1147 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
1148
1149 if (hrtimer_cancel(p_ht))
Arjan van de Ven18dd36a2008-09-01 15:19:11 -07001150 hrtimer_start_expires(p_ht, HRTIMER_MODE_ABS);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001151}
1152
1153static enum hrtimer_restart hlt_timer_fn(struct hrtimer *data)
1154{
1155 struct kvm_vcpu *vcpu;
1156 wait_queue_head_t *q;
1157
1158 vcpu = container_of(data, struct kvm_vcpu, arch.hlt_timer);
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001159 q = &vcpu->wq;
1160
Avi Kivitya4535292008-04-13 17:54:35 +03001161 if (vcpu->arch.mp_state != KVM_MP_STATE_HALTED)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001162 goto out;
1163
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001164 if (waitqueue_active(q))
Xiantao Zhangb024b792008-04-01 15:29:29 +08001165 wake_up_interruptible(q);
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001166
Xiantao Zhangb024b792008-04-01 15:29:29 +08001167out:
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001168 vcpu->arch.timer_fired = 1;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001169 vcpu->arch.timer_check = 1;
1170 return HRTIMER_NORESTART;
1171}
1172
1173#define PALE_RESET_ENTRY 0x80000000ffffffb0UL
1174
Avi Kivity3e515702012-03-05 14:23:29 +02001175bool kvm_vcpu_compatible(struct kvm_vcpu *vcpu)
1176{
Avi Kivity331b6462012-04-18 19:23:50 +03001177 return irqchip_in_kernel(vcpu->kvm) == (vcpu->arch.apic != NULL);
Avi Kivity3e515702012-03-05 14:23:29 +02001178}
1179
Xiantao Zhangb024b792008-04-01 15:29:29 +08001180int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1181{
1182 struct kvm_vcpu *v;
1183 int r;
1184 int i;
1185 long itc_offset;
1186 struct kvm *kvm = vcpu->kvm;
1187 struct kvm_pt_regs *regs = vcpu_regs(vcpu);
1188
1189 union context *p_ctx = &vcpu->arch.guest;
1190 struct kvm_vcpu *vmm_vcpu = to_guest(vcpu->kvm, vcpu);
1191
1192 /*Init vcpu context for first run.*/
1193 if (IS_ERR(vmm_vcpu))
1194 return PTR_ERR(vmm_vcpu);
1195
Gleb Natapovc5af89b2009-06-09 15:56:26 +03001196 if (kvm_vcpu_is_bsp(vcpu)) {
Avi Kivitya4535292008-04-13 17:54:35 +03001197 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001198
1199 /*Set entry address for first run.*/
1200 regs->cr_iip = PALE_RESET_ENTRY;
1201
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001202 /*Initialize itc offset for vcpus*/
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -06001203 itc_offset = 0UL - kvm_get_itc(vcpu);
Jes Sorensen3032b922009-05-25 10:22:17 +02001204 for (i = 0; i < KVM_MAX_VCPUS; i++) {
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001205 v = (struct kvm_vcpu *)((char *)vcpu +
1206 sizeof(struct kvm_vcpu_data) * i);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001207 v->arch.itc_offset = itc_offset;
1208 v->arch.last_itc = 0;
1209 }
1210 } else
Avi Kivitya4535292008-04-13 17:54:35 +03001211 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001212
1213 r = -ENOMEM;
1214 vcpu->arch.apic = kzalloc(sizeof(struct kvm_lapic), GFP_KERNEL);
1215 if (!vcpu->arch.apic)
1216 goto out;
1217 vcpu->arch.apic->vcpu = vcpu;
1218
1219 p_ctx->gr[1] = 0;
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001220 p_ctx->gr[12] = (unsigned long)((char *)vmm_vcpu + KVM_STK_OFFSET);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001221 p_ctx->gr[13] = (unsigned long)vmm_vcpu;
1222 p_ctx->psr = 0x1008522000UL;
1223 p_ctx->ar[40] = FPSR_DEFAULT; /*fpsr*/
1224 p_ctx->caller_unat = 0;
1225 p_ctx->pr = 0x0;
1226 p_ctx->ar[36] = 0x0; /*unat*/
1227 p_ctx->ar[19] = 0x0; /*rnat*/
1228 p_ctx->ar[18] = (unsigned long)vmm_vcpu +
1229 ((sizeof(struct kvm_vcpu)+15) & ~15);
1230 p_ctx->ar[64] = 0x0; /*pfs*/
1231 p_ctx->cr[0] = 0x7e04UL;
1232 p_ctx->cr[2] = (unsigned long)kvm_vmm_info->vmm_ivt;
1233 p_ctx->cr[8] = 0x3c;
1234
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001235 /*Initialize region register*/
Xiantao Zhangb024b792008-04-01 15:29:29 +08001236 p_ctx->rr[0] = 0x30;
1237 p_ctx->rr[1] = 0x30;
1238 p_ctx->rr[2] = 0x30;
1239 p_ctx->rr[3] = 0x30;
1240 p_ctx->rr[4] = 0x30;
1241 p_ctx->rr[5] = 0x30;
1242 p_ctx->rr[7] = 0x30;
1243
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001244 /*Initialize branch register 0*/
Xiantao Zhangb024b792008-04-01 15:29:29 +08001245 p_ctx->br[0] = *(unsigned long *)kvm_vmm_info->vmm_entry;
1246
1247 vcpu->arch.vmm_rr = kvm->arch.vmm_init_rr;
1248 vcpu->arch.metaphysical_rr0 = kvm->arch.metaphysical_rr0;
1249 vcpu->arch.metaphysical_rr4 = kvm->arch.metaphysical_rr4;
1250
1251 hrtimer_init(&vcpu->arch.hlt_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1252 vcpu->arch.hlt_timer.function = hlt_timer_fn;
1253
1254 vcpu->arch.last_run_cpu = -1;
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001255 vcpu->arch.vpd = (struct vpd *)VPD_BASE(vcpu->vcpu_id);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001256 vcpu->arch.vsa_base = kvm_vsa_base;
1257 vcpu->arch.__gp = kvm_vmm_gp;
1258 vcpu->arch.dirty_log_lock_pa = __pa(&kvm->arch.dirty_log_lock);
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001259 vcpu->arch.vhpt.hash = (struct thash_data *)VHPT_BASE(vcpu->vcpu_id);
1260 vcpu->arch.vtlb.hash = (struct thash_data *)VTLB_BASE(vcpu->vcpu_id);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001261 init_ptce_info(vcpu);
1262
1263 r = 0;
1264out:
1265 return r;
1266}
1267
1268static int vti_vcpu_setup(struct kvm_vcpu *vcpu, int id)
1269{
1270 unsigned long psr;
1271 int r;
1272
1273 local_irq_save(psr);
1274 r = kvm_insert_vmm_mapping(vcpu);
Jes Sorensen457459c2009-04-16 16:08:29 +02001275 local_irq_restore(psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001276 if (r)
1277 goto fail;
1278 r = kvm_vcpu_init(vcpu, vcpu->kvm, id);
1279 if (r)
1280 goto fail;
1281
1282 r = vti_init_vpd(vcpu);
1283 if (r) {
1284 printk(KERN_DEBUG"kvm: vpd init error!!\n");
1285 goto uninit;
1286 }
1287
1288 r = vti_create_vp(vcpu);
1289 if (r)
1290 goto uninit;
1291
1292 kvm_purge_vmm_mapping(vcpu);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001293
1294 return 0;
1295uninit:
1296 kvm_vcpu_uninit(vcpu);
1297fail:
1298 return r;
1299}
1300
1301struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1302 unsigned int id)
1303{
1304 struct kvm_vcpu *vcpu;
1305 unsigned long vm_base = kvm->arch.vm_base;
1306 int r;
1307 int cpu;
1308
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001309 BUG_ON(sizeof(struct kvm_vcpu) > VCPU_STRUCT_SIZE/2);
1310
1311 r = -EINVAL;
1312 if (id >= KVM_MAX_VCPUS) {
1313 printk(KERN_ERR"kvm: Can't configure vcpus > %ld",
1314 KVM_MAX_VCPUS);
1315 goto fail;
1316 }
1317
Xiantao Zhangb024b792008-04-01 15:29:29 +08001318 r = -ENOMEM;
1319 if (!vm_base) {
1320 printk(KERN_ERR"kvm: Create vcpu[%d] error!\n", id);
1321 goto fail;
1322 }
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001323 vcpu = (struct kvm_vcpu *)(vm_base + offsetof(struct kvm_vm_data,
1324 vcpu_data[id].vcpu_struct));
Xiantao Zhangb024b792008-04-01 15:29:29 +08001325 vcpu->kvm = kvm;
1326
1327 cpu = get_cpu();
Xiantao Zhangb024b792008-04-01 15:29:29 +08001328 r = vti_vcpu_setup(vcpu, id);
1329 put_cpu();
1330
1331 if (r) {
1332 printk(KERN_DEBUG"kvm: vcpu_setup error!!\n");
1333 goto fail;
1334 }
1335
1336 return vcpu;
1337fail:
1338 return ERR_PTR(r);
1339}
1340
1341int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1342{
1343 return 0;
1344}
1345
1346int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1347{
1348 return -EINVAL;
1349}
1350
1351int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1352{
1353 return -EINVAL;
1354}
1355
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001356int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1357 struct kvm_guest_debug *dbg)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001358{
1359 return -EINVAL;
1360}
1361
Jan Kiszkad89f5ef2010-11-09 17:02:49 +01001362void kvm_arch_free_vm(struct kvm *kvm)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001363{
1364 unsigned long vm_base = kvm->arch.vm_base;
1365
1366 if (vm_base) {
1367 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
1368 free_pages(vm_base, get_order(KVM_VM_DATA_SIZE));
1369 }
1370
1371}
1372
1373static void kvm_release_vm_pages(struct kvm *kvm)
1374{
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001375 struct kvm_memslots *slots;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001376 struct kvm_memory_slot *memslot;
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +08001377 int j;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001378 unsigned long base_gfn;
1379
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08001380 slots = kvm_memslots(kvm);
Xiao Guangrongbe6ba0f2011-11-24 17:39:18 +08001381 kvm_for_each_memslot(memslot, slots) {
Xiantao Zhangb024b792008-04-01 15:29:29 +08001382 base_gfn = memslot->base_gfn;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001383 for (j = 0; j < memslot->npages; j++) {
1384 if (memslot->rmap[j])
1385 put_page((struct page *)memslot->rmap[j]);
1386 }
1387 }
1388}
1389
Sheng Yangad8ba2c2009-01-06 10:03:02 +08001390void kvm_arch_sync_events(struct kvm *kvm)
1391{
1392}
1393
Xiantao Zhangb024b792008-04-01 15:29:29 +08001394void kvm_arch_destroy_vm(struct kvm *kvm)
1395{
Xiantao Zhang2381ad22008-10-08 08:29:33 +08001396 kvm_iommu_unmap_guest(kvm);
1397#ifdef KVM_CAP_DEVICE_ASSIGNMENT
1398 kvm_free_all_assigned_devices(kvm);
1399#endif
Xiantao Zhangb024b792008-04-01 15:29:29 +08001400 kfree(kvm->arch.vioapic);
1401 kvm_release_vm_pages(kvm);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001402}
1403
1404void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1405{
1406}
1407
1408void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1409{
1410 if (cpu != vcpu->cpu) {
1411 vcpu->cpu = cpu;
1412 if (vcpu->arch.ht_active)
1413 kvm_migrate_hlt_timer(vcpu);
1414 }
1415}
1416
1417#define SAVE_REGS(_x) regs->_x = vcpu->arch._x
1418
1419int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1420{
Xiantao Zhangb024b792008-04-01 15:29:29 +08001421 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
Jes Sorensen042b26e2008-12-16 16:45:47 +01001422 int i;
1423
Xiantao Zhangb024b792008-04-01 15:29:29 +08001424 vcpu_load(vcpu);
1425
1426 for (i = 0; i < 16; i++) {
1427 regs->vpd.vgr[i] = vpd->vgr[i];
1428 regs->vpd.vbgr[i] = vpd->vbgr[i];
1429 }
1430 for (i = 0; i < 128; i++)
1431 regs->vpd.vcr[i] = vpd->vcr[i];
1432 regs->vpd.vhpi = vpd->vhpi;
1433 regs->vpd.vnat = vpd->vnat;
1434 regs->vpd.vbnat = vpd->vbnat;
1435 regs->vpd.vpsr = vpd->vpsr;
1436 regs->vpd.vpr = vpd->vpr;
1437
Jes Sorensen042b26e2008-12-16 16:45:47 +01001438 memcpy(&regs->saved_guest, &vcpu->arch.guest, sizeof(union context));
1439
Xiantao Zhangb024b792008-04-01 15:29:29 +08001440 SAVE_REGS(mp_state);
1441 SAVE_REGS(vmm_rr);
1442 memcpy(regs->itrs, vcpu->arch.itrs, sizeof(struct thash_data) * NITRS);
1443 memcpy(regs->dtrs, vcpu->arch.dtrs, sizeof(struct thash_data) * NDTRS);
1444 SAVE_REGS(itr_regions);
1445 SAVE_REGS(dtr_regions);
1446 SAVE_REGS(tc_regions);
1447 SAVE_REGS(irq_check);
1448 SAVE_REGS(itc_check);
1449 SAVE_REGS(timer_check);
1450 SAVE_REGS(timer_pending);
1451 SAVE_REGS(last_itc);
1452 for (i = 0; i < 8; i++) {
1453 regs->vrr[i] = vcpu->arch.vrr[i];
1454 regs->ibr[i] = vcpu->arch.ibr[i];
1455 regs->dbr[i] = vcpu->arch.dbr[i];
1456 }
1457 for (i = 0; i < 4; i++)
1458 regs->insvc[i] = vcpu->arch.insvc[i];
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -06001459 regs->saved_itc = vcpu->arch.itc_offset + kvm_get_itc(vcpu);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001460 SAVE_REGS(xtp);
1461 SAVE_REGS(metaphysical_rr0);
1462 SAVE_REGS(metaphysical_rr4);
1463 SAVE_REGS(metaphysical_saved_rr0);
1464 SAVE_REGS(metaphysical_saved_rr4);
1465 SAVE_REGS(fp_psr);
1466 SAVE_REGS(saved_gp);
Jes Sorensen042b26e2008-12-16 16:45:47 +01001467
Xiantao Zhangb024b792008-04-01 15:29:29 +08001468 vcpu_put(vcpu);
Jes Sorensen042b26e2008-12-16 16:45:47 +01001469 return 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001470}
1471
Jes Sorensene9a999f2008-12-18 12:17:51 +01001472int kvm_arch_vcpu_ioctl_get_stack(struct kvm_vcpu *vcpu,
1473 struct kvm_ia64_vcpu_stack *stack)
1474{
1475 memcpy(stack, vcpu, sizeof(struct kvm_ia64_vcpu_stack));
1476 return 0;
1477}
1478
1479int kvm_arch_vcpu_ioctl_set_stack(struct kvm_vcpu *vcpu,
1480 struct kvm_ia64_vcpu_stack *stack)
1481{
1482 memcpy(vcpu + 1, &stack->stack[0] + sizeof(struct kvm_vcpu),
1483 sizeof(struct kvm_ia64_vcpu_stack) - sizeof(struct kvm_vcpu));
1484
1485 vcpu->arch.exit_data = ((struct kvm_vcpu *)stack)->arch.exit_data;
1486 return 0;
1487}
1488
Xiantao Zhangb024b792008-04-01 15:29:29 +08001489void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1490{
1491
1492 hrtimer_cancel(&vcpu->arch.hlt_timer);
1493 kfree(vcpu->arch.apic);
1494}
1495
1496
1497long kvm_arch_vcpu_ioctl(struct file *filp,
Jes Sorensene9a999f2008-12-18 12:17:51 +01001498 unsigned int ioctl, unsigned long arg)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001499{
Jes Sorensene9a999f2008-12-18 12:17:51 +01001500 struct kvm_vcpu *vcpu = filp->private_data;
1501 void __user *argp = (void __user *)arg;
1502 struct kvm_ia64_vcpu_stack *stack = NULL;
1503 long r;
1504
1505 switch (ioctl) {
1506 case KVM_IA64_VCPU_GET_STACK: {
1507 struct kvm_ia64_vcpu_stack __user *user_stack;
1508 void __user *first_p = argp;
1509
1510 r = -EFAULT;
1511 if (copy_from_user(&user_stack, first_p, sizeof(void *)))
1512 goto out;
1513
1514 if (!access_ok(VERIFY_WRITE, user_stack,
1515 sizeof(struct kvm_ia64_vcpu_stack))) {
1516 printk(KERN_INFO "KVM_IA64_VCPU_GET_STACK: "
1517 "Illegal user destination address for stack\n");
1518 goto out;
1519 }
1520 stack = kzalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL);
1521 if (!stack) {
1522 r = -ENOMEM;
1523 goto out;
1524 }
1525
1526 r = kvm_arch_vcpu_ioctl_get_stack(vcpu, stack);
1527 if (r)
1528 goto out;
1529
1530 if (copy_to_user(user_stack, stack,
Wei Yongjun5c0d0922010-03-12 08:45:39 +08001531 sizeof(struct kvm_ia64_vcpu_stack))) {
1532 r = -EFAULT;
Jes Sorensene9a999f2008-12-18 12:17:51 +01001533 goto out;
Wei Yongjun5c0d0922010-03-12 08:45:39 +08001534 }
Jes Sorensene9a999f2008-12-18 12:17:51 +01001535
1536 break;
1537 }
1538 case KVM_IA64_VCPU_SET_STACK: {
1539 struct kvm_ia64_vcpu_stack __user *user_stack;
1540 void __user *first_p = argp;
1541
1542 r = -EFAULT;
1543 if (copy_from_user(&user_stack, first_p, sizeof(void *)))
1544 goto out;
1545
1546 if (!access_ok(VERIFY_READ, user_stack,
1547 sizeof(struct kvm_ia64_vcpu_stack))) {
1548 printk(KERN_INFO "KVM_IA64_VCPU_SET_STACK: "
1549 "Illegal user address for stack\n");
1550 goto out;
1551 }
1552 stack = kmalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL);
1553 if (!stack) {
1554 r = -ENOMEM;
1555 goto out;
1556 }
1557 if (copy_from_user(stack, user_stack,
1558 sizeof(struct kvm_ia64_vcpu_stack)))
1559 goto out;
1560
1561 r = kvm_arch_vcpu_ioctl_set_stack(vcpu, stack);
1562 break;
1563 }
1564
1565 default:
1566 r = -EINVAL;
1567 }
1568
1569out:
1570 kfree(stack);
1571 return r;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001572}
1573
Carsten Otte5b1c1492012-01-04 10:25:23 +01001574int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
1575{
1576 return VM_FAULT_SIGBUS;
1577}
1578
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +09001579void kvm_arch_free_memslot(struct kvm_memory_slot *free,
1580 struct kvm_memory_slot *dont)
1581{
1582}
1583
1584int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
1585{
1586 return 0;
1587}
1588
Marcelo Tosattif7784b82009-12-23 14:35:18 -02001589int kvm_arch_prepare_memory_region(struct kvm *kvm,
1590 struct kvm_memory_slot *memslot,
Xiantao Zhangb024b792008-04-01 15:29:29 +08001591 struct kvm_memory_slot old,
Marcelo Tosattif7784b82009-12-23 14:35:18 -02001592 struct kvm_userspace_memory_region *mem,
Xiantao Zhangb024b792008-04-01 15:29:29 +08001593 int user_alloc)
1594{
1595 unsigned long i;
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001596 unsigned long pfn;
Marcelo Tosattif7784b82009-12-23 14:35:18 -02001597 int npages = memslot->npages;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001598 unsigned long base_gfn = memslot->base_gfn;
1599
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001600 if (base_gfn + npages > (KVM_MAX_MEM_SIZE >> PAGE_SHIFT))
1601 return -ENOMEM;
1602
Xiantao Zhangb024b792008-04-01 15:29:29 +08001603 for (i = 0; i < npages; i++) {
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001604 pfn = gfn_to_pfn(kvm, base_gfn + i);
1605 if (!kvm_is_mmio_pfn(pfn)) {
1606 kvm_set_pmt_entry(kvm, base_gfn + i,
1607 pfn << PAGE_SHIFT,
Xiantao Zhangb010eb52008-09-28 01:39:46 -07001608 _PAGE_AR_RWX | _PAGE_MA_WB);
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001609 memslot->rmap[i] = (unsigned long)pfn_to_page(pfn);
1610 } else {
1611 kvm_set_pmt_entry(kvm, base_gfn + i,
Xiantao Zhangb010eb52008-09-28 01:39:46 -07001612 GPFN_PHYS_MMIO | (pfn << PAGE_SHIFT),
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001613 _PAGE_MA_UC);
1614 memslot->rmap[i] = 0;
1615 }
Xiantao Zhangb024b792008-04-01 15:29:29 +08001616 }
1617
1618 return 0;
1619}
1620
Marcelo Tosattif7784b82009-12-23 14:35:18 -02001621void kvm_arch_commit_memory_region(struct kvm *kvm,
1622 struct kvm_userspace_memory_region *mem,
1623 struct kvm_memory_slot old,
1624 int user_alloc)
1625{
1626 return;
1627}
1628
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001629void kvm_arch_flush_shadow(struct kvm *kvm)
1630{
Xiantao Zhang64f6afb2009-04-16 17:59:16 +08001631 kvm_flush_remote_tlbs(kvm);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001632}
Xiantao Zhangb024b792008-04-01 15:29:29 +08001633
1634long kvm_arch_dev_ioctl(struct file *filp,
Jes Sorensene9a999f2008-12-18 12:17:51 +01001635 unsigned int ioctl, unsigned long arg)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001636{
1637 return -EINVAL;
1638}
1639
1640void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1641{
1642 kvm_vcpu_uninit(vcpu);
1643}
1644
1645static int vti_cpu_has_kvm_support(void)
1646{
1647 long avail = 1, status = 1, control = 1;
1648 long ret;
1649
1650 ret = ia64_pal_proc_get_features(&avail, &status, &control, 0);
1651 if (ret)
1652 goto out;
1653
1654 if (!(avail & PAL_PROC_VM_BIT))
1655 goto out;
1656
1657 printk(KERN_DEBUG"kvm: Hardware Supports VT\n");
1658
1659 ret = ia64_pal_vp_env_info(&kvm_vm_buffer_size, &vp_env_info);
1660 if (ret)
1661 goto out;
1662 printk(KERN_DEBUG"kvm: VM Buffer Size:0x%lx\n", kvm_vm_buffer_size);
1663
1664 if (!(vp_env_info & VP_OPCODE)) {
1665 printk(KERN_WARNING"kvm: No opcode ability on hardware, "
1666 "vm_env_info:0x%lx\n", vp_env_info);
1667 }
1668
1669 return 1;
1670out:
1671 return 0;
1672}
1673
Jes Sorensen0b5d7a22009-02-25 10:38:55 -06001674
1675/*
1676 * On SN2, the ITC isn't stable, so copy in fast path code to use the
1677 * SN2 RTC, replacing the ITC based default verion.
1678 */
1679static void kvm_patch_vmm(struct kvm_vmm_info *vmm_info,
1680 struct module *module)
1681{
1682 unsigned long new_ar, new_ar_sn2;
1683 unsigned long module_base;
1684
1685 if (!ia64_platform_is("sn2"))
1686 return;
1687
1688 module_base = (unsigned long)module->module_core;
1689
1690 new_ar = kvm_vmm_base + vmm_info->patch_mov_ar - module_base;
1691 new_ar_sn2 = kvm_vmm_base + vmm_info->patch_mov_ar_sn2 - module_base;
1692
1693 printk(KERN_INFO "kvm: Patching ITC emulation to use SGI SN2 RTC "
1694 "as source\n");
1695
1696 /*
1697 * Copy the SN2 version of mov_ar into place. They are both
1698 * the same size, so 6 bundles is sufficient (6 * 0x10).
1699 */
1700 memcpy((void *)new_ar, (void *)new_ar_sn2, 0x60);
1701}
1702
Xiantao Zhangb024b792008-04-01 15:29:29 +08001703static int kvm_relocate_vmm(struct kvm_vmm_info *vmm_info,
Jes Sorensen0b5d7a22009-02-25 10:38:55 -06001704 struct module *module)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001705{
1706 unsigned long module_base;
1707 unsigned long vmm_size;
1708
1709 unsigned long vmm_offset, func_offset, fdesc_offset;
1710 struct fdesc *p_fdesc;
1711
1712 BUG_ON(!module);
1713
1714 if (!kvm_vmm_base) {
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001715 printk("kvm: kvm area hasn't been initialized yet!!\n");
Xiantao Zhangb024b792008-04-01 15:29:29 +08001716 return -EFAULT;
1717 }
1718
1719 /*Calculate new position of relocated vmm module.*/
1720 module_base = (unsigned long)module->module_core;
1721 vmm_size = module->core_size;
1722 if (unlikely(vmm_size > KVM_VMM_SIZE))
1723 return -EFAULT;
1724
1725 memcpy((void *)kvm_vmm_base, (void *)module_base, vmm_size);
Jes Sorensen0b5d7a22009-02-25 10:38:55 -06001726 kvm_patch_vmm(vmm_info, module);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001727 kvm_flush_icache(kvm_vmm_base, vmm_size);
1728
1729 /*Recalculate kvm_vmm_info based on new VMM*/
1730 vmm_offset = vmm_info->vmm_ivt - module_base;
1731 kvm_vmm_info->vmm_ivt = KVM_VMM_BASE + vmm_offset;
1732 printk(KERN_DEBUG"kvm: Relocated VMM's IVT Base Addr:%lx\n",
1733 kvm_vmm_info->vmm_ivt);
1734
1735 fdesc_offset = (unsigned long)vmm_info->vmm_entry - module_base;
1736 kvm_vmm_info->vmm_entry = (kvm_vmm_entry *)(KVM_VMM_BASE +
1737 fdesc_offset);
1738 func_offset = *(unsigned long *)vmm_info->vmm_entry - module_base;
1739 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1740 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1741 p_fdesc->gp = KVM_VMM_BASE+(p_fdesc->gp - module_base);
1742
1743 printk(KERN_DEBUG"kvm: Relocated VMM's Init Entry Addr:%lx\n",
1744 KVM_VMM_BASE+func_offset);
1745
1746 fdesc_offset = (unsigned long)vmm_info->tramp_entry - module_base;
1747 kvm_vmm_info->tramp_entry = (kvm_tramp_entry *)(KVM_VMM_BASE +
1748 fdesc_offset);
1749 func_offset = *(unsigned long *)vmm_info->tramp_entry - module_base;
1750 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1751 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1752 p_fdesc->gp = KVM_VMM_BASE + (p_fdesc->gp - module_base);
1753
1754 kvm_vmm_gp = p_fdesc->gp;
1755
1756 printk(KERN_DEBUG"kvm: Relocated VMM's Entry IP:%p\n",
1757 kvm_vmm_info->vmm_entry);
1758 printk(KERN_DEBUG"kvm: Relocated VMM's Trampoline Entry IP:0x%lx\n",
1759 KVM_VMM_BASE + func_offset);
1760
1761 return 0;
1762}
1763
1764int kvm_arch_init(void *opaque)
1765{
1766 int r;
1767 struct kvm_vmm_info *vmm_info = (struct kvm_vmm_info *)opaque;
1768
1769 if (!vti_cpu_has_kvm_support()) {
1770 printk(KERN_ERR "kvm: No Hardware Virtualization Support!\n");
1771 r = -EOPNOTSUPP;
1772 goto out;
1773 }
1774
1775 if (kvm_vmm_info) {
1776 printk(KERN_ERR "kvm: Already loaded VMM module!\n");
1777 r = -EEXIST;
1778 goto out;
1779 }
1780
1781 r = -ENOMEM;
1782 kvm_vmm_info = kzalloc(sizeof(struct kvm_vmm_info), GFP_KERNEL);
1783 if (!kvm_vmm_info)
1784 goto out;
1785
1786 if (kvm_alloc_vmm_area())
1787 goto out_free0;
1788
1789 r = kvm_relocate_vmm(vmm_info, vmm_info->module);
1790 if (r)
1791 goto out_free1;
1792
1793 return 0;
1794
1795out_free1:
1796 kvm_free_vmm_area();
1797out_free0:
1798 kfree(kvm_vmm_info);
1799out:
1800 return r;
1801}
1802
1803void kvm_arch_exit(void)
1804{
1805 kvm_free_vmm_area();
1806 kfree(kvm_vmm_info);
1807 kvm_vmm_info = NULL;
1808}
1809
Takuya Yoshikawa979586e2010-06-23 15:00:29 +09001810static void kvm_ia64_sync_dirty_log(struct kvm *kvm,
1811 struct kvm_memory_slot *memslot)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001812{
Takuya Yoshikawa979586e2010-06-23 15:00:29 +09001813 int i;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001814 long base;
1815 unsigned long n;
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001816 unsigned long *dirty_bitmap = (unsigned long *)(kvm->arch.vm_base +
1817 offsetof(struct kvm_vm_data, kvm_mem_dirty_log));
Xiantao Zhangb024b792008-04-01 15:29:29 +08001818
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001819 n = kvm_dirty_bitmap_bytes(memslot);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001820 base = memslot->base_gfn / BITS_PER_LONG;
1821
Takuya Yoshikawa4482b062010-06-23 14:59:29 +09001822 spin_lock(&kvm->arch.dirty_log_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001823 for (i = 0; i < n/sizeof(long); ++i) {
1824 memslot->dirty_bitmap[i] = dirty_bitmap[base + i];
1825 dirty_bitmap[base + i] = 0;
1826 }
Takuya Yoshikawa4482b062010-06-23 14:59:29 +09001827 spin_unlock(&kvm->arch.dirty_log_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001828}
1829
1830int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1831 struct kvm_dirty_log *log)
1832{
1833 int r;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001834 unsigned long n;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001835 struct kvm_memory_slot *memslot;
1836 int is_dirty = 0;
1837
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001838 mutex_lock(&kvm->slots_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001839
Takuya Yoshikawa979586e2010-06-23 15:00:29 +09001840 r = -EINVAL;
1841 if (log->slot >= KVM_MEMORY_SLOTS)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001842 goto out;
1843
Xiao Guangrong28a37542011-11-24 19:04:35 +08001844 memslot = id_to_memslot(kvm->memslots, log->slot);
Takuya Yoshikawa979586e2010-06-23 15:00:29 +09001845 r = -ENOENT;
1846 if (!memslot->dirty_bitmap)
1847 goto out;
1848
1849 kvm_ia64_sync_dirty_log(kvm, memslot);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001850 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1851 if (r)
1852 goto out;
1853
1854 /* If nothing is dirty, don't bother messing with page tables. */
1855 if (is_dirty) {
1856 kvm_flush_remote_tlbs(kvm);
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001857 n = kvm_dirty_bitmap_bytes(memslot);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001858 memset(memslot->dirty_bitmap, 0, n);
1859 }
1860 r = 0;
1861out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001862 mutex_unlock(&kvm->slots_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001863 return r;
1864}
1865
1866int kvm_arch_hardware_setup(void)
1867{
1868 return 0;
1869}
1870
1871void kvm_arch_hardware_unsetup(void)
1872{
1873}
1874
Xiantao Zhangb024b792008-04-01 15:29:29 +08001875void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1876{
Marcelo Tosatti32f88402009-05-07 17:55:12 -03001877 int me;
1878 int cpu = vcpu->cpu;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001879
1880 if (waitqueue_active(&vcpu->wq))
1881 wake_up_interruptible(&vcpu->wq);
1882
Marcelo Tosatti32f88402009-05-07 17:55:12 -03001883 me = get_cpu();
1884 if (cpu != me && (unsigned) cpu < nr_cpu_ids && cpu_online(cpu))
1885 if (!test_and_set_bit(KVM_REQ_KICK, &vcpu->requests))
1886 smp_send_reschedule(cpu);
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001887 put_cpu();
Xiantao Zhangb024b792008-04-01 15:29:29 +08001888}
1889
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001890int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001891{
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001892 return __apic_accept_irq(vcpu, irq->vector);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001893}
1894
1895int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
1896{
1897 return apic->vcpu->vcpu_id == dest;
1898}
1899
1900int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
1901{
1902 return 0;
1903}
1904
Gleb Natapove1035712009-03-05 16:34:59 +02001905int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001906{
Gleb Natapove1035712009-03-05 16:34:59 +02001907 return vcpu1->arch.xtp - vcpu2->arch.xtp;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001908}
1909
Gleb Natapov343f94f2009-03-05 16:34:54 +02001910int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
1911 int short_hand, int dest, int dest_mode)
1912{
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001913 struct kvm_lapic *target = vcpu->arch.apic;
Gleb Natapov343f94f2009-03-05 16:34:54 +02001914 return (dest_mode == 0) ?
1915 kvm_apic_match_physical_addr(target, dest) :
1916 kvm_apic_match_logical_addr(target, dest);
1917}
1918
Xiantao Zhangb024b792008-04-01 15:29:29 +08001919static int find_highest_bits(int *dat)
1920{
1921 u32 bits, bitnum;
1922 int i;
1923
1924 /* loop for all 256 bits */
1925 for (i = 7; i >= 0 ; i--) {
1926 bits = dat[i];
1927 if (bits) {
1928 bitnum = fls(bits);
1929 return i * 32 + bitnum - 1;
1930 }
1931 }
1932
1933 return -1;
1934}
1935
1936int kvm_highest_pending_irq(struct kvm_vcpu *vcpu)
1937{
1938 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1939
1940 if (vpd->irr[0] & (1UL << NMI_VECTOR))
1941 return NMI_VECTOR;
1942 if (vpd->irr[0] & (1UL << ExtINT_VECTOR))
1943 return ExtINT_VECTOR;
1944
1945 return find_highest_bits((int *)&vpd->irr[0]);
1946}
1947
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001948int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1949{
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001950 return vcpu->arch.timer_fired;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001951}
1952
Xiantao Zhangb024b792008-04-01 15:29:29 +08001953int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1954{
Gleb Natapova1b37102009-07-09 15:33:52 +03001955 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE) ||
1956 (kvm_highest_pending_irq(vcpu) != -1);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001957}
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001958
1959int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1960 struct kvm_mp_state *mp_state)
1961{
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001962 mp_state->mp_state = vcpu->arch.mp_state;
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001963 return 0;
1964}
1965
1966static int vcpu_reset(struct kvm_vcpu *vcpu)
1967{
1968 int r;
1969 long psr;
1970 local_irq_save(psr);
1971 r = kvm_insert_vmm_mapping(vcpu);
Jes Sorensen43890ae2009-04-17 16:43:27 +02001972 local_irq_restore(psr);
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001973 if (r)
1974 goto fail;
1975
1976 vcpu->arch.launched = 0;
1977 kvm_arch_vcpu_uninit(vcpu);
1978 r = kvm_arch_vcpu_init(vcpu);
1979 if (r)
1980 goto fail;
1981
1982 kvm_purge_vmm_mapping(vcpu);
1983 r = 0;
1984fail:
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001985 return r;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001986}
1987
1988int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1989 struct kvm_mp_state *mp_state)
1990{
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001991 int r = 0;
1992
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001993 vcpu->arch.mp_state = mp_state->mp_state;
1994 if (vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)
1995 r = vcpu_reset(vcpu);
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001996 return r;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001997}