-
Notifications
You must be signed in to change notification settings - Fork 0
/
lowmem.cpp
152 lines (127 loc) · 5.25 KB
/
lowmem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <sys/mman.h>
#include <cassert>
#include <cstring>
#include <iostream>
#include "mptable.h"
#include "runslice.h"
extern "C" char realmode_blob_start[];
struct realmode_header {
uint64_t reserved;
uint64_t kernel_entry;
uint64_t kernel_arg;
} __attribute__((__packed__));
// MPTABLE kludges, only necessary if the guest enables CONFIG_X86_MPPARSE
#ifdef CONFIG_EMIT_MPTABLE
static uintptr_t obliterate_mptable_range(void* lowmem, uintptr_t base, uintptr_t size)
{
constexpr uint32_t BOGUS_MAGIC = ('-'<<24) | ('P'<<16) | ('M'<<8) | '-';
assert(base % sizeof(uint32_t) == 0);
assert(size % sizeof(uint32_t) == 0);
uint32_t* const start = reinterpret_cast<uint32_t*>(static_cast<char*>(lowmem) + base);
uint32_t* const end = start + size / sizeof(uint32_t);
uintptr_t found = 0;
for (uint32_t* p = start; p < end; p++)
{
if (*p == MPTABLE_PTR_MAGIC)
{
const uintptr_t addr = base + (p - start) * sizeof(*p);
if (!found)
found = addr;
printf("Obliterating MP table at 0x%lx\n", addr);
*p = BOGUS_MAGIC;
}
}
return found;
}
static void write_mptable(const Options& options, void* vaddr, uintptr_t paddr)
{
MPFloatingPointer* mpfp = static_cast<MPFloatingPointer*>(vaddr);
memset(mpfp, 0, sizeof(*mpfp));
mpfp->signature = MPTABLE_PTR_MAGIC;
mpfp->length = 1;
mpfp->specrev = 4;
mpfp->physaddr = paddr + sizeof(*mpfp);
mpfp->checksum = acpi_checksum(mpfp, sizeof(*mpfp));
MPConfigHeader* mpt = reinterpret_cast<MPConfigHeader*>(mpfp + 1);
memset(mpt, 0, sizeof(*mpt));
mpt->signature = MPTABLE_CONFIG_MAGIC;
// base_length and checksum are computed at the end
mpt->spec_rev = 4;
memset(mpt->oemid, ' ', sizeof(mpt->oemid));
memset(mpt->prodid, ' ', sizeof(mpt->oemid));
memcpy(mpt->oemid, "SLICER", 6);
memcpy(mpt->prodid, "SLICER", 6);
mpt->lapic_addr = 0xfee00000;
mpt->entries = 0;
// Assume uniform CPUs.
uint16_t family_model_stepping;
uint32_t cpu_feature_flags;
{
uint32_t eax, ebx, ecx;
cpuid(1, 0, eax, ebx, ecx, cpu_feature_flags);
family_model_stepping = eax & 0xfff;
}
// Emit one processor entry for each CPU.
mpt->entries += options.apic_ids.size();
MPProcessorEntry* mpp = reinterpret_cast<MPProcessorEntry*>(mpt + 1);
for (size_t i = 0; i < options.apic_ids.size(); i++)
{
mpp[i].type = MPEntryType::Processor;
mpp[i].apic_id = options.apic_ids[i];
mpp[i].apic_ver = 0x14;
mpp[i].cpu_flags = MP_PROCESSOR_ENABLED;
if (i == 0) {
mpp[i].cpu_flags |= MP_PROCESSOR_BSP;
}
mpp[i].cpu_signature = family_model_stepping;
mpp[i].feature_flags = cpu_feature_flags;
memset(&mpp[i].reserved, 0, sizeof(mpp[i].reserved));
}
// Emit NMI routing.
mpt->entries++;
MPInterruptEntry* mpi = reinterpret_cast<MPInterruptEntry*>(mpp + options.apic_ids.size());
mpi->type = MPEntryType::LocalInterrupt;
mpi->int_type = MPInterruptType::Nmi;
mpi->flags = 0;
mpi->source_bus = 0;
mpi->source_irq = 0;
mpi->dest_apic_id = 0xff;
mpi->dest_apic_int = 1;
// Compute total length and checksum.
mpt->base_length = reinterpret_cast<char*>(mpi + 1) - reinterpret_cast<char*>(mpt);
mpt->checksum = acpi_checksum(mpt, mpt->base_length);
}
#endif
bool lowmem_init(const Options& options, const AutoFd& devmem, uintptr_t kernel_entry, uintptr_t kernel_arg, uintptr_t &boot_ip)
{
constexpr size_t MiB = 0x100000;
void* lowmem = mmap(nullptr, MiB, PROT_READ | PROT_WRITE, MAP_SHARED, devmem, 0);
if (lowmem == MAP_FAILED) {
perror("Error: Failed to map first MiB of RAM");
return false;
}
struct realmode_header* realmode_header = reinterpret_cast<struct realmode_header*>(realmode_blob_start);
assert(realmode_blob_size > sizeof(*realmode_header));
assert(realmode_header->kernel_entry == 0x5c3921544fd4ae2d);
realmode_header->kernel_entry = kernel_entry;
realmode_header->kernel_arg = kernel_arg;
memcpy(static_cast<char*>(lowmem) + options.lowmem, realmode_blob_start, realmode_blob_size);
boot_ip = options.lowmem;
#ifdef CONFIG_EMIT_MPTABLE
// Scan through the same memory ranges that Linux does looking for MPTABLE structures.
constexpr size_t KiB = 0x400;
uintptr_t mptable1_pa = obliterate_mptable_range(lowmem, 0, KiB); // bottom 1K
uintptr_t mptable2_pa = obliterate_mptable_range(lowmem, 639 * KiB, KiB); // top 1K of base RAM
// This won't work for any MPTABLE structures in the BIOS area, so instead we need to write
// our own in low-memory. We try to overwrite an existing one, but fall back to punting on an
// arbitrary address.
constexpr uintptr_t FALLBACK_MPTABLE_ADDR = 639 * KiB;
uintptr_t mptable_pa = mptable1_pa ? mptable1_pa : (mptable2_pa ? mptable2_pa : FALLBACK_MPTABLE_ADDR);
printf("Dummy MP table at 0x%lx\n", mptable_pa);
write_mptable(options, static_cast<char*>(lowmem) + mptable_pa, mptable_pa);
#endif
munmap(lowmem, MiB);
printf("Copied real-mode boot code to 0x%lx-%lx. Will enter kernel at %lx.\n",
options.lowmem, options.lowmem + realmode_blob_size - 1, kernel_entry);
return true;
}