ia64/xen-unstable

view xen/arch/ia64/vmx/vmx_init.c @ 12011:1eb880e9ff94

[IA64] fix domVTi boot with maxmem

We found the following bug.
http://bugzilla.xensource.com/bugzilla/show_bug.cgi?id=800

I change assigned page number by assign_domain_page()
from d->max_pages to d->tot_pages in vmx_build_physmap_table()
because domVTi don't support maxmem.

Signed-off-by: Akio Takebe <takebe_akio@jp.fujitsu.com>
author awilliam@xenbuild.aw
date Tue Oct 31 22:25:25 2006 -0700 (2006-10-31)
parents 3e26719ab827
children 8ee7bb2af497
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/hvm/vioapic.h>
52 #include <xen/event.h>
53 #include <asm/vlsapic.h>
55 /* Global flag to identify whether Intel vmx feature is on */
56 u32 vmx_enabled = 0;
57 unsigned int opt_vmx_debug_level = 0;
58 static u32 vm_order;
59 static u64 buffer_size;
60 static u64 vp_env_info;
61 static u64 vm_buffer = 0; /* Buffer required to bring up VMX feature */
62 u64 __vsa_base = 0; /* Run-time service base of VMX */
64 /* Check whether vt feature is enabled or not. */
65 void
66 identify_vmx_feature(void)
67 {
68 pal_status_t ret;
69 u64 avail = 1, status = 1, control = 1;
71 vmx_enabled = 0;
72 /* Check VT-i feature */
73 ret = ia64_pal_proc_get_features(&avail, &status, &control);
74 if (ret != PAL_STATUS_SUCCESS) {
75 printk("Get proc features failed.\n");
76 goto no_vti;
77 }
79 /* FIXME: do we need to check status field, to see whether
80 * PSR.vm is actually enabled? If yes, aonther call to
81 * ia64_pal_proc_set_features may be reuqired then.
82 */
83 printk("avail:0x%lx, status:0x%lx,control:0x%lx, vm?0x%lx\n",
84 avail, status, control, avail & PAL_PROC_VM_BIT);
85 if (!(avail & PAL_PROC_VM_BIT)) {
86 printk("No VT feature supported.\n");
87 goto no_vti;
88 }
90 ret = ia64_pal_vp_env_info(&buffer_size, &vp_env_info);
91 if (ret != PAL_STATUS_SUCCESS) {
92 printk("Get vp environment info failed.\n");
93 goto no_vti;
94 }
96 /* Does xen has ability to decode itself? */
97 if (!(vp_env_info & VP_OPCODE))
98 printk("WARNING: no opcode provided from hardware(%lx)!!!\n", vp_env_info);
99 vm_order = get_order(buffer_size);
100 printk("vm buffer size: %ld, order: %d\n", buffer_size, vm_order);
102 vmx_enabled = 1;
103 no_vti:
104 return;
105 }
107 /*
108 * Init virtual environment on current LP
109 * vsa_base is the indicator whether it's first LP to be initialized
110 * for current domain.
111 */
112 void
113 vmx_init_env(void)
114 {
115 u64 status, tmp_base;
117 if (!vm_buffer) {
118 vm_buffer = (unsigned long)alloc_xenheap_pages(vm_order);
119 ASSERT(vm_buffer);
120 printk("vm_buffer: 0x%lx\n", vm_buffer);
121 }
123 status=ia64_pal_vp_init_env(__vsa_base ? VP_INIT_ENV : VP_INIT_ENV_INITALIZE,
124 __pa(vm_buffer),
125 vm_buffer,
126 &tmp_base);
128 if (status != PAL_STATUS_SUCCESS) {
129 printk("ia64_pal_vp_init_env failed.\n");
130 return ;
131 }
133 if (!__vsa_base)
134 __vsa_base = tmp_base;
135 else
136 ASSERT(tmp_base != __vsa_base);
138 }
140 typedef union {
141 u64 value;
142 struct {
143 u64 number : 8;
144 u64 revision : 8;
145 u64 model : 8;
146 u64 family : 8;
147 u64 archrev : 8;
148 u64 rv : 24;
149 };
150 } cpuid3_t;
152 /* Allocate vpd from xenheap */
153 static vpd_t *alloc_vpd(void)
154 {
155 int i;
156 cpuid3_t cpuid3;
157 vpd_t *vpd;
158 mapped_regs_t *mregs;
160 vpd = alloc_xenheap_pages(get_order(VPD_SIZE));
161 if (!vpd) {
162 printk("VPD allocation failed.\n");
163 return NULL;
164 }
166 printk("vpd base: 0x%p, vpd size:%ld\n", vpd, sizeof(vpd_t));
167 memset(vpd, 0, VPD_SIZE);
168 mregs = &vpd->vpd_low;
170 /* CPUID init */
171 for (i = 0; i < 5; i++)
172 mregs->vcpuid[i] = ia64_get_cpuid(i);
174 /* Limit the CPUID number to 5 */
175 cpuid3.value = mregs->vcpuid[3];
176 cpuid3.number = 4; /* 5 - 1 */
177 mregs->vcpuid[3] = cpuid3.value;
179 mregs->vac.a_from_int_cr = 1;
180 mregs->vac.a_to_int_cr = 1;
181 mregs->vac.a_from_psr = 1;
182 mregs->vac.a_from_cpuid = 1;
183 mregs->vac.a_cover = 1;
184 mregs->vac.a_bsw = 1;
185 mregs->vac.a_int = 1;
187 mregs->vdc.d_vmsw = 1;
189 return vpd;
190 }
192 /* Free vpd to xenheap */
193 static void
194 free_vpd(struct vcpu *v)
195 {
196 if ( v->arch.privregs )
197 free_xenheap_pages(v->arch.privregs, get_order(VPD_SIZE));
198 }
200 /*
201 * Create a VP on intialized VMX environment.
202 */
203 static void
204 vmx_create_vp(struct vcpu *v)
205 {
206 u64 ret;
207 vpd_t *vpd = (vpd_t *)v->arch.privregs;
208 u64 ivt_base;
209 extern char vmx_ia64_ivt;
210 /* ia64_ivt is function pointer, so need this tranlation */
211 ivt_base = (u64) &vmx_ia64_ivt;
212 printk("ivt_base: 0x%lx\n", ivt_base);
213 ret = ia64_pal_vp_create((u64 *)vpd, (u64 *)ivt_base, 0);
214 if (ret != PAL_STATUS_SUCCESS){
215 panic_domain(vcpu_regs(v),"ia64_pal_vp_create failed. \n");
216 }
217 }
219 /* Other non-context related tasks can be done in context switch */
220 void
221 vmx_save_state(struct vcpu *v)
222 {
223 u64 status;
225 /* FIXME: about setting of pal_proc_vector... time consuming */
226 status = ia64_pal_vp_save((u64 *)v->arch.privregs, 0);
227 if (status != PAL_STATUS_SUCCESS){
228 panic_domain(vcpu_regs(v),"Save vp status failed\n");
229 }
232 /* Need to save KR when domain switch, though HV itself doesn;t
233 * use them.
234 */
235 v->arch.arch_vmx.vkr[0] = ia64_get_kr(0);
236 v->arch.arch_vmx.vkr[1] = ia64_get_kr(1);
237 v->arch.arch_vmx.vkr[2] = ia64_get_kr(2);
238 v->arch.arch_vmx.vkr[3] = ia64_get_kr(3);
239 v->arch.arch_vmx.vkr[4] = ia64_get_kr(4);
240 v->arch.arch_vmx.vkr[5] = ia64_get_kr(5);
241 v->arch.arch_vmx.vkr[6] = ia64_get_kr(6);
242 v->arch.arch_vmx.vkr[7] = ia64_get_kr(7);
243 }
245 /* Even guest is in physical mode, we still need such double mapping */
246 void
247 vmx_load_state(struct vcpu *v)
248 {
249 u64 status;
251 status = ia64_pal_vp_restore((u64 *)v->arch.privregs, 0);
252 if (status != PAL_STATUS_SUCCESS){
253 panic_domain(vcpu_regs(v),"Restore vp status failed\n");
254 }
256 ia64_set_kr(0, v->arch.arch_vmx.vkr[0]);
257 ia64_set_kr(1, v->arch.arch_vmx.vkr[1]);
258 ia64_set_kr(2, v->arch.arch_vmx.vkr[2]);
259 ia64_set_kr(3, v->arch.arch_vmx.vkr[3]);
260 ia64_set_kr(4, v->arch.arch_vmx.vkr[4]);
261 ia64_set_kr(5, v->arch.arch_vmx.vkr[5]);
262 ia64_set_kr(6, v->arch.arch_vmx.vkr[6]);
263 ia64_set_kr(7, v->arch.arch_vmx.vkr[7]);
264 /* Guest vTLB is not required to be switched explicitly, since
265 * anchored in vcpu */
266 }
268 static void vmx_create_event_channels(struct vcpu *v)
269 {
270 vcpu_iodata_t *p;
271 struct vcpu *o;
273 if (v->vcpu_id == 0) {
274 /* Ugly: create event channels for every vcpu when vcpu 0
275 starts, so that they're available for ioemu to bind to. */
276 for_each_vcpu(v->domain, o) {
277 p = get_vio(v->domain, o->vcpu_id);
278 o->arch.arch_vmx.xen_port = p->vp_eport =
279 alloc_unbound_xen_event_channel(o, 0);
280 DPRINTK("Allocated port %d for hvm.\n",
281 o->arch.arch_vmx.xen_port);
282 }
283 }
284 }
286 static void vmx_release_assist_channel(struct vcpu *v)
287 {
288 free_xen_event_channel(v, v->arch.arch_vmx.xen_port);
289 }
291 /*
292 * Initialize VMX envirenment for guest. Only the 1st vp/vcpu
293 * is registered here.
294 */
295 void
296 vmx_final_setup_guest(struct vcpu *v)
297 {
298 vpd_t *vpd;
300 vpd = alloc_vpd();
301 ASSERT(vpd);
303 v->arch.privregs = (mapped_regs_t *)vpd;
304 vpd->vpd_low.virt_env_vaddr = vm_buffer;
306 /* Per-domain vTLB and vhpt implementation. Now vmx domain will stick
307 * to this solution. Maybe it can be deferred until we know created
308 * one as vmx domain */
309 #ifndef HASH_VHPT
310 init_domain_tlb(v);
311 #endif
312 vmx_create_event_channels(v);
314 /* v->arch.schedule_tail = arch_vmx_do_launch; */
315 vmx_create_vp(v);
317 /* Physical mode emulation initialization, including
318 * emulation ID allcation and related memory request
319 */
320 physical_mode_init(v);
322 vlsapic_reset(v);
323 vtm_init(v);
325 /* One more step to enable interrupt assist */
326 set_bit(ARCH_VMX_INTR_ASSIST, &v->arch.arch_vmx.flags);
327 /* Set up guest 's indicator for VTi domain*/
328 set_bit(ARCH_VMX_DOMAIN, &v->arch.arch_vmx.flags);
329 }
331 void
332 vmx_relinquish_guest_resources(struct domain *d)
333 {
334 struct vcpu *v;
336 for_each_vcpu(d, v)
337 vmx_release_assist_channel(v);
338 }
340 void
341 vmx_relinquish_vcpu_resources(struct vcpu *v)
342 {
343 vtime_t *vtm = &(v->arch.arch_vmx.vtm);
345 kill_timer(&vtm->vtm_timer);
347 free_domain_tlb(v);
348 free_vpd(v);
349 }
351 typedef struct io_range {
352 unsigned long start;
353 unsigned long size;
354 unsigned long type;
355 } io_range_t;
357 static const io_range_t io_ranges[] = {
358 {VGA_IO_START, VGA_IO_SIZE, GPFN_FRAME_BUFFER},
359 {MMIO_START, MMIO_SIZE, GPFN_LOW_MMIO},
360 {LEGACY_IO_START, LEGACY_IO_SIZE, GPFN_LEGACY_IO},
361 {IO_SAPIC_START, IO_SAPIC_SIZE, GPFN_IOSAPIC},
362 {PIB_START, PIB_SIZE, GPFN_PIB},
363 };
365 /* Reseve 1 page for shared I/O ,1 page for xenstore and 1 page for buffer I/O. */
366 #define VMX_SYS_PAGES (3 + (GFW_SIZE >> PAGE_SHIFT))
367 /* If we support maxmem for domVTi, we should change from tot_page to max_pages.
368 * #define VMX_CONFIG_PAGES(d) ((d)->max_pages - VMX_SYS_PAGES)
369 */
370 #define VMX_CONFIG_PAGES(d) ((d)->tot_pages - VMX_SYS_PAGES)
372 static void vmx_build_physmap_table(struct domain *d)
373 {
374 unsigned long i, j, start, tmp, end, mfn;
375 struct list_head *list_ent = d->page_list.next;
377 ASSERT(d->max_pages == d->tot_pages);
379 /* Mark I/O ranges */
380 for (i = 0; i < (sizeof(io_ranges) / sizeof(io_range_t)); i++) {
381 for (j = io_ranges[i].start;
382 j < io_ranges[i].start + io_ranges[i].size;
383 j += PAGE_SIZE)
384 (void)__assign_domain_page(d, j, io_ranges[i].type,
385 ASSIGN_writable);
386 }
388 /* Map normal memory below 3G */
389 end = VMX_CONFIG_PAGES(d) << PAGE_SHIFT;
390 tmp = end < MMIO_START ? end : MMIO_START;
391 for (i = 0; (i < tmp) && (list_ent != &d->page_list); i += PAGE_SIZE) {
392 mfn = page_to_mfn(list_entry(list_ent, struct page_info, list));
393 list_ent = mfn_to_page(mfn)->list.next;
394 if (VGA_IO_START <= i && i < VGA_IO_START + VGA_IO_SIZE)
395 continue;
396 assign_domain_page(d, i, mfn << PAGE_SHIFT);
397 }
398 ASSERT(list_ent != &d->page_list);
400 /* Map normal memory beyond 4G */
401 if (unlikely(end > MMIO_START)) {
402 start = 4 * MEM_G;
403 end = start + (end - 3 * MEM_G);
404 for (i = start;
405 (i < end) && (list_ent != &d->page_list); i += PAGE_SIZE) {
406 mfn = page_to_mfn(list_entry(list_ent, struct page_info, list));
407 assign_domain_page(d, i, mfn << PAGE_SHIFT);
408 list_ent = mfn_to_page(mfn)->list.next;
409 }
410 ASSERT(list_ent != &d->page_list);
411 }
413 /* Map guest firmware */
414 for (i = GFW_START; (i < GFW_START + GFW_SIZE) &&
415 (list_ent != &d->page_list); i += PAGE_SIZE) {
416 mfn = page_to_mfn(list_entry(list_ent, struct page_info, list));
417 assign_domain_page(d, i, mfn << PAGE_SHIFT);
418 list_ent = mfn_to_page(mfn)->list.next;
419 }
420 ASSERT(list_ent != &d->page_list);
422 /* Map for shared I/O page and xenstore */
423 mfn = page_to_mfn(list_entry(list_ent, struct page_info, list));
424 assign_domain_page(d, IO_PAGE_START, mfn << PAGE_SHIFT);
425 list_ent = mfn_to_page(mfn)->list.next;
426 ASSERT(list_ent != &d->page_list);
428 mfn = page_to_mfn(list_entry(list_ent, struct page_info, list));
429 assign_domain_page(d, STORE_PAGE_START, mfn << PAGE_SHIFT);
430 list_ent = mfn_to_page(mfn)->list.next;
431 ASSERT(list_ent != &d->page_list);
433 mfn = page_to_mfn(list_entry(list_ent, struct page_info, list));
434 assign_domain_page(d, BUFFER_IO_PAGE_START, mfn << PAGE_SHIFT);
435 list_ent = mfn_to_page(mfn)->list.next;
436 ASSERT(list_ent == &d->page_list);
437 }
439 void vmx_setup_platform(struct domain *d)
440 {
441 ASSERT(d != dom0); /* only for non-privileged vti domain */
443 vmx_build_physmap_table(d);
445 d->arch.vmx_platform.shared_page_va =
446 (unsigned long)__va(__gpa_to_mpa(d, IO_PAGE_START));
447 //For buffered IO requests.
448 spin_lock_init(&d->arch.hvm_domain.buffered_io_lock);
449 d->arch.hvm_domain.buffered_io_va =
450 (unsigned long)__va(__gpa_to_mpa(d, BUFFER_IO_PAGE_START));
451 /* TEMP */
452 d->arch.vmx_platform.pib_base = 0xfee00000UL;
454 d->arch.sal_data = xmalloc(struct xen_sal_data);
456 /* Only open one port for I/O and interrupt emulation */
457 memset(&d->shared_info->evtchn_mask[0], 0xff,
458 sizeof(d->shared_info->evtchn_mask));
460 /* initiate spinlock for pass virq */
461 spin_lock_init(&d->arch.arch_vmx.virq_assist_lock);
463 /* Initialize the virtual interrupt lines */
464 vmx_virq_line_init(d);
466 /* Initialize iosapic model within hypervisor */
467 hvm_vioapic_init(d);
468 }
470 void vmx_do_launch(struct vcpu *v)
471 {
472 vmx_load_all_rr(v);
473 }