process.cc revision 11800
13804SN/A/*
212776Snikos.nikoleris@arm.com * Copyright (c) 2003-2004 The Regents of The University of Michigan
39235Sandreas.hansson@arm.com * All rights reserved.
49235Sandreas.hansson@arm.com *
59235Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
69235Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
79235Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
89235Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
99235Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
109235Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
119235Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
129235Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
139235Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from
143804SN/A * this software without specific prior written permission.
153804SN/A *
163804SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
173804SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
183804SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
193804SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
203804SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
213804SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
223804SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233804SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243804SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253804SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
263804SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273804SN/A *
283804SN/A * Authors: Gabe Black
293804SN/A *          Ali Saidi
303804SN/A */
313804SN/A
323804SN/A#include "arch/sparc/process.hh"
333804SN/A
343804SN/A#include "arch/sparc/asi.hh"
353804SN/A#include "arch/sparc/handlers.hh"
363804SN/A#include "arch/sparc/isa_traits.hh"
373804SN/A#include "arch/sparc/registers.hh"
383804SN/A#include "arch/sparc/types.hh"
393804SN/A#include "base/loader/elf_object.hh"
403804SN/A#include "base/loader/object_file.hh"
419235Sandreas.hansson@arm.com#include "base/misc.hh"
423804SN/A#include "cpu/thread_context.hh"
433804SN/A#include "debug/Stack.hh"
449235Sandreas.hansson@arm.com#include "mem/page_table.hh"
459235Sandreas.hansson@arm.com#include "sim/process_impl.hh"
463804SN/A#include "sim/syscall_return.hh"
4712783Snikos.nikoleris@arm.com#include "sim/system.hh"
4812783Snikos.nikoleris@arm.com
4912783Snikos.nikoleris@arm.comusing namespace std;
508229SN/Ausing namespace SparcISA;
518902SN/A
528229SN/Astatic const int FirstArgumentReg = 8;
539235Sandreas.hansson@arm.com
5412783Snikos.nikoleris@arm.com
553804SN/ASparcLiveProcess::SparcLiveProcess(LiveProcessParams * params,
568918SN/A        ObjectFile *objFile, Addr _StackBias)
579235Sandreas.hansson@arm.com    : LiveProcess(params, objFile), StackBias(_StackBias)
589235Sandreas.hansson@arm.com{
599235Sandreas.hansson@arm.com
608918SN/A    // XXX all the below need to be updated for SPARC - Ali
6112777Sgabeblack@google.com    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
629235Sandreas.hansson@arm.com    brk_point = roundUp(brk_point, PageBytes);
633804SN/A
643804SN/A    // Set pointer for next thread stack.  Reserve 8M for main stack.
659235Sandreas.hansson@arm.com    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
663804SN/A
673804SN/A    // Initialize these to 0s
683804SN/A    fillStart = 0;
698918SN/A    spillStart = 0;
703804SN/A}
7112776Snikos.nikoleris@arm.com
7212776Snikos.nikoleris@arm.comvoid
7312776Snikos.nikoleris@arm.comSparcLiveProcess::handleTrap(int trapNum, ThreadContext *tc)
7412776Snikos.nikoleris@arm.com{
7512776Snikos.nikoleris@arm.com    PCState pc = tc->pcState();
7612776Snikos.nikoleris@arm.com    switch (trapNum) {
7712776Snikos.nikoleris@arm.com      case 0x01: // Software breakpoint
7812776Snikos.nikoleris@arm.com        warn("Software breakpoint encountered at pc %#x.\n", pc.pc());
7912776Snikos.nikoleris@arm.com        break;
8012776Snikos.nikoleris@arm.com      case 0x02: // Division by zero
818918SN/A        warn("Software signaled a division by zero at pc %#x.\n", pc.pc());
8212776Snikos.nikoleris@arm.com        break;
838918SN/A      case 0x03: // Flush window trap
8412776Snikos.nikoleris@arm.com        flushWindows(tc);
853804SN/A        break;
8613807Sgabeblack@google.com      case 0x04: // Clean windows
8713807Sgabeblack@google.com        warn("Ignoring process request for clean register "
8813807Sgabeblack@google.com                "windows at pc %#x.\n", pc.pc());
8913807Sgabeblack@google.com        break;
9013807Sgabeblack@google.com      case 0x05: // Range check
913804SN/A        warn("Software signaled a range check at pc %#x.\n", pc.pc());
9212776Snikos.nikoleris@arm.com        break;
9312776Snikos.nikoleris@arm.com      case 0x06: // Fix alignment
9412776Snikos.nikoleris@arm.com        warn("Ignoring process request for os assisted unaligned accesses "
9512776Snikos.nikoleris@arm.com                "at pc %#x.\n", pc.pc());
9612776Snikos.nikoleris@arm.com        break;
9712776Snikos.nikoleris@arm.com      case 0x07: // Integer overflow
9812776Snikos.nikoleris@arm.com        warn("Software signaled an integer overflow at pc %#x.\n", pc.pc());
9912776Snikos.nikoleris@arm.com        break;
10012776Snikos.nikoleris@arm.com      case 0x32: // Get integer condition codes
10112776Snikos.nikoleris@arm.com        warn("Ignoring process request to get the integer condition codes "
1028918SN/A                "at pc %#x.\n", pc.pc());
10312776Snikos.nikoleris@arm.com        break;
1048918SN/A      case 0x33: // Set integer condition codes
10512776Snikos.nikoleris@arm.com        warn("Ignoring process request to set the integer condition codes "
1068918SN/A                "at pc %#x.\n", pc.pc());
10713807Sgabeblack@google.com        break;
10813807Sgabeblack@google.com      default:
10913807Sgabeblack@google.com        panic("Unimplemented trap to operating system: trap number %#x.\n", trapNum);
11013807Sgabeblack@google.com    }
11113807Sgabeblack@google.com}
1128918SN/A
11312776Snikos.nikoleris@arm.comvoid
11412776Snikos.nikoleris@arm.comSparcLiveProcess::initState()
11512776Snikos.nikoleris@arm.com{
11612776Snikos.nikoleris@arm.com    LiveProcess::initState();
11712776Snikos.nikoleris@arm.com
11812776Snikos.nikoleris@arm.com    ThreadContext *tc = system->getThreadContext(contextIds[0]);
11912776Snikos.nikoleris@arm.com    // From the SPARC ABI
12012776Snikos.nikoleris@arm.com
12112776Snikos.nikoleris@arm.com    // Setup default FP state
12212776Snikos.nikoleris@arm.com    tc->setMiscRegNoEffect(MISCREG_FSR, 0);
12312776Snikos.nikoleris@arm.com
12412776Snikos.nikoleris@arm.com    tc->setMiscRegNoEffect(MISCREG_TICK, 0);
1255609SN/A
12612776Snikos.nikoleris@arm.com    /*
1275609SN/A     * Register window management registers
12813807Sgabeblack@google.com     */
12913807Sgabeblack@google.com
13013807Sgabeblack@google.com    // No windows contain info from other programs
13113807Sgabeblack@google.com    // tc->setMiscRegNoEffect(MISCREG_OTHERWIN, 0);
13213807Sgabeblack@google.com    tc->setIntReg(NumIntArchRegs + 6, 0);
1335609SN/A    // There are no windows to pop
13413807Sgabeblack@google.com    // tc->setMiscRegNoEffect(MISCREG_CANRESTORE, 0);
1359235Sandreas.hansson@arm.com    tc->setIntReg(NumIntArchRegs + 4, 0);
1363804SN/A    // All windows are available to save into
13712776Snikos.nikoleris@arm.com    // tc->setMiscRegNoEffect(MISCREG_CANSAVE, NWindows - 2);
1383804SN/A    tc->setIntReg(NumIntArchRegs + 3, NWindows - 2);
1393804SN/A    // All windows are "clean"
1408902SN/A    // tc->setMiscRegNoEffect(MISCREG_CLEANWIN, NWindows);
1413804SN/A    tc->setIntReg(NumIntArchRegs + 5, NWindows);
1423804SN/A    // Start with register window 0
1435608SN/A    tc->setMiscReg(MISCREG_CWP, 0);
1445608SN/A    // Always use spill and fill traps 0
1453804SN/A    // tc->setMiscRegNoEffect(MISCREG_WSTATE, 0);
14612777Sgabeblack@google.com    tc->setIntReg(NumIntArchRegs + 7, 0);
1473804SN/A    // Set the trap level to 0
1483804SN/A    tc->setMiscRegNoEffect(MISCREG_TL, 0);
1493804SN/A    // Set the ASI register to something fixed
1505608SN/A    tc->setMiscReg(MISCREG_ASI, ASI_PRIMARY);
1515608SN/A
1523804SN/A    /*
15312777Sgabeblack@google.com     * T1 specific registers
15412777Sgabeblack@google.com     */
15512777Sgabeblack@google.com    // Turn on the icache, dcache, dtb translation, and itb translation.
1563804SN/A    tc->setMiscRegNoEffect(MISCREG_MMU_LSU_CTRL, 15);
1573804SN/A}
1583804SN/A
1595608SN/Avoid
1605608SN/ASparc32LiveProcess::initState()
1613804SN/A{
16212777Sgabeblack@google.com    SparcLiveProcess::initState();
1633804SN/A
1643804SN/A    ThreadContext *tc = system->getThreadContext(contextIds[0]);
1653804SN/A    // The process runs in user mode with 32 bit addresses
1668918SN/A    PSTATE pstate = 0;
1678918SN/A    pstate.ie = 1;
1688918SN/A    pstate.am = 1;
1698918SN/A    tc->setMiscReg(MISCREG_PSTATE, pstate);
1708918SN/A
1718918SN/A    argsInit(32 / 8, PageBytes);
1725608SN/A}
1735608SN/A
1743804SN/Avoid
1753804SN/ASparc64LiveProcess::initState()
1763804SN/A{
1773804SN/A    SparcLiveProcess::initState();
1788918SN/A
1798918SN/A    ThreadContext *tc = system->getThreadContext(contextIds[0]);
1808918SN/A    // The process runs in user mode
1818918SN/A    PSTATE pstate = 0;
1828918SN/A    pstate.ie = 1;
1838918SN/A    tc->setMiscReg(MISCREG_PSTATE, pstate);
1845608SN/A
1855608SN/A    argsInit(sizeof(IntReg), PageBytes);
1863804SN/A}
1873804SN/A
1883804SN/Atemplate<class IntType>
1893804SN/Avoid
1909235Sandreas.hansson@arm.comSparcLiveProcess::argsInit(int pageSize)
1918918SN/A{
1923804SN/A    int intSize = sizeof(IntType);
1933804SN/A
1943804SN/A    typedef AuxVector<IntType> auxv_t;
1953804SN/A
1965608SN/A    std::vector<auxv_t> auxv;
1978918SN/A
1983804SN/A    string filename;
1993804SN/A    if (argv.size() < 1)
2003804SN/A        filename = "";
20112776Snikos.nikoleris@arm.com    else
20212776Snikos.nikoleris@arm.com        filename = argv[0];
20312776Snikos.nikoleris@arm.com
20412777Sgabeblack@google.com    // Even for a 32 bit process, the ABI says we still need to
20512777Sgabeblack@google.com    // maintain double word alignment of the stack pointer.
20612777Sgabeblack@google.com    uint64_t align = 16;
20712777Sgabeblack@google.com
20812777Sgabeblack@google.com    // Patch the ld_bias for dynamic executables.
20913807Sgabeblack@google.com    updateBias();
21012777Sgabeblack@google.com
21112777Sgabeblack@google.com    // load object file into target memory
21212777Sgabeblack@google.com    objFile->loadSections(initVirtMem);
21312777Sgabeblack@google.com
21412777Sgabeblack@google.com    enum hardwareCaps
21512777Sgabeblack@google.com    {
21612777Sgabeblack@google.com        M5_HWCAP_SPARC_FLUSH = 1,
21712777Sgabeblack@google.com        M5_HWCAP_SPARC_STBAR = 2,
21812777Sgabeblack@google.com        M5_HWCAP_SPARC_SWAP = 4,
21912777Sgabeblack@google.com        M5_HWCAP_SPARC_MULDIV = 8,
22012777Sgabeblack@google.com        M5_HWCAP_SPARC_V9 = 16,
22112777Sgabeblack@google.com        // This one should technically only be set
22212777Sgabeblack@google.com        // if there is a cheetah or cheetah_plus tlb,
22312777Sgabeblack@google.com        // but we'll use it all the time
22412777Sgabeblack@google.com        M5_HWCAP_SPARC_ULTRA3 = 32
22512777Sgabeblack@google.com    };
22612777Sgabeblack@google.com
22712777Sgabeblack@google.com    const int64_t hwcap =
22812777Sgabeblack@google.com        M5_HWCAP_SPARC_FLUSH |
22912776Snikos.nikoleris@arm.com        M5_HWCAP_SPARC_STBAR |
23012776Snikos.nikoleris@arm.com        M5_HWCAP_SPARC_SWAP |
23112776Snikos.nikoleris@arm.com        M5_HWCAP_SPARC_MULDIV |
23212776Snikos.nikoleris@arm.com        M5_HWCAP_SPARC_V9 |
23312776Snikos.nikoleris@arm.com        M5_HWCAP_SPARC_ULTRA3;
23412776Snikos.nikoleris@arm.com
23512776Snikos.nikoleris@arm.com    // Setup the auxilliary vectors. These will already have endian conversion.
23612776Snikos.nikoleris@arm.com    // Auxilliary vectors are loaded only for elf formatted executables.
23712776Snikos.nikoleris@arm.com    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
23812776Snikos.nikoleris@arm.com    if (elfObject) {
23913807Sgabeblack@google.com        // Bits which describe the system hardware capabilities
24013807Sgabeblack@google.com        auxv.push_back(auxv_t(M5_AT_HWCAP, hwcap));
24112776Snikos.nikoleris@arm.com        // The system page size
24212777Sgabeblack@google.com        auxv.push_back(auxv_t(M5_AT_PAGESZ, SparcISA::PageBytes));
24312777Sgabeblack@google.com        // Defined to be 100 in the kernel source.
24412777Sgabeblack@google.com        // Frequency at which times() increments
24512777Sgabeblack@google.com        auxv.push_back(auxv_t(M5_AT_CLKTCK, 100));
24612777Sgabeblack@google.com        // For statically linked executables, this is the virtual address of the
24712777Sgabeblack@google.com        // program header tables if they appear in the executable image
24812777Sgabeblack@google.com        auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
24912777Sgabeblack@google.com        // This is the size of a program header entry from the elf file.
25012777Sgabeblack@google.com        auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
25112777Sgabeblack@google.com        // This is the number of program headers from the original elf file.
25212777Sgabeblack@google.com        auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
25313807Sgabeblack@google.com        // This is the base address of the ELF interpreter; it should be
25412776Snikos.nikoleris@arm.com        // zero for static executables or contain the base address for
25512777Sgabeblack@google.com        // dynamic executables.
25612776Snikos.nikoleris@arm.com        auxv.push_back(auxv_t(M5_AT_BASE, getBias()));
25712776Snikos.nikoleris@arm.com        // This is hardwired to 0 in the elf loading code in the kernel
25812776Snikos.nikoleris@arm.com        auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
25912776Snikos.nikoleris@arm.com        // The entry point to the program
26012776Snikos.nikoleris@arm.com        auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
26112776Snikos.nikoleris@arm.com        // Different user and group IDs
26213807Sgabeblack@google.com        auxv.push_back(auxv_t(M5_AT_UID, uid()));
26312776Snikos.nikoleris@arm.com        auxv.push_back(auxv_t(M5_AT_EUID, euid()));
26412776Snikos.nikoleris@arm.com        auxv.push_back(auxv_t(M5_AT_GID, gid()));
26512776Snikos.nikoleris@arm.com        auxv.push_back(auxv_t(M5_AT_EGID, egid()));
26612777Sgabeblack@google.com        // Whether to enable "secure mode" in the executable
26712776Snikos.nikoleris@arm.com        auxv.push_back(auxv_t(M5_AT_SECURE, 0));
26812776Snikos.nikoleris@arm.com    }
26912776Snikos.nikoleris@arm.com
27012776Snikos.nikoleris@arm.com    // Figure out how big the initial stack needs to be
27112776Snikos.nikoleris@arm.com
27212776Snikos.nikoleris@arm.com    // The unaccounted for 8 byte 0 at the top of the stack
27312776Snikos.nikoleris@arm.com    int sentry_size = 8;
27412776Snikos.nikoleris@arm.com
27512776Snikos.nikoleris@arm.com    // This is the name of the file which is present on the initial stack
27613807Sgabeblack@google.com    // It's purpose is to let the user space linker examine the original file.
27713807Sgabeblack@google.com    int file_name_size = filename.size() + 1;
27813807Sgabeblack@google.com
27913807Sgabeblack@google.com    int env_data_size = 0;
28013807Sgabeblack@google.com    for (int i = 0; i < envp.size(); ++i) {
28113807Sgabeblack@google.com        env_data_size += envp[i].size() + 1;
28212776Snikos.nikoleris@arm.com    }
28312777Sgabeblack@google.com    int arg_data_size = 0;
28412777Sgabeblack@google.com    for (int i = 0; i < argv.size(); ++i) {
28512777Sgabeblack@google.com        arg_data_size += argv[i].size() + 1;
28612777Sgabeblack@google.com    }
28712777Sgabeblack@google.com
28812777Sgabeblack@google.com    // The info_block.
28912777Sgabeblack@google.com    int base_info_block_size =
29013807Sgabeblack@google.com        sentry_size + file_name_size + env_data_size + arg_data_size;
2913804SN/A
2923804SN/A    int info_block_size = roundUp(base_info_block_size, align);
2939235Sandreas.hansson@arm.com
294    int info_block_padding = info_block_size - base_info_block_size;
295
296    // Each auxilliary vector is two words
297    int aux_array_size = intSize * 2 * (auxv.size() + 1);
298
299    int envp_array_size = intSize * (envp.size() + 1);
300    int argv_array_size = intSize * (argv.size() + 1);
301
302    int argc_size = intSize;
303    int window_save_size = intSize * 16;
304
305    // Figure out the size of the contents of the actual initial frame
306    int frame_size =
307        aux_array_size +
308        envp_array_size +
309        argv_array_size +
310        argc_size +
311        window_save_size;
312
313    // There needs to be padding after the auxiliary vector data so that the
314    // very bottom of the stack is aligned properly.
315    int aligned_partial_size = roundUp(frame_size, align);
316    int aux_padding = aligned_partial_size - frame_size;
317
318    int space_needed =
319        info_block_size +
320        aux_padding +
321        frame_size;
322
323    stack_min = stack_base - space_needed;
324    stack_min = roundDown(stack_min, align);
325    stack_size = stack_base - stack_min;
326
327    // Allocate space for the stack
328    allocateMem(roundDown(stack_min, pageSize), roundUp(stack_size, pageSize));
329
330    // map out initial stack contents
331    IntType sentry_base = stack_base - sentry_size;
332    IntType file_name_base = sentry_base - file_name_size;
333    IntType env_data_base = file_name_base - env_data_size;
334    IntType arg_data_base = env_data_base - arg_data_size;
335    IntType auxv_array_base = arg_data_base -
336        info_block_padding - aux_array_size - aux_padding;
337    IntType envp_array_base = auxv_array_base - envp_array_size;
338    IntType argv_array_base = envp_array_base - argv_array_size;
339    IntType argc_base = argv_array_base - argc_size;
340#if TRACING_ON
341    IntType window_save_base = argc_base - window_save_size;
342#endif
343
344    DPRINTF(Stack, "The addresses of items on the initial stack:\n");
345    DPRINTF(Stack, "%#x - sentry NULL\n", sentry_base);
346    DPRINTF(Stack, "filename = %s\n", filename);
347    DPRINTF(Stack, "%#x - file name\n", file_name_base);
348    DPRINTF(Stack, "%#x - env data\n", env_data_base);
349    DPRINTF(Stack, "%#x - arg data\n", arg_data_base);
350    DPRINTF(Stack, "%#x - auxv array\n", auxv_array_base);
351    DPRINTF(Stack, "%#x - envp array\n", envp_array_base);
352    DPRINTF(Stack, "%#x - argv array\n", argv_array_base);
353    DPRINTF(Stack, "%#x - argc \n", argc_base);
354    DPRINTF(Stack, "%#x - window save\n", window_save_base);
355    DPRINTF(Stack, "%#x - stack min\n", stack_min);
356
357    assert(window_save_base == stack_min);
358
359    // write contents to stack
360
361    // figure out argc
362    IntType argc = argv.size();
363    IntType guestArgc = SparcISA::htog(argc);
364
365    // Write out the sentry void *
366    uint64_t sentry_NULL = 0;
367    initVirtMem.writeBlob(sentry_base,
368            (uint8_t*)&sentry_NULL, sentry_size);
369
370    // Write the file name
371    initVirtMem.writeString(file_name_base, filename.c_str());
372
373    // Copy the aux stuff
374    for (int x = 0; x < auxv.size(); x++) {
375        initVirtMem.writeBlob(auxv_array_base + x * 2 * intSize,
376                (uint8_t*)&(auxv[x].a_type), intSize);
377        initVirtMem.writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
378                (uint8_t*)&(auxv[x].a_val), intSize);
379    }
380
381    // Write out the terminating zeroed auxilliary vector
382    const IntType zero = 0;
383    initVirtMem.writeBlob(auxv_array_base + intSize * 2 * auxv.size(),
384            (uint8_t*)&zero, intSize);
385    initVirtMem.writeBlob(auxv_array_base + intSize * (2 * auxv.size() + 1),
386            (uint8_t*)&zero, intSize);
387
388    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
389    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
390
391    initVirtMem.writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
392
393    // Set up space for the trap handlers into the processes address space.
394    // Since the stack grows down and there is reserved address space abov
395    // it, we can put stuff above it and stay out of the way.
396    fillStart = stack_base;
397    spillStart = fillStart + sizeof(MachInst) * numFillInsts;
398
399    ThreadContext *tc = system->getThreadContext(contextIds[0]);
400    // Set up the thread context to start running the process
401    // assert(NumArgumentRegs >= 2);
402    // tc->setIntReg(ArgumentReg[0], argc);
403    // tc->setIntReg(ArgumentReg[1], argv_array_base);
404    tc->setIntReg(StackPointerReg, stack_min - StackBias);
405
406    // %g1 is a pointer to a function that should be run at exit. Since we
407    // don't have anything like that, it should be set to 0.
408    tc->setIntReg(1, 0);
409
410    tc->pcState(getStartPC());
411
412    // Align the "stack_min" to a page boundary.
413    stack_min = roundDown(stack_min, pageSize);
414
415//    num_processes++;
416}
417
418void
419Sparc64LiveProcess::argsInit(int intSize, int pageSize)
420{
421    SparcLiveProcess::argsInit<uint64_t>(pageSize);
422
423    // Stuff the trap handlers into the process address space
424    initVirtMem.writeBlob(fillStart,
425            (uint8_t*)fillHandler64, sizeof(MachInst) * numFillInsts);
426    initVirtMem.writeBlob(spillStart,
427            (uint8_t*)spillHandler64, sizeof(MachInst) *  numSpillInsts);
428}
429
430void
431Sparc32LiveProcess::argsInit(int intSize, int pageSize)
432{
433    SparcLiveProcess::argsInit<uint32_t>(pageSize);
434
435    // Stuff the trap handlers into the process address space
436    initVirtMem.writeBlob(fillStart,
437            (uint8_t*)fillHandler32, sizeof(MachInst) * numFillInsts);
438    initVirtMem.writeBlob(spillStart,
439            (uint8_t*)spillHandler32, sizeof(MachInst) *  numSpillInsts);
440}
441
442void Sparc32LiveProcess::flushWindows(ThreadContext *tc)
443{
444    IntReg Cansave = tc->readIntReg(NumIntArchRegs + 3);
445    IntReg Canrestore = tc->readIntReg(NumIntArchRegs + 4);
446    IntReg Otherwin = tc->readIntReg(NumIntArchRegs + 6);
447    MiscReg CWP = tc->readMiscReg(MISCREG_CWP);
448    MiscReg origCWP = CWP;
449    CWP = (CWP + Cansave + 2) % NWindows;
450    while (NWindows - 2 - Cansave != 0) {
451        if (Otherwin) {
452            panic("Otherwin non-zero.\n");
453        } else {
454            tc->setMiscReg(MISCREG_CWP, CWP);
455            // Do the stores
456            IntReg sp = tc->readIntReg(StackPointerReg);
457            for (int index = 16; index < 32; index++) {
458                uint32_t regVal = tc->readIntReg(index);
459                regVal = htog(regVal);
460                if (!tc->getMemProxy().tryWriteBlob(
461                        sp + (index - 16) * 4, (uint8_t *)&regVal, 4)) {
462                    warn("Failed to save register to the stack when "
463                            "flushing windows.\n");
464                }
465            }
466            Canrestore--;
467            Cansave++;
468            CWP = (CWP + 1) % NWindows;
469        }
470    }
471    tc->setIntReg(NumIntArchRegs + 3, Cansave);
472    tc->setIntReg(NumIntArchRegs + 4, Canrestore);
473    tc->setMiscReg(MISCREG_CWP, origCWP);
474}
475
476void
477Sparc64LiveProcess::flushWindows(ThreadContext *tc)
478{
479    IntReg Cansave = tc->readIntReg(NumIntArchRegs + 3);
480    IntReg Canrestore = tc->readIntReg(NumIntArchRegs + 4);
481    IntReg Otherwin = tc->readIntReg(NumIntArchRegs + 6);
482    MiscReg CWP = tc->readMiscReg(MISCREG_CWP);
483    MiscReg origCWP = CWP;
484    CWP = (CWP + Cansave + 2) % NWindows;
485    while (NWindows - 2 - Cansave != 0) {
486        if (Otherwin) {
487            panic("Otherwin non-zero.\n");
488        } else {
489            tc->setMiscReg(MISCREG_CWP, CWP);
490            // Do the stores
491            IntReg sp = tc->readIntReg(StackPointerReg);
492            for (int index = 16; index < 32; index++) {
493                IntReg regVal = tc->readIntReg(index);
494                regVal = htog(regVal);
495                if (!tc->getMemProxy().tryWriteBlob(
496                        sp + 2047 + (index - 16) * 8, (uint8_t *)&regVal, 8)) {
497                    warn("Failed to save register to the stack when "
498                            "flushing windows.\n");
499                }
500            }
501            Canrestore--;
502            Cansave++;
503            CWP = (CWP + 1) % NWindows;
504        }
505    }
506    tc->setIntReg(NumIntArchRegs + 3, Cansave);
507    tc->setIntReg(NumIntArchRegs + 4, Canrestore);
508    tc->setMiscReg(MISCREG_CWP, origCWP);
509}
510
511IntReg
512Sparc32LiveProcess::getSyscallArg(ThreadContext *tc, int &i)
513{
514    assert(i < 6);
515    return bits(tc->readIntReg(FirstArgumentReg + i++), 31, 0);
516}
517
518void
519Sparc32LiveProcess::setSyscallArg(ThreadContext *tc, int i, IntReg val)
520{
521    assert(i < 6);
522    tc->setIntReg(FirstArgumentReg + i, bits(val, 31, 0));
523}
524
525IntReg
526Sparc64LiveProcess::getSyscallArg(ThreadContext *tc, int &i)
527{
528    assert(i < 6);
529    return tc->readIntReg(FirstArgumentReg + i++);
530}
531
532void
533Sparc64LiveProcess::setSyscallArg(ThreadContext *tc, int i, IntReg val)
534{
535    assert(i < 6);
536    tc->setIntReg(FirstArgumentReg + i, val);
537}
538
539void
540SparcLiveProcess::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
541{
542    // check for error condition.  SPARC syscall convention is to
543    // indicate success/failure in reg the carry bit of the ccr
544    // and put the return value itself in the standard return value reg ().
545    PSTATE pstate = tc->readMiscRegNoEffect(MISCREG_PSTATE);
546    if (sysret.successful()) {
547        // no error, clear XCC.C
548        tc->setIntReg(NumIntArchRegs + 2,
549                      tc->readIntReg(NumIntArchRegs + 2) & 0xEE);
550        IntReg val = sysret.returnValue();
551        if (pstate.am)
552            val = bits(val, 31, 0);
553        tc->setIntReg(ReturnValueReg, val);
554    } else {
555        // got an error, set XCC.C
556        tc->setIntReg(NumIntArchRegs + 2,
557                      tc->readIntReg(NumIntArchRegs + 2) | 0x11);
558        IntReg val = sysret.errnoValue();
559        if (pstate.am)
560            val = bits(val, 31, 0);
561        tc->setIntReg(ReturnValueReg, val);
562    }
563}
564