2 * Copyright (c) 2007-2008 The Florida State University
3 * Copyright (c) 2009 The University of Edinburgh
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.
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.
29 * Authors: Stephen Hines
33 #include "arch/power/isa_traits.hh"
34 #include "arch/power/process.hh"
35 #include "arch/power/types.hh"
36 #include "base/loader/elf_object.hh"
37 #include "base/loader/object_file.hh"
38 #include "base/misc.hh"
39 #include "cpu/thread_context.hh"
40 #include "debug/Stack.hh"
41 #include "mem/page_table.hh"
42 #include "sim/process_impl.hh"
43 #include "sim/system.hh"
46 using namespace PowerISA
;
48 PowerLiveProcess::PowerLiveProcess(LiveProcessParams
*params
,
50 : LiveProcess(params
, objFile
)
52 stack_base
= 0xbf000000L
;
54 // Set pointer for next thread stack. Reserve 8M for main stack.
55 next_thread_stack_base
= stack_base
- (8 * 1024 * 1024);
57 // Set up break point (Top of Heap)
58 brk_point
= objFile
->dataBase() + objFile
->dataSize() + objFile
->bssSize();
59 brk_point
= roundUp(brk_point
, VMPageSize
);
61 // Set up region for mmaps. For now, start at bottom of kuseg space.
62 mmap_start
= mmap_end
= 0x70000000L
;
66 PowerLiveProcess::initState()
70 argsInit(MachineBytes
, VMPageSize
);
74 PowerLiveProcess::argsInit(int intSize
, int pageSize
)
76 typedef AuxVector
<uint32_t> auxv_t
;
77 std::vector
<auxv_t
> auxv
;
85 //We want 16 byte alignment
88 // load object file into target memory
89 objFile
->loadSections(initVirtMem
);
91 //Setup the auxilliary vectors. These will already have endian conversion.
92 //Auxilliary vectors are loaded only for elf formatted executables.
93 ElfObject
* elfObject
= dynamic_cast<ElfObject
*>(objFile
);
95 uint32_t features
= 0;
97 //Bits which describe the system hardware capabilities
98 //XXX Figure out what these should be
99 auxv
.push_back(auxv_t(M5_AT_HWCAP
, features
));
100 //The system page size
101 auxv
.push_back(auxv_t(M5_AT_PAGESZ
, PowerISA::VMPageSize
));
102 //Frequency at which times() increments
103 auxv
.push_back(auxv_t(M5_AT_CLKTCK
, 0x64));
104 // For statically linked executables, this is the virtual address of the
105 // program header tables if they appear in the executable image
106 auxv
.push_back(auxv_t(M5_AT_PHDR
, elfObject
->programHeaderTable()));
107 // This is the size of a program header entry from the elf file.
108 auxv
.push_back(auxv_t(M5_AT_PHENT
, elfObject
->programHeaderSize()));
109 // This is the number of program headers from the original elf file.
110 auxv
.push_back(auxv_t(M5_AT_PHNUM
, elfObject
->programHeaderCount()));
111 //This is the address of the elf "interpreter", It should be set
112 //to 0 for regular executables. It should be something else
113 //(not sure what) for dynamic libraries.
114 auxv
.push_back(auxv_t(M5_AT_BASE
, 0));
116 //XXX Figure out what this should be.
117 auxv
.push_back(auxv_t(M5_AT_FLAGS
, 0));
118 //The entry point to the program
119 auxv
.push_back(auxv_t(M5_AT_ENTRY
, objFile
->entryPoint()));
120 //Different user and group IDs
121 auxv
.push_back(auxv_t(M5_AT_UID
, uid()));
122 auxv
.push_back(auxv_t(M5_AT_EUID
, euid()));
123 auxv
.push_back(auxv_t(M5_AT_GID
, gid()));
124 auxv
.push_back(auxv_t(M5_AT_EGID
, egid()));
125 //Whether to enable "secure mode" in the executable
126 auxv
.push_back(auxv_t(M5_AT_SECURE
, 0));
127 //The filename of the program
128 auxv
.push_back(auxv_t(M5_AT_EXECFN
, 0));
129 //The string "v51" with unknown meaning
130 auxv
.push_back(auxv_t(M5_AT_PLATFORM
, 0));
133 //Figure out how big the initial stack nedes to be
135 // A sentry NULL void pointer at the top of the stack.
136 int sentry_size
= intSize
;
138 string platform
= "v51";
139 int platform_size
= platform
.size() + 1;
141 // The aux vectors are put on the stack in two groups. The first group are
142 // the vectors that are generated as the elf is loaded. The second group
143 // are the ones that were computed ahead of time and include the platform
145 int aux_data_size
= filename
.size() + 1;
147 int env_data_size
= 0;
148 for (int i
= 0; i
< envp
.size(); ++i
) {
149 env_data_size
+= envp
[i
].size() + 1;
151 int arg_data_size
= 0;
152 for (int i
= 0; i
< argv
.size(); ++i
) {
153 arg_data_size
+= argv
[i
].size() + 1;
156 int info_block_size
=
157 sentry_size
+ env_data_size
+ arg_data_size
+
158 aux_data_size
+ platform_size
;
160 //Each auxilliary vector is two 4 byte words
161 int aux_array_size
= intSize
* 2 * (auxv
.size() + 1);
163 int envp_array_size
= intSize
* (envp
.size() + 1);
164 int argv_array_size
= intSize
* (argv
.size() + 1);
166 int argc_size
= intSize
;
168 //Figure out the size of the contents of the actual initial frame
176 //There needs to be padding after the auxiliary vector data so that the
177 //very bottom of the stack is aligned properly.
178 int partial_size
= frame_size
;
179 int aligned_partial_size
= roundUp(partial_size
, align
);
180 int aux_padding
= aligned_partial_size
- partial_size
;
182 int space_needed
= frame_size
+ aux_padding
;
184 stack_min
= stack_base
- space_needed
;
185 stack_min
= roundDown(stack_min
, align
);
186 stack_size
= stack_base
- stack_min
;
189 allocateMem(roundDown(stack_min
, pageSize
), roundUp(stack_size
, pageSize
));
191 // map out initial stack contents
192 uint32_t sentry_base
= stack_base
- sentry_size
;
193 uint32_t aux_data_base
= sentry_base
- aux_data_size
;
194 uint32_t env_data_base
= aux_data_base
- env_data_size
;
195 uint32_t arg_data_base
= env_data_base
- arg_data_size
;
196 uint32_t platform_base
= arg_data_base
- platform_size
;
197 uint32_t auxv_array_base
= platform_base
- aux_array_size
- aux_padding
;
198 uint32_t envp_array_base
= auxv_array_base
- envp_array_size
;
199 uint32_t argv_array_base
= envp_array_base
- argv_array_size
;
200 uint32_t argc_base
= argv_array_base
- argc_size
;
202 DPRINTF(Stack
, "The addresses of items on the initial stack:\n");
203 DPRINTF(Stack
, "0x%x - aux data\n", aux_data_base
);
204 DPRINTF(Stack
, "0x%x - env data\n", env_data_base
);
205 DPRINTF(Stack
, "0x%x - arg data\n", arg_data_base
);
206 DPRINTF(Stack
, "0x%x - platform base\n", platform_base
);
207 DPRINTF(Stack
, "0x%x - auxv array\n", auxv_array_base
);
208 DPRINTF(Stack
, "0x%x - envp array\n", envp_array_base
);
209 DPRINTF(Stack
, "0x%x - argv array\n", argv_array_base
);
210 DPRINTF(Stack
, "0x%x - argc \n", argc_base
);
211 DPRINTF(Stack
, "0x%x - stack min\n", stack_min
);
213 // write contents to stack
216 uint32_t argc
= argv
.size();
217 uint32_t guestArgc
= PowerISA::htog(argc
);
219 //Write out the sentry void *
220 uint32_t sentry_NULL
= 0;
221 initVirtMem
.writeBlob(sentry_base
,
222 (uint8_t*)&sentry_NULL
, sentry_size
);
224 //Fix up the aux vectors which point to other data
225 for (int i
= auxv
.size() - 1; i
>= 0; i
--) {
226 if (auxv
[i
].a_type
== M5_AT_PLATFORM
) {
227 auxv
[i
].a_val
= platform_base
;
228 initVirtMem
.writeString(platform_base
, platform
.c_str());
229 } else if (auxv
[i
].a_type
== M5_AT_EXECFN
) {
230 auxv
[i
].a_val
= aux_data_base
;
231 initVirtMem
.writeString(aux_data_base
, filename
.c_str());
236 for (int x
= 0; x
< auxv
.size(); x
++)
238 initVirtMem
.writeBlob(auxv_array_base
+ x
* 2 * intSize
,
239 (uint8_t*)&(auxv
[x
].a_type
), intSize
);
240 initVirtMem
.writeBlob(auxv_array_base
+ (x
* 2 + 1) * intSize
,
241 (uint8_t*)&(auxv
[x
].a_val
), intSize
);
243 //Write out the terminating zeroed auxilliary vector
244 const uint64_t zero
= 0;
245 initVirtMem
.writeBlob(auxv_array_base
+ 2 * intSize
* auxv
.size(),
246 (uint8_t*)&zero
, 2 * intSize
);
248 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
249 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
251 initVirtMem
.writeBlob(argc_base
, (uint8_t*)&guestArgc
, intSize
);
253 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
255 //Set the stack pointer register
256 tc
->setIntReg(StackPointerReg
, stack_min
);
258 tc
->pcState(objFile
->entryPoint());
260 //Align the "stack_min" to a page boundary.
261 stack_min
= roundDown(stack_min
, pageSize
);
265 PowerLiveProcess::getSyscallArg(ThreadContext
*tc
, int &i
)
268 return tc
->readIntReg(ArgumentReg0
+ i
++);
272 PowerLiveProcess::setSyscallArg(ThreadContext
*tc
,
273 int i
, PowerISA::IntReg val
)
276 tc
->setIntReg(ArgumentReg0
+ i
, val
);
280 PowerLiveProcess::setSyscallReturn(ThreadContext
*tc
,
281 SyscallReturn return_value
)
283 Cr cr
= tc
->readIntReg(INTREG_CR
);
284 if (return_value
.successful()) {
289 tc
->setIntReg(INTREG_CR
, cr
);
290 tc
->setIntReg(ReturnValueReg
, return_value
.value());