gem5  v22.1.0.0
process.cc
Go to the documentation of this file.
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 
42 #include "arch/x86/process.hh"
43 
44 #include <string>
45 #include <vector>
46 
47 #include "arch/x86/fs_workload.hh"
48 #include "arch/x86/page_size.hh"
49 #include "arch/x86/regs/int.hh"
50 #include "arch/x86/regs/misc.hh"
51 #include "arch/x86/regs/segment.hh"
52 #include "arch/x86/se_workload.hh"
53 #include "arch/x86/types.hh"
56 #include "base/logging.hh"
57 #include "base/trace.hh"
58 #include "cpu/thread_context.hh"
59 #include "debug/Stack.hh"
61 #include "mem/page_table.hh"
62 #include "params/Process.hh"
63 #include "sim/aux_vector.hh"
64 #include "sim/byteswap.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 namespace gem5
71 {
72 
73 using namespace X86ISA;
74 
75 template class MultiLevelPageTable<LongModePTE<47, 39>,
76  LongModePTE<38, 30>,
77  LongModePTE<29, 21>,
78  LongModePTE<20, 12> >;
79 typedef MultiLevelPageTable<LongModePTE<47, 39>,
80  LongModePTE<38, 30>,
81  LongModePTE<29, 21>,
83 
84 X86Process::X86Process(const ProcessParams &params,
85  loader::ObjectFile *objFile) :
86  Process(params, params.useArchPT ?
87  static_cast<EmulationPageTable *>(
88  new ArchPageTable(params.name, params.pid,
89  params.system, PageBytes)) :
90  new EmulationPageTable(params.name, params.pid,
91  PageBytes),
92  objFile)
93 {
94 }
95 
98 {
99  Process::clone(old_tc, new_tc, p, flags);
100  X86Process *process = (X86Process*)p;
101  *process = *this;
102 }
103 
104 X86_64Process::X86_64Process(const ProcessParams &params,
105  loader::ObjectFile *objFile) :
106  X86Process(params, objFile)
107 {
108  vsyscallPage.base = 0xffffffffff600000ULL;
110  vsyscallPage.vtimeOffset = 0x400;
112 
113  Addr brk_point = roundUp(image.maxAddr(), PageBytes);
114  Addr stack_base = 0x7FFFFFFFF000ULL;
115  Addr max_stack_size = 8 * 1024 * 1024;
116  Addr next_thread_stack_base = stack_base - max_stack_size;
117  Addr mmap_end = 0x7FFFF7FFF000ULL;
118 
119  memState = std::make_shared<MemState>(
120  this, brk_point, stack_base, max_stack_size,
121  next_thread_stack_base, mmap_end);
122 }
123 
124 
125 I386Process::I386Process(const ProcessParams &params,
126  loader::ObjectFile *objFile) :
127  X86Process(params, objFile)
128 {
129  if (kvmInSE)
130  panic("KVM CPU model does not support 32 bit processes");
131 
132  _gdtStart = 0xffffd000ULL;
134 
135  vsyscallPage.base = 0xffffe000ULL;
139 
140  Addr brk_point = roundUp(image.maxAddr(), PageBytes);
141  Addr stack_base = _gdtStart;
142  Addr max_stack_size = 8 * 1024 * 1024;
143  Addr next_thread_stack_base = stack_base - max_stack_size;
144  Addr mmap_end = 0xB7FFF000ULL;
145 
146  memState = std::make_shared<MemState>(
147  this, brk_point, stack_base, max_stack_size,
148  next_thread_stack_base, mmap_end);
149 }
150 
151 void
153 {
155 
156  if (useForClone)
157  return;
158 
160 
161  // Set up the vsyscall page for this process.
162  memState->mapRegion(vsyscallPage.base, vsyscallPage.size, "vsyscall");
163  uint8_t vtimeBlob[] = {
164  0x48,0xc7,0xc0,0xc9,0x00,0x00,0x00, // mov $0xc9,%rax
165  0x0f,0x05, // syscall
166  0xc3 // retq
167  };
169  vtimeBlob, sizeof(vtimeBlob));
170 
171  uint8_t vgettimeofdayBlob[] = {
172  0x48,0xc7,0xc0,0x60,0x00,0x00,0x00, // mov $0x60,%rax
173  0x0f,0x05, // syscall
174  0xc3 // retq
175  };
176  initVirtMem->writeBlob(
178  vgettimeofdayBlob, sizeof(vgettimeofdayBlob));
179 
180  if (kvmInSE) {
181  PortProxy physProxy = system->physProxy;
182 
183  Addr syscallCodePhysAddr = seWorkload->allocPhysPages(1);
184  Addr gdtPhysAddr = seWorkload->allocPhysPages(1);
185  Addr idtPhysAddr = seWorkload->allocPhysPages(1);
186  Addr istPhysAddr = seWorkload->allocPhysPages(1);
187  Addr tssPhysAddr = seWorkload->allocPhysPages(1);
188  Addr pfHandlerPhysAddr = seWorkload->allocPhysPages(1);
189 
190  /*
191  * Set up the gdt.
192  */
193  uint8_t numGDTEntries = 0;
194  uint64_t nullDescriptor = 0;
195  physProxy.writeBlob(gdtPhysAddr + numGDTEntries * 8,
196  &nullDescriptor, 8);
197  numGDTEntries++;
198 
199  SegDescriptor initDesc = 0;
200  initDesc.type.codeOrData = 0; // code or data type
201  initDesc.type.c = 0; // conforming
202  initDesc.type.r = 1; // readable
203  initDesc.dpl = 0; // privilege
204  initDesc.p = 1; // present
205  initDesc.l = 1; // longmode - 64 bit
206  initDesc.d = 0; // operand size
207  initDesc.g = 1;
208  initDesc.s = 1; // system segment
209  initDesc.limit = 0xFFFFFFFF;
210  initDesc.base = 0;
211 
212  //64 bit code segment
213  SegDescriptor csLowPLDesc = initDesc;
214  csLowPLDesc.type.codeOrData = 1;
215  csLowPLDesc.dpl = 0;
216  uint64_t csLowPLDescVal = csLowPLDesc;
217  physProxy.writeBlob(gdtPhysAddr + numGDTEntries * 8,
218  &csLowPLDescVal, 8);
219 
220  numGDTEntries++;
221 
222  SegSelector csLowPL = 0;
223  csLowPL.si = numGDTEntries - 1;
224  csLowPL.rpl = 0;
225 
226  //64 bit data segment
227  SegDescriptor dsLowPLDesc = initDesc;
228  dsLowPLDesc.type.codeOrData = 0;
229  dsLowPLDesc.dpl = 0;
230  uint64_t dsLowPLDescVal = dsLowPLDesc;
231  physProxy.writeBlob(gdtPhysAddr + numGDTEntries * 8,
232  &dsLowPLDescVal, 8);
233 
234  numGDTEntries++;
235 
236  SegSelector dsLowPL = 0;
237  dsLowPL.si = numGDTEntries - 1;
238  dsLowPL.rpl = 0;
239 
240  //64 bit data segment
241  SegDescriptor dsDesc = initDesc;
242  dsDesc.type.codeOrData = 0;
243  dsDesc.dpl = 3;
244  uint64_t dsDescVal = dsDesc;
245  physProxy.writeBlob(gdtPhysAddr + numGDTEntries * 8,
246  &dsDescVal, 8);
247 
248  numGDTEntries++;
249 
250  SegSelector ds = 0;
251  ds.si = numGDTEntries - 1;
252  ds.rpl = 3;
253 
254  //64 bit code segment
255  SegDescriptor csDesc = initDesc;
256  csDesc.type.codeOrData = 1;
257  csDesc.dpl = 3;
258  uint64_t csDescVal = csDesc;
259  physProxy.writeBlob(gdtPhysAddr + numGDTEntries * 8,
260  &csDescVal, 8);
261 
262  numGDTEntries++;
263 
264  SegSelector cs = 0;
265  cs.si = numGDTEntries - 1;
266  cs.rpl = 3;
267 
268  SegSelector scall = 0;
269  scall.si = csLowPL.si;
270  scall.rpl = 0;
271 
272  SegSelector sret = 0;
273  sret.si = dsLowPL.si;
274  sret.rpl = 3;
275 
276  /* In long mode the TSS has been extended to 16 Bytes */
277  TSSlow TSSDescLow = 0;
278  TSSDescLow.type = 0xB;
279  TSSDescLow.dpl = 0; // Privelege level 0
280  TSSDescLow.p = 1; // Present
281  TSSDescLow.limit = 0xFFFFFFFF;
282  TSSDescLow.base = bits(TSSVirtAddr, 31, 0);
283 
284  TSShigh TSSDescHigh = 0;
285  TSSDescHigh.base = bits(TSSVirtAddr, 63, 32);
286 
287  struct TSSDesc
288  {
289  uint64_t low;
290  uint64_t high;
291  } tssDescVal = {TSSDescLow, TSSDescHigh};
292 
293  physProxy.writeBlob(gdtPhysAddr + numGDTEntries * 8,
294  &tssDescVal, sizeof(tssDescVal));
295 
296  numGDTEntries++;
297 
298  SegSelector tssSel = 0;
299  tssSel.si = numGDTEntries - 1;
300 
301  uint64_t tss_base_addr = (TSSDescHigh.base << 32) | TSSDescLow.base;
302  uint64_t tss_limit = TSSDescLow.limit;
303 
304  SegAttr tss_attr = 0;
305 
306  tss_attr.type = TSSDescLow.type;
307  tss_attr.dpl = TSSDescLow.dpl;
308  tss_attr.present = TSSDescLow.p;
309  tss_attr.granularity = TSSDescLow.g;
310  tss_attr.unusable = 0;
311 
312  for (int i = 0; i < contextIds.size(); i++) {
314 
315  tc->setMiscReg(misc_reg::Cs, cs);
316  tc->setMiscReg(misc_reg::Ds, ds);
317  tc->setMiscReg(misc_reg::Es, ds);
318  tc->setMiscReg(misc_reg::Fs, ds);
319  tc->setMiscReg(misc_reg::Gs, ds);
320  tc->setMiscReg(misc_reg::Ss, ds);
321 
322  // LDT
323  tc->setMiscReg(misc_reg::Tsl, 0);
324  SegAttr tslAttr = 0;
325  tslAttr.unusable = 1;
326  tslAttr.present = 0;
327  tslAttr.type = 2;
328  tc->setMiscReg(misc_reg::TslAttr, tslAttr);
329 
331  tc->setMiscReg(misc_reg::TsgLimit, 8 * numGDTEntries - 1);
332 
333  tc->setMiscReg(misc_reg::Tr, tssSel);
334  tc->setMiscReg(misc_reg::TrBase, tss_base_addr);
335  tc->setMiscReg(misc_reg::TrEffBase, tss_base_addr);
336  tc->setMiscReg(misc_reg::TrLimit, tss_limit);
337  tc->setMiscReg(misc_reg::TrAttr, tss_attr);
338 
339  //Start using longmode segments.
340  installSegDesc(tc, segment_idx::Cs, csDesc, true);
341  installSegDesc(tc, segment_idx::Ds, dsDesc, true);
342  installSegDesc(tc, segment_idx::Es, dsDesc, true);
343  installSegDesc(tc, segment_idx::Fs, dsDesc, true);
344  installSegDesc(tc, segment_idx::Gs, dsDesc, true);
345  installSegDesc(tc, segment_idx::Ss, dsDesc, true);
346 
347  Efer efer = 0;
348  efer.sce = 1; // Enable system call extensions.
349  efer.lme = 1; // Enable long mode.
350  efer.lma = 1; // Activate long mode.
351  efer.nxe = 1; // Enable nx support.
352  efer.svme = 0; // Disable svm support for now.
353  efer.ffxsr = 0; // Disable fast fxsave and fxrstor.
354  tc->setMiscReg(misc_reg::Efer, efer);
355 
356  //Set up the registers that describe the operating mode.
357  CR0 cr0 = 0;
358  cr0.pg = 1; // Turn on paging.
359  cr0.cd = 0; // Don't disable caching.
360  cr0.nw = 0; // This is bit is defined to be ignored.
361  cr0.am = 0; // No alignment checking
362  cr0.wp = 0; // Supervisor mode can write read only pages
363  cr0.ne = 1;
364  cr0.et = 1; // This should always be 1
365  cr0.ts = 0; // We don't do task switching, so causing fp exceptions
366  // would be pointless.
367  cr0.em = 0; // Allow x87 instructions to execute natively.
368  cr0.mp = 1; // This doesn't really matter, but the manual suggests
369  // setting it to one.
370  cr0.pe = 1; // We're definitely in protected mode.
371  tc->setMiscReg(misc_reg::Cr0, cr0);
372 
373  CR0 cr2 = 0;
374  tc->setMiscReg(misc_reg::Cr2, cr2);
375 
376  CR3 cr3 = dynamic_cast<ArchPageTable *>(pTable)->basePtr();
377  tc->setMiscReg(misc_reg::Cr3, cr3);
378 
379  CR4 cr4 = 0;
380  //Turn on pae.
381  cr4.osxsave = 0; // Disable XSAVE and Proc Extended States
382  cr4.osxmmexcpt = 0; // Operating System Unmasked Exception
383  cr4.osfxsr = 1; // Operating System FXSave/FSRSTOR Support
384  cr4.pce = 0; // Performance-Monitoring Counter Enable
385  cr4.pge = 0; // Page-Global Enable
386  cr4.mce = 0; // Machine Check Enable
387  cr4.pae = 1; // Physical-Address Extension
388  cr4.pse = 0; // Page Size Extensions
389  cr4.de = 0; // Debugging Extensions
390  cr4.tsd = 0; // Time Stamp Disable
391  cr4.pvi = 0; // Protected-Mode Virtual Interrupts
392  cr4.vme = 0; // Virtual-8086 Mode Extensions
393 
394  tc->setMiscReg(misc_reg::Cr4, cr4);
395 
396  CR8 cr8 = 0;
397  tc->setMiscReg(misc_reg::Cr8, cr8);
398 
399  tc->setMiscReg(misc_reg::Mxcsr, 0x1f80);
400 
401  tc->setMiscReg(misc_reg::ApicBase, 0xfee00900);
402 
404  tc->setMiscReg(misc_reg::TsgLimit, 0xffff);
405 
407  tc->setMiscReg(misc_reg::IdtrLimit, 0xffff);
408 
409  /* enabling syscall and sysret */
410  RegVal star = ((RegVal)sret << 48) | ((RegVal)scall << 32);
411  tc->setMiscReg(misc_reg::Star, star);
413  tc->setMiscReg(misc_reg::Lstar, lstar);
414  RegVal sfmask = (1 << 8) | (1 << 10); // TF | DF
415  tc->setMiscReg(misc_reg::SfMask, sfmask);
416  }
417 
418  /* Set up the content of the TSS and write it to physical memory. */
419 
420  struct
421  {
422  uint32_t reserved0; // +00h
423  uint32_t RSP0_low; // +04h
424  uint32_t RSP0_high; // +08h
425  uint32_t RSP1_low; // +0Ch
426  uint32_t RSP1_high; // +10h
427  uint32_t RSP2_low; // +14h
428  uint32_t RSP2_high; // +18h
429  uint32_t reserved1; // +1Ch
430  uint32_t reserved2; // +20h
431  uint32_t IST1_low; // +24h
432  uint32_t IST1_high; // +28h
433  uint32_t IST2_low; // +2Ch
434  uint32_t IST2_high; // +30h
435  uint32_t IST3_low; // +34h
436  uint32_t IST3_high; // +38h
437  uint32_t IST4_low; // +3Ch
438  uint32_t IST4_high; // +40h
439  uint32_t IST5_low; // +44h
440  uint32_t IST5_high; // +48h
441  uint32_t IST6_low; // +4Ch
442  uint32_t IST6_high; // +50h
443  uint32_t IST7_low; // +54h
444  uint32_t IST7_high; // +58h
445  uint32_t reserved3; // +5Ch
446  uint32_t reserved4; // +60h
447  uint16_t reserved5; // +64h
448  uint16_t IO_MapBase; // +66h
449  } tss;
450 
452  uint64_t IST_start = ISTVirtAddr + PageBytes;
453  tss.IST1_low = IST_start;
454  tss.IST1_high = IST_start >> 32;
455  tss.RSP0_low = tss.IST1_low;
456  tss.RSP0_high = tss.IST1_high;
457  tss.RSP1_low = tss.IST1_low;
458  tss.RSP1_high = tss.IST1_high;
459  tss.RSP2_low = tss.IST1_low;
460  tss.RSP2_high = tss.IST1_high;
461  physProxy.writeBlob(tssPhysAddr, &tss, sizeof(tss));
462 
463  /* Setting IDT gates */
464  GateDescriptorLow PFGateLow = 0;
465  PFGateLow.offsetHigh = bits(PFHandlerVirtAddr, 31, 16);
466  PFGateLow.offsetLow = bits(PFHandlerVirtAddr, 15, 0);
467  PFGateLow.selector = csLowPL;
468  PFGateLow.p = 1;
469  PFGateLow.dpl = 0;
470  PFGateLow.type = 0xe; // gate interrupt type
471  PFGateLow.IST = 0; // setting IST to 0 and using RSP0
472 
473  GateDescriptorHigh PFGateHigh = 0;
474  PFGateHigh.offset = bits(PFHandlerVirtAddr, 63, 32);
475 
476  struct
477  {
478  uint64_t low;
479  uint64_t high;
480  } PFGate = {PFGateLow, PFGateHigh};
481 
482  physProxy.writeBlob(idtPhysAddr + 0xE0, &PFGate, sizeof(PFGate));
483 
484  /* System call handler */
485  uint8_t syscallBlob[] = {
486  // mov %rax, (0xffffc90000007000)
487  0x48, 0xa3, 0x00, 0x70, 0x00,
488  0x00, 0x00, 0xc9, 0xff, 0xff,
489  // sysret
490  0x48, 0x0f, 0x07
491  };
492 
493  physProxy.writeBlob(syscallCodePhysAddr,
494  syscallBlob, sizeof(syscallBlob));
495 
497  uint8_t faultBlob[] = {
498  // mov %rax, (0xffffc90000007000)
499  0x48, 0xa3, 0x00, 0x70, 0x00,
500  0x00, 0x00, 0xc9, 0xff, 0xff,
501  // add $0x8, %rsp # skip error
502  0x48, 0x83, 0xc4, 0x08,
503  // iretq
504  0x48, 0xcf
505  };
506 
507  physProxy.writeBlob(pfHandlerPhysAddr, faultBlob, sizeof(faultBlob));
508 
509  /* Syscall handler */
510  pTable->map(syscallCodeVirtAddr, syscallCodePhysAddr,
511  PageBytes, false);
512  /* GDT */
513  pTable->map(GDTVirtAddr, gdtPhysAddr, PageBytes, false);
514  /* IDT */
515  pTable->map(IDTVirtAddr, idtPhysAddr, PageBytes, false);
516  /* TSS */
517  pTable->map(TSSVirtAddr, tssPhysAddr, PageBytes, false);
518  /* IST */
519  pTable->map(ISTVirtAddr, istPhysAddr, PageBytes, false);
520  /* PF handler */
521  pTable->map(PFHandlerVirtAddr, pfHandlerPhysAddr, PageBytes, false);
522  /* MMIO region for m5ops */
523  auto m5op_range = system->m5opRange();
524  if (m5op_range.size()) {
525  pTable->map(MMIORegionVirtAddr, m5op_range.start(),
526  m5op_range.size(), false);
527  }
528  } else {
529  for (int i = 0; i < contextIds.size(); i++) {
531 
532  SegAttr dataAttr = 0;
533  dataAttr.dpl = 3;
534  dataAttr.unusable = 0;
535  dataAttr.defaultSize = 1;
536  dataAttr.longMode = 1;
537  dataAttr.avl = 0;
538  dataAttr.granularity = 1;
539  dataAttr.present = 1;
540  dataAttr.type = 3;
541  dataAttr.writable = 1;
542  dataAttr.readable = 1;
543  dataAttr.expandDown = 0;
544  dataAttr.system = 1;
545 
546  // Initialize the segment registers.
547  for (int seg = 0; seg < segment_idx::NumIdxs; seg++) {
550  tc->setMiscRegNoEffect(misc_reg::segAttr(seg), dataAttr);
551  }
552 
553  SegAttr csAttr = 0;
554  csAttr.dpl = 3;
555  csAttr.unusable = 0;
556  csAttr.defaultSize = 0;
557  csAttr.longMode = 1;
558  csAttr.avl = 0;
559  csAttr.granularity = 1;
560  csAttr.present = 1;
561  csAttr.type = 10;
562  csAttr.writable = 0;
563  csAttr.readable = 1;
564  csAttr.expandDown = 0;
565  csAttr.system = 1;
566 
568 
569  Efer efer = 0;
570  efer.sce = 1; // Enable system call extensions.
571  efer.lme = 1; // Enable long mode.
572  efer.lma = 1; // Activate long mode.
573  efer.nxe = 1; // Enable nx support.
574  efer.svme = 0; // Disable svm support for now. It isn't implemented.
575  efer.ffxsr = 1; // Turn on fast fxsave and fxrstor.
576  tc->setMiscReg(misc_reg::Efer, efer);
577 
578  // Set up the registers that describe the operating mode.
579  CR0 cr0 = 0;
580  cr0.pg = 1; // Turn on paging.
581  cr0.cd = 0; // Don't disable caching.
582  cr0.nw = 0; // This is bit is defined to be ignored.
583  cr0.am = 0; // No alignment checking
584  cr0.wp = 0; // Supervisor mode can write read only pages
585  cr0.ne = 1;
586  cr0.et = 1; // This should always be 1
587  cr0.ts = 0; // We don't do task switching, so causing fp exceptions
588  // would be pointless.
589  cr0.em = 0; // Allow x87 instructions to execute natively.
590  cr0.mp = 1; // This doesn't really matter, but the manual suggests
591  // setting it to one.
592  cr0.pe = 1; // We're definitely in protected mode.
593  tc->setMiscReg(misc_reg::Cr0, cr0);
594 
595  tc->setMiscReg(misc_reg::Mxcsr, 0x1f80);
596 
597  // Setting CR3 to the process pid so that concatinated
598  // page addr with lower 12 bits of CR3 can be used in SE
599  // mode as well to avoid conflicts between tlb entries with
600  // same virtual addresses belonging to different processes
602 
603  // Setting pcide bit in CR4
604  CR4 cr4 = tc->readMiscRegNoEffect(misc_reg::Cr4);
605  cr4.pcide = 1;
606  tc->setMiscReg(misc_reg::Cr4, cr4);
607  }
608  }
609 }
610 
611 void
613 {
615 
617 
618  /*
619  * Set up a GDT for this process. The whole GDT wouldn't really be for
620  * this process, but the only parts we care about are.
621  */
623  uint64_t zero = 0;
624  assert(_gdtSize % sizeof(zero) == 0);
625  for (Addr gdtCurrent = _gdtStart;
626  gdtCurrent < _gdtStart + _gdtSize; gdtCurrent += sizeof(zero)) {
627  initVirtMem->write(gdtCurrent, zero);
628  }
629 
630  // Set up the vsyscall page for this process.
631  memState->mapRegion(vsyscallPage.base, vsyscallPage.size, "vsyscall");
632  uint8_t vsyscallBlob[] = {
633  0x51, // push %ecx
634  0x52, // push %edp
635  0x55, // push %ebp
636  0x89, 0xe5, // mov %esp, %ebp
637  0x0f, 0x34 // sysenter
638  };
640  vsyscallBlob, sizeof(vsyscallBlob));
641 
642  uint8_t vsysexitBlob[] = {
643  0x5d, // pop %ebp
644  0x5a, // pop %edx
645  0x59, // pop %ecx
646  0xc3 // ret
647  };
649  vsysexitBlob, sizeof(vsysexitBlob));
650 
651  for (int i = 0; i < contextIds.size(); i++) {
653 
654  SegAttr dataAttr = 0;
655  dataAttr.dpl = 3;
656  dataAttr.unusable = 0;
657  dataAttr.defaultSize = 1;
658  dataAttr.longMode = 0;
659  dataAttr.avl = 0;
660  dataAttr.granularity = 1;
661  dataAttr.present = 1;
662  dataAttr.type = 3;
663  dataAttr.writable = 1;
664  dataAttr.readable = 1;
665  dataAttr.expandDown = 0;
666  dataAttr.system = 1;
667 
668  // Initialize the segment registers.
669  for (int seg = 0; seg < segment_idx::NumIdxs; seg++) {
672  tc->setMiscRegNoEffect(misc_reg::segAttr(seg), dataAttr);
674  tc->setMiscRegNoEffect(misc_reg::segLimit(seg), (uint32_t)(-1));
675  }
676 
677  SegAttr csAttr = 0;
678  csAttr.dpl = 3;
679  csAttr.unusable = 0;
680  csAttr.defaultSize = 1;
681  csAttr.longMode = 0;
682  csAttr.avl = 0;
683  csAttr.granularity = 1;
684  csAttr.present = 1;
685  csAttr.type = 0xa;
686  csAttr.writable = 0;
687  csAttr.readable = 1;
688  csAttr.expandDown = 0;
689  csAttr.system = 1;
690 
692 
696 
697  // Set the LDT selector to 0 to deactivate it.
699  SegAttr attr = 0;
700  attr.unusable = 1;
702 
703  Efer efer = 0;
704  efer.sce = 1; // Enable system call extensions.
705  efer.lme = 1; // Enable long mode.
706  efer.lma = 0; // Deactivate long mode.
707  efer.nxe = 1; // Enable nx support.
708  efer.svme = 0; // Disable svm support for now. It isn't implemented.
709  efer.ffxsr = 1; // Turn on fast fxsave and fxrstor.
710  tc->setMiscReg(misc_reg::Efer, efer);
711 
712  // Set up the registers that describe the operating mode.
713  CR0 cr0 = 0;
714  cr0.pg = 1; // Turn on paging.
715  cr0.cd = 0; // Don't disable caching.
716  cr0.nw = 0; // This is bit is defined to be ignored.
717  cr0.am = 0; // No alignment checking
718  cr0.wp = 0; // Supervisor mode can write read only pages
719  cr0.ne = 1;
720  cr0.et = 1; // This should always be 1
721  cr0.ts = 0; // We don't do task switching, so causing fp exceptions
722  // would be pointless.
723  cr0.em = 0; // Allow x87 instructions to execute natively.
724  cr0.mp = 1; // This doesn't really matter, but the manual suggests
725  // setting it to one.
726  cr0.pe = 1; // We're definitely in protected mode.
727  tc->setMiscReg(misc_reg::Cr0, cr0);
728 
729  tc->setMiscReg(misc_reg::Mxcsr, 0x1f80);
730  }
731 }
732 
733 template<class IntType>
734 void
735 X86Process::argsInit(int pageSize,
737 {
738  int intSize = sizeof(IntType);
739 
741 
742  std::string filename;
743  if (argv.size() < 1)
744  filename = "";
745  else
746  filename = argv[0];
747 
748  // We want 16 byte alignment
749  uint64_t align = 16;
750 
751  enum X86CpuFeature
752  {
753  X86_OnboardFPU = 1 << 0,
754  X86_VirtualModeExtensions = 1 << 1,
755  X86_DebuggingExtensions = 1 << 2,
756  X86_PageSizeExtensions = 1 << 3,
757 
758  X86_TimeStampCounter = 1 << 4,
759  X86_ModelSpecificRegisters = 1 << 5,
760  X86_PhysicalAddressExtensions = 1 << 6,
761  X86_MachineCheckExtensions = 1 << 7,
762 
763  X86_CMPXCHG8Instruction = 1 << 8,
764  X86_OnboardAPIC = 1 << 9,
765  X86_SYSENTER_SYSEXIT = 1 << 11,
766 
767  X86_MemoryTypeRangeRegisters = 1 << 12,
768  X86_PageGlobalEnable = 1 << 13,
769  X86_MachineCheckArchitecture = 1 << 14,
770  X86_CMOVInstruction = 1 << 15,
771 
772  X86_PageAttributeTable = 1 << 16,
773  X86_36BitPSEs = 1 << 17,
774  X86_ProcessorSerialNumber = 1 << 18,
775  X86_CLFLUSHInstruction = 1 << 19,
776 
777  X86_DebugTraceStore = 1 << 21,
778  X86_ACPIViaMSR = 1 << 22,
779  X86_MultimediaExtensions = 1 << 23,
780 
781  X86_FXSAVE_FXRSTOR = 1 << 24,
782  X86_StreamingSIMDExtensions = 1 << 25,
783  X86_StreamingSIMDExtensions2 = 1 << 26,
784  X86_CPUSelfSnoop = 1 << 27,
785 
786  X86_HyperThreading = 1 << 28,
787  X86_AutomaticClockControl = 1 << 29,
788  X86_IA64Processor = 1 << 30
789  };
790 
791  // Setup the auxiliary vectors. These will already have endian
792  // conversion. Auxiliary vectors are loaded only for elf formatted
793  // executables; the auxv is responsible for passing information from
794  // the OS to the interpreter.
795  auto *elfObject = dynamic_cast<loader::ElfObject *>(objFile);
796  if (elfObject) {
797  uint64_t features =
798  X86_OnboardFPU |
799  X86_VirtualModeExtensions |
800  X86_DebuggingExtensions |
801  X86_PageSizeExtensions |
802  X86_TimeStampCounter |
803  X86_ModelSpecificRegisters |
804  X86_PhysicalAddressExtensions |
805  X86_MachineCheckExtensions |
806  X86_CMPXCHG8Instruction |
807  X86_OnboardAPIC |
808  X86_SYSENTER_SYSEXIT |
809  X86_MemoryTypeRangeRegisters |
810  X86_PageGlobalEnable |
811  X86_MachineCheckArchitecture |
812  X86_CMOVInstruction |
813  X86_PageAttributeTable |
814  X86_36BitPSEs |
815 // X86_ProcessorSerialNumber |
816  X86_CLFLUSHInstruction |
817 // X86_DebugTraceStore |
818 // X86_ACPIViaMSR |
819  X86_MultimediaExtensions |
820  X86_FXSAVE_FXRSTOR |
821  X86_StreamingSIMDExtensions |
822  X86_StreamingSIMDExtensions2 |
823 // X86_CPUSelfSnoop |
824 // X86_HyperThreading |
825 // X86_AutomaticClockControl |
826 // X86_IA64Processor |
827  0;
828 
829  // Bits which describe the system hardware capabilities
830  // XXX Figure out what these should be
831  auxv.emplace_back(gem5::auxv::Hwcap, features);
832  // The system page size
833  auxv.emplace_back(gem5::auxv::Pagesz, X86ISA::PageBytes);
834  // Frequency at which times() increments
835  // Defined to be 100 in the kernel source.
836  auxv.emplace_back(gem5::auxv::Clktck, 100);
837  // This is the virtual address of the program header tables if they
838  // appear in the executable image.
839  auxv.emplace_back(gem5::auxv::Phdr, elfObject->programHeaderTable());
840  // This is the size of a program header entry from the elf file.
841  auxv.emplace_back(gem5::auxv::Phent, elfObject->programHeaderSize());
842  // This is the number of program headers from the original elf file.
843  auxv.emplace_back(gem5::auxv::Phnum, elfObject->programHeaderCount());
844  // This is the base address of the ELF interpreter; it should be
845  // zero for static executables or contain the base address for
846  // dynamic executables.
847  auxv.emplace_back(gem5::auxv::Base, getBias());
848  // XXX Figure out what this should be.
849  auxv.emplace_back(gem5::auxv::Flags, 0);
850  // The entry point to the program
851  auxv.emplace_back(gem5::auxv::Entry, objFile->entryPoint());
852  // Different user and group IDs
853  auxv.emplace_back(gem5::auxv::Uid, uid());
854  auxv.emplace_back(gem5::auxv::Euid, euid());
855  auxv.emplace_back(gem5::auxv::Gid, gid());
856  auxv.emplace_back(gem5::auxv::Egid, egid());
857  // Whether to enable "secure mode" in the executable
858  auxv.emplace_back(gem5::auxv::Secure, 0);
859  // The address of 16 "random" bytes.
860  auxv.emplace_back(gem5::auxv::Random, 0);
861  // The name of the program
862  auxv.emplace_back(gem5::auxv::Execfn, 0);
863  // The platform string
864  auxv.emplace_back(gem5::auxv::Platform, 0);
865  }
866 
867  // Figure out how big the initial stack needs to be
868 
869  // A sentry NULL void pointer at the top of the stack.
870  int sentry_size = intSize;
871 
872  // This is the name of the file which is present on the initial stack
873  // It's purpose is to let the user space linker examine the original file.
874  int file_name_size = filename.size() + 1;
875 
876  const int numRandomBytes = 16;
877  int aux_data_size = numRandomBytes;
878 
879  std::string platform = "x86_64";
880  aux_data_size += platform.size() + 1;
881 
882  int env_data_size = 0;
883  for (int i = 0; i < envp.size(); ++i)
884  env_data_size += envp[i].size() + 1;
885  int arg_data_size = 0;
886  for (int i = 0; i < argv.size(); ++i)
887  arg_data_size += argv[i].size() + 1;
888 
889  // The info_block needs to be padded so its size is a multiple of the
890  // alignment mask. Also, it appears that there needs to be at least some
891  // padding, so if the size is already a multiple, we need to increase it
892  // anyway.
893  int base_info_block_size =
894  sentry_size + file_name_size + env_data_size + arg_data_size;
895 
896  int info_block_size = roundUp(base_info_block_size, align);
897 
898  int info_block_padding = info_block_size - base_info_block_size;
899 
900  // Each auxiliary vector is two 8 byte words
901  int aux_array_size = intSize * 2 * (auxv.size() + 1);
902 
903  int envp_array_size = intSize * (envp.size() + 1);
904  int argv_array_size = intSize * (argv.size() + 1);
905 
906  int argc_size = intSize;
907 
908  // Figure out the size of the contents of the actual initial frame
909  int frame_size =
910  aux_array_size +
911  envp_array_size +
912  argv_array_size +
913  argc_size;
914 
915  // There needs to be padding after the auxiliary vector data so that the
916  // very bottom of the stack is aligned properly.
917  int partial_size = frame_size + aux_data_size;
918  int aligned_partial_size = roundUp(partial_size, align);
919  int aux_padding = aligned_partial_size - partial_size;
920 
921  int space_needed =
922  info_block_size +
923  aux_data_size +
924  aux_padding +
925  frame_size;
926 
927  Addr stack_base = memState->getStackBase();
928 
929  Addr stack_min = stack_base - space_needed;
930  stack_min = roundDown(stack_min, align);
931 
932  unsigned stack_size = stack_base - stack_min;
933  stack_size = roundUp(stack_size, pageSize);
934  memState->setStackSize(stack_size);
935 
936  // map memory
937  Addr stack_end = roundDown(stack_base - stack_size, pageSize);
938 
939  DPRINTF(Stack, "Mapping the stack: 0x%x %dB\n", stack_end, stack_size);
940  memState->mapRegion(stack_end, stack_size, "stack");
941 
942  // map out initial stack contents
943  IntType sentry_base = stack_base - sentry_size;
944  IntType file_name_base = sentry_base - file_name_size;
945  IntType env_data_base = file_name_base - env_data_size;
946  IntType arg_data_base = env_data_base - arg_data_size;
947  IntType aux_data_base = arg_data_base - info_block_padding - aux_data_size;
948  IntType auxv_array_base = aux_data_base - aux_array_size - aux_padding;
949  IntType envp_array_base = auxv_array_base - envp_array_size;
950  IntType argv_array_base = envp_array_base - argv_array_size;
951  IntType argc_base = argv_array_base - argc_size;
952 
953  DPRINTF(Stack, "The addresses of items on the initial stack:\n");
954  DPRINTF(Stack, "0x%x - file name\n", file_name_base);
955  DPRINTF(Stack, "0x%x - env data\n", env_data_base);
956  DPRINTF(Stack, "0x%x - arg data\n", arg_data_base);
957  DPRINTF(Stack, "0x%x - aux data\n", aux_data_base);
958  DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base);
959  DPRINTF(Stack, "0x%x - envp array\n", envp_array_base);
960  DPRINTF(Stack, "0x%x - argv array\n", argv_array_base);
961  DPRINTF(Stack, "0x%x - argc \n", argc_base);
962  DPRINTF(Stack, "0x%x - stack min\n", stack_min);
963 
964  // write contents to stack
965 
966  // figure out argc
967  IntType argc = argv.size();
968  IntType guestArgc = htole(argc);
969 
970  // Write out the sentry void *
971  IntType sentry_NULL = 0;
972  initVirtMem->writeBlob(sentry_base, &sentry_NULL, sentry_size);
973 
974  // Write the file name
975  initVirtMem->writeString(file_name_base, filename.c_str());
976 
977  // Fix up the aux vectors which point to data
978  assert(auxv[auxv.size() - 3].type == gem5::auxv::Random);
979  auxv[auxv.size() - 3].val = aux_data_base;
980  assert(auxv[auxv.size() - 2].type == gem5::auxv::Execfn);
981  auxv[auxv.size() - 2].val = argv_array_base;
982  assert(auxv[auxv.size() - 1].type == gem5::auxv::Platform);
983  auxv[auxv.size() - 1].val = aux_data_base + numRandomBytes;
984 
985 
986  // Copy the aux stuff
987  Addr auxv_array_end = auxv_array_base;
988  for (const auto &aux: auxv) {
989  initVirtMem->write(auxv_array_end, aux, ByteOrder::little);
990  auxv_array_end += sizeof(aux);
991  }
992  // Write out the terminating zeroed auxiliary vector
993  const gem5::auxv::AuxVector<uint64_t> zero(0, 0);
994  initVirtMem->write(auxv_array_end, zero);
995  auxv_array_end += sizeof(zero);
996 
997  initVirtMem->writeString(aux_data_base, platform.c_str());
998 
999  copyStringArray(envp, envp_array_base, env_data_base,
1000  ByteOrder::little, *initVirtMem);
1001  copyStringArray(argv, argv_array_base, arg_data_base,
1002  ByteOrder::little, *initVirtMem);
1003 
1004  initVirtMem->writeBlob(argc_base, &guestArgc, intSize);
1005 
1007  // Set the stack pointer register
1008  tc->setReg(int_reg::Rsp, stack_min);
1009 
1010  // There doesn't need to be any segment base added in since we're dealing
1011  // with the flat segmentation model.
1012  tc->pcState(getStartPC());
1013 
1014  // Align the "stack_min" to a page boundary.
1015  memState->setStackMin(roundDown(stack_min, pageSize));
1016 }
1017 
1018 void
1020 {
1022  extraAuxvs.emplace_back(auxv::SysinfoEhdr, vsyscallPage.base);
1023  X86Process::argsInit<uint64_t>(pageSize, extraAuxvs);
1024 }
1025 
1026 void
1028 {
1030  //Tell the binary where the vsyscall part of the vsyscall page is.
1031  extraAuxvs.emplace_back(auxv::Sysinfo,
1033  extraAuxvs.emplace_back(auxv::SysinfoEhdr, vsyscallPage.base);
1034  X86Process::argsInit<uint32_t>(pageSize, extraAuxvs);
1035 }
1036 
1037 void
1039  Process *p, RegVal flags)
1040 {
1041  X86Process::clone(old_tc, new_tc, p, flags);
1042  ((X86_64Process*)p)->vsyscallPage = vsyscallPage;
1043 }
1044 
1045 void
1047  Process *p, RegVal flags)
1048 {
1049  X86Process::clone(old_tc, new_tc, p, flags);
1050  ((I386Process*)p)->vsyscallPage = vsyscallPage;
1051 }
1052 
1053 } // namespace gem5
#define DPRINTF(x,...)
Definition: trace.hh:186
virtual void map(Addr vaddr, Addr paddr, int64_t size, uint64_t flags=0)
Maps a virtual memory region to a physical memory region.
Definition: page_table.cc:48
uint64_t pid() const
Definition: page_table.hh:86
This object is a proxy for a port or other object which implements the functional response protocol,...
Definition: port_proxy.hh:87
void writeBlob(Addr addr, const void *p, int size) const
Same as tryWriteBlob, but insists on success.
Definition: port_proxy.hh:192
loader::MemoryImage image
Definition: process.hh:225
void allocateMem(Addr vaddr, int64_t size, bool clobber=false)
Definition: process.cc:317
uint64_t egid()
Definition: process.hh:86
SEWorkload * seWorkload
Definition: process.hh:176
std::unique_ptr< SETranslatingPortProxy > initVirtMem
Definition: process.hh:188
virtual void clone(ThreadContext *old_tc, ThreadContext *new_tc, Process *new_p, RegVal flags)
Definition: process.cc:167
std::shared_ptr< MemState > memState
Definition: process.hh:290
uint64_t uid()
Definition: process.hh:83
std::vector< std::string > argv
Definition: process.hh:227
bool kvmInSE
Definition: process.hh:181
bool useForClone
Definition: process.hh:183
std::vector< ContextID > contextIds
Definition: process.hh:171
System * system
Definition: process.hh:174
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
Definition: process.cc:288
std::vector< std::string > envp
Definition: process.hh:228
uint64_t gid()
Definition: process.hh:85
Addr getStartPC()
Definition: process.cc:497
loader::ObjectFile * objFile
Definition: process.hh:224
Addr getBias()
Definition: process.cc:489
uint64_t euid()
Definition: process.hh:84
EmulationPageTable * pTable
Definition: process.hh:185
Addr allocPhysPages(int npages, int pool_id=0)
Definition: se_workload.cc:75
PortProxy physProxy
Port to physical memory used for writing object files into ram at boot.
Definition: system.hh:326
const AddrRange & m5opRange() const
Range used by memory-mapped m5 pseudo-ops if enabled.
Definition: system.hh:576
Threads threads
Definition: system.hh:313
ThreadContext is the external interface to all thread state for anything outside of the CPU.
virtual void setMiscReg(RegIndex misc_reg, RegVal val)=0
virtual void setMiscRegNoEffect(RegIndex misc_reg, RegVal val)=0
virtual const PCStateBase & pcState() const =0
virtual void setReg(const RegId &reg, RegVal val)
virtual RegVal readMiscRegNoEffect(RegIndex misc_reg) const =0
void clone(ThreadContext *old_tc, ThreadContext *new_tc, Process *process, RegVal flags) override
Definition: process.cc:1046
VSyscallPage vsyscallPage
Definition: process.hh:162
I386Process(const ProcessParams &params, loader::ObjectFile *objFile)
Definition: process.cc:125
void argsInit(int pageSize)
Definition: process.cc:1027
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
Definition: process.cc:612
void clone(ThreadContext *old_tc, ThreadContext *new_tc, Process *process, RegVal flags) override
Definition: process.cc:96
void argsInit(int pageSize, std::vector< gem5::auxv::AuxVector< IntType >> extraAuxvs)
Definition: process.cc:735
X86Process(const ProcessParams &params, loader::ObjectFile *objFile)
Definition: process.cc:84
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
Definition: process.cc:152
void clone(ThreadContext *old_tc, ThreadContext *new_tc, Process *process, RegVal flags) override
Definition: process.cc:1038
VSyscallPage vsyscallPage
Definition: process.hh:124
X86_64Process(const ProcessParams &params, loader::ObjectFile *objFile)
Definition: process.cc:104
void argsInit(int pageSize)
Definition: process.cc:1019
STL vector class.
Definition: stl.hh:37
static constexpr T roundDown(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:279
static constexpr T roundUp(const T &val, const U &align)
This function is used to align addresses in memory.
Definition: intmath.hh:260
constexpr T bits(T val, unsigned first, unsigned last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.
Definition: bitfield.hh:76
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
uint8_t flags
Definition: helpers.cc:66
Declaration of a multi-level page table.
Bitfield< 7 > i
Definition: misc_types.hh:67
Bitfield< 15, 13 > ds
constexpr RegId Rsp
Definition: int.hh:136
static RegIndex segSel(int index)
Definition: misc.hh:505
static RegIndex segAttr(int index)
Definition: misc.hh:533
static RegIndex segBase(int index)
Definition: misc.hh:512
static RegIndex segLimit(int index)
Definition: misc.hh:526
static RegIndex segEffBase(int index)
Definition: misc.hh:519
Bitfield< 15 > system
Definition: misc.hh:1004
const Addr MMIORegionVirtAddr
Definition: se_workload.hh:47
const Addr GDTVirtAddr
Definition: se_workload.hh:41
Bitfield< 2, 0 > seg
Definition: types.hh:87
const Addr ISTVirtAddr
Definition: se_workload.hh:45
void installSegDesc(ThreadContext *tc, int seg, SegDescriptor desc, bool longmode)
Definition: fs_workload.cc:65
Bitfield< 0 > p
Definition: pagetable.hh:151
const Addr TSSVirtAddr
Definition: se_workload.hh:43
const Addr syscallCodeVirtAddr
Definition: se_workload.hh:40
const Addr IDTVirtAddr
Definition: se_workload.hh:42
const Addr PFHandlerVirtAddr
Definition: se_workload.hh:46
const Addr PageBytes
Definition: page_size.hh:49
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
high
Definition: intmath.hh:176
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
MultiLevelPageTable< LongModePTE< 47, 39 >, LongModePTE< 38, 30 >, LongModePTE< 29, 21 >, LongModePTE< 20, 12 > > ArchPageTable
Definition: process.cc:82
T htole(T value)
Definition: byteswap.hh:172
uint64_t RegVal
Definition: types.hh:173
void copyStringArray(std::vector< std::string > &strings, AddrType array_ptr, AddrType data_ptr, const ByteOrder bo, PortProxy &memProxy)
Definition: process_impl.hh:43
void align(const scfx_rep &lhs, const scfx_rep &rhs, int &new_wp, int &len_mant, scfx_mant_ref &lhs_mant, scfx_mant_ref &rhs_mant)
Definition: scfx_rep.cc:2051
Declarations of a non-full system Page Table.
const std::string & name()
Definition: trace.cc:49

Generated on Wed Dec 21 2022 10:22:26 for gem5 by doxygen 1.9.1