gem5
v24.0.0.0
Loading...
Searching...
No Matches
kern
linux
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.
48
Random
Linux::random
;
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 ==
"/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
94
std::string
95
Linux::procMeminfo
(
Process
*process,
ThreadContext
*tc)
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
103
Linux::etcPasswd
(
Process
*process,
ThreadContext
*tc)
104
{
105
return
csprintf
(
"gem5-user:x:1000:1000:gem5-user,,,:%s:/bin/bash\n"
,
106
process->
tgtCwd
);
107
}
108
109
std::string
110
Linux::procSelfMaps
(
Process
*process,
ThreadContext
*tc)
111
{
112
return
process->
memState
->printVmaList();
113
}
114
115
std::string
116
Linux::cpuOnline
(
Process
*process,
ThreadContext
*tc)
117
{
118
return
csprintf
(
"0-%d\n"
, tc->
getSystemPtr
()->
threads
.
size
() - 1);
119
}
120
121
std::string
122
Linux::devRandom
(
Process
*process,
ThreadContext
*tc)
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
DPRINTFR
#define DPRINTFR(x,...)
Definition
trace.hh:224
data
const char data[]
Definition
circlebuf.test.cc:48
gem5::Linux::random
static Random random
Definition
linux.hh:259
gem5::Linux::openSpecialFile
static int openSpecialFile(std::string path, Process *process, ThreadContext *tc)
Definition
linux.cc:51
gem5::Linux::procMeminfo
static std::string procMeminfo(Process *process, ThreadContext *tc)
Definition
linux.cc:95
gem5::Linux::devRandom
static std::string devRandom(Process *process, ThreadContext *tc)
Definition
linux.cc:122
gem5::Linux::procSelfMaps
static std::string procSelfMaps(Process *process, ThreadContext *tc)
Definition
linux.cc:110
gem5::Linux::cpuOnline
static std::string cpuOnline(Process *process, ThreadContext *tc)
Definition
linux.cc:116
gem5::Linux::etcPasswd
static std::string etcPasswd(Process *process, ThreadContext *tc)
Definition
linux.cc:103
gem5::Named::name
virtual std::string name() const
Definition
named.hh:47
gem5::Process
Definition
process.hh:68
gem5::Process::seWorkload
SEWorkload * seWorkload
Definition
process.hh:175
gem5::Process::memState
std::shared_ptr< MemState > memState
Definition
process.hh:289
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:265
gem5::SEWorkload::freeMemSize
Addr freeMemSize(int pool_id=0) const
Definition
se_workload.cc:87
gem5::SEWorkload::memSize
Addr memSize(int pool_id=0) const
Definition
se_workload.cc:81
gem5::System::Threads::size
int size() const
Definition
system.hh:210
gem5::System::threads
Threads threads
Definition
system.hh:310
gem5::ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition
guest_abi.test.cc:41
gem5::ThreadContext::getSystemPtr
virtual System * getSystemPtr()=0
gem5::ThreadContext::getCpuPtr
virtual BaseCPU * getCpuPtr()=0
compiler.hh
base.hh
gem5::Random::random
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
linux.hh
warn
#define warn(...)
Definition
logging.hh:256
mem_state.hh
gem5::ArmISA::fd
Bitfield< 14, 12 > fd
Definition
types.hh:150
gem5::ArmISA::i
Bitfield< 7 > i
Definition
misc_types.hh:67
gem5::ArmISA::f
Bitfield< 6 > f
Definition
misc_types.hh:68
gem5::auxv::Random
@ Random
Definition
aux_vector.hh:87
gem5
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition
binary32.hh:36
gem5::curTick
Tick curTick()
The universal simulation clock.
Definition
cur_tick.hh:46
gem5::csprintf
std::string csprintf(const char *format, const Args &...args)
Definition
cprintf.hh:161
process.hh
se_workload.hh
system.hh
vma.hh
Generated on Tue Jun 18 2024 16:24:04 for gem5 by
doxygen
1.11.0