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 "memmap.h" // malloc_fseg
10 #include "config.h" // CONFIG_*
11 #include "mptable.h" // MPTABLE_SIGNATURE
25 dprintf(3, "init MPTable\n");
27 int smp_cpus = CountCPUs;
29 // Building an mptable on uniprocessor machines confuses some OSes.
32 int length = (sizeof(struct mptable_floating_s)
33 + sizeof(struct mptable_config_s)
34 + sizeof(struct mpt_cpu) * smp_cpus
35 + sizeof(struct mpt_bus)
36 + sizeof(struct mpt_ioapic)
37 + sizeof(struct mpt_intsrc) * 16);
38 void *start = malloc_fseg(length);
40 dprintf(1, "No room for MPTABLE!\n");
44 /* floating pointer structure */
45 struct mptable_floating_s *floating = start;
46 memset(floating, 0, sizeof(*floating));
47 struct mptable_config_s *config = (void*)&floating[1];
48 floating->signature = MPTABLE_SIGNATURE;
49 floating->physaddr = (u32)config;
51 floating->spec_rev = 4;
52 floating->checksum -= checksum(floating, sizeof(*floating));
55 memset(config, 0, sizeof(*config));
56 config->signature = MPCONFIG_SIGNATURE;
58 memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
59 memcpy(config->productid, "0.1 ", sizeof(config->productid));
60 config->entrycount = smp_cpus + 2 + 16;
61 config->lapic = BUILD_APIC_ADDR;
64 u32 cpuid_signature, ebx, ecx, cpuid_features;
65 cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
66 struct mpt_cpu *cpus = (void*)&config[1];
68 for (i = 0; i < smp_cpus; i++) {
69 struct mpt_cpu *cpu = &cpus[i];
70 memset(cpu, 0, sizeof(*cpu));
71 cpu->type = MPT_TYPE_CPU;
74 /* cpu flags: enabled, bootstrap cpu */
75 cpu->cpuflag = (i == 0 ? 3 : 1);
76 if (cpuid_signature) {
77 cpu->cpusignature = cpuid_signature;
78 cpu->featureflag = cpuid_features;
80 cpu->cpusignature = 0x600;
81 cpu->featureflag = 0x201;
86 struct mpt_bus *bus = (void*)&cpus[smp_cpus];
87 memset(bus, 0, sizeof(*bus));
88 bus->type = MPT_TYPE_BUS;
89 memcpy(bus->bustype, "ISA ", sizeof(bus->bustype));
92 u8 ioapic_id = smp_cpus;
93 struct mpt_ioapic *ioapic = (void*)&bus[1];
94 memset(ioapic, 0, sizeof(*ioapic));
95 ioapic->type = MPT_TYPE_IOAPIC;
96 ioapic->apicid = ioapic_id;
97 ioapic->apicver = 0x11;
98 ioapic->flags = 1; // enable
99 ioapic->apicaddr = BUILD_IOAPIC_ADDR;
102 struct mpt_intsrc *intsrc = (void*)&ioapic[1];
103 for (i = 0; i < 16; i++) {
104 memset(intsrc, 0, sizeof(*intsrc));
105 intsrc->type = MPT_TYPE_INTSRC;
106 intsrc->srcbusirq = i;
107 intsrc->dstapic = ioapic_id;
110 /* Destination 2 is covered by irq0->inti2 override (i ==
111 0). Source IRQ 2 is unused */
121 config->length = (void*)intsrc - (void*)config;
122 config->checksum -= checksum(config, config->length);
124 dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
125 floating, config, config->length);