forked from project-oak/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout.ld
361 lines (326 loc) · 12.9 KB
/
layout.ld
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* Copyright 2022 The Project Oak Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
HIDDEN(TOP = 4096M);
/* We assume BIOS_SIZE will be provided externally. */
MEMORY {
ram_low : ORIGIN = 0, LENGTH = 1M
bios : ORIGIN = TOP - BIOS_SIZE, LENGTH = BIOS_SIZE
}
ENTRY(reset_vector)
/* Segment descriptor flags.
* See Sections 4.7 and 4.8 in AMD64 Architecture Programmer's Manual, Volume 2 for more details.
*/
HIDDEN(SEGMENT_4K = 1 << (32 + 23)); /* G */
HIDDEN(SEGMENT_DEFAULT_32BIT_OP = 1 << (32 + 22)); /* D/B */
HIDDEN(SEGMENT_LONG = 1 << (32 + 21));
HIDDEN(SEGMENT_PRESENT = 1 << (32 + 15)); /* P */
HIDDEN(SEGMENT_USER = 1 << (32 + 12)); /* S */
HIDDEN(SEGMENT_CODE = 1 << (32 + 11));
HIDDEN(SEGMENT_CONFORMING = 1 << (32 + 10));
HIDDEN(SEGMENT_WRITABLE = 1 << (32 + 9));
HIDDEN(SEGMENT_ACCESSED = 1 << (32 + 8));
/* Page table flags */
HIDDEN(PAGE_PRESENT = 1 << 0);
HIDDEN(PAGE_WRITABLE = 1 << 1);
HIDDEN(PAGE_SIZE = 1 << 7);
/* Gate descriptor flags */
HIDDEN(DESCRIPTOR_PRESENT = 1 << 47);
HIDDEN(DESCRIPTOR_INTERRUPT_GATE = 0xE << 40);
/* SEV metadata section types; values defined in
* https://github.com/tianocore/edk2/blob/master/OvmfPkg/ResetVector/X64/OvmfSevMetadata.asm
*/
HIDDEN(SEV_SECTION_UNMEASURED = 0x1);
HIDDEN(SEV_SECTION_SECRET = 0x2);
HIDDEN(SEV_SECTION_CPUID = 0x3);
SECTIONS {
/* Lowest 1MB of memory (the real mode address space) where we place a lot of our data structures, as
* that region is usually avoided by OS kernels.
* For more background, see:
* https://wiki.osdev.org/Memory_Map_(x86)
* https://google.github.io/crosvm/appendix/memory_layout.html
* https://github.com/tianocore/edk2/blob/master/OvmfPkg/OvmfPkgX64.fdf
*
* These sections have to be marked NOLOAD, otherwise you risk ending up with a 4GB BIOS image.
*/
.boot 0x1000 (NOLOAD) : {
KEEP(*(.boot))
. = ALIGN(4K);
} > ram_low
.ap_text : AT(TOP - 4K) {
ap_text_start = .;
*(.ap_text .ap_text.*)
ap_text_size = . - ap_text_start;
} > ram_low
.ap_bss (NOLOAD) : AT (ADDR(.ap_text) + SIZEOF(.ap_text)) {
ap_bss_start = .;
*(.ap_bss .ap_bss.*)
ap_bss_size = . - ap_bss_start;
} > ram_low
ASSERT(. <= 0x10000, "AP bootstrap code needs to be in the first 64K")
.bss (NOLOAD) : {
bss_start = .;
/* Include large section (.lbss) to support large code model.
* See <https://lld.llvm.org/ELF/large_sections.html>.
*/
*(.bss .bss.* .lbss.*)
bss_size = . - bss_start;
} > ram_low
.data :
AT ( ADDR (.text) + SIZEOF (.text) ) {
data_start = .;
/* Include large section (.ldata) to support large code model.
* See <https://lld.llvm.org/ELF/large_sections.html>.
*/
*(.data .data.* .ldata.*)
data_size = . - data_start;
} > ram_low
/* Put the stack just below the EBDA, at the end of 512K. */
.stack (0x80000 - 32K) (NOLOAD) : {
. += 32K;
stack_start = .;
} > ram_low
/* EBDA - Extended BIOS Data Area; 128K of memory between [512K, 640K). The RSDP must be within
* the first KiB of the EBDA.
*/
.ebda 0x80000 (NOLOAD) : {
/* Ensure RSDP is always first in the EBDA. */
KEEP(*(.ebda.rsdp))
. = ALIGN(1K);
*(.ebda .ebda.*)
} > ram_low
ASSERT(. <= 0xA0000, "EBDA overflow")
. = ORIGIN(bios);
.rodata : {
/* Include large section (.lrodata) to support large code model.
* See <https://lld.llvm.org/ELF/large_sections.html>.
*/
KEEP(*(.rodata .rodata.* .lrodata.*))
} > bios
.text : {
/* Include large section (.ltext) to support large code model.
* See <https://lld.llvm.org/ELF/large_sections.html>.
*/
*(.text .text.* .ltext.*)
text_end = .;
} > bios
/* Everything below this line interacts with 16-bit code, so should be kept as close to the end of the file as
* possible; max TOP - 32k. */
.text16 ALIGN(TOP - 4K + SIZEOF(.ap_text), 16) : {
*(.text16 .text16.*)
} > bios
.rodata.gdt ALIGN(8) : {
/* Null segment */
QUAD(0)
/* 64-bit code segment (see Section 4.8.1):
* - D=0, as it's ignored when L=1
* - L=1 (long mode)
* - P=1 (present)
* - DPL=00 (ring 0)
* - S=1 (user)
* - C/D=1 (code)
* - C=0
*/
cs = . - ADDR(.rodata.gdt);
QUAD(SEGMENT_LONG |
SEGMENT_PRESENT |
SEGMENT_USER |
SEGMENT_CODE |
SEGMENT_ACCESSED)
/* 64-bit data segment (see Section 4.8.2):
* - P=1 (present)
* - S=1 (user)
* - C/D=0 (data)
* ...and for 32-bit compatibility (see Section 4.7.3):
* - G=1 (limit field is in 4K blocks)
* - D/S=1 (default operations are 32-bit)
* - limit = 0xFFFFF (in 4K blocks because G=1)
*
* Although according to the manual in 64-bit mode WRITABLE should be ignored, at least qemu
* will cause a #GP if we try to load SS with a segment descriptor that doesn't have it set.
*/
ds = . - ADDR(.rodata.gdt);
QUAD(SEGMENT_4K |
SEGMENT_DEFAULT_32BIT_OP |
SEGMENT_PRESENT |
SEGMENT_USER |
SEGMENT_WRITABLE |
SEGMENT_ACCESSED |
(0xF << (32 + 16)) | /* Segment Limit [19:16] */
0xFFFF) /* Segment Limit [15:0] */
/* 32-bit code segment, although we only need it for a really short time (see Section 4.7.2):
* - G=1 (limit field is in 4K blocks)
* - D=1 (default operations are 32-bit)
* - P=1 (present)
* - S=1 (user)
* - C/D=1 (code segment)
* - limit = 0xFFFFF (in 4K blocks because G=1)
*/
cs32 = . - ADDR(.rodata.gdt);
QUAD(SEGMENT_4K |
SEGMENT_DEFAULT_32BIT_OP |
SEGMENT_PRESENT |
SEGMENT_USER |
SEGMENT_CODE |
SEGMENT_ACCESSED |
(0xF << (32 + 16)) | /* Segment Limit [19:16] */
0xFFFF) /* Segment Limit [15:0] */
} > bios
/* 32-bit interrupt table */
/* See Section 8.2 in AMD64 Architecture Programmer's Manual, Volume 2 for more details. */
.rodata.idt ALIGN(8) : {
QUAD(0) /* 0 - #DE */
QUAD(0) /* 1 - #DB */
QUAD(0) /* 2 - #NMI */
QUAD(0) /* 3 - #BP */
QUAD(0) /* 4 - #OF */
QUAD(0) /* 5 - #BR */
QUAD(0) /* 6 - #UD */
QUAD(0) /* 7 - #NM */
QUAD(0) /* 8 - #DF */
QUAD(0) /* 9 - reserved */
QUAD(0) /* 10 - #TS */
QUAD(0) /* 11 - #NP */
QUAD(0) /* 12 - #SS */
QUAD( /* 13 - #GP */
(gp_handler & 0xFFFF0000) << 32 | /* target offset [31:16] */
DESCRIPTOR_PRESENT |
DESCRIPTOR_INTERRUPT_GATE |
cs32 << 16 | /* code segment selector */
gp_handler & 0xFFFF) /* target offset [15:0] */
QUAD(0) /* 14 - #PF */
QUAD(0) /* 15 - reserved */
QUAD(0) /* 16 - #MF */
QUAD(0) /* 17 - #AC */
QUAD(0) /* 18 - #MC */
QUAD(0) /* 19 - #XF */
QUAD(0) /* 20 - reserved */
QUAD(0) /* 21 - #CP */
QUAD(0) /* 22 - reserved */
QUAD(0) /* 23 - reserved */
QUAD(0) /* 24 - reserved */
QUAD(0) /* 25 - reserved */
QUAD(0) /* 26 - reserved */
QUAD(0) /* 27 - reserved */
QUAD(0) /* 28 - #HV */
QUAD( /* 29 - #VC */
(vc_handler & 0xFFFF0000) << 32 | /* target offset [31:16] */
DESCRIPTOR_PRESENT |
DESCRIPTOR_INTERRUPT_GATE |
cs32 << 16 | /* code segment selector */
vc_handler & 0xFFFF) /* target offset [15:0] */
QUAD(0) /* 30 - #SX */
QUAD(0) /* 31 - reserved */
} > bios
.rodata.gdt_desc ALIGN(8) : {
SHORT(SIZEOF(.rodata.gdt) - 1)
LONG(ADDR(.rodata.gdt))
} > bios
.rodata.idt_desc ALIGN(8) : {
SHORT(SIZEOF(.rodata.idt) - 1)
LONG(ADDR(.rodata.idt))
} > bios
gdt_desc_offset = ADDR(.rodata.gdt_desc) & 0xFFFF;
idt_desc_offset = ADDR(.rodata.idt_desc) & 0xFFFF;
/* GUIDed tables have to *end* at 0x20 from the end of the file.
* Documentation about the GUID table format can be found in QEMU docs:
* https://github.com/qemu/qemu/blob/master/docs/specs/sev-guest-firmware.rst
* and EDK2 source code:
* https://github.com/tianocore/edk2/blob/master/OvmfPkg/ResetVector/Ia16/ResetVectorVtf0.asm
*
* Unfortunately it doesn't look like `SIZEOF()` works here, so we have to keep track of the data
* structure size by hand to ensure that it ends at the correct location in memory.
*/
.guid_tables TOP - 0x20 - (16 + 3*12 + 2*22 + 18) : {
/* SEV metadata. This data structure is not part of the GUIDed tables, but it makes sense to have
* them together at the end of the file, thus we put them in the .guid_tables section along with
* the GUIDed tables themselves. The data structure is defined in the EDK2 source:
* https://github.com/tianocore/edk2/blob/master/OvmfPkg/ResetVector/X64/OvmfSevMetadata.asm
*
* Size: header is 16 bytes + a number of 12-byte sections.
*/
HIDDEN(sev_guided_structure_start = .);
/* Header: uint32 signature ("ASEV"), uint32 length, uint32 version, uint32 number of sections */
BYTE(0x41) BYTE(0x53) BYTE(0x45) BYTE(0x56)
LONG(sev_guided_structure_end - sev_guided_structure_start)
LONG(1)
LONG((sev_guided_structure_end - sev_guided_structure_start - 16) / 12)
/* Each section has the basic structure of uint32 address, uint32 length, uint32 type
* The locations of these pages have been chosen to be the same as EDK2:
* https://github.com/tianocore/edk2/blob/c05a218a9758225ddf94eedb365633f2154551da/OvmfPkg/OvmfPkgX64.fdf
* Thankfully none of these clashed with existing data structures that we're setting up.
*/
/* Unmeasured page location */
/* We mark the stack as unmeasured, as we need to support interrupts before we even know whether
* we're running under some form of SEV, and interrupts require a functioning stack.
*/
LONG(ADDR(.stack))
LONG(SIZEOF(.stack))
LONG(SEV_SECTION_UNMEASURED)
/* Secrets page location */
LONG(SEV_SECRETS)
LONG(0x1000)
LONG(SEV_SECTION_SECRET)
/* CPUID page location */
LONG(SEV_CPUID)
LONG(0x1000)
LONG(SEV_SECTION_CPUID)
HIDDEN(sev_guided_structure_end = .);
/*
* This is where the GUIDed tables actually start.
* Size: each entry is 22 bytes + a 18-byte footer.
*/
HIDDEN(tables_start = .);
/* SEV metadata descriptor: uint32 offset, uint16 size, 16-byte GUID */
HIDDEN(sev_metadata_offset_start = .);
LONG(TOP - sev_guided_structure_start)
SHORT(sev_metadata_offset_end - sev_metadata_offset_start)
LONG(0xdc886566)
SHORT(0x984a)
SHORT(0x4798)
SHORT(0x5ea7)
BYTE(0x55) BYTE(0x85) BYTE(0xa7) BYTE(0xbf) BYTE(0x67) BYTE(0xcc)
HIDDEN(sev_metadata_offset_end = .);
/* SEV-ES reset block: uint32 addr, uint16 size, 16-byte GUID */
HIDDEN(sev_es_reset_block_start = .);
LONG(sev_es_start)
SHORT(sev_es_reset_block_end - sev_es_reset_block_start)
LONG(0x00f771de)
SHORT(0x1a7e)
SHORT(0x4fcb)
SHORT(0x0e89)
BYTE(0x68) BYTE(0xc7) BYTE(0x7e) BYTE(0x2f) BYTE(0xb4) BYTE(0x4e)
HIDDEN(sev_es_reset_block_end = .);
/* Footer: uint16 size, 16-byte GUID */
SHORT(tables_end - tables_start)
LONG(0x96b582de)
SHORT(0x1fb2)
SHORT(0x45f7)
SHORT(0xeaba)
BYTE(0xa3) BYTE(0x66) BYTE(0xc5) BYTE(0x5a) BYTE(0x08) BYTE(0x2d)
HIDDEN(tables_end = .);
} > bios
ASSERT((. == TOP - 0x20), "GUID tables are to expected to end at top - 0x20")
/* The reset vector needs to be placed 0x10 from the end of the file. */
.reset_vector TOP - 0x10 : {
*(.reset_vector)
FILL(0x00)
. = TOP;
} > bios
/DISCARD/ : {
*(.eh_frame)
*(.comment*)
}
}