arch: Get rid of the now unused setSyscallArg.
[gem5.git] / src / arch / x86 / process.cc
1 /*
2 * Copyright (c) 2014 Advanced Micro Devices, Inc.
3 * Copyright (c) 2007 The Hewlett-Packard Development Company
4 * All rights reserved.
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Copyright (c) 2003-2006 The Regents of The University of Michigan
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Authors: Gabe Black
42 * Ali Saidi
43 */
44
45 #include "arch/x86/process.hh"
46
47 #include <string>
48 #include <vector>
49
50 #include "arch/x86/isa_traits.hh"
51 #include "arch/x86/regs/misc.hh"
52 #include "arch/x86/regs/segment.hh"
53 #include "arch/x86/system.hh"
54 #include "arch/x86/types.hh"
55 #include "base/loader/elf_object.hh"
56 #include "base/loader/object_file.hh"
57 #include "base/logging.hh"
58 #include "base/trace.hh"
59 #include "cpu/thread_context.hh"
60 #include "debug/Stack.hh"
61 #include "mem/multi_level_page_table.hh"
62 #include "mem/page_table.hh"
63 #include "params/Process.hh"
64 #include "sim/aux_vector.hh"
65 #include "sim/process_impl.hh"
66 #include "sim/syscall_desc.hh"
67 #include "sim/syscall_return.hh"
68 #include "sim/system.hh"
69
70 using namespace std;
71 using namespace X86ISA;
72
73 static const int ArgumentReg[] = {
74 INTREG_RDI,
75 INTREG_RSI,
76 INTREG_RDX,
77 // This argument register is r10 for syscalls and rcx for C.
78 INTREG_R10W,
79 // INTREG_RCX,
80 INTREG_R8W,
81 INTREG_R9W
82 };
83
84 static const int NumArgumentRegs M5_VAR_USED =
85 sizeof(ArgumentReg) / sizeof(const int);
86
87 static const int ArgumentReg32[] = {
88 INTREG_EBX,
89 INTREG_ECX,
90 INTREG_EDX,
91 INTREG_ESI,
92 INTREG_EDI,
93 INTREG_EBP
94 };
95
96 static const int NumArgumentRegs32 M5_VAR_USED =
97 sizeof(ArgumentReg) / sizeof(const int);
98
99 template class MultiLevelPageTable<LongModePTE<47, 39>,
100 LongModePTE<38, 30>,
101 LongModePTE<29, 21>,
102 LongModePTE<20, 12> >;
103 typedef MultiLevelPageTable<LongModePTE<47, 39>,
104 LongModePTE<38, 30>,
105 LongModePTE<29, 21>,
106 LongModePTE<20, 12> > ArchPageTable;
107
108 X86Process::X86Process(ProcessParams *params, ObjectFile *objFile,
109 SyscallDesc *_syscallDescs, int _numSyscallDescs)
110 : Process(params, params->useArchPT ?
111 static_cast<EmulationPageTable *>(
112 new ArchPageTable(params->name, params->pid,
113 params->system, PageBytes)) :
114 new EmulationPageTable(params->name, params->pid,
115 PageBytes),
116 objFile),
117 syscallDescs(_syscallDescs), numSyscallDescs(_numSyscallDescs)
118 {
119 }
120
121 void X86Process::clone(ThreadContext *old_tc, ThreadContext *new_tc,
122 Process *p, RegVal flags)
123 {
124 Process::clone(old_tc, new_tc, p, flags);
125 X86Process *process = (X86Process*)p;
126 *process = *this;
127 }
128
129 X86_64Process::X86_64Process(ProcessParams *params, ObjectFile *objFile,
130 SyscallDesc *_syscallDescs, int _numSyscallDescs)
131 : X86Process(params, objFile, _syscallDescs, _numSyscallDescs)
132 {
133
134 vsyscallPage.base = 0xffffffffff600000ULL;
135 vsyscallPage.size = PageBytes;
136 vsyscallPage.vtimeOffset = 0x400;
137 vsyscallPage.vgettimeofdayOffset = 0x0;
138
139 Addr brk_point = roundUp(image.maxAddr(), PageBytes);
140 Addr stack_base = 0x7FFFFFFFF000ULL;
141 Addr max_stack_size = 8 * 1024 * 1024;
142 Addr next_thread_stack_base = stack_base - max_stack_size;
143 Addr mmap_end = 0x7FFFF7FFF000ULL;
144
145 memState = make_shared<MemState>(brk_point, stack_base, max_stack_size,
146 next_thread_stack_base, mmap_end);
147 }
148
149 void
150 I386Process::syscall(int64_t callnum, ThreadContext *tc, Fault *fault)
151 {
152 PCState pc = tc->pcState();
153 Addr eip = pc.pc();
154 if (eip >= vsyscallPage.base &&
155 eip < vsyscallPage.base + vsyscallPage.size) {
156 pc.npc(vsyscallPage.base + vsyscallPage.vsysexitOffset);
157 tc->pcState(pc);
158 }
159 X86Process::syscall(callnum, tc, fault);
160 }
161
162
163 I386Process::I386Process(ProcessParams *params, ObjectFile *objFile,
164 SyscallDesc *_syscallDescs, int _numSyscallDescs)
165 : X86Process(params, objFile, _syscallDescs, _numSyscallDescs)
166 {
167 if (kvmInSE)
168 panic("KVM CPU model does not support 32 bit processes");
169
170 _gdtStart = ULL(0xffffd000);
171 _gdtSize = PageBytes;
172
173 vsyscallPage.base = 0xffffe000ULL;
174 vsyscallPage.size = PageBytes;
175 vsyscallPage.vsyscallOffset = 0x400;
176 vsyscallPage.vsysexitOffset = 0x410;
177
178 Addr brk_point = roundUp(image.maxAddr(), PageBytes);
179 Addr stack_base = _gdtStart;
180 Addr max_stack_size = 8 * 1024 * 1024;
181 Addr next_thread_stack_base = stack_base - max_stack_size;
182 Addr mmap_end = 0xB7FFF000ULL;
183
184 memState = make_shared<MemState>(brk_point, stack_base, max_stack_size,
185 next_thread_stack_base, mmap_end);
186 }
187
188 SyscallDesc*
189 X86Process::getDesc(int callnum)
190 {
191 if (callnum < 0 || callnum >= numSyscallDescs)
192 return NULL;
193 return &syscallDescs[callnum];
194 }
195
196 void
197 X86_64Process::initState()
198 {
199 X86Process::initState();
200
201 if (useForClone)
202 return;
203
204 argsInit(PageBytes);
205
206 // Set up the vsyscall page for this process.
207 allocateMem(vsyscallPage.base, vsyscallPage.size);
208 uint8_t vtimeBlob[] = {
209 0x48,0xc7,0xc0,0xc9,0x00,0x00,0x00, // mov $0xc9,%rax
210 0x0f,0x05, // syscall
211 0xc3 // retq
212 };
213 initVirtMem.writeBlob(vsyscallPage.base + vsyscallPage.vtimeOffset,
214 vtimeBlob, sizeof(vtimeBlob));
215
216 uint8_t vgettimeofdayBlob[] = {
217 0x48,0xc7,0xc0,0x60,0x00,0x00,0x00, // mov $0x60,%rax
218 0x0f,0x05, // syscall
219 0xc3 // retq
220 };
221 initVirtMem.writeBlob(vsyscallPage.base + vsyscallPage.vgettimeofdayOffset,
222 vgettimeofdayBlob, sizeof(vgettimeofdayBlob));
223
224 if (kvmInSE) {
225 PortProxy physProxy = system->physProxy;
226
227 Addr syscallCodePhysAddr = system->allocPhysPages(1);
228 Addr gdtPhysAddr = system->allocPhysPages(1);
229 Addr idtPhysAddr = system->allocPhysPages(1);
230 Addr istPhysAddr = system->allocPhysPages(1);
231 Addr tssPhysAddr = system->allocPhysPages(1);
232 Addr pfHandlerPhysAddr = system->allocPhysPages(1);
233
234 /*
235 * Set up the gdt.
236 */
237 uint8_t numGDTEntries = 0;
238 uint64_t nullDescriptor = 0;
239 physProxy.writeBlob(gdtPhysAddr + numGDTEntries * 8,
240 &nullDescriptor, 8);
241 numGDTEntries++;
242
243 SegDescriptor initDesc = 0;
244 initDesc.type.codeOrData = 0; // code or data type
245 initDesc.type.c = 0; // conforming
246 initDesc.type.r = 1; // readable
247 initDesc.dpl = 0; // privilege
248 initDesc.p = 1; // present
249 initDesc.l = 1; // longmode - 64 bit
250 initDesc.d = 0; // operand size
251 initDesc.s = 1; // system segment
252 initDesc.limit = 0xFFFFFFFF;
253 initDesc.base = 0;
254
255 //64 bit code segment
256 SegDescriptor csLowPLDesc = initDesc;
257 csLowPLDesc.type.codeOrData = 1;
258 csLowPLDesc.dpl = 0;
259 uint64_t csLowPLDescVal = csLowPLDesc;
260 physProxy.writeBlob(gdtPhysAddr + numGDTEntries * 8,
261 &csLowPLDescVal, 8);
262
263 numGDTEntries++;
264
265 SegSelector csLowPL = 0;
266 csLowPL.si = numGDTEntries - 1;
267 csLowPL.rpl = 0;
268
269 //64 bit data segment
270 SegDescriptor dsLowPLDesc = initDesc;
271 dsLowPLDesc.type.codeOrData = 0;
272 dsLowPLDesc.dpl = 0;
273 uint64_t dsLowPLDescVal = dsLowPLDesc;
274 physProxy.writeBlob(gdtPhysAddr + numGDTEntries * 8,
275 &dsLowPLDescVal, 8);
276
277 numGDTEntries++;
278
279 SegSelector dsLowPL = 0;
280 dsLowPL.si = numGDTEntries - 1;
281 dsLowPL.rpl = 0;
282
283 //64 bit data segment
284 SegDescriptor dsDesc = initDesc;
285 dsDesc.type.codeOrData = 0;
286 dsDesc.dpl = 3;
287 uint64_t dsDescVal = dsDesc;
288 physProxy.writeBlob(gdtPhysAddr + numGDTEntries * 8,
289 &dsDescVal, 8);
290
291 numGDTEntries++;
292
293 SegSelector ds = 0;
294 ds.si = numGDTEntries - 1;
295 ds.rpl = 3;
296
297 //64 bit code segment
298 SegDescriptor csDesc = initDesc;
299 csDesc.type.codeOrData = 1;
300 csDesc.dpl = 3;
301 uint64_t csDescVal = csDesc;
302 physProxy.writeBlob(gdtPhysAddr + numGDTEntries * 8,
303 &csDescVal, 8);
304
305 numGDTEntries++;
306
307 SegSelector cs = 0;
308 cs.si = numGDTEntries - 1;
309 cs.rpl = 3;
310
311 SegSelector scall = 0;
312 scall.si = csLowPL.si;
313 scall.rpl = 0;
314
315 SegSelector sret = 0;
316 sret.si = dsLowPL.si;
317 sret.rpl = 3;
318
319 /* In long mode the TSS has been extended to 16 Bytes */
320 TSSlow TSSDescLow = 0;
321 TSSDescLow.type = 0xB;
322 TSSDescLow.dpl = 0; // Privelege level 0
323 TSSDescLow.p = 1; // Present
324 TSSDescLow.limit = 0xFFFFFFFF;
325 TSSDescLow.base = bits(TSSVirtAddr, 31, 0);
326
327 TSShigh TSSDescHigh = 0;
328 TSSDescHigh.base = bits(TSSVirtAddr, 63, 32);
329
330 struct TSSDesc {
331 uint64_t low;
332 uint64_t high;
333 } tssDescVal = {TSSDescLow, TSSDescHigh};
334
335 physProxy.writeBlob(gdtPhysAddr + numGDTEntries * 8,
336 &tssDescVal, sizeof(tssDescVal));
337
338 numGDTEntries++;
339
340 SegSelector tssSel = 0;
341 tssSel.si = numGDTEntries - 1;
342
343 uint64_t tss_base_addr = (TSSDescHigh.base << 32) | TSSDescLow.base;
344 uint64_t tss_limit = TSSDescLow.limit;
345
346 SegAttr tss_attr = 0;
347
348 tss_attr.type = TSSDescLow.type;
349 tss_attr.dpl = TSSDescLow.dpl;
350 tss_attr.present = TSSDescLow.p;
351 tss_attr.granularity = TSSDescLow.g;
352 tss_attr.unusable = 0;
353
354 for (int i = 0; i < contextIds.size(); i++) {
355 ThreadContext * tc = system->getThreadContext(contextIds[i]);
356
357 tc->setMiscReg(MISCREG_CS, cs);
358 tc->setMiscReg(MISCREG_DS, ds);
359 tc->setMiscReg(MISCREG_ES, ds);
360 tc->setMiscReg(MISCREG_FS, ds);
361 tc->setMiscReg(MISCREG_GS, ds);
362 tc->setMiscReg(MISCREG_SS, ds);
363
364 // LDT
365 tc->setMiscReg(MISCREG_TSL, 0);
366 SegAttr tslAttr = 0;
367 tslAttr.present = 1;
368 tslAttr.type = 2;
369 tc->setMiscReg(MISCREG_TSL_ATTR, tslAttr);
370
371 tc->setMiscReg(MISCREG_TSG_BASE, GDTVirtAddr);
372 tc->setMiscReg(MISCREG_TSG_LIMIT, 8 * numGDTEntries - 1);
373
374 tc->setMiscReg(MISCREG_TR, tssSel);
375 tc->setMiscReg(MISCREG_TR_BASE, tss_base_addr);
376 tc->setMiscReg(MISCREG_TR_EFF_BASE, 0);
377 tc->setMiscReg(MISCREG_TR_LIMIT, tss_limit);
378 tc->setMiscReg(MISCREG_TR_ATTR, tss_attr);
379
380 //Start using longmode segments.
381 installSegDesc(tc, SEGMENT_REG_CS, csDesc, true);
382 installSegDesc(tc, SEGMENT_REG_DS, dsDesc, true);
383 installSegDesc(tc, SEGMENT_REG_ES, dsDesc, true);
384 installSegDesc(tc, SEGMENT_REG_FS, dsDesc, true);
385 installSegDesc(tc, SEGMENT_REG_GS, dsDesc, true);
386 installSegDesc(tc, SEGMENT_REG_SS, dsDesc, true);
387
388 Efer efer = 0;
389 efer.sce = 1; // Enable system call extensions.
390 efer.lme = 1; // Enable long mode.
391 efer.lma = 1; // Activate long mode.
392 efer.nxe = 0; // Enable nx support.
393 efer.svme = 1; // Enable svm support for now.
394 efer.ffxsr = 0; // Turn on fast fxsave and fxrstor.
395 tc->setMiscReg(MISCREG_EFER, efer);
396
397 //Set up the registers that describe the operating mode.
398 CR0 cr0 = 0;
399 cr0.pg = 1; // Turn on paging.
400 cr0.cd = 0; // Don't disable caching.
401 cr0.nw = 0; // This is bit is defined to be ignored.
402 cr0.am = 1; // No alignment checking
403 cr0.wp = 1; // Supervisor mode can write read only pages
404 cr0.ne = 1;
405 cr0.et = 1; // This should always be 1
406 cr0.ts = 0; // We don't do task switching, so causing fp exceptions
407 // would be pointless.
408 cr0.em = 0; // Allow x87 instructions to execute natively.
409 cr0.mp = 1; // This doesn't really matter, but the manual suggests
410 // setting it to one.
411 cr0.pe = 1; // We're definitely in protected mode.
412 tc->setMiscReg(MISCREG_CR0, cr0);
413
414 CR0 cr2 = 0;
415 tc->setMiscReg(MISCREG_CR2, cr2);
416
417 CR3 cr3 = dynamic_cast<ArchPageTable *>(pTable)->basePtr();
418 tc->setMiscReg(MISCREG_CR3, cr3);
419
420 CR4 cr4 = 0;
421 //Turn on pae.
422 cr4.osxsave = 1; // Enable XSAVE and Proc Extended States
423 cr4.osxmmexcpt = 1; // Operating System Unmasked Exception
424 cr4.osfxsr = 1; // Operating System FXSave/FSRSTOR Support
425 cr4.pce = 0; // Performance-Monitoring Counter Enable
426 cr4.pge = 0; // Page-Global Enable
427 cr4.mce = 0; // Machine Check Enable
428 cr4.pae = 1; // Physical-Address Extension
429 cr4.pse = 0; // Page Size Extensions
430 cr4.de = 0; // Debugging Extensions
431 cr4.tsd = 0; // Time Stamp Disable
432 cr4.pvi = 0; // Protected-Mode Virtual Interrupts
433 cr4.vme = 0; // Virtual-8086 Mode Extensions
434
435 tc->setMiscReg(MISCREG_CR4, cr4);
436
437 CR4 cr8 = 0;
438 tc->setMiscReg(MISCREG_CR8, cr8);
439
440 tc->setMiscReg(MISCREG_MXCSR, 0x1f80);
441
442 tc->setMiscReg(MISCREG_APIC_BASE, 0xfee00900);
443
444 tc->setMiscReg(MISCREG_TSG_BASE, GDTVirtAddr);
445 tc->setMiscReg(MISCREG_TSG_LIMIT, 0xffff);
446
447 tc->setMiscReg(MISCREG_IDTR_BASE, IDTVirtAddr);
448 tc->setMiscReg(MISCREG_IDTR_LIMIT, 0xffff);
449
450 /* enabling syscall and sysret */
451 RegVal star = ((RegVal)sret << 48) | ((RegVal)scall << 32);
452 tc->setMiscReg(MISCREG_STAR, star);
453 RegVal lstar = (RegVal)syscallCodeVirtAddr;
454 tc->setMiscReg(MISCREG_LSTAR, lstar);
455 RegVal sfmask = (1 << 8) | (1 << 10); // TF | DF
456 tc->setMiscReg(MISCREG_SF_MASK, sfmask);
457 }
458
459 /* Set up the content of the TSS and write it to physical memory. */
460
461 struct {
462 uint32_t reserved0; // +00h
463 uint32_t RSP0_low; // +04h
464 uint32_t RSP0_high; // +08h
465 uint32_t RSP1_low; // +0Ch
466 uint32_t RSP1_high; // +10h
467 uint32_t RSP2_low; // +14h
468 uint32_t RSP2_high; // +18h
469 uint32_t reserved1; // +1Ch
470 uint32_t reserved2; // +20h
471 uint32_t IST1_low; // +24h
472 uint32_t IST1_high; // +28h
473 uint32_t IST2_low; // +2Ch
474 uint32_t IST2_high; // +30h
475 uint32_t IST3_low; // +34h
476 uint32_t IST3_high; // +38h
477 uint32_t IST4_low; // +3Ch
478 uint32_t IST4_high; // +40h
479 uint32_t IST5_low; // +44h
480 uint32_t IST5_high; // +48h
481 uint32_t IST6_low; // +4Ch
482 uint32_t IST6_high; // +50h
483 uint32_t IST7_low; // +54h
484 uint32_t IST7_high; // +58h
485 uint32_t reserved3; // +5Ch
486 uint32_t reserved4; // +60h
487 uint16_t reserved5; // +64h
488 uint16_t IO_MapBase; // +66h
489 } tss;
490
491 /** setting Interrupt Stack Table */
492 uint64_t IST_start = ISTVirtAddr + PageBytes;
493 tss.IST1_low = IST_start;
494 tss.IST1_high = IST_start >> 32;
495 tss.RSP0_low = tss.IST1_low;
496 tss.RSP0_high = tss.IST1_high;
497 tss.RSP1_low = tss.IST1_low;
498 tss.RSP1_high = tss.IST1_high;
499 tss.RSP2_low = tss.IST1_low;
500 tss.RSP2_high = tss.IST1_high;
501 physProxy.writeBlob(tssPhysAddr, &tss, sizeof(tss));
502
503 /* Setting IDT gates */
504 GateDescriptorLow PFGateLow = 0;
505 PFGateLow.offsetHigh = bits(PFHandlerVirtAddr, 31, 16);
506 PFGateLow.offsetLow = bits(PFHandlerVirtAddr, 15, 0);
507 PFGateLow.selector = csLowPL;
508 PFGateLow.p = 1;
509 PFGateLow.dpl = 0;
510 PFGateLow.type = 0xe; // gate interrupt type
511 PFGateLow.IST = 0; // setting IST to 0 and using RSP0
512
513 GateDescriptorHigh PFGateHigh = 0;
514 PFGateHigh.offset = bits(PFHandlerVirtAddr, 63, 32);
515
516 struct {
517 uint64_t low;
518 uint64_t high;
519 } PFGate = {PFGateLow, PFGateHigh};
520
521 physProxy.writeBlob(idtPhysAddr + 0xE0, &PFGate, sizeof(PFGate));
522
523 /* System call handler */
524 uint8_t syscallBlob[] = {
525 // mov %rax, (0xffffc90000005600)
526 0x48, 0xa3, 0x00, 0x60, 0x00,
527 0x00, 0x00, 0xc9, 0xff, 0xff,
528 // sysret
529 0x48, 0x0f, 0x07
530 };
531
532 physProxy.writeBlob(syscallCodePhysAddr,
533 syscallBlob, sizeof(syscallBlob));
534
535 /** Page fault handler */
536 uint8_t faultBlob[] = {
537 // mov %rax, (0xffffc90000005700)
538 0x48, 0xa3, 0x00, 0x61, 0x00,
539 0x00, 0x00, 0xc9, 0xff, 0xff,
540 // add $0x8, %rsp # skip error
541 0x48, 0x83, 0xc4, 0x08,
542 // iretq
543 0x48, 0xcf
544 };
545
546 physProxy.writeBlob(pfHandlerPhysAddr, faultBlob, sizeof(faultBlob));
547
548 /* Syscall handler */
549 pTable->map(syscallCodeVirtAddr, syscallCodePhysAddr,
550 PageBytes, false);
551 /* GDT */
552 pTable->map(GDTVirtAddr, gdtPhysAddr, PageBytes, false);
553 /* IDT */
554 pTable->map(IDTVirtAddr, idtPhysAddr, PageBytes, false);
555 /* TSS */
556 pTable->map(TSSVirtAddr, tssPhysAddr, PageBytes, false);
557 /* IST */
558 pTable->map(ISTVirtAddr, istPhysAddr, PageBytes, false);
559 /* PF handler */
560 pTable->map(PFHandlerVirtAddr, pfHandlerPhysAddr, PageBytes, false);
561 /* MMIO region for m5ops */
562 pTable->map(MMIORegionVirtAddr, MMIORegionPhysAddr,
563 16 * PageBytes, false);
564 } else {
565 for (int i = 0; i < contextIds.size(); i++) {
566 ThreadContext * tc = system->getThreadContext(contextIds[i]);
567
568 SegAttr dataAttr = 0;
569 dataAttr.dpl = 3;
570 dataAttr.unusable = 0;
571 dataAttr.defaultSize = 1;
572 dataAttr.longMode = 1;
573 dataAttr.avl = 0;
574 dataAttr.granularity = 1;
575 dataAttr.present = 1;
576 dataAttr.type = 3;
577 dataAttr.writable = 1;
578 dataAttr.readable = 1;
579 dataAttr.expandDown = 0;
580 dataAttr.system = 1;
581
582 // Initialize the segment registers.
583 for (int seg = 0; seg < NUM_SEGMENTREGS; seg++) {
584 tc->setMiscRegNoEffect(MISCREG_SEG_BASE(seg), 0);
585 tc->setMiscRegNoEffect(MISCREG_SEG_EFF_BASE(seg), 0);
586 tc->setMiscRegNoEffect(MISCREG_SEG_ATTR(seg), dataAttr);
587 }
588
589 SegAttr csAttr = 0;
590 csAttr.dpl = 3;
591 csAttr.unusable = 0;
592 csAttr.defaultSize = 0;
593 csAttr.longMode = 1;
594 csAttr.avl = 0;
595 csAttr.granularity = 1;
596 csAttr.present = 1;
597 csAttr.type = 10;
598 csAttr.writable = 0;
599 csAttr.readable = 1;
600 csAttr.expandDown = 0;
601 csAttr.system = 1;
602
603 tc->setMiscRegNoEffect(MISCREG_CS_ATTR, csAttr);
604
605 Efer efer = 0;
606 efer.sce = 1; // Enable system call extensions.
607 efer.lme = 1; // Enable long mode.
608 efer.lma = 1; // Activate long mode.
609 efer.nxe = 1; // Enable nx support.
610 efer.svme = 0; // Disable svm support for now. It isn't implemented.
611 efer.ffxsr = 1; // Turn on fast fxsave and fxrstor.
612 tc->setMiscReg(MISCREG_EFER, efer);
613
614 // Set up the registers that describe the operating mode.
615 CR0 cr0 = 0;
616 cr0.pg = 1; // Turn on paging.
617 cr0.cd = 0; // Don't disable caching.
618 cr0.nw = 0; // This is bit is defined to be ignored.
619 cr0.am = 0; // No alignment checking
620 cr0.wp = 0; // Supervisor mode can write read only pages
621 cr0.ne = 1;
622 cr0.et = 1; // This should always be 1
623 cr0.ts = 0; // We don't do task switching, so causing fp exceptions
624 // would be pointless.
625 cr0.em = 0; // Allow x87 instructions to execute natively.
626 cr0.mp = 1; // This doesn't really matter, but the manual suggests
627 // setting it to one.
628 cr0.pe = 1; // We're definitely in protected mode.
629 tc->setMiscReg(MISCREG_CR0, cr0);
630
631 tc->setMiscReg(MISCREG_MXCSR, 0x1f80);
632 }
633 }
634 }
635
636 void
637 I386Process::initState()
638 {
639 X86Process::initState();
640
641 argsInit(PageBytes);
642
643 /*
644 * Set up a GDT for this process. The whole GDT wouldn't really be for
645 * this process, but the only parts we care about are.
646 */
647 allocateMem(_gdtStart, _gdtSize);
648 uint64_t zero = 0;
649 assert(_gdtSize % sizeof(zero) == 0);
650 for (Addr gdtCurrent = _gdtStart;
651 gdtCurrent < _gdtStart + _gdtSize; gdtCurrent += sizeof(zero)) {
652 initVirtMem.write(gdtCurrent, zero);
653 }
654
655 // Set up the vsyscall page for this process.
656 allocateMem(vsyscallPage.base, vsyscallPage.size);
657 uint8_t vsyscallBlob[] = {
658 0x51, // push %ecx
659 0x52, // push %edp
660 0x55, // push %ebp
661 0x89, 0xe5, // mov %esp, %ebp
662 0x0f, 0x34 // sysenter
663 };
664 initVirtMem.writeBlob(vsyscallPage.base + vsyscallPage.vsyscallOffset,
665 vsyscallBlob, sizeof(vsyscallBlob));
666
667 uint8_t vsysexitBlob[] = {
668 0x5d, // pop %ebp
669 0x5a, // pop %edx
670 0x59, // pop %ecx
671 0xc3 // ret
672 };
673 initVirtMem.writeBlob(vsyscallPage.base + vsyscallPage.vsysexitOffset,
674 vsysexitBlob, sizeof(vsysexitBlob));
675
676 for (int i = 0; i < contextIds.size(); i++) {
677 ThreadContext * tc = system->getThreadContext(contextIds[i]);
678
679 SegAttr dataAttr = 0;
680 dataAttr.dpl = 3;
681 dataAttr.unusable = 0;
682 dataAttr.defaultSize = 1;
683 dataAttr.longMode = 0;
684 dataAttr.avl = 0;
685 dataAttr.granularity = 1;
686 dataAttr.present = 1;
687 dataAttr.type = 3;
688 dataAttr.writable = 1;
689 dataAttr.readable = 1;
690 dataAttr.expandDown = 0;
691 dataAttr.system = 1;
692
693 // Initialize the segment registers.
694 for (int seg = 0; seg < NUM_SEGMENTREGS; seg++) {
695 tc->setMiscRegNoEffect(MISCREG_SEG_BASE(seg), 0);
696 tc->setMiscRegNoEffect(MISCREG_SEG_EFF_BASE(seg), 0);
697 tc->setMiscRegNoEffect(MISCREG_SEG_ATTR(seg), dataAttr);
698 tc->setMiscRegNoEffect(MISCREG_SEG_SEL(seg), 0xB);
699 tc->setMiscRegNoEffect(MISCREG_SEG_LIMIT(seg), (uint32_t)(-1));
700 }
701
702 SegAttr csAttr = 0;
703 csAttr.dpl = 3;
704 csAttr.unusable = 0;
705 csAttr.defaultSize = 1;
706 csAttr.longMode = 0;
707 csAttr.avl = 0;
708 csAttr.granularity = 1;
709 csAttr.present = 1;
710 csAttr.type = 0xa;
711 csAttr.writable = 0;
712 csAttr.readable = 1;
713 csAttr.expandDown = 0;
714 csAttr.system = 1;
715
716 tc->setMiscRegNoEffect(MISCREG_CS_ATTR, csAttr);
717
718 tc->setMiscRegNoEffect(MISCREG_TSG_BASE, _gdtStart);
719 tc->setMiscRegNoEffect(MISCREG_TSG_EFF_BASE, _gdtStart);
720 tc->setMiscRegNoEffect(MISCREG_TSG_LIMIT, _gdtStart + _gdtSize - 1);
721
722 // Set the LDT selector to 0 to deactivate it.
723 tc->setMiscRegNoEffect(MISCREG_TSL, 0);
724
725 Efer efer = 0;
726 efer.sce = 1; // Enable system call extensions.
727 efer.lme = 1; // Enable long mode.
728 efer.lma = 0; // Deactivate long mode.
729 efer.nxe = 1; // Enable nx support.
730 efer.svme = 0; // Disable svm support for now. It isn't implemented.
731 efer.ffxsr = 1; // Turn on fast fxsave and fxrstor.
732 tc->setMiscReg(MISCREG_EFER, efer);
733
734 // Set up the registers that describe the operating mode.
735 CR0 cr0 = 0;
736 cr0.pg = 1; // Turn on paging.
737 cr0.cd = 0; // Don't disable caching.
738 cr0.nw = 0; // This is bit is defined to be ignored.
739 cr0.am = 0; // No alignment checking
740 cr0.wp = 0; // Supervisor mode can write read only pages
741 cr0.ne = 1;
742 cr0.et = 1; // This should always be 1
743 cr0.ts = 0; // We don't do task switching, so causing fp exceptions
744 // would be pointless.
745 cr0.em = 0; // Allow x87 instructions to execute natively.
746 cr0.mp = 1; // This doesn't really matter, but the manual suggests
747 // setting it to one.
748 cr0.pe = 1; // We're definitely in protected mode.
749 tc->setMiscReg(MISCREG_CR0, cr0);
750
751 tc->setMiscReg(MISCREG_MXCSR, 0x1f80);
752 }
753 }
754
755 template<class IntType>
756 void
757 X86Process::argsInit(int pageSize,
758 std::vector<AuxVector<IntType> > extraAuxvs)
759 {
760 int intSize = sizeof(IntType);
761
762 std::vector<AuxVector<IntType>> auxv = extraAuxvs;
763
764 string filename;
765 if (argv.size() < 1)
766 filename = "";
767 else
768 filename = argv[0];
769
770 // We want 16 byte alignment
771 uint64_t align = 16;
772
773 enum X86CpuFeature {
774 X86_OnboardFPU = 1 << 0,
775 X86_VirtualModeExtensions = 1 << 1,
776 X86_DebuggingExtensions = 1 << 2,
777 X86_PageSizeExtensions = 1 << 3,
778
779 X86_TimeStampCounter = 1 << 4,
780 X86_ModelSpecificRegisters = 1 << 5,
781 X86_PhysicalAddressExtensions = 1 << 6,
782 X86_MachineCheckExtensions = 1 << 7,
783
784 X86_CMPXCHG8Instruction = 1 << 8,
785 X86_OnboardAPIC = 1 << 9,
786 X86_SYSENTER_SYSEXIT = 1 << 11,
787
788 X86_MemoryTypeRangeRegisters = 1 << 12,
789 X86_PageGlobalEnable = 1 << 13,
790 X86_MachineCheckArchitecture = 1 << 14,
791 X86_CMOVInstruction = 1 << 15,
792
793 X86_PageAttributeTable = 1 << 16,
794 X86_36BitPSEs = 1 << 17,
795 X86_ProcessorSerialNumber = 1 << 18,
796 X86_CLFLUSHInstruction = 1 << 19,
797
798 X86_DebugTraceStore = 1 << 21,
799 X86_ACPIViaMSR = 1 << 22,
800 X86_MultimediaExtensions = 1 << 23,
801
802 X86_FXSAVE_FXRSTOR = 1 << 24,
803 X86_StreamingSIMDExtensions = 1 << 25,
804 X86_StreamingSIMDExtensions2 = 1 << 26,
805 X86_CPUSelfSnoop = 1 << 27,
806
807 X86_HyperThreading = 1 << 28,
808 X86_AutomaticClockControl = 1 << 29,
809 X86_IA64Processor = 1 << 30
810 };
811
812 // Setup the auxiliary vectors. These will already have endian
813 // conversion. Auxiliary vectors are loaded only for elf formatted
814 // executables; the auxv is responsible for passing information from
815 // the OS to the interpreter.
816 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile);
817 if (elfObject) {
818 uint64_t features =
819 X86_OnboardFPU |
820 X86_VirtualModeExtensions |
821 X86_DebuggingExtensions |
822 X86_PageSizeExtensions |
823 X86_TimeStampCounter |
824 X86_ModelSpecificRegisters |
825 X86_PhysicalAddressExtensions |
826 X86_MachineCheckExtensions |
827 X86_CMPXCHG8Instruction |
828 X86_OnboardAPIC |
829 X86_SYSENTER_SYSEXIT |
830 X86_MemoryTypeRangeRegisters |
831 X86_PageGlobalEnable |
832 X86_MachineCheckArchitecture |
833 X86_CMOVInstruction |
834 X86_PageAttributeTable |
835 X86_36BitPSEs |
836 // X86_ProcessorSerialNumber |
837 X86_CLFLUSHInstruction |
838 // X86_DebugTraceStore |
839 // X86_ACPIViaMSR |
840 X86_MultimediaExtensions |
841 X86_FXSAVE_FXRSTOR |
842 X86_StreamingSIMDExtensions |
843 X86_StreamingSIMDExtensions2 |
844 // X86_CPUSelfSnoop |
845 // X86_HyperThreading |
846 // X86_AutomaticClockControl |
847 // X86_IA64Processor |
848 0;
849
850 // Bits which describe the system hardware capabilities
851 // XXX Figure out what these should be
852 auxv.emplace_back(M5_AT_HWCAP, features);
853 // The system page size
854 auxv.emplace_back(M5_AT_PAGESZ, X86ISA::PageBytes);
855 // Frequency at which times() increments
856 // Defined to be 100 in the kernel source.
857 auxv.emplace_back(M5_AT_CLKTCK, 100);
858 // This is the virtual address of the program header tables if they
859 // appear in the executable image.
860 auxv.emplace_back(M5_AT_PHDR, elfObject->programHeaderTable());
861 // This is the size of a program header entry from the elf file.
862 auxv.emplace_back(M5_AT_PHENT, elfObject->programHeaderSize());
863 // This is the number of program headers from the original elf file.
864 auxv.emplace_back(M5_AT_PHNUM, elfObject->programHeaderCount());
865 // This is the base address of the ELF interpreter; it should be
866 // zero for static executables or contain the base address for
867 // dynamic executables.
868 auxv.emplace_back(M5_AT_BASE, getBias());
869 // XXX Figure out what this should be.
870 auxv.emplace_back(M5_AT_FLAGS, 0);
871 // The entry point to the program
872 auxv.emplace_back(M5_AT_ENTRY, objFile->entryPoint());
873 // Different user and group IDs
874 auxv.emplace_back(M5_AT_UID, uid());
875 auxv.emplace_back(M5_AT_EUID, euid());
876 auxv.emplace_back(M5_AT_GID, gid());
877 auxv.emplace_back(M5_AT_EGID, egid());
878 // Whether to enable "secure mode" in the executable
879 auxv.emplace_back(M5_AT_SECURE, 0);
880 // The address of 16 "random" bytes.
881 auxv.emplace_back(M5_AT_RANDOM, 0);
882 // The name of the program
883 auxv.emplace_back(M5_AT_EXECFN, 0);
884 // The platform string
885 auxv.emplace_back(M5_AT_PLATFORM, 0);
886 }
887
888 // Figure out how big the initial stack needs to be
889
890 // A sentry NULL void pointer at the top of the stack.
891 int sentry_size = intSize;
892
893 // This is the name of the file which is present on the initial stack
894 // It's purpose is to let the user space linker examine the original file.
895 int file_name_size = filename.size() + 1;
896
897 const int numRandomBytes = 16;
898 int aux_data_size = numRandomBytes;
899
900 string platform = "x86_64";
901 aux_data_size += platform.size() + 1;
902
903 int env_data_size = 0;
904 for (int i = 0; i < envp.size(); ++i)
905 env_data_size += envp[i].size() + 1;
906 int arg_data_size = 0;
907 for (int i = 0; i < argv.size(); ++i)
908 arg_data_size += argv[i].size() + 1;
909
910 // The info_block needs to be padded so its size is a multiple of the
911 // alignment mask. Also, it appears that there needs to be at least some
912 // padding, so if the size is already a multiple, we need to increase it
913 // anyway.
914 int base_info_block_size =
915 sentry_size + file_name_size + env_data_size + arg_data_size;
916
917 int info_block_size = roundUp(base_info_block_size, align);
918
919 int info_block_padding = info_block_size - base_info_block_size;
920
921 // Each auxiliary vector is two 8 byte words
922 int aux_array_size = intSize * 2 * (auxv.size() + 1);
923
924 int envp_array_size = intSize * (envp.size() + 1);
925 int argv_array_size = intSize * (argv.size() + 1);
926
927 int argc_size = intSize;
928
929 // Figure out the size of the contents of the actual initial frame
930 int frame_size =
931 aux_array_size +
932 envp_array_size +
933 argv_array_size +
934 argc_size;
935
936 // There needs to be padding after the auxiliary vector data so that the
937 // very bottom of the stack is aligned properly.
938 int partial_size = frame_size + aux_data_size;
939 int aligned_partial_size = roundUp(partial_size, align);
940 int aux_padding = aligned_partial_size - partial_size;
941
942 int space_needed =
943 info_block_size +
944 aux_data_size +
945 aux_padding +
946 frame_size;
947
948 Addr stack_base = memState->getStackBase();
949
950 Addr stack_min = stack_base - space_needed;
951 stack_min = roundDown(stack_min, align);
952
953 unsigned stack_size = stack_base - stack_min;
954 stack_size = roundUp(stack_size, pageSize);
955 memState->setStackSize(stack_size);
956
957 // map memory
958 Addr stack_end = roundDown(stack_base - stack_size, pageSize);
959
960 DPRINTF(Stack, "Mapping the stack: 0x%x %dB\n", stack_end, stack_size);
961 allocateMem(stack_end, stack_size);
962
963 // map out initial stack contents
964 IntType sentry_base = stack_base - sentry_size;
965 IntType file_name_base = sentry_base - file_name_size;
966 IntType env_data_base = file_name_base - env_data_size;
967 IntType arg_data_base = env_data_base - arg_data_size;
968 IntType aux_data_base = arg_data_base - info_block_padding - aux_data_size;
969 IntType auxv_array_base = aux_data_base - aux_array_size - aux_padding;
970 IntType envp_array_base = auxv_array_base - envp_array_size;
971 IntType argv_array_base = envp_array_base - argv_array_size;
972 IntType argc_base = argv_array_base - argc_size;
973
974 DPRINTF(Stack, "The addresses of items on the initial stack:\n");
975 DPRINTF(Stack, "0x%x - file name\n", file_name_base);
976 DPRINTF(Stack, "0x%x - env data\n", env_data_base);
977 DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
978 DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
979 DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
980 DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
981 DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
982 DPRINTF(Stack, "0x%x - argc \n", argc_base);
983 DPRINTF(Stack, "0x%x - stack min\n", stack_min);
984
985 // write contents to stack
986
987 // figure out argc
988 IntType argc = argv.size();
989 IntType guestArgc = htole(argc);
990
991 // Write out the sentry void *
992 IntType sentry_NULL = 0;
993 initVirtMem.writeBlob(sentry_base, &sentry_NULL, sentry_size);
994
995 // Write the file name
996 initVirtMem.writeString(file_name_base, filename.c_str());
997
998 // Fix up the aux vectors which point to data
999 assert(auxv[auxv.size() - 3].type == M5_AT_RANDOM);
1000 auxv[auxv.size() - 3].val = aux_data_base;
1001 assert(auxv[auxv.size() - 2].type == M5_AT_EXECFN);
1002 auxv[auxv.size() - 2].val = argv_array_base;
1003 assert(auxv[auxv.size() - 1].type == M5_AT_PLATFORM);
1004 auxv[auxv.size() - 1].val = aux_data_base + numRandomBytes;
1005
1006
1007 // Copy the aux stuff
1008 Addr auxv_array_end = auxv_array_base;
1009 for (const auto &aux: auxv) {
1010 initVirtMem.write(auxv_array_end, aux, GuestByteOrder);
1011 auxv_array_end += sizeof(aux);
1012 }
1013 // Write out the terminating zeroed auxiliary vector
1014 const AuxVector<uint64_t> zero(0, 0);
1015 initVirtMem.write(auxv_array_end, zero);
1016 auxv_array_end += sizeof(zero);
1017
1018 initVirtMem.writeString(aux_data_base, platform.c_str());
1019
1020 copyStringArray(envp, envp_array_base, env_data_base,
1021 LittleEndianByteOrder, initVirtMem);
1022 copyStringArray(argv, argv_array_base, arg_data_base,
1023 LittleEndianByteOrder, initVirtMem);
1024
1025 initVirtMem.writeBlob(argc_base, &guestArgc, intSize);
1026
1027 ThreadContext *tc = system->getThreadContext(contextIds[0]);
1028 // Set the stack pointer register
1029 tc->setIntReg(StackPointerReg, stack_min);
1030
1031 // There doesn't need to be any segment base added in since we're dealing
1032 // with the flat segmentation model.
1033 tc->pcState(getStartPC());
1034
1035 // Align the "stack_min" to a page boundary.
1036 memState->setStackMin(roundDown(stack_min, pageSize));
1037 }
1038
1039 void
1040 X86_64Process::argsInit(int pageSize)
1041 {
1042 std::vector<AuxVector<uint64_t> > extraAuxvs;
1043 extraAuxvs.emplace_back(M5_AT_SYSINFO_EHDR, vsyscallPage.base);
1044 X86Process::argsInit<uint64_t>(pageSize, extraAuxvs);
1045 }
1046
1047 void
1048 I386Process::argsInit(int pageSize)
1049 {
1050 std::vector<AuxVector<uint32_t> > extraAuxvs;
1051 //Tell the binary where the vsyscall part of the vsyscall page is.
1052 extraAuxvs.emplace_back(M5_AT_SYSINFO,
1053 vsyscallPage.base + vsyscallPage.vsyscallOffset);
1054 extraAuxvs.emplace_back(M5_AT_SYSINFO_EHDR, vsyscallPage.base);
1055 X86Process::argsInit<uint32_t>(pageSize, extraAuxvs);
1056 }
1057
1058 void
1059 X86Process::setSyscallReturn(ThreadContext *tc, SyscallReturn retval)
1060 {
1061 tc->setIntReg(INTREG_RAX, retval.encodedValue());
1062 }
1063
1064 RegVal
1065 X86_64Process::getSyscallArg(ThreadContext *tc, int &i)
1066 {
1067 assert(i < NumArgumentRegs);
1068 return tc->readIntReg(ArgumentReg[i++]);
1069 }
1070
1071 void
1072 X86_64Process::clone(ThreadContext *old_tc, ThreadContext *new_tc,
1073 Process *p, RegVal flags)
1074 {
1075 X86Process::clone(old_tc, new_tc, p, flags);
1076 ((X86_64Process*)p)->vsyscallPage = vsyscallPage;
1077 }
1078
1079 RegVal
1080 I386Process::getSyscallArg(ThreadContext *tc, int &i)
1081 {
1082 assert(i < NumArgumentRegs32);
1083 return tc->readIntReg(ArgumentReg32[i++]);
1084 }
1085
1086 RegVal
1087 I386Process::getSyscallArg(ThreadContext *tc, int &i, int width)
1088 {
1089 assert(width == 32 || width == 64);
1090 assert(i < NumArgumentRegs);
1091 uint64_t retVal = tc->readIntReg(ArgumentReg32[i++]) & mask(32);
1092 if (width == 64)
1093 retVal |= ((uint64_t)tc->readIntReg(ArgumentReg[i++]) << 32);
1094 return retVal;
1095 }
1096
1097 void
1098 I386Process::clone(ThreadContext *old_tc, ThreadContext *new_tc,
1099 Process *p, RegVal flags)
1100 {
1101 X86Process::clone(old_tc, new_tc, p, flags);
1102 ((I386Process*)p)->vsyscallPage = vsyscallPage;
1103 }