process.cc revision 11793
16691Stjones1@inf.ed.ac.uk/*
26691Stjones1@inf.ed.ac.uk * Copyright (c) 2007-2008 The Florida State University
36691Stjones1@inf.ed.ac.uk * Copyright (c) 2009 The University of Edinburgh
46691Stjones1@inf.ed.ac.uk * All rights reserved.
56691Stjones1@inf.ed.ac.uk *
66691Stjones1@inf.ed.ac.uk * Redistribution and use in source and binary forms, with or without
76691Stjones1@inf.ed.ac.uk * modification, are permitted provided that the following conditions are
86691Stjones1@inf.ed.ac.uk * met: redistributions of source code must retain the above copyright
96691Stjones1@inf.ed.ac.uk * notice, this list of conditions and the following disclaimer;
106691Stjones1@inf.ed.ac.uk * redistributions in binary form must reproduce the above copyright
116691Stjones1@inf.ed.ac.uk * notice, this list of conditions and the following disclaimer in the
126691Stjones1@inf.ed.ac.uk * documentation and/or other materials provided with the distribution;
136691Stjones1@inf.ed.ac.uk * neither the name of the copyright holders nor the names of its
146691Stjones1@inf.ed.ac.uk * contributors may be used to endorse or promote products derived from
156691Stjones1@inf.ed.ac.uk * this software without specific prior written permission.
166691Stjones1@inf.ed.ac.uk *
176691Stjones1@inf.ed.ac.uk * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
186691Stjones1@inf.ed.ac.uk * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
196691Stjones1@inf.ed.ac.uk * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
206691Stjones1@inf.ed.ac.uk * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
216691Stjones1@inf.ed.ac.uk * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
226691Stjones1@inf.ed.ac.uk * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
236691Stjones1@inf.ed.ac.uk * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246691Stjones1@inf.ed.ac.uk * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256691Stjones1@inf.ed.ac.uk * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
266691Stjones1@inf.ed.ac.uk * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
276691Stjones1@inf.ed.ac.uk * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286691Stjones1@inf.ed.ac.uk *
296691Stjones1@inf.ed.ac.uk * Authors: Stephen Hines
306691Stjones1@inf.ed.ac.uk *          Timothy M. Jones
316691Stjones1@inf.ed.ac.uk */
326691Stjones1@inf.ed.ac.uk
336691Stjones1@inf.ed.ac.uk#include "arch/power/process.hh"
346691Stjones1@inf.ed.ac.uk
356691Stjones1@inf.ed.ac.uk#include "arch/power/isa_traits.hh"
366691Stjones1@inf.ed.ac.uk#include "arch/power/types.hh"
376691Stjones1@inf.ed.ac.uk#include "base/loader/elf_object.hh"
386691Stjones1@inf.ed.ac.uk#include "base/loader/object_file.hh"
396691Stjones1@inf.ed.ac.uk#include "base/misc.hh"
406691Stjones1@inf.ed.ac.uk#include "cpu/thread_context.hh"
416691Stjones1@inf.ed.ac.uk#include "debug/Stack.hh"
4210687SAndreas.Sandberg@ARM.com#include "mem/page_table.hh"
436691Stjones1@inf.ed.ac.uk#include "sim/process_impl.hh"
448229Snate@binkert.org#include "sim/system.hh"
456691Stjones1@inf.ed.ac.uk
466691Stjones1@inf.ed.ac.ukusing namespace std;
476691Stjones1@inf.ed.ac.ukusing namespace PowerISA;
486691Stjones1@inf.ed.ac.uk
496691Stjones1@inf.ed.ac.ukPowerLiveProcess::PowerLiveProcess(LiveProcessParams *params,
506691Stjones1@inf.ed.ac.uk        ObjectFile *objFile)
516691Stjones1@inf.ed.ac.uk    : LiveProcess(params, objFile)
526691Stjones1@inf.ed.ac.uk{
536691Stjones1@inf.ed.ac.uk    stack_base = 0xbf000000L;
546691Stjones1@inf.ed.ac.uk
556691Stjones1@inf.ed.ac.uk    // Set pointer for next thread stack.  Reserve 8M for main stack.
566691Stjones1@inf.ed.ac.uk    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
576691Stjones1@inf.ed.ac.uk
586691Stjones1@inf.ed.ac.uk    // Set up break point (Top of Heap)
596691Stjones1@inf.ed.ac.uk    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
606691Stjones1@inf.ed.ac.uk    brk_point = roundUp(brk_point, PageBytes);
616691Stjones1@inf.ed.ac.uk
626691Stjones1@inf.ed.ac.uk    // Set up region for mmaps. For now, start at bottom of kuseg space.
636691Stjones1@inf.ed.ac.uk    mmap_end = 0x70000000L;
646691Stjones1@inf.ed.ac.uk}
6510558Salexandru.dutu@amd.com
6610558Salexandru.dutu@amd.comvoid
676691Stjones1@inf.ed.ac.ukPowerLiveProcess::initState()
686691Stjones1@inf.ed.ac.uk{
6910558Salexandru.dutu@amd.com    Process::initState();
7010558Salexandru.dutu@amd.com
7110558Salexandru.dutu@amd.com    argsInit(MachineBytes, PageBytes);
726691Stjones1@inf.ed.ac.uk}
736691Stjones1@inf.ed.ac.uk
746691Stjones1@inf.ed.ac.ukvoid
756691Stjones1@inf.ed.ac.ukPowerLiveProcess::argsInit(int intSize, int pageSize)
766691Stjones1@inf.ed.ac.uk{
776691Stjones1@inf.ed.ac.uk    typedef AuxVector<uint32_t> auxv_t;
786691Stjones1@inf.ed.ac.uk    std::vector<auxv_t> auxv;
796691Stjones1@inf.ed.ac.uk
806691Stjones1@inf.ed.ac.uk    string filename;
816691Stjones1@inf.ed.ac.uk    if (argv.size() < 1)
826691Stjones1@inf.ed.ac.uk        filename = "";
836691Stjones1@inf.ed.ac.uk    else
846691Stjones1@inf.ed.ac.uk        filename = argv[0];
856691Stjones1@inf.ed.ac.uk
866691Stjones1@inf.ed.ac.uk    //We want 16 byte alignment
8710905Sandreas.sandberg@arm.com    uint64_t align = 16;
886691Stjones1@inf.ed.ac.uk
896691Stjones1@inf.ed.ac.uk    // Patch the ld_bias for dynamic executables.
906691Stjones1@inf.ed.ac.uk    updateBias();
916691Stjones1@inf.ed.ac.uk
926691Stjones1@inf.ed.ac.uk    // load object file into target memory
9310905Sandreas.sandberg@arm.com    objFile->loadSections(initVirtMem);
946691Stjones1@inf.ed.ac.uk
956691Stjones1@inf.ed.ac.uk    //Setup the auxilliary vectors. These will already have endian conversion.
966691Stjones1@inf.ed.ac.uk    //Auxilliary vectors are loaded only for elf formatted executables.
976691Stjones1@inf.ed.ac.uk    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
986691Stjones1@inf.ed.ac.uk    if (elfObject) {
996691Stjones1@inf.ed.ac.uk        uint32_t features = 0;
1006691Stjones1@inf.ed.ac.uk
1016691Stjones1@inf.ed.ac.uk        //Bits which describe the system hardware capabilities
1026691Stjones1@inf.ed.ac.uk        //XXX Figure out what these should be
1036691Stjones1@inf.ed.ac.uk        auxv.push_back(auxv_t(M5_AT_HWCAP, features));
1046691Stjones1@inf.ed.ac.uk        //The system page size
1056691Stjones1@inf.ed.ac.uk        auxv.push_back(auxv_t(M5_AT_PAGESZ, PowerISA::PageBytes));
1066691Stjones1@inf.ed.ac.uk        //Frequency at which times() increments
1076691Stjones1@inf.ed.ac.uk        auxv.push_back(auxv_t(M5_AT_CLKTCK, 0x64));
1086691Stjones1@inf.ed.ac.uk        // For statically linked executables, this is the virtual address of the
1096691Stjones1@inf.ed.ac.uk        // program header tables if they appear in the executable image
1106691Stjones1@inf.ed.ac.uk        auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
1116691Stjones1@inf.ed.ac.uk        // This is the size of a program header entry from the elf file.
1126691Stjones1@inf.ed.ac.uk        auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
1136691Stjones1@inf.ed.ac.uk        // This is the number of program headers from the original elf file.
1146691Stjones1@inf.ed.ac.uk        auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
1156691Stjones1@inf.ed.ac.uk        // This is the base address of the ELF interpreter; it should be
1166691Stjones1@inf.ed.ac.uk        // zero for static executables or contain the base address for
1176691Stjones1@inf.ed.ac.uk        // dynamic executables.
1186691Stjones1@inf.ed.ac.uk        auxv.push_back(auxv_t(M5_AT_BASE, getBias()));
1196691Stjones1@inf.ed.ac.uk        //XXX Figure out what this should be.
1206691Stjones1@inf.ed.ac.uk        auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
1216691Stjones1@inf.ed.ac.uk        //The entry point to the program
1226691Stjones1@inf.ed.ac.uk        auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
1236691Stjones1@inf.ed.ac.uk        //Different user and group IDs
1246691Stjones1@inf.ed.ac.uk        auxv.push_back(auxv_t(M5_AT_UID, uid()));
1256691Stjones1@inf.ed.ac.uk        auxv.push_back(auxv_t(M5_AT_EUID, euid()));
1266691Stjones1@inf.ed.ac.uk        auxv.push_back(auxv_t(M5_AT_GID, gid()));
1276691Stjones1@inf.ed.ac.uk        auxv.push_back(auxv_t(M5_AT_EGID, egid()));
1286691Stjones1@inf.ed.ac.uk        //Whether to enable "secure mode" in the executable
1296691Stjones1@inf.ed.ac.uk        auxv.push_back(auxv_t(M5_AT_SECURE, 0));
1306691Stjones1@inf.ed.ac.uk        //The filename of the program
1316691Stjones1@inf.ed.ac.uk        auxv.push_back(auxv_t(M5_AT_EXECFN, 0));
1326691Stjones1@inf.ed.ac.uk        //The string "v51" with unknown meaning
1336691Stjones1@inf.ed.ac.uk        auxv.push_back(auxv_t(M5_AT_PLATFORM, 0));
1346691Stjones1@inf.ed.ac.uk    }
1356691Stjones1@inf.ed.ac.uk
13611347Sandreas.hansson@arm.com    //Figure out how big the initial stack nedes to be
13710194SGeoffrey.Blake@arm.com
1386691Stjones1@inf.ed.ac.uk    // A sentry NULL void pointer at the top of the stack.
1396691Stjones1@inf.ed.ac.uk    int sentry_size = intSize;
1406691Stjones1@inf.ed.ac.uk
1416691Stjones1@inf.ed.ac.uk    string platform = "v51";
1426691Stjones1@inf.ed.ac.uk    int platform_size = platform.size() + 1;
1436691Stjones1@inf.ed.ac.uk
1446691Stjones1@inf.ed.ac.uk    // The aux vectors are put on the stack in two groups. The first group are
1456691Stjones1@inf.ed.ac.uk    // the vectors that are generated as the elf is loaded. The second group
1466691Stjones1@inf.ed.ac.uk    // are the ones that were computed ahead of time and include the platform
1476691Stjones1@inf.ed.ac.uk    // string.
1486691Stjones1@inf.ed.ac.uk    int aux_data_size = filename.size() + 1;
1496691Stjones1@inf.ed.ac.uk
1506691Stjones1@inf.ed.ac.uk    int env_data_size = 0;
1516691Stjones1@inf.ed.ac.uk    for (int i = 0; i < envp.size(); ++i) {
15211347Sandreas.hansson@arm.com        env_data_size += envp[i].size() + 1;
1536691Stjones1@inf.ed.ac.uk    }
1546691Stjones1@inf.ed.ac.uk    int arg_data_size = 0;
15511347Sandreas.hansson@arm.com    for (int i = 0; i < argv.size(); ++i) {
1566691Stjones1@inf.ed.ac.uk        arg_data_size += argv[i].size() + 1;
1576691Stjones1@inf.ed.ac.uk    }
1586691Stjones1@inf.ed.ac.uk
1596691Stjones1@inf.ed.ac.uk    int info_block_size =
1606691Stjones1@inf.ed.ac.uk        sentry_size + env_data_size + arg_data_size +
1616691Stjones1@inf.ed.ac.uk        aux_data_size + platform_size;
1626691Stjones1@inf.ed.ac.uk
1636972Stjones1@inf.ed.ac.uk    //Each auxilliary vector is two 4 byte words
1646972Stjones1@inf.ed.ac.uk    int aux_array_size = intSize * 2 * (auxv.size() + 1);
16512406Sgabeblack@google.com
16612406Sgabeblack@google.com    int envp_array_size = intSize * (envp.size() + 1);
16712406Sgabeblack@google.com    int argv_array_size = intSize * (argv.size() + 1);
16812406Sgabeblack@google.com
16912406Sgabeblack@google.com    int argc_size = intSize;
17012406Sgabeblack@google.com
17112406Sgabeblack@google.com    //Figure out the size of the contents of the actual initial frame
1726691Stjones1@inf.ed.ac.uk    int frame_size =
1736691Stjones1@inf.ed.ac.uk        info_block_size +
17411168Sandreas.hansson@arm.com        aux_array_size +
17511168Sandreas.hansson@arm.com        envp_array_size +
17610905Sandreas.sandberg@arm.com        argv_array_size +
17711347Sandreas.hansson@arm.com        argc_size;
1786691Stjones1@inf.ed.ac.uk
1796691Stjones1@inf.ed.ac.uk    //There needs to be padding after the auxiliary vector data so that the
1807811Ssteve.reinhardt@amd.com    //very bottom of the stack is aligned properly.
1816691Stjones1@inf.ed.ac.uk    int partial_size = frame_size;
1826691Stjones1@inf.ed.ac.uk    int aligned_partial_size = roundUp(partial_size, align);
183    int aux_padding = aligned_partial_size - partial_size;
184
185    int space_needed = frame_size + aux_padding;
186
187    stack_min = stack_base - space_needed;
188    stack_min = roundDown(stack_min, align);
189    stack_size = stack_base - stack_min;
190
191    // map memory
192    allocateMem(roundDown(stack_min, pageSize), roundUp(stack_size, pageSize));
193
194    // map out initial stack contents
195    uint32_t sentry_base = stack_base - sentry_size;
196    uint32_t aux_data_base = sentry_base - aux_data_size;
197    uint32_t env_data_base = aux_data_base - env_data_size;
198    uint32_t arg_data_base = env_data_base - arg_data_size;
199    uint32_t platform_base = arg_data_base - platform_size;
200    uint32_t auxv_array_base = platform_base - aux_array_size - aux_padding;
201    uint32_t envp_array_base = auxv_array_base - envp_array_size;
202    uint32_t argv_array_base = envp_array_base - argv_array_size;
203    uint32_t argc_base = argv_array_base - argc_size;
204
205    DPRINTF(Stack, "The addresses of items on the initial stack:\n");
206    DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
207    DPRINTF(Stack, "0x%x - env data\n", env_data_base);
208    DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
209    DPRINTF(Stack, "0x%x - platform base\n", platform_base);
210    DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
211    DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
212    DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
213    DPRINTF(Stack, "0x%x - argc \n", argc_base);
214    DPRINTF(Stack, "0x%x - stack min\n", stack_min);
215
216    // write contents to stack
217
218    // figure out argc
219    uint32_t argc = argv.size();
220    uint32_t guestArgc = PowerISA::htog(argc);
221
222    //Write out the sentry void *
223    uint32_t sentry_NULL = 0;
224    initVirtMem.writeBlob(sentry_base,
225            (uint8_t*)&sentry_NULL, sentry_size);
226
227    //Fix up the aux vectors which point to other data
228    for (int i = auxv.size() - 1; i >= 0; i--) {
229        if (auxv[i].a_type == M5_AT_PLATFORM) {
230            auxv[i].a_val = platform_base;
231            initVirtMem.writeString(platform_base, platform.c_str());
232        } else if (auxv[i].a_type == M5_AT_EXECFN) {
233            auxv[i].a_val = aux_data_base;
234            initVirtMem.writeString(aux_data_base, filename.c_str());
235        }
236    }
237
238    //Copy the aux stuff
239    for (int x = 0; x < auxv.size(); x++)
240    {
241        initVirtMem.writeBlob(auxv_array_base + x * 2 * intSize,
242                (uint8_t*)&(auxv[x].a_type), intSize);
243        initVirtMem.writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
244                (uint8_t*)&(auxv[x].a_val), intSize);
245    }
246    //Write out the terminating zeroed auxilliary vector
247    const uint64_t zero = 0;
248    initVirtMem.writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
249            (uint8_t*)&zero, 2 * intSize);
250
251    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
252    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
253
254    initVirtMem.writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
255
256    ThreadContext *tc = system->getThreadContext(contextIds[0]);
257
258    //Set the stack pointer register
259    tc->setIntReg(StackPointerReg, stack_min);
260
261    tc->pcState(getStartPC());
262
263    //Align the "stack_min" to a page boundary.
264    stack_min = roundDown(stack_min, pageSize);
265}
266
267PowerISA::IntReg
268PowerLiveProcess::getSyscallArg(ThreadContext *tc, int &i)
269{
270    assert(i < 5);
271    return tc->readIntReg(ArgumentReg0 + i++);
272}
273
274void
275PowerLiveProcess::setSyscallArg(ThreadContext *tc,
276        int i, PowerISA::IntReg val)
277{
278    assert(i < 5);
279    tc->setIntReg(ArgumentReg0 + i, val);
280}
281
282void
283PowerLiveProcess::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret)
284{
285    Cr cr = tc->readIntReg(INTREG_CR);
286    if (sysret.successful()) {
287        cr.cr0.so = 0;
288    } else {
289        cr.cr0.so = 1;
290    }
291    tc->setIntReg(INTREG_CR, cr);
292    tc->setIntReg(ReturnValueReg, sysret.encodedValue());
293}
294