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.
30 #include "arch/power/process.hh"
32 #include "arch/power/isa_traits.hh"
33 #include "arch/power/types.hh"
34 #include "base/loader/elf_object.hh"
35 #include "base/loader/object_file.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"
46 using namespace PowerISA
;
48 PowerProcess::PowerProcess(
49 const ProcessParams
¶ms
, ::Loader::ObjectFile
*objFile
)
51 new EmulationPageTable(params
.name
, params
.pid
, PageBytes
),
54 fatal_if(params
.useArchPT
, "Arch page tables not implemented.");
55 // Set up break point (Top of Heap)
56 Addr brk_point
= image
.maxAddr();
57 brk_point
= roundUp(brk_point
, PageBytes
);
59 Addr stack_base
= 0xbf000000L
;
61 Addr max_stack_size
= 8 * 1024 * 1024;
63 // Set pointer for next thread stack. Reserve 8M for main stack.
64 Addr next_thread_stack_base
= stack_base
- max_stack_size
;
66 // Set up region for mmaps. For now, start at bottom of kuseg space.
67 Addr mmap_end
= 0x70000000L
;
69 memState
= std::make_shared
<MemState
>(
70 this, brk_point
, stack_base
, max_stack_size
,
71 next_thread_stack_base
, mmap_end
);
75 PowerProcess::initState()
79 if (objFile
->getArch() == ::Loader::Power
)
80 argsInit
<uint32_t>(PageBytes
);
82 argsInit
<uint64_t>(PageBytes
);
84 // Fix up entry point and symbol table for 64-bit ELF ABI v1
85 if (objFile
->getOpSys() != ::Loader::LinuxPower64ABIv1
)
88 // Fix entry point address and the base TOC pointer by looking the
89 // the function descriptor in the .opd section
90 Addr entryPoint
, tocBase
;
91 ByteOrder byteOrder
= objFile
->getByteOrder();
92 ThreadContext
*tc
= system
->threads
[contextIds
[0]];
94 // The first doubleword of the descriptor contains the address of the
95 // entry point of the function
96 initVirtMem
->readBlob(getStartPC(), &entryPoint
, sizeof(Addr
));
98 // Update the PC state
99 auto pc
= tc
->pcState();
100 pc
.byteOrder(byteOrder
);
101 pc
.set(gtoh(entryPoint
, byteOrder
));
104 // The second doubleword of the descriptor contains the TOC base
105 // address for the function
106 initVirtMem
->readBlob(getStartPC() + 8, &tocBase
, sizeof(Addr
));
107 tc
->setIntReg(TOCPointerReg
, gtoh(tocBase
, byteOrder
));
109 // Fix symbol table entries as they would otherwise point to the
110 // function descriptor rather than the actual entry point address
111 auto *symbolTable
= new ::Loader::SymbolTable
;
113 for (auto sym
: ::Loader::debugSymbolTable
) {
115 ::Loader::Symbol symbol
= sym
;
117 // Try to read entry point from function descriptor
118 if (initVirtMem
->tryReadBlob(sym
.address
, &entry
, sizeof(Addr
)))
119 symbol
.address
= gtoh(entry
, byteOrder
);
121 symbolTable
->insert(symbol
);
124 // Replace the current debug symbol table
125 ::Loader::debugSymbolTable
.clear();
126 ::Loader::debugSymbolTable
.insert(*symbolTable
);
130 template <typename IntType
>
132 PowerProcess::argsInit(int pageSize
)
134 int intSize
= sizeof(IntType
);
135 ByteOrder byteOrder
= objFile
->getByteOrder();
136 bool is64bit
= (objFile
->getArch() == ::Loader::Power64
);
137 bool isLittleEndian
= (byteOrder
== ByteOrder::little
);
138 std::vector
<AuxVector
<IntType
>> auxv
;
140 std::string filename
;
146 //We want 16 byte alignment
149 // load object file into target memory
150 image
.write(*initVirtMem
);
151 interpImage
.write(*initVirtMem
);
153 //Setup the auxilliary vectors. These will already have endian conversion.
154 //Auxilliary vectors are loaded only for elf formatted executables.
155 auto *elfObject
= dynamic_cast<::Loader::ElfObject
*>(objFile
);
157 IntType features
= PPC_FEATURE_32
;
159 // Check if running in 64-bit mode
161 features
|= PPC_FEATURE_64
;
163 // Check if running in little endian mode
165 features
|= PPC_FEATURE_PPC_LE
| PPC_FEATURE_TRUE_LE
;
167 //Bits which describe the system hardware capabilities
168 //XXX Figure out what these should be
169 auxv
.emplace_back(M5_AT_HWCAP
, features
);
170 //The system page size
171 auxv
.emplace_back(M5_AT_PAGESZ
, pageSize
);
172 //Frequency at which times() increments
173 auxv
.emplace_back(M5_AT_CLKTCK
, 0x64);
174 // For statically linked executables, this is the virtual address of
175 // the program header tables if they appear in the executable image
176 auxv
.emplace_back(M5_AT_PHDR
, elfObject
->programHeaderTable());
177 // This is the size of a program header entry from the elf file.
178 auxv
.emplace_back(M5_AT_PHENT
, elfObject
->programHeaderSize());
179 // This is the number of program headers from the original elf file.
180 auxv
.emplace_back(M5_AT_PHNUM
, elfObject
->programHeaderCount());
181 // This is the base address of the ELF interpreter; it should be
182 // zero for static executables or contain the base address for
183 // dynamic executables.
184 auxv
.emplace_back(M5_AT_BASE
, getBias());
185 //XXX Figure out what this should be.
186 auxv
.emplace_back(M5_AT_FLAGS
, 0);
187 //The entry point to the program
188 auxv
.emplace_back(M5_AT_ENTRY
, objFile
->entryPoint());
189 //Different user and group IDs
190 auxv
.emplace_back(M5_AT_UID
, uid());
191 auxv
.emplace_back(M5_AT_EUID
, euid());
192 auxv
.emplace_back(M5_AT_GID
, gid());
193 auxv
.emplace_back(M5_AT_EGID
, egid());
194 //Whether to enable "secure mode" in the executable
195 auxv
.emplace_back(M5_AT_SECURE
, 0);
196 //The address of 16 "random" bytes
197 auxv
.emplace_back(M5_AT_RANDOM
, 0);
198 //The filename of the program
199 auxv
.emplace_back(M5_AT_EXECFN
, 0);
200 //The string "v51" with unknown meaning
201 auxv
.emplace_back(M5_AT_PLATFORM
, 0);
204 //Figure out how big the initial stack nedes to be
206 // A sentry NULL void pointer at the top of the stack.
207 int sentry_size
= intSize
;
209 std::string platform
= "v51";
210 int platform_size
= platform
.size() + 1;
212 // The aux vectors are put on the stack in two groups. The first group are
213 // the vectors that are generated as the elf is loaded. The second group
214 // are the ones that were computed ahead of time and include the platform
216 int aux_data_size
= filename
.size() + 1;
218 const int numRandomBytes
= 16;
219 aux_data_size
+= numRandomBytes
;
221 int env_data_size
= 0;
222 for (int i
= 0; i
< envp
.size(); ++i
) {
223 env_data_size
+= envp
[i
].size() + 1;
225 int arg_data_size
= 0;
226 for (int i
= 0; i
< argv
.size(); ++i
) {
227 arg_data_size
+= argv
[i
].size() + 1;
230 int info_block_size
=
231 sentry_size
+ env_data_size
+ arg_data_size
+
232 aux_data_size
+ platform_size
;
234 //Each auxilliary vector is two 4 byte words
235 int aux_array_size
= intSize
* 2 * (auxv
.size() + 1);
237 int envp_array_size
= intSize
* (envp
.size() + 1);
238 int argv_array_size
= intSize
* (argv
.size() + 1);
240 int argc_size
= intSize
;
242 //Figure out the size of the contents of the actual initial frame
250 //There needs to be padding after the auxiliary vector data so that the
251 //very bottom of the stack is aligned properly.
252 int partial_size
= frame_size
;
253 int aligned_partial_size
= roundUp(partial_size
, align
);
254 int aux_padding
= aligned_partial_size
- partial_size
;
256 int space_needed
= frame_size
+ aux_padding
;
258 Addr stack_min
= memState
->getStackBase() - space_needed
;
259 stack_min
= roundDown(stack_min
, align
);
261 memState
->setStackSize(memState
->getStackBase() - stack_min
);
264 memState
->mapRegion(roundDown(stack_min
, pageSize
),
265 roundUp(memState
->getStackSize(), pageSize
), "stack");
267 // map out initial stack contents
268 IntType sentry_base
= memState
->getStackBase() - sentry_size
;
269 IntType aux_data_base
= sentry_base
- aux_data_size
;
270 IntType env_data_base
= aux_data_base
- env_data_size
;
271 IntType arg_data_base
= env_data_base
- arg_data_size
;
272 IntType platform_base
= arg_data_base
- platform_size
;
273 IntType auxv_array_base
= platform_base
- aux_array_size
- aux_padding
;
274 IntType envp_array_base
= auxv_array_base
- envp_array_size
;
275 IntType argv_array_base
= envp_array_base
- argv_array_size
;
276 IntType argc_base
= argv_array_base
- argc_size
;
278 DPRINTF(Stack
, "The addresses of items on the initial stack:\n");
279 DPRINTF(Stack
, "0x%x - aux data\n", aux_data_base
);
280 DPRINTF(Stack
, "0x%x - env data\n", env_data_base
);
281 DPRINTF(Stack
, "0x%x - arg data\n", arg_data_base
);
282 DPRINTF(Stack
, "0x%x - platform base\n", platform_base
);
283 DPRINTF(Stack
, "0x%x - auxv array\n", auxv_array_base
);
284 DPRINTF(Stack
, "0x%x - envp array\n", envp_array_base
);
285 DPRINTF(Stack
, "0x%x - argv array\n", argv_array_base
);
286 DPRINTF(Stack
, "0x%x - argc \n", argc_base
);
287 DPRINTF(Stack
, "0x%x - stack min\n", stack_min
);
289 // write contents to stack
292 IntType argc
= argv
.size();
293 IntType guestArgc
= htog(argc
, byteOrder
);
295 //Write out the sentry void *
296 IntType sentry_NULL
= 0;
297 initVirtMem
->writeBlob(sentry_base
, &sentry_NULL
, sentry_size
);
299 //Fix up the aux vectors which point to other data
300 for (int i
= auxv
.size() - 1; i
>= 0; i
--) {
301 if (auxv
[i
].type
== M5_AT_PLATFORM
) {
302 auxv
[i
].val
= platform_base
;
303 initVirtMem
->writeString(platform_base
, platform
.c_str());
304 } else if (auxv
[i
].type
== M5_AT_EXECFN
) {
305 auxv
[i
].val
= aux_data_base
+ numRandomBytes
;
306 initVirtMem
->writeString(aux_data_base
, filename
.c_str());
307 } else if (auxv
[i
].type
== M5_AT_RANDOM
) {
308 auxv
[i
].val
= aux_data_base
;
313 Addr auxv_array_end
= auxv_array_base
;
314 for (const auto &aux
: auxv
) {
315 initVirtMem
->write(auxv_array_end
, aux
, byteOrder
);
316 auxv_array_end
+= sizeof(aux
);
318 //Write out the terminating zeroed auxilliary vector
319 const AuxVector
<uint64_t> zero(0, 0);
320 initVirtMem
->write(auxv_array_end
, zero
);
321 auxv_array_end
+= sizeof(zero
);
323 copyStringArray(envp
, envp_array_base
, env_data_base
,
324 byteOrder
, *initVirtMem
);
325 copyStringArray(argv
, argv_array_base
, arg_data_base
,
326 byteOrder
, *initVirtMem
);
328 initVirtMem
->writeBlob(argc_base
, &guestArgc
, intSize
);
330 ThreadContext
*tc
= system
->threads
[contextIds
[0]];
332 //Set the stack pointer register
333 tc
->setIntReg(StackPointerReg
, stack_min
);
335 //Set the machine status for a typical userspace
345 msr
.le
= isLittleEndian
;
346 tc
->setMiscReg(MISCREG_MSR
, msr
);
348 auto pc
= tc
->pcState();
349 pc
.set(getStartPC());
350 pc
.byteOrder(byteOrder
);
353 //Align the "stack_min" to a page boundary.
354 memState
->setStackMin(roundDown(stack_min
, pageSize
));