gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
43namespace gem5
44{
45
46// The OS methods are called statically. Instantiate the random number
47// generator for access to /dev/urandom here.
49
50int
51Linux::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 == "/proc/meminfo") {
62 data = Linux::procMeminfo(process, tc);
63 matched = true;
64 } else if (path == "/etc/passwd") {
65 data = Linux::etcPasswd(process, tc);
66 matched = true;
67 } else if (path == "/proc/self/maps") {
68 data = Linux::procSelfMaps(process, tc);
69 matched = true;
70 } else if (path == "/sys/devices/system/cpu/online") {
71 data = Linux::cpuOnline(process, tc);
72 matched = true;
73 } else if (path == "/dev/urandom") {
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
94std::string
96{
97 return csprintf("MemTotal:%12d kB\nMemFree: %12d kB\n",
98 process->seWorkload->memSize() >> 10,
99 process->seWorkload->freeMemSize() >> 10);
100}
101
102std::string
104{
105 return csprintf("gem5-user:x:1000:1000:gem5-user,,,:%s:/bin/bash\n",
106 process->tgtCwd);
107}
108
109std::string
111{
112 return process->memState->printVmaList();
113}
114
115std::string
117{
118 return csprintf("0-%d\n", tc->getSystemPtr()->threads.size() - 1);
119}
120
121std::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:224
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:175
std::shared_ptr< MemState > memState
Definition process.hh:289
std::string tgtCwd
The cwd members are used to track changes to the current working directory for the purpose of executi...
Definition process.hh:265
Addr freeMemSize(int pool_id=0) const
Addr memSize(int pool_id=0) const
int size() const
Definition system.hh:210
Threads threads
Definition system.hh:310
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:256
Bitfield< 14, 12 > fd
Definition types.hh:150
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 6 > f
Definition misc_types.hh:68
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
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 Tue Jun 18 2024 16:24:04 for gem5 by doxygen 1.11.0