2 * Copyright (c) 2003-2006 The Regents of The University of Michigan
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.
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.
33 * Copyright (c) 2007 The Hewlett-Packard Development Company
34 * All rights reserved.
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:
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.
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
55 * Palo Alto, California 94304
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.
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.
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"
105 using namespace X86ISA
;
107 static const int ReturnValueReg
= INTREG_RAX
;
108 static const int ArgumentReg
[] = {
112 //This argument register is r10 for syscalls and rcx for C.
118 static const int NumArgumentRegs
= sizeof(ArgumentReg
) / sizeof(const int);
119 static const int ArgumentReg32
[] = {
126 static const int NumArgumentRegs32
= sizeof(ArgumentReg
) / sizeof(const int);
128 X86LiveProcess::X86LiveProcess(LiveProcessParams
* params
, ObjectFile
*objFile
,
129 SyscallDesc
*_syscallDescs
, int _numSyscallDescs
) :
130 LiveProcess(params
, objFile
), syscallDescs(_syscallDescs
),
131 numSyscallDescs(_numSyscallDescs
)
133 brk_point
= objFile
->dataBase() + objFile
->dataSize() + objFile
->bssSize();
134 brk_point
= roundUp(brk_point
, VMPageSize
);
137 X86_64LiveProcess::X86_64LiveProcess(LiveProcessParams
*params
,
138 ObjectFile
*objFile
, SyscallDesc
*_syscallDescs
,
139 int _numSyscallDescs
) :
140 X86LiveProcess(params
, objFile
, _syscallDescs
, _numSyscallDescs
)
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
;
147 // Set pointer for next thread stack. Reserve 8M for main stack.
148 next_thread_stack_base
= stack_base
- (8 * 1024 * 1024);
150 // Set up region for mmaps. This was determined empirically and may not
151 // always be correct.
152 mmap_start
= mmap_end
= (Addr
)0x2aaaaaaab000ULL
;
156 I386LiveProcess::syscall(int64_t callnum
, ThreadContext
*tc
)
158 Addr eip
= tc
->readPC();
159 if (eip
>= vsyscallPage
.base
&&
160 eip
< vsyscallPage
.base
+ vsyscallPage
.size
) {
161 tc
->setNextPC(vsyscallPage
.base
+ vsyscallPage
.vsysexitOffset
);
163 X86LiveProcess::syscall(callnum
, tc
);
167 I386LiveProcess::I386LiveProcess(LiveProcessParams
*params
,
168 ObjectFile
*objFile
, SyscallDesc
*_syscallDescs
,
169 int _numSyscallDescs
) :
170 X86LiveProcess(params
, objFile
, _syscallDescs
, _numSyscallDescs
)
172 _gdtStart
= 0x100000000;
173 _gdtSize
= VMPageSize
;
175 vsyscallPage
.base
= 0xffffe000ULL
;
176 vsyscallPage
.size
= VMPageSize
;
177 vsyscallPage
.vsyscallOffset
= 0x400;
178 vsyscallPage
.vsysexitOffset
= 0x410;
180 stack_base
= vsyscallPage
.base
;
182 // Set pointer for next thread stack. Reserve 8M for main stack.
183 next_thread_stack_base
= stack_base
- (8 * 1024 * 1024);
185 // Set up region for mmaps. This was determined empirically and may not
186 // always be correct.
187 mmap_start
= mmap_end
= (Addr
)0xf7ffd000ULL
;
191 X86LiveProcess::getDesc(int callnum
)
193 if (callnum
< 0 || callnum
>= numSyscallDescs
)
195 return &syscallDescs
[callnum
];
199 X86_64LiveProcess::startup()
201 LiveProcess::startup();
203 if (checkpointRestored
)
206 argsInit(sizeof(uint64_t), VMPageSize
);
208 for (int i
= 0; i
< contextIds
.size(); i
++) {
209 ThreadContext
* tc
= system
->getThreadContext(contextIds
[i
]);
211 SegAttr dataAttr
= 0;
212 dataAttr
.writable
= 1;
213 dataAttr
.readable
= 1;
214 dataAttr
.expandDown
= 0;
216 dataAttr
.defaultSize
= 0;
217 dataAttr
.longMode
= 1;
219 //Initialize the segment registers.
220 for(int seg
= 0; seg
< NUM_SEGMENTREGS
; seg
++) {
221 tc
->setMiscRegNoEffect(MISCREG_SEG_BASE(seg
), 0);
222 tc
->setMiscRegNoEffect(MISCREG_SEG_EFF_BASE(seg
), 0);
223 tc
->setMiscRegNoEffect(MISCREG_SEG_ATTR(seg
), dataAttr
);
229 csAttr
.expandDown
= 0;
231 csAttr
.defaultSize
= 0;
234 tc
->setMiscRegNoEffect(MISCREG_CS_ATTR
, csAttr
);
236 //Set up the registers that describe the operating mode.
238 cr0
.pg
= 1; // Turn on paging.
239 cr0
.cd
= 0; // Don't disable caching.
240 cr0
.nw
= 0; // This is bit is defined to be ignored.
241 cr0
.am
= 0; // No alignment checking
242 cr0
.wp
= 0; // Supervisor mode can write read only pages
244 cr0
.et
= 1; // This should always be 1
245 cr0
.ts
= 0; // We don't do task switching, so causing fp exceptions
246 // would be pointless.
247 cr0
.em
= 0; // Allow x87 instructions to execute natively.
248 cr0
.mp
= 1; // This doesn't really matter, but the manual suggests
249 // setting it to one.
250 cr0
.pe
= 1; // We're definitely in protected mode.
251 tc
->setMiscReg(MISCREG_CR0
, cr0
);
254 efer
.sce
= 1; // Enable system call extensions.
255 efer
.lme
= 1; // Enable long mode.
256 efer
.lma
= 1; // Activate long mode.
257 efer
.nxe
= 1; // Enable nx support.
258 efer
.svme
= 0; // Disable svm support for now. It isn't implemented.
259 efer
.ffxsr
= 1; // Turn on fast fxsave and fxrstor.
260 tc
->setMiscReg(MISCREG_EFER
, efer
);
265 I386LiveProcess::startup()
267 LiveProcess::startup();
269 if (checkpointRestored
)
272 argsInit(sizeof(uint32_t), VMPageSize
);
275 * Set up a GDT for this process. The whole GDT wouldn't really be for
276 * this process, but the only parts we care about are.
278 pTable
->allocate(_gdtStart
, _gdtSize
);
280 assert(_gdtSize
% sizeof(zero
) == 0);
281 for (Addr gdtCurrent
= _gdtStart
;
282 gdtCurrent
< _gdtStart
+ _gdtSize
; gdtCurrent
+= sizeof(zero
)) {
283 initVirtMem
->write(gdtCurrent
, zero
);
286 // Set up the vsyscall page for this process.
287 pTable
->allocate(vsyscallPage
.base
, vsyscallPage
.size
);
288 uint8_t vsyscallBlob
[] = {
292 0x89, 0xe5, // mov %esp, %ebp
293 0x0f, 0x34 // sysenter
295 initVirtMem
->writeBlob(vsyscallPage
.base
+ vsyscallPage
.vsyscallOffset
,
296 vsyscallBlob
, sizeof(vsyscallBlob
));
298 uint8_t vsysexitBlob
[] = {
304 initVirtMem
->writeBlob(vsyscallPage
.base
+ vsyscallPage
.vsysexitOffset
,
305 vsysexitBlob
, sizeof(vsysexitBlob
));
307 for (int i
= 0; i
< contextIds
.size(); i
++) {
308 ThreadContext
* tc
= system
->getThreadContext(contextIds
[i
]);
310 SegAttr dataAttr
= 0;
311 dataAttr
.writable
= 1;
312 dataAttr
.readable
= 1;
313 dataAttr
.expandDown
= 0;
315 dataAttr
.defaultSize
= 1;
316 dataAttr
.longMode
= 0;
318 //Initialize the segment registers.
319 for(int seg
= 0; seg
< NUM_SEGMENTREGS
; seg
++) {
320 tc
->setMiscRegNoEffect(MISCREG_SEG_BASE(seg
), 0);
321 tc
->setMiscRegNoEffect(MISCREG_SEG_EFF_BASE(seg
), 0);
322 tc
->setMiscRegNoEffect(MISCREG_SEG_ATTR(seg
), dataAttr
);
323 tc
->setMiscRegNoEffect(MISCREG_SEG_SEL(seg
), 0xB);
324 tc
->setMiscRegNoEffect(MISCREG_SEG_LIMIT(seg
), (uint32_t)(-1));
330 csAttr
.expandDown
= 0;
332 csAttr
.defaultSize
= 1;
335 tc
->setMiscRegNoEffect(MISCREG_CS_ATTR
, csAttr
);
337 tc
->setMiscRegNoEffect(MISCREG_TSG_BASE
, _gdtStart
);
338 tc
->setMiscRegNoEffect(MISCREG_TSG_EFF_BASE
, _gdtStart
);
339 tc
->setMiscRegNoEffect(MISCREG_TSG_LIMIT
, _gdtStart
+ _gdtSize
- 1);
341 // Set the LDT selector to 0 to deactivate it.
342 tc
->setMiscRegNoEffect(MISCREG_TSL
, 0);
344 //Set up the registers that describe the operating mode.
346 cr0
.pg
= 1; // Turn on paging.
347 cr0
.cd
= 0; // Don't disable caching.
348 cr0
.nw
= 0; // This is bit is defined to be ignored.
349 cr0
.am
= 0; // No alignment checking
350 cr0
.wp
= 0; // Supervisor mode can write read only pages
352 cr0
.et
= 1; // This should always be 1
353 cr0
.ts
= 0; // We don't do task switching, so causing fp exceptions
354 // would be pointless.
355 cr0
.em
= 0; // Allow x87 instructions to execute natively.
356 cr0
.mp
= 1; // This doesn't really matter, but the manual suggests
357 // setting it to one.
358 cr0
.pe
= 1; // We're definitely in protected mode.
359 tc
->setMiscReg(MISCREG_CR0
, cr0
);
362 efer
.sce
= 1; // Enable system call extensions.
363 efer
.lme
= 1; // Enable long mode.
364 efer
.lma
= 0; // Deactivate long mode.
365 efer
.nxe
= 1; // Enable nx support.
366 efer
.svme
= 0; // Disable svm support for now. It isn't implemented.
367 efer
.ffxsr
= 1; // Turn on fast fxsave and fxrstor.
368 tc
->setMiscReg(MISCREG_EFER
, efer
);
372 template<class IntType
>
374 X86LiveProcess::argsInit(int pageSize
,
375 std::vector
<AuxVector
<IntType
> > extraAuxvs
)
377 int intSize
= sizeof(IntType
);
379 typedef AuxVector
<IntType
> auxv_t
;
380 std::vector
<auxv_t
> auxv
= extraAuxvs
;
388 //We want 16 byte alignment
391 // load object file into target memory
392 objFile
->loadSections(initVirtMem
);
395 X86_OnboardFPU
= 1 << 0,
396 X86_VirtualModeExtensions
= 1 << 1,
397 X86_DebuggingExtensions
= 1 << 2,
398 X86_PageSizeExtensions
= 1 << 3,
400 X86_TimeStampCounter
= 1 << 4,
401 X86_ModelSpecificRegisters
= 1 << 5,
402 X86_PhysicalAddressExtensions
= 1 << 6,
403 X86_MachineCheckExtensions
= 1 << 7,
405 X86_CMPXCHG8Instruction
= 1 << 8,
406 X86_OnboardAPIC
= 1 << 9,
407 X86_SYSENTER_SYSEXIT
= 1 << 11,
409 X86_MemoryTypeRangeRegisters
= 1 << 12,
410 X86_PageGlobalEnable
= 1 << 13,
411 X86_MachineCheckArchitecture
= 1 << 14,
412 X86_CMOVInstruction
= 1 << 15,
414 X86_PageAttributeTable
= 1 << 16,
415 X86_36BitPSEs
= 1 << 17,
416 X86_ProcessorSerialNumber
= 1 << 18,
417 X86_CLFLUSHInstruction
= 1 << 19,
419 X86_DebugTraceStore
= 1 << 21,
420 X86_ACPIViaMSR
= 1 << 22,
421 X86_MultimediaExtensions
= 1 << 23,
423 X86_FXSAVE_FXRSTOR
= 1 << 24,
424 X86_StreamingSIMDExtensions
= 1 << 25,
425 X86_StreamingSIMDExtensions2
= 1 << 26,
426 X86_CPUSelfSnoop
= 1 << 27,
428 X86_HyperThreading
= 1 << 28,
429 X86_AutomaticClockControl
= 1 << 29,
430 X86_IA64Processor
= 1 << 30
433 //Setup the auxilliary vectors. These will already have endian conversion.
434 //Auxilliary vectors are loaded only for elf formatted executables.
435 ElfObject
* elfObject
= dynamic_cast<ElfObject
*>(objFile
);
440 X86_VirtualModeExtensions
|
441 X86_DebuggingExtensions
|
442 X86_PageSizeExtensions
|
443 X86_TimeStampCounter
|
444 X86_ModelSpecificRegisters
|
445 X86_PhysicalAddressExtensions
|
446 X86_MachineCheckExtensions
|
447 X86_CMPXCHG8Instruction
|
449 X86_SYSENTER_SYSEXIT
|
450 X86_MemoryTypeRangeRegisters
|
451 X86_PageGlobalEnable
|
452 X86_MachineCheckArchitecture
|
453 X86_CMOVInstruction
|
454 X86_PageAttributeTable
|
456 // X86_ProcessorSerialNumber |
457 X86_CLFLUSHInstruction
|
458 // X86_DebugTraceStore |
460 X86_MultimediaExtensions
|
462 X86_StreamingSIMDExtensions
|
463 X86_StreamingSIMDExtensions2
|
464 // X86_CPUSelfSnoop |
465 // X86_HyperThreading |
466 // X86_AutomaticClockControl |
467 // X86_IA64Processor |
470 //Bits which describe the system hardware capabilities
471 //XXX Figure out what these should be
472 auxv
.push_back(auxv_t(M5_AT_HWCAP
, features
));
473 //The system page size
474 auxv
.push_back(auxv_t(M5_AT_PAGESZ
, X86ISA::VMPageSize
));
475 //Frequency at which times() increments
476 auxv
.push_back(auxv_t(M5_AT_CLKTCK
, 100));
477 // For statically linked executables, this is the virtual address of the
478 // program header tables if they appear in the executable image
479 auxv
.push_back(auxv_t(M5_AT_PHDR
, elfObject
->programHeaderTable()));
480 // This is the size of a program header entry from the elf file.
481 auxv
.push_back(auxv_t(M5_AT_PHENT
, elfObject
->programHeaderSize()));
482 // This is the number of program headers from the original elf file.
483 auxv
.push_back(auxv_t(M5_AT_PHNUM
, elfObject
->programHeaderCount()));
484 //Defined to be 100 in the kernel source.
485 //This is the address of the elf "interpreter", It should be set
486 //to 0 for regular executables. It should be something else
487 //(not sure what) for dynamic libraries.
488 auxv
.push_back(auxv_t(M5_AT_BASE
, 0));
490 //XXX Figure out what this should be.
491 auxv
.push_back(auxv_t(M5_AT_FLAGS
, 0));
492 //The entry point to the program
493 auxv
.push_back(auxv_t(M5_AT_ENTRY
, objFile
->entryPoint()));
494 //Different user and group IDs
495 auxv
.push_back(auxv_t(M5_AT_UID
, uid()));
496 auxv
.push_back(auxv_t(M5_AT_EUID
, euid()));
497 auxv
.push_back(auxv_t(M5_AT_GID
, gid()));
498 auxv
.push_back(auxv_t(M5_AT_EGID
, egid()));
499 //Whether to enable "secure mode" in the executable
500 auxv
.push_back(auxv_t(M5_AT_SECURE
, 0));
501 //The string "x86_64" with unknown meaning
502 auxv
.push_back(auxv_t(M5_AT_PLATFORM
, 0));
505 //Figure out how big the initial stack needs to be
507 // A sentry NULL void pointer at the top of the stack.
508 int sentry_size
= intSize
;
510 //This is the name of the file which is present on the initial stack
511 //It's purpose is to let the user space linker examine the original file.
512 int file_name_size
= filename
.size() + 1;
514 string platform
= "x86_64";
515 int aux_data_size
= platform
.size() + 1;
517 int env_data_size
= 0;
518 for (int i
= 0; i
< envp
.size(); ++i
) {
519 env_data_size
+= envp
[i
].size() + 1;
521 int arg_data_size
= 0;
522 for (int i
= 0; i
< argv
.size(); ++i
) {
523 arg_data_size
+= argv
[i
].size() + 1;
526 //The info_block needs to be padded so it's size is a multiple of the
527 //alignment mask. Also, it appears that there needs to be at least some
528 //padding, so if the size is already a multiple, we need to increase it
530 int base_info_block_size
=
531 sentry_size
+ file_name_size
+ env_data_size
+ arg_data_size
;
533 int info_block_size
= roundUp(base_info_block_size
, align
);
535 int info_block_padding
= info_block_size
- base_info_block_size
;
537 //Each auxilliary vector is two 8 byte words
538 int aux_array_size
= intSize
* 2 * (auxv
.size() + 1);
540 int envp_array_size
= intSize
* (envp
.size() + 1);
541 int argv_array_size
= intSize
* (argv
.size() + 1);
543 int argc_size
= intSize
;
545 //Figure out the size of the contents of the actual initial frame
552 //There needs to be padding after the auxiliary vector data so that the
553 //very bottom of the stack is aligned properly.
554 int partial_size
= frame_size
+ aux_data_size
;
555 int aligned_partial_size
= roundUp(partial_size
, align
);
556 int aux_padding
= aligned_partial_size
- partial_size
;
564 stack_min
= stack_base
- space_needed
;
565 stack_min
= roundDown(stack_min
, align
);
566 stack_size
= stack_base
- stack_min
;
569 pTable
->allocate(roundDown(stack_min
, pageSize
),
570 roundUp(stack_size
, pageSize
));
572 // map out initial stack contents
573 IntType sentry_base
= stack_base
- sentry_size
;
574 IntType file_name_base
= sentry_base
- file_name_size
;
575 IntType env_data_base
= file_name_base
- env_data_size
;
576 IntType arg_data_base
= env_data_base
- arg_data_size
;
577 IntType aux_data_base
= arg_data_base
- info_block_padding
- aux_data_size
;
578 IntType auxv_array_base
= aux_data_base
- aux_array_size
- aux_padding
;
579 IntType envp_array_base
= auxv_array_base
- envp_array_size
;
580 IntType argv_array_base
= envp_array_base
- argv_array_size
;
581 IntType argc_base
= argv_array_base
- argc_size
;
583 DPRINTF(Stack
, "The addresses of items on the initial stack:\n");
584 DPRINTF(Stack
, "0x%x - file name\n", file_name_base
);
585 DPRINTF(Stack
, "0x%x - env data\n", env_data_base
);
586 DPRINTF(Stack
, "0x%x - arg data\n", arg_data_base
);
587 DPRINTF(Stack
, "0x%x - aux data\n", aux_data_base
);
588 DPRINTF(Stack
, "0x%x - auxv array\n", auxv_array_base
);
589 DPRINTF(Stack
, "0x%x - envp array\n", envp_array_base
);
590 DPRINTF(Stack
, "0x%x - argv array\n", argv_array_base
);
591 DPRINTF(Stack
, "0x%x - argc \n", argc_base
);
592 DPRINTF(Stack
, "0x%x - stack min\n", stack_min
);
594 // write contents to stack
597 IntType argc
= argv
.size();
598 IntType guestArgc
= X86ISA::htog(argc
);
600 //Write out the sentry void *
601 IntType sentry_NULL
= 0;
602 initVirtMem
->writeBlob(sentry_base
,
603 (uint8_t*)&sentry_NULL
, sentry_size
);
605 //Write the file name
606 initVirtMem
->writeString(file_name_base
, filename
.c_str());
608 //Fix up the aux vector which points to the "platform" string
609 assert(auxv
[auxv
.size() - 1].a_type
= M5_AT_PLATFORM
);
610 auxv
[auxv
.size() - 1].a_val
= aux_data_base
;
613 for(int x
= 0; x
< auxv
.size(); x
++)
615 initVirtMem
->writeBlob(auxv_array_base
+ x
* 2 * intSize
,
616 (uint8_t*)&(auxv
[x
].a_type
), intSize
);
617 initVirtMem
->writeBlob(auxv_array_base
+ (x
* 2 + 1) * intSize
,
618 (uint8_t*)&(auxv
[x
].a_val
), intSize
);
620 //Write out the terminating zeroed auxilliary vector
621 const uint64_t zero
= 0;
622 initVirtMem
->writeBlob(auxv_array_base
+ 2 * intSize
* auxv
.size(),
623 (uint8_t*)&zero
, 2 * intSize
);
625 initVirtMem
->writeString(aux_data_base
, platform
.c_str());
627 copyStringArray(envp
, envp_array_base
, env_data_base
, initVirtMem
);
628 copyStringArray(argv
, argv_array_base
, arg_data_base
, initVirtMem
);
630 initVirtMem
->writeBlob(argc_base
, (uint8_t*)&guestArgc
, intSize
);
632 ThreadContext
*tc
= system
->getThreadContext(contextIds
[0]);
633 //Set the stack pointer register
634 tc
->setIntReg(StackPointerReg
, stack_min
);
636 Addr prog_entry
= objFile
->entryPoint();
637 // There doesn't need to be any segment base added in since we're dealing
638 // with the flat segmentation model.
639 tc
->setPC(prog_entry
);
640 tc
->setNextPC(prog_entry
+ sizeof(MachInst
));
642 //Align the "stack_min" to a page boundary.
643 stack_min
= roundDown(stack_min
, pageSize
);
649 X86_64LiveProcess::argsInit(int intSize
, int pageSize
)
651 std::vector
<AuxVector
<uint64_t> > extraAuxvs
;
652 X86LiveProcess::argsInit
<uint64_t>(pageSize
, extraAuxvs
);
656 I386LiveProcess::argsInit(int intSize
, int pageSize
)
658 std::vector
<AuxVector
<uint32_t> > extraAuxvs
;
659 //Tell the binary where the vsyscall part of the vsyscall page is.
660 extraAuxvs
.push_back(AuxVector
<uint32_t>(0x20,
661 vsyscallPage
.base
+ vsyscallPage
.vsyscallOffset
));
662 extraAuxvs
.push_back(AuxVector
<uint32_t>(0x21, vsyscallPage
.base
));
663 X86LiveProcess::argsInit
<uint32_t>(pageSize
, extraAuxvs
);
667 X86LiveProcess::setSyscallReturn(ThreadContext
*tc
, SyscallReturn return_value
)
669 tc
->setIntReg(INTREG_RAX
, return_value
.value());
673 X86_64LiveProcess::getSyscallArg(ThreadContext
*tc
, int i
)
675 assert(i
< NumArgumentRegs
);
676 return tc
->readIntReg(ArgumentReg
[i
]);
680 X86_64LiveProcess::setSyscallArg(ThreadContext
*tc
, int i
, X86ISA::IntReg val
)
682 assert(i
< NumArgumentRegs
);
683 return tc
->setIntReg(ArgumentReg
[i
], val
);
687 I386LiveProcess::getSyscallArg(ThreadContext
*tc
, int i
)
689 assert(i
< NumArgumentRegs32
);
690 return tc
->readIntReg(ArgumentReg32
[i
]);
694 I386LiveProcess::setSyscallArg(ThreadContext
*tc
, int i
, X86ISA::IntReg val
)
696 assert(i
< NumArgumentRegs
);
697 return tc
->setIntReg(ArgumentReg
[i
], val
);