debuggers.hg

view tools/firmware/hvmloader/mp_tables.c @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children 5c0bf00e371d
line source
1 /*
2 * mp_tables.c: Dynamically writes MP table info into the ROMBIOS.
3 *
4 * In order to work with various VCPU counts, this code reads the VCPU count
5 * for the HVM partition and creates the correct MP tables for the VCPU count
6 * and places the information into a predetermined location set aside in the
7 * ROMBIOS during build time.
8 *
9 * Please note that many of the values, such as the CPU's
10 * family/model/stepping, are hard-coded based upon the values that were used
11 * in the ROMBIOS and may need to be modified or calculated dynamically to
12 * correspond with what an HVM guest's CPUID returns.
13 *
14 * Travis Betak, travis.betak@amd.com
15 * Copyright (c) 2006, AMD.
16 *
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms and conditions of the GNU General Public License,
19 * version 2, as published by the Free Software Foundation.
20 *
21 * This program is distributed in the hope it will be useful, but WITHOUT
22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
24 * more details.
25 *
26 * You should have received a copy of the GNU General Public License along with
27 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
28 * Place - Suite 330, Boston, MA 02111-1307 USA.
29 */
31 #include <stdint.h>
32 #include "config.h"
34 /* number of non-processor MP table entries */
35 #define NR_NONPROC_ENTRIES 18
37 #define ENTRY_TYPE_PROCESSOR 0
38 #define ENTRY_TYPE_BUS 1
39 #define ENTRY_TYPE_IOAPIC 2
40 #define ENTRY_TYPE_IO_INTR 3
41 #define ENTRY_TYPE_LOCAL_INTR 4
43 #define CPU_FLAG_ENABLED 0x01
44 #define CPU_FLAG_BSP 0x02
46 /* TODO change this to correspond with what the guest's see's from CPUID */
47 #define CPU_SIG_FAMILY 0x06
48 #define CPU_SIG_MODEL 0x00
49 #define CPU_SIG_STEPPING 0x00
50 #define CPU_SIGNATURE ((CPU_SIG_FAMILY << 8) \
51 | (CPU_SIG_MODEL << 4) \
52 | (CPU_SIG_STEPPING))
53 #define CPU_FEATURE_FPU (1U << 0)
54 #define CPU_FEATURE_MCE (1U << 7)
55 #define CPU_FEATURE_CX8 (1U << 8)
56 #define CPU_FEATURE_APIC (1U << 9)
57 #define CPU_FEATURES (CPU_FEATURE_FPU | CPU_FEATURE_APIC)
59 #define BUS_TYPE_LENGTH 6
60 #define BUS_TYPE_STR_ISA "ISA "
61 #define BUS_ID_ISA 0
63 #define INTR_TYPE_INT 0
64 #define INTR_TYPE_NMI 1
65 #define INTR_TYPE_SMI 2
66 #define INTR_TYPE_EXTINT 3
68 #define INTR_MAX_NR 16
70 #include "util.h"
72 extern int get_vcpu_nr(void); /* for the guest's VCPU count */
74 /*
75 * The following structures are defined in the MuliProcessor Specifiation v1.4
76 */
78 /* MP Floating Pointer Structure */
79 struct mp_floating_pointer_struct {
80 uint8_t signature[4];
81 uint32_t mp_table;
82 uint8_t length;
83 uint8_t revision;
84 uint8_t checksum;
85 uint8_t feature[5];
86 };
88 /* MP Configuration Table */
89 struct mp_config_table {
90 uint8_t signature[4];
91 uint16_t length;
92 uint8_t revision;
93 uint8_t checksum;
94 uint8_t oem_id[8];
95 uint8_t vendor_id[12];
96 uint32_t oem_table;
97 uint16_t oem_table_sz;
98 uint16_t nr_entries;
99 uint32_t lapic;
100 uint16_t extended_length;
101 uint8_t extended_checksum;
102 uint8_t reserved;
103 };
105 /* MP Processor Entry */
106 struct mp_proc_entry {
107 uint8_t type;
108 uint8_t lapic_id;
109 uint8_t lapic_version;
110 uint8_t cpu_flags;
111 uint32_t cpu_signature;
112 uint32_t feature_flags;
113 uint8_t reserved[8];
114 };
116 /* MP Bus Entry */
117 struct mp_bus_entry {
118 uint8_t type;
119 uint8_t bus_id;
120 uint8_t bus_type_str[6];
121 };
123 /* MP IOAPIC Entry */
124 struct mp_ioapic_entry {
125 uint8_t type;
126 uint8_t ioapic_id;
127 uint8_t ioapic_version;
128 uint8_t ioapic_flags;
129 uint32_t ioapic_addr;
130 };
132 /* MP IO Interrupt Entry */
133 struct mp_io_intr_entry {
134 uint8_t type;
135 uint8_t intr_type;
136 uint16_t io_intr_flags;
137 uint8_t src_bus_id;
138 uint8_t src_bus_irq;
139 uint8_t dst_ioapic_id;
140 uint8_t dst_ioapic_intin;
141 };
143 /* MP Local Interrupt Entry */
144 struct mp_local_intr_entry {
145 uint8_t type;
146 uint8_t intr_type;
147 uint16_t local_intr_flags;
148 uint8_t src_bus_id;
149 uint8_t src_bus_irq;
150 uint8_t dst_lapic_id;
151 uint8_t dst_lapic_lintin;
152 };
155 void fill_mp_config_table(struct mp_config_table *mpct, int length)
156 {
157 int vcpu_nr, i;
158 uint8_t checksum;
160 vcpu_nr = get_vcpu_nr();
162 /* fill in the MP configuration table signature, "PCMP" */
163 mpct->signature[0] = 'P';
164 mpct->signature[1] = 'C';
165 mpct->signature[2] = 'M';
166 mpct->signature[3] = 'P';
168 mpct->length = length;
170 mpct->revision = 4;
172 /* fill in the OEM ID string, "_HVMCPU_" */
173 mpct->oem_id[0] = '_'; mpct->oem_id[3] = 'M'; mpct->oem_id[6] = 'U';
174 mpct->oem_id[1] = 'H'; mpct->oem_id[4] = 'C'; mpct->oem_id[7] = '_';
175 mpct->oem_id[2] = 'V'; mpct->oem_id[5] = 'P';
177 /* fill in the Vendor ID string, "XEN " */
178 mpct->vendor_id[0] = 'X'; mpct->vendor_id[6] = ' ';
179 mpct->vendor_id[1] = 'E'; mpct->vendor_id[7] = ' ';
180 mpct->vendor_id[2] = 'N'; mpct->vendor_id[8] = ' ';
181 mpct->vendor_id[3] = ' '; mpct->vendor_id[9] = ' ';
182 mpct->vendor_id[4] = ' '; mpct->vendor_id[10] = ' ';
183 mpct->vendor_id[5] = ' '; mpct->vendor_id[11] = ' ';
185 mpct->oem_table = 0;
186 mpct->oem_table_sz = 0;
188 mpct->nr_entries = vcpu_nr + NR_NONPROC_ENTRIES;
190 mpct->lapic = LAPIC_BASE_ADDRESS;
191 mpct->extended_length = 0;
192 mpct->extended_checksum = 0;
194 /* Finally, fill in the checksum. */
195 mpct->checksum = checksum = 0;
196 for ( i = 0; i < length; i++ )
197 checksum += ((uint8_t *)(mpct))[i];
198 mpct->checksum = -checksum;
199 }
201 /* fills in an MP processor entry for VCPU 'vcpu_id' */
202 void fill_mp_proc_entry(struct mp_proc_entry *mppe, int vcpu_id)
203 {
204 mppe->type = ENTRY_TYPE_PROCESSOR;
205 mppe->lapic_id = LAPIC_ID(vcpu_id);
206 mppe->lapic_version = 0x11;
207 mppe->cpu_flags = CPU_FLAG_ENABLED;
208 if ( vcpu_id == 0 )
209 mppe->cpu_flags |= CPU_FLAG_BSP;
210 mppe->cpu_signature = CPU_SIGNATURE;
211 mppe->feature_flags = CPU_FEATURES;
212 }
215 /* fills in an MP bus entry of type 'type' and bus ID 'bus_id' */
216 void fill_mp_bus_entry(struct mp_bus_entry *mpbe, int bus_id, const char *type)
217 {
218 int i;
220 mpbe->type = ENTRY_TYPE_BUS;
221 mpbe->bus_id = bus_id;
222 for ( i = 0; i < BUS_TYPE_LENGTH; i++ )
223 mpbe->bus_type_str[i] = type[i]; /* FIXME length check? */
224 }
227 /* fills in an MP IOAPIC entry for IOAPIC 'ioapic_id' */
228 void fill_mp_ioapic_entry(struct mp_ioapic_entry *mpie)
229 {
230 mpie->type = ENTRY_TYPE_IOAPIC;
231 mpie->ioapic_id = IOAPIC_ID;
232 mpie->ioapic_version = IOAPIC_VERSION;
233 mpie->ioapic_flags = 1; /* enabled */
234 mpie->ioapic_addr = IOAPIC_BASE_ADDRESS;
235 }
238 /* fills in an IO interrupt entry for IOAPIC 'ioapic_id' */
239 void fill_mp_io_intr_entry(
240 struct mp_io_intr_entry *mpiie,
241 int src_bus_id, int src_bus_irq, int ioapic_id, int dst_ioapic_intin)
242 {
243 mpiie->type = ENTRY_TYPE_IO_INTR;
244 mpiie->intr_type = INTR_TYPE_INT;
245 mpiie->io_intr_flags = (PCI_ISA_IRQ_MASK & (1U<<src_bus_irq)) ? 0xf : 0x0;
246 mpiie->src_bus_id = src_bus_id;
247 mpiie->src_bus_irq = src_bus_irq;
248 mpiie->dst_ioapic_id = ioapic_id;
249 mpiie->dst_ioapic_intin = dst_ioapic_intin;
250 }
253 /* fill in the mp floating processor structure */
254 void fill_mpfps(struct mp_floating_pointer_struct *mpfps, uint32_t mpct)
255 {
256 int i;
257 uint8_t checksum;
260 mpfps->signature[0] = '_';
261 mpfps->signature[1] = 'M';
262 mpfps->signature[2] = 'P';
263 mpfps->signature[3] = '_';
265 mpfps->mp_table = mpct;
266 mpfps->length = 1;
267 mpfps->revision = 4;
268 mpfps->checksum = 0;
269 for (i = 0; i < 5; ++i)
270 mpfps->feature[i] = 0;
272 /* compute the checksum for our new table */
273 checksum = 0;
274 for ( i = 0; i < sizeof(struct mp_floating_pointer_struct); i++ )
275 checksum += ((uint8_t *)(mpfps))[i];
276 mpfps->checksum = -checksum;
277 }
280 /*
281 * find_mp_table_start - searchs through BIOS memory for '___HVMMP' signature
282 *
283 * The '___HVMMP' signature is created by the ROMBIOS and designates a chunk
284 * of space inside the ROMBIOS that is safe for us to write our MP table info
285 */
286 void* get_mp_table_start(void)
287 {
288 char *bios_mem;
290 for ( bios_mem = (char *)ROMBIOS_BEGIN;
291 bios_mem != (char *)ROMBIOS_END;
292 bios_mem++ )
293 {
294 if ( strncmp(bios_mem, "___HVMMP", 8) == 0)
295 return bios_mem;
296 }
298 return NULL;
299 }
302 /* recalculate the new ROMBIOS checksum after adding MP tables */
303 void reset_bios_checksum(void)
304 {
305 uint32_t i;
306 uint8_t checksum;
308 checksum = 0;
309 for (i = 0; i < ROMBIOS_MAXOFFSET; ++i)
310 checksum += ((uint8_t *)(ROMBIOS_BEGIN))[i];
312 *((uint8_t *)(ROMBIOS_BEGIN + ROMBIOS_MAXOFFSET)) = -checksum;
313 }
316 /* create_mp_tables - creates MP tables for the guest based upon config data */
317 void create_mp_tables(void)
318 {
319 void *mp_table_base;
320 char *p;
321 int vcpu_nr, i, length;
323 vcpu_nr = get_vcpu_nr();
325 printf("Creating MP tables ...\n");
327 /* Find the 'safe' place in ROMBIOS for the MP tables. */
328 mp_table_base = get_mp_table_start();
329 if ( mp_table_base == NULL )
330 {
331 printf("Couldn't find start point for MP tables\n");
332 return;
333 }
335 p = mp_table_base + sizeof(struct mp_config_table);
337 for ( i = 0; i < vcpu_nr; i++ )
338 {
339 fill_mp_proc_entry((struct mp_proc_entry *)p, i);
340 p += sizeof(struct mp_proc_entry);
341 }
343 fill_mp_bus_entry((struct mp_bus_entry *)p, BUS_ID_ISA, BUS_TYPE_STR_ISA);
344 p += sizeof(struct mp_bus_entry);
346 fill_mp_ioapic_entry((struct mp_ioapic_entry *)p);
347 p += sizeof(struct mp_ioapic_entry);
349 for ( i = 0; i < 16; i++ )
350 {
351 if ( i == 2 ) continue; /* skip the slave PIC connection */
352 fill_mp_io_intr_entry((struct mp_io_intr_entry *)p,
353 BUS_ID_ISA, i, IOAPIC_ID, (i == 0) ? 2 : i);
354 p += sizeof(struct mp_io_intr_entry);
355 }
357 length = p - (char *)mp_table_base;
359 /* find the next 16-byte boundary to place the mp floating pointer */
360 while ( (unsigned long)p & 0xF )
361 p++;
363 fill_mpfps((struct mp_floating_pointer_struct *)p,
364 (uint32_t)mp_table_base);
366 fill_mp_config_table((struct mp_config_table *)mp_table_base, length);
367 reset_bios_checksum();
368 }