process.cc revision 4793
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    {
764111Sgblack@eecs.umich.edu      case 0x03: //Flush window trap
774111Sgblack@eecs.umich.edu        warn("Ignoring request to flush register windows.\n");
784111Sgblack@eecs.umich.edu        break;
794111Sgblack@eecs.umich.edu      default:
804111Sgblack@eecs.umich.edu        panic("Unimplemented trap to operating system: trap number %#x.\n", trapNum);
814111Sgblack@eecs.umich.edu    }
824111Sgblack@eecs.umich.edu}
834111Sgblack@eecs.umich.edu
842474SN/Avoid
854111Sgblack@eecs.umich.eduSparc32LiveProcess::startup()
864111Sgblack@eecs.umich.edu{
874111Sgblack@eecs.umich.edu    argsInit(32 / 8, VMPageSize);
884111Sgblack@eecs.umich.edu
894111Sgblack@eecs.umich.edu    //From the SPARC ABI
904111Sgblack@eecs.umich.edu
914648Sgblack@eecs.umich.edu    //The process runs in user mode with 32 bit addresses
924648Sgblack@eecs.umich.edu    threadContexts[0]->setMiscReg(MISCREG_PSTATE, 0x0a);
934111Sgblack@eecs.umich.edu
944111Sgblack@eecs.umich.edu    //Setup default FP state
954172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_FSR, 0);
964111Sgblack@eecs.umich.edu
974172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_TICK, 0);
984111Sgblack@eecs.umich.edu    //
994111Sgblack@eecs.umich.edu    /*
1004111Sgblack@eecs.umich.edu     * Register window management registers
1014111Sgblack@eecs.umich.edu     */
1024111Sgblack@eecs.umich.edu
1034111Sgblack@eecs.umich.edu    //No windows contain info from other programs
1044172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_OTHERWIN, 0);
1054111Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 6, 0);
1064111Sgblack@eecs.umich.edu    //There are no windows to pop
1074172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_CANRESTORE, 0);
1084111Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 4, 0);
1094111Sgblack@eecs.umich.edu    //All windows are available to save into
1104172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_CANSAVE, NWindows - 2);
1114111Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 3, NWindows - 2);
1124111Sgblack@eecs.umich.edu    //All windows are "clean"
1134172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_CLEANWIN, NWindows);
1144111Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 5, NWindows);
1154111Sgblack@eecs.umich.edu    //Start with register window 0
1164172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_CWP, 0);
1174111Sgblack@eecs.umich.edu    //Always use spill and fill traps 0
1184172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_WSTATE, 0);
1194111Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 7, 0);
1204111Sgblack@eecs.umich.edu    //Set the trap level to 0
1214172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_TL, 0);
1224111Sgblack@eecs.umich.edu    //Set the ASI register to something fixed
1234172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY);
1244111Sgblack@eecs.umich.edu}
1254111Sgblack@eecs.umich.edu
1264111Sgblack@eecs.umich.eduvoid
1274111Sgblack@eecs.umich.eduSparc64LiveProcess::startup()
1282474SN/A{
1293760Sgblack@eecs.umich.edu    argsInit(sizeof(IntReg), VMPageSize);
1302561SN/A
1312561SN/A    //From the SPARC ABI
1322561SN/A
1332561SN/A    //The process runs in user mode
1344172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscReg(MISCREG_PSTATE, 0x02);
1352561SN/A
1362646Ssaidi@eecs.umich.edu    //Setup default FP state
1374172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_FSR, 0);
1382646Ssaidi@eecs.umich.edu
1394172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_TICK, 0);
1402646Ssaidi@eecs.umich.edu    //
1412561SN/A    /*
1422561SN/A     * Register window management registers
1432561SN/A     */
1442561SN/A
1452561SN/A    //No windows contain info from other programs
1464172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_OTHERWIN, 0);
1473761Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 6, 0);
1482561SN/A    //There are no windows to pop
1494172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_CANRESTORE, 0);
1503761Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 4, 0);
1512561SN/A    //All windows are available to save into
1524172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_CANSAVE, NWindows - 2);
1533761Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 3, NWindows - 2);
1542561SN/A    //All windows are "clean"
1554172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_CLEANWIN, NWindows);
1563761Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 5, NWindows);
1572561SN/A    //Start with register window 0
1584172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_CWP, 0);
1593415Sgblack@eecs.umich.edu    //Always use spill and fill traps 0
1604172Ssaidi@eecs.umich.edu    //threadContexts[0]->setMiscRegNoEffect(MISCREG_WSTATE, 0);
1613761Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(NumIntArchRegs + 7, 0);
1623415Sgblack@eecs.umich.edu    //Set the trap level to 0
1634172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_TL, 0);
1643589Sgblack@eecs.umich.edu    //Set the ASI register to something fixed
1654172Ssaidi@eecs.umich.edu    threadContexts[0]->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY);
1662474SN/A}
1672474SN/A
1684111Sgblack@eecs.umich.eduM5_32_auxv_t::M5_32_auxv_t(int32_t type, int32_t val)
1692585SN/A{
1704111Sgblack@eecs.umich.edu    a_type = TheISA::htog(type);
1714111Sgblack@eecs.umich.edu    a_val = TheISA::htog(val);
1722585SN/A}
1732585SN/A
1744111Sgblack@eecs.umich.eduM5_64_auxv_t::M5_64_auxv_t(int64_t type, int64_t val)
1753415Sgblack@eecs.umich.edu{
1764111Sgblack@eecs.umich.edu    a_type = TheISA::htog(type);
1774111Sgblack@eecs.umich.edu    a_val = TheISA::htog(val);
1784111Sgblack@eecs.umich.edu}
1793415Sgblack@eecs.umich.edu
1802561SN/Avoid
1814111Sgblack@eecs.umich.eduSparc64LiveProcess::argsInit(int intSize, int pageSize)
1822561SN/A{
1834111Sgblack@eecs.umich.edu    typedef M5_64_auxv_t auxv_t;
1842561SN/A    Process::startup();
1852474SN/A
1863044Sgblack@eecs.umich.edu    string filename;
1873044Sgblack@eecs.umich.edu    if(argv.size() < 1)
1883044Sgblack@eecs.umich.edu        filename = "";
1893044Sgblack@eecs.umich.edu    else
1903044Sgblack@eecs.umich.edu        filename = argv[0];
1913044Sgblack@eecs.umich.edu
1922561SN/A    Addr alignmentMask = ~(intSize - 1);
1932561SN/A
1942561SN/A    // load object file into target memory
1952561SN/A    objFile->loadSections(initVirtMem);
1962561SN/A
1972585SN/A    enum hardwareCaps
1982585SN/A    {
1992585SN/A        M5_HWCAP_SPARC_FLUSH = 1,
2002585SN/A        M5_HWCAP_SPARC_STBAR = 2,
2012585SN/A        M5_HWCAP_SPARC_SWAP = 4,
2022585SN/A        M5_HWCAP_SPARC_MULDIV = 8,
2032585SN/A        M5_HWCAP_SPARC_V9 = 16,
2042585SN/A        //This one should technically only be set
2052585SN/A        //if there is a cheetah or cheetah_plus tlb,
2062585SN/A        //but we'll use it all the time
2072585SN/A        M5_HWCAP_SPARC_ULTRA3 = 32
2082585SN/A    };
2092585SN/A
2102585SN/A    const int64_t hwcap =
2112585SN/A        M5_HWCAP_SPARC_FLUSH |
2122585SN/A        M5_HWCAP_SPARC_STBAR |
2132585SN/A        M5_HWCAP_SPARC_SWAP |
2142585SN/A        M5_HWCAP_SPARC_MULDIV |
2152585SN/A        M5_HWCAP_SPARC_V9 |
2162585SN/A        M5_HWCAP_SPARC_ULTRA3;
2172585SN/A
2182976Sgblack@eecs.umich.edu
2192976Sgblack@eecs.umich.edu    //Setup the auxilliary vectors. These will already have endian conversion.
2202976Sgblack@eecs.umich.edu    //Auxilliary vectors are loaded only for elf formatted executables.
2212976Sgblack@eecs.umich.edu    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
2222976Sgblack@eecs.umich.edu    if(elfObject)
2232976Sgblack@eecs.umich.edu    {
2242976Sgblack@eecs.umich.edu        //Bits which describe the system hardware capabilities
2254793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_HWCAP, hwcap));
2262976Sgblack@eecs.umich.edu        //The system page size
2274793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PAGESZ, SparcISA::VMPageSize));
2282976Sgblack@eecs.umich.edu        //Defined to be 100 in the kernel source.
2292976Sgblack@eecs.umich.edu        //Frequency at which times() increments
2304793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_CLKTCK, 100));
2312976Sgblack@eecs.umich.edu        // For statically linked executables, this is the virtual address of the
2322976Sgblack@eecs.umich.edu        // program header tables if they appear in the executable image
2334793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
2342976Sgblack@eecs.umich.edu        // This is the size of a program header entry from the elf file.
2354793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
2362976Sgblack@eecs.umich.edu        // This is the number of program headers from the original elf file.
2374793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
2382976Sgblack@eecs.umich.edu        //This is the address of the elf "interpreter", It should be set
2392976Sgblack@eecs.umich.edu        //to 0 for regular executables. It should be something else
2402976Sgblack@eecs.umich.edu        //(not sure what) for dynamic libraries.
2414793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_BASE, 0));
2422976Sgblack@eecs.umich.edu        //This is hardwired to 0 in the elf loading code in the kernel
2434793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
2442976Sgblack@eecs.umich.edu        //The entry point to the program
2454793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
2462976Sgblack@eecs.umich.edu        //Different user and group IDs
2474793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_UID, uid()));
2484793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_EUID, euid()));
2494793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_GID, gid()));
2504793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_EGID, egid()));
2512976Sgblack@eecs.umich.edu        //Whether to enable "secure mode" in the executable
2524793Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_SECURE, 0));
2532976Sgblack@eecs.umich.edu    }
2542585SN/A
2552561SN/A    //Figure out how big the initial stack needs to be
2562561SN/A
2573044Sgblack@eecs.umich.edu    // The unaccounted for 0 at the top of the stack
2583044Sgblack@eecs.umich.edu    int mysterious_size = intSize;
2593044Sgblack@eecs.umich.edu
2603044Sgblack@eecs.umich.edu    //This is the name of the file which is present on the initial stack
2613044Sgblack@eecs.umich.edu    //It's purpose is to let the user space linker examine the original file.
2623044Sgblack@eecs.umich.edu    int file_name_size = filename.size() + 1;
2633044Sgblack@eecs.umich.edu
2642561SN/A    int env_data_size = 0;
2652561SN/A    for (int i = 0; i < envp.size(); ++i) {
2662561SN/A        env_data_size += envp[i].size() + 1;
2672561SN/A    }
2682561SN/A    int arg_data_size = 0;
2692561SN/A    for (int i = 0; i < argv.size(); ++i) {
2702561SN/A        arg_data_size += argv[i].size() + 1;
2712561SN/A    }
2722561SN/A
2733044Sgblack@eecs.umich.edu    //The info_block needs to be padded so it's size is a multiple of the
2743044Sgblack@eecs.umich.edu    //alignment mask. Also, it appears that there needs to be at least some
2753044Sgblack@eecs.umich.edu    //padding, so if the size is already a multiple, we need to increase it
2763044Sgblack@eecs.umich.edu    //anyway.
2773044Sgblack@eecs.umich.edu    int info_block_size =
2783044Sgblack@eecs.umich.edu        (file_name_size +
2793044Sgblack@eecs.umich.edu        env_data_size +
2803044Sgblack@eecs.umich.edu        arg_data_size +
2813044Sgblack@eecs.umich.edu        intSize) & alignmentMask;
2823044Sgblack@eecs.umich.edu
2833044Sgblack@eecs.umich.edu    int info_block_padding =
2843044Sgblack@eecs.umich.edu        info_block_size -
2853044Sgblack@eecs.umich.edu        file_name_size -
2863044Sgblack@eecs.umich.edu        env_data_size -
2873044Sgblack@eecs.umich.edu        arg_data_size;
2883044Sgblack@eecs.umich.edu
2893044Sgblack@eecs.umich.edu    //Each auxilliary vector is two 8 byte words
2902561SN/A    int aux_array_size = intSize * 2 * (auxv.size() + 1);
2912561SN/A
2923044Sgblack@eecs.umich.edu    int envp_array_size = intSize * (envp.size() + 1);
2932561SN/A    int argv_array_size = intSize * (argv.size() + 1);
2942561SN/A
2952561SN/A    int argc_size = intSize;
2962561SN/A    int window_save_size = intSize * 16;
2972561SN/A
2982561SN/A    int space_needed =
2993044Sgblack@eecs.umich.edu        mysterious_size +
3002561SN/A        info_block_size +
3012561SN/A        aux_array_size +
3022561SN/A        envp_array_size +
3032561SN/A        argv_array_size +
3042561SN/A        argc_size +
3052561SN/A        window_save_size;
3062561SN/A
3072561SN/A    stack_min = stack_base - space_needed;
3082561SN/A    stack_min &= alignmentMask;
3092561SN/A    stack_size = stack_base - stack_min;
3102561SN/A
3112561SN/A    // map memory
3122561SN/A    pTable->allocate(roundDown(stack_min, pageSize),
3132561SN/A                     roundUp(stack_size, pageSize));
3142561SN/A
3152561SN/A    // map out initial stack contents
3163044Sgblack@eecs.umich.edu    Addr mysterious_base = stack_base - mysterious_size;
3173044Sgblack@eecs.umich.edu    Addr file_name_base = mysterious_base - file_name_size;
3183044Sgblack@eecs.umich.edu    Addr env_data_base = file_name_base - env_data_size;
3192561SN/A    Addr arg_data_base = env_data_base - arg_data_size;
3203044Sgblack@eecs.umich.edu    Addr auxv_array_base = arg_data_base - aux_array_size - info_block_padding;
3212585SN/A    Addr envp_array_base = auxv_array_base - envp_array_size;
3222561SN/A    Addr argv_array_base = envp_array_base - argv_array_size;
3232561SN/A    Addr argc_base = argv_array_base - argc_size;
3243039Sstever@eecs.umich.edu#ifndef NDEBUG
3253039Sstever@eecs.umich.edu    // only used in DPRINTF
3262561SN/A    Addr window_save_base = argc_base - window_save_size;
3273039Sstever@eecs.umich.edu#endif
3282561SN/A
3292561SN/A    DPRINTF(Sparc, "The addresses of items on the initial stack:\n");
3303044Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - file name\n", file_name_base);
3312561SN/A    DPRINTF(Sparc, "0x%x - env data\n", env_data_base);
3322561SN/A    DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base);
3332585SN/A    DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base);
3342585SN/A    DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base);
3352585SN/A    DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base);
3362561SN/A    DPRINTF(Sparc, "0x%x - argc \n", argc_base);
3372561SN/A    DPRINTF(Sparc, "0x%x - window save\n", window_save_base);
3382561SN/A    DPRINTF(Sparc, "0x%x - stack min\n", stack_min);
3392561SN/A
3402561SN/A    // write contents to stack
3413044Sgblack@eecs.umich.edu
3423044Sgblack@eecs.umich.edu    // figure out argc
3432561SN/A    uint64_t argc = argv.size();
3442585SN/A    uint64_t guestArgc = TheISA::htog(argc);
3452561SN/A
3463044Sgblack@eecs.umich.edu    //Write out the mysterious 0
3473044Sgblack@eecs.umich.edu    uint64_t mysterious_zero = 0;
3483044Sgblack@eecs.umich.edu    initVirtMem->writeBlob(mysterious_base,
3493044Sgblack@eecs.umich.edu            (uint8_t*)&mysterious_zero, mysterious_size);
3503044Sgblack@eecs.umich.edu
3513044Sgblack@eecs.umich.edu    //Write the file name
3523044Sgblack@eecs.umich.edu    initVirtMem->writeString(file_name_base, filename.c_str());
3533044Sgblack@eecs.umich.edu
3542585SN/A    //Copy the aux stuff
3552585SN/A    for(int x = 0; x < auxv.size(); x++)
3562585SN/A    {
3572585SN/A        initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
3582585SN/A                (uint8_t*)&(auxv[x].a_type), intSize);
3592585SN/A        initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
3602585SN/A                (uint8_t*)&(auxv[x].a_val), intSize);
3612585SN/A    }
3622585SN/A    //Write out the terminating zeroed auxilliary vector
3632561SN/A    const uint64_t zero = 0;
3642585SN/A    initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
3652585SN/A            (uint8_t*)&zero, 2 * intSize);
3662561SN/A
3672561SN/A    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
3682561SN/A    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
3692561SN/A
3702585SN/A    initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
3712561SN/A
3723415Sgblack@eecs.umich.edu    //Stuff the trap handlers into the processes address space.
3733415Sgblack@eecs.umich.edu    //Since the stack grows down and is the highest area in the processes
3743415Sgblack@eecs.umich.edu    //address space, we can put stuff above it and stay out of the way.
3753415Sgblack@eecs.umich.edu    int fillSize = sizeof(MachInst) * numFillInsts;
3763415Sgblack@eecs.umich.edu    int spillSize = sizeof(MachInst) * numSpillInsts;
3773415Sgblack@eecs.umich.edu    fillStart = stack_base;
3783415Sgblack@eecs.umich.edu    spillStart = fillStart + fillSize;
3794111Sgblack@eecs.umich.edu    initVirtMem->writeBlob(fillStart, (uint8_t*)fillHandler64, fillSize);
3804111Sgblack@eecs.umich.edu    initVirtMem->writeBlob(spillStart, (uint8_t*)spillHandler64, spillSize);
3813415Sgblack@eecs.umich.edu
3823415Sgblack@eecs.umich.edu    //Set up the thread context to start running the process
3834772Sgblack@eecs.umich.edu    assert(NumArgumentRegs >= 2);
3844772Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(ArgumentReg[0], argc);
3854772Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(ArgumentReg[1], argv_array_base);
3862680Sktlim@umich.edu    threadContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias);
3872561SN/A
3882561SN/A    Addr prog_entry = objFile->entryPoint();
3892680Sktlim@umich.edu    threadContexts[0]->setPC(prog_entry);
3902680Sktlim@umich.edu    threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
3912680Sktlim@umich.edu    threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
3922561SN/A
3933959Sgblack@eecs.umich.edu    //Align the "stack_min" to a page boundary.
3943959Sgblack@eecs.umich.edu    stack_min = roundDown(stack_min, pageSize);
3953959Sgblack@eecs.umich.edu
3962561SN/A//    num_processes++;
3972561SN/A}
3984111Sgblack@eecs.umich.edu
3994111Sgblack@eecs.umich.eduvoid
4004111Sgblack@eecs.umich.eduSparc32LiveProcess::argsInit(int intSize, int pageSize)
4014111Sgblack@eecs.umich.edu{
4024111Sgblack@eecs.umich.edu    typedef M5_32_auxv_t auxv_t;
4034111Sgblack@eecs.umich.edu    Process::startup();
4044111Sgblack@eecs.umich.edu
4054111Sgblack@eecs.umich.edu    string filename;
4064111Sgblack@eecs.umich.edu    if(argv.size() < 1)
4074111Sgblack@eecs.umich.edu        filename = "";
4084111Sgblack@eecs.umich.edu    else
4094111Sgblack@eecs.umich.edu        filename = argv[0];
4104111Sgblack@eecs.umich.edu
4114117Sgblack@eecs.umich.edu    //Even though this is a 32 bit process, the ABI says we still need to
4124117Sgblack@eecs.umich.edu    //maintain double word alignment of the stack pointer.
4134117Sgblack@eecs.umich.edu    Addr alignmentMask = ~(8 - 1);
4144111Sgblack@eecs.umich.edu
4154111Sgblack@eecs.umich.edu    // load object file into target memory
4164111Sgblack@eecs.umich.edu    objFile->loadSections(initVirtMem);
4174111Sgblack@eecs.umich.edu
4184111Sgblack@eecs.umich.edu    //These are the auxilliary vector types
4194111Sgblack@eecs.umich.edu    enum auxTypes
4204111Sgblack@eecs.umich.edu    {
4214111Sgblack@eecs.umich.edu        SPARC_AT_HWCAP = 16,
4224111Sgblack@eecs.umich.edu        SPARC_AT_PAGESZ = 6,
4234111Sgblack@eecs.umich.edu        SPARC_AT_CLKTCK = 17,
4244111Sgblack@eecs.umich.edu        SPARC_AT_PHDR = 3,
4254111Sgblack@eecs.umich.edu        SPARC_AT_PHENT = 4,
4264111Sgblack@eecs.umich.edu        SPARC_AT_PHNUM = 5,
4274111Sgblack@eecs.umich.edu        SPARC_AT_BASE = 7,
4284111Sgblack@eecs.umich.edu        SPARC_AT_FLAGS = 8,
4294111Sgblack@eecs.umich.edu        SPARC_AT_ENTRY = 9,
4304111Sgblack@eecs.umich.edu        SPARC_AT_UID = 11,
4314111Sgblack@eecs.umich.edu        SPARC_AT_EUID = 12,
4324111Sgblack@eecs.umich.edu        SPARC_AT_GID = 13,
4334111Sgblack@eecs.umich.edu        SPARC_AT_EGID = 14,
4344111Sgblack@eecs.umich.edu        SPARC_AT_SECURE = 23
4354111Sgblack@eecs.umich.edu    };
4364111Sgblack@eecs.umich.edu
4374111Sgblack@eecs.umich.edu    enum hardwareCaps
4384111Sgblack@eecs.umich.edu    {
4394111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_FLUSH = 1,
4404111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_STBAR = 2,
4414111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_SWAP = 4,
4424111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_MULDIV = 8,
4434111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_V9 = 16,
4444111Sgblack@eecs.umich.edu        //This one should technically only be set
4454111Sgblack@eecs.umich.edu        //if there is a cheetah or cheetah_plus tlb,
4464111Sgblack@eecs.umich.edu        //but we'll use it all the time
4474111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_ULTRA3 = 32
4484111Sgblack@eecs.umich.edu    };
4494111Sgblack@eecs.umich.edu
4504111Sgblack@eecs.umich.edu    const int64_t hwcap =
4514111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_FLUSH |
4524111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_STBAR |
4534111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_SWAP |
4544111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_MULDIV |
4554111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_V9 |
4564111Sgblack@eecs.umich.edu        M5_HWCAP_SPARC_ULTRA3;
4574111Sgblack@eecs.umich.edu
4584111Sgblack@eecs.umich.edu
4594111Sgblack@eecs.umich.edu    //Setup the auxilliary vectors. These will already have endian conversion.
4604111Sgblack@eecs.umich.edu    //Auxilliary vectors are loaded only for elf formatted executables.
4614111Sgblack@eecs.umich.edu    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
4624111Sgblack@eecs.umich.edu    if(elfObject)
4634111Sgblack@eecs.umich.edu    {
4644111Sgblack@eecs.umich.edu        //Bits which describe the system hardware capabilities
4654111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_HWCAP, hwcap));
4664111Sgblack@eecs.umich.edu        //The system page size
4674111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_PAGESZ, SparcISA::VMPageSize));
4684111Sgblack@eecs.umich.edu        //Defined to be 100 in the kernel source.
4694111Sgblack@eecs.umich.edu        //Frequency at which times() increments
4704111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_CLKTCK, 100));
4714111Sgblack@eecs.umich.edu        // For statically linked executables, this is the virtual address of the
4724111Sgblack@eecs.umich.edu        // program header tables if they appear in the executable image
4734111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_PHDR, elfObject->programHeaderTable()));
4744111Sgblack@eecs.umich.edu        // This is the size of a program header entry from the elf file.
4754111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_PHENT, elfObject->programHeaderSize()));
4764111Sgblack@eecs.umich.edu        // This is the number of program headers from the original elf file.
4774111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_PHNUM, elfObject->programHeaderCount()));
4784111Sgblack@eecs.umich.edu        //This is the address of the elf "interpreter", It should be set
4794111Sgblack@eecs.umich.edu        //to 0 for regular executables. It should be something else
4804111Sgblack@eecs.umich.edu        //(not sure what) for dynamic libraries.
4814111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_BASE, 0));
4824111Sgblack@eecs.umich.edu        //This is hardwired to 0 in the elf loading code in the kernel
4834111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_FLAGS, 0));
4844111Sgblack@eecs.umich.edu        //The entry point to the program
4854111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_ENTRY, objFile->entryPoint()));
4864111Sgblack@eecs.umich.edu        //Different user and group IDs
4874111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_UID, uid()));
4884111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_EUID, euid()));
4894111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_GID, gid()));
4904111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_EGID, egid()));
4914111Sgblack@eecs.umich.edu        //Whether to enable "secure mode" in the executable
4924111Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(SPARC_AT_SECURE, 0));
4934111Sgblack@eecs.umich.edu    }
4944111Sgblack@eecs.umich.edu
4954111Sgblack@eecs.umich.edu    //Figure out how big the initial stack needs to be
4964111Sgblack@eecs.umich.edu
4974164Sgblack@eecs.umich.edu    // The unaccounted for 8 byte 0 at the top of the stack
4984164Sgblack@eecs.umich.edu    int mysterious_size = 8;
4994111Sgblack@eecs.umich.edu
5004111Sgblack@eecs.umich.edu    //This is the name of the file which is present on the initial stack
5014111Sgblack@eecs.umich.edu    //It's purpose is to let the user space linker examine the original file.
5024111Sgblack@eecs.umich.edu    int file_name_size = filename.size() + 1;
5034111Sgblack@eecs.umich.edu
5044111Sgblack@eecs.umich.edu    int env_data_size = 0;
5054111Sgblack@eecs.umich.edu    for (int i = 0; i < envp.size(); ++i) {
5064111Sgblack@eecs.umich.edu        env_data_size += envp[i].size() + 1;
5074111Sgblack@eecs.umich.edu    }
5084111Sgblack@eecs.umich.edu    int arg_data_size = 0;
5094111Sgblack@eecs.umich.edu    for (int i = 0; i < argv.size(); ++i) {
5104111Sgblack@eecs.umich.edu        arg_data_size += argv[i].size() + 1;
5114111Sgblack@eecs.umich.edu    }
5124111Sgblack@eecs.umich.edu
5134164Sgblack@eecs.umich.edu    //The info_block - This seems to need an pad for some reason.
5144111Sgblack@eecs.umich.edu    int info_block_size =
5154164Sgblack@eecs.umich.edu        (mysterious_size +
5164164Sgblack@eecs.umich.edu        file_name_size +
5174111Sgblack@eecs.umich.edu        env_data_size +
5184164Sgblack@eecs.umich.edu        arg_data_size + intSize);
5194111Sgblack@eecs.umich.edu
5204164Sgblack@eecs.umich.edu    //Each auxilliary vector is two 4 byte words
5214111Sgblack@eecs.umich.edu    int aux_array_size = intSize * 2 * (auxv.size() + 1);
5224111Sgblack@eecs.umich.edu
5234111Sgblack@eecs.umich.edu    int envp_array_size = intSize * (envp.size() + 1);
5244111Sgblack@eecs.umich.edu    int argv_array_size = intSize * (argv.size() + 1);
5254111Sgblack@eecs.umich.edu
5264111Sgblack@eecs.umich.edu    int argc_size = intSize;
5274111Sgblack@eecs.umich.edu    int window_save_size = intSize * 16;
5284111Sgblack@eecs.umich.edu
5294111Sgblack@eecs.umich.edu    int space_needed =
5304164Sgblack@eecs.umich.edu        info_block_size +
5314111Sgblack@eecs.umich.edu        aux_array_size +
5324111Sgblack@eecs.umich.edu        envp_array_size +
5334111Sgblack@eecs.umich.edu        argv_array_size +
5344111Sgblack@eecs.umich.edu        argc_size +
5354111Sgblack@eecs.umich.edu        window_save_size;
5364111Sgblack@eecs.umich.edu
5374111Sgblack@eecs.umich.edu    stack_min = stack_base - space_needed;
5384111Sgblack@eecs.umich.edu    stack_min &= alignmentMask;
5394111Sgblack@eecs.umich.edu    stack_size = stack_base - stack_min;
5404111Sgblack@eecs.umich.edu
5414111Sgblack@eecs.umich.edu    // map memory
5424111Sgblack@eecs.umich.edu    pTable->allocate(roundDown(stack_min, pageSize),
5434111Sgblack@eecs.umich.edu                     roundUp(stack_size, pageSize));
5444111Sgblack@eecs.umich.edu
5454111Sgblack@eecs.umich.edu    // map out initial stack contents
5464117Sgblack@eecs.umich.edu    uint32_t window_save_base = stack_min;
5474117Sgblack@eecs.umich.edu    uint32_t argc_base = window_save_base + window_save_size;
5484117Sgblack@eecs.umich.edu    uint32_t argv_array_base = argc_base + argc_size;
5494117Sgblack@eecs.umich.edu    uint32_t envp_array_base = argv_array_base + argv_array_size;
5504117Sgblack@eecs.umich.edu    uint32_t auxv_array_base = envp_array_base + envp_array_size;
5514117Sgblack@eecs.umich.edu    //The info block is pushed up against the top of the stack, while
5524117Sgblack@eecs.umich.edu    //the rest of the initial stack frame is aligned to an 8 byte boudary.
5534164Sgblack@eecs.umich.edu    uint32_t arg_data_base = stack_base - info_block_size + intSize;
5544117Sgblack@eecs.umich.edu    uint32_t env_data_base = arg_data_base + arg_data_size;
5554117Sgblack@eecs.umich.edu    uint32_t file_name_base = env_data_base + env_data_size;
5564117Sgblack@eecs.umich.edu    uint32_t mysterious_base = file_name_base + file_name_size;
5574111Sgblack@eecs.umich.edu
5584111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "The addresses of items on the initial stack:\n");
5594111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - file name\n", file_name_base);
5604111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - env data\n", env_data_base);
5614111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base);
5624111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base);
5634111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base);
5644111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base);
5654111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - argc \n", argc_base);
5664111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - window save\n", window_save_base);
5674111Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - stack min\n", stack_min);
5684111Sgblack@eecs.umich.edu
5694111Sgblack@eecs.umich.edu    // write contents to stack
5704111Sgblack@eecs.umich.edu
5714111Sgblack@eecs.umich.edu    // figure out argc
5724111Sgblack@eecs.umich.edu    uint32_t argc = argv.size();
5734111Sgblack@eecs.umich.edu    uint32_t guestArgc = TheISA::htog(argc);
5744111Sgblack@eecs.umich.edu
5754111Sgblack@eecs.umich.edu    //Write out the mysterious 0
5764111Sgblack@eecs.umich.edu    uint64_t mysterious_zero = 0;
5774111Sgblack@eecs.umich.edu    initVirtMem->writeBlob(mysterious_base,
5784111Sgblack@eecs.umich.edu            (uint8_t*)&mysterious_zero, mysterious_size);
5794111Sgblack@eecs.umich.edu
5804111Sgblack@eecs.umich.edu    //Write the file name
5814111Sgblack@eecs.umich.edu    initVirtMem->writeString(file_name_base, filename.c_str());
5824111Sgblack@eecs.umich.edu
5834111Sgblack@eecs.umich.edu    //Copy the aux stuff
5844111Sgblack@eecs.umich.edu    for(int x = 0; x < auxv.size(); x++)
5854111Sgblack@eecs.umich.edu    {
5864111Sgblack@eecs.umich.edu        initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
5874111Sgblack@eecs.umich.edu                (uint8_t*)&(auxv[x].a_type), intSize);
5884111Sgblack@eecs.umich.edu        initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
5894111Sgblack@eecs.umich.edu                (uint8_t*)&(auxv[x].a_val), intSize);
5904111Sgblack@eecs.umich.edu    }
5914111Sgblack@eecs.umich.edu    //Write out the terminating zeroed auxilliary vector
5924111Sgblack@eecs.umich.edu    const uint64_t zero = 0;
5934111Sgblack@eecs.umich.edu    initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
5944111Sgblack@eecs.umich.edu            (uint8_t*)&zero, 2 * intSize);
5954111Sgblack@eecs.umich.edu
5964117Sgblack@eecs.umich.edu    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
5974117Sgblack@eecs.umich.edu    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
5984111Sgblack@eecs.umich.edu
5994111Sgblack@eecs.umich.edu    initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
6004111Sgblack@eecs.umich.edu
6014111Sgblack@eecs.umich.edu    //Stuff the trap handlers into the processes address space.
6024111Sgblack@eecs.umich.edu    //Since the stack grows down and is the highest area in the processes
6034111Sgblack@eecs.umich.edu    //address space, we can put stuff above it and stay out of the way.
6044111Sgblack@eecs.umich.edu    int fillSize = sizeof(MachInst) * numFillInsts;
6054111Sgblack@eecs.umich.edu    int spillSize = sizeof(MachInst) * numSpillInsts;
6064111Sgblack@eecs.umich.edu    fillStart = stack_base;
6074111Sgblack@eecs.umich.edu    spillStart = fillStart + fillSize;
6084111Sgblack@eecs.umich.edu    initVirtMem->writeBlob(fillStart, (uint8_t*)fillHandler32, fillSize);
6094111Sgblack@eecs.umich.edu    initVirtMem->writeBlob(spillStart, (uint8_t*)spillHandler32, spillSize);
6104111Sgblack@eecs.umich.edu
6114111Sgblack@eecs.umich.edu    //Set up the thread context to start running the process
6124772Sgblack@eecs.umich.edu    //assert(NumArgumentRegs >= 2);
6134772Sgblack@eecs.umich.edu    //threadContexts[0]->setIntReg(ArgumentReg[0], argc);
6144772Sgblack@eecs.umich.edu    //threadContexts[0]->setIntReg(ArgumentReg[1], argv_array_base);
6154111Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(StackPointerReg, stack_min);
6164111Sgblack@eecs.umich.edu
6174117Sgblack@eecs.umich.edu    uint32_t prog_entry = objFile->entryPoint();
6184111Sgblack@eecs.umich.edu    threadContexts[0]->setPC(prog_entry);
6194111Sgblack@eecs.umich.edu    threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
6204111Sgblack@eecs.umich.edu    threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
6214111Sgblack@eecs.umich.edu
6224111Sgblack@eecs.umich.edu    //Align the "stack_min" to a page boundary.
6234111Sgblack@eecs.umich.edu    stack_min = roundDown(stack_min, pageSize);
6244111Sgblack@eecs.umich.edu
6254111Sgblack@eecs.umich.edu//    num_processes++;
6264111Sgblack@eecs.umich.edu}
627