gem5  v21.0.1.0
se_workload.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2007 The Hewlett-Packard Development Company
3  *
4  * The license below extends only to copyright in the software and shall
5  * not be construed as granting a license to any other intellectual
6  * property including but not limited to intellectual property relating
7  * to a hardware implementation of the functionality of the software
8  * licensed hereunder. You may use the software subject to the license
9  * terms below provided that you ensure that this notice is replicated
10  * unmodified and in its entirety in all distributions of the software,
11  * modified or unmodified, in source code or in binary form.
12  *
13  * Copyright 2020 Google Inc.
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 <sys/syscall.h>
42 
43 #include "arch/x86/isa_traits.hh"
44 #include "arch/x86/linux/linux.hh"
45 #include "arch/x86/process.hh"
46 #include "arch/x86/registers.hh"
47 #include "arch/x86/se_workload.hh"
48 #include "base/trace.hh"
49 #include "cpu/thread_context.hh"
50 #include "kern/linux/linux.hh"
51 #include "sim/process.hh"
52 #include "sim/syscall_desc.hh"
53 #include "sim/syscall_emul.hh"
54 
55 namespace
56 {
57 
58 class LinuxLoader : public Process::Loader
59 {
60  public:
61  Process *
62  load(const ProcessParams &params, ::Loader::ObjectFile *obj_file)
63  {
64  auto arch = obj_file->getArch();
65  auto opsys = obj_file->getOpSys();
66 
67  if (arch != ::Loader::X86_64 && arch != ::Loader::I386)
68  return nullptr;
69 
70  if (opsys == ::Loader::UnknownOpSys) {
71  warn("Unknown operating system; assuming Linux.");
72  opsys = ::Loader::Linux;
73  }
74 
75  if (opsys != ::Loader::Linux)
76  return nullptr;
77 
78  if (arch == ::Loader::X86_64)
79  return new X86ISA::X86_64Process(params, obj_file);
80  else
81  return new X86ISA::I386Process(params, obj_file);
82  }
83 };
84 
85 LinuxLoader loader;
86 
87 } // anonymous namespace
88 
89 namespace X86ISA
90 {
91 
93 {}
94 
96  INTREG_RDI, INTREG_RSI, INTREG_RDX, INTREG_R10W, INTREG_R8W, INTREG_R9W
97 };
98 
100  INTREG_EBX, INTREG_ECX, INTREG_EDX, INTREG_ESI, INTREG_EDI, INTREG_EBP
101 };
102 
103 void
105 {
106  Process *process = tc->getProcessPtr();
107  // Call the syscall function in the base Process class to update stats.
108  // This will move into the base SEWorkload function at some point.
109  process->Process::syscall(tc);
110 
111  RegVal rax = tc->readIntReg(INTREG_RAX);
112  if (dynamic_cast<X86_64Process *>(process)) {
113  syscallDescs64.get(rax)->doSyscall(tc);
114  } else if (auto *proc32 = dynamic_cast<I386Process *>(process)) {
115  PCState pc = tc->pcState();
116  Addr eip = pc.pc();
117  const auto &vsyscall = proc32->getVSyscallPage();
118  if (eip >= vsyscall.base && eip < vsyscall.base + vsyscall.size) {
119  pc.npc(vsyscall.base + vsyscall.vsysexitOffset);
120  tc->pcState(pc);
121  }
122  syscallDescs32.get(rax)->doSyscall(tc);
123  } else {
124  panic("Unrecognized process type.");
125  }
126 }
127 
128 void
130 {
131  Process *process = tc->getProcessPtr();
132  auto pcState = tc->pcState();
133 
134  if (process->kvmInSE) {
135  Addr pc_page = mbits(pcState.pc(), 63, 12);
136  if (pc_page == syscallCodeVirtAddr) {
137  syscall(tc);
138  return;
139  } else if (pc_page == PFHandlerVirtAddr) {
140  pageFault(tc);
141  return;
142  }
143  }
144  warn("Unexpected workload event at pc %#x.", pcState.pc());
145 }
146 
147 void
149 {
150  Process *p = tc->getProcessPtr();
151  if (!p->fixupFault(tc->readMiscReg(MISCREG_CR2))) {
152  PortProxy &proxy = tc->getVirtProxy();
153  // at this point we should have 6 values on the interrupt stack
154  int size = 6;
155  uint64_t is[size];
156  // reading the interrupt handler stack
157  proxy.readBlob(ISTVirtAddr + PageBytes - size * sizeof(uint64_t),
158  &is, sizeof(is));
159  panic("Page fault at addr %#x\n\tInterrupt handler stack:\n"
160  "\tss: %#x\n"
161  "\trsp: %#x\n"
162  "\trflags: %#x\n"
163  "\tcs: %#x\n"
164  "\trip: %#x\n"
165  "\terr_code: %#x\n",
167  is[5], is[4], is[3], is[2], is[1], is[0]);
168  }
169 }
170 
171 } // namespace X86ISA
X86ISA::EmuLinux::syscall
void syscall(ThreadContext *tc) override
Definition: se_workload.cc:104
Loader::Linux
@ Linux
Definition: object_file.hh:64
Process::Loader
Each instance of a Loader subclass will have a chance to try to load an object file when tryLoaders i...
Definition: process.hh:180
warn
#define warn(...)
Definition: logging.hh:239
linux.hh
Process::kvmInSE
bool kvmInSE
Definition: process.hh:165
SEWorkload
Definition: se_workload.hh:34
se_workload.hh
registers.hh
X86ISA::EmuLinux::SyscallABI64::ArgumentRegs
static const std::vector< IntRegIndex > ArgumentRegs
Definition: se_workload.hh:69
X86ISA::EmuLinux::Params
X86EmuLinuxParams Params
Definition: se_workload.hh:55
Process
Definition: process.hh:65
X86ISA::EmuLinux::event
void event(ThreadContext *tc) override
Definition: se_workload.cc:129
X86ISA::MISCREG_CR2
@ MISCREG_CR2
Definition: misc.hh:107
std::vector< IntRegIndex >
ThreadContext::getProcessPtr
virtual Process * getProcessPtr()=0
MipsISA::is
Bitfield< 24, 22 > is
Definition: pra_constants.hh:232
Loader::ObjectFile
Definition: object_file.hh:74
X86ISA::X86_64Process
Definition: process.hh:90
ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:88
X86ISA::I386Process
Definition: process.hh:128
X86ISA::EmuLinux::EmuLinux
EmuLinux(const Params &p)
Definition: se_workload.cc:92
X86ISA::EmuLinux::SyscallABI32::ArgumentRegs
static const std::vector< IntRegIndex > ArgumentRegs
Definition: se_workload.hh:75
process.hh
X86ISA::PageBytes
const Addr PageBytes
Definition: isa_traits.hh:49
Loader::UnknownOpSys
@ UnknownOpSys
Definition: object_file.hh:62
isa_traits.hh
X86ISA::syscallCodeVirtAddr
const Addr syscallCodeVirtAddr
Definition: se_workload.hh:37
X86ISA::PCState
Definition: types.hh:286
Loader::I386
@ I386
Definition: object_file.hh:50
X86ISA::EmuLinux::pageFault
void pageFault(ThreadContext *tc)
Definition: se_workload.cc:148
mbits
constexpr T mbits(T val, unsigned first, unsigned last)
Mask off the given bits in place like bits() but without shifting.
Definition: bitfield.hh:100
X86ISA
This is exposed globally, independent of the ISA.
Definition: acpi.hh:55
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:148
X86ISA::PFHandlerVirtAddr
const Addr PFHandlerVirtAddr
Definition: se_workload.hh:43
se_workload.hh
ThreadContext::pcState
virtual TheISA::PCState pcState() const =0
ThreadContext::getVirtProxy
virtual PortProxy & getVirtProxy()=0
X86ISA::EmuLinux::syscallDescs32
static SyscallDescTable< SyscallABI32 > syscallDescs32
Definition: se_workload.hh:80
syscall_emul.hh
Loader::ObjectFile::getArch
Arch getArch() const
Definition: object_file.hh:103
PortProxy
This object is a proxy for a port or other object which implements the functional response protocol,...
Definition: port_proxy.hh:80
Loader::X86_64
@ X86_64
Definition: object_file.hh:49
process.hh
X86ISA::p
Bitfield< 0 > p
Definition: pagetable.hh:148
X86ISA::EmuLinux::syscallDescs64
static SyscallDescTable< SyscallABI64 > syscallDescs64
Definition: se_workload.hh:79
Process::Loader::load
virtual Process * load(const ProcessParams &params, ::Loader::ObjectFile *obj_file)=0
Each subclass needs to implement this method.
ThreadContext::readMiscReg
virtual RegVal readMiscReg(RegIndex misc_reg)=0
linux.hh
Loader::ObjectFile::getOpSys
OpSys getOpSys() const
Definition: object_file.hh:104
X86ISA::ISTVirtAddr
const Addr ISTVirtAddr
Definition: se_workload.hh:42
trace.hh
ThreadContext::readIntReg
virtual RegVal readIntReg(RegIndex reg_idx) const =0
X86ISA::pc
Bitfield< 19 > pc
Definition: misc.hh:805
PortProxy::readBlob
void readBlob(Addr addr, void *p, int size) const
Higher level interfaces based on the above.
Definition: port_proxy.hh:177
thread_context.hh
RegVal
uint64_t RegVal
Definition: types.hh:174
syscall_desc.hh
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171

Generated on Tue Jun 22 2021 15:28:20 for gem5 by doxygen 1.8.17