58 #include "debug/Stack.hh"
61 #include "params/Process.hh"
72 using namespace X86ISA;
74 template class MultiLevelPageTable<LongModePTE<47, 39>,
77 LongModePTE<20, 12> >;
78 typedef MultiLevelPageTable<LongModePTE<47, 39>,
85 Process(params, params.useArchPT ?
113 Addr stack_base = 0x7FFFFFFFF000ULL;
114 Addr max_stack_size = 8 * 1024 * 1024;
115 Addr next_thread_stack_base = stack_base - max_stack_size;
116 Addr mmap_end = 0x7FFFF7FFF000ULL;
118 memState = std::make_shared<MemState>(
119 this, brk_point, stack_base, max_stack_size,
120 next_thread_stack_base, mmap_end);
129 panic(
"KVM CPU model does not support 32 bit processes");
141 Addr max_stack_size = 8 * 1024 * 1024;
142 Addr next_thread_stack_base = stack_base - max_stack_size;
143 Addr mmap_end = 0xB7FFF000ULL;
145 memState = std::make_shared<MemState>(
146 this, brk_point, stack_base, max_stack_size,
147 next_thread_stack_base, mmap_end);
162 uint8_t vtimeBlob[] = {
163 0x48,0xc7,0xc0,0xc9,0x00,0x00,0x00,
168 vtimeBlob,
sizeof(vtimeBlob));
170 uint8_t vgettimeofdayBlob[] = {
171 0x48,0xc7,0xc0,0x60,0x00,0x00,0x00,
177 vgettimeofdayBlob,
sizeof(vgettimeofdayBlob));
192 uint8_t numGDTEntries = 0;
193 uint64_t nullDescriptor = 0;
194 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
198 SegDescriptor initDesc = 0;
199 initDesc.type.codeOrData = 0;
208 initDesc.limit = 0xFFFFFFFF;
212 SegDescriptor csLowPLDesc = initDesc;
213 csLowPLDesc.type.codeOrData = 1;
215 uint64_t csLowPLDescVal = csLowPLDesc;
216 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
221 SegSelector csLowPL = 0;
222 csLowPL.si = numGDTEntries - 1;
226 SegDescriptor dsLowPLDesc = initDesc;
227 dsLowPLDesc.type.codeOrData = 0;
229 uint64_t dsLowPLDescVal = dsLowPLDesc;
230 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
235 SegSelector dsLowPL = 0;
236 dsLowPL.si = numGDTEntries - 1;
240 SegDescriptor dsDesc = initDesc;
241 dsDesc.type.codeOrData = 0;
243 uint64_t dsDescVal = dsDesc;
244 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
250 ds.si = numGDTEntries - 1;
254 SegDescriptor csDesc = initDesc;
255 csDesc.type.codeOrData = 1;
257 uint64_t csDescVal = csDesc;
258 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
264 cs.si = numGDTEntries - 1;
267 SegSelector scall = 0;
268 scall.si = csLowPL.si;
271 SegSelector sret = 0;
272 sret.si = dsLowPL.si;
276 TSSlow TSSDescLow = 0;
277 TSSDescLow.type = 0xB;
280 TSSDescLow.limit = 0xFFFFFFFF;
283 TSShigh TSSDescHigh = 0;
290 } tssDescVal = {TSSDescLow, TSSDescHigh};
292 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
293 &tssDescVal,
sizeof(tssDescVal));
297 SegSelector tssSel = 0;
298 tssSel.si = numGDTEntries - 1;
300 uint64_t tss_base_addr = (TSSDescHigh.base << 32) | TSSDescLow.base;
301 uint64_t tss_limit = TSSDescLow.limit;
303 SegAttr tss_attr = 0;
305 tss_attr.type = TSSDescLow.type;
306 tss_attr.dpl = TSSDescLow.dpl;
307 tss_attr.present = TSSDescLow.p;
308 tss_attr.granularity = TSSDescLow.g;
309 tss_attr.unusable = 0;
412 RegVal sfmask = (1 << 8) | (1 << 10);
451 tss.IST1_low = IST_start;
452 tss.IST1_high = IST_start >> 32;
453 tss.RSP0_low = tss.IST1_low;
454 tss.RSP0_high = tss.IST1_high;
455 tss.RSP1_low = tss.IST1_low;
456 tss.RSP1_high = tss.IST1_high;
457 tss.RSP2_low = tss.IST1_low;
458 tss.RSP2_high = tss.IST1_high;
459 physProxy.
writeBlob(tssPhysAddr, &tss,
sizeof(tss));
462 GateDescriptorLow PFGateLow = 0;
465 PFGateLow.selector = csLowPL;
468 PFGateLow.type = 0xe;
471 GateDescriptorHigh PFGateHigh = 0;
478 } PFGate = {PFGateLow, PFGateHigh};
480 physProxy.
writeBlob(idtPhysAddr + 0xE0, &PFGate,
sizeof(PFGate));
483 uint8_t syscallBlob[] = {
485 0x48, 0xa3, 0x00, 0x70, 0x00,
486 0x00, 0x00, 0xc9, 0xff, 0xff,
492 syscallBlob,
sizeof(syscallBlob));
495 uint8_t faultBlob[] = {
497 0x48, 0xa3, 0x00, 0x70, 0x00,
498 0x00, 0x00, 0xc9, 0xff, 0xff,
500 0x48, 0x83, 0xc4, 0x08,
505 physProxy.
writeBlob(pfHandlerPhysAddr, faultBlob,
sizeof(faultBlob));
527 SegAttr dataAttr = 0;
529 dataAttr.unusable = 0;
530 dataAttr.defaultSize = 1;
531 dataAttr.longMode = 1;
533 dataAttr.granularity = 1;
534 dataAttr.present = 1;
536 dataAttr.writable = 1;
537 dataAttr.readable = 1;
538 dataAttr.expandDown = 0;
551 csAttr.defaultSize = 0;
554 csAttr.granularity = 1;
559 csAttr.expandDown = 0;
608 assert(
_gdtSize %
sizeof(zero) == 0);
616 uint8_t vsyscallBlob[] = {
624 vsyscallBlob,
sizeof(vsyscallBlob));
626 uint8_t vsysexitBlob[] = {
633 vsysexitBlob,
sizeof(vsysexitBlob));
638 SegAttr dataAttr = 0;
640 dataAttr.unusable = 0;
641 dataAttr.defaultSize = 1;
642 dataAttr.longMode = 0;
644 dataAttr.granularity = 1;
645 dataAttr.present = 1;
647 dataAttr.writable = 1;
648 dataAttr.readable = 1;
649 dataAttr.expandDown = 0;
664 csAttr.defaultSize = 1;
667 csAttr.granularity = 1;
672 csAttr.expandDown = 0;
714 template<
class IntType>
719 int intSize =
sizeof(IntType);
723 std::string filename;
734 X86_OnboardFPU = 1 << 0,
735 X86_VirtualModeExtensions = 1 << 1,
736 X86_DebuggingExtensions = 1 << 2,
737 X86_PageSizeExtensions = 1 << 3,
739 X86_TimeStampCounter = 1 << 4,
740 X86_ModelSpecificRegisters = 1 << 5,
741 X86_PhysicalAddressExtensions = 1 << 6,
742 X86_MachineCheckExtensions = 1 << 7,
744 X86_CMPXCHG8Instruction = 1 << 8,
745 X86_OnboardAPIC = 1 << 9,
746 X86_SYSENTER_SYSEXIT = 1 << 11,
748 X86_MemoryTypeRangeRegisters = 1 << 12,
749 X86_PageGlobalEnable = 1 << 13,
750 X86_MachineCheckArchitecture = 1 << 14,
751 X86_CMOVInstruction = 1 << 15,
753 X86_PageAttributeTable = 1 << 16,
754 X86_36BitPSEs = 1 << 17,
755 X86_ProcessorSerialNumber = 1 << 18,
756 X86_CLFLUSHInstruction = 1 << 19,
758 X86_DebugTraceStore = 1 << 21,
759 X86_ACPIViaMSR = 1 << 22,
760 X86_MultimediaExtensions = 1 << 23,
762 X86_FXSAVE_FXRSTOR = 1 << 24,
763 X86_StreamingSIMDExtensions = 1 << 25,
764 X86_StreamingSIMDExtensions2 = 1 << 26,
765 X86_CPUSelfSnoop = 1 << 27,
767 X86_HyperThreading = 1 << 28,
768 X86_AutomaticClockControl = 1 << 29,
769 X86_IA64Processor = 1 << 30
780 X86_VirtualModeExtensions |
781 X86_DebuggingExtensions |
782 X86_PageSizeExtensions |
783 X86_TimeStampCounter |
784 X86_ModelSpecificRegisters |
785 X86_PhysicalAddressExtensions |
786 X86_MachineCheckExtensions |
787 X86_CMPXCHG8Instruction |
789 X86_SYSENTER_SYSEXIT |
790 X86_MemoryTypeRangeRegisters |
791 X86_PageGlobalEnable |
792 X86_MachineCheckArchitecture |
793 X86_CMOVInstruction |
794 X86_PageAttributeTable |
797 X86_CLFLUSHInstruction |
800 X86_MultimediaExtensions |
802 X86_StreamingSIMDExtensions |
803 X86_StreamingSIMDExtensions2 |
851 int sentry_size = intSize;
855 int file_name_size = filename.size() + 1;
857 const int numRandomBytes = 16;
858 int aux_data_size = numRandomBytes;
860 std::string platform =
"x86_64";
861 aux_data_size += platform.size() + 1;
863 int env_data_size = 0;
864 for (
int i = 0;
i <
envp.size(); ++
i)
865 env_data_size +=
envp[
i].size() + 1;
866 int arg_data_size = 0;
867 for (
int i = 0;
i <
argv.size(); ++
i)
868 arg_data_size +=
argv[
i].size() + 1;
874 int base_info_block_size =
875 sentry_size + file_name_size + env_data_size + arg_data_size;
877 int info_block_size =
roundUp(base_info_block_size,
align);
879 int info_block_padding = info_block_size - base_info_block_size;
882 int aux_array_size = intSize * 2 * (auxv.size() + 1);
884 int envp_array_size = intSize * (
envp.size() + 1);
885 int argv_array_size = intSize * (
argv.size() + 1);
887 int argc_size = intSize;
898 int partial_size = frame_size + aux_data_size;
899 int aligned_partial_size =
roundUp(partial_size,
align);
900 int aux_padding = aligned_partial_size - partial_size;
910 Addr stack_min = stack_base - space_needed;
913 unsigned stack_size = stack_base - stack_min;
914 stack_size =
roundUp(stack_size, pageSize);
918 Addr stack_end =
roundDown(stack_base - stack_size, pageSize);
920 DPRINTF(Stack,
"Mapping the stack: 0x%x %dB\n", stack_end, stack_size);
921 memState->mapRegion(stack_end, stack_size,
"stack");
924 IntType sentry_base = stack_base - sentry_size;
925 IntType file_name_base = sentry_base - file_name_size;
926 IntType env_data_base = file_name_base - env_data_size;
927 IntType arg_data_base = env_data_base - arg_data_size;
928 IntType aux_data_base = arg_data_base - info_block_padding - aux_data_size;
929 IntType auxv_array_base = aux_data_base - aux_array_size - aux_padding;
930 IntType envp_array_base = auxv_array_base - envp_array_size;
931 IntType argv_array_base = envp_array_base - argv_array_size;
932 IntType argc_base = argv_array_base - argc_size;
934 DPRINTF(Stack,
"The addresses of items on the initial stack:\n");
935 DPRINTF(Stack,
"0x%x - file name\n", file_name_base);
936 DPRINTF(Stack,
"0x%x - env data\n", env_data_base);
937 DPRINTF(Stack,
"0x%x - arg data\n", arg_data_base);
938 DPRINTF(Stack,
"0x%x - aux data\n", aux_data_base);
939 DPRINTF(Stack,
"0x%x - auxv array\n", auxv_array_base);
940 DPRINTF(Stack,
"0x%x - envp array\n", envp_array_base);
941 DPRINTF(Stack,
"0x%x - argv array\n", argv_array_base);
942 DPRINTF(Stack,
"0x%x - argc \n", argc_base);
943 DPRINTF(Stack,
"0x%x - stack min\n", stack_min);
948 IntType argc =
argv.size();
949 IntType guestArgc =
htole(argc);
952 IntType sentry_NULL = 0;
953 initVirtMem->writeBlob(sentry_base, &sentry_NULL, sentry_size);
956 initVirtMem->writeString(file_name_base, filename.c_str());
960 auxv[auxv.size() - 3].val = aux_data_base;
962 auxv[auxv.size() - 2].val = argv_array_base;
964 auxv[auxv.size() - 1].val = aux_data_base + numRandomBytes;
968 Addr auxv_array_end = auxv_array_base;
969 for (
const auto &aux: auxv) {
970 initVirtMem->write(auxv_array_end, aux, ByteOrder::little);
971 auxv_array_end +=
sizeof(aux);
976 auxv_array_end +=
sizeof(zero);
978 initVirtMem->writeString(aux_data_base, platform.c_str());
985 initVirtMem->writeBlob(argc_base, &guestArgc, intSize);
1004 X86Process::argsInit<uint64_t>(pageSize, extraAuxvs);
1015 X86Process::argsInit<uint32_t>(pageSize, extraAuxvs);