ia64/xen-unstable

view tools/firmware/hvmloader/hvmloader.c @ 11715:f426f6e646eb

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