1 // MPTable generation (on emulators)
3 // Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2006 Fabrice Bellard
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
8 #include "util.h" // dprintf
9 #include "config.h" // CONFIG_*
10 #include "mptable.h" // MPTABLE_SIGNATURE
19 dprintf(3, "init MPTable\n");
21 if (MaxCountCPUs <= 1)
22 // Building an mptable on uniprocessor machines confuses some OSes.
25 int length = (sizeof(struct mptable_config_s)
26 + sizeof(struct mpt_cpu) * MaxCountCPUs
27 + sizeof(struct mpt_bus)
28 + sizeof(struct mpt_ioapic)
29 + sizeof(struct mpt_intsrc) * 16);
30 struct mptable_config_s *config = malloc_fseg(length);
31 struct mptable_floating_s *floating = malloc_fseg(sizeof(*floating));
32 if (!config || !floating) {
33 dprintf(1, "No room for MPTABLE!\n");
37 /* floating pointer structure */
38 memset(floating, 0, sizeof(*floating));
39 floating->signature = MPTABLE_SIGNATURE;
40 floating->physaddr = (u32)config;
42 floating->spec_rev = 4;
43 floating->checksum -= checksum(floating, sizeof(*floating));
46 memset(config, 0, sizeof(*config));
47 config->signature = MPCONFIG_SIGNATURE;
49 memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
50 memcpy(config->productid, "0.1 ", sizeof(config->productid));
51 config->entrycount = MaxCountCPUs + 2 + 16;
52 config->lapic = BUILD_APIC_ADDR;
55 u32 cpuid_signature, ebx, ecx, cpuid_features;
56 cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
57 struct mpt_cpu *cpus = (void*)&config[1];
59 for (i = 0; i < MaxCountCPUs; i++) {
60 struct mpt_cpu *cpu = &cpus[i];
61 memset(cpu, 0, sizeof(*cpu));
62 cpu->type = MPT_TYPE_CPU;
65 /* cpu flags: enabled, bootstrap cpu */
67 cpu->cpuflag = 1 | (i == 0) ? 2 : 0;
70 if (cpuid_signature) {
71 cpu->cpusignature = cpuid_signature;
72 cpu->featureflag = cpuid_features;
74 cpu->cpusignature = 0x600;
75 cpu->featureflag = 0x201;
80 struct mpt_bus *bus = (void*)&cpus[MaxCountCPUs];
81 memset(bus, 0, sizeof(*bus));
82 bus->type = MPT_TYPE_BUS;
83 memcpy(bus->bustype, "ISA ", sizeof(bus->bustype));
86 u8 ioapic_id = CountCPUs;
87 struct mpt_ioapic *ioapic = (void*)&bus[1];
88 memset(ioapic, 0, sizeof(*ioapic));
89 ioapic->type = MPT_TYPE_IOAPIC;
90 ioapic->apicid = ioapic_id;
91 ioapic->apicver = 0x11;
92 ioapic->flags = 1; // enable
93 ioapic->apicaddr = BUILD_IOAPIC_ADDR;
96 struct mpt_intsrc *intsrc = (void*)&ioapic[1];
97 for (i = 0; i < 16; i++) {
98 memset(intsrc, 0, sizeof(*intsrc));
99 intsrc->type = MPT_TYPE_INTSRC;
100 intsrc->srcbusirq = i;
101 intsrc->dstapic = ioapic_id;
103 if (qemu_cfg_irq0_override()) {
104 /* Destination 2 is covered by irq0->inti2 override (i ==
105 0). Source IRQ 2 is unused */
115 config->length = (void*)intsrc - (void*)config;
116 config->checksum -= checksum(config, config->length);
118 dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
119 floating, config, config->length);