12207SN/A/*
22207SN/A * Copyright (c) 2003-2004 The Regents of The University of Michigan
32207SN/A * All rights reserved.
42207SN/A *
52207SN/A * Redistribution and use in source and binary forms, with or without
62207SN/A * modification, are permitted provided that the following conditions are
72207SN/A * met: redistributions of source code must retain the above copyright
82207SN/A * notice, this list of conditions and the following disclaimer;
92207SN/A * redistributions in binary form must reproduce the above copyright
102207SN/A * notice, this list of conditions and the following disclaimer in the
112207SN/A * documentation and/or other materials provided with the distribution;
122207SN/A * neither the name of the copyright holders nor the names of its
132207SN/A * contributors may be used to endorse or promote products derived from
142207SN/A * this software without specific prior written permission.
152207SN/A *
162207SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172207SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182207SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192207SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202207SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212207SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222207SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232207SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242207SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252207SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262207SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Gabe Black
292665Ssaidi@eecs.umich.edu *          Ali Saidi
302207SN/A */
312207SN/A
3211793Sbrandon.potter@amd.com#include "arch/sparc/process.hh"
3311793Sbrandon.potter@amd.com
343589Sgblack@eecs.umich.edu#include "arch/sparc/asi.hh"
354111Sgblack@eecs.umich.edu#include "arch/sparc/handlers.hh"
362474SN/A#include "arch/sparc/isa_traits.hh"
376335Sgblack@eecs.umich.edu#include "arch/sparc/registers.hh"
383760Sgblack@eecs.umich.edu#include "arch/sparc/types.hh"
398229Snate@binkert.org#include "base/loader/elf_object.hh"
402454SN/A#include "base/loader/object_file.hh"
4112334Sgabeblack@google.com#include "base/logging.hh"
422680Sktlim@umich.edu#include "cpu/thread_context.hh"
438232Snate@binkert.org#include "debug/Stack.hh"
442561SN/A#include "mem/page_table.hh"
4512431Sgabeblack@google.com#include "params/Process.hh"
4611854Sbrandon.potter@amd.com#include "sim/aux_vector.hh"
474434Ssaidi@eecs.umich.edu#include "sim/process_impl.hh"
4811800Sbrandon.potter@amd.com#include "sim/syscall_return.hh"
492474SN/A#include "sim/system.hh"
502207SN/A
512458SN/Ausing namespace std;
522474SN/Ausing namespace SparcISA;
532458SN/A
545958Sgblack@eecs.umich.edustatic const int FirstArgumentReg = 8;
555958Sgblack@eecs.umich.edu
562207SN/A
5712431Sgabeblack@google.comSparcProcess::SparcProcess(ProcessParams *params, ObjectFile *objFile,
5811851Sbrandon.potter@amd.com                           Addr _StackBias)
5912448Sgabeblack@google.com    : Process(params,
6012448Sgabeblack@google.com              new EmulationPageTable(params->name, params->pid, PageBytes),
6112432Sgabeblack@google.com              objFile),
6212432Sgabeblack@google.com      StackBias(_StackBias)
632474SN/A{
6412441Sgabeblack@google.com    fatal_if(params->useArchPT, "Arch page tables not implemented.");
657741Sgblack@eecs.umich.edu    // Initialize these to 0s
663415Sgblack@eecs.umich.edu    fillStart = 0;
673415Sgblack@eecs.umich.edu    spillStart = 0;
682474SN/A}
692474SN/A
707741Sgblack@eecs.umich.eduvoid
7111877Sbrandon.potter@amd.comSparcProcess::handleTrap(int trapNum, ThreadContext *tc, Fault *fault)
724111Sgblack@eecs.umich.edu{
737720Sgblack@eecs.umich.edu    PCState pc = tc->pcState();
747741Sgblack@eecs.umich.edu    switch (trapNum) {
757741Sgblack@eecs.umich.edu      case 0x01: // Software breakpoint
767720Sgblack@eecs.umich.edu        warn("Software breakpoint encountered at pc %#x.\n", pc.pc());
775128Sgblack@eecs.umich.edu        break;
787741Sgblack@eecs.umich.edu      case 0x02: // Division by zero
797720Sgblack@eecs.umich.edu        warn("Software signaled a division by zero at pc %#x.\n", pc.pc());
805128Sgblack@eecs.umich.edu        break;
817741Sgblack@eecs.umich.edu      case 0x03: // Flush window trap
825128Sgblack@eecs.umich.edu        flushWindows(tc);
835128Sgblack@eecs.umich.edu        break;
847741Sgblack@eecs.umich.edu      case 0x04: // Clean windows
855128Sgblack@eecs.umich.edu        warn("Ignoring process request for clean register "
867720Sgblack@eecs.umich.edu                "windows at pc %#x.\n", pc.pc());
875128Sgblack@eecs.umich.edu        break;
887741Sgblack@eecs.umich.edu      case 0x05: // Range check
897720Sgblack@eecs.umich.edu        warn("Software signaled a range check at pc %#x.\n", pc.pc());
905128Sgblack@eecs.umich.edu        break;
917741Sgblack@eecs.umich.edu      case 0x06: // Fix alignment
925128Sgblack@eecs.umich.edu        warn("Ignoring process request for os assisted unaligned accesses "
937720Sgblack@eecs.umich.edu                "at pc %#x.\n", pc.pc());
945128Sgblack@eecs.umich.edu        break;
957741Sgblack@eecs.umich.edu      case 0x07: // Integer overflow
967720Sgblack@eecs.umich.edu        warn("Software signaled an integer overflow at pc %#x.\n", pc.pc());
975128Sgblack@eecs.umich.edu        break;
987741Sgblack@eecs.umich.edu      case 0x32: // Get integer condition codes
995128Sgblack@eecs.umich.edu        warn("Ignoring process request to get the integer condition codes "
1007720Sgblack@eecs.umich.edu                "at pc %#x.\n", pc.pc());
1015128Sgblack@eecs.umich.edu        break;
1027741Sgblack@eecs.umich.edu      case 0x33: // Set integer condition codes
1035128Sgblack@eecs.umich.edu        warn("Ignoring process request to set the integer condition codes "
1047720Sgblack@eecs.umich.edu                "at pc %#x.\n", pc.pc());
1054111Sgblack@eecs.umich.edu        break;
1064111Sgblack@eecs.umich.edu      default:
1074111Sgblack@eecs.umich.edu        panic("Unimplemented trap to operating system: trap number %#x.\n", trapNum);
1084111Sgblack@eecs.umich.edu    }
1094111Sgblack@eecs.umich.edu}
1104111Sgblack@eecs.umich.edu
1112474SN/Avoid
11211851Sbrandon.potter@amd.comSparcProcess::initState()
1134111Sgblack@eecs.umich.edu{
11411851Sbrandon.potter@amd.com    Process::initState();
1154111Sgblack@eecs.umich.edu
1165713Shsul@eecs.umich.edu    ThreadContext *tc = system->getThreadContext(contextIds[0]);
1177741Sgblack@eecs.umich.edu    // From the SPARC ABI
1184111Sgblack@eecs.umich.edu
1197741Sgblack@eecs.umich.edu    // Setup default FP state
1205713Shsul@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_FSR, 0);
1212646Ssaidi@eecs.umich.edu
1225713Shsul@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_TICK, 0);
1234997Sgblack@eecs.umich.edu
1242561SN/A    /*
1252561SN/A     * Register window management registers
1262561SN/A     */
1272561SN/A
1287741Sgblack@eecs.umich.edu    // No windows contain info from other programs
1297741Sgblack@eecs.umich.edu    // tc->setMiscRegNoEffect(MISCREG_OTHERWIN, 0);
1305713Shsul@eecs.umich.edu    tc->setIntReg(NumIntArchRegs + 6, 0);
1317741Sgblack@eecs.umich.edu    // There are no windows to pop
1327741Sgblack@eecs.umich.edu    // tc->setMiscRegNoEffect(MISCREG_CANRESTORE, 0);
1335713Shsul@eecs.umich.edu    tc->setIntReg(NumIntArchRegs + 4, 0);
1347741Sgblack@eecs.umich.edu    // All windows are available to save into
1357741Sgblack@eecs.umich.edu    // tc->setMiscRegNoEffect(MISCREG_CANSAVE, NWindows - 2);
1365713Shsul@eecs.umich.edu    tc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
1377741Sgblack@eecs.umich.edu    // All windows are "clean"
1387741Sgblack@eecs.umich.edu    // tc->setMiscRegNoEffect(MISCREG_CLEANWIN, NWindows);
1395713Shsul@eecs.umich.edu    tc->setIntReg(NumIntArchRegs + 5, NWindows);
1407741Sgblack@eecs.umich.edu    // Start with register window 0
1416337Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_CWP, 0);
1427741Sgblack@eecs.umich.edu    // Always use spill and fill traps 0
1437741Sgblack@eecs.umich.edu    // tc->setMiscRegNoEffect(MISCREG_WSTATE, 0);
1445713Shsul@eecs.umich.edu    tc->setIntReg(NumIntArchRegs + 7, 0);
1457741Sgblack@eecs.umich.edu    // Set the trap level to 0
1465713Shsul@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_TL, 0);
1477741Sgblack@eecs.umich.edu    // Set the ASI register to something fixed
1489375Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
1494997Sgblack@eecs.umich.edu
15011850Sbrandon.potter@amd.com    // Set the MMU Primary Context Register to hold the process' pid
15111850Sbrandon.potter@amd.com    tc->setMiscReg(MISCREG_MMU_P_CONTEXT, _pid);
15211850Sbrandon.potter@amd.com
1534997Sgblack@eecs.umich.edu    /*
1544997Sgblack@eecs.umich.edu     * T1 specific registers
1554997Sgblack@eecs.umich.edu     */
1567741Sgblack@eecs.umich.edu    // Turn on the icache, dcache, dtb translation, and itb translation.
1575713Shsul@eecs.umich.edu    tc->setMiscRegNoEffect(MISCREG_MMU_LSU_CTRL, 15);
1582474SN/A}
1592474SN/A
1605285Sgblack@eecs.umich.eduvoid
16111851Sbrandon.potter@amd.comSparc32Process::initState()
1622585SN/A{
16311851Sbrandon.potter@amd.com    SparcProcess::initState();
1645285Sgblack@eecs.umich.edu
1655713Shsul@eecs.umich.edu    ThreadContext *tc = system->getThreadContext(contextIds[0]);
1667741Sgblack@eecs.umich.edu    // The process runs in user mode with 32 bit addresses
1678829Sgblack@eecs.umich.edu    PSTATE pstate = 0;
1688829Sgblack@eecs.umich.edu    pstate.ie = 1;
1698829Sgblack@eecs.umich.edu    pstate.am = 1;
1708829Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_PSTATE, pstate);
1715285Sgblack@eecs.umich.edu
17210318Sandreas.hansson@arm.com    argsInit(32 / 8, PageBytes);
1734111Sgblack@eecs.umich.edu}
1743415Sgblack@eecs.umich.edu
1752561SN/Avoid
17611851Sbrandon.potter@amd.comSparc64Process::initState()
1772561SN/A{
17811851Sbrandon.potter@amd.com    SparcProcess::initState();
1795285Sgblack@eecs.umich.edu
1805713Shsul@eecs.umich.edu    ThreadContext *tc = system->getThreadContext(contextIds[0]);
1817741Sgblack@eecs.umich.edu    // The process runs in user mode
1828829Sgblack@eecs.umich.edu    PSTATE pstate = 0;
1838829Sgblack@eecs.umich.edu    pstate.ie = 1;
1848829Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_PSTATE, pstate);
1855285Sgblack@eecs.umich.edu
18613583Sgabeblack@google.com    argsInit(sizeof(RegVal), PageBytes);
1875285Sgblack@eecs.umich.edu}
1885285Sgblack@eecs.umich.edu
1895285Sgblack@eecs.umich.edutemplate<class IntType>
1905285Sgblack@eecs.umich.eduvoid
19111851Sbrandon.potter@amd.comSparcProcess::argsInit(int pageSize)
1925285Sgblack@eecs.umich.edu{
1935285Sgblack@eecs.umich.edu    int intSize = sizeof(IntType);
1945285Sgblack@eecs.umich.edu
19513894Sgabeblack@google.com    std::vector<AuxVector<IntType>> auxv;
1962474SN/A
1973044Sgblack@eecs.umich.edu    string filename;
1987741Sgblack@eecs.umich.edu    if (argv.size() < 1)
1993044Sgblack@eecs.umich.edu        filename = "";
2003044Sgblack@eecs.umich.edu    else
2013044Sgblack@eecs.umich.edu        filename = argv[0];
2023044Sgblack@eecs.umich.edu
2037741Sgblack@eecs.umich.edu    // Even for a 32 bit process, the ABI says we still need to
2047741Sgblack@eecs.umich.edu    // maintain double word alignment of the stack pointer.
2055286Sgblack@eecs.umich.edu    uint64_t align = 16;
2062561SN/A
20711389Sbrandon.potter@amd.com    // Patch the ld_bias for dynamic executables.
20811389Sbrandon.potter@amd.com    updateBias();
20911389Sbrandon.potter@amd.com
2102561SN/A    // load object file into target memory
2112561SN/A    objFile->loadSections(initVirtMem);
2122561SN/A
2132585SN/A    enum hardwareCaps
2142585SN/A    {
2152585SN/A        M5_HWCAP_SPARC_FLUSH = 1,
2162585SN/A        M5_HWCAP_SPARC_STBAR = 2,
2172585SN/A        M5_HWCAP_SPARC_SWAP = 4,
2182585SN/A        M5_HWCAP_SPARC_MULDIV = 8,
2192585SN/A        M5_HWCAP_SPARC_V9 = 16,
2207741Sgblack@eecs.umich.edu        // This one should technically only be set
2217741Sgblack@eecs.umich.edu        // if there is a cheetah or cheetah_plus tlb,
2227741Sgblack@eecs.umich.edu        // but we'll use it all the time
2232585SN/A        M5_HWCAP_SPARC_ULTRA3 = 32
2242585SN/A    };
2252585SN/A
2262585SN/A    const int64_t hwcap =
2272585SN/A        M5_HWCAP_SPARC_FLUSH |
2282585SN/A        M5_HWCAP_SPARC_STBAR |
2292585SN/A        M5_HWCAP_SPARC_SWAP |
2302585SN/A        M5_HWCAP_SPARC_MULDIV |
2312585SN/A        M5_HWCAP_SPARC_V9 |
2322585SN/A        M5_HWCAP_SPARC_ULTRA3;
2332585SN/A
2347741Sgblack@eecs.umich.edu    // Setup the auxilliary vectors. These will already have endian conversion.
2357741Sgblack@eecs.umich.edu    // Auxilliary vectors are loaded only for elf formatted executables.
2362976Sgblack@eecs.umich.edu    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
2377741Sgblack@eecs.umich.edu    if (elfObject) {
2387741Sgblack@eecs.umich.edu        // Bits which describe the system hardware capabilities
23913894Sgabeblack@google.com        auxv.emplace_back(M5_AT_HWCAP, hwcap);
2407741Sgblack@eecs.umich.edu        // The system page size
24113894Sgabeblack@google.com        auxv.emplace_back(M5_AT_PAGESZ, SparcISA::PageBytes);
2427741Sgblack@eecs.umich.edu        // Defined to be 100 in the kernel source.
2437741Sgblack@eecs.umich.edu        // Frequency at which times() increments
24413894Sgabeblack@google.com        auxv.emplace_back(M5_AT_CLKTCK, 100);
24513894Sgabeblack@google.com        // For statically linked executables, this is the virtual address of
24613894Sgabeblack@google.com        // the program header tables if they appear in the executable image
24713894Sgabeblack@google.com        auxv.emplace_back(M5_AT_PHDR, elfObject->programHeaderTable());
2482976Sgblack@eecs.umich.edu        // This is the size of a program header entry from the elf file.
24913894Sgabeblack@google.com        auxv.emplace_back(M5_AT_PHENT, elfObject->programHeaderSize());
2502976Sgblack@eecs.umich.edu        // This is the number of program headers from the original elf file.
25113894Sgabeblack@google.com        auxv.emplace_back(M5_AT_PHNUM, elfObject->programHeaderCount());
25211389Sbrandon.potter@amd.com        // This is the base address of the ELF interpreter; it should be
25311389Sbrandon.potter@amd.com        // zero for static executables or contain the base address for
25411389Sbrandon.potter@amd.com        // dynamic executables.
25513894Sgabeblack@google.com        auxv.emplace_back(M5_AT_BASE, getBias());
2567741Sgblack@eecs.umich.edu        // This is hardwired to 0 in the elf loading code in the kernel
25713894Sgabeblack@google.com        auxv.emplace_back(M5_AT_FLAGS, 0);
2587741Sgblack@eecs.umich.edu        // The entry point to the program
25913894Sgabeblack@google.com        auxv.emplace_back(M5_AT_ENTRY, objFile->entryPoint());
2607741Sgblack@eecs.umich.edu        // Different user and group IDs
26113894Sgabeblack@google.com        auxv.emplace_back(M5_AT_UID, uid());
26213894Sgabeblack@google.com        auxv.emplace_back(M5_AT_EUID, euid());
26313894Sgabeblack@google.com        auxv.emplace_back(M5_AT_GID, gid());
26413894Sgabeblack@google.com        auxv.emplace_back(M5_AT_EGID, egid());
2657741Sgblack@eecs.umich.edu        // Whether to enable "secure mode" in the executable
26613894Sgabeblack@google.com        auxv.emplace_back(M5_AT_SECURE, 0);
2672976Sgblack@eecs.umich.edu    }
2682585SN/A
2697741Sgblack@eecs.umich.edu    // Figure out how big the initial stack needs to be
2702561SN/A
2714164Sgblack@eecs.umich.edu    // The unaccounted for 8 byte 0 at the top of the stack
2725286Sgblack@eecs.umich.edu    int sentry_size = 8;
2734111Sgblack@eecs.umich.edu
2747741Sgblack@eecs.umich.edu    // This is the name of the file which is present on the initial stack
2757741Sgblack@eecs.umich.edu    // It's purpose is to let the user space linker examine the original file.
2764111Sgblack@eecs.umich.edu    int file_name_size = filename.size() + 1;
2774111Sgblack@eecs.umich.edu
2784111Sgblack@eecs.umich.edu    int env_data_size = 0;
2794111Sgblack@eecs.umich.edu    for (int i = 0; i < envp.size(); ++i) {
2804111Sgblack@eecs.umich.edu        env_data_size += envp[i].size() + 1;
2814111Sgblack@eecs.umich.edu    }
2824111Sgblack@eecs.umich.edu    int arg_data_size = 0;
2834111Sgblack@eecs.umich.edu    for (int i = 0; i < argv.size(); ++i) {
2844111Sgblack@eecs.umich.edu        arg_data_size += argv[i].size() + 1;
2854111Sgblack@eecs.umich.edu    }
2864111Sgblack@eecs.umich.edu
2877741Sgblack@eecs.umich.edu    // The info_block.
2885286Sgblack@eecs.umich.edu    int base_info_block_size =
2895286Sgblack@eecs.umich.edu        sentry_size + file_name_size + env_data_size + arg_data_size;
2905286Sgblack@eecs.umich.edu
2915286Sgblack@eecs.umich.edu    int info_block_size = roundUp(base_info_block_size, align);
2925286Sgblack@eecs.umich.edu
2935286Sgblack@eecs.umich.edu    int info_block_padding = info_block_size - base_info_block_size;
2944111Sgblack@eecs.umich.edu
2957741Sgblack@eecs.umich.edu    // Each auxilliary vector is two words
2964111Sgblack@eecs.umich.edu    int aux_array_size = intSize * 2 * (auxv.size() + 1);
2974111Sgblack@eecs.umich.edu
2984111Sgblack@eecs.umich.edu    int envp_array_size = intSize * (envp.size() + 1);
2994111Sgblack@eecs.umich.edu    int argv_array_size = intSize * (argv.size() + 1);
3004111Sgblack@eecs.umich.edu
3014111Sgblack@eecs.umich.edu    int argc_size = intSize;
3024111Sgblack@eecs.umich.edu    int window_save_size = intSize * 16;
3034111Sgblack@eecs.umich.edu
3047741Sgblack@eecs.umich.edu    // Figure out the size of the contents of the actual initial frame
3055286Sgblack@eecs.umich.edu    int frame_size =
3064111Sgblack@eecs.umich.edu        aux_array_size +
3074111Sgblack@eecs.umich.edu        envp_array_size +
3084111Sgblack@eecs.umich.edu        argv_array_size +
3094111Sgblack@eecs.umich.edu        argc_size +
3104111Sgblack@eecs.umich.edu        window_save_size;
3114111Sgblack@eecs.umich.edu
3127741Sgblack@eecs.umich.edu    // There needs to be padding after the auxiliary vector data so that the
3137741Sgblack@eecs.umich.edu    // very bottom of the stack is aligned properly.
3145286Sgblack@eecs.umich.edu    int aligned_partial_size = roundUp(frame_size, align);
3155286Sgblack@eecs.umich.edu    int aux_padding = aligned_partial_size - frame_size;
3165286Sgblack@eecs.umich.edu
3175286Sgblack@eecs.umich.edu    int space_needed =
3185286Sgblack@eecs.umich.edu        info_block_size +
3195286Sgblack@eecs.umich.edu        aux_padding +
3205286Sgblack@eecs.umich.edu        frame_size;
3215286Sgblack@eecs.umich.edu
32211905SBrandon.Potter@amd.com    memState->setStackMin(memState->getStackBase() - space_needed);
32311905SBrandon.Potter@amd.com    memState->setStackMin(roundDown(memState->getStackMin(), align));
32411905SBrandon.Potter@amd.com    memState->setStackSize(memState->getStackBase() - memState->getStackMin());
3254111Sgblack@eecs.umich.edu
3265285Sgblack@eecs.umich.edu    // Allocate space for the stack
32711905SBrandon.Potter@amd.com    allocateMem(roundDown(memState->getStackMin(), pageSize),
32811905SBrandon.Potter@amd.com                roundUp(memState->getStackSize(), pageSize));
3294111Sgblack@eecs.umich.edu
3304111Sgblack@eecs.umich.edu    // map out initial stack contents
33111905SBrandon.Potter@amd.com    IntType sentry_base = memState->getStackBase() - sentry_size;
3325286Sgblack@eecs.umich.edu    IntType file_name_base = sentry_base - file_name_size;
3335286Sgblack@eecs.umich.edu    IntType env_data_base = file_name_base - env_data_size;
3345286Sgblack@eecs.umich.edu    IntType arg_data_base = env_data_base - arg_data_size;
3355286Sgblack@eecs.umich.edu    IntType auxv_array_base = arg_data_base -
3365286Sgblack@eecs.umich.edu        info_block_padding - aux_array_size - aux_padding;
3375286Sgblack@eecs.umich.edu    IntType envp_array_base = auxv_array_base - envp_array_size;
3385286Sgblack@eecs.umich.edu    IntType argv_array_base = envp_array_base - argv_array_size;
3395286Sgblack@eecs.umich.edu    IntType argc_base = argv_array_base - argc_size;
3405286Sgblack@eecs.umich.edu#if TRACING_ON
3415286Sgblack@eecs.umich.edu    IntType window_save_base = argc_base - window_save_size;
3425286Sgblack@eecs.umich.edu#endif
3434111Sgblack@eecs.umich.edu
3445941Sgblack@eecs.umich.edu    DPRINTF(Stack, "The addresses of items on the initial stack:\n");
3455941Sgblack@eecs.umich.edu    DPRINTF(Stack, "%#x - sentry NULL\n", sentry_base);
3465941Sgblack@eecs.umich.edu    DPRINTF(Stack, "filename = %s\n", filename);
3475941Sgblack@eecs.umich.edu    DPRINTF(Stack, "%#x - file name\n", file_name_base);
3485941Sgblack@eecs.umich.edu    DPRINTF(Stack, "%#x - env data\n", env_data_base);
3495941Sgblack@eecs.umich.edu    DPRINTF(Stack, "%#x - arg data\n", arg_data_base);
3505941Sgblack@eecs.umich.edu    DPRINTF(Stack, "%#x - auxv array\n", auxv_array_base);
3515941Sgblack@eecs.umich.edu    DPRINTF(Stack, "%#x - envp array\n", envp_array_base);
3525941Sgblack@eecs.umich.edu    DPRINTF(Stack, "%#x - argv array\n", argv_array_base);
3535941Sgblack@eecs.umich.edu    DPRINTF(Stack, "%#x - argc \n", argc_base);
3545941Sgblack@eecs.umich.edu    DPRINTF(Stack, "%#x - window save\n", window_save_base);
35511905SBrandon.Potter@amd.com    DPRINTF(Stack, "%#x - stack min\n", memState->getStackMin());
3564111Sgblack@eecs.umich.edu
35711905SBrandon.Potter@amd.com    assert(window_save_base == memState->getStackMin());
3585286Sgblack@eecs.umich.edu
3594111Sgblack@eecs.umich.edu    // write contents to stack
3604111Sgblack@eecs.umich.edu
3614111Sgblack@eecs.umich.edu    // figure out argc
3625285Sgblack@eecs.umich.edu    IntType argc = argv.size();
3635567Snate@binkert.org    IntType guestArgc = SparcISA::htog(argc);
3644111Sgblack@eecs.umich.edu
3657741Sgblack@eecs.umich.edu    // Write out the sentry void *
3665286Sgblack@eecs.umich.edu    uint64_t sentry_NULL = 0;
36714010Sgabeblack@google.com    initVirtMem.writeBlob(sentry_base, &sentry_NULL, sentry_size);
3684111Sgblack@eecs.umich.edu
3697741Sgblack@eecs.umich.edu    // Write the file name
3708852Sandreas.hansson@arm.com    initVirtMem.writeString(file_name_base, filename.c_str());
3714111Sgblack@eecs.umich.edu
3727741Sgblack@eecs.umich.edu    // Copy the aux stuff
37313894Sgabeblack@google.com    Addr auxv_array_end = auxv_array_base;
37413894Sgabeblack@google.com    for (const auto &aux: auxv) {
37513894Sgabeblack@google.com        initVirtMem.write(auxv_array_end, aux, GuestByteOrder);
37613894Sgabeblack@google.com        auxv_array_end += sizeof(aux);
3774111Sgblack@eecs.umich.edu    }
3785285Sgblack@eecs.umich.edu
3797741Sgblack@eecs.umich.edu    // Write out the terminating zeroed auxilliary vector
38013894Sgabeblack@google.com    const AuxVector<IntType> zero(0, 0);
38113894Sgabeblack@google.com    initVirtMem.write(auxv_array_end, zero);
38213894Sgabeblack@google.com    auxv_array_end += sizeof(zero);
3834111Sgblack@eecs.umich.edu
3844117Sgblack@eecs.umich.edu    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
3854117Sgblack@eecs.umich.edu    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
3864111Sgblack@eecs.umich.edu
38714010Sgabeblack@google.com    initVirtMem.writeBlob(argc_base, &guestArgc, intSize);
3884111Sgblack@eecs.umich.edu
3897741Sgblack@eecs.umich.edu    // Set up space for the trap handlers into the processes address space.
3907741Sgblack@eecs.umich.edu    // Since the stack grows down and there is reserved address space abov
3917741Sgblack@eecs.umich.edu    // it, we can put stuff above it and stay out of the way.
39211905SBrandon.Potter@amd.com    fillStart = memState->getStackBase();
3935285Sgblack@eecs.umich.edu    spillStart = fillStart + sizeof(MachInst) * numFillInsts;
3944111Sgblack@eecs.umich.edu
3955713Shsul@eecs.umich.edu    ThreadContext *tc = system->getThreadContext(contextIds[0]);
3967741Sgblack@eecs.umich.edu    // Set up the thread context to start running the process
3977741Sgblack@eecs.umich.edu    // assert(NumArgumentRegs >= 2);
3987741Sgblack@eecs.umich.edu    // tc->setIntReg(ArgumentReg[0], argc);
3997741Sgblack@eecs.umich.edu    // tc->setIntReg(ArgumentReg[1], argv_array_base);
40011905SBrandon.Potter@amd.com    tc->setIntReg(StackPointerReg, memState->getStackMin() - StackBias);
4014111Sgblack@eecs.umich.edu
4025231Sgblack@eecs.umich.edu    // %g1 is a pointer to a function that should be run at exit. Since we
4035231Sgblack@eecs.umich.edu    // don't have anything like that, it should be set to 0.
4045713Shsul@eecs.umich.edu    tc->setIntReg(1, 0);
4055231Sgblack@eecs.umich.edu
40611389Sbrandon.potter@amd.com    tc->pcState(getStartPC());
4074111Sgblack@eecs.umich.edu
4087741Sgblack@eecs.umich.edu    // Align the "stack_min" to a page boundary.
40911905SBrandon.Potter@amd.com    memState->setStackMin(roundDown(memState->getStackMin(), pageSize));
4104111Sgblack@eecs.umich.edu}
4115128Sgblack@eecs.umich.edu
4125285Sgblack@eecs.umich.eduvoid
41311851Sbrandon.potter@amd.comSparc64Process::argsInit(int intSize, int pageSize)
4145285Sgblack@eecs.umich.edu{
41511851Sbrandon.potter@amd.com    SparcProcess::argsInit<uint64_t>(pageSize);
4165285Sgblack@eecs.umich.edu
4175285Sgblack@eecs.umich.edu    // Stuff the trap handlers into the process address space
4188852Sandreas.hansson@arm.com    initVirtMem.writeBlob(fillStart,
41914010Sgabeblack@google.com            fillHandler64, sizeof(MachInst) * numFillInsts);
4208852Sandreas.hansson@arm.com    initVirtMem.writeBlob(spillStart,
42114010Sgabeblack@google.com            spillHandler64, sizeof(MachInst) *  numSpillInsts);
4225285Sgblack@eecs.umich.edu}
4235285Sgblack@eecs.umich.edu
4245285Sgblack@eecs.umich.eduvoid
42511851Sbrandon.potter@amd.comSparc32Process::argsInit(int intSize, int pageSize)
4265285Sgblack@eecs.umich.edu{
42711851Sbrandon.potter@amd.com    SparcProcess::argsInit<uint32_t>(pageSize);
4285285Sgblack@eecs.umich.edu
4295285Sgblack@eecs.umich.edu    // Stuff the trap handlers into the process address space
4308852Sandreas.hansson@arm.com    initVirtMem.writeBlob(fillStart,
43114010Sgabeblack@google.com            fillHandler32, sizeof(MachInst) * numFillInsts);
4328852Sandreas.hansson@arm.com    initVirtMem.writeBlob(spillStart,
43314010Sgabeblack@google.com            spillHandler32, sizeof(MachInst) *  numSpillInsts);
4345285Sgblack@eecs.umich.edu}
4355285Sgblack@eecs.umich.edu
43611851Sbrandon.potter@amd.comvoid Sparc32Process::flushWindows(ThreadContext *tc)
4375128Sgblack@eecs.umich.edu{
43813583Sgabeblack@google.com    RegVal Cansave = tc->readIntReg(NumIntArchRegs + 3);
43913583Sgabeblack@google.com    RegVal Canrestore = tc->readIntReg(NumIntArchRegs + 4);
44013583Sgabeblack@google.com    RegVal Otherwin = tc->readIntReg(NumIntArchRegs + 6);
44113583Sgabeblack@google.com    RegVal CWP = tc->readMiscReg(MISCREG_CWP);
44213583Sgabeblack@google.com    RegVal origCWP = CWP;
4435128Sgblack@eecs.umich.edu    CWP = (CWP + Cansave + 2) % NWindows;
4447741Sgblack@eecs.umich.edu    while (NWindows - 2 - Cansave != 0) {
4455128Sgblack@eecs.umich.edu        if (Otherwin) {
4465128Sgblack@eecs.umich.edu            panic("Otherwin non-zero.\n");
4475128Sgblack@eecs.umich.edu        } else {
4485128Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_CWP, CWP);
4497741Sgblack@eecs.umich.edu            // Do the stores
45013583Sgabeblack@google.com            RegVal sp = tc->readIntReg(StackPointerReg);
4515128Sgblack@eecs.umich.edu            for (int index = 16; index < 32; index++) {
4525287Sgblack@eecs.umich.edu                uint32_t regVal = tc->readIntReg(index);
4535128Sgblack@eecs.umich.edu                regVal = htog(regVal);
45414024Sgabeblack@google.com                if (!tc->getVirtProxy().tryWriteBlob(
4555128Sgblack@eecs.umich.edu                        sp + (index - 16) * 4, (uint8_t *)&regVal, 4)) {
4565128Sgblack@eecs.umich.edu                    warn("Failed to save register to the stack when "
4575128Sgblack@eecs.umich.edu                            "flushing windows.\n");
4585128Sgblack@eecs.umich.edu                }
4595128Sgblack@eecs.umich.edu            }
4605128Sgblack@eecs.umich.edu            Canrestore--;
4615128Sgblack@eecs.umich.edu            Cansave++;
4625128Sgblack@eecs.umich.edu            CWP = (CWP + 1) % NWindows;
4635128Sgblack@eecs.umich.edu        }
4645128Sgblack@eecs.umich.edu    }
4655128Sgblack@eecs.umich.edu    tc->setIntReg(NumIntArchRegs + 3, Cansave);
4665128Sgblack@eecs.umich.edu    tc->setIntReg(NumIntArchRegs + 4, Canrestore);
4675128Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_CWP, origCWP);
4685128Sgblack@eecs.umich.edu}
4695128Sgblack@eecs.umich.edu
4707741Sgblack@eecs.umich.eduvoid
47111851Sbrandon.potter@amd.comSparc64Process::flushWindows(ThreadContext *tc)
4725128Sgblack@eecs.umich.edu{
47313583Sgabeblack@google.com    RegVal Cansave = tc->readIntReg(NumIntArchRegs + 3);
47413583Sgabeblack@google.com    RegVal Canrestore = tc->readIntReg(NumIntArchRegs + 4);
47513583Sgabeblack@google.com    RegVal Otherwin = tc->readIntReg(NumIntArchRegs + 6);
47613583Sgabeblack@google.com    RegVal CWP = tc->readMiscReg(MISCREG_CWP);
47713583Sgabeblack@google.com    RegVal origCWP = CWP;
4785128Sgblack@eecs.umich.edu    CWP = (CWP + Cansave + 2) % NWindows;
4797741Sgblack@eecs.umich.edu    while (NWindows - 2 - Cansave != 0) {
4805128Sgblack@eecs.umich.edu        if (Otherwin) {
4815128Sgblack@eecs.umich.edu            panic("Otherwin non-zero.\n");
4825128Sgblack@eecs.umich.edu        } else {
4835128Sgblack@eecs.umich.edu            tc->setMiscReg(MISCREG_CWP, CWP);
4847741Sgblack@eecs.umich.edu            // Do the stores
48513583Sgabeblack@google.com            RegVal sp = tc->readIntReg(StackPointerReg);
4865128Sgblack@eecs.umich.edu            for (int index = 16; index < 32; index++) {
48713583Sgabeblack@google.com                RegVal regVal = tc->readIntReg(index);
4885128Sgblack@eecs.umich.edu                regVal = htog(regVal);
48914024Sgabeblack@google.com                if (!tc->getVirtProxy().tryWriteBlob(
4905128Sgblack@eecs.umich.edu                        sp + 2047 + (index - 16) * 8, (uint8_t *)&regVal, 8)) {
4915128Sgblack@eecs.umich.edu                    warn("Failed to save register to the stack when "
4925128Sgblack@eecs.umich.edu                            "flushing windows.\n");
4935128Sgblack@eecs.umich.edu                }
4945128Sgblack@eecs.umich.edu            }
4955128Sgblack@eecs.umich.edu            Canrestore--;
4965128Sgblack@eecs.umich.edu            Cansave++;
4975128Sgblack@eecs.umich.edu            CWP = (CWP + 1) % NWindows;
4985128Sgblack@eecs.umich.edu        }
4995128Sgblack@eecs.umich.edu    }
5005128Sgblack@eecs.umich.edu    tc->setIntReg(NumIntArchRegs + 3, Cansave);
5015128Sgblack@eecs.umich.edu    tc->setIntReg(NumIntArchRegs + 4, Canrestore);
5025128Sgblack@eecs.umich.edu    tc->setMiscReg(MISCREG_CWP, origCWP);
5035128Sgblack@eecs.umich.edu}
5045958Sgblack@eecs.umich.edu
50513583Sgabeblack@google.comRegVal
50611851Sbrandon.potter@amd.comSparc32Process::getSyscallArg(ThreadContext *tc, int &i)
5075958Sgblack@eecs.umich.edu{
5085958Sgblack@eecs.umich.edu    assert(i < 6);
5096701Sgblack@eecs.umich.edu    return bits(tc->readIntReg(FirstArgumentReg + i++), 31, 0);
5105958Sgblack@eecs.umich.edu}
5115958Sgblack@eecs.umich.edu
5125958Sgblack@eecs.umich.eduvoid
51313583Sgabeblack@google.comSparc32Process::setSyscallArg(ThreadContext *tc, int i, RegVal val)
5145958Sgblack@eecs.umich.edu{
5155958Sgblack@eecs.umich.edu    assert(i < 6);
5165958Sgblack@eecs.umich.edu    tc->setIntReg(FirstArgumentReg + i, bits(val, 31, 0));
5175958Sgblack@eecs.umich.edu}
5185958Sgblack@eecs.umich.edu
51913583Sgabeblack@google.comRegVal
52011851Sbrandon.potter@amd.comSparc64Process::getSyscallArg(ThreadContext *tc, int &i)
5215958Sgblack@eecs.umich.edu{
5225958Sgblack@eecs.umich.edu    assert(i < 6);
5236701Sgblack@eecs.umich.edu    return tc->readIntReg(FirstArgumentReg + i++);
5245958Sgblack@eecs.umich.edu}
5255958Sgblack@eecs.umich.edu
5265958Sgblack@eecs.umich.eduvoid
52713583Sgabeblack@google.comSparc64Process::setSyscallArg(ThreadContext *tc, int i, RegVal val)
5285958Sgblack@eecs.umich.edu{
5295958Sgblack@eecs.umich.edu    assert(i < 6);
5305958Sgblack@eecs.umich.edu    tc->setIntReg(FirstArgumentReg + i, val);
5315958Sgblack@eecs.umich.edu}
5325958Sgblack@eecs.umich.edu
5335958Sgblack@eecs.umich.eduvoid
53411851Sbrandon.potter@amd.comSparcProcess::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
5355958Sgblack@eecs.umich.edu{
5365958Sgblack@eecs.umich.edu    // check for error condition.  SPARC syscall convention is to
5375958Sgblack@eecs.umich.edu    // indicate success/failure in reg the carry bit of the ccr
5385958Sgblack@eecs.umich.edu    // and put the return value itself in the standard return value reg ().
5398829Sgblack@eecs.umich.edu    PSTATE pstate = tc->readMiscRegNoEffect(MISCREG_PSTATE);
54010223Ssteve.reinhardt@amd.com    if (sysret.successful()) {
5415958Sgblack@eecs.umich.edu        // no error, clear XCC.C
5425958Sgblack@eecs.umich.edu        tc->setIntReg(NumIntArchRegs + 2,
54310223Ssteve.reinhardt@amd.com                      tc->readIntReg(NumIntArchRegs + 2) & 0xEE);
54413583Sgabeblack@google.com        RegVal val = sysret.returnValue();
5458829Sgblack@eecs.umich.edu        if (pstate.am)
5465958Sgblack@eecs.umich.edu            val = bits(val, 31, 0);
5475958Sgblack@eecs.umich.edu        tc->setIntReg(ReturnValueReg, val);
5485958Sgblack@eecs.umich.edu    } else {
5495958Sgblack@eecs.umich.edu        // got an error, set XCC.C
5505958Sgblack@eecs.umich.edu        tc->setIntReg(NumIntArchRegs + 2,
55110223Ssteve.reinhardt@amd.com                      tc->readIntReg(NumIntArchRegs + 2) | 0x11);
55213583Sgabeblack@google.com        RegVal val = sysret.errnoValue();
5538829Sgblack@eecs.umich.edu        if (pstate.am)
5545958Sgblack@eecs.umich.edu            val = bits(val, 31, 0);
5555958Sgblack@eecs.umich.edu        tc->setIntReg(ReturnValueReg, val);
5565958Sgblack@eecs.umich.edu    }
5575958Sgblack@eecs.umich.edu}
558