process.cc revision 5958
112855Sgabeblack@google.com/*
212855Sgabeblack@google.com * Copyright (c) 2003-2006 The Regents of The University of Michigan
312855Sgabeblack@google.com * All rights reserved.
412855Sgabeblack@google.com *
512855Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
612855Sgabeblack@google.com * modification, are permitted provided that the following conditions are
712855Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
812855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
912855Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1012855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1112855Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1212855Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1312855Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1412855Sgabeblack@google.com * this software without specific prior written permission.
1512855Sgabeblack@google.com *
1612855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712855Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812855Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912855Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012855Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112855Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212855Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312855Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412855Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512855Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612855Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712855Sgabeblack@google.com *
2812855Sgabeblack@google.com * Authors: Gabe Black
2912855Sgabeblack@google.com *          Ali Saidi
3012855Sgabeblack@google.com */
3112855Sgabeblack@google.com
3212855Sgabeblack@google.com/*
3312855Sgabeblack@google.com * Copyright (c) 2007 The Hewlett-Packard Development Company
3412855Sgabeblack@google.com * All rights reserved.
3512855Sgabeblack@google.com *
3612855Sgabeblack@google.com * Redistribution and use of this software in source and binary forms,
3712855Sgabeblack@google.com * with or without modification, are permitted provided that the
3812855Sgabeblack@google.com * following conditions are met:
3912855Sgabeblack@google.com *
4012855Sgabeblack@google.com * The software must be used only for Non-Commercial Use which means any
4112855Sgabeblack@google.com * use which is NOT directed to receiving any direct monetary
4212855Sgabeblack@google.com * compensation for, or commercial advantage from such use.  Illustrative
4312855Sgabeblack@google.com * examples of non-commercial use are academic research, personal study,
4412855Sgabeblack@google.com * teaching, education and corporate research & development.
4512855Sgabeblack@google.com * Illustrative examples of commercial use are distributing products for
4612855Sgabeblack@google.com * commercial advantage and providing services using the software for
4712855Sgabeblack@google.com * commercial advantage.
4812855Sgabeblack@google.com *
4912855Sgabeblack@google.com * If you wish to use this software or functionality therein that may be
5012855Sgabeblack@google.com * covered by patents for commercial use, please contact:
5112855Sgabeblack@google.com *     Director of Intellectual Property Licensing
5212855Sgabeblack@google.com *     Office of Strategy and Technology
5312855Sgabeblack@google.com *     Hewlett-Packard Company
5412855Sgabeblack@google.com *     1501 Page Mill Road
5512855Sgabeblack@google.com *     Palo Alto, California  94304
5612855Sgabeblack@google.com *
5712855Sgabeblack@google.com * Redistributions of source code must retain the above copyright notice,
5812855Sgabeblack@google.com * this list of conditions and the following disclaimer.  Redistributions
5912855Sgabeblack@google.com * in binary form must reproduce the above copyright notice, this list of
6012855Sgabeblack@google.com * conditions and the following disclaimer in the documentation and/or
6112855Sgabeblack@google.com * other materials provided with the distribution.  Neither the name of
6212855Sgabeblack@google.com * the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
6312855Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
6412855Sgabeblack@google.com * this software without specific prior written permission.  No right of
6512855Sgabeblack@google.com * sublicense is granted herewith.  Derivatives of the software and
6612855Sgabeblack@google.com * output created using the software may be prepared, but only for
6712855Sgabeblack@google.com * Non-Commercial Uses.  Derivatives of the software may be shared with
6812855Sgabeblack@google.com * others provided: (i) the others agree to abide by the list of
6912855Sgabeblack@google.com * conditions herein which includes the Non-Commercial Use restrictions;
7012855Sgabeblack@google.com * and (ii) such Derivatives of the software include the above copyright
7112855Sgabeblack@google.com * notice to acknowledge the contribution from this software where
7212855Sgabeblack@google.com * applicable, this list of conditions and the disclaimer below.
7312855Sgabeblack@google.com *
7412855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7512855Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7612855Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7712855Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
78 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
81 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
82 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
83 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
84 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
85 *
86 * Authors: Gabe Black
87 */
88
89#include "arch/x86/isa_traits.hh"
90#include "arch/x86/process.hh"
91#include "arch/x86/segmentregs.hh"
92#include "arch/x86/types.hh"
93#include "base/loader/object_file.hh"
94#include "base/loader/elf_object.hh"
95#include "base/misc.hh"
96#include "base/trace.hh"
97#include "cpu/thread_context.hh"
98#include "mem/page_table.hh"
99#include "mem/translating_port.hh"
100#include "sim/process_impl.hh"
101#include "sim/syscall_emul.hh"
102#include "sim/system.hh"
103
104using namespace std;
105using namespace X86ISA;
106
107static const int ReturnValueReg = INTREG_RAX;
108static const int ArgumentReg[] = {
109    INTREG_RDI,
110    INTREG_RSI,
111    INTREG_RDX,
112    //This argument register is r10 for syscalls and rcx for C.
113    INTREG_R10W,
114    //INTREG_RCX,
115    INTREG_R8W,
116    INTREG_R9W
117};
118static const int NumArgumentRegs = sizeof(ArgumentReg) / sizeof(const int);
119
120X86LiveProcess::X86LiveProcess(LiveProcessParams * params, ObjectFile *objFile,
121        SyscallDesc *_syscallDescs, int _numSyscallDescs) :
122    LiveProcess(params, objFile), syscallDescs(_syscallDescs),
123    numSyscallDescs(_numSyscallDescs)
124{
125    brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
126    brk_point = roundUp(brk_point, VMPageSize);
127}
128
129X86_64LiveProcess::X86_64LiveProcess(LiveProcessParams *params,
130        ObjectFile *objFile, SyscallDesc *_syscallDescs,
131        int _numSyscallDescs) :
132    X86LiveProcess(params, objFile, _syscallDescs, _numSyscallDescs)
133{
134    // Set up stack. On X86_64 Linux, stack goes from the top of memory
135    // downward, less the hole for the kernel address space plus one page
136    // for undertermined purposes.
137    stack_base = (Addr)0x7FFFFFFFF000ULL;
138
139    // Set pointer for next thread stack.  Reserve 8M for main stack.
140    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
141
142    // Set up region for mmaps. This was determined empirically and may not
143    // always be correct.
144    mmap_start = mmap_end = (Addr)0x2aaaaaaab000ULL;
145}
146
147I386LiveProcess::I386LiveProcess(LiveProcessParams *params,
148        ObjectFile *objFile, SyscallDesc *_syscallDescs,
149        int _numSyscallDescs) :
150    X86LiveProcess(params, objFile, _syscallDescs, _numSyscallDescs)
151{
152    stack_base = (Addr)0xffffe000ULL;
153
154    // Set pointer for next thread stack.  Reserve 8M for main stack.
155    next_thread_stack_base = stack_base - (8 * 1024 * 1024);
156
157    // Set up region for mmaps. This was determined empirically and may not
158    // always be correct.
159    mmap_start = mmap_end = (Addr)0xf7ffd000ULL;
160}
161
162SyscallDesc*
163X86LiveProcess::getDesc(int callnum)
164{
165    if (callnum < 0 || callnum >= numSyscallDescs)
166        return NULL;
167    return &syscallDescs[callnum];
168}
169
170void
171X86_64LiveProcess::startup()
172{
173    LiveProcess::startup();
174
175    if (checkpointRestored)
176        return;
177
178    argsInit(sizeof(uint64_t), VMPageSize);
179
180    for (int i = 0; i < contextIds.size(); i++) {
181        ThreadContext * tc = system->getThreadContext(contextIds[i]);
182
183        SegAttr dataAttr = 0;
184        dataAttr.writable = 1;
185        dataAttr.readable = 1;
186        dataAttr.expandDown = 0;
187        dataAttr.dpl = 3;
188        dataAttr.defaultSize = 0;
189        dataAttr.longMode = 1;
190
191        //Initialize the segment registers.
192        for(int seg = 0; seg < NUM_SEGMENTREGS; seg++) {
193            tc->setMiscRegNoEffect(MISCREG_SEG_BASE(seg), 0);
194            tc->setMiscRegNoEffect(MISCREG_SEG_EFF_BASE(seg), 0);
195            tc->setMiscRegNoEffect(MISCREG_SEG_ATTR(seg), dataAttr);
196        }
197
198        SegAttr csAttr = 0;
199        csAttr.writable = 0;
200        csAttr.readable = 1;
201        csAttr.expandDown = 0;
202        csAttr.dpl = 3;
203        csAttr.defaultSize = 0;
204        csAttr.longMode = 1;
205
206        tc->setMiscRegNoEffect(MISCREG_CS_ATTR, csAttr);
207
208        //Set up the registers that describe the operating mode.
209        CR0 cr0 = 0;
210        cr0.pg = 1; // Turn on paging.
211        cr0.cd = 0; // Don't disable caching.
212        cr0.nw = 0; // This is bit is defined to be ignored.
213        cr0.am = 0; // No alignment checking
214        cr0.wp = 0; // Supervisor mode can write read only pages
215        cr0.ne = 1;
216        cr0.et = 1; // This should always be 1
217        cr0.ts = 0; // We don't do task switching, so causing fp exceptions
218                    // would be pointless.
219        cr0.em = 0; // Allow x87 instructions to execute natively.
220        cr0.mp = 1; // This doesn't really matter, but the manual suggests
221                    // setting it to one.
222        cr0.pe = 1; // We're definitely in protected mode.
223        tc->setMiscReg(MISCREG_CR0, cr0);
224
225        Efer efer = 0;
226        efer.sce = 1; // Enable system call extensions.
227        efer.lme = 1; // Enable long mode.
228        efer.lma = 1; // Activate long mode.
229        efer.nxe = 1; // Enable nx support.
230        efer.svme = 0; // Disable svm support for now. It isn't implemented.
231        efer.ffxsr = 1; // Turn on fast fxsave and fxrstor.
232        tc->setMiscReg(MISCREG_EFER, efer);
233    }
234}
235
236void
237I386LiveProcess::startup()
238{
239    LiveProcess::startup();
240
241    if (checkpointRestored)
242        return;
243
244    argsInit(sizeof(uint32_t), VMPageSize);
245
246    for (int i = 0; i < contextIds.size(); i++) {
247        ThreadContext * tc = system->getThreadContext(contextIds[i]);
248
249        SegAttr dataAttr = 0;
250        dataAttr.writable = 1;
251        dataAttr.readable = 1;
252        dataAttr.expandDown = 0;
253        dataAttr.dpl = 3;
254        dataAttr.defaultSize = 1;
255        dataAttr.longMode = 0;
256
257        //Initialize the segment registers.
258        for(int seg = 0; seg < NUM_SEGMENTREGS; seg++) {
259            tc->setMiscRegNoEffect(MISCREG_SEG_BASE(seg), 0);
260            tc->setMiscRegNoEffect(MISCREG_SEG_EFF_BASE(seg), 0);
261            tc->setMiscRegNoEffect(MISCREG_SEG_ATTR(seg), dataAttr);
262            tc->setMiscRegNoEffect(MISCREG_SEG_SEL(seg), 0xB);
263        }
264
265        SegAttr csAttr = 0;
266        csAttr.writable = 0;
267        csAttr.readable = 1;
268        csAttr.expandDown = 0;
269        csAttr.dpl = 3;
270        csAttr.defaultSize = 1;
271        csAttr.longMode = 0;
272
273        tc->setMiscRegNoEffect(MISCREG_CS_ATTR, csAttr);
274
275        //Set up the registers that describe the operating mode.
276        CR0 cr0 = 0;
277        cr0.pg = 1; // Turn on paging.
278        cr0.cd = 0; // Don't disable caching.
279        cr0.nw = 0; // This is bit is defined to be ignored.
280        cr0.am = 0; // No alignment checking
281        cr0.wp = 0; // Supervisor mode can write read only pages
282        cr0.ne = 1;
283        cr0.et = 1; // This should always be 1
284        cr0.ts = 0; // We don't do task switching, so causing fp exceptions
285                    // would be pointless.
286        cr0.em = 0; // Allow x87 instructions to execute natively.
287        cr0.mp = 1; // This doesn't really matter, but the manual suggests
288                    // setting it to one.
289        cr0.pe = 1; // We're definitely in protected mode.
290        tc->setMiscReg(MISCREG_CR0, cr0);
291
292        Efer efer = 0;
293        efer.sce = 1; // Enable system call extensions.
294        efer.lme = 1; // Enable long mode.
295        efer.lma = 0; // Deactivate long mode.
296        efer.nxe = 1; // Enable nx support.
297        efer.svme = 0; // Disable svm support for now. It isn't implemented.
298        efer.ffxsr = 1; // Turn on fast fxsave and fxrstor.
299        tc->setMiscReg(MISCREG_EFER, efer);
300    }
301}
302
303template<class IntType>
304void
305X86LiveProcess::argsInit(int pageSize)
306{
307    int intSize = sizeof(IntType);
308
309    typedef AuxVector<IntType> auxv_t;
310    std::vector<auxv_t>  auxv;
311
312    string filename;
313    if(argv.size() < 1)
314        filename = "";
315    else
316        filename = argv[0];
317
318    //We want 16 byte alignment
319    uint64_t align = 16;
320
321    // load object file into target memory
322    objFile->loadSections(initVirtMem);
323
324    enum X86CpuFeature {
325        X86_OnboardFPU = 1 << 0,
326        X86_VirtualModeExtensions = 1 << 1,
327        X86_DebuggingExtensions = 1 << 2,
328        X86_PageSizeExtensions = 1 << 3,
329
330        X86_TimeStampCounter = 1 << 4,
331        X86_ModelSpecificRegisters = 1 << 5,
332        X86_PhysicalAddressExtensions = 1 << 6,
333        X86_MachineCheckExtensions = 1 << 7,
334
335        X86_CMPXCHG8Instruction = 1 << 8,
336        X86_OnboardAPIC = 1 << 9,
337        X86_SYSENTER_SYSEXIT = 1 << 11,
338
339        X86_MemoryTypeRangeRegisters = 1 << 12,
340        X86_PageGlobalEnable = 1 << 13,
341        X86_MachineCheckArchitecture = 1 << 14,
342        X86_CMOVInstruction = 1 << 15,
343
344        X86_PageAttributeTable = 1 << 16,
345        X86_36BitPSEs = 1 << 17,
346        X86_ProcessorSerialNumber = 1 << 18,
347        X86_CLFLUSHInstruction = 1 << 19,
348
349        X86_DebugTraceStore = 1 << 21,
350        X86_ACPIViaMSR = 1 << 22,
351        X86_MultimediaExtensions = 1 << 23,
352
353        X86_FXSAVE_FXRSTOR = 1 << 24,
354        X86_StreamingSIMDExtensions = 1 << 25,
355        X86_StreamingSIMDExtensions2 = 1 << 26,
356        X86_CPUSelfSnoop = 1 << 27,
357
358        X86_HyperThreading = 1 << 28,
359        X86_AutomaticClockControl = 1 << 29,
360        X86_IA64Processor = 1 << 30
361    };
362
363    //Setup the auxilliary vectors. These will already have endian conversion.
364    //Auxilliary vectors are loaded only for elf formatted executables.
365    ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
366    if(elfObject)
367    {
368        uint64_t features =
369            X86_OnboardFPU |
370            X86_VirtualModeExtensions |
371            X86_DebuggingExtensions |
372            X86_PageSizeExtensions |
373            X86_TimeStampCounter |
374            X86_ModelSpecificRegisters |
375            X86_PhysicalAddressExtensions |
376            X86_MachineCheckExtensions |
377            X86_CMPXCHG8Instruction |
378            X86_OnboardAPIC |
379            X86_SYSENTER_SYSEXIT |
380            X86_MemoryTypeRangeRegisters |
381            X86_PageGlobalEnable |
382            X86_MachineCheckArchitecture |
383            X86_CMOVInstruction |
384            X86_PageAttributeTable |
385            X86_36BitPSEs |
386//            X86_ProcessorSerialNumber |
387            X86_CLFLUSHInstruction |
388//            X86_DebugTraceStore |
389//            X86_ACPIViaMSR |
390            X86_MultimediaExtensions |
391            X86_FXSAVE_FXRSTOR |
392            X86_StreamingSIMDExtensions |
393            X86_StreamingSIMDExtensions2 |
394//            X86_CPUSelfSnoop |
395//            X86_HyperThreading |
396//            X86_AutomaticClockControl |
397//            X86_IA64Processor |
398            0;
399
400        //Bits which describe the system hardware capabilities
401        //XXX Figure out what these should be
402        auxv.push_back(auxv_t(M5_AT_HWCAP, features));
403        //The system page size
404        auxv.push_back(auxv_t(M5_AT_PAGESZ, X86ISA::VMPageSize));
405        //Frequency at which times() increments
406        auxv.push_back(auxv_t(M5_AT_CLKTCK, 100));
407        // For statically linked executables, this is the virtual address of the
408        // program header tables if they appear in the executable image
409        auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
410        // This is the size of a program header entry from the elf file.
411        auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
412        // This is the number of program headers from the original elf file.
413        auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
414        //Defined to be 100 in the kernel source.
415        //This is the address of the elf "interpreter", It should be set
416        //to 0 for regular executables. It should be something else
417        //(not sure what) for dynamic libraries.
418        auxv.push_back(auxv_t(M5_AT_BASE, 0));
419
420        //XXX Figure out what this should be.
421        auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
422        //The entry point to the program
423        auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
424        //Different user and group IDs
425        auxv.push_back(auxv_t(M5_AT_UID, uid()));
426        auxv.push_back(auxv_t(M5_AT_EUID, euid()));
427        auxv.push_back(auxv_t(M5_AT_GID, gid()));
428        auxv.push_back(auxv_t(M5_AT_EGID, egid()));
429        //Whether to enable "secure mode" in the executable
430        auxv.push_back(auxv_t(M5_AT_SECURE, 0));
431        //The string "x86_64" with unknown meaning
432        auxv.push_back(auxv_t(M5_AT_PLATFORM, 0));
433    }
434
435    //Figure out how big the initial stack needs to be
436
437    // A sentry NULL void pointer at the top of the stack.
438    int sentry_size = intSize;
439
440    //This is the name of the file which is present on the initial stack
441    //It's purpose is to let the user space linker examine the original file.
442    int file_name_size = filename.size() + 1;
443
444    string platform = "x86_64";
445    int aux_data_size = platform.size() + 1;
446
447    int env_data_size = 0;
448    for (int i = 0; i < envp.size(); ++i) {
449        env_data_size += envp[i].size() + 1;
450    }
451    int arg_data_size = 0;
452    for (int i = 0; i < argv.size(); ++i) {
453        arg_data_size += argv[i].size() + 1;
454    }
455
456    //The info_block needs to be padded so it's size is a multiple of the
457    //alignment mask. Also, it appears that there needs to be at least some
458    //padding, so if the size is already a multiple, we need to increase it
459    //anyway.
460    int base_info_block_size =
461        sentry_size + file_name_size + env_data_size + arg_data_size;
462
463    int info_block_size = roundUp(base_info_block_size, align);
464
465    int info_block_padding = info_block_size - base_info_block_size;
466
467    //Each auxilliary vector is two 8 byte words
468    int aux_array_size = intSize * 2 * (auxv.size() + 1);
469
470    int envp_array_size = intSize * (envp.size() + 1);
471    int argv_array_size = intSize * (argv.size() + 1);
472
473    int argc_size = intSize;
474
475    //Figure out the size of the contents of the actual initial frame
476    int frame_size =
477        aux_array_size +
478        envp_array_size +
479        argv_array_size +
480        argc_size;
481
482    //There needs to be padding after the auxiliary vector data so that the
483    //very bottom of the stack is aligned properly.
484    int partial_size = frame_size + aux_data_size;
485    int aligned_partial_size = roundUp(partial_size, align);
486    int aux_padding = aligned_partial_size - partial_size;
487
488    int space_needed =
489        info_block_size +
490        aux_data_size +
491        aux_padding +
492        frame_size;
493
494    stack_min = stack_base - space_needed;
495    stack_min = roundDown(stack_min, align);
496    stack_size = stack_base - stack_min;
497
498    // map memory
499    pTable->allocate(roundDown(stack_min, pageSize),
500                     roundUp(stack_size, pageSize));
501
502    // map out initial stack contents
503    IntType sentry_base = stack_base - sentry_size;
504    IntType file_name_base = sentry_base - file_name_size;
505    IntType env_data_base = file_name_base - env_data_size;
506    IntType arg_data_base = env_data_base - arg_data_size;
507    IntType aux_data_base = arg_data_base - info_block_padding - aux_data_size;
508    IntType auxv_array_base = aux_data_base - aux_array_size - aux_padding;
509    IntType envp_array_base = auxv_array_base - envp_array_size;
510    IntType argv_array_base = envp_array_base - argv_array_size;
511    IntType argc_base = argv_array_base - argc_size;
512
513    DPRINTF(Stack, "The addresses of items on the initial stack:\n");
514    DPRINTF(Stack, "0x%x - file name\n", file_name_base);
515    DPRINTF(Stack, "0x%x - env data\n", env_data_base);
516    DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
517    DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
518    DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
519    DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
520    DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
521    DPRINTF(Stack, "0x%x - argc \n", argc_base);
522    DPRINTF(Stack, "0x%x - stack min\n", stack_min);
523
524    // write contents to stack
525
526    // figure out argc
527    IntType argc = argv.size();
528    IntType guestArgc = X86ISA::htog(argc);
529
530    //Write out the sentry void *
531    IntType sentry_NULL = 0;
532    initVirtMem->writeBlob(sentry_base,
533            (uint8_t*)&sentry_NULL, sentry_size);
534
535    //Write the file name
536    initVirtMem->writeString(file_name_base, filename.c_str());
537
538    //Fix up the aux vector which points to the "platform" string
539    assert(auxv[auxv.size() - 1].a_type = M5_AT_PLATFORM);
540    auxv[auxv.size() - 1].a_val = aux_data_base;
541
542    //Copy the aux stuff
543    for(int x = 0; x < auxv.size(); x++)
544    {
545        initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
546                (uint8_t*)&(auxv[x].a_type), intSize);
547        initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
548                (uint8_t*)&(auxv[x].a_val), intSize);
549    }
550    //Write out the terminating zeroed auxilliary vector
551    const uint64_t zero = 0;
552    initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
553            (uint8_t*)&zero, 2 * intSize);
554
555    initVirtMem->writeString(aux_data_base, platform.c_str());
556
557    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
558    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
559
560    initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
561
562    ThreadContext *tc = system->getThreadContext(contextIds[0]);
563    //Set the stack pointer register
564    tc->setIntReg(StackPointerReg, stack_min);
565
566    Addr prog_entry = objFile->entryPoint();
567    // There doesn't need to be any segment base added in since we're dealing
568    // with the flat segmentation model.
569    tc->setPC(prog_entry);
570    tc->setNextPC(prog_entry + sizeof(MachInst));
571
572    //Align the "stack_min" to a page boundary.
573    stack_min = roundDown(stack_min, pageSize);
574
575//    num_processes++;
576}
577
578void
579X86_64LiveProcess::argsInit(int intSize, int pageSize)
580{
581    X86LiveProcess::argsInit<uint64_t>(pageSize);
582}
583
584void
585I386LiveProcess::argsInit(int intSize, int pageSize)
586{
587    X86LiveProcess::argsInit<uint32_t>(pageSize);
588}
589
590void
591X86LiveProcess::setSyscallReturn(ThreadContext *tc, SyscallReturn return_value)
592{
593    tc->setIntReg(INTREG_RAX, return_value.value());
594}
595
596X86ISA::IntReg
597X86_64LiveProcess::getSyscallArg(ThreadContext *tc, int i)
598{
599    assert(i < NumArgumentRegs);
600    return tc->readIntReg(ArgumentReg[i]);
601}
602
603void
604X86_64LiveProcess::setSyscallArg(ThreadContext *tc, int i, X86ISA::IntReg val)
605{
606    assert(i < NumArgumentRegs);
607    return tc->setIntReg(ArgumentReg[i], val);
608}
609
610X86ISA::IntReg
611I386LiveProcess::getSyscallArg(ThreadContext *tc, int i)
612{
613    panic("32 bit getSyscallArg not implemented.\n");
614}
615
616void
617I386LiveProcess::setSyscallArg(ThreadContext *tc, int i, X86ISA::IntReg val)
618{
619    panic("32 bit setSyscallArg not implemented.\n");
620}
621