gem5  v21.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
syscalls.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Google Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met: redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer;
8  * redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution;
11  * neither the name of the copyright holders nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
29 
30 #include "arch/x86/linux/linux.hh"
31 #include "arch/x86/process.hh"
32 #include "arch/x86/registers.hh"
33 #include "base/trace.hh"
34 #include "cpu/thread_context.hh"
35 #include "kern/linux/linux.hh"
36 #include "sim/process.hh"
37 #include "sim/syscall_desc.hh"
38 #include "sim/syscall_emul.hh"
39 
40 namespace X86ISA
41 {
42 
46 {
47  auto process = tc->getProcessPtr();
48 
49  strcpy(name->sysname, "Linux");
50  strcpy(name->nodename, "sim.gem5.org");
51  strcpy(name->release, process->release.c_str());
52  strcpy(name->version, "#1 Mon Aug 18 11:32:15 EDT 2003");
53  strcpy(name->machine, "x86_64");
54 
55  return 0;
56 }
57 
59 archPrctlFunc(SyscallDesc *desc, ThreadContext *tc, int code, uint64_t addr)
60 {
61  enum ArchPrctlCodes
62  {
63  SetFS = 0x1002,
64  GetFS = 0x1003,
65  SetGS = 0x1001,
66  GetGS = 0x1004
67  };
68 
69  uint64_t fsBase, gsBase;
70  PortProxy &p = tc->getVirtProxy();
71  switch(code)
72  {
73  // Each of these valid options should actually check addr.
74  case SetFS:
77  return 0;
78  case GetFS:
80  p.write(addr, fsBase);
81  return 0;
82  case SetGS:
85  return 0;
86  case GetGS:
88  p.write(addr, gsBase);
89  return 0;
90  default:
91  return -EINVAL;
92  }
93 }
94 
97  VPtr<UserDesc32> userDesc)
98 {
99  const int minTLSEntry = 6;
100  const int numTLSEntries = 3;
101  const int maxTLSEntry = minTLSEntry + numTLSEntries - 1;
102 
103  auto process = tc->getProcessPtr();
104 
105  X86Process *x86p = dynamic_cast<X86Process *>(process);
106  assert(x86p);
107 
108  assert((maxTLSEntry + 1) * sizeof(uint64_t) <= x86p->gdtSize());
109 
111  gdt(x86p->gdtStart() + minTLSEntry * sizeof(uint64_t),
112  numTLSEntries * sizeof(uint64_t));
113 
114  if (!gdt.copyIn(tc->getVirtProxy()))
115  panic("Failed to copy in GDT for %s.\n", desc->name());
116 
117  if (userDesc->entry_number == (uint32_t)(-1)) {
118  // Find a free TLS entry.
119  for (int i = 0; i < numTLSEntries; i++) {
120  if (gdt[i] == 0) {
121  userDesc->entry_number = i + minTLSEntry;
122  break;
123  }
124  }
125  // We failed to find one.
126  if (userDesc->entry_number == (uint32_t)(-1))
127  return -ESRCH;
128  }
129 
130  int index = userDesc->entry_number;
131 
132  if (index < minTLSEntry || index > maxTLSEntry)
133  return -EINVAL;
134 
135  index -= minTLSEntry;
136 
137  // Build the entry we're going to add.
138  SegDescriptor segDesc = 0;
139  UserDescFlags flags = userDesc->flags;
140 
141  segDesc.limitLow = bits(userDesc->limit, 15, 0);
142  segDesc.baseLow = bits(userDesc->base_addr, 23, 0);
143  segDesc.type.a = 1;
144  if (!flags.read_exec_only)
145  segDesc.type.w = 1;
146  if (bits((uint8_t)flags.contents, 0))
147  segDesc.type.e = 1;
148  if (bits((uint8_t)flags.contents, 1))
149  segDesc.type.codeOrData = 1;
150  segDesc.s = 1;
151  segDesc.dpl = 3;
152  if (!flags.seg_not_present)
153  segDesc.p = 1;
154  segDesc.limitHigh = bits(userDesc->limit, 19, 16);
155  if (flags.useable)
156  segDesc.avl = 1;
157  segDesc.l = 0;
158  if (flags.seg_32bit)
159  segDesc.d = 1;
160  if (flags.limit_in_pages)
161  segDesc.g = 1;
162  segDesc.baseHigh = bits(userDesc->base_addr, 31, 24);
163 
164  gdt[index] = (uint64_t)segDesc;
165 
166  if (!gdt.copyOut(tc->getVirtProxy()))
167  panic("Failed to copy out GDT for %s.\n", desc->name());
168 
169  return 0;
170 }
171 
172 } // namespace X86ISA
ThreadContext::readMiscRegNoEffect
virtual RegVal readMiscRegNoEffect(RegIndex misc_reg) const =0
X86ISA::X86Process
Definition: process.hh:58
linux.hh
X86ISA::MISCREG_GS_BASE
@ MISCREG_GS_BASE
Definition: misc.hh:317
registers.hh
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
X86ISA::X86Process::gdtSize
Addr gdtSize() const
Definition: process.hh:72
ProxyPtr
Definition: proxy_ptr.hh:235
ThreadContext::getProcessPtr
virtual Process * getProcessPtr()=0
X86ISA::unameFunc
SyscallReturn unameFunc(SyscallDesc *desc, ThreadContext *tc, VPtr< Linux::utsname > name)
Target uname() handler.
Definition: syscalls.cc:45
X86ISA::setThreadArea32Func
SyscallReturn setThreadArea32Func(SyscallDesc *desc, ThreadContext *tc, VPtr< UserDesc32 > userDesc)
Definition: syscalls.cc:96
X86ISA::index
Bitfield< 5, 3 > index
Definition: types.hh:94
ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:88
SyscallDesc::name
std::string name() const
Definition: syscall_desc.hh:76
X86ISA::MISCREG_GS_EFF_BASE
@ MISCREG_GS_EFF_BASE
Definition: misc.hh:335
process.hh
X86ISA
This is exposed globally, independent of the ISA.
Definition: acpi.hh:55
SyscallReturn
This class represents the return value from an emulated system call, including any errno setting.
Definition: syscall_return.hh:52
name
const std::string & name()
Definition: trace.cc:48
BaseBufferArg::copyIn
bool copyIn(PortProxy &memproxy)
copy data into simulator space (read from target memory)
Definition: syscall_emul_buf.hh:77
X86ISA::addr
Bitfield< 3 > addr
Definition: types.hh:80
ThreadContext::getVirtProxy
virtual PortProxy & getVirtProxy()=0
syscalls.hh
syscall_emul.hh
PortProxy
This object is a proxy for a port or other object which implements the functional response protocol,...
Definition: port_proxy.hh:80
process.hh
X86ISA::p
Bitfield< 0 > p
Definition: pagetable.hh:148
linux.hh
X86ISA::MISCREG_FS_EFF_BASE
@ MISCREG_FS_EFF_BASE
Definition: misc.hh:334
bits
constexpr T bits(T val, unsigned first, unsigned last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.
Definition: bitfield.hh:73
TypedBufferArg
TypedBufferArg is a class template; instances of this template represent typed buffers in target user...
Definition: syscall_emul_buf.hh:128
trace.hh
ThreadContext::setMiscRegNoEffect
virtual void setMiscRegNoEffect(RegIndex misc_reg, RegVal val)=0
X86ISA::X86Process::gdtStart
Addr gdtStart() const
Definition: process.hh:71
X86ISA::MISCREG_FS_BASE
@ MISCREG_FS_BASE
Definition: misc.hh:316
thread_context.hh
SyscallDesc
This class provides the wrapper interface for the system call implementations which are defined in th...
Definition: syscall_desc.hh:66
X86ISA::archPrctlFunc
SyscallReturn archPrctlFunc(SyscallDesc *desc, ThreadContext *tc, int code, uint64_t addr)
Definition: syscalls.cc:59
syscall_desc.hh
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171
BaseBufferArg::copyOut
bool copyOut(PortProxy &memproxy)
copy data out of simulator space (write to target memory)
Definition: syscall_emul_buf.hh:87

Generated on Tue Mar 23 2021 19:41:23 for gem5 by doxygen 1.8.17