SE: move page allocation from PageTable to Process
[gem5.git] / src / arch / x86 / process.cc
1 /*
2 * Copyright (c) 2007 The Hewlett-Packard Development Company
3 * All rights reserved.
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2003-2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Gabe Black
41 * Ali Saidi
42 */
43
44 #include "arch/x86/regs/misc.hh"
45 #include "arch/x86/regs/segment.hh"
46 #include "arch/x86/isa_traits.hh"
47 #include "arch/x86/process.hh"
48 #include "arch/x86/types.hh"
49 #include "base/loader/elf_object.hh"
50 #include "base/loader/object_file.hh"
51 #include "base/misc.hh"
52 #include "base/trace.hh"
53 #include "cpu/thread_context.hh"
54 #include "debug/Stack.hh"
55 #include "mem/page_table.hh"
56 #include "mem/translating_port.hh"
57 #include "sim/process_impl.hh"
58 #include "sim/syscall_emul.hh"
59 #include "sim/system.hh"
60
61 using namespace std;
62 using namespace X86ISA;
63
64 static const int ArgumentReg[] = {
65 INTREG_RDI,
66 INTREG_RSI,
67 INTREG_RDX,
68 //This argument register is r10 for syscalls and rcx for C.
69 INTREG_R10W,
70 //INTREG_RCX,
71 INTREG_R8W,
72 INTREG_R9W
73 };
74 static const int NumArgumentRegs = sizeof(ArgumentReg) / sizeof(const int);
75 static const int ArgumentReg32[] = {
76 INTREG_EBX,
77 INTREG_ECX,
78 INTREG_EDX,
79 INTREG_ESI,
80 INTREG_EDI,
81 };
82 static const int NumArgumentRegs32 = sizeof(ArgumentReg) / sizeof(const int);
83
84 X86LiveProcess::X86LiveProcess(LiveProcessParams * params, ObjectFile *objFile,
85 SyscallDesc *_syscallDescs, int _numSyscallDescs) :
86 LiveProcess(params, objFile), syscallDescs(_syscallDescs),
87 numSyscallDescs(_numSyscallDescs)
88 {
89 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize();
90 brk_point = roundUp(brk_point, VMPageSize);
91 }
92
93 X86_64LiveProcess::X86_64LiveProcess(LiveProcessParams *params,
94 ObjectFile *objFile, SyscallDesc *_syscallDescs,
95 int _numSyscallDescs) :
96 X86LiveProcess(params, objFile, _syscallDescs, _numSyscallDescs)
97 {
98
99 vsyscallPage.base = 0xffffffffff600000ULL;
100 vsyscallPage.size = VMPageSize;
101 vsyscallPage.vtimeOffset = 0x400;
102 vsyscallPage.vgettimeofdayOffset = 0x410;
103
104 // Set up stack. On X86_64 Linux, stack goes from the top of memory
105 // downward, less the hole for the kernel address space plus one page
106 // for undertermined purposes.
107 stack_base = (Addr)0x7FFFFFFFF000ULL;
108
109 // Set pointer for next thread stack. Reserve 8M for main stack.
110 next_thread_stack_base = stack_base - (8 * 1024 * 1024);
111
112 // Set up region for mmaps. This was determined empirically and may not
113 // always be correct.
114 mmap_start = mmap_end = (Addr)0x2aaaaaaab000ULL;
115 }
116
117 void
118 I386LiveProcess::syscall(int64_t callnum, ThreadContext *tc)
119 {
120 TheISA::PCState pc = tc->pcState();
121 Addr eip = pc.pc();
122 if (eip >= vsyscallPage.base &&
123 eip < vsyscallPage.base + vsyscallPage.size) {
124 pc.npc(vsyscallPage.base + vsyscallPage.vsysexitOffset);
125 tc->pcState(pc);
126 }
127 X86LiveProcess::syscall(callnum, tc);
128 }
129
130
131 I386LiveProcess::I386LiveProcess(LiveProcessParams *params,
132 ObjectFile *objFile, SyscallDesc *_syscallDescs,
133 int _numSyscallDescs) :
134 X86LiveProcess(params, objFile, _syscallDescs, _numSyscallDescs)
135 {
136 _gdtStart = ULL(0x100000000);
137 _gdtSize = VMPageSize;
138
139 vsyscallPage.base = 0xffffe000ULL;
140 vsyscallPage.size = VMPageSize;
141 vsyscallPage.vsyscallOffset = 0x400;
142 vsyscallPage.vsysexitOffset = 0x410;
143
144 stack_base = vsyscallPage.base;
145
146 // Set pointer for next thread stack. Reserve 8M for main stack.
147 next_thread_stack_base = stack_base - (8 * 1024 * 1024);
148
149 // Set up region for mmaps. This was determined empirically and may not
150 // always be correct.
151 mmap_start = mmap_end = (Addr)0xf7ffe000ULL;
152 }
153
154 SyscallDesc*
155 X86LiveProcess::getDesc(int callnum)
156 {
157 if (callnum < 0 || callnum >= numSyscallDescs)
158 return NULL;
159 return &syscallDescs[callnum];
160 }
161
162 void
163 X86_64LiveProcess::initState()
164 {
165 X86LiveProcess::initState();
166
167 argsInit(sizeof(uint64_t), VMPageSize);
168
169 // Set up the vsyscall page for this process.
170 allocateMem(vsyscallPage.base, vsyscallPage.size);
171 uint8_t vtimeBlob[] = {
172 0x48,0xc7,0xc0,0xc9,0x00,0x00,0x00, // mov $0xc9,%rax
173 0x0f,0x05, // syscall
174 0xc3 // retq
175 };
176 initVirtMem->writeBlob(vsyscallPage.base + vsyscallPage.vtimeOffset,
177 vtimeBlob, sizeof(vtimeBlob));
178
179 uint8_t vgettimeofdayBlob[] = {
180 0x48,0xc7,0xc0,0x60,0x00,0x00,0x00, // mov $0x60,%rax
181 0x0f,0x05, // syscall
182 0xc3 // retq
183 };
184 initVirtMem->writeBlob(vsyscallPage.base + vsyscallPage.vgettimeofdayOffset,
185 vgettimeofdayBlob, sizeof(vgettimeofdayBlob));
186
187 for (int i = 0; i < contextIds.size(); i++) {
188 ThreadContext * tc = system->getThreadContext(contextIds[i]);
189
190 SegAttr dataAttr = 0;
191 dataAttr.dpl = 3;
192 dataAttr.unusable = 0;
193 dataAttr.defaultSize = 1;
194 dataAttr.longMode = 1;
195 dataAttr.avl = 0;
196 dataAttr.granularity = 1;
197 dataAttr.present = 1;
198 dataAttr.type = 3;
199 dataAttr.writable = 1;
200 dataAttr.readable = 1;
201 dataAttr.expandDown = 0;
202 dataAttr.system = 1;
203
204 //Initialize the segment registers.
205 for(int seg = 0; seg < NUM_SEGMENTREGS; seg++) {
206 tc->setMiscRegNoEffect(MISCREG_SEG_BASE(seg), 0);
207 tc->setMiscRegNoEffect(MISCREG_SEG_EFF_BASE(seg), 0);
208 tc->setMiscRegNoEffect(MISCREG_SEG_ATTR(seg), dataAttr);
209 }
210
211 SegAttr csAttr = 0;
212 csAttr.dpl = 3;
213 csAttr.unusable = 0;
214 csAttr.defaultSize = 0;
215 csAttr.longMode = 1;
216 csAttr.avl = 0;
217 csAttr.granularity = 1;
218 csAttr.present = 1;
219 csAttr.type = 10;
220 csAttr.writable = 0;
221 csAttr.readable = 1;
222 csAttr.expandDown = 0;
223 csAttr.system = 1;
224
225 tc->setMiscRegNoEffect(MISCREG_CS_ATTR, csAttr);
226
227 Efer efer = 0;
228 efer.sce = 1; // Enable system call extensions.
229 efer.lme = 1; // Enable long mode.
230 efer.lma = 1; // Activate long mode.
231 efer.nxe = 1; // Enable nx support.
232 efer.svme = 0; // Disable svm support for now. It isn't implemented.
233 efer.ffxsr = 1; // Turn on fast fxsave and fxrstor.
234 tc->setMiscReg(MISCREG_EFER, efer);
235
236 //Set up the registers that describe the operating mode.
237 CR0 cr0 = 0;
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
243 cr0.ne = 1;
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);
252
253 tc->setMiscReg(MISCREG_MXCSR, 0x1f80);
254 }
255 }
256
257 void
258 I386LiveProcess::initState()
259 {
260 X86LiveProcess::initState();
261
262 argsInit(sizeof(uint32_t), VMPageSize);
263
264 /*
265 * Set up a GDT for this process. The whole GDT wouldn't really be for
266 * this process, but the only parts we care about are.
267 */
268 allocateMem(_gdtStart, _gdtSize);
269 uint64_t zero = 0;
270 assert(_gdtSize % sizeof(zero) == 0);
271 for (Addr gdtCurrent = _gdtStart;
272 gdtCurrent < _gdtStart + _gdtSize; gdtCurrent += sizeof(zero)) {
273 initVirtMem->write(gdtCurrent, zero);
274 }
275
276 // Set up the vsyscall page for this process.
277 allocateMem(vsyscallPage.base, vsyscallPage.size);
278 uint8_t vsyscallBlob[] = {
279 0x51, // push %ecx
280 0x52, // push %edp
281 0x55, // push %ebp
282 0x89, 0xe5, // mov %esp, %ebp
283 0x0f, 0x34 // sysenter
284 };
285 initVirtMem->writeBlob(vsyscallPage.base + vsyscallPage.vsyscallOffset,
286 vsyscallBlob, sizeof(vsyscallBlob));
287
288 uint8_t vsysexitBlob[] = {
289 0x5d, // pop %ebp
290 0x5a, // pop %edx
291 0x59, // pop %ecx
292 0xc3 // ret
293 };
294 initVirtMem->writeBlob(vsyscallPage.base + vsyscallPage.vsysexitOffset,
295 vsysexitBlob, sizeof(vsysexitBlob));
296
297 for (int i = 0; i < contextIds.size(); i++) {
298 ThreadContext * tc = system->getThreadContext(contextIds[i]);
299
300 SegAttr dataAttr = 0;
301 dataAttr.dpl = 3;
302 dataAttr.unusable = 0;
303 dataAttr.defaultSize = 1;
304 dataAttr.longMode = 0;
305 dataAttr.avl = 0;
306 dataAttr.granularity = 1;
307 dataAttr.present = 1;
308 dataAttr.type = 3;
309 dataAttr.writable = 1;
310 dataAttr.readable = 1;
311 dataAttr.expandDown = 0;
312 dataAttr.system = 1;
313
314 //Initialize the segment registers.
315 for(int seg = 0; seg < NUM_SEGMENTREGS; seg++) {
316 tc->setMiscRegNoEffect(MISCREG_SEG_BASE(seg), 0);
317 tc->setMiscRegNoEffect(MISCREG_SEG_EFF_BASE(seg), 0);
318 tc->setMiscRegNoEffect(MISCREG_SEG_ATTR(seg), dataAttr);
319 tc->setMiscRegNoEffect(MISCREG_SEG_SEL(seg), 0xB);
320 tc->setMiscRegNoEffect(MISCREG_SEG_LIMIT(seg), (uint32_t)(-1));
321 }
322
323 SegAttr csAttr = 0;
324 csAttr.dpl = 3;
325 csAttr.unusable = 0;
326 csAttr.defaultSize = 1;
327 csAttr.longMode = 0;
328 csAttr.avl = 0;
329 csAttr.granularity = 1;
330 csAttr.present = 1;
331 csAttr.type = 0xa;
332 csAttr.writable = 0;
333 csAttr.readable = 1;
334 csAttr.expandDown = 0;
335 csAttr.system = 1;
336
337 tc->setMiscRegNoEffect(MISCREG_CS_ATTR, csAttr);
338
339 tc->setMiscRegNoEffect(MISCREG_TSG_BASE, _gdtStart);
340 tc->setMiscRegNoEffect(MISCREG_TSG_EFF_BASE, _gdtStart);
341 tc->setMiscRegNoEffect(MISCREG_TSG_LIMIT, _gdtStart + _gdtSize - 1);
342
343 // Set the LDT selector to 0 to deactivate it.
344 tc->setMiscRegNoEffect(MISCREG_TSL, 0);
345
346 Efer efer = 0;
347 efer.sce = 1; // Enable system call extensions.
348 efer.lme = 1; // Enable long mode.
349 efer.lma = 0; // Deactivate long mode.
350 efer.nxe = 1; // Enable nx support.
351 efer.svme = 0; // Disable svm support for now. It isn't implemented.
352 efer.ffxsr = 1; // Turn on fast fxsave and fxrstor.
353 tc->setMiscReg(MISCREG_EFER, efer);
354
355 //Set up the registers that describe the operating mode.
356 CR0 cr0 = 0;
357 cr0.pg = 1; // Turn on paging.
358 cr0.cd = 0; // Don't disable caching.
359 cr0.nw = 0; // This is bit is defined to be ignored.
360 cr0.am = 0; // No alignment checking
361 cr0.wp = 0; // Supervisor mode can write read only pages
362 cr0.ne = 1;
363 cr0.et = 1; // This should always be 1
364 cr0.ts = 0; // We don't do task switching, so causing fp exceptions
365 // would be pointless.
366 cr0.em = 0; // Allow x87 instructions to execute natively.
367 cr0.mp = 1; // This doesn't really matter, but the manual suggests
368 // setting it to one.
369 cr0.pe = 1; // We're definitely in protected mode.
370 tc->setMiscReg(MISCREG_CR0, cr0);
371
372 tc->setMiscReg(MISCREG_MXCSR, 0x1f80);
373 }
374 }
375
376 template<class IntType>
377 void
378 X86LiveProcess::argsInit(int pageSize,
379 std::vector<AuxVector<IntType> > extraAuxvs)
380 {
381 int intSize = sizeof(IntType);
382
383 typedef AuxVector<IntType> auxv_t;
384 std::vector<auxv_t> auxv = extraAuxvs;
385
386 string filename;
387 if(argv.size() < 1)
388 filename = "";
389 else
390 filename = argv[0];
391
392 //We want 16 byte alignment
393 uint64_t align = 16;
394
395 // load object file into target memory
396 objFile->loadSections(initVirtMem);
397
398 enum X86CpuFeature {
399 X86_OnboardFPU = 1 << 0,
400 X86_VirtualModeExtensions = 1 << 1,
401 X86_DebuggingExtensions = 1 << 2,
402 X86_PageSizeExtensions = 1 << 3,
403
404 X86_TimeStampCounter = 1 << 4,
405 X86_ModelSpecificRegisters = 1 << 5,
406 X86_PhysicalAddressExtensions = 1 << 6,
407 X86_MachineCheckExtensions = 1 << 7,
408
409 X86_CMPXCHG8Instruction = 1 << 8,
410 X86_OnboardAPIC = 1 << 9,
411 X86_SYSENTER_SYSEXIT = 1 << 11,
412
413 X86_MemoryTypeRangeRegisters = 1 << 12,
414 X86_PageGlobalEnable = 1 << 13,
415 X86_MachineCheckArchitecture = 1 << 14,
416 X86_CMOVInstruction = 1 << 15,
417
418 X86_PageAttributeTable = 1 << 16,
419 X86_36BitPSEs = 1 << 17,
420 X86_ProcessorSerialNumber = 1 << 18,
421 X86_CLFLUSHInstruction = 1 << 19,
422
423 X86_DebugTraceStore = 1 << 21,
424 X86_ACPIViaMSR = 1 << 22,
425 X86_MultimediaExtensions = 1 << 23,
426
427 X86_FXSAVE_FXRSTOR = 1 << 24,
428 X86_StreamingSIMDExtensions = 1 << 25,
429 X86_StreamingSIMDExtensions2 = 1 << 26,
430 X86_CPUSelfSnoop = 1 << 27,
431
432 X86_HyperThreading = 1 << 28,
433 X86_AutomaticClockControl = 1 << 29,
434 X86_IA64Processor = 1 << 30
435 };
436
437 //Setup the auxilliary vectors. These will already have endian conversion.
438 //Auxilliary vectors are loaded only for elf formatted executables.
439 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
440 if(elfObject)
441 {
442 uint64_t features =
443 X86_OnboardFPU |
444 X86_VirtualModeExtensions |
445 X86_DebuggingExtensions |
446 X86_PageSizeExtensions |
447 X86_TimeStampCounter |
448 X86_ModelSpecificRegisters |
449 X86_PhysicalAddressExtensions |
450 X86_MachineCheckExtensions |
451 X86_CMPXCHG8Instruction |
452 X86_OnboardAPIC |
453 X86_SYSENTER_SYSEXIT |
454 X86_MemoryTypeRangeRegisters |
455 X86_PageGlobalEnable |
456 X86_MachineCheckArchitecture |
457 X86_CMOVInstruction |
458 X86_PageAttributeTable |
459 X86_36BitPSEs |
460 // X86_ProcessorSerialNumber |
461 X86_CLFLUSHInstruction |
462 // X86_DebugTraceStore |
463 // X86_ACPIViaMSR |
464 X86_MultimediaExtensions |
465 X86_FXSAVE_FXRSTOR |
466 X86_StreamingSIMDExtensions |
467 X86_StreamingSIMDExtensions2 |
468 // X86_CPUSelfSnoop |
469 // X86_HyperThreading |
470 // X86_AutomaticClockControl |
471 // X86_IA64Processor |
472 0;
473
474 //Bits which describe the system hardware capabilities
475 //XXX Figure out what these should be
476 auxv.push_back(auxv_t(M5_AT_HWCAP, features));
477 //The system page size
478 auxv.push_back(auxv_t(M5_AT_PAGESZ, X86ISA::VMPageSize));
479 //Frequency at which times() increments
480 //Defined to be 100 in the kernel source.
481 auxv.push_back(auxv_t(M5_AT_CLKTCK, 100));
482 // For statically linked executables, this is the virtual address of the
483 // program header tables if they appear in the executable image
484 auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable()));
485 // This is the size of a program header entry from the elf file.
486 auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize()));
487 // This is the number of program headers from the original elf file.
488 auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount()));
489 //This is the address of the elf "interpreter", It should be set
490 //to 0 for regular executables. It should be something else
491 //(not sure what) for dynamic libraries.
492 auxv.push_back(auxv_t(M5_AT_BASE, 0));
493
494 //XXX Figure out what this should be.
495 auxv.push_back(auxv_t(M5_AT_FLAGS, 0));
496 //The entry point to the program
497 auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint()));
498 //Different user and group IDs
499 auxv.push_back(auxv_t(M5_AT_UID, uid()));
500 auxv.push_back(auxv_t(M5_AT_EUID, euid()));
501 auxv.push_back(auxv_t(M5_AT_GID, gid()));
502 auxv.push_back(auxv_t(M5_AT_EGID, egid()));
503 //Whether to enable "secure mode" in the executable
504 auxv.push_back(auxv_t(M5_AT_SECURE, 0));
505 //The address of 16 "random" bytes.
506 auxv.push_back(auxv_t(M5_AT_RANDOM, 0));
507 //The name of the program
508 auxv.push_back(auxv_t(M5_AT_EXECFN, 0));
509 //The platform string
510 auxv.push_back(auxv_t(M5_AT_PLATFORM, 0));
511 }
512
513 //Figure out how big the initial stack needs to be
514
515 // A sentry NULL void pointer at the top of the stack.
516 int sentry_size = intSize;
517
518 //This is the name of the file which is present on the initial stack
519 //It's purpose is to let the user space linker examine the original file.
520 int file_name_size = filename.size() + 1;
521
522 const int numRandomBytes = 16;
523 int aux_data_size = numRandomBytes;
524
525 string platform = "x86_64";
526 aux_data_size += platform.size() + 1;
527
528 int env_data_size = 0;
529 for (int i = 0; i < envp.size(); ++i) {
530 env_data_size += envp[i].size() + 1;
531 }
532 int arg_data_size = 0;
533 for (int i = 0; i < argv.size(); ++i) {
534 arg_data_size += argv[i].size() + 1;
535 }
536
537 //The info_block needs to be padded so it's size is a multiple of the
538 //alignment mask. Also, it appears that there needs to be at least some
539 //padding, so if the size is already a multiple, we need to increase it
540 //anyway.
541 int base_info_block_size =
542 sentry_size + file_name_size + env_data_size + arg_data_size;
543
544 int info_block_size = roundUp(base_info_block_size, align);
545
546 int info_block_padding = info_block_size - base_info_block_size;
547
548 //Each auxilliary vector is two 8 byte words
549 int aux_array_size = intSize * 2 * (auxv.size() + 1);
550
551 int envp_array_size = intSize * (envp.size() + 1);
552 int argv_array_size = intSize * (argv.size() + 1);
553
554 int argc_size = intSize;
555
556 //Figure out the size of the contents of the actual initial frame
557 int frame_size =
558 aux_array_size +
559 envp_array_size +
560 argv_array_size +
561 argc_size;
562
563 //There needs to be padding after the auxiliary vector data so that the
564 //very bottom of the stack is aligned properly.
565 int partial_size = frame_size + aux_data_size;
566 int aligned_partial_size = roundUp(partial_size, align);
567 int aux_padding = aligned_partial_size - partial_size;
568
569 int space_needed =
570 info_block_size +
571 aux_data_size +
572 aux_padding +
573 frame_size;
574
575 stack_min = stack_base - space_needed;
576 stack_min = roundDown(stack_min, align);
577 stack_size = stack_base - stack_min;
578
579 // map memory
580 allocateMem(roundDown(stack_min, pageSize), roundUp(stack_size, pageSize));
581
582 // map out initial stack contents
583 IntType sentry_base = stack_base - sentry_size;
584 IntType file_name_base = sentry_base - file_name_size;
585 IntType env_data_base = file_name_base - env_data_size;
586 IntType arg_data_base = env_data_base - arg_data_size;
587 IntType aux_data_base = arg_data_base - info_block_padding - aux_data_size;
588 IntType auxv_array_base = aux_data_base - aux_array_size - aux_padding;
589 IntType envp_array_base = auxv_array_base - envp_array_size;
590 IntType argv_array_base = envp_array_base - argv_array_size;
591 IntType argc_base = argv_array_base - argc_size;
592
593 DPRINTF(Stack, "The addresses of items on the initial stack:\n");
594 DPRINTF(Stack, "0x%x - file name\n", file_name_base);
595 DPRINTF(Stack, "0x%x - env data\n", env_data_base);
596 DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
597 DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
598 DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
599 DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
600 DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
601 DPRINTF(Stack, "0x%x - argc \n", argc_base);
602 DPRINTF(Stack, "0x%x - stack min\n", stack_min);
603
604 // write contents to stack
605
606 // figure out argc
607 IntType argc = argv.size();
608 IntType guestArgc = X86ISA::htog(argc);
609
610 //Write out the sentry void *
611 IntType sentry_NULL = 0;
612 initVirtMem->writeBlob(sentry_base,
613 (uint8_t*)&sentry_NULL, sentry_size);
614
615 //Write the file name
616 initVirtMem->writeString(file_name_base, filename.c_str());
617
618 //Fix up the aux vectors which point to data
619 assert(auxv[auxv.size() - 3].a_type == M5_AT_RANDOM);
620 auxv[auxv.size() - 3].a_val = aux_data_base;
621 assert(auxv[auxv.size() - 2].a_type == M5_AT_EXECFN);
622 auxv[auxv.size() - 2].a_val = argv_array_base;
623 assert(auxv[auxv.size() - 1].a_type == M5_AT_PLATFORM);
624 auxv[auxv.size() - 1].a_val = aux_data_base + numRandomBytes;
625
626 //Copy the aux stuff
627 for(int x = 0; x < auxv.size(); x++)
628 {
629 initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize,
630 (uint8_t*)&(auxv[x].a_type), intSize);
631 initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize,
632 (uint8_t*)&(auxv[x].a_val), intSize);
633 }
634 //Write out the terminating zeroed auxilliary vector
635 const uint64_t zero = 0;
636 initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(),
637 (uint8_t*)&zero, 2 * intSize);
638
639 initVirtMem->writeString(aux_data_base, platform.c_str());
640
641 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
642 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
643
644 initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize);
645
646 ThreadContext *tc = system->getThreadContext(contextIds[0]);
647 //Set the stack pointer register
648 tc->setIntReg(StackPointerReg, stack_min);
649
650 // There doesn't need to be any segment base added in since we're dealing
651 // with the flat segmentation model.
652 tc->pcState(objFile->entryPoint());
653
654 //Align the "stack_min" to a page boundary.
655 stack_min = roundDown(stack_min, pageSize);
656
657 // num_processes++;
658 }
659
660 void
661 X86_64LiveProcess::argsInit(int intSize, int pageSize)
662 {
663 std::vector<AuxVector<uint64_t> > extraAuxvs;
664 extraAuxvs.push_back(AuxVector<uint64_t>(M5_AT_SYSINFO_EHDR,
665 vsyscallPage.base));
666 X86LiveProcess::argsInit<uint64_t>(pageSize, extraAuxvs);
667 }
668
669 void
670 I386LiveProcess::argsInit(int intSize, int pageSize)
671 {
672 std::vector<AuxVector<uint32_t> > extraAuxvs;
673 //Tell the binary where the vsyscall part of the vsyscall page is.
674 extraAuxvs.push_back(AuxVector<uint32_t>(M5_AT_SYSINFO,
675 vsyscallPage.base + vsyscallPage.vsyscallOffset));
676 extraAuxvs.push_back(AuxVector<uint32_t>(M5_AT_SYSINFO_EHDR,
677 vsyscallPage.base));
678 X86LiveProcess::argsInit<uint32_t>(pageSize, extraAuxvs);
679 }
680
681 void
682 X86LiveProcess::setSyscallReturn(ThreadContext *tc, SyscallReturn return_value)
683 {
684 tc->setIntReg(INTREG_RAX, return_value.value());
685 }
686
687 X86ISA::IntReg
688 X86_64LiveProcess::getSyscallArg(ThreadContext *tc, int &i)
689 {
690 assert(i < NumArgumentRegs);
691 return tc->readIntReg(ArgumentReg[i++]);
692 }
693
694 void
695 X86_64LiveProcess::setSyscallArg(ThreadContext *tc, int i, X86ISA::IntReg val)
696 {
697 assert(i < NumArgumentRegs);
698 return tc->setIntReg(ArgumentReg[i], val);
699 }
700
701 X86ISA::IntReg
702 I386LiveProcess::getSyscallArg(ThreadContext *tc, int &i)
703 {
704 assert(i < NumArgumentRegs32);
705 return tc->readIntReg(ArgumentReg32[i++]);
706 }
707
708 X86ISA::IntReg
709 I386LiveProcess::getSyscallArg(ThreadContext *tc, int &i, int width)
710 {
711 assert(width == 32 || width == 64);
712 assert(i < NumArgumentRegs);
713 uint64_t retVal = tc->readIntReg(ArgumentReg32[i++]) & mask(32);
714 if (width == 64)
715 retVal |= ((uint64_t)tc->readIntReg(ArgumentReg[i++]) << 32);
716 return retVal;
717 }
718
719 void
720 I386LiveProcess::setSyscallArg(ThreadContext *tc, int i, X86ISA::IntReg val)
721 {
722 assert(i < NumArgumentRegs);
723 return tc->setIntReg(ArgumentReg[i], val);
724 }