process.cc revision 3114
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
322474SN/A#include "arch/sparc/isa_traits.hh"
332207SN/A#include "arch/sparc/process.hh"
342454SN/A#include "base/loader/object_file.hh"
352976Sgblack@eecs.umich.edu#include "base/loader/elf_object.hh"
362454SN/A#include "base/misc.hh"
372680Sktlim@umich.edu#include "cpu/thread_context.hh"
382561SN/A#include "mem/page_table.hh"
392561SN/A#include "mem/translating_port.hh"
402474SN/A#include "sim/system.hh"
412207SN/A
422458SN/Ausing namespace std;
432474SN/Ausing namespace SparcISA;
442458SN/A
452207SN/A
462474SN/ASparcLiveProcess::SparcLiveProcess(const std::string &nm, ObjectFile *objFile,
472474SN/A        System *_system, int stdin_fd, int stdout_fd, int stderr_fd,
483114Sgblack@eecs.umich.edu        std::vector<std::string> &argv, std::vector<std::string> &envp,
493114Sgblack@eecs.umich.edu        uint64_t _uid, uint64_t _euid, uint64_t _gid, uint64_t _egid,
503114Sgblack@eecs.umich.edu        uint64_t _pid, uint64_t _ppid)
512474SN/A    : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd,
523114Sgblack@eecs.umich.edu        argv, envp, _uid, _euid, _gid, _egid, _pid, _ppid)
532474SN/A{
542474SN/A
552474SN/A    // XXX all the below need to be updated for SPARC - Ali
562474SN/A    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
572474SN/A    brk_point = roundUp(brk_point, VMPageSize);
582474SN/A
592561SN/A    // Set up stack. On SPARC Linux, stack goes from the top of memory
602561SN/A    // downward, less the hole for the kernel address space.
613044Sgblack@eecs.umich.edu    stack_base = (Addr)0x80000000000ULL;
622474SN/A
632474SN/A    // Set up region for mmaps.  Tru64 seems to start just above 0 and
642474SN/A    // grow up from there.
653044Sgblack@eecs.umich.edu    mmap_start = mmap_end = 0xfffff80000000000ULL;
662474SN/A
672474SN/A    // Set pointer for next thread stack.  Reserve 8M for main stack.
682474SN/A    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
692474SN/A}
702474SN/A
712474SN/Avoid
722474SN/ASparcLiveProcess::startup()
732474SN/A{
742474SN/A    argsInit(MachineBytes, VMPageSize);
752561SN/A
762561SN/A    //From the SPARC ABI
772561SN/A
782561SN/A    //The process runs in user mode
792680Sktlim@umich.edu    threadContexts[0]->setMiscRegWithEffect(MISCREG_PSTATE, 0x02);
802561SN/A
812646Ssaidi@eecs.umich.edu    //Setup default FP state
822680Sktlim@umich.edu    threadContexts[0]->setMiscReg(MISCREG_FSR, 0);
832646Ssaidi@eecs.umich.edu
842680Sktlim@umich.edu    threadContexts[0]->setMiscReg(MISCREG_TICK, 0);
852646Ssaidi@eecs.umich.edu    //
862561SN/A    /*
872561SN/A     * Register window management registers
882561SN/A     */
892561SN/A
902561SN/A    //No windows contain info from other programs
912680Sktlim@umich.edu    threadContexts[0]->setMiscRegWithEffect(MISCREG_OTHERWIN, 0);
922561SN/A    //There are no windows to pop
932680Sktlim@umich.edu    threadContexts[0]->setMiscRegWithEffect(MISCREG_CANRESTORE, 0);
942561SN/A    //All windows are available to save into
952680Sktlim@umich.edu    threadContexts[0]->setMiscRegWithEffect(MISCREG_CANSAVE, NWindows - 2);
962561SN/A    //All windows are "clean"
972680Sktlim@umich.edu    threadContexts[0]->setMiscRegWithEffect(MISCREG_CLEANWIN, NWindows);
982561SN/A    //Start with register window 0
992680Sktlim@umich.edu    threadContexts[0]->setMiscRegWithEffect(MISCREG_CWP, 0);
1002474SN/A}
1012474SN/A
1022585SN/Am5_auxv_t buildAuxVect(int64_t type, int64_t val)
1032585SN/A{
1042585SN/A    m5_auxv_t result;
1052585SN/A    result.a_type = TheISA::htog(type);
1062585SN/A    result.a_val = TheISA::htog(val);
1072585SN/A    return result;
1082585SN/A}
1092585SN/A
1102561SN/Avoid
1112561SN/ASparcLiveProcess::argsInit(int intSize, int pageSize)
1122561SN/A{
1132561SN/A    Process::startup();
1142474SN/A
1153044Sgblack@eecs.umich.edu    string filename;
1163044Sgblack@eecs.umich.edu    if(argv.size() < 1)
1173044Sgblack@eecs.umich.edu        filename = "";
1183044Sgblack@eecs.umich.edu    else
1193044Sgblack@eecs.umich.edu        filename = argv[0];
1203044Sgblack@eecs.umich.edu
1212561SN/A    Addr alignmentMask = ~(intSize - 1);
1222561SN/A
1232561SN/A    // load object file into target memory
1242561SN/A    objFile->loadSections(initVirtMem);
1252561SN/A
1262585SN/A    //These are the auxilliary vector types
1272585SN/A    enum auxTypes
1282585SN/A    {
1292585SN/A        SPARC_AT_HWCAP = 16,
1302585SN/A        SPARC_AT_PAGESZ = 6,
1312585SN/A        SPARC_AT_CLKTCK = 17,
1322585SN/A        SPARC_AT_PHDR = 3,
1332585SN/A        SPARC_AT_PHENT = 4,
1342585SN/A        SPARC_AT_PHNUM = 5,
1352585SN/A        SPARC_AT_BASE = 7,
1362585SN/A        SPARC_AT_FLAGS = 8,
1372585SN/A        SPARC_AT_ENTRY = 9,
1382585SN/A        SPARC_AT_UID = 11,
1392585SN/A        SPARC_AT_EUID = 12,
1402585SN/A        SPARC_AT_GID = 13,
1412976Sgblack@eecs.umich.edu        SPARC_AT_EGID = 14,
1422976Sgblack@eecs.umich.edu        SPARC_AT_SECURE = 23
1432585SN/A    };
1442585SN/A
1452585SN/A    enum hardwareCaps
1462585SN/A    {
1472585SN/A        M5_HWCAP_SPARC_FLUSH = 1,
1482585SN/A        M5_HWCAP_SPARC_STBAR = 2,
1492585SN/A        M5_HWCAP_SPARC_SWAP = 4,
1502585SN/A        M5_HWCAP_SPARC_MULDIV = 8,
1512585SN/A        M5_HWCAP_SPARC_V9 = 16,
1522585SN/A        //This one should technically only be set
1532585SN/A        //if there is a cheetah or cheetah_plus tlb,
1542585SN/A        //but we'll use it all the time
1552585SN/A        M5_HWCAP_SPARC_ULTRA3 = 32
1562585SN/A    };
1572585SN/A
1582585SN/A    const int64_t hwcap =
1592585SN/A        M5_HWCAP_SPARC_FLUSH |
1602585SN/A        M5_HWCAP_SPARC_STBAR |
1612585SN/A        M5_HWCAP_SPARC_SWAP |
1622585SN/A        M5_HWCAP_SPARC_MULDIV |
1632585SN/A        M5_HWCAP_SPARC_V9 |
1642585SN/A        M5_HWCAP_SPARC_ULTRA3;
1652585SN/A
1662976Sgblack@eecs.umich.edu
1672976Sgblack@eecs.umich.edu    //Setup the auxilliary vectors. These will already have endian conversion.
1682976Sgblack@eecs.umich.edu    //Auxilliary vectors are loaded only for elf formatted executables.
1692976Sgblack@eecs.umich.edu    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
1702976Sgblack@eecs.umich.edu    if(elfObject)
1712976Sgblack@eecs.umich.edu    {
1722976Sgblack@eecs.umich.edu        //Bits which describe the system hardware capabilities
1732976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_HWCAP, hwcap));
1742976Sgblack@eecs.umich.edu        //The system page size
1752976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_PAGESZ, SparcISA::VMPageSize));
1762976Sgblack@eecs.umich.edu        //Defined to be 100 in the kernel source.
1772976Sgblack@eecs.umich.edu        //Frequency at which times() increments
1782976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_CLKTCK, 100));
1792976Sgblack@eecs.umich.edu        // For statically linked executables, this is the virtual address of the
1802976Sgblack@eecs.umich.edu        // program header tables if they appear in the executable image
1812976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_PHDR, elfObject->programHeaderTable()));
1822976Sgblack@eecs.umich.edu        // This is the size of a program header entry from the elf file.
1832976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_PHENT, elfObject->programHeaderSize()));
1842976Sgblack@eecs.umich.edu        // This is the number of program headers from the original elf file.
1852976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_PHNUM, elfObject->programHeaderCount()));
1862976Sgblack@eecs.umich.edu        //This is the address of the elf "interpreter", It should be set
1872976Sgblack@eecs.umich.edu        //to 0 for regular executables. It should be something else
1882976Sgblack@eecs.umich.edu        //(not sure what) for dynamic libraries.
1892976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_BASE, 0));
1902976Sgblack@eecs.umich.edu        //This is hardwired to 0 in the elf loading code in the kernel
1912976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_FLAGS, 0));
1922976Sgblack@eecs.umich.edu        //The entry point to the program
1932976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_ENTRY, objFile->entryPoint()));
1942976Sgblack@eecs.umich.edu        //Different user and group IDs
1953114Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_UID, uid()));
1963114Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_EUID, euid()));
1973114Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_GID, gid()));
1983114Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_EGID, egid()));
1992976Sgblack@eecs.umich.edu        //Whether to enable "secure mode" in the executable
2002976Sgblack@eecs.umich.edu        auxv.push_back(buildAuxVect(SPARC_AT_SECURE, 0));
2012976Sgblack@eecs.umich.edu    }
2022585SN/A
2032561SN/A    //Figure out how big the initial stack needs to be
2042561SN/A
2053044Sgblack@eecs.umich.edu    // The unaccounted for 0 at the top of the stack
2063044Sgblack@eecs.umich.edu    int mysterious_size = intSize;
2073044Sgblack@eecs.umich.edu
2083044Sgblack@eecs.umich.edu    //This is the name of the file which is present on the initial stack
2093044Sgblack@eecs.umich.edu    //It's purpose is to let the user space linker examine the original file.
2103044Sgblack@eecs.umich.edu    int file_name_size = filename.size() + 1;
2113044Sgblack@eecs.umich.edu
2122561SN/A    int env_data_size = 0;
2132561SN/A    for (int i = 0; i < envp.size(); ++i) {
2142561SN/A        env_data_size += envp[i].size() + 1;
2152561SN/A    }
2162561SN/A    int arg_data_size = 0;
2172561SN/A    for (int i = 0; i < argv.size(); ++i) {
2182561SN/A        arg_data_size += argv[i].size() + 1;
2192561SN/A    }
2202561SN/A
2213044Sgblack@eecs.umich.edu    //The info_block needs to be padded so it's size is a multiple of the
2223044Sgblack@eecs.umich.edu    //alignment mask. Also, it appears that there needs to be at least some
2233044Sgblack@eecs.umich.edu    //padding, so if the size is already a multiple, we need to increase it
2243044Sgblack@eecs.umich.edu    //anyway.
2253044Sgblack@eecs.umich.edu    int info_block_size =
2263044Sgblack@eecs.umich.edu        (file_name_size +
2273044Sgblack@eecs.umich.edu        env_data_size +
2283044Sgblack@eecs.umich.edu        arg_data_size +
2293044Sgblack@eecs.umich.edu        intSize) & alignmentMask;
2303044Sgblack@eecs.umich.edu
2313044Sgblack@eecs.umich.edu    int info_block_padding =
2323044Sgblack@eecs.umich.edu        info_block_size -
2333044Sgblack@eecs.umich.edu        file_name_size -
2343044Sgblack@eecs.umich.edu        env_data_size -
2353044Sgblack@eecs.umich.edu        arg_data_size;
2363044Sgblack@eecs.umich.edu
2373044Sgblack@eecs.umich.edu    //Each auxilliary vector is two 8 byte words
2382561SN/A    int aux_array_size = intSize * 2 * (auxv.size() + 1);
2392561SN/A
2403044Sgblack@eecs.umich.edu    int envp_array_size = intSize * (envp.size() + 1);
2412561SN/A    int argv_array_size = intSize * (argv.size() + 1);
2422561SN/A
2432561SN/A    int argc_size = intSize;
2442561SN/A    int window_save_size = intSize * 16;
2452561SN/A
2462561SN/A    int space_needed =
2473044Sgblack@eecs.umich.edu        mysterious_size +
2482561SN/A        info_block_size +
2492561SN/A        aux_array_size +
2502561SN/A        envp_array_size +
2512561SN/A        argv_array_size +
2522561SN/A        argc_size +
2532561SN/A        window_save_size;
2542561SN/A
2552561SN/A    stack_min = stack_base - space_needed;
2562561SN/A    stack_min &= alignmentMask;
2572561SN/A    stack_size = stack_base - stack_min;
2582561SN/A
2592561SN/A    // map memory
2602561SN/A    pTable->allocate(roundDown(stack_min, pageSize),
2612561SN/A                     roundUp(stack_size, pageSize));
2622561SN/A
2632561SN/A    // map out initial stack contents
2643044Sgblack@eecs.umich.edu    Addr mysterious_base = stack_base - mysterious_size;
2653044Sgblack@eecs.umich.edu    Addr file_name_base = mysterious_base - file_name_size;
2663044Sgblack@eecs.umich.edu    Addr env_data_base = file_name_base - env_data_size;
2672561SN/A    Addr arg_data_base = env_data_base - arg_data_size;
2683044Sgblack@eecs.umich.edu    Addr auxv_array_base = arg_data_base - aux_array_size - info_block_padding;
2692585SN/A    Addr envp_array_base = auxv_array_base - envp_array_size;
2702561SN/A    Addr argv_array_base = envp_array_base - argv_array_size;
2712561SN/A    Addr argc_base = argv_array_base - argc_size;
2723039Sstever@eecs.umich.edu#ifndef NDEBUG
2733039Sstever@eecs.umich.edu    // only used in DPRINTF
2742561SN/A    Addr window_save_base = argc_base - window_save_size;
2753039Sstever@eecs.umich.edu#endif
2762561SN/A
2772561SN/A    DPRINTF(Sparc, "The addresses of items on the initial stack:\n");
2783044Sgblack@eecs.umich.edu    DPRINTF(Sparc, "0x%x - file name\n", file_name_base);
2792561SN/A    DPRINTF(Sparc, "0x%x - env data\n", env_data_base);
2802561SN/A    DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base);
2812585SN/A    DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base);
2822585SN/A    DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base);
2832585SN/A    DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base);
2842561SN/A    DPRINTF(Sparc, "0x%x - argc \n", argc_base);
2852561SN/A    DPRINTF(Sparc, "0x%x - window save\n", window_save_base);
2862561SN/A    DPRINTF(Sparc, "0x%x - stack min\n", stack_min);
2872561SN/A
2882561SN/A    // write contents to stack
2893044Sgblack@eecs.umich.edu
2903044Sgblack@eecs.umich.edu    // figure out argc
2912561SN/A    uint64_t argc = argv.size();
2922585SN/A    uint64_t guestArgc = TheISA::htog(argc);
2932561SN/A
2943044Sgblack@eecs.umich.edu    //Write out the mysterious 0
2953044Sgblack@eecs.umich.edu    uint64_t mysterious_zero = 0;
2963044Sgblack@eecs.umich.edu    initVirtMem->writeBlob(mysterious_base,
2973044Sgblack@eecs.umich.edu            (uint8_t*)&mysterious_zero, mysterious_size);
2983044Sgblack@eecs.umich.edu
2993044Sgblack@eecs.umich.edu    //Write the file name
3003044Sgblack@eecs.umich.edu    initVirtMem->writeString(file_name_base, filename.c_str());
3013044Sgblack@eecs.umich.edu
3022585SN/A    //Copy the aux stuff
3032585SN/A    for(int x = 0; x < auxv.size(); x++)
3042585SN/A    {
3052585SN/A        initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
3062585SN/A                (uint8_t*)&(auxv[x].a_type), intSize);
3072585SN/A        initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
3082585SN/A                (uint8_t*)&(auxv[x].a_val), intSize);
3092585SN/A    }
3102585SN/A    //Write out the terminating zeroed auxilliary vector
3112561SN/A    const uint64_t zero = 0;
3122585SN/A    initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
3132585SN/A            (uint8_t*)&zero, 2 * intSize);
3142561SN/A
3152561SN/A    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
3162561SN/A    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
3172561SN/A
3182585SN/A    initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
3192561SN/A
3202680Sktlim@umich.edu    threadContexts[0]->setIntReg(ArgumentReg0, argc);
3212680Sktlim@umich.edu    threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base);
3222680Sktlim@umich.edu    threadContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias);
3232561SN/A
3242561SN/A    Addr prog_entry = objFile->entryPoint();
3252680Sktlim@umich.edu    threadContexts[0]->setPC(prog_entry);
3262680Sktlim@umich.edu    threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
3272680Sktlim@umich.edu    threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
3282561SN/A
3292561SN/A//    num_processes++;
3302561SN/A}
331