ia64/xen-unstable

annotate tools/firmware/hvmloader/hvmloader.c @ 14373:1721f90e1422

AMD HVM: Exit hvmloader via a normal jump-to-realmode code sequence.
Strip out all SVM-special hypercall code.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Mon Mar 12 11:15:56 2007 +0000 (2007-03-12)
parents 480436ef6255
children f233456b89d5
rev   line source
kaf24@8708 1 /*
kaf24@8708 2 * hvmloader.c: HVM ROMBIOS/VGABIOS/ACPI/VMXAssist image loader.
kaf24@8708 3 *
kaf24@8708 4 * Leendert van Doorn, leendert@watson.ibm.com
kaf24@8708 5 * Copyright (c) 2005, International Business Machines Corporation.
kaf24@8708 6 *
kfraser@12554 7 * Copyright (c) 2006, Keir Fraser, XenSource Inc.
kfraser@12554 8 *
kaf24@8708 9 * This program is free software; you can redistribute it and/or modify it
kaf24@8708 10 * under the terms and conditions of the GNU General Public License,
kaf24@8708 11 * version 2, as published by the Free Software Foundation.
kaf24@8708 12 *
kaf24@8708 13 * This program is distributed in the hope it will be useful, but WITHOUT
kaf24@8708 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
kaf24@8708 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
kaf24@8708 16 * more details.
kaf24@8708 17 *
kaf24@8708 18 * You should have received a copy of the GNU General Public License along with
kaf24@8708 19 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
kaf24@8708 20 * Place - Suite 330, Boston, MA 02111-1307 USA.
kaf24@8708 21 */
kaf24@8708 22 #include "roms.h"
kfraser@12057 23 #include "acpi/acpi2_0.h" /* for ACPI_PHYSICAL_ADDRESS */
kfraser@10976 24 #include "hypercall.h"
kfraser@10976 25 #include "util.h"
kfraser@11115 26 #include "smbios.h"
kfraser@12548 27 #include "config.h"
kaf24@12571 28 #include "apic_regs.h"
kfraser@12554 29 #include "pci_regs.h"
kfraser@10976 30 #include <xen/version.h>
kfraser@11081 31 #include <xen/hvm/params.h>
kfraser@12554 32 #include <xen/hvm/e820.h>
kaf24@8708 33
kaf24@8708 34 /* memory map */
kfraser@12548 35 #define HYPERCALL_PHYSICAL_ADDRESS 0x00080000
kfraser@12548 36 #define VGABIOS_PHYSICAL_ADDRESS 0x000C0000
Tim@13140 37 #define ETHERBOOT_PHYSICAL_ADDRESS 0x000C8000
kfraser@12548 38 #define VMXASSIST_PHYSICAL_ADDRESS 0x000D0000
kfraser@12548 39 #define ROMBIOS_PHYSICAL_ADDRESS 0x000F0000
kaf24@8708 40
kaf24@8708 41 asm(
kfraser@12548 42 " .text \n"
kfraser@12548 43 " .globl _start \n"
kfraser@12548 44 "_start: \n"
kfraser@14373 45 /* C runtime kickoff. */
kfraser@12548 46 " cld \n"
kfraser@12548 47 " cli \n"
kfraser@14373 48 " movl $stack_top,%esp \n"
kfraser@14373 49 " movl %esp,%ebp \n"
kfraser@14373 50 " call main \n"
kfraser@14373 51 /* Relocate real-mode trampoline to 0x0. */
kfraser@14373 52 " mov $trampoline_start,%esi \n"
kfraser@14373 53 " xor %edi,%edi \n"
kfraser@14373 54 " mov $trampoline_end,%ecx \n"
kfraser@14373 55 " sub %esi,%ecx \n"
kfraser@14373 56 " rep movsb \n"
kfraser@14373 57 /* Load real-mode compatible segment state (base 0x0000, limit 0xffff). */
kfraser@12548 58 " lgdt gdt_desr \n"
kfraser@14373 59 " mov $0x0010,%ax \n"
kfraser@14373 60 " mov %ax,%ds \n"
kfraser@14373 61 " mov %ax,%es \n"
kfraser@14373 62 " mov %ax,%fs \n"
kfraser@14373 63 " mov %ax,%gs \n"
kfraser@14373 64 " mov %ax,%ss \n"
kfraser@14373 65 " ljmp $0x8,$0x0 \n"
kfraser@14373 66 /* Enter real mode, reload all segment registers and IDT. */
kfraser@14373 67 "trampoline_start: .code16 \n"
kfraser@14373 68 " mov %cr0,%eax \n"
kfraser@14373 69 " and $0xfe,%al \n"
kfraser@14373 70 " mov %eax,%cr0 \n"
kfraser@14373 71 " ljmp $0,$1f-trampoline_start\n"
kfraser@14373 72 "1: xor %ax,%ax \n"
kfraser@14373 73 " mov %ax,%ds \n"
kfraser@14373 74 " mov %ax,%es \n"
kfraser@14373 75 " mov %ax,%fs \n"
kfraser@14373 76 " mov %ax,%gs \n"
kfraser@14373 77 " mov %ax,%ss \n"
kfraser@14373 78 " lidt 1f-trampoline_start \n"
kfraser@14373 79 " ljmp $0xf000,$0xfff0 \n"
kfraser@14373 80 "1: .word 0x3ff,0,0 \n"
kfraser@14373 81 "trampoline_end: .code32 \n"
kfraser@12548 82 " \n"
kfraser@12548 83 "gdt_desr: \n"
kfraser@12548 84 " .word gdt_end - gdt - 1 \n"
kfraser@12548 85 " .long gdt \n"
kfraser@12548 86 " \n"
kfraser@12548 87 " .align 8 \n"
kfraser@12548 88 "gdt: \n"
kfraser@12548 89 " .quad 0x0000000000000000 \n"
kfraser@14373 90 " .quad 0x00009a000000ffff \n" /* Ring 0 code, base 0 limit 0xffff */
kfraser@14373 91 " .quad 0x000092000000ffff \n" /* Ring 0 data, base 0 limit 0xffff */
kfraser@12548 92 "gdt_end: \n"
kfraser@12548 93 " \n"
kfraser@12548 94 " .bss \n"
kfraser@12548 95 " .align 8 \n"
kfraser@12548 96 "stack: \n"
kfraser@12548 97 " .skip 0x4000 \n"
kfraser@12548 98 "stack_top: \n"
kfraser@12548 99 );
kaf24@8708 100
kaf24@10575 101 extern void create_mp_tables(void);
kaf24@8708 102
kfraser@10976 103 static int
kaf24@8708 104 cirrus_check(void)
kaf24@8708 105 {
kfraser@12548 106 outw(0x3C4, 0x9206);
kfraser@12548 107 return inb(0x3C5) == 0x12;
kaf24@8708 108 }
kaf24@8708 109
kfraser@10976 110 static int
kaf24@8708 111 check_amd(void)
kaf24@8708 112 {
kfraser@12548 113 char id[12];
kaf24@8708 114
kfraser@12548 115 __asm__ __volatile__ (
kfraser@12548 116 "cpuid"
kfraser@12548 117 : "=b" (*(int *)(&id[0])),
kfraser@12548 118 "=c" (*(int *)(&id[8])),
kfraser@12548 119 "=d" (*(int *)(&id[4]))
kfraser@12548 120 : "a" (0) );
kfraser@12548 121 return __builtin_memcmp(id, "AuthenticAMD", 12) == 0;
kaf24@8708 122 }
kaf24@8708 123
kfraser@10976 124 static void
kfraser@10976 125 wrmsr(uint32_t idx, uint64_t v)
kfraser@10976 126 {
kfraser@12548 127 __asm__ __volatile__ (
kfraser@12548 128 "wrmsr"
kfraser@12548 129 : : "c" (idx), "a" ((uint32_t)v), "d" ((uint32_t)(v>>32)) );
kfraser@10976 130 }
kfraser@10976 131
kfraser@10976 132 static void
kfraser@10976 133 init_hypercalls(void)
kfraser@10976 134 {
kfraser@12548 135 uint32_t eax, ebx, ecx, edx;
kfraser@12548 136 unsigned long i;
kfraser@12554 137 char signature[13];
kfraser@12548 138 xen_extraversion_t extraversion;
kfraser@10976 139
kfraser@12548 140 cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
kfraser@10976 141
kfraser@12548 142 *(uint32_t *)(signature + 0) = ebx;
kfraser@12548 143 *(uint32_t *)(signature + 4) = ecx;
kfraser@12548 144 *(uint32_t *)(signature + 8) = edx;
kfraser@12548 145 signature[12] = '\0';
kfraser@10976 146
kfraser@12548 147 if ( strcmp("XenVMMXenVMM", signature) || (eax < 0x40000002) )
kfraser@12548 148 {
kfraser@12554 149 printf("FATAL: Xen hypervisor not detected\n");
kfraser@12548 150 __asm__ __volatile__( "ud2" );
kfraser@12548 151 }
kfraser@10976 152
kfraser@12554 153 /* Fill in hypercall transfer pages. */
kfraser@12548 154 cpuid(0x40000002, &eax, &ebx, &ecx, &edx);
kfraser@12548 155 for ( i = 0; i < eax; i++ )
kfraser@12548 156 wrmsr(ebx, HYPERCALL_PHYSICAL_ADDRESS + (i << 12) + i);
kfraser@12548 157
kfraser@12554 158 /* Print version information. */
kfraser@12554 159 cpuid(0x40000001, &eax, &ebx, &ecx, &edx);
kfraser@12548 160 hypercall_xen_version(XENVER_extraversion, extraversion);
kfraser@12554 161 printf("Detected Xen v%u.%u%s\n", eax >> 16, eax & 0xffff, extraversion);
kfraser@10976 162 }
kfraser@10976 163
kfraser@12548 164 static void apic_setup(void)
kaf24@8708 165 {
kaf24@12571 166 /* Set the IOAPIC ID to tha static value used in the MP/ACPI tables. */
kaf24@12571 167 ioapic_write(0x00, IOAPIC_ID);
kaf24@8708 168
kaf24@12571 169 /* Set up Virtual Wire mode. */
kaf24@12571 170 lapic_write(APIC_SPIV, APIC_SPIV_APIC_ENABLED | 0xFF);
kaf24@12571 171 lapic_write(APIC_LVT0, APIC_MODE_EXTINT << 8);
kaf24@12571 172 lapic_write(APIC_LVT1, APIC_MODE_NMI << 8);
kaf24@8708 173 }
kaf24@8708 174
kfraser@12554 175 static void pci_setup(void)
kfraser@12554 176 {
kfraser@12554 177 uint32_t devfn, bar_reg, bar_data, bar_sz, cmd;
kfraser@12554 178 uint32_t *base, io_base = 0xc000, mem_base = HVM_BELOW_4G_MMIO_START;
kfraser@12554 179 uint16_t class, vendor_id, device_id;
kfraser@12554 180 unsigned int bar, pin, link, isa_irq;
kfraser@12554 181
kfraser@12554 182 /* Program PCI-ISA bridge with appropriate link routes. */
kfraser@12554 183 link = 0;
kfraser@12554 184 for ( isa_irq = 0; isa_irq < 15; isa_irq++ )
kfraser@12554 185 {
kfraser@12554 186 if ( !(PCI_ISA_IRQ_MASK & (1U << isa_irq)) )
kfraser@12554 187 continue;
kfraser@12554 188 pci_writeb(PCI_ISA_DEVFN, 0x60 + link, isa_irq);
kfraser@12554 189 printf("PCI-ISA link %u routed to IRQ%u\n", link, isa_irq);
kfraser@12554 190 if ( link++ == 4 )
kfraser@12554 191 break;
kfraser@12554 192 }
kfraser@12554 193
kfraser@12554 194 /* Program ELCR to match PCI-wired IRQs. */
kfraser@12554 195 outb(0x4d0, (uint8_t)(PCI_ISA_IRQ_MASK >> 0));
kfraser@12554 196 outb(0x4d1, (uint8_t)(PCI_ISA_IRQ_MASK >> 8));
kfraser@12554 197
kfraser@12554 198 /* Scan the PCI bus and map resources. */
kfraser@12554 199 for ( devfn = 0; devfn < 128; devfn++ )
kfraser@12554 200 {
kfraser@12554 201 class = pci_readw(devfn, PCI_CLASS_DEVICE);
kfraser@12554 202 vendor_id = pci_readw(devfn, PCI_VENDOR_ID);
kfraser@12554 203 device_id = pci_readw(devfn, PCI_DEVICE_ID);
kfraser@12554 204 if ( (vendor_id == 0xffff) && (device_id == 0xffff) )
kfraser@12554 205 continue;
kfraser@12554 206
kfraser@12554 207 ASSERT((devfn != PCI_ISA_DEVFN) ||
kfraser@12554 208 ((vendor_id == 0x8086) && (device_id == 0x7000)));
kfraser@12554 209
kfraser@12554 210 switch ( class )
kfraser@12554 211 {
kfraser@12554 212 case 0x0680:
kfraser@12554 213 ASSERT((vendor_id == 0x8086) && (device_id == 0x7113));
kfraser@12554 214 /*
kfraser@12554 215 * PIIX4 ACPI PM. Special device with special PCI config space.
kfraser@12554 216 * No ordinary BARs.
kfraser@12554 217 */
kfraser@12554 218 pci_writew(devfn, 0x20, 0x0000); /* No smb bus IO enable */
kfraser@12554 219 pci_writew(devfn, 0x22, 0x0000);
kfraser@12554 220 pci_writew(devfn, 0x3c, 0x0009); /* Hardcoded IRQ9 */
kfraser@12554 221 pci_writew(devfn, 0x3d, 0x0001);
kfraser@12554 222 break;
kfraser@12554 223 case 0x0101:
kfraser@12554 224 /* PIIX3 IDE */
kfraser@12554 225 ASSERT((vendor_id == 0x8086) && (device_id == 0x7010));
kfraser@12554 226 pci_writew(devfn, 0x40, 0x8000); /* enable IDE0 */
kfraser@12554 227 pci_writew(devfn, 0x42, 0x8000); /* enable IDE1 */
kfraser@12554 228 /* fall through */
kfraser@12554 229 default:
kfraser@12554 230 /* Default memory mappings. */
kfraser@12554 231 for ( bar = 0; bar < 7; bar++ )
kfraser@12554 232 {
kfraser@12554 233 bar_reg = PCI_BASE_ADDRESS_0 + 4*bar;
kfraser@12554 234 if ( bar == 6 )
kfraser@12554 235 bar_reg = PCI_ROM_ADDRESS;
kfraser@12554 236
kfraser@12554 237 bar_data = pci_readl(devfn, bar_reg);
kfraser@12554 238
kfraser@12554 239 pci_writel(devfn, bar_reg, ~0);
kfraser@12554 240 bar_sz = pci_readl(devfn, bar_reg);
kfraser@12554 241 if ( bar_sz == 0 )
kfraser@12554 242 continue;
kfraser@12554 243
kfraser@12554 244 if ( (bar_data & PCI_BASE_ADDRESS_SPACE) ==
kfraser@12554 245 PCI_BASE_ADDRESS_SPACE_MEMORY )
kfraser@12554 246 {
kfraser@12554 247 base = &mem_base;
kfraser@12554 248 bar_sz &= PCI_BASE_ADDRESS_MEM_MASK;
kfraser@12554 249 bar_data &= ~PCI_BASE_ADDRESS_MEM_MASK;
kfraser@12554 250 }
kfraser@12554 251 else
kfraser@12554 252 {
kfraser@12554 253 base = &io_base;
kfraser@12554 254 bar_sz &= PCI_BASE_ADDRESS_IO_MASK & 0xffff;
kfraser@12554 255 bar_data &= ~PCI_BASE_ADDRESS_IO_MASK;
kfraser@12554 256 }
kfraser@12554 257 bar_sz &= ~(bar_sz - 1);
kfraser@12554 258
kfraser@12554 259 *base = (*base + bar_sz - 1) & ~(bar_sz - 1);
kfraser@12554 260 bar_data |= *base;
kfraser@12554 261 *base += bar_sz;
kfraser@12554 262
kfraser@12554 263 pci_writel(devfn, bar_reg, bar_data);
kfraser@12554 264 printf("pci dev %02x:%x bar %02x size %08x: %08x\n",
kfraser@12554 265 devfn>>3, devfn&7, bar_reg, bar_sz, bar_data);
kfraser@12554 266
kfraser@12554 267 /* Now enable the memory or I/O mapping. */
kfraser@12554 268 cmd = pci_readw(devfn, PCI_COMMAND);
kfraser@12554 269 if ( (bar_reg == PCI_ROM_ADDRESS) ||
kfraser@12554 270 ((bar_data & PCI_BASE_ADDRESS_SPACE) ==
kfraser@12554 271 PCI_BASE_ADDRESS_SPACE_MEMORY) )
kfraser@12554 272 cmd |= PCI_COMMAND_MEMORY;
kfraser@12554 273 else
kfraser@12554 274 cmd |= PCI_COMMAND_IO;
kfraser@12554 275 pci_writew(devfn, PCI_COMMAND, cmd);
kfraser@12554 276 }
kfraser@12554 277 break;
kfraser@12554 278 }
kfraser@12554 279
kfraser@12554 280 /* Map the interrupt. */
kfraser@12554 281 pin = pci_readb(devfn, PCI_INTERRUPT_PIN);
kfraser@12554 282 if ( pin != 0 )
kfraser@12554 283 {
kfraser@12554 284 /* This is the barber's pole mapping used by Xen. */
kfraser@12554 285 link = ((pin - 1) + (devfn >> 3)) & 3;
kfraser@12554 286 isa_irq = pci_readb(PCI_ISA_DEVFN, 0x60 + link);
kfraser@12554 287 pci_writeb(devfn, PCI_INTERRUPT_LINE, isa_irq);
kfraser@12554 288 printf("pci dev %02x:%x INT%c->IRQ%u\n",
kfraser@12554 289 devfn>>3, devfn&7, 'A'+pin-1, isa_irq);
kfraser@12554 290 }
kfraser@12554 291 }
kfraser@12554 292 }
kfraser@12554 293
Tim@13140 294 static
Tim@13140 295 int must_load_nic(void)
Tim@13140 296 {
Tim@13140 297 /* If the network card is in the boot order, load the Etherboot
Tim@13140 298 * option ROM. Read the boot order bytes from CMOS and check
Tim@13140 299 * if any of them are 0x4. */
Tim@13140 300 uint8_t boot_order;
Tim@13140 301
Tim@13140 302 /* Read CMOS register 0x3d (boot choices 0 and 1) */
Tim@13140 303 outb(0x70, 0x3d);
Tim@13140 304 boot_order = inb(0x71);
Tim@13140 305 if ( (boot_order & 0xf) == 0x4 || (boot_order & 0xf0) == 0x40 )
Tim@13140 306 return 1;
Tim@13140 307 /* Read CMOS register 0x38 (boot choice 2 and FDD test flag) */
Tim@13140 308 outb(0x70, 0x38);
Tim@13140 309 boot_order = inb(0x71);
Tim@13140 310 if ( (boot_order & 0xf0) == 0x40 )
Tim@13140 311 return 1;
Tim@13140 312 return 0;
Tim@13140 313 }
Tim@13140 314
kfraser@12548 315 int main(void)
kfraser@12548 316 {
kaf24@12574 317 int acpi_sz;
kaf24@12574 318
kfraser@12554 319 printf("HVM Loader\n");
kfraser@12548 320
kfraser@12548 321 init_hypercalls();
kfraser@12548 322
kfraser@12554 323 printf("Writing SMBIOS tables ...\n");
kfraser@12548 324 hvm_write_smbios_tables();
kfraser@12548 325
kfraser@12554 326 printf("Loading ROMBIOS ...\n");
kfraser@12548 327 memcpy((void *)ROMBIOS_PHYSICAL_ADDRESS, rombios, sizeof(rombios));
kaf24@13656 328 highbios_setup();
kfraser@12548 329
kfraser@12548 330 apic_setup();
kfraser@12554 331 pci_setup();
kaf24@12574 332
kfraser@12600 333 if ( (get_vcpu_nr() > 1) || get_apic_mode() )
kaf24@12574 334 create_mp_tables();
kfraser@12548 335
kfraser@12548 336 if ( cirrus_check() )
kfraser@12548 337 {
kfraser@12554 338 printf("Loading Cirrus VGABIOS ...\n");
kfraser@12548 339 memcpy((void *)VGABIOS_PHYSICAL_ADDRESS,
kfraser@12548 340 vgabios_cirrusvga, sizeof(vgabios_cirrusvga));
kfraser@12548 341 }
kfraser@12548 342 else
kfraser@12548 343 {
kfraser@12554 344 printf("Loading Standard VGABIOS ...\n");
kfraser@12548 345 memcpy((void *)VGABIOS_PHYSICAL_ADDRESS,
kfraser@12548 346 vgabios_stdvga, sizeof(vgabios_stdvga));
kfraser@12548 347 }
kfraser@12548 348
Tim@13140 349 if ( must_load_nic() )
Tim@13140 350 {
Tim@13140 351 printf("Loading ETHERBOOT ...\n");
Tim@13140 352 memcpy((void *)ETHERBOOT_PHYSICAL_ADDRESS,
Tim@13140 353 etherboot, sizeof(etherboot));
Tim@13140 354 }
Tim@13140 355
kfraser@12548 356 if ( get_acpi_enabled() != 0 )
kfraser@12548 357 {
kfraser@12554 358 printf("Loading ACPI ...\n");
kaf24@12574 359 acpi_sz = acpi_build_tables((uint8_t *)ACPI_PHYSICAL_ADDRESS);
kfraser@12634 360 ASSERT((ACPI_PHYSICAL_ADDRESS + acpi_sz) <= 0xF0000);
kfraser@12548 361 }
kfraser@12548 362
kfraser@14373 363 if ( !check_amd() )
kfraser@12548 364 {
kfraser@12554 365 printf("Loading VMXAssist ...\n");
kfraser@12548 366 memcpy((void *)VMXASSIST_PHYSICAL_ADDRESS,
kfraser@12548 367 vmxassist, sizeof(vmxassist));
kfraser@12548 368
kfraser@12554 369 printf("VMX go ...\n");
kfraser@12548 370 __asm__ __volatile__(
kfraser@12548 371 "jmp *%%eax"
kfraser@12548 372 : : "a" (VMXASSIST_PHYSICAL_ADDRESS), "d" (0)
kfraser@12548 373 );
kfraser@12548 374 }
kfraser@12548 375
kfraser@14373 376 printf("Invoking ROMBIOS ...\n");
kfraser@12548 377 return 0;
kfraser@12548 378 }
kfraser@12548 379
kfraser@12548 380 /*
kfraser@12548 381 * Local variables:
kfraser@12548 382 * mode: C
kfraser@12548 383 * c-set-style: "BSD"
kfraser@12548 384 * c-basic-offset: 4
kfraser@12548 385 * tab-width: 4
kfraser@12548 386 * indent-tabs-mode: nil
kfraser@12548 387 * End:
kfraser@12548 388 */