60 #include "debug/Stack.hh" 63 #include "params/Process.hh" 108 X86Process::X86Process(ProcessParams *params,
ObjectFile *objFile,
110 :
Process(params, params->useArchPT ?
112 new ArchPageTable(params->
name, params->pid,
117 syscallDescs(_syscallDescs), numSyscallDescs(_numSyscallDescs)
131 :
X86Process(params, objFile, _syscallDescs, _numSyscallDescs)
140 Addr stack_base = 0x7FFFFFFFF000
ULL;
141 Addr max_stack_size = 8 * 1024 * 1024;
142 Addr next_thread_stack_base = stack_base - max_stack_size;
143 Addr mmap_end = 0x7FFFF7FFF000
ULL;
145 memState = make_shared<MemState>(brk_point, stack_base, max_stack_size,
146 next_thread_stack_base, mmap_end);
152 :
X86Process(params, objFile, _syscallDescs, _numSyscallDescs)
155 panic(
"KVM CPU model does not support 32 bit processes");
167 Addr max_stack_size = 8 * 1024 * 1024;
168 Addr next_thread_stack_base = stack_base - max_stack_size;
171 memState = make_shared<MemState>(brk_point, stack_base, max_stack_size,
172 next_thread_stack_base, mmap_end);
195 uint8_t vtimeBlob[] = {
196 0x48,0xc7,0xc0,0xc9,0x00,0x00,0x00,
201 vtimeBlob,
sizeof(vtimeBlob));
203 uint8_t vgettimeofdayBlob[] = {
204 0x48,0xc7,0xc0,0x60,0x00,0x00,0x00,
209 vgettimeofdayBlob,
sizeof(vgettimeofdayBlob));
224 uint8_t numGDTEntries = 0;
225 uint64_t nullDescriptor = 0;
226 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
230 SegDescriptor initDesc = 0;
231 initDesc.type.codeOrData = 0;
240 initDesc.limit = 0xFFFFFFFF;
244 SegDescriptor csLowPLDesc = initDesc;
245 csLowPLDesc.type.codeOrData = 1;
247 uint64_t csLowPLDescVal = csLowPLDesc;
248 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
253 SegSelector csLowPL = 0;
254 csLowPL.si = numGDTEntries - 1;
258 SegDescriptor dsLowPLDesc = initDesc;
259 dsLowPLDesc.type.codeOrData = 0;
261 uint64_t dsLowPLDescVal = dsLowPLDesc;
262 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
267 SegSelector dsLowPL = 0;
268 dsLowPL.si = numGDTEntries - 1;
272 SegDescriptor dsDesc = initDesc;
273 dsDesc.type.codeOrData = 0;
275 uint64_t dsDescVal = dsDesc;
276 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
282 ds.si = numGDTEntries - 1;
286 SegDescriptor csDesc = initDesc;
287 csDesc.type.codeOrData = 1;
289 uint64_t csDescVal = csDesc;
290 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
296 cs.si = numGDTEntries - 1;
299 SegSelector scall = 0;
300 scall.si = csLowPL.si;
303 SegSelector sret = 0;
304 sret.si = dsLowPL.si;
308 TSSlow TSSDescLow = 0;
309 TSSDescLow.type = 0xB;
312 TSSDescLow.limit = 0xFFFFFFFF;
315 TSShigh TSSDescHigh = 0;
321 } tssDescVal = {TSSDescLow, TSSDescHigh};
323 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
324 &tssDescVal,
sizeof(tssDescVal));
328 SegSelector tssSel = 0;
329 tssSel.si = numGDTEntries - 1;
331 uint64_t tss_base_addr = (TSSDescHigh.base << 32) | TSSDescLow.base;
332 uint64_t tss_limit = TSSDescLow.limit;
334 SegAttr tss_attr = 0;
336 tss_attr.type = TSSDescLow.type;
337 tss_attr.dpl = TSSDescLow.dpl;
338 tss_attr.present = TSSDescLow.p;
339 tss_attr.granularity = TSSDescLow.g;
340 tss_attr.unusable = 0;
405 CR3 cr3 =
dynamic_cast<ArchPageTable *
>(
pTable)->basePtr();
443 RegVal sfmask = (1 << 8) | (1 << 10);
481 tss.IST1_low = IST_start;
482 tss.IST1_high = IST_start >> 32;
483 tss.RSP0_low = tss.IST1_low;
484 tss.RSP0_high = tss.IST1_high;
485 tss.RSP1_low = tss.IST1_low;
486 tss.RSP1_high = tss.IST1_high;
487 tss.RSP2_low = tss.IST1_low;
488 tss.RSP2_high = tss.IST1_high;
489 physProxy.
writeBlob(tssPhysAddr, &tss,
sizeof(tss));
492 GateDescriptorLow PFGateLow = 0;
495 PFGateLow.selector = csLowPL;
498 PFGateLow.type = 0xe;
501 GateDescriptorHigh PFGateHigh = 0;
507 } PFGate = {PFGateLow, PFGateHigh};
509 physProxy.
writeBlob(idtPhysAddr + 0xE0, &PFGate,
sizeof(PFGate));
512 uint8_t syscallBlob[] = {
514 0x48, 0xa3, 0x00, 0x60, 0x00,
515 0x00, 0x00, 0xc9, 0xff, 0xff,
521 syscallBlob,
sizeof(syscallBlob));
524 uint8_t faultBlob[] = {
526 0x48, 0xa3, 0x00, 0x61, 0x00,
527 0x00, 0x00, 0xc9, 0xff, 0xff,
529 0x48, 0x83, 0xc4, 0x08,
534 physProxy.
writeBlob(pfHandlerPhysAddr, faultBlob,
sizeof(faultBlob));
551 16 * PageBytes,
false);
556 SegAttr dataAttr = 0;
558 dataAttr.unusable = 0;
559 dataAttr.defaultSize = 1;
560 dataAttr.longMode = 1;
562 dataAttr.granularity = 1;
563 dataAttr.present = 1;
565 dataAttr.writable = 1;
566 dataAttr.readable = 1;
567 dataAttr.expandDown = 0;
580 csAttr.defaultSize = 0;
583 csAttr.granularity = 1;
588 csAttr.expandDown = 0;
637 assert(
_gdtSize %
sizeof(zero) == 0);
645 uint8_t vsyscallBlob[] = {
653 vsyscallBlob,
sizeof(vsyscallBlob));
655 uint8_t vsysexitBlob[] = {
662 vsysexitBlob,
sizeof(vsysexitBlob));
667 SegAttr dataAttr = 0;
669 dataAttr.unusable = 0;
670 dataAttr.defaultSize = 1;
671 dataAttr.longMode = 0;
673 dataAttr.granularity = 1;
674 dataAttr.present = 1;
676 dataAttr.writable = 1;
677 dataAttr.readable = 1;
678 dataAttr.expandDown = 0;
693 csAttr.defaultSize = 1;
696 csAttr.granularity = 1;
701 csAttr.expandDown = 0;
743 template<
class IntType>
748 int intSize =
sizeof(IntType);
762 X86_OnboardFPU = 1 << 0,
763 X86_VirtualModeExtensions = 1 << 1,
764 X86_DebuggingExtensions = 1 << 2,
765 X86_PageSizeExtensions = 1 << 3,
767 X86_TimeStampCounter = 1 << 4,
768 X86_ModelSpecificRegisters = 1 << 5,
769 X86_PhysicalAddressExtensions = 1 << 6,
770 X86_MachineCheckExtensions = 1 << 7,
772 X86_CMPXCHG8Instruction = 1 << 8,
773 X86_OnboardAPIC = 1 << 9,
774 X86_SYSENTER_SYSEXIT = 1 << 11,
776 X86_MemoryTypeRangeRegisters = 1 << 12,
777 X86_PageGlobalEnable = 1 << 13,
778 X86_MachineCheckArchitecture = 1 << 14,
779 X86_CMOVInstruction = 1 << 15,
781 X86_PageAttributeTable = 1 << 16,
782 X86_36BitPSEs = 1 << 17,
783 X86_ProcessorSerialNumber = 1 << 18,
784 X86_CLFLUSHInstruction = 1 << 19,
786 X86_DebugTraceStore = 1 << 21,
787 X86_ACPIViaMSR = 1 << 22,
788 X86_MultimediaExtensions = 1 << 23,
790 X86_FXSAVE_FXRSTOR = 1 << 24,
791 X86_StreamingSIMDExtensions = 1 << 25,
792 X86_StreamingSIMDExtensions2 = 1 << 26,
793 X86_CPUSelfSnoop = 1 << 27,
795 X86_HyperThreading = 1 << 28,
796 X86_AutomaticClockControl = 1 << 29,
797 X86_IA64Processor = 1 << 30
808 X86_VirtualModeExtensions |
809 X86_DebuggingExtensions |
810 X86_PageSizeExtensions |
811 X86_TimeStampCounter |
812 X86_ModelSpecificRegisters |
813 X86_PhysicalAddressExtensions |
814 X86_MachineCheckExtensions |
815 X86_CMPXCHG8Instruction |
817 X86_SYSENTER_SYSEXIT |
818 X86_MemoryTypeRangeRegisters |
819 X86_PageGlobalEnable |
820 X86_MachineCheckArchitecture |
821 X86_CMOVInstruction |
822 X86_PageAttributeTable |
825 X86_CLFLUSHInstruction |
828 X86_MultimediaExtensions |
830 X86_StreamingSIMDExtensions |
831 X86_StreamingSIMDExtensions2 |
879 int sentry_size = intSize;
883 int file_name_size = filename.size() + 1;
885 const int numRandomBytes = 16;
886 int aux_data_size = numRandomBytes;
888 string platform =
"x86_64";
889 aux_data_size += platform.size() + 1;
891 int env_data_size = 0;
892 for (
int i = 0;
i <
envp.size(); ++
i)
893 env_data_size +=
envp[
i].size() + 1;
894 int arg_data_size = 0;
895 for (
int i = 0;
i <
argv.size(); ++
i)
896 arg_data_size +=
argv[
i].size() + 1;
902 int base_info_block_size =
903 sentry_size + file_name_size + env_data_size + arg_data_size;
905 int info_block_size =
roundUp(base_info_block_size, align);
907 int info_block_padding = info_block_size - base_info_block_size;
910 int aux_array_size = intSize * 2 * (auxv.size() + 1);
912 int envp_array_size = intSize * (
envp.size() + 1);
913 int argv_array_size = intSize * (
argv.size() + 1);
915 int argc_size = intSize;
926 int partial_size = frame_size + aux_data_size;
927 int aligned_partial_size =
roundUp(partial_size, align);
928 int aux_padding = aligned_partial_size - partial_size;
938 Addr stack_min = stack_base - space_needed;
941 unsigned stack_size = stack_base - stack_min;
942 stack_size =
roundUp(stack_size, pageSize);
946 Addr stack_end =
roundDown(stack_base - stack_size, pageSize);
948 DPRINTF(Stack,
"Mapping the stack: 0x%x %dB\n", stack_end, stack_size);
952 IntType sentry_base = stack_base - sentry_size;
953 IntType file_name_base = sentry_base - file_name_size;
954 IntType env_data_base = file_name_base - env_data_size;
955 IntType arg_data_base = env_data_base - arg_data_size;
956 IntType aux_data_base = arg_data_base - info_block_padding - aux_data_size;
957 IntType auxv_array_base = aux_data_base - aux_array_size - aux_padding;
958 IntType envp_array_base = auxv_array_base - envp_array_size;
959 IntType argv_array_base = envp_array_base - argv_array_size;
960 IntType argc_base = argv_array_base - argc_size;
962 DPRINTF(Stack,
"The addresses of items on the initial stack:\n");
963 DPRINTF(Stack,
"0x%x - file name\n", file_name_base);
964 DPRINTF(Stack,
"0x%x - env data\n", env_data_base);
965 DPRINTF(Stack,
"0x%x - arg data\n", arg_data_base);
966 DPRINTF(Stack,
"0x%x - aux data\n", aux_data_base);
967 DPRINTF(Stack,
"0x%x - auxv array\n", auxv_array_base);
968 DPRINTF(Stack,
"0x%x - envp array\n", envp_array_base);
969 DPRINTF(Stack,
"0x%x - argv array\n", argv_array_base);
970 DPRINTF(Stack,
"0x%x - argc \n", argc_base);
971 DPRINTF(Stack,
"0x%x - stack min\n", stack_min);
976 IntType argc =
argv.size();
977 IntType guestArgc =
htole(argc);
980 IntType sentry_NULL = 0;
988 auxv[auxv.size() - 3].val = aux_data_base;
990 auxv[auxv.size() - 2].val = argv_array_base;
992 auxv[auxv.size() - 1].val = aux_data_base + numRandomBytes;
996 Addr auxv_array_end = auxv_array_base;
997 for (
const auto &aux: auxv) {
999 auxv_array_end +=
sizeof(aux);
1004 auxv_array_end +=
sizeof(zero);
1032 X86Process::argsInit<uint64_t>(pageSize, extraAuxvs);
1043 X86Process::argsInit<uint32_t>(pageSize, extraAuxvs);
1070 assert(i < NumArgumentRegs32);
1077 assert(width == 32 || width == 64);
#define panic(...)
This implements a cprintf based panic() function.
VSyscallPage vsyscallPage
virtual void setMiscReg(RegIndex misc_reg, RegVal val)=0
virtual void map(Addr vaddr, Addr paddr, int64_t size, uint64_t flags=0)
Maps a virtual memory region to a physical memory region.
void installSegDesc(ThreadContext *tc, SegmentRegIndex seg, SegDescriptor desc, bool longmode)
Addr programHeaderTable()
void clone(ThreadContext *old_tc, ThreadContext *new_tc, Process *process, RegVal flags) override
const std::string & name()
void argsInit(int pageSize)
const uint64_t syscallCodeVirtAddr
virtual TheISA::PCState pcState() const =0
virtual RegVal readIntReg(RegIndex reg_idx) const =0
std::vector< ContextID > contextIds
void writeString(Addr addr, const char *str) const
Same as tryWriteString, but insists on success.
virtual void setMiscRegNoEffect(RegIndex misc_reg, RegVal val)=0
void allocateMem(Addr vaddr, int64_t size, bool clobber=false)
MultiLevelPageTable< LongModePTE< 47, 39 >, LongModePTE< 38, 30 >, LongModePTE< 29, 21 >, LongModePTE< 20, 12 > > ArchPageTable
SETranslatingPortProxy initVirtMem
const ByteOrder GuestByteOrder
void argsInit(int pageSize, std::vector< AuxVector< IntType > > extraAuxvs)
const uint64_t MMIORegionPhysAddr
Addr allocPhysPages(int npages)
Allocate npages contiguous unused physical pages.
Overload hash function for BasicBlockRange type.
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
Declaration of a multi-level page table.
T roundUp(const T &val, const U &align)
This function is used to align addresses in memory.
X86_64Process(ProcessParams *params, ObjectFile *objFile, SyscallDesc *_syscallDescs, int _numSyscallDescs)
std::shared_ptr< MemState > memState
const int numSyscallDescs
ThreadContext is the external interface to all thread state for anything outside of the CPU...
const int StackPointerReg
const uint64_t ISTVirtAddr
int64_t encodedValue() const
The encoded value (as described above)
ThreadContext * getThreadContext(ContextID tid) const
const uint64_t TSSVirtAddr
void argsInit(int pageSize)
PortProxy physProxy
Port to physical memory used for writing object files into ram at boot.
static MiscRegIndex MISCREG_SEG_ATTR(int index)
static MiscRegIndex MISCREG_SEG_LIMIT(int index)
const uint64_t GDTVirtAddr
const uint64_t MMIORegionVirtAddr
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)
void writeBlob(Addr addr, const void *p, int size) const
Same as tryWriteBlob, but insists on success.
void clone(ThreadContext *old_tc, ThreadContext *new_tc, Process *process, RegVal flags) override
const Params * params() const
void copyStringArray(std::vector< std::string > &strings, AddrType array_ptr, AddrType data_ptr, const ByteOrder bo, PortProxy &memProxy)
static const int ArgumentReg32[]
static const int ArgumentReg[]
T roundDown(const T &val, const U &align)
This function is used to align addresses in memory.
const int NumArgumentRegs
std::vector< std::string > envp
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
virtual void setIntReg(RegIndex reg_idx, RegVal val)=0
static MiscRegIndex MISCREG_SEG_SEL(int index)
const uint64_t PFHandlerVirtAddr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
#define ULL(N)
uint64_t constant
const uint64_t IDTVirtAddr
VSyscallPage vsyscallPage
uint16_t programHeaderSize()
This class provides the wrapper interface for the system call implementations which are defined in th...
This object is a proxy for a port or other object which implements the functional response protocol...
void clone(ThreadContext *old_tc, ThreadContext *new_tc, Process *process, RegVal flags) override
EmulationPageTable * pTable
Declarations of a non-full system Page Table.
static MiscRegIndex MISCREG_SEG_BASE(int index)
RegVal getSyscallArg(ThreadContext *tc, int &i) override
uint16_t programHeaderCount()
This is exposed globally, independent of the ISA.
SyscallDesc * syscallDescs
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
virtual void clone(ThreadContext *old_tc, ThreadContext *new_tc, Process *new_p, RegVal flags)
void setSyscallReturn(ThreadContext *tc, SyscallReturn return_value) override
static MiscRegIndex MISCREG_SEG_EFF_BASE(int index)
void write(Addr address, const T &data) const
Write object T to address.
std::vector< std::string > argv
I386Process(ProcessParams *params, ObjectFile *objFile, SyscallDesc *_syscallDescs, int _numSyscallDescs)
This class represents the return value from an emulated system call, including any errno setting...
RegVal getSyscallArg(ThreadContext *tc, int &i) override
static const int NumArgumentRegs M5_VAR_USED
T bits(T val, int first, int last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it...
SyscallDesc * getDesc(int callnum) override