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
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_floating_s)
27 + sizeof(struct mptable_config_s)
28 + sizeof(struct mpt_cpu) * smp_cpus
29 + sizeof(struct mpt_bus)
30 + sizeof(struct mpt_ioapic)
31 + sizeof(struct mpt_intsrc) * 16);
32 void *start = malloc_fseg(length);
34 dprintf(1, "No room for MPTABLE!\n");
38 /* floating pointer structure */
39 struct mptable_floating_s *floating = start;
40 memset(floating, 0, sizeof(*floating));
41 struct mptable_config_s *config = (void*)&floating[1];
42 floating->signature = MPTABLE_SIGNATURE;
43 floating->physaddr = (u32)config;
45 floating->spec_rev = 4;
46 floating->checksum -= checksum(floating, sizeof(*floating));
49 memset(config, 0, sizeof(*config));
50 config->signature = MPCONFIG_SIGNATURE;
51 config->length = length - sizeof(*floating);
53 memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
54 memcpy(config->productid, "0.1 ", sizeof(config->productid));
55 config->entrycount = smp_cpus + 2 + 16;
56 config->lapic = BUILD_APIC_ADDR;
59 u32 cpuid_signature, ebx, ecx, cpuid_features;
60 cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
61 struct mpt_cpu *cpus = (void*)&config[1];
63 for (i = 0; i < smp_cpus; i++) {
64 struct mpt_cpu *cpu = &cpus[i];
65 memset(cpu, 0, sizeof(*cpu));
66 cpu->type = MPT_TYPE_CPU;
69 /* cpu flags: enabled, bootstrap cpu */
70 cpu->cpuflag = (i == 0 ? 3 : 1);
71 if (cpuid_signature) {
72 cpu->cpusignature = cpuid_signature;
73 cpu->featureflag = cpuid_features;
75 cpu->cpusignature = 0x600;
76 cpu->featureflag = 0x201;
81 struct mpt_bus *bus = (void*)&cpus[smp_cpus];
82 memset(bus, 0, sizeof(*bus));
83 bus->type = MPT_TYPE_BUS;
84 memcpy(bus->bustype, "ISA ", sizeof(bus->bustype));
87 u8 ioapic_id = smp_cpus;
88 struct mpt_ioapic *ioapic = (void*)&bus[1];
89 memset(ioapic, 0, sizeof(*ioapic));
90 ioapic->type = MPT_TYPE_IOAPIC;
91 ioapic->apicid = ioapic_id;
92 ioapic->apicver = 0x11;
93 ioapic->flags = 1; // enable
94 ioapic->apicaddr = BUILD_IOAPIC_ADDR;
97 struct mpt_intsrc *intsrcs = (void *)&ioapic[1];
98 for(i = 0; i < 16; i++) {
99 struct mpt_intsrc *isrc = &intsrcs[i];
100 memset(isrc, 0, sizeof(*isrc));
101 isrc->type = MPT_TYPE_INTSRC;
103 isrc->dstapic = ioapic_id;
108 config->checksum -= checksum(config, config->length);
110 dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
111 floating, config, length);