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