blob: 4623a90e515abdab4d181df04cb8f7f0aee8b169 [file] [log] [blame]
Xiantao Zhangb024b792008-04-01 15:29:29 +08001/*
2 * kvm_ia64.c: Basic KVM suppport On Itanium series processors
3 *
4 *
5 * Copyright (C) 2007, Intel Corporation.
6 * Xiantao Zhang (xiantao.zhang@intel.com)
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place - Suite 330, Boston, MA 02111-1307 USA.
20 *
21 */
22
23#include <linux/module.h>
24#include <linux/errno.h>
25#include <linux/percpu.h>
26#include <linux/gfp.h>
27#include <linux/fs.h>
28#include <linux/smp.h>
29#include <linux/kvm_host.h>
30#include <linux/kvm.h>
31#include <linux/bitops.h>
32#include <linux/hrtimer.h>
33#include <linux/uaccess.h>
Joerg Roedel19de40a2008-12-03 14:43:34 +010034#include <linux/iommu.h>
Xiantao Zhang2381ad22008-10-08 08:29:33 +080035#include <linux/intel-iommu.h>
Xiantao Zhangb024b792008-04-01 15:29:29 +080036
37#include <asm/pgtable.h>
38#include <asm/gcc_intrin.h>
39#include <asm/pal.h>
40#include <asm/cacheflush.h>
41#include <asm/div64.h>
42#include <asm/tlb.h>
Jes Sorensen9f726322008-09-12 14:12:08 +020043#include <asm/elf.h>
Jes Sorensen0c72ea72009-02-25 10:38:52 -060044#include <asm/sn/addrs.h>
45#include <asm/sn/clksupport.h>
46#include <asm/sn/shub_mmr.h>
Xiantao Zhangb024b792008-04-01 15:29:29 +080047
48#include "misc.h"
49#include "vti.h"
50#include "iodev.h"
51#include "ioapic.h"
52#include "lapic.h"
Xiantao Zhang2f749772008-09-27 11:46:36 +080053#include "irq.h"
Xiantao Zhangb024b792008-04-01 15:29:29 +080054
55static unsigned long kvm_vmm_base;
56static unsigned long kvm_vsa_base;
57static unsigned long kvm_vm_buffer;
58static unsigned long kvm_vm_buffer_size;
59unsigned long kvm_vmm_gp;
60
61static long vp_env_info;
62
63static struct kvm_vmm_info *kvm_vmm_info;
64
65static DEFINE_PER_CPU(struct kvm_vcpu *, last_vcpu);
66
67struct kvm_stats_debugfs_item debugfs_entries[] = {
68 { NULL }
69};
70
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -060071static unsigned long kvm_get_itc(struct kvm_vcpu *vcpu)
72{
73#if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
74 if (vcpu->kvm->arch.is_sn2)
75 return rtc_time();
76 else
77#endif
78 return ia64_getreg(_IA64_REG_AR_ITC);
79}
80
Xiantao Zhangb024b792008-04-01 15:29:29 +080081static void kvm_flush_icache(unsigned long start, unsigned long len)
82{
83 int l;
84
85 for (l = 0; l < (len + 32); l += 32)
Isaku Yamahata71205692009-03-27 15:11:57 +090086 ia64_fc((void *)(start + l));
Xiantao Zhangb024b792008-04-01 15:29:29 +080087
88 ia64_sync_i();
89 ia64_srlz_i();
90}
91
92static void kvm_flush_tlb_all(void)
93{
94 unsigned long i, j, count0, count1, stride0, stride1, addr;
95 long flags;
96
97 addr = local_cpu_data->ptce_base;
98 count0 = local_cpu_data->ptce_count[0];
99 count1 = local_cpu_data->ptce_count[1];
100 stride0 = local_cpu_data->ptce_stride[0];
101 stride1 = local_cpu_data->ptce_stride[1];
102
103 local_irq_save(flags);
104 for (i = 0; i < count0; ++i) {
105 for (j = 0; j < count1; ++j) {
106 ia64_ptce(addr);
107 addr += stride1;
108 }
109 addr += stride0;
110 }
111 local_irq_restore(flags);
112 ia64_srlz_i(); /* srlz.i implies srlz.d */
113}
114
115long ia64_pal_vp_create(u64 *vpd, u64 *host_iva, u64 *opt_handler)
116{
117 struct ia64_pal_retval iprv;
118
119 PAL_CALL_STK(iprv, PAL_VP_CREATE, (u64)vpd, (u64)host_iva,
120 (u64)opt_handler);
121
122 return iprv.status;
123}
124
125static DEFINE_SPINLOCK(vp_lock);
126
127void kvm_arch_hardware_enable(void *garbage)
128{
129 long status;
130 long tmp_base;
131 unsigned long pte;
132 unsigned long saved_psr;
133 int slot;
134
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600135 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
Xiantao Zhangb024b792008-04-01 15:29:29 +0800136 local_irq_save(saved_psr);
137 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
Julia Lawallcab7a1e2008-07-22 21:38:18 +0200138 local_irq_restore(saved_psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800139 if (slot < 0)
140 return;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800141
142 spin_lock(&vp_lock);
143 status = ia64_pal_vp_init_env(kvm_vsa_base ?
144 VP_INIT_ENV : VP_INIT_ENV_INITALIZE,
145 __pa(kvm_vm_buffer), KVM_VM_BUFFER_BASE, &tmp_base);
146 if (status != 0) {
147 printk(KERN_WARNING"kvm: Failed to Enable VT Support!!!!\n");
148 return ;
149 }
150
151 if (!kvm_vsa_base) {
152 kvm_vsa_base = tmp_base;
153 printk(KERN_INFO"kvm: kvm_vsa_base:0x%lx\n", kvm_vsa_base);
154 }
155 spin_unlock(&vp_lock);
156 ia64_ptr_entry(0x3, slot);
157}
158
159void kvm_arch_hardware_disable(void *garbage)
160{
161
162 long status;
163 int slot;
164 unsigned long pte;
165 unsigned long saved_psr;
166 unsigned long host_iva = ia64_getreg(_IA64_REG_CR_IVA);
167
168 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base),
169 PAGE_KERNEL));
170
171 local_irq_save(saved_psr);
172 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
Julia Lawallcab7a1e2008-07-22 21:38:18 +0200173 local_irq_restore(saved_psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800174 if (slot < 0)
175 return;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800176
177 status = ia64_pal_vp_exit_env(host_iva);
178 if (status)
179 printk(KERN_DEBUG"kvm: Failed to disable VT support! :%ld\n",
180 status);
181 ia64_ptr_entry(0x3, slot);
182}
183
184void kvm_arch_check_processor_compat(void *rtn)
185{
186 *(int *)rtn = 0;
187}
188
189int kvm_dev_ioctl_check_extension(long ext)
190{
191
192 int r;
193
194 switch (ext) {
195 case KVM_CAP_IRQCHIP:
Xiantao Zhang8c4b5372008-08-28 09:34:08 +0800196 case KVM_CAP_MP_STATE:
Gleb Natapov49256632009-02-04 17:28:14 +0200197 case KVM_CAP_IRQ_INJECT_STATUS:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800198 r = 1;
199 break;
Laurent Vivier7f39f8a2008-05-30 16:05:57 +0200200 case KVM_CAP_COALESCED_MMIO:
201 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
202 break;
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800203 case KVM_CAP_IOMMU:
Joerg Roedel19de40a2008-12-03 14:43:34 +0100204 r = iommu_found();
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800205 break;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800206 default:
207 r = 0;
208 }
209 return r;
210
211}
212
213static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +0200214 gpa_t addr, int len, int is_write)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800215{
216 struct kvm_io_device *dev;
217
Laurent Vivier92760492008-05-30 16:05:53 +0200218 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len, is_write);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800219
220 return dev;
221}
222
223static int handle_vm_error(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
224{
225 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
226 kvm_run->hw.hardware_exit_reason = 1;
227 return 0;
228}
229
230static int handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
231{
232 struct kvm_mmio_req *p;
233 struct kvm_io_device *mmio_dev;
234
235 p = kvm_get_vcpu_ioreq(vcpu);
236
237 if ((p->addr & PAGE_MASK) == IOAPIC_DEFAULT_BASE_ADDRESS)
238 goto mmio;
239 vcpu->mmio_needed = 1;
240 vcpu->mmio_phys_addr = kvm_run->mmio.phys_addr = p->addr;
241 vcpu->mmio_size = kvm_run->mmio.len = p->size;
242 vcpu->mmio_is_write = kvm_run->mmio.is_write = !p->dir;
243
244 if (vcpu->mmio_is_write)
245 memcpy(vcpu->mmio_data, &p->data, p->size);
246 memcpy(kvm_run->mmio.data, &p->data, p->size);
247 kvm_run->exit_reason = KVM_EXIT_MMIO;
248 return 0;
249mmio:
Laurent Vivier92760492008-05-30 16:05:53 +0200250 mmio_dev = vcpu_find_mmio_dev(vcpu, p->addr, p->size, !p->dir);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800251 if (mmio_dev) {
252 if (!p->dir)
253 kvm_iodevice_write(mmio_dev, p->addr, p->size,
254 &p->data);
255 else
256 kvm_iodevice_read(mmio_dev, p->addr, p->size,
257 &p->data);
258
259 } else
260 printk(KERN_ERR"kvm: No iodevice found! addr:%lx\n", p->addr);
261 p->state = STATE_IORESP_READY;
262
263 return 1;
264}
265
266static int handle_pal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
267{
268 struct exit_ctl_data *p;
269
270 p = kvm_get_exit_data(vcpu);
271
272 if (p->exit_reason == EXIT_REASON_PAL_CALL)
273 return kvm_pal_emul(vcpu, kvm_run);
274 else {
275 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
276 kvm_run->hw.hardware_exit_reason = 2;
277 return 0;
278 }
279}
280
281static int handle_sal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
282{
283 struct exit_ctl_data *p;
284
285 p = kvm_get_exit_data(vcpu);
286
287 if (p->exit_reason == EXIT_REASON_SAL_CALL) {
288 kvm_sal_emul(vcpu);
289 return 1;
290 } else {
291 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
292 kvm_run->hw.hardware_exit_reason = 3;
293 return 0;
294 }
295
296}
297
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200298static int __apic_accept_irq(struct kvm_vcpu *vcpu, uint64_t vector)
299{
300 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
301
302 if (!test_and_set_bit(vector, &vpd->irr[0])) {
303 vcpu->arch.irq_new_pending = 1;
304 kvm_vcpu_kick(vcpu);
305 return 1;
306 }
307 return 0;
308}
309
Xiantao Zhangb024b792008-04-01 15:29:29 +0800310/*
311 * offset: address offset to IPI space.
312 * value: deliver value.
313 */
314static void vcpu_deliver_ipi(struct kvm_vcpu *vcpu, uint64_t dm,
315 uint64_t vector)
316{
317 switch (dm) {
318 case SAPIC_FIXED:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800319 break;
320 case SAPIC_NMI:
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200321 vector = 2;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800322 break;
323 case SAPIC_EXTINT:
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200324 vector = 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800325 break;
326 case SAPIC_INIT:
327 case SAPIC_PMI:
328 default:
329 printk(KERN_ERR"kvm: Unimplemented Deliver reserved IPI!\n");
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200330 return;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800331 }
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200332 __apic_accept_irq(vcpu, vector);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800333}
334
335static struct kvm_vcpu *lid_to_vcpu(struct kvm *kvm, unsigned long id,
336 unsigned long eid)
337{
338 union ia64_lid lid;
339 int i;
340
Jes Sorensen934d5342009-01-21 15:16:43 +0100341 for (i = 0; i < kvm->arch.online_vcpus; i++) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800342 if (kvm->vcpus[i]) {
343 lid.val = VCPU_LID(kvm->vcpus[i]);
344 if (lid.id == id && lid.eid == eid)
345 return kvm->vcpus[i];
346 }
347 }
348
349 return NULL;
350}
351
352static int handle_ipi(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
353{
354 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
355 struct kvm_vcpu *target_vcpu;
356 struct kvm_pt_regs *regs;
357 union ia64_ipi_a addr = p->u.ipi_data.addr;
358 union ia64_ipi_d data = p->u.ipi_data.data;
359
360 target_vcpu = lid_to_vcpu(vcpu->kvm, addr.id, addr.eid);
361 if (!target_vcpu)
362 return handle_vm_error(vcpu, kvm_run);
363
364 if (!target_vcpu->arch.launched) {
365 regs = vcpu_regs(target_vcpu);
366
367 regs->cr_iip = vcpu->kvm->arch.rdv_sal_data.boot_ip;
368 regs->r1 = vcpu->kvm->arch.rdv_sal_data.boot_gp;
369
Avi Kivitya4535292008-04-13 17:54:35 +0300370 target_vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800371 if (waitqueue_active(&target_vcpu->wq))
372 wake_up_interruptible(&target_vcpu->wq);
373 } else {
374 vcpu_deliver_ipi(target_vcpu, data.dm, data.vector);
375 if (target_vcpu != vcpu)
376 kvm_vcpu_kick(target_vcpu);
377 }
378
379 return 1;
380}
381
382struct call_data {
383 struct kvm_ptc_g ptc_g_data;
384 struct kvm_vcpu *vcpu;
385};
386
387static void vcpu_global_purge(void *info)
388{
389 struct call_data *p = (struct call_data *)info;
390 struct kvm_vcpu *vcpu = p->vcpu;
391
392 if (test_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
393 return;
394
395 set_bit(KVM_REQ_PTC_G, &vcpu->requests);
396 if (vcpu->arch.ptc_g_count < MAX_PTC_G_NUM) {
397 vcpu->arch.ptc_g_data[vcpu->arch.ptc_g_count++] =
398 p->ptc_g_data;
399 } else {
400 clear_bit(KVM_REQ_PTC_G, &vcpu->requests);
401 vcpu->arch.ptc_g_count = 0;
402 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
403 }
404}
405
406static int handle_global_purge(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
407{
408 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
409 struct kvm *kvm = vcpu->kvm;
410 struct call_data call_data;
411 int i;
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800412
Xiantao Zhangb024b792008-04-01 15:29:29 +0800413 call_data.ptc_g_data = p->u.ptc_g_data;
414
Jes Sorensen934d5342009-01-21 15:16:43 +0100415 for (i = 0; i < kvm->arch.online_vcpus; i++) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800416 if (!kvm->vcpus[i] || kvm->vcpus[i]->arch.mp_state ==
Avi Kivitya4535292008-04-13 17:54:35 +0300417 KVM_MP_STATE_UNINITIALIZED ||
Xiantao Zhangb024b792008-04-01 15:29:29 +0800418 vcpu == kvm->vcpus[i])
419 continue;
420
421 if (waitqueue_active(&kvm->vcpus[i]->wq))
422 wake_up_interruptible(&kvm->vcpus[i]->wq);
423
424 if (kvm->vcpus[i]->cpu != -1) {
425 call_data.vcpu = kvm->vcpus[i];
426 smp_call_function_single(kvm->vcpus[i]->cpu,
Takashi Iwai2f73cca2008-07-17 18:09:12 +0200427 vcpu_global_purge, &call_data, 1);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800428 } else
429 printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n");
430
431 }
432 return 1;
433}
434
435static int handle_switch_rr6(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
436{
437 return 1;
438}
439
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600440static int kvm_sn2_setup_mappings(struct kvm_vcpu *vcpu)
441{
442 unsigned long pte, rtc_phys_addr, map_addr;
443 int slot;
444
445 map_addr = KVM_VMM_BASE + (1UL << KVM_VMM_SHIFT);
446 rtc_phys_addr = LOCAL_MMR_OFFSET | SH_RTC;
447 pte = pte_val(mk_pte_phys(rtc_phys_addr, PAGE_KERNEL_UC));
448 slot = ia64_itr_entry(0x3, map_addr, pte, PAGE_SHIFT);
449 vcpu->arch.sn_rtc_tr_slot = slot;
450 if (slot < 0) {
451 printk(KERN_ERR "Mayday mayday! RTC mapping failed!\n");
452 slot = 0;
453 }
454 return slot;
455}
456
Xiantao Zhangb024b792008-04-01 15:29:29 +0800457int kvm_emulate_halt(struct kvm_vcpu *vcpu)
458{
459
460 ktime_t kt;
461 long itc_diff;
462 unsigned long vcpu_now_itc;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800463 unsigned long expires;
464 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
465 unsigned long cyc_per_usec = local_cpu_data->cyc_per_usec;
466 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
467
Xiantao Zhangb024b792008-04-01 15:29:29 +0800468 if (irqchip_in_kernel(vcpu->kvm)) {
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800469
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -0600470 vcpu_now_itc = kvm_get_itc(vcpu) + vcpu->arch.itc_offset;
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800471
472 if (time_after(vcpu_now_itc, vpd->itm)) {
473 vcpu->arch.timer_check = 1;
474 return 1;
475 }
476 itc_diff = vpd->itm - vcpu_now_itc;
477 if (itc_diff < 0)
478 itc_diff = -itc_diff;
479
480 expires = div64_u64(itc_diff, cyc_per_usec);
481 kt = ktime_set(0, 1000 * expires);
482
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800483 vcpu->arch.ht_active = 1;
484 hrtimer_start(p_ht, kt, HRTIMER_MODE_ABS);
485
Avi Kivitya4535292008-04-13 17:54:35 +0300486 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800487 kvm_vcpu_block(vcpu);
488 hrtimer_cancel(p_ht);
489 vcpu->arch.ht_active = 0;
490
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800491 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
492 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
493 vcpu->arch.mp_state =
494 KVM_MP_STATE_RUNNABLE;
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800495
Avi Kivitya4535292008-04-13 17:54:35 +0300496 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800497 return -EINTR;
498 return 1;
499 } else {
500 printk(KERN_ERR"kvm: Unsupported userspace halt!");
501 return 0;
502 }
503}
504
505static int handle_vm_shutdown(struct kvm_vcpu *vcpu,
506 struct kvm_run *kvm_run)
507{
508 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
509 return 0;
510}
511
512static int handle_external_interrupt(struct kvm_vcpu *vcpu,
513 struct kvm_run *kvm_run)
514{
515 return 1;
516}
517
Xiantao Zhang7d637972008-11-21 20:58:11 +0800518static int handle_vcpu_debug(struct kvm_vcpu *vcpu,
519 struct kvm_run *kvm_run)
520{
521 printk("VMM: %s", vcpu->arch.log_buf);
522 return 1;
523}
524
Xiantao Zhangb024b792008-04-01 15:29:29 +0800525static int (*kvm_vti_exit_handlers[])(struct kvm_vcpu *vcpu,
526 struct kvm_run *kvm_run) = {
527 [EXIT_REASON_VM_PANIC] = handle_vm_error,
528 [EXIT_REASON_MMIO_INSTRUCTION] = handle_mmio,
529 [EXIT_REASON_PAL_CALL] = handle_pal_call,
530 [EXIT_REASON_SAL_CALL] = handle_sal_call,
531 [EXIT_REASON_SWITCH_RR6] = handle_switch_rr6,
532 [EXIT_REASON_VM_DESTROY] = handle_vm_shutdown,
533 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
534 [EXIT_REASON_IPI] = handle_ipi,
535 [EXIT_REASON_PTC_G] = handle_global_purge,
Xiantao Zhang7d637972008-11-21 20:58:11 +0800536 [EXIT_REASON_DEBUG] = handle_vcpu_debug,
Xiantao Zhangb024b792008-04-01 15:29:29 +0800537
538};
539
540static const int kvm_vti_max_exit_handlers =
541 sizeof(kvm_vti_exit_handlers)/sizeof(*kvm_vti_exit_handlers);
542
Xiantao Zhangb024b792008-04-01 15:29:29 +0800543static uint32_t kvm_get_exit_reason(struct kvm_vcpu *vcpu)
544{
545 struct exit_ctl_data *p_exit_data;
546
547 p_exit_data = kvm_get_exit_data(vcpu);
548 return p_exit_data->exit_reason;
549}
550
551/*
552 * The guest has exited. See if we can fix it or if we need userspace
553 * assistance.
554 */
555static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
556{
557 u32 exit_reason = kvm_get_exit_reason(vcpu);
558 vcpu->arch.last_exit = exit_reason;
559
560 if (exit_reason < kvm_vti_max_exit_handlers
561 && kvm_vti_exit_handlers[exit_reason])
562 return kvm_vti_exit_handlers[exit_reason](vcpu, kvm_run);
563 else {
564 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
565 kvm_run->hw.hardware_exit_reason = exit_reason;
566 }
567 return 0;
568}
569
570static inline void vti_set_rr6(unsigned long rr6)
571{
572 ia64_set_rr(RR6, rr6);
573 ia64_srlz_i();
574}
575
576static int kvm_insert_vmm_mapping(struct kvm_vcpu *vcpu)
577{
578 unsigned long pte;
579 struct kvm *kvm = vcpu->kvm;
580 int r;
581
582 /*Insert a pair of tr to map vmm*/
583 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
584 r = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
585 if (r < 0)
586 goto out;
587 vcpu->arch.vmm_tr_slot = r;
588 /*Insert a pairt of tr to map data of vm*/
589 pte = pte_val(mk_pte_phys(__pa(kvm->arch.vm_base), PAGE_KERNEL));
590 r = ia64_itr_entry(0x3, KVM_VM_DATA_BASE,
591 pte, KVM_VM_DATA_SHIFT);
592 if (r < 0)
593 goto out;
594 vcpu->arch.vm_tr_slot = r;
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600595
596#if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
597 if (kvm->arch.is_sn2) {
598 r = kvm_sn2_setup_mappings(vcpu);
599 if (r < 0)
600 goto out;
601 }
602#endif
603
Xiantao Zhangb024b792008-04-01 15:29:29 +0800604 r = 0;
605out:
606 return r;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800607}
608
609static void kvm_purge_vmm_mapping(struct kvm_vcpu *vcpu)
610{
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600611 struct kvm *kvm = vcpu->kvm;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800612 ia64_ptr_entry(0x3, vcpu->arch.vmm_tr_slot);
613 ia64_ptr_entry(0x3, vcpu->arch.vm_tr_slot);
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600614#if defined(CONFIG_IA64_SGI_SN2) || defined(CONFIG_IA64_GENERIC)
615 if (kvm->arch.is_sn2)
616 ia64_ptr_entry(0x3, vcpu->arch.sn_rtc_tr_slot);
617#endif
Xiantao Zhangb024b792008-04-01 15:29:29 +0800618}
619
620static int kvm_vcpu_pre_transition(struct kvm_vcpu *vcpu)
621{
622 int cpu = smp_processor_id();
623
624 if (vcpu->arch.last_run_cpu != cpu ||
625 per_cpu(last_vcpu, cpu) != vcpu) {
626 per_cpu(last_vcpu, cpu) = vcpu;
627 vcpu->arch.last_run_cpu = cpu;
628 kvm_flush_tlb_all();
629 }
630
631 vcpu->arch.host_rr6 = ia64_get_rr(RR6);
632 vti_set_rr6(vcpu->arch.vmm_rr);
633 return kvm_insert_vmm_mapping(vcpu);
634}
635static 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
641static int vti_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
642{
643 union context *host_ctx, *guest_ctx;
644 int r;
645
646 /*Get host and guest context with guest address space.*/
647 host_ctx = kvm_get_host_context(vcpu);
648 guest_ctx = kvm_get_guest_context(vcpu);
649
650 r = kvm_vcpu_pre_transition(vcpu);
651 if (r < 0)
652 goto out;
653 kvm_vmm_info->tramp_entry(host_ctx, guest_ctx);
654 kvm_vcpu_post_transition(vcpu);
655 r = 0;
656out:
657 return r;
658}
659
660static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
661{
662 int r;
663
664again:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800665 if (signal_pending(current)) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800666 r = -EINTR;
667 kvm_run->exit_reason = KVM_EXIT_INTR;
668 goto out;
669 }
670
Jes Sorensend24d2c12009-04-09 16:38:14 +0200671 /*
672 * down_read() may sleep and return with interrupts enabled
673 */
674 down_read(&vcpu->kvm->slots_lock);
675
676 preempt_disable();
677 local_irq_disable();
678
Xiantao Zhangb024b792008-04-01 15:29:29 +0800679 vcpu->guest_mode = 1;
680 kvm_guest_enter();
Xiantao Zhangb024b792008-04-01 15:29:29 +0800681 r = vti_vcpu_run(vcpu, kvm_run);
682 if (r < 0) {
683 local_irq_enable();
684 preempt_enable();
685 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
686 goto out;
687 }
688
689 vcpu->arch.launched = 1;
690 vcpu->guest_mode = 0;
691 local_irq_enable();
692
693 /*
694 * We must have an instruction between local_irq_enable() and
695 * kvm_guest_exit(), so the timer interrupt isn't delayed by
696 * the interrupt shadow. The stat.exits increment will do nicely.
697 * But we need to prevent reordering, hence this barrier():
698 */
699 barrier();
Xiantao Zhangb024b792008-04-01 15:29:29 +0800700 kvm_guest_exit();
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800701 up_read(&vcpu->kvm->slots_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800702 preempt_enable();
703
704 r = kvm_handle_exit(kvm_run, vcpu);
705
706 if (r > 0) {
707 if (!need_resched())
708 goto again;
709 }
710
711out:
712 if (r > 0) {
713 kvm_resched(vcpu);
714 goto again;
715 }
716
717 return r;
718}
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)
725 memcpy(&p->data, vcpu->mmio_data, 8);
726 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
734 vcpu_load(vcpu);
735
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800736 if (vcpu->sigset_active)
737 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
738
Avi Kivitya4535292008-04-13 17:54:35 +0300739 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800740 kvm_vcpu_block(vcpu);
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800741 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800742 r = -EAGAIN;
743 goto out;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800744 }
745
Xiantao Zhangb024b792008-04-01 15:29:29 +0800746 if (vcpu->mmio_needed) {
747 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
748 kvm_set_mmio_data(vcpu);
749 vcpu->mmio_read_completed = 1;
750 vcpu->mmio_needed = 0;
751 }
752 r = __vcpu_run(vcpu, kvm_run);
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800753out:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800754 if (vcpu->sigset_active)
755 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
756
757 vcpu_put(vcpu);
758 return r;
759}
760
Xiantao Zhangb024b792008-04-01 15:29:29 +0800761static struct kvm *kvm_alloc_kvm(void)
762{
763
764 struct kvm *kvm;
765 uint64_t vm_base;
766
Xiantao Zhanga917f7af32008-10-23 14:56:44 +0800767 BUG_ON(sizeof(struct kvm) > KVM_VM_STRUCT_SIZE);
768
Xiantao Zhangb024b792008-04-01 15:29:29 +0800769 vm_base = __get_free_pages(GFP_KERNEL, get_order(KVM_VM_DATA_SIZE));
770
771 if (!vm_base)
772 return ERR_PTR(-ENOMEM);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800773
Xiantao Zhangb024b792008-04-01 15:29:29 +0800774 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
Xiantao Zhanga917f7af32008-10-23 14:56:44 +0800775 kvm = (struct kvm *)(vm_base +
776 offsetof(struct kvm_vm_data, kvm_vm_struct));
Xiantao Zhangb024b792008-04-01 15:29:29 +0800777 kvm->arch.vm_base = vm_base;
Xiantao Zhanga917f7af32008-10-23 14:56:44 +0800778 printk(KERN_DEBUG"kvm: vm's data area:0x%lx\n", vm_base);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800779
780 return kvm;
781}
782
783struct kvm_io_range {
784 unsigned long start;
785 unsigned long size;
786 unsigned long type;
787};
788
789static const struct kvm_io_range io_ranges[] = {
790 {VGA_IO_START, VGA_IO_SIZE, GPFN_FRAME_BUFFER},
791 {MMIO_START, MMIO_SIZE, GPFN_LOW_MMIO},
792 {LEGACY_IO_START, LEGACY_IO_SIZE, GPFN_LEGACY_IO},
793 {IO_SAPIC_START, IO_SAPIC_SIZE, GPFN_IOSAPIC},
794 {PIB_START, PIB_SIZE, GPFN_PIB},
795};
796
797static void kvm_build_io_pmt(struct kvm *kvm)
798{
799 unsigned long i, j;
800
801 /* Mark I/O ranges */
802 for (i = 0; i < (sizeof(io_ranges) / sizeof(struct kvm_io_range));
803 i++) {
804 for (j = io_ranges[i].start;
805 j < io_ranges[i].start + io_ranges[i].size;
806 j += PAGE_SIZE)
807 kvm_set_pmt_entry(kvm, j >> PAGE_SHIFT,
808 io_ranges[i].type, 0);
809 }
810
811}
812
813/*Use unused rids to virtualize guest rid.*/
814#define GUEST_PHYSICAL_RR0 0x1739
815#define GUEST_PHYSICAL_RR4 0x2739
816#define VMM_INIT_RR 0x1660
817
818static void kvm_init_vm(struct kvm *kvm)
819{
Xiantao Zhangb024b792008-04-01 15:29:29 +0800820 BUG_ON(!kvm);
821
822 kvm->arch.metaphysical_rr0 = GUEST_PHYSICAL_RR0;
823 kvm->arch.metaphysical_rr4 = GUEST_PHYSICAL_RR4;
824 kvm->arch.vmm_init_rr = VMM_INIT_RR;
825
Xiantao Zhangb024b792008-04-01 15:29:29 +0800826 /*
827 *Fill P2M entries for MMIO/IO ranges
828 */
829 kvm_build_io_pmt(kvm);
830
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800831 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Sheng Yang5550af42008-10-15 20:15:06 +0800832
833 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
834 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800835}
836
837struct kvm *kvm_arch_create_vm(void)
838{
839 struct kvm *kvm = kvm_alloc_kvm();
840
841 if (IS_ERR(kvm))
842 return ERR_PTR(-ENOMEM);
Jes Sorensen0c72ea72009-02-25 10:38:52 -0600843
844 kvm->arch.is_sn2 = ia64_platform_is("sn2");
845
Xiantao Zhangb024b792008-04-01 15:29:29 +0800846 kvm_init_vm(kvm);
847
Jes Sorensen934d5342009-01-21 15:16:43 +0100848 kvm->arch.online_vcpus = 0;
849
Xiantao Zhangb024b792008-04-01 15:29:29 +0800850 return kvm;
851
852}
853
854static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm,
855 struct kvm_irqchip *chip)
856{
857 int r;
858
859 r = 0;
860 switch (chip->chip_id) {
861 case KVM_IRQCHIP_IOAPIC:
862 memcpy(&chip->chip.ioapic, ioapic_irqchip(kvm),
863 sizeof(struct kvm_ioapic_state));
864 break;
865 default:
866 r = -EINVAL;
867 break;
868 }
869 return r;
870}
871
872static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
873{
874 int r;
875
876 r = 0;
877 switch (chip->chip_id) {
878 case KVM_IRQCHIP_IOAPIC:
879 memcpy(ioapic_irqchip(kvm),
880 &chip->chip.ioapic,
881 sizeof(struct kvm_ioapic_state));
882 break;
883 default:
884 r = -EINVAL;
885 break;
886 }
887 return r;
888}
889
890#define RESTORE_REGS(_x) vcpu->arch._x = regs->_x
891
892int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
893{
Xiantao Zhangb024b792008-04-01 15:29:29 +0800894 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
Jes Sorensen042b26e2008-12-16 16:45:47 +0100895 int i;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800896
897 vcpu_load(vcpu);
898
899 for (i = 0; i < 16; i++) {
900 vpd->vgr[i] = regs->vpd.vgr[i];
901 vpd->vbgr[i] = regs->vpd.vbgr[i];
902 }
903 for (i = 0; i < 128; i++)
904 vpd->vcr[i] = regs->vpd.vcr[i];
905 vpd->vhpi = regs->vpd.vhpi;
906 vpd->vnat = regs->vpd.vnat;
907 vpd->vbnat = regs->vpd.vbnat;
908 vpd->vpsr = regs->vpd.vpsr;
909
910 vpd->vpr = regs->vpd.vpr;
911
Jes Sorensen042b26e2008-12-16 16:45:47 +0100912 memcpy(&vcpu->arch.guest, &regs->saved_guest, sizeof(union context));
Xiantao Zhangb024b792008-04-01 15:29:29 +0800913
914 RESTORE_REGS(mp_state);
915 RESTORE_REGS(vmm_rr);
916 memcpy(vcpu->arch.itrs, regs->itrs, sizeof(struct thash_data) * NITRS);
917 memcpy(vcpu->arch.dtrs, regs->dtrs, sizeof(struct thash_data) * NDTRS);
918 RESTORE_REGS(itr_regions);
919 RESTORE_REGS(dtr_regions);
920 RESTORE_REGS(tc_regions);
921 RESTORE_REGS(irq_check);
922 RESTORE_REGS(itc_check);
923 RESTORE_REGS(timer_check);
924 RESTORE_REGS(timer_pending);
925 RESTORE_REGS(last_itc);
926 for (i = 0; i < 8; i++) {
927 vcpu->arch.vrr[i] = regs->vrr[i];
928 vcpu->arch.ibr[i] = regs->ibr[i];
929 vcpu->arch.dbr[i] = regs->dbr[i];
930 }
931 for (i = 0; i < 4; i++)
932 vcpu->arch.insvc[i] = regs->insvc[i];
933 RESTORE_REGS(xtp);
934 RESTORE_REGS(metaphysical_rr0);
935 RESTORE_REGS(metaphysical_rr4);
936 RESTORE_REGS(metaphysical_saved_rr0);
937 RESTORE_REGS(metaphysical_saved_rr4);
938 RESTORE_REGS(fp_psr);
939 RESTORE_REGS(saved_gp);
940
941 vcpu->arch.irq_new_pending = 1;
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -0600942 vcpu->arch.itc_offset = regs->saved_itc - kvm_get_itc(vcpu);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800943 set_bit(KVM_REQ_RESUME, &vcpu->requests);
944
945 vcpu_put(vcpu);
Jes Sorensen042b26e2008-12-16 16:45:47 +0100946
947 return 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800948}
949
950long kvm_arch_vm_ioctl(struct file *filp,
951 unsigned int ioctl, unsigned long arg)
952{
953 struct kvm *kvm = filp->private_data;
954 void __user *argp = (void __user *)arg;
955 int r = -EINVAL;
956
957 switch (ioctl) {
958 case KVM_SET_MEMORY_REGION: {
959 struct kvm_memory_region kvm_mem;
960 struct kvm_userspace_memory_region kvm_userspace_mem;
961
962 r = -EFAULT;
963 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
964 goto out;
965 kvm_userspace_mem.slot = kvm_mem.slot;
966 kvm_userspace_mem.flags = kvm_mem.flags;
967 kvm_userspace_mem.guest_phys_addr =
968 kvm_mem.guest_phys_addr;
969 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
970 r = kvm_vm_ioctl_set_memory_region(kvm,
971 &kvm_userspace_mem, 0);
972 if (r)
973 goto out;
974 break;
975 }
976 case KVM_CREATE_IRQCHIP:
977 r = -EFAULT;
978 r = kvm_ioapic_init(kvm);
979 if (r)
980 goto out;
Avi Kivity399ec802008-11-19 13:58:46 +0200981 r = kvm_setup_default_irq_routing(kvm);
982 if (r) {
983 kfree(kvm->arch.vioapic);
984 goto out;
985 }
Xiantao Zhangb024b792008-04-01 15:29:29 +0800986 break;
Gleb Natapov49256632009-02-04 17:28:14 +0200987 case KVM_IRQ_LINE_STATUS:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800988 case KVM_IRQ_LINE: {
989 struct kvm_irq_level irq_event;
990
991 r = -EFAULT;
992 if (copy_from_user(&irq_event, argp, sizeof irq_event))
993 goto out;
994 if (irqchip_in_kernel(kvm)) {
Gleb Natapov49256632009-02-04 17:28:14 +0200995 __s32 status;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800996 mutex_lock(&kvm->lock);
Gleb Natapov49256632009-02-04 17:28:14 +0200997 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Sheng Yang5550af42008-10-15 20:15:06 +0800998 irq_event.irq, irq_event.level);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800999 mutex_unlock(&kvm->lock);
Gleb Natapov49256632009-02-04 17:28:14 +02001000 if (ioctl == KVM_IRQ_LINE_STATUS) {
1001 irq_event.status = status;
1002 if (copy_to_user(argp, &irq_event,
1003 sizeof irq_event))
1004 goto out;
1005 }
Xiantao Zhangb024b792008-04-01 15:29:29 +08001006 r = 0;
1007 }
1008 break;
1009 }
1010 case KVM_GET_IRQCHIP: {
1011 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1012 struct kvm_irqchip chip;
1013
1014 r = -EFAULT;
1015 if (copy_from_user(&chip, argp, sizeof chip))
1016 goto out;
1017 r = -ENXIO;
1018 if (!irqchip_in_kernel(kvm))
1019 goto out;
1020 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
1021 if (r)
1022 goto out;
1023 r = -EFAULT;
1024 if (copy_to_user(argp, &chip, sizeof chip))
1025 goto out;
1026 r = 0;
1027 break;
1028 }
1029 case KVM_SET_IRQCHIP: {
1030 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
1031 struct kvm_irqchip chip;
1032
1033 r = -EFAULT;
1034 if (copy_from_user(&chip, argp, sizeof chip))
1035 goto out;
1036 r = -ENXIO;
1037 if (!irqchip_in_kernel(kvm))
1038 goto out;
1039 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
1040 if (r)
1041 goto out;
1042 r = 0;
1043 break;
1044 }
1045 default:
1046 ;
1047 }
1048out:
1049 return r;
1050}
1051
1052int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1053 struct kvm_sregs *sregs)
1054{
1055 return -EINVAL;
1056}
1057
1058int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1059 struct kvm_sregs *sregs)
1060{
1061 return -EINVAL;
1062
1063}
1064int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1065 struct kvm_translation *tr)
1066{
1067
1068 return -EINVAL;
1069}
1070
1071static int kvm_alloc_vmm_area(void)
1072{
1073 if (!kvm_vmm_base && (kvm_vm_buffer_size < KVM_VM_BUFFER_SIZE)) {
1074 kvm_vmm_base = __get_free_pages(GFP_KERNEL,
1075 get_order(KVM_VMM_SIZE));
1076 if (!kvm_vmm_base)
1077 return -ENOMEM;
1078
1079 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1080 kvm_vm_buffer = kvm_vmm_base + VMM_SIZE;
1081
1082 printk(KERN_DEBUG"kvm:VMM's Base Addr:0x%lx, vm_buffer:0x%lx\n",
1083 kvm_vmm_base, kvm_vm_buffer);
1084 }
1085
1086 return 0;
1087}
1088
1089static void kvm_free_vmm_area(void)
1090{
1091 if (kvm_vmm_base) {
1092 /*Zero this area before free to avoid bits leak!!*/
1093 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1094 free_pages(kvm_vmm_base, get_order(KVM_VMM_SIZE));
1095 kvm_vmm_base = 0;
1096 kvm_vm_buffer = 0;
1097 kvm_vsa_base = 0;
1098 }
1099}
1100
Xiantao Zhangb024b792008-04-01 15:29:29 +08001101static void vti_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1102{
1103}
1104
1105static int vti_init_vpd(struct kvm_vcpu *vcpu)
1106{
1107 int i;
1108 union cpuid3_t cpuid3;
1109 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1110
1111 if (IS_ERR(vpd))
1112 return PTR_ERR(vpd);
1113
1114 /* CPUID init */
1115 for (i = 0; i < 5; i++)
1116 vpd->vcpuid[i] = ia64_get_cpuid(i);
1117
1118 /* Limit the CPUID number to 5 */
1119 cpuid3.value = vpd->vcpuid[3];
1120 cpuid3.number = 4; /* 5 - 1 */
1121 vpd->vcpuid[3] = cpuid3.value;
1122
1123 /*Set vac and vdc fields*/
1124 vpd->vac.a_from_int_cr = 1;
1125 vpd->vac.a_to_int_cr = 1;
1126 vpd->vac.a_from_psr = 1;
1127 vpd->vac.a_from_cpuid = 1;
1128 vpd->vac.a_cover = 1;
1129 vpd->vac.a_bsw = 1;
1130 vpd->vac.a_int = 1;
1131 vpd->vdc.d_vmsw = 1;
1132
1133 /*Set virtual buffer*/
1134 vpd->virt_env_vaddr = KVM_VM_BUFFER_BASE;
1135
1136 return 0;
1137}
1138
1139static int vti_create_vp(struct kvm_vcpu *vcpu)
1140{
1141 long ret;
1142 struct vpd *vpd = vcpu->arch.vpd;
1143 unsigned long vmm_ivt;
1144
1145 vmm_ivt = kvm_vmm_info->vmm_ivt;
1146
1147 printk(KERN_DEBUG "kvm: vcpu:%p,ivt: 0x%lx\n", vcpu, vmm_ivt);
1148
1149 ret = ia64_pal_vp_create((u64 *)vpd, (u64 *)vmm_ivt, 0);
1150
1151 if (ret) {
1152 printk(KERN_ERR"kvm: ia64_pal_vp_create failed!\n");
1153 return -EINVAL;
1154 }
1155 return 0;
1156}
1157
1158static void init_ptce_info(struct kvm_vcpu *vcpu)
1159{
1160 ia64_ptce_info_t ptce = {0};
1161
1162 ia64_get_ptce(&ptce);
1163 vcpu->arch.ptce_base = ptce.base;
1164 vcpu->arch.ptce_count[0] = ptce.count[0];
1165 vcpu->arch.ptce_count[1] = ptce.count[1];
1166 vcpu->arch.ptce_stride[0] = ptce.stride[0];
1167 vcpu->arch.ptce_stride[1] = ptce.stride[1];
1168}
1169
1170static void kvm_migrate_hlt_timer(struct kvm_vcpu *vcpu)
1171{
1172 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
1173
1174 if (hrtimer_cancel(p_ht))
Arjan van de Ven18dd36a2008-09-01 15:19:11 -07001175 hrtimer_start_expires(p_ht, HRTIMER_MODE_ABS);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001176}
1177
1178static enum hrtimer_restart hlt_timer_fn(struct hrtimer *data)
1179{
1180 struct kvm_vcpu *vcpu;
1181 wait_queue_head_t *q;
1182
1183 vcpu = container_of(data, struct kvm_vcpu, arch.hlt_timer);
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001184 q = &vcpu->wq;
1185
Avi Kivitya4535292008-04-13 17:54:35 +03001186 if (vcpu->arch.mp_state != KVM_MP_STATE_HALTED)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001187 goto out;
1188
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001189 if (waitqueue_active(q))
Xiantao Zhangb024b792008-04-01 15:29:29 +08001190 wake_up_interruptible(q);
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001191
Xiantao Zhangb024b792008-04-01 15:29:29 +08001192out:
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001193 vcpu->arch.timer_fired = 1;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001194 vcpu->arch.timer_check = 1;
1195 return HRTIMER_NORESTART;
1196}
1197
1198#define PALE_RESET_ENTRY 0x80000000ffffffb0UL
1199
1200int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1201{
1202 struct kvm_vcpu *v;
1203 int r;
1204 int i;
1205 long itc_offset;
1206 struct kvm *kvm = vcpu->kvm;
1207 struct kvm_pt_regs *regs = vcpu_regs(vcpu);
1208
1209 union context *p_ctx = &vcpu->arch.guest;
1210 struct kvm_vcpu *vmm_vcpu = to_guest(vcpu->kvm, vcpu);
1211
1212 /*Init vcpu context for first run.*/
1213 if (IS_ERR(vmm_vcpu))
1214 return PTR_ERR(vmm_vcpu);
1215
1216 if (vcpu->vcpu_id == 0) {
Avi Kivitya4535292008-04-13 17:54:35 +03001217 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001218
1219 /*Set entry address for first run.*/
1220 regs->cr_iip = PALE_RESET_ENTRY;
1221
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001222 /*Initialize itc offset for vcpus*/
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -06001223 itc_offset = 0UL - kvm_get_itc(vcpu);
Jes Sorensen934d5342009-01-21 15:16:43 +01001224 for (i = 0; i < kvm->arch.online_vcpus; i++) {
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001225 v = (struct kvm_vcpu *)((char *)vcpu +
1226 sizeof(struct kvm_vcpu_data) * i);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001227 v->arch.itc_offset = itc_offset;
1228 v->arch.last_itc = 0;
1229 }
1230 } else
Avi Kivitya4535292008-04-13 17:54:35 +03001231 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001232
1233 r = -ENOMEM;
1234 vcpu->arch.apic = kzalloc(sizeof(struct kvm_lapic), GFP_KERNEL);
1235 if (!vcpu->arch.apic)
1236 goto out;
1237 vcpu->arch.apic->vcpu = vcpu;
1238
1239 p_ctx->gr[1] = 0;
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001240 p_ctx->gr[12] = (unsigned long)((char *)vmm_vcpu + KVM_STK_OFFSET);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001241 p_ctx->gr[13] = (unsigned long)vmm_vcpu;
1242 p_ctx->psr = 0x1008522000UL;
1243 p_ctx->ar[40] = FPSR_DEFAULT; /*fpsr*/
1244 p_ctx->caller_unat = 0;
1245 p_ctx->pr = 0x0;
1246 p_ctx->ar[36] = 0x0; /*unat*/
1247 p_ctx->ar[19] = 0x0; /*rnat*/
1248 p_ctx->ar[18] = (unsigned long)vmm_vcpu +
1249 ((sizeof(struct kvm_vcpu)+15) & ~15);
1250 p_ctx->ar[64] = 0x0; /*pfs*/
1251 p_ctx->cr[0] = 0x7e04UL;
1252 p_ctx->cr[2] = (unsigned long)kvm_vmm_info->vmm_ivt;
1253 p_ctx->cr[8] = 0x3c;
1254
1255 /*Initilize region register*/
1256 p_ctx->rr[0] = 0x30;
1257 p_ctx->rr[1] = 0x30;
1258 p_ctx->rr[2] = 0x30;
1259 p_ctx->rr[3] = 0x30;
1260 p_ctx->rr[4] = 0x30;
1261 p_ctx->rr[5] = 0x30;
1262 p_ctx->rr[7] = 0x30;
1263
1264 /*Initilize branch register 0*/
1265 p_ctx->br[0] = *(unsigned long *)kvm_vmm_info->vmm_entry;
1266
1267 vcpu->arch.vmm_rr = kvm->arch.vmm_init_rr;
1268 vcpu->arch.metaphysical_rr0 = kvm->arch.metaphysical_rr0;
1269 vcpu->arch.metaphysical_rr4 = kvm->arch.metaphysical_rr4;
1270
1271 hrtimer_init(&vcpu->arch.hlt_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1272 vcpu->arch.hlt_timer.function = hlt_timer_fn;
1273
1274 vcpu->arch.last_run_cpu = -1;
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001275 vcpu->arch.vpd = (struct vpd *)VPD_BASE(vcpu->vcpu_id);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001276 vcpu->arch.vsa_base = kvm_vsa_base;
1277 vcpu->arch.__gp = kvm_vmm_gp;
1278 vcpu->arch.dirty_log_lock_pa = __pa(&kvm->arch.dirty_log_lock);
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001279 vcpu->arch.vhpt.hash = (struct thash_data *)VHPT_BASE(vcpu->vcpu_id);
1280 vcpu->arch.vtlb.hash = (struct thash_data *)VTLB_BASE(vcpu->vcpu_id);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001281 init_ptce_info(vcpu);
1282
1283 r = 0;
1284out:
1285 return r;
1286}
1287
1288static int vti_vcpu_setup(struct kvm_vcpu *vcpu, int id)
1289{
1290 unsigned long psr;
1291 int r;
1292
1293 local_irq_save(psr);
1294 r = kvm_insert_vmm_mapping(vcpu);
1295 if (r)
1296 goto fail;
1297 r = kvm_vcpu_init(vcpu, vcpu->kvm, id);
1298 if (r)
1299 goto fail;
1300
1301 r = vti_init_vpd(vcpu);
1302 if (r) {
1303 printk(KERN_DEBUG"kvm: vpd init error!!\n");
1304 goto uninit;
1305 }
1306
1307 r = vti_create_vp(vcpu);
1308 if (r)
1309 goto uninit;
1310
1311 kvm_purge_vmm_mapping(vcpu);
1312 local_irq_restore(psr);
1313
1314 return 0;
1315uninit:
1316 kvm_vcpu_uninit(vcpu);
1317fail:
Julia Lawallcab7a1e2008-07-22 21:38:18 +02001318 local_irq_restore(psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001319 return r;
1320}
1321
1322struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1323 unsigned int id)
1324{
1325 struct kvm_vcpu *vcpu;
1326 unsigned long vm_base = kvm->arch.vm_base;
1327 int r;
1328 int cpu;
1329
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001330 BUG_ON(sizeof(struct kvm_vcpu) > VCPU_STRUCT_SIZE/2);
1331
1332 r = -EINVAL;
1333 if (id >= KVM_MAX_VCPUS) {
1334 printk(KERN_ERR"kvm: Can't configure vcpus > %ld",
1335 KVM_MAX_VCPUS);
1336 goto fail;
1337 }
1338
Xiantao Zhangb024b792008-04-01 15:29:29 +08001339 r = -ENOMEM;
1340 if (!vm_base) {
1341 printk(KERN_ERR"kvm: Create vcpu[%d] error!\n", id);
1342 goto fail;
1343 }
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001344 vcpu = (struct kvm_vcpu *)(vm_base + offsetof(struct kvm_vm_data,
1345 vcpu_data[id].vcpu_struct));
Xiantao Zhangb024b792008-04-01 15:29:29 +08001346 vcpu->kvm = kvm;
1347
1348 cpu = get_cpu();
1349 vti_vcpu_load(vcpu, cpu);
1350 r = vti_vcpu_setup(vcpu, id);
1351 put_cpu();
1352
1353 if (r) {
1354 printk(KERN_DEBUG"kvm: vcpu_setup error!!\n");
1355 goto fail;
1356 }
1357
Jes Sorensen934d5342009-01-21 15:16:43 +01001358 kvm->arch.online_vcpus++;
1359
Xiantao Zhangb024b792008-04-01 15:29:29 +08001360 return vcpu;
1361fail:
1362 return ERR_PTR(r);
1363}
1364
1365int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1366{
1367 return 0;
1368}
1369
1370int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1371{
1372 return -EINVAL;
1373}
1374
1375int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1376{
1377 return -EINVAL;
1378}
1379
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001380int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1381 struct kvm_guest_debug *dbg)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001382{
1383 return -EINVAL;
1384}
1385
1386static void free_kvm(struct kvm *kvm)
1387{
1388 unsigned long vm_base = kvm->arch.vm_base;
1389
1390 if (vm_base) {
1391 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
1392 free_pages(vm_base, get_order(KVM_VM_DATA_SIZE));
1393 }
1394
1395}
1396
1397static void kvm_release_vm_pages(struct kvm *kvm)
1398{
1399 struct kvm_memory_slot *memslot;
1400 int i, j;
1401 unsigned long base_gfn;
1402
1403 for (i = 0; i < kvm->nmemslots; i++) {
1404 memslot = &kvm->memslots[i];
1405 base_gfn = memslot->base_gfn;
1406
1407 for (j = 0; j < memslot->npages; j++) {
1408 if (memslot->rmap[j])
1409 put_page((struct page *)memslot->rmap[j]);
1410 }
1411 }
1412}
1413
Sheng Yangad8ba2c2009-01-06 10:03:02 +08001414void kvm_arch_sync_events(struct kvm *kvm)
1415{
1416}
1417
Xiantao Zhangb024b792008-04-01 15:29:29 +08001418void kvm_arch_destroy_vm(struct kvm *kvm)
1419{
Xiantao Zhang2381ad22008-10-08 08:29:33 +08001420 kvm_iommu_unmap_guest(kvm);
1421#ifdef KVM_CAP_DEVICE_ASSIGNMENT
1422 kvm_free_all_assigned_devices(kvm);
1423#endif
Xiantao Zhangb024b792008-04-01 15:29:29 +08001424 kfree(kvm->arch.vioapic);
1425 kvm_release_vm_pages(kvm);
1426 kvm_free_physmem(kvm);
1427 free_kvm(kvm);
1428}
1429
1430void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1431{
1432}
1433
1434void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1435{
1436 if (cpu != vcpu->cpu) {
1437 vcpu->cpu = cpu;
1438 if (vcpu->arch.ht_active)
1439 kvm_migrate_hlt_timer(vcpu);
1440 }
1441}
1442
1443#define SAVE_REGS(_x) regs->_x = vcpu->arch._x
1444
1445int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1446{
Xiantao Zhangb024b792008-04-01 15:29:29 +08001447 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
Jes Sorensen042b26e2008-12-16 16:45:47 +01001448 int i;
1449
Xiantao Zhangb024b792008-04-01 15:29:29 +08001450 vcpu_load(vcpu);
1451
1452 for (i = 0; i < 16; i++) {
1453 regs->vpd.vgr[i] = vpd->vgr[i];
1454 regs->vpd.vbgr[i] = vpd->vbgr[i];
1455 }
1456 for (i = 0; i < 128; i++)
1457 regs->vpd.vcr[i] = vpd->vcr[i];
1458 regs->vpd.vhpi = vpd->vhpi;
1459 regs->vpd.vnat = vpd->vnat;
1460 regs->vpd.vbnat = vpd->vbnat;
1461 regs->vpd.vpsr = vpd->vpsr;
1462 regs->vpd.vpr = vpd->vpr;
1463
Jes Sorensen042b26e2008-12-16 16:45:47 +01001464 memcpy(&regs->saved_guest, &vcpu->arch.guest, sizeof(union context));
1465
Xiantao Zhangb024b792008-04-01 15:29:29 +08001466 SAVE_REGS(mp_state);
1467 SAVE_REGS(vmm_rr);
1468 memcpy(regs->itrs, vcpu->arch.itrs, sizeof(struct thash_data) * NITRS);
1469 memcpy(regs->dtrs, vcpu->arch.dtrs, sizeof(struct thash_data) * NDTRS);
1470 SAVE_REGS(itr_regions);
1471 SAVE_REGS(dtr_regions);
1472 SAVE_REGS(tc_regions);
1473 SAVE_REGS(irq_check);
1474 SAVE_REGS(itc_check);
1475 SAVE_REGS(timer_check);
1476 SAVE_REGS(timer_pending);
1477 SAVE_REGS(last_itc);
1478 for (i = 0; i < 8; i++) {
1479 regs->vrr[i] = vcpu->arch.vrr[i];
1480 regs->ibr[i] = vcpu->arch.ibr[i];
1481 regs->dbr[i] = vcpu->arch.dbr[i];
1482 }
1483 for (i = 0; i < 4; i++)
1484 regs->insvc[i] = vcpu->arch.insvc[i];
Jes Sorensenc6c9fcd2009-02-25 10:38:53 -06001485 regs->saved_itc = vcpu->arch.itc_offset + kvm_get_itc(vcpu);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001486 SAVE_REGS(xtp);
1487 SAVE_REGS(metaphysical_rr0);
1488 SAVE_REGS(metaphysical_rr4);
1489 SAVE_REGS(metaphysical_saved_rr0);
1490 SAVE_REGS(metaphysical_saved_rr4);
1491 SAVE_REGS(fp_psr);
1492 SAVE_REGS(saved_gp);
Jes Sorensen042b26e2008-12-16 16:45:47 +01001493
Xiantao Zhangb024b792008-04-01 15:29:29 +08001494 vcpu_put(vcpu);
Jes Sorensen042b26e2008-12-16 16:45:47 +01001495 return 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001496}
1497
Jes Sorensene9a999f2008-12-18 12:17:51 +01001498int kvm_arch_vcpu_ioctl_get_stack(struct kvm_vcpu *vcpu,
1499 struct kvm_ia64_vcpu_stack *stack)
1500{
1501 memcpy(stack, vcpu, sizeof(struct kvm_ia64_vcpu_stack));
1502 return 0;
1503}
1504
1505int kvm_arch_vcpu_ioctl_set_stack(struct kvm_vcpu *vcpu,
1506 struct kvm_ia64_vcpu_stack *stack)
1507{
1508 memcpy(vcpu + 1, &stack->stack[0] + sizeof(struct kvm_vcpu),
1509 sizeof(struct kvm_ia64_vcpu_stack) - sizeof(struct kvm_vcpu));
1510
1511 vcpu->arch.exit_data = ((struct kvm_vcpu *)stack)->arch.exit_data;
1512 return 0;
1513}
1514
Xiantao Zhangb024b792008-04-01 15:29:29 +08001515void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1516{
1517
1518 hrtimer_cancel(&vcpu->arch.hlt_timer);
1519 kfree(vcpu->arch.apic);
1520}
1521
1522
1523long kvm_arch_vcpu_ioctl(struct file *filp,
Jes Sorensene9a999f2008-12-18 12:17:51 +01001524 unsigned int ioctl, unsigned long arg)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001525{
Jes Sorensene9a999f2008-12-18 12:17:51 +01001526 struct kvm_vcpu *vcpu = filp->private_data;
1527 void __user *argp = (void __user *)arg;
1528 struct kvm_ia64_vcpu_stack *stack = NULL;
1529 long r;
1530
1531 switch (ioctl) {
1532 case KVM_IA64_VCPU_GET_STACK: {
1533 struct kvm_ia64_vcpu_stack __user *user_stack;
1534 void __user *first_p = argp;
1535
1536 r = -EFAULT;
1537 if (copy_from_user(&user_stack, first_p, sizeof(void *)))
1538 goto out;
1539
1540 if (!access_ok(VERIFY_WRITE, user_stack,
1541 sizeof(struct kvm_ia64_vcpu_stack))) {
1542 printk(KERN_INFO "KVM_IA64_VCPU_GET_STACK: "
1543 "Illegal user destination address for stack\n");
1544 goto out;
1545 }
1546 stack = kzalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL);
1547 if (!stack) {
1548 r = -ENOMEM;
1549 goto out;
1550 }
1551
1552 r = kvm_arch_vcpu_ioctl_get_stack(vcpu, stack);
1553 if (r)
1554 goto out;
1555
1556 if (copy_to_user(user_stack, stack,
1557 sizeof(struct kvm_ia64_vcpu_stack)))
1558 goto out;
1559
1560 break;
1561 }
1562 case KVM_IA64_VCPU_SET_STACK: {
1563 struct kvm_ia64_vcpu_stack __user *user_stack;
1564 void __user *first_p = argp;
1565
1566 r = -EFAULT;
1567 if (copy_from_user(&user_stack, first_p, sizeof(void *)))
1568 goto out;
1569
1570 if (!access_ok(VERIFY_READ, user_stack,
1571 sizeof(struct kvm_ia64_vcpu_stack))) {
1572 printk(KERN_INFO "KVM_IA64_VCPU_SET_STACK: "
1573 "Illegal user address for stack\n");
1574 goto out;
1575 }
1576 stack = kmalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL);
1577 if (!stack) {
1578 r = -ENOMEM;
1579 goto out;
1580 }
1581 if (copy_from_user(stack, user_stack,
1582 sizeof(struct kvm_ia64_vcpu_stack)))
1583 goto out;
1584
1585 r = kvm_arch_vcpu_ioctl_set_stack(vcpu, stack);
1586 break;
1587 }
1588
1589 default:
1590 r = -EINVAL;
1591 }
1592
1593out:
1594 kfree(stack);
1595 return r;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001596}
1597
1598int kvm_arch_set_memory_region(struct kvm *kvm,
1599 struct kvm_userspace_memory_region *mem,
1600 struct kvm_memory_slot old,
1601 int user_alloc)
1602{
1603 unsigned long i;
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001604 unsigned long pfn;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001605 int npages = mem->memory_size >> PAGE_SHIFT;
1606 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
1607 unsigned long base_gfn = memslot->base_gfn;
1608
Xiantao Zhanga917f7af32008-10-23 14:56:44 +08001609 if (base_gfn + npages > (KVM_MAX_MEM_SIZE >> PAGE_SHIFT))
1610 return -ENOMEM;
1611
Xiantao Zhangb024b792008-04-01 15:29:29 +08001612 for (i = 0; i < npages; i++) {
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001613 pfn = gfn_to_pfn(kvm, base_gfn + i);
1614 if (!kvm_is_mmio_pfn(pfn)) {
1615 kvm_set_pmt_entry(kvm, base_gfn + i,
1616 pfn << PAGE_SHIFT,
Xiantao Zhangb010eb52008-09-28 01:39:46 -07001617 _PAGE_AR_RWX | _PAGE_MA_WB);
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001618 memslot->rmap[i] = (unsigned long)pfn_to_page(pfn);
1619 } else {
1620 kvm_set_pmt_entry(kvm, base_gfn + i,
Xiantao Zhangb010eb52008-09-28 01:39:46 -07001621 GPFN_PHYS_MMIO | (pfn << PAGE_SHIFT),
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001622 _PAGE_MA_UC);
1623 memslot->rmap[i] = 0;
1624 }
Xiantao Zhangb024b792008-04-01 15:29:29 +08001625 }
1626
1627 return 0;
1628}
1629
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001630void kvm_arch_flush_shadow(struct kvm *kvm)
1631{
1632}
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) {
1715 printk("kvm: kvm area hasn't been initilized yet!!\n");
1716 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
1810static int kvm_ia64_sync_dirty_log(struct kvm *kvm,
1811 struct kvm_dirty_log *log)
1812{
1813 struct kvm_memory_slot *memslot;
1814 int r, i;
1815 long n, base;
Xiantao Zhanga917f7af32008-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
1819 r = -EINVAL;
1820 if (log->slot >= KVM_MEMORY_SLOTS)
1821 goto out;
1822
1823 memslot = &kvm->memslots[log->slot];
1824 r = -ENOENT;
1825 if (!memslot->dirty_bitmap)
1826 goto out;
1827
1828 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1829 base = memslot->base_gfn / BITS_PER_LONG;
1830
1831 for (i = 0; i < n/sizeof(long); ++i) {
1832 memslot->dirty_bitmap[i] = dirty_bitmap[base + i];
1833 dirty_bitmap[base + i] = 0;
1834 }
1835 r = 0;
1836out:
1837 return r;
1838}
1839
1840int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1841 struct kvm_dirty_log *log)
1842{
1843 int r;
1844 int n;
1845 struct kvm_memory_slot *memslot;
1846 int is_dirty = 0;
1847
1848 spin_lock(&kvm->arch.dirty_log_lock);
1849
1850 r = kvm_ia64_sync_dirty_log(kvm, log);
1851 if (r)
1852 goto out;
1853
1854 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1855 if (r)
1856 goto out;
1857
1858 /* If nothing is dirty, don't bother messing with page tables. */
1859 if (is_dirty) {
1860 kvm_flush_remote_tlbs(kvm);
1861 memslot = &kvm->memslots[log->slot];
1862 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1863 memset(memslot->dirty_bitmap, 0, n);
1864 }
1865 r = 0;
1866out:
1867 spin_unlock(&kvm->arch.dirty_log_lock);
1868 return r;
1869}
1870
1871int kvm_arch_hardware_setup(void)
1872{
1873 return 0;
1874}
1875
1876void kvm_arch_hardware_unsetup(void)
1877{
1878}
1879
1880static void vcpu_kick_intr(void *info)
1881{
1882#ifdef DEBUG
1883 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
1884 printk(KERN_DEBUG"vcpu_kick_intr %p \n", vcpu);
1885#endif
1886}
1887
1888void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1889{
1890 int ipi_pcpu = vcpu->cpu;
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001891 int cpu = get_cpu();
Xiantao Zhangb024b792008-04-01 15:29:29 +08001892
1893 if (waitqueue_active(&vcpu->wq))
1894 wake_up_interruptible(&vcpu->wq);
1895
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001896 if (vcpu->guest_mode && cpu != ipi_pcpu)
Takashi Iwai2f73cca2008-07-17 18:09:12 +02001897 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001898 put_cpu();
Xiantao Zhangb024b792008-04-01 15:29:29 +08001899}
1900
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001901int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001902{
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001903 return __apic_accept_irq(vcpu, irq->vector);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001904}
1905
1906int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
1907{
1908 return apic->vcpu->vcpu_id == dest;
1909}
1910
1911int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
1912{
1913 return 0;
1914}
1915
Gleb Natapove1035712009-03-05 16:34:59 +02001916int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001917{
Gleb Natapove1035712009-03-05 16:34:59 +02001918 return vcpu1->arch.xtp - vcpu2->arch.xtp;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001919}
1920
Gleb Natapov343f94f2009-03-05 16:34:54 +02001921int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
1922 int short_hand, int dest, int dest_mode)
1923{
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001924 struct kvm_lapic *target = vcpu->arch.apic;
Gleb Natapov343f94f2009-03-05 16:34:54 +02001925 return (dest_mode == 0) ?
1926 kvm_apic_match_physical_addr(target, dest) :
1927 kvm_apic_match_logical_addr(target, dest);
1928}
1929
Xiantao Zhangb024b792008-04-01 15:29:29 +08001930static int find_highest_bits(int *dat)
1931{
1932 u32 bits, bitnum;
1933 int i;
1934
1935 /* loop for all 256 bits */
1936 for (i = 7; i >= 0 ; i--) {
1937 bits = dat[i];
1938 if (bits) {
1939 bitnum = fls(bits);
1940 return i * 32 + bitnum - 1;
1941 }
1942 }
1943
1944 return -1;
1945}
1946
1947int kvm_highest_pending_irq(struct kvm_vcpu *vcpu)
1948{
1949 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1950
1951 if (vpd->irr[0] & (1UL << NMI_VECTOR))
1952 return NMI_VECTOR;
1953 if (vpd->irr[0] & (1UL << ExtINT_VECTOR))
1954 return ExtINT_VECTOR;
1955
1956 return find_highest_bits((int *)&vpd->irr[0]);
1957}
1958
1959int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
1960{
1961 if (kvm_highest_pending_irq(vcpu) != -1)
1962 return 1;
1963 return 0;
1964}
1965
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001966int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1967{
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001968 return vcpu->arch.timer_fired;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001969}
1970
Xiantao Zhangb024b792008-04-01 15:29:29 +08001971gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1972{
1973 return gfn;
1974}
1975
1976int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1977{
Avi Kivitya4535292008-04-13 17:54:35 +03001978 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001979}
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001980
1981int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1982 struct kvm_mp_state *mp_state)
1983{
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001984 vcpu_load(vcpu);
1985 mp_state->mp_state = vcpu->arch.mp_state;
1986 vcpu_put(vcpu);
1987 return 0;
1988}
1989
1990static int vcpu_reset(struct kvm_vcpu *vcpu)
1991{
1992 int r;
1993 long psr;
1994 local_irq_save(psr);
1995 r = kvm_insert_vmm_mapping(vcpu);
1996 if (r)
1997 goto fail;
1998
1999 vcpu->arch.launched = 0;
2000 kvm_arch_vcpu_uninit(vcpu);
2001 r = kvm_arch_vcpu_init(vcpu);
2002 if (r)
2003 goto fail;
2004
2005 kvm_purge_vmm_mapping(vcpu);
2006 r = 0;
2007fail:
2008 local_irq_restore(psr);
2009 return r;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03002010}
2011
2012int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
2013 struct kvm_mp_state *mp_state)
2014{
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08002015 int r = 0;
2016
2017 vcpu_load(vcpu);
2018 vcpu->arch.mp_state = mp_state->mp_state;
2019 if (vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)
2020 r = vcpu_reset(vcpu);
2021 vcpu_put(vcpu);
2022 return r;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03002023}