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

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