process.cc revision 12186
12SN/A/*
21762SN/A * Copyright (c) 2003-2004 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665SN/A *
282665SN/A * Authors: Gabe Black
292SN/A *          Ali Saidi
302SN/A */
312SN/A
322SN/A#include "arch/alpha/process.hh"
332SN/A
342SN/A#include "arch/alpha/isa_traits.hh"
3511263Sandreas.sandberg@arm.com#include "base/loader/elf_object.hh"
3611263Sandreas.sandberg@arm.com#include "base/loader/object_file.hh"
372SN/A#include "base/misc.hh"
382SN/A#include "cpu/thread_context.hh"
392SN/A#include "debug/Loader.hh"
402SN/A#include "mem/page_table.hh"
414981SN/A#include "sim/aux_vector.hh"
4212056Sgabeblack@google.com#include "sim/byteswap.hh"
4311263Sandreas.sandberg@arm.com#include "sim/process_impl.hh"
4411263Sandreas.sandberg@arm.com#include "sim/syscall_return.hh"
4511263Sandreas.sandberg@arm.com#include "sim/system.hh"
4612056Sgabeblack@google.com
4712056Sgabeblack@google.comusing namespace AlphaISA;
4812056Sgabeblack@google.comusing namespace std;
4912056Sgabeblack@google.com
5012056Sgabeblack@google.comAlphaProcess::AlphaProcess(ProcessParams *params, ObjectFile *objFile)
5112056Sgabeblack@google.com    : Process(params, objFile)
5212054Sgabeblack@google.com{
5356SN/A    Addr brk_point = objFile->dataBase() + objFile->dataSize() +
5456SN/A                     objFile->bssSize();
552SN/A    brk_point = roundUp(brk_point, PageBytes);
561872SN/A
5712055Sgabeblack@google.com    // Set up stack.  On Alpha, stack goes below text section.  This
5812055Sgabeblack@google.com    // code should get moved to some architecture-specific spot.
5912055Sgabeblack@google.com    Addr stack_base = objFile->textBase() - (409600+4096);
6012055Sgabeblack@google.com
6112055Sgabeblack@google.com    // Set up region for mmaps.
6212055Sgabeblack@google.com    Addr mmap_end = 0x10000;
6312055Sgabeblack@google.com
6412055Sgabeblack@google.com    Addr max_stack_size = 8 * 1024 * 1024;
6512055Sgabeblack@google.com
6612055Sgabeblack@google.com    // Set pointer for next thread stack.  Reserve 8M for main stack.
6712055Sgabeblack@google.com    Addr next_thread_stack_base = stack_base - max_stack_size;
6812055Sgabeblack@google.com
6912055Sgabeblack@google.com    memState = make_shared<MemState>(brk_point, stack_base, max_stack_size,
7012055Sgabeblack@google.com                                     next_thread_stack_base, mmap_end);
7112055Sgabeblack@google.com}
7212055Sgabeblack@google.com
7312055Sgabeblack@google.comvoid
7412055Sgabeblack@google.comAlphaProcess::argsInit(int intSize, int pageSize)
7512055Sgabeblack@google.com{
7612055Sgabeblack@google.com    // Patch the ld_bias for dynamic executables.
7712055Sgabeblack@google.com    updateBias();
7812055Sgabeblack@google.com
7912055Sgabeblack@google.com    objFile->loadSections(initVirtMem);
8012055Sgabeblack@google.com
8112055Sgabeblack@google.com    typedef AuxVector<uint64_t> auxv_t;
8212055Sgabeblack@google.com    std::vector<auxv_t>  auxv;
8312055Sgabeblack@google.com
8412055Sgabeblack@google.com    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
8512055Sgabeblack@google.com    if (elfObject)
8612055Sgabeblack@google.com    {
8712055Sgabeblack@google.com        // modern glibc uses a bunch of auxiliary vectors to set up
8812055Sgabeblack@google.com        // TLS as well as do a bunch of other stuff
8912055Sgabeblack@google.com        // these vectors go on the bottom of the stack, below argc/argv/envp
9012055Sgabeblack@google.com        // pointers but above actual arg strings
9112055Sgabeblack@google.com        // I don't have all the ones glibc looks at here, but so far it doesn't
9212055Sgabeblack@google.com        // seem to be a problem.
9312055Sgabeblack@google.com        // check out _dl_aux_init() in glibc/elf/dl-support.c for details
9412055Sgabeblack@google.com        // --Lisa
9512055Sgabeblack@google.com        auxv.push_back(auxv_t(M5_AT_PAGESZ, AlphaISA::PageBytes));
9612055Sgabeblack@google.com        auxv.push_back(auxv_t(M5_AT_CLKTCK, 100));
9712055Sgabeblack@google.com        auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
9812055Sgabeblack@google.com        DPRINTF(Loader, "auxv at PHDR %08p\n", elfObject->programHeaderTable());
9912055Sgabeblack@google.com        auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
10012055Sgabeblack@google.com        // This is the base address of the ELF interpreter; it should be
10112055Sgabeblack@google.com        // zero for static executables or contain the base address for
10212055Sgabeblack@google.com        // dynamic executables.
10312055Sgabeblack@google.com        auxv.push_back(auxv_t(M5_AT_BASE, getBias()));
10412055Sgabeblack@google.com        auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
10512055Sgabeblack@google.com        auxv.push_back(auxv_t(M5_AT_UID, uid()));
10612055Sgabeblack@google.com        auxv.push_back(auxv_t(M5_AT_EUID, euid()));
10712055Sgabeblack@google.com        auxv.push_back(auxv_t(M5_AT_GID, gid()));
10812055Sgabeblack@google.com        auxv.push_back(auxv_t(M5_AT_EGID, egid()));
10912055Sgabeblack@google.com
11012055Sgabeblack@google.com    }
11112055Sgabeblack@google.com
11212055Sgabeblack@google.com    // Calculate how much space we need for arg & env & auxv arrays.
11312055Sgabeblack@google.com    int argv_array_size = intSize * (argv.size() + 1);
11412055Sgabeblack@google.com    int envp_array_size = intSize * (envp.size() + 1);
11512055Sgabeblack@google.com    int auxv_array_size = intSize * 2 * (auxv.size() + 1);
11612055Sgabeblack@google.com
11712055Sgabeblack@google.com    int arg_data_size = 0;
11812055Sgabeblack@google.com    for (vector<string>::size_type i = 0; i < argv.size(); ++i) {
11912055Sgabeblack@google.com        arg_data_size += argv[i].size() + 1;
12012055Sgabeblack@google.com    }
12112055Sgabeblack@google.com    int env_data_size = 0;
12212055Sgabeblack@google.com    for (vector<string>::size_type i = 0; i < envp.size(); ++i) {
12312055Sgabeblack@google.com        env_data_size += envp[i].size() + 1;
12412055Sgabeblack@google.com    }
12512055Sgabeblack@google.com
12612055Sgabeblack@google.com    int space_needed =
12712055Sgabeblack@google.com        argv_array_size +
12812055Sgabeblack@google.com        envp_array_size +
12912055Sgabeblack@google.com        auxv_array_size +
13012055Sgabeblack@google.com        arg_data_size +
13112055Sgabeblack@google.com        env_data_size;
13212055Sgabeblack@google.com
13312055Sgabeblack@google.com    if (space_needed < 32*1024)
13412055Sgabeblack@google.com        space_needed = 32*1024;
13512055Sgabeblack@google.com
13612055Sgabeblack@google.com    // set bottom of stack
13712055Sgabeblack@google.com    memState->setStackMin(memState->getStackBase() - space_needed);
13812055Sgabeblack@google.com    // align it
13912055Sgabeblack@google.com    memState->setStackMin(roundDown(memState->getStackMin(), pageSize));
14012055Sgabeblack@google.com    memState->setStackSize(memState->getStackBase() - memState->getStackMin());
14112055Sgabeblack@google.com    // map memory
14212055Sgabeblack@google.com    allocateMem(memState->getStackMin(), roundUp(memState->getStackSize(),
14312055Sgabeblack@google.com                pageSize));
1441872SN/A
1451872SN/A    // map out initial stack contents
1462SN/A    Addr argv_array_base = memState->getStackMin() + intSize; // room for argc
14712054Sgabeblack@google.com    Addr envp_array_base = argv_array_base + argv_array_size;
14812054Sgabeblack@google.com    Addr auxv_array_base = envp_array_base + envp_array_size;
14912054Sgabeblack@google.com    Addr arg_data_base = auxv_array_base + auxv_array_size;
15012054Sgabeblack@google.com    Addr env_data_base = arg_data_base + arg_data_size;
1512SN/A
15212055Sgabeblack@google.com    // write contents to stack
1532SN/A    uint64_t argc = argv.size();
1542SN/A    if (intSize == 8)
15512054Sgabeblack@google.com        argc = htog((uint64_t)argc);
15612054Sgabeblack@google.com    else if (intSize == 4)
15712055Sgabeblack@google.com        argc = htog((uint32_t)argc);
1582SN/A    else
1594981SN/A        panic("Unknown int size");
1604981SN/A
1614981SN/A    initVirtMem.writeBlob(memState->getStackMin(), (uint8_t*)&argc, intSize);
1624981SN/A
1634981SN/A    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
1644981SN/A    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
16511168SN/A
16611168SN/A    //Copy the aux stuff
16712055Sgabeblack@google.com    for (vector<auxv_t>::size_type x = 0; x < auxv.size(); x++) {
16812055Sgabeblack@google.com        initVirtMem.writeBlob(auxv_array_base + x * 2 * intSize,
16912055Sgabeblack@google.com                (uint8_t*)&(auxv[x].a_type), intSize);
17012055Sgabeblack@google.com        initVirtMem.writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
17112055Sgabeblack@google.com                (uint8_t*)&(auxv[x].a_val), intSize);
17212055Sgabeblack@google.com    }
17312055Sgabeblack@google.com
17412055Sgabeblack@google.com    ThreadContext *tc = system->getThreadContext(contextIds[0]);
17512055Sgabeblack@google.com
17612055Sgabeblack@google.com    setSyscallArg(tc, 0, argc);
17712055Sgabeblack@google.com    setSyscallArg(tc, 1, argv_array_base);
17812055Sgabeblack@google.com    tc->setIntReg(StackPointerReg, memState->getStackMin());
17912055Sgabeblack@google.com
18012055Sgabeblack@google.com    tc->pcState(getStartPC());
18112055Sgabeblack@google.com}
18212055Sgabeblack@google.com
1832SN/Avoid
1842SN/AAlphaProcess::setupASNReg()
1854981SN/A{
18612056Sgabeblack@google.com    ThreadContext *tc = system->getThreadContext(contextIds[0]);
18712056Sgabeblack@google.com    tc->setMiscRegNoEffect(IPR_DTB_ASN, _pid << 57);
18812056Sgabeblack@google.com}
18912056Sgabeblack@google.com
19012056Sgabeblack@google.com
19112056Sgabeblack@google.comvoid
19212056Sgabeblack@google.comAlphaProcess::unserialize(CheckpointIn &cp)
19312056Sgabeblack@google.com{
19412056Sgabeblack@google.com    Process::unserialize(cp);
19512056Sgabeblack@google.com    // need to set up ASN after unserialization since _pid value may
19612056Sgabeblack@google.com    // come from checkpoint
19712056Sgabeblack@google.com    setupASNReg();
19812056Sgabeblack@google.com}
19912056Sgabeblack@google.com
20012056Sgabeblack@google.com
20112056Sgabeblack@google.comvoid
20212056Sgabeblack@google.comAlphaProcess::initState()
20312056Sgabeblack@google.com{
20412056Sgabeblack@google.com    // need to set up ASN before further initialization since init
20512056Sgabeblack@google.com    // will involve writing to virtual memory addresses
20612056Sgabeblack@google.com    setupASNReg();
20712056Sgabeblack@google.com
20812056Sgabeblack@google.com    Process::initState();
20912056Sgabeblack@google.com
21011263Sandreas.sandberg@arm.com    argsInit(MachineBytes, PageBytes);
211
212    ThreadContext *tc = system->getThreadContext(contextIds[0]);
213    tc->setIntReg(GlobalPointerReg, objFile->globalPointer());
214    //Operate in user mode
215    tc->setMiscRegNoEffect(IPR_ICM, mode_user << 3);
216    tc->setMiscRegNoEffect(IPR_DTB_CM, mode_user << 3);
217    //No super page mapping
218    tc->setMiscRegNoEffect(IPR_MCSR, 0);
219}
220
221AlphaISA::IntReg
222AlphaProcess::getSyscallArg(ThreadContext *tc, int &i)
223{
224    assert(i < 6);
225    return tc->readIntReg(FirstArgumentReg + i++);
226}
227
228void
229AlphaProcess::setSyscallArg(ThreadContext *tc, int i, AlphaISA::IntReg val)
230{
231    assert(i < 6);
232    tc->setIntReg(FirstArgumentReg + i, val);
233}
234
235void
236AlphaProcess::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
237{
238    // check for error condition.  Alpha syscall convention is to
239    // indicate success/failure in reg a3 (r19) and put the
240    // return value itself in the standard return value reg (v0).
241    if (sysret.successful()) {
242        // no error
243        tc->setIntReg(SyscallSuccessReg, 0);
244        tc->setIntReg(ReturnValueReg, sysret.returnValue());
245    } else {
246        // got an error, return details
247        tc->setIntReg(SyscallSuccessReg, (IntReg)-1);
248        tc->setIntReg(ReturnValueReg, sysret.errnoValue());
249    }
250}
251