process.cc revision 2665
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 "arch/sparc/linux/process.hh"
352600SN/A#include "arch/sparc/solaris/process.hh"
362454SN/A#include "base/loader/object_file.hh"
372454SN/A#include "base/misc.hh"
382474SN/A#include "cpu/exec_context.hh"
392561SN/A#include "mem/page_table.hh"
402561SN/A#include "mem/translating_port.hh"
412474SN/A#include "sim/builder.hh"
422474SN/A#include "sim/system.hh"
432207SN/A
442458SN/Ausing namespace std;
452474SN/Ausing namespace SparcISA;
462458SN/A
472474SN/ASparcLiveProcess *
482474SN/ASparcLiveProcess::create(const std::string &nm, System *system, int stdin_fd,
492474SN/A        int stdout_fd, int stderr_fd, std::string executable,
502474SN/A        std::vector<std::string> &argv, std::vector<std::string> &envp)
512207SN/A{
522474SN/A    SparcLiveProcess *process = NULL;
532207SN/A
542474SN/A    ObjectFile *objFile = createObjectFile(executable);
552474SN/A    if (objFile == NULL) {
562474SN/A        fatal("Can't load object file %s", executable);
572474SN/A    }
582474SN/A
592474SN/A
602207SN/A    if (objFile->getArch() != ObjectFile::SPARC)
612600SN/A        fatal("Object file with arch %x does not match architecture %x.",
622600SN/A                objFile->getArch(), ObjectFile::SPARC);
632207SN/A    switch (objFile->getOpSys()) {
642207SN/A      case ObjectFile::Linux:
652458SN/A        process = new SparcLinuxProcess(nm, objFile, system,
662207SN/A                                        stdin_fd, stdout_fd, stderr_fd,
672207SN/A                                        argv, envp);
682207SN/A        break;
692207SN/A
702600SN/A
712207SN/A      case ObjectFile::Solaris:
722600SN/A        process = new SparcSolarisProcess(nm, objFile, system,
732600SN/A                                        stdin_fd, stdout_fd, stderr_fd,
742600SN/A                                        argv, envp);
752600SN/A        break;
762207SN/A      default:
772207SN/A        fatal("Unknown/unsupported operating system.");
782207SN/A    }
792474SN/A
802474SN/A    if (process == NULL)
812474SN/A        fatal("Unknown error creating process object.");
822207SN/A    return process;
832207SN/A}
842207SN/A
852474SN/ASparcLiveProcess::SparcLiveProcess(const std::string &nm, ObjectFile *objFile,
862474SN/A        System *_system, int stdin_fd, int stdout_fd, int stderr_fd,
872474SN/A        std::vector<std::string> &argv, std::vector<std::string> &envp)
882474SN/A    : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd,
892474SN/A        argv, envp)
902474SN/A{
912474SN/A
922474SN/A    // XXX all the below need to be updated for SPARC - Ali
932474SN/A    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
942474SN/A    brk_point = roundUp(brk_point, VMPageSize);
952474SN/A
962561SN/A    // Set up stack. On SPARC Linux, stack goes from the top of memory
972561SN/A    // downward, less the hole for the kernel address space.
982588SN/A    stack_base = ((Addr)0x80000000000ULL);
992474SN/A
1002474SN/A    // Set up region for mmaps.  Tru64 seems to start just above 0 and
1012474SN/A    // grow up from there.
1022585SN/A    mmap_start = mmap_end = 0x800000;
1032474SN/A
1042474SN/A    // Set pointer for next thread stack.  Reserve 8M for main stack.
1052474SN/A    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
1062474SN/A}
1072474SN/A
1082474SN/Avoid
1092474SN/ASparcLiveProcess::startup()
1102474SN/A{
1112474SN/A    argsInit(MachineBytes, VMPageSize);
1122561SN/A
1132561SN/A    //From the SPARC ABI
1142561SN/A
1152561SN/A    //The process runs in user mode
1162646Ssaidi@eecs.umich.edu    execContexts[0]->setMiscRegWithEffect(MISCREG_PSTATE, 0x02);
1172561SN/A
1182646Ssaidi@eecs.umich.edu    //Setup default FP state
1192646Ssaidi@eecs.umich.edu    execContexts[0]->setMiscReg(MISCREG_FSR, 0);
1202646Ssaidi@eecs.umich.edu
1212646Ssaidi@eecs.umich.edu    execContexts[0]->setMiscReg(MISCREG_TICK, 0);
1222646Ssaidi@eecs.umich.edu    //
1232561SN/A    /*
1242561SN/A     * Register window management registers
1252561SN/A     */
1262561SN/A
1272561SN/A    //No windows contain info from other programs
1282561SN/A    execContexts[0]->setMiscRegWithEffect(MISCREG_OTHERWIN, 0);
1292561SN/A    //There are no windows to pop
1302561SN/A    execContexts[0]->setMiscRegWithEffect(MISCREG_CANRESTORE, 0);
1312561SN/A    //All windows are available to save into
1322561SN/A    execContexts[0]->setMiscRegWithEffect(MISCREG_CANSAVE, NWindows - 2);
1332561SN/A    //All windows are "clean"
1342561SN/A    execContexts[0]->setMiscRegWithEffect(MISCREG_CLEANWIN, NWindows);
1352561SN/A    //Start with register window 0
1362561SN/A    execContexts[0]->setMiscRegWithEffect(MISCREG_CWP, 0);
1372474SN/A}
1382474SN/A
1392585SN/Am5_auxv_t buildAuxVect(int64_t type, int64_t val)
1402585SN/A{
1412585SN/A    m5_auxv_t result;
1422585SN/A    result.a_type = TheISA::htog(type);
1432585SN/A    result.a_val = TheISA::htog(val);
1442585SN/A    return result;
1452585SN/A}
1462585SN/A
1472561SN/Avoid
1482561SN/ASparcLiveProcess::argsInit(int intSize, int pageSize)
1492561SN/A{
1502561SN/A    Process::startup();
1512474SN/A
1522561SN/A    Addr alignmentMask = ~(intSize - 1);
1532561SN/A
1542561SN/A    // load object file into target memory
1552561SN/A    objFile->loadSections(initVirtMem);
1562561SN/A
1572585SN/A    //These are the auxilliary vector types
1582585SN/A    enum auxTypes
1592585SN/A    {
1602585SN/A        SPARC_AT_HWCAP = 16,
1612585SN/A        SPARC_AT_PAGESZ = 6,
1622585SN/A        SPARC_AT_CLKTCK = 17,
1632585SN/A        SPARC_AT_PHDR = 3,
1642585SN/A        SPARC_AT_PHENT = 4,
1652585SN/A        SPARC_AT_PHNUM = 5,
1662585SN/A        SPARC_AT_BASE = 7,
1672585SN/A        SPARC_AT_FLAGS = 8,
1682585SN/A        SPARC_AT_ENTRY = 9,
1692585SN/A        SPARC_AT_UID = 11,
1702585SN/A        SPARC_AT_EUID = 12,
1712585SN/A        SPARC_AT_GID = 13,
1722585SN/A        SPARC_AT_EGID = 14
1732585SN/A    };
1742585SN/A
1752585SN/A    enum hardwareCaps
1762585SN/A    {
1772585SN/A        M5_HWCAP_SPARC_FLUSH = 1,
1782585SN/A        M5_HWCAP_SPARC_STBAR = 2,
1792585SN/A        M5_HWCAP_SPARC_SWAP = 4,
1802585SN/A        M5_HWCAP_SPARC_MULDIV = 8,
1812585SN/A        M5_HWCAP_SPARC_V9 = 16,
1822585SN/A        //This one should technically only be set
1832585SN/A        //if there is a cheetah or cheetah_plus tlb,
1842585SN/A        //but we'll use it all the time
1852585SN/A        M5_HWCAP_SPARC_ULTRA3 = 32
1862585SN/A    };
1872585SN/A
1882585SN/A    const int64_t hwcap =
1892585SN/A        M5_HWCAP_SPARC_FLUSH |
1902585SN/A        M5_HWCAP_SPARC_STBAR |
1912585SN/A        M5_HWCAP_SPARC_SWAP |
1922585SN/A        M5_HWCAP_SPARC_MULDIV |
1932585SN/A        M5_HWCAP_SPARC_V9 |
1942585SN/A        M5_HWCAP_SPARC_ULTRA3;
1952585SN/A
1962585SN/A    //Setup the auxilliary vectors. These will already have
1972585SN/A    //endian conversion.
1982585SN/A    auxv.push_back(buildAuxVect(SPARC_AT_EGID, 100));
1992585SN/A    auxv.push_back(buildAuxVect(SPARC_AT_GID, 100));
2002585SN/A    auxv.push_back(buildAuxVect(SPARC_AT_EUID, 100));
2012585SN/A    auxv.push_back(buildAuxVect(SPARC_AT_UID, 100));
2022585SN/A    //This would work, but the entry point is a protected member
2032585SN/A    //auxv.push_back(buildAuxVect(SPARC_AT_ENTRY, objFile->entry));
2042585SN/A    auxv.push_back(buildAuxVect(SPARC_AT_FLAGS, 0));
2052585SN/A    //This is the address of the elf "interpreter", which I don't
2062585SN/A    //think we currently set up. It should be set to 0 (I think)
2072585SN/A    //auxv.push_back(buildAuxVect(SPARC_AT_BASE, 0));
2082585SN/A    //This is the number of headers which were in the original elf
2092585SN/A    //file. This information isn't avaibale by this point.
2102585SN/A    //auxv.push_back(buildAuxVect(SPARC_AT_PHNUM, 3));
2112585SN/A    //This is the size of a program header entry. This isn't easy
2122585SN/A    //to compute here.
2132585SN/A    //auxv.push_back(buildAuxVect(SPARC_AT_PHENT, blah));
2142585SN/A    //This is should be set to load_addr (whatever that is) +
2152585SN/A    //e_phoff. I think it's a pointer to the program headers.
2162585SN/A    //auxv.push_back(buildAuxVect(SPARC_AT_PHDR, blah));
2172585SN/A    //This should be easy to get right, but I won't set it for now
2182585SN/A    //auxv.push_back(buildAuxVect(SPARC_AT_CLKTCK, blah));
2192585SN/A    auxv.push_back(buildAuxVect(SPARC_AT_PAGESZ, SparcISA::VMPageSize));
2202585SN/A    auxv.push_back(buildAuxVect(SPARC_AT_HWCAP, hwcap));
2212585SN/A
2222561SN/A    //Figure out how big the initial stack needs to be
2232561SN/A
2242585SN/A    //Each auxilliary vector is two 8 byte words
2252585SN/A    int aux_data_size = 2 * intSize * auxv.size();
2262561SN/A    int env_data_size = 0;
2272561SN/A    for (int i = 0; i < envp.size(); ++i) {
2282561SN/A        env_data_size += envp[i].size() + 1;
2292561SN/A    }
2302561SN/A    int arg_data_size = 0;
2312561SN/A    for (int i = 0; i < argv.size(); ++i) {
2322561SN/A        arg_data_size += argv[i].size() + 1;
2332561SN/A    }
2342561SN/A
2352561SN/A    int aux_array_size = intSize * 2 * (auxv.size() + 1);
2362561SN/A
2372561SN/A    int argv_array_size = intSize * (argv.size() + 1);
2382561SN/A    int envp_array_size = intSize * (envp.size() + 1);
2392561SN/A
2402561SN/A    int argc_size = intSize;
2412561SN/A    int window_save_size = intSize * 16;
2422561SN/A
2432561SN/A    int info_block_size =
2442561SN/A        (aux_data_size +
2452561SN/A        env_data_size +
2462561SN/A        arg_data_size +
2472561SN/A        ~alignmentMask) & alignmentMask;
2482561SN/A
2492561SN/A    int info_block_padding =
2502561SN/A        info_block_size -
2512561SN/A        aux_data_size -
2522561SN/A        env_data_size -
2532561SN/A        arg_data_size;
2542561SN/A
2552561SN/A    int space_needed =
2562561SN/A        info_block_size +
2572561SN/A        aux_array_size +
2582561SN/A        envp_array_size +
2592561SN/A        argv_array_size +
2602561SN/A        argc_size +
2612561SN/A        window_save_size;
2622561SN/A
2632561SN/A    stack_min = stack_base - space_needed;
2642561SN/A    stack_min &= alignmentMask;
2652561SN/A    stack_size = stack_base - stack_min;
2662561SN/A
2672561SN/A    // map memory
2682561SN/A    pTable->allocate(roundDown(stack_min, pageSize),
2692561SN/A                     roundUp(stack_size, pageSize));
2702561SN/A
2712561SN/A    // map out initial stack contents
2722561SN/A    Addr aux_data_base = stack_base - aux_data_size - info_block_padding;
2732561SN/A    Addr env_data_base = aux_data_base - env_data_size;
2742561SN/A    Addr arg_data_base = env_data_base - arg_data_size;
2752585SN/A    Addr auxv_array_base = arg_data_base - aux_array_size;
2762585SN/A    Addr envp_array_base = auxv_array_base - envp_array_size;
2772561SN/A    Addr argv_array_base = envp_array_base - argv_array_size;
2782561SN/A    Addr argc_base = argv_array_base - argc_size;
2792561SN/A    Addr window_save_base = argc_base - window_save_size;
2802561SN/A
2812561SN/A    DPRINTF(Sparc, "The addresses of items on the initial stack:\n");
2822561SN/A    DPRINTF(Sparc, "0x%x - aux data\n", aux_data_base);
2832561SN/A    DPRINTF(Sparc, "0x%x - env data\n", env_data_base);
2842561SN/A    DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base);
2852585SN/A    DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base);
2862585SN/A    DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base);
2872585SN/A    DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base);
2882561SN/A    DPRINTF(Sparc, "0x%x - argc \n", argc_base);
2892561SN/A    DPRINTF(Sparc, "0x%x - window save\n", window_save_base);
2902561SN/A    DPRINTF(Sparc, "0x%x - stack min\n", stack_min);
2912561SN/A
2922561SN/A    // write contents to stack
2932561SN/A    uint64_t argc = argv.size();
2942585SN/A    uint64_t guestArgc = TheISA::htog(argc);
2952561SN/A
2962585SN/A    //Copy the aux stuff
2972585SN/A    for(int x = 0; x < auxv.size(); x++)
2982585SN/A    {
2992585SN/A        initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
3002585SN/A                (uint8_t*)&(auxv[x].a_type), intSize);
3012585SN/A        initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
3022585SN/A                (uint8_t*)&(auxv[x].a_val), intSize);
3032585SN/A    }
3042585SN/A    //Write out the terminating zeroed auxilliary vector
3052561SN/A    const uint64_t zero = 0;
3062585SN/A    initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
3072585SN/A            (uint8_t*)&zero, 2 * intSize);
3082561SN/A
3092561SN/A    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
3102561SN/A    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
3112561SN/A
3122585SN/A    initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
3132561SN/A
3142561SN/A    execContexts[0]->setIntReg(ArgumentReg0, argc);
3152561SN/A    execContexts[0]->setIntReg(ArgumentReg1, argv_array_base);
3162561SN/A    execContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias);
3172561SN/A
3182561SN/A    Addr prog_entry = objFile->entryPoint();
3192561SN/A    execContexts[0]->setPC(prog_entry);
3202561SN/A    execContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
3212561SN/A    execContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
3222561SN/A
3232561SN/A//    num_processes++;
3242561SN/A}
3252474SN/A
3262474SN/A
3272474SN/ABEGIN_DECLARE_SIM_OBJECT_PARAMS(SparcLiveProcess)
3282474SN/A
3292474SN/A    VectorParam<string> cmd;
3302474SN/A    Param<string> executable;
3312474SN/A    Param<string> input;
3322474SN/A    Param<string> output;
3332474SN/A    VectorParam<string> env;
3342474SN/A    SimObjectParam<System *> system;
3352474SN/A
3362474SN/AEND_DECLARE_SIM_OBJECT_PARAMS(SparcLiveProcess)
3372474SN/A
3382474SN/A
3392474SN/ABEGIN_INIT_SIM_OBJECT_PARAMS(SparcLiveProcess)
3402474SN/A
3412474SN/A    INIT_PARAM(cmd, "command line (executable plus arguments)"),
3422474SN/A    INIT_PARAM(executable, "executable (overrides cmd[0] if set)"),
3432474SN/A    INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"),
3442474SN/A    INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"),
3452474SN/A    INIT_PARAM(env, "environment settings"),
3462474SN/A    INIT_PARAM(system, "system")
3472474SN/A
3482474SN/AEND_INIT_SIM_OBJECT_PARAMS(SparcLiveProcess)
3492474SN/A
3502474SN/A
3512474SN/ACREATE_SIM_OBJECT(SparcLiveProcess)
3522474SN/A{
3532474SN/A    string in = input;
3542474SN/A    string out = output;
3552474SN/A
3562474SN/A    // initialize file descriptors to default: same as simulator
3572474SN/A    int stdin_fd, stdout_fd, stderr_fd;
3582474SN/A
3592474SN/A    if (in == "stdin" || in == "cin")
3602474SN/A        stdin_fd = STDIN_FILENO;
3612474SN/A    else
3622474SN/A        stdin_fd = Process::openInputFile(input);
3632474SN/A
3642474SN/A    if (out == "stdout" || out == "cout")
3652474SN/A        stdout_fd = STDOUT_FILENO;
3662474SN/A    else if (out == "stderr" || out == "cerr")
3672474SN/A        stdout_fd = STDERR_FILENO;
3682474SN/A    else
3692474SN/A        stdout_fd = Process::openOutputFile(out);
3702474SN/A
3712474SN/A    stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO;
3722474SN/A
3732474SN/A    return SparcLiveProcess::create(getInstanceName(), system,
3742474SN/A                               stdin_fd, stdout_fd, stderr_fd,
3752474SN/A                               (string)executable == "" ? cmd[0] : executable,
3762474SN/A                               cmd, env);
3772474SN/A}
3782474SN/A
3792474SN/A
3802474SN/AREGISTER_SIM_OBJECT("SparcLiveProcess", SparcLiveProcess)
3812474SN/A
3822474SN/A
383