process.cc revision 8229
12207SN/A/*
25254Sksewell@umich.edu * Copyright (c) 2004-2005 The Regents of The University of Michigan
35254Sksewell@umich.edu * All rights reserved.
42207SN/A *
55254Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without
65254Sksewell@umich.edu * modification, are permitted provided that the following conditions are
75254Sksewell@umich.edu * met: redistributions of source code must retain the above copyright
85254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer;
95254Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright
105254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the
115254Sksewell@umich.edu * documentation and/or other materials provided with the distribution;
125254Sksewell@umich.edu * neither the name of the copyright holders nor the names of its
135254Sksewell@umich.edu * contributors may be used to endorse or promote products derived from
145254Sksewell@umich.edu * this software without specific prior written permission.
152207SN/A *
165254Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
175254Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
185254Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
195254Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
205254Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
215254Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
225254Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235254Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245254Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255254Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
265254Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
285254Sksewell@umich.edu * Authors: Gabe Black
295254Sksewell@umich.edu *          Ali Saidi
305254Sksewell@umich.edu *          Korey Sewell
312207SN/A */
322207SN/A
332474SN/A#include "arch/mips/isa_traits.hh"
342207SN/A#include "arch/mips/process.hh"
358229Snate@binkert.org#include "base/loader/elf_object.hh"
362454SN/A#include "base/loader/object_file.hh"
372454SN/A#include "base/misc.hh"
382680Sktlim@umich.edu#include "cpu/thread_context.hh"
396650Sksewell@umich.edu#include "mem/page_table.hh"
406650Sksewell@umich.edu#include "sim/process.hh"
416650Sksewell@umich.edu#include "sim/process_impl.hh"
422474SN/A#include "sim/system.hh"
432207SN/A
442447SN/Ausing namespace std;
452474SN/Ausing namespace MipsISA;
462447SN/A
475154Sgblack@eecs.umich.eduMipsLiveProcess::MipsLiveProcess(LiveProcessParams * params,
485154Sgblack@eecs.umich.edu        ObjectFile *objFile)
495154Sgblack@eecs.umich.edu    : LiveProcess(params, objFile)
502474SN/A{
512686Sksewell@umich.edu    // Set up stack. On MIPS, stack starts at the top of kuseg
522686Sksewell@umich.edu    // user address space. MIPS stack grows down from here
532935Sksewell@umich.edu    stack_base = 0x7FFFFFFF;
542474SN/A
552474SN/A    // Set pointer for next thread stack.  Reserve 8M for main stack.
562474SN/A    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
572474SN/A
582686Sksewell@umich.edu    // Set up break point (Top of Heap)
592686Sksewell@umich.edu    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
602686Sksewell@umich.edu    brk_point = roundUp(brk_point, VMPageSize);
612686Sksewell@umich.edu
626811SMatt DeVuyst    // Set up region for mmaps.  Start it 1GB above the top of the heap.
636811SMatt DeVuyst    mmap_start = mmap_end = brk_point + 0x40000000L;
642474SN/A}
652474SN/A
662474SN/Avoid
677532Ssteve.reinhardt@amd.comMipsLiveProcess::initState()
682474SN/A{
697532Ssteve.reinhardt@amd.com    LiveProcess::initState();
706650Sksewell@umich.edu
716811SMatt DeVuyst    argsInit<uint32_t>(VMPageSize);
722474SN/A}
735958Sgblack@eecs.umich.edu
746811SMatt DeVuysttemplate<class IntType>
756650Sksewell@umich.eduvoid
766811SMatt DeVuystMipsLiveProcess::argsInit(int pageSize)
776650Sksewell@umich.edu{
786811SMatt DeVuyst    int intSize = sizeof(IntType);
796811SMatt DeVuyst
806650Sksewell@umich.edu    // load object file into target memory
816650Sksewell@umich.edu    objFile->loadSections(initVirtMem);
826650Sksewell@umich.edu
836811SMatt DeVuyst    typedef AuxVector<IntType> auxv_t;
846811SMatt DeVuyst    std::vector<auxv_t> auxv;
856811SMatt DeVuyst
866811SMatt DeVuyst    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
876811SMatt DeVuyst    if (elfObject)
886811SMatt DeVuyst    {
896811SMatt DeVuyst        // Set the system page size
906811SMatt DeVuyst        auxv.push_back(auxv_t(M5_AT_PAGESZ, MipsISA::VMPageSize));
916811SMatt DeVuyst        // Set the frequency at which time() increments
926811SMatt DeVuyst        auxv.push_back(auxv_t(M5_AT_CLKTCK, 100));
936811SMatt DeVuyst        // For statically linked executables, this is the virtual
946811SMatt DeVuyst        // address of the program header tables if they appear in the
956811SMatt DeVuyst        // executable image.
966811SMatt DeVuyst        auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
976811SMatt DeVuyst        DPRINTF(Loader, "auxv at PHDR %08p\n", elfObject->programHeaderTable());
986811SMatt DeVuyst        // This is the size of a program header entry from the elf file.
996811SMatt DeVuyst        auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
1006811SMatt DeVuyst        // This is the number of program headers from the original elf file.
1016811SMatt DeVuyst        auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
1026811SMatt DeVuyst        //The entry point to the program
1036811SMatt DeVuyst        auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
1046811SMatt DeVuyst        //Different user and group IDs
1056811SMatt DeVuyst        auxv.push_back(auxv_t(M5_AT_UID, uid()));
1066811SMatt DeVuyst        auxv.push_back(auxv_t(M5_AT_EUID, euid()));
1076811SMatt DeVuyst        auxv.push_back(auxv_t(M5_AT_GID, gid()));
1086811SMatt DeVuyst        auxv.push_back(auxv_t(M5_AT_EGID, egid()));
1096811SMatt DeVuyst    }
1106811SMatt DeVuyst
1116811SMatt DeVuyst    // Calculate how much space we need for arg & env & auxv arrays.
1126650Sksewell@umich.edu    int argv_array_size = intSize * (argv.size() + 1);
1136650Sksewell@umich.edu    int envp_array_size = intSize * (envp.size() + 1);
1146811SMatt DeVuyst    int auxv_array_size = intSize * 2 * (auxv.size() + 1);
1156811SMatt DeVuyst
1166650Sksewell@umich.edu    int arg_data_size = 0;
1176650Sksewell@umich.edu    for (vector<string>::size_type i = 0; i < argv.size(); ++i) {
1186650Sksewell@umich.edu        arg_data_size += argv[i].size() + 1;
1196650Sksewell@umich.edu    }
1206650Sksewell@umich.edu    int env_data_size = 0;
1216650Sksewell@umich.edu    for (vector<string>::size_type i = 0; i < envp.size(); ++i) {
1226650Sksewell@umich.edu        env_data_size += envp[i].size() + 1;
1236650Sksewell@umich.edu    }
1246650Sksewell@umich.edu
1256650Sksewell@umich.edu    int space_needed =
1266811SMatt DeVuyst        argv_array_size +
1276811SMatt DeVuyst        envp_array_size +
1286811SMatt DeVuyst        auxv_array_size +
1296811SMatt DeVuyst        arg_data_size +
1306811SMatt DeVuyst        env_data_size;
1316650Sksewell@umich.edu
1326650Sksewell@umich.edu    // set bottom of stack
1336650Sksewell@umich.edu    stack_min = stack_base - space_needed;
1346650Sksewell@umich.edu    // align it
1356650Sksewell@umich.edu    stack_min = roundDown(stack_min, pageSize);
1366650Sksewell@umich.edu    stack_size = stack_base - stack_min;
1376650Sksewell@umich.edu    // map memory
1386650Sksewell@umich.edu    pTable->allocate(stack_min, roundUp(stack_size, pageSize));
1396650Sksewell@umich.edu
1406650Sksewell@umich.edu    // map out initial stack contents
1416811SMatt DeVuyst    IntType argv_array_base = stack_min + intSize; // room for argc
1426811SMatt DeVuyst    IntType envp_array_base = argv_array_base + argv_array_size;
1436811SMatt DeVuyst    IntType auxv_array_base = envp_array_base + envp_array_size;
1446811SMatt DeVuyst    IntType arg_data_base = auxv_array_base + auxv_array_size;
1456811SMatt DeVuyst    IntType env_data_base = arg_data_base + arg_data_size;
1466650Sksewell@umich.edu
1476650Sksewell@umich.edu    // write contents to stack
1486811SMatt DeVuyst    IntType argc = argv.size();
1496650Sksewell@umich.edu
1506811SMatt DeVuyst    argc = htog((IntType)argc);
1516650Sksewell@umich.edu
1526650Sksewell@umich.edu    initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize);
1536650Sksewell@umich.edu
1546650Sksewell@umich.edu    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
1556650Sksewell@umich.edu
1566650Sksewell@umich.edu    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
1576650Sksewell@umich.edu
1586811SMatt DeVuyst    // Copy the aux vector
1596811SMatt DeVuyst    for (typename vector<auxv_t>::size_type x = 0; x < auxv.size(); x++) {
1606811SMatt DeVuyst        initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
1616811SMatt DeVuyst                (uint8_t*)&(auxv[x].a_type), intSize);
1626811SMatt DeVuyst        initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
1636811SMatt DeVuyst                (uint8_t*)&(auxv[x].a_val), intSize);
1646811SMatt DeVuyst    }
1656811SMatt DeVuyst
1666811SMatt DeVuyst    // Write out the terminating zeroed auxilliary vector
1676811SMatt DeVuyst    for (unsigned i = 0; i < 2; i++) {
1686811SMatt DeVuyst        const IntType zero = 0;
1696811SMatt DeVuyst        const Addr addr = auxv_array_base + 2 * intSize * (auxv.size() + i);
1706811SMatt DeVuyst        initVirtMem->writeBlob(addr, (uint8_t*)&zero, intSize);
1716811SMatt DeVuyst    }
1726811SMatt DeVuyst
1736650Sksewell@umich.edu    ThreadContext *tc = system->getThreadContext(contextIds[0]);
1746650Sksewell@umich.edu
1756650Sksewell@umich.edu    setSyscallArg(tc, 0, argc);
1766650Sksewell@umich.edu    setSyscallArg(tc, 1, argv_array_base);
1776650Sksewell@umich.edu    tc->setIntReg(StackPointerReg, stack_min);
1786650Sksewell@umich.edu
1797720Sgblack@eecs.umich.edu    tc->pcState(objFile->entryPoint());
1806650Sksewell@umich.edu}
1816650Sksewell@umich.edu
1826650Sksewell@umich.edu
1835958Sgblack@eecs.umich.eduMipsISA::IntReg
1846701Sgblack@eecs.umich.eduMipsLiveProcess::getSyscallArg(ThreadContext *tc, int &i)
1855958Sgblack@eecs.umich.edu{
1865958Sgblack@eecs.umich.edu    assert(i < 6);
1876701Sgblack@eecs.umich.edu    return tc->readIntReg(FirstArgumentReg + i++);
1885958Sgblack@eecs.umich.edu}
1895958Sgblack@eecs.umich.edu
1905958Sgblack@eecs.umich.eduvoid
1915958Sgblack@eecs.umich.eduMipsLiveProcess::setSyscallArg(ThreadContext *tc,
1925958Sgblack@eecs.umich.edu        int i, MipsISA::IntReg val)
1935958Sgblack@eecs.umich.edu{
1945958Sgblack@eecs.umich.edu    assert(i < 6);
1955958Sgblack@eecs.umich.edu    tc->setIntReg(FirstArgumentReg + i, val);
1965958Sgblack@eecs.umich.edu}
1975958Sgblack@eecs.umich.edu
1985958Sgblack@eecs.umich.eduvoid
1995958Sgblack@eecs.umich.eduMipsLiveProcess::setSyscallReturn(ThreadContext *tc,
2005958Sgblack@eecs.umich.edu        SyscallReturn return_value)
2015958Sgblack@eecs.umich.edu{
2025958Sgblack@eecs.umich.edu    if (return_value.successful()) {
2035958Sgblack@eecs.umich.edu        // no error
2045958Sgblack@eecs.umich.edu        tc->setIntReg(SyscallSuccessReg, 0);
2055958Sgblack@eecs.umich.edu        tc->setIntReg(ReturnValueReg, return_value.value());
2065958Sgblack@eecs.umich.edu    } else {
2075958Sgblack@eecs.umich.edu        // got an error, return details
2085958Sgblack@eecs.umich.edu        tc->setIntReg(SyscallSuccessReg, (IntReg) -1);
2095958Sgblack@eecs.umich.edu        tc->setIntReg(ReturnValueReg, -return_value.value());
2105958Sgblack@eecs.umich.edu    }
2115958Sgblack@eecs.umich.edu}
212