58 #include "debug/Stack.hh"
61 #include "params/Process.hh"
81 Process(params, params.useArchPT ?
109 Addr stack_base = 0x7FFFFFFFF000
ULL;
110 Addr max_stack_size = 8 * 1024 * 1024;
111 Addr next_thread_stack_base = stack_base - max_stack_size;
112 Addr mmap_end = 0x7FFFF7FFF000
ULL;
114 memState = std::make_shared<MemState>(
115 this, brk_point, stack_base, max_stack_size,
116 next_thread_stack_base, mmap_end);
125 panic(
"KVM CPU model does not support 32 bit processes");
137 Addr max_stack_size = 8 * 1024 * 1024;
138 Addr next_thread_stack_base = stack_base - max_stack_size;
141 memState = std::make_shared<MemState>(
142 this, brk_point, stack_base, max_stack_size,
143 next_thread_stack_base, mmap_end);
158 uint8_t vtimeBlob[] = {
159 0x48,0xc7,0xc0,0xc9,0x00,0x00,0x00,
164 vtimeBlob,
sizeof(vtimeBlob));
166 uint8_t vgettimeofdayBlob[] = {
167 0x48,0xc7,0xc0,0x60,0x00,0x00,0x00,
173 vgettimeofdayBlob,
sizeof(vgettimeofdayBlob));
188 uint8_t numGDTEntries = 0;
189 uint64_t nullDescriptor = 0;
190 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
194 SegDescriptor initDesc = 0;
195 initDesc.type.codeOrData = 0;
204 initDesc.limit = 0xFFFFFFFF;
208 SegDescriptor csLowPLDesc = initDesc;
209 csLowPLDesc.type.codeOrData = 1;
211 uint64_t csLowPLDescVal = csLowPLDesc;
212 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
217 SegSelector csLowPL = 0;
218 csLowPL.si = numGDTEntries - 1;
222 SegDescriptor dsLowPLDesc = initDesc;
223 dsLowPLDesc.type.codeOrData = 0;
225 uint64_t dsLowPLDescVal = dsLowPLDesc;
226 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
231 SegSelector dsLowPL = 0;
232 dsLowPL.si = numGDTEntries - 1;
236 SegDescriptor dsDesc = initDesc;
237 dsDesc.type.codeOrData = 0;
239 uint64_t dsDescVal = dsDesc;
240 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
246 ds.si = numGDTEntries - 1;
250 SegDescriptor csDesc = initDesc;
251 csDesc.type.codeOrData = 1;
253 uint64_t csDescVal = csDesc;
254 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
260 cs.si = numGDTEntries - 1;
263 SegSelector scall = 0;
264 scall.si = csLowPL.si;
267 SegSelector sret = 0;
268 sret.si = dsLowPL.si;
272 TSSlow TSSDescLow = 0;
273 TSSDescLow.type = 0xB;
276 TSSDescLow.limit = 0xFFFFFFFF;
279 TSShigh TSSDescHigh = 0;
285 } tssDescVal = {TSSDescLow, TSSDescHigh};
287 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
288 &tssDescVal,
sizeof(tssDescVal));
292 SegSelector tssSel = 0;
293 tssSel.si = numGDTEntries - 1;
295 uint64_t tss_base_addr = (TSSDescHigh.base << 32) | TSSDescLow.base;
296 uint64_t tss_limit = TSSDescLow.limit;
298 SegAttr tss_attr = 0;
300 tss_attr.type = TSSDescLow.type;
301 tss_attr.dpl = TSSDescLow.dpl;
302 tss_attr.present = TSSDescLow.p;
303 tss_attr.granularity = TSSDescLow.g;
304 tss_attr.unusable = 0;
407 RegVal sfmask = (1 << 8) | (1 << 10);
445 tss.IST1_low = IST_start;
446 tss.IST1_high = IST_start >> 32;
447 tss.RSP0_low = tss.IST1_low;
448 tss.RSP0_high = tss.IST1_high;
449 tss.RSP1_low = tss.IST1_low;
450 tss.RSP1_high = tss.IST1_high;
451 tss.RSP2_low = tss.IST1_low;
452 tss.RSP2_high = tss.IST1_high;
453 physProxy.
writeBlob(tssPhysAddr, &tss,
sizeof(tss));
456 GateDescriptorLow PFGateLow = 0;
459 PFGateLow.selector = csLowPL;
462 PFGateLow.type = 0xe;
465 GateDescriptorHigh PFGateHigh = 0;
471 } PFGate = {PFGateLow, PFGateHigh};
473 physProxy.
writeBlob(idtPhysAddr + 0xE0, &PFGate,
sizeof(PFGate));
476 uint8_t syscallBlob[] = {
478 0x48, 0xa3, 0x00, 0x70, 0x00,
479 0x00, 0x00, 0xc9, 0xff, 0xff,
485 syscallBlob,
sizeof(syscallBlob));
488 uint8_t faultBlob[] = {
490 0x48, 0xa3, 0x00, 0x70, 0x00,
491 0x00, 0x00, 0xc9, 0xff, 0xff,
493 0x48, 0x83, 0xc4, 0x08,
498 physProxy.
writeBlob(pfHandlerPhysAddr, faultBlob,
sizeof(faultBlob));
520 SegAttr dataAttr = 0;
522 dataAttr.unusable = 0;
523 dataAttr.defaultSize = 1;
524 dataAttr.longMode = 1;
526 dataAttr.granularity = 1;
527 dataAttr.present = 1;
529 dataAttr.writable = 1;
530 dataAttr.readable = 1;
531 dataAttr.expandDown = 0;
544 csAttr.defaultSize = 0;
547 csAttr.granularity = 1;
552 csAttr.expandDown = 0;
601 assert(
_gdtSize %
sizeof(zero) == 0);
609 uint8_t vsyscallBlob[] = {
617 vsyscallBlob,
sizeof(vsyscallBlob));
619 uint8_t vsysexitBlob[] = {
626 vsysexitBlob,
sizeof(vsysexitBlob));
631 SegAttr dataAttr = 0;
633 dataAttr.unusable = 0;
634 dataAttr.defaultSize = 1;
635 dataAttr.longMode = 0;
637 dataAttr.granularity = 1;
638 dataAttr.present = 1;
640 dataAttr.writable = 1;
641 dataAttr.readable = 1;
642 dataAttr.expandDown = 0;
657 csAttr.defaultSize = 1;
660 csAttr.granularity = 1;
665 csAttr.expandDown = 0;
707 template<
class IntType>
712 int intSize =
sizeof(IntType);
716 std::string filename;
726 X86_OnboardFPU = 1 << 0,
727 X86_VirtualModeExtensions = 1 << 1,
728 X86_DebuggingExtensions = 1 << 2,
729 X86_PageSizeExtensions = 1 << 3,
731 X86_TimeStampCounter = 1 << 4,
732 X86_ModelSpecificRegisters = 1 << 5,
733 X86_PhysicalAddressExtensions = 1 << 6,
734 X86_MachineCheckExtensions = 1 << 7,
736 X86_CMPXCHG8Instruction = 1 << 8,
737 X86_OnboardAPIC = 1 << 9,
738 X86_SYSENTER_SYSEXIT = 1 << 11,
740 X86_MemoryTypeRangeRegisters = 1 << 12,
741 X86_PageGlobalEnable = 1 << 13,
742 X86_MachineCheckArchitecture = 1 << 14,
743 X86_CMOVInstruction = 1 << 15,
745 X86_PageAttributeTable = 1 << 16,
746 X86_36BitPSEs = 1 << 17,
747 X86_ProcessorSerialNumber = 1 << 18,
748 X86_CLFLUSHInstruction = 1 << 19,
750 X86_DebugTraceStore = 1 << 21,
751 X86_ACPIViaMSR = 1 << 22,
752 X86_MultimediaExtensions = 1 << 23,
754 X86_FXSAVE_FXRSTOR = 1 << 24,
755 X86_StreamingSIMDExtensions = 1 << 25,
756 X86_StreamingSIMDExtensions2 = 1 << 26,
757 X86_CPUSelfSnoop = 1 << 27,
759 X86_HyperThreading = 1 << 28,
760 X86_AutomaticClockControl = 1 << 29,
761 X86_IA64Processor = 1 << 30
772 X86_VirtualModeExtensions |
773 X86_DebuggingExtensions |
774 X86_PageSizeExtensions |
775 X86_TimeStampCounter |
776 X86_ModelSpecificRegisters |
777 X86_PhysicalAddressExtensions |
778 X86_MachineCheckExtensions |
779 X86_CMPXCHG8Instruction |
781 X86_SYSENTER_SYSEXIT |
782 X86_MemoryTypeRangeRegisters |
783 X86_PageGlobalEnable |
784 X86_MachineCheckArchitecture |
785 X86_CMOVInstruction |
786 X86_PageAttributeTable |
789 X86_CLFLUSHInstruction |
792 X86_MultimediaExtensions |
794 X86_StreamingSIMDExtensions |
795 X86_StreamingSIMDExtensions2 |
812 auxv.emplace_back(
M5_AT_PHDR, elfObject->programHeaderTable());
814 auxv.emplace_back(
M5_AT_PHENT, elfObject->programHeaderSize());
816 auxv.emplace_back(
M5_AT_PHNUM, elfObject->programHeaderCount());
843 int sentry_size = intSize;
847 int file_name_size = filename.size() + 1;
849 const int numRandomBytes = 16;
850 int aux_data_size = numRandomBytes;
852 std::string platform =
"x86_64";
853 aux_data_size += platform.size() + 1;
855 int env_data_size = 0;
856 for (
int i = 0;
i <
envp.size(); ++
i)
857 env_data_size +=
envp[
i].size() + 1;
858 int arg_data_size = 0;
859 for (
int i = 0;
i <
argv.size(); ++
i)
860 arg_data_size +=
argv[
i].size() + 1;
866 int base_info_block_size =
867 sentry_size + file_name_size + env_data_size + arg_data_size;
869 int info_block_size =
roundUp(base_info_block_size,
align);
871 int info_block_padding = info_block_size - base_info_block_size;
874 int aux_array_size = intSize * 2 * (auxv.size() + 1);
876 int envp_array_size = intSize * (
envp.size() + 1);
877 int argv_array_size = intSize * (
argv.size() + 1);
879 int argc_size = intSize;
890 int partial_size = frame_size + aux_data_size;
891 int aligned_partial_size =
roundUp(partial_size,
align);
892 int aux_padding = aligned_partial_size - partial_size;
902 Addr stack_min = stack_base - space_needed;
905 unsigned stack_size = stack_base - stack_min;
906 stack_size =
roundUp(stack_size, pageSize);
910 Addr stack_end =
roundDown(stack_base - stack_size, pageSize);
912 DPRINTF(Stack,
"Mapping the stack: 0x%x %dB\n", stack_end, stack_size);
913 memState->mapRegion(stack_end, stack_size,
"stack");
916 IntType sentry_base = stack_base - sentry_size;
917 IntType file_name_base = sentry_base - file_name_size;
918 IntType env_data_base = file_name_base - env_data_size;
919 IntType arg_data_base = env_data_base - arg_data_size;
920 IntType aux_data_base = arg_data_base - info_block_padding - aux_data_size;
921 IntType auxv_array_base = aux_data_base - aux_array_size - aux_padding;
922 IntType envp_array_base = auxv_array_base - envp_array_size;
923 IntType argv_array_base = envp_array_base - argv_array_size;
924 IntType argc_base = argv_array_base - argc_size;
926 DPRINTF(Stack,
"The addresses of items on the initial stack:\n");
927 DPRINTF(Stack,
"0x%x - file name\n", file_name_base);
928 DPRINTF(Stack,
"0x%x - env data\n", env_data_base);
929 DPRINTF(Stack,
"0x%x - arg data\n", arg_data_base);
930 DPRINTF(Stack,
"0x%x - aux data\n", aux_data_base);
931 DPRINTF(Stack,
"0x%x - auxv array\n", auxv_array_base);
932 DPRINTF(Stack,
"0x%x - envp array\n", envp_array_base);
933 DPRINTF(Stack,
"0x%x - argv array\n", argv_array_base);
934 DPRINTF(Stack,
"0x%x - argc \n", argc_base);
935 DPRINTF(Stack,
"0x%x - stack min\n", stack_min);
940 IntType argc =
argv.size();
941 IntType guestArgc =
htole(argc);
944 IntType sentry_NULL = 0;
945 initVirtMem->writeBlob(sentry_base, &sentry_NULL, sentry_size);
948 initVirtMem->writeString(file_name_base, filename.c_str());
952 auxv[auxv.size() - 3].val = aux_data_base;
954 auxv[auxv.size() - 2].val = argv_array_base;
956 auxv[auxv.size() - 1].val = aux_data_base + numRandomBytes;
960 Addr auxv_array_end = auxv_array_base;
961 for (
const auto &aux: auxv) {
963 auxv_array_end +=
sizeof(aux);
968 auxv_array_end +=
sizeof(zero);
970 initVirtMem->writeString(aux_data_base, platform.c_str());
977 initVirtMem->writeBlob(argc_base, &guestArgc, intSize);
996 X86Process::argsInit<uint64_t>(pageSize, extraAuxvs);
1007 X86Process::argsInit<uint32_t>(pageSize, extraAuxvs);