gem5  v20.1.0.0
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/isa_traits.hh"
34 #include "base/logging.hh"
35 #include "cpu/thread_context.hh"
36 #include "debug/Loader.hh"
37 #include "mem/page_table.hh"
38 #include "params/Process.hh"
39 #include "sim/aux_vector.hh"
40 #include "sim/process.hh"
41 #include "sim/process_impl.hh"
42 #include "sim/syscall_return.hh"
43 #include "sim/system.hh"
44 
45 using namespace std;
46 using namespace MipsISA;
47 
48 MipsProcess::MipsProcess(ProcessParams *params, ::Loader::ObjectFile *objFile)
49  : Process(params,
50  new EmulationPageTable(params->name, params->pid, PageBytes),
51  objFile)
52 {
53  fatal_if(params->useArchPT, "Arch page tables not implemented.");
54  // Set up stack. On MIPS, stack starts at the top of kuseg
55  // user address space. MIPS stack grows down from here
56  Addr stack_base = 0x7FFFFFFF;
57 
58  Addr max_stack_size = 8 * 1024 * 1024;
59 
60  // Set pointer for next thread stack. Reserve 8M for main stack.
61  Addr next_thread_stack_base = stack_base - max_stack_size;
62 
63  // Set up break point (Top of Heap)
64  Addr brk_point = image.maxAddr();
65  brk_point = roundUp(brk_point, PageBytes);
66 
67  // Set up region for mmaps. Start it 1GB above the top of the heap.
68  Addr mmap_end = brk_point + 0x40000000L;
69 
70  memState = make_shared<MemState>(this, brk_point, stack_base,
71  max_stack_size, next_thread_stack_base,
72  mmap_end);
73 }
74 
75 void
77 {
79 
80  argsInit<uint32_t>(PageBytes);
81 }
82 
83 template<class IntType>
84 void
85 MipsProcess::argsInit(int pageSize)
86 {
87  int intSize = sizeof(IntType);
88 
90 
91  auto *elfObject = dynamic_cast<::Loader::ElfObject *>(objFile);
92  if (elfObject)
93  {
94  // Set the system page size
95  auxv.emplace_back(M5_AT_PAGESZ, MipsISA::PageBytes);
96  // Set the frequency at which time() increments
97  auxv.emplace_back(M5_AT_CLKTCK, 100);
98  // For statically linked executables, this is the virtual
99  // address of the program header tables if they appear in the
100  // executable image.
101  auxv.emplace_back(M5_AT_PHDR, elfObject->programHeaderTable());
102  DPRINTF(Loader, "auxv at PHDR %08p\n",
103  elfObject->programHeaderTable());
104  // This is the size of a program header entry from the elf file.
105  auxv.emplace_back(M5_AT_PHENT, elfObject->programHeaderSize());
106  // This is the number of program headers from the original elf file.
107  auxv.emplace_back(M5_AT_PHNUM, elfObject->programHeaderCount());
108  // This is the base address of the ELF interpreter; it should be
109  // zero for static executables or contain the base address for
110  // dynamic executables.
111  auxv.emplace_back(M5_AT_BASE, getBias());
112  //The entry point to the program
113  auxv.emplace_back(M5_AT_ENTRY, objFile->entryPoint());
114  //Different user and group IDs
115  auxv.emplace_back(M5_AT_UID, uid());
116  auxv.emplace_back(M5_AT_EUID, euid());
117  auxv.emplace_back(M5_AT_GID, gid());
118  auxv.emplace_back(M5_AT_EGID, egid());
119  auxv.emplace_back(M5_AT_RANDOM, 0);
120  }
121 
122  // Calculate how much space we need for arg & env & auxv arrays.
123  int argv_array_size = intSize * (argv.size() + 1);
124  int envp_array_size = intSize * (envp.size() + 1);
125  int auxv_array_size = intSize * 2 * (auxv.size() + 1);
126 
127  int arg_data_size = 0;
128  for (vector<string>::size_type i = 0; i < argv.size(); ++i) {
129  arg_data_size += argv[i].size() + 1;
130  }
131 
132  const int numRandomBytes = 16;
133  int aux_data_size = numRandomBytes;
134 
135  int env_data_size = 0;
136  for (vector<string>::size_type i = 0; i < envp.size(); ++i) {
137  env_data_size += envp[i].size() + 1;
138  }
139 
140  int space_needed =
141  argv_array_size +
142  envp_array_size +
143  auxv_array_size +
144  arg_data_size +
145  aux_data_size +
146  env_data_size;
147 
148  // set bottom of stack
149  memState->setStackMin(memState->getStackBase() - space_needed);
150  // align it
151  memState->setStackMin(roundDown(memState->getStackMin(), pageSize));
152  memState->setStackSize(memState->getStackBase() - memState->getStackMin());
153  // map memory
154  memState->mapRegion(memState->getStackMin(),
155  roundUp(memState->getStackSize(), pageSize), "stack");
156 
157  // map out initial stack contents; leave room for argc
158  IntType argv_array_base = memState->getStackMin() + intSize;
159  IntType envp_array_base = argv_array_base + argv_array_size;
160  IntType auxv_array_base = envp_array_base + envp_array_size;
161  IntType arg_data_base = auxv_array_base + auxv_array_size;
162  IntType aux_data_base = arg_data_base - arg_data_size;
163  IntType env_data_base = aux_data_base + aux_data_size;
164 
165  // write contents to stack
166  IntType argc = argv.size();
167 
168  argc = htole((IntType)argc);
169 
170  initVirtMem->writeBlob(memState->getStackMin(), &argc, intSize);
171 
172  copyStringArray(argv, argv_array_base, arg_data_base,
173  ByteOrder::little, *initVirtMem);
174 
175  copyStringArray(envp, envp_array_base, env_data_base,
176  ByteOrder::little, *initVirtMem);
177 
178  // Fix up the aux vectors which point to data.
179  for (auto &aux: auxv) {
180  if (aux.type == M5_AT_RANDOM)
181  aux.val = aux_data_base;
182  }
183 
184  // Copy the aux vector
185  Addr auxv_array_end = auxv_array_base;
186  for (const auto &aux: auxv) {
187  initVirtMem->write(auxv_array_end, aux, GuestByteOrder);
188  auxv_array_end += sizeof(aux);
189  }
190 
191  // Write out the terminating zeroed auxilliary vector
192  const AuxVector<IntType> zero(0, 0);
193  initVirtMem->write(auxv_array_end, zero);
194  auxv_array_end += sizeof(zero);
195 
197 
198  tc->setIntReg(FirstArgumentReg, argc);
199  tc->setIntReg(FirstArgumentReg + 1, argv_array_base);
200  tc->setIntReg(StackPointerReg, memState->getStackMin());
201 
202  tc->pcState(getStartPC());
203 }
204 
206  4, 5, 6, 7, 8, 9
207 };
roundDown
T roundDown(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:150
X86ISA::L
Bitfield< 7, 0 > L
Definition: int.hh:57
system.hh
Process::envp
std::vector< std::string > envp
Definition: process.hh:217
Process::gid
uint64_t gid()
Definition: process.hh:82
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
Process
Definition: process.hh:65
M5_AT_UID
@ M5_AT_UID
Definition: aux_vector.hh:69
MipsProcess::SyscallABI::ArgumentRegs
static const std::vector< int > ArgumentRegs
Definition: process.hh:57
htole
T htole(T value)
Definition: byteswap.hh:140
Process::argv
std::vector< std::string > argv
Definition: process.hh:216
ThreadContext::setIntReg
virtual void setIntReg(RegIndex reg_idx, RegVal val)=0
M5_AT_PAGESZ
@ M5_AT_PAGESZ
Definition: aux_vector.hh:64
M5_AT_PHENT
@ M5_AT_PHENT
Definition: aux_vector.hh:62
ArmISA::GuestByteOrder
const ByteOrder GuestByteOrder
Definition: isa_traits.hh:49
process_impl.hh
M5_AT_EUID
@ M5_AT_EUID
Definition: aux_vector.hh:70
std::vector
STL vector class.
Definition: stl.hh:37
Process::initVirtMem
std::unique_ptr< SETranslatingPortProxy > initVirtMem
Definition: process.hh:177
MipsProcess::MipsProcess
MipsProcess(ProcessParams *params, ::Loader::ObjectFile *objFile)
Definition: process.cc:48
Process::egid
uint64_t egid()
Definition: process.hh:83
Loader::ElfObject
Definition: elf_object.hh:59
MipsISA
Definition: decoder.cc:31
Loader::MemoryImage::maxAddr
Addr maxAddr() const
Definition: memory_image.hh:131
M5_AT_ENTRY
@ M5_AT_ENTRY
Definition: aux_vector.hh:67
M5_AT_BASE
@ M5_AT_BASE
Definition: aux_vector.hh:65
Loader
Definition: process.hh:39
Loader::ObjectFile
Definition: object_file.hh:70
M5_AT_PHNUM
@ M5_AT_PHNUM
Definition: aux_vector.hh:63
elf_object.hh
AuxVector
Definition: aux_vector.hh:38
Loader::ObjectFile::entryPoint
Addr entryPoint() const
Definition: object_file.hh:108
syscall_return.hh
ThreadContext
ThreadContext is the external interface to all thread state for anything outside of the CPU.
Definition: thread_context.hh:88
EmulationPageTable
Definition: page_table.hh:48
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:234
MipsProcess::argsInit
void argsInit(int pageSize)
Definition: process.cc:85
process.hh
process.hh
MipsProcess::initState
void initState()
initState() is called on each SimObject when not restoring from a checkpoint.
Definition: process.cc:76
Process::image
::Loader::MemoryImage image
Definition: process.hh:214
M5_AT_RANDOM
@ M5_AT_RANDOM
Definition: aux_vector.hh:78
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
Process::objFile
::Loader::ObjectFile * objFile
Definition: process.hh:213
Process::getBias
Addr getBias()
Definition: process.cc:468
SimObject::params
const Params * params() const
Definition: sim_object.hh:119
name
const std::string & name()
Definition: trace.cc:50
Process::contextIds
std::vector< ContextID > contextIds
Definition: process.hh:160
ThreadContext::pcState
virtual TheISA::PCState pcState() const =0
MipsISA::FirstArgumentReg
const int FirstArgumentReg
Definition: registers.hh:111
M5_AT_GID
@ M5_AT_GID
Definition: aux_vector.hh:71
System::threads
Threads threads
Definition: system.hh:309
ArmISA::PageBytes
const Addr PageBytes
Definition: isa_traits.hh:52
aux_vector.hh
ArmISA::StackPointerReg
const int StackPointerReg
Definition: registers.hh:114
std
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:587
Process::initState
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
Definition: process.cc:290
roundUp
T roundUp(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:131
MipsISA::PageBytes
const Addr PageBytes
Definition: isa_traits.hh:41
logging.hh
isa_traits.hh
Process::euid
uint64_t euid()
Definition: process.hh:81
Process::getStartPC
Addr getStartPC()
Definition: process.cc:476
Process::system
System * system
Definition: process.hh:163
Process::uid
uint64_t uid()
Definition: process.hh:80
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:219
M5_AT_CLKTCK
@ M5_AT_CLKTCK
Definition: aux_vector.hh:75
page_table.hh
copyStringArray
void copyStringArray(std::vector< std::string > &strings, AddrType array_ptr, AddrType data_ptr, const ByteOrder bo, PortProxy &memProxy)
Definition: process_impl.hh:40
M5_AT_EGID
@ M5_AT_EGID
Definition: aux_vector.hh:72
object_file.hh
thread_context.hh
Process::memState
std::shared_ptr< MemState > memState
Definition: process.hh:279
M5_AT_PHDR
@ M5_AT_PHDR
Definition: aux_vector.hh:61

Generated on Wed Sep 30 2020 14:01:59 for gem5 by doxygen 1.8.17