process.cc revision 5128
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"
334111Sgblack@eecs.umich.edu#include "arch/sparc/handlers.hh"
342474SN/A#include "arch/sparc/isa_traits.hh"
352207SN/A#include "arch/sparc/process.hh"
363760Sgblack@eecs.umich.edu#include "arch/sparc/types.hh"
372454SN/A#include "base/loader/object_file.hh"
382976Sgblack@eecs.umich.edu#include "base/loader/elf_object.hh"
392454SN/A#include "base/misc.hh"
402680Sktlim@umich.edu#include "cpu/thread_context.hh"
412561SN/A#include "mem/page_table.hh"
424434Ssaidi@eecs.umich.edu#include "sim/process_impl.hh"
432561SN/A#include "mem/translating_port.hh"
442474SN/A#include "sim/system.hh"
452207SN/A
462458SN/Ausing namespace std;
472474SN/Ausing namespace SparcISA;
482458SN/A
492207SN/A
502474SN/ASparcLiveProcess::SparcLiveProcess(const std::string &nm, ObjectFile *objFile,
512474SN/A        System *_system, int stdin_fd, int stdout_fd, int stderr_fd,
523114Sgblack@eecs.umich.edu        std::vector<std::string> &argv, std::vector<std::string> &envp,
533669Sbinkertn@umich.edu        const std::string &cwd,
543114Sgblack@eecs.umich.edu        uint64_t _uid, uint64_t _euid, uint64_t _gid, uint64_t _egid,
553114Sgblack@eecs.umich.edu        uint64_t _pid, uint64_t _ppid)
562474SN/A    : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd,
573669Sbinkertn@umich.edu        argv, envp, cwd, _uid, _euid, _gid, _egid, _pid, _ppid)
582474SN/A{
592474SN/A
602474SN/A    // XXX all the below need to be updated for SPARC - Ali
612474SN/A    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
622474SN/A    brk_point = roundUp(brk_point, VMPageSize);
632474SN/A
642474SN/A    // Set pointer for next thread stack.  Reserve 8M for main stack.
652474SN/A    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
663415Sgblack@eecs.umich.edu
673415Sgblack@eecs.umich.edu    //Initialize these to 0s
683415Sgblack@eecs.umich.edu    fillStart = 0;
693415Sgblack@eecs.umich.edu    spillStart = 0;
702474SN/A}
712474SN/A
724111Sgblack@eecs.umich.eduvoid SparcLiveProcess::handleTrap(int trapNum, ThreadContext *tc)
734111Sgblack@eecs.umich.edu{
744111Sgblack@eecs.umich.edu    switch(trapNum)
754111Sgblack@eecs.umich.edu    {
765128Sgblack@eecs.umich.edu      case 0x01: //Software breakpoint
775128Sgblack@eecs.umich.edu        warn("Software breakpoint encountered at pc %#x.\n", tc->readPC());
785128Sgblack@eecs.umich.edu        break;
795128Sgblack@eecs.umich.edu      case 0x02: //Division by zero
805128Sgblack@eecs.umich.edu        warn("Software signaled a division by zero at pc %#x.\n",
815128Sgblack@eecs.umich.edu                tc->readPC());
825128Sgblack@eecs.umich.edu        break;
834111Sgblack@eecs.umich.edu      case 0x03: //Flush window trap
845128Sgblack@eecs.umich.edu        flushWindows(tc);
855128Sgblack@eecs.umich.edu        break;
865128Sgblack@eecs.umich.edu      case 0x04: //Clean windows
875128Sgblack@eecs.umich.edu        warn("Ignoring process request for clean register "
885128Sgblack@eecs.umich.edu                "windows at pc %#x.\n", tc->readPC());
895128Sgblack@eecs.umich.edu        break;
905128Sgblack@eecs.umich.edu      case 0x05: //Range check
915128Sgblack@eecs.umich.edu        warn("Software signaled a range check at pc %#x.\n",
925128Sgblack@eecs.umich.edu                tc->readPC());
935128Sgblack@eecs.umich.edu        break;
945128Sgblack@eecs.umich.edu      case 0x06: //Fix alignment
955128Sgblack@eecs.umich.edu        warn("Ignoring process request for os assisted unaligned accesses "
965128Sgblack@eecs.umich.edu                "at pc %#x.\n", tc->readPC());
975128Sgblack@eecs.umich.edu        break;
985128Sgblack@eecs.umich.edu      case 0x07: //Integer overflow
995128Sgblack@eecs.umich.edu        warn("Software signaled an integer overflow at pc %#x.\n",
1005128Sgblack@eecs.umich.edu                tc->readPC());
1015128Sgblack@eecs.umich.edu        break;
1025128Sgblack@eecs.umich.edu      case 0x32: //Get integer condition codes
1035128Sgblack@eecs.umich.edu        warn("Ignoring process request to get the integer condition codes "
1045128Sgblack@eecs.umich.edu                "at pc %#x.\n", tc->readPC());
1055128Sgblack@eecs.umich.edu        break;
1065128Sgblack@eecs.umich.edu      case 0x33: //Set integer condition codes
1075128Sgblack@eecs.umich.edu        warn("Ignoring process request to set the integer condition codes "
1085128Sgblack@eecs.umich.edu                "at pc %#x.\n", tc->readPC());
1094111Sgblack@eecs.umich.edu        break;
1104111Sgblack@eecs.umich.edu      default:
1114111Sgblack@eecs.umich.edu        panic("Unimplemented trap to operating system: trap number %#x.\n", trapNum);
1124111Sgblack@eecs.umich.edu    }
1134111Sgblack@eecs.umich.edu}
1144111Sgblack@eecs.umich.edu
1152474SN/Avoid
1164111Sgblack@eecs.umich.eduSparc32LiveProcess::startup()
1174111Sgblack@eecs.umich.edu{
1184111Sgblack@eecs.umich.edu    argsInit(32 / 8, VMPageSize);
1194111Sgblack@eecs.umich.edu
1204111Sgblack@eecs.umich.edu    //From the SPARC ABI
1214111Sgblack@eecs.umich.edu
1224648Sgblack@eecs.umich.edu    //The process runs in user mode with 32 bit addresses
1234648Sgblack@eecs.umich.edu    threadContexts[0]->setMiscReg(MISCREG_PSTATE, 0x0a);
1244111Sgblack@eecs.umich.edu
1254111Sgblack@eecs.umich.edu    //Setup default FP state
1264172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_FSR, 0);
1274111Sgblack@eecs.umich.edu
1284172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_TICK, 0);
1294111Sgblack@eecs.umich.edu    //
1304111Sgblack@eecs.umich.edu    /*
1314111Sgblack@eecs.umich.edu     * Register window management registers
1324111Sgblack@eecs.umich.edu     */
1334111Sgblack@eecs.umich.edu
1344111Sgblack@eecs.umich.edu    //No windows contain info from other programs
1354172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_OTHERWIN, 0);
1364111Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 6, 0);
1374111Sgblack@eecs.umich.edu    //There are no windows to pop
1384172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_CANRESTORE, 0);
1394111Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 4, 0);
1404111Sgblack@eecs.umich.edu    //All windows are available to save into
1414172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_CANSAVE, NWindows - 2);
1424111Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 3, NWindows - 2);
1434111Sgblack@eecs.umich.edu    //All windows are "clean"
1444172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_CLEANWIN, NWindows);
1454111Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 5, NWindows);
1464111Sgblack@eecs.umich.edu    //Start with register window 0
1474172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_CWP, 0);
1484111Sgblack@eecs.umich.edu    //Always use spill and fill traps 0
1494172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_WSTATE, 0);
1504111Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 7, 0);
1514111Sgblack@eecs.umich.edu    //Set the trap level to 0
1524172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_TL, 0);
1534111Sgblack@eecs.umich.edu    //Set the ASI register to something fixed
1544172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY);
1554997Sgblack@eecs.umich.edu
1564997Sgblack@eecs.umich.edu    /*
1574997Sgblack@eecs.umich.edu     * T1 specific registers
1584997Sgblack@eecs.umich.edu     */
1594997Sgblack@eecs.umich.edu    //Turn on the icache, dcache, dtb translation, and itb translation.
1604997Sgblack@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_MMU_LSU_CTRL, 15);
1614111Sgblack@eecs.umich.edu}
1624111Sgblack@eecs.umich.edu
1634111Sgblack@eecs.umich.eduvoid
1644111Sgblack@eecs.umich.eduSparc64LiveProcess::startup()
1652474SN/A{
1663760Sgblack@eecs.umich.edu    argsInit(sizeof(IntReg), VMPageSize);
1672561SN/A
1682561SN/A    //From the SPARC ABI
1692561SN/A
1702561SN/A    //The process runs in user mode
1714172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscReg(MISCREG_PSTATE, 0x02);
1722561SN/A
1732646Ssaidi@eecs.umich.edu    //Setup default FP state
1744172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_FSR, 0);
1752646Ssaidi@eecs.umich.edu
1764172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_TICK, 0);
1774997Sgblack@eecs.umich.edu
1782561SN/A    /*
1792561SN/A     * Register window management registers
1802561SN/A     */
1812561SN/A
1822561SN/A    //No windows contain info from other programs
1834172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_OTHERWIN, 0);
1843761Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 6, 0);
1852561SN/A    //There are no windows to pop
1864172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_CANRESTORE, 0);
1873761Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 4, 0);
1882561SN/A    //All windows are available to save into
1894172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_CANSAVE, NWindows - 2);
1903761Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 3, NWindows - 2);
1912561SN/A    //All windows are "clean"
1924172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_CLEANWIN, NWindows);
1933761Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 5, NWindows);
1942561SN/A    //Start with register window 0
1954172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_CWP, 0);
1963415Sgblack@eecs.umich.edu    //Always use spill and fill traps 0
1974172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_WSTATE, 0);
1983761Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 7, 0);
1993415Sgblack@eecs.umich.edu    //Set the trap level to 0
2004172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_TL, 0);
2013589Sgblack@eecs.umich.edu    //Set the ASI register to something fixed
2024172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY);
2034997Sgblack@eecs.umich.edu
2044997Sgblack@eecs.umich.edu    /*
2054997Sgblack@eecs.umich.edu     * T1 specific registers
2064997Sgblack@eecs.umich.edu     */
2074997Sgblack@eecs.umich.edu    //Turn on the icache, dcache, dtb translation, and itb translation.
2084997Sgblack@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_MMU_LSU_CTRL, 15);
2092474SN/A}
2102474SN/A
2114111Sgblack@eecs.umich.eduM5_32_auxv_t::M5_32_auxv_t(int32_t type, int32_t val)
2122585SN/A{
2134111Sgblack@eecs.umich.edu    a_type = TheISA::htog(type);
2144111Sgblack@eecs.umich.edu    a_val = TheISA::htog(val);
2152585SN/A}
2162585SN/A
2174111Sgblack@eecs.umich.eduM5_64_auxv_t::M5_64_auxv_t(int64_t type, int64_t val)
2183415Sgblack@eecs.umich.edu{
2194111Sgblack@eecs.umich.edu    a_type = TheISA::htog(type);
2204111Sgblack@eecs.umich.edu    a_val = TheISA::htog(val);
2214111Sgblack@eecs.umich.edu}
2223415Sgblack@eecs.umich.edu
2232561SN/Avoid
2244111Sgblack@eecs.umich.eduSparc64LiveProcess::argsInit(int intSize, int pageSize)
2252561SN/A{
2264111Sgblack@eecs.umich.edu    typedef M5_64_auxv_t auxv_t;
2272561SN/A    Process::startup();
2282474SN/A
2293044Sgblack@eecs.umich.edu    string filename;
2303044Sgblack@eecs.umich.edu    if(argv.size() < 1)
2313044Sgblack@eecs.umich.edu        filename = "";
2323044Sgblack@eecs.umich.edu    else
2333044Sgblack@eecs.umich.edu        filename = argv[0];
2343044Sgblack@eecs.umich.edu
2352561SN/A    Addr alignmentMask = ~(intSize - 1);
2362561SN/A
2372561SN/A    // load object file into target memory
2382561SN/A    objFile->loadSections(initVirtMem);
2392561SN/A
2402585SN/A    enum hardwareCaps
2412585SN/A    {
2422585SN/A        M5_HWCAP_SPARC_FLUSH = 1,
2432585SN/A        M5_HWCAP_SPARC_STBAR = 2,
2442585SN/A        M5_HWCAP_SPARC_SWAP = 4,
2452585SN/A        M5_HWCAP_SPARC_MULDIV = 8,
2462585SN/A        M5_HWCAP_SPARC_V9 = 16,
2472585SN/A        //This one should technically only be set
2482585SN/A        //if there is a cheetah or cheetah_plus tlb,
2492585SN/A        //but we'll use it all the time
2502585SN/A        M5_HWCAP_SPARC_ULTRA3 = 32
2512585SN/A    };
2522585SN/A
2532585SN/A    const int64_t hwcap =
2542585SN/A        M5_HWCAP_SPARC_FLUSH |
2552585SN/A        M5_HWCAP_SPARC_STBAR |
2562585SN/A        M5_HWCAP_SPARC_SWAP |
2572585SN/A        M5_HWCAP_SPARC_MULDIV |
2582585SN/A        M5_HWCAP_SPARC_V9 |
2592585SN/A        M5_HWCAP_SPARC_ULTRA3;
2602585SN/A
2612976Sgblack@eecs.umich.edu
2622976Sgblack@eecs.umich.edu    //Setup the auxilliary vectors. These will already have endian conversion.
2632976Sgblack@eecs.umich.edu    //Auxilliary vectors are loaded only for elf formatted executables.
2642976Sgblack@eecs.umich.edu    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
2652976Sgblack@eecs.umich.edu    if(elfObject)
2662976Sgblack@eecs.umich.edu    {
2672976Sgblack@eecs.umich.edu        //Bits which describe the system hardware capabilities
2684793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_HWCAP, hwcap));
2692976Sgblack@eecs.umich.edu        //The system page size
2704793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PAGESZ, SparcISA::VMPageSize));
2712976Sgblack@eecs.umich.edu        //Defined to be 100 in the kernel source.
2722976Sgblack@eecs.umich.edu        //Frequency at which times() increments
2734793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_CLKTCK, 100));
2742976Sgblack@eecs.umich.edu        // For statically linked executables, this is the virtual address of the
2752976Sgblack@eecs.umich.edu        // program header tables if they appear in the executable image
2764793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
2772976Sgblack@eecs.umich.edu        // This is the size of a program header entry from the elf file.
2784793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
2792976Sgblack@eecs.umich.edu        // This is the number of program headers from the original elf file.
2804793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
2812976Sgblack@eecs.umich.edu        //This is the address of the elf "interpreter", It should be set
2822976Sgblack@eecs.umich.edu        //to 0 for regular executables. It should be something else
2832976Sgblack@eecs.umich.edu        //(not sure what) for dynamic libraries.
2844793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_BASE, 0));
2852976Sgblack@eecs.umich.edu        //This is hardwired to 0 in the elf loading code in the kernel
2864793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
2872976Sgblack@eecs.umich.edu        //The entry point to the program
2884793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
2892976Sgblack@eecs.umich.edu        //Different user and group IDs
2904793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_UID, uid()));
2914793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_EUID, euid()));
2924793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_GID, gid()));
2934793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_EGID, egid()));
2942976Sgblack@eecs.umich.edu        //Whether to enable "secure mode" in the executable
2954793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_SECURE, 0));
2962976Sgblack@eecs.umich.edu    }
2972585SN/A
2982561SN/A    //Figure out how big the initial stack needs to be
2992561SN/A
3003044Sgblack@eecs.umich.edu    // The unaccounted for 0 at the top of the stack
3013044Sgblack@eecs.umich.edu    int mysterious_size = intSize;
3023044Sgblack@eecs.umich.edu
3033044Sgblack@eecs.umich.edu    //This is the name of the file which is present on the initial stack
3043044Sgblack@eecs.umich.edu    //It's purpose is to let the user space linker examine the original file.
3053044Sgblack@eecs.umich.edu    int file_name_size = filename.size() + 1;
3063044Sgblack@eecs.umich.edu
3072561SN/A    int env_data_size = 0;
3082561SN/A    for (int i = 0; i < envp.size(); ++i) {
3092561SN/A        env_data_size += envp[i].size() + 1;
3102561SN/A    }
3112561SN/A    int arg_data_size = 0;
3122561SN/A    for (int i = 0; i < argv.size(); ++i) {
3132561SN/A        arg_data_size += argv[i].size() + 1;
3142561SN/A    }
3152561SN/A
3163044Sgblack@eecs.umich.edu    //The info_block needs to be padded so it's size is a multiple of the
3173044Sgblack@eecs.umich.edu    //alignment mask. Also, it appears that there needs to be at least some
3183044Sgblack@eecs.umich.edu    //padding, so if the size is already a multiple, we need to increase it
3193044Sgblack@eecs.umich.edu    //anyway.
3203044Sgblack@eecs.umich.edu    int info_block_size =
3213044Sgblack@eecs.umich.edu        (file_name_size +
3223044Sgblack@eecs.umich.edu        env_data_size +
3233044Sgblack@eecs.umich.edu        arg_data_size +
3243044Sgblack@eecs.umich.edu        intSize) & alignmentMask;
3253044Sgblack@eecs.umich.edu
3263044Sgblack@eecs.umich.edu    int info_block_padding =
3273044Sgblack@eecs.umich.edu        info_block_size -
3283044Sgblack@eecs.umich.edu        file_name_size -
3293044Sgblack@eecs.umich.edu        env_data_size -
3303044Sgblack@eecs.umich.edu        arg_data_size;
3313044Sgblack@eecs.umich.edu
3323044Sgblack@eecs.umich.edu    //Each auxilliary vector is two 8 byte words
3332561SN/A    int aux_array_size = intSize * 2 * (auxv.size() + 1);
3342561SN/A
3353044Sgblack@eecs.umich.edu    int envp_array_size = intSize * (envp.size() + 1);
3362561SN/A    int argv_array_size = intSize * (argv.size() + 1);
3372561SN/A
3382561SN/A    int argc_size = intSize;
3392561SN/A    int window_save_size = intSize * 16;
3402561SN/A
3412561SN/A    int space_needed =
3423044Sgblack@eecs.umich.edu        mysterious_size +
3432561SN/A        info_block_size +
3442561SN/A        aux_array_size +
3452561SN/A        envp_array_size +
3462561SN/A        argv_array_size +
3472561SN/A        argc_size +
3482561SN/A        window_save_size;
3492561SN/A
3502561SN/A    stack_min = stack_base - space_needed;
3512561SN/A    stack_min &= alignmentMask;
3522561SN/A    stack_size = stack_base - stack_min;
3532561SN/A
3542561SN/A    // map memory
3552561SN/A    pTable->allocate(roundDown(stack_min, pageSize),
3562561SN/A                     roundUp(stack_size, pageSize));
3572561SN/A
3582561SN/A    // map out initial stack contents
3593044Sgblack@eecs.umich.edu    Addr mysterious_base = stack_base - mysterious_size;
3603044Sgblack@eecs.umich.edu    Addr file_name_base = mysterious_base - file_name_size;
3613044Sgblack@eecs.umich.edu    Addr env_data_base = file_name_base - env_data_size;
3622561SN/A    Addr arg_data_base = env_data_base - arg_data_size;
3633044Sgblack@eecs.umich.edu    Addr auxv_array_base = arg_data_base - aux_array_size - info_block_padding;
3642585SN/A    Addr envp_array_base = auxv_array_base - envp_array_size;
3652561SN/A    Addr argv_array_base = envp_array_base - argv_array_size;
3662561SN/A    Addr argc_base = argv_array_base - argc_size;
3673039Sstever@eecs.umich.edu#ifndef NDEBUG
3683039Sstever@eecs.umich.edu    // only used in DPRINTF
3692561SN/A    Addr window_save_base = argc_base - window_save_size;
3703039Sstever@eecs.umich.edu#endif
3712561SN/A
3722561SN/A    DPRINTF(Sparc, "The addresses of items on the initial stack:\n");
3733044Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - file name\n", file_name_base);
3742561SN/A    DPRINTF(Sparc, "0x%x - env data\n", env_data_base);
3752561SN/A    DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base);
3762585SN/A    DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base);
3772585SN/A    DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base);
3782585SN/A    DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base);
3792561SN/A    DPRINTF(Sparc, "0x%x - argc \n", argc_base);
3802561SN/A    DPRINTF(Sparc, "0x%x - window save\n", window_save_base);
3812561SN/A    DPRINTF(Sparc, "0x%x - stack min\n", stack_min);
3822561SN/A
3832561SN/A    // write contents to stack
3843044Sgblack@eecs.umich.edu
3853044Sgblack@eecs.umich.edu    // figure out argc
3862561SN/A    uint64_t argc = argv.size();
3872585SN/A    uint64_t guestArgc = TheISA::htog(argc);
3882561SN/A
3893044Sgblack@eecs.umich.edu    //Write out the mysterious 0
3903044Sgblack@eecs.umich.edu    uint64_t mysterious_zero = 0;
3913044Sgblack@eecs.umich.edu    initVirtMem->writeBlob(mysterious_base,
3923044Sgblack@eecs.umich.edu            (uint8_t*)&mysterious_zero, mysterious_size);
3933044Sgblack@eecs.umich.edu
3943044Sgblack@eecs.umich.edu    //Write the file name
3953044Sgblack@eecs.umich.edu    initVirtMem->writeString(file_name_base, filename.c_str());
3963044Sgblack@eecs.umich.edu
3972585SN/A    //Copy the aux stuff
3982585SN/A    for(int x = 0; x < auxv.size(); x++)
3992585SN/A    {
4002585SN/A        initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
4012585SN/A                (uint8_t*)&(auxv[x].a_type), intSize);
4022585SN/A        initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
4032585SN/A                (uint8_t*)&(auxv[x].a_val), intSize);
4042585SN/A    }
4052585SN/A    //Write out the terminating zeroed auxilliary vector
4062561SN/A    const uint64_t zero = 0;
4072585SN/A    initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
4082585SN/A            (uint8_t*)&zero, 2 * intSize);
4092561SN/A
4102561SN/A    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
4112561SN/A    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
4122561SN/A
4132585SN/A    initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
4142561SN/A
4153415Sgblack@eecs.umich.edu    //Stuff the trap handlers into the processes address space.
4163415Sgblack@eecs.umich.edu    //Since the stack grows down and is the highest area in the processes
4173415Sgblack@eecs.umich.edu    //address space, we can put stuff above it and stay out of the way.
4183415Sgblack@eecs.umich.edu    int fillSize = sizeof(MachInst) * numFillInsts;
4193415Sgblack@eecs.umich.edu    int spillSize = sizeof(MachInst) * numSpillInsts;
4203415Sgblack@eecs.umich.edu    fillStart = stack_base;
4213415Sgblack@eecs.umich.edu    spillStart = fillStart + fillSize;
4224111Sgblack@eecs.umich.edu    initVirtMem->writeBlob(fillStart, (uint8_t*)fillHandler64, fillSize);
4234111Sgblack@eecs.umich.edu    initVirtMem->writeBlob(spillStart, (uint8_t*)spillHandler64, spillSize);
4243415Sgblack@eecs.umich.edu
4253415Sgblack@eecs.umich.edu    //Set up the thread context to start running the process
4264772Sgblack@eecs.umich.edu    assert(NumArgumentRegs >= 2);
4274772Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(ArgumentReg[0], argc);
4284772Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(ArgumentReg[1], argv_array_base);
4292680Sktlim@umich.edu    threadContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias);
4302561SN/A
4312561SN/A    Addr prog_entry = objFile->entryPoint();
4322680Sktlim@umich.edu    threadContexts[0]->setPC(prog_entry);
4332680Sktlim@umich.edu    threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
4342680Sktlim@umich.edu    threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
4352561SN/A
4363959Sgblack@eecs.umich.edu    //Align the "stack_min" to a page boundary.
4373959Sgblack@eecs.umich.edu    stack_min = roundDown(stack_min, pageSize);
4383959Sgblack@eecs.umich.edu
4392561SN/A//    num_processes++;
4402561SN/A}
4414111Sgblack@eecs.umich.edu
4424111Sgblack@eecs.umich.eduvoid
4434111Sgblack@eecs.umich.eduSparc32LiveProcess::argsInit(int intSize, int pageSize)
4444111Sgblack@eecs.umich.edu{
4454111Sgblack@eecs.umich.edu    typedef M5_32_auxv_t auxv_t;
4464111Sgblack@eecs.umich.edu    Process::startup();
4474111Sgblack@eecs.umich.edu
4484111Sgblack@eecs.umich.edu    string filename;
4494111Sgblack@eecs.umich.edu    if(argv.size() < 1)
4504111Sgblack@eecs.umich.edu        filename = "";
4514111Sgblack@eecs.umich.edu    else
4524111Sgblack@eecs.umich.edu        filename = argv[0];
4534111Sgblack@eecs.umich.edu
4544117Sgblack@eecs.umich.edu    //Even though this is a 32 bit process, the ABI says we still need to
4554117Sgblack@eecs.umich.edu    //maintain double word alignment of the stack pointer.
4564117Sgblack@eecs.umich.edu    Addr alignmentMask = ~(8 - 1);
4574111Sgblack@eecs.umich.edu
4584111Sgblack@eecs.umich.edu    // load object file into target memory
4594111Sgblack@eecs.umich.edu    objFile->loadSections(initVirtMem);
4604111Sgblack@eecs.umich.edu
4614111Sgblack@eecs.umich.edu    //These are the auxilliary vector types
4624111Sgblack@eecs.umich.edu    enum auxTypes
4634111Sgblack@eecs.umich.edu    {
4644111Sgblack@eecs.umich.edu        SPARC_AT_HWCAP = 16,
4654111Sgblack@eecs.umich.edu        SPARC_AT_PAGESZ = 6,
4664111Sgblack@eecs.umich.edu        SPARC_AT_CLKTCK = 17,
4674111Sgblack@eecs.umich.edu        SPARC_AT_PHDR = 3,
4684111Sgblack@eecs.umich.edu        SPARC_AT_PHENT = 4,
4694111Sgblack@eecs.umich.edu        SPARC_AT_PHNUM = 5,
4704111Sgblack@eecs.umich.edu        SPARC_AT_BASE = 7,
4714111Sgblack@eecs.umich.edu        SPARC_AT_FLAGS = 8,
4724111Sgblack@eecs.umich.edu        SPARC_AT_ENTRY = 9,
4734111Sgblack@eecs.umich.edu        SPARC_AT_UID = 11,
4744111Sgblack@eecs.umich.edu        SPARC_AT_EUID = 12,
4754111Sgblack@eecs.umich.edu        SPARC_AT_GID = 13,
4764111Sgblack@eecs.umich.edu        SPARC_AT_EGID = 14,
4774111Sgblack@eecs.umich.edu        SPARC_AT_SECURE = 23
4784111Sgblack@eecs.umich.edu    };
4794111Sgblack@eecs.umich.edu
4804111Sgblack@eecs.umich.edu    enum hardwareCaps
4814111Sgblack@eecs.umich.edu    {
4824111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_FLUSH = 1,
4834111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_STBAR = 2,
4844111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_SWAP = 4,
4854111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_MULDIV = 8,
4864111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_V9 = 16,
4874111Sgblack@eecs.umich.edu        //This one should technically only be set
4884111Sgblack@eecs.umich.edu        //if there is a cheetah or cheetah_plus tlb,
4894111Sgblack@eecs.umich.edu        //but we'll use it all the time
4904111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_ULTRA3 = 32
4914111Sgblack@eecs.umich.edu    };
4924111Sgblack@eecs.umich.edu
4934111Sgblack@eecs.umich.edu    const int64_t hwcap =
4944111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_FLUSH |
4954111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_STBAR |
4964111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_SWAP |
4974111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_MULDIV |
4984111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_V9 |
4994111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_ULTRA3;
5004111Sgblack@eecs.umich.edu
5014111Sgblack@eecs.umich.edu
5024111Sgblack@eecs.umich.edu    //Setup the auxilliary vectors. These will already have endian conversion.
5034111Sgblack@eecs.umich.edu    //Auxilliary vectors are loaded only for elf formatted executables.
5044111Sgblack@eecs.umich.edu    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
5054111Sgblack@eecs.umich.edu    if(elfObject)
5064111Sgblack@eecs.umich.edu    {
5074111Sgblack@eecs.umich.edu        //Bits which describe the system hardware capabilities
5084111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_HWCAP, hwcap));
5094111Sgblack@eecs.umich.edu        //The system page size
5104111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_PAGESZ, SparcISA::VMPageSize));
5114111Sgblack@eecs.umich.edu        //Defined to be 100 in the kernel source.
5124111Sgblack@eecs.umich.edu        //Frequency at which times() increments
5134111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_CLKTCK, 100));
5144111Sgblack@eecs.umich.edu        // For statically linked executables, this is the virtual address of the
5154111Sgblack@eecs.umich.edu        // program header tables if they appear in the executable image
5164111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_PHDR, elfObject->programHeaderTable()));
5174111Sgblack@eecs.umich.edu        // This is the size of a program header entry from the elf file.
5184111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_PHENT, elfObject->programHeaderSize()));
5194111Sgblack@eecs.umich.edu        // This is the number of program headers from the original elf file.
5204111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_PHNUM, elfObject->programHeaderCount()));
5214111Sgblack@eecs.umich.edu        //This is the address of the elf "interpreter", It should be set
5224111Sgblack@eecs.umich.edu        //to 0 for regular executables. It should be something else
5234111Sgblack@eecs.umich.edu        //(not sure what) for dynamic libraries.
5244111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_BASE, 0));
5254111Sgblack@eecs.umich.edu        //This is hardwired to 0 in the elf loading code in the kernel
5264111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_FLAGS, 0));
5274111Sgblack@eecs.umich.edu        //The entry point to the program
5284111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_ENTRY, objFile->entryPoint()));
5294111Sgblack@eecs.umich.edu        //Different user and group IDs
5304111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_UID, uid()));
5314111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_EUID, euid()));
5324111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_GID, gid()));
5334111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_EGID, egid()));
5344111Sgblack@eecs.umich.edu        //Whether to enable "secure mode" in the executable
5354111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_SECURE, 0));
5364111Sgblack@eecs.umich.edu    }
5374111Sgblack@eecs.umich.edu
5384111Sgblack@eecs.umich.edu    //Figure out how big the initial stack needs to be
5394111Sgblack@eecs.umich.edu
5404164Sgblack@eecs.umich.edu    // The unaccounted for 8 byte 0 at the top of the stack
5414164Sgblack@eecs.umich.edu    int mysterious_size = 8;
5424111Sgblack@eecs.umich.edu
5434111Sgblack@eecs.umich.edu    //This is the name of the file which is present on the initial stack
5444111Sgblack@eecs.umich.edu    //It's purpose is to let the user space linker examine the original file.
5454111Sgblack@eecs.umich.edu    int file_name_size = filename.size() + 1;
5464111Sgblack@eecs.umich.edu
5474111Sgblack@eecs.umich.edu    int env_data_size = 0;
5484111Sgblack@eecs.umich.edu    for (int i = 0; i < envp.size(); ++i) {
5494111Sgblack@eecs.umich.edu        env_data_size += envp[i].size() + 1;
5504111Sgblack@eecs.umich.edu    }
5514111Sgblack@eecs.umich.edu    int arg_data_size = 0;
5524111Sgblack@eecs.umich.edu    for (int i = 0; i < argv.size(); ++i) {
5534111Sgblack@eecs.umich.edu        arg_data_size += argv[i].size() + 1;
5544111Sgblack@eecs.umich.edu    }
5554111Sgblack@eecs.umich.edu
5564164Sgblack@eecs.umich.edu    //The info_block - This seems to need an pad for some reason.
5574111Sgblack@eecs.umich.edu    int info_block_size =
5584164Sgblack@eecs.umich.edu        (mysterious_size +
5594164Sgblack@eecs.umich.edu        file_name_size +
5604111Sgblack@eecs.umich.edu        env_data_size +
5614164Sgblack@eecs.umich.edu        arg_data_size + intSize);
5624111Sgblack@eecs.umich.edu
5634164Sgblack@eecs.umich.edu    //Each auxilliary vector is two 4 byte words
5644111Sgblack@eecs.umich.edu    int aux_array_size = intSize * 2 * (auxv.size() + 1);
5654111Sgblack@eecs.umich.edu
5664111Sgblack@eecs.umich.edu    int envp_array_size = intSize * (envp.size() + 1);
5674111Sgblack@eecs.umich.edu    int argv_array_size = intSize * (argv.size() + 1);
5684111Sgblack@eecs.umich.edu
5694111Sgblack@eecs.umich.edu    int argc_size = intSize;
5704111Sgblack@eecs.umich.edu    int window_save_size = intSize * 16;
5714111Sgblack@eecs.umich.edu
5724111Sgblack@eecs.umich.edu    int space_needed =
5734164Sgblack@eecs.umich.edu        info_block_size +
5744111Sgblack@eecs.umich.edu        aux_array_size +
5754111Sgblack@eecs.umich.edu        envp_array_size +
5764111Sgblack@eecs.umich.edu        argv_array_size +
5774111Sgblack@eecs.umich.edu        argc_size +
5784111Sgblack@eecs.umich.edu        window_save_size;
5794111Sgblack@eecs.umich.edu
5804111Sgblack@eecs.umich.edu    stack_min = stack_base - space_needed;
5814111Sgblack@eecs.umich.edu    stack_min &= alignmentMask;
5824111Sgblack@eecs.umich.edu    stack_size = stack_base - stack_min;
5834111Sgblack@eecs.umich.edu
5844111Sgblack@eecs.umich.edu    // map memory
5854111Sgblack@eecs.umich.edu    pTable->allocate(roundDown(stack_min, pageSize),
5864111Sgblack@eecs.umich.edu                     roundUp(stack_size, pageSize));
5874111Sgblack@eecs.umich.edu
5884111Sgblack@eecs.umich.edu    // map out initial stack contents
5894117Sgblack@eecs.umich.edu    uint32_t window_save_base = stack_min;
5904117Sgblack@eecs.umich.edu    uint32_t argc_base = window_save_base + window_save_size;
5914117Sgblack@eecs.umich.edu    uint32_t argv_array_base = argc_base + argc_size;
5924117Sgblack@eecs.umich.edu    uint32_t envp_array_base = argv_array_base + argv_array_size;
5934117Sgblack@eecs.umich.edu    uint32_t auxv_array_base = envp_array_base + envp_array_size;
5944117Sgblack@eecs.umich.edu    //The info block is pushed up against the top of the stack, while
5954117Sgblack@eecs.umich.edu    //the rest of the initial stack frame is aligned to an 8 byte boudary.
5964164Sgblack@eecs.umich.edu    uint32_t arg_data_base = stack_base - info_block_size + intSize;
5974117Sgblack@eecs.umich.edu    uint32_t env_data_base = arg_data_base + arg_data_size;
5984117Sgblack@eecs.umich.edu    uint32_t file_name_base = env_data_base + env_data_size;
5994117Sgblack@eecs.umich.edu    uint32_t mysterious_base = file_name_base + file_name_size;
6004111Sgblack@eecs.umich.edu
6014111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "The addresses of items on the initial stack:\n");
6024111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - file name\n", file_name_base);
6034111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - env data\n", env_data_base);
6044111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base);
6054111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base);
6064111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base);
6074111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base);
6084111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - argc \n", argc_base);
6094111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - window save\n", window_save_base);
6104111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - stack min\n", stack_min);
6114111Sgblack@eecs.umich.edu
6124111Sgblack@eecs.umich.edu    // write contents to stack
6134111Sgblack@eecs.umich.edu
6144111Sgblack@eecs.umich.edu    // figure out argc
6154111Sgblack@eecs.umich.edu    uint32_t argc = argv.size();
6164111Sgblack@eecs.umich.edu    uint32_t guestArgc = TheISA::htog(argc);
6174111Sgblack@eecs.umich.edu
6184111Sgblack@eecs.umich.edu    //Write out the mysterious 0
6194111Sgblack@eecs.umich.edu    uint64_t mysterious_zero = 0;
6204111Sgblack@eecs.umich.edu    initVirtMem->writeBlob(mysterious_base,
6214111Sgblack@eecs.umich.edu            (uint8_t*)&mysterious_zero, mysterious_size);
6224111Sgblack@eecs.umich.edu
6234111Sgblack@eecs.umich.edu    //Write the file name
6244111Sgblack@eecs.umich.edu    initVirtMem->writeString(file_name_base, filename.c_str());
6254111Sgblack@eecs.umich.edu
6264111Sgblack@eecs.umich.edu    //Copy the aux stuff
6274111Sgblack@eecs.umich.edu    for(int x = 0; x < auxv.size(); x++)
6284111Sgblack@eecs.umich.edu    {
6294111Sgblack@eecs.umich.edu        initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
6304111Sgblack@eecs.umich.edu                (uint8_t*)&(auxv[x].a_type), intSize);
6314111Sgblack@eecs.umich.edu        initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
6324111Sgblack@eecs.umich.edu                (uint8_t*)&(auxv[x].a_val), intSize);
6334111Sgblack@eecs.umich.edu    }
6344111Sgblack@eecs.umich.edu    //Write out the terminating zeroed auxilliary vector
6354111Sgblack@eecs.umich.edu    const uint64_t zero = 0;
6364111Sgblack@eecs.umich.edu    initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
6374111Sgblack@eecs.umich.edu            (uint8_t*)&zero, 2 * intSize);
6384111Sgblack@eecs.umich.edu
6394117Sgblack@eecs.umich.edu    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
6404117Sgblack@eecs.umich.edu    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
6414111Sgblack@eecs.umich.edu
6424111Sgblack@eecs.umich.edu    initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
6434111Sgblack@eecs.umich.edu
6444111Sgblack@eecs.umich.edu    //Stuff the trap handlers into the processes address space.
6454111Sgblack@eecs.umich.edu    //Since the stack grows down and is the highest area in the processes
6464111Sgblack@eecs.umich.edu    //address space, we can put stuff above it and stay out of the way.
6474111Sgblack@eecs.umich.edu    int fillSize = sizeof(MachInst) * numFillInsts;
6484111Sgblack@eecs.umich.edu    int spillSize = sizeof(MachInst) * numSpillInsts;
6494111Sgblack@eecs.umich.edu    fillStart = stack_base;
6504111Sgblack@eecs.umich.edu    spillStart = fillStart + fillSize;
6514111Sgblack@eecs.umich.edu    initVirtMem->writeBlob(fillStart, (uint8_t*)fillHandler32, fillSize);
6524111Sgblack@eecs.umich.edu    initVirtMem->writeBlob(spillStart, (uint8_t*)spillHandler32, spillSize);
6534111Sgblack@eecs.umich.edu
6544111Sgblack@eecs.umich.edu    //Set up the thread context to start running the process
6554772Sgblack@eecs.umich.edu    //assert(NumArgumentRegs >= 2);
6564772Sgblack@eecs.umich.edu    //threadContexts[0]->setIntReg(ArgumentReg[0], argc);
6574772Sgblack@eecs.umich.edu    //threadContexts[0]->setIntReg(ArgumentReg[1], argv_array_base);
6584111Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(StackPointerReg, stack_min);
6594111Sgblack@eecs.umich.edu
6604117Sgblack@eecs.umich.edu    uint32_t prog_entry = objFile->entryPoint();
6614111Sgblack@eecs.umich.edu    threadContexts[0]->setPC(prog_entry);
6624111Sgblack@eecs.umich.edu    threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
6634111Sgblack@eecs.umich.edu    threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
6644111Sgblack@eecs.umich.edu
6654111Sgblack@eecs.umich.edu    //Align the "stack_min" to a page boundary.
6664111Sgblack@eecs.umich.edu    stack_min = roundDown(stack_min, pageSize);
6674111Sgblack@eecs.umich.edu
6684111Sgblack@eecs.umich.edu//    num_processes++;
6694111Sgblack@eecs.umich.edu}
6705128Sgblack@eecs.umich.edu
6715128Sgblack@eecs.umich.eduvoid Sparc32LiveProcess::flushWindows(ThreadContext *tc)
6725128Sgblack@eecs.umich.edu{
6735128Sgblack@eecs.umich.edu    IntReg Cansave = tc->readIntReg(NumIntArchRegs + 3);
6745128Sgblack@eecs.umich.edu    IntReg Canrestore = tc->readIntReg(NumIntArchRegs + 4);
6755128Sgblack@eecs.umich.edu    IntReg Otherwin = tc->readIntReg(NumIntArchRegs + 6);
6765128Sgblack@eecs.umich.edu    MiscReg CWP = tc->readMiscReg(MISCREG_CWP);
6775128Sgblack@eecs.umich.edu    MiscReg origCWP = CWP;
6785128Sgblack@eecs.umich.edu    CWP = (CWP + Cansave + 2) % NWindows;
6795128Sgblack@eecs.umich.edu    while(NWindows - 2 - Cansave != 0)
6805128Sgblack@eecs.umich.edu    {
6815128Sgblack@eecs.umich.edu        if (Otherwin) {
6825128Sgblack@eecs.umich.edu            panic("Otherwin non-zero.\n");
6835128Sgblack@eecs.umich.edu        } else {
6845128Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_CWP, CWP);
6855128Sgblack@eecs.umich.edu            //Do the stores
6865128Sgblack@eecs.umich.edu            IntReg sp = tc->readIntReg(StackPointerReg);
6875128Sgblack@eecs.umich.edu            for (int index = 16; index < 32; index++) {
6885128Sgblack@eecs.umich.edu                IntReg regVal = tc->readIntReg(index);
6895128Sgblack@eecs.umich.edu                regVal = htog(regVal);
6905128Sgblack@eecs.umich.edu                if (!tc->getMemPort()->tryWriteBlob(
6915128Sgblack@eecs.umich.edu                        sp + (index - 16) * 4, (uint8_t *)&regVal, 4)) {
6925128Sgblack@eecs.umich.edu                    warn("Failed to save register to the stack when "
6935128Sgblack@eecs.umich.edu                            "flushing windows.\n");
6945128Sgblack@eecs.umich.edu                }
6955128Sgblack@eecs.umich.edu            }
6965128Sgblack@eecs.umich.edu            Canrestore--;
6975128Sgblack@eecs.umich.edu            Cansave++;
6985128Sgblack@eecs.umich.edu            CWP = (CWP + 1) % NWindows;
6995128Sgblack@eecs.umich.edu        }
7005128Sgblack@eecs.umich.edu    }
7015128Sgblack@eecs.umich.edu    tc->setIntReg(NumIntArchRegs + 3, Cansave);
7025128Sgblack@eecs.umich.edu    tc->setIntReg(NumIntArchRegs + 4, Canrestore);
7035128Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_CWP, origCWP);
7045128Sgblack@eecs.umich.edu}
7055128Sgblack@eecs.umich.edu
7065128Sgblack@eecs.umich.eduvoid Sparc64LiveProcess::flushWindows(ThreadContext *tc)
7075128Sgblack@eecs.umich.edu{
7085128Sgblack@eecs.umich.edu    IntReg Cansave = tc->readIntReg(NumIntArchRegs + 3);
7095128Sgblack@eecs.umich.edu    IntReg Canrestore = tc->readIntReg(NumIntArchRegs + 4);
7105128Sgblack@eecs.umich.edu    IntReg Otherwin = tc->readIntReg(NumIntArchRegs + 6);
7115128Sgblack@eecs.umich.edu    MiscReg CWP = tc->readMiscReg(MISCREG_CWP);
7125128Sgblack@eecs.umich.edu    MiscReg origCWP = CWP;
7135128Sgblack@eecs.umich.edu    CWP = (CWP + Cansave + 2) % NWindows;
7145128Sgblack@eecs.umich.edu    while(NWindows - 2 - Cansave != 0)
7155128Sgblack@eecs.umich.edu    {
7165128Sgblack@eecs.umich.edu        if (Otherwin) {
7175128Sgblack@eecs.umich.edu            panic("Otherwin non-zero.\n");
7185128Sgblack@eecs.umich.edu        } else {
7195128Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_CWP, CWP);
7205128Sgblack@eecs.umich.edu            //Do the stores
7215128Sgblack@eecs.umich.edu            IntReg sp = tc->readIntReg(StackPointerReg);
7225128Sgblack@eecs.umich.edu            for (int index = 16; index < 32; index++) {
7235128Sgblack@eecs.umich.edu                IntReg regVal = tc->readIntReg(index);
7245128Sgblack@eecs.umich.edu                regVal = htog(regVal);
7255128Sgblack@eecs.umich.edu                if (!tc->getMemPort()->tryWriteBlob(
7265128Sgblack@eecs.umich.edu                        sp + 2047 + (index - 16) * 8, (uint8_t *)&regVal, 8)) {
7275128Sgblack@eecs.umich.edu                    warn("Failed to save register to the stack when "
7285128Sgblack@eecs.umich.edu                            "flushing windows.\n");
7295128Sgblack@eecs.umich.edu                }
7305128Sgblack@eecs.umich.edu            }
7315128Sgblack@eecs.umich.edu            Canrestore--;
7325128Sgblack@eecs.umich.edu            Cansave++;
7335128Sgblack@eecs.umich.edu            CWP = (CWP + 1) % NWindows;
7345128Sgblack@eecs.umich.edu        }
7355128Sgblack@eecs.umich.edu    }
7365128Sgblack@eecs.umich.edu    tc->setIntReg(NumIntArchRegs + 3, Cansave);
7375128Sgblack@eecs.umich.edu    tc->setIntReg(NumIntArchRegs + 4, Canrestore);
7385128Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_CWP, origCWP);
7395128Sgblack@eecs.umich.edu}
740