1 /*
2 * hvmloader.c: HVM bootloader.
3 *
4 * Leendert van Doorn, leendert@watson.ibm.com
5 * Copyright (c) 2005, International Business Machines Corporation.
6 *
7 * Copyright (c) 2006, Keir Fraser, XenSource Inc.
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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "util.h"
23 #include "hypercall.h"
24 #include "config.h"
25 #include "pci_regs.h"
26 #include "apic_regs.h"
27 #include "vnuma.h"
28 #include <acpi2_0.h>
29 #include <xen/version.h>
30 #include <xen/hvm/params.h>
31 #include <xen/arch-x86/hvm/start_info.h>
32
33 const struct hvm_start_info *hvm_start_info;
34
35 asm (
36 " .text \n"
37 " .globl _start \n"
38 "_start: \n"
39 /* C runtime kickoff. */
40 " cld \n"
41 " cli \n"
42 " lgdt gdt_desr \n"
43 " mov $"STR(SEL_DATA32)",%ax \n"
44 " mov %ax,%ds \n"
45 " mov %ax,%es \n"
46 " mov %ax,%fs \n"
47 " mov %ax,%gs \n"
48 " mov %ax,%ss \n"
49 " ljmp $"STR(SEL_CODE32)",$1f \n"
50 "1: movl $stack_top,%esp \n"
51 " movl %esp,%ebp \n"
52 /* store HVM start info ptr */
53 " mov %ebx, hvm_start_info \n"
54 " call main \n"
55 /* Relocate real-mode trampoline to 0x0. */
56 " mov $trampoline_start,%esi \n"
57 " xor %edi,%edi \n"
58 " mov $trampoline_end,%ecx \n"
59 " sub %esi,%ecx \n"
60 " rep movsb \n"
61 /* Load real-mode compatible segment state (base 0x0000, limit 0xffff). */
62 " mov $"STR(SEL_DATA16)",%ax \n"
63 " mov %ax,%ds \n"
64 " mov %ax,%es \n"
65 " mov %ax,%fs \n"
66 " mov %ax,%gs \n"
67 " mov %ax,%ss \n"
68 /* Initialise all 32-bit GPRs to zero. */
69 " xor %eax,%eax \n"
70 " xor %ebx,%ebx \n"
71 " xor %ecx,%ecx \n"
72 " xor %edx,%edx \n"
73 " xor %esp,%esp \n"
74 " xor %ebp,%ebp \n"
75 " xor %esi,%esi \n"
76 " xor %edi,%edi \n"
77 /* Enter real mode, reload all segment registers and IDT. */
78 " ljmp $"STR(SEL_CODE16)",$0x0\n"
79 "trampoline_start: .code16 \n"
80 " mov %eax,%cr0 \n"
81 " ljmp $0,$1f-trampoline_start\n"
82 "1: mov %ax,%ds \n"
83 " mov %ax,%es \n"
84 " mov %ax,%fs \n"
85 " mov %ax,%gs \n"
86 " mov %ax,%ss \n"
87 " lidt 1f-trampoline_start \n"
88 " ljmp $0xf000,$0xfff0 \n"
89 "1: .word 0x3ff,0,0 \n"
90 "trampoline_end: .code32 \n"
91 " \n"
92 "gdt_desr: \n"
93 " .word gdt_end - gdt - 1 \n"
94 " .long gdt \n"
95 " \n"
96 " .align 8 \n"
97 "gdt: \n"
98 " .quad 0x0000000000000000 \n"
99 " .quad 0x008f9a000000ffff \n" /* Ring 0 16b code, base 0 limit 4G */
100 " .quad 0x008f92000000ffff \n" /* Ring 0 16b data, base 0 limit 4G */
101 " .quad 0x00cf9a000000ffff \n" /* Ring 0 32b code, base 0 limit 4G */
102 " .quad 0x00cf92000000ffff \n" /* Ring 0 32b data, base 0 limit 4G */
103 " .quad 0x00af9a000000ffff \n" /* Ring 0 64b code */
104 "gdt_end: \n"
105 " \n"
106 " .bss \n"
107 " .align 8 \n"
108 "stack: \n"
109 " .skip 0x4000 \n"
110 "stack_top: \n"
111 " .text \n"
112 );
113
114 unsigned long scratch_start = SCRATCH_PHYSICAL_ADDRESS;
115
116 uint32_t ioapic_base_address = 0xfec00000;
117 uint8_t ioapic_version;
118
init_hypercalls(void)119 static void init_hypercalls(void)
120 {
121 uint32_t eax, ebx, ecx, edx;
122 unsigned long i;
123 char signature[13];
124 xen_extraversion_t extraversion;
125 uint32_t base;
126
127 for ( base = 0x40000000; base < 0x40010000; base += 0x100 )
128 {
129 cpuid(base, &eax, &ebx, &ecx, &edx);
130
131 *(uint32_t *)(signature + 0) = ebx;
132 *(uint32_t *)(signature + 4) = ecx;
133 *(uint32_t *)(signature + 8) = edx;
134 signature[12] = '\0';
135
136 if ( !strcmp("XenVMMXenVMM", signature) )
137 break;
138 }
139
140 BUG_ON(strcmp("XenVMMXenVMM", signature) || ((eax - base) < 2));
141
142 /* Fill in hypercall transfer pages. */
143 cpuid(base + 2, &eax, &ebx, &ecx, &edx);
144 for ( i = 0; i < eax; i++ )
145 wrmsr(ebx, HYPERCALL_PHYSICAL_ADDRESS + (i << 12) + i);
146
147 /* Print version information. */
148 cpuid(base + 1, &eax, &ebx, &ecx, &edx);
149 hypercall_xen_version(XENVER_extraversion, extraversion);
150 printf("Detected Xen v%u.%u%s\n", eax >> 16, eax & 0xffff, extraversion);
151 }
152
153 /* Replace possibly erroneous memory-size CMOS fields with correct values. */
cmos_write_memory_size(void)154 static void cmos_write_memory_size(void)
155 {
156 uint32_t base_mem = 640, ext_mem, alt_mem;
157
158 alt_mem = ext_mem = hvm_info->low_mem_pgend << PAGE_SHIFT;
159 ext_mem = (ext_mem > 0x0100000) ? (ext_mem - 0x0100000) >> 10 : 0;
160 if ( ext_mem > 0xffff )
161 ext_mem = 0xffff;
162 alt_mem = (alt_mem > 0x1000000) ? (alt_mem - 0x1000000) >> 16 : 0;
163
164 /* All BIOSes: conventional memory (CMOS *always* reports 640kB). */
165 cmos_outb(0x15, (uint8_t)(base_mem >> 0));
166 cmos_outb(0x16, (uint8_t)(base_mem >> 8));
167
168 /* All BIOSes: extended memory (1kB chunks above 1MB). */
169 cmos_outb(0x17, (uint8_t)( ext_mem >> 0));
170 cmos_outb(0x18, (uint8_t)( ext_mem >> 8));
171 cmos_outb(0x30, (uint8_t)( ext_mem >> 0));
172 cmos_outb(0x31, (uint8_t)( ext_mem >> 8));
173
174 /* Some BIOSes: alternative extended memory (64kB chunks above 16MB). */
175 cmos_outb(0x34, (uint8_t)( alt_mem >> 0));
176 cmos_outb(0x35, (uint8_t)( alt_mem >> 8));
177 }
178
179 /*
180 * Set up an empty TSS area for virtual 8086 mode to use. Its content is
181 * going to be managed by Xen, but zero fill it just in case.
182 */
init_vm86_tss(void)183 static void init_vm86_tss(void)
184 {
185 /*
186 * Have the TSS cover the ISA port range, which makes it
187 * - 104 bytes base structure
188 * - 32 bytes interrupt redirection bitmap
189 * - 128 bytes I/O bitmap
190 * - one trailing byte
191 * or a total of to 265 bytes. As it needs to be a multiple of the requested
192 * alignment, this ends up requiring 384 bytes.
193 */
194 #define TSS_SIZE (3 * 128)
195 void *tss;
196
197 tss = mem_alloc(TSS_SIZE, 128);
198 memset(tss, 0, TSS_SIZE);
199 hvm_param_set(HVM_PARAM_VM86_TSS_SIZED,
200 ((uint64_t)TSS_SIZE << 32) | virt_to_phys(tss));
201 printf("vm86 TSS at %08lx\n", virt_to_phys(tss));
202 #undef TSS_SIZE
203 }
204
apic_setup(void)205 static void apic_setup(void)
206 {
207 /*
208 * This would the The Right Thing To Do (tm), if only qemu negotiated
209 * with Xen where the IO-APIC actually sits (which is currently hard
210 * coded in Xen and can't be controlled externally). Uncomment this code
211 * once that changed.
212 ioapic_base_address |= (pci_readb(PCI_ISA_DEVFN, 0x80) & 0x3f) << 10;
213 */
214 ioapic_version = ioapic_read(0x01) & 0xff;
215
216 /* Set the IOAPIC ID to the static value used in the MP/ACPI tables. */
217 ioapic_write(0x00, IOAPIC_ID);
218
219 /* NMIs are delivered direct to the BSP. */
220 lapic_write(APIC_SPIV, APIC_SPIV_APIC_ENABLED | 0xFF);
221 lapic_write(APIC_LVT0, (APIC_MODE_EXTINT << 8) | APIC_LVT_MASKED);
222 lapic_write(APIC_LVT1, APIC_MODE_NMI << 8);
223
224 /* 8259A ExtInts are delivered through IOAPIC pin 0 (Virtual Wire Mode). */
225 ioapic_write(0x10, APIC_DM_EXTINT);
226 ioapic_write(0x11, SET_APIC_ID(LAPIC_ID(0)));
227 }
228
229 struct bios_info {
230 const char *key;
231 const struct bios_config *bios;
232 } bios_configs[] = {
233 #ifdef ENABLE_ROMBIOS
234 { "rombios", &rombios_config, },
235 #endif
236 { "seabios", &seabios_config, },
237 { "ovmf", &ovmf_config, },
238 { NULL, NULL }
239 };
240
detect_bios(void)241 static const struct bios_config *detect_bios(void)
242 {
243 const struct bios_info *b;
244 const char *bios;
245
246 bios = xenstore_read("hvmloader/bios", "rombios");
247
248 for ( b = &bios_configs[0]; b->key != NULL; b++ )
249 if ( !strcmp(bios, b->key) )
250 return b->bios;
251
252 printf("Unknown BIOS %s, no ROM image found\n", bios);
253 BUG();
254 return NULL;
255 }
256
acpi_enable_sci(void)257 static void acpi_enable_sci(void)
258 {
259 uint8_t pm1a_cnt_val;
260
261 #define PIIX4_SMI_CMD_IOPORT 0xb2
262 #define PIIX4_ACPI_ENABLE 0xf1
263
264 /*
265 * PIIX4 emulation in QEMU has SCI_EN=0 by default. We have no legacy
266 * SMM implementation, so give ACPI control to the OSPM immediately.
267 */
268 pm1a_cnt_val = inb(ACPI_PM1A_CNT_BLK_ADDRESS_V1);
269 if ( !(pm1a_cnt_val & ACPI_PM1C_SCI_EN) )
270 outb(PIIX4_SMI_CMD_IOPORT, PIIX4_ACPI_ENABLE);
271
272 pm1a_cnt_val = inb(ACPI_PM1A_CNT_BLK_ADDRESS_V1);
273 BUG_ON(!(pm1a_cnt_val & ACPI_PM1C_SCI_EN));
274 }
275
get_module_entry(const struct hvm_start_info * info,const char * name)276 const struct hvm_modlist_entry *get_module_entry(
277 const struct hvm_start_info *info,
278 const char *name)
279 {
280 const struct hvm_modlist_entry *modlist =
281 (struct hvm_modlist_entry *)(uintptr_t)info->modlist_paddr;
282 unsigned int i;
283
284 if ( !modlist ||
285 info->modlist_paddr > UINTPTR_MAX ||
286 (UINTPTR_MAX - (uintptr_t)info->modlist_paddr) / sizeof(*modlist)
287 < info->nr_modules )
288 return NULL;
289
290 for ( i = 0; i < info->nr_modules; i++ )
291 {
292 char *module_name = (char*)(uintptr_t)modlist[i].cmdline_paddr;
293
294 /* Skip if the module or its cmdline is missing. */
295 if ( !module_name || !modlist[i].paddr )
296 continue;
297
298 /* Skip if the cmdline cannot be read. */
299 if ( modlist[i].cmdline_paddr > UINTPTR_MAX ||
300 (modlist[i].cmdline_paddr + strlen(name)) > UINTPTR_MAX )
301 continue;
302
303 if ( !strcmp(name, module_name) )
304 {
305 if ( modlist[i].paddr > UINTPTR_MAX ||
306 modlist[i].size > UINTPTR_MAX ||
307 (modlist[i].paddr + modlist[i].size - 1) > UINTPTR_MAX )
308 {
309 printf("Cannot load \"%s\" from 0x"PRIllx" (0x"PRIllx")\n",
310 name, PRIllx_arg(modlist[i].paddr),
311 PRIllx_arg(modlist[i].size));
312 BUG();
313 }
314 return &modlist[i];
315 }
316 }
317
318 return NULL;
319 }
320
main(void)321 int main(void)
322 {
323 const struct bios_config *bios;
324 int acpi_enabled;
325 const struct hvm_modlist_entry *bios_module;
326
327 /* Initialise hypercall stubs with RET, rendering them no-ops. */
328 memset((void *)HYPERCALL_PHYSICAL_ADDRESS, 0xc3 /* RET */, PAGE_SIZE);
329
330 printf("HVM Loader\n");
331 BUG_ON(hvm_start_info->magic != XEN_HVM_START_MAGIC_VALUE);
332
333 init_hypercalls();
334
335 memory_map_setup();
336
337 xenbus_setup();
338
339 bios = detect_bios();
340 printf("System requested %s\n", bios->name);
341
342 printf("CPU speed is %u MHz\n", get_cpu_mhz());
343
344 apic_setup();
345 pci_setup();
346
347 smp_initialise();
348
349 perform_tests();
350
351 if ( bios->bios_info_setup )
352 bios->bios_info_setup();
353
354 if ( bios->create_smbios_tables )
355 {
356 printf("Writing SMBIOS tables ...\n");
357 bios->create_smbios_tables();
358 }
359
360 printf("Loading %s ...\n", bios->name);
361 bios_module = get_module_entry(hvm_start_info, "firmware");
362 if ( bios_module )
363 {
364 uint32_t paddr = bios_module->paddr;
365
366 bios->bios_load(bios, (void*)paddr, bios_module->size);
367 }
368 #ifdef ENABLE_ROMBIOS
369 else if ( bios == &rombios_config )
370 {
371 bios->bios_load(bios, NULL, 0);
372 }
373 #endif
374 else
375 {
376 /*
377 * If there is no BIOS module supplied and if there is no embeded BIOS
378 * image, then we failed. Only rombios might have an embedded bios blob.
379 */
380 printf("no BIOS ROM image found\n");
381 BUG();
382 }
383
384 if ( (hvm_info->nr_vcpus > 1) || hvm_info->apic_mode )
385 {
386 if ( bios->create_mp_tables )
387 bios->create_mp_tables();
388 if ( bios->create_pir_tables )
389 bios->create_pir_tables();
390 }
391
392 if ( bios->load_roms )
393 bios->load_roms();
394
395 acpi_enabled = !strncmp(xenstore_read("platform/acpi", "1"), "1", 1);
396
397 if ( acpi_enabled )
398 {
399 init_vnuma_info();
400
401 if ( bios->acpi_build_tables )
402 {
403 printf("Loading ACPI ...\n");
404 bios->acpi_build_tables();
405 }
406
407 acpi_enable_sci();
408
409 hvm_param_set(HVM_PARAM_ACPI_IOPORTS_LOCATION, 1);
410 }
411
412 init_vm86_tss();
413
414 cmos_write_memory_size();
415
416 printf("BIOS map:\n");
417 if ( SCRATCH_PHYSICAL_ADDRESS != scratch_start )
418 printf(" %05x-%05lx: Scratch space\n",
419 SCRATCH_PHYSICAL_ADDRESS, scratch_start);
420 printf(" %05x-%05x: Main BIOS\n",
421 bios->bios_address,
422 bios->bios_address + bios->image_size - 1);
423
424 if ( bios->e820_setup )
425 bios->e820_setup();
426
427 if ( bios->bios_info_finish )
428 bios->bios_info_finish();
429
430 xenbus_shutdown();
431
432 printf("Invoking %s ...\n", bios->name);
433 return 0;
434 }
435
436 /*
437 * Local variables:
438 * mode: C
439 * c-file-style: "BSD"
440 * c-basic-offset: 4
441 * tab-width: 4
442 * indent-tabs-mode: nil
443 * End:
444 */
445