gem5  v20.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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",
114  tc->getSystemPtr()->numContexts() - 1);
115 }
116 
117 std::string
119 {
120  DPRINTFR(SyscallVerbose,
121  "%d: %s: open: generating urandom\n",
122  curTick(), tc->getCpuPtr()->name());
123 
124  std::stringstream line;
125  int max = 1E5;
126  for (int i = 0; i < max; i++) {
127  uint8_t rand_uint = random.random<uint8_t>(0, 255);
128 
129  line << rand_uint;
130  }
131  return line.str();
132 }
virtual System * getSystemPtr()=0
Bitfield< 7 > i
static std::string etcPasswd(Process *process, ThreadContext *tc)
Definition: linux.cc:98
Addr freeMemSize() const
Amount of physical memory that is still free.
Definition: system.cc:348
virtual BaseCPU * getCpuPtr()=0
static std::string devRandom(Process *process, ThreadContext *tc)
Definition: linux.cc:118
std::shared_ptr< MemState > memState
Definition: process.hh:279
static std::string procSelfMaps(Process *process, ThreadContext *tc)
Definition: linux.cc:105
ThreadContext is the external interface to all thread state for anything outside of the CPU...
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:79
Bitfield< 6 > f
Tick curTick()
The current simulated tick.
Definition: core.hh:44
Definition: random.hh:58
Addr memSize() const
Amount of physical memory that exists.
Definition: system.cc:342
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:158
static std::string cpuOnline(Process *process, ThreadContext *tc)
Definition: linux.cc:111
unsigned numContexts() const
Definition: system.hh:198
System * system
Definition: process.hh:163
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
static Random random
Definition: linux.hh:234
static int openSpecialFile(std::string path, Process *process, ThreadContext *tc)
Definition: linux.cc:46
virtual const std::string name() const
Definition: sim_object.hh:129
#define warn(...)
Definition: logging.hh:208
Bitfield< 14, 12 > fd
Definition: types.hh:158
const char data[]
static std::string procMeminfo(Process *process, ThreadContext *tc)
Definition: linux.cc:90
#define DPRINTFR(...)
Definition: trace.hh:227

Generated on Thu May 28 2020 16:11:02 for gem5 by doxygen 1.8.13