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 int smp_cpus = CountCPUs;
23 // Building an mptable on uniprocessor machines confuses some OSes.
26 int length = (sizeof(struct mptable_config_s)
27 + sizeof(struct mpt_cpu) * smp_cpus
28 + sizeof(struct mpt_bus)
29 + sizeof(struct mpt_ioapic)
30 + sizeof(struct mpt_intsrc) * 16);
31 struct mptable_config_s *config = malloc_fseg(length);
32 struct mptable_floating_s *floating = malloc_fseg(sizeof(*floating));
33 if (!config || !floating) {
34 dprintf(1, "No room for MPTABLE!\n");
38 /* floating pointer structure */
39 memset(floating, 0, sizeof(*floating));
40 floating->signature = MPTABLE_SIGNATURE;
41 floating->physaddr = (u32)config;
43 floating->spec_rev = 4;
44 floating->checksum -= checksum(floating, sizeof(*floating));
47 memset(config, 0, sizeof(*config));
48 config->signature = MPCONFIG_SIGNATURE;
50 memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
51 memcpy(config->productid, "0.1 ", sizeof(config->productid));
52 config->entrycount = smp_cpus + 2 + 16;
53 config->lapic = BUILD_APIC_ADDR;
56 u32 cpuid_signature, ebx, ecx, cpuid_features;
57 cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
58 struct mpt_cpu *cpus = (void*)&config[1];
60 for (i = 0; i < smp_cpus; i++) {
61 struct mpt_cpu *cpu = &cpus[i];
62 memset(cpu, 0, sizeof(*cpu));
63 cpu->type = MPT_TYPE_CPU;
66 /* cpu flags: enabled, bootstrap cpu */
67 cpu->cpuflag = (i == 0 ? 3 : 1);
68 if (cpuid_signature) {
69 cpu->cpusignature = cpuid_signature;
70 cpu->featureflag = cpuid_features;
72 cpu->cpusignature = 0x600;
73 cpu->featureflag = 0x201;
78 struct mpt_bus *bus = (void*)&cpus[smp_cpus];
79 memset(bus, 0, sizeof(*bus));
80 bus->type = MPT_TYPE_BUS;
81 memcpy(bus->bustype, "ISA ", sizeof(bus->bustype));
84 u8 ioapic_id = smp_cpus;
85 struct mpt_ioapic *ioapic = (void*)&bus[1];
86 memset(ioapic, 0, sizeof(*ioapic));
87 ioapic->type = MPT_TYPE_IOAPIC;
88 ioapic->apicid = ioapic_id;
89 ioapic->apicver = 0x11;
90 ioapic->flags = 1; // enable
91 ioapic->apicaddr = BUILD_IOAPIC_ADDR;
94 struct mpt_intsrc *intsrc = (void*)&ioapic[1];
95 for (i = 0; i < 16; i++) {
96 memset(intsrc, 0, sizeof(*intsrc));
97 intsrc->type = MPT_TYPE_INTSRC;
98 intsrc->srcbusirq = i;
99 intsrc->dstapic = ioapic_id;
101 if (qemu_cfg_irq0_override()) {
102 /* Destination 2 is covered by irq0->inti2 override (i ==
103 0). Source IRQ 2 is unused */
113 config->length = (void*)intsrc - (void*)config;
114 config->checksum -= checksum(config, config->length);
116 dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
117 floating, config, config->length);