gem5  v21.1.0.2
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 "base/compiler.hh"
35 #include "cpu/base.hh"
36 #include "debug/SyscallVerbose.hh"
37 #include "sim/mem_state.hh"
38 #include "sim/process.hh"
39 #include "sim/system.hh"
40 #include "sim/vma.hh"
41 
42 namespace gem5
43 {
44 
45 // The OS methods are called statically. Instantiate the random number
46 // generator for access to /dev/urandom here.
48 
49 int
50 Linux::openSpecialFile(std::string path, Process *process,
51  ThreadContext *tc)
52 {
53  DPRINTFR(SyscallVerbose,
54  "%d: %s: generic-open: opening special file: %s\n",
55  curTick(), tc->getCpuPtr()->name(), path.c_str());
56 
57  bool matched = false;
58  std::string data;
59 
60  if (path.compare(0, 13, "/proc/meminfo") == 0) {
61  data = Linux::procMeminfo(process, tc);
62  matched = true;
63  } else if (path.compare(0, 11, "/etc/passwd") == 0) {
64  data = Linux::etcPasswd(process, tc);
65  matched = true;
66  } else if (path.compare(0, 15, "/proc/self/maps") == 0) {
67  data = Linux::procSelfMaps(process, tc);
68  matched = true;
69  } else if (path.compare(0, 30, "/sys/devices/system/cpu/online") == 0) {
70  data = Linux::cpuOnline(process, tc);
71  matched = true;
72  } else if (path.compare(0, 12 ,"/dev/urandom") == 0) {
73  data = Linux::devRandom(process, tc);
74  matched = true;
75  }
76 
77  if (matched) {
78  FILE *f = tmpfile();
79  int fd = fileno(f);
80  GEM5_VAR_USED size_t ret = fwrite(data.c_str(), 1, data.size(), f);
81  assert(ret == data.size());
82  rewind(f);
83  return fd;
84  } else {
85  warn("Attempting to open special file: %s. Ignoring. Simulation may "
86  "take un-expected code path or be non-deterministic until proper "
87  "handling is implemented.\n", path.c_str());
88  errno = EACCES;
89  return -1;
90  }
91 }
92 
93 std::string
95 {
96  return csprintf("MemTotal:%12d kB\nMemFree: %12d kB\n",
97  process->system->memSize() >> 10,
98  process->system->freeMemSize() >> 10);
99 }
100 
101 std::string
103 {
104  return csprintf("gem5-user:x:1000:1000:gem5-user,,,:%s:/bin/bash\n",
105  process->tgtCwd);
106 }
107 
108 std::string
110 {
111  return process->memState->printVmaList();
112 }
113 
114 std::string
116 {
117  return csprintf("0-%d\n", tc->getSystemPtr()->threads.size() - 1);
118 }
119 
120 std::string
122 {
123  DPRINTFR(SyscallVerbose,
124  "%d: %s: open: generating urandom\n",
125  curTick(), tc->getCpuPtr()->name());
126 
127  std::stringstream line;
128  int max = 1E5;
129  for (int i = 0; i < max; i++) {
130  uint8_t rand_uint = random.random<uint8_t>(0, 255);
131 
132  line << rand_uint;
133  }
134  return line.str();
135 }
136 
137 } // namespace gem5
gem5::curTick
Tick curTick()
The universal simulation clock.
Definition: cur_tick.hh:46
gem5::ThreadContext::getSystemPtr
virtual System * getSystemPtr()=0
warn
#define warn(...)
Definition: logging.hh:245
gem5::System::Threads::size
int size() const
Definition: system.hh:216
linux.hh
system.hh
DPRINTFR
#define DPRINTFR(x,...)
Definition: trace.hh:200
data
const char data[]
Definition: circlebuf.test.cc:48
gem5::ArmISA::fd
Bitfield< 14, 12 > fd
Definition: types.hh:150
gem5::Linux::openSpecialFile
static int openSpecialFile(std::string path, Process *process, ThreadContext *tc)
Definition: linux.cc:50
gem5::auxv::Random
@ Random
Definition: aux_vector.hh:89
gem5::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:252
gem5::ArmISA::f
Bitfield< 6 > f
Definition: misc_types.hh:67
gem5::Linux::procSelfMaps
static std::string procSelfMaps(Process *process, ThreadContext *tc)
Definition: linux.cc:109
gem5::csprintf
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:161
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:66
mem_state.hh
gem5::Process::memState
std::shared_ptr< MemState > memState
Definition: process.hh:276
gem5::Linux::random
static Random random
Definition: linux.hh:247
gem5::Random::random
std::enable_if_t< std::is_integral< T >::value, T > random()
Use the SFINAE idiom to choose an implementation based on whether the type is integral or floating po...
Definition: random.hh:90
gem5::ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:93
gem5::Named::name
virtual std::string name() const
Definition: named.hh:47
gem5::Linux::etcPasswd
static std::string etcPasswd(Process *process, ThreadContext *tc)
Definition: linux.cc:102
process.hh
gem5::Linux::cpuOnline
static std::string cpuOnline(Process *process, ThreadContext *tc)
Definition: linux.cc:115
compiler.hh
gem5::System::freeMemSize
Addr freeMemSize(int poolID=0) const
Amount of physical memory that is still free.
Definition: system.cc:369
gem5::Linux::procMeminfo
static std::string procMeminfo(Process *process, ThreadContext *tc)
Definition: linux.cc:94
gem5::System::memSize
Addr memSize(int poolID=0) const
Amount of physical memory that exists.
Definition: system.cc:362
gem5::Process
Definition: process.hh:67
base.hh
gem5::System::threads
Threads threads
Definition: system.hh:316
gem5::Linux::devRandom
static std::string devRandom(Process *process, ThreadContext *tc)
Definition: linux.cc:121
gem5::Process::system
System * system
Definition: process.hh:162
gem5::ThreadContext::getCpuPtr
virtual BaseCPU * getCpuPtr()=0
vma.hh
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40

Generated on Tue Sep 21 2021 12:25:25 for gem5 by doxygen 1.8.17