blob: a6cf719811b7af85aa9949aa5a03a6ba2839afea [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>
34
35#include <asm/pgtable.h>
36#include <asm/gcc_intrin.h>
37#include <asm/pal.h>
38#include <asm/cacheflush.h>
39#include <asm/div64.h>
40#include <asm/tlb.h>
Jes Sorensen9f726322008-09-12 14:12:08 +020041#include <asm/elf.h>
Xiantao Zhangb024b792008-04-01 15:29:29 +080042
43#include "misc.h"
44#include "vti.h"
45#include "iodev.h"
46#include "ioapic.h"
47#include "lapic.h"
Xiantao Zhang2f749772008-09-27 11:46:36 +080048#include "irq.h"
Xiantao Zhangb024b792008-04-01 15:29:29 +080049
50static unsigned long kvm_vmm_base;
51static unsigned long kvm_vsa_base;
52static unsigned long kvm_vm_buffer;
53static unsigned long kvm_vm_buffer_size;
54unsigned long kvm_vmm_gp;
55
56static long vp_env_info;
57
58static struct kvm_vmm_info *kvm_vmm_info;
59
60static DEFINE_PER_CPU(struct kvm_vcpu *, last_vcpu);
61
62struct kvm_stats_debugfs_item debugfs_entries[] = {
63 { NULL }
64};
65
Xiantao Zhangb024b792008-04-01 15:29:29 +080066static void kvm_flush_icache(unsigned long start, unsigned long len)
67{
68 int l;
69
70 for (l = 0; l < (len + 32); l += 32)
71 ia64_fc(start + l);
72
73 ia64_sync_i();
74 ia64_srlz_i();
75}
76
77static void kvm_flush_tlb_all(void)
78{
79 unsigned long i, j, count0, count1, stride0, stride1, addr;
80 long flags;
81
82 addr = local_cpu_data->ptce_base;
83 count0 = local_cpu_data->ptce_count[0];
84 count1 = local_cpu_data->ptce_count[1];
85 stride0 = local_cpu_data->ptce_stride[0];
86 stride1 = local_cpu_data->ptce_stride[1];
87
88 local_irq_save(flags);
89 for (i = 0; i < count0; ++i) {
90 for (j = 0; j < count1; ++j) {
91 ia64_ptce(addr);
92 addr += stride1;
93 }
94 addr += stride0;
95 }
96 local_irq_restore(flags);
97 ia64_srlz_i(); /* srlz.i implies srlz.d */
98}
99
100long ia64_pal_vp_create(u64 *vpd, u64 *host_iva, u64 *opt_handler)
101{
102 struct ia64_pal_retval iprv;
103
104 PAL_CALL_STK(iprv, PAL_VP_CREATE, (u64)vpd, (u64)host_iva,
105 (u64)opt_handler);
106
107 return iprv.status;
108}
109
110static DEFINE_SPINLOCK(vp_lock);
111
112void kvm_arch_hardware_enable(void *garbage)
113{
114 long status;
115 long tmp_base;
116 unsigned long pte;
117 unsigned long saved_psr;
118 int slot;
119
120 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base),
121 PAGE_KERNEL));
122 local_irq_save(saved_psr);
123 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
Julia Lawallcab7a1e2008-07-22 21:38:18 +0200124 local_irq_restore(saved_psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800125 if (slot < 0)
126 return;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800127
128 spin_lock(&vp_lock);
129 status = ia64_pal_vp_init_env(kvm_vsa_base ?
130 VP_INIT_ENV : VP_INIT_ENV_INITALIZE,
131 __pa(kvm_vm_buffer), KVM_VM_BUFFER_BASE, &tmp_base);
132 if (status != 0) {
133 printk(KERN_WARNING"kvm: Failed to Enable VT Support!!!!\n");
134 return ;
135 }
136
137 if (!kvm_vsa_base) {
138 kvm_vsa_base = tmp_base;
139 printk(KERN_INFO"kvm: kvm_vsa_base:0x%lx\n", kvm_vsa_base);
140 }
141 spin_unlock(&vp_lock);
142 ia64_ptr_entry(0x3, slot);
143}
144
145void kvm_arch_hardware_disable(void *garbage)
146{
147
148 long status;
149 int slot;
150 unsigned long pte;
151 unsigned long saved_psr;
152 unsigned long host_iva = ia64_getreg(_IA64_REG_CR_IVA);
153
154 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base),
155 PAGE_KERNEL));
156
157 local_irq_save(saved_psr);
158 slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
Julia Lawallcab7a1e2008-07-22 21:38:18 +0200159 local_irq_restore(saved_psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800160 if (slot < 0)
161 return;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800162
163 status = ia64_pal_vp_exit_env(host_iva);
164 if (status)
165 printk(KERN_DEBUG"kvm: Failed to disable VT support! :%ld\n",
166 status);
167 ia64_ptr_entry(0x3, slot);
168}
169
170void kvm_arch_check_processor_compat(void *rtn)
171{
172 *(int *)rtn = 0;
173}
174
175int kvm_dev_ioctl_check_extension(long ext)
176{
177
178 int r;
179
180 switch (ext) {
181 case KVM_CAP_IRQCHIP:
182 case KVM_CAP_USER_MEMORY:
Xiantao Zhang8c4b5372008-08-28 09:34:08 +0800183 case KVM_CAP_MP_STATE:
Xiantao Zhangb024b792008-04-01 15:29:29 +0800184
185 r = 1;
186 break;
Laurent Vivier7f39f8a2008-05-30 16:05:57 +0200187 case KVM_CAP_COALESCED_MMIO:
188 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
189 break;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800190 default:
191 r = 0;
192 }
193 return r;
194
195}
196
197static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
Laurent Vivier92760492008-05-30 16:05:53 +0200198 gpa_t addr, int len, int is_write)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800199{
200 struct kvm_io_device *dev;
201
Laurent Vivier92760492008-05-30 16:05:53 +0200202 dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr, len, is_write);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800203
204 return dev;
205}
206
207static int handle_vm_error(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
208{
209 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
210 kvm_run->hw.hardware_exit_reason = 1;
211 return 0;
212}
213
214static int handle_mmio(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
215{
216 struct kvm_mmio_req *p;
217 struct kvm_io_device *mmio_dev;
218
219 p = kvm_get_vcpu_ioreq(vcpu);
220
221 if ((p->addr & PAGE_MASK) == IOAPIC_DEFAULT_BASE_ADDRESS)
222 goto mmio;
223 vcpu->mmio_needed = 1;
224 vcpu->mmio_phys_addr = kvm_run->mmio.phys_addr = p->addr;
225 vcpu->mmio_size = kvm_run->mmio.len = p->size;
226 vcpu->mmio_is_write = kvm_run->mmio.is_write = !p->dir;
227
228 if (vcpu->mmio_is_write)
229 memcpy(vcpu->mmio_data, &p->data, p->size);
230 memcpy(kvm_run->mmio.data, &p->data, p->size);
231 kvm_run->exit_reason = KVM_EXIT_MMIO;
232 return 0;
233mmio:
Laurent Vivier92760492008-05-30 16:05:53 +0200234 mmio_dev = vcpu_find_mmio_dev(vcpu, p->addr, p->size, !p->dir);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800235 if (mmio_dev) {
236 if (!p->dir)
237 kvm_iodevice_write(mmio_dev, p->addr, p->size,
238 &p->data);
239 else
240 kvm_iodevice_read(mmio_dev, p->addr, p->size,
241 &p->data);
242
243 } else
244 printk(KERN_ERR"kvm: No iodevice found! addr:%lx\n", p->addr);
245 p->state = STATE_IORESP_READY;
246
247 return 1;
248}
249
250static int handle_pal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
251{
252 struct exit_ctl_data *p;
253
254 p = kvm_get_exit_data(vcpu);
255
256 if (p->exit_reason == EXIT_REASON_PAL_CALL)
257 return kvm_pal_emul(vcpu, kvm_run);
258 else {
259 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
260 kvm_run->hw.hardware_exit_reason = 2;
261 return 0;
262 }
263}
264
265static int handle_sal_call(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
266{
267 struct exit_ctl_data *p;
268
269 p = kvm_get_exit_data(vcpu);
270
271 if (p->exit_reason == EXIT_REASON_SAL_CALL) {
272 kvm_sal_emul(vcpu);
273 return 1;
274 } else {
275 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
276 kvm_run->hw.hardware_exit_reason = 3;
277 return 0;
278 }
279
280}
281
282/*
283 * offset: address offset to IPI space.
284 * value: deliver value.
285 */
286static void vcpu_deliver_ipi(struct kvm_vcpu *vcpu, uint64_t dm,
287 uint64_t vector)
288{
289 switch (dm) {
290 case SAPIC_FIXED:
291 kvm_apic_set_irq(vcpu, vector, 0);
292 break;
293 case SAPIC_NMI:
294 kvm_apic_set_irq(vcpu, 2, 0);
295 break;
296 case SAPIC_EXTINT:
297 kvm_apic_set_irq(vcpu, 0, 0);
298 break;
299 case SAPIC_INIT:
300 case SAPIC_PMI:
301 default:
302 printk(KERN_ERR"kvm: Unimplemented Deliver reserved IPI!\n");
303 break;
304 }
305}
306
307static struct kvm_vcpu *lid_to_vcpu(struct kvm *kvm, unsigned long id,
308 unsigned long eid)
309{
310 union ia64_lid lid;
311 int i;
312
313 for (i = 0; i < KVM_MAX_VCPUS; i++) {
314 if (kvm->vcpus[i]) {
315 lid.val = VCPU_LID(kvm->vcpus[i]);
316 if (lid.id == id && lid.eid == eid)
317 return kvm->vcpus[i];
318 }
319 }
320
321 return NULL;
322}
323
324static int handle_ipi(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
325{
326 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
327 struct kvm_vcpu *target_vcpu;
328 struct kvm_pt_regs *regs;
329 union ia64_ipi_a addr = p->u.ipi_data.addr;
330 union ia64_ipi_d data = p->u.ipi_data.data;
331
332 target_vcpu = lid_to_vcpu(vcpu->kvm, addr.id, addr.eid);
333 if (!target_vcpu)
334 return handle_vm_error(vcpu, kvm_run);
335
336 if (!target_vcpu->arch.launched) {
337 regs = vcpu_regs(target_vcpu);
338
339 regs->cr_iip = vcpu->kvm->arch.rdv_sal_data.boot_ip;
340 regs->r1 = vcpu->kvm->arch.rdv_sal_data.boot_gp;
341
Avi Kivitya4535292008-04-13 17:54:35 +0300342 target_vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800343 if (waitqueue_active(&target_vcpu->wq))
344 wake_up_interruptible(&target_vcpu->wq);
345 } else {
346 vcpu_deliver_ipi(target_vcpu, data.dm, data.vector);
347 if (target_vcpu != vcpu)
348 kvm_vcpu_kick(target_vcpu);
349 }
350
351 return 1;
352}
353
354struct call_data {
355 struct kvm_ptc_g ptc_g_data;
356 struct kvm_vcpu *vcpu;
357};
358
359static void vcpu_global_purge(void *info)
360{
361 struct call_data *p = (struct call_data *)info;
362 struct kvm_vcpu *vcpu = p->vcpu;
363
364 if (test_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
365 return;
366
367 set_bit(KVM_REQ_PTC_G, &vcpu->requests);
368 if (vcpu->arch.ptc_g_count < MAX_PTC_G_NUM) {
369 vcpu->arch.ptc_g_data[vcpu->arch.ptc_g_count++] =
370 p->ptc_g_data;
371 } else {
372 clear_bit(KVM_REQ_PTC_G, &vcpu->requests);
373 vcpu->arch.ptc_g_count = 0;
374 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
375 }
376}
377
378static int handle_global_purge(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
379{
380 struct exit_ctl_data *p = kvm_get_exit_data(vcpu);
381 struct kvm *kvm = vcpu->kvm;
382 struct call_data call_data;
383 int i;
384 call_data.ptc_g_data = p->u.ptc_g_data;
385
386 for (i = 0; i < KVM_MAX_VCPUS; i++) {
387 if (!kvm->vcpus[i] || kvm->vcpus[i]->arch.mp_state ==
Avi Kivitya4535292008-04-13 17:54:35 +0300388 KVM_MP_STATE_UNINITIALIZED ||
Xiantao Zhangb024b792008-04-01 15:29:29 +0800389 vcpu == kvm->vcpus[i])
390 continue;
391
392 if (waitqueue_active(&kvm->vcpus[i]->wq))
393 wake_up_interruptible(&kvm->vcpus[i]->wq);
394
395 if (kvm->vcpus[i]->cpu != -1) {
396 call_data.vcpu = kvm->vcpus[i];
397 smp_call_function_single(kvm->vcpus[i]->cpu,
Takashi Iwai2f73cca2008-07-17 18:09:12 +0200398 vcpu_global_purge, &call_data, 1);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800399 } else
400 printk(KERN_WARNING"kvm: Uninit vcpu received ipi!\n");
401
402 }
403 return 1;
404}
405
406static int handle_switch_rr6(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
407{
408 return 1;
409}
410
411int kvm_emulate_halt(struct kvm_vcpu *vcpu)
412{
413
414 ktime_t kt;
415 long itc_diff;
416 unsigned long vcpu_now_itc;
417
418 unsigned long expires;
419 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
420 unsigned long cyc_per_usec = local_cpu_data->cyc_per_usec;
421 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
422
423 vcpu_now_itc = ia64_getreg(_IA64_REG_AR_ITC) + vcpu->arch.itc_offset;
424
425 if (time_after(vcpu_now_itc, vpd->itm)) {
426 vcpu->arch.timer_check = 1;
427 return 1;
428 }
429 itc_diff = vpd->itm - vcpu_now_itc;
430 if (itc_diff < 0)
431 itc_diff = -itc_diff;
432
Roman Zippel6f6d6a12008-05-01 04:34:28 -0700433 expires = div64_u64(itc_diff, cyc_per_usec);
Xiantao Zhangb024b792008-04-01 15:29:29 +0800434 kt = ktime_set(0, 1000 * expires);
435 vcpu->arch.ht_active = 1;
436 hrtimer_start(p_ht, kt, HRTIMER_MODE_ABS);
437
438 if (irqchip_in_kernel(vcpu->kvm)) {
Avi Kivitya4535292008-04-13 17:54:35 +0300439 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
Xiantao Zhangb024b792008-04-01 15:29:29 +0800440 kvm_vcpu_block(vcpu);
441 hrtimer_cancel(p_ht);
442 vcpu->arch.ht_active = 0;
443
Avi Kivitya4535292008-04-13 17:54:35 +0300444 if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE)
Xiantao Zhangb024b792008-04-01 15:29:29 +0800445 return -EINTR;
446 return 1;
447 } else {
448 printk(KERN_ERR"kvm: Unsupported userspace halt!");
449 return 0;
450 }
451}
452
453static int handle_vm_shutdown(struct kvm_vcpu *vcpu,
454 struct kvm_run *kvm_run)
455{
456 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
457 return 0;
458}
459
460static int handle_external_interrupt(struct kvm_vcpu *vcpu,
461 struct kvm_run *kvm_run)
462{
463 return 1;
464}
465
466static int (*kvm_vti_exit_handlers[])(struct kvm_vcpu *vcpu,
467 struct kvm_run *kvm_run) = {
468 [EXIT_REASON_VM_PANIC] = handle_vm_error,
469 [EXIT_REASON_MMIO_INSTRUCTION] = handle_mmio,
470 [EXIT_REASON_PAL_CALL] = handle_pal_call,
471 [EXIT_REASON_SAL_CALL] = handle_sal_call,
472 [EXIT_REASON_SWITCH_RR6] = handle_switch_rr6,
473 [EXIT_REASON_VM_DESTROY] = handle_vm_shutdown,
474 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
475 [EXIT_REASON_IPI] = handle_ipi,
476 [EXIT_REASON_PTC_G] = handle_global_purge,
477
478};
479
480static const int kvm_vti_max_exit_handlers =
481 sizeof(kvm_vti_exit_handlers)/sizeof(*kvm_vti_exit_handlers);
482
483static void kvm_prepare_guest_switch(struct kvm_vcpu *vcpu)
484{
485}
486
487static uint32_t kvm_get_exit_reason(struct kvm_vcpu *vcpu)
488{
489 struct exit_ctl_data *p_exit_data;
490
491 p_exit_data = kvm_get_exit_data(vcpu);
492 return p_exit_data->exit_reason;
493}
494
495/*
496 * The guest has exited. See if we can fix it or if we need userspace
497 * assistance.
498 */
499static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
500{
501 u32 exit_reason = kvm_get_exit_reason(vcpu);
502 vcpu->arch.last_exit = exit_reason;
503
504 if (exit_reason < kvm_vti_max_exit_handlers
505 && kvm_vti_exit_handlers[exit_reason])
506 return kvm_vti_exit_handlers[exit_reason](vcpu, kvm_run);
507 else {
508 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
509 kvm_run->hw.hardware_exit_reason = exit_reason;
510 }
511 return 0;
512}
513
514static inline void vti_set_rr6(unsigned long rr6)
515{
516 ia64_set_rr(RR6, rr6);
517 ia64_srlz_i();
518}
519
520static int kvm_insert_vmm_mapping(struct kvm_vcpu *vcpu)
521{
522 unsigned long pte;
523 struct kvm *kvm = vcpu->kvm;
524 int r;
525
526 /*Insert a pair of tr to map vmm*/
527 pte = pte_val(mk_pte_phys(__pa(kvm_vmm_base), PAGE_KERNEL));
528 r = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
529 if (r < 0)
530 goto out;
531 vcpu->arch.vmm_tr_slot = r;
532 /*Insert a pairt of tr to map data of vm*/
533 pte = pte_val(mk_pte_phys(__pa(kvm->arch.vm_base), PAGE_KERNEL));
534 r = ia64_itr_entry(0x3, KVM_VM_DATA_BASE,
535 pte, KVM_VM_DATA_SHIFT);
536 if (r < 0)
537 goto out;
538 vcpu->arch.vm_tr_slot = r;
539 r = 0;
540out:
541 return r;
542
543}
544
545static void kvm_purge_vmm_mapping(struct kvm_vcpu *vcpu)
546{
547
548 ia64_ptr_entry(0x3, vcpu->arch.vmm_tr_slot);
549 ia64_ptr_entry(0x3, vcpu->arch.vm_tr_slot);
550
551}
552
553static int kvm_vcpu_pre_transition(struct kvm_vcpu *vcpu)
554{
555 int cpu = smp_processor_id();
556
557 if (vcpu->arch.last_run_cpu != cpu ||
558 per_cpu(last_vcpu, cpu) != vcpu) {
559 per_cpu(last_vcpu, cpu) = vcpu;
560 vcpu->arch.last_run_cpu = cpu;
561 kvm_flush_tlb_all();
562 }
563
564 vcpu->arch.host_rr6 = ia64_get_rr(RR6);
565 vti_set_rr6(vcpu->arch.vmm_rr);
566 return kvm_insert_vmm_mapping(vcpu);
567}
568static void kvm_vcpu_post_transition(struct kvm_vcpu *vcpu)
569{
570 kvm_purge_vmm_mapping(vcpu);
571 vti_set_rr6(vcpu->arch.host_rr6);
572}
573
574static int vti_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
575{
576 union context *host_ctx, *guest_ctx;
577 int r;
578
579 /*Get host and guest context with guest address space.*/
580 host_ctx = kvm_get_host_context(vcpu);
581 guest_ctx = kvm_get_guest_context(vcpu);
582
583 r = kvm_vcpu_pre_transition(vcpu);
584 if (r < 0)
585 goto out;
586 kvm_vmm_info->tramp_entry(host_ctx, guest_ctx);
587 kvm_vcpu_post_transition(vcpu);
588 r = 0;
589out:
590 return r;
591}
592
593static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
594{
595 int r;
596
597again:
598 preempt_disable();
599
600 kvm_prepare_guest_switch(vcpu);
601 local_irq_disable();
602
603 if (signal_pending(current)) {
604 local_irq_enable();
605 preempt_enable();
606 r = -EINTR;
607 kvm_run->exit_reason = KVM_EXIT_INTR;
608 goto out;
609 }
610
611 vcpu->guest_mode = 1;
612 kvm_guest_enter();
613
614 r = vti_vcpu_run(vcpu, kvm_run);
615 if (r < 0) {
616 local_irq_enable();
617 preempt_enable();
618 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
619 goto out;
620 }
621
622 vcpu->arch.launched = 1;
623 vcpu->guest_mode = 0;
624 local_irq_enable();
625
626 /*
627 * We must have an instruction between local_irq_enable() and
628 * kvm_guest_exit(), so the timer interrupt isn't delayed by
629 * the interrupt shadow. The stat.exits increment will do nicely.
630 * But we need to prevent reordering, hence this barrier():
631 */
632 barrier();
633
634 kvm_guest_exit();
635
636 preempt_enable();
637
638 r = kvm_handle_exit(kvm_run, vcpu);
639
640 if (r > 0) {
641 if (!need_resched())
642 goto again;
643 }
644
645out:
646 if (r > 0) {
647 kvm_resched(vcpu);
648 goto again;
649 }
650
651 return r;
652}
653
654static void kvm_set_mmio_data(struct kvm_vcpu *vcpu)
655{
656 struct kvm_mmio_req *p = kvm_get_vcpu_ioreq(vcpu);
657
658 if (!vcpu->mmio_is_write)
659 memcpy(&p->data, vcpu->mmio_data, 8);
660 p->state = STATE_IORESP_READY;
661}
662
663int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
664{
665 int r;
666 sigset_t sigsaved;
667
668 vcpu_load(vcpu);
669
Avi Kivitya4535292008-04-13 17:54:35 +0300670 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
Xiantao Zhangb024b792008-04-01 15:29:29 +0800671 kvm_vcpu_block(vcpu);
672 vcpu_put(vcpu);
673 return -EAGAIN;
674 }
675
676 if (vcpu->sigset_active)
677 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
678
679 if (vcpu->mmio_needed) {
680 memcpy(vcpu->mmio_data, kvm_run->mmio.data, 8);
681 kvm_set_mmio_data(vcpu);
682 vcpu->mmio_read_completed = 1;
683 vcpu->mmio_needed = 0;
684 }
685 r = __vcpu_run(vcpu, kvm_run);
686
687 if (vcpu->sigset_active)
688 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
689
690 vcpu_put(vcpu);
691 return r;
692}
693
694/*
695 * Allocate 16M memory for every vm to hold its specific data.
696 * Its memory map is defined in kvm_host.h.
697 */
698static struct kvm *kvm_alloc_kvm(void)
699{
700
701 struct kvm *kvm;
702 uint64_t vm_base;
703
704 vm_base = __get_free_pages(GFP_KERNEL, get_order(KVM_VM_DATA_SIZE));
705
706 if (!vm_base)
707 return ERR_PTR(-ENOMEM);
708 printk(KERN_DEBUG"kvm: VM data's base Address:0x%lx\n", vm_base);
709
710 /* Zero all pages before use! */
711 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
712
713 kvm = (struct kvm *)(vm_base + KVM_VM_OFS);
714 kvm->arch.vm_base = vm_base;
715
716 return kvm;
717}
718
719struct kvm_io_range {
720 unsigned long start;
721 unsigned long size;
722 unsigned long type;
723};
724
725static const struct kvm_io_range io_ranges[] = {
726 {VGA_IO_START, VGA_IO_SIZE, GPFN_FRAME_BUFFER},
727 {MMIO_START, MMIO_SIZE, GPFN_LOW_MMIO},
728 {LEGACY_IO_START, LEGACY_IO_SIZE, GPFN_LEGACY_IO},
729 {IO_SAPIC_START, IO_SAPIC_SIZE, GPFN_IOSAPIC},
730 {PIB_START, PIB_SIZE, GPFN_PIB},
731};
732
733static void kvm_build_io_pmt(struct kvm *kvm)
734{
735 unsigned long i, j;
736
737 /* Mark I/O ranges */
738 for (i = 0; i < (sizeof(io_ranges) / sizeof(struct kvm_io_range));
739 i++) {
740 for (j = io_ranges[i].start;
741 j < io_ranges[i].start + io_ranges[i].size;
742 j += PAGE_SIZE)
743 kvm_set_pmt_entry(kvm, j >> PAGE_SHIFT,
744 io_ranges[i].type, 0);
745 }
746
747}
748
749/*Use unused rids to virtualize guest rid.*/
750#define GUEST_PHYSICAL_RR0 0x1739
751#define GUEST_PHYSICAL_RR4 0x2739
752#define VMM_INIT_RR 0x1660
753
754static void kvm_init_vm(struct kvm *kvm)
755{
756 long vm_base;
757
758 BUG_ON(!kvm);
759
760 kvm->arch.metaphysical_rr0 = GUEST_PHYSICAL_RR0;
761 kvm->arch.metaphysical_rr4 = GUEST_PHYSICAL_RR4;
762 kvm->arch.vmm_init_rr = VMM_INIT_RR;
763
764 vm_base = kvm->arch.vm_base;
765 if (vm_base) {
766 kvm->arch.vhpt_base = vm_base + KVM_VHPT_OFS;
767 kvm->arch.vtlb_base = vm_base + KVM_VTLB_OFS;
768 kvm->arch.vpd_base = vm_base + KVM_VPD_OFS;
769 }
770
771 /*
772 *Fill P2M entries for MMIO/IO ranges
773 */
774 kvm_build_io_pmt(kvm);
775
776}
777
778struct kvm *kvm_arch_create_vm(void)
779{
780 struct kvm *kvm = kvm_alloc_kvm();
781
782 if (IS_ERR(kvm))
783 return ERR_PTR(-ENOMEM);
784 kvm_init_vm(kvm);
785
786 return kvm;
787
788}
789
790static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm,
791 struct kvm_irqchip *chip)
792{
793 int r;
794
795 r = 0;
796 switch (chip->chip_id) {
797 case KVM_IRQCHIP_IOAPIC:
798 memcpy(&chip->chip.ioapic, ioapic_irqchip(kvm),
799 sizeof(struct kvm_ioapic_state));
800 break;
801 default:
802 r = -EINVAL;
803 break;
804 }
805 return r;
806}
807
808static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
809{
810 int r;
811
812 r = 0;
813 switch (chip->chip_id) {
814 case KVM_IRQCHIP_IOAPIC:
815 memcpy(ioapic_irqchip(kvm),
816 &chip->chip.ioapic,
817 sizeof(struct kvm_ioapic_state));
818 break;
819 default:
820 r = -EINVAL;
821 break;
822 }
823 return r;
824}
825
826#define RESTORE_REGS(_x) vcpu->arch._x = regs->_x
827
828int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
829{
830 int i;
831 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
832 int r;
833
834 vcpu_load(vcpu);
835
836 for (i = 0; i < 16; i++) {
837 vpd->vgr[i] = regs->vpd.vgr[i];
838 vpd->vbgr[i] = regs->vpd.vbgr[i];
839 }
840 for (i = 0; i < 128; i++)
841 vpd->vcr[i] = regs->vpd.vcr[i];
842 vpd->vhpi = regs->vpd.vhpi;
843 vpd->vnat = regs->vpd.vnat;
844 vpd->vbnat = regs->vpd.vbnat;
845 vpd->vpsr = regs->vpd.vpsr;
846
847 vpd->vpr = regs->vpd.vpr;
848
849 r = -EFAULT;
850 r = copy_from_user(&vcpu->arch.guest, regs->saved_guest,
851 sizeof(union context));
852 if (r)
853 goto out;
854 r = copy_from_user(vcpu + 1, regs->saved_stack +
855 sizeof(struct kvm_vcpu),
856 IA64_STK_OFFSET - sizeof(struct kvm_vcpu));
857 if (r)
858 goto out;
859 vcpu->arch.exit_data =
860 ((struct kvm_vcpu *)(regs->saved_stack))->arch.exit_data;
861
862 RESTORE_REGS(mp_state);
863 RESTORE_REGS(vmm_rr);
864 memcpy(vcpu->arch.itrs, regs->itrs, sizeof(struct thash_data) * NITRS);
865 memcpy(vcpu->arch.dtrs, regs->dtrs, sizeof(struct thash_data) * NDTRS);
866 RESTORE_REGS(itr_regions);
867 RESTORE_REGS(dtr_regions);
868 RESTORE_REGS(tc_regions);
869 RESTORE_REGS(irq_check);
870 RESTORE_REGS(itc_check);
871 RESTORE_REGS(timer_check);
872 RESTORE_REGS(timer_pending);
873 RESTORE_REGS(last_itc);
874 for (i = 0; i < 8; i++) {
875 vcpu->arch.vrr[i] = regs->vrr[i];
876 vcpu->arch.ibr[i] = regs->ibr[i];
877 vcpu->arch.dbr[i] = regs->dbr[i];
878 }
879 for (i = 0; i < 4; i++)
880 vcpu->arch.insvc[i] = regs->insvc[i];
881 RESTORE_REGS(xtp);
882 RESTORE_REGS(metaphysical_rr0);
883 RESTORE_REGS(metaphysical_rr4);
884 RESTORE_REGS(metaphysical_saved_rr0);
885 RESTORE_REGS(metaphysical_saved_rr4);
886 RESTORE_REGS(fp_psr);
887 RESTORE_REGS(saved_gp);
888
889 vcpu->arch.irq_new_pending = 1;
890 vcpu->arch.itc_offset = regs->saved_itc - ia64_getreg(_IA64_REG_AR_ITC);
891 set_bit(KVM_REQ_RESUME, &vcpu->requests);
892
893 vcpu_put(vcpu);
894 r = 0;
895out:
896 return r;
897}
898
899long kvm_arch_vm_ioctl(struct file *filp,
900 unsigned int ioctl, unsigned long arg)
901{
902 struct kvm *kvm = filp->private_data;
903 void __user *argp = (void __user *)arg;
904 int r = -EINVAL;
905
906 switch (ioctl) {
907 case KVM_SET_MEMORY_REGION: {
908 struct kvm_memory_region kvm_mem;
909 struct kvm_userspace_memory_region kvm_userspace_mem;
910
911 r = -EFAULT;
912 if (copy_from_user(&kvm_mem, argp, sizeof kvm_mem))
913 goto out;
914 kvm_userspace_mem.slot = kvm_mem.slot;
915 kvm_userspace_mem.flags = kvm_mem.flags;
916 kvm_userspace_mem.guest_phys_addr =
917 kvm_mem.guest_phys_addr;
918 kvm_userspace_mem.memory_size = kvm_mem.memory_size;
919 r = kvm_vm_ioctl_set_memory_region(kvm,
920 &kvm_userspace_mem, 0);
921 if (r)
922 goto out;
923 break;
924 }
925 case KVM_CREATE_IRQCHIP:
926 r = -EFAULT;
927 r = kvm_ioapic_init(kvm);
928 if (r)
929 goto out;
930 break;
931 case KVM_IRQ_LINE: {
932 struct kvm_irq_level irq_event;
933
934 r = -EFAULT;
935 if (copy_from_user(&irq_event, argp, sizeof irq_event))
936 goto out;
937 if (irqchip_in_kernel(kvm)) {
938 mutex_lock(&kvm->lock);
939 kvm_ioapic_set_irq(kvm->arch.vioapic,
940 irq_event.irq,
941 irq_event.level);
942 mutex_unlock(&kvm->lock);
943 r = 0;
944 }
945 break;
946 }
947 case KVM_GET_IRQCHIP: {
948 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
949 struct kvm_irqchip chip;
950
951 r = -EFAULT;
952 if (copy_from_user(&chip, argp, sizeof chip))
953 goto out;
954 r = -ENXIO;
955 if (!irqchip_in_kernel(kvm))
956 goto out;
957 r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
958 if (r)
959 goto out;
960 r = -EFAULT;
961 if (copy_to_user(argp, &chip, sizeof chip))
962 goto out;
963 r = 0;
964 break;
965 }
966 case KVM_SET_IRQCHIP: {
967 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
968 struct kvm_irqchip chip;
969
970 r = -EFAULT;
971 if (copy_from_user(&chip, argp, sizeof chip))
972 goto out;
973 r = -ENXIO;
974 if (!irqchip_in_kernel(kvm))
975 goto out;
976 r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
977 if (r)
978 goto out;
979 r = 0;
980 break;
981 }
982 default:
983 ;
984 }
985out:
986 return r;
987}
988
989int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
990 struct kvm_sregs *sregs)
991{
992 return -EINVAL;
993}
994
995int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
996 struct kvm_sregs *sregs)
997{
998 return -EINVAL;
999
1000}
1001int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1002 struct kvm_translation *tr)
1003{
1004
1005 return -EINVAL;
1006}
1007
1008static int kvm_alloc_vmm_area(void)
1009{
1010 if (!kvm_vmm_base && (kvm_vm_buffer_size < KVM_VM_BUFFER_SIZE)) {
1011 kvm_vmm_base = __get_free_pages(GFP_KERNEL,
1012 get_order(KVM_VMM_SIZE));
1013 if (!kvm_vmm_base)
1014 return -ENOMEM;
1015
1016 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1017 kvm_vm_buffer = kvm_vmm_base + VMM_SIZE;
1018
1019 printk(KERN_DEBUG"kvm:VMM's Base Addr:0x%lx, vm_buffer:0x%lx\n",
1020 kvm_vmm_base, kvm_vm_buffer);
1021 }
1022
1023 return 0;
1024}
1025
1026static void kvm_free_vmm_area(void)
1027{
1028 if (kvm_vmm_base) {
1029 /*Zero this area before free to avoid bits leak!!*/
1030 memset((void *)kvm_vmm_base, 0, KVM_VMM_SIZE);
1031 free_pages(kvm_vmm_base, get_order(KVM_VMM_SIZE));
1032 kvm_vmm_base = 0;
1033 kvm_vm_buffer = 0;
1034 kvm_vsa_base = 0;
1035 }
1036}
1037
Xiantao Zhangb024b792008-04-01 15:29:29 +08001038static void vti_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1039{
1040}
1041
1042static int vti_init_vpd(struct kvm_vcpu *vcpu)
1043{
1044 int i;
1045 union cpuid3_t cpuid3;
1046 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1047
1048 if (IS_ERR(vpd))
1049 return PTR_ERR(vpd);
1050
1051 /* CPUID init */
1052 for (i = 0; i < 5; i++)
1053 vpd->vcpuid[i] = ia64_get_cpuid(i);
1054
1055 /* Limit the CPUID number to 5 */
1056 cpuid3.value = vpd->vcpuid[3];
1057 cpuid3.number = 4; /* 5 - 1 */
1058 vpd->vcpuid[3] = cpuid3.value;
1059
1060 /*Set vac and vdc fields*/
1061 vpd->vac.a_from_int_cr = 1;
1062 vpd->vac.a_to_int_cr = 1;
1063 vpd->vac.a_from_psr = 1;
1064 vpd->vac.a_from_cpuid = 1;
1065 vpd->vac.a_cover = 1;
1066 vpd->vac.a_bsw = 1;
1067 vpd->vac.a_int = 1;
1068 vpd->vdc.d_vmsw = 1;
1069
1070 /*Set virtual buffer*/
1071 vpd->virt_env_vaddr = KVM_VM_BUFFER_BASE;
1072
1073 return 0;
1074}
1075
1076static int vti_create_vp(struct kvm_vcpu *vcpu)
1077{
1078 long ret;
1079 struct vpd *vpd = vcpu->arch.vpd;
1080 unsigned long vmm_ivt;
1081
1082 vmm_ivt = kvm_vmm_info->vmm_ivt;
1083
1084 printk(KERN_DEBUG "kvm: vcpu:%p,ivt: 0x%lx\n", vcpu, vmm_ivt);
1085
1086 ret = ia64_pal_vp_create((u64 *)vpd, (u64 *)vmm_ivt, 0);
1087
1088 if (ret) {
1089 printk(KERN_ERR"kvm: ia64_pal_vp_create failed!\n");
1090 return -EINVAL;
1091 }
1092 return 0;
1093}
1094
1095static void init_ptce_info(struct kvm_vcpu *vcpu)
1096{
1097 ia64_ptce_info_t ptce = {0};
1098
1099 ia64_get_ptce(&ptce);
1100 vcpu->arch.ptce_base = ptce.base;
1101 vcpu->arch.ptce_count[0] = ptce.count[0];
1102 vcpu->arch.ptce_count[1] = ptce.count[1];
1103 vcpu->arch.ptce_stride[0] = ptce.stride[0];
1104 vcpu->arch.ptce_stride[1] = ptce.stride[1];
1105}
1106
1107static void kvm_migrate_hlt_timer(struct kvm_vcpu *vcpu)
1108{
1109 struct hrtimer *p_ht = &vcpu->arch.hlt_timer;
1110
1111 if (hrtimer_cancel(p_ht))
1112 hrtimer_start(p_ht, p_ht->expires, HRTIMER_MODE_ABS);
1113}
1114
1115static enum hrtimer_restart hlt_timer_fn(struct hrtimer *data)
1116{
1117 struct kvm_vcpu *vcpu;
1118 wait_queue_head_t *q;
1119
1120 vcpu = container_of(data, struct kvm_vcpu, arch.hlt_timer);
Avi Kivitya4535292008-04-13 17:54:35 +03001121 if (vcpu->arch.mp_state != KVM_MP_STATE_HALTED)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001122 goto out;
1123
1124 q = &vcpu->wq;
1125 if (waitqueue_active(q)) {
Avi Kivitya4535292008-04-13 17:54:35 +03001126 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001127 wake_up_interruptible(q);
1128 }
1129out:
1130 vcpu->arch.timer_check = 1;
1131 return HRTIMER_NORESTART;
1132}
1133
1134#define PALE_RESET_ENTRY 0x80000000ffffffb0UL
1135
1136int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1137{
1138 struct kvm_vcpu *v;
1139 int r;
1140 int i;
1141 long itc_offset;
1142 struct kvm *kvm = vcpu->kvm;
1143 struct kvm_pt_regs *regs = vcpu_regs(vcpu);
1144
1145 union context *p_ctx = &vcpu->arch.guest;
1146 struct kvm_vcpu *vmm_vcpu = to_guest(vcpu->kvm, vcpu);
1147
1148 /*Init vcpu context for first run.*/
1149 if (IS_ERR(vmm_vcpu))
1150 return PTR_ERR(vmm_vcpu);
1151
1152 if (vcpu->vcpu_id == 0) {
Avi Kivitya4535292008-04-13 17:54:35 +03001153 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001154
1155 /*Set entry address for first run.*/
1156 regs->cr_iip = PALE_RESET_ENTRY;
1157
1158 /*Initilize itc offset for vcpus*/
1159 itc_offset = 0UL - ia64_getreg(_IA64_REG_AR_ITC);
1160 for (i = 0; i < MAX_VCPU_NUM; i++) {
1161 v = (struct kvm_vcpu *)((char *)vcpu + VCPU_SIZE * i);
1162 v->arch.itc_offset = itc_offset;
1163 v->arch.last_itc = 0;
1164 }
1165 } else
Avi Kivitya4535292008-04-13 17:54:35 +03001166 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001167
1168 r = -ENOMEM;
1169 vcpu->arch.apic = kzalloc(sizeof(struct kvm_lapic), GFP_KERNEL);
1170 if (!vcpu->arch.apic)
1171 goto out;
1172 vcpu->arch.apic->vcpu = vcpu;
1173
1174 p_ctx->gr[1] = 0;
1175 p_ctx->gr[12] = (unsigned long)((char *)vmm_vcpu + IA64_STK_OFFSET);
1176 p_ctx->gr[13] = (unsigned long)vmm_vcpu;
1177 p_ctx->psr = 0x1008522000UL;
1178 p_ctx->ar[40] = FPSR_DEFAULT; /*fpsr*/
1179 p_ctx->caller_unat = 0;
1180 p_ctx->pr = 0x0;
1181 p_ctx->ar[36] = 0x0; /*unat*/
1182 p_ctx->ar[19] = 0x0; /*rnat*/
1183 p_ctx->ar[18] = (unsigned long)vmm_vcpu +
1184 ((sizeof(struct kvm_vcpu)+15) & ~15);
1185 p_ctx->ar[64] = 0x0; /*pfs*/
1186 p_ctx->cr[0] = 0x7e04UL;
1187 p_ctx->cr[2] = (unsigned long)kvm_vmm_info->vmm_ivt;
1188 p_ctx->cr[8] = 0x3c;
1189
1190 /*Initilize region register*/
1191 p_ctx->rr[0] = 0x30;
1192 p_ctx->rr[1] = 0x30;
1193 p_ctx->rr[2] = 0x30;
1194 p_ctx->rr[3] = 0x30;
1195 p_ctx->rr[4] = 0x30;
1196 p_ctx->rr[5] = 0x30;
1197 p_ctx->rr[7] = 0x30;
1198
1199 /*Initilize branch register 0*/
1200 p_ctx->br[0] = *(unsigned long *)kvm_vmm_info->vmm_entry;
1201
1202 vcpu->arch.vmm_rr = kvm->arch.vmm_init_rr;
1203 vcpu->arch.metaphysical_rr0 = kvm->arch.metaphysical_rr0;
1204 vcpu->arch.metaphysical_rr4 = kvm->arch.metaphysical_rr4;
1205
1206 hrtimer_init(&vcpu->arch.hlt_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1207 vcpu->arch.hlt_timer.function = hlt_timer_fn;
1208
1209 vcpu->arch.last_run_cpu = -1;
1210 vcpu->arch.vpd = (struct vpd *)VPD_ADDR(vcpu->vcpu_id);
1211 vcpu->arch.vsa_base = kvm_vsa_base;
1212 vcpu->arch.__gp = kvm_vmm_gp;
1213 vcpu->arch.dirty_log_lock_pa = __pa(&kvm->arch.dirty_log_lock);
1214 vcpu->arch.vhpt.hash = (struct thash_data *)VHPT_ADDR(vcpu->vcpu_id);
1215 vcpu->arch.vtlb.hash = (struct thash_data *)VTLB_ADDR(vcpu->vcpu_id);
1216 init_ptce_info(vcpu);
1217
1218 r = 0;
1219out:
1220 return r;
1221}
1222
1223static int vti_vcpu_setup(struct kvm_vcpu *vcpu, int id)
1224{
1225 unsigned long psr;
1226 int r;
1227
1228 local_irq_save(psr);
1229 r = kvm_insert_vmm_mapping(vcpu);
1230 if (r)
1231 goto fail;
1232 r = kvm_vcpu_init(vcpu, vcpu->kvm, id);
1233 if (r)
1234 goto fail;
1235
1236 r = vti_init_vpd(vcpu);
1237 if (r) {
1238 printk(KERN_DEBUG"kvm: vpd init error!!\n");
1239 goto uninit;
1240 }
1241
1242 r = vti_create_vp(vcpu);
1243 if (r)
1244 goto uninit;
1245
1246 kvm_purge_vmm_mapping(vcpu);
1247 local_irq_restore(psr);
1248
1249 return 0;
1250uninit:
1251 kvm_vcpu_uninit(vcpu);
1252fail:
Julia Lawallcab7a1e2008-07-22 21:38:18 +02001253 local_irq_restore(psr);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001254 return r;
1255}
1256
1257struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1258 unsigned int id)
1259{
1260 struct kvm_vcpu *vcpu;
1261 unsigned long vm_base = kvm->arch.vm_base;
1262 int r;
1263 int cpu;
1264
1265 r = -ENOMEM;
1266 if (!vm_base) {
1267 printk(KERN_ERR"kvm: Create vcpu[%d] error!\n", id);
1268 goto fail;
1269 }
1270 vcpu = (struct kvm_vcpu *)(vm_base + KVM_VCPU_OFS + VCPU_SIZE * id);
1271 vcpu->kvm = kvm;
1272
1273 cpu = get_cpu();
1274 vti_vcpu_load(vcpu, cpu);
1275 r = vti_vcpu_setup(vcpu, id);
1276 put_cpu();
1277
1278 if (r) {
1279 printk(KERN_DEBUG"kvm: vcpu_setup error!!\n");
1280 goto fail;
1281 }
1282
1283 return vcpu;
1284fail:
1285 return ERR_PTR(r);
1286}
1287
1288int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1289{
1290 return 0;
1291}
1292
1293int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1294{
1295 return -EINVAL;
1296}
1297
1298int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1299{
1300 return -EINVAL;
1301}
1302
1303int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
1304 struct kvm_debug_guest *dbg)
1305{
1306 return -EINVAL;
1307}
1308
1309static void free_kvm(struct kvm *kvm)
1310{
1311 unsigned long vm_base = kvm->arch.vm_base;
1312
1313 if (vm_base) {
1314 memset((void *)vm_base, 0, KVM_VM_DATA_SIZE);
1315 free_pages(vm_base, get_order(KVM_VM_DATA_SIZE));
1316 }
1317
1318}
1319
1320static void kvm_release_vm_pages(struct kvm *kvm)
1321{
1322 struct kvm_memory_slot *memslot;
1323 int i, j;
1324 unsigned long base_gfn;
1325
1326 for (i = 0; i < kvm->nmemslots; i++) {
1327 memslot = &kvm->memslots[i];
1328 base_gfn = memslot->base_gfn;
1329
1330 for (j = 0; j < memslot->npages; j++) {
1331 if (memslot->rmap[j])
1332 put_page((struct page *)memslot->rmap[j]);
1333 }
1334 }
1335}
1336
1337void kvm_arch_destroy_vm(struct kvm *kvm)
1338{
1339 kfree(kvm->arch.vioapic);
1340 kvm_release_vm_pages(kvm);
1341 kvm_free_physmem(kvm);
1342 free_kvm(kvm);
1343}
1344
1345void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1346{
1347}
1348
1349void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1350{
1351 if (cpu != vcpu->cpu) {
1352 vcpu->cpu = cpu;
1353 if (vcpu->arch.ht_active)
1354 kvm_migrate_hlt_timer(vcpu);
1355 }
1356}
1357
1358#define SAVE_REGS(_x) regs->_x = vcpu->arch._x
1359
1360int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1361{
1362 int i;
1363 int r;
1364 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1365 vcpu_load(vcpu);
1366
1367 for (i = 0; i < 16; i++) {
1368 regs->vpd.vgr[i] = vpd->vgr[i];
1369 regs->vpd.vbgr[i] = vpd->vbgr[i];
1370 }
1371 for (i = 0; i < 128; i++)
1372 regs->vpd.vcr[i] = vpd->vcr[i];
1373 regs->vpd.vhpi = vpd->vhpi;
1374 regs->vpd.vnat = vpd->vnat;
1375 regs->vpd.vbnat = vpd->vbnat;
1376 regs->vpd.vpsr = vpd->vpsr;
1377 regs->vpd.vpr = vpd->vpr;
1378
1379 r = -EFAULT;
1380 r = copy_to_user(regs->saved_guest, &vcpu->arch.guest,
1381 sizeof(union context));
1382 if (r)
1383 goto out;
1384 r = copy_to_user(regs->saved_stack, (void *)vcpu, IA64_STK_OFFSET);
1385 if (r)
1386 goto out;
1387 SAVE_REGS(mp_state);
1388 SAVE_REGS(vmm_rr);
1389 memcpy(regs->itrs, vcpu->arch.itrs, sizeof(struct thash_data) * NITRS);
1390 memcpy(regs->dtrs, vcpu->arch.dtrs, sizeof(struct thash_data) * NDTRS);
1391 SAVE_REGS(itr_regions);
1392 SAVE_REGS(dtr_regions);
1393 SAVE_REGS(tc_regions);
1394 SAVE_REGS(irq_check);
1395 SAVE_REGS(itc_check);
1396 SAVE_REGS(timer_check);
1397 SAVE_REGS(timer_pending);
1398 SAVE_REGS(last_itc);
1399 for (i = 0; i < 8; i++) {
1400 regs->vrr[i] = vcpu->arch.vrr[i];
1401 regs->ibr[i] = vcpu->arch.ibr[i];
1402 regs->dbr[i] = vcpu->arch.dbr[i];
1403 }
1404 for (i = 0; i < 4; i++)
1405 regs->insvc[i] = vcpu->arch.insvc[i];
1406 regs->saved_itc = vcpu->arch.itc_offset + ia64_getreg(_IA64_REG_AR_ITC);
1407 SAVE_REGS(xtp);
1408 SAVE_REGS(metaphysical_rr0);
1409 SAVE_REGS(metaphysical_rr4);
1410 SAVE_REGS(metaphysical_saved_rr0);
1411 SAVE_REGS(metaphysical_saved_rr4);
1412 SAVE_REGS(fp_psr);
1413 SAVE_REGS(saved_gp);
1414 vcpu_put(vcpu);
1415 r = 0;
1416out:
1417 return r;
1418}
1419
1420void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
1421{
1422
1423 hrtimer_cancel(&vcpu->arch.hlt_timer);
1424 kfree(vcpu->arch.apic);
1425}
1426
1427
1428long kvm_arch_vcpu_ioctl(struct file *filp,
1429 unsigned int ioctl, unsigned long arg)
1430{
1431 return -EINVAL;
1432}
1433
1434int kvm_arch_set_memory_region(struct kvm *kvm,
1435 struct kvm_userspace_memory_region *mem,
1436 struct kvm_memory_slot old,
1437 int user_alloc)
1438{
1439 unsigned long i;
1440 struct page *page;
1441 int npages = mem->memory_size >> PAGE_SHIFT;
1442 struct kvm_memory_slot *memslot = &kvm->memslots[mem->slot];
1443 unsigned long base_gfn = memslot->base_gfn;
1444
1445 for (i = 0; i < npages; i++) {
1446 page = gfn_to_page(kvm, base_gfn + i);
1447 kvm_set_pmt_entry(kvm, base_gfn + i,
1448 page_to_pfn(page) << PAGE_SHIFT,
1449 _PAGE_AR_RWX|_PAGE_MA_WB);
1450 memslot->rmap[i] = (unsigned long)page;
1451 }
1452
1453 return 0;
1454}
1455
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -03001456void kvm_arch_flush_shadow(struct kvm *kvm)
1457{
1458}
Xiantao Zhangb024b792008-04-01 15:29:29 +08001459
1460long kvm_arch_dev_ioctl(struct file *filp,
1461 unsigned int ioctl, unsigned long arg)
1462{
1463 return -EINVAL;
1464}
1465
1466void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1467{
1468 kvm_vcpu_uninit(vcpu);
1469}
1470
1471static int vti_cpu_has_kvm_support(void)
1472{
1473 long avail = 1, status = 1, control = 1;
1474 long ret;
1475
1476 ret = ia64_pal_proc_get_features(&avail, &status, &control, 0);
1477 if (ret)
1478 goto out;
1479
1480 if (!(avail & PAL_PROC_VM_BIT))
1481 goto out;
1482
1483 printk(KERN_DEBUG"kvm: Hardware Supports VT\n");
1484
1485 ret = ia64_pal_vp_env_info(&kvm_vm_buffer_size, &vp_env_info);
1486 if (ret)
1487 goto out;
1488 printk(KERN_DEBUG"kvm: VM Buffer Size:0x%lx\n", kvm_vm_buffer_size);
1489
1490 if (!(vp_env_info & VP_OPCODE)) {
1491 printk(KERN_WARNING"kvm: No opcode ability on hardware, "
1492 "vm_env_info:0x%lx\n", vp_env_info);
1493 }
1494
1495 return 1;
1496out:
1497 return 0;
1498}
1499
1500static int kvm_relocate_vmm(struct kvm_vmm_info *vmm_info,
1501 struct module *module)
1502{
1503 unsigned long module_base;
1504 unsigned long vmm_size;
1505
1506 unsigned long vmm_offset, func_offset, fdesc_offset;
1507 struct fdesc *p_fdesc;
1508
1509 BUG_ON(!module);
1510
1511 if (!kvm_vmm_base) {
1512 printk("kvm: kvm area hasn't been initilized yet!!\n");
1513 return -EFAULT;
1514 }
1515
1516 /*Calculate new position of relocated vmm module.*/
1517 module_base = (unsigned long)module->module_core;
1518 vmm_size = module->core_size;
1519 if (unlikely(vmm_size > KVM_VMM_SIZE))
1520 return -EFAULT;
1521
1522 memcpy((void *)kvm_vmm_base, (void *)module_base, vmm_size);
1523 kvm_flush_icache(kvm_vmm_base, vmm_size);
1524
1525 /*Recalculate kvm_vmm_info based on new VMM*/
1526 vmm_offset = vmm_info->vmm_ivt - module_base;
1527 kvm_vmm_info->vmm_ivt = KVM_VMM_BASE + vmm_offset;
1528 printk(KERN_DEBUG"kvm: Relocated VMM's IVT Base Addr:%lx\n",
1529 kvm_vmm_info->vmm_ivt);
1530
1531 fdesc_offset = (unsigned long)vmm_info->vmm_entry - module_base;
1532 kvm_vmm_info->vmm_entry = (kvm_vmm_entry *)(KVM_VMM_BASE +
1533 fdesc_offset);
1534 func_offset = *(unsigned long *)vmm_info->vmm_entry - module_base;
1535 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1536 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1537 p_fdesc->gp = KVM_VMM_BASE+(p_fdesc->gp - module_base);
1538
1539 printk(KERN_DEBUG"kvm: Relocated VMM's Init Entry Addr:%lx\n",
1540 KVM_VMM_BASE+func_offset);
1541
1542 fdesc_offset = (unsigned long)vmm_info->tramp_entry - module_base;
1543 kvm_vmm_info->tramp_entry = (kvm_tramp_entry *)(KVM_VMM_BASE +
1544 fdesc_offset);
1545 func_offset = *(unsigned long *)vmm_info->tramp_entry - module_base;
1546 p_fdesc = (struct fdesc *)(kvm_vmm_base + fdesc_offset);
1547 p_fdesc->ip = KVM_VMM_BASE + func_offset;
1548 p_fdesc->gp = KVM_VMM_BASE + (p_fdesc->gp - module_base);
1549
1550 kvm_vmm_gp = p_fdesc->gp;
1551
1552 printk(KERN_DEBUG"kvm: Relocated VMM's Entry IP:%p\n",
1553 kvm_vmm_info->vmm_entry);
1554 printk(KERN_DEBUG"kvm: Relocated VMM's Trampoline Entry IP:0x%lx\n",
1555 KVM_VMM_BASE + func_offset);
1556
1557 return 0;
1558}
1559
1560int kvm_arch_init(void *opaque)
1561{
1562 int r;
1563 struct kvm_vmm_info *vmm_info = (struct kvm_vmm_info *)opaque;
1564
1565 if (!vti_cpu_has_kvm_support()) {
1566 printk(KERN_ERR "kvm: No Hardware Virtualization Support!\n");
1567 r = -EOPNOTSUPP;
1568 goto out;
1569 }
1570
1571 if (kvm_vmm_info) {
1572 printk(KERN_ERR "kvm: Already loaded VMM module!\n");
1573 r = -EEXIST;
1574 goto out;
1575 }
1576
1577 r = -ENOMEM;
1578 kvm_vmm_info = kzalloc(sizeof(struct kvm_vmm_info), GFP_KERNEL);
1579 if (!kvm_vmm_info)
1580 goto out;
1581
1582 if (kvm_alloc_vmm_area())
1583 goto out_free0;
1584
1585 r = kvm_relocate_vmm(vmm_info, vmm_info->module);
1586 if (r)
1587 goto out_free1;
1588
1589 return 0;
1590
1591out_free1:
1592 kvm_free_vmm_area();
1593out_free0:
1594 kfree(kvm_vmm_info);
1595out:
1596 return r;
1597}
1598
1599void kvm_arch_exit(void)
1600{
1601 kvm_free_vmm_area();
1602 kfree(kvm_vmm_info);
1603 kvm_vmm_info = NULL;
1604}
1605
1606static int kvm_ia64_sync_dirty_log(struct kvm *kvm,
1607 struct kvm_dirty_log *log)
1608{
1609 struct kvm_memory_slot *memslot;
1610 int r, i;
1611 long n, base;
1612 unsigned long *dirty_bitmap = (unsigned long *)((void *)kvm - KVM_VM_OFS
1613 + KVM_MEM_DIRTY_LOG_OFS);
1614
1615 r = -EINVAL;
1616 if (log->slot >= KVM_MEMORY_SLOTS)
1617 goto out;
1618
1619 memslot = &kvm->memslots[log->slot];
1620 r = -ENOENT;
1621 if (!memslot->dirty_bitmap)
1622 goto out;
1623
1624 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1625 base = memslot->base_gfn / BITS_PER_LONG;
1626
1627 for (i = 0; i < n/sizeof(long); ++i) {
1628 memslot->dirty_bitmap[i] = dirty_bitmap[base + i];
1629 dirty_bitmap[base + i] = 0;
1630 }
1631 r = 0;
1632out:
1633 return r;
1634}
1635
1636int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1637 struct kvm_dirty_log *log)
1638{
1639 int r;
1640 int n;
1641 struct kvm_memory_slot *memslot;
1642 int is_dirty = 0;
1643
1644 spin_lock(&kvm->arch.dirty_log_lock);
1645
1646 r = kvm_ia64_sync_dirty_log(kvm, log);
1647 if (r)
1648 goto out;
1649
1650 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1651 if (r)
1652 goto out;
1653
1654 /* If nothing is dirty, don't bother messing with page tables. */
1655 if (is_dirty) {
1656 kvm_flush_remote_tlbs(kvm);
1657 memslot = &kvm->memslots[log->slot];
1658 n = ALIGN(memslot->npages, BITS_PER_LONG) / 8;
1659 memset(memslot->dirty_bitmap, 0, n);
1660 }
1661 r = 0;
1662out:
1663 spin_unlock(&kvm->arch.dirty_log_lock);
1664 return r;
1665}
1666
1667int kvm_arch_hardware_setup(void)
1668{
1669 return 0;
1670}
1671
1672void kvm_arch_hardware_unsetup(void)
1673{
1674}
1675
1676static void vcpu_kick_intr(void *info)
1677{
1678#ifdef DEBUG
1679 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)info;
1680 printk(KERN_DEBUG"vcpu_kick_intr %p \n", vcpu);
1681#endif
1682}
1683
1684void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
1685{
1686 int ipi_pcpu = vcpu->cpu;
1687
1688 if (waitqueue_active(&vcpu->wq))
1689 wake_up_interruptible(&vcpu->wq);
1690
1691 if (vcpu->guest_mode)
Takashi Iwai2f73cca2008-07-17 18:09:12 +02001692 smp_call_function_single(ipi_pcpu, vcpu_kick_intr, vcpu, 0);
Xiantao Zhangb024b792008-04-01 15:29:29 +08001693}
1694
1695int kvm_apic_set_irq(struct kvm_vcpu *vcpu, u8 vec, u8 trig)
1696{
1697
1698 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1699
1700 if (!test_and_set_bit(vec, &vpd->irr[0])) {
1701 vcpu->arch.irq_new_pending = 1;
Avi Kivitya4535292008-04-13 17:54:35 +03001702 if (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE)
Xiantao Zhangb024b792008-04-01 15:29:29 +08001703 kvm_vcpu_kick(vcpu);
Avi Kivitya4535292008-04-13 17:54:35 +03001704 else if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) {
1705 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001706 if (waitqueue_active(&vcpu->wq))
1707 wake_up_interruptible(&vcpu->wq);
1708 }
1709 return 1;
1710 }
1711 return 0;
1712}
1713
1714int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest)
1715{
1716 return apic->vcpu->vcpu_id == dest;
1717}
1718
1719int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda)
1720{
1721 return 0;
1722}
1723
1724struct kvm_vcpu *kvm_get_lowest_prio_vcpu(struct kvm *kvm, u8 vector,
1725 unsigned long bitmap)
1726{
1727 struct kvm_vcpu *lvcpu = kvm->vcpus[0];
1728 int i;
1729
1730 for (i = 1; i < KVM_MAX_VCPUS; i++) {
1731 if (!kvm->vcpus[i])
1732 continue;
1733 if (lvcpu->arch.xtp > kvm->vcpus[i]->arch.xtp)
1734 lvcpu = kvm->vcpus[i];
1735 }
1736
1737 return lvcpu;
1738}
1739
1740static int find_highest_bits(int *dat)
1741{
1742 u32 bits, bitnum;
1743 int i;
1744
1745 /* loop for all 256 bits */
1746 for (i = 7; i >= 0 ; i--) {
1747 bits = dat[i];
1748 if (bits) {
1749 bitnum = fls(bits);
1750 return i * 32 + bitnum - 1;
1751 }
1752 }
1753
1754 return -1;
1755}
1756
1757int kvm_highest_pending_irq(struct kvm_vcpu *vcpu)
1758{
1759 struct vpd *vpd = to_host(vcpu->kvm, vcpu->arch.vpd);
1760
1761 if (vpd->irr[0] & (1UL << NMI_VECTOR))
1762 return NMI_VECTOR;
1763 if (vpd->irr[0] & (1UL << ExtINT_VECTOR))
1764 return ExtINT_VECTOR;
1765
1766 return find_highest_bits((int *)&vpd->irr[0]);
1767}
1768
1769int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
1770{
1771 if (kvm_highest_pending_irq(vcpu) != -1)
1772 return 1;
1773 return 0;
1774}
1775
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001776int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1777{
1778 return 0;
1779}
1780
Xiantao Zhangb024b792008-04-01 15:29:29 +08001781gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
1782{
1783 return gfn;
1784}
1785
1786int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1787{
Avi Kivitya4535292008-04-13 17:54:35 +03001788 return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE;
Xiantao Zhangb024b792008-04-01 15:29:29 +08001789}
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001790
1791int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
1792 struct kvm_mp_state *mp_state)
1793{
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001794 vcpu_load(vcpu);
1795 mp_state->mp_state = vcpu->arch.mp_state;
1796 vcpu_put(vcpu);
1797 return 0;
1798}
1799
1800static int vcpu_reset(struct kvm_vcpu *vcpu)
1801{
1802 int r;
1803 long psr;
1804 local_irq_save(psr);
1805 r = kvm_insert_vmm_mapping(vcpu);
1806 if (r)
1807 goto fail;
1808
1809 vcpu->arch.launched = 0;
1810 kvm_arch_vcpu_uninit(vcpu);
1811 r = kvm_arch_vcpu_init(vcpu);
1812 if (r)
1813 goto fail;
1814
1815 kvm_purge_vmm_mapping(vcpu);
1816 r = 0;
1817fail:
1818 local_irq_restore(psr);
1819 return r;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001820}
1821
1822int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
1823 struct kvm_mp_state *mp_state)
1824{
Xiantao Zhang8c4b5372008-08-28 09:34:08 +08001825 int r = 0;
1826
1827 vcpu_load(vcpu);
1828 vcpu->arch.mp_state = mp_state->mp_state;
1829 if (vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)
1830 r = vcpu_reset(vcpu);
1831 vcpu_put(vcpu);
1832 return r;
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03001833}