gem5  v22.1.0.0
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
bool copyIn(const PortProxy &memproxy)
copy data into simulator space (read from target memory)
bool copyOut(const PortProxy &memproxy)
copy data out of simulator space (write to target memory)
This class provides the wrapper interface for the system call implementations which are defined in th...
Definition: syscall_desc.hh:70
std::string name() const
Definition: syscall_desc.hh:80
This class represents the return value from an emulated system call, including any errno setting.
ThreadContext is the external interface to all thread state for anything outside of the CPU.
virtual void setMiscRegNoEffect(RegIndex misc_reg, RegVal val)=0
virtual RegVal readMiscRegNoEffect(RegIndex misc_reg) const =0
virtual Process * getProcessPtr()=0
TypedBufferArg is a class template; instances of this template represent typed buffers in target user...
Addr gdtStart() const
Definition: process.hh:80
Addr gdtSize() const
Definition: process.hh:81
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
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
uint8_t flags
Definition: helpers.cc:66
Bitfield< 7 > i
Definition: misc_types.hh:67
SyscallReturn unameFunc(SyscallDesc *desc, ThreadContext *tc, VPtr< Linux::utsname > name)
Target uname() handler.
Definition: syscalls.cc:49
Bitfield< 3 > addr
Definition: types.hh:84
Bitfield< 0 > p
Definition: pagetable.hh:151
SyscallReturn archPrctlFunc(SyscallDesc *desc, ThreadContext *tc, int code, uint64_t addr)
Definition: syscalls.cc:63
Bitfield< 5, 3 > index
Definition: types.hh:98
SyscallReturn setThreadArea32Func(SyscallDesc *desc, ThreadContext *tc, VPtr< UserDesc32 > userDesc)
Definition: syscalls.cc:100
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
This file defines objects used to emulate syscalls from the target application on the host machine.
const std::string & name()
Definition: trace.cc:49

Generated on Wed Dec 21 2022 10:22:27 for gem5 by doxygen 1.9.1