gem5 [DEVELOP-FOR-25.1]
Loading...
Searching...
No Matches
fs_workload.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 The Hewlett-Packard Development Company
3 * Copyright (c) 2018 TU Dresden
4 * All rights reserved.
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are
17 * met: redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer;
19 * redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution;
22 * neither the name of the copyright holders nor the names of its
23 * contributors may be used to endorse or promote products derived from
24 * this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
40
41#include "arch/x86/bios/acpi.hh"
44#include "arch/x86/faults.hh"
46#include "base/trace.hh"
47#include "cpu/pc_event.hh"
48#include "cpu/thread_context.hh"
49#include "debug/ACPI.hh"
50#include "kern/linux/events.hh"
51#include "params/X86FsWorkload.hh"
52#include "sim/sim_exit.hh"
53#include "sim/system.hh"
54
55namespace gem5
56{
57
58namespace X86ISA
59{
60
62 smbiosTable(p.smbios_table),
63 mpFloatingPointer(p.intel_mp_pointer),
64 mpConfigTable(p.intel_mp_table),
65 rsdp(p.acpi_description_table_pointer),
67{}
68
69void
70installSegDesc(ThreadContext *tc, int seg, SegDescriptor desc, bool longmode)
71{
72 bool honorBase = !longmode || seg == segment_idx::Fs ||
74
75 SegAttr attr = 0;
76
77 attr.dpl = desc.dpl;
78 attr.unusable = 0;
79 attr.defaultSize = desc.d;
80 attr.longMode = desc.l;
81 attr.avl = desc.avl;
82 attr.granularity = desc.g;
83 attr.present = desc.p;
84 attr.system = desc.s;
85 attr.type = desc.type;
86 if (desc.s) {
87 if (desc.type.codeOrData) {
88 // Code segment
89 attr.expandDown = 0;
90 attr.readable = desc.type.r;
91 attr.writable = 0;
92 } else {
93 // Data segment
94 attr.expandDown = desc.type.e;
95 attr.readable = 1;
96 attr.writable = desc.type.w;
97 }
98 } else {
99 attr.readable = 1;
100 attr.writable = 1;
101 attr.expandDown = 0;
102 }
103
104 tc->setMiscReg(misc_reg::segBase(seg), desc.base);
105 tc->setMiscReg(misc_reg::segEffBase(seg), honorBase ? desc.base : 0);
106 tc->setMiscReg(misc_reg::segLimit(seg), desc.limit);
108}
109
110void
117
118void
120{
121 const std::string dmesg_output = name() + ".dmesg";
122 if (params().exit_on_kernel_panic) {
123 // This was taken from the RISCV implementation. Some kernels may not
124 // have kernel symbols, causing `kernelSymtab` to be empty.
125 // In that case, addKernelFuncEvent tries to access the "panic" symbol
126 // in the symbol table but can't, so the event won't be added and the
127 // simulation will hang upon kernel panic.
129 "panic", "Kernel panic in simulated system.",
130 dmesg_output, params().on_panic
131 );
132 warn_if(!kernelPanicPcEvent, "Failed to find kernel symbol 'panic'");
133 }
134}
135
136void
138{
139 const std::string dmesg_output = name() + ".dmesg";
140 // This was taken from the RISCV implementation. Some kernels may not
141 // have kernel symbols, causing `kernelSymtab` to be empty.
142 // In that case, addKernelFuncEvent tries to access the "oops_exit" symbol
143 // in the symbol table but can't, so the event won't be added and the
144 // simulation will continue upon kernel oops.
145 if (params().exit_on_kernel_oops) {
147 "oops_exit", "Kernel oops in simulated system.",
148 dmesg_output, params().on_oops
149 );
151 "Failed to find kernel symbol 'oops_exit'");
152 }
153
154}
155
156
157void
159{
161
162 for (auto *tc: system->threads) {
164
165 if (tc->contextId() == 0) {
166 tc->activate();
167 } else {
168 // This is an application processor (AP). It should be initialized
169 // to look like only the BIOS POST has run on it and put then put
170 // it into a halted state.
171 tc->suspend();
172 }
173 }
174
175 fatal_if(!kernelObj, "No kernel to load.");
176
177 fatal_if(kernelObj->getArch() == loader::I386,
178 "Loading a 32 bit x86 kernel is not supported.");
179
180 ThreadContext *tc = system->threads[0];
181 auto phys_proxy = system->physProxy;
182
183 // This is the boot strap processor (BSP). Initialize it to look like
184 // the boot loader has just turned control over to the 64 bit OS. We
185 // won't actually set up real mode or legacy protected mode descriptor
186 // tables because we aren't executing any code that would require
187 // them. We do, however toggle the control bits in the correct order
188 // while allowing consistency checks and the underlying mechansims
189 // just to be safe.
190
191 const int NumPDTs = 4;
192
193 const Addr PageMapLevel4 = 0x70000;
194 const Addr PageDirPtrTable = 0x71000;
195 const Addr PageDirTable[NumPDTs] =
196 {0x72000, 0x73000, 0x74000, 0x75000};
197 const Addr GDTBase = 0x76000;
198
199 const int PML4Bits = 9;
200 const int PDPTBits = 9;
201 const int PDTBits = 9;
202
203 /*
204 * Set up the gdt.
205 */
206 uint8_t numGDTEntries = 0;
207 // Place holder at selector 0
208 uint64_t nullDescriptor = 0;
209 phys_proxy.writeBlob(GDTBase + numGDTEntries * 8, &nullDescriptor, 8);
210 numGDTEntries++;
211
212 SegDescriptor initDesc = 0;
213 initDesc.type.codeOrData = 0; // code or data type
214 initDesc.type.c = 0; // conforming
215 initDesc.type.r = 1; // readable
216 initDesc.dpl = 0; // privilege
217 initDesc.p = 1; // present
218 initDesc.l = 1; // longmode - 64 bit
219 initDesc.d = 0; // operand size
220 initDesc.g = 1; // granularity
221 initDesc.s = 1; // system segment
222 initDesc.limit = 0xFFFFFFFF;
223 initDesc.base = 0;
224
225 // 64 bit code segment
226 SegDescriptor csDesc = initDesc;
227 csDesc.type.codeOrData = 1;
228 csDesc.dpl = 0;
229 // Because we're dealing with a pointer and I don't think it's
230 // guaranteed that there isn't anything in a nonvirtual class between
231 // it's beginning in memory and it's actual data, we'll use an
232 // intermediary.
233 uint64_t csDescVal = csDesc;
234 phys_proxy.writeBlob(GDTBase + numGDTEntries * 8, (&csDescVal), 8);
235
236 numGDTEntries++;
237
238 SegSelector cs = 0;
239 cs.si = numGDTEntries - 1;
240
242
243 // 32 bit data segment
244 SegDescriptor dsDesc = initDesc;
245 dsDesc.type.e = 0;
246 dsDesc.type.w = 1;
247 dsDesc.d = 1;
248 dsDesc.baseHigh = 0;
249 dsDesc.baseLow = 0;
250
251 uint64_t dsDescVal = dsDesc;
252 phys_proxy.writeBlob(GDTBase + numGDTEntries * 8, (&dsDescVal), 8);
253
254 numGDTEntries++;
255
256 SegSelector ds = 0;
257 ds.si = numGDTEntries - 1;
258
264
266 SegAttr ldtAttr = 0;
267 ldtAttr.unusable = 1;
268 tc->setMiscReg(misc_reg::TslAttr, ldtAttr);
269 tc->setMiscReg(misc_reg::TsgBase, GDTBase);
270 tc->setMiscReg(misc_reg::TsgLimit, 8 * numGDTEntries - 1);
271
272 SegDescriptor tssDesc = initDesc;
273 tssDesc.type = 0xB;
274 tssDesc.s = 0;
275
276 uint64_t tssDescVal = tssDesc;
277 phys_proxy.writeBlob(GDTBase + numGDTEntries * 8, (&tssDescVal), 8);
278
279 numGDTEntries++;
280
281 SegSelector tss = 0;
282 tss.si = numGDTEntries - 1;
283
284 tc->setMiscReg(misc_reg::Tr, (RegVal)tss);
285 installSegDesc(tc, segment_idx::Tr, tssDesc, true);
286
287 /*
288 * Identity map the first 4GB of memory. In order to map this region
289 * of memory in long mode, there needs to be one actual page map level
290 * 4 entry which points to one page directory pointer table which
291 * points to 4 different page directory tables which are full of two
292 * megabyte pages. All of the other entries in valid tables are set
293 * to indicate that they don't pertain to anything valid and will
294 * cause a fault if used.
295 */
296
297 // Put valid values in all of the various table entries which indicate
298 // that those entries don't point to further tables or pages. Then
299 // set the values of those entries which are needed.
300
301 // Page Map Level 4
302
303 // read/write, user, not present
304 uint64_t pml4e = htole<uint64_t>(0x6);
305 for (int offset = 0; offset < (1 << PML4Bits) * 8; offset += 8)
306 phys_proxy.writeBlob(PageMapLevel4 + offset, (&pml4e), 8);
307 // Point to the only PDPT
308 pml4e = htole<uint64_t>(0x7 | PageDirPtrTable);
309 phys_proxy.writeBlob(PageMapLevel4, (&pml4e), 8);
310
311 // Page Directory Pointer Table
312
313 // read/write, user, not present
314 uint64_t pdpe = htole<uint64_t>(0x6);
315 for (int offset = 0; offset < (1 << PDPTBits) * 8; offset += 8)
316 phys_proxy.writeBlob(PageDirPtrTable + offset, &pdpe, 8);
317 // Point to the PDTs
318 for (int table = 0; table < NumPDTs; table++) {
319 pdpe = htole<uint64_t>(0x7 | PageDirTable[table]);
320 phys_proxy.writeBlob(PageDirPtrTable + table * 8, &pdpe, 8);
321 }
322
323 // Page Directory Tables
324
325 Addr base = 0;
326 const Addr pageSize = 2 << 20;
327 for (int table = 0; table < NumPDTs; table++) {
328 for (int offset = 0; offset < (1 << PDTBits) * 8; offset += 8) {
329 // read/write, user, present, 4MB
330 uint64_t pdte = htole(0x87 | base);
331 phys_proxy.writeBlob(PageDirTable[table] + offset, &pdte, 8);
332 base += pageSize;
333 }
334 }
335
336 /*
337 * Transition from real mode all the way up to Long mode
338 */
339 CR0 cr0 = tc->readMiscRegNoEffect(misc_reg::Cr0);
340 // Turn off paging.
341 cr0.pg = 0;
342 tc->setMiscReg(misc_reg::Cr0, cr0);
343 // Turn on protected mode.
344 cr0.pe = 1;
345 tc->setMiscReg(misc_reg::Cr0, cr0);
346
347 CR4 cr4 = tc->readMiscRegNoEffect(misc_reg::Cr4);
348 // Turn on pae.
349 cr4.pae = 1;
350 cr4.osxsave = enable_osxsave;
351 tc->setMiscReg(misc_reg::Cr4, cr4);
352
353 // Point to the page tables.
354 tc->setMiscReg(misc_reg::Cr3, PageMapLevel4);
355
356 // Only used if cr4.osxsave is set
357 XCR0 xcr0 = tc->readMiscRegNoEffect(misc_reg::Xcr0);
358 xcr0.x87 = 1; // Must be 1 according to x86 specification
359 tc->setMiscReg(misc_reg::Xcr0, xcr0);
360
361 Efer efer = tc->readMiscRegNoEffect(misc_reg::Efer);
362 // Enable long mode.
363 efer.lme = 1;
364 tc->setMiscReg(misc_reg::Efer, efer);
365
366 // Start using longmode segments.
367 installSegDesc(tc, segment_idx::Cs, csDesc, true);
368 installSegDesc(tc, segment_idx::Ds, dsDesc, true);
369 installSegDesc(tc, segment_idx::Es, dsDesc, true);
370 installSegDesc(tc, segment_idx::Fs, dsDesc, true);
371 installSegDesc(tc, segment_idx::Gs, dsDesc, true);
372 installSegDesc(tc, segment_idx::Ss, dsDesc, true);
373
374 // Activate long mode.
375 cr0.pg = 1;
376 tc->setMiscReg(misc_reg::Cr0, cr0);
377
378 tc->pcState(kernelObj->entryPoint());
379
380 // We should now be in long mode. Yay!
381
382 Addr ebdaPos = 0xF0000;
383 Addr fixed, table;
384
385 // Write out the SMBios/DMI table.
386 writeOutSMBiosTable(ebdaPos, fixed, table);
387 ebdaPos += (fixed + table);
388 ebdaPos = roundUp(ebdaPos, 16);
389
390 // Write out the Intel MP Specification configuration table.
391 writeOutMPTable(ebdaPos, fixed, table);
392 ebdaPos += (fixed + table);
393
394 // Write out ACPI tables
395 writeOutACPITables(ebdaPos, table);
396 ebdaPos += table;
397}
398
399void
401 Addr &headerSize, Addr &structSize, Addr table)
402{
403 // If the table location isn't specified, just put it after the header.
404 // The header size as of the 2.5 SMBios specification is 0x1F bytes.
405 if (!table)
406 table = header + 0x1F;
407 smbiosTable->setTableAddr(table);
408
409 smbiosTable->writeOut(system->physProxy, header, headerSize, structSize);
410
411 // Do some bounds checking to make sure we at least didn't step on
412 // ourselves.
413 assert(header > table || header + headerSize <= table);
414 assert(table > header || table + structSize <= header);
415}
416
417void
418FsWorkload::writeOutMPTable(Addr fp, Addr &fpSize, Addr &tableSize, Addr table)
419{
420 // If the table location isn't specified and it exists, just put
421 // it after the floating pointer. The fp size as of the 1.4 Intel MP
422 // specification is 0x10 bytes.
423 if (mpConfigTable) {
424 if (!table)
425 table = fp + 0x10;
426 mpFloatingPointer->setTableAddr(table);
427 }
428
429 fpSize = mpFloatingPointer->writeOut(system->physProxy, fp);
430 if (mpConfigTable)
431 tableSize = mpConfigTable->writeOut(system->physProxy, table);
432 else
433 tableSize = 0;
434
435 // Do some bounds checking to make sure we at least didn't step on
436 // ourselves and the fp structure was the size we thought it was.
437 assert(fp > table || fp + fpSize <= table);
438 assert(table > fp || table + tableSize <= fp);
439 assert(fpSize == 0x10);
440}
441
442void
444{
445 fpSize = 0;
446 if (rsdp) {
447 ACPI::LinearAllocator alloc(fp, 0x000FFFFF);
448 rsdp->write(system->physProxy, alloc);
449 fpSize = alloc.alloc(0, 0) - fp;
450 DPRINTF(ACPI, "Wrote ACPI tables to memory at %llx with size %llx.\n",
451 fp, fpSize);
452 }
453}
454
455} // namespace X86ISA
456} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:209
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
loader::ObjectFile * kernelObj
T * addKernelFuncEvent(const char *lbl, Args... args)
Add a function-based event to a kernel symbol.
KernelWorkload(const Params &p)
virtual std::string name() const
Definition named.hh:60
SimObjectParams Params
ThreadContext is the external interface to all thread state for anything outside of the CPU.
virtual void setMiscReg(RegIndex misc_reg, RegVal val)=0
virtual const PCStateBase & pcState() const =0
virtual RegVal readMiscRegNoEffect(RegIndex misc_reg) const =0
void writeOutACPITables(Addr begin, Addr &size)
void writeOutSMBiosTable(Addr header, Addr &headerSize, Addr &tableSize, Addr table=0)
smbios::SMBiosTable * smbiosTable
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
FsWorkload(const Params &p)
void startup() override
startup() is the final initialization call before simulation.
intelmp::FloatingPointer * mpFloatingPointer
intelmp::ConfigTable * mpConfigTable
void writeOutMPTable(Addr fp, Addr &fpSize, Addr &tableSize, Addr table=0)
void invoke(ThreadContext *tc, const StaticInstPtr &inst=nullStaticInstPtr) override
Definition faults.cc:183
static constexpr T roundUp(const T &val, const U &align)
This function is used to align addresses in memory.
Definition intmath.hh:260
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition logging.hh:268
const Params & params() const
virtual void startup()
startup() is the final initialization call before simulation.
Definition sim_object.cc:96
#define warn_if(cond,...)
Conditional warning macro that checks the supplied condition and only prints a warning if the conditi...
Definition logging.hh:315
Bitfield< 19, 16 > fp
Bitfield< 15, 13 > ds
static RegIndex segAttr(int index)
Definition misc.hh:543
static RegIndex segBase(int index)
Definition misc.hh:522
static RegIndex segLimit(int index)
Definition misc.hh:536
static RegIndex segEffBase(int index)
Definition misc.hh:529
This is exposed globally, independent of the ISA.
Definition tlb.cc:65
Bitfield< 15 > system
Definition misc.hh:1032
Bitfield< 51, 12 > base
Definition pagetable.hh:141
Bitfield< 2, 0 > seg
Definition types.hh:87
void installSegDesc(ThreadContext *tc, int seg, SegDescriptor desc, bool longmode)
Bitfield< 0 > p
Definition pagetable.hh:151
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
uint64_t RegVal
Definition types.hh:173
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
T htole(T value)
Definition byteswap.hh:172
output header
Definition nop.cc:36
Addr alloc(std::size_t size, unsigned align) override
Definition acpi.cc:91

Generated on Mon Oct 27 2025 04:12:59 for gem5 by doxygen 1.14.0