ia64/xen-unstable

view xen/arch/ia64/vmx/vmx_init.c @ 16785:af3550f53874

[IA64] domheap: Don't pin xenheap down. Now it's unnecessary.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Alex Williamson <alex.williamson@hp.com>
date Thu Jan 17 12:05:43 2008 -0700 (2008-01-17)
parents 6f7e6608cb74
children a739d3edc185
line source
1 /* -*- Mode:C; c-basic-offset:4; tab-width:4; indent-tabs-mode:nil -*- */
2 /*
3 * vmx_init.c: initialization work for vt specific domain
4 * Copyright (c) 2005, Intel Corporation.
5 * Kun Tian (Kevin Tian) <kevin.tian@intel.com>
6 * Xuefei Xu (Anthony Xu) <anthony.xu@intel.com>
7 * Fred Yang <fred.yang@intel.com>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20 * Place - Suite 330, Boston, MA 02111-1307 USA.
21 *
22 */
24 /*
25 * 05/08/16 Kun tian (Kevin Tian) <kevin.tian@intel.com>:
26 * Disable doubling mapping
27 *
28 * 05/03/23 Kun Tian (Kevin Tian) <kevin.tian@intel.com>:
29 * Simplied design in first step:
30 * - One virtual environment
31 * - Domain is bound to one LP
32 * Later to support guest SMP:
33 * - Need interface to handle VP scheduled to different LP
34 */
35 #include <xen/config.h>
36 #include <xen/types.h>
37 #include <xen/sched.h>
38 #include <asm/pal.h>
39 #include <asm/page.h>
40 #include <asm/processor.h>
41 #include <asm/vmx_vcpu.h>
42 #include <xen/lib.h>
43 #include <asm/vmmu.h>
44 #include <public/xen.h>
45 #include <public/hvm/ioreq.h>
46 #include <public/event_channel.h>
47 #include <asm/vmx_phy_mode.h>
48 #include <asm/processor.h>
49 #include <asm/vmx.h>
50 #include <xen/mm.h>
51 #include <asm/viosapic.h>
52 #include <xen/event.h>
53 #include <asm/vlsapic.h>
54 #include <asm/vhpt.h>
55 #include "entry.h"
57 /* Global flag to identify whether Intel vmx feature is on */
58 u32 vmx_enabled = 0;
59 static u32 vm_order;
60 static u64 buffer_size;
61 static u64 vp_env_info;
62 static u64 vm_buffer = 0; /* Buffer required to bring up VMX feature */
63 u64 __vsa_base = 0; /* Run-time service base of VMX */
65 /* Check whether vt feature is enabled or not. */
66 void
67 identify_vmx_feature(void)
68 {
69 pal_status_t ret;
70 u64 avail = 1, status = 1, control = 1;
72 vmx_enabled = 0;
73 /* Check VT-i feature */
74 ret = ia64_pal_proc_get_features(&avail, &status, &control);
75 if (ret != PAL_STATUS_SUCCESS) {
76 printk("Get proc features failed.\n");
77 goto no_vti;
78 }
80 /* FIXME: do we need to check status field, to see whether
81 * PSR.vm is actually enabled? If yes, aonther call to
82 * ia64_pal_proc_set_features may be reuqired then.
83 */
84 printk("avail:0x%lx, status:0x%lx,control:0x%lx, vm?0x%lx\n",
85 avail, status, control, avail & PAL_PROC_VM_BIT);
86 if (!(avail & PAL_PROC_VM_BIT)) {
87 printk("No VT feature supported.\n");
88 goto no_vti;
89 }
91 ret = ia64_pal_vp_env_info(&buffer_size, &vp_env_info);
92 if (ret != PAL_STATUS_SUCCESS) {
93 printk("Get vp environment info failed.\n");
94 goto no_vti;
95 }
97 /* Does xen has ability to decode itself? */
98 if (!(vp_env_info & VP_OPCODE))
99 printk("WARNING: no opcode provided from hardware(%lx)!!!\n", vp_env_info);
100 vm_order = get_order(buffer_size);
101 printk("vm buffer size: %ld, order: %d\n", buffer_size, vm_order);
103 vmx_enabled = 1;
104 no_vti:
105 return;
106 }
108 /*
109 * Init virtual environment on current LP
110 * vsa_base is the indicator whether it's first LP to be initialized
111 * for current domain.
112 */
113 void
114 vmx_init_env(void)
115 {
116 u64 status, tmp_base;
118 if (!vm_buffer) {
119 vm_buffer = (unsigned long)alloc_xenheap_pages(vm_order);
120 ASSERT(vm_buffer);
121 vm_buffer = virt_to_xenva((vm_buffer));
122 printk("vm_buffer: 0x%lx\n", vm_buffer);
123 }
125 status=ia64_pal_vp_init_env(__vsa_base ? VP_INIT_ENV : VP_INIT_ENV_INITALIZE,
126 __pa(vm_buffer),
127 vm_buffer,
128 &tmp_base);
130 if (status != PAL_STATUS_SUCCESS) {
131 printk("ia64_pal_vp_init_env failed.\n");
132 return ;
133 }
135 if (!__vsa_base)
136 __vsa_base = tmp_base;
137 else
138 ASSERT(tmp_base == __vsa_base);
140 }
142 typedef union {
143 u64 value;
144 struct {
145 u64 number : 8;
146 u64 revision : 8;
147 u64 model : 8;
148 u64 family : 8;
149 u64 archrev : 8;
150 u64 rv : 24;
151 };
152 } cpuid3_t;
154 /* Allocate vpd from domheap */
155 static vpd_t *alloc_vpd(void)
156 {
157 int i;
158 cpuid3_t cpuid3;
159 struct page_info *page;
160 vpd_t *vpd;
161 mapped_regs_t *mregs;
163 page = alloc_domheap_pages(NULL, get_order(VPD_SIZE), 0);
164 if (page == NULL) {
165 printk("VPD allocation failed.\n");
166 return NULL;
167 }
168 vpd = page_to_virt(page);
170 printk(XENLOG_DEBUG "vpd base: 0x%p, vpd size:%ld\n",
171 vpd, sizeof(vpd_t));
172 memset(vpd, 0, VPD_SIZE);
173 mregs = &vpd->vpd_low;
175 /* CPUID init */
176 for (i = 0; i < 5; i++)
177 mregs->vcpuid[i] = ia64_get_cpuid(i);
179 /* Limit the CPUID number to 5 */
180 cpuid3.value = mregs->vcpuid[3];
181 cpuid3.number = 4; /* 5 - 1 */
182 mregs->vcpuid[3] = cpuid3.value;
184 mregs->vac.a_from_int_cr = 1;
185 mregs->vac.a_to_int_cr = 1;
186 mregs->vac.a_from_psr = 1;
187 mregs->vac.a_from_cpuid = 1;
188 mregs->vac.a_cover = 1;
189 mregs->vac.a_bsw = 1;
190 mregs->vac.a_int = 1;
191 mregs->vdc.d_vmsw = 1;
193 return vpd;
194 }
196 /* Free vpd to domheap */
197 static void
198 free_vpd(struct vcpu *v)
199 {
200 if ( v->arch.privregs )
201 free_domheap_pages(virt_to_page(v->arch.privregs),
202 get_order(VPD_SIZE));
203 }
205 // This is used for PAL_VP_CREATE and PAL_VPS_SET_PENDING_INTERRUPT
206 // so that we don't have to pin the vpd down with itr[].
207 void
208 __vmx_vpd_pin(struct vcpu* v)
209 {
210 unsigned long privregs = (unsigned long)v->arch.privregs;
211 u64 psr;
213 privregs &= ~(IA64_GRANULE_SIZE - 1);
215 // check overlapping with current stack
216 if (privregs ==
217 ((unsigned long)current & ~(IA64_GRANULE_SIZE - 1)))
218 return;
220 if (!VMX_DOMAIN(current)) {
221 // check overlapping with vhpt
222 if (privregs ==
223 (vcpu_vhpt_maddr(current) & ~(IA64_GRANULE_SHIFT - 1)))
224 return;
225 } else {
226 // check overlapping with vhpt
227 if (privregs ==
228 ((unsigned long)current->arch.vhpt.hash &
229 ~(IA64_GRANULE_SHIFT - 1)))
230 return;
232 // check overlapping with privregs
233 if (privregs ==
234 ((unsigned long)current->arch.privregs &
235 ~(IA64_GRANULE_SHIFT - 1)))
236 return;
237 }
239 psr = ia64_clear_ic();
240 ia64_ptr(0x2 /*D*/, privregs, IA64_GRANULE_SIZE);
241 ia64_srlz_d();
242 ia64_itr(0x2 /*D*/, IA64_TR_MAPPED_REGS, privregs,
243 pte_val(pfn_pte(__pa(privregs) >> PAGE_SHIFT, PAGE_KERNEL)),
244 IA64_GRANULE_SHIFT);
245 ia64_set_psr(psr);
246 ia64_srlz_d();
247 }
249 void
250 __vmx_vpd_unpin(struct vcpu* v)
251 {
252 if (!VMX_DOMAIN(current)) {
253 int rc;
254 rc = !set_one_rr(VRN7 << VRN_SHIFT, VCPU(current, rrs[VRN7]));
255 BUG_ON(rc);
256 } else {
257 IA64FAULT fault;
258 fault = vmx_vcpu_set_rr(current, VRN7 << VRN_SHIFT,
259 VMX(current, vrr[VRN7]));
260 BUG_ON(fault != IA64_NO_FAULT);
261 }
262 }
264 /*
265 * Create a VP on intialized VMX environment.
266 */
267 static void
268 vmx_create_vp(struct vcpu *v)
269 {
270 u64 ret;
271 vpd_t *vpd = (vpd_t *)v->arch.privregs;
272 u64 ivt_base;
273 extern char vmx_ia64_ivt;
274 /* ia64_ivt is function pointer, so need this tranlation */
275 ivt_base = (u64) &vmx_ia64_ivt;
276 printk(XENLOG_DEBUG "ivt_base: 0x%lx\n", ivt_base);
278 vmx_vpd_pin(v);
279 ret = ia64_pal_vp_create((u64 *)vpd, (u64 *)ivt_base, 0);
280 vmx_vpd_unpin(v);
282 if (ret != PAL_STATUS_SUCCESS){
283 panic_domain(vcpu_regs(v),"ia64_pal_vp_create failed. \n");
284 }
285 }
287 /* Other non-context related tasks can be done in context switch */
288 void
289 vmx_save_state(struct vcpu *v)
290 {
291 u64 status;
293 BUG_ON(v != current);
294 /* FIXME: about setting of pal_proc_vector... time consuming */
295 status = ia64_pal_vp_save((u64 *)v->arch.privregs, 0);
296 if (status != PAL_STATUS_SUCCESS){
297 panic_domain(vcpu_regs(v),"Save vp status failed\n");
298 }
301 /* Need to save KR when domain switch, though HV itself doesn;t
302 * use them.
303 */
304 v->arch.arch_vmx.vkr[0] = ia64_get_kr(0);
305 v->arch.arch_vmx.vkr[1] = ia64_get_kr(1);
306 v->arch.arch_vmx.vkr[2] = ia64_get_kr(2);
307 v->arch.arch_vmx.vkr[3] = ia64_get_kr(3);
308 v->arch.arch_vmx.vkr[4] = ia64_get_kr(4);
309 v->arch.arch_vmx.vkr[5] = ia64_get_kr(5);
310 v->arch.arch_vmx.vkr[6] = ia64_get_kr(6);
311 v->arch.arch_vmx.vkr[7] = ia64_get_kr(7);
312 }
314 /* Even guest is in physical mode, we still need such double mapping */
315 void
316 vmx_load_state(struct vcpu *v)
317 {
318 u64 status;
320 BUG_ON(v != current);
321 status = ia64_pal_vp_restore((u64 *)v->arch.privregs, 0);
322 if (status != PAL_STATUS_SUCCESS){
323 panic_domain(vcpu_regs(v),"Restore vp status failed\n");
324 }
326 ia64_set_kr(0, v->arch.arch_vmx.vkr[0]);
327 ia64_set_kr(1, v->arch.arch_vmx.vkr[1]);
328 ia64_set_kr(2, v->arch.arch_vmx.vkr[2]);
329 ia64_set_kr(3, v->arch.arch_vmx.vkr[3]);
330 ia64_set_kr(4, v->arch.arch_vmx.vkr[4]);
331 ia64_set_kr(5, v->arch.arch_vmx.vkr[5]);
332 ia64_set_kr(6, v->arch.arch_vmx.vkr[6]);
333 ia64_set_kr(7, v->arch.arch_vmx.vkr[7]);
334 /* Guest vTLB is not required to be switched explicitly, since
335 * anchored in vcpu */
336 }
338 static int
339 vmx_vcpu_initialise(struct vcpu *v)
340 {
341 struct vmx_ioreq_page *iorp = &v->domain->arch.hvm_domain.ioreq;
343 int rc = alloc_unbound_xen_event_channel(v, 0);
344 if (rc < 0)
345 return rc;
346 v->arch.arch_vmx.xen_port = rc;
348 spin_lock(&iorp->lock);
349 if (v->domain->arch.vmx_platform.ioreq.va != 0) {
350 vcpu_iodata_t *p = get_vio(v);
351 p->vp_eport = v->arch.arch_vmx.xen_port;
352 }
353 spin_unlock(&iorp->lock);
355 gdprintk(XENLOG_INFO, "Allocated port %ld for hvm %d vcpu %d.\n",
356 v->arch.arch_vmx.xen_port, v->domain->domain_id, v->vcpu_id);
358 return 0;
359 }
361 static int vmx_create_event_channels(struct vcpu *v)
362 {
363 struct vcpu *o;
365 if (v->vcpu_id == 0) {
366 /* Ugly: create event channels for every vcpu when vcpu 0
367 starts, so that they're available for ioemu to bind to. */
368 for_each_vcpu(v->domain, o) {
369 int rc = vmx_vcpu_initialise(o);
370 if (rc < 0) //XXX error recovery
371 return rc;
372 }
373 }
375 return 0;
376 }
378 /*
379 * Event channel has destoryed in domain_kill(), so we needn't
380 * do anything here
381 */
382 static void vmx_release_assist_channel(struct vcpu *v)
383 {
384 return;
385 }
387 /* following three functions are based from hvm_xxx_ioreq_page()
388 * in xen/arch/x86/hvm/hvm.c */
389 static void vmx_init_ioreq_page(
390 struct domain *d, struct vmx_ioreq_page *iorp)
391 {
392 memset(iorp, 0, sizeof(*iorp));
393 spin_lock_init(&iorp->lock);
394 domain_pause(d);
395 }
397 static void vmx_destroy_ioreq_page(
398 struct domain *d, struct vmx_ioreq_page *iorp)
399 {
400 spin_lock(&iorp->lock);
402 ASSERT(d->is_dying);
404 if (iorp->va != NULL) {
405 put_page(iorp->page);
406 iorp->page = NULL;
407 iorp->va = NULL;
408 }
410 spin_unlock(&iorp->lock);
411 }
413 int vmx_set_ioreq_page(
414 struct domain *d, struct vmx_ioreq_page *iorp, unsigned long gpfn)
415 {
416 struct page_info *page;
417 unsigned long mfn;
418 pte_t pte;
420 pte = *lookup_noalloc_domain_pte(d, gpfn << PAGE_SHIFT);
421 if (!pte_present(pte) || !pte_mem(pte))
422 return -EINVAL;
423 mfn = (pte_val(pte) & _PFN_MASK) >> PAGE_SHIFT;
424 ASSERT(mfn_valid(mfn));
426 page = mfn_to_page(mfn);
427 if (get_page(page, d) == 0)
428 return -EINVAL;
430 spin_lock(&iorp->lock);
432 if ((iorp->va != NULL) || d->is_dying) {
433 spin_unlock(&iorp->lock);
434 put_page(page);
435 return -EINVAL;
436 }
438 iorp->va = mfn_to_virt(mfn);
439 iorp->page = page;
441 spin_unlock(&iorp->lock);
443 domain_unpause(d);
445 return 0;
446 }
448 /*
449 * Initialize VMX envirenment for guest. Only the 1st vp/vcpu
450 * is registered here.
451 */
452 int
453 vmx_final_setup_guest(struct vcpu *v)
454 {
455 vpd_t *vpd;
456 int rc;
457 struct switch_stack *sw;
459 vpd = alloc_vpd();
460 ASSERT(vpd);
461 if (!vpd)
462 return -ENOMEM;
464 v->arch.privregs = (mapped_regs_t *)vpd;
465 vpd->vpd_low.virt_env_vaddr = vm_buffer;
467 /* Per-domain vTLB and vhpt implementation. Now vmx domain will stick
468 * to this solution. Maybe it can be deferred until we know created
469 * one as vmx domain */
470 rc = init_domain_tlb(v);
471 if (rc)
472 return rc;
474 rc = vmx_create_event_channels(v);
475 if (rc)
476 return rc;
478 /* v->arch.schedule_tail = arch_vmx_do_launch; */
479 vmx_create_vp(v);
481 /* Physical mode emulation initialization, including
482 * emulation ID allcation and related memory request
483 */
484 physical_mode_init(v);
486 vlsapic_reset(v);
487 vtm_init(v);
489 /* Set up guest 's indicator for VTi domain*/
490 set_bit(ARCH_VMX_DOMAIN, &v->arch.arch_vmx.flags);
492 /* Initialize pNonSys=1 for the first context switching */
493 sw = (struct switch_stack *)vcpu_regs(v) - 1;
494 sw->pr = (1UL << PRED_NON_SYSCALL);
496 return 0;
497 }
499 void
500 vmx_relinquish_guest_resources(struct domain *d)
501 {
502 struct vcpu *v;
504 for_each_vcpu(d, v)
505 vmx_release_assist_channel(v);
507 vacpi_relinquish_resources(d);
509 vmx_destroy_ioreq_page(d, &d->arch.vmx_platform.ioreq);
510 vmx_destroy_ioreq_page(d, &d->arch.vmx_platform.buf_ioreq);
511 vmx_destroy_ioreq_page(d, &d->arch.vmx_platform.buf_pioreq);
512 }
514 void
515 vmx_relinquish_vcpu_resources(struct vcpu *v)
516 {
517 vtime_t *vtm = &(v->arch.arch_vmx.vtm);
519 kill_timer(&vtm->vtm_timer);
521 free_domain_tlb(v);
522 free_vpd(v);
523 }
525 typedef struct io_range {
526 unsigned long start;
527 unsigned long size;
528 unsigned long type;
529 } io_range_t;
531 static const io_range_t io_ranges[] = {
532 {VGA_IO_START, VGA_IO_SIZE, GPFN_FRAME_BUFFER},
533 {MMIO_START, MMIO_SIZE, GPFN_LOW_MMIO},
534 {LEGACY_IO_START, LEGACY_IO_SIZE, GPFN_LEGACY_IO},
535 {IO_SAPIC_START, IO_SAPIC_SIZE, GPFN_IOSAPIC},
536 {PIB_START, PIB_SIZE, GPFN_PIB},
537 };
539 // The P2M table is built in libxc/ia64/xc_ia64_hvm_build.c @ setup_guest()
540 // so only mark IO memory space here
541 static void vmx_build_io_physmap_table(struct domain *d)
542 {
543 unsigned long i, j;
545 /* Mark I/O ranges */
546 for (i = 0; i < (sizeof(io_ranges) / sizeof(io_range_t)); i++) {
547 for (j = io_ranges[i].start;
548 j < io_ranges[i].start + io_ranges[i].size; j += PAGE_SIZE)
549 (void)__assign_domain_page(d, j, io_ranges[i].type,
550 ASSIGN_writable);
551 }
553 }
555 int vmx_setup_platform(struct domain *d)
556 {
557 ASSERT(d != dom0); /* only for non-privileged vti domain */
559 vmx_build_io_physmap_table(d);
561 vmx_init_ioreq_page(d, &d->arch.vmx_platform.ioreq);
562 vmx_init_ioreq_page(d, &d->arch.vmx_platform.buf_ioreq);
563 vmx_init_ioreq_page(d, &d->arch.vmx_platform.buf_pioreq);
565 /* TEMP */
566 d->arch.vmx_platform.pib_base = 0xfee00000UL;
568 d->arch.sal_data = xmalloc(struct xen_sal_data);
569 if (d->arch.sal_data == NULL)
570 return -ENOMEM;
572 /* Only open one port for I/O and interrupt emulation */
573 memset(&d->shared_info->evtchn_mask[0], 0xff,
574 sizeof(d->shared_info->evtchn_mask));
576 /* Initialize iosapic model within hypervisor */
577 viosapic_init(d);
579 vacpi_init(d);
581 return 0;
582 }
584 void vmx_do_resume(struct vcpu *v)
585 {
586 ioreq_t *p;
588 vmx_load_all_rr(v);
589 vmx_load_state(v);
590 migrate_timer(&v->arch.arch_vmx.vtm.vtm_timer, v->processor);
592 /* stolen from hvm_do_resume() in arch/x86/hvm/hvm.c */
593 /* NB. Optimised for common case (p->state == STATE_IOREQ_NONE). */
594 p = &get_vio(v)->vp_ioreq;
595 while (p->state != STATE_IOREQ_NONE) {
596 switch (p->state) {
597 case STATE_IORESP_READY: /* IORESP_READY -> NONE */
598 vmx_io_assist(v);
599 break;
600 case STATE_IOREQ_READY:
601 case STATE_IOREQ_INPROCESS:
602 /* IOREQ_{READY,INPROCESS} -> IORESP_READY */
603 wait_on_xen_event_channel(v->arch.arch_vmx.xen_port,
604 (p->state != STATE_IOREQ_READY) &&
605 (p->state != STATE_IOREQ_INPROCESS));
606 break;
607 default:
608 gdprintk(XENLOG_ERR,
609 "Weird HVM iorequest state %d.\n", p->state);
610 domain_crash_synchronous();
611 }
612 }
613 }