gem5  v20.1.0.0
process.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007-2008 The Florida State University
3  * Copyright (c) 2009 The University of Edinburgh
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met: redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer;
10  * redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution;
13  * neither the name of the copyright holders nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "arch/power/process.hh"
31 
32 #include "arch/power/isa_traits.hh"
33 #include "arch/power/types.hh"
36 #include "base/logging.hh"
37 #include "cpu/thread_context.hh"
38 #include "debug/Stack.hh"
39 #include "mem/page_table.hh"
40 #include "params/Process.hh"
41 #include "sim/aux_vector.hh"
42 #include "sim/process_impl.hh"
43 #include "sim/syscall_return.hh"
44 #include "sim/system.hh"
45 
46 using namespace std;
47 using namespace PowerISA;
48 
50  ProcessParams *params, ::Loader::ObjectFile *objFile)
51  : Process(params,
52  new EmulationPageTable(params->name, params->pid, PageBytes),
53  objFile)
54 {
55  fatal_if(params->useArchPT, "Arch page tables not implemented.");
56  // Set up break point (Top of Heap)
57  Addr brk_point = image.maxAddr();
58  brk_point = roundUp(brk_point, PageBytes);
59 
60  Addr stack_base = 0xbf000000L;
61 
62  Addr max_stack_size = 8 * 1024 * 1024;
63 
64  // Set pointer for next thread stack. Reserve 8M for main stack.
65  Addr next_thread_stack_base = stack_base - max_stack_size;
66 
67  // Set up region for mmaps. For now, start at bottom of kuseg space.
68  Addr mmap_end = 0x70000000L;
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(sizeof(uint32_t), PageBytes);
81 }
82 
83 void
84 PowerProcess::argsInit(int intSize, int pageSize)
85 {
87 
88  string filename;
89  if (argv.size() < 1)
90  filename = "";
91  else
92  filename = argv[0];
93 
94  //We want 16 byte alignment
95  uint64_t align = 16;
96 
97  // load object file into target memory
100 
101  //Setup the auxilliary vectors. These will already have endian conversion.
102  //Auxilliary vectors are loaded only for elf formatted executables.
103  auto *elfObject = dynamic_cast<::Loader::ElfObject *>(objFile);
104  if (elfObject) {
105  uint32_t features = 0;
106 
107  //Bits which describe the system hardware capabilities
108  //XXX Figure out what these should be
109  auxv.emplace_back(M5_AT_HWCAP, features);
110  //The system page size
111  auxv.emplace_back(M5_AT_PAGESZ, PowerISA::PageBytes);
112  //Frequency at which times() increments
113  auxv.emplace_back(M5_AT_CLKTCK, 0x64);
114  // For statically linked executables, this is the virtual address of
115  // the program header tables if they appear in the executable image
116  auxv.emplace_back(M5_AT_PHDR, elfObject->programHeaderTable());
117  // This is the size of a program header entry from the elf file.
118  auxv.emplace_back(M5_AT_PHENT, elfObject->programHeaderSize());
119  // This is the number of program headers from the original elf file.
120  auxv.emplace_back(M5_AT_PHNUM, elfObject->programHeaderCount());
121  // This is the base address of the ELF interpreter; it should be
122  // zero for static executables or contain the base address for
123  // dynamic executables.
124  auxv.emplace_back(M5_AT_BASE, getBias());
125  //XXX Figure out what this should be.
126  auxv.emplace_back(M5_AT_FLAGS, 0);
127  //The entry point to the program
128  auxv.emplace_back(M5_AT_ENTRY, objFile->entryPoint());
129  //Different user and group IDs
130  auxv.emplace_back(M5_AT_UID, uid());
131  auxv.emplace_back(M5_AT_EUID, euid());
132  auxv.emplace_back(M5_AT_GID, gid());
133  auxv.emplace_back(M5_AT_EGID, egid());
134  //Whether to enable "secure mode" in the executable
135  auxv.emplace_back(M5_AT_SECURE, 0);
136  //The address of 16 "random" bytes
137  auxv.emplace_back(M5_AT_RANDOM, 0);
138  //The filename of the program
139  auxv.emplace_back(M5_AT_EXECFN, 0);
140  //The string "v51" with unknown meaning
141  auxv.emplace_back(M5_AT_PLATFORM, 0);
142  }
143 
144  //Figure out how big the initial stack nedes to be
145 
146  // A sentry NULL void pointer at the top of the stack.
147  int sentry_size = intSize;
148 
149  string platform = "v51";
150  int platform_size = platform.size() + 1;
151 
152  // The aux vectors are put on the stack in two groups. The first group are
153  // the vectors that are generated as the elf is loaded. The second group
154  // are the ones that were computed ahead of time and include the platform
155  // string.
156  int aux_data_size = filename.size() + 1;
157 
158  const int numRandomBytes = 16;
159  aux_data_size += numRandomBytes;
160 
161  int env_data_size = 0;
162  for (int i = 0; i < envp.size(); ++i) {
163  env_data_size += envp[i].size() + 1;
164  }
165  int arg_data_size = 0;
166  for (int i = 0; i < argv.size(); ++i) {
167  arg_data_size += argv[i].size() + 1;
168  }
169 
170  int info_block_size =
171  sentry_size + env_data_size + arg_data_size +
172  aux_data_size + platform_size;
173 
174  //Each auxilliary vector is two 4 byte words
175  int aux_array_size = intSize * 2 * (auxv.size() + 1);
176 
177  int envp_array_size = intSize * (envp.size() + 1);
178  int argv_array_size = intSize * (argv.size() + 1);
179 
180  int argc_size = intSize;
181 
182  //Figure out the size of the contents of the actual initial frame
183  int frame_size =
184  info_block_size +
185  aux_array_size +
186  envp_array_size +
187  argv_array_size +
188  argc_size;
189 
190  //There needs to be padding after the auxiliary vector data so that the
191  //very bottom of the stack is aligned properly.
192  int partial_size = frame_size;
193  int aligned_partial_size = roundUp(partial_size, align);
194  int aux_padding = aligned_partial_size - partial_size;
195 
196  int space_needed = frame_size + aux_padding;
197 
198  Addr stack_min = memState->getStackBase() - space_needed;
199  stack_min = roundDown(stack_min, align);
200 
201  memState->setStackSize(memState->getStackBase() - stack_min);
202 
203  // map memory
204  memState->mapRegion(roundDown(stack_min, pageSize),
205  roundUp(memState->getStackSize(), pageSize), "stack");
206 
207  // map out initial stack contents
208  uint32_t sentry_base = memState->getStackBase() - sentry_size;
209  uint32_t aux_data_base = sentry_base - aux_data_size;
210  uint32_t env_data_base = aux_data_base - env_data_size;
211  uint32_t arg_data_base = env_data_base - arg_data_size;
212  uint32_t platform_base = arg_data_base - platform_size;
213  uint32_t auxv_array_base = platform_base - aux_array_size - aux_padding;
214  uint32_t envp_array_base = auxv_array_base - envp_array_size;
215  uint32_t argv_array_base = envp_array_base - argv_array_size;
216  uint32_t argc_base = argv_array_base - argc_size;
217 
218  DPRINTF(Stack, "The addresses of items on the initial stack:\n");
219  DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
220  DPRINTF(Stack, "0x%x - env data\n", env_data_base);
221  DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
222  DPRINTF(Stack, "0x%x - platform base\n", platform_base);
223  DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
224  DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
225  DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
226  DPRINTF(Stack, "0x%x - argc \n", argc_base);
227  DPRINTF(Stack, "0x%x - stack min\n", stack_min);
228 
229  // write contents to stack
230 
231  // figure out argc
232  uint32_t argc = argv.size();
233  uint32_t guestArgc = htobe(argc);
234 
235  //Write out the sentry void *
236  uint32_t sentry_NULL = 0;
237  initVirtMem->writeBlob(sentry_base, &sentry_NULL, sentry_size);
238 
239  //Fix up the aux vectors which point to other data
240  for (int i = auxv.size() - 1; i >= 0; i--) {
241  if (auxv[i].type == M5_AT_PLATFORM) {
242  auxv[i].val = platform_base;
243  initVirtMem->writeString(platform_base, platform.c_str());
244  } else if (auxv[i].type == M5_AT_EXECFN) {
245  auxv[i].val = aux_data_base + numRandomBytes;
246  initVirtMem->writeString(aux_data_base, filename.c_str());
247  } else if (auxv[i].type == M5_AT_RANDOM) {
248  auxv[i].val = aux_data_base;
249  }
250  }
251 
252  //Copy the aux stuff
253  Addr auxv_array_end = auxv_array_base;
254  for (const auto &aux: auxv) {
255  initVirtMem->write(auxv_array_end, aux, GuestByteOrder);
256  auxv_array_end += sizeof(aux);
257  }
258  //Write out the terminating zeroed auxilliary vector
259  const AuxVector<uint64_t> zero(0, 0);
260  initVirtMem->write(auxv_array_end, zero);
261  auxv_array_end += sizeof(zero);
262 
263  copyStringArray(envp, envp_array_base, env_data_base,
264  ByteOrder::big, *initVirtMem);
265  copyStringArray(argv, argv_array_base, arg_data_base,
266  ByteOrder::big, *initVirtMem);
267 
268  initVirtMem->writeBlob(argc_base, &guestArgc, intSize);
269 
271 
272  //Set the stack pointer register
273  tc->setIntReg(StackPointerReg, stack_min);
274 
275  tc->pcState(getStartPC());
276 
277  //Align the "stack_min" to a page boundary.
278  memState->setStackMin(roundDown(stack_min, pageSize));
279 }
280 
282  3, 4, 5, 6, 7, 8
283 };
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
process.hh
system.hh
Process::envp
std::vector< std::string > envp
Definition: process.hh:217
Loader::MemoryImage::write
bool write(const PortProxy &proxy) const
Definition: memory_image.cc:50
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
Process::argv
std::vector< std::string > argv
Definition: process.hh:216
type
uint8_t type
Definition: inet.hh:421
ThreadContext::setIntReg
virtual void setIntReg(RegIndex reg_idx, RegVal val)=0
M5_AT_PAGESZ
@ M5_AT_PAGESZ
Definition: aux_vector.hh:64
M5_AT_SECURE
@ M5_AT_SECURE
Definition: aux_vector.hh:76
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
Process::egid
uint64_t egid()
Definition: process.hh:83
Loader::ElfObject
Definition: elf_object.hh:59
Loader::MemoryImage::maxAddr
Addr maxAddr() const
Definition: memory_image.hh:131
sc_dt::align
void align(const scfx_rep &lhs, const scfx_rep &rhs, int &new_wp, int &len_mant, scfx_mant_ref &lhs_mant, scfx_mant_ref &rhs_mant)
Definition: scfx_rep.cc:2083
M5_AT_ENTRY
@ M5_AT_ENTRY
Definition: aux_vector.hh:67
PowerProcess::PowerProcess
PowerProcess(ProcessParams *params, ::Loader::ObjectFile *objFile)
Definition: process.cc:49
M5_AT_BASE
@ M5_AT_BASE
Definition: aux_vector.hh:65
PowerProcess::initState
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
Definition: process.cc:76
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
PowerISA
Definition: decoder.cc:31
M5_AT_PLATFORM
@ M5_AT_PLATFORM
Definition: aux_vector.hh:73
EmulationPageTable
Definition: page_table.hh:48
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:234
M5_AT_FLAGS
@ M5_AT_FLAGS
Definition: aux_vector.hh:66
M5_AT_EXECFN
@ M5_AT_EXECFN
Definition: aux_vector.hh:80
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
Process::interpImage
::Loader::MemoryImage interpImage
Definition: process.hh:215
ThreadContext::pcState
virtual TheISA::PCState pcState() const =0
M5_AT_GID
@ M5_AT_GID
Definition: aux_vector.hh:71
isa_traits.hh
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
htobe
T htobe(T value)
Definition: byteswap.hh:142
logging.hh
Process::euid
uint64_t euid()
Definition: process.hh:81
Process::getStartPC
Addr getStartPC()
Definition: process.cc:476
PowerProcess::SyscallABI::ArgumentRegs
static const std::vector< int > ArgumentRegs
Definition: process.hh:57
M5_AT_HWCAP
@ M5_AT_HWCAP
Definition: aux_vector.hh:74
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
PowerProcess::argsInit
void argsInit(int intSize, int pageSize)
Definition: process.cc:84
thread_context.hh
PowerISA::PageBytes
const Addr PageBytes
Definition: isa_traits.hh:42
types.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