blob: acf43ec42704b2ec28afeb4e90aa574dd32a7db1 [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>
Xiantao Zhangb024b792008-04-01 15:29:29 +080044
45#include "misc.h"
46#include "vti.h"
47#include "iodev.h"
48#include "ioapic.h"
49#include "lapic.h"
Xiantao Zhang2f749772008-09-27 11:46:36 +080050#include "irq.h"
Xiantao Zhangb024b792008-04-01 15:29:29 +080051
52static unsigned long kvm_vmm_base;
53static unsigned long kvm_vsa_base;
54static unsigned long kvm_vm_buffer;
55static unsigned long kvm_vm_buffer_size;
56unsigned long kvm_vmm_gp;
57
58static long vp_env_info;
59
60static struct kvm_vmm_info *kvm_vmm_info;
61
62static DEFINE_PER_CPU(struct kvm_vcpu *, last_vcpu);
63
64struct kvm_stats_debugfs_item debugfs_entries[] = {
65 { NULL }
66};
67
Xiantao Zhangb024b792008-04-01 15:29:29 +080068static void kvm_flush_icache(unsigned long start, unsigned long len)
69{
70 int l;
71
72 for (l = 0; l < (len + 32); l += 32)
Isaku Yamahata71205692009-03-27 15:11:57 +090073 ia64_fc((void *)(start + l));
Xiantao Zhangb024b792008-04-01 15:29:29 +080074
75 ia64_sync_i();
76 ia64_srlz_i();
77}
78
79static void kvm_flush_tlb_all(void)
80{
81 unsigned long i, j, count0, count1, stride0, stride1, addr;
82 long flags;
83
84 addr = local_cpu_data->ptce_base;
85 count0 = local_cpu_data->ptce_count[0];
86 count1 = local_cpu_data->ptce_count[1];
87 stride0 = local_cpu_data->ptce_stride[0];
88 stride1 = local_cpu_data->ptce_stride[1];
89
90 local_irq_save(flags);
91 for (i = 0; i < count0; ++i) {
92 for (j = 0; j < count1; ++j) {
93 ia64_ptce(addr);
94 addr += stride1;
95 }
96 addr += stride0;
97 }
98 local_irq_restore(flags);
99 ia64_srlz_i(); /* srlz.i implies srlz.d */
100}
101
102long ia64_pal_vp_create(u64 *vpd, u64 *host_iva, u64 *opt_handler)
103{
104 struct ia64_pal_retval iprv;
105
106 PAL_CALL_STK(iprv, PAL_VP_CREATE, (u64)vpd, (u64)host_iva,
107 (u64)opt_handler);
108
109 return iprv.status;
110}
111
112static DEFINE_SPINLOCK(vp_lock);
113
114void kvm_arch_hardware_enable(void *garbage)
115{
116 long status;
117 long tmp_base;
118 unsigned long pte;
119 unsigned long saved_psr;
120 int slot;
121
122 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base),
123 PAGE_KERNEL));
124 local_irq_save(saved_psr);
125 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
Julia Lawallcab7a1e2008-07-22 21:38:18 +0200126 local_irq_restore(saved_psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800127 if (slot < 0)
128 return;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800129
130 spin_lock(&vp_lock);
131 status = ia64_pal_vp_init_env(kvm_vsa_base ?
132 VP_INIT_ENV : VP_INIT_ENV_INITALIZE,
133 __pa(kvm_vm_buffer), KVM_VM_BUFFER_BASE, &tmp_base);
134 if (status != 0) {
135 printk(KERN_WARNING"kvm: Failed to Enable VT Support!!!!\n");
136 return ;
137 }
138
139 if (!kvm_vsa_base) {
140 kvm_vsa_base = tmp_base;
141 printk(KERN_INFO"kvm: kvm_vsa_base:0x%lx\n", kvm_vsa_base);
142 }
143 spin_unlock(&vp_lock);
144 ia64_ptr_entry(0x3, slot);
145}
146
147void kvm_arch_hardware_disable(void *garbage)
148{
149
150 long status;
151 int slot;
152 unsigned long pte;
153 unsigned long saved_psr;
154 unsigned long host_iva = ia64_getreg(_IA64_REG_CR_IVA);
155
156 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base),
157 PAGE_KERNEL));
158
159 local_irq_save(saved_psr);
160 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
Julia Lawallcab7a1e2008-07-22 21:38:18 +0200161 local_irq_restore(saved_psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800162 if (slot < 0)
163 return;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800164
165 status = ia64_pal_vp_exit_env(host_iva);
166 if (status)
167 printk(KERN_DEBUG"kvm: Failed to disable VT support! :%ld\n",
168 status);
169 ia64_ptr_entry(0x3, slot);
170}
171
172void kvm_arch_check_processor_compat(void *rtn)
173{
174 *(int *)rtn = 0;
175}
176
177int kvm_dev_ioctl_check_extension(long ext)
178{
179
180 int r;
181
182 switch (ext) {
183 case KVM_CAP_IRQCHIP:
Xiantao Zhang8c4b5372008-08-28 09:34:08 +0800184 case KVM_CAP_MP_STATE:
Gleb Natapov49256632009-02-04 17:28:14 +0200185 case KVM_CAP_IRQ_INJECT_STATUS:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800186 r = 1;
187 break;
Laurent Vivier7f39f8a2008-05-30 16:05:57 +0200188 case KVM_CAP_COALESCED_MMIO:
189 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
190 break;
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800191 case KVM_CAP_IOMMU:
Joerg Roedel19de40a2008-12-03 14:43:34 +0100192 r = iommu_found();
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800193 break;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800194 default:
195 r = 0;
196 }
197 return r;
198
199}
200
201static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +0200202 gpa_t addr, int len, int is_write)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800203{
204 struct kvm_io_device *dev;
205
Laurent Vivier92760492008-05-30 16:05:53 +0200206 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len, is_write);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800207
208 return dev;
209}
210
211static int handle_vm_error(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
212{
213 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
214 kvm_run->hw.hardware_exit_reason = 1;
215 return 0;
216}
217
218static int handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
219{
220 struct kvm_mmio_req *p;
221 struct kvm_io_device *mmio_dev;
222
223 p = kvm_get_vcpu_ioreq(vcpu);
224
225 if ((p->addr & PAGE_MASK) == IOAPIC_DEFAULT_BASE_ADDRESS)
226 goto mmio;
227 vcpu->mmio_needed = 1;
228 vcpu->mmio_phys_addr = kvm_run->mmio.phys_addr = p->addr;
229 vcpu->mmio_size = kvm_run->mmio.len = p->size;
230 vcpu->mmio_is_write = kvm_run->mmio.is_write = !p->dir;
231
232 if (vcpu->mmio_is_write)
233 memcpy(vcpu->mmio_data, &p->data, p->size);
234 memcpy(kvm_run->mmio.data, &p->data, p->size);
235 kvm_run->exit_reason = KVM_EXIT_MMIO;
236 return 0;
237mmio:
Laurent Vivier92760492008-05-30 16:05:53 +0200238 mmio_dev = vcpu_find_mmio_dev(vcpu, p->addr, p->size, !p->dir);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800239 if (mmio_dev) {
240 if (!p->dir)
241 kvm_iodevice_write(mmio_dev, p->addr, p->size,
242 &p->data);
243 else
244 kvm_iodevice_read(mmio_dev, p->addr, p->size,
245 &p->data);
246
247 } else
248 printk(KERN_ERR"kvm: No iodevice found! addr:%lx\n", p->addr);
249 p->state = STATE_IORESP_READY;
250
251 return 1;
252}
253
254static int handle_pal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
255{
256 struct exit_ctl_data *p;
257
258 p = kvm_get_exit_data(vcpu);
259
260 if (p->exit_reason == EXIT_REASON_PAL_CALL)
261 return kvm_pal_emul(vcpu, kvm_run);
262 else {
263 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
264 kvm_run->hw.hardware_exit_reason = 2;
265 return 0;
266 }
267}
268
269static int handle_sal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
270{
271 struct exit_ctl_data *p;
272
273 p = kvm_get_exit_data(vcpu);
274
275 if (p->exit_reason == EXIT_REASON_SAL_CALL) {
276 kvm_sal_emul(vcpu);
277 return 1;
278 } else {
279 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
280 kvm_run->hw.hardware_exit_reason = 3;
281 return 0;
282 }
283
284}
285
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200286static int __apic_accept_irq(struct kvm_vcpu *vcpu, uint64_t vector)
287{
288 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
289
290 if (!test_and_set_bit(vector, &vpd->irr[0])) {
291 vcpu->arch.irq_new_pending = 1;
292 kvm_vcpu_kick(vcpu);
293 return 1;
294 }
295 return 0;
296}
297
Xiantao Zhangb024b792008-04-01 15:29:29 +0800298/*
299 * offset: address offset to IPI space.
300 * value: deliver value.
301 */
302static void vcpu_deliver_ipi(struct kvm_vcpu *vcpu, uint64_t dm,
303 uint64_t vector)
304{
305 switch (dm) {
306 case SAPIC_FIXED:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800307 break;
308 case SAPIC_NMI:
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200309 vector = 2;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800310 break;
311 case SAPIC_EXTINT:
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200312 vector = 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800313 break;
314 case SAPIC_INIT:
315 case SAPIC_PMI:
316 default:
317 printk(KERN_ERR"kvm: Unimplemented Deliver reserved IPI!\n");
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200318 return;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800319 }
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200320 __apic_accept_irq(vcpu, vector);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800321}
322
323static struct kvm_vcpu *lid_to_vcpu(struct kvm *kvm, unsigned long id,
324 unsigned long eid)
325{
326 union ia64_lid lid;
327 int i;
328
Jes Sorensen934d5342009-01-21 15:16:43 +0100329 for (i = 0; i < kvm->arch.online_vcpus; i++) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800330 if (kvm->vcpus[i]) {
331 lid.val = VCPU_LID(kvm->vcpus[i]);
332 if (lid.id == id && lid.eid == eid)
333 return kvm->vcpus[i];
334 }
335 }
336
337 return NULL;
338}
339
340static int handle_ipi(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
341{
342 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
343 struct kvm_vcpu *target_vcpu;
344 struct kvm_pt_regs *regs;
345 union ia64_ipi_a addr = p->u.ipi_data.addr;
346 union ia64_ipi_d data = p->u.ipi_data.data;
347
348 target_vcpu = lid_to_vcpu(vcpu->kvm, addr.id, addr.eid);
349 if (!target_vcpu)
350 return handle_vm_error(vcpu, kvm_run);
351
352 if (!target_vcpu->arch.launched) {
353 regs = vcpu_regs(target_vcpu);
354
355 regs->cr_iip = vcpu->kvm->arch.rdv_sal_data.boot_ip;
356 regs->r1 = vcpu->kvm->arch.rdv_sal_data.boot_gp;
357
Avi Kivitya4535292008-04-13 17:54:35 +0300358 target_vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800359 if (waitqueue_active(&target_vcpu->wq))
360 wake_up_interruptible(&target_vcpu->wq);
361 } else {
362 vcpu_deliver_ipi(target_vcpu, data.dm, data.vector);
363 if (target_vcpu != vcpu)
364 kvm_vcpu_kick(target_vcpu);
365 }
366
367 return 1;
368}
369
370struct call_data {
371 struct kvm_ptc_g ptc_g_data;
372 struct kvm_vcpu *vcpu;
373};
374
375static void vcpu_global_purge(void *info)
376{
377 struct call_data *p = (struct call_data *)info;
378 struct kvm_vcpu *vcpu = p->vcpu;
379
380 if (test_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
381 return;
382
383 set_bit(KVM_REQ_PTC_G, &vcpu->requests);
384 if (vcpu->arch.ptc_g_count < MAX_PTC_G_NUM) {
385 vcpu->arch.ptc_g_data[vcpu->arch.ptc_g_count++] =
386 p->ptc_g_data;
387 } else {
388 clear_bit(KVM_REQ_PTC_G, &vcpu->requests);
389 vcpu->arch.ptc_g_count = 0;
390 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
391 }
392}
393
394static int handle_global_purge(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
395{
396 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
397 struct kvm *kvm = vcpu->kvm;
398 struct call_data call_data;
399 int i;
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800400
Xiantao Zhangb024b792008-04-01 15:29:29 +0800401 call_data.ptc_g_data = p->u.ptc_g_data;
402
Jes Sorensen934d5342009-01-21 15:16:43 +0100403 for (i = 0; i < kvm->arch.online_vcpus; i++) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800404 if (!kvm->vcpus[i] || kvm->vcpus[i]->arch.mp_state ==
Avi Kivitya4535292008-04-13 17:54:35 +0300405 KVM_MP_STATE_UNINITIALIZED ||
Xiantao Zhangb024b792008-04-01 15:29:29 +0800406 vcpu == kvm->vcpus[i])
407 continue;
408
409 if (waitqueue_active(&kvm->vcpus[i]->wq))
410 wake_up_interruptible(&kvm->vcpus[i]->wq);
411
412 if (kvm->vcpus[i]->cpu != -1) {
413 call_data.vcpu = kvm->vcpus[i];
414 smp_call_function_single(kvm->vcpus[i]->cpu,
Takashi Iwai2f73cca2008-07-17 18:09:12 +0200415 vcpu_global_purge, &call_data, 1);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800416 } else
417 printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n");
418
419 }
420 return 1;
421}
422
423static int handle_switch_rr6(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
424{
425 return 1;
426}
427
428int kvm_emulate_halt(struct kvm_vcpu *vcpu)
429{
430
431 ktime_t kt;
432 long itc_diff;
433 unsigned long vcpu_now_itc;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800434 unsigned long expires;
435 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
436 unsigned long cyc_per_usec = local_cpu_data->cyc_per_usec;
437 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
438
Xiantao Zhangb024b792008-04-01 15:29:29 +0800439 if (irqchip_in_kernel(vcpu->kvm)) {
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800440
441 vcpu_now_itc = ia64_getreg(_IA64_REG_AR_ITC) + vcpu->arch.itc_offset;
442
443 if (time_after(vcpu_now_itc, vpd->itm)) {
444 vcpu->arch.timer_check = 1;
445 return 1;
446 }
447 itc_diff = vpd->itm - vcpu_now_itc;
448 if (itc_diff < 0)
449 itc_diff = -itc_diff;
450
451 expires = div64_u64(itc_diff, cyc_per_usec);
452 kt = ktime_set(0, 1000 * expires);
453
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800454 vcpu->arch.ht_active = 1;
455 hrtimer_start(p_ht, kt, HRTIMER_MODE_ABS);
456
Avi Kivitya4535292008-04-13 17:54:35 +0300457 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800458 kvm_vcpu_block(vcpu);
459 hrtimer_cancel(p_ht);
460 vcpu->arch.ht_active = 0;
461
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800462 if (test_and_clear_bit(KVM_REQ_UNHALT, &vcpu->requests))
463 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED)
464 vcpu->arch.mp_state =
465 KVM_MP_STATE_RUNNABLE;
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800466
Avi Kivitya4535292008-04-13 17:54:35 +0300467 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800468 return -EINTR;
469 return 1;
470 } else {
471 printk(KERN_ERR"kvm: Unsupported userspace halt!");
472 return 0;
473 }
474}
475
476static int handle_vm_shutdown(struct kvm_vcpu *vcpu,
477 struct kvm_run *kvm_run)
478{
479 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
480 return 0;
481}
482
483static int handle_external_interrupt(struct kvm_vcpu *vcpu,
484 struct kvm_run *kvm_run)
485{
486 return 1;
487}
488
Xiantao Zhang7d637972008-11-21 20:58:11 +0800489static int handle_vcpu_debug(struct kvm_vcpu *vcpu,
490 struct kvm_run *kvm_run)
491{
492 printk("VMM: %s", vcpu->arch.log_buf);
493 return 1;
494}
495
Xiantao Zhangb024b792008-04-01 15:29:29 +0800496static int (*kvm_vti_exit_handlers[])(struct kvm_vcpu *vcpu,
497 struct kvm_run *kvm_run) = {
498 [EXIT_REASON_VM_PANIC] = handle_vm_error,
499 [EXIT_REASON_MMIO_INSTRUCTION] = handle_mmio,
500 [EXIT_REASON_PAL_CALL] = handle_pal_call,
501 [EXIT_REASON_SAL_CALL] = handle_sal_call,
502 [EXIT_REASON_SWITCH_RR6] = handle_switch_rr6,
503 [EXIT_REASON_VM_DESTROY] = handle_vm_shutdown,
504 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
505 [EXIT_REASON_IPI] = handle_ipi,
506 [EXIT_REASON_PTC_G] = handle_global_purge,
Xiantao Zhang7d637972008-11-21 20:58:11 +0800507 [EXIT_REASON_DEBUG] = handle_vcpu_debug,
Xiantao Zhangb024b792008-04-01 15:29:29 +0800508
509};
510
511static const int kvm_vti_max_exit_handlers =
512 sizeof(kvm_vti_exit_handlers)/sizeof(*kvm_vti_exit_handlers);
513
Xiantao Zhangb024b792008-04-01 15:29:29 +0800514static uint32_t kvm_get_exit_reason(struct kvm_vcpu *vcpu)
515{
516 struct exit_ctl_data *p_exit_data;
517
518 p_exit_data = kvm_get_exit_data(vcpu);
519 return p_exit_data->exit_reason;
520}
521
522/*
523 * The guest has exited. See if we can fix it or if we need userspace
524 * assistance.
525 */
526static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
527{
528 u32 exit_reason = kvm_get_exit_reason(vcpu);
529 vcpu->arch.last_exit = exit_reason;
530
531 if (exit_reason < kvm_vti_max_exit_handlers
532 && kvm_vti_exit_handlers[exit_reason])
533 return kvm_vti_exit_handlers[exit_reason](vcpu, kvm_run);
534 else {
535 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
536 kvm_run->hw.hardware_exit_reason = exit_reason;
537 }
538 return 0;
539}
540
541static inline void vti_set_rr6(unsigned long rr6)
542{
543 ia64_set_rr(RR6, rr6);
544 ia64_srlz_i();
545}
546
547static int kvm_insert_vmm_mapping(struct kvm_vcpu *vcpu)
548{
549 unsigned long pte;
550 struct kvm *kvm = vcpu->kvm;
551 int r;
552
553 /*Insert a pair of tr to map vmm*/
554 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
555 r = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
556 if (r < 0)
557 goto out;
558 vcpu->arch.vmm_tr_slot = r;
559 /*Insert a pairt of tr to map data of vm*/
560 pte = pte_val(mk_pte_phys(__pa(kvm->arch.vm_base), PAGE_KERNEL));
561 r = ia64_itr_entry(0x3, KVM_VM_DATA_BASE,
562 pte, KVM_VM_DATA_SHIFT);
563 if (r < 0)
564 goto out;
565 vcpu->arch.vm_tr_slot = r;
566 r = 0;
567out:
568 return r;
569
570}
571
572static void kvm_purge_vmm_mapping(struct kvm_vcpu *vcpu)
573{
574
575 ia64_ptr_entry(0x3, vcpu->arch.vmm_tr_slot);
576 ia64_ptr_entry(0x3, vcpu->arch.vm_tr_slot);
577
578}
579
580static int kvm_vcpu_pre_transition(struct kvm_vcpu *vcpu)
581{
582 int cpu = smp_processor_id();
583
584 if (vcpu->arch.last_run_cpu != cpu ||
585 per_cpu(last_vcpu, cpu) != vcpu) {
586 per_cpu(last_vcpu, cpu) = vcpu;
587 vcpu->arch.last_run_cpu = cpu;
588 kvm_flush_tlb_all();
589 }
590
591 vcpu->arch.host_rr6 = ia64_get_rr(RR6);
592 vti_set_rr6(vcpu->arch.vmm_rr);
593 return kvm_insert_vmm_mapping(vcpu);
594}
595static void kvm_vcpu_post_transition(struct kvm_vcpu *vcpu)
596{
597 kvm_purge_vmm_mapping(vcpu);
598 vti_set_rr6(vcpu->arch.host_rr6);
599}
600
601static int vti_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
602{
603 union context *host_ctx, *guest_ctx;
604 int r;
605
606 /*Get host and guest context with guest address space.*/
607 host_ctx = kvm_get_host_context(vcpu);
608 guest_ctx = kvm_get_guest_context(vcpu);
609
610 r = kvm_vcpu_pre_transition(vcpu);
611 if (r < 0)
612 goto out;
613 kvm_vmm_info->tramp_entry(host_ctx, guest_ctx);
614 kvm_vcpu_post_transition(vcpu);
615 r = 0;
616out:
617 return r;
618}
619
620static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
621{
622 int r;
623
624again:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800625 if (signal_pending(current)) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800626 r = -EINTR;
627 kvm_run->exit_reason = KVM_EXIT_INTR;
628 goto out;
629 }
630
Jes Sorensend24d2c12009-04-09 16:38:14 +0200631 /*
632 * down_read() may sleep and return with interrupts enabled
633 */
634 down_read(&vcpu->kvm->slots_lock);
635
636 preempt_disable();
637 local_irq_disable();
638
Xiantao Zhangb024b792008-04-01 15:29:29 +0800639 vcpu->guest_mode = 1;
640 kvm_guest_enter();
Xiantao Zhangb024b792008-04-01 15:29:29 +0800641 r = vti_vcpu_run(vcpu, kvm_run);
642 if (r < 0) {
643 local_irq_enable();
644 preempt_enable();
645 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
646 goto out;
647 }
648
649 vcpu->arch.launched = 1;
650 vcpu->guest_mode = 0;
651 local_irq_enable();
652
653 /*
654 * We must have an instruction between local_irq_enable() and
655 * kvm_guest_exit(), so the timer interrupt isn't delayed by
656 * the interrupt shadow. The stat.exits increment will do nicely.
657 * But we need to prevent reordering, hence this barrier():
658 */
659 barrier();
Xiantao Zhangb024b792008-04-01 15:29:29 +0800660 kvm_guest_exit();
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800661 up_read(&vcpu->kvm->slots_lock);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800662 preempt_enable();
663
664 r = kvm_handle_exit(kvm_run, vcpu);
665
666 if (r > 0) {
667 if (!need_resched())
668 goto again;
669 }
670
671out:
672 if (r > 0) {
673 kvm_resched(vcpu);
674 goto again;
675 }
676
677 return r;
678}
679
680static void kvm_set_mmio_data(struct kvm_vcpu *vcpu)
681{
682 struct kvm_mmio_req *p = kvm_get_vcpu_ioreq(vcpu);
683
684 if (!vcpu->mmio_is_write)
685 memcpy(&p->data, vcpu->mmio_data, 8);
686 p->state = STATE_IORESP_READY;
687}
688
689int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
690{
691 int r;
692 sigset_t sigsaved;
693
694 vcpu_load(vcpu);
695
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800696 if (vcpu->sigset_active)
697 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
698
Avi Kivitya4535292008-04-13 17:54:35 +0300699 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800700 kvm_vcpu_block(vcpu);
Xiantao Zhangdecc9012008-10-16 15:58:15 +0800701 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800702 r = -EAGAIN;
703 goto out;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800704 }
705
Xiantao Zhangb024b792008-04-01 15:29:29 +0800706 if (vcpu->mmio_needed) {
707 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
708 kvm_set_mmio_data(vcpu);
709 vcpu->mmio_read_completed = 1;
710 vcpu->mmio_needed = 0;
711 }
712 r = __vcpu_run(vcpu, kvm_run);
Xiantao Zhanga2e4e282008-10-23 15:02:52 +0800713out:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800714 if (vcpu->sigset_active)
715 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
716
717 vcpu_put(vcpu);
718 return r;
719}
720
Xiantao Zhangb024b792008-04-01 15:29:29 +0800721static struct kvm *kvm_alloc_kvm(void)
722{
723
724 struct kvm *kvm;
725 uint64_t vm_base;
726
Xiantao Zhanga917f7a2008-10-23 14:56:44 +0800727 BUG_ON(sizeof(struct kvm) > KVM_VM_STRUCT_SIZE);
728
Xiantao Zhangb024b792008-04-01 15:29:29 +0800729 vm_base = __get_free_pages(GFP_KERNEL, get_order(KVM_VM_DATA_SIZE));
730
731 if (!vm_base)
732 return ERR_PTR(-ENOMEM);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800733
Xiantao Zhangb024b792008-04-01 15:29:29 +0800734 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
Xiantao Zhanga917f7a2008-10-23 14:56:44 +0800735 kvm = (struct kvm *)(vm_base +
736 offsetof(struct kvm_vm_data, kvm_vm_struct));
Xiantao Zhangb024b792008-04-01 15:29:29 +0800737 kvm->arch.vm_base = vm_base;
Xiantao Zhanga917f7a2008-10-23 14:56:44 +0800738 printk(KERN_DEBUG"kvm: vm's data area:0x%lx\n", vm_base);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800739
740 return kvm;
741}
742
743struct kvm_io_range {
744 unsigned long start;
745 unsigned long size;
746 unsigned long type;
747};
748
749static const struct kvm_io_range io_ranges[] = {
750 {VGA_IO_START, VGA_IO_SIZE, GPFN_FRAME_BUFFER},
751 {MMIO_START, MMIO_SIZE, GPFN_LOW_MMIO},
752 {LEGACY_IO_START, LEGACY_IO_SIZE, GPFN_LEGACY_IO},
753 {IO_SAPIC_START, IO_SAPIC_SIZE, GPFN_IOSAPIC},
754 {PIB_START, PIB_SIZE, GPFN_PIB},
755};
756
757static void kvm_build_io_pmt(struct kvm *kvm)
758{
759 unsigned long i, j;
760
761 /* Mark I/O ranges */
762 for (i = 0; i < (sizeof(io_ranges) / sizeof(struct kvm_io_range));
763 i++) {
764 for (j = io_ranges[i].start;
765 j < io_ranges[i].start + io_ranges[i].size;
766 j += PAGE_SIZE)
767 kvm_set_pmt_entry(kvm, j >> PAGE_SHIFT,
768 io_ranges[i].type, 0);
769 }
770
771}
772
773/*Use unused rids to virtualize guest rid.*/
774#define GUEST_PHYSICAL_RR0 0x1739
775#define GUEST_PHYSICAL_RR4 0x2739
776#define VMM_INIT_RR 0x1660
777
778static void kvm_init_vm(struct kvm *kvm)
779{
Xiantao Zhangb024b792008-04-01 15:29:29 +0800780 BUG_ON(!kvm);
781
782 kvm->arch.metaphysical_rr0 = GUEST_PHYSICAL_RR0;
783 kvm->arch.metaphysical_rr4 = GUEST_PHYSICAL_RR4;
784 kvm->arch.vmm_init_rr = VMM_INIT_RR;
785
Xiantao Zhangb024b792008-04-01 15:29:29 +0800786 /*
787 *Fill P2M entries for MMIO/IO ranges
788 */
789 kvm_build_io_pmt(kvm);
790
Xiantao Zhang2381ad22008-10-08 08:29:33 +0800791 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
Sheng Yang5550af42008-10-15 20:15:06 +0800792
793 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
794 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800795}
796
797struct kvm *kvm_arch_create_vm(void)
798{
799 struct kvm *kvm = kvm_alloc_kvm();
800
801 if (IS_ERR(kvm))
802 return ERR_PTR(-ENOMEM);
803 kvm_init_vm(kvm);
804
Jes Sorensen934d5342009-01-21 15:16:43 +0100805 kvm->arch.online_vcpus = 0;
806
Xiantao Zhangb024b792008-04-01 15:29:29 +0800807 return kvm;
808
809}
810
811static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm,
812 struct kvm_irqchip *chip)
813{
814 int r;
815
816 r = 0;
817 switch (chip->chip_id) {
818 case KVM_IRQCHIP_IOAPIC:
819 memcpy(&chip->chip.ioapic, ioapic_irqchip(kvm),
820 sizeof(struct kvm_ioapic_state));
821 break;
822 default:
823 r = -EINVAL;
824 break;
825 }
826 return r;
827}
828
829static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
830{
831 int r;
832
833 r = 0;
834 switch (chip->chip_id) {
835 case KVM_IRQCHIP_IOAPIC:
836 memcpy(ioapic_irqchip(kvm),
837 &chip->chip.ioapic,
838 sizeof(struct kvm_ioapic_state));
839 break;
840 default:
841 r = -EINVAL;
842 break;
843 }
844 return r;
845}
846
847#define RESTORE_REGS(_x) vcpu->arch._x = regs->_x
848
849int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
850{
Xiantao Zhangb024b792008-04-01 15:29:29 +0800851 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
Jes Sorensen042b26e2008-12-16 16:45:47 +0100852 int i;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800853
854 vcpu_load(vcpu);
855
856 for (i = 0; i < 16; i++) {
857 vpd->vgr[i] = regs->vpd.vgr[i];
858 vpd->vbgr[i] = regs->vpd.vbgr[i];
859 }
860 for (i = 0; i < 128; i++)
861 vpd->vcr[i] = regs->vpd.vcr[i];
862 vpd->vhpi = regs->vpd.vhpi;
863 vpd->vnat = regs->vpd.vnat;
864 vpd->vbnat = regs->vpd.vbnat;
865 vpd->vpsr = regs->vpd.vpsr;
866
867 vpd->vpr = regs->vpd.vpr;
868
Jes Sorensen042b26e2008-12-16 16:45:47 +0100869 memcpy(&vcpu->arch.guest, &regs->saved_guest, sizeof(union context));
Xiantao Zhangb024b792008-04-01 15:29:29 +0800870
871 RESTORE_REGS(mp_state);
872 RESTORE_REGS(vmm_rr);
873 memcpy(vcpu->arch.itrs, regs->itrs, sizeof(struct thash_data) * NITRS);
874 memcpy(vcpu->arch.dtrs, regs->dtrs, sizeof(struct thash_data) * NDTRS);
875 RESTORE_REGS(itr_regions);
876 RESTORE_REGS(dtr_regions);
877 RESTORE_REGS(tc_regions);
878 RESTORE_REGS(irq_check);
879 RESTORE_REGS(itc_check);
880 RESTORE_REGS(timer_check);
881 RESTORE_REGS(timer_pending);
882 RESTORE_REGS(last_itc);
883 for (i = 0; i < 8; i++) {
884 vcpu->arch.vrr[i] = regs->vrr[i];
885 vcpu->arch.ibr[i] = regs->ibr[i];
886 vcpu->arch.dbr[i] = regs->dbr[i];
887 }
888 for (i = 0; i < 4; i++)
889 vcpu->arch.insvc[i] = regs->insvc[i];
890 RESTORE_REGS(xtp);
891 RESTORE_REGS(metaphysical_rr0);
892 RESTORE_REGS(metaphysical_rr4);
893 RESTORE_REGS(metaphysical_saved_rr0);
894 RESTORE_REGS(metaphysical_saved_rr4);
895 RESTORE_REGS(fp_psr);
896 RESTORE_REGS(saved_gp);
897
898 vcpu->arch.irq_new_pending = 1;
899 vcpu->arch.itc_offset = regs->saved_itc - ia64_getreg(_IA64_REG_AR_ITC);
900 set_bit(KVM_REQ_RESUME, &vcpu->requests);
901
902 vcpu_put(vcpu);
Jes Sorensen042b26e2008-12-16 16:45:47 +0100903
904 return 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800905}
906
907long kvm_arch_vm_ioctl(struct file *filp,
908 unsigned int ioctl, unsigned long arg)
909{
910 struct kvm *kvm = filp->private_data;
911 void __user *argp = (void __user *)arg;
912 int r = -EINVAL;
913
914 switch (ioctl) {
915 case KVM_SET_MEMORY_REGION: {
916 struct kvm_memory_region kvm_mem;
917 struct kvm_userspace_memory_region kvm_userspace_mem;
918
919 r = -EFAULT;
920 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
921 goto out;
922 kvm_userspace_mem.slot = kvm_mem.slot;
923 kvm_userspace_mem.flags = kvm_mem.flags;
924 kvm_userspace_mem.guest_phys_addr =
925 kvm_mem.guest_phys_addr;
926 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
927 r = kvm_vm_ioctl_set_memory_region(kvm,
928 &kvm_userspace_mem, 0);
929 if (r)
930 goto out;
931 break;
932 }
933 case KVM_CREATE_IRQCHIP:
934 r = -EFAULT;
935 r = kvm_ioapic_init(kvm);
936 if (r)
937 goto out;
Avi Kivity399ec802008-11-19 13:58:46 +0200938 r = kvm_setup_default_irq_routing(kvm);
939 if (r) {
940 kfree(kvm->arch.vioapic);
941 goto out;
942 }
Xiantao Zhangb024b792008-04-01 15:29:29 +0800943 break;
Gleb Natapov49256632009-02-04 17:28:14 +0200944 case KVM_IRQ_LINE_STATUS:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800945 case KVM_IRQ_LINE: {
946 struct kvm_irq_level irq_event;
947
948 r = -EFAULT;
949 if (copy_from_user(&irq_event, argp, sizeof irq_event))
950 goto out;
951 if (irqchip_in_kernel(kvm)) {
Gleb Natapov49256632009-02-04 17:28:14 +0200952 __s32 status;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800953 mutex_lock(&kvm->lock);
Gleb Natapov49256632009-02-04 17:28:14 +0200954 status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
Sheng Yang5550af42008-10-15 20:15:06 +0800955 irq_event.irq, irq_event.level);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800956 mutex_unlock(&kvm->lock);
Gleb Natapov49256632009-02-04 17:28:14 +0200957 if (ioctl == KVM_IRQ_LINE_STATUS) {
958 irq_event.status = status;
959 if (copy_to_user(argp, &irq_event,
960 sizeof irq_event))
961 goto out;
962 }
Xiantao Zhangb024b792008-04-01 15:29:29 +0800963 r = 0;
964 }
965 break;
966 }
967 case KVM_GET_IRQCHIP: {
968 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
969 struct kvm_irqchip chip;
970
971 r = -EFAULT;
972 if (copy_from_user(&chip, argp, sizeof chip))
973 goto out;
974 r = -ENXIO;
975 if (!irqchip_in_kernel(kvm))
976 goto out;
977 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
978 if (r)
979 goto out;
980 r = -EFAULT;
981 if (copy_to_user(argp, &chip, sizeof chip))
982 goto out;
983 r = 0;
984 break;
985 }
986 case KVM_SET_IRQCHIP: {
987 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
988 struct kvm_irqchip chip;
989
990 r = -EFAULT;
991 if (copy_from_user(&chip, argp, sizeof chip))
992 goto out;
993 r = -ENXIO;
994 if (!irqchip_in_kernel(kvm))
995 goto out;
996 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
997 if (r)
998 goto out;
999 r = 0;
1000 break;
1001 }
1002 default:
1003 ;
1004 }
1005out:
1006 return r;
1007}
1008
1009int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1010 struct kvm_sregs *sregs)
1011{
1012 return -EINVAL;
1013}
1014
1015int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1016 struct kvm_sregs *sregs)
1017{
1018 return -EINVAL;
1019
1020}
1021int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1022 struct kvm_translation *tr)
1023{
1024
1025 return -EINVAL;
1026}
1027
1028static int kvm_alloc_vmm_area(void)
1029{
1030 if (!kvm_vmm_base && (kvm_vm_buffer_size < KVM_VM_BUFFER_SIZE)) {
1031 kvm_vmm_base = __get_free_pages(GFP_KERNEL,
1032 get_order(KVM_VMM_SIZE));
1033 if (!kvm_vmm_base)
1034 return -ENOMEM;
1035
1036 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1037 kvm_vm_buffer = kvm_vmm_base + VMM_SIZE;
1038
1039 printk(KERN_DEBUG"kvm:VMM's Base Addr:0x%lx, vm_buffer:0x%lx\n",
1040 kvm_vmm_base, kvm_vm_buffer);
1041 }
1042
1043 return 0;
1044}
1045
1046static void kvm_free_vmm_area(void)
1047{
1048 if (kvm_vmm_base) {
1049 /*Zero this area before free to avoid bits leak!!*/
1050 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1051 free_pages(kvm_vmm_base, get_order(KVM_VMM_SIZE));
1052 kvm_vmm_base = 0;
1053 kvm_vm_buffer = 0;
1054 kvm_vsa_base = 0;
1055 }
1056}
1057
Xiantao Zhangb024b792008-04-01 15:29:29 +08001058static void vti_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1059{
1060}
1061
1062static int vti_init_vpd(struct kvm_vcpu *vcpu)
1063{
1064 int i;
1065 union cpuid3_t cpuid3;
1066 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1067
1068 if (IS_ERR(vpd))
1069 return PTR_ERR(vpd);
1070
1071 /* CPUID init */
1072 for (i = 0; i < 5; i++)
1073 vpd->vcpuid[i] = ia64_get_cpuid(i);
1074
1075 /* Limit the CPUID number to 5 */
1076 cpuid3.value = vpd->vcpuid[3];
1077 cpuid3.number = 4; /* 5 - 1 */
1078 vpd->vcpuid[3] = cpuid3.value;
1079
1080 /*Set vac and vdc fields*/
1081 vpd->vac.a_from_int_cr = 1;
1082 vpd->vac.a_to_int_cr = 1;
1083 vpd->vac.a_from_psr = 1;
1084 vpd->vac.a_from_cpuid = 1;
1085 vpd->vac.a_cover = 1;
1086 vpd->vac.a_bsw = 1;
1087 vpd->vac.a_int = 1;
1088 vpd->vdc.d_vmsw = 1;
1089
1090 /*Set virtual buffer*/
1091 vpd->virt_env_vaddr = KVM_VM_BUFFER_BASE;
1092
1093 return 0;
1094}
1095
1096static int vti_create_vp(struct kvm_vcpu *vcpu)
1097{
1098 long ret;
1099 struct vpd *vpd = vcpu->arch.vpd;
1100 unsigned long vmm_ivt;
1101
1102 vmm_ivt = kvm_vmm_info->vmm_ivt;
1103
1104 printk(KERN_DEBUG "kvm: vcpu:%p,ivt: 0x%lx\n", vcpu, vmm_ivt);
1105
1106 ret = ia64_pal_vp_create((u64 *)vpd, (u64 *)vmm_ivt, 0);
1107
1108 if (ret) {
1109 printk(KERN_ERR"kvm: ia64_pal_vp_create failed!\n");
1110 return -EINVAL;
1111 }
1112 return 0;
1113}
1114
1115static void init_ptce_info(struct kvm_vcpu *vcpu)
1116{
1117 ia64_ptce_info_t ptce = {0};
1118
1119 ia64_get_ptce(&ptce);
1120 vcpu->arch.ptce_base = ptce.base;
1121 vcpu->arch.ptce_count[0] = ptce.count[0];
1122 vcpu->arch.ptce_count[1] = ptce.count[1];
1123 vcpu->arch.ptce_stride[0] = ptce.stride[0];
1124 vcpu->arch.ptce_stride[1] = ptce.stride[1];
1125}
1126
1127static void kvm_migrate_hlt_timer(struct kvm_vcpu *vcpu)
1128{
1129 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
1130
1131 if (hrtimer_cancel(p_ht))
Arjan van de Ven18dd36a2008-09-01 15:19:11 -07001132 hrtimer_start_expires(p_ht, HRTIMER_MODE_ABS);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001133}
1134
1135static enum hrtimer_restart hlt_timer_fn(struct hrtimer *data)
1136{
1137 struct kvm_vcpu *vcpu;
1138 wait_queue_head_t *q;
1139
1140 vcpu = container_of(data, struct kvm_vcpu, arch.hlt_timer);
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001141 q = &vcpu->wq;
1142
Avi Kivitya4535292008-04-13 17:54:35 +03001143 if (vcpu->arch.mp_state != KVM_MP_STATE_HALTED)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001144 goto out;
1145
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001146 if (waitqueue_active(q))
Xiantao Zhangb024b792008-04-01 15:29:29 +08001147 wake_up_interruptible(q);
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001148
Xiantao Zhangb024b792008-04-01 15:29:29 +08001149out:
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001150 vcpu->arch.timer_fired = 1;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001151 vcpu->arch.timer_check = 1;
1152 return HRTIMER_NORESTART;
1153}
1154
1155#define PALE_RESET_ENTRY 0x80000000ffffffb0UL
1156
1157int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1158{
1159 struct kvm_vcpu *v;
1160 int r;
1161 int i;
1162 long itc_offset;
1163 struct kvm *kvm = vcpu->kvm;
1164 struct kvm_pt_regs *regs = vcpu_regs(vcpu);
1165
1166 union context *p_ctx = &vcpu->arch.guest;
1167 struct kvm_vcpu *vmm_vcpu = to_guest(vcpu->kvm, vcpu);
1168
1169 /*Init vcpu context for first run.*/
1170 if (IS_ERR(vmm_vcpu))
1171 return PTR_ERR(vmm_vcpu);
1172
1173 if (vcpu->vcpu_id == 0) {
Avi Kivitya4535292008-04-13 17:54:35 +03001174 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001175
1176 /*Set entry address for first run.*/
1177 regs->cr_iip = PALE_RESET_ENTRY;
1178
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001179 /*Initialize itc offset for vcpus*/
Xiantao Zhangb024b792008-04-01 15:29:29 +08001180 itc_offset = 0UL - ia64_getreg(_IA64_REG_AR_ITC);
Jes Sorensen934d5342009-01-21 15:16:43 +01001181 for (i = 0; i < kvm->arch.online_vcpus; i++) {
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001182 v = (struct kvm_vcpu *)((char *)vcpu +
1183 sizeof(struct kvm_vcpu_data) * i);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001184 v->arch.itc_offset = itc_offset;
1185 v->arch.last_itc = 0;
1186 }
1187 } else
Avi Kivitya4535292008-04-13 17:54:35 +03001188 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001189
1190 r = -ENOMEM;
1191 vcpu->arch.apic = kzalloc(sizeof(struct kvm_lapic), GFP_KERNEL);
1192 if (!vcpu->arch.apic)
1193 goto out;
1194 vcpu->arch.apic->vcpu = vcpu;
1195
1196 p_ctx->gr[1] = 0;
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001197 p_ctx->gr[12] = (unsigned long)((char *)vmm_vcpu + KVM_STK_OFFSET);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001198 p_ctx->gr[13] = (unsigned long)vmm_vcpu;
1199 p_ctx->psr = 0x1008522000UL;
1200 p_ctx->ar[40] = FPSR_DEFAULT; /*fpsr*/
1201 p_ctx->caller_unat = 0;
1202 p_ctx->pr = 0x0;
1203 p_ctx->ar[36] = 0x0; /*unat*/
1204 p_ctx->ar[19] = 0x0; /*rnat*/
1205 p_ctx->ar[18] = (unsigned long)vmm_vcpu +
1206 ((sizeof(struct kvm_vcpu)+15) & ~15);
1207 p_ctx->ar[64] = 0x0; /*pfs*/
1208 p_ctx->cr[0] = 0x7e04UL;
1209 p_ctx->cr[2] = (unsigned long)kvm_vmm_info->vmm_ivt;
1210 p_ctx->cr[8] = 0x3c;
1211
1212 /*Initilize region register*/
1213 p_ctx->rr[0] = 0x30;
1214 p_ctx->rr[1] = 0x30;
1215 p_ctx->rr[2] = 0x30;
1216 p_ctx->rr[3] = 0x30;
1217 p_ctx->rr[4] = 0x30;
1218 p_ctx->rr[5] = 0x30;
1219 p_ctx->rr[7] = 0x30;
1220
1221 /*Initilize branch register 0*/
1222 p_ctx->br[0] = *(unsigned long *)kvm_vmm_info->vmm_entry;
1223
1224 vcpu->arch.vmm_rr = kvm->arch.vmm_init_rr;
1225 vcpu->arch.metaphysical_rr0 = kvm->arch.metaphysical_rr0;
1226 vcpu->arch.metaphysical_rr4 = kvm->arch.metaphysical_rr4;
1227
1228 hrtimer_init(&vcpu->arch.hlt_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1229 vcpu->arch.hlt_timer.function = hlt_timer_fn;
1230
1231 vcpu->arch.last_run_cpu = -1;
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001232 vcpu->arch.vpd = (struct vpd *)VPD_BASE(vcpu->vcpu_id);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001233 vcpu->arch.vsa_base = kvm_vsa_base;
1234 vcpu->arch.__gp = kvm_vmm_gp;
1235 vcpu->arch.dirty_log_lock_pa = __pa(&kvm->arch.dirty_log_lock);
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001236 vcpu->arch.vhpt.hash = (struct thash_data *)VHPT_BASE(vcpu->vcpu_id);
1237 vcpu->arch.vtlb.hash = (struct thash_data *)VTLB_BASE(vcpu->vcpu_id);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001238 init_ptce_info(vcpu);
1239
1240 r = 0;
1241out:
1242 return r;
1243}
1244
1245static int vti_vcpu_setup(struct kvm_vcpu *vcpu, int id)
1246{
1247 unsigned long psr;
1248 int r;
1249
1250 local_irq_save(psr);
1251 r = kvm_insert_vmm_mapping(vcpu);
1252 if (r)
1253 goto fail;
1254 r = kvm_vcpu_init(vcpu, vcpu->kvm, id);
1255 if (r)
1256 goto fail;
1257
1258 r = vti_init_vpd(vcpu);
1259 if (r) {
1260 printk(KERN_DEBUG"kvm: vpd init error!!\n");
1261 goto uninit;
1262 }
1263
1264 r = vti_create_vp(vcpu);
1265 if (r)
1266 goto uninit;
1267
1268 kvm_purge_vmm_mapping(vcpu);
1269 local_irq_restore(psr);
1270
1271 return 0;
1272uninit:
1273 kvm_vcpu_uninit(vcpu);
1274fail:
Julia Lawallcab7a1e2008-07-22 21:38:18 +02001275 local_irq_restore(psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001276 return r;
1277}
1278
1279struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1280 unsigned int id)
1281{
1282 struct kvm_vcpu *vcpu;
1283 unsigned long vm_base = kvm->arch.vm_base;
1284 int r;
1285 int cpu;
1286
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001287 BUG_ON(sizeof(struct kvm_vcpu) > VCPU_STRUCT_SIZE/2);
1288
1289 r = -EINVAL;
1290 if (id >= KVM_MAX_VCPUS) {
1291 printk(KERN_ERR"kvm: Can't configure vcpus > %ld",
1292 KVM_MAX_VCPUS);
1293 goto fail;
1294 }
1295
Xiantao Zhangb024b792008-04-01 15:29:29 +08001296 r = -ENOMEM;
1297 if (!vm_base) {
1298 printk(KERN_ERR"kvm: Create vcpu[%d] error!\n", id);
1299 goto fail;
1300 }
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001301 vcpu = (struct kvm_vcpu *)(vm_base + offsetof(struct kvm_vm_data,
1302 vcpu_data[id].vcpu_struct));
Xiantao Zhangb024b792008-04-01 15:29:29 +08001303 vcpu->kvm = kvm;
1304
1305 cpu = get_cpu();
1306 vti_vcpu_load(vcpu, cpu);
1307 r = vti_vcpu_setup(vcpu, id);
1308 put_cpu();
1309
1310 if (r) {
1311 printk(KERN_DEBUG"kvm: vcpu_setup error!!\n");
1312 goto fail;
1313 }
1314
Jes Sorensen934d5342009-01-21 15:16:43 +01001315 kvm->arch.online_vcpus++;
1316
Xiantao Zhangb024b792008-04-01 15:29:29 +08001317 return vcpu;
1318fail:
1319 return ERR_PTR(r);
1320}
1321
1322int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1323{
1324 return 0;
1325}
1326
1327int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1328{
1329 return -EINVAL;
1330}
1331
1332int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1333{
1334 return -EINVAL;
1335}
1336
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001337int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1338 struct kvm_guest_debug *dbg)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001339{
1340 return -EINVAL;
1341}
1342
1343static void free_kvm(struct kvm *kvm)
1344{
1345 unsigned long vm_base = kvm->arch.vm_base;
1346
1347 if (vm_base) {
1348 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
1349 free_pages(vm_base, get_order(KVM_VM_DATA_SIZE));
1350 }
1351
1352}
1353
1354static void kvm_release_vm_pages(struct kvm *kvm)
1355{
1356 struct kvm_memory_slot *memslot;
1357 int i, j;
1358 unsigned long base_gfn;
1359
1360 for (i = 0; i < kvm->nmemslots; i++) {
1361 memslot = &kvm->memslots[i];
1362 base_gfn = memslot->base_gfn;
1363
1364 for (j = 0; j < memslot->npages; j++) {
1365 if (memslot->rmap[j])
1366 put_page((struct page *)memslot->rmap[j]);
1367 }
1368 }
1369}
1370
Sheng Yangad8ba2c2009-01-06 10:03:02 +08001371void kvm_arch_sync_events(struct kvm *kvm)
1372{
1373}
1374
Xiantao Zhangb024b792008-04-01 15:29:29 +08001375void kvm_arch_destroy_vm(struct kvm *kvm)
1376{
Xiantao Zhang2381ad22008-10-08 08:29:33 +08001377 kvm_iommu_unmap_guest(kvm);
1378#ifdef KVM_CAP_DEVICE_ASSIGNMENT
1379 kvm_free_all_assigned_devices(kvm);
1380#endif
Xiantao Zhangb024b792008-04-01 15:29:29 +08001381 kfree(kvm->arch.vioapic);
1382 kvm_release_vm_pages(kvm);
1383 kvm_free_physmem(kvm);
1384 free_kvm(kvm);
1385}
1386
1387void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1388{
1389}
1390
1391void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1392{
1393 if (cpu != vcpu->cpu) {
1394 vcpu->cpu = cpu;
1395 if (vcpu->arch.ht_active)
1396 kvm_migrate_hlt_timer(vcpu);
1397 }
1398}
1399
1400#define SAVE_REGS(_x) regs->_x = vcpu->arch._x
1401
1402int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1403{
Xiantao Zhangb024b792008-04-01 15:29:29 +08001404 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
Jes Sorensen042b26e2008-12-16 16:45:47 +01001405 int i;
1406
Xiantao Zhangb024b792008-04-01 15:29:29 +08001407 vcpu_load(vcpu);
1408
1409 for (i = 0; i < 16; i++) {
1410 regs->vpd.vgr[i] = vpd->vgr[i];
1411 regs->vpd.vbgr[i] = vpd->vbgr[i];
1412 }
1413 for (i = 0; i < 128; i++)
1414 regs->vpd.vcr[i] = vpd->vcr[i];
1415 regs->vpd.vhpi = vpd->vhpi;
1416 regs->vpd.vnat = vpd->vnat;
1417 regs->vpd.vbnat = vpd->vbnat;
1418 regs->vpd.vpsr = vpd->vpsr;
1419 regs->vpd.vpr = vpd->vpr;
1420
Jes Sorensen042b26e2008-12-16 16:45:47 +01001421 memcpy(&regs->saved_guest, &vcpu->arch.guest, sizeof(union context));
1422
Xiantao Zhangb024b792008-04-01 15:29:29 +08001423 SAVE_REGS(mp_state);
1424 SAVE_REGS(vmm_rr);
1425 memcpy(regs->itrs, vcpu->arch.itrs, sizeof(struct thash_data) * NITRS);
1426 memcpy(regs->dtrs, vcpu->arch.dtrs, sizeof(struct thash_data) * NDTRS);
1427 SAVE_REGS(itr_regions);
1428 SAVE_REGS(dtr_regions);
1429 SAVE_REGS(tc_regions);
1430 SAVE_REGS(irq_check);
1431 SAVE_REGS(itc_check);
1432 SAVE_REGS(timer_check);
1433 SAVE_REGS(timer_pending);
1434 SAVE_REGS(last_itc);
1435 for (i = 0; i < 8; i++) {
1436 regs->vrr[i] = vcpu->arch.vrr[i];
1437 regs->ibr[i] = vcpu->arch.ibr[i];
1438 regs->dbr[i] = vcpu->arch.dbr[i];
1439 }
1440 for (i = 0; i < 4; i++)
1441 regs->insvc[i] = vcpu->arch.insvc[i];
1442 regs->saved_itc = vcpu->arch.itc_offset + ia64_getreg(_IA64_REG_AR_ITC);
1443 SAVE_REGS(xtp);
1444 SAVE_REGS(metaphysical_rr0);
1445 SAVE_REGS(metaphysical_rr4);
1446 SAVE_REGS(metaphysical_saved_rr0);
1447 SAVE_REGS(metaphysical_saved_rr4);
1448 SAVE_REGS(fp_psr);
1449 SAVE_REGS(saved_gp);
Jes Sorensen042b26e2008-12-16 16:45:47 +01001450
Xiantao Zhangb024b792008-04-01 15:29:29 +08001451 vcpu_put(vcpu);
Jes Sorensen042b26e2008-12-16 16:45:47 +01001452 return 0;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001453}
1454
Jes Sorensene9a999f2008-12-18 12:17:51 +01001455int kvm_arch_vcpu_ioctl_get_stack(struct kvm_vcpu *vcpu,
1456 struct kvm_ia64_vcpu_stack *stack)
1457{
1458 memcpy(stack, vcpu, sizeof(struct kvm_ia64_vcpu_stack));
1459 return 0;
1460}
1461
1462int kvm_arch_vcpu_ioctl_set_stack(struct kvm_vcpu *vcpu,
1463 struct kvm_ia64_vcpu_stack *stack)
1464{
1465 memcpy(vcpu + 1, &stack->stack[0] + sizeof(struct kvm_vcpu),
1466 sizeof(struct kvm_ia64_vcpu_stack) - sizeof(struct kvm_vcpu));
1467
1468 vcpu->arch.exit_data = ((struct kvm_vcpu *)stack)->arch.exit_data;
1469 return 0;
1470}
1471
Xiantao Zhangb024b792008-04-01 15:29:29 +08001472void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1473{
1474
1475 hrtimer_cancel(&vcpu->arch.hlt_timer);
1476 kfree(vcpu->arch.apic);
1477}
1478
1479
1480long kvm_arch_vcpu_ioctl(struct file *filp,
Jes Sorensene9a999f2008-12-18 12:17:51 +01001481 unsigned int ioctl, unsigned long arg)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001482{
Jes Sorensene9a999f2008-12-18 12:17:51 +01001483 struct kvm_vcpu *vcpu = filp->private_data;
1484 void __user *argp = (void __user *)arg;
1485 struct kvm_ia64_vcpu_stack *stack = NULL;
1486 long r;
1487
1488 switch (ioctl) {
1489 case KVM_IA64_VCPU_GET_STACK: {
1490 struct kvm_ia64_vcpu_stack __user *user_stack;
1491 void __user *first_p = argp;
1492
1493 r = -EFAULT;
1494 if (copy_from_user(&user_stack, first_p, sizeof(void *)))
1495 goto out;
1496
1497 if (!access_ok(VERIFY_WRITE, user_stack,
1498 sizeof(struct kvm_ia64_vcpu_stack))) {
1499 printk(KERN_INFO "KVM_IA64_VCPU_GET_STACK: "
1500 "Illegal user destination address for stack\n");
1501 goto out;
1502 }
1503 stack = kzalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL);
1504 if (!stack) {
1505 r = -ENOMEM;
1506 goto out;
1507 }
1508
1509 r = kvm_arch_vcpu_ioctl_get_stack(vcpu, stack);
1510 if (r)
1511 goto out;
1512
1513 if (copy_to_user(user_stack, stack,
1514 sizeof(struct kvm_ia64_vcpu_stack)))
1515 goto out;
1516
1517 break;
1518 }
1519 case KVM_IA64_VCPU_SET_STACK: {
1520 struct kvm_ia64_vcpu_stack __user *user_stack;
1521 void __user *first_p = argp;
1522
1523 r = -EFAULT;
1524 if (copy_from_user(&user_stack, first_p, sizeof(void *)))
1525 goto out;
1526
1527 if (!access_ok(VERIFY_READ, user_stack,
1528 sizeof(struct kvm_ia64_vcpu_stack))) {
1529 printk(KERN_INFO "KVM_IA64_VCPU_SET_STACK: "
1530 "Illegal user address for stack\n");
1531 goto out;
1532 }
1533 stack = kmalloc(sizeof(struct kvm_ia64_vcpu_stack), GFP_KERNEL);
1534 if (!stack) {
1535 r = -ENOMEM;
1536 goto out;
1537 }
1538 if (copy_from_user(stack, user_stack,
1539 sizeof(struct kvm_ia64_vcpu_stack)))
1540 goto out;
1541
1542 r = kvm_arch_vcpu_ioctl_set_stack(vcpu, stack);
1543 break;
1544 }
1545
1546 default:
1547 r = -EINVAL;
1548 }
1549
1550out:
1551 kfree(stack);
1552 return r;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001553}
1554
1555int kvm_arch_set_memory_region(struct kvm *kvm,
1556 struct kvm_userspace_memory_region *mem,
1557 struct kvm_memory_slot old,
1558 int user_alloc)
1559{
1560 unsigned long i;
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001561 unsigned long pfn;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001562 int npages = mem->memory_size >> PAGE_SHIFT;
1563 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
1564 unsigned long base_gfn = memslot->base_gfn;
1565
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001566 if (base_gfn + npages > (KVM_MAX_MEM_SIZE >> PAGE_SHIFT))
1567 return -ENOMEM;
1568
Xiantao Zhangb024b792008-04-01 15:29:29 +08001569 for (i = 0; i < npages; i++) {
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001570 pfn = gfn_to_pfn(kvm, base_gfn + i);
1571 if (!kvm_is_mmio_pfn(pfn)) {
1572 kvm_set_pmt_entry(kvm, base_gfn + i,
1573 pfn << PAGE_SHIFT,
Xiantao Zhangb010eb52008-09-28 01:39:46 -07001574 _PAGE_AR_RWX | _PAGE_MA_WB);
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001575 memslot->rmap[i] = (unsigned long)pfn_to_page(pfn);
1576 } else {
1577 kvm_set_pmt_entry(kvm, base_gfn + i,
Xiantao Zhangb010eb52008-09-28 01:39:46 -07001578 GPFN_PHYS_MMIO | (pfn << PAGE_SHIFT),
Xiantao Zhang1cbea802008-10-03 14:58:09 +08001579 _PAGE_MA_UC);
1580 memslot->rmap[i] = 0;
1581 }
Xiantao Zhangb024b792008-04-01 15:29:29 +08001582 }
1583
1584 return 0;
1585}
1586
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001587void kvm_arch_flush_shadow(struct kvm *kvm)
1588{
1589}
Xiantao Zhangb024b792008-04-01 15:29:29 +08001590
1591long kvm_arch_dev_ioctl(struct file *filp,
Jes Sorensene9a999f2008-12-18 12:17:51 +01001592 unsigned int ioctl, unsigned long arg)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001593{
1594 return -EINVAL;
1595}
1596
1597void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1598{
1599 kvm_vcpu_uninit(vcpu);
1600}
1601
1602static int vti_cpu_has_kvm_support(void)
1603{
1604 long avail = 1, status = 1, control = 1;
1605 long ret;
1606
1607 ret = ia64_pal_proc_get_features(&avail, &status, &control, 0);
1608 if (ret)
1609 goto out;
1610
1611 if (!(avail & PAL_PROC_VM_BIT))
1612 goto out;
1613
1614 printk(KERN_DEBUG"kvm: Hardware Supports VT\n");
1615
1616 ret = ia64_pal_vp_env_info(&kvm_vm_buffer_size, &vp_env_info);
1617 if (ret)
1618 goto out;
1619 printk(KERN_DEBUG"kvm: VM Buffer Size:0x%lx\n", kvm_vm_buffer_size);
1620
1621 if (!(vp_env_info & VP_OPCODE)) {
1622 printk(KERN_WARNING"kvm: No opcode ability on hardware, "
1623 "vm_env_info:0x%lx\n", vp_env_info);
1624 }
1625
1626 return 1;
1627out:
1628 return 0;
1629}
1630
1631static int kvm_relocate_vmm(struct kvm_vmm_info *vmm_info,
1632 struct module *module)
1633{
1634 unsigned long module_base;
1635 unsigned long vmm_size;
1636
1637 unsigned long vmm_offset, func_offset, fdesc_offset;
1638 struct fdesc *p_fdesc;
1639
1640 BUG_ON(!module);
1641
1642 if (!kvm_vmm_base) {
1643 printk("kvm: kvm area hasn't been initilized yet!!\n");
1644 return -EFAULT;
1645 }
1646
1647 /*Calculate new position of relocated vmm module.*/
1648 module_base = (unsigned long)module->module_core;
1649 vmm_size = module->core_size;
1650 if (unlikely(vmm_size > KVM_VMM_SIZE))
1651 return -EFAULT;
1652
1653 memcpy((void *)kvm_vmm_base, (void *)module_base, vmm_size);
1654 kvm_flush_icache(kvm_vmm_base, vmm_size);
1655
1656 /*Recalculate kvm_vmm_info based on new VMM*/
1657 vmm_offset = vmm_info->vmm_ivt - module_base;
1658 kvm_vmm_info->vmm_ivt = KVM_VMM_BASE + vmm_offset;
1659 printk(KERN_DEBUG"kvm: Relocated VMM's IVT Base Addr:%lx\n",
1660 kvm_vmm_info->vmm_ivt);
1661
1662 fdesc_offset = (unsigned long)vmm_info->vmm_entry - module_base;
1663 kvm_vmm_info->vmm_entry = (kvm_vmm_entry *)(KVM_VMM_BASE +
1664 fdesc_offset);
1665 func_offset = *(unsigned long *)vmm_info->vmm_entry - module_base;
1666 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1667 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1668 p_fdesc->gp = KVM_VMM_BASE+(p_fdesc->gp - module_base);
1669
1670 printk(KERN_DEBUG"kvm: Relocated VMM's Init Entry Addr:%lx\n",
1671 KVM_VMM_BASE+func_offset);
1672
1673 fdesc_offset = (unsigned long)vmm_info->tramp_entry - module_base;
1674 kvm_vmm_info->tramp_entry = (kvm_tramp_entry *)(KVM_VMM_BASE +
1675 fdesc_offset);
1676 func_offset = *(unsigned long *)vmm_info->tramp_entry - module_base;
1677 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1678 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1679 p_fdesc->gp = KVM_VMM_BASE + (p_fdesc->gp - module_base);
1680
1681 kvm_vmm_gp = p_fdesc->gp;
1682
1683 printk(KERN_DEBUG"kvm: Relocated VMM's Entry IP:%p\n",
1684 kvm_vmm_info->vmm_entry);
1685 printk(KERN_DEBUG"kvm: Relocated VMM's Trampoline Entry IP:0x%lx\n",
1686 KVM_VMM_BASE + func_offset);
1687
1688 return 0;
1689}
1690
1691int kvm_arch_init(void *opaque)
1692{
1693 int r;
1694 struct kvm_vmm_info *vmm_info = (struct kvm_vmm_info *)opaque;
1695
1696 if (!vti_cpu_has_kvm_support()) {
1697 printk(KERN_ERR "kvm: No Hardware Virtualization Support!\n");
1698 r = -EOPNOTSUPP;
1699 goto out;
1700 }
1701
1702 if (kvm_vmm_info) {
1703 printk(KERN_ERR "kvm: Already loaded VMM module!\n");
1704 r = -EEXIST;
1705 goto out;
1706 }
1707
1708 r = -ENOMEM;
1709 kvm_vmm_info = kzalloc(sizeof(struct kvm_vmm_info), GFP_KERNEL);
1710 if (!kvm_vmm_info)
1711 goto out;
1712
1713 if (kvm_alloc_vmm_area())
1714 goto out_free0;
1715
1716 r = kvm_relocate_vmm(vmm_info, vmm_info->module);
1717 if (r)
1718 goto out_free1;
1719
1720 return 0;
1721
1722out_free1:
1723 kvm_free_vmm_area();
1724out_free0:
1725 kfree(kvm_vmm_info);
1726out:
1727 return r;
1728}
1729
1730void kvm_arch_exit(void)
1731{
1732 kvm_free_vmm_area();
1733 kfree(kvm_vmm_info);
1734 kvm_vmm_info = NULL;
1735}
1736
1737static int kvm_ia64_sync_dirty_log(struct kvm *kvm,
1738 struct kvm_dirty_log *log)
1739{
1740 struct kvm_memory_slot *memslot;
1741 int r, i;
1742 long n, base;
Xiantao Zhanga917f7a2008-10-23 14:56:44 +08001743 unsigned long *dirty_bitmap = (unsigned long *)(kvm->arch.vm_base +
1744 offsetof(struct kvm_vm_data, kvm_mem_dirty_log));
Xiantao Zhangb024b792008-04-01 15:29:29 +08001745
1746 r = -EINVAL;
1747 if (log->slot >= KVM_MEMORY_SLOTS)
1748 goto out;
1749
1750 memslot = &kvm->memslots[log->slot];
1751 r = -ENOENT;
1752 if (!memslot->dirty_bitmap)
1753 goto out;
1754
1755 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1756 base = memslot->base_gfn / BITS_PER_LONG;
1757
1758 for (i = 0; i < n/sizeof(long); ++i) {
1759 memslot->dirty_bitmap[i] = dirty_bitmap[base + i];
1760 dirty_bitmap[base + i] = 0;
1761 }
1762 r = 0;
1763out:
1764 return r;
1765}
1766
1767int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1768 struct kvm_dirty_log *log)
1769{
1770 int r;
1771 int n;
1772 struct kvm_memory_slot *memslot;
1773 int is_dirty = 0;
1774
1775 spin_lock(&kvm->arch.dirty_log_lock);
1776
1777 r = kvm_ia64_sync_dirty_log(kvm, log);
1778 if (r)
1779 goto out;
1780
1781 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1782 if (r)
1783 goto out;
1784
1785 /* If nothing is dirty, don't bother messing with page tables. */
1786 if (is_dirty) {
1787 kvm_flush_remote_tlbs(kvm);
1788 memslot = &kvm->memslots[log->slot];
1789 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1790 memset(memslot->dirty_bitmap, 0, n);
1791 }
1792 r = 0;
1793out:
1794 spin_unlock(&kvm->arch.dirty_log_lock);
1795 return r;
1796}
1797
1798int kvm_arch_hardware_setup(void)
1799{
1800 return 0;
1801}
1802
1803void kvm_arch_hardware_unsetup(void)
1804{
1805}
1806
1807static void vcpu_kick_intr(void *info)
1808{
1809#ifdef DEBUG
1810 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
1811 printk(KERN_DEBUG"vcpu_kick_intr %p \n", vcpu);
1812#endif
1813}
1814
1815void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1816{
1817 int ipi_pcpu = vcpu->cpu;
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001818 int cpu = get_cpu();
Xiantao Zhangb024b792008-04-01 15:29:29 +08001819
1820 if (waitqueue_active(&vcpu->wq))
1821 wake_up_interruptible(&vcpu->wq);
1822
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001823 if (vcpu->guest_mode && cpu != ipi_pcpu)
Takashi Iwai2f73cca2008-07-17 18:09:12 +02001824 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001825 put_cpu();
Xiantao Zhangb024b792008-04-01 15:29:29 +08001826}
1827
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001828int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001829{
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001830 return __apic_accept_irq(vcpu, irq->vector);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001831}
1832
1833int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
1834{
1835 return apic->vcpu->vcpu_id == dest;
1836}
1837
1838int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
1839{
1840 return 0;
1841}
1842
Gleb Natapove1035712009-03-05 16:34:59 +02001843int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001844{
Gleb Natapove1035712009-03-05 16:34:59 +02001845 return vcpu1->arch.xtp - vcpu2->arch.xtp;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001846}
1847
Gleb Natapov343f94f2009-03-05 16:34:54 +02001848int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
1849 int short_hand, int dest, int dest_mode)
1850{
Gleb Natapov58c2dde2009-03-05 16:35:04 +02001851 struct kvm_lapic *target = vcpu->arch.apic;
Gleb Natapov343f94f2009-03-05 16:34:54 +02001852 return (dest_mode == 0) ?
1853 kvm_apic_match_physical_addr(target, dest) :
1854 kvm_apic_match_logical_addr(target, dest);
1855}
1856
Xiantao Zhangb024b792008-04-01 15:29:29 +08001857static int find_highest_bits(int *dat)
1858{
1859 u32 bits, bitnum;
1860 int i;
1861
1862 /* loop for all 256 bits */
1863 for (i = 7; i >= 0 ; i--) {
1864 bits = dat[i];
1865 if (bits) {
1866 bitnum = fls(bits);
1867 return i * 32 + bitnum - 1;
1868 }
1869 }
1870
1871 return -1;
1872}
1873
1874int kvm_highest_pending_irq(struct kvm_vcpu *vcpu)
1875{
1876 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1877
1878 if (vpd->irr[0] & (1UL << NMI_VECTOR))
1879 return NMI_VECTOR;
1880 if (vpd->irr[0] & (1UL << ExtINT_VECTOR))
1881 return ExtINT_VECTOR;
1882
1883 return find_highest_bits((int *)&vpd->irr[0]);
1884}
1885
1886int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
1887{
1888 if (kvm_highest_pending_irq(vcpu) != -1)
1889 return 1;
1890 return 0;
1891}
1892
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001893int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1894{
Xiantao Zhangdecc9012008-10-16 15:58:15 +08001895 return vcpu->arch.timer_fired;
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001896}
1897
Xiantao Zhangb024b792008-04-01 15:29:29 +08001898gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1899{
1900 return gfn;
1901}
1902
1903int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1904{
Avi Kivitya4535292008-04-13 17:54:35 +03001905 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001906}
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001907
1908int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1909 struct kvm_mp_state *mp_state)
1910{
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001911 vcpu_load(vcpu);
1912 mp_state->mp_state = vcpu->arch.mp_state;
1913 vcpu_put(vcpu);
1914 return 0;
1915}
1916
1917static int vcpu_reset(struct kvm_vcpu *vcpu)
1918{
1919 int r;
1920 long psr;
1921 local_irq_save(psr);
1922 r = kvm_insert_vmm_mapping(vcpu);
1923 if (r)
1924 goto fail;
1925
1926 vcpu->arch.launched = 0;
1927 kvm_arch_vcpu_uninit(vcpu);
1928 r = kvm_arch_vcpu_init(vcpu);
1929 if (r)
1930 goto fail;
1931
1932 kvm_purge_vmm_mapping(vcpu);
1933 r = 0;
1934fail:
1935 local_irq_restore(psr);
1936 return r;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001937}
1938
1939int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1940 struct kvm_mp_state *mp_state)
1941{
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001942 int r = 0;
1943
1944 vcpu_load(vcpu);
1945 vcpu->arch.mp_state = mp_state->mp_state;
1946 if (vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)
1947 r = vcpu_reset(vcpu);
1948 vcpu_put(vcpu);
1949 return r;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001950}