process.cc (3415:72c48f292f6a) process.cc (3589:2fec1358ce80)
1/*
2 * Copyright (c) 2003-2004 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Gabe Black
29 * Ali Saidi
30 */
31
1/*
2 * Copyright (c) 2003-2004 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Gabe Black
29 * Ali Saidi
30 */
31
32#include "arch/sparc/asi.hh"
32#include "arch/sparc/isa_traits.hh"
33#include "arch/sparc/process.hh"
34#include "base/loader/object_file.hh"
35#include "base/loader/elf_object.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/system.hh"
41
42using namespace std;
43using namespace SparcISA;
44
45
46SparcLiveProcess::SparcLiveProcess(const std::string &nm, ObjectFile *objFile,
47 System *_system, int stdin_fd, int stdout_fd, int stderr_fd,
48 std::vector<std::string> &argv, std::vector<std::string> &envp,
49 uint64_t _uid, uint64_t _euid, uint64_t _gid, uint64_t _egid,
50 uint64_t _pid, uint64_t _ppid)
51 : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd,
52 argv, envp, _uid, _euid, _gid, _egid, _pid, _ppid)
53{
54
55 // XXX all the below need to be updated for SPARC - Ali
56 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
57 brk_point = roundUp(brk_point, VMPageSize);
58
59 // Set up stack. On SPARC Linux, stack goes from the top of memory
60 // downward, less the hole for the kernel address space.
61 stack_base = (Addr)0x80000000000ULL;
62
63 // Set up region for mmaps. Tru64 seems to start just above 0 and
64 // grow up from there.
65 mmap_start = mmap_end = 0xfffff80000000000ULL;
66
67 // Set pointer for next thread stack. Reserve 8M for main stack.
68 next_thread_stack_base = stack_base - (8 * 1024 * 1024);
69
70 //Initialize these to 0s
71 fillStart = 0;
72 spillStart = 0;
73}
74
75void
76SparcLiveProcess::startup()
77{
78 argsInit(MachineBytes, VMPageSize);
79
80 //From the SPARC ABI
81
82 //The process runs in user mode
83 threadContexts[0]->setMiscRegWithEffect(MISCREG_PSTATE, 0x02);
84
85 //Setup default FP state
86 threadContexts[0]->setMiscReg(MISCREG_FSR, 0);
87
88 threadContexts[0]->setMiscReg(MISCREG_TICK, 0);
89 //
90 /*
91 * Register window management registers
92 */
93
94 //No windows contain info from other programs
95 threadContexts[0]->setMiscReg(MISCREG_OTHERWIN, 0);
96 //There are no windows to pop
97 threadContexts[0]->setMiscReg(MISCREG_CANRESTORE, 0);
98 //All windows are available to save into
99 threadContexts[0]->setMiscReg(MISCREG_CANSAVE, NWindows - 2);
100 //All windows are "clean"
101 threadContexts[0]->setMiscReg(MISCREG_CLEANWIN, NWindows);
102 //Start with register window 0
103 threadContexts[0]->setMiscReg(MISCREG_CWP, 0);
104 //Always use spill and fill traps 0
105 threadContexts[0]->setMiscReg(MISCREG_WSTATE, 0);
106 //Set the trap level to 0
107 threadContexts[0]->setMiscReg(MISCREG_TL, 0);
33#include "arch/sparc/isa_traits.hh"
34#include "arch/sparc/process.hh"
35#include "base/loader/object_file.hh"
36#include "base/loader/elf_object.hh"
37#include "base/misc.hh"
38#include "cpu/thread_context.hh"
39#include "mem/page_table.hh"
40#include "mem/translating_port.hh"
41#include "sim/system.hh"
42
43using namespace std;
44using namespace SparcISA;
45
46
47SparcLiveProcess::SparcLiveProcess(const std::string &nm, ObjectFile *objFile,
48 System *_system, int stdin_fd, int stdout_fd, int stderr_fd,
49 std::vector<std::string> &argv, std::vector<std::string> &envp,
50 uint64_t _uid, uint64_t _euid, uint64_t _gid, uint64_t _egid,
51 uint64_t _pid, uint64_t _ppid)
52 : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd,
53 argv, envp, _uid, _euid, _gid, _egid, _pid, _ppid)
54{
55
56 // XXX all the below need to be updated for SPARC - Ali
57 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
58 brk_point = roundUp(brk_point, VMPageSize);
59
60 // Set up stack. On SPARC Linux, stack goes from the top of memory
61 // downward, less the hole for the kernel address space.
62 stack_base = (Addr)0x80000000000ULL;
63
64 // Set up region for mmaps. Tru64 seems to start just above 0 and
65 // grow up from there.
66 mmap_start = mmap_end = 0xfffff80000000000ULL;
67
68 // Set pointer for next thread stack. Reserve 8M for main stack.
69 next_thread_stack_base = stack_base - (8 * 1024 * 1024);
70
71 //Initialize these to 0s
72 fillStart = 0;
73 spillStart = 0;
74}
75
76void
77SparcLiveProcess::startup()
78{
79 argsInit(MachineBytes, VMPageSize);
80
81 //From the SPARC ABI
82
83 //The process runs in user mode
84 threadContexts[0]->setMiscRegWithEffect(MISCREG_PSTATE, 0x02);
85
86 //Setup default FP state
87 threadContexts[0]->setMiscReg(MISCREG_FSR, 0);
88
89 threadContexts[0]->setMiscReg(MISCREG_TICK, 0);
90 //
91 /*
92 * Register window management registers
93 */
94
95 //No windows contain info from other programs
96 threadContexts[0]->setMiscReg(MISCREG_OTHERWIN, 0);
97 //There are no windows to pop
98 threadContexts[0]->setMiscReg(MISCREG_CANRESTORE, 0);
99 //All windows are available to save into
100 threadContexts[0]->setMiscReg(MISCREG_CANSAVE, NWindows - 2);
101 //All windows are "clean"
102 threadContexts[0]->setMiscReg(MISCREG_CLEANWIN, NWindows);
103 //Start with register window 0
104 threadContexts[0]->setMiscReg(MISCREG_CWP, 0);
105 //Always use spill and fill traps 0
106 threadContexts[0]->setMiscReg(MISCREG_WSTATE, 0);
107 //Set the trap level to 0
108 threadContexts[0]->setMiscReg(MISCREG_TL, 0);
109 //Set the ASI register to something fixed
110 threadContexts[0]->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
108}
109
110m5_auxv_t buildAuxVect(int64_t type, int64_t val)
111{
112 m5_auxv_t result;
113 result.a_type = TheISA::htog(type);
114 result.a_val = TheISA::htog(val);
115 return result;
116}
117
118//We only use 19 instructions for the trap handlers, but there would be
119//space for 32 in a real SPARC trap table.
120const int numFillInsts = 32;
121const int numSpillInsts = 32;
122
123MachInst fillHandler[numFillInsts] =
124{
125 htog(0x87802018), //wr %g0, ASI_AIUP, %asi
126 htog(0xe0dba7ff), //ldxa [%sp + BIAS + (0*8)] %asi, %l0
127 htog(0xe2dba807), //ldxa [%sp + BIAS + (1*8)] %asi, %l1
128 htog(0xe4dba80f), //ldxa [%sp + BIAS + (2*8)] %asi, %l2
129 htog(0xe6dba817), //ldxa [%sp + BIAS + (3*8)] %asi, %l3
130 htog(0xe8dba81f), //ldxa [%sp + BIAS + (4*8)] %asi, %l4
131 htog(0xeadba827), //ldxa [%sp + BIAS + (5*8)] %asi, %l5
132 htog(0xecdba82f), //ldxa [%sp + BIAS + (6*8)] %asi, %l6
133 htog(0xeedba837), //ldxa [%sp + BIAS + (7*8)] %asi, %l7
134 htog(0xf0dba83f), //ldxa [%sp + BIAS + (8*8)] %asi, %i0
135 htog(0xf2dba847), //ldxa [%sp + BIAS + (9*8)] %asi, %i1
136 htog(0xf4dba84f), //ldxa [%sp + BIAS + (10*8)] %asi, %i2
137 htog(0xf6dba857), //ldxa [%sp + BIAS + (11*8)] %asi, %i3
138 htog(0xf8dba85f), //ldxa [%sp + BIAS + (12*8)] %asi, %i4
139 htog(0xfadba867), //ldxa [%sp + BIAS + (13*8)] %asi, %i5
140 htog(0xfcdba86f), //ldxa [%sp + BIAS + (14*8)] %asi, %i6
141 htog(0xfedba877), //ldxa [%sp + BIAS + (15*8)] %asi, %i7
142 htog(0x83880000), //restored
143 htog(0x83F00000), //retry
144 htog(0x00000000), //illtrap
145 htog(0x00000000), //illtrap
146 htog(0x00000000), //illtrap
147 htog(0x00000000), //illtrap
148 htog(0x00000000), //illtrap
149 htog(0x00000000), //illtrap
150 htog(0x00000000), //illtrap
151 htog(0x00000000), //illtrap
152 htog(0x00000000), //illtrap
153 htog(0x00000000), //illtrap
154 htog(0x00000000), //illtrap
155 htog(0x00000000), //illtrap
156 htog(0x00000000) //illtrap
157};
158
159MachInst spillHandler[numSpillInsts] =
160{
161 htog(0x87802018), //wr %g0, ASI_AIUP, %asi
162 htog(0xe0f3a7ff), //stxa %l0, [%sp + BIAS + (0*8)] %asi
163 htog(0xe2f3a807), //stxa %l1, [%sp + BIAS + (1*8)] %asi
164 htog(0xe4f3a80f), //stxa %l2, [%sp + BIAS + (2*8)] %asi
165 htog(0xe6f3a817), //stxa %l3, [%sp + BIAS + (3*8)] %asi
166 htog(0xe8f3a81f), //stxa %l4, [%sp + BIAS + (4*8)] %asi
167 htog(0xeaf3a827), //stxa %l5, [%sp + BIAS + (5*8)] %asi
168 htog(0xecf3a82f), //stxa %l6, [%sp + BIAS + (6*8)] %asi
169 htog(0xeef3a837), //stxa %l7, [%sp + BIAS + (7*8)] %asi
170 htog(0xf0f3a83f), //stxa %i0, [%sp + BIAS + (8*8)] %asi
171 htog(0xf2f3a847), //stxa %i1, [%sp + BIAS + (9*8)] %asi
172 htog(0xf4f3a84f), //stxa %i2, [%sp + BIAS + (10*8)] %asi
173 htog(0xf6f3a857), //stxa %i3, [%sp + BIAS + (11*8)] %asi
174 htog(0xf8f3a85f), //stxa %i4, [%sp + BIAS + (12*8)] %asi
175 htog(0xfaf3a867), //stxa %i5, [%sp + BIAS + (13*8)] %asi
176 htog(0xfcf3a86f), //stxa %i6, [%sp + BIAS + (14*8)] %asi
177 htog(0xfef3a877), //stxa %i7, [%sp + BIAS + (15*8)] %asi
178 htog(0x81880000), //saved
179 htog(0x83F00000), //retry
180 htog(0x00000000), //illtrap
181 htog(0x00000000), //illtrap
182 htog(0x00000000), //illtrap
183 htog(0x00000000), //illtrap
184 htog(0x00000000), //illtrap
185 htog(0x00000000), //illtrap
186 htog(0x00000000), //illtrap
187 htog(0x00000000), //illtrap
188 htog(0x00000000), //illtrap
189 htog(0x00000000), //illtrap
190 htog(0x00000000), //illtrap
191 htog(0x00000000), //illtrap
192 htog(0x00000000) //illtrap
193};
194
195void
196SparcLiveProcess::argsInit(int intSize, int pageSize)
197{
198 Process::startup();
199
200 string filename;
201 if(argv.size() < 1)
202 filename = "";
203 else
204 filename = argv[0];
205
206 Addr alignmentMask = ~(intSize - 1);
207
208 // load object file into target memory
209 objFile->loadSections(initVirtMem);
210
211 //These are the auxilliary vector types
212 enum auxTypes
213 {
214 SPARC_AT_HWCAP = 16,
215 SPARC_AT_PAGESZ = 6,
216 SPARC_AT_CLKTCK = 17,
217 SPARC_AT_PHDR = 3,
218 SPARC_AT_PHENT = 4,
219 SPARC_AT_PHNUM = 5,
220 SPARC_AT_BASE = 7,
221 SPARC_AT_FLAGS = 8,
222 SPARC_AT_ENTRY = 9,
223 SPARC_AT_UID = 11,
224 SPARC_AT_EUID = 12,
225 SPARC_AT_GID = 13,
226 SPARC_AT_EGID = 14,
227 SPARC_AT_SECURE = 23
228 };
229
230 enum hardwareCaps
231 {
232 M5_HWCAP_SPARC_FLUSH = 1,
233 M5_HWCAP_SPARC_STBAR = 2,
234 M5_HWCAP_SPARC_SWAP = 4,
235 M5_HWCAP_SPARC_MULDIV = 8,
236 M5_HWCAP_SPARC_V9 = 16,
237 //This one should technically only be set
238 //if there is a cheetah or cheetah_plus tlb,
239 //but we'll use it all the time
240 M5_HWCAP_SPARC_ULTRA3 = 32
241 };
242
243 const int64_t hwcap =
244 M5_HWCAP_SPARC_FLUSH |
245 M5_HWCAP_SPARC_STBAR |
246 M5_HWCAP_SPARC_SWAP |
247 M5_HWCAP_SPARC_MULDIV |
248 M5_HWCAP_SPARC_V9 |
249 M5_HWCAP_SPARC_ULTRA3;
250
251
252 //Setup the auxilliary vectors. These will already have endian conversion.
253 //Auxilliary vectors are loaded only for elf formatted executables.
254 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
255 if(elfObject)
256 {
257 //Bits which describe the system hardware capabilities
258 auxv.push_back(buildAuxVect(SPARC_AT_HWCAP, hwcap));
259 //The system page size
260 auxv.push_back(buildAuxVect(SPARC_AT_PAGESZ, SparcISA::VMPageSize));
261 //Defined to be 100 in the kernel source.
262 //Frequency at which times() increments
263 auxv.push_back(buildAuxVect(SPARC_AT_CLKTCK, 100));
264 // For statically linked executables, this is the virtual address of the
265 // program header tables if they appear in the executable image
266 auxv.push_back(buildAuxVect(SPARC_AT_PHDR, elfObject->programHeaderTable()));
267 // This is the size of a program header entry from the elf file.
268 auxv.push_back(buildAuxVect(SPARC_AT_PHENT, elfObject->programHeaderSize()));
269 // This is the number of program headers from the original elf file.
270 auxv.push_back(buildAuxVect(SPARC_AT_PHNUM, elfObject->programHeaderCount()));
271 //This is the address of the elf "interpreter", It should be set
272 //to 0 for regular executables. It should be something else
273 //(not sure what) for dynamic libraries.
274 auxv.push_back(buildAuxVect(SPARC_AT_BASE, 0));
275 //This is hardwired to 0 in the elf loading code in the kernel
276 auxv.push_back(buildAuxVect(SPARC_AT_FLAGS, 0));
277 //The entry point to the program
278 auxv.push_back(buildAuxVect(SPARC_AT_ENTRY, objFile->entryPoint()));
279 //Different user and group IDs
280 auxv.push_back(buildAuxVect(SPARC_AT_UID, uid()));
281 auxv.push_back(buildAuxVect(SPARC_AT_EUID, euid()));
282 auxv.push_back(buildAuxVect(SPARC_AT_GID, gid()));
283 auxv.push_back(buildAuxVect(SPARC_AT_EGID, egid()));
284 //Whether to enable "secure mode" in the executable
285 auxv.push_back(buildAuxVect(SPARC_AT_SECURE, 0));
286 }
287
288 //Figure out how big the initial stack needs to be
289
290 // The unaccounted for 0 at the top of the stack
291 int mysterious_size = intSize;
292
293 //This is the name of the file which is present on the initial stack
294 //It's purpose is to let the user space linker examine the original file.
295 int file_name_size = filename.size() + 1;
296
297 int env_data_size = 0;
298 for (int i = 0; i < envp.size(); ++i) {
299 env_data_size += envp[i].size() + 1;
300 }
301 int arg_data_size = 0;
302 for (int i = 0; i < argv.size(); ++i) {
303 arg_data_size += argv[i].size() + 1;
304 }
305
306 //The info_block needs to be padded so it's size is a multiple of the
307 //alignment mask. Also, it appears that there needs to be at least some
308 //padding, so if the size is already a multiple, we need to increase it
309 //anyway.
310 int info_block_size =
311 (file_name_size +
312 env_data_size +
313 arg_data_size +
314 intSize) & alignmentMask;
315
316 int info_block_padding =
317 info_block_size -
318 file_name_size -
319 env_data_size -
320 arg_data_size;
321
322 //Each auxilliary vector is two 8 byte words
323 int aux_array_size = intSize * 2 * (auxv.size() + 1);
324
325 int envp_array_size = intSize * (envp.size() + 1);
326 int argv_array_size = intSize * (argv.size() + 1);
327
328 int argc_size = intSize;
329 int window_save_size = intSize * 16;
330
331 int space_needed =
332 mysterious_size +
333 info_block_size +
334 aux_array_size +
335 envp_array_size +
336 argv_array_size +
337 argc_size +
338 window_save_size;
339
340 stack_min = stack_base - space_needed;
341 stack_min &= alignmentMask;
342 stack_size = stack_base - stack_min;
343
344 // map memory
345 pTable->allocate(roundDown(stack_min, pageSize),
346 roundUp(stack_size, pageSize));
347
348 // map out initial stack contents
349 Addr mysterious_base = stack_base - mysterious_size;
350 Addr file_name_base = mysterious_base - file_name_size;
351 Addr env_data_base = file_name_base - env_data_size;
352 Addr arg_data_base = env_data_base - arg_data_size;
353 Addr auxv_array_base = arg_data_base - aux_array_size - info_block_padding;
354 Addr envp_array_base = auxv_array_base - envp_array_size;
355 Addr argv_array_base = envp_array_base - argv_array_size;
356 Addr argc_base = argv_array_base - argc_size;
357#ifndef NDEBUG
358 // only used in DPRINTF
359 Addr window_save_base = argc_base - window_save_size;
360#endif
361
362 DPRINTF(Sparc, "The addresses of items on the initial stack:\n");
363 DPRINTF(Sparc, "0x%x - file name\n", file_name_base);
364 DPRINTF(Sparc, "0x%x - env data\n", env_data_base);
365 DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base);
366 DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base);
367 DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base);
368 DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base);
369 DPRINTF(Sparc, "0x%x - argc \n", argc_base);
370 DPRINTF(Sparc, "0x%x - window save\n", window_save_base);
371 DPRINTF(Sparc, "0x%x - stack min\n", stack_min);
372
373 // write contents to stack
374
375 // figure out argc
376 uint64_t argc = argv.size();
377 uint64_t guestArgc = TheISA::htog(argc);
378
379 //Write out the mysterious 0
380 uint64_t mysterious_zero = 0;
381 initVirtMem->writeBlob(mysterious_base,
382 (uint8_t*)&mysterious_zero, mysterious_size);
383
384 //Write the file name
385 initVirtMem->writeString(file_name_base, filename.c_str());
386
387 //Copy the aux stuff
388 for(int x = 0; x < auxv.size(); x++)
389 {
390 initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
391 (uint8_t*)&(auxv[x].a_type), intSize);
392 initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
393 (uint8_t*)&(auxv[x].a_val), intSize);
394 }
395 //Write out the terminating zeroed auxilliary vector
396 const uint64_t zero = 0;
397 initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
398 (uint8_t*)&zero, 2 * intSize);
399
400 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
401 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
402
403 initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
404
405 //Stuff the trap handlers into the processes address space.
406 //Since the stack grows down and is the highest area in the processes
407 //address space, we can put stuff above it and stay out of the way.
408 int fillSize = sizeof(MachInst) * numFillInsts;
409 int spillSize = sizeof(MachInst) * numSpillInsts;
410 fillStart = stack_base;
411 spillStart = fillStart + fillSize;
412 initVirtMem->writeBlob(fillStart, (uint8_t*)fillHandler, fillSize);
413 initVirtMem->writeBlob(spillStart, (uint8_t*)spillHandler, spillSize);
414
415 //Set up the thread context to start running the process
416 threadContexts[0]->setIntReg(ArgumentReg0, argc);
417 threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base);
418 threadContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias);
419
420 Addr prog_entry = objFile->entryPoint();
421 threadContexts[0]->setPC(prog_entry);
422 threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
423 threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
424
425// num_processes++;
426}
111}
112
113m5_auxv_t buildAuxVect(int64_t type, int64_t val)
114{
115 m5_auxv_t result;
116 result.a_type = TheISA::htog(type);
117 result.a_val = TheISA::htog(val);
118 return result;
119}
120
121//We only use 19 instructions for the trap handlers, but there would be
122//space for 32 in a real SPARC trap table.
123const int numFillInsts = 32;
124const int numSpillInsts = 32;
125
126MachInst fillHandler[numFillInsts] =
127{
128 htog(0x87802018), //wr %g0, ASI_AIUP, %asi
129 htog(0xe0dba7ff), //ldxa [%sp + BIAS + (0*8)] %asi, %l0
130 htog(0xe2dba807), //ldxa [%sp + BIAS + (1*8)] %asi, %l1
131 htog(0xe4dba80f), //ldxa [%sp + BIAS + (2*8)] %asi, %l2
132 htog(0xe6dba817), //ldxa [%sp + BIAS + (3*8)] %asi, %l3
133 htog(0xe8dba81f), //ldxa [%sp + BIAS + (4*8)] %asi, %l4
134 htog(0xeadba827), //ldxa [%sp + BIAS + (5*8)] %asi, %l5
135 htog(0xecdba82f), //ldxa [%sp + BIAS + (6*8)] %asi, %l6
136 htog(0xeedba837), //ldxa [%sp + BIAS + (7*8)] %asi, %l7
137 htog(0xf0dba83f), //ldxa [%sp + BIAS + (8*8)] %asi, %i0
138 htog(0xf2dba847), //ldxa [%sp + BIAS + (9*8)] %asi, %i1
139 htog(0xf4dba84f), //ldxa [%sp + BIAS + (10*8)] %asi, %i2
140 htog(0xf6dba857), //ldxa [%sp + BIAS + (11*8)] %asi, %i3
141 htog(0xf8dba85f), //ldxa [%sp + BIAS + (12*8)] %asi, %i4
142 htog(0xfadba867), //ldxa [%sp + BIAS + (13*8)] %asi, %i5
143 htog(0xfcdba86f), //ldxa [%sp + BIAS + (14*8)] %asi, %i6
144 htog(0xfedba877), //ldxa [%sp + BIAS + (15*8)] %asi, %i7
145 htog(0x83880000), //restored
146 htog(0x83F00000), //retry
147 htog(0x00000000), //illtrap
148 htog(0x00000000), //illtrap
149 htog(0x00000000), //illtrap
150 htog(0x00000000), //illtrap
151 htog(0x00000000), //illtrap
152 htog(0x00000000), //illtrap
153 htog(0x00000000), //illtrap
154 htog(0x00000000), //illtrap
155 htog(0x00000000), //illtrap
156 htog(0x00000000), //illtrap
157 htog(0x00000000), //illtrap
158 htog(0x00000000), //illtrap
159 htog(0x00000000) //illtrap
160};
161
162MachInst spillHandler[numSpillInsts] =
163{
164 htog(0x87802018), //wr %g0, ASI_AIUP, %asi
165 htog(0xe0f3a7ff), //stxa %l0, [%sp + BIAS + (0*8)] %asi
166 htog(0xe2f3a807), //stxa %l1, [%sp + BIAS + (1*8)] %asi
167 htog(0xe4f3a80f), //stxa %l2, [%sp + BIAS + (2*8)] %asi
168 htog(0xe6f3a817), //stxa %l3, [%sp + BIAS + (3*8)] %asi
169 htog(0xe8f3a81f), //stxa %l4, [%sp + BIAS + (4*8)] %asi
170 htog(0xeaf3a827), //stxa %l5, [%sp + BIAS + (5*8)] %asi
171 htog(0xecf3a82f), //stxa %l6, [%sp + BIAS + (6*8)] %asi
172 htog(0xeef3a837), //stxa %l7, [%sp + BIAS + (7*8)] %asi
173 htog(0xf0f3a83f), //stxa %i0, [%sp + BIAS + (8*8)] %asi
174 htog(0xf2f3a847), //stxa %i1, [%sp + BIAS + (9*8)] %asi
175 htog(0xf4f3a84f), //stxa %i2, [%sp + BIAS + (10*8)] %asi
176 htog(0xf6f3a857), //stxa %i3, [%sp + BIAS + (11*8)] %asi
177 htog(0xf8f3a85f), //stxa %i4, [%sp + BIAS + (12*8)] %asi
178 htog(0xfaf3a867), //stxa %i5, [%sp + BIAS + (13*8)] %asi
179 htog(0xfcf3a86f), //stxa %i6, [%sp + BIAS + (14*8)] %asi
180 htog(0xfef3a877), //stxa %i7, [%sp + BIAS + (15*8)] %asi
181 htog(0x81880000), //saved
182 htog(0x83F00000), //retry
183 htog(0x00000000), //illtrap
184 htog(0x00000000), //illtrap
185 htog(0x00000000), //illtrap
186 htog(0x00000000), //illtrap
187 htog(0x00000000), //illtrap
188 htog(0x00000000), //illtrap
189 htog(0x00000000), //illtrap
190 htog(0x00000000), //illtrap
191 htog(0x00000000), //illtrap
192 htog(0x00000000), //illtrap
193 htog(0x00000000), //illtrap
194 htog(0x00000000), //illtrap
195 htog(0x00000000) //illtrap
196};
197
198void
199SparcLiveProcess::argsInit(int intSize, int pageSize)
200{
201 Process::startup();
202
203 string filename;
204 if(argv.size() < 1)
205 filename = "";
206 else
207 filename = argv[0];
208
209 Addr alignmentMask = ~(intSize - 1);
210
211 // load object file into target memory
212 objFile->loadSections(initVirtMem);
213
214 //These are the auxilliary vector types
215 enum auxTypes
216 {
217 SPARC_AT_HWCAP = 16,
218 SPARC_AT_PAGESZ = 6,
219 SPARC_AT_CLKTCK = 17,
220 SPARC_AT_PHDR = 3,
221 SPARC_AT_PHENT = 4,
222 SPARC_AT_PHNUM = 5,
223 SPARC_AT_BASE = 7,
224 SPARC_AT_FLAGS = 8,
225 SPARC_AT_ENTRY = 9,
226 SPARC_AT_UID = 11,
227 SPARC_AT_EUID = 12,
228 SPARC_AT_GID = 13,
229 SPARC_AT_EGID = 14,
230 SPARC_AT_SECURE = 23
231 };
232
233 enum hardwareCaps
234 {
235 M5_HWCAP_SPARC_FLUSH = 1,
236 M5_HWCAP_SPARC_STBAR = 2,
237 M5_HWCAP_SPARC_SWAP = 4,
238 M5_HWCAP_SPARC_MULDIV = 8,
239 M5_HWCAP_SPARC_V9 = 16,
240 //This one should technically only be set
241 //if there is a cheetah or cheetah_plus tlb,
242 //but we'll use it all the time
243 M5_HWCAP_SPARC_ULTRA3 = 32
244 };
245
246 const int64_t hwcap =
247 M5_HWCAP_SPARC_FLUSH |
248 M5_HWCAP_SPARC_STBAR |
249 M5_HWCAP_SPARC_SWAP |
250 M5_HWCAP_SPARC_MULDIV |
251 M5_HWCAP_SPARC_V9 |
252 M5_HWCAP_SPARC_ULTRA3;
253
254
255 //Setup the auxilliary vectors. These will already have endian conversion.
256 //Auxilliary vectors are loaded only for elf formatted executables.
257 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
258 if(elfObject)
259 {
260 //Bits which describe the system hardware capabilities
261 auxv.push_back(buildAuxVect(SPARC_AT_HWCAP, hwcap));
262 //The system page size
263 auxv.push_back(buildAuxVect(SPARC_AT_PAGESZ, SparcISA::VMPageSize));
264 //Defined to be 100 in the kernel source.
265 //Frequency at which times() increments
266 auxv.push_back(buildAuxVect(SPARC_AT_CLKTCK, 100));
267 // For statically linked executables, this is the virtual address of the
268 // program header tables if they appear in the executable image
269 auxv.push_back(buildAuxVect(SPARC_AT_PHDR, elfObject->programHeaderTable()));
270 // This is the size of a program header entry from the elf file.
271 auxv.push_back(buildAuxVect(SPARC_AT_PHENT, elfObject->programHeaderSize()));
272 // This is the number of program headers from the original elf file.
273 auxv.push_back(buildAuxVect(SPARC_AT_PHNUM, elfObject->programHeaderCount()));
274 //This is the address of the elf "interpreter", It should be set
275 //to 0 for regular executables. It should be something else
276 //(not sure what) for dynamic libraries.
277 auxv.push_back(buildAuxVect(SPARC_AT_BASE, 0));
278 //This is hardwired to 0 in the elf loading code in the kernel
279 auxv.push_back(buildAuxVect(SPARC_AT_FLAGS, 0));
280 //The entry point to the program
281 auxv.push_back(buildAuxVect(SPARC_AT_ENTRY, objFile->entryPoint()));
282 //Different user and group IDs
283 auxv.push_back(buildAuxVect(SPARC_AT_UID, uid()));
284 auxv.push_back(buildAuxVect(SPARC_AT_EUID, euid()));
285 auxv.push_back(buildAuxVect(SPARC_AT_GID, gid()));
286 auxv.push_back(buildAuxVect(SPARC_AT_EGID, egid()));
287 //Whether to enable "secure mode" in the executable
288 auxv.push_back(buildAuxVect(SPARC_AT_SECURE, 0));
289 }
290
291 //Figure out how big the initial stack needs to be
292
293 // The unaccounted for 0 at the top of the stack
294 int mysterious_size = intSize;
295
296 //This is the name of the file which is present on the initial stack
297 //It's purpose is to let the user space linker examine the original file.
298 int file_name_size = filename.size() + 1;
299
300 int env_data_size = 0;
301 for (int i = 0; i < envp.size(); ++i) {
302 env_data_size += envp[i].size() + 1;
303 }
304 int arg_data_size = 0;
305 for (int i = 0; i < argv.size(); ++i) {
306 arg_data_size += argv[i].size() + 1;
307 }
308
309 //The info_block needs to be padded so it's size is a multiple of the
310 //alignment mask. Also, it appears that there needs to be at least some
311 //padding, so if the size is already a multiple, we need to increase it
312 //anyway.
313 int info_block_size =
314 (file_name_size +
315 env_data_size +
316 arg_data_size +
317 intSize) & alignmentMask;
318
319 int info_block_padding =
320 info_block_size -
321 file_name_size -
322 env_data_size -
323 arg_data_size;
324
325 //Each auxilliary vector is two 8 byte words
326 int aux_array_size = intSize * 2 * (auxv.size() + 1);
327
328 int envp_array_size = intSize * (envp.size() + 1);
329 int argv_array_size = intSize * (argv.size() + 1);
330
331 int argc_size = intSize;
332 int window_save_size = intSize * 16;
333
334 int space_needed =
335 mysterious_size +
336 info_block_size +
337 aux_array_size +
338 envp_array_size +
339 argv_array_size +
340 argc_size +
341 window_save_size;
342
343 stack_min = stack_base - space_needed;
344 stack_min &= alignmentMask;
345 stack_size = stack_base - stack_min;
346
347 // map memory
348 pTable->allocate(roundDown(stack_min, pageSize),
349 roundUp(stack_size, pageSize));
350
351 // map out initial stack contents
352 Addr mysterious_base = stack_base - mysterious_size;
353 Addr file_name_base = mysterious_base - file_name_size;
354 Addr env_data_base = file_name_base - env_data_size;
355 Addr arg_data_base = env_data_base - arg_data_size;
356 Addr auxv_array_base = arg_data_base - aux_array_size - info_block_padding;
357 Addr envp_array_base = auxv_array_base - envp_array_size;
358 Addr argv_array_base = envp_array_base - argv_array_size;
359 Addr argc_base = argv_array_base - argc_size;
360#ifndef NDEBUG
361 // only used in DPRINTF
362 Addr window_save_base = argc_base - window_save_size;
363#endif
364
365 DPRINTF(Sparc, "The addresses of items on the initial stack:\n");
366 DPRINTF(Sparc, "0x%x - file name\n", file_name_base);
367 DPRINTF(Sparc, "0x%x - env data\n", env_data_base);
368 DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base);
369 DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base);
370 DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base);
371 DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base);
372 DPRINTF(Sparc, "0x%x - argc \n", argc_base);
373 DPRINTF(Sparc, "0x%x - window save\n", window_save_base);
374 DPRINTF(Sparc, "0x%x - stack min\n", stack_min);
375
376 // write contents to stack
377
378 // figure out argc
379 uint64_t argc = argv.size();
380 uint64_t guestArgc = TheISA::htog(argc);
381
382 //Write out the mysterious 0
383 uint64_t mysterious_zero = 0;
384 initVirtMem->writeBlob(mysterious_base,
385 (uint8_t*)&mysterious_zero, mysterious_size);
386
387 //Write the file name
388 initVirtMem->writeString(file_name_base, filename.c_str());
389
390 //Copy the aux stuff
391 for(int x = 0; x < auxv.size(); x++)
392 {
393 initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
394 (uint8_t*)&(auxv[x].a_type), intSize);
395 initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
396 (uint8_t*)&(auxv[x].a_val), intSize);
397 }
398 //Write out the terminating zeroed auxilliary vector
399 const uint64_t zero = 0;
400 initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
401 (uint8_t*)&zero, 2 * intSize);
402
403 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
404 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
405
406 initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
407
408 //Stuff the trap handlers into the processes address space.
409 //Since the stack grows down and is the highest area in the processes
410 //address space, we can put stuff above it and stay out of the way.
411 int fillSize = sizeof(MachInst) * numFillInsts;
412 int spillSize = sizeof(MachInst) * numSpillInsts;
413 fillStart = stack_base;
414 spillStart = fillStart + fillSize;
415 initVirtMem->writeBlob(fillStart, (uint8_t*)fillHandler, fillSize);
416 initVirtMem->writeBlob(spillStart, (uint8_t*)spillHandler, spillSize);
417
418 //Set up the thread context to start running the process
419 threadContexts[0]->setIntReg(ArgumentReg0, argc);
420 threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base);
421 threadContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias);
422
423 Addr prog_entry = objFile->entryPoint();
424 threadContexts[0]->setPC(prog_entry);
425 threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
426 threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
427
428// num_processes++;
429}