process.cc (8852:c744483edfcf) process.cc (10037:5cac77888310)
1/*
1/*
2 * Copyright (c) 2010 ARM Limited
2 * Copyright (c) 2010, 2012 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2007-2008 The Florida State University
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Stephen Hines
41 * Ali Saidi
42 */
43
44#include "arch/arm/isa_traits.hh"
45#include "arch/arm/process.hh"
46#include "arch/arm/types.hh"
47#include "base/loader/elf_object.hh"
48#include "base/loader/object_file.hh"
49#include "base/misc.hh"
50#include "cpu/thread_context.hh"
51#include "debug/Stack.hh"
52#include "mem/page_table.hh"
53#include "sim/byteswap.hh"
54#include "sim/process_impl.hh"
55#include "sim/system.hh"
56
57using namespace std;
58using namespace ArmISA;
59
60ArmLiveProcess::ArmLiveProcess(LiveProcessParams *params, ObjectFile *objFile,
61 ObjectFile::Arch _arch)
62 : LiveProcess(params, objFile), arch(_arch)
63{
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2007-2008 The Florida State University
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Stephen Hines
41 * Ali Saidi
42 */
43
44#include "arch/arm/isa_traits.hh"
45#include "arch/arm/process.hh"
46#include "arch/arm/types.hh"
47#include "base/loader/elf_object.hh"
48#include "base/loader/object_file.hh"
49#include "base/misc.hh"
50#include "cpu/thread_context.hh"
51#include "debug/Stack.hh"
52#include "mem/page_table.hh"
53#include "sim/byteswap.hh"
54#include "sim/process_impl.hh"
55#include "sim/system.hh"
56
57using namespace std;
58using namespace ArmISA;
59
60ArmLiveProcess::ArmLiveProcess(LiveProcessParams *params, ObjectFile *objFile,
61 ObjectFile::Arch _arch)
62 : LiveProcess(params, objFile), arch(_arch)
63{
64}
65
66ArmLiveProcess32::ArmLiveProcess32(LiveProcessParams *params,
67 ObjectFile *objFile, ObjectFile::Arch _arch)
68 : ArmLiveProcess(params, objFile, _arch)
69{
64 stack_base = 0xbf000000L;
65
66 // Set pointer for next thread stack. Reserve 8M for main stack.
67 next_thread_stack_base = stack_base - (8 * 1024 * 1024);
68
69 // Set up break point (Top of Heap)
70 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
71 brk_point = roundUp(brk_point, VMPageSize);
72
73 // Set up region for mmaps. For now, start at bottom of kuseg space.
74 mmap_start = mmap_end = 0x40000000L;
75}
76
70 stack_base = 0xbf000000L;
71
72 // Set pointer for next thread stack. Reserve 8M for main stack.
73 next_thread_stack_base = stack_base - (8 * 1024 * 1024);
74
75 // Set up break point (Top of Heap)
76 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
77 brk_point = roundUp(brk_point, VMPageSize);
78
79 // Set up region for mmaps. For now, start at bottom of kuseg space.
80 mmap_start = mmap_end = 0x40000000L;
81}
82
83ArmLiveProcess64::ArmLiveProcess64(LiveProcessParams *params,
84 ObjectFile *objFile, ObjectFile::Arch _arch)
85 : ArmLiveProcess(params, objFile, _arch)
86{
87 stack_base = 0x7fffff0000L;
88
89 // Set pointer for next thread stack. Reserve 8M for main stack.
90 next_thread_stack_base = stack_base - (8 * 1024 * 1024);
91
92 // Set up break point (Top of Heap)
93 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
94 brk_point = roundUp(brk_point, VMPageSize);
95
96 // Set up region for mmaps. For now, start at bottom of kuseg space.
97 mmap_start = mmap_end = 0x4000000000L;
98}
99
77void
100void
78ArmLiveProcess::initState()
101ArmLiveProcess32::initState()
79{
80 LiveProcess::initState();
102{
103 LiveProcess::initState();
81 argsInit(MachineBytes, VMPageSize);
104 argsInit<uint32_t>(VMPageSize, INTREG_SP);
82 for (int i = 0; i < contextIds.size(); i++) {
83 ThreadContext * tc = system->getThreadContext(contextIds[i]);
84 CPACR cpacr = tc->readMiscReg(MISCREG_CPACR);
85 // Enable the floating point coprocessors.
86 cpacr.cp10 = 0x3;
87 cpacr.cp11 = 0x3;
88 tc->setMiscReg(MISCREG_CPACR, cpacr);
89 // Generically enable floating point support.
90 FPEXC fpexc = tc->readMiscReg(MISCREG_FPEXC);
91 fpexc.en = 1;
92 tc->setMiscReg(MISCREG_FPEXC, fpexc);
93 }
94}
95
96void
105 for (int i = 0; i < contextIds.size(); i++) {
106 ThreadContext * tc = system->getThreadContext(contextIds[i]);
107 CPACR cpacr = tc->readMiscReg(MISCREG_CPACR);
108 // Enable the floating point coprocessors.
109 cpacr.cp10 = 0x3;
110 cpacr.cp11 = 0x3;
111 tc->setMiscReg(MISCREG_CPACR, cpacr);
112 // Generically enable floating point support.
113 FPEXC fpexc = tc->readMiscReg(MISCREG_FPEXC);
114 fpexc.en = 1;
115 tc->setMiscReg(MISCREG_FPEXC, fpexc);
116 }
117}
118
119void
97ArmLiveProcess::argsInit(int intSize, int pageSize)
120ArmLiveProcess64::initState()
98{
121{
99 typedef AuxVector<uint32_t> auxv_t;
122 LiveProcess::initState();
123 argsInit<uint64_t>(VMPageSize, INTREG_SP0);
124 for (int i = 0; i < contextIds.size(); i++) {
125 ThreadContext * tc = system->getThreadContext(contextIds[i]);
126 CPSR cpsr = tc->readMiscReg(MISCREG_CPSR);
127 cpsr.mode = MODE_EL0T;
128 tc->setMiscReg(MISCREG_CPSR, cpsr);
129 CPACR cpacr = tc->readMiscReg(MISCREG_CPACR_EL1);
130 // Enable the floating point coprocessors.
131 cpacr.cp10 = 0x3;
132 cpacr.cp11 = 0x3;
133 tc->setMiscReg(MISCREG_CPACR_EL1, cpacr);
134 // Generically enable floating point support.
135 FPEXC fpexc = tc->readMiscReg(MISCREG_FPEXC);
136 fpexc.en = 1;
137 tc->setMiscReg(MISCREG_FPEXC, fpexc);
138 }
139}
140
141template <class IntType>
142void
143ArmLiveProcess::argsInit(int pageSize, IntRegIndex spIndex)
144{
145 int intSize = sizeof(IntType);
146
147 typedef AuxVector<IntType> auxv_t;
100 std::vector<auxv_t> auxv;
101
102 string filename;
103 if (argv.size() < 1)
104 filename = "";
105 else
106 filename = argv[0];
107
108 //We want 16 byte alignment
109 uint64_t align = 16;
110
111 // load object file into target memory
112 objFile->loadSections(initVirtMem);
113
114 enum ArmCpuFeature {
115 Arm_Swp = 1 << 0,
116 Arm_Half = 1 << 1,
117 Arm_Thumb = 1 << 2,
118 Arm_26Bit = 1 << 3,
119 Arm_FastMult = 1 << 4,
120 Arm_Fpa = 1 << 5,
121 Arm_Vfp = 1 << 6,
122 Arm_Edsp = 1 << 7,
123 Arm_Java = 1 << 8,
124 Arm_Iwmmxt = 1 << 9,
125 Arm_Crunch = 1 << 10,
126 Arm_ThumbEE = 1 << 11,
127 Arm_Neon = 1 << 12,
128 Arm_Vfpv3 = 1 << 13,
129 Arm_Vfpv3d16 = 1 << 14
130 };
131
132 //Setup the auxilliary vectors. These will already have endian conversion.
133 //Auxilliary vectors are loaded only for elf formatted executables.
134 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
135 if (elfObject) {
148 std::vector<auxv_t> auxv;
149
150 string filename;
151 if (argv.size() < 1)
152 filename = "";
153 else
154 filename = argv[0];
155
156 //We want 16 byte alignment
157 uint64_t align = 16;
158
159 // load object file into target memory
160 objFile->loadSections(initVirtMem);
161
162 enum ArmCpuFeature {
163 Arm_Swp = 1 << 0,
164 Arm_Half = 1 << 1,
165 Arm_Thumb = 1 << 2,
166 Arm_26Bit = 1 << 3,
167 Arm_FastMult = 1 << 4,
168 Arm_Fpa = 1 << 5,
169 Arm_Vfp = 1 << 6,
170 Arm_Edsp = 1 << 7,
171 Arm_Java = 1 << 8,
172 Arm_Iwmmxt = 1 << 9,
173 Arm_Crunch = 1 << 10,
174 Arm_ThumbEE = 1 << 11,
175 Arm_Neon = 1 << 12,
176 Arm_Vfpv3 = 1 << 13,
177 Arm_Vfpv3d16 = 1 << 14
178 };
179
180 //Setup the auxilliary vectors. These will already have endian conversion.
181 //Auxilliary vectors are loaded only for elf formatted executables.
182 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
183 if (elfObject) {
136 uint32_t features =
184 IntType features =
137 Arm_Swp |
138 Arm_Half |
139 Arm_Thumb |
140// Arm_26Bit |
141 Arm_FastMult |
142// Arm_Fpa |
143 Arm_Vfp |
144 Arm_Edsp |
145// Arm_Java |
146// Arm_Iwmmxt |
147// Arm_Crunch |
148 Arm_ThumbEE |
149 Arm_Neon |
150 Arm_Vfpv3 |
151 Arm_Vfpv3d16 |
152 0;
153
154 //Bits which describe the system hardware capabilities
155 //XXX Figure out what these should be
156 auxv.push_back(auxv_t(M5_AT_HWCAP, features));
157 //The system page size
158 auxv.push_back(auxv_t(M5_AT_PAGESZ, ArmISA::VMPageSize));
159 //Frequency at which times() increments
160 auxv.push_back(auxv_t(M5_AT_CLKTCK, 0x64));
161 // For statically linked executables, this is the virtual address of the
162 // program header tables if they appear in the executable image
163 auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
164 // This is the size of a program header entry from the elf file.
165 auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
166 // This is the number of program headers from the original elf file.
167 auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
168 //This is the address of the elf "interpreter", It should be set
169 //to 0 for regular executables. It should be something else
170 //(not sure what) for dynamic libraries.
171 auxv.push_back(auxv_t(M5_AT_BASE, 0));
172
173 //XXX Figure out what this should be.
174 auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
175 //The entry point to the program
176 auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
177 //Different user and group IDs
178 auxv.push_back(auxv_t(M5_AT_UID, uid()));
179 auxv.push_back(auxv_t(M5_AT_EUID, euid()));
180 auxv.push_back(auxv_t(M5_AT_GID, gid()));
181 auxv.push_back(auxv_t(M5_AT_EGID, egid()));
182 //Whether to enable "secure mode" in the executable
183 auxv.push_back(auxv_t(M5_AT_SECURE, 0));
184
185 // Pointer to 16 bytes of random data
186 auxv.push_back(auxv_t(M5_AT_RANDOM, 0));
187
188 //The filename of the program
189 auxv.push_back(auxv_t(M5_AT_EXECFN, 0));
190 //The string "v71" -- ARM v7 architecture
191 auxv.push_back(auxv_t(M5_AT_PLATFORM, 0));
192 }
193
194 //Figure out how big the initial stack nedes to be
195
196 // A sentry NULL void pointer at the top of the stack.
197 int sentry_size = intSize;
198
199 string platform = "v71";
200 int platform_size = platform.size() + 1;
201
202 // Bytes for AT_RANDOM above, we'll just keep them 0
203 int aux_random_size = 16; // as per the specification
204
205 // The aux vectors are put on the stack in two groups. The first group are
206 // the vectors that are generated as the elf is loaded. The second group
207 // are the ones that were computed ahead of time and include the platform
208 // string.
209 int aux_data_size = filename.size() + 1;
210
211 int env_data_size = 0;
212 for (int i = 0; i < envp.size(); ++i) {
213 env_data_size += envp[i].size() + 1;
214 }
215 int arg_data_size = 0;
216 for (int i = 0; i < argv.size(); ++i) {
217 arg_data_size += argv[i].size() + 1;
218 }
219
220 int info_block_size =
221 sentry_size + env_data_size + arg_data_size +
222 aux_data_size + platform_size + aux_random_size;
223
224 //Each auxilliary vector is two 4 byte words
225 int aux_array_size = intSize * 2 * (auxv.size() + 1);
226
227 int envp_array_size = intSize * (envp.size() + 1);
228 int argv_array_size = intSize * (argv.size() + 1);
229
230 int argc_size = intSize;
231
232 //Figure out the size of the contents of the actual initial frame
233 int frame_size =
234 info_block_size +
235 aux_array_size +
236 envp_array_size +
237 argv_array_size +
238 argc_size;
239
240 //There needs to be padding after the auxiliary vector data so that the
241 //very bottom of the stack is aligned properly.
242 int partial_size = frame_size;
243 int aligned_partial_size = roundUp(partial_size, align);
244 int aux_padding = aligned_partial_size - partial_size;
245
246 int space_needed = frame_size + aux_padding;
247
248 stack_min = stack_base - space_needed;
249 stack_min = roundDown(stack_min, align);
250 stack_size = stack_base - stack_min;
251
252 // map memory
253 allocateMem(roundDown(stack_min, pageSize), roundUp(stack_size, pageSize));
254
255 // map out initial stack contents
185 Arm_Swp |
186 Arm_Half |
187 Arm_Thumb |
188// Arm_26Bit |
189 Arm_FastMult |
190// Arm_Fpa |
191 Arm_Vfp |
192 Arm_Edsp |
193// Arm_Java |
194// Arm_Iwmmxt |
195// Arm_Crunch |
196 Arm_ThumbEE |
197 Arm_Neon |
198 Arm_Vfpv3 |
199 Arm_Vfpv3d16 |
200 0;
201
202 //Bits which describe the system hardware capabilities
203 //XXX Figure out what these should be
204 auxv.push_back(auxv_t(M5_AT_HWCAP, features));
205 //The system page size
206 auxv.push_back(auxv_t(M5_AT_PAGESZ, ArmISA::VMPageSize));
207 //Frequency at which times() increments
208 auxv.push_back(auxv_t(M5_AT_CLKTCK, 0x64));
209 // For statically linked executables, this is the virtual address of the
210 // program header tables if they appear in the executable image
211 auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
212 // This is the size of a program header entry from the elf file.
213 auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
214 // This is the number of program headers from the original elf file.
215 auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
216 //This is the address of the elf "interpreter", It should be set
217 //to 0 for regular executables. It should be something else
218 //(not sure what) for dynamic libraries.
219 auxv.push_back(auxv_t(M5_AT_BASE, 0));
220
221 //XXX Figure out what this should be.
222 auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
223 //The entry point to the program
224 auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
225 //Different user and group IDs
226 auxv.push_back(auxv_t(M5_AT_UID, uid()));
227 auxv.push_back(auxv_t(M5_AT_EUID, euid()));
228 auxv.push_back(auxv_t(M5_AT_GID, gid()));
229 auxv.push_back(auxv_t(M5_AT_EGID, egid()));
230 //Whether to enable "secure mode" in the executable
231 auxv.push_back(auxv_t(M5_AT_SECURE, 0));
232
233 // Pointer to 16 bytes of random data
234 auxv.push_back(auxv_t(M5_AT_RANDOM, 0));
235
236 //The filename of the program
237 auxv.push_back(auxv_t(M5_AT_EXECFN, 0));
238 //The string "v71" -- ARM v7 architecture
239 auxv.push_back(auxv_t(M5_AT_PLATFORM, 0));
240 }
241
242 //Figure out how big the initial stack nedes to be
243
244 // A sentry NULL void pointer at the top of the stack.
245 int sentry_size = intSize;
246
247 string platform = "v71";
248 int platform_size = platform.size() + 1;
249
250 // Bytes for AT_RANDOM above, we'll just keep them 0
251 int aux_random_size = 16; // as per the specification
252
253 // The aux vectors are put on the stack in two groups. The first group are
254 // the vectors that are generated as the elf is loaded. The second group
255 // are the ones that were computed ahead of time and include the platform
256 // string.
257 int aux_data_size = filename.size() + 1;
258
259 int env_data_size = 0;
260 for (int i = 0; i < envp.size(); ++i) {
261 env_data_size += envp[i].size() + 1;
262 }
263 int arg_data_size = 0;
264 for (int i = 0; i < argv.size(); ++i) {
265 arg_data_size += argv[i].size() + 1;
266 }
267
268 int info_block_size =
269 sentry_size + env_data_size + arg_data_size +
270 aux_data_size + platform_size + aux_random_size;
271
272 //Each auxilliary vector is two 4 byte words
273 int aux_array_size = intSize * 2 * (auxv.size() + 1);
274
275 int envp_array_size = intSize * (envp.size() + 1);
276 int argv_array_size = intSize * (argv.size() + 1);
277
278 int argc_size = intSize;
279
280 //Figure out the size of the contents of the actual initial frame
281 int frame_size =
282 info_block_size +
283 aux_array_size +
284 envp_array_size +
285 argv_array_size +
286 argc_size;
287
288 //There needs to be padding after the auxiliary vector data so that the
289 //very bottom of the stack is aligned properly.
290 int partial_size = frame_size;
291 int aligned_partial_size = roundUp(partial_size, align);
292 int aux_padding = aligned_partial_size - partial_size;
293
294 int space_needed = frame_size + aux_padding;
295
296 stack_min = stack_base - space_needed;
297 stack_min = roundDown(stack_min, align);
298 stack_size = stack_base - stack_min;
299
300 // map memory
301 allocateMem(roundDown(stack_min, pageSize), roundUp(stack_size, pageSize));
302
303 // map out initial stack contents
256 uint32_t sentry_base = stack_base - sentry_size;
257 uint32_t aux_data_base = sentry_base - aux_data_size;
258 uint32_t env_data_base = aux_data_base - env_data_size;
259 uint32_t arg_data_base = env_data_base - arg_data_size;
260 uint32_t platform_base = arg_data_base - platform_size;
261 uint32_t aux_random_base = platform_base - aux_random_size;
262 uint32_t auxv_array_base = aux_random_base - aux_array_size - aux_padding;
263 uint32_t envp_array_base = auxv_array_base - envp_array_size;
264 uint32_t argv_array_base = envp_array_base - argv_array_size;
265 uint32_t argc_base = argv_array_base - argc_size;
304 IntType sentry_base = stack_base - sentry_size;
305 IntType aux_data_base = sentry_base - aux_data_size;
306 IntType env_data_base = aux_data_base - env_data_size;
307 IntType arg_data_base = env_data_base - arg_data_size;
308 IntType platform_base = arg_data_base - platform_size;
309 IntType aux_random_base = platform_base - aux_random_size;
310 IntType auxv_array_base = aux_random_base - aux_array_size - aux_padding;
311 IntType envp_array_base = auxv_array_base - envp_array_size;
312 IntType argv_array_base = envp_array_base - argv_array_size;
313 IntType argc_base = argv_array_base - argc_size;
266
267 DPRINTF(Stack, "The addresses of items on the initial stack:\n");
268 DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
269 DPRINTF(Stack, "0x%x - env data\n", env_data_base);
270 DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
271 DPRINTF(Stack, "0x%x - random data\n", aux_random_base);
272 DPRINTF(Stack, "0x%x - platform base\n", platform_base);
273 DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
274 DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
275 DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
276 DPRINTF(Stack, "0x%x - argc \n", argc_base);
277 DPRINTF(Stack, "0x%x - stack min\n", stack_min);
278
279 // write contents to stack
280
281 // figure out argc
314
315 DPRINTF(Stack, "The addresses of items on the initial stack:\n");
316 DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
317 DPRINTF(Stack, "0x%x - env data\n", env_data_base);
318 DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
319 DPRINTF(Stack, "0x%x - random data\n", aux_random_base);
320 DPRINTF(Stack, "0x%x - platform base\n", platform_base);
321 DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
322 DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
323 DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
324 DPRINTF(Stack, "0x%x - argc \n", argc_base);
325 DPRINTF(Stack, "0x%x - stack min\n", stack_min);
326
327 // write contents to stack
328
329 // figure out argc
282 uint32_t argc = argv.size();
283 uint32_t guestArgc = ArmISA::htog(argc);
330 IntType argc = argv.size();
331 IntType guestArgc = ArmISA::htog(argc);
284
285 //Write out the sentry void *
332
333 //Write out the sentry void *
286 uint32_t sentry_NULL = 0;
334 IntType sentry_NULL = 0;
287 initVirtMem.writeBlob(sentry_base,
288 (uint8_t*)&sentry_NULL, sentry_size);
289
290 //Fix up the aux vectors which point to other data
291 for (int i = auxv.size() - 1; i >= 0; i--) {
292 if (auxv[i].a_type == M5_AT_PLATFORM) {
293 auxv[i].a_val = platform_base;
294 initVirtMem.writeString(platform_base, platform.c_str());
295 } else if (auxv[i].a_type == M5_AT_EXECFN) {
296 auxv[i].a_val = aux_data_base;
297 initVirtMem.writeString(aux_data_base, filename.c_str());
298 } else if (auxv[i].a_type == M5_AT_RANDOM) {
299 auxv[i].a_val = aux_random_base;
300 // Just leave the value 0, we don't want randomness
301 }
302 }
303
304 //Copy the aux stuff
335 initVirtMem.writeBlob(sentry_base,
336 (uint8_t*)&sentry_NULL, sentry_size);
337
338 //Fix up the aux vectors which point to other data
339 for (int i = auxv.size() - 1; i >= 0; i--) {
340 if (auxv[i].a_type == M5_AT_PLATFORM) {
341 auxv[i].a_val = platform_base;
342 initVirtMem.writeString(platform_base, platform.c_str());
343 } else if (auxv[i].a_type == M5_AT_EXECFN) {
344 auxv[i].a_val = aux_data_base;
345 initVirtMem.writeString(aux_data_base, filename.c_str());
346 } else if (auxv[i].a_type == M5_AT_RANDOM) {
347 auxv[i].a_val = aux_random_base;
348 // Just leave the value 0, we don't want randomness
349 }
350 }
351
352 //Copy the aux stuff
305 for(int x = 0; x < auxv.size(); x++)
306 {
353 for (int x = 0; x < auxv.size(); x++) {
307 initVirtMem.writeBlob(auxv_array_base + x * 2 * intSize,
308 (uint8_t*)&(auxv[x].a_type), intSize);
309 initVirtMem.writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
310 (uint8_t*)&(auxv[x].a_val), intSize);
311 }
312 //Write out the terminating zeroed auxilliary vector
313 const uint64_t zero = 0;
314 initVirtMem.writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
315 (uint8_t*)&zero, 2 * intSize);
316
317 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
318 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
319
320 initVirtMem.writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
321
322 ThreadContext *tc = system->getThreadContext(contextIds[0]);
323 //Set the stack pointer register
354 initVirtMem.writeBlob(auxv_array_base + x * 2 * intSize,
355 (uint8_t*)&(auxv[x].a_type), intSize);
356 initVirtMem.writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
357 (uint8_t*)&(auxv[x].a_val), intSize);
358 }
359 //Write out the terminating zeroed auxilliary vector
360 const uint64_t zero = 0;
361 initVirtMem.writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
362 (uint8_t*)&zero, 2 * intSize);
363
364 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
365 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
366
367 initVirtMem.writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
368
369 ThreadContext *tc = system->getThreadContext(contextIds[0]);
370 //Set the stack pointer register
324 tc->setIntReg(StackPointerReg, stack_min);
371 tc->setIntReg(spIndex, stack_min);
325 //A pointer to a function to run when the program exits. We'll set this
326 //to zero explicitly to make sure this isn't used.
327 tc->setIntReg(ArgumentReg0, 0);
328 //Set argument regs 1 and 2 to argv[0] and envp[0] respectively
329 if (argv.size() > 0) {
330 tc->setIntReg(ArgumentReg1, arg_data_base + arg_data_size -
331 argv[argv.size() - 1].size() - 1);
332 } else {
333 tc->setIntReg(ArgumentReg1, 0);
334 }
335 if (envp.size() > 0) {
336 tc->setIntReg(ArgumentReg2, env_data_base + env_data_size -
337 envp[envp.size() - 1].size() - 1);
338 } else {
339 tc->setIntReg(ArgumentReg2, 0);
340 }
341
342 PCState pc;
343 pc.thumb(arch == ObjectFile::Thumb);
344 pc.nextThumb(pc.thumb());
372 //A pointer to a function to run when the program exits. We'll set this
373 //to zero explicitly to make sure this isn't used.
374 tc->setIntReg(ArgumentReg0, 0);
375 //Set argument regs 1 and 2 to argv[0] and envp[0] respectively
376 if (argv.size() > 0) {
377 tc->setIntReg(ArgumentReg1, arg_data_base + arg_data_size -
378 argv[argv.size() - 1].size() - 1);
379 } else {
380 tc->setIntReg(ArgumentReg1, 0);
381 }
382 if (envp.size() > 0) {
383 tc->setIntReg(ArgumentReg2, env_data_base + env_data_size -
384 envp[envp.size() - 1].size() - 1);
385 } else {
386 tc->setIntReg(ArgumentReg2, 0);
387 }
388
389 PCState pc;
390 pc.thumb(arch == ObjectFile::Thumb);
391 pc.nextThumb(pc.thumb());
392 pc.aarch64(arch == ObjectFile::Arm64);
393 pc.nextAArch64(pc.aarch64());
345 pc.set(objFile->entryPoint() & ~mask(1));
346 tc->pcState(pc);
347
348 //Align the "stack_min" to a page boundary.
349 stack_min = roundDown(stack_min, pageSize);
350}
351
352ArmISA::IntReg
394 pc.set(objFile->entryPoint() & ~mask(1));
395 tc->pcState(pc);
396
397 //Align the "stack_min" to a page boundary.
398 stack_min = roundDown(stack_min, pageSize);
399}
400
401ArmISA::IntReg
353ArmLiveProcess::getSyscallArg(ThreadContext *tc, int &i)
402ArmLiveProcess32::getSyscallArg(ThreadContext *tc, int &i)
354{
355 assert(i < 6);
356 return tc->readIntReg(ArgumentReg0 + i++);
357}
358
403{
404 assert(i < 6);
405 return tc->readIntReg(ArgumentReg0 + i++);
406}
407
359uint64_t
360ArmLiveProcess::getSyscallArg(ThreadContext *tc, int &i, int width)
408ArmISA::IntReg
409ArmLiveProcess64::getSyscallArg(ThreadContext *tc, int &i)
361{
410{
411 assert(i < 8);
412 return tc->readIntReg(ArgumentReg0 + i++);
413}
414
415ArmISA::IntReg
416ArmLiveProcess32::getSyscallArg(ThreadContext *tc, int &i, int width)
417{
362 assert(width == 32 || width == 64);
363 if (width == 32)
364 return getSyscallArg(tc, i);
365
366 // 64 bit arguments are passed starting in an even register
367 if (i % 2 != 0)
368 i++;
369
370 // Registers r0-r6 can be used
371 assert(i < 5);
372 uint64_t val;
373 val = tc->readIntReg(ArgumentReg0 + i++);
374 val |= ((uint64_t)tc->readIntReg(ArgumentReg0 + i++) << 32);
375 return val;
376}
377
418 assert(width == 32 || width == 64);
419 if (width == 32)
420 return getSyscallArg(tc, i);
421
422 // 64 bit arguments are passed starting in an even register
423 if (i % 2 != 0)
424 i++;
425
426 // Registers r0-r6 can be used
427 assert(i < 5);
428 uint64_t val;
429 val = tc->readIntReg(ArgumentReg0 + i++);
430 val |= ((uint64_t)tc->readIntReg(ArgumentReg0 + i++) << 32);
431 return val;
432}
433
434ArmISA::IntReg
435ArmLiveProcess64::getSyscallArg(ThreadContext *tc, int &i, int width)
436{
437 return getSyscallArg(tc, i);
438}
378
439
440
379void
441void
380ArmLiveProcess::setSyscallArg(ThreadContext *tc,
442ArmLiveProcess32::setSyscallArg(ThreadContext *tc, int i, ArmISA::IntReg val)
443{
444 assert(i < 6);
445 tc->setIntReg(ArgumentReg0 + i, val);
446}
447
448void
449ArmLiveProcess64::setSyscallArg(ThreadContext *tc,
381 int i, ArmISA::IntReg val)
382{
450 int i, ArmISA::IntReg val)
451{
383 assert(i < 4);
452 assert(i < 8);
384 tc->setIntReg(ArgumentReg0 + i, val);
385}
386
387void
453 tc->setIntReg(ArgumentReg0 + i, val);
454}
455
456void
388ArmLiveProcess::setSyscallReturn(ThreadContext *tc,
457ArmLiveProcess32::setSyscallReturn(ThreadContext *tc,
389 SyscallReturn return_value)
390{
391 tc->setIntReg(ReturnValueReg, return_value.value());
392}
458 SyscallReturn return_value)
459{
460 tc->setIntReg(ReturnValueReg, return_value.value());
461}
462
463void
464ArmLiveProcess64::setSyscallReturn(ThreadContext *tc,
465 SyscallReturn return_value)
466{
467 tc->setIntReg(ReturnValueReg, return_value.value());
468}