process.cc revision 6701
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"
356650Sksewell@umich.edu
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
406650Sksewell@umich.edu#include "mem/page_table.hh"
416650Sksewell@umich.edu
426650Sksewell@umich.edu#include "sim/process.hh"
436650Sksewell@umich.edu#include "sim/process_impl.hh"
442474SN/A#include "sim/system.hh"
452207SN/A
462447SN/Ausing namespace std;
472474SN/Ausing namespace MipsISA;
482447SN/A
495154Sgblack@eecs.umich.eduMipsLiveProcess::MipsLiveProcess(LiveProcessParams * params,
505154Sgblack@eecs.umich.edu        ObjectFile *objFile)
515154Sgblack@eecs.umich.edu    : LiveProcess(params, objFile)
522474SN/A{
532686Sksewell@umich.edu    // Set up stack. On MIPS, stack starts at the top of kuseg
542686Sksewell@umich.edu    // user address space. MIPS stack grows down from here
552935Sksewell@umich.edu    stack_base = 0x7FFFFFFF;
562474SN/A
572474SN/A    // Set pointer for next thread stack.  Reserve 8M for main stack.
582474SN/A    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
592474SN/A
602686Sksewell@umich.edu    // Set up break point (Top of Heap)
612686Sksewell@umich.edu    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
622686Sksewell@umich.edu    brk_point = roundUp(brk_point, VMPageSize);
632686Sksewell@umich.edu
642686Sksewell@umich.edu    // Set up region for mmaps. For now, start at bottom of kuseg space.
652686Sksewell@umich.edu    mmap_start = mmap_end = 0x10000;
662474SN/A}
672474SN/A
682474SN/Avoid
692474SN/AMipsLiveProcess::startup()
702474SN/A{
716650Sksewell@umich.edu    Process::startup();
726650Sksewell@umich.edu
732474SN/A    argsInit(MachineBytes, VMPageSize);
742474SN/A}
755958Sgblack@eecs.umich.edu
766650Sksewell@umich.eduvoid
776650Sksewell@umich.eduMipsLiveProcess::argsInit(int intSize, int pageSize)
786650Sksewell@umich.edu{
796650Sksewell@umich.edu    // load object file into target memory
806650Sksewell@umich.edu    objFile->loadSections(initVirtMem);
816650Sksewell@umich.edu
826650Sksewell@umich.edu    // Calculate how much space we need for arg & env arrays.
836650Sksewell@umich.edu    int argv_array_size = intSize * (argv.size() + 1);
846650Sksewell@umich.edu    int envp_array_size = intSize * (envp.size() + 1);
856650Sksewell@umich.edu    int arg_data_size = 0;
866650Sksewell@umich.edu    for (vector<string>::size_type i = 0; i < argv.size(); ++i) {
876650Sksewell@umich.edu        arg_data_size += argv[i].size() + 1;
886650Sksewell@umich.edu    }
896650Sksewell@umich.edu    int env_data_size = 0;
906650Sksewell@umich.edu    for (vector<string>::size_type i = 0; i < envp.size(); ++i) {
916650Sksewell@umich.edu        env_data_size += envp[i].size() + 1;
926650Sksewell@umich.edu    }
936650Sksewell@umich.edu
946650Sksewell@umich.edu    int space_needed =
956650Sksewell@umich.edu         argv_array_size + envp_array_size + arg_data_size + env_data_size;
966650Sksewell@umich.edu    if (space_needed < 32*1024)
976650Sksewell@umich.edu        space_needed = 32*1024;
986650Sksewell@umich.edu
996650Sksewell@umich.edu    // set bottom of stack
1006650Sksewell@umich.edu    stack_min = stack_base - space_needed;
1016650Sksewell@umich.edu    // align it
1026650Sksewell@umich.edu    stack_min = roundDown(stack_min, pageSize);
1036650Sksewell@umich.edu    stack_size = stack_base - stack_min;
1046650Sksewell@umich.edu    // map memory
1056650Sksewell@umich.edu    pTable->allocate(stack_min, roundUp(stack_size, pageSize));
1066650Sksewell@umich.edu
1076650Sksewell@umich.edu    // map out initial stack contents
1086650Sksewell@umich.edu    // ========
1096650Sksewell@umich.edu    // NOTE: Using uint32_t hardcodes MIPS32 and not MIPS64
1106650Sksewell@umich.edu    // even if MIPS64 was intended. This is because the
1116650Sksewell@umich.edu    // copyStringArray function templates on the parameters.
1126650Sksewell@umich.edu    // Elegant way to check intSize and vary between 32/64?
1136650Sksewell@umich.edu    // ========
1146650Sksewell@umich.edu    uint32_t argv_array_base = stack_min + intSize; // room for argc
1156650Sksewell@umich.edu    uint32_t envp_array_base = argv_array_base + argv_array_size;
1166650Sksewell@umich.edu    uint32_t arg_data_base = envp_array_base + envp_array_size;
1176650Sksewell@umich.edu    uint32_t env_data_base = arg_data_base + arg_data_size;
1186650Sksewell@umich.edu
1196650Sksewell@umich.edu    // write contents to stack
1206650Sksewell@umich.edu    uint32_t argc = argv.size();
1216650Sksewell@umich.edu
1226650Sksewell@umich.edu    if (intSize == 8)
1236650Sksewell@umich.edu        argc = htog((uint64_t)argc);
1246650Sksewell@umich.edu    else if (intSize == 4)
1256650Sksewell@umich.edu        argc = htog((uint32_t)argc);
1266650Sksewell@umich.edu    else
1276650Sksewell@umich.edu        panic("Unknown int size");
1286650Sksewell@umich.edu
1296650Sksewell@umich.edu
1306650Sksewell@umich.edu    initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize);
1316650Sksewell@umich.edu
1326650Sksewell@umich.edu    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
1336650Sksewell@umich.edu
1346650Sksewell@umich.edu    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
1356650Sksewell@umich.edu
1366650Sksewell@umich.edu    ThreadContext *tc = system->getThreadContext(contextIds[0]);
1376650Sksewell@umich.edu
1386650Sksewell@umich.edu    setSyscallArg(tc, 0, argc);
1396650Sksewell@umich.edu    setSyscallArg(tc, 1, argv_array_base);
1406650Sksewell@umich.edu    tc->setIntReg(StackPointerReg, stack_min);
1416650Sksewell@umich.edu
1426650Sksewell@umich.edu    Addr prog_entry = objFile->entryPoint();
1436650Sksewell@umich.edu    tc->setPC(prog_entry);
1446650Sksewell@umich.edu    tc->setNextPC(prog_entry + sizeof(MachInst));
1456650Sksewell@umich.edu    tc->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
1466650Sksewell@umich.edu}
1476650Sksewell@umich.edu
1486650Sksewell@umich.edu
1495958Sgblack@eecs.umich.eduMipsISA::IntReg
1506701Sgblack@eecs.umich.eduMipsLiveProcess::getSyscallArg(ThreadContext *tc, int &i)
1515958Sgblack@eecs.umich.edu{
1525958Sgblack@eecs.umich.edu    assert(i < 6);
1536701Sgblack@eecs.umich.edu    return tc->readIntReg(FirstArgumentReg + i++);
1545958Sgblack@eecs.umich.edu}
1555958Sgblack@eecs.umich.edu
1565958Sgblack@eecs.umich.eduvoid
1575958Sgblack@eecs.umich.eduMipsLiveProcess::setSyscallArg(ThreadContext *tc,
1585958Sgblack@eecs.umich.edu        int i, MipsISA::IntReg val)
1595958Sgblack@eecs.umich.edu{
1605958Sgblack@eecs.umich.edu    assert(i < 6);
1615958Sgblack@eecs.umich.edu    tc->setIntReg(FirstArgumentReg + i, val);
1625958Sgblack@eecs.umich.edu}
1635958Sgblack@eecs.umich.edu
1645958Sgblack@eecs.umich.eduvoid
1655958Sgblack@eecs.umich.eduMipsLiveProcess::setSyscallReturn(ThreadContext *tc,
1665958Sgblack@eecs.umich.edu        SyscallReturn return_value)
1675958Sgblack@eecs.umich.edu{
1685958Sgblack@eecs.umich.edu    if (return_value.successful()) {
1695958Sgblack@eecs.umich.edu        // no error
1705958Sgblack@eecs.umich.edu        tc->setIntReg(SyscallSuccessReg, 0);
1715958Sgblack@eecs.umich.edu        tc->setIntReg(ReturnValueReg, return_value.value());
1725958Sgblack@eecs.umich.edu    } else {
1735958Sgblack@eecs.umich.edu        // got an error, return details
1745958Sgblack@eecs.umich.edu        tc->setIntReg(SyscallSuccessReg, (IntReg) -1);
1755958Sgblack@eecs.umich.edu        tc->setIntReg(ReturnValueReg, -return_value.value());
1765958Sgblack@eecs.umich.edu    }
1775958Sgblack@eecs.umich.edu}
178