gem5 v24.0.0.0
Loading...
Searching...
No Matches
process.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2004-2005 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 "arch/mips/process.hh"
30
32#include "arch/mips/regs/int.hh"
35#include "base/logging.hh"
36#include "cpu/thread_context.hh"
37#include "debug/Loader.hh"
38#include "mem/page_table.hh"
39#include "params/Process.hh"
40#include "sim/aux_vector.hh"
41#include "sim/byteswap.hh"
42#include "sim/process.hh"
43#include "sim/process_impl.hh"
44#include "sim/syscall_return.hh"
45#include "sim/system.hh"
46
47namespace gem5
48{
49
50using namespace MipsISA;
51
52MipsProcess::MipsProcess(const ProcessParams &params,
53 loader::ObjectFile *objFile)
54 : Process(params,
55 new EmulationPageTable(params.name, params.pid, PageBytes),
56 objFile)
57{
58 fatal_if(params.useArchPT, "Arch page tables not implemented.");
59 // Set up stack. On MIPS, stack starts at the top of kuseg
60 // user address space. MIPS stack grows down from here
61 Addr stack_base = 0x7FFFFFFF;
62
63 Addr max_stack_size = 8 * 1024 * 1024;
64
65 // Set pointer for next thread stack. Reserve 8M for main stack.
66 Addr next_thread_stack_base = stack_base - max_stack_size;
67
68 // Set up break point (Top of Heap)
69 Addr brk_point = image.maxAddr();
70 brk_point = roundUp(brk_point, PageBytes);
71
72 // Set up region for mmaps. Start it 1GB above the top of the heap.
73 Addr mmap_end = brk_point + 0x40000000L;
74
75 memState = std::make_shared<MemState>(
76 this, brk_point, stack_base, max_stack_size,
77 next_thread_stack_base, mmap_end);
78}
79
80void
87
88template<class IntType>
89void
91{
92 int intSize = sizeof(IntType);
93
95
96 auto *elfObject = dynamic_cast<loader::ElfObject *>(objFile);
97 if (elfObject)
98 {
99 // Set the system page size
100 auxv.emplace_back(gem5::auxv::Pagesz, MipsISA::PageBytes);
101 // Set the frequency at which time() increments
102 auxv.emplace_back(gem5::auxv::Clktck, 100);
103 // For statically linked executables, this is the virtual
104 // address of the program header tables if they appear in the
105 // executable image.
106 auxv.emplace_back(gem5::auxv::Phdr, elfObject->programHeaderTable());
107 DPRINTF(Loader, "auxv at PHDR %08p\n",
108 elfObject->programHeaderTable());
109 // This is the size of a program header entry from the elf file.
110 auxv.emplace_back(gem5::auxv::Phent, elfObject->programHeaderSize());
111 // This is the number of program headers from the original elf file.
112 auxv.emplace_back(gem5::auxv::Phnum, elfObject->programHeaderCount());
113 // This is the base address of the ELF interpreter; it should be
114 // zero for static executables or contain the base address for
115 // dynamic executables.
116 auxv.emplace_back(gem5::auxv::Base, getBias());
117 //The entry point to the program
118 auxv.emplace_back(gem5::auxv::Entry, objFile->entryPoint());
119 //Different user and group IDs
120 auxv.emplace_back(gem5::auxv::Uid, uid());
121 auxv.emplace_back(gem5::auxv::Euid, euid());
122 auxv.emplace_back(gem5::auxv::Gid, gid());
123 auxv.emplace_back(gem5::auxv::Egid, egid());
124 auxv.emplace_back(gem5::auxv::Random, 0);
125 }
126
127 // Calculate how much space we need for arg & env & auxv arrays.
128 int argv_array_size = intSize * (argv.size() + 1);
129 int envp_array_size = intSize * (envp.size() + 1);
130 int auxv_array_size = intSize * 2 * (auxv.size() + 1);
131
132 int arg_data_size = 0;
133 for (std::vector<std::string>::size_type i = 0; i < argv.size(); ++i) {
134 arg_data_size += argv[i].size() + 1;
135 }
136
137 const int numRandomBytes = 16;
138 int aux_data_size = numRandomBytes;
139
140 int env_data_size = 0;
141 for (std::vector<std::string>::size_type i = 0; i < envp.size(); ++i) {
142 env_data_size += envp[i].size() + 1;
143 }
144
145 int space_needed =
146 argv_array_size +
147 envp_array_size +
148 auxv_array_size +
149 arg_data_size +
150 aux_data_size +
151 env_data_size;
152
153 // set bottom of stack
154 memState->setStackMin(memState->getStackBase() - space_needed);
155 // align it
156 memState->setStackMin(roundDown(memState->getStackMin(), pageSize));
157 memState->setStackSize(memState->getStackBase() - memState->getStackMin());
158 // map memory
159 memState->mapRegion(memState->getStackMin(),
160 roundUp(memState->getStackSize(), pageSize), "stack");
161
162 // map out initial stack contents; leave room for argc
163 IntType argv_array_base = memState->getStackMin() + intSize;
164 IntType envp_array_base = argv_array_base + argv_array_size;
165 IntType auxv_array_base = envp_array_base + envp_array_size;
166 IntType arg_data_base = auxv_array_base + auxv_array_size;
167 IntType aux_data_base = arg_data_base - arg_data_size;
168 IntType env_data_base = aux_data_base + aux_data_size;
169
170 // write contents to stack
171 IntType argc = argv.size();
172
173 argc = htole((IntType)argc);
174
175 initVirtMem->writeBlob(memState->getStackMin(), &argc, intSize);
176
177 copyStringArray(argv, argv_array_base, arg_data_base,
178 ByteOrder::little, *initVirtMem);
179
180 copyStringArray(envp, envp_array_base, env_data_base,
181 ByteOrder::little, *initVirtMem);
182
183 // Fix up the aux vectors which point to data.
184 for (auto &aux: auxv) {
185 if (aux.type == gem5::auxv::Random)
186 aux.val = aux_data_base;
187 }
188
189 // Copy the aux vector
190 Addr auxv_array_end = auxv_array_base;
191 for (const auto &aux: auxv) {
192 initVirtMem->write(auxv_array_end, aux, ByteOrder::little);
193 auxv_array_end += sizeof(aux);
194 }
195
196 // Write out the terminating zeroed auxilliary vector
197 const gem5::auxv::AuxVector<IntType> zero(0, 0);
198 initVirtMem->write(auxv_array_end, zero);
199 auxv_array_end += sizeof(zero);
200
202
203 tc->setReg(int_reg::A0, argc);
204 tc->setReg(int_reg::A1, argv_array_base);
205 tc->setReg(int_reg::Sp, memState->getStackMin());
206
207 tc->pcState(getStartPC());
208}
209
210} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:210
MipsProcess(const ProcessParams &params, loader::ObjectFile *objFile)
Definition process.cc:52
void initState()
initState() is called on each SimObject when not restoring from a checkpoint.
Definition process.cc:81
void argsInit(int pageSize)
Definition process.cc:90
Each instance of a Loader subclass will have a chance to try to load an object file when tryLoaders i...
Definition process.hh:196
loader::MemoryImage image
Definition process.hh:224
uint64_t egid()
Definition process.hh:85
std::unique_ptr< SETranslatingPortProxy > initVirtMem
Definition process.hh:187
std::shared_ptr< MemState > memState
Definition process.hh:289
uint64_t uid()
Definition process.hh:82
std::vector< std::string > argv
Definition process.hh:226
std::vector< ContextID > contextIds
Definition process.hh:170
System * system
Definition process.hh:173
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
Definition process.cc:288
std::vector< std::string > envp
Definition process.hh:227
uint64_t gid()
Definition process.hh:84
Addr getStartPC()
Definition process.cc:497
loader::ObjectFile * objFile
Definition process.hh:223
Addr getBias()
Definition process.cc:489
uint64_t euid()
Definition process.hh:83
Threads threads
Definition system.hh:310
ThreadContext is the external interface to all thread state for anything outside of the CPU.
virtual void setReg(const RegId &reg, RegVal val)
virtual const PCStateBase & pcState() const =0
STL vector class.
Definition stl.hh:37
static constexpr T roundDown(const T &val, const U &align)
This function is used to align addresses in memory.
Definition intmath.hh:279
static constexpr T roundUp(const T &val, const U &align)
This function is used to align addresses in memory.
Definition intmath.hh:260
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition logging.hh:236
const Params & params() const
constexpr auto & Sp
Definition int.hh:274
Bitfield< 7 > i
Definition misc_types.hh:67
const Addr PageBytes
Definition page_size.hh:53
constexpr RegId A1
Definition int.hh:141
constexpr RegId A0
Definition int.hh:140
const Addr PageBytes
Definition page_size.hh:42
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
T htole(T value)
Definition byteswap.hh:172
void copyStringArray(std::vector< std::string > &strings, AddrType array_ptr, AddrType data_ptr, const ByteOrder bo, PortProxy &memProxy)
Declarations of a non-full system Page Table.
const std::string & name()
Definition trace.cc:48

Generated on Tue Jun 18 2024 16:24:06 for gem5 by doxygen 1.11.0