gem5  v22.0.0.1
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/regs/misc.hh"
33 #include "base/trace.hh"
34 #include "cpu/thread_context.hh"
35 #include "kern/linux/linux.hh"
37 #include "sim/process.hh"
38 #include "sim/syscall_desc.hh"
39 #include "sim/syscall_emul.hh"
40 
41 namespace gem5
42 {
43 
44 namespace X86ISA
45 {
46 
48 SyscallReturn
50 {
51  auto process = tc->getProcessPtr();
52 
53  strcpy(name->sysname, "Linux");
54  strcpy(name->nodename, "sim.gem5.org");
55  strcpy(name->release, process->release.c_str());
56  strcpy(name->version, "#1 Mon Aug 18 11:32:15 EDT 2003");
57  strcpy(name->machine, "x86_64");
58 
59  return 0;
60 }
61 
63 archPrctlFunc(SyscallDesc *desc, ThreadContext *tc, int code, uint64_t addr)
64 {
65  enum ArchPrctlCodes
66  {
67  SetFS = 0x1002,
68  GetFS = 0x1003,
69  SetGS = 0x1001,
70  GetGS = 0x1004
71  };
72 
73  uint64_t fsBase, gsBase;
75  switch(code)
76  {
77  // Each of these valid options should actually check addr.
78  case SetFS:
81  return 0;
82  case GetFS:
84  p.write(addr, fsBase);
85  return 0;
86  case SetGS:
89  return 0;
90  case GetGS:
92  p.write(addr, gsBase);
93  return 0;
94  default:
95  return -EINVAL;
96  }
97 }
98 
101  VPtr<UserDesc32> userDesc)
102 {
103  const int minTLSEntry = 6;
104  const int numTLSEntries = 3;
105  const int maxTLSEntry = minTLSEntry + numTLSEntries - 1;
106 
107  auto process = tc->getProcessPtr();
108  SETranslatingPortProxy proxy(tc);
109 
110  X86Process *x86p = dynamic_cast<X86Process *>(process);
111  assert(x86p);
112 
113  assert((maxTLSEntry + 1) * sizeof(uint64_t) <= x86p->gdtSize());
114 
116  gdt(x86p->gdtStart() + minTLSEntry * sizeof(uint64_t),
117  numTLSEntries * sizeof(uint64_t));
118 
119  if (!gdt.copyIn(proxy))
120  panic("Failed to copy in GDT for %s.\n", desc->name());
121 
122  if (userDesc->entry_number == (uint32_t)(-1)) {
123  // Find a free TLS entry.
124  for (int i = 0; i < numTLSEntries; i++) {
125  if (gdt[i] == 0) {
126  userDesc->entry_number = i + minTLSEntry;
127  break;
128  }
129  }
130  // We failed to find one.
131  if (userDesc->entry_number == (uint32_t)(-1))
132  return -ESRCH;
133  }
134 
135  int index = userDesc->entry_number;
136 
137  if (index < minTLSEntry || index > maxTLSEntry)
138  return -EINVAL;
139 
140  index -= minTLSEntry;
141 
142  // Build the entry we're going to add.
143  SegDescriptor segDesc = 0;
144  UserDescFlags flags = userDesc->flags;
145 
146  segDesc.limitLow = bits(userDesc->limit, 15, 0);
147  segDesc.baseLow = bits(userDesc->base_addr, 23, 0);
148  segDesc.type.a = 1;
149  if (!flags.read_exec_only)
150  segDesc.type.w = 1;
151  if (bits((uint8_t)flags.contents, 0))
152  segDesc.type.e = 1;
153  if (bits((uint8_t)flags.contents, 1))
154  segDesc.type.codeOrData = 1;
155  segDesc.s = 1;
156  segDesc.dpl = 3;
157  if (!flags.seg_not_present)
158  segDesc.p = 1;
159  segDesc.limitHigh = bits(userDesc->limit, 19, 16);
160  if (flags.useable)
161  segDesc.avl = 1;
162  segDesc.l = 0;
163  if (flags.seg_32bit)
164  segDesc.d = 1;
165  if (flags.limit_in_pages)
166  segDesc.g = 1;
167  segDesc.baseHigh = bits(userDesc->base_addr, 31, 24);
168 
169  gdt[index] = (uint64_t)segDesc;
170 
171  if (!gdt.copyOut(proxy))
172  panic("Failed to copy out GDT for %s.\n", desc->name());
173 
174  return 0;
175 }
176 
177 } // namespace X86ISA
178 } // namespace gem5
gem5::SETranslatingPortProxy
Definition: se_translating_port_proxy.hh:49
linux.hh
gem5::TypedBufferArg
TypedBufferArg is a class template; instances of this template represent typed buffers in target user...
Definition: syscall_emul_buf.hh:132
gem5::SyscallReturn
This class represents the return value from an emulated system call, including any errno setting.
Definition: syscall_return.hh:55
gem5::X86ISA::archPrctlFunc
SyscallReturn archPrctlFunc(SyscallDesc *desc, ThreadContext *tc, int code, uint64_t addr)
Definition: syscalls.cc:63
gem5::X86ISA::unameFunc
SyscallReturn unameFunc(SyscallDesc *desc, ThreadContext *tc, VPtr< Linux::utsname > name)
Target uname() handler.
Definition: syscalls.cc:49
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:67
gem5::X86ISA::X86Process::gdtSize
Addr gdtSize() const
Definition: process.hh:81
gem5::X86ISA::X86Process
Definition: process.hh:67
gem5::ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:94
gem5::X86ISA::misc_reg::GsBase
@ GsBase
Definition: misc.hh:326
process.hh
gem5::ProxyPtr
Definition: proxy_ptr.hh:238
gem5::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:76
flags
uint8_t flags
Definition: helpers.cc:66
gem5::ThreadContext::readMiscRegNoEffect
virtual RegVal readMiscRegNoEffect(RegIndex misc_reg) const =0
name
const std::string & name()
Definition: trace.cc:49
gem5::X86ISA::setThreadArea32Func
SyscallReturn setThreadArea32Func(SyscallDesc *desc, ThreadContext *tc, VPtr< UserDesc32 > userDesc)
Definition: syscalls.cc:100
gem5::BaseBufferArg::copyIn
bool copyIn(const PortProxy &memproxy)
copy data into simulator space (read from target memory)
Definition: syscall_emul_buf.hh:81
gem5::ThreadContext::getProcessPtr
virtual Process * getProcessPtr()=0
syscalls.hh
gem5::X86ISA::misc_reg::FsBase
@ FsBase
Definition: misc.hh:325
syscall_emul.hh
process.hh
gem5::X86ISA::index
Bitfield< 5, 3 > index
Definition: types.hh:98
gem5::BaseBufferArg::copyOut
bool copyOut(const PortProxy &memproxy)
copy data out of simulator space (write to target memory)
Definition: syscall_emul_buf.hh:91
gem5::X86ISA::misc_reg::FsEffBase
@ FsEffBase
Definition: misc.hh:343
linux.hh
gem5::X86ISA::p
Bitfield< 0 > p
Definition: pagetable.hh:151
gem5::X86ISA::X86Process::gdtStart
Addr gdtStart() const
Definition: process.hh:80
gem5::X86ISA::misc_reg::GsEffBase
@ GsEffBase
Definition: misc.hh:344
se_translating_port_proxy.hh
trace.hh
gem5::SyscallDesc
This class provides the wrapper interface for the system call implementations which are defined in th...
Definition: syscall_desc.hh:69
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::SyscallDesc::name
std::string name() const
Definition: syscall_desc.hh:80
misc.hh
thread_context.hh
syscall_desc.hh
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
gem5::X86ISA::addr
Bitfield< 3 > addr
Definition: types.hh:84
gem5::ThreadContext::setMiscRegNoEffect
virtual void setMiscRegNoEffect(RegIndex misc_reg, RegVal val)=0

Generated on Sat Jun 18 2022 08:12:17 for gem5 by doxygen 1.8.17