process.cc revision 11793
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"
547678Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
556019Shines@cs.fsu.edu#include "sim/process_impl.hh"
566019Shines@cs.fsu.edu#include "sim/system.hh"
576019Shines@cs.fsu.edu
586019Shines@cs.fsu.eduusing namespace std;
596019Shines@cs.fsu.eduusing namespace ArmISA;
606019Shines@cs.fsu.edu
617096Sgblack@eecs.umich.eduArmLiveProcess::ArmLiveProcess(LiveProcessParams *params, ObjectFile *objFile,
627096Sgblack@eecs.umich.edu                               ObjectFile::Arch _arch)
637096Sgblack@eecs.umich.edu    : LiveProcess(params, objFile), arch(_arch)
646019Shines@cs.fsu.edu{
6510037SARM gem5 Developers}
6610037SARM gem5 Developers
6710037SARM gem5 DevelopersArmLiveProcess32::ArmLiveProcess32(LiveProcessParams *params,
6810037SARM gem5 Developers                                   ObjectFile *objFile, ObjectFile::Arch _arch)
6910037SARM gem5 Developers    : ArmLiveProcess(params, objFile, _arch)
7010037SARM gem5 Developers{
716400Sgblack@eecs.umich.edu    stack_base = 0xbf000000L;
726019Shines@cs.fsu.edu
736019Shines@cs.fsu.edu    // Set pointer for next thread stack.  Reserve 8M for main stack.
746019Shines@cs.fsu.edu    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
756019Shines@cs.fsu.edu
766019Shines@cs.fsu.edu    // Set up break point (Top of Heap)
776019Shines@cs.fsu.edu    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
7810318Sandreas.hansson@arm.com    brk_point = roundUp(brk_point, PageBytes);
796019Shines@cs.fsu.edu
806019Shines@cs.fsu.edu    // Set up region for mmaps. For now, start at bottom of kuseg space.
8111386Ssteve.reinhardt@amd.com    mmap_end = 0x40000000L;
826019Shines@cs.fsu.edu}
836019Shines@cs.fsu.edu
8410037SARM gem5 DevelopersArmLiveProcess64::ArmLiveProcess64(LiveProcessParams *params,
8510037SARM gem5 Developers                                   ObjectFile *objFile, ObjectFile::Arch _arch)
8610037SARM gem5 Developers    : ArmLiveProcess(params, objFile, _arch)
8710037SARM gem5 Developers{
8810037SARM gem5 Developers    stack_base = 0x7fffff0000L;
8910037SARM gem5 Developers
9010037SARM gem5 Developers    // Set pointer for next thread stack.  Reserve 8M for main stack.
9110037SARM gem5 Developers    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
9210037SARM gem5 Developers
9310037SARM gem5 Developers    // Set up break point (Top of Heap)
9410037SARM gem5 Developers    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
9510318Sandreas.hansson@arm.com    brk_point = roundUp(brk_point, PageBytes);
9610037SARM gem5 Developers
9710037SARM gem5 Developers    // Set up region for mmaps. For now, start at bottom of kuseg space.
9811386Ssteve.reinhardt@amd.com    mmap_end = 0x4000000000L;
9910037SARM gem5 Developers}
10010037SARM gem5 Developers
1016019Shines@cs.fsu.eduvoid
10210037SARM gem5 DevelopersArmLiveProcess32::initState()
1036019Shines@cs.fsu.edu{
1048216Ssaidi@eecs.umich.edu    LiveProcess::initState();
10510318Sandreas.hansson@arm.com    argsInit<uint32_t>(PageBytes, INTREG_SP);
1067640Sgblack@eecs.umich.edu    for (int i = 0; i < contextIds.size(); i++) {
1077640Sgblack@eecs.umich.edu        ThreadContext * tc = system->getThreadContext(contextIds[i]);
1087640Sgblack@eecs.umich.edu        CPACR cpacr = tc->readMiscReg(MISCREG_CPACR);
1097640Sgblack@eecs.umich.edu        // Enable the floating point coprocessors.
1107640Sgblack@eecs.umich.edu        cpacr.cp10 = 0x3;
1117640Sgblack@eecs.umich.edu        cpacr.cp11 = 0x3;
1127640Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CPACR, cpacr);
1137640Sgblack@eecs.umich.edu        // Generically enable floating point support.
1147640Sgblack@eecs.umich.edu        FPEXC fpexc = tc->readMiscReg(MISCREG_FPEXC);
1157640Sgblack@eecs.umich.edu        fpexc.en = 1;
1167640Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_FPEXC, fpexc);
1177640Sgblack@eecs.umich.edu    }
1186019Shines@cs.fsu.edu}
1196019Shines@cs.fsu.edu
1206019Shines@cs.fsu.eduvoid
12110037SARM gem5 DevelopersArmLiveProcess64::initState()
1226019Shines@cs.fsu.edu{
12310037SARM gem5 Developers    LiveProcess::initState();
12410318Sandreas.hansson@arm.com    argsInit<uint64_t>(PageBytes, INTREG_SP0);
12510037SARM gem5 Developers    for (int i = 0; i < contextIds.size(); i++) {
12610037SARM gem5 Developers        ThreadContext * tc = system->getThreadContext(contextIds[i]);
12710037SARM gem5 Developers        CPSR cpsr = tc->readMiscReg(MISCREG_CPSR);
12810037SARM gem5 Developers        cpsr.mode = MODE_EL0T;
12910037SARM gem5 Developers        tc->setMiscReg(MISCREG_CPSR, cpsr);
13010037SARM gem5 Developers        CPACR cpacr = tc->readMiscReg(MISCREG_CPACR_EL1);
13110037SARM gem5 Developers        // Enable the floating point coprocessors.
13210037SARM gem5 Developers        cpacr.cp10 = 0x3;
13310037SARM gem5 Developers        cpacr.cp11 = 0x3;
13410037SARM gem5 Developers        tc->setMiscReg(MISCREG_CPACR_EL1, cpacr);
13510037SARM gem5 Developers        // Generically enable floating point support.
13610037SARM gem5 Developers        FPEXC fpexc = tc->readMiscReg(MISCREG_FPEXC);
13710037SARM gem5 Developers        fpexc.en = 1;
13810037SARM gem5 Developers        tc->setMiscReg(MISCREG_FPEXC, fpexc);
13910037SARM gem5 Developers    }
14010037SARM gem5 Developers}
14110037SARM gem5 Developers
14210037SARM gem5 Developerstemplate <class IntType>
14310037SARM gem5 Developersvoid
14410037SARM gem5 DevelopersArmLiveProcess::argsInit(int pageSize, IntRegIndex spIndex)
14510037SARM gem5 Developers{
14610037SARM gem5 Developers    int intSize = sizeof(IntType);
14710037SARM gem5 Developers
14810037SARM gem5 Developers    typedef AuxVector<IntType> auxv_t;
1496400Sgblack@eecs.umich.edu    std::vector<auxv_t> auxv;
1506400Sgblack@eecs.umich.edu
1516400Sgblack@eecs.umich.edu    string filename;
1526400Sgblack@eecs.umich.edu    if (argv.size() < 1)
1536400Sgblack@eecs.umich.edu        filename = "";
1546400Sgblack@eecs.umich.edu    else
1556400Sgblack@eecs.umich.edu        filename = argv[0];
1566400Sgblack@eecs.umich.edu
1576400Sgblack@eecs.umich.edu    //We want 16 byte alignment
1586400Sgblack@eecs.umich.edu    uint64_t align = 16;
1596400Sgblack@eecs.umich.edu
16011389Sbrandon.potter@amd.com    // Patch the ld_bias for dynamic executables.
16111389Sbrandon.potter@amd.com    updateBias();
16211389Sbrandon.potter@amd.com
1636019Shines@cs.fsu.edu    // load object file into target memory
1646019Shines@cs.fsu.edu    objFile->loadSections(initVirtMem);
1656019Shines@cs.fsu.edu
1666400Sgblack@eecs.umich.edu    enum ArmCpuFeature {
1676400Sgblack@eecs.umich.edu        Arm_Swp = 1 << 0,
1686400Sgblack@eecs.umich.edu        Arm_Half = 1 << 1,
1696400Sgblack@eecs.umich.edu        Arm_Thumb = 1 << 2,
1706400Sgblack@eecs.umich.edu        Arm_26Bit = 1 << 3,
1716400Sgblack@eecs.umich.edu        Arm_FastMult = 1 << 4,
1726400Sgblack@eecs.umich.edu        Arm_Fpa = 1 << 5,
1736400Sgblack@eecs.umich.edu        Arm_Vfp = 1 << 6,
1746400Sgblack@eecs.umich.edu        Arm_Edsp = 1 << 7,
1756400Sgblack@eecs.umich.edu        Arm_Java = 1 << 8,
1766400Sgblack@eecs.umich.edu        Arm_Iwmmxt = 1 << 9,
1777414SAli.Saidi@ARM.com        Arm_Crunch = 1 << 10,
1787414SAli.Saidi@ARM.com        Arm_ThumbEE = 1 << 11,
1797414SAli.Saidi@ARM.com        Arm_Neon = 1 << 12,
1807414SAli.Saidi@ARM.com        Arm_Vfpv3 = 1 << 13,
1817414SAli.Saidi@ARM.com        Arm_Vfpv3d16 = 1 << 14
1826400Sgblack@eecs.umich.edu    };
1836400Sgblack@eecs.umich.edu
1846400Sgblack@eecs.umich.edu    //Setup the auxilliary vectors. These will already have endian conversion.
1856400Sgblack@eecs.umich.edu    //Auxilliary vectors are loaded only for elf formatted executables.
1866400Sgblack@eecs.umich.edu    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
1876400Sgblack@eecs.umich.edu    if (elfObject) {
1886400Sgblack@eecs.umich.edu
18910810Sbr@bsdpad.com        if (objFile->getOpSys() == ObjectFile::Linux) {
19010810Sbr@bsdpad.com            IntType features =
19110810Sbr@bsdpad.com                Arm_Swp |
19210810Sbr@bsdpad.com                Arm_Half |
19310810Sbr@bsdpad.com                Arm_Thumb |
19410810Sbr@bsdpad.com//                Arm_26Bit |
19510810Sbr@bsdpad.com                Arm_FastMult |
19610810Sbr@bsdpad.com//                Arm_Fpa |
19710810Sbr@bsdpad.com                Arm_Vfp |
19810810Sbr@bsdpad.com                Arm_Edsp |
19910810Sbr@bsdpad.com//                Arm_Java |
20010810Sbr@bsdpad.com//                Arm_Iwmmxt |
20110810Sbr@bsdpad.com//                Arm_Crunch |
20210810Sbr@bsdpad.com                Arm_ThumbEE |
20310810Sbr@bsdpad.com                Arm_Neon |
20410810Sbr@bsdpad.com                Arm_Vfpv3 |
20510810Sbr@bsdpad.com                Arm_Vfpv3d16 |
20610810Sbr@bsdpad.com                0;
20710810Sbr@bsdpad.com
20810810Sbr@bsdpad.com            //Bits which describe the system hardware capabilities
20910810Sbr@bsdpad.com            //XXX Figure out what these should be
21010810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_HWCAP, features));
21110810Sbr@bsdpad.com            //Frequency at which times() increments
21210810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_CLKTCK, 0x64));
21310810Sbr@bsdpad.com            //Whether to enable "secure mode" in the executable
21410810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_SECURE, 0));
21510810Sbr@bsdpad.com            // Pointer to 16 bytes of random data
21610810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_RANDOM, 0));
21710810Sbr@bsdpad.com            //The filename of the program
21810810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_EXECFN, 0));
21910810Sbr@bsdpad.com            //The string "v71" -- ARM v7 architecture
22010810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_PLATFORM, 0));
22110810Sbr@bsdpad.com        }
22210810Sbr@bsdpad.com
2236400Sgblack@eecs.umich.edu        //The system page size
22410318Sandreas.hansson@arm.com        auxv.push_back(auxv_t(M5_AT_PAGESZ, ArmISA::PageBytes));
2256400Sgblack@eecs.umich.edu        // For statically linked executables, this is the virtual address of the
2266400Sgblack@eecs.umich.edu        // program header tables if they appear in the executable image
2276400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
2286400Sgblack@eecs.umich.edu        // This is the size of a program header entry from the elf file.
2296400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
2306400Sgblack@eecs.umich.edu        // This is the number of program headers from the original elf file.
2316400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
23211389Sbrandon.potter@amd.com        // This is the base address of the ELF interpreter; it should be
23311389Sbrandon.potter@amd.com        // zero for static executables or contain the base address for
23411389Sbrandon.potter@amd.com        // dynamic executables.
23511389Sbrandon.potter@amd.com        auxv.push_back(auxv_t(M5_AT_BASE, getBias()));
2366400Sgblack@eecs.umich.edu        //XXX Figure out what this should be.
2376400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
2386400Sgblack@eecs.umich.edu        //The entry point to the program
2396400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
2406400Sgblack@eecs.umich.edu        //Different user and group IDs
2416400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_UID, uid()));
2426400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_EUID, euid()));
2436400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_GID, gid()));
2446400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_EGID, egid()));
2456400Sgblack@eecs.umich.edu    }
2466400Sgblack@eecs.umich.edu
2476400Sgblack@eecs.umich.edu    //Figure out how big the initial stack nedes to be
2486400Sgblack@eecs.umich.edu
2496400Sgblack@eecs.umich.edu    // A sentry NULL void pointer at the top of the stack.
2506400Sgblack@eecs.umich.edu    int sentry_size = intSize;
2516400Sgblack@eecs.umich.edu
2527414SAli.Saidi@ARM.com    string platform = "v71";
2536400Sgblack@eecs.umich.edu    int platform_size = platform.size() + 1;
2546400Sgblack@eecs.umich.edu
2557414SAli.Saidi@ARM.com    // Bytes for AT_RANDOM above, we'll just keep them 0
2567414SAli.Saidi@ARM.com    int aux_random_size = 16; // as per the specification
2577414SAli.Saidi@ARM.com
2586400Sgblack@eecs.umich.edu    // The aux vectors are put on the stack in two groups. The first group are
2596400Sgblack@eecs.umich.edu    // the vectors that are generated as the elf is loaded. The second group
2606400Sgblack@eecs.umich.edu    // are the ones that were computed ahead of time and include the platform
2616400Sgblack@eecs.umich.edu    // string.
2626400Sgblack@eecs.umich.edu    int aux_data_size = filename.size() + 1;
2636400Sgblack@eecs.umich.edu
2646400Sgblack@eecs.umich.edu    int env_data_size = 0;
2656400Sgblack@eecs.umich.edu    for (int i = 0; i < envp.size(); ++i) {
2666400Sgblack@eecs.umich.edu        env_data_size += envp[i].size() + 1;
2676400Sgblack@eecs.umich.edu    }
2686019Shines@cs.fsu.edu    int arg_data_size = 0;
2696019Shines@cs.fsu.edu    for (int i = 0; i < argv.size(); ++i) {
2706019Shines@cs.fsu.edu        arg_data_size += argv[i].size() + 1;
2716019Shines@cs.fsu.edu    }
2726400Sgblack@eecs.umich.edu
2736400Sgblack@eecs.umich.edu    int info_block_size =
2746400Sgblack@eecs.umich.edu        sentry_size + env_data_size + arg_data_size +
2757414SAli.Saidi@ARM.com        aux_data_size + platform_size + aux_random_size;
2766400Sgblack@eecs.umich.edu
2776400Sgblack@eecs.umich.edu    //Each auxilliary vector is two 4 byte words
2786400Sgblack@eecs.umich.edu    int aux_array_size = intSize * 2 * (auxv.size() + 1);
2796400Sgblack@eecs.umich.edu
2806400Sgblack@eecs.umich.edu    int envp_array_size = intSize * (envp.size() + 1);
2816400Sgblack@eecs.umich.edu    int argv_array_size = intSize * (argv.size() + 1);
2826400Sgblack@eecs.umich.edu
2836400Sgblack@eecs.umich.edu    int argc_size = intSize;
2846400Sgblack@eecs.umich.edu
2856400Sgblack@eecs.umich.edu    //Figure out the size of the contents of the actual initial frame
2866400Sgblack@eecs.umich.edu    int frame_size =
2876400Sgblack@eecs.umich.edu        info_block_size +
2886400Sgblack@eecs.umich.edu        aux_array_size +
2896400Sgblack@eecs.umich.edu        envp_array_size +
2906400Sgblack@eecs.umich.edu        argv_array_size +
2916400Sgblack@eecs.umich.edu        argc_size;
2926400Sgblack@eecs.umich.edu
2936400Sgblack@eecs.umich.edu    //There needs to be padding after the auxiliary vector data so that the
2946400Sgblack@eecs.umich.edu    //very bottom of the stack is aligned properly.
2956400Sgblack@eecs.umich.edu    int partial_size = frame_size;
2966400Sgblack@eecs.umich.edu    int aligned_partial_size = roundUp(partial_size, align);
2976400Sgblack@eecs.umich.edu    int aux_padding = aligned_partial_size - partial_size;
2986400Sgblack@eecs.umich.edu
2996400Sgblack@eecs.umich.edu    int space_needed = frame_size + aux_padding;
3006400Sgblack@eecs.umich.edu
3016400Sgblack@eecs.umich.edu    stack_min = stack_base - space_needed;
3026400Sgblack@eecs.umich.edu    stack_min = roundDown(stack_min, align);
3036400Sgblack@eecs.umich.edu    stack_size = stack_base - stack_min;
3046400Sgblack@eecs.umich.edu
3056400Sgblack@eecs.umich.edu    // map memory
3068601Ssteve.reinhardt@amd.com    allocateMem(roundDown(stack_min, pageSize), roundUp(stack_size, pageSize));
3076400Sgblack@eecs.umich.edu
3086400Sgblack@eecs.umich.edu    // map out initial stack contents
30910037SARM gem5 Developers    IntType sentry_base = stack_base - sentry_size;
31010037SARM gem5 Developers    IntType aux_data_base = sentry_base - aux_data_size;
31110037SARM gem5 Developers    IntType env_data_base = aux_data_base - env_data_size;
31210037SARM gem5 Developers    IntType arg_data_base = env_data_base - arg_data_size;
31310037SARM gem5 Developers    IntType platform_base = arg_data_base - platform_size;
31410037SARM gem5 Developers    IntType aux_random_base = platform_base - aux_random_size;
31510037SARM gem5 Developers    IntType auxv_array_base = aux_random_base - aux_array_size - aux_padding;
31610037SARM gem5 Developers    IntType envp_array_base = auxv_array_base - envp_array_size;
31710037SARM gem5 Developers    IntType argv_array_base = envp_array_base - argv_array_size;
31810037SARM gem5 Developers    IntType argc_base = argv_array_base - argc_size;
3196400Sgblack@eecs.umich.edu
3206400Sgblack@eecs.umich.edu    DPRINTF(Stack, "The addresses of items on the initial stack:\n");
3216400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
3226400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - env data\n", env_data_base);
3236400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
3247414SAli.Saidi@ARM.com    DPRINTF(Stack, "0x%x - random data\n", aux_random_base);
3256400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - platform base\n", platform_base);
3266400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
3276400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
3286400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
3296400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - argc \n", argc_base);
3306400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - stack min\n", stack_min);
3316400Sgblack@eecs.umich.edu
3326400Sgblack@eecs.umich.edu    // write contents to stack
3336400Sgblack@eecs.umich.edu
3346400Sgblack@eecs.umich.edu    // figure out argc
33510037SARM gem5 Developers    IntType argc = argv.size();
33610037SARM gem5 Developers    IntType guestArgc = ArmISA::htog(argc);
3376400Sgblack@eecs.umich.edu
3386400Sgblack@eecs.umich.edu    //Write out the sentry void *
33910037SARM gem5 Developers    IntType sentry_NULL = 0;
3408852Sandreas.hansson@arm.com    initVirtMem.writeBlob(sentry_base,
3416400Sgblack@eecs.umich.edu            (uint8_t*)&sentry_NULL, sentry_size);
3426400Sgblack@eecs.umich.edu
3436400Sgblack@eecs.umich.edu    //Fix up the aux vectors which point to other data
3446400Sgblack@eecs.umich.edu    for (int i = auxv.size() - 1; i >= 0; i--) {
3456400Sgblack@eecs.umich.edu        if (auxv[i].a_type == M5_AT_PLATFORM) {
3466400Sgblack@eecs.umich.edu            auxv[i].a_val = platform_base;
3478852Sandreas.hansson@arm.com            initVirtMem.writeString(platform_base, platform.c_str());
3486400Sgblack@eecs.umich.edu        } else if (auxv[i].a_type == M5_AT_EXECFN) {
3496400Sgblack@eecs.umich.edu            auxv[i].a_val = aux_data_base;
3508852Sandreas.hansson@arm.com            initVirtMem.writeString(aux_data_base, filename.c_str());
3517414SAli.Saidi@ARM.com        } else if (auxv[i].a_type == M5_AT_RANDOM) {
3527414SAli.Saidi@ARM.com            auxv[i].a_val = aux_random_base;
3537414SAli.Saidi@ARM.com            // Just leave the value 0, we don't want randomness
3546400Sgblack@eecs.umich.edu        }
3556019Shines@cs.fsu.edu    }
3566019Shines@cs.fsu.edu
3576400Sgblack@eecs.umich.edu    //Copy the aux stuff
35810037SARM gem5 Developers    for (int x = 0; x < auxv.size(); x++) {
3598852Sandreas.hansson@arm.com        initVirtMem.writeBlob(auxv_array_base + x * 2 * intSize,
3606400Sgblack@eecs.umich.edu                (uint8_t*)&(auxv[x].a_type), intSize);
3618852Sandreas.hansson@arm.com        initVirtMem.writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
3626400Sgblack@eecs.umich.edu                (uint8_t*)&(auxv[x].a_val), intSize);
3636400Sgblack@eecs.umich.edu    }
3646400Sgblack@eecs.umich.edu    //Write out the terminating zeroed auxilliary vector
3656400Sgblack@eecs.umich.edu    const uint64_t zero = 0;
3668852Sandreas.hansson@arm.com    initVirtMem.writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
3676400Sgblack@eecs.umich.edu            (uint8_t*)&zero, 2 * intSize);
3686019Shines@cs.fsu.edu
3696400Sgblack@eecs.umich.edu    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
3706400Sgblack@eecs.umich.edu    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
3716019Shines@cs.fsu.edu
3728852Sandreas.hansson@arm.com    initVirtMem.writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
3736019Shines@cs.fsu.edu
3746020Sgblack@eecs.umich.edu    ThreadContext *tc = system->getThreadContext(contextIds[0]);
3756400Sgblack@eecs.umich.edu    //Set the stack pointer register
37610037SARM gem5 Developers    tc->setIntReg(spIndex, stack_min);
3776400Sgblack@eecs.umich.edu    //A pointer to a function to run when the program exits. We'll set this
3786400Sgblack@eecs.umich.edu    //to zero explicitly to make sure this isn't used.
3796400Sgblack@eecs.umich.edu    tc->setIntReg(ArgumentReg0, 0);
3806400Sgblack@eecs.umich.edu    //Set argument regs 1 and 2 to argv[0] and envp[0] respectively
3816400Sgblack@eecs.umich.edu    if (argv.size() > 0) {
3826400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg1, arg_data_base + arg_data_size -
3836400Sgblack@eecs.umich.edu                                    argv[argv.size() - 1].size() - 1);
3846400Sgblack@eecs.umich.edu    } else {
3856400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg1, 0);
3866400Sgblack@eecs.umich.edu    }
3876400Sgblack@eecs.umich.edu    if (envp.size() > 0) {
3886400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg2, env_data_base + env_data_size -
3896400Sgblack@eecs.umich.edu                                    envp[envp.size() - 1].size() - 1);
3906400Sgblack@eecs.umich.edu    } else {
3916400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg2, 0);
3926400Sgblack@eecs.umich.edu    }
3936019Shines@cs.fsu.edu
3947720Sgblack@eecs.umich.edu    PCState pc;
3957720Sgblack@eecs.umich.edu    pc.thumb(arch == ObjectFile::Thumb);
3967720Sgblack@eecs.umich.edu    pc.nextThumb(pc.thumb());
39710037SARM gem5 Developers    pc.aarch64(arch == ObjectFile::Arm64);
39810037SARM gem5 Developers    pc.nextAArch64(pc.aarch64());
39911389Sbrandon.potter@amd.com    pc.set(getStartPC() & ~mask(1));
4007720Sgblack@eecs.umich.edu    tc->pcState(pc);
4016400Sgblack@eecs.umich.edu
4026400Sgblack@eecs.umich.edu    //Align the "stack_min" to a page boundary.
4036400Sgblack@eecs.umich.edu    stack_min = roundDown(stack_min, pageSize);
4046019Shines@cs.fsu.edu}
4056019Shines@cs.fsu.edu
4066020Sgblack@eecs.umich.eduArmISA::IntReg
40710037SARM gem5 DevelopersArmLiveProcess32::getSyscallArg(ThreadContext *tc, int &i)
4086020Sgblack@eecs.umich.edu{
4097441SAli.Saidi@ARM.com    assert(i < 6);
4106701Sgblack@eecs.umich.edu    return tc->readIntReg(ArgumentReg0 + i++);
4116020Sgblack@eecs.umich.edu}
4126020Sgblack@eecs.umich.edu
41310037SARM gem5 DevelopersArmISA::IntReg
41410037SARM gem5 DevelopersArmLiveProcess64::getSyscallArg(ThreadContext *tc, int &i)
41510037SARM gem5 Developers{
41610037SARM gem5 Developers    assert(i < 8);
41710037SARM gem5 Developers    return tc->readIntReg(ArgumentReg0 + i++);
41810037SARM gem5 Developers}
41910037SARM gem5 Developers
42010037SARM gem5 DevelopersArmISA::IntReg
42110037SARM gem5 DevelopersArmLiveProcess32::getSyscallArg(ThreadContext *tc, int &i, int width)
4227441SAli.Saidi@ARM.com{
4237441SAli.Saidi@ARM.com    assert(width == 32 || width == 64);
4247441SAli.Saidi@ARM.com    if (width == 32)
4257441SAli.Saidi@ARM.com        return getSyscallArg(tc, i);
4267441SAli.Saidi@ARM.com
4277441SAli.Saidi@ARM.com    // 64 bit arguments are passed starting in an even register
4287441SAli.Saidi@ARM.com    if (i % 2 != 0)
4297441SAli.Saidi@ARM.com       i++;
4307441SAli.Saidi@ARM.com
4317441SAli.Saidi@ARM.com    // Registers r0-r6 can be used
4327441SAli.Saidi@ARM.com    assert(i < 5);
4337441SAli.Saidi@ARM.com    uint64_t val;
4347441SAli.Saidi@ARM.com    val = tc->readIntReg(ArgumentReg0 + i++);
4357441SAli.Saidi@ARM.com    val |= ((uint64_t)tc->readIntReg(ArgumentReg0 + i++) << 32);
4367441SAli.Saidi@ARM.com    return val;
4377441SAli.Saidi@ARM.com}
4387441SAli.Saidi@ARM.com
43910037SARM gem5 DevelopersArmISA::IntReg
44010037SARM gem5 DevelopersArmLiveProcess64::getSyscallArg(ThreadContext *tc, int &i, int width)
44110037SARM gem5 Developers{
44210037SARM gem5 Developers    return getSyscallArg(tc, i);
44310037SARM gem5 Developers}
44410037SARM gem5 Developers
4457441SAli.Saidi@ARM.com
4466020Sgblack@eecs.umich.eduvoid
44710037SARM gem5 DevelopersArmLiveProcess32::setSyscallArg(ThreadContext *tc, int i, ArmISA::IntReg val)
4486020Sgblack@eecs.umich.edu{
44910037SARM gem5 Developers    assert(i < 6);
4506020Sgblack@eecs.umich.edu    tc->setIntReg(ArgumentReg0 + i, val);
4516020Sgblack@eecs.umich.edu}
4526020Sgblack@eecs.umich.edu
4536020Sgblack@eecs.umich.eduvoid
45410037SARM gem5 DevelopersArmLiveProcess64::setSyscallArg(ThreadContext *tc,
45510037SARM gem5 Developers        int i, ArmISA::IntReg val)
45610037SARM gem5 Developers{
45710037SARM gem5 Developers    assert(i < 8);
45810037SARM gem5 Developers    tc->setIntReg(ArgumentReg0 + i, val);
45910037SARM gem5 Developers}
46010037SARM gem5 Developers
46110037SARM gem5 Developersvoid
46210223Ssteve.reinhardt@amd.comArmLiveProcess32::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
4636020Sgblack@eecs.umich.edu{
46410810Sbr@bsdpad.com
46510810Sbr@bsdpad.com    if (objFile->getOpSys() == ObjectFile::FreeBSD) {
46610810Sbr@bsdpad.com        // Decode return value
46710810Sbr@bsdpad.com        if (sysret.encodedValue() >= 0)
46810810Sbr@bsdpad.com            // FreeBSD checks the carry bit to determine if syscall is succeeded
46910810Sbr@bsdpad.com            tc->setCCReg(CCREG_C, 0);
47010810Sbr@bsdpad.com        else {
47110810Sbr@bsdpad.com            sysret = -sysret.encodedValue();
47210810Sbr@bsdpad.com        }
47310810Sbr@bsdpad.com    }
47410810Sbr@bsdpad.com
47510223Ssteve.reinhardt@amd.com    tc->setIntReg(ReturnValueReg, sysret.encodedValue());
4766020Sgblack@eecs.umich.edu}
47710037SARM gem5 Developers
47810037SARM gem5 Developersvoid
47910223Ssteve.reinhardt@amd.comArmLiveProcess64::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
48010037SARM gem5 Developers{
48110810Sbr@bsdpad.com
48210810Sbr@bsdpad.com    if (objFile->getOpSys() == ObjectFile::FreeBSD) {
48310810Sbr@bsdpad.com        // Decode return value
48410810Sbr@bsdpad.com        if (sysret.encodedValue() >= 0)
48510810Sbr@bsdpad.com            // FreeBSD checks the carry bit to determine if syscall is succeeded
48610810Sbr@bsdpad.com            tc->setCCReg(CCREG_C, 0);
48710810Sbr@bsdpad.com        else {
48810810Sbr@bsdpad.com            sysret = -sysret.encodedValue();
48910810Sbr@bsdpad.com        }
49010810Sbr@bsdpad.com    }
49110810Sbr@bsdpad.com
49210223Ssteve.reinhardt@amd.com    tc->setIntReg(ReturnValueReg, sysret.encodedValue());
49310037SARM gem5 Developers}
494