gem5  v20.1.0.0
linux.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 The Regents of The University of Michigan
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "kern/linux/linux.hh"
30 
31 #include <cstdio>
32 #include <string>
33 
34 #include "cpu/base.hh"
35 #include "debug/SyscallVerbose.hh"
36 #include "sim/mem_state.hh"
37 #include "sim/process.hh"
38 #include "sim/system.hh"
39 #include "sim/vma.hh"
40 
41 // The OS methods are called statically. Instantiate the random number
42 // generator for access to /dev/urandom here.
44 
45 int
46 Linux::openSpecialFile(std::string path, Process *process,
47  ThreadContext *tc)
48 {
49  DPRINTFR(SyscallVerbose,
50  "%d: %s: generic-open: opening special file: %s\n",
51  curTick(), tc->getCpuPtr()->name(), path.c_str());
52 
53  bool matched = false;
54  std::string data;
55 
56  if (path.compare(0, 13, "/proc/meminfo") == 0) {
57  data = Linux::procMeminfo(process, tc);
58  matched = true;
59  } else if (path.compare(0, 11, "/etc/passwd") == 0) {
60  data = Linux::etcPasswd(process, tc);
61  matched = true;
62  } else if (path.compare(0, 15, "/proc/self/maps") == 0) {
63  data = Linux::procSelfMaps(process, tc);
64  matched = true;
65  } else if (path.compare(0, 30, "/sys/devices/system/cpu/online") == 0) {
66  data = Linux::cpuOnline(process, tc);
67  matched = true;
68  } else if (path.compare(0, 12 ,"/dev/urandom") == 0) {
69  data = Linux::devRandom(process, tc);
70  matched = true;
71  }
72 
73  if (matched) {
74  FILE *f = tmpfile();
75  int fd = fileno(f);
76  size_t ret M5_VAR_USED = fwrite(data.c_str(), 1, data.size(), f);
77  assert(ret == data.size());
78  rewind(f);
79  return fd;
80  } else {
81  warn("Attempting to open special file: %s. Ignoring. Simulation may "
82  "take un-expected code path or be non-deterministic until proper "
83  "handling is implemented.\n", path.c_str());
84  errno = EACCES;
85  return -1;
86  }
87 }
88 
89 std::string
91 {
92  return csprintf("MemTotal:%12d kB\nMemFree: %12d kB\n",
93  process->system->memSize() >> 10,
94  process->system->freeMemSize() >> 10);
95 }
96 
97 std::string
99 {
100  return csprintf("gem5-user:x:1000:1000:gem5-user,,,:%s:/bin/bash\n",
101  process->tgtCwd);
102 }
103 
104 std::string
106 {
107  return process->memState->printVmaList();
108 }
109 
110 std::string
112 {
113  return csprintf("0-%d\n", tc->getSystemPtr()->threads.size() - 1);
114 }
115 
116 std::string
118 {
119  DPRINTFR(SyscallVerbose,
120  "%d: %s: open: generating urandom\n",
121  curTick(), tc->getCpuPtr()->name());
122 
123  std::stringstream line;
124  int max = 1E5;
125  for (int i = 0; i < max; i++) {
126  uint8_t rand_uint = random.random<uint8_t>(0, 255);
127 
128  line << rand_uint;
129  }
130  return line.str();
131 }
warn
#define warn(...)
Definition: logging.hh:239
linux.hh
system.hh
Process::tgtCwd
std::string tgtCwd
The cwd members are used to track changes to the current working directory for the purpose of executi...
Definition: process.hh:255
data
const char data[]
Definition: circlebuf.test.cc:42
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
Process
Definition: process.hh:65
ArmISA::fd
Bitfield< 14, 12 > fd
Definition: types.hh:159
Linux::procMeminfo
static std::string procMeminfo(Process *process, ThreadContext *tc)
Definition: linux.cc:90
Linux::etcPasswd
static std::string etcPasswd(Process *process, ThreadContext *tc)
Definition: linux.cc:98
mem_state.hh
ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:88
Random
Definition: random.hh:58
Linux::procSelfMaps
static std::string procSelfMaps(Process *process, ThreadContext *tc)
Definition: linux.cc:105
Linux::openSpecialFile
static int openSpecialFile(std::string path, Process *process, ThreadContext *tc)
Definition: linux.cc:46
Linux::devRandom
static std::string devRandom(Process *process, ThreadContext *tc)
Definition: linux.cc:117
process.hh
DPRINTFR
#define DPRINTFR(...)
Definition: trace.hh:236
System::Threads::size
int size() const
Definition: system.hh:204
System::memSize
Addr memSize() const
Amount of physical memory that exists.
Definition: system.cc:405
Linux::cpuOnline
static std::string cpuOnline(Process *process, ThreadContext *tc)
Definition: linux.cc:111
System::freeMemSize
Addr freeMemSize() const
Amount of physical memory that is still free.
Definition: system.cc:411
System::threads
Threads threads
Definition: system.hh:309
base.hh
Linux::random
static Random random
Definition: linux.hh:234
Random::random
std::enable_if< std::is_integral< T >::value, T >::type random()
Use the SFINAE idiom to choose an implementation based on whether the type is integral or floating po...
Definition: random.hh:86
Process::system
System * system
Definition: process.hh:163
vma.hh
ThreadContext::getCpuPtr
virtual BaseCPU * getCpuPtr()=0
csprintf
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:158
Process::memState
std::shared_ptr< MemState > memState
Definition: process.hh:279
ThreadContext::getSystemPtr
virtual System * getSystemPtr()=0
ArmISA::f
Bitfield< 6 > f
Definition: miscregs_types.hh:64
curTick
Tick curTick()
The current simulated tick.
Definition: core.hh:45

Generated on Wed Sep 30 2020 14:02:00 for gem5 by doxygen 1.8.17