2 * Copyright (c) 2007-2008 The Florida State University
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.
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.
28 * Authors: Stephen Hines
31 #include "arch/arm/isa_traits.hh"
32 #include "arch/arm/process.hh"
33 #include "arch/arm/types.hh"
34 #include "base/loader/elf_object.hh"
35 #include "base/loader/object_file.hh"
36 #include "base/misc.hh"
37 #include "cpu/thread_context.hh"
38 #include "mem/page_table.hh"
39 #include "mem/translating_port.hh"
40 #include "sim/process_impl.hh"
41 #include "sim/system.hh"
44 using namespace ArmISA
;
46 ArmLiveProcess::ArmLiveProcess(LiveProcessParams
*params
, ObjectFile
*objFile
)
47 : LiveProcess(params
, objFile
)
49 stack_base
= 0xbf000000L
;
51 // Set pointer for next thread stack. Reserve 8M for main stack.
52 next_thread_stack_base
= stack_base
- (8 * 1024 * 1024);
54 // Set up break point (Top of Heap)
55 brk_point
= objFile
->dataBase() + objFile
->dataSize() + objFile
->bssSize();
56 brk_point
= roundUp(brk_point
, VMPageSize
);
58 // Set up region for mmaps. For now, start at bottom of kuseg space.
59 mmap_start
= mmap_end
= 0x70000000L
;
63 ArmLiveProcess::startup()
65 argsInit(MachineBytes
, VMPageSize
);
69 ArmLiveProcess::copyStringArray32(std::vector
<std::string
> &strings
,
70 Addr array_ptr
, Addr data_ptr
,
71 TranslatingPort
* memPort
)
74 for (int i
= 0; i
< strings
.size(); ++i
) {
75 data_ptr_swap
= htog(data_ptr
);
76 memPort
->writeBlob(array_ptr
, (uint8_t*)&data_ptr_swap
,
78 memPort
->writeString(data_ptr
, strings
[i
].c_str());
79 array_ptr
+= sizeof(uint32_t);
80 data_ptr
+= strings
[i
].size() + 1;
82 // add NULL terminator
85 memPort
->writeBlob(array_ptr
, (uint8_t*)&data_ptr
, sizeof(uint32_t));
89 ArmLiveProcess::argsInit(int intSize
, int pageSize
)
91 typedef AuxVector
<uint32_t> auxv_t
;
92 std::vector
<auxv_t
> auxv
;
100 //We want 16 byte alignment
103 // Overloaded argsInit so that we can fine-tune for ARM architecture
106 // load object file into target memory
107 objFile
->loadSections(initVirtMem
);
114 Arm_FastMult
= 1 << 4,
123 //Setup the auxilliary vectors. These will already have endian conversion.
124 //Auxilliary vectors are loaded only for elf formatted executables.
125 ElfObject
* elfObject
= dynamic_cast<ElfObject
*>(objFile
);
141 //Bits which describe the system hardware capabilities
142 //XXX Figure out what these should be
143 auxv
.push_back(auxv_t(M5_AT_HWCAP
, features
));
144 //The system page size
145 auxv
.push_back(auxv_t(M5_AT_PAGESZ
, ArmISA::VMPageSize
));
146 //Frequency at which times() increments
147 auxv
.push_back(auxv_t(M5_AT_CLKTCK
, 0x64));
148 // For statically linked executables, this is the virtual address of the
149 // program header tables if they appear in the executable image
150 auxv
.push_back(auxv_t(M5_AT_PHDR
, elfObject
->programHeaderTable()));
151 // This is the size of a program header entry from the elf file.
152 auxv
.push_back(auxv_t(M5_AT_PHENT
, elfObject
->programHeaderSize()));
153 // This is the number of program headers from the original elf file.
154 auxv
.push_back(auxv_t(M5_AT_PHNUM
, elfObject
->programHeaderCount()));
155 //This is the address of the elf "interpreter", It should be set
156 //to 0 for regular executables. It should be something else
157 //(not sure what) for dynamic libraries.
158 auxv
.push_back(auxv_t(M5_AT_BASE
, 0));
160 //XXX Figure out what this should be.
161 auxv
.push_back(auxv_t(M5_AT_FLAGS
, 0));
162 //The entry point to the program
163 auxv
.push_back(auxv_t(M5_AT_ENTRY
, objFile
->entryPoint()));
164 //Different user and group IDs
165 auxv
.push_back(auxv_t(M5_AT_UID
, uid()));
166 auxv
.push_back(auxv_t(M5_AT_EUID
, euid()));
167 auxv
.push_back(auxv_t(M5_AT_GID
, gid()));
168 auxv
.push_back(auxv_t(M5_AT_EGID
, egid()));
169 //Whether to enable "secure mode" in the executable
170 auxv
.push_back(auxv_t(M5_AT_SECURE
, 0));
171 //The filename of the program
172 auxv
.push_back(auxv_t(M5_AT_EXECFN
, 0));
173 //The string "v51" with unknown meaning
174 auxv
.push_back(auxv_t(M5_AT_PLATFORM
, 0));
177 //Figure out how big the initial stack nedes to be
179 // A sentry NULL void pointer at the top of the stack.
180 int sentry_size
= intSize
;
182 string platform
= "v51";
183 int platform_size
= platform
.size() + 1;
185 // The aux vectors are put on the stack in two groups. The first group are
186 // the vectors that are generated as the elf is loaded. The second group
187 // are the ones that were computed ahead of time and include the platform
189 int aux_data_size
= filename
.size() + 1;
191 int env_data_size
= 0;
192 for (int i
= 0; i
< envp
.size(); ++i
) {
193 env_data_size
+= envp
[i
].size() + 1;
195 int arg_data_size
= 0;
196 for (int i
= 0; i
< argv
.size(); ++i
) {
197 arg_data_size
+= argv
[i
].size() + 1;
200 int info_block_size
=
201 sentry_size
+ env_data_size
+ arg_data_size
+
202 aux_data_size
+ platform_size
;
204 //Each auxilliary vector is two 4 byte words
205 int aux_array_size
= intSize
* 2 * (auxv
.size() + 1);
207 int envp_array_size
= intSize
* (envp
.size() + 1);
208 int argv_array_size
= intSize
* (argv
.size() + 1);
210 int argc_size
= intSize
;
212 //Figure out the size of the contents of the actual initial frame
220 //There needs to be padding after the auxiliary vector data so that the
221 //very bottom of the stack is aligned properly.
222 int partial_size
= frame_size
;
223 int aligned_partial_size
= roundUp(partial_size
, align
);
224 int aux_padding
= aligned_partial_size
- partial_size
;
226 int space_needed
= frame_size
+ aux_padding
;
228 stack_min
= stack_base
- space_needed
;
229 stack_min
= roundDown(stack_min
, align
);
230 stack_size
= stack_base
- stack_min
;
233 pTable
->allocate(roundDown(stack_min
, pageSize
),
234 roundUp(stack_size
, pageSize
));
236 // map out initial stack contents
237 uint32_t sentry_base
= stack_base
- sentry_size
;
238 uint32_t aux_data_base
= sentry_base
- aux_data_size
;
239 uint32_t env_data_base
= aux_data_base
- env_data_size
;
240 uint32_t arg_data_base
= env_data_base
- arg_data_size
;
241 uint32_t platform_base
= arg_data_base
- platform_size
;
242 uint32_t auxv_array_base
= platform_base
- aux_array_size
- aux_padding
;
243 uint32_t envp_array_base
= auxv_array_base
- envp_array_size
;
244 uint32_t argv_array_base
= envp_array_base
- argv_array_size
;
245 uint32_t argc_base
= argv_array_base
- argc_size
;
247 DPRINTF(Stack
, "The addresses of items on the initial stack:\n");
248 DPRINTF(Stack
, "0x%x - aux data\n", aux_data_base
);
249 DPRINTF(Stack
, "0x%x - env data\n", env_data_base
);
250 DPRINTF(Stack
, "0x%x - arg data\n", arg_data_base
);
251 DPRINTF(Stack
, "0x%x - platform base\n", platform_base
);
252 DPRINTF(Stack
, "0x%x - auxv array\n", auxv_array_base
);
253 DPRINTF(Stack
, "0x%x - envp array\n", envp_array_base
);
254 DPRINTF(Stack
, "0x%x - argv array\n", argv_array_base
);
255 DPRINTF(Stack
, "0x%x - argc \n", argc_base
);
256 DPRINTF(Stack
, "0x%x - stack min\n", stack_min
);
258 // write contents to stack
261 uint32_t argc
= argv
.size();
262 uint32_t guestArgc
= ArmISA::htog(argc
);
264 //Write out the sentry void *
265 uint32_t sentry_NULL
= 0;
266 initVirtMem
->writeBlob(sentry_base
,
267 (uint8_t*)&sentry_NULL
, sentry_size
);
269 //Fix up the aux vectors which point to other data
270 for (int i
= auxv
.size() - 1; i
>= 0; i
--) {
271 if (auxv
[i
].a_type
== M5_AT_PLATFORM
) {
272 auxv
[i
].a_val
= platform_base
;
273 initVirtMem
->writeString(platform_base
, platform
.c_str());
274 } else if (auxv
[i
].a_type
== M5_AT_EXECFN
) {
275 auxv
[i
].a_val
= aux_data_base
;
276 initVirtMem
->writeString(aux_data_base
, filename
.c_str());
281 for(int x
= 0; x
< auxv
.size(); x
++)
283 initVirtMem
->writeBlob(auxv_array_base
+ x
* 2 * intSize
,
284 (uint8_t*)&(auxv
[x
].a_type
), intSize
);
285 initVirtMem
->writeBlob(auxv_array_base
+ (x
* 2 + 1) * intSize
,
286 (uint8_t*)&(auxv
[x
].a_val
), intSize
);
288 //Write out the terminating zeroed auxilliary vector
289 const uint64_t zero
= 0;
290 initVirtMem
->writeBlob(auxv_array_base
+ 2 * intSize
* auxv
.size(),
291 (uint8_t*)&zero
, 2 * intSize
);
293 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
294 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
296 initVirtMem
->writeBlob(argc_base
, (uint8_t*)&guestArgc
, intSize
);
298 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
299 //Set the stack pointer register
300 tc
->setIntReg(StackPointerReg
, stack_min
);
301 //A pointer to a function to run when the program exits. We'll set this
302 //to zero explicitly to make sure this isn't used.
303 tc
->setIntReg(ArgumentReg0
, 0);
304 //Set argument regs 1 and 2 to argv[0] and envp[0] respectively
305 if (argv
.size() > 0) {
306 tc
->setIntReg(ArgumentReg1
, arg_data_base
+ arg_data_size
-
307 argv
[argv
.size() - 1].size() - 1);
309 tc
->setIntReg(ArgumentReg1
, 0);
311 if (envp
.size() > 0) {
312 tc
->setIntReg(ArgumentReg2
, env_data_base
+ env_data_size
-
313 envp
[envp
.size() - 1].size() - 1);
315 tc
->setIntReg(ArgumentReg2
, 0);
318 Addr prog_entry
= objFile
->entryPoint();
319 tc
->setPC(prog_entry
);
320 tc
->setNextPC(prog_entry
+ sizeof(MachInst
));
322 //Align the "stack_min" to a page boundary.
323 stack_min
= roundDown(stack_min
, pageSize
);
327 ArmLiveProcess::getSyscallArg(ThreadContext
*tc
, int &i
)
330 return tc
->readIntReg(ArgumentReg0
+ i
++);
334 ArmLiveProcess::setSyscallArg(ThreadContext
*tc
,
335 int i
, ArmISA::IntReg val
)
338 tc
->setIntReg(ArgumentReg0
+ i
, val
);
342 ArmLiveProcess::setSyscallReturn(ThreadContext
*tc
,
343 SyscallReturn return_value
)
345 tc
->setIntReg(ReturnValueReg
, return_value
.value());