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