process.cc revision 3760
12207SN/A/*
22207SN/A * Copyright (c) 2003-2004 The Regents of The University of Michigan
32207SN/A * All rights reserved.
42207SN/A *
52207SN/A * Redistribution and use in source and binary forms, with or without
62207SN/A * modification, are permitted provided that the following conditions are
72207SN/A * met: redistributions of source code must retain the above copyright
82207SN/A * notice, this list of conditions and the following disclaimer;
92207SN/A * redistributions in binary form must reproduce the above copyright
102207SN/A * notice, this list of conditions and the following disclaimer in the
112207SN/A * documentation and/or other materials provided with the distribution;
122207SN/A * neither the name of the copyright holders nor the names of its
132207SN/A * contributors may be used to endorse or promote products derived from
142207SN/A * this software without specific prior written permission.
152207SN/A *
162207SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172207SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182207SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192207SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202207SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212207SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222207SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232207SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242207SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252207SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262207SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Gabe Black
292665Ssaidi@eecs.umich.edu *          Ali Saidi
302207SN/A */
312207SN/A
323589Sgblack@eecs.umich.edu#include "arch/sparc/asi.hh"
332474SN/A#include "arch/sparc/isa_traits.hh"
342207SN/A#include "arch/sparc/process.hh"
353760Sgblack@eecs.umich.edu#include "arch/sparc/types.hh"
362454SN/A#include "base/loader/object_file.hh"
372976Sgblack@eecs.umich.edu#include "base/loader/elf_object.hh"
382454SN/A#include "base/misc.hh"
392680Sktlim@umich.edu#include "cpu/thread_context.hh"
402561SN/A#include "mem/page_table.hh"
412561SN/A#include "mem/translating_port.hh"
422474SN/A#include "sim/system.hh"
432207SN/A
442458SN/Ausing namespace std;
452474SN/Ausing namespace SparcISA;
462458SN/A
472207SN/A
482474SN/ASparcLiveProcess::SparcLiveProcess(const std::string &nm, ObjectFile *objFile,
492474SN/A        System *_system, int stdin_fd, int stdout_fd, int stderr_fd,
503114Sgblack@eecs.umich.edu        std::vector<std::string> &argv, std::vector<std::string> &envp,
513669Sbinkertn@umich.edu        const std::string &cwd,
523114Sgblack@eecs.umich.edu        uint64_t _uid, uint64_t _euid, uint64_t _gid, uint64_t _egid,
533114Sgblack@eecs.umich.edu        uint64_t _pid, uint64_t _ppid)
542474SN/A    : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd,
553669Sbinkertn@umich.edu        argv, envp, cwd, _uid, _euid, _gid, _egid, _pid, _ppid)
562474SN/A{
572474SN/A
582474SN/A    // XXX all the below need to be updated for SPARC - Ali
592474SN/A    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
602474SN/A    brk_point = roundUp(brk_point, VMPageSize);
612474SN/A
622561SN/A    // Set up stack. On SPARC Linux, stack goes from the top of memory
632561SN/A    // downward, less the hole for the kernel address space.
643044Sgblack@eecs.umich.edu    stack_base = (Addr)0x80000000000ULL;
652474SN/A
662474SN/A    // Set up region for mmaps.  Tru64 seems to start just above 0 and
672474SN/A    // grow up from there.
683044Sgblack@eecs.umich.edu    mmap_start = mmap_end = 0xfffff80000000000ULL;
692474SN/A
702474SN/A    // Set pointer for next thread stack.  Reserve 8M for main stack.
712474SN/A    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
723415Sgblack@eecs.umich.edu
733415Sgblack@eecs.umich.edu    //Initialize these to 0s
743415Sgblack@eecs.umich.edu    fillStart = 0;
753415Sgblack@eecs.umich.edu    spillStart = 0;
762474SN/A}
772474SN/A
782474SN/Avoid
792474SN/ASparcLiveProcess::startup()
802474SN/A{
813760Sgblack@eecs.umich.edu    argsInit(sizeof(IntReg), VMPageSize);
822561SN/A
832561SN/A    //From the SPARC ABI
842561SN/A
852561SN/A    //The process runs in user mode
862680Sktlim@umich.edu    threadContexts[0]->setMiscRegWithEffect(MISCREG_PSTATE, 0x02);
872561SN/A
882646Ssaidi@eecs.umich.edu    //Setup default FP state
892680Sktlim@umich.edu    threadContexts[0]->setMiscReg(MISCREG_FSR, 0);
902646Ssaidi@eecs.umich.edu
912680Sktlim@umich.edu    threadContexts[0]->setMiscReg(MISCREG_TICK, 0);
922646Ssaidi@eecs.umich.edu    //
932561SN/A    /*
942561SN/A     * Register window management registers
952561SN/A     */
962561SN/A
972561SN/A    //No windows contain info from other programs
983415Sgblack@eecs.umich.edu    threadContexts[0]->setMiscReg(MISCREG_OTHERWIN, 0);
992561SN/A    //There are no windows to pop
1003415Sgblack@eecs.umich.edu    threadContexts[0]->setMiscReg(MISCREG_CANRESTORE, 0);
1012561SN/A    //All windows are available to save into
1023415Sgblack@eecs.umich.edu    threadContexts[0]->setMiscReg(MISCREG_CANSAVE, NWindows - 2);
1032561SN/A    //All windows are "clean"
1043415Sgblack@eecs.umich.edu    threadContexts[0]->setMiscReg(MISCREG_CLEANWIN, NWindows);
1052561SN/A    //Start with register window 0
1063415Sgblack@eecs.umich.edu    threadContexts[0]->setMiscReg(MISCREG_CWP, 0);
1073415Sgblack@eecs.umich.edu    //Always use spill and fill traps 0
1083415Sgblack@eecs.umich.edu    threadContexts[0]->setMiscReg(MISCREG_WSTATE, 0);
1093415Sgblack@eecs.umich.edu    //Set the trap level to 0
1103415Sgblack@eecs.umich.edu    threadContexts[0]->setMiscReg(MISCREG_TL, 0);
1113589Sgblack@eecs.umich.edu    //Set the ASI register to something fixed
1123589Sgblack@eecs.umich.edu    threadContexts[0]->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
1132474SN/A}
1142474SN/A
1152585SN/Am5_auxv_t buildAuxVect(int64_t type, int64_t val)
1162585SN/A{
1172585SN/A    m5_auxv_t result;
1182585SN/A    result.a_type = TheISA::htog(type);
1192585SN/A    result.a_val = TheISA::htog(val);
1202585SN/A    return result;
1212585SN/A}
1222585SN/A
1233415Sgblack@eecs.umich.edu//We only use 19 instructions for the trap handlers, but there would be
1243415Sgblack@eecs.umich.edu//space for 32 in a real SPARC trap table.
1253415Sgblack@eecs.umich.educonst int numFillInsts = 32;
1263415Sgblack@eecs.umich.educonst int numSpillInsts = 32;
1273415Sgblack@eecs.umich.edu
1283415Sgblack@eecs.umich.eduMachInst fillHandler[numFillInsts] =
1293415Sgblack@eecs.umich.edu{
1303415Sgblack@eecs.umich.edu    htog(0x87802018), //wr %g0, ASI_AIUP, %asi
1313415Sgblack@eecs.umich.edu    htog(0xe0dba7ff), //ldxa [%sp + BIAS + (0*8)] %asi, %l0
1323415Sgblack@eecs.umich.edu    htog(0xe2dba807), //ldxa [%sp + BIAS + (1*8)] %asi, %l1
1333415Sgblack@eecs.umich.edu    htog(0xe4dba80f), //ldxa [%sp + BIAS + (2*8)] %asi, %l2
1343415Sgblack@eecs.umich.edu    htog(0xe6dba817), //ldxa [%sp + BIAS + (3*8)] %asi, %l3
1353415Sgblack@eecs.umich.edu    htog(0xe8dba81f), //ldxa [%sp + BIAS + (4*8)] %asi, %l4
1363415Sgblack@eecs.umich.edu    htog(0xeadba827), //ldxa [%sp + BIAS + (5*8)] %asi, %l5
1373415Sgblack@eecs.umich.edu    htog(0xecdba82f), //ldxa [%sp + BIAS + (6*8)] %asi, %l6
1383415Sgblack@eecs.umich.edu    htog(0xeedba837), //ldxa [%sp + BIAS + (7*8)] %asi, %l7
1393415Sgblack@eecs.umich.edu    htog(0xf0dba83f), //ldxa [%sp + BIAS + (8*8)] %asi, %i0
1403415Sgblack@eecs.umich.edu    htog(0xf2dba847), //ldxa [%sp + BIAS + (9*8)] %asi, %i1
1413415Sgblack@eecs.umich.edu    htog(0xf4dba84f), //ldxa [%sp + BIAS + (10*8)] %asi, %i2
1423415Sgblack@eecs.umich.edu    htog(0xf6dba857), //ldxa [%sp + BIAS + (11*8)] %asi, %i3
1433415Sgblack@eecs.umich.edu    htog(0xf8dba85f), //ldxa [%sp + BIAS + (12*8)] %asi, %i4
1443415Sgblack@eecs.umich.edu    htog(0xfadba867), //ldxa [%sp + BIAS + (13*8)] %asi, %i5
1453415Sgblack@eecs.umich.edu    htog(0xfcdba86f), //ldxa [%sp + BIAS + (14*8)] %asi, %i6
1463415Sgblack@eecs.umich.edu    htog(0xfedba877), //ldxa [%sp + BIAS + (15*8)] %asi, %i7
1473415Sgblack@eecs.umich.edu    htog(0x83880000), //restored
1483415Sgblack@eecs.umich.edu    htog(0x83F00000), //retry
1493415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1503415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1513415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1523415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1533415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1543415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1553415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1563415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1573415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1583415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1593415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1603415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1613415Sgblack@eecs.umich.edu    htog(0x00000000)  //illtrap
1623415Sgblack@eecs.umich.edu};
1633415Sgblack@eecs.umich.edu
1643415Sgblack@eecs.umich.eduMachInst spillHandler[numSpillInsts] =
1653415Sgblack@eecs.umich.edu{
1663415Sgblack@eecs.umich.edu    htog(0x87802018), //wr %g0, ASI_AIUP, %asi
1673415Sgblack@eecs.umich.edu    htog(0xe0f3a7ff), //stxa %l0, [%sp + BIAS + (0*8)] %asi
1683415Sgblack@eecs.umich.edu    htog(0xe2f3a807), //stxa %l1, [%sp + BIAS + (1*8)] %asi
1693415Sgblack@eecs.umich.edu    htog(0xe4f3a80f), //stxa %l2, [%sp + BIAS + (2*8)] %asi
1703415Sgblack@eecs.umich.edu    htog(0xe6f3a817), //stxa %l3, [%sp + BIAS + (3*8)] %asi
1713415Sgblack@eecs.umich.edu    htog(0xe8f3a81f), //stxa %l4, [%sp + BIAS + (4*8)] %asi
1723415Sgblack@eecs.umich.edu    htog(0xeaf3a827), //stxa %l5, [%sp + BIAS + (5*8)] %asi
1733415Sgblack@eecs.umich.edu    htog(0xecf3a82f), //stxa %l6, [%sp + BIAS + (6*8)] %asi
1743415Sgblack@eecs.umich.edu    htog(0xeef3a837), //stxa %l7, [%sp + BIAS + (7*8)] %asi
1753415Sgblack@eecs.umich.edu    htog(0xf0f3a83f), //stxa %i0, [%sp + BIAS + (8*8)] %asi
1763415Sgblack@eecs.umich.edu    htog(0xf2f3a847), //stxa %i1, [%sp + BIAS + (9*8)] %asi
1773415Sgblack@eecs.umich.edu    htog(0xf4f3a84f), //stxa %i2, [%sp + BIAS + (10*8)] %asi
1783415Sgblack@eecs.umich.edu    htog(0xf6f3a857), //stxa %i3, [%sp + BIAS + (11*8)] %asi
1793415Sgblack@eecs.umich.edu    htog(0xf8f3a85f), //stxa %i4, [%sp + BIAS + (12*8)] %asi
1803415Sgblack@eecs.umich.edu    htog(0xfaf3a867), //stxa %i5, [%sp + BIAS + (13*8)] %asi
1813415Sgblack@eecs.umich.edu    htog(0xfcf3a86f), //stxa %i6, [%sp + BIAS + (14*8)] %asi
1823415Sgblack@eecs.umich.edu    htog(0xfef3a877), //stxa %i7, [%sp + BIAS + (15*8)] %asi
1833415Sgblack@eecs.umich.edu    htog(0x81880000), //saved
1843415Sgblack@eecs.umich.edu    htog(0x83F00000), //retry
1853415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1863415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1873415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1883415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1893415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1903415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1913415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1923415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1933415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1943415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1953415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1963415Sgblack@eecs.umich.edu    htog(0x00000000), //illtrap
1973415Sgblack@eecs.umich.edu    htog(0x00000000)  //illtrap
1983415Sgblack@eecs.umich.edu};
1993415Sgblack@eecs.umich.edu
2002561SN/Avoid
2012561SN/ASparcLiveProcess::argsInit(int intSize, int pageSize)
2022561SN/A{
2032561SN/A    Process::startup();
2042474SN/A
2053044Sgblack@eecs.umich.edu    string filename;
2063044Sgblack@eecs.umich.edu    if(argv.size() < 1)
2073044Sgblack@eecs.umich.edu        filename = "";
2083044Sgblack@eecs.umich.edu    else
2093044Sgblack@eecs.umich.edu        filename = argv[0];
2103044Sgblack@eecs.umich.edu
2112561SN/A    Addr alignmentMask = ~(intSize - 1);
2122561SN/A
2132561SN/A    // load object file into target memory
2142561SN/A    objFile->loadSections(initVirtMem);
2152561SN/A
2162585SN/A    //These are the auxilliary vector types
2172585SN/A    enum auxTypes
2182585SN/A    {
2192585SN/A        SPARC_AT_HWCAP = 16,
2202585SN/A        SPARC_AT_PAGESZ = 6,
2212585SN/A        SPARC_AT_CLKTCK = 17,
2222585SN/A        SPARC_AT_PHDR = 3,
2232585SN/A        SPARC_AT_PHENT = 4,
2242585SN/A        SPARC_AT_PHNUM = 5,
2252585SN/A        SPARC_AT_BASE = 7,
2262585SN/A        SPARC_AT_FLAGS = 8,
2272585SN/A        SPARC_AT_ENTRY = 9,
2282585SN/A        SPARC_AT_UID = 11,
2292585SN/A        SPARC_AT_EUID = 12,
2302585SN/A        SPARC_AT_GID = 13,
2312976Sgblack@eecs.umich.edu        SPARC_AT_EGID = 14,
2322976Sgblack@eecs.umich.edu        SPARC_AT_SECURE = 23
2332585SN/A    };
2342585SN/A
2352585SN/A    enum hardwareCaps
2362585SN/A    {
2372585SN/A        M5_HWCAP_SPARC_FLUSH = 1,
2382585SN/A        M5_HWCAP_SPARC_STBAR = 2,
2392585SN/A        M5_HWCAP_SPARC_SWAP = 4,
2402585SN/A        M5_HWCAP_SPARC_MULDIV = 8,
2412585SN/A        M5_HWCAP_SPARC_V9 = 16,
2422585SN/A        //This one should technically only be set
2432585SN/A        //if there is a cheetah or cheetah_plus tlb,
2442585SN/A        //but we'll use it all the time
2452585SN/A        M5_HWCAP_SPARC_ULTRA3 = 32
2462585SN/A    };
2472585SN/A
2482585SN/A    const int64_t hwcap =
2492585SN/A        M5_HWCAP_SPARC_FLUSH |
2502585SN/A        M5_HWCAP_SPARC_STBAR |
2512585SN/A        M5_HWCAP_SPARC_SWAP |
2522585SN/A        M5_HWCAP_SPARC_MULDIV |
2532585SN/A        M5_HWCAP_SPARC_V9 |
2542585SN/A        M5_HWCAP_SPARC_ULTRA3;
2552585SN/A
2562976Sgblack@eecs.umich.edu
2572976Sgblack@eecs.umich.edu    //Setup the auxilliary vectors. These will already have endian conversion.
2582976Sgblack@eecs.umich.edu    //Auxilliary vectors are loaded only for elf formatted executables.
2592976Sgblack@eecs.umich.edu    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
2602976Sgblack@eecs.umich.edu    if(elfObject)
2612976Sgblack@eecs.umich.edu    {
2622976Sgblack@eecs.umich.edu        //Bits which describe the system hardware capabilities
2632976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_HWCAP, hwcap));
2642976Sgblack@eecs.umich.edu        //The system page size
2652976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_PAGESZ, SparcISA::VMPageSize));
2662976Sgblack@eecs.umich.edu        //Defined to be 100 in the kernel source.
2672976Sgblack@eecs.umich.edu        //Frequency at which times() increments
2682976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_CLKTCK, 100));
2692976Sgblack@eecs.umich.edu        // For statically linked executables, this is the virtual address of the
2702976Sgblack@eecs.umich.edu        // program header tables if they appear in the executable image
2712976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_PHDR, elfObject->programHeaderTable()));
2722976Sgblack@eecs.umich.edu        // This is the size of a program header entry from the elf file.
2732976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_PHENT, elfObject->programHeaderSize()));
2742976Sgblack@eecs.umich.edu        // This is the number of program headers from the original elf file.
2752976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_PHNUM, elfObject->programHeaderCount()));
2762976Sgblack@eecs.umich.edu        //This is the address of the elf "interpreter", It should be set
2772976Sgblack@eecs.umich.edu        //to 0 for regular executables. It should be something else
2782976Sgblack@eecs.umich.edu        //(not sure what) for dynamic libraries.
2792976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_BASE, 0));
2802976Sgblack@eecs.umich.edu        //This is hardwired to 0 in the elf loading code in the kernel
2812976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_FLAGS, 0));
2822976Sgblack@eecs.umich.edu        //The entry point to the program
2832976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_ENTRY, objFile->entryPoint()));
2842976Sgblack@eecs.umich.edu        //Different user and group IDs
2853114Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_UID, uid()));
2863114Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_EUID, euid()));
2873114Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_GID, gid()));
2883114Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_EGID, egid()));
2892976Sgblack@eecs.umich.edu        //Whether to enable "secure mode" in the executable
2902976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_SECURE, 0));
2912976Sgblack@eecs.umich.edu    }
2922585SN/A
2932561SN/A    //Figure out how big the initial stack needs to be
2942561SN/A
2953044Sgblack@eecs.umich.edu    // The unaccounted for 0 at the top of the stack
2963044Sgblack@eecs.umich.edu    int mysterious_size = intSize;
2973044Sgblack@eecs.umich.edu
2983044Sgblack@eecs.umich.edu    //This is the name of the file which is present on the initial stack
2993044Sgblack@eecs.umich.edu    //It's purpose is to let the user space linker examine the original file.
3003044Sgblack@eecs.umich.edu    int file_name_size = filename.size() + 1;
3013044Sgblack@eecs.umich.edu
3022561SN/A    int env_data_size = 0;
3032561SN/A    for (int i = 0; i < envp.size(); ++i) {
3042561SN/A        env_data_size += envp[i].size() + 1;
3052561SN/A    }
3062561SN/A    int arg_data_size = 0;
3072561SN/A    for (int i = 0; i < argv.size(); ++i) {
3082561SN/A        arg_data_size += argv[i].size() + 1;
3092561SN/A    }
3102561SN/A
3113044Sgblack@eecs.umich.edu    //The info_block needs to be padded so it's size is a multiple of the
3123044Sgblack@eecs.umich.edu    //alignment mask. Also, it appears that there needs to be at least some
3133044Sgblack@eecs.umich.edu    //padding, so if the size is already a multiple, we need to increase it
3143044Sgblack@eecs.umich.edu    //anyway.
3153044Sgblack@eecs.umich.edu    int info_block_size =
3163044Sgblack@eecs.umich.edu        (file_name_size +
3173044Sgblack@eecs.umich.edu        env_data_size +
3183044Sgblack@eecs.umich.edu        arg_data_size +
3193044Sgblack@eecs.umich.edu        intSize) & alignmentMask;
3203044Sgblack@eecs.umich.edu
3213044Sgblack@eecs.umich.edu    int info_block_padding =
3223044Sgblack@eecs.umich.edu        info_block_size -
3233044Sgblack@eecs.umich.edu        file_name_size -
3243044Sgblack@eecs.umich.edu        env_data_size -
3253044Sgblack@eecs.umich.edu        arg_data_size;
3263044Sgblack@eecs.umich.edu
3273044Sgblack@eecs.umich.edu    //Each auxilliary vector is two 8 byte words
3282561SN/A    int aux_array_size = intSize * 2 * (auxv.size() + 1);
3292561SN/A
3303044Sgblack@eecs.umich.edu    int envp_array_size = intSize * (envp.size() + 1);
3312561SN/A    int argv_array_size = intSize * (argv.size() + 1);
3322561SN/A
3332561SN/A    int argc_size = intSize;
3342561SN/A    int window_save_size = intSize * 16;
3352561SN/A
3362561SN/A    int space_needed =
3373044Sgblack@eecs.umich.edu        mysterious_size +
3382561SN/A        info_block_size +
3392561SN/A        aux_array_size +
3402561SN/A        envp_array_size +
3412561SN/A        argv_array_size +
3422561SN/A        argc_size +
3432561SN/A        window_save_size;
3442561SN/A
3452561SN/A    stack_min = stack_base - space_needed;
3462561SN/A    stack_min &= alignmentMask;
3472561SN/A    stack_size = stack_base - stack_min;
3482561SN/A
3492561SN/A    // map memory
3502561SN/A    pTable->allocate(roundDown(stack_min, pageSize),
3512561SN/A                     roundUp(stack_size, pageSize));
3522561SN/A
3532561SN/A    // map out initial stack contents
3543044Sgblack@eecs.umich.edu    Addr mysterious_base = stack_base - mysterious_size;
3553044Sgblack@eecs.umich.edu    Addr file_name_base = mysterious_base - file_name_size;
3563044Sgblack@eecs.umich.edu    Addr env_data_base = file_name_base - env_data_size;
3572561SN/A    Addr arg_data_base = env_data_base - arg_data_size;
3583044Sgblack@eecs.umich.edu    Addr auxv_array_base = arg_data_base - aux_array_size - info_block_padding;
3592585SN/A    Addr envp_array_base = auxv_array_base - envp_array_size;
3602561SN/A    Addr argv_array_base = envp_array_base - argv_array_size;
3612561SN/A    Addr argc_base = argv_array_base - argc_size;
3623039Sstever@eecs.umich.edu#ifndef NDEBUG
3633039Sstever@eecs.umich.edu    // only used in DPRINTF
3642561SN/A    Addr window_save_base = argc_base - window_save_size;
3653039Sstever@eecs.umich.edu#endif
3662561SN/A
3672561SN/A    DPRINTF(Sparc, "The addresses of items on the initial stack:\n");
3683044Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - file name\n", file_name_base);
3692561SN/A    DPRINTF(Sparc, "0x%x - env data\n", env_data_base);
3702561SN/A    DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base);
3712585SN/A    DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base);
3722585SN/A    DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base);
3732585SN/A    DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base);
3742561SN/A    DPRINTF(Sparc, "0x%x - argc \n", argc_base);
3752561SN/A    DPRINTF(Sparc, "0x%x - window save\n", window_save_base);
3762561SN/A    DPRINTF(Sparc, "0x%x - stack min\n", stack_min);
3772561SN/A
3782561SN/A    // write contents to stack
3793044Sgblack@eecs.umich.edu
3803044Sgblack@eecs.umich.edu    // figure out argc
3812561SN/A    uint64_t argc = argv.size();
3822585SN/A    uint64_t guestArgc = TheISA::htog(argc);
3832561SN/A
3843044Sgblack@eecs.umich.edu    //Write out the mysterious 0
3853044Sgblack@eecs.umich.edu    uint64_t mysterious_zero = 0;
3863044Sgblack@eecs.umich.edu    initVirtMem->writeBlob(mysterious_base,
3873044Sgblack@eecs.umich.edu            (uint8_t*)&mysterious_zero, mysterious_size);
3883044Sgblack@eecs.umich.edu
3893044Sgblack@eecs.umich.edu    //Write the file name
3903044Sgblack@eecs.umich.edu    initVirtMem->writeString(file_name_base, filename.c_str());
3913044Sgblack@eecs.umich.edu
3922585SN/A    //Copy the aux stuff
3932585SN/A    for(int x = 0; x < auxv.size(); x++)
3942585SN/A    {
3952585SN/A        initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
3962585SN/A                (uint8_t*)&(auxv[x].a_type), intSize);
3972585SN/A        initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
3982585SN/A                (uint8_t*)&(auxv[x].a_val), intSize);
3992585SN/A    }
4002585SN/A    //Write out the terminating zeroed auxilliary vector
4012561SN/A    const uint64_t zero = 0;
4022585SN/A    initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
4032585SN/A            (uint8_t*)&zero, 2 * intSize);
4042561SN/A
4052561SN/A    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
4062561SN/A    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
4072561SN/A
4082585SN/A    initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
4092561SN/A
4103415Sgblack@eecs.umich.edu    //Stuff the trap handlers into the processes address space.
4113415Sgblack@eecs.umich.edu    //Since the stack grows down and is the highest area in the processes
4123415Sgblack@eecs.umich.edu    //address space, we can put stuff above it and stay out of the way.
4133415Sgblack@eecs.umich.edu    int fillSize = sizeof(MachInst) * numFillInsts;
4143415Sgblack@eecs.umich.edu    int spillSize = sizeof(MachInst) * numSpillInsts;
4153415Sgblack@eecs.umich.edu    fillStart = stack_base;
4163415Sgblack@eecs.umich.edu    spillStart = fillStart + fillSize;
4173415Sgblack@eecs.umich.edu    initVirtMem->writeBlob(fillStart, (uint8_t*)fillHandler, fillSize);
4183415Sgblack@eecs.umich.edu    initVirtMem->writeBlob(spillStart, (uint8_t*)spillHandler, spillSize);
4193415Sgblack@eecs.umich.edu
4203415Sgblack@eecs.umich.edu    //Set up the thread context to start running the process
4212680Sktlim@umich.edu    threadContexts[0]->setIntReg(ArgumentReg0, argc);
4222680Sktlim@umich.edu    threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base);
4232680Sktlim@umich.edu    threadContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias);
4242561SN/A
4252561SN/A    Addr prog_entry = objFile->entryPoint();
4262680Sktlim@umich.edu    threadContexts[0]->setPC(prog_entry);
4272680Sktlim@umich.edu    threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
4282680Sktlim@umich.edu    threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
4292561SN/A
4302561SN/A//    num_processes++;
4312561SN/A}
432