process.cc revision 12448
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)
6612448Sgabeblack@google.com    : Process(params,
6712448Sgabeblack@google.com              new EmulationPageTable(params->name, params->pid, PageBytes),
6812432Sgabeblack@google.com              objFile),
6912448Sgabeblack@google.com      arch(_arch)
706019Shines@cs.fsu.edu{
7112441Sgabeblack@google.com    fatal_if(params->useArchPT, "Arch page tables not implemented.");
7210037SARM gem5 Developers}
7310037SARM gem5 Developers
7411851Sbrandon.potter@amd.comArmProcess32::ArmProcess32(ProcessParams *params, ObjectFile *objFile,
7511851Sbrandon.potter@amd.com                           ObjectFile::Arch _arch)
7611851Sbrandon.potter@amd.com    : ArmProcess(params, objFile, _arch)
7710037SARM gem5 Developers{
7811905SBrandon.Potter@amd.com    Addr brk_point = roundUp(objFile->dataBase() + objFile->dataSize() +
7911905SBrandon.Potter@amd.com                             objFile->bssSize(), PageBytes);
8011905SBrandon.Potter@amd.com    Addr stack_base = 0xbf000000L;
8111905SBrandon.Potter@amd.com    Addr max_stack_size = 8 * 1024 * 1024;
8211905SBrandon.Potter@amd.com    Addr next_thread_stack_base = stack_base - max_stack_size;
8311905SBrandon.Potter@amd.com    Addr mmap_end = 0x40000000L;
846019Shines@cs.fsu.edu
8511905SBrandon.Potter@amd.com    memState = make_shared<MemState>(brk_point, stack_base, max_stack_size,
8611905SBrandon.Potter@amd.com                                     next_thread_stack_base, mmap_end);
876019Shines@cs.fsu.edu}
886019Shines@cs.fsu.edu
8911851Sbrandon.potter@amd.comArmProcess64::ArmProcess64(ProcessParams *params, ObjectFile *objFile,
9011851Sbrandon.potter@amd.com                           ObjectFile::Arch _arch)
9111851Sbrandon.potter@amd.com    : ArmProcess(params, objFile, _arch)
9210037SARM gem5 Developers{
9311905SBrandon.Potter@amd.com    Addr brk_point = roundUp(objFile->dataBase() + objFile->dataSize() +
9411905SBrandon.Potter@amd.com                             objFile->bssSize(), PageBytes);
9511905SBrandon.Potter@amd.com    Addr stack_base = 0x7fffff0000L;
9611905SBrandon.Potter@amd.com    Addr max_stack_size = 8 * 1024 * 1024;
9711905SBrandon.Potter@amd.com    Addr next_thread_stack_base = stack_base - max_stack_size;
9811905SBrandon.Potter@amd.com    Addr mmap_end = 0x4000000000L;
9910037SARM gem5 Developers
10011905SBrandon.Potter@amd.com    memState = make_shared<MemState>(brk_point, stack_base, max_stack_size,
10111905SBrandon.Potter@amd.com                                     next_thread_stack_base, mmap_end);
10210037SARM gem5 Developers}
10310037SARM gem5 Developers
1046019Shines@cs.fsu.eduvoid
10511851Sbrandon.potter@amd.comArmProcess32::initState()
1066019Shines@cs.fsu.edu{
10711851Sbrandon.potter@amd.com    Process::initState();
10810318Sandreas.hansson@arm.com    argsInit<uint32_t>(PageBytes, INTREG_SP);
1097640Sgblack@eecs.umich.edu    for (int i = 0; i < contextIds.size(); i++) {
1107640Sgblack@eecs.umich.edu        ThreadContext * tc = system->getThreadContext(contextIds[i]);
1117640Sgblack@eecs.umich.edu        CPACR cpacr = tc->readMiscReg(MISCREG_CPACR);
1127640Sgblack@eecs.umich.edu        // Enable the floating point coprocessors.
1137640Sgblack@eecs.umich.edu        cpacr.cp10 = 0x3;
1147640Sgblack@eecs.umich.edu        cpacr.cp11 = 0x3;
1157640Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CPACR, cpacr);
1167640Sgblack@eecs.umich.edu        // Generically enable floating point support.
1177640Sgblack@eecs.umich.edu        FPEXC fpexc = tc->readMiscReg(MISCREG_FPEXC);
1187640Sgblack@eecs.umich.edu        fpexc.en = 1;
1197640Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_FPEXC, fpexc);
1207640Sgblack@eecs.umich.edu    }
1216019Shines@cs.fsu.edu}
1226019Shines@cs.fsu.edu
1236019Shines@cs.fsu.eduvoid
12411851Sbrandon.potter@amd.comArmProcess64::initState()
1256019Shines@cs.fsu.edu{
12611851Sbrandon.potter@amd.com    Process::initState();
12710318Sandreas.hansson@arm.com    argsInit<uint64_t>(PageBytes, INTREG_SP0);
12810037SARM gem5 Developers    for (int i = 0; i < contextIds.size(); i++) {
12910037SARM gem5 Developers        ThreadContext * tc = system->getThreadContext(contextIds[i]);
13010037SARM gem5 Developers        CPSR cpsr = tc->readMiscReg(MISCREG_CPSR);
13110037SARM gem5 Developers        cpsr.mode = MODE_EL0T;
13210037SARM gem5 Developers        tc->setMiscReg(MISCREG_CPSR, cpsr);
13310037SARM gem5 Developers        CPACR cpacr = tc->readMiscReg(MISCREG_CPACR_EL1);
13410037SARM gem5 Developers        // Enable the floating point coprocessors.
13510037SARM gem5 Developers        cpacr.cp10 = 0x3;
13610037SARM gem5 Developers        cpacr.cp11 = 0x3;
13710037SARM gem5 Developers        tc->setMiscReg(MISCREG_CPACR_EL1, cpacr);
13810037SARM gem5 Developers        // Generically enable floating point support.
13910037SARM gem5 Developers        FPEXC fpexc = tc->readMiscReg(MISCREG_FPEXC);
14010037SARM gem5 Developers        fpexc.en = 1;
14110037SARM gem5 Developers        tc->setMiscReg(MISCREG_FPEXC, fpexc);
14210037SARM gem5 Developers    }
14310037SARM gem5 Developers}
14410037SARM gem5 Developers
14510037SARM gem5 Developerstemplate <class IntType>
14610037SARM gem5 Developersvoid
14711851Sbrandon.potter@amd.comArmProcess::argsInit(int pageSize, IntRegIndex spIndex)
14810037SARM gem5 Developers{
14910037SARM gem5 Developers    int intSize = sizeof(IntType);
15010037SARM gem5 Developers
15110037SARM gem5 Developers    typedef AuxVector<IntType> auxv_t;
1526400Sgblack@eecs.umich.edu    std::vector<auxv_t> auxv;
1536400Sgblack@eecs.umich.edu
1546400Sgblack@eecs.umich.edu    string filename;
1556400Sgblack@eecs.umich.edu    if (argv.size() < 1)
1566400Sgblack@eecs.umich.edu        filename = "";
1576400Sgblack@eecs.umich.edu    else
1586400Sgblack@eecs.umich.edu        filename = argv[0];
1596400Sgblack@eecs.umich.edu
1606400Sgblack@eecs.umich.edu    //We want 16 byte alignment
1616400Sgblack@eecs.umich.edu    uint64_t align = 16;
1626400Sgblack@eecs.umich.edu
16311389Sbrandon.potter@amd.com    // Patch the ld_bias for dynamic executables.
16411389Sbrandon.potter@amd.com    updateBias();
16511389Sbrandon.potter@amd.com
1666019Shines@cs.fsu.edu    // load object file into target memory
1676019Shines@cs.fsu.edu    objFile->loadSections(initVirtMem);
1686019Shines@cs.fsu.edu
1696400Sgblack@eecs.umich.edu    enum ArmCpuFeature {
1706400Sgblack@eecs.umich.edu        Arm_Swp = 1 << 0,
1716400Sgblack@eecs.umich.edu        Arm_Half = 1 << 1,
1726400Sgblack@eecs.umich.edu        Arm_Thumb = 1 << 2,
1736400Sgblack@eecs.umich.edu        Arm_26Bit = 1 << 3,
1746400Sgblack@eecs.umich.edu        Arm_FastMult = 1 << 4,
1756400Sgblack@eecs.umich.edu        Arm_Fpa = 1 << 5,
1766400Sgblack@eecs.umich.edu        Arm_Vfp = 1 << 6,
1776400Sgblack@eecs.umich.edu        Arm_Edsp = 1 << 7,
1786400Sgblack@eecs.umich.edu        Arm_Java = 1 << 8,
1796400Sgblack@eecs.umich.edu        Arm_Iwmmxt = 1 << 9,
1807414SAli.Saidi@ARM.com        Arm_Crunch = 1 << 10,
1817414SAli.Saidi@ARM.com        Arm_ThumbEE = 1 << 11,
1827414SAli.Saidi@ARM.com        Arm_Neon = 1 << 12,
1837414SAli.Saidi@ARM.com        Arm_Vfpv3 = 1 << 13,
1847414SAli.Saidi@ARM.com        Arm_Vfpv3d16 = 1 << 14
1856400Sgblack@eecs.umich.edu    };
1866400Sgblack@eecs.umich.edu
1876400Sgblack@eecs.umich.edu    //Setup the auxilliary vectors. These will already have endian conversion.
1886400Sgblack@eecs.umich.edu    //Auxilliary vectors are loaded only for elf formatted executables.
1896400Sgblack@eecs.umich.edu    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
1906400Sgblack@eecs.umich.edu    if (elfObject) {
1916400Sgblack@eecs.umich.edu
19210810Sbr@bsdpad.com        if (objFile->getOpSys() == ObjectFile::Linux) {
19310810Sbr@bsdpad.com            IntType features =
19410810Sbr@bsdpad.com                Arm_Swp |
19510810Sbr@bsdpad.com                Arm_Half |
19610810Sbr@bsdpad.com                Arm_Thumb |
19710810Sbr@bsdpad.com//                Arm_26Bit |
19810810Sbr@bsdpad.com                Arm_FastMult |
19910810Sbr@bsdpad.com//                Arm_Fpa |
20010810Sbr@bsdpad.com                Arm_Vfp |
20110810Sbr@bsdpad.com                Arm_Edsp |
20210810Sbr@bsdpad.com//                Arm_Java |
20310810Sbr@bsdpad.com//                Arm_Iwmmxt |
20410810Sbr@bsdpad.com//                Arm_Crunch |
20510810Sbr@bsdpad.com                Arm_ThumbEE |
20610810Sbr@bsdpad.com                Arm_Neon |
20710810Sbr@bsdpad.com                Arm_Vfpv3 |
20810810Sbr@bsdpad.com                Arm_Vfpv3d16 |
20910810Sbr@bsdpad.com                0;
21010810Sbr@bsdpad.com
21110810Sbr@bsdpad.com            //Bits which describe the system hardware capabilities
21210810Sbr@bsdpad.com            //XXX Figure out what these should be
21310810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_HWCAP, features));
21410810Sbr@bsdpad.com            //Frequency at which times() increments
21510810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_CLKTCK, 0x64));
21610810Sbr@bsdpad.com            //Whether to enable "secure mode" in the executable
21710810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_SECURE, 0));
21810810Sbr@bsdpad.com            // Pointer to 16 bytes of random data
21910810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_RANDOM, 0));
22010810Sbr@bsdpad.com            //The filename of the program
22110810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_EXECFN, 0));
22210810Sbr@bsdpad.com            //The string "v71" -- ARM v7 architecture
22310810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_PLATFORM, 0));
22410810Sbr@bsdpad.com        }
22510810Sbr@bsdpad.com
2266400Sgblack@eecs.umich.edu        //The system page size
22710318Sandreas.hansson@arm.com        auxv.push_back(auxv_t(M5_AT_PAGESZ, ArmISA::PageBytes));
2286400Sgblack@eecs.umich.edu        // For statically linked executables, this is the virtual address of the
2296400Sgblack@eecs.umich.edu        // program header tables if they appear in the executable image
2306400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
2316400Sgblack@eecs.umich.edu        // This is the size of a program header entry from the elf file.
2326400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
2336400Sgblack@eecs.umich.edu        // This is the number of program headers from the original elf file.
2346400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
23511389Sbrandon.potter@amd.com        // This is the base address of the ELF interpreter; it should be
23611389Sbrandon.potter@amd.com        // zero for static executables or contain the base address for
23711389Sbrandon.potter@amd.com        // dynamic executables.
23811389Sbrandon.potter@amd.com        auxv.push_back(auxv_t(M5_AT_BASE, getBias()));
2396400Sgblack@eecs.umich.edu        //XXX Figure out what this should be.
2406400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
2416400Sgblack@eecs.umich.edu        //The entry point to the program
2426400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
2436400Sgblack@eecs.umich.edu        //Different user and group IDs
2446400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_UID, uid()));
2456400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_EUID, euid()));
2466400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_GID, gid()));
2476400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_EGID, egid()));
2486400Sgblack@eecs.umich.edu    }
2496400Sgblack@eecs.umich.edu
2506400Sgblack@eecs.umich.edu    //Figure out how big the initial stack nedes to be
2516400Sgblack@eecs.umich.edu
2526400Sgblack@eecs.umich.edu    // A sentry NULL void pointer at the top of the stack.
2536400Sgblack@eecs.umich.edu    int sentry_size = intSize;
2546400Sgblack@eecs.umich.edu
2557414SAli.Saidi@ARM.com    string platform = "v71";
2566400Sgblack@eecs.umich.edu    int platform_size = platform.size() + 1;
2576400Sgblack@eecs.umich.edu
2587414SAli.Saidi@ARM.com    // Bytes for AT_RANDOM above, we'll just keep them 0
2597414SAli.Saidi@ARM.com    int aux_random_size = 16; // as per the specification
2607414SAli.Saidi@ARM.com
2616400Sgblack@eecs.umich.edu    // The aux vectors are put on the stack in two groups. The first group are
2626400Sgblack@eecs.umich.edu    // the vectors that are generated as the elf is loaded. The second group
2636400Sgblack@eecs.umich.edu    // are the ones that were computed ahead of time and include the platform
2646400Sgblack@eecs.umich.edu    // string.
2656400Sgblack@eecs.umich.edu    int aux_data_size = filename.size() + 1;
2666400Sgblack@eecs.umich.edu
2676400Sgblack@eecs.umich.edu    int env_data_size = 0;
2686400Sgblack@eecs.umich.edu    for (int i = 0; i < envp.size(); ++i) {
2696400Sgblack@eecs.umich.edu        env_data_size += envp[i].size() + 1;
2706400Sgblack@eecs.umich.edu    }
2716019Shines@cs.fsu.edu    int arg_data_size = 0;
2726019Shines@cs.fsu.edu    for (int i = 0; i < argv.size(); ++i) {
2736019Shines@cs.fsu.edu        arg_data_size += argv[i].size() + 1;
2746019Shines@cs.fsu.edu    }
2756400Sgblack@eecs.umich.edu
2766400Sgblack@eecs.umich.edu    int info_block_size =
2776400Sgblack@eecs.umich.edu        sentry_size + env_data_size + arg_data_size +
2787414SAli.Saidi@ARM.com        aux_data_size + platform_size + aux_random_size;
2796400Sgblack@eecs.umich.edu
2806400Sgblack@eecs.umich.edu    //Each auxilliary vector is two 4 byte words
2816400Sgblack@eecs.umich.edu    int aux_array_size = intSize * 2 * (auxv.size() + 1);
2826400Sgblack@eecs.umich.edu
2836400Sgblack@eecs.umich.edu    int envp_array_size = intSize * (envp.size() + 1);
2846400Sgblack@eecs.umich.edu    int argv_array_size = intSize * (argv.size() + 1);
2856400Sgblack@eecs.umich.edu
2866400Sgblack@eecs.umich.edu    int argc_size = intSize;
2876400Sgblack@eecs.umich.edu
2886400Sgblack@eecs.umich.edu    //Figure out the size of the contents of the actual initial frame
2896400Sgblack@eecs.umich.edu    int frame_size =
2906400Sgblack@eecs.umich.edu        info_block_size +
2916400Sgblack@eecs.umich.edu        aux_array_size +
2926400Sgblack@eecs.umich.edu        envp_array_size +
2936400Sgblack@eecs.umich.edu        argv_array_size +
2946400Sgblack@eecs.umich.edu        argc_size;
2956400Sgblack@eecs.umich.edu
2966400Sgblack@eecs.umich.edu    //There needs to be padding after the auxiliary vector data so that the
2976400Sgblack@eecs.umich.edu    //very bottom of the stack is aligned properly.
2986400Sgblack@eecs.umich.edu    int partial_size = frame_size;
2996400Sgblack@eecs.umich.edu    int aligned_partial_size = roundUp(partial_size, align);
3006400Sgblack@eecs.umich.edu    int aux_padding = aligned_partial_size - partial_size;
3016400Sgblack@eecs.umich.edu
3026400Sgblack@eecs.umich.edu    int space_needed = frame_size + aux_padding;
3036400Sgblack@eecs.umich.edu
30411905SBrandon.Potter@amd.com    memState->setStackMin(memState->getStackBase() - space_needed);
30511905SBrandon.Potter@amd.com    memState->setStackMin(roundDown(memState->getStackMin(), align));
30611905SBrandon.Potter@amd.com    memState->setStackSize(memState->getStackBase() - memState->getStackMin());
3076400Sgblack@eecs.umich.edu
3086400Sgblack@eecs.umich.edu    // map memory
30911905SBrandon.Potter@amd.com    allocateMem(roundDown(memState->getStackMin(), pageSize),
31011905SBrandon.Potter@amd.com                          roundUp(memState->getStackSize(), pageSize));
3116400Sgblack@eecs.umich.edu
3126400Sgblack@eecs.umich.edu    // map out initial stack contents
31311905SBrandon.Potter@amd.com    IntType sentry_base = memState->getStackBase() - sentry_size;
31410037SARM gem5 Developers    IntType aux_data_base = sentry_base - aux_data_size;
31510037SARM gem5 Developers    IntType env_data_base = aux_data_base - env_data_size;
31610037SARM gem5 Developers    IntType arg_data_base = env_data_base - arg_data_size;
31710037SARM gem5 Developers    IntType platform_base = arg_data_base - platform_size;
31810037SARM gem5 Developers    IntType aux_random_base = platform_base - aux_random_size;
31910037SARM gem5 Developers    IntType auxv_array_base = aux_random_base - aux_array_size - aux_padding;
32010037SARM gem5 Developers    IntType envp_array_base = auxv_array_base - envp_array_size;
32110037SARM gem5 Developers    IntType argv_array_base = envp_array_base - argv_array_size;
32210037SARM gem5 Developers    IntType argc_base = argv_array_base - argc_size;
3236400Sgblack@eecs.umich.edu
3246400Sgblack@eecs.umich.edu    DPRINTF(Stack, "The addresses of items on the initial stack:\n");
3256400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
3266400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - env data\n", env_data_base);
3276400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
3287414SAli.Saidi@ARM.com    DPRINTF(Stack, "0x%x - random data\n", aux_random_base);
3296400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - platform base\n", platform_base);
3306400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
3316400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
3326400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
3336400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - argc \n", argc_base);
33411905SBrandon.Potter@amd.com    DPRINTF(Stack, "0x%x - stack min\n", memState->getStackMin());
3356400Sgblack@eecs.umich.edu
3366400Sgblack@eecs.umich.edu    // write contents to stack
3376400Sgblack@eecs.umich.edu
3386400Sgblack@eecs.umich.edu    // figure out argc
33910037SARM gem5 Developers    IntType argc = argv.size();
34010037SARM gem5 Developers    IntType guestArgc = ArmISA::htog(argc);
3416400Sgblack@eecs.umich.edu
3426400Sgblack@eecs.umich.edu    //Write out the sentry void *
34310037SARM gem5 Developers    IntType sentry_NULL = 0;
3448852Sandreas.hansson@arm.com    initVirtMem.writeBlob(sentry_base,
3456400Sgblack@eecs.umich.edu            (uint8_t*)&sentry_NULL, sentry_size);
3466400Sgblack@eecs.umich.edu
3476400Sgblack@eecs.umich.edu    //Fix up the aux vectors which point to other data
3486400Sgblack@eecs.umich.edu    for (int i = auxv.size() - 1; i >= 0; i--) {
3496400Sgblack@eecs.umich.edu        if (auxv[i].a_type == M5_AT_PLATFORM) {
3506400Sgblack@eecs.umich.edu            auxv[i].a_val = platform_base;
3518852Sandreas.hansson@arm.com            initVirtMem.writeString(platform_base, platform.c_str());
3526400Sgblack@eecs.umich.edu        } else if (auxv[i].a_type == M5_AT_EXECFN) {
3536400Sgblack@eecs.umich.edu            auxv[i].a_val = aux_data_base;
3548852Sandreas.hansson@arm.com            initVirtMem.writeString(aux_data_base, filename.c_str());
3557414SAli.Saidi@ARM.com        } else if (auxv[i].a_type == M5_AT_RANDOM) {
3567414SAli.Saidi@ARM.com            auxv[i].a_val = aux_random_base;
3577414SAli.Saidi@ARM.com            // Just leave the value 0, we don't want randomness
3586400Sgblack@eecs.umich.edu        }
3596019Shines@cs.fsu.edu    }
3606019Shines@cs.fsu.edu
3616400Sgblack@eecs.umich.edu    //Copy the aux stuff
36210037SARM gem5 Developers    for (int x = 0; x < auxv.size(); x++) {
3638852Sandreas.hansson@arm.com        initVirtMem.writeBlob(auxv_array_base + x * 2 * intSize,
3646400Sgblack@eecs.umich.edu                (uint8_t*)&(auxv[x].a_type), intSize);
3658852Sandreas.hansson@arm.com        initVirtMem.writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
3666400Sgblack@eecs.umich.edu                (uint8_t*)&(auxv[x].a_val), intSize);
3676400Sgblack@eecs.umich.edu    }
3686400Sgblack@eecs.umich.edu    //Write out the terminating zeroed auxilliary vector
3696400Sgblack@eecs.umich.edu    const uint64_t zero = 0;
3708852Sandreas.hansson@arm.com    initVirtMem.writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
3716400Sgblack@eecs.umich.edu            (uint8_t*)&zero, 2 * intSize);
3726019Shines@cs.fsu.edu
3736400Sgblack@eecs.umich.edu    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
3746400Sgblack@eecs.umich.edu    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
3756019Shines@cs.fsu.edu
3768852Sandreas.hansson@arm.com    initVirtMem.writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
3776019Shines@cs.fsu.edu
3786020Sgblack@eecs.umich.edu    ThreadContext *tc = system->getThreadContext(contextIds[0]);
3796400Sgblack@eecs.umich.edu    //Set the stack pointer register
38011905SBrandon.Potter@amd.com    tc->setIntReg(spIndex, memState->getStackMin());
3816400Sgblack@eecs.umich.edu    //A pointer to a function to run when the program exits. We'll set this
3826400Sgblack@eecs.umich.edu    //to zero explicitly to make sure this isn't used.
3836400Sgblack@eecs.umich.edu    tc->setIntReg(ArgumentReg0, 0);
3846400Sgblack@eecs.umich.edu    //Set argument regs 1 and 2 to argv[0] and envp[0] respectively
3856400Sgblack@eecs.umich.edu    if (argv.size() > 0) {
3866400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg1, arg_data_base + arg_data_size -
3876400Sgblack@eecs.umich.edu                                    argv[argv.size() - 1].size() - 1);
3886400Sgblack@eecs.umich.edu    } else {
3896400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg1, 0);
3906400Sgblack@eecs.umich.edu    }
3916400Sgblack@eecs.umich.edu    if (envp.size() > 0) {
3926400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg2, env_data_base + env_data_size -
3936400Sgblack@eecs.umich.edu                                    envp[envp.size() - 1].size() - 1);
3946400Sgblack@eecs.umich.edu    } else {
3956400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg2, 0);
3966400Sgblack@eecs.umich.edu    }
3976019Shines@cs.fsu.edu
3987720Sgblack@eecs.umich.edu    PCState pc;
3997720Sgblack@eecs.umich.edu    pc.thumb(arch == ObjectFile::Thumb);
4007720Sgblack@eecs.umich.edu    pc.nextThumb(pc.thumb());
40110037SARM gem5 Developers    pc.aarch64(arch == ObjectFile::Arm64);
40210037SARM gem5 Developers    pc.nextAArch64(pc.aarch64());
40311389Sbrandon.potter@amd.com    pc.set(getStartPC() & ~mask(1));
4047720Sgblack@eecs.umich.edu    tc->pcState(pc);
4056400Sgblack@eecs.umich.edu
40611886Sbrandon.potter@amd.com    //Align the "stackMin" to a page boundary.
40711905SBrandon.Potter@amd.com    memState->setStackMin(roundDown(memState->getStackMin(), pageSize));
4086019Shines@cs.fsu.edu}
4096019Shines@cs.fsu.edu
4106020Sgblack@eecs.umich.eduArmISA::IntReg
41111851Sbrandon.potter@amd.comArmProcess32::getSyscallArg(ThreadContext *tc, int &i)
4126020Sgblack@eecs.umich.edu{
4137441SAli.Saidi@ARM.com    assert(i < 6);
4146701Sgblack@eecs.umich.edu    return tc->readIntReg(ArgumentReg0 + i++);
4156020Sgblack@eecs.umich.edu}
4166020Sgblack@eecs.umich.edu
41710037SARM gem5 DevelopersArmISA::IntReg
41811851Sbrandon.potter@amd.comArmProcess64::getSyscallArg(ThreadContext *tc, int &i)
41910037SARM gem5 Developers{
42010037SARM gem5 Developers    assert(i < 8);
42110037SARM gem5 Developers    return tc->readIntReg(ArgumentReg0 + i++);
42210037SARM gem5 Developers}
42310037SARM gem5 Developers
42410037SARM gem5 DevelopersArmISA::IntReg
42511851Sbrandon.potter@amd.comArmProcess32::getSyscallArg(ThreadContext *tc, int &i, int width)
4267441SAli.Saidi@ARM.com{
4277441SAli.Saidi@ARM.com    assert(width == 32 || width == 64);
4287441SAli.Saidi@ARM.com    if (width == 32)
4297441SAli.Saidi@ARM.com        return getSyscallArg(tc, i);
4307441SAli.Saidi@ARM.com
4317441SAli.Saidi@ARM.com    // 64 bit arguments are passed starting in an even register
4327441SAli.Saidi@ARM.com    if (i % 2 != 0)
4337441SAli.Saidi@ARM.com       i++;
4347441SAli.Saidi@ARM.com
4357441SAli.Saidi@ARM.com    // Registers r0-r6 can be used
4367441SAli.Saidi@ARM.com    assert(i < 5);
4377441SAli.Saidi@ARM.com    uint64_t val;
4387441SAli.Saidi@ARM.com    val = tc->readIntReg(ArgumentReg0 + i++);
4397441SAli.Saidi@ARM.com    val |= ((uint64_t)tc->readIntReg(ArgumentReg0 + i++) << 32);
4407441SAli.Saidi@ARM.com    return val;
4417441SAli.Saidi@ARM.com}
4427441SAli.Saidi@ARM.com
44310037SARM gem5 DevelopersArmISA::IntReg
44411851Sbrandon.potter@amd.comArmProcess64::getSyscallArg(ThreadContext *tc, int &i, int width)
44510037SARM gem5 Developers{
44610037SARM gem5 Developers    return getSyscallArg(tc, i);
44710037SARM gem5 Developers}
44810037SARM gem5 Developers
4497441SAli.Saidi@ARM.com
4506020Sgblack@eecs.umich.eduvoid
45111851Sbrandon.potter@amd.comArmProcess32::setSyscallArg(ThreadContext *tc, int i, ArmISA::IntReg val)
4526020Sgblack@eecs.umich.edu{
45310037SARM gem5 Developers    assert(i < 6);
4546020Sgblack@eecs.umich.edu    tc->setIntReg(ArgumentReg0 + i, val);
4556020Sgblack@eecs.umich.edu}
4566020Sgblack@eecs.umich.edu
4576020Sgblack@eecs.umich.eduvoid
45811851Sbrandon.potter@amd.comArmProcess64::setSyscallArg(ThreadContext *tc, int i, ArmISA::IntReg val)
45910037SARM gem5 Developers{
46010037SARM gem5 Developers    assert(i < 8);
46110037SARM gem5 Developers    tc->setIntReg(ArgumentReg0 + i, val);
46210037SARM gem5 Developers}
46310037SARM gem5 Developers
46410037SARM gem5 Developersvoid
46511851Sbrandon.potter@amd.comArmProcess32::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
4666020Sgblack@eecs.umich.edu{
46710810Sbr@bsdpad.com
46810810Sbr@bsdpad.com    if (objFile->getOpSys() == ObjectFile::FreeBSD) {
46910810Sbr@bsdpad.com        // Decode return value
47010810Sbr@bsdpad.com        if (sysret.encodedValue() >= 0)
47110810Sbr@bsdpad.com            // FreeBSD checks the carry bit to determine if syscall is succeeded
47210810Sbr@bsdpad.com            tc->setCCReg(CCREG_C, 0);
47310810Sbr@bsdpad.com        else {
47410810Sbr@bsdpad.com            sysret = -sysret.encodedValue();
47510810Sbr@bsdpad.com        }
47610810Sbr@bsdpad.com    }
47710810Sbr@bsdpad.com
47810223Ssteve.reinhardt@amd.com    tc->setIntReg(ReturnValueReg, sysret.encodedValue());
4796020Sgblack@eecs.umich.edu}
48010037SARM gem5 Developers
48110037SARM gem5 Developersvoid
48211851Sbrandon.potter@amd.comArmProcess64::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
48310037SARM gem5 Developers{
48410810Sbr@bsdpad.com
48510810Sbr@bsdpad.com    if (objFile->getOpSys() == ObjectFile::FreeBSD) {
48610810Sbr@bsdpad.com        // Decode return value
48710810Sbr@bsdpad.com        if (sysret.encodedValue() >= 0)
48810810Sbr@bsdpad.com            // FreeBSD checks the carry bit to determine if syscall is succeeded
48910810Sbr@bsdpad.com            tc->setCCReg(CCREG_C, 0);
49010810Sbr@bsdpad.com        else {
49110810Sbr@bsdpad.com            sysret = -sysret.encodedValue();
49210810Sbr@bsdpad.com        }
49310810Sbr@bsdpad.com    }
49410810Sbr@bsdpad.com
49510223Ssteve.reinhardt@amd.com    tc->setIntReg(ReturnValueReg, sysret.encodedValue());
49610037SARM gem5 Developers}
497