process.cc revision 12431
16019Shines@cs.fsu.edu/*
210037SARM gem5 Developers * Copyright (c) 2010, 2012 ARM Limited
37414SAli.Saidi@ARM.com * All rights reserved
47414SAli.Saidi@ARM.com *
57414SAli.Saidi@ARM.com * The license below extends only to copyright in the software and shall
67414SAli.Saidi@ARM.com * not be construed as granting a license to any other intellectual
77414SAli.Saidi@ARM.com * property including but not limited to intellectual property relating
87414SAli.Saidi@ARM.com * to a hardware implementation of the functionality of the software
97414SAli.Saidi@ARM.com * licensed hereunder.  You may use the software subject to the license
107414SAli.Saidi@ARM.com * terms below provided that you ensure that this notice is replicated
117414SAli.Saidi@ARM.com * unmodified and in its entirety in all distributions of the software,
127414SAli.Saidi@ARM.com * modified or unmodified, in source code or in binary form.
137414SAli.Saidi@ARM.com *
146019Shines@cs.fsu.edu * Copyright (c) 2007-2008 The Florida State University
156019Shines@cs.fsu.edu * All rights reserved.
166019Shines@cs.fsu.edu *
176019Shines@cs.fsu.edu * Redistribution and use in source and binary forms, with or without
186019Shines@cs.fsu.edu * modification, are permitted provided that the following conditions are
196019Shines@cs.fsu.edu * met: redistributions of source code must retain the above copyright
206019Shines@cs.fsu.edu * notice, this list of conditions and the following disclaimer;
216019Shines@cs.fsu.edu * redistributions in binary form must reproduce the above copyright
226019Shines@cs.fsu.edu * notice, this list of conditions and the following disclaimer in the
236019Shines@cs.fsu.edu * documentation and/or other materials provided with the distribution;
246019Shines@cs.fsu.edu * neither the name of the copyright holders nor the names of its
256019Shines@cs.fsu.edu * contributors may be used to endorse or promote products derived from
266019Shines@cs.fsu.edu * this software without specific prior written permission.
276019Shines@cs.fsu.edu *
286019Shines@cs.fsu.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
296019Shines@cs.fsu.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
306019Shines@cs.fsu.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
316019Shines@cs.fsu.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
326019Shines@cs.fsu.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
336019Shines@cs.fsu.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
346019Shines@cs.fsu.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
356019Shines@cs.fsu.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
366019Shines@cs.fsu.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
376019Shines@cs.fsu.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
386019Shines@cs.fsu.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
396019Shines@cs.fsu.edu *
406019Shines@cs.fsu.edu * Authors: Stephen Hines
417414SAli.Saidi@ARM.com *          Ali Saidi
426019Shines@cs.fsu.edu */
436019Shines@cs.fsu.edu
4411793Sbrandon.potter@amd.com#include "arch/arm/process.hh"
4511793Sbrandon.potter@amd.com
466019Shines@cs.fsu.edu#include "arch/arm/isa_traits.hh"
476019Shines@cs.fsu.edu#include "arch/arm/types.hh"
486019Shines@cs.fsu.edu#include "base/loader/elf_object.hh"
496019Shines@cs.fsu.edu#include "base/loader/object_file.hh"
5012334Sgabeblack@google.com#include "base/logging.hh"
516019Shines@cs.fsu.edu#include "cpu/thread_context.hh"
528232Snate@binkert.org#include "debug/Stack.hh"
536019Shines@cs.fsu.edu#include "mem/page_table.hh"
5412431Sgabeblack@google.com#include "params/Process.hh"
5511854Sbrandon.potter@amd.com#include "sim/aux_vector.hh"
567678Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
576019Shines@cs.fsu.edu#include "sim/process_impl.hh"
5811800Sbrandon.potter@amd.com#include "sim/syscall_return.hh"
596019Shines@cs.fsu.edu#include "sim/system.hh"
606019Shines@cs.fsu.edu
616019Shines@cs.fsu.eduusing namespace std;
626019Shines@cs.fsu.eduusing namespace ArmISA;
636019Shines@cs.fsu.edu
6411851Sbrandon.potter@amd.comArmProcess::ArmProcess(ProcessParams *params, ObjectFile *objFile,
6511851Sbrandon.potter@amd.com                       ObjectFile::Arch _arch)
6612431Sgabeblack@google.com    : Process(params, new FuncPageTable(params->name, params->pid), objFile),
6712431Sgabeblack@google.com              arch(_arch)
686019Shines@cs.fsu.edu{
6912431Sgabeblack@google.com    fatal_if(!params->useArchPT, "Arch page tables not implemented.");
7010037SARM gem5 Developers}
7110037SARM gem5 Developers
7211851Sbrandon.potter@amd.comArmProcess32::ArmProcess32(ProcessParams *params, ObjectFile *objFile,
7311851Sbrandon.potter@amd.com                           ObjectFile::Arch _arch)
7411851Sbrandon.potter@amd.com    : ArmProcess(params, objFile, _arch)
7510037SARM gem5 Developers{
7611905SBrandon.Potter@amd.com    Addr brk_point = roundUp(objFile->dataBase() + objFile->dataSize() +
7711905SBrandon.Potter@amd.com                             objFile->bssSize(), PageBytes);
7811905SBrandon.Potter@amd.com    Addr stack_base = 0xbf000000L;
7911905SBrandon.Potter@amd.com    Addr max_stack_size = 8 * 1024 * 1024;
8011905SBrandon.Potter@amd.com    Addr next_thread_stack_base = stack_base - max_stack_size;
8111905SBrandon.Potter@amd.com    Addr mmap_end = 0x40000000L;
826019Shines@cs.fsu.edu
8311905SBrandon.Potter@amd.com    memState = make_shared<MemState>(brk_point, stack_base, max_stack_size,
8411905SBrandon.Potter@amd.com                                     next_thread_stack_base, mmap_end);
856019Shines@cs.fsu.edu}
866019Shines@cs.fsu.edu
8711851Sbrandon.potter@amd.comArmProcess64::ArmProcess64(ProcessParams *params, ObjectFile *objFile,
8811851Sbrandon.potter@amd.com                           ObjectFile::Arch _arch)
8911851Sbrandon.potter@amd.com    : ArmProcess(params, objFile, _arch)
9010037SARM gem5 Developers{
9111905SBrandon.Potter@amd.com    Addr brk_point = roundUp(objFile->dataBase() + objFile->dataSize() +
9211905SBrandon.Potter@amd.com                             objFile->bssSize(), PageBytes);
9311905SBrandon.Potter@amd.com    Addr stack_base = 0x7fffff0000L;
9411905SBrandon.Potter@amd.com    Addr max_stack_size = 8 * 1024 * 1024;
9511905SBrandon.Potter@amd.com    Addr next_thread_stack_base = stack_base - max_stack_size;
9611905SBrandon.Potter@amd.com    Addr mmap_end = 0x4000000000L;
9710037SARM gem5 Developers
9811905SBrandon.Potter@amd.com    memState = make_shared<MemState>(brk_point, stack_base, max_stack_size,
9911905SBrandon.Potter@amd.com                                     next_thread_stack_base, mmap_end);
10010037SARM gem5 Developers}
10110037SARM gem5 Developers
1026019Shines@cs.fsu.eduvoid
10311851Sbrandon.potter@amd.comArmProcess32::initState()
1046019Shines@cs.fsu.edu{
10511851Sbrandon.potter@amd.com    Process::initState();
10610318Sandreas.hansson@arm.com    argsInit<uint32_t>(PageBytes, INTREG_SP);
1077640Sgblack@eecs.umich.edu    for (int i = 0; i < contextIds.size(); i++) {
1087640Sgblack@eecs.umich.edu        ThreadContext * tc = system->getThreadContext(contextIds[i]);
1097640Sgblack@eecs.umich.edu        CPACR cpacr = tc->readMiscReg(MISCREG_CPACR);
1107640Sgblack@eecs.umich.edu        // Enable the floating point coprocessors.
1117640Sgblack@eecs.umich.edu        cpacr.cp10 = 0x3;
1127640Sgblack@eecs.umich.edu        cpacr.cp11 = 0x3;
1137640Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CPACR, cpacr);
1147640Sgblack@eecs.umich.edu        // Generically enable floating point support.
1157640Sgblack@eecs.umich.edu        FPEXC fpexc = tc->readMiscReg(MISCREG_FPEXC);
1167640Sgblack@eecs.umich.edu        fpexc.en = 1;
1177640Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_FPEXC, fpexc);
1187640Sgblack@eecs.umich.edu    }
1196019Shines@cs.fsu.edu}
1206019Shines@cs.fsu.edu
1216019Shines@cs.fsu.eduvoid
12211851Sbrandon.potter@amd.comArmProcess64::initState()
1236019Shines@cs.fsu.edu{
12411851Sbrandon.potter@amd.com    Process::initState();
12510318Sandreas.hansson@arm.com    argsInit<uint64_t>(PageBytes, INTREG_SP0);
12610037SARM gem5 Developers    for (int i = 0; i < contextIds.size(); i++) {
12710037SARM gem5 Developers        ThreadContext * tc = system->getThreadContext(contextIds[i]);
12810037SARM gem5 Developers        CPSR cpsr = tc->readMiscReg(MISCREG_CPSR);
12910037SARM gem5 Developers        cpsr.mode = MODE_EL0T;
13010037SARM gem5 Developers        tc->setMiscReg(MISCREG_CPSR, cpsr);
13110037SARM gem5 Developers        CPACR cpacr = tc->readMiscReg(MISCREG_CPACR_EL1);
13210037SARM gem5 Developers        // Enable the floating point coprocessors.
13310037SARM gem5 Developers        cpacr.cp10 = 0x3;
13410037SARM gem5 Developers        cpacr.cp11 = 0x3;
13510037SARM gem5 Developers        tc->setMiscReg(MISCREG_CPACR_EL1, cpacr);
13610037SARM gem5 Developers        // Generically enable floating point support.
13710037SARM gem5 Developers        FPEXC fpexc = tc->readMiscReg(MISCREG_FPEXC);
13810037SARM gem5 Developers        fpexc.en = 1;
13910037SARM gem5 Developers        tc->setMiscReg(MISCREG_FPEXC, fpexc);
14010037SARM gem5 Developers    }
14110037SARM gem5 Developers}
14210037SARM gem5 Developers
14310037SARM gem5 Developerstemplate <class IntType>
14410037SARM gem5 Developersvoid
14511851Sbrandon.potter@amd.comArmProcess::argsInit(int pageSize, IntRegIndex spIndex)
14610037SARM gem5 Developers{
14710037SARM gem5 Developers    int intSize = sizeof(IntType);
14810037SARM gem5 Developers
14910037SARM gem5 Developers    typedef AuxVector<IntType> auxv_t;
1506400Sgblack@eecs.umich.edu    std::vector<auxv_t> auxv;
1516400Sgblack@eecs.umich.edu
1526400Sgblack@eecs.umich.edu    string filename;
1536400Sgblack@eecs.umich.edu    if (argv.size() < 1)
1546400Sgblack@eecs.umich.edu        filename = "";
1556400Sgblack@eecs.umich.edu    else
1566400Sgblack@eecs.umich.edu        filename = argv[0];
1576400Sgblack@eecs.umich.edu
1586400Sgblack@eecs.umich.edu    //We want 16 byte alignment
1596400Sgblack@eecs.umich.edu    uint64_t align = 16;
1606400Sgblack@eecs.umich.edu
16111389Sbrandon.potter@amd.com    // Patch the ld_bias for dynamic executables.
16211389Sbrandon.potter@amd.com    updateBias();
16311389Sbrandon.potter@amd.com
1646019Shines@cs.fsu.edu    // load object file into target memory
1656019Shines@cs.fsu.edu    objFile->loadSections(initVirtMem);
1666019Shines@cs.fsu.edu
1676400Sgblack@eecs.umich.edu    enum ArmCpuFeature {
1686400Sgblack@eecs.umich.edu        Arm_Swp = 1 << 0,
1696400Sgblack@eecs.umich.edu        Arm_Half = 1 << 1,
1706400Sgblack@eecs.umich.edu        Arm_Thumb = 1 << 2,
1716400Sgblack@eecs.umich.edu        Arm_26Bit = 1 << 3,
1726400Sgblack@eecs.umich.edu        Arm_FastMult = 1 << 4,
1736400Sgblack@eecs.umich.edu        Arm_Fpa = 1 << 5,
1746400Sgblack@eecs.umich.edu        Arm_Vfp = 1 << 6,
1756400Sgblack@eecs.umich.edu        Arm_Edsp = 1 << 7,
1766400Sgblack@eecs.umich.edu        Arm_Java = 1 << 8,
1776400Sgblack@eecs.umich.edu        Arm_Iwmmxt = 1 << 9,
1787414SAli.Saidi@ARM.com        Arm_Crunch = 1 << 10,
1797414SAli.Saidi@ARM.com        Arm_ThumbEE = 1 << 11,
1807414SAli.Saidi@ARM.com        Arm_Neon = 1 << 12,
1817414SAli.Saidi@ARM.com        Arm_Vfpv3 = 1 << 13,
1827414SAli.Saidi@ARM.com        Arm_Vfpv3d16 = 1 << 14
1836400Sgblack@eecs.umich.edu    };
1846400Sgblack@eecs.umich.edu
1856400Sgblack@eecs.umich.edu    //Setup the auxilliary vectors. These will already have endian conversion.
1866400Sgblack@eecs.umich.edu    //Auxilliary vectors are loaded only for elf formatted executables.
1876400Sgblack@eecs.umich.edu    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
1886400Sgblack@eecs.umich.edu    if (elfObject) {
1896400Sgblack@eecs.umich.edu
19010810Sbr@bsdpad.com        if (objFile->getOpSys() == ObjectFile::Linux) {
19110810Sbr@bsdpad.com            IntType features =
19210810Sbr@bsdpad.com                Arm_Swp |
19310810Sbr@bsdpad.com                Arm_Half |
19410810Sbr@bsdpad.com                Arm_Thumb |
19510810Sbr@bsdpad.com//                Arm_26Bit |
19610810Sbr@bsdpad.com                Arm_FastMult |
19710810Sbr@bsdpad.com//                Arm_Fpa |
19810810Sbr@bsdpad.com                Arm_Vfp |
19910810Sbr@bsdpad.com                Arm_Edsp |
20010810Sbr@bsdpad.com//                Arm_Java |
20110810Sbr@bsdpad.com//                Arm_Iwmmxt |
20210810Sbr@bsdpad.com//                Arm_Crunch |
20310810Sbr@bsdpad.com                Arm_ThumbEE |
20410810Sbr@bsdpad.com                Arm_Neon |
20510810Sbr@bsdpad.com                Arm_Vfpv3 |
20610810Sbr@bsdpad.com                Arm_Vfpv3d16 |
20710810Sbr@bsdpad.com                0;
20810810Sbr@bsdpad.com
20910810Sbr@bsdpad.com            //Bits which describe the system hardware capabilities
21010810Sbr@bsdpad.com            //XXX Figure out what these should be
21110810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_HWCAP, features));
21210810Sbr@bsdpad.com            //Frequency at which times() increments
21310810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_CLKTCK, 0x64));
21410810Sbr@bsdpad.com            //Whether to enable "secure mode" in the executable
21510810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_SECURE, 0));
21610810Sbr@bsdpad.com            // Pointer to 16 bytes of random data
21710810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_RANDOM, 0));
21810810Sbr@bsdpad.com            //The filename of the program
21910810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_EXECFN, 0));
22010810Sbr@bsdpad.com            //The string "v71" -- ARM v7 architecture
22110810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_PLATFORM, 0));
22210810Sbr@bsdpad.com        }
22310810Sbr@bsdpad.com
2246400Sgblack@eecs.umich.edu        //The system page size
22510318Sandreas.hansson@arm.com        auxv.push_back(auxv_t(M5_AT_PAGESZ, ArmISA::PageBytes));
2266400Sgblack@eecs.umich.edu        // For statically linked executables, this is the virtual address of the
2276400Sgblack@eecs.umich.edu        // program header tables if they appear in the executable image
2286400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
2296400Sgblack@eecs.umich.edu        // This is the size of a program header entry from the elf file.
2306400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
2316400Sgblack@eecs.umich.edu        // This is the number of program headers from the original elf file.
2326400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
23311389Sbrandon.potter@amd.com        // This is the base address of the ELF interpreter; it should be
23411389Sbrandon.potter@amd.com        // zero for static executables or contain the base address for
23511389Sbrandon.potter@amd.com        // dynamic executables.
23611389Sbrandon.potter@amd.com        auxv.push_back(auxv_t(M5_AT_BASE, getBias()));
2376400Sgblack@eecs.umich.edu        //XXX Figure out what this should be.
2386400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
2396400Sgblack@eecs.umich.edu        //The entry point to the program
2406400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
2416400Sgblack@eecs.umich.edu        //Different user and group IDs
2426400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_UID, uid()));
2436400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_EUID, euid()));
2446400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_GID, gid()));
2456400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_EGID, egid()));
2466400Sgblack@eecs.umich.edu    }
2476400Sgblack@eecs.umich.edu
2486400Sgblack@eecs.umich.edu    //Figure out how big the initial stack nedes to be
2496400Sgblack@eecs.umich.edu
2506400Sgblack@eecs.umich.edu    // A sentry NULL void pointer at the top of the stack.
2516400Sgblack@eecs.umich.edu    int sentry_size = intSize;
2526400Sgblack@eecs.umich.edu
2537414SAli.Saidi@ARM.com    string platform = "v71";
2546400Sgblack@eecs.umich.edu    int platform_size = platform.size() + 1;
2556400Sgblack@eecs.umich.edu
2567414SAli.Saidi@ARM.com    // Bytes for AT_RANDOM above, we'll just keep them 0
2577414SAli.Saidi@ARM.com    int aux_random_size = 16; // as per the specification
2587414SAli.Saidi@ARM.com
2596400Sgblack@eecs.umich.edu    // The aux vectors are put on the stack in two groups. The first group are
2606400Sgblack@eecs.umich.edu    // the vectors that are generated as the elf is loaded. The second group
2616400Sgblack@eecs.umich.edu    // are the ones that were computed ahead of time and include the platform
2626400Sgblack@eecs.umich.edu    // string.
2636400Sgblack@eecs.umich.edu    int aux_data_size = filename.size() + 1;
2646400Sgblack@eecs.umich.edu
2656400Sgblack@eecs.umich.edu    int env_data_size = 0;
2666400Sgblack@eecs.umich.edu    for (int i = 0; i < envp.size(); ++i) {
2676400Sgblack@eecs.umich.edu        env_data_size += envp[i].size() + 1;
2686400Sgblack@eecs.umich.edu    }
2696019Shines@cs.fsu.edu    int arg_data_size = 0;
2706019Shines@cs.fsu.edu    for (int i = 0; i < argv.size(); ++i) {
2716019Shines@cs.fsu.edu        arg_data_size += argv[i].size() + 1;
2726019Shines@cs.fsu.edu    }
2736400Sgblack@eecs.umich.edu
2746400Sgblack@eecs.umich.edu    int info_block_size =
2756400Sgblack@eecs.umich.edu        sentry_size + env_data_size + arg_data_size +
2767414SAli.Saidi@ARM.com        aux_data_size + platform_size + aux_random_size;
2776400Sgblack@eecs.umich.edu
2786400Sgblack@eecs.umich.edu    //Each auxilliary vector is two 4 byte words
2796400Sgblack@eecs.umich.edu    int aux_array_size = intSize * 2 * (auxv.size() + 1);
2806400Sgblack@eecs.umich.edu
2816400Sgblack@eecs.umich.edu    int envp_array_size = intSize * (envp.size() + 1);
2826400Sgblack@eecs.umich.edu    int argv_array_size = intSize * (argv.size() + 1);
2836400Sgblack@eecs.umich.edu
2846400Sgblack@eecs.umich.edu    int argc_size = intSize;
2856400Sgblack@eecs.umich.edu
2866400Sgblack@eecs.umich.edu    //Figure out the size of the contents of the actual initial frame
2876400Sgblack@eecs.umich.edu    int frame_size =
2886400Sgblack@eecs.umich.edu        info_block_size +
2896400Sgblack@eecs.umich.edu        aux_array_size +
2906400Sgblack@eecs.umich.edu        envp_array_size +
2916400Sgblack@eecs.umich.edu        argv_array_size +
2926400Sgblack@eecs.umich.edu        argc_size;
2936400Sgblack@eecs.umich.edu
2946400Sgblack@eecs.umich.edu    //There needs to be padding after the auxiliary vector data so that the
2956400Sgblack@eecs.umich.edu    //very bottom of the stack is aligned properly.
2966400Sgblack@eecs.umich.edu    int partial_size = frame_size;
2976400Sgblack@eecs.umich.edu    int aligned_partial_size = roundUp(partial_size, align);
2986400Sgblack@eecs.umich.edu    int aux_padding = aligned_partial_size - partial_size;
2996400Sgblack@eecs.umich.edu
3006400Sgblack@eecs.umich.edu    int space_needed = frame_size + aux_padding;
3016400Sgblack@eecs.umich.edu
30211905SBrandon.Potter@amd.com    memState->setStackMin(memState->getStackBase() - space_needed);
30311905SBrandon.Potter@amd.com    memState->setStackMin(roundDown(memState->getStackMin(), align));
30411905SBrandon.Potter@amd.com    memState->setStackSize(memState->getStackBase() - memState->getStackMin());
3056400Sgblack@eecs.umich.edu
3066400Sgblack@eecs.umich.edu    // map memory
30711905SBrandon.Potter@amd.com    allocateMem(roundDown(memState->getStackMin(), pageSize),
30811905SBrandon.Potter@amd.com                          roundUp(memState->getStackSize(), pageSize));
3096400Sgblack@eecs.umich.edu
3106400Sgblack@eecs.umich.edu    // map out initial stack contents
31111905SBrandon.Potter@amd.com    IntType sentry_base = memState->getStackBase() - sentry_size;
31210037SARM gem5 Developers    IntType aux_data_base = sentry_base - aux_data_size;
31310037SARM gem5 Developers    IntType env_data_base = aux_data_base - env_data_size;
31410037SARM gem5 Developers    IntType arg_data_base = env_data_base - arg_data_size;
31510037SARM gem5 Developers    IntType platform_base = arg_data_base - platform_size;
31610037SARM gem5 Developers    IntType aux_random_base = platform_base - aux_random_size;
31710037SARM gem5 Developers    IntType auxv_array_base = aux_random_base - aux_array_size - aux_padding;
31810037SARM gem5 Developers    IntType envp_array_base = auxv_array_base - envp_array_size;
31910037SARM gem5 Developers    IntType argv_array_base = envp_array_base - argv_array_size;
32010037SARM gem5 Developers    IntType argc_base = argv_array_base - argc_size;
3216400Sgblack@eecs.umich.edu
3226400Sgblack@eecs.umich.edu    DPRINTF(Stack, "The addresses of items on the initial stack:\n");
3236400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
3246400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - env data\n", env_data_base);
3256400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
3267414SAli.Saidi@ARM.com    DPRINTF(Stack, "0x%x - random data\n", aux_random_base);
3276400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - platform base\n", platform_base);
3286400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
3296400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
3306400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
3316400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - argc \n", argc_base);
33211905SBrandon.Potter@amd.com    DPRINTF(Stack, "0x%x - stack min\n", memState->getStackMin());
3336400Sgblack@eecs.umich.edu
3346400Sgblack@eecs.umich.edu    // write contents to stack
3356400Sgblack@eecs.umich.edu
3366400Sgblack@eecs.umich.edu    // figure out argc
33710037SARM gem5 Developers    IntType argc = argv.size();
33810037SARM gem5 Developers    IntType guestArgc = ArmISA::htog(argc);
3396400Sgblack@eecs.umich.edu
3406400Sgblack@eecs.umich.edu    //Write out the sentry void *
34110037SARM gem5 Developers    IntType sentry_NULL = 0;
3428852Sandreas.hansson@arm.com    initVirtMem.writeBlob(sentry_base,
3436400Sgblack@eecs.umich.edu            (uint8_t*)&sentry_NULL, sentry_size);
3446400Sgblack@eecs.umich.edu
3456400Sgblack@eecs.umich.edu    //Fix up the aux vectors which point to other data
3466400Sgblack@eecs.umich.edu    for (int i = auxv.size() - 1; i >= 0; i--) {
3476400Sgblack@eecs.umich.edu        if (auxv[i].a_type == M5_AT_PLATFORM) {
3486400Sgblack@eecs.umich.edu            auxv[i].a_val = platform_base;
3498852Sandreas.hansson@arm.com            initVirtMem.writeString(platform_base, platform.c_str());
3506400Sgblack@eecs.umich.edu        } else if (auxv[i].a_type == M5_AT_EXECFN) {
3516400Sgblack@eecs.umich.edu            auxv[i].a_val = aux_data_base;
3528852Sandreas.hansson@arm.com            initVirtMem.writeString(aux_data_base, filename.c_str());
3537414SAli.Saidi@ARM.com        } else if (auxv[i].a_type == M5_AT_RANDOM) {
3547414SAli.Saidi@ARM.com            auxv[i].a_val = aux_random_base;
3557414SAli.Saidi@ARM.com            // Just leave the value 0, we don't want randomness
3566400Sgblack@eecs.umich.edu        }
3576019Shines@cs.fsu.edu    }
3586019Shines@cs.fsu.edu
3596400Sgblack@eecs.umich.edu    //Copy the aux stuff
36010037SARM gem5 Developers    for (int x = 0; x < auxv.size(); x++) {
3618852Sandreas.hansson@arm.com        initVirtMem.writeBlob(auxv_array_base + x * 2 * intSize,
3626400Sgblack@eecs.umich.edu                (uint8_t*)&(auxv[x].a_type), intSize);
3638852Sandreas.hansson@arm.com        initVirtMem.writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
3646400Sgblack@eecs.umich.edu                (uint8_t*)&(auxv[x].a_val), intSize);
3656400Sgblack@eecs.umich.edu    }
3666400Sgblack@eecs.umich.edu    //Write out the terminating zeroed auxilliary vector
3676400Sgblack@eecs.umich.edu    const uint64_t zero = 0;
3688852Sandreas.hansson@arm.com    initVirtMem.writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
3696400Sgblack@eecs.umich.edu            (uint8_t*)&zero, 2 * intSize);
3706019Shines@cs.fsu.edu
3716400Sgblack@eecs.umich.edu    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
3726400Sgblack@eecs.umich.edu    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
3736019Shines@cs.fsu.edu
3748852Sandreas.hansson@arm.com    initVirtMem.writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
3756019Shines@cs.fsu.edu
3766020Sgblack@eecs.umich.edu    ThreadContext *tc = system->getThreadContext(contextIds[0]);
3776400Sgblack@eecs.umich.edu    //Set the stack pointer register
37811905SBrandon.Potter@amd.com    tc->setIntReg(spIndex, memState->getStackMin());
3796400Sgblack@eecs.umich.edu    //A pointer to a function to run when the program exits. We'll set this
3806400Sgblack@eecs.umich.edu    //to zero explicitly to make sure this isn't used.
3816400Sgblack@eecs.umich.edu    tc->setIntReg(ArgumentReg0, 0);
3826400Sgblack@eecs.umich.edu    //Set argument regs 1 and 2 to argv[0] and envp[0] respectively
3836400Sgblack@eecs.umich.edu    if (argv.size() > 0) {
3846400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg1, arg_data_base + arg_data_size -
3856400Sgblack@eecs.umich.edu                                    argv[argv.size() - 1].size() - 1);
3866400Sgblack@eecs.umich.edu    } else {
3876400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg1, 0);
3886400Sgblack@eecs.umich.edu    }
3896400Sgblack@eecs.umich.edu    if (envp.size() > 0) {
3906400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg2, env_data_base + env_data_size -
3916400Sgblack@eecs.umich.edu                                    envp[envp.size() - 1].size() - 1);
3926400Sgblack@eecs.umich.edu    } else {
3936400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg2, 0);
3946400Sgblack@eecs.umich.edu    }
3956019Shines@cs.fsu.edu
3967720Sgblack@eecs.umich.edu    PCState pc;
3977720Sgblack@eecs.umich.edu    pc.thumb(arch == ObjectFile::Thumb);
3987720Sgblack@eecs.umich.edu    pc.nextThumb(pc.thumb());
39910037SARM gem5 Developers    pc.aarch64(arch == ObjectFile::Arm64);
40010037SARM gem5 Developers    pc.nextAArch64(pc.aarch64());
40111389Sbrandon.potter@amd.com    pc.set(getStartPC() & ~mask(1));
4027720Sgblack@eecs.umich.edu    tc->pcState(pc);
4036400Sgblack@eecs.umich.edu
40411886Sbrandon.potter@amd.com    //Align the "stackMin" to a page boundary.
40511905SBrandon.Potter@amd.com    memState->setStackMin(roundDown(memState->getStackMin(), pageSize));
4066019Shines@cs.fsu.edu}
4076019Shines@cs.fsu.edu
4086020Sgblack@eecs.umich.eduArmISA::IntReg
40911851Sbrandon.potter@amd.comArmProcess32::getSyscallArg(ThreadContext *tc, int &i)
4106020Sgblack@eecs.umich.edu{
4117441SAli.Saidi@ARM.com    assert(i < 6);
4126701Sgblack@eecs.umich.edu    return tc->readIntReg(ArgumentReg0 + i++);
4136020Sgblack@eecs.umich.edu}
4146020Sgblack@eecs.umich.edu
41510037SARM gem5 DevelopersArmISA::IntReg
41611851Sbrandon.potter@amd.comArmProcess64::getSyscallArg(ThreadContext *tc, int &i)
41710037SARM gem5 Developers{
41810037SARM gem5 Developers    assert(i < 8);
41910037SARM gem5 Developers    return tc->readIntReg(ArgumentReg0 + i++);
42010037SARM gem5 Developers}
42110037SARM gem5 Developers
42210037SARM gem5 DevelopersArmISA::IntReg
42311851Sbrandon.potter@amd.comArmProcess32::getSyscallArg(ThreadContext *tc, int &i, int width)
4247441SAli.Saidi@ARM.com{
4257441SAli.Saidi@ARM.com    assert(width == 32 || width == 64);
4267441SAli.Saidi@ARM.com    if (width == 32)
4277441SAli.Saidi@ARM.com        return getSyscallArg(tc, i);
4287441SAli.Saidi@ARM.com
4297441SAli.Saidi@ARM.com    // 64 bit arguments are passed starting in an even register
4307441SAli.Saidi@ARM.com    if (i % 2 != 0)
4317441SAli.Saidi@ARM.com       i++;
4327441SAli.Saidi@ARM.com
4337441SAli.Saidi@ARM.com    // Registers r0-r6 can be used
4347441SAli.Saidi@ARM.com    assert(i < 5);
4357441SAli.Saidi@ARM.com    uint64_t val;
4367441SAli.Saidi@ARM.com    val = tc->readIntReg(ArgumentReg0 + i++);
4377441SAli.Saidi@ARM.com    val |= ((uint64_t)tc->readIntReg(ArgumentReg0 + i++) << 32);
4387441SAli.Saidi@ARM.com    return val;
4397441SAli.Saidi@ARM.com}
4407441SAli.Saidi@ARM.com
44110037SARM gem5 DevelopersArmISA::IntReg
44211851Sbrandon.potter@amd.comArmProcess64::getSyscallArg(ThreadContext *tc, int &i, int width)
44310037SARM gem5 Developers{
44410037SARM gem5 Developers    return getSyscallArg(tc, i);
44510037SARM gem5 Developers}
44610037SARM gem5 Developers
4477441SAli.Saidi@ARM.com
4486020Sgblack@eecs.umich.eduvoid
44911851Sbrandon.potter@amd.comArmProcess32::setSyscallArg(ThreadContext *tc, int i, ArmISA::IntReg val)
4506020Sgblack@eecs.umich.edu{
45110037SARM gem5 Developers    assert(i < 6);
4526020Sgblack@eecs.umich.edu    tc->setIntReg(ArgumentReg0 + i, val);
4536020Sgblack@eecs.umich.edu}
4546020Sgblack@eecs.umich.edu
4556020Sgblack@eecs.umich.eduvoid
45611851Sbrandon.potter@amd.comArmProcess64::setSyscallArg(ThreadContext *tc, int i, ArmISA::IntReg val)
45710037SARM gem5 Developers{
45810037SARM gem5 Developers    assert(i < 8);
45910037SARM gem5 Developers    tc->setIntReg(ArgumentReg0 + i, val);
46010037SARM gem5 Developers}
46110037SARM gem5 Developers
46210037SARM gem5 Developersvoid
46311851Sbrandon.potter@amd.comArmProcess32::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
4646020Sgblack@eecs.umich.edu{
46510810Sbr@bsdpad.com
46610810Sbr@bsdpad.com    if (objFile->getOpSys() == ObjectFile::FreeBSD) {
46710810Sbr@bsdpad.com        // Decode return value
46810810Sbr@bsdpad.com        if (sysret.encodedValue() >= 0)
46910810Sbr@bsdpad.com            // FreeBSD checks the carry bit to determine if syscall is succeeded
47010810Sbr@bsdpad.com            tc->setCCReg(CCREG_C, 0);
47110810Sbr@bsdpad.com        else {
47210810Sbr@bsdpad.com            sysret = -sysret.encodedValue();
47310810Sbr@bsdpad.com        }
47410810Sbr@bsdpad.com    }
47510810Sbr@bsdpad.com
47610223Ssteve.reinhardt@amd.com    tc->setIntReg(ReturnValueReg, sysret.encodedValue());
4776020Sgblack@eecs.umich.edu}
47810037SARM gem5 Developers
47910037SARM gem5 Developersvoid
48011851Sbrandon.potter@amd.comArmProcess64::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
48110037SARM gem5 Developers{
48210810Sbr@bsdpad.com
48310810Sbr@bsdpad.com    if (objFile->getOpSys() == ObjectFile::FreeBSD) {
48410810Sbr@bsdpad.com        // Decode return value
48510810Sbr@bsdpad.com        if (sysret.encodedValue() >= 0)
48610810Sbr@bsdpad.com            // FreeBSD checks the carry bit to determine if syscall is succeeded
48710810Sbr@bsdpad.com            tc->setCCReg(CCREG_C, 0);
48810810Sbr@bsdpad.com        else {
48910810Sbr@bsdpad.com            sysret = -sysret.encodedValue();
49010810Sbr@bsdpad.com        }
49110810Sbr@bsdpad.com    }
49210810Sbr@bsdpad.com
49310223Ssteve.reinhardt@amd.com    tc->setIntReg(ReturnValueReg, sysret.encodedValue());
49410037SARM gem5 Developers}
495