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