Deleted Added
sdiff udiff text old ( 4186:7dc7d9390e6d ) new ( 4434:2ea7b6e0b78f )
full compact
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"
33#include "arch/sparc/handlers.hh"
34#include "arch/sparc/isa_traits.hh"
35#include "arch/sparc/process.hh"
36#include "arch/sparc/types.hh"
37#include "base/loader/object_file.hh"
38#include "base/loader/elf_object.hh"
39#include "base/misc.hh"
40#include "cpu/thread_context.hh"
41#include "mem/page_table.hh"
42#include "sim/process_impl.hh"
43#include "mem/translating_port.hh"
44#include "sim/system.hh"
45
46using namespace std;
47using namespace SparcISA;
48
49
50SparcLiveProcess::SparcLiveProcess(const std::string &nm, ObjectFile *objFile,
51 System *_system, int stdin_fd, int stdout_fd, int stderr_fd,
52 std::vector<std::string> &argv, std::vector<std::string> &envp,
53 const std::string &cwd,
54 uint64_t _uid, uint64_t _euid, uint64_t _gid, uint64_t _egid,
55 uint64_t _pid, uint64_t _ppid)
56 : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd,
57 argv, envp, cwd, _uid, _euid, _gid, _egid, _pid, _ppid)
58{
59
60 // XXX all the below need to be updated for SPARC - Ali
61 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
62 brk_point = roundUp(brk_point, VMPageSize);
63
64 // Set pointer for next thread stack. Reserve 8M for main stack.
65 next_thread_stack_base = stack_base - (8 * 1024 * 1024);
66
67 //Initialize these to 0s
68 fillStart = 0;
69 spillStart = 0;
70}
71
72void SparcLiveProcess::handleTrap(int trapNum, ThreadContext *tc)
73{
74 switch(trapNum)
75 {
76 case 0x03: //Flush window trap
77 warn("Ignoring request to flush register windows.\n");
78 break;
79 default:
80 panic("Unimplemented trap to operating system: trap number %#x.\n", trapNum);
81 }
82}
83
84void
85Sparc32LiveProcess::startup()
86{
87 argsInit(32 / 8, VMPageSize);
88
89 //From the SPARC ABI
90
91 //The process runs in user mode
92 threadContexts[0]->setMiscReg(MISCREG_PSTATE, 0x02);
93
94 //Setup default FP state
95 threadContexts[0]->setMiscRegNoEffect(MISCREG_FSR, 0);
96
97 threadContexts[0]->setMiscRegNoEffect(MISCREG_TICK, 0);
98 //
99 /*
100 * Register window management registers
101 */
102
103 //No windows contain info from other programs
104 //threadContexts[0]->setMiscRegNoEffect(MISCREG_OTHERWIN, 0);
105 threadContexts[0]->setIntReg(NumIntArchRegs + 6, 0);
106 //There are no windows to pop
107 //threadContexts[0]->setMiscRegNoEffect(MISCREG_CANRESTORE, 0);
108 threadContexts[0]->setIntReg(NumIntArchRegs + 4, 0);
109 //All windows are available to save into
110 //threadContexts[0]->setMiscRegNoEffect(MISCREG_CANSAVE, NWindows - 2);
111 threadContexts[0]->setIntReg(NumIntArchRegs + 3, NWindows - 2);
112 //All windows are "clean"
113 //threadContexts[0]->setMiscRegNoEffect(MISCREG_CLEANWIN, NWindows);
114 threadContexts[0]->setIntReg(NumIntArchRegs + 5, NWindows);
115 //Start with register window 0
116 threadContexts[0]->setMiscRegNoEffect(MISCREG_CWP, 0);
117 //Always use spill and fill traps 0
118 //threadContexts[0]->setMiscRegNoEffect(MISCREG_WSTATE, 0);
119 threadContexts[0]->setIntReg(NumIntArchRegs + 7, 0);
120 //Set the trap level to 0
121 threadContexts[0]->setMiscRegNoEffect(MISCREG_TL, 0);
122 //Set the ASI register to something fixed
123 threadContexts[0]->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY);
124}
125
126void
127Sparc64LiveProcess::startup()
128{
129 argsInit(sizeof(IntReg), VMPageSize);
130
131 //From the SPARC ABI
132
133 //The process runs in user mode
134 threadContexts[0]->setMiscReg(MISCREG_PSTATE, 0x02);
135
136 //Setup default FP state
137 threadContexts[0]->setMiscRegNoEffect(MISCREG_FSR, 0);
138
139 threadContexts[0]->setMiscRegNoEffect(MISCREG_TICK, 0);
140 //
141 /*
142 * Register window management registers
143 */
144
145 //No windows contain info from other programs
146 //threadContexts[0]->setMiscRegNoEffect(MISCREG_OTHERWIN, 0);
147 threadContexts[0]->setIntReg(NumIntArchRegs + 6, 0);
148 //There are no windows to pop
149 //threadContexts[0]->setMiscRegNoEffect(MISCREG_CANRESTORE, 0);
150 threadContexts[0]->setIntReg(NumIntArchRegs + 4, 0);
151 //All windows are available to save into
152 //threadContexts[0]->setMiscRegNoEffect(MISCREG_CANSAVE, NWindows - 2);
153 threadContexts[0]->setIntReg(NumIntArchRegs + 3, NWindows - 2);
154 //All windows are "clean"
155 //threadContexts[0]->setMiscRegNoEffect(MISCREG_CLEANWIN, NWindows);
156 threadContexts[0]->setIntReg(NumIntArchRegs + 5, NWindows);
157 //Start with register window 0
158 threadContexts[0]->setMiscRegNoEffect(MISCREG_CWP, 0);
159 //Always use spill and fill traps 0
160 //threadContexts[0]->setMiscRegNoEffect(MISCREG_WSTATE, 0);
161 threadContexts[0]->setIntReg(NumIntArchRegs + 7, 0);
162 //Set the trap level to 0
163 threadContexts[0]->setMiscRegNoEffect(MISCREG_TL, 0);
164 //Set the ASI register to something fixed
165 threadContexts[0]->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY);
166}
167
168M5_32_auxv_t::M5_32_auxv_t(int32_t type, int32_t val)
169{
170 a_type = TheISA::htog(type);
171 a_val = TheISA::htog(val);
172}
173
174M5_64_auxv_t::M5_64_auxv_t(int64_t type, int64_t val)
175{
176 a_type = TheISA::htog(type);
177 a_val = TheISA::htog(val);
178}
179
180void
181Sparc64LiveProcess::argsInit(int intSize, int pageSize)
182{
183 typedef M5_64_auxv_t auxv_t;
184 Process::startup();
185
186 string filename;
187 if(argv.size() < 1)
188 filename = "";
189 else
190 filename = argv[0];
191
192 Addr alignmentMask = ~(intSize - 1);
193
194 // load object file into target memory
195 objFile->loadSections(initVirtMem);
196
197 //These are the auxilliary vector types
198 enum auxTypes
199 {
200 SPARC_AT_HWCAP = 16,
201 SPARC_AT_PAGESZ = 6,
202 SPARC_AT_CLKTCK = 17,
203 SPARC_AT_PHDR = 3,
204 SPARC_AT_PHENT = 4,
205 SPARC_AT_PHNUM = 5,
206 SPARC_AT_BASE = 7,
207 SPARC_AT_FLAGS = 8,
208 SPARC_AT_ENTRY = 9,
209 SPARC_AT_UID = 11,
210 SPARC_AT_EUID = 12,
211 SPARC_AT_GID = 13,
212 SPARC_AT_EGID = 14,
213 SPARC_AT_SECURE = 23
214 };
215
216 enum hardwareCaps
217 {
218 M5_HWCAP_SPARC_FLUSH = 1,
219 M5_HWCAP_SPARC_STBAR = 2,
220 M5_HWCAP_SPARC_SWAP = 4,
221 M5_HWCAP_SPARC_MULDIV = 8,
222 M5_HWCAP_SPARC_V9 = 16,
223 //This one should technically only be set
224 //if there is a cheetah or cheetah_plus tlb,
225 //but we'll use it all the time
226 M5_HWCAP_SPARC_ULTRA3 = 32
227 };
228
229 const int64_t hwcap =
230 M5_HWCAP_SPARC_FLUSH |
231 M5_HWCAP_SPARC_STBAR |
232 M5_HWCAP_SPARC_SWAP |
233 M5_HWCAP_SPARC_MULDIV |
234 M5_HWCAP_SPARC_V9 |
235 M5_HWCAP_SPARC_ULTRA3;
236
237
238 //Setup the auxilliary vectors. These will already have endian conversion.
239 //Auxilliary vectors are loaded only for elf formatted executables.
240 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
241 if(elfObject)
242 {
243 //Bits which describe the system hardware capabilities
244 auxv.push_back(auxv_t(SPARC_AT_HWCAP, hwcap));
245 //The system page size
246 auxv.push_back(auxv_t(SPARC_AT_PAGESZ, SparcISA::VMPageSize));
247 //Defined to be 100 in the kernel source.
248 //Frequency at which times() increments
249 auxv.push_back(auxv_t(SPARC_AT_CLKTCK, 100));
250 // For statically linked executables, this is the virtual address of the
251 // program header tables if they appear in the executable image
252 auxv.push_back(auxv_t(SPARC_AT_PHDR, elfObject->programHeaderTable()));
253 // This is the size of a program header entry from the elf file.
254 auxv.push_back(auxv_t(SPARC_AT_PHENT, elfObject->programHeaderSize()));
255 // This is the number of program headers from the original elf file.
256 auxv.push_back(auxv_t(SPARC_AT_PHNUM, elfObject->programHeaderCount()));
257 //This is the address of the elf "interpreter", It should be set
258 //to 0 for regular executables. It should be something else
259 //(not sure what) for dynamic libraries.
260 auxv.push_back(auxv_t(SPARC_AT_BASE, 0));
261 //This is hardwired to 0 in the elf loading code in the kernel
262 auxv.push_back(auxv_t(SPARC_AT_FLAGS, 0));
263 //The entry point to the program
264 auxv.push_back(auxv_t(SPARC_AT_ENTRY, objFile->entryPoint()));
265 //Different user and group IDs
266 auxv.push_back(auxv_t(SPARC_AT_UID, uid()));
267 auxv.push_back(auxv_t(SPARC_AT_EUID, euid()));
268 auxv.push_back(auxv_t(SPARC_AT_GID, gid()));
269 auxv.push_back(auxv_t(SPARC_AT_EGID, egid()));
270 //Whether to enable "secure mode" in the executable
271 auxv.push_back(auxv_t(SPARC_AT_SECURE, 0));
272 }
273
274 //Figure out how big the initial stack needs to be
275
276 // The unaccounted for 0 at the top of the stack
277 int mysterious_size = intSize;
278
279 //This is the name of the file which is present on the initial stack
280 //It's purpose is to let the user space linker examine the original file.
281 int file_name_size = filename.size() + 1;
282
283 int env_data_size = 0;
284 for (int i = 0; i < envp.size(); ++i) {
285 env_data_size += envp[i].size() + 1;
286 }
287 int arg_data_size = 0;
288 for (int i = 0; i < argv.size(); ++i) {
289 arg_data_size += argv[i].size() + 1;
290 }
291
292 //The info_block needs to be padded so it's size is a multiple of the
293 //alignment mask. Also, it appears that there needs to be at least some
294 //padding, so if the size is already a multiple, we need to increase it
295 //anyway.
296 int info_block_size =
297 (file_name_size +
298 env_data_size +
299 arg_data_size +
300 intSize) & alignmentMask;
301
302 int info_block_padding =
303 info_block_size -
304 file_name_size -
305 env_data_size -
306 arg_data_size;
307
308 //Each auxilliary vector is two 8 byte words
309 int aux_array_size = intSize * 2 * (auxv.size() + 1);
310
311 int envp_array_size = intSize * (envp.size() + 1);
312 int argv_array_size = intSize * (argv.size() + 1);
313
314 int argc_size = intSize;
315 int window_save_size = intSize * 16;
316
317 int space_needed =
318 mysterious_size +
319 info_block_size +
320 aux_array_size +
321 envp_array_size +
322 argv_array_size +
323 argc_size +
324 window_save_size;
325
326 stack_min = stack_base - space_needed;
327 stack_min &= alignmentMask;
328 stack_size = stack_base - stack_min;
329
330 // map memory
331 pTable->allocate(roundDown(stack_min, pageSize),
332 roundUp(stack_size, pageSize));
333
334 // map out initial stack contents
335 Addr mysterious_base = stack_base - mysterious_size;
336 Addr file_name_base = mysterious_base - file_name_size;
337 Addr env_data_base = file_name_base - env_data_size;
338 Addr arg_data_base = env_data_base - arg_data_size;
339 Addr auxv_array_base = arg_data_base - aux_array_size - info_block_padding;
340 Addr envp_array_base = auxv_array_base - envp_array_size;
341 Addr argv_array_base = envp_array_base - argv_array_size;
342 Addr argc_base = argv_array_base - argc_size;
343#ifndef NDEBUG
344 // only used in DPRINTF
345 Addr window_save_base = argc_base - window_save_size;
346#endif
347
348 DPRINTF(Sparc, "The addresses of items on the initial stack:\n");
349 DPRINTF(Sparc, "0x%x - file name\n", file_name_base);
350 DPRINTF(Sparc, "0x%x - env data\n", env_data_base);
351 DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base);
352 DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base);
353 DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base);
354 DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base);
355 DPRINTF(Sparc, "0x%x - argc \n", argc_base);
356 DPRINTF(Sparc, "0x%x - window save\n", window_save_base);
357 DPRINTF(Sparc, "0x%x - stack min\n", stack_min);
358
359 // write contents to stack
360
361 // figure out argc
362 uint64_t argc = argv.size();
363 uint64_t guestArgc = TheISA::htog(argc);
364
365 //Write out the mysterious 0
366 uint64_t mysterious_zero = 0;
367 initVirtMem->writeBlob(mysterious_base,
368 (uint8_t*)&mysterious_zero, mysterious_size);
369
370 //Write the file name
371 initVirtMem->writeString(file_name_base, filename.c_str());
372
373 //Copy the aux stuff
374 for(int x = 0; x < auxv.size(); x++)
375 {
376 initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
377 (uint8_t*)&(auxv[x].a_type), intSize);
378 initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
379 (uint8_t*)&(auxv[x].a_val), intSize);
380 }
381 //Write out the terminating zeroed auxilliary vector
382 const uint64_t zero = 0;
383 initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
384 (uint8_t*)&zero, 2 * intSize);
385
386 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
387 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
388
389 initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
390
391 //Stuff the trap handlers into the processes address space.
392 //Since the stack grows down and is the highest area in the processes
393 //address space, we can put stuff above it and stay out of the way.
394 int fillSize = sizeof(MachInst) * numFillInsts;
395 int spillSize = sizeof(MachInst) * numSpillInsts;
396 fillStart = stack_base;
397 spillStart = fillStart + fillSize;
398 initVirtMem->writeBlob(fillStart, (uint8_t*)fillHandler64, fillSize);
399 initVirtMem->writeBlob(spillStart, (uint8_t*)spillHandler64, spillSize);
400
401 //Set up the thread context to start running the process
402 threadContexts[0]->setIntReg(ArgumentReg0, argc);
403 threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base);
404 threadContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias);
405
406 Addr prog_entry = objFile->entryPoint();
407 threadContexts[0]->setPC(prog_entry);
408 threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
409 threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
410
411 //Align the "stack_min" to a page boundary.
412 stack_min = roundDown(stack_min, pageSize);
413
414// num_processes++;
415}
416
417void
418Sparc32LiveProcess::argsInit(int intSize, int pageSize)
419{
420 typedef M5_32_auxv_t auxv_t;
421 Process::startup();
422
423 string filename;
424 if(argv.size() < 1)
425 filename = "";
426 else
427 filename = argv[0];
428
429 //Even though this is a 32 bit process, the ABI says we still need to
430 //maintain double word alignment of the stack pointer.
431 Addr alignmentMask = ~(8 - 1);
432
433 // load object file into target memory
434 objFile->loadSections(initVirtMem);
435
436 //These are the auxilliary vector types
437 enum auxTypes
438 {
439 SPARC_AT_HWCAP = 16,
440 SPARC_AT_PAGESZ = 6,
441 SPARC_AT_CLKTCK = 17,
442 SPARC_AT_PHDR = 3,
443 SPARC_AT_PHENT = 4,
444 SPARC_AT_PHNUM = 5,
445 SPARC_AT_BASE = 7,
446 SPARC_AT_FLAGS = 8,
447 SPARC_AT_ENTRY = 9,
448 SPARC_AT_UID = 11,
449 SPARC_AT_EUID = 12,
450 SPARC_AT_GID = 13,
451 SPARC_AT_EGID = 14,
452 SPARC_AT_SECURE = 23
453 };
454
455 enum hardwareCaps
456 {
457 M5_HWCAP_SPARC_FLUSH = 1,
458 M5_HWCAP_SPARC_STBAR = 2,
459 M5_HWCAP_SPARC_SWAP = 4,
460 M5_HWCAP_SPARC_MULDIV = 8,
461 M5_HWCAP_SPARC_V9 = 16,
462 //This one should technically only be set
463 //if there is a cheetah or cheetah_plus tlb,
464 //but we'll use it all the time
465 M5_HWCAP_SPARC_ULTRA3 = 32
466 };
467
468 const int64_t hwcap =
469 M5_HWCAP_SPARC_FLUSH |
470 M5_HWCAP_SPARC_STBAR |
471 M5_HWCAP_SPARC_SWAP |
472 M5_HWCAP_SPARC_MULDIV |
473 M5_HWCAP_SPARC_V9 |
474 M5_HWCAP_SPARC_ULTRA3;
475
476
477 //Setup the auxilliary vectors. These will already have endian conversion.
478 //Auxilliary vectors are loaded only for elf formatted executables.
479 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
480 if(elfObject)
481 {
482 //Bits which describe the system hardware capabilities
483 auxv.push_back(auxv_t(SPARC_AT_HWCAP, hwcap));
484 //The system page size
485 auxv.push_back(auxv_t(SPARC_AT_PAGESZ, SparcISA::VMPageSize));
486 //Defined to be 100 in the kernel source.
487 //Frequency at which times() increments
488 auxv.push_back(auxv_t(SPARC_AT_CLKTCK, 100));
489 // For statically linked executables, this is the virtual address of the
490 // program header tables if they appear in the executable image
491 auxv.push_back(auxv_t(SPARC_AT_PHDR, elfObject->programHeaderTable()));
492 // This is the size of a program header entry from the elf file.
493 auxv.push_back(auxv_t(SPARC_AT_PHENT, elfObject->programHeaderSize()));
494 // This is the number of program headers from the original elf file.
495 auxv.push_back(auxv_t(SPARC_AT_PHNUM, elfObject->programHeaderCount()));
496 //This is the address of the elf "interpreter", It should be set
497 //to 0 for regular executables. It should be something else
498 //(not sure what) for dynamic libraries.
499 auxv.push_back(auxv_t(SPARC_AT_BASE, 0));
500 //This is hardwired to 0 in the elf loading code in the kernel
501 auxv.push_back(auxv_t(SPARC_AT_FLAGS, 0));
502 //The entry point to the program
503 auxv.push_back(auxv_t(SPARC_AT_ENTRY, objFile->entryPoint()));
504 //Different user and group IDs
505 auxv.push_back(auxv_t(SPARC_AT_UID, uid()));
506 auxv.push_back(auxv_t(SPARC_AT_EUID, euid()));
507 auxv.push_back(auxv_t(SPARC_AT_GID, gid()));
508 auxv.push_back(auxv_t(SPARC_AT_EGID, egid()));
509 //Whether to enable "secure mode" in the executable
510 auxv.push_back(auxv_t(SPARC_AT_SECURE, 0));
511 }
512
513 //Figure out how big the initial stack needs to be
514
515 // The unaccounted for 8 byte 0 at the top of the stack
516 int mysterious_size = 8;
517
518 //This is the name of the file which is present on the initial stack
519 //It's purpose is to let the user space linker examine the original file.
520 int file_name_size = filename.size() + 1;
521
522 int env_data_size = 0;
523 for (int i = 0; i < envp.size(); ++i) {
524 env_data_size += envp[i].size() + 1;
525 }
526 int arg_data_size = 0;
527 for (int i = 0; i < argv.size(); ++i) {
528 arg_data_size += argv[i].size() + 1;
529 }
530
531 //The info_block - This seems to need an pad for some reason.
532 int info_block_size =
533 (mysterious_size +
534 file_name_size +
535 env_data_size +
536 arg_data_size + intSize);
537
538 //Each auxilliary vector is two 4 byte words
539 int aux_array_size = intSize * 2 * (auxv.size() + 1);
540
541 int envp_array_size = intSize * (envp.size() + 1);
542 int argv_array_size = intSize * (argv.size() + 1);
543
544 int argc_size = intSize;
545 int window_save_size = intSize * 16;
546
547 int space_needed =
548 info_block_size +
549 aux_array_size +
550 envp_array_size +
551 argv_array_size +
552 argc_size +
553 window_save_size;
554
555 stack_min = stack_base - space_needed;
556 stack_min &= alignmentMask;
557 stack_size = stack_base - stack_min;
558
559 // map memory
560 pTable->allocate(roundDown(stack_min, pageSize),
561 roundUp(stack_size, pageSize));
562
563 // map out initial stack contents
564 uint32_t window_save_base = stack_min;
565 uint32_t argc_base = window_save_base + window_save_size;
566 uint32_t argv_array_base = argc_base + argc_size;
567 uint32_t envp_array_base = argv_array_base + argv_array_size;
568 uint32_t auxv_array_base = envp_array_base + envp_array_size;
569 //The info block is pushed up against the top of the stack, while
570 //the rest of the initial stack frame is aligned to an 8 byte boudary.
571 uint32_t arg_data_base = stack_base - info_block_size + intSize;
572 uint32_t env_data_base = arg_data_base + arg_data_size;
573 uint32_t file_name_base = env_data_base + env_data_size;
574 uint32_t mysterious_base = file_name_base + file_name_size;
575
576 DPRINTF(Sparc, "The addresses of items on the initial stack:\n");
577 DPRINTF(Sparc, "0x%x - file name\n", file_name_base);
578 DPRINTF(Sparc, "0x%x - env data\n", env_data_base);
579 DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base);
580 DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base);
581 DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base);
582 DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base);
583 DPRINTF(Sparc, "0x%x - argc \n", argc_base);
584 DPRINTF(Sparc, "0x%x - window save\n", window_save_base);
585 DPRINTF(Sparc, "0x%x - stack min\n", stack_min);
586
587 // write contents to stack
588
589 // figure out argc
590 uint32_t argc = argv.size();
591 uint32_t guestArgc = TheISA::htog(argc);
592
593 //Write out the mysterious 0
594 uint64_t mysterious_zero = 0;
595 initVirtMem->writeBlob(mysterious_base,
596 (uint8_t*)&mysterious_zero, mysterious_size);
597
598 //Write the file name
599 initVirtMem->writeString(file_name_base, filename.c_str());
600
601 //Copy the aux stuff
602 for(int x = 0; x < auxv.size(); x++)
603 {
604 initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
605 (uint8_t*)&(auxv[x].a_type), intSize);
606 initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
607 (uint8_t*)&(auxv[x].a_val), intSize);
608 }
609 //Write out the terminating zeroed auxilliary vector
610 const uint64_t zero = 0;
611 initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
612 (uint8_t*)&zero, 2 * intSize);
613
614 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
615 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
616
617 initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
618
619 //Stuff the trap handlers into the processes address space.
620 //Since the stack grows down and is the highest area in the processes
621 //address space, we can put stuff above it and stay out of the way.
622 int fillSize = sizeof(MachInst) * numFillInsts;
623 int spillSize = sizeof(MachInst) * numSpillInsts;
624 fillStart = stack_base;
625 spillStart = fillStart + fillSize;
626 initVirtMem->writeBlob(fillStart, (uint8_t*)fillHandler32, fillSize);
627 initVirtMem->writeBlob(spillStart, (uint8_t*)spillHandler32, spillSize);
628
629 //Set up the thread context to start running the process
630 //threadContexts[0]->setIntReg(ArgumentReg0, argc);
631 //threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base);
632 threadContexts[0]->setIntReg(StackPointerReg, stack_min);
633
634 uint32_t prog_entry = objFile->entryPoint();
635 threadContexts[0]->setPC(prog_entry);
636 threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
637 threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
638
639 //Align the "stack_min" to a page boundary.
640 stack_min = roundDown(stack_min, pageSize);
641
642// num_processes++;
643}