gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * Authors: Stephen Hines
30  * Timothy M. Jones
31  */
32 
33 #include "arch/power/process.hh"
34 
35 #include "arch/power/isa_traits.hh"
36 #include "arch/power/types.hh"
39 #include "base/logging.hh"
40 #include "cpu/thread_context.hh"
41 #include "debug/Stack.hh"
42 #include "mem/page_table.hh"
43 #include "params/Process.hh"
44 #include "sim/aux_vector.hh"
45 #include "sim/process_impl.hh"
46 #include "sim/syscall_return.hh"
47 #include "sim/system.hh"
48 
49 using namespace std;
50 using namespace PowerISA;
51 
52 PowerProcess::PowerProcess(ProcessParams *params, ObjectFile *objFile)
53  : Process(params,
54  new EmulationPageTable(params->name, params->pid, PageBytes),
55  objFile)
56 {
57  fatal_if(params->useArchPT, "Arch page tables not implemented.");
58  // Set up break point (Top of Heap)
59  Addr brk_point = image.maxAddr();
60  brk_point = roundUp(brk_point, PageBytes);
61 
62  Addr stack_base = 0xbf000000L;
63 
64  Addr max_stack_size = 8 * 1024 * 1024;
65 
66  // Set pointer for next thread stack. Reserve 8M for main stack.
67  Addr next_thread_stack_base = stack_base - max_stack_size;
68 
69  // Set up region for mmaps. For now, start at bottom of kuseg space.
70  Addr mmap_end = 0x70000000L;
71 
72  memState = make_shared<MemState>(brk_point, stack_base, max_stack_size,
73  next_thread_stack_base, mmap_end);
74 }
75 
76 void
78 {
80 
82 }
83 
84 void
85 PowerProcess::argsInit(int intSize, int pageSize)
86 {
88 
89  string filename;
90  if (argv.size() < 1)
91  filename = "";
92  else
93  filename = argv[0];
94 
95  //We want 16 byte alignment
96  uint64_t align = 16;
97 
98  // load object file into target memory
101 
102  //Setup the auxilliary vectors. These will already have endian conversion.
103  //Auxilliary vectors are loaded only for elf formatted executables.
104  ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
105  if (elfObject) {
106  uint32_t features = 0;
107 
108  //Bits which describe the system hardware capabilities
109  //XXX Figure out what these should be
110  auxv.emplace_back(M5_AT_HWCAP, features);
111  //The system page size
112  auxv.emplace_back(M5_AT_PAGESZ, PowerISA::PageBytes);
113  //Frequency at which times() increments
114  auxv.emplace_back(M5_AT_CLKTCK, 0x64);
115  // For statically linked executables, this is the virtual address of
116  // the program header tables if they appear in the executable image
117  auxv.emplace_back(M5_AT_PHDR, elfObject->programHeaderTable());
118  // This is the size of a program header entry from the elf file.
119  auxv.emplace_back(M5_AT_PHENT, elfObject->programHeaderSize());
120  // This is the number of program headers from the original elf file.
121  auxv.emplace_back(M5_AT_PHNUM, elfObject->programHeaderCount());
122  // This is the base address of the ELF interpreter; it should be
123  // zero for static executables or contain the base address for
124  // dynamic executables.
125  auxv.emplace_back(M5_AT_BASE, getBias());
126  //XXX Figure out what this should be.
127  auxv.emplace_back(M5_AT_FLAGS, 0);
128  //The entry point to the program
129  auxv.emplace_back(M5_AT_ENTRY, objFile->entryPoint());
130  //Different user and group IDs
131  auxv.emplace_back(M5_AT_UID, uid());
132  auxv.emplace_back(M5_AT_EUID, euid());
133  auxv.emplace_back(M5_AT_GID, gid());
134  auxv.emplace_back(M5_AT_EGID, egid());
135  //Whether to enable "secure mode" in the executable
136  auxv.emplace_back(M5_AT_SECURE, 0);
137  //The filename of the program
138  auxv.emplace_back(M5_AT_EXECFN, 0);
139  //The string "v51" with unknown meaning
140  auxv.emplace_back(M5_AT_PLATFORM, 0);
141  }
142 
143  //Figure out how big the initial stack nedes to be
144 
145  // A sentry NULL void pointer at the top of the stack.
146  int sentry_size = intSize;
147 
148  string platform = "v51";
149  int platform_size = platform.size() + 1;
150 
151  // The aux vectors are put on the stack in two groups. The first group are
152  // the vectors that are generated as the elf is loaded. The second group
153  // are the ones that were computed ahead of time and include the platform
154  // string.
155  int aux_data_size = filename.size() + 1;
156 
157  int env_data_size = 0;
158  for (int i = 0; i < envp.size(); ++i) {
159  env_data_size += envp[i].size() + 1;
160  }
161  int arg_data_size = 0;
162  for (int i = 0; i < argv.size(); ++i) {
163  arg_data_size += argv[i].size() + 1;
164  }
165 
166  int info_block_size =
167  sentry_size + env_data_size + arg_data_size +
168  aux_data_size + platform_size;
169 
170  //Each auxilliary vector is two 4 byte words
171  int aux_array_size = intSize * 2 * (auxv.size() + 1);
172 
173  int envp_array_size = intSize * (envp.size() + 1);
174  int argv_array_size = intSize * (argv.size() + 1);
175 
176  int argc_size = intSize;
177 
178  //Figure out the size of the contents of the actual initial frame
179  int frame_size =
180  info_block_size +
181  aux_array_size +
182  envp_array_size +
183  argv_array_size +
184  argc_size;
185 
186  //There needs to be padding after the auxiliary vector data so that the
187  //very bottom of the stack is aligned properly.
188  int partial_size = frame_size;
189  int aligned_partial_size = roundUp(partial_size, align);
190  int aux_padding = aligned_partial_size - partial_size;
191 
192  int space_needed = frame_size + aux_padding;
193 
194  Addr stack_min = memState->getStackBase() - space_needed;
195  stack_min = roundDown(stack_min, align);
196 
197  memState->setStackSize(memState->getStackBase() - stack_min);
198 
199  // map memory
200  allocateMem(roundDown(stack_min, pageSize),
201  roundUp(memState->getStackSize(), pageSize));
202 
203  // map out initial stack contents
204  uint32_t sentry_base = memState->getStackBase() - sentry_size;
205  uint32_t aux_data_base = sentry_base - aux_data_size;
206  uint32_t env_data_base = aux_data_base - env_data_size;
207  uint32_t arg_data_base = env_data_base - arg_data_size;
208  uint32_t platform_base = arg_data_base - platform_size;
209  uint32_t auxv_array_base = platform_base - aux_array_size - aux_padding;
210  uint32_t envp_array_base = auxv_array_base - envp_array_size;
211  uint32_t argv_array_base = envp_array_base - argv_array_size;
212  uint32_t argc_base = argv_array_base - argc_size;
213 
214  DPRINTF(Stack, "The addresses of items on the initial stack:\n");
215  DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
216  DPRINTF(Stack, "0x%x - env data\n", env_data_base);
217  DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
218  DPRINTF(Stack, "0x%x - platform base\n", platform_base);
219  DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
220  DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
221  DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
222  DPRINTF(Stack, "0x%x - argc \n", argc_base);
223  DPRINTF(Stack, "0x%x - stack min\n", stack_min);
224 
225  // write contents to stack
226 
227  // figure out argc
228  uint32_t argc = argv.size();
229  uint32_t guestArgc = htobe(argc);
230 
231  //Write out the sentry void *
232  uint32_t sentry_NULL = 0;
233  initVirtMem.writeBlob(sentry_base, &sentry_NULL, sentry_size);
234 
235  //Fix up the aux vectors which point to other data
236  for (int i = auxv.size() - 1; i >= 0; i--) {
237  if (auxv[i].type == M5_AT_PLATFORM) {
238  auxv[i].val = platform_base;
239  initVirtMem.writeString(platform_base, platform.c_str());
240  } else if (auxv[i].type == M5_AT_EXECFN) {
241  auxv[i].val = aux_data_base;
242  initVirtMem.writeString(aux_data_base, filename.c_str());
243  }
244  }
245 
246  //Copy the aux stuff
247  Addr auxv_array_end = auxv_array_base;
248  for (const auto &aux: auxv) {
249  initVirtMem.write(auxv_array_end, aux, GuestByteOrder);
250  auxv_array_end += sizeof(aux);
251  }
252  //Write out the terminating zeroed auxilliary vector
253  const AuxVector<uint64_t> zero(0, 0);
254  initVirtMem.write(auxv_array_end, zero);
255  auxv_array_end += sizeof(zero);
256 
257  copyStringArray(envp, envp_array_base, env_data_base,
259  copyStringArray(argv, argv_array_base, arg_data_base,
261 
262  initVirtMem.writeBlob(argc_base, &guestArgc, intSize);
263 
265 
266  //Set the stack pointer register
267  tc->setIntReg(StackPointerReg, stack_min);
268 
269  tc->pcState(getStartPC());
270 
271  //Align the "stack_min" to a page boundary.
272  memState->setStackMin(roundDown(stack_min, pageSize));
273 }
274 
275 RegVal
277 {
278  assert(i < 5);
279  return tc->readIntReg(ArgumentReg0 + i++);
280 }
281 
282 void
284 {
285  Cr cr = tc->readIntReg(INTREG_CR);
286  if (sysret.successful()) {
287  cr.cr0.so = 0;
288  } else {
289  cr.cr0.so = 1;
290  }
291  tc->setIntReg(INTREG_CR, cr);
292  tc->setIntReg(ReturnValueReg, sysret.encodedValue());
293 }
#define DPRINTF(x,...)
Definition: trace.hh:229
ObjectFile * objFile
Definition: process.hh:217
Addr programHeaderTable()
Definition: elf_object.hh:128
Bitfield< 7, 0 > L
Definition: int.hh:59
const std::string & name()
Definition: trace.cc:54
Bitfield< 7 > i
bool write(const PortProxy &proxy) const
Definition: memory_image.cc:50
const int ArgumentReg0
Definition: registers.hh:111
virtual TheISA::PCState pcState() const =0
virtual RegVal readIntReg(RegIndex reg_idx) const =0
MemoryImage interpImage
Definition: process.hh:219
bool successful() const
Was the system call successful?
std::vector< ContextID > contextIds
Definition: process.hh:167
void writeString(Addr addr, const char *str) const
Same as tryWriteString, but insists on success.
Definition: port_proxy.hh:241
void allocateMem(Addr vaddr, int64_t size, bool clobber=false)
Definition: process.cc:333
SETranslatingPortProxy initVirtMem
Definition: process.hh:183
uint64_t uid()
Definition: process.hh:87
void setSyscallReturn(ThreadContext *tc, SyscallReturn return_value) override
Definition: process.cc:283
uint64_t RegVal
Definition: types.hh:168
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:586
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
Definition: process.cc:307
T roundUp(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:168
std::shared_ptr< MemState > memState
Definition: process.hh:283
ThreadContext is the external interface to all thread state for anything outside of the CPU...
int64_t encodedValue() const
The encoded value (as described above)
const ByteOrder GuestByteOrder
Definition: isa_traits.hh:42
STL vector class.
Definition: stl.hh:40
PowerProcess(ProcessParams *params, ObjectFile *objFile)
Definition: process.cc:52
ThreadContext * getThreadContext(ContextID tid) const
Definition: system.hh:194
uint8_t type
Definition: inet.hh:333
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
Definition: process.cc:77
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:2051
void writeBlob(Addr addr, const void *p, int size) const
Same as tryWriteBlob, but insists on success.
Definition: port_proxy.hh:189
Addr maxAddr() const
void copyStringArray(std::vector< std::string > &strings, AddrType array_ptr, AddrType data_ptr, const ByteOrder bo, PortProxy &memProxy)
Definition: process_impl.hh:43
uint64_t euid()
Definition: process.hh:88
void argsInit(int intSize, int pageSize)
Definition: process.cc:85
Addr getStartPC()
Definition: process.cc:532
System * system
Definition: process.hh:170
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:203
const RegIndex StackPointerReg
Definition: registers.hh:77
T roundDown(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:185
std::vector< std::string > envp
Definition: process.hh:221
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:142
const Addr PageBytes
Definition: isa_traits.hh:47
uint16_t programHeaderSize()
Definition: elf_object.hh:129
const int MachineBytes
Definition: isa_traits.hh:107
Declarations of a non-full system Page Table.
T htobe(T value)
Definition: byteswap.hh:146
uint16_t programHeaderCount()
Definition: elf_object.hh:130
const Addr PageBytes
Definition: isa_traits.hh:50
Addr entryPoint() const
Definition: object_file.hh:131
uint64_t gid()
Definition: process.hh:89
void write(Addr address, const T &data) const
Write object T to address.
Definition: port_proxy.hh:293
const RegIndex ReturnValueReg
Definition: registers.hh:81
std::vector< std::string > argv
Definition: process.hh:220
This class represents the return value from an emulated system call, including any errno setting...
RegVal getSyscallArg(ThreadContext *tc, int &i) override
Definition: process.cc:276
Addr getBias()
Definition: process.cc:524
MemoryImage image
Definition: process.hh:218
uint64_t egid()
Definition: process.hh:90

Generated on Fri Feb 28 2020 16:26:56 for gem5 by doxygen 1.8.13