gem5  v21.1.0.2
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 
31 #include "arch/mips/page_size.hh"
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 
47 namespace gem5
48 {
49 
50 using namespace MipsISA;
51 
52 MipsProcess::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 
80 void
82 {
84 
85  argsInit<uint32_t>(PageBytes);
86 }
87 
88 template<class IntType>
89 void
90 MipsProcess::argsInit(int pageSize)
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->setIntReg(FirstArgumentReg, argc);
204  tc->setIntReg(FirstArgumentReg + 1, argv_array_base);
205  tc->setIntReg(StackPointerReg, memState->getStackMin());
206 
207  tc->pcState(getStartPC());
208 }
209 
210 } // namespace gem5
gem5::ThreadContext::setIntReg
virtual void setIntReg(RegIndex reg_idx, RegVal val)=0
gem5::loader::MemoryImage::maxAddr
Addr maxAddr() const
Definition: memory_image.hh:136
gem5::Process::euid
uint64_t euid()
Definition: process.hh:83
gem5::EmulationPageTable
Definition: page_table.hh:52
gem5::loader::ObjectFile
Definition: object_file.hh:85
gem5::auxv::Gid
@ Gid
Definition: aux_vector.hh:82
system.hh
gem5::X86ISA::L
Bitfield< 7, 0 > L
Definition: int.hh:59
gem5::Process::getStartPC
Addr getStartPC()
Definition: process.cc:490
gem5::Process::initVirtMem
std::unique_ptr< SETranslatingPortProxy > initVirtMem
Definition: process.hh:174
gem5::loader::ObjectFile::entryPoint
Addr entryPoint() const
Definition: object_file.hh:125
gem5::auxv::Random
@ Random
Definition: aux_vector.hh:89
gem5::Process::getBias
Addr getBias()
Definition: process.cc:482
gem5::loader::ElfObject
Definition: elf_object.hh:64
gem5::Process::initState
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
Definition: process.cc:287
process_impl.hh
gem5::Process::egid
uint64_t egid()
Definition: process.hh:85
std::vector
STL vector class.
Definition: stl.hh:37
gem5::MipsISA::PageBytes
const Addr PageBytes
Definition: page_size.hh:42
gem5::auxv::Phent
@ Phent
Definition: aux_vector.hh:73
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:66
gem5::Process::gid
uint64_t gid()
Definition: process.hh:84
page_size.hh
gem5::Process::memState
std::shared_ptr< MemState > memState
Definition: process.hh:276
gem5::Process::uid
uint64_t uid()
Definition: process.hh:82
gem5::auxv::Base
@ Base
Definition: aux_vector.hh:76
gem5::auxv::Pagesz
@ Pagesz
Definition: aux_vector.hh:75
elf_object.hh
syscall_return.hh
gem5::ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:93
gem5::SimObject::params
const Params & params() const
Definition: sim_object.hh:176
gem5::auxv::Clktck
@ Clktck
Definition: aux_vector.hh:86
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:186
gem5::copyStringArray
void copyStringArray(std::vector< std::string > &strings, AddrType array_ptr, AddrType data_ptr, const ByteOrder bo, PortProxy &memProxy)
Definition: process_impl.hh:43
gem5::auxv::AuxVector
Definition: aux_vector.hh:45
gem5::auxv::Phdr
@ Phdr
Definition: aux_vector.hh:72
process.hh
gem5::Process::argv
std::vector< std::string > argv
Definition: process.hh:213
process.hh
gem5::roundDown
static constexpr T roundDown(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:279
gem5::ThreadContext::pcState
virtual TheISA::PCState pcState() const =0
gem5::auxv::Egid
@ Egid
Definition: aux_vector.hh:83
gem5::auxv::Entry
@ Entry
Definition: aux_vector.hh:78
gem5::Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
gem5::Process::envp
std::vector< std::string > envp
Definition: process.hh:214
gem5::auxv::Phnum
@ Phnum
Definition: aux_vector.hh:74
name
const std::string & name()
Definition: trace.cc:49
gem5::ArmISA::StackPointerReg
const int StackPointerReg
Definition: int.hh:545
gem5::Process
Definition: process.hh:67
aux_vector.hh
int.hh
gem5::System::threads
Threads threads
Definition: system.hh:316
gem5::MipsProcess::MipsProcess
MipsProcess(const ProcessParams &params, loader::ObjectFile *objFile)
Definition: process.cc:52
gem5::MipsISA::FirstArgumentReg
const int FirstArgumentReg
Definition: int.hh:67
gem5::MipsProcess::argsInit
void argsInit(int pageSize)
Definition: process.cc:90
gem5::htole
T htole(T value)
Definition: byteswap.hh:172
gem5::auxv::Uid
@ Uid
Definition: aux_vector.hh:80
gem5::ArmISA::PageBytes
const Addr PageBytes
Definition: page_size.hh:53
gem5::roundUp
static constexpr T roundUp(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:260
logging.hh
gem5::Process::system
System * system
Definition: process.hh:162
gem5::auxv::Euid
@ Euid
Definition: aux_vector.hh:81
gem5::Process::contextIds
std::vector< ContextID > contextIds
Definition: process.hh:159
fatal_if
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:225
page_table.hh
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
object_file.hh
gem5::MipsProcess::initState
void initState()
initState() is called on each SimObject when not restoring from a checkpoint.
Definition: process.cc:81
gem5::Process::image
loader::MemoryImage image
Definition: process.hh:211
thread_context.hh
gem5::Process::objFile
loader::ObjectFile * objFile
Definition: process.hh:210
gem5::Process::Loader
Each instance of a Loader subclass will have a chance to try to load an object file when tryLoaders i...
Definition: process.hh:182
byteswap.hh

Generated on Tue Sep 21 2021 12:24:36 for gem5 by doxygen 1.8.17