process.cc revision 11886
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"
506019Shines@cs.fsu.edu#include "base/misc.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"
5411854Sbrandon.potter@amd.com#include "sim/aux_vector.hh"
557678Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
566019Shines@cs.fsu.edu#include "sim/process_impl.hh"
5711800Sbrandon.potter@amd.com#include "sim/syscall_return.hh"
586019Shines@cs.fsu.edu#include "sim/system.hh"
596019Shines@cs.fsu.edu
606019Shines@cs.fsu.eduusing namespace std;
616019Shines@cs.fsu.eduusing namespace ArmISA;
626019Shines@cs.fsu.edu
6311851Sbrandon.potter@amd.comArmProcess::ArmProcess(ProcessParams *params, ObjectFile *objFile,
6411851Sbrandon.potter@amd.com                       ObjectFile::Arch _arch)
6511851Sbrandon.potter@amd.com    : Process(params, objFile), arch(_arch)
666019Shines@cs.fsu.edu{
6710037SARM gem5 Developers}
6810037SARM gem5 Developers
6911851Sbrandon.potter@amd.comArmProcess32::ArmProcess32(ProcessParams *params, ObjectFile *objFile,
7011851Sbrandon.potter@amd.com                           ObjectFile::Arch _arch)
7111851Sbrandon.potter@amd.com    : ArmProcess(params, objFile, _arch)
7210037SARM gem5 Developers{
7311886Sbrandon.potter@amd.com    memState->stackBase = 0xbf000000L;
746019Shines@cs.fsu.edu
756019Shines@cs.fsu.edu    // Set pointer for next thread stack.  Reserve 8M for main stack.
7611886Sbrandon.potter@amd.com    memState->nextThreadStackBase = memState->stackBase - (8 * 1024 * 1024);
776019Shines@cs.fsu.edu
786019Shines@cs.fsu.edu    // Set up break point (Top of Heap)
7911886Sbrandon.potter@amd.com    memState->brkPoint = objFile->dataBase() + objFile->dataSize() +
8011886Sbrandon.potter@amd.com                         objFile->bssSize();
8111886Sbrandon.potter@amd.com    memState->brkPoint = roundUp(memState->brkPoint, PageBytes);
826019Shines@cs.fsu.edu
836019Shines@cs.fsu.edu    // Set up region for mmaps. For now, start at bottom of kuseg space.
8411886Sbrandon.potter@amd.com    memState->mmapEnd = 0x40000000L;
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{
9111886Sbrandon.potter@amd.com    memState->stackBase = 0x7fffff0000L;
9210037SARM gem5 Developers
9310037SARM gem5 Developers    // Set pointer for next thread stack.  Reserve 8M for main stack.
9411886Sbrandon.potter@amd.com    memState->nextThreadStackBase = memState->stackBase - (8 * 1024 * 1024);
9510037SARM gem5 Developers
9610037SARM gem5 Developers    // Set up break point (Top of Heap)
9711886Sbrandon.potter@amd.com    memState->brkPoint = objFile->dataBase() + objFile->dataSize() +
9811886Sbrandon.potter@amd.com                         objFile->bssSize();
9911886Sbrandon.potter@amd.com    memState->brkPoint = roundUp(memState->brkPoint, PageBytes);
10010037SARM gem5 Developers
10110037SARM gem5 Developers    // Set up region for mmaps. For now, start at bottom of kuseg space.
10211886Sbrandon.potter@amd.com    memState->mmapEnd = 0x4000000000L;
10310037SARM gem5 Developers}
10410037SARM gem5 Developers
1056019Shines@cs.fsu.eduvoid
10611851Sbrandon.potter@amd.comArmProcess32::initState()
1076019Shines@cs.fsu.edu{
10811851Sbrandon.potter@amd.com    Process::initState();
10910318Sandreas.hansson@arm.com    argsInit<uint32_t>(PageBytes, INTREG_SP);
1107640Sgblack@eecs.umich.edu    for (int i = 0; i < contextIds.size(); i++) {
1117640Sgblack@eecs.umich.edu        ThreadContext * tc = system->getThreadContext(contextIds[i]);
1127640Sgblack@eecs.umich.edu        CPACR cpacr = tc->readMiscReg(MISCREG_CPACR);
1137640Sgblack@eecs.umich.edu        // Enable the floating point coprocessors.
1147640Sgblack@eecs.umich.edu        cpacr.cp10 = 0x3;
1157640Sgblack@eecs.umich.edu        cpacr.cp11 = 0x3;
1167640Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CPACR, cpacr);
1177640Sgblack@eecs.umich.edu        // Generically enable floating point support.
1187640Sgblack@eecs.umich.edu        FPEXC fpexc = tc->readMiscReg(MISCREG_FPEXC);
1197640Sgblack@eecs.umich.edu        fpexc.en = 1;
1207640Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_FPEXC, fpexc);
1217640Sgblack@eecs.umich.edu    }
1226019Shines@cs.fsu.edu}
1236019Shines@cs.fsu.edu
1246019Shines@cs.fsu.eduvoid
12511851Sbrandon.potter@amd.comArmProcess64::initState()
1266019Shines@cs.fsu.edu{
12711851Sbrandon.potter@amd.com    Process::initState();
12810318Sandreas.hansson@arm.com    argsInit<uint64_t>(PageBytes, INTREG_SP0);
12910037SARM gem5 Developers    for (int i = 0; i < contextIds.size(); i++) {
13010037SARM gem5 Developers        ThreadContext * tc = system->getThreadContext(contextIds[i]);
13110037SARM gem5 Developers        CPSR cpsr = tc->readMiscReg(MISCREG_CPSR);
13210037SARM gem5 Developers        cpsr.mode = MODE_EL0T;
13310037SARM gem5 Developers        tc->setMiscReg(MISCREG_CPSR, cpsr);
13410037SARM gem5 Developers        CPACR cpacr = tc->readMiscReg(MISCREG_CPACR_EL1);
13510037SARM gem5 Developers        // Enable the floating point coprocessors.
13610037SARM gem5 Developers        cpacr.cp10 = 0x3;
13710037SARM gem5 Developers        cpacr.cp11 = 0x3;
13810037SARM gem5 Developers        tc->setMiscReg(MISCREG_CPACR_EL1, cpacr);
13910037SARM gem5 Developers        // Generically enable floating point support.
14010037SARM gem5 Developers        FPEXC fpexc = tc->readMiscReg(MISCREG_FPEXC);
14110037SARM gem5 Developers        fpexc.en = 1;
14210037SARM gem5 Developers        tc->setMiscReg(MISCREG_FPEXC, fpexc);
14310037SARM gem5 Developers    }
14410037SARM gem5 Developers}
14510037SARM gem5 Developers
14610037SARM gem5 Developerstemplate <class IntType>
14710037SARM gem5 Developersvoid
14811851Sbrandon.potter@amd.comArmProcess::argsInit(int pageSize, IntRegIndex spIndex)
14910037SARM gem5 Developers{
15010037SARM gem5 Developers    int intSize = sizeof(IntType);
15110037SARM gem5 Developers
15210037SARM gem5 Developers    typedef AuxVector<IntType> auxv_t;
1536400Sgblack@eecs.umich.edu    std::vector<auxv_t> auxv;
1546400Sgblack@eecs.umich.edu
1556400Sgblack@eecs.umich.edu    string filename;
1566400Sgblack@eecs.umich.edu    if (argv.size() < 1)
1576400Sgblack@eecs.umich.edu        filename = "";
1586400Sgblack@eecs.umich.edu    else
1596400Sgblack@eecs.umich.edu        filename = argv[0];
1606400Sgblack@eecs.umich.edu
1616400Sgblack@eecs.umich.edu    //We want 16 byte alignment
1626400Sgblack@eecs.umich.edu    uint64_t align = 16;
1636400Sgblack@eecs.umich.edu
16411389Sbrandon.potter@amd.com    // Patch the ld_bias for dynamic executables.
16511389Sbrandon.potter@amd.com    updateBias();
16611389Sbrandon.potter@amd.com
1676019Shines@cs.fsu.edu    // load object file into target memory
1686019Shines@cs.fsu.edu    objFile->loadSections(initVirtMem);
1696019Shines@cs.fsu.edu
1706400Sgblack@eecs.umich.edu    enum ArmCpuFeature {
1716400Sgblack@eecs.umich.edu        Arm_Swp = 1 << 0,
1726400Sgblack@eecs.umich.edu        Arm_Half = 1 << 1,
1736400Sgblack@eecs.umich.edu        Arm_Thumb = 1 << 2,
1746400Sgblack@eecs.umich.edu        Arm_26Bit = 1 << 3,
1756400Sgblack@eecs.umich.edu        Arm_FastMult = 1 << 4,
1766400Sgblack@eecs.umich.edu        Arm_Fpa = 1 << 5,
1776400Sgblack@eecs.umich.edu        Arm_Vfp = 1 << 6,
1786400Sgblack@eecs.umich.edu        Arm_Edsp = 1 << 7,
1796400Sgblack@eecs.umich.edu        Arm_Java = 1 << 8,
1806400Sgblack@eecs.umich.edu        Arm_Iwmmxt = 1 << 9,
1817414SAli.Saidi@ARM.com        Arm_Crunch = 1 << 10,
1827414SAli.Saidi@ARM.com        Arm_ThumbEE = 1 << 11,
1837414SAli.Saidi@ARM.com        Arm_Neon = 1 << 12,
1847414SAli.Saidi@ARM.com        Arm_Vfpv3 = 1 << 13,
1857414SAli.Saidi@ARM.com        Arm_Vfpv3d16 = 1 << 14
1866400Sgblack@eecs.umich.edu    };
1876400Sgblack@eecs.umich.edu
1886400Sgblack@eecs.umich.edu    //Setup the auxilliary vectors. These will already have endian conversion.
1896400Sgblack@eecs.umich.edu    //Auxilliary vectors are loaded only for elf formatted executables.
1906400Sgblack@eecs.umich.edu    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
1916400Sgblack@eecs.umich.edu    if (elfObject) {
1926400Sgblack@eecs.umich.edu
19310810Sbr@bsdpad.com        if (objFile->getOpSys() == ObjectFile::Linux) {
19410810Sbr@bsdpad.com            IntType features =
19510810Sbr@bsdpad.com                Arm_Swp |
19610810Sbr@bsdpad.com                Arm_Half |
19710810Sbr@bsdpad.com                Arm_Thumb |
19810810Sbr@bsdpad.com//                Arm_26Bit |
19910810Sbr@bsdpad.com                Arm_FastMult |
20010810Sbr@bsdpad.com//                Arm_Fpa |
20110810Sbr@bsdpad.com                Arm_Vfp |
20210810Sbr@bsdpad.com                Arm_Edsp |
20310810Sbr@bsdpad.com//                Arm_Java |
20410810Sbr@bsdpad.com//                Arm_Iwmmxt |
20510810Sbr@bsdpad.com//                Arm_Crunch |
20610810Sbr@bsdpad.com                Arm_ThumbEE |
20710810Sbr@bsdpad.com                Arm_Neon |
20810810Sbr@bsdpad.com                Arm_Vfpv3 |
20910810Sbr@bsdpad.com                Arm_Vfpv3d16 |
21010810Sbr@bsdpad.com                0;
21110810Sbr@bsdpad.com
21210810Sbr@bsdpad.com            //Bits which describe the system hardware capabilities
21310810Sbr@bsdpad.com            //XXX Figure out what these should be
21410810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_HWCAP, features));
21510810Sbr@bsdpad.com            //Frequency at which times() increments
21610810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_CLKTCK, 0x64));
21710810Sbr@bsdpad.com            //Whether to enable "secure mode" in the executable
21810810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_SECURE, 0));
21910810Sbr@bsdpad.com            // Pointer to 16 bytes of random data
22010810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_RANDOM, 0));
22110810Sbr@bsdpad.com            //The filename of the program
22210810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_EXECFN, 0));
22310810Sbr@bsdpad.com            //The string "v71" -- ARM v7 architecture
22410810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_PLATFORM, 0));
22510810Sbr@bsdpad.com        }
22610810Sbr@bsdpad.com
2276400Sgblack@eecs.umich.edu        //The system page size
22810318Sandreas.hansson@arm.com        auxv.push_back(auxv_t(M5_AT_PAGESZ, ArmISA::PageBytes));
2296400Sgblack@eecs.umich.edu        // For statically linked executables, this is the virtual address of the
2306400Sgblack@eecs.umich.edu        // program header tables if they appear in the executable image
2316400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
2326400Sgblack@eecs.umich.edu        // This is the size of a program header entry from the elf file.
2336400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
2346400Sgblack@eecs.umich.edu        // This is the number of program headers from the original elf file.
2356400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
23611389Sbrandon.potter@amd.com        // This is the base address of the ELF interpreter; it should be
23711389Sbrandon.potter@amd.com        // zero for static executables or contain the base address for
23811389Sbrandon.potter@amd.com        // dynamic executables.
23911389Sbrandon.potter@amd.com        auxv.push_back(auxv_t(M5_AT_BASE, getBias()));
2406400Sgblack@eecs.umich.edu        //XXX Figure out what this should be.
2416400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
2426400Sgblack@eecs.umich.edu        //The entry point to the program
2436400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
2446400Sgblack@eecs.umich.edu        //Different user and group IDs
2456400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_UID, uid()));
2466400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_EUID, euid()));
2476400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_GID, gid()));
2486400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_EGID, egid()));
2496400Sgblack@eecs.umich.edu    }
2506400Sgblack@eecs.umich.edu
2516400Sgblack@eecs.umich.edu    //Figure out how big the initial stack nedes to be
2526400Sgblack@eecs.umich.edu
2536400Sgblack@eecs.umich.edu    // A sentry NULL void pointer at the top of the stack.
2546400Sgblack@eecs.umich.edu    int sentry_size = intSize;
2556400Sgblack@eecs.umich.edu
2567414SAli.Saidi@ARM.com    string platform = "v71";
2576400Sgblack@eecs.umich.edu    int platform_size = platform.size() + 1;
2586400Sgblack@eecs.umich.edu
2597414SAli.Saidi@ARM.com    // Bytes for AT_RANDOM above, we'll just keep them 0
2607414SAli.Saidi@ARM.com    int aux_random_size = 16; // as per the specification
2617414SAli.Saidi@ARM.com
2626400Sgblack@eecs.umich.edu    // The aux vectors are put on the stack in two groups. The first group are
2636400Sgblack@eecs.umich.edu    // the vectors that are generated as the elf is loaded. The second group
2646400Sgblack@eecs.umich.edu    // are the ones that were computed ahead of time and include the platform
2656400Sgblack@eecs.umich.edu    // string.
2666400Sgblack@eecs.umich.edu    int aux_data_size = filename.size() + 1;
2676400Sgblack@eecs.umich.edu
2686400Sgblack@eecs.umich.edu    int env_data_size = 0;
2696400Sgblack@eecs.umich.edu    for (int i = 0; i < envp.size(); ++i) {
2706400Sgblack@eecs.umich.edu        env_data_size += envp[i].size() + 1;
2716400Sgblack@eecs.umich.edu    }
2726019Shines@cs.fsu.edu    int arg_data_size = 0;
2736019Shines@cs.fsu.edu    for (int i = 0; i < argv.size(); ++i) {
2746019Shines@cs.fsu.edu        arg_data_size += argv[i].size() + 1;
2756019Shines@cs.fsu.edu    }
2766400Sgblack@eecs.umich.edu
2776400Sgblack@eecs.umich.edu    int info_block_size =
2786400Sgblack@eecs.umich.edu        sentry_size + env_data_size + arg_data_size +
2797414SAli.Saidi@ARM.com        aux_data_size + platform_size + aux_random_size;
2806400Sgblack@eecs.umich.edu
2816400Sgblack@eecs.umich.edu    //Each auxilliary vector is two 4 byte words
2826400Sgblack@eecs.umich.edu    int aux_array_size = intSize * 2 * (auxv.size() + 1);
2836400Sgblack@eecs.umich.edu
2846400Sgblack@eecs.umich.edu    int envp_array_size = intSize * (envp.size() + 1);
2856400Sgblack@eecs.umich.edu    int argv_array_size = intSize * (argv.size() + 1);
2866400Sgblack@eecs.umich.edu
2876400Sgblack@eecs.umich.edu    int argc_size = intSize;
2886400Sgblack@eecs.umich.edu
2896400Sgblack@eecs.umich.edu    //Figure out the size of the contents of the actual initial frame
2906400Sgblack@eecs.umich.edu    int frame_size =
2916400Sgblack@eecs.umich.edu        info_block_size +
2926400Sgblack@eecs.umich.edu        aux_array_size +
2936400Sgblack@eecs.umich.edu        envp_array_size +
2946400Sgblack@eecs.umich.edu        argv_array_size +
2956400Sgblack@eecs.umich.edu        argc_size;
2966400Sgblack@eecs.umich.edu
2976400Sgblack@eecs.umich.edu    //There needs to be padding after the auxiliary vector data so that the
2986400Sgblack@eecs.umich.edu    //very bottom of the stack is aligned properly.
2996400Sgblack@eecs.umich.edu    int partial_size = frame_size;
3006400Sgblack@eecs.umich.edu    int aligned_partial_size = roundUp(partial_size, align);
3016400Sgblack@eecs.umich.edu    int aux_padding = aligned_partial_size - partial_size;
3026400Sgblack@eecs.umich.edu
3036400Sgblack@eecs.umich.edu    int space_needed = frame_size + aux_padding;
3046400Sgblack@eecs.umich.edu
30511886Sbrandon.potter@amd.com    memState->stackMin = memState->stackBase - space_needed;
30611886Sbrandon.potter@amd.com    memState->stackMin = roundDown(memState->stackMin, align);
30711886Sbrandon.potter@amd.com    memState->stackSize = memState->stackBase - memState->stackMin;
3086400Sgblack@eecs.umich.edu
3096400Sgblack@eecs.umich.edu    // map memory
31011886Sbrandon.potter@amd.com    allocateMem(roundDown(memState->stackMin, pageSize),
31111886Sbrandon.potter@amd.com                          roundUp(memState->stackSize, pageSize));
3126400Sgblack@eecs.umich.edu
3136400Sgblack@eecs.umich.edu    // map out initial stack contents
31411886Sbrandon.potter@amd.com    IntType sentry_base = memState->stackBase - sentry_size;
31510037SARM gem5 Developers    IntType aux_data_base = sentry_base - aux_data_size;
31610037SARM gem5 Developers    IntType env_data_base = aux_data_base - env_data_size;
31710037SARM gem5 Developers    IntType arg_data_base = env_data_base - arg_data_size;
31810037SARM gem5 Developers    IntType platform_base = arg_data_base - platform_size;
31910037SARM gem5 Developers    IntType aux_random_base = platform_base - aux_random_size;
32010037SARM gem5 Developers    IntType auxv_array_base = aux_random_base - aux_array_size - aux_padding;
32110037SARM gem5 Developers    IntType envp_array_base = auxv_array_base - envp_array_size;
32210037SARM gem5 Developers    IntType argv_array_base = envp_array_base - argv_array_size;
32310037SARM gem5 Developers    IntType argc_base = argv_array_base - argc_size;
3246400Sgblack@eecs.umich.edu
3256400Sgblack@eecs.umich.edu    DPRINTF(Stack, "The addresses of items on the initial stack:\n");
3266400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
3276400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - env data\n", env_data_base);
3286400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
3297414SAli.Saidi@ARM.com    DPRINTF(Stack, "0x%x - random data\n", aux_random_base);
3306400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - platform base\n", platform_base);
3316400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
3326400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
3336400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
3346400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - argc \n", argc_base);
33511886Sbrandon.potter@amd.com    DPRINTF(Stack, "0x%x - stack min\n", memState->stackMin);
3366400Sgblack@eecs.umich.edu
3376400Sgblack@eecs.umich.edu    // write contents to stack
3386400Sgblack@eecs.umich.edu
3396400Sgblack@eecs.umich.edu    // figure out argc
34010037SARM gem5 Developers    IntType argc = argv.size();
34110037SARM gem5 Developers    IntType guestArgc = ArmISA::htog(argc);
3426400Sgblack@eecs.umich.edu
3436400Sgblack@eecs.umich.edu    //Write out the sentry void *
34410037SARM gem5 Developers    IntType sentry_NULL = 0;
3458852Sandreas.hansson@arm.com    initVirtMem.writeBlob(sentry_base,
3466400Sgblack@eecs.umich.edu            (uint8_t*)&sentry_NULL, sentry_size);
3476400Sgblack@eecs.umich.edu
3486400Sgblack@eecs.umich.edu    //Fix up the aux vectors which point to other data
3496400Sgblack@eecs.umich.edu    for (int i = auxv.size() - 1; i >= 0; i--) {
3506400Sgblack@eecs.umich.edu        if (auxv[i].a_type == M5_AT_PLATFORM) {
3516400Sgblack@eecs.umich.edu            auxv[i].a_val = platform_base;
3528852Sandreas.hansson@arm.com            initVirtMem.writeString(platform_base, platform.c_str());
3536400Sgblack@eecs.umich.edu        } else if (auxv[i].a_type == M5_AT_EXECFN) {
3546400Sgblack@eecs.umich.edu            auxv[i].a_val = aux_data_base;
3558852Sandreas.hansson@arm.com            initVirtMem.writeString(aux_data_base, filename.c_str());
3567414SAli.Saidi@ARM.com        } else if (auxv[i].a_type == M5_AT_RANDOM) {
3577414SAli.Saidi@ARM.com            auxv[i].a_val = aux_random_base;
3587414SAli.Saidi@ARM.com            // Just leave the value 0, we don't want randomness
3596400Sgblack@eecs.umich.edu        }
3606019Shines@cs.fsu.edu    }
3616019Shines@cs.fsu.edu
3626400Sgblack@eecs.umich.edu    //Copy the aux stuff
36310037SARM gem5 Developers    for (int x = 0; x < auxv.size(); x++) {
3648852Sandreas.hansson@arm.com        initVirtMem.writeBlob(auxv_array_base + x * 2 * intSize,
3656400Sgblack@eecs.umich.edu                (uint8_t*)&(auxv[x].a_type), intSize);
3668852Sandreas.hansson@arm.com        initVirtMem.writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
3676400Sgblack@eecs.umich.edu                (uint8_t*)&(auxv[x].a_val), intSize);
3686400Sgblack@eecs.umich.edu    }
3696400Sgblack@eecs.umich.edu    //Write out the terminating zeroed auxilliary vector
3706400Sgblack@eecs.umich.edu    const uint64_t zero = 0;
3718852Sandreas.hansson@arm.com    initVirtMem.writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
3726400Sgblack@eecs.umich.edu            (uint8_t*)&zero, 2 * intSize);
3736019Shines@cs.fsu.edu
3746400Sgblack@eecs.umich.edu    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
3756400Sgblack@eecs.umich.edu    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
3766019Shines@cs.fsu.edu
3778852Sandreas.hansson@arm.com    initVirtMem.writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
3786019Shines@cs.fsu.edu
3796020Sgblack@eecs.umich.edu    ThreadContext *tc = system->getThreadContext(contextIds[0]);
3806400Sgblack@eecs.umich.edu    //Set the stack pointer register
38111886Sbrandon.potter@amd.com    tc->setIntReg(spIndex, memState->stackMin);
3826400Sgblack@eecs.umich.edu    //A pointer to a function to run when the program exits. We'll set this
3836400Sgblack@eecs.umich.edu    //to zero explicitly to make sure this isn't used.
3846400Sgblack@eecs.umich.edu    tc->setIntReg(ArgumentReg0, 0);
3856400Sgblack@eecs.umich.edu    //Set argument regs 1 and 2 to argv[0] and envp[0] respectively
3866400Sgblack@eecs.umich.edu    if (argv.size() > 0) {
3876400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg1, arg_data_base + arg_data_size -
3886400Sgblack@eecs.umich.edu                                    argv[argv.size() - 1].size() - 1);
3896400Sgblack@eecs.umich.edu    } else {
3906400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg1, 0);
3916400Sgblack@eecs.umich.edu    }
3926400Sgblack@eecs.umich.edu    if (envp.size() > 0) {
3936400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg2, env_data_base + env_data_size -
3946400Sgblack@eecs.umich.edu                                    envp[envp.size() - 1].size() - 1);
3956400Sgblack@eecs.umich.edu    } else {
3966400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg2, 0);
3976400Sgblack@eecs.umich.edu    }
3986019Shines@cs.fsu.edu
3997720Sgblack@eecs.umich.edu    PCState pc;
4007720Sgblack@eecs.umich.edu    pc.thumb(arch == ObjectFile::Thumb);
4017720Sgblack@eecs.umich.edu    pc.nextThumb(pc.thumb());
40210037SARM gem5 Developers    pc.aarch64(arch == ObjectFile::Arm64);
40310037SARM gem5 Developers    pc.nextAArch64(pc.aarch64());
40411389Sbrandon.potter@amd.com    pc.set(getStartPC() & ~mask(1));
4057720Sgblack@eecs.umich.edu    tc->pcState(pc);
4066400Sgblack@eecs.umich.edu
40711886Sbrandon.potter@amd.com    //Align the "stackMin" to a page boundary.
40811886Sbrandon.potter@amd.com    memState->stackMin = roundDown(memState->stackMin, pageSize);
4096019Shines@cs.fsu.edu}
4106019Shines@cs.fsu.edu
4116020Sgblack@eecs.umich.eduArmISA::IntReg
41211851Sbrandon.potter@amd.comArmProcess32::getSyscallArg(ThreadContext *tc, int &i)
4136020Sgblack@eecs.umich.edu{
4147441SAli.Saidi@ARM.com    assert(i < 6);
4156701Sgblack@eecs.umich.edu    return tc->readIntReg(ArgumentReg0 + i++);
4166020Sgblack@eecs.umich.edu}
4176020Sgblack@eecs.umich.edu
41810037SARM gem5 DevelopersArmISA::IntReg
41911851Sbrandon.potter@amd.comArmProcess64::getSyscallArg(ThreadContext *tc, int &i)
42010037SARM gem5 Developers{
42110037SARM gem5 Developers    assert(i < 8);
42210037SARM gem5 Developers    return tc->readIntReg(ArgumentReg0 + i++);
42310037SARM gem5 Developers}
42410037SARM gem5 Developers
42510037SARM gem5 DevelopersArmISA::IntReg
42611851Sbrandon.potter@amd.comArmProcess32::getSyscallArg(ThreadContext *tc, int &i, int width)
4277441SAli.Saidi@ARM.com{
4287441SAli.Saidi@ARM.com    assert(width == 32 || width == 64);
4297441SAli.Saidi@ARM.com    if (width == 32)
4307441SAli.Saidi@ARM.com        return getSyscallArg(tc, i);
4317441SAli.Saidi@ARM.com
4327441SAli.Saidi@ARM.com    // 64 bit arguments are passed starting in an even register
4337441SAli.Saidi@ARM.com    if (i % 2 != 0)
4347441SAli.Saidi@ARM.com       i++;
4357441SAli.Saidi@ARM.com
4367441SAli.Saidi@ARM.com    // Registers r0-r6 can be used
4377441SAli.Saidi@ARM.com    assert(i < 5);
4387441SAli.Saidi@ARM.com    uint64_t val;
4397441SAli.Saidi@ARM.com    val = tc->readIntReg(ArgumentReg0 + i++);
4407441SAli.Saidi@ARM.com    val |= ((uint64_t)tc->readIntReg(ArgumentReg0 + i++) << 32);
4417441SAli.Saidi@ARM.com    return val;
4427441SAli.Saidi@ARM.com}
4437441SAli.Saidi@ARM.com
44410037SARM gem5 DevelopersArmISA::IntReg
44511851Sbrandon.potter@amd.comArmProcess64::getSyscallArg(ThreadContext *tc, int &i, int width)
44610037SARM gem5 Developers{
44710037SARM gem5 Developers    return getSyscallArg(tc, i);
44810037SARM gem5 Developers}
44910037SARM gem5 Developers
4507441SAli.Saidi@ARM.com
4516020Sgblack@eecs.umich.eduvoid
45211851Sbrandon.potter@amd.comArmProcess32::setSyscallArg(ThreadContext *tc, int i, ArmISA::IntReg val)
4536020Sgblack@eecs.umich.edu{
45410037SARM gem5 Developers    assert(i < 6);
4556020Sgblack@eecs.umich.edu    tc->setIntReg(ArgumentReg0 + i, val);
4566020Sgblack@eecs.umich.edu}
4576020Sgblack@eecs.umich.edu
4586020Sgblack@eecs.umich.eduvoid
45911851Sbrandon.potter@amd.comArmProcess64::setSyscallArg(ThreadContext *tc, int i, ArmISA::IntReg val)
46010037SARM gem5 Developers{
46110037SARM gem5 Developers    assert(i < 8);
46210037SARM gem5 Developers    tc->setIntReg(ArgumentReg0 + i, val);
46310037SARM gem5 Developers}
46410037SARM gem5 Developers
46510037SARM gem5 Developersvoid
46611851Sbrandon.potter@amd.comArmProcess32::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
4676020Sgblack@eecs.umich.edu{
46810810Sbr@bsdpad.com
46910810Sbr@bsdpad.com    if (objFile->getOpSys() == ObjectFile::FreeBSD) {
47010810Sbr@bsdpad.com        // Decode return value
47110810Sbr@bsdpad.com        if (sysret.encodedValue() >= 0)
47210810Sbr@bsdpad.com            // FreeBSD checks the carry bit to determine if syscall is succeeded
47310810Sbr@bsdpad.com            tc->setCCReg(CCREG_C, 0);
47410810Sbr@bsdpad.com        else {
47510810Sbr@bsdpad.com            sysret = -sysret.encodedValue();
47610810Sbr@bsdpad.com        }
47710810Sbr@bsdpad.com    }
47810810Sbr@bsdpad.com
47910223Ssteve.reinhardt@amd.com    tc->setIntReg(ReturnValueReg, sysret.encodedValue());
4806020Sgblack@eecs.umich.edu}
48110037SARM gem5 Developers
48210037SARM gem5 Developersvoid
48311851Sbrandon.potter@amd.comArmProcess64::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
48410037SARM gem5 Developers{
48510810Sbr@bsdpad.com
48610810Sbr@bsdpad.com    if (objFile->getOpSys() == ObjectFile::FreeBSD) {
48710810Sbr@bsdpad.com        // Decode return value
48810810Sbr@bsdpad.com        if (sysret.encodedValue() >= 0)
48910810Sbr@bsdpad.com            // FreeBSD checks the carry bit to determine if syscall is succeeded
49010810Sbr@bsdpad.com            tc->setCCReg(CCREG_C, 0);
49110810Sbr@bsdpad.com        else {
49210810Sbr@bsdpad.com            sysret = -sysret.encodedValue();
49310810Sbr@bsdpad.com        }
49410810Sbr@bsdpad.com    }
49510810Sbr@bsdpad.com
49610223Ssteve.reinhardt@amd.com    tc->setIntReg(ReturnValueReg, sysret.encodedValue());
49710037SARM gem5 Developers}
498