ia64/xen-unstable

annotate tools/firmware/hvmloader/hvmloader.c @ 11115:0b176f84c3bf

Add code to generate SMBIOS tables to hvmloader.
Signed-off-by: Andrew D. Ball <aball@us.ibm.com>
author kfraser@localhost.localdomain
date Mon Aug 14 17:37:07 2006 +0100 (2006-08-14)
parents 4210049a5854
children f426f6e646eb
rev   line source
kaf24@8708 1 /*
kaf24@8708 2 * hvmloader.c: HVM ROMBIOS/VGABIOS/ACPI/VMXAssist image loader.
kaf24@8708 3 *
kaf24@8708 4 * A quicky so that we can boot rom images as if they were a Linux kernel.
kaf24@8708 5 * This code will copy the rom images (ROMBIOS/VGABIOS/VM86) into their
kaf24@8708 6 * respective spaces and transfer control to VM86 to execute the BIOSes.
kaf24@8708 7 *
kaf24@8708 8 * Leendert van Doorn, leendert@watson.ibm.com
kaf24@8708 9 * Copyright (c) 2005, International Business Machines Corporation.
kaf24@8708 10 *
kaf24@8708 11 * This program is free software; you can redistribute it and/or modify it
kaf24@8708 12 * under the terms and conditions of the GNU General Public License,
kaf24@8708 13 * version 2, as published by the Free Software Foundation.
kaf24@8708 14 *
kaf24@8708 15 * This program is distributed in the hope it will be useful, but WITHOUT
kaf24@8708 16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
kaf24@8708 17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
kaf24@8708 18 * more details.
kaf24@8708 19 *
kaf24@8708 20 * You should have received a copy of the GNU General Public License along with
kaf24@8708 21 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
kaf24@8708 22 * Place - Suite 330, Boston, MA 02111-1307 USA.
kaf24@8708 23 */
kaf24@8708 24 #include "roms.h"
kaf24@8708 25 #include "../acpi/acpi2_0.h" /* for ACPI_PHYSICAL_ADDRESS */
kfraser@10976 26 #include "hypercall.h"
kfraser@10976 27 #include "util.h"
kfraser@11115 28 #include "smbios.h"
kfraser@10976 29 #include <xen/version.h>
kfraser@11081 30 #include <xen/hvm/params.h>
kaf24@8708 31
kaf24@8708 32 /* memory map */
kfraser@10976 33 #define HYPERCALL_PHYSICAL_ADDRESS 0x00080000
kaf24@8708 34 #define VGABIOS_PHYSICAL_ADDRESS 0x000C0000
kaf24@8708 35 #define VMXASSIST_PHYSICAL_ADDRESS 0x000D0000
kaf24@8708 36 #define ROMBIOS_PHYSICAL_ADDRESS 0x000F0000
kaf24@8708 37
kaf24@8708 38 /* invoke SVM's paged realmode support */
kfraser@10893 39 #define SVM_VMMCALL_RESET_TO_REALMODE 0x80000001
kaf24@8708 40
kaf24@8708 41 /*
kaf24@8708 42 * C runtime start off
kaf24@8708 43 */
kaf24@8708 44 asm(
kaf24@8708 45 " .text \n"
kaf24@8708 46 " .globl _start \n"
kaf24@8708 47 "_start: \n"
kaf24@8708 48 " cld \n"
kaf24@8708 49 " cli \n"
kaf24@8708 50 " lgdt gdt_desr \n"
kaf24@8708 51 " movl $stack_top, %esp \n"
kaf24@8708 52 " movl %esp, %ebp \n"
kaf24@8708 53 " call main \n"
kaf24@8708 54 " jmp halt \n"
kaf24@8708 55 " \n"
kaf24@8708 56 "gdt_desr: \n"
kaf24@8708 57 " .word gdt_end - gdt - 1 \n"
kaf24@8708 58 " .long gdt \n"
kaf24@8708 59 " \n"
kaf24@8708 60 " .align 8 \n"
kaf24@8708 61 "gdt: \n"
kaf24@8708 62 " .quad 0x0000000000000000 \n"
kaf24@8708 63 " .quad 0x00CF92000000FFFF \n"
kaf24@8708 64 " .quad 0x00CF9A000000FFFF \n"
kaf24@8708 65 "gdt_end: \n"
kaf24@8708 66 " \n"
kaf24@8708 67 "halt: \n"
kaf24@8708 68 " sti \n"
kaf24@8708 69 " jmp . \n"
kaf24@8708 70 " \n"
kaf24@8708 71 " .bss \n"
kaf24@8708 72 " .align 8 \n"
kaf24@8708 73 "stack: \n"
kaf24@8708 74 " .skip 0x4000 \n"
kaf24@8708 75 "stack_top: \n"
kaf24@8708 76 );
kaf24@8708 77
kaf24@8708 78 extern int get_acpi_enabled(void);
kaf24@8708 79 extern int acpi_madt_update(unsigned char* acpi_start);
kaf24@10575 80 extern void create_mp_tables(void);
kaf24@10575 81 struct hvm_info_table *get_hvm_info_table(void);
kaf24@8708 82
kfraser@10976 83 static int
kaf24@8708 84 cirrus_check(void)
kaf24@8708 85 {
kaf24@8708 86 outw(0x3C4, 0x9206);
kaf24@8708 87 return inb(0x3C5) == 0x12;
kaf24@8708 88 }
kaf24@8708 89
kfraser@10976 90 static int
kfraser@10893 91 vmmcall(int function, int edi, int esi, int edx, int ecx, int ebx)
kaf24@8708 92 {
kaf24@8708 93 int eax;
kaf24@8708 94
kaf24@8708 95 __asm__ __volatile__(
kaf24@8708 96 ".byte 0x0F,0x01,0xD9"
kaf24@8708 97 : "=a" (eax)
kfraser@10893 98 : "a"(function),
kaf24@8708 99 "b"(ebx), "c"(ecx), "d"(edx), "D"(edi), "S"(esi)
kaf24@8708 100 );
kaf24@8708 101 return eax;
kaf24@8708 102 }
kaf24@8708 103
kfraser@10976 104 static int
kaf24@8708 105 check_amd(void)
kaf24@8708 106 {
kaf24@8708 107 char id[12];
kaf24@8708 108
kaf24@8708 109 __asm__ __volatile__(
kaf24@8708 110 "cpuid"
kaf24@8708 111 : "=b" (*(int *)(&id[0])),
kaf24@8708 112 "=c" (*(int *)(&id[8])),
kaf24@8708 113 "=d" (*(int *)(&id[4]))
kaf24@8708 114 : "a" (0)
kaf24@8708 115 );
kaf24@8708 116 return __builtin_memcmp(id, "AuthenticAMD", 12) == 0;
kaf24@8708 117 }
kaf24@8708 118
kfraser@10976 119 static void
kfraser@10976 120 wrmsr(uint32_t idx, uint64_t v)
kfraser@10976 121 {
kfraser@10976 122 __asm__ __volatile__(
kfraser@10976 123 "wrmsr"
kfraser@10976 124 : : "c" (idx), "a" ((uint32_t)v), "d" ((uint32_t)(v>>32)) );
kfraser@10976 125 }
kfraser@10976 126
kfraser@10976 127 static void
kfraser@10976 128 init_hypercalls(void)
kfraser@10976 129 {
kfraser@10976 130 uint32_t eax, ebx, ecx, edx;
kfraser@10976 131 unsigned long i;
kfraser@10976 132 char signature[13], number[13];
kfraser@10976 133 xen_extraversion_t extraversion;
kfraser@10976 134
kfraser@10976 135 cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
kfraser@10976 136
kfraser@10976 137 *(uint32_t *)(signature + 0) = ebx;
kfraser@10976 138 *(uint32_t *)(signature + 4) = ecx;
kfraser@10976 139 *(uint32_t *)(signature + 8) = edx;
kfraser@10976 140 signature[12] = '\0';
kfraser@10976 141
kfraser@10976 142 if (strcmp("XenVMMXenVMM", signature) || (eax < 0x40000002)) {
kfraser@10976 143 puts("FATAL: Xen hypervisor not detected\n");
kfraser@10976 144 __asm__ __volatile__( "ud2" );
kfraser@10976 145 }
kfraser@10976 146
kfraser@10976 147 cpuid(0x40000001, &eax, &ebx, &ecx, &edx);
kfraser@10976 148
kfraser@10976 149 puts("Detected Xen v");
kfraser@10976 150 puts(itoa(number, eax >> 16));
kfraser@10976 151 puts(".");
kfraser@10976 152 puts(itoa(number, eax & 0xffff));
kfraser@10976 153
kfraser@10976 154 cpuid(0x40000002, &eax, &ebx, &ecx, &edx);
kfraser@10976 155
kfraser@10976 156 for (i = 0; i < eax; i++)
kfraser@10976 157 wrmsr(ebx, HYPERCALL_PHYSICAL_ADDRESS + (i << 12) + i);
kfraser@10976 158
kfraser@10976 159 hypercall_xen_version(XENVER_extraversion, extraversion);
kfraser@10976 160 puts(extraversion);
kfraser@10976 161 puts("\n");
kfraser@10976 162 }
kfraser@10976 163
kaf24@8708 164 int
kaf24@8708 165 main(void)
kaf24@8708 166 {
kfraser@11081 167 struct xen_hvm_param hvm_param;
kaf24@10575 168
kaf24@8708 169 puts("HVM Loader\n");
kaf24@8708 170
kfraser@10976 171 init_hypercalls();
kfraser@10976 172
kaf24@8708 173 puts("Loading ROMBIOS ...\n");
kaf24@8708 174 memcpy((void *)ROMBIOS_PHYSICAL_ADDRESS, rombios, sizeof(rombios));
kfraser@11081 175
kfraser@11081 176 hvm_param.domid = DOMID_SELF;
kfraser@11081 177 hvm_param.index = HVM_PARAM_APIC_ENABLED;
kfraser@11081 178 if (!hypercall_hvm_op(HVMOP_get_param, &hvm_param) && hvm_param.value)
kaf24@10575 179 create_mp_tables();
kaf24@10575 180
kaf24@8708 181 if (cirrus_check()) {
kaf24@8708 182 puts("Loading Cirrus VGABIOS ...\n");
kaf24@8708 183 memcpy((void *)VGABIOS_PHYSICAL_ADDRESS,
kaf24@8708 184 vgabios_cirrusvga, sizeof(vgabios_cirrusvga));
kaf24@8708 185 } else {
kaf24@8708 186 puts("Loading Standard VGABIOS ...\n");
kaf24@8708 187 memcpy((void *)VGABIOS_PHYSICAL_ADDRESS,
kaf24@8708 188 vgabios_stdvga, sizeof(vgabios_stdvga));
kaf24@8708 189 }
kaf24@8708 190
kaf24@8708 191 if (get_acpi_enabled() != 0) {
kaf24@8708 192 puts("Loading ACPI ...\n");
kaf24@8708 193 acpi_madt_update((unsigned char *) acpi);
kaf24@8708 194 if (ACPI_PHYSICAL_ADDRESS+sizeof(acpi) <= 0xF0000) {
kaf24@8708 195 /*
kaf24@8708 196 * Make sure acpi table does not overlap rombios
kaf24@8708 197 * currently acpi less than 8K will be OK.
kaf24@8708 198 */
kaf24@8708 199 memcpy((void *)ACPI_PHYSICAL_ADDRESS, acpi,
kaf24@8708 200 sizeof(acpi));
kaf24@8708 201 }
kaf24@8708 202 }
kaf24@8708 203
kfraser@11115 204 puts("Writing SMBIOS tables ...\n");
kfraser@11115 205 hvm_write_smbios_tables();
kfraser@11115 206
kaf24@8708 207 if (check_amd()) {
kaf24@8708 208 /* AMD implies this is SVM */
kaf24@8708 209 puts("SVM go ...\n");
kfraser@10893 210 vmmcall(SVM_VMMCALL_RESET_TO_REALMODE, 0, 0, 0, 0, 0);
kaf24@8708 211 } else {
kaf24@8708 212 puts("Loading VMXAssist ...\n");
kaf24@8708 213 memcpy((void *)VMXASSIST_PHYSICAL_ADDRESS,
kaf24@8708 214 vmxassist, sizeof(vmxassist));
kaf24@8708 215
kaf24@8708 216 puts("VMX go ...\n");
kaf24@8708 217 __asm__ __volatile__(
kaf24@8708 218 "jmp *%%eax"
kaf24@8708 219 : : "a" (VMXASSIST_PHYSICAL_ADDRESS), "d" (0)
kaf24@8708 220 );
kaf24@8708 221 }
kaf24@8708 222
kaf24@8708 223 puts("Failed to invoke ROMBIOS\n");
kaf24@8708 224 return 0;
kaf24@8708 225 }
kaf24@8708 226