process.cc revision 4111:65fffcb4fae9
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
91    threadContexts[0]->setMiscRegWithEffect(MISCREG_PSTATE, 0x02);
92
93    //Setup default FP state
94    threadContexts[0]->setMiscReg(MISCREG_FSR, 0);
95
96    threadContexts[0]->setMiscReg(MISCREG_TICK, 0);
97    //
98    /*
99     * Register window management registers
100     */
101
102    //No windows contain info from other programs
103    //threadContexts[0]->setMiscReg(MISCREG_OTHERWIN, 0);
104    threadContexts[0]->setIntReg(NumIntArchRegs + 6, 0);
105    //There are no windows to pop
106    //threadContexts[0]->setMiscReg(MISCREG_CANRESTORE, 0);
107    threadContexts[0]->setIntReg(NumIntArchRegs + 4, 0);
108    //All windows are available to save into
109    //threadContexts[0]->setMiscReg(MISCREG_CANSAVE, NWindows - 2);
110    threadContexts[0]->setIntReg(NumIntArchRegs + 3, NWindows - 2);
111    //All windows are "clean"
112    //threadContexts[0]->setMiscReg(MISCREG_CLEANWIN, NWindows);
113    threadContexts[0]->setIntReg(NumIntArchRegs + 5, NWindows);
114    //Start with register window 0
115    threadContexts[0]->setMiscReg(MISCREG_CWP, 0);
116    //Always use spill and fill traps 0
117    //threadContexts[0]->setMiscReg(MISCREG_WSTATE, 0);
118    threadContexts[0]->setIntReg(NumIntArchRegs + 7, 0);
119    //Set the trap level to 0
120    threadContexts[0]->setMiscReg(MISCREG_TL, 0);
121    //Set the ASI register to something fixed
122    threadContexts[0]->setMiscReg(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]->setMiscRegWithEffect(MISCREG_PSTATE, 0x02);
134
135    //Setup default FP state
136    threadContexts[0]->setMiscReg(MISCREG_FSR, 0);
137
138    threadContexts[0]->setMiscReg(MISCREG_TICK, 0);
139    //
140    /*
141     * Register window management registers
142     */
143
144    //No windows contain info from other programs
145    //threadContexts[0]->setMiscReg(MISCREG_OTHERWIN, 0);
146    threadContexts[0]->setIntReg(NumIntArchRegs + 6, 0);
147    //There are no windows to pop
148    //threadContexts[0]->setMiscReg(MISCREG_CANRESTORE, 0);
149    threadContexts[0]->setIntReg(NumIntArchRegs + 4, 0);
150    //All windows are available to save into
151    //threadContexts[0]->setMiscReg(MISCREG_CANSAVE, NWindows - 2);
152    threadContexts[0]->setIntReg(NumIntArchRegs + 3, NWindows - 2);
153    //All windows are "clean"
154    //threadContexts[0]->setMiscReg(MISCREG_CLEANWIN, NWindows);
155    threadContexts[0]->setIntReg(NumIntArchRegs + 5, NWindows);
156    //Start with register window 0
157    threadContexts[0]->setMiscReg(MISCREG_CWP, 0);
158    //Always use spill and fill traps 0
159    //threadContexts[0]->setMiscReg(MISCREG_WSTATE, 0);
160    threadContexts[0]->setIntReg(NumIntArchRegs + 7, 0);
161    //Set the trap level to 0
162    threadContexts[0]->setMiscReg(MISCREG_TL, 0);
163    //Set the ASI register to something fixed
164    threadContexts[0]->setMiscReg(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    Addr alignmentMask = ~(intSize - 1);
429
430    // load object file into target memory
431    objFile->loadSections(initVirtMem);
432
433    //These are the auxilliary vector types
434    enum auxTypes
435    {
436        SPARC_AT_HWCAP = 16,
437        SPARC_AT_PAGESZ = 6,
438        SPARC_AT_CLKTCK = 17,
439        SPARC_AT_PHDR = 3,
440        SPARC_AT_PHENT = 4,
441        SPARC_AT_PHNUM = 5,
442        SPARC_AT_BASE = 7,
443        SPARC_AT_FLAGS = 8,
444        SPARC_AT_ENTRY = 9,
445        SPARC_AT_UID = 11,
446        SPARC_AT_EUID = 12,
447        SPARC_AT_GID = 13,
448        SPARC_AT_EGID = 14,
449        SPARC_AT_SECURE = 23
450    };
451
452    enum hardwareCaps
453    {
454        M5_HWCAP_SPARC_FLUSH = 1,
455        M5_HWCAP_SPARC_STBAR = 2,
456        M5_HWCAP_SPARC_SWAP = 4,
457        M5_HWCAP_SPARC_MULDIV = 8,
458        M5_HWCAP_SPARC_V9 = 16,
459        //This one should technically only be set
460        //if there is a cheetah or cheetah_plus tlb,
461        //but we'll use it all the time
462        M5_HWCAP_SPARC_ULTRA3 = 32
463    };
464
465    const int64_t hwcap =
466        M5_HWCAP_SPARC_FLUSH |
467        M5_HWCAP_SPARC_STBAR |
468        M5_HWCAP_SPARC_SWAP |
469        M5_HWCAP_SPARC_MULDIV |
470        M5_HWCAP_SPARC_V9 |
471        M5_HWCAP_SPARC_ULTRA3;
472
473
474    //Setup the auxilliary vectors. These will already have endian conversion.
475    //Auxilliary vectors are loaded only for elf formatted executables.
476    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
477    if(elfObject)
478    {
479        //Bits which describe the system hardware capabilities
480        auxv.push_back(auxv_t(SPARC_AT_HWCAP, hwcap));
481        //The system page size
482        auxv.push_back(auxv_t(SPARC_AT_PAGESZ, SparcISA::VMPageSize));
483        //Defined to be 100 in the kernel source.
484        //Frequency at which times() increments
485        auxv.push_back(auxv_t(SPARC_AT_CLKTCK, 100));
486        // For statically linked executables, this is the virtual address of the
487        // program header tables if they appear in the executable image
488        auxv.push_back(auxv_t(SPARC_AT_PHDR, elfObject->programHeaderTable()));
489        // This is the size of a program header entry from the elf file.
490        auxv.push_back(auxv_t(SPARC_AT_PHENT, elfObject->programHeaderSize()));
491        // This is the number of program headers from the original elf file.
492        auxv.push_back(auxv_t(SPARC_AT_PHNUM, elfObject->programHeaderCount()));
493        //This is the address of the elf "interpreter", It should be set
494        //to 0 for regular executables. It should be something else
495        //(not sure what) for dynamic libraries.
496        auxv.push_back(auxv_t(SPARC_AT_BASE, 0));
497        //This is hardwired to 0 in the elf loading code in the kernel
498        auxv.push_back(auxv_t(SPARC_AT_FLAGS, 0));
499        //The entry point to the program
500        auxv.push_back(auxv_t(SPARC_AT_ENTRY, objFile->entryPoint()));
501        //Different user and group IDs
502        auxv.push_back(auxv_t(SPARC_AT_UID, uid()));
503        auxv.push_back(auxv_t(SPARC_AT_EUID, euid()));
504        auxv.push_back(auxv_t(SPARC_AT_GID, gid()));
505        auxv.push_back(auxv_t(SPARC_AT_EGID, egid()));
506        //Whether to enable "secure mode" in the executable
507        auxv.push_back(auxv_t(SPARC_AT_SECURE, 0));
508    }
509
510    //Figure out how big the initial stack needs to be
511
512    // The unaccounted for 0 at the top of the stack
513    int mysterious_size = intSize;
514
515    //This is the name of the file which is present on the initial stack
516    //It's purpose is to let the user space linker examine the original file.
517    int file_name_size = filename.size() + 1;
518
519    int env_data_size = 0;
520    for (int i = 0; i < envp.size(); ++i) {
521        env_data_size += envp[i].size() + 1;
522    }
523    int arg_data_size = 0;
524    for (int i = 0; i < argv.size(); ++i) {
525        arg_data_size += argv[i].size() + 1;
526    }
527
528    //The info_block needs to be padded so it's size is a multiple of the
529    //alignment mask. Also, it appears that there needs to be at least some
530    //padding, so if the size is already a multiple, we need to increase it
531    //anyway.
532    int info_block_size =
533        (file_name_size +
534        env_data_size +
535        arg_data_size +
536        intSize) & alignmentMask;
537
538    int info_block_padding =
539        info_block_size -
540        file_name_size -
541        env_data_size -
542        arg_data_size;
543
544    //Each auxilliary vector is two 8 byte words
545    int aux_array_size = intSize * 2 * (auxv.size() + 1);
546
547    int envp_array_size = intSize * (envp.size() + 1);
548    int argv_array_size = intSize * (argv.size() + 1);
549
550    int argc_size = intSize;
551    int window_save_size = intSize * 16;
552
553    int space_needed =
554        mysterious_size +
555        info_block_size +
556        aux_array_size +
557        envp_array_size +
558        argv_array_size +
559        argc_size +
560        window_save_size;
561
562    stack_min = stack_base - space_needed;
563    stack_min &= alignmentMask;
564    stack_size = stack_base - stack_min;
565
566    // map memory
567    pTable->allocate(roundDown(stack_min, pageSize),
568                     roundUp(stack_size, pageSize));
569
570    // map out initial stack contents
571    Addr mysterious_base = stack_base - mysterious_size;
572    Addr file_name_base = mysterious_base - file_name_size;
573    Addr env_data_base = file_name_base - env_data_size;
574    Addr arg_data_base = env_data_base - arg_data_size;
575    Addr auxv_array_base = arg_data_base - aux_array_size - info_block_padding;
576    Addr envp_array_base = auxv_array_base - envp_array_size;
577    Addr argv_array_base = envp_array_base - argv_array_size;
578    Addr argc_base = argv_array_base - argc_size;
579#ifndef NDEBUG
580    // only used in DPRINTF
581    Addr window_save_base = argc_base - window_save_size;
582#endif
583
584    DPRINTF(Sparc, "The addresses of items on the initial stack:\n");
585    DPRINTF(Sparc, "0x%x - file name\n", file_name_base);
586    DPRINTF(Sparc, "0x%x - env data\n", env_data_base);
587    DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base);
588    DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base);
589    DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base);
590    DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base);
591    DPRINTF(Sparc, "0x%x - argc \n", argc_base);
592    DPRINTF(Sparc, "0x%x - window save\n", window_save_base);
593    DPRINTF(Sparc, "0x%x - stack min\n", stack_min);
594
595    // write contents to stack
596
597    // figure out argc
598    uint32_t argc = argv.size();
599    uint32_t guestArgc = TheISA::htog(argc);
600
601    //Write out the mysterious 0
602    uint64_t mysterious_zero = 0;
603    initVirtMem->writeBlob(mysterious_base,
604            (uint8_t*)&mysterious_zero, mysterious_size);
605
606    //Write the file name
607    initVirtMem->writeString(file_name_base, filename.c_str());
608
609    //Copy the aux stuff
610    for(int x = 0; x < auxv.size(); x++)
611    {
612        initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
613                (uint8_t*)&(auxv[x].a_type), intSize);
614        initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
615                (uint8_t*)&(auxv[x].a_val), intSize);
616    }
617    //Write out the terminating zeroed auxilliary vector
618    const uint64_t zero = 0;
619    initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
620            (uint8_t*)&zero, 2 * intSize);
621
622    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem, intSize);
623    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem, intSize);
624
625    initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
626
627    //Stuff the trap handlers into the processes address space.
628    //Since the stack grows down and is the highest area in the processes
629    //address space, we can put stuff above it and stay out of the way.
630    int fillSize = sizeof(MachInst) * numFillInsts;
631    int spillSize = sizeof(MachInst) * numSpillInsts;
632    fillStart = stack_base;
633    spillStart = fillStart + fillSize;
634    initVirtMem->writeBlob(fillStart, (uint8_t*)fillHandler32, fillSize);
635    initVirtMem->writeBlob(spillStart, (uint8_t*)spillHandler32, spillSize);
636
637    //Set up the thread context to start running the process
638    threadContexts[0]->setIntReg(ArgumentReg0, argc);
639    threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base);
640    threadContexts[0]->setIntReg(StackPointerReg, stack_min);
641
642    Addr prog_entry = objFile->entryPoint();
643    threadContexts[0]->setPC(prog_entry);
644    threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
645    threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
646
647    //Align the "stack_min" to a page boundary.
648    stack_min = roundDown(stack_min, pageSize);
649
650//    num_processes++;
651}
652