gem5  v20.0.0.3
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,
174 
175  copyStringArray(envp, envp_array_base, env_data_base,
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 };
#define DPRINTF(x,...)
Definition: trace.hh:225
Bitfield< 7, 0 > L
Definition: int.hh:57
const std::string & name()
Definition: trace.cc:50
Bitfield< 7 > i
virtual TheISA::PCState pcState() const =0
std::unique_ptr< SETranslatingPortProxy > initVirtMem
Definition: process.hh:177
std::vector< ContextID > contextIds
Definition: process.hh:160
static const std::vector< int > ArgumentRegs
Definition: process.hh:57
uint64_t uid()
Definition: process.hh:80
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:587
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
Definition: process.cc:297
Addr maxAddr() const
T roundUp(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:114
std::shared_ptr< MemState > memState
Definition: process.hh:279
ThreadContext is the external interface to all thread state for anything outside of the CPU...
STL vector class.
Definition: stl.hh:37
ThreadContext * getThreadContext(ContextID tid) const
Definition: system.hh:186
T htole(T value)
Definition: byteswap.hh:140
MipsProcess(ProcessParams *params, ::Loader::ObjectFile *objFile)
Definition: process.cc:48
const int StackPointerReg
Definition: registers.hh:114
void copyStringArray(std::vector< std::string > &strings, AddrType array_ptr, AddrType data_ptr, const ByteOrder bo, PortProxy &memProxy)
Definition: process_impl.hh:40
uint64_t euid()
Definition: process.hh:81
Addr getStartPC()
Definition: process.cc:483
System * system
Definition: process.hh:163
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:199
T roundDown(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:131
std::vector< std::string > envp
Definition: process.hh:217
virtual void setIntReg(RegIndex reg_idx, RegVal val)=0
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:140
const int FirstArgumentReg
Definition: registers.hh:111
Declarations of a non-full system Page Table.
::Loader::MemoryImage image
Definition: process.hh:214
void argsInit(int pageSize)
Definition: process.cc:85
Addr entryPoint() const
Definition: object_file.hh:128
uint64_t gid()
Definition: process.hh:82
const Addr PageBytes
Definition: isa_traits.hh:56
void initState()
initState() is called on each SimObject when not restoring from a checkpoint.
Definition: process.cc:76
const Addr PageBytes
Definition: isa_traits.hh:45
const ByteOrder GuestByteOrder
Definition: isa_traits.hh:51
std::vector< std::string > argv
Definition: process.hh:216
::Loader::ObjectFile * objFile
Definition: process.hh:213
Addr getBias()
Definition: process.cc:475
uint64_t egid()
Definition: process.hh:83

Generated on Fri Jul 3 2020 15:42:38 for gem5 by doxygen 1.8.13