process.cc revision 11389
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
446019Shines@cs.fsu.edu#include "arch/arm/isa_traits.hh"
456019Shines@cs.fsu.edu#include "arch/arm/process.hh"
466019Shines@cs.fsu.edu#include "arch/arm/types.hh"
476019Shines@cs.fsu.edu#include "base/loader/elf_object.hh"
486019Shines@cs.fsu.edu#include "base/loader/object_file.hh"
496019Shines@cs.fsu.edu#include "base/misc.hh"
506019Shines@cs.fsu.edu#include "cpu/thread_context.hh"
518232Snate@binkert.org#include "debug/Stack.hh"
526019Shines@cs.fsu.edu#include "mem/page_table.hh"
537678Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
546019Shines@cs.fsu.edu#include "sim/process_impl.hh"
556019Shines@cs.fsu.edu#include "sim/system.hh"
566019Shines@cs.fsu.edu
576019Shines@cs.fsu.eduusing namespace std;
586019Shines@cs.fsu.eduusing namespace ArmISA;
596019Shines@cs.fsu.edu
607096Sgblack@eecs.umich.eduArmLiveProcess::ArmLiveProcess(LiveProcessParams *params, ObjectFile *objFile,
617096Sgblack@eecs.umich.edu                               ObjectFile::Arch _arch)
627096Sgblack@eecs.umich.edu    : LiveProcess(params, objFile), arch(_arch)
636019Shines@cs.fsu.edu{
6410037SARM gem5 Developers}
6510037SARM gem5 Developers
6610037SARM gem5 DevelopersArmLiveProcess32::ArmLiveProcess32(LiveProcessParams *params,
6710037SARM gem5 Developers                                   ObjectFile *objFile, ObjectFile::Arch _arch)
6810037SARM gem5 Developers    : ArmLiveProcess(params, objFile, _arch)
6910037SARM gem5 Developers{
706400Sgblack@eecs.umich.edu    stack_base = 0xbf000000L;
716019Shines@cs.fsu.edu
726019Shines@cs.fsu.edu    // Set pointer for next thread stack.  Reserve 8M for main stack.
736019Shines@cs.fsu.edu    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
746019Shines@cs.fsu.edu
756019Shines@cs.fsu.edu    // Set up break point (Top of Heap)
766019Shines@cs.fsu.edu    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
7710318Sandreas.hansson@arm.com    brk_point = roundUp(brk_point, PageBytes);
786019Shines@cs.fsu.edu
796019Shines@cs.fsu.edu    // Set up region for mmaps. For now, start at bottom of kuseg space.
8011386Ssteve.reinhardt@amd.com    mmap_end = 0x40000000L;
816019Shines@cs.fsu.edu}
826019Shines@cs.fsu.edu
8310037SARM gem5 DevelopersArmLiveProcess64::ArmLiveProcess64(LiveProcessParams *params,
8410037SARM gem5 Developers                                   ObjectFile *objFile, ObjectFile::Arch _arch)
8510037SARM gem5 Developers    : ArmLiveProcess(params, objFile, _arch)
8610037SARM gem5 Developers{
8710037SARM gem5 Developers    stack_base = 0x7fffff0000L;
8810037SARM gem5 Developers
8910037SARM gem5 Developers    // Set pointer for next thread stack.  Reserve 8M for main stack.
9010037SARM gem5 Developers    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
9110037SARM gem5 Developers
9210037SARM gem5 Developers    // Set up break point (Top of Heap)
9310037SARM gem5 Developers    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
9410318Sandreas.hansson@arm.com    brk_point = roundUp(brk_point, PageBytes);
9510037SARM gem5 Developers
9610037SARM gem5 Developers    // Set up region for mmaps. For now, start at bottom of kuseg space.
9711386Ssteve.reinhardt@amd.com    mmap_end = 0x4000000000L;
9810037SARM gem5 Developers}
9910037SARM gem5 Developers
1006019Shines@cs.fsu.eduvoid
10110037SARM gem5 DevelopersArmLiveProcess32::initState()
1026019Shines@cs.fsu.edu{
1038216Ssaidi@eecs.umich.edu    LiveProcess::initState();
10410318Sandreas.hansson@arm.com    argsInit<uint32_t>(PageBytes, INTREG_SP);
1057640Sgblack@eecs.umich.edu    for (int i = 0; i < contextIds.size(); i++) {
1067640Sgblack@eecs.umich.edu        ThreadContext * tc = system->getThreadContext(contextIds[i]);
1077640Sgblack@eecs.umich.edu        CPACR cpacr = tc->readMiscReg(MISCREG_CPACR);
1087640Sgblack@eecs.umich.edu        // Enable the floating point coprocessors.
1097640Sgblack@eecs.umich.edu        cpacr.cp10 = 0x3;
1107640Sgblack@eecs.umich.edu        cpacr.cp11 = 0x3;
1117640Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_CPACR, cpacr);
1127640Sgblack@eecs.umich.edu        // Generically enable floating point support.
1137640Sgblack@eecs.umich.edu        FPEXC fpexc = tc->readMiscReg(MISCREG_FPEXC);
1147640Sgblack@eecs.umich.edu        fpexc.en = 1;
1157640Sgblack@eecs.umich.edu        tc->setMiscReg(MISCREG_FPEXC, fpexc);
1167640Sgblack@eecs.umich.edu    }
1176019Shines@cs.fsu.edu}
1186019Shines@cs.fsu.edu
1196019Shines@cs.fsu.eduvoid
12010037SARM gem5 DevelopersArmLiveProcess64::initState()
1216019Shines@cs.fsu.edu{
12210037SARM gem5 Developers    LiveProcess::initState();
12310318Sandreas.hansson@arm.com    argsInit<uint64_t>(PageBytes, INTREG_SP0);
12410037SARM gem5 Developers    for (int i = 0; i < contextIds.size(); i++) {
12510037SARM gem5 Developers        ThreadContext * tc = system->getThreadContext(contextIds[i]);
12610037SARM gem5 Developers        CPSR cpsr = tc->readMiscReg(MISCREG_CPSR);
12710037SARM gem5 Developers        cpsr.mode = MODE_EL0T;
12810037SARM gem5 Developers        tc->setMiscReg(MISCREG_CPSR, cpsr);
12910037SARM gem5 Developers        CPACR cpacr = tc->readMiscReg(MISCREG_CPACR_EL1);
13010037SARM gem5 Developers        // Enable the floating point coprocessors.
13110037SARM gem5 Developers        cpacr.cp10 = 0x3;
13210037SARM gem5 Developers        cpacr.cp11 = 0x3;
13310037SARM gem5 Developers        tc->setMiscReg(MISCREG_CPACR_EL1, cpacr);
13410037SARM gem5 Developers        // Generically enable floating point support.
13510037SARM gem5 Developers        FPEXC fpexc = tc->readMiscReg(MISCREG_FPEXC);
13610037SARM gem5 Developers        fpexc.en = 1;
13710037SARM gem5 Developers        tc->setMiscReg(MISCREG_FPEXC, fpexc);
13810037SARM gem5 Developers    }
13910037SARM gem5 Developers}
14010037SARM gem5 Developers
14110037SARM gem5 Developerstemplate <class IntType>
14210037SARM gem5 Developersvoid
14310037SARM gem5 DevelopersArmLiveProcess::argsInit(int pageSize, IntRegIndex spIndex)
14410037SARM gem5 Developers{
14510037SARM gem5 Developers    int intSize = sizeof(IntType);
14610037SARM gem5 Developers
14710037SARM gem5 Developers    typedef AuxVector<IntType> auxv_t;
1486400Sgblack@eecs.umich.edu    std::vector<auxv_t> auxv;
1496400Sgblack@eecs.umich.edu
1506400Sgblack@eecs.umich.edu    string filename;
1516400Sgblack@eecs.umich.edu    if (argv.size() < 1)
1526400Sgblack@eecs.umich.edu        filename = "";
1536400Sgblack@eecs.umich.edu    else
1546400Sgblack@eecs.umich.edu        filename = argv[0];
1556400Sgblack@eecs.umich.edu
1566400Sgblack@eecs.umich.edu    //We want 16 byte alignment
1576400Sgblack@eecs.umich.edu    uint64_t align = 16;
1586400Sgblack@eecs.umich.edu
15911389Sbrandon.potter@amd.com    // Patch the ld_bias for dynamic executables.
16011389Sbrandon.potter@amd.com    updateBias();
16111389Sbrandon.potter@amd.com
1626019Shines@cs.fsu.edu    // load object file into target memory
1636019Shines@cs.fsu.edu    objFile->loadSections(initVirtMem);
1646019Shines@cs.fsu.edu
1656400Sgblack@eecs.umich.edu    enum ArmCpuFeature {
1666400Sgblack@eecs.umich.edu        Arm_Swp = 1 << 0,
1676400Sgblack@eecs.umich.edu        Arm_Half = 1 << 1,
1686400Sgblack@eecs.umich.edu        Arm_Thumb = 1 << 2,
1696400Sgblack@eecs.umich.edu        Arm_26Bit = 1 << 3,
1706400Sgblack@eecs.umich.edu        Arm_FastMult = 1 << 4,
1716400Sgblack@eecs.umich.edu        Arm_Fpa = 1 << 5,
1726400Sgblack@eecs.umich.edu        Arm_Vfp = 1 << 6,
1736400Sgblack@eecs.umich.edu        Arm_Edsp = 1 << 7,
1746400Sgblack@eecs.umich.edu        Arm_Java = 1 << 8,
1756400Sgblack@eecs.umich.edu        Arm_Iwmmxt = 1 << 9,
1767414SAli.Saidi@ARM.com        Arm_Crunch = 1 << 10,
1777414SAli.Saidi@ARM.com        Arm_ThumbEE = 1 << 11,
1787414SAli.Saidi@ARM.com        Arm_Neon = 1 << 12,
1797414SAli.Saidi@ARM.com        Arm_Vfpv3 = 1 << 13,
1807414SAli.Saidi@ARM.com        Arm_Vfpv3d16 = 1 << 14
1816400Sgblack@eecs.umich.edu    };
1826400Sgblack@eecs.umich.edu
1836400Sgblack@eecs.umich.edu    //Setup the auxilliary vectors. These will already have endian conversion.
1846400Sgblack@eecs.umich.edu    //Auxilliary vectors are loaded only for elf formatted executables.
1856400Sgblack@eecs.umich.edu    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
1866400Sgblack@eecs.umich.edu    if (elfObject) {
1876400Sgblack@eecs.umich.edu
18810810Sbr@bsdpad.com        if (objFile->getOpSys() == ObjectFile::Linux) {
18910810Sbr@bsdpad.com            IntType features =
19010810Sbr@bsdpad.com                Arm_Swp |
19110810Sbr@bsdpad.com                Arm_Half |
19210810Sbr@bsdpad.com                Arm_Thumb |
19310810Sbr@bsdpad.com//                Arm_26Bit |
19410810Sbr@bsdpad.com                Arm_FastMult |
19510810Sbr@bsdpad.com//                Arm_Fpa |
19610810Sbr@bsdpad.com                Arm_Vfp |
19710810Sbr@bsdpad.com                Arm_Edsp |
19810810Sbr@bsdpad.com//                Arm_Java |
19910810Sbr@bsdpad.com//                Arm_Iwmmxt |
20010810Sbr@bsdpad.com//                Arm_Crunch |
20110810Sbr@bsdpad.com                Arm_ThumbEE |
20210810Sbr@bsdpad.com                Arm_Neon |
20310810Sbr@bsdpad.com                Arm_Vfpv3 |
20410810Sbr@bsdpad.com                Arm_Vfpv3d16 |
20510810Sbr@bsdpad.com                0;
20610810Sbr@bsdpad.com
20710810Sbr@bsdpad.com            //Bits which describe the system hardware capabilities
20810810Sbr@bsdpad.com            //XXX Figure out what these should be
20910810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_HWCAP, features));
21010810Sbr@bsdpad.com            //Frequency at which times() increments
21110810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_CLKTCK, 0x64));
21210810Sbr@bsdpad.com            //Whether to enable "secure mode" in the executable
21310810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_SECURE, 0));
21410810Sbr@bsdpad.com            // Pointer to 16 bytes of random data
21510810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_RANDOM, 0));
21610810Sbr@bsdpad.com            //The filename of the program
21710810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_EXECFN, 0));
21810810Sbr@bsdpad.com            //The string "v71" -- ARM v7 architecture
21910810Sbr@bsdpad.com            auxv.push_back(auxv_t(M5_AT_PLATFORM, 0));
22010810Sbr@bsdpad.com        }
22110810Sbr@bsdpad.com
2226400Sgblack@eecs.umich.edu        //The system page size
22310318Sandreas.hansson@arm.com        auxv.push_back(auxv_t(M5_AT_PAGESZ, ArmISA::PageBytes));
2246400Sgblack@eecs.umich.edu        // For statically linked executables, this is the virtual address of the
2256400Sgblack@eecs.umich.edu        // program header tables if they appear in the executable image
2266400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
2276400Sgblack@eecs.umich.edu        // This is the size of a program header entry from the elf file.
2286400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
2296400Sgblack@eecs.umich.edu        // This is the number of program headers from the original elf file.
2306400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
23111389Sbrandon.potter@amd.com        // This is the base address of the ELF interpreter; it should be
23211389Sbrandon.potter@amd.com        // zero for static executables or contain the base address for
23311389Sbrandon.potter@amd.com        // dynamic executables.
23411389Sbrandon.potter@amd.com        auxv.push_back(auxv_t(M5_AT_BASE, getBias()));
2356400Sgblack@eecs.umich.edu        //XXX Figure out what this should be.
2366400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
2376400Sgblack@eecs.umich.edu        //The entry point to the program
2386400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
2396400Sgblack@eecs.umich.edu        //Different user and group IDs
2406400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_UID, uid()));
2416400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_EUID, euid()));
2426400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_GID, gid()));
2436400Sgblack@eecs.umich.edu        auxv.push_back(auxv_t(M5_AT_EGID, egid()));
2446400Sgblack@eecs.umich.edu    }
2456400Sgblack@eecs.umich.edu
2466400Sgblack@eecs.umich.edu    //Figure out how big the initial stack nedes to be
2476400Sgblack@eecs.umich.edu
2486400Sgblack@eecs.umich.edu    // A sentry NULL void pointer at the top of the stack.
2496400Sgblack@eecs.umich.edu    int sentry_size = intSize;
2506400Sgblack@eecs.umich.edu
2517414SAli.Saidi@ARM.com    string platform = "v71";
2526400Sgblack@eecs.umich.edu    int platform_size = platform.size() + 1;
2536400Sgblack@eecs.umich.edu
2547414SAli.Saidi@ARM.com    // Bytes for AT_RANDOM above, we'll just keep them 0
2557414SAli.Saidi@ARM.com    int aux_random_size = 16; // as per the specification
2567414SAli.Saidi@ARM.com
2576400Sgblack@eecs.umich.edu    // The aux vectors are put on the stack in two groups. The first group are
2586400Sgblack@eecs.umich.edu    // the vectors that are generated as the elf is loaded. The second group
2596400Sgblack@eecs.umich.edu    // are the ones that were computed ahead of time and include the platform
2606400Sgblack@eecs.umich.edu    // string.
2616400Sgblack@eecs.umich.edu    int aux_data_size = filename.size() + 1;
2626400Sgblack@eecs.umich.edu
2636400Sgblack@eecs.umich.edu    int env_data_size = 0;
2646400Sgblack@eecs.umich.edu    for (int i = 0; i < envp.size(); ++i) {
2656400Sgblack@eecs.umich.edu        env_data_size += envp[i].size() + 1;
2666400Sgblack@eecs.umich.edu    }
2676019Shines@cs.fsu.edu    int arg_data_size = 0;
2686019Shines@cs.fsu.edu    for (int i = 0; i < argv.size(); ++i) {
2696019Shines@cs.fsu.edu        arg_data_size += argv[i].size() + 1;
2706019Shines@cs.fsu.edu    }
2716400Sgblack@eecs.umich.edu
2726400Sgblack@eecs.umich.edu    int info_block_size =
2736400Sgblack@eecs.umich.edu        sentry_size + env_data_size + arg_data_size +
2747414SAli.Saidi@ARM.com        aux_data_size + platform_size + aux_random_size;
2756400Sgblack@eecs.umich.edu
2766400Sgblack@eecs.umich.edu    //Each auxilliary vector is two 4 byte words
2776400Sgblack@eecs.umich.edu    int aux_array_size = intSize * 2 * (auxv.size() + 1);
2786400Sgblack@eecs.umich.edu
2796400Sgblack@eecs.umich.edu    int envp_array_size = intSize * (envp.size() + 1);
2806400Sgblack@eecs.umich.edu    int argv_array_size = intSize * (argv.size() + 1);
2816400Sgblack@eecs.umich.edu
2826400Sgblack@eecs.umich.edu    int argc_size = intSize;
2836400Sgblack@eecs.umich.edu
2846400Sgblack@eecs.umich.edu    //Figure out the size of the contents of the actual initial frame
2856400Sgblack@eecs.umich.edu    int frame_size =
2866400Sgblack@eecs.umich.edu        info_block_size +
2876400Sgblack@eecs.umich.edu        aux_array_size +
2886400Sgblack@eecs.umich.edu        envp_array_size +
2896400Sgblack@eecs.umich.edu        argv_array_size +
2906400Sgblack@eecs.umich.edu        argc_size;
2916400Sgblack@eecs.umich.edu
2926400Sgblack@eecs.umich.edu    //There needs to be padding after the auxiliary vector data so that the
2936400Sgblack@eecs.umich.edu    //very bottom of the stack is aligned properly.
2946400Sgblack@eecs.umich.edu    int partial_size = frame_size;
2956400Sgblack@eecs.umich.edu    int aligned_partial_size = roundUp(partial_size, align);
2966400Sgblack@eecs.umich.edu    int aux_padding = aligned_partial_size - partial_size;
2976400Sgblack@eecs.umich.edu
2986400Sgblack@eecs.umich.edu    int space_needed = frame_size + aux_padding;
2996400Sgblack@eecs.umich.edu
3006400Sgblack@eecs.umich.edu    stack_min = stack_base - space_needed;
3016400Sgblack@eecs.umich.edu    stack_min = roundDown(stack_min, align);
3026400Sgblack@eecs.umich.edu    stack_size = stack_base - stack_min;
3036400Sgblack@eecs.umich.edu
3046400Sgblack@eecs.umich.edu    // map memory
3058601Ssteve.reinhardt@amd.com    allocateMem(roundDown(stack_min, pageSize), roundUp(stack_size, pageSize));
3066400Sgblack@eecs.umich.edu
3076400Sgblack@eecs.umich.edu    // map out initial stack contents
30810037SARM gem5 Developers    IntType sentry_base = stack_base - sentry_size;
30910037SARM gem5 Developers    IntType aux_data_base = sentry_base - aux_data_size;
31010037SARM gem5 Developers    IntType env_data_base = aux_data_base - env_data_size;
31110037SARM gem5 Developers    IntType arg_data_base = env_data_base - arg_data_size;
31210037SARM gem5 Developers    IntType platform_base = arg_data_base - platform_size;
31310037SARM gem5 Developers    IntType aux_random_base = platform_base - aux_random_size;
31410037SARM gem5 Developers    IntType auxv_array_base = aux_random_base - aux_array_size - aux_padding;
31510037SARM gem5 Developers    IntType envp_array_base = auxv_array_base - envp_array_size;
31610037SARM gem5 Developers    IntType argv_array_base = envp_array_base - argv_array_size;
31710037SARM gem5 Developers    IntType argc_base = argv_array_base - argc_size;
3186400Sgblack@eecs.umich.edu
3196400Sgblack@eecs.umich.edu    DPRINTF(Stack, "The addresses of items on the initial stack:\n");
3206400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
3216400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - env data\n", env_data_base);
3226400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
3237414SAli.Saidi@ARM.com    DPRINTF(Stack, "0x%x - random data\n", aux_random_base);
3246400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - platform base\n", platform_base);
3256400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
3266400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
3276400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
3286400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - argc \n", argc_base);
3296400Sgblack@eecs.umich.edu    DPRINTF(Stack, "0x%x - stack min\n", stack_min);
3306400Sgblack@eecs.umich.edu
3316400Sgblack@eecs.umich.edu    // write contents to stack
3326400Sgblack@eecs.umich.edu
3336400Sgblack@eecs.umich.edu    // figure out argc
33410037SARM gem5 Developers    IntType argc = argv.size();
33510037SARM gem5 Developers    IntType guestArgc = ArmISA::htog(argc);
3366400Sgblack@eecs.umich.edu
3376400Sgblack@eecs.umich.edu    //Write out the sentry void *
33810037SARM gem5 Developers    IntType sentry_NULL = 0;
3398852Sandreas.hansson@arm.com    initVirtMem.writeBlob(sentry_base,
3406400Sgblack@eecs.umich.edu            (uint8_t*)&sentry_NULL, sentry_size);
3416400Sgblack@eecs.umich.edu
3426400Sgblack@eecs.umich.edu    //Fix up the aux vectors which point to other data
3436400Sgblack@eecs.umich.edu    for (int i = auxv.size() - 1; i >= 0; i--) {
3446400Sgblack@eecs.umich.edu        if (auxv[i].a_type == M5_AT_PLATFORM) {
3456400Sgblack@eecs.umich.edu            auxv[i].a_val = platform_base;
3468852Sandreas.hansson@arm.com            initVirtMem.writeString(platform_base, platform.c_str());
3476400Sgblack@eecs.umich.edu        } else if (auxv[i].a_type == M5_AT_EXECFN) {
3486400Sgblack@eecs.umich.edu            auxv[i].a_val = aux_data_base;
3498852Sandreas.hansson@arm.com            initVirtMem.writeString(aux_data_base, filename.c_str());
3507414SAli.Saidi@ARM.com        } else if (auxv[i].a_type == M5_AT_RANDOM) {
3517414SAli.Saidi@ARM.com            auxv[i].a_val = aux_random_base;
3527414SAli.Saidi@ARM.com            // Just leave the value 0, we don't want randomness
3536400Sgblack@eecs.umich.edu        }
3546019Shines@cs.fsu.edu    }
3556019Shines@cs.fsu.edu
3566400Sgblack@eecs.umich.edu    //Copy the aux stuff
35710037SARM gem5 Developers    for (int x = 0; x < auxv.size(); x++) {
3588852Sandreas.hansson@arm.com        initVirtMem.writeBlob(auxv_array_base + x * 2 * intSize,
3596400Sgblack@eecs.umich.edu                (uint8_t*)&(auxv[x].a_type), intSize);
3608852Sandreas.hansson@arm.com        initVirtMem.writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
3616400Sgblack@eecs.umich.edu                (uint8_t*)&(auxv[x].a_val), intSize);
3626400Sgblack@eecs.umich.edu    }
3636400Sgblack@eecs.umich.edu    //Write out the terminating zeroed auxilliary vector
3646400Sgblack@eecs.umich.edu    const uint64_t zero = 0;
3658852Sandreas.hansson@arm.com    initVirtMem.writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
3666400Sgblack@eecs.umich.edu            (uint8_t*)&zero, 2 * intSize);
3676019Shines@cs.fsu.edu
3686400Sgblack@eecs.umich.edu    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
3696400Sgblack@eecs.umich.edu    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
3706019Shines@cs.fsu.edu
3718852Sandreas.hansson@arm.com    initVirtMem.writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
3726019Shines@cs.fsu.edu
3736020Sgblack@eecs.umich.edu    ThreadContext *tc = system->getThreadContext(contextIds[0]);
3746400Sgblack@eecs.umich.edu    //Set the stack pointer register
37510037SARM gem5 Developers    tc->setIntReg(spIndex, stack_min);
3766400Sgblack@eecs.umich.edu    //A pointer to a function to run when the program exits. We'll set this
3776400Sgblack@eecs.umich.edu    //to zero explicitly to make sure this isn't used.
3786400Sgblack@eecs.umich.edu    tc->setIntReg(ArgumentReg0, 0);
3796400Sgblack@eecs.umich.edu    //Set argument regs 1 and 2 to argv[0] and envp[0] respectively
3806400Sgblack@eecs.umich.edu    if (argv.size() > 0) {
3816400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg1, arg_data_base + arg_data_size -
3826400Sgblack@eecs.umich.edu                                    argv[argv.size() - 1].size() - 1);
3836400Sgblack@eecs.umich.edu    } else {
3846400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg1, 0);
3856400Sgblack@eecs.umich.edu    }
3866400Sgblack@eecs.umich.edu    if (envp.size() > 0) {
3876400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg2, env_data_base + env_data_size -
3886400Sgblack@eecs.umich.edu                                    envp[envp.size() - 1].size() - 1);
3896400Sgblack@eecs.umich.edu    } else {
3906400Sgblack@eecs.umich.edu        tc->setIntReg(ArgumentReg2, 0);
3916400Sgblack@eecs.umich.edu    }
3926019Shines@cs.fsu.edu
3937720Sgblack@eecs.umich.edu    PCState pc;
3947720Sgblack@eecs.umich.edu    pc.thumb(arch == ObjectFile::Thumb);
3957720Sgblack@eecs.umich.edu    pc.nextThumb(pc.thumb());
39610037SARM gem5 Developers    pc.aarch64(arch == ObjectFile::Arm64);
39710037SARM gem5 Developers    pc.nextAArch64(pc.aarch64());
39811389Sbrandon.potter@amd.com    pc.set(getStartPC() & ~mask(1));
3997720Sgblack@eecs.umich.edu    tc->pcState(pc);
4006400Sgblack@eecs.umich.edu
4016400Sgblack@eecs.umich.edu    //Align the "stack_min" to a page boundary.
4026400Sgblack@eecs.umich.edu    stack_min = roundDown(stack_min, pageSize);
4036019Shines@cs.fsu.edu}
4046019Shines@cs.fsu.edu
4056020Sgblack@eecs.umich.eduArmISA::IntReg
40610037SARM gem5 DevelopersArmLiveProcess32::getSyscallArg(ThreadContext *tc, int &i)
4076020Sgblack@eecs.umich.edu{
4087441SAli.Saidi@ARM.com    assert(i < 6);
4096701Sgblack@eecs.umich.edu    return tc->readIntReg(ArgumentReg0 + i++);
4106020Sgblack@eecs.umich.edu}
4116020Sgblack@eecs.umich.edu
41210037SARM gem5 DevelopersArmISA::IntReg
41310037SARM gem5 DevelopersArmLiveProcess64::getSyscallArg(ThreadContext *tc, int &i)
41410037SARM gem5 Developers{
41510037SARM gem5 Developers    assert(i < 8);
41610037SARM gem5 Developers    return tc->readIntReg(ArgumentReg0 + i++);
41710037SARM gem5 Developers}
41810037SARM gem5 Developers
41910037SARM gem5 DevelopersArmISA::IntReg
42010037SARM gem5 DevelopersArmLiveProcess32::getSyscallArg(ThreadContext *tc, int &i, int width)
4217441SAli.Saidi@ARM.com{
4227441SAli.Saidi@ARM.com    assert(width == 32 || width == 64);
4237441SAli.Saidi@ARM.com    if (width == 32)
4247441SAli.Saidi@ARM.com        return getSyscallArg(tc, i);
4257441SAli.Saidi@ARM.com
4267441SAli.Saidi@ARM.com    // 64 bit arguments are passed starting in an even register
4277441SAli.Saidi@ARM.com    if (i % 2 != 0)
4287441SAli.Saidi@ARM.com       i++;
4297441SAli.Saidi@ARM.com
4307441SAli.Saidi@ARM.com    // Registers r0-r6 can be used
4317441SAli.Saidi@ARM.com    assert(i < 5);
4327441SAli.Saidi@ARM.com    uint64_t val;
4337441SAli.Saidi@ARM.com    val = tc->readIntReg(ArgumentReg0 + i++);
4347441SAli.Saidi@ARM.com    val |= ((uint64_t)tc->readIntReg(ArgumentReg0 + i++) << 32);
4357441SAli.Saidi@ARM.com    return val;
4367441SAli.Saidi@ARM.com}
4377441SAli.Saidi@ARM.com
43810037SARM gem5 DevelopersArmISA::IntReg
43910037SARM gem5 DevelopersArmLiveProcess64::getSyscallArg(ThreadContext *tc, int &i, int width)
44010037SARM gem5 Developers{
44110037SARM gem5 Developers    return getSyscallArg(tc, i);
44210037SARM gem5 Developers}
44310037SARM gem5 Developers
4447441SAli.Saidi@ARM.com
4456020Sgblack@eecs.umich.eduvoid
44610037SARM gem5 DevelopersArmLiveProcess32::setSyscallArg(ThreadContext *tc, int i, ArmISA::IntReg val)
4476020Sgblack@eecs.umich.edu{
44810037SARM gem5 Developers    assert(i < 6);
4496020Sgblack@eecs.umich.edu    tc->setIntReg(ArgumentReg0 + i, val);
4506020Sgblack@eecs.umich.edu}
4516020Sgblack@eecs.umich.edu
4526020Sgblack@eecs.umich.eduvoid
45310037SARM gem5 DevelopersArmLiveProcess64::setSyscallArg(ThreadContext *tc,
45410037SARM gem5 Developers        int i, ArmISA::IntReg val)
45510037SARM gem5 Developers{
45610037SARM gem5 Developers    assert(i < 8);
45710037SARM gem5 Developers    tc->setIntReg(ArgumentReg0 + i, val);
45810037SARM gem5 Developers}
45910037SARM gem5 Developers
46010037SARM gem5 Developersvoid
46110223Ssteve.reinhardt@amd.comArmLiveProcess32::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
4626020Sgblack@eecs.umich.edu{
46310810Sbr@bsdpad.com
46410810Sbr@bsdpad.com    if (objFile->getOpSys() == ObjectFile::FreeBSD) {
46510810Sbr@bsdpad.com        // Decode return value
46610810Sbr@bsdpad.com        if (sysret.encodedValue() >= 0)
46710810Sbr@bsdpad.com            // FreeBSD checks the carry bit to determine if syscall is succeeded
46810810Sbr@bsdpad.com            tc->setCCReg(CCREG_C, 0);
46910810Sbr@bsdpad.com        else {
47010810Sbr@bsdpad.com            sysret = -sysret.encodedValue();
47110810Sbr@bsdpad.com        }
47210810Sbr@bsdpad.com    }
47310810Sbr@bsdpad.com
47410223Ssteve.reinhardt@amd.com    tc->setIntReg(ReturnValueReg, sysret.encodedValue());
4756020Sgblack@eecs.umich.edu}
47610037SARM gem5 Developers
47710037SARM gem5 Developersvoid
47810223Ssteve.reinhardt@amd.comArmLiveProcess64::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
47910037SARM gem5 Developers{
48010810Sbr@bsdpad.com
48110810Sbr@bsdpad.com    if (objFile->getOpSys() == ObjectFile::FreeBSD) {
48210810Sbr@bsdpad.com        // Decode return value
48310810Sbr@bsdpad.com        if (sysret.encodedValue() >= 0)
48410810Sbr@bsdpad.com            // FreeBSD checks the carry bit to determine if syscall is succeeded
48510810Sbr@bsdpad.com            tc->setCCReg(CCREG_C, 0);
48610810Sbr@bsdpad.com        else {
48710810Sbr@bsdpad.com            sysret = -sysret.encodedValue();
48810810Sbr@bsdpad.com        }
48910810Sbr@bsdpad.com    }
49010810Sbr@bsdpad.com
49110223Ssteve.reinhardt@amd.com    tc->setIntReg(ReturnValueReg, sysret.encodedValue());
49210037SARM gem5 Developers}
493