59 #include "debug/Stack.hh"
62 #include "params/Process.hh"
73 using namespace X86ISA;
75 template class MultiLevelPageTable<LongModePTE<47, 39>,
78 LongModePTE<20, 12> >;
79 typedef MultiLevelPageTable<LongModePTE<47, 39>,
86 Process(params, params.useArchPT ?
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;
119 memState = std::make_shared<MemState>(
120 this, brk_point, stack_base, max_stack_size,
121 next_thread_stack_base, mmap_end);
130 panic(
"KVM CPU model does not support 32 bit processes");
142 Addr max_stack_size = 8 * 1024 * 1024;
143 Addr next_thread_stack_base = stack_base - max_stack_size;
144 Addr mmap_end = 0xB7FFF000ULL;
146 memState = std::make_shared<MemState>(
147 this, brk_point, stack_base, max_stack_size,
148 next_thread_stack_base, mmap_end);
163 uint8_t vtimeBlob[] = {
164 0x48,0xc7,0xc0,0xc9,0x00,0x00,0x00,
169 vtimeBlob,
sizeof(vtimeBlob));
171 uint8_t vgettimeofdayBlob[] = {
172 0x48,0xc7,0xc0,0x60,0x00,0x00,0x00,
178 vgettimeofdayBlob,
sizeof(vgettimeofdayBlob));
193 uint8_t numGDTEntries = 0;
194 uint64_t nullDescriptor = 0;
195 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
199 SegDescriptor initDesc = 0;
200 initDesc.type.codeOrData = 0;
209 initDesc.limit = 0xFFFFFFFF;
213 SegDescriptor csLowPLDesc = initDesc;
214 csLowPLDesc.type.codeOrData = 1;
216 uint64_t csLowPLDescVal = csLowPLDesc;
217 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
222 SegSelector csLowPL = 0;
223 csLowPL.si = numGDTEntries - 1;
227 SegDescriptor dsLowPLDesc = initDesc;
228 dsLowPLDesc.type.codeOrData = 0;
230 uint64_t dsLowPLDescVal = dsLowPLDesc;
231 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
236 SegSelector dsLowPL = 0;
237 dsLowPL.si = numGDTEntries - 1;
241 SegDescriptor dsDesc = initDesc;
242 dsDesc.type.codeOrData = 0;
244 uint64_t dsDescVal = dsDesc;
245 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
251 ds.si = numGDTEntries - 1;
255 SegDescriptor csDesc = initDesc;
256 csDesc.type.codeOrData = 1;
258 uint64_t csDescVal = csDesc;
259 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
265 cs.si = numGDTEntries - 1;
268 SegSelector scall = 0;
269 scall.si = csLowPL.si;
272 SegSelector sret = 0;
273 sret.si = dsLowPL.si;
277 TSSlow TSSDescLow = 0;
278 TSSDescLow.type = 0xB;
281 TSSDescLow.limit = 0xFFFFFFFF;
284 TSShigh TSSDescHigh = 0;
291 } tssDescVal = {TSSDescLow, TSSDescHigh};
293 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
294 &tssDescVal,
sizeof(tssDescVal));
298 SegSelector tssSel = 0;
299 tssSel.si = numGDTEntries - 1;
301 uint64_t tss_base_addr = (TSSDescHigh.base << 32) | TSSDescLow.base;
302 uint64_t tss_limit = TSSDescLow.limit;
304 SegAttr tss_attr = 0;
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;
325 tslAttr.unusable = 1;
414 RegVal sfmask = (1 << 8) | (1 << 10);
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));
464 GateDescriptorLow PFGateLow = 0;
467 PFGateLow.selector = csLowPL;
470 PFGateLow.type = 0xe;
473 GateDescriptorHigh PFGateHigh = 0;
480 } PFGate = {PFGateLow, PFGateHigh};
482 physProxy.
writeBlob(idtPhysAddr + 0xE0, &PFGate,
sizeof(PFGate));
485 uint8_t syscallBlob[] = {
487 0x48, 0xa3, 0x00, 0x70, 0x00,
488 0x00, 0x00, 0xc9, 0xff, 0xff,
494 syscallBlob,
sizeof(syscallBlob));
497 uint8_t faultBlob[] = {
499 0x48, 0xa3, 0x00, 0x70, 0x00,
500 0x00, 0x00, 0xc9, 0xff, 0xff,
502 0x48, 0x83, 0xc4, 0x08,
507 physProxy.
writeBlob(pfHandlerPhysAddr, faultBlob,
sizeof(faultBlob));
524 if (m5op_range.size()) {
526 m5op_range.size(),
false);
532 SegAttr dataAttr = 0;
534 dataAttr.unusable = 0;
535 dataAttr.defaultSize = 1;
536 dataAttr.longMode = 1;
538 dataAttr.granularity = 1;
539 dataAttr.present = 1;
541 dataAttr.writable = 1;
542 dataAttr.readable = 1;
543 dataAttr.expandDown = 0;
556 csAttr.defaultSize = 0;
559 csAttr.granularity = 1;
564 csAttr.expandDown = 0;
624 assert(
_gdtSize %
sizeof(zero) == 0);
632 uint8_t vsyscallBlob[] = {
640 vsyscallBlob,
sizeof(vsyscallBlob));
642 uint8_t vsysexitBlob[] = {
649 vsysexitBlob,
sizeof(vsysexitBlob));
654 SegAttr dataAttr = 0;
656 dataAttr.unusable = 0;
657 dataAttr.defaultSize = 1;
658 dataAttr.longMode = 0;
660 dataAttr.granularity = 1;
661 dataAttr.present = 1;
663 dataAttr.writable = 1;
664 dataAttr.readable = 1;
665 dataAttr.expandDown = 0;
680 csAttr.defaultSize = 1;
683 csAttr.granularity = 1;
688 csAttr.expandDown = 0;
733 template<
class IntType>
738 int intSize =
sizeof(IntType);
742 std::string filename;
753 X86_OnboardFPU = 1 << 0,
754 X86_VirtualModeExtensions = 1 << 1,
755 X86_DebuggingExtensions = 1 << 2,
756 X86_PageSizeExtensions = 1 << 3,
758 X86_TimeStampCounter = 1 << 4,
759 X86_ModelSpecificRegisters = 1 << 5,
760 X86_PhysicalAddressExtensions = 1 << 6,
761 X86_MachineCheckExtensions = 1 << 7,
763 X86_CMPXCHG8Instruction = 1 << 8,
764 X86_OnboardAPIC = 1 << 9,
765 X86_SYSENTER_SYSEXIT = 1 << 11,
767 X86_MemoryTypeRangeRegisters = 1 << 12,
768 X86_PageGlobalEnable = 1 << 13,
769 X86_MachineCheckArchitecture = 1 << 14,
770 X86_CMOVInstruction = 1 << 15,
772 X86_PageAttributeTable = 1 << 16,
773 X86_36BitPSEs = 1 << 17,
774 X86_ProcessorSerialNumber = 1 << 18,
775 X86_CLFLUSHInstruction = 1 << 19,
777 X86_DebugTraceStore = 1 << 21,
778 X86_ACPIViaMSR = 1 << 22,
779 X86_MultimediaExtensions = 1 << 23,
781 X86_FXSAVE_FXRSTOR = 1 << 24,
782 X86_StreamingSIMDExtensions = 1 << 25,
783 X86_StreamingSIMDExtensions2 = 1 << 26,
784 X86_CPUSelfSnoop = 1 << 27,
786 X86_HyperThreading = 1 << 28,
787 X86_AutomaticClockControl = 1 << 29,
788 X86_IA64Processor = 1 << 30
799 X86_VirtualModeExtensions |
800 X86_DebuggingExtensions |
801 X86_PageSizeExtensions |
802 X86_TimeStampCounter |
803 X86_ModelSpecificRegisters |
804 X86_PhysicalAddressExtensions |
805 X86_MachineCheckExtensions |
806 X86_CMPXCHG8Instruction |
808 X86_SYSENTER_SYSEXIT |
809 X86_MemoryTypeRangeRegisters |
810 X86_PageGlobalEnable |
811 X86_MachineCheckArchitecture |
812 X86_CMOVInstruction |
813 X86_PageAttributeTable |
816 X86_CLFLUSHInstruction |
819 X86_MultimediaExtensions |
821 X86_StreamingSIMDExtensions |
822 X86_StreamingSIMDExtensions2 |
870 int sentry_size = intSize;
874 int file_name_size = filename.size() + 1;
876 const int numRandomBytes = 16;
877 int aux_data_size = numRandomBytes;
879 std::string platform =
"x86_64";
880 aux_data_size += platform.size() + 1;
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;
893 int base_info_block_size =
894 sentry_size + file_name_size + env_data_size + arg_data_size;
896 int info_block_size =
roundUp(base_info_block_size,
align);
898 int info_block_padding = info_block_size - base_info_block_size;
901 int aux_array_size = intSize * 2 * (auxv.size() + 1);
903 int envp_array_size = intSize * (
envp.size() + 1);
904 int argv_array_size = intSize * (
argv.size() + 1);
906 int argc_size = intSize;
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;
929 Addr stack_min = stack_base - space_needed;
932 unsigned stack_size = stack_base - stack_min;
933 stack_size =
roundUp(stack_size, pageSize);
937 Addr stack_end =
roundDown(stack_base - stack_size, pageSize);
939 DPRINTF(Stack,
"Mapping the stack: 0x%x %dB\n", stack_end, stack_size);
940 memState->mapRegion(stack_end, stack_size,
"stack");
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;
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);
967 IntType argc =
argv.size();
968 IntType guestArgc =
htole(argc);
971 IntType sentry_NULL = 0;
972 initVirtMem->writeBlob(sentry_base, &sentry_NULL, sentry_size);
975 initVirtMem->writeString(file_name_base, filename.c_str());
979 auxv[auxv.size() - 3].val = aux_data_base;
981 auxv[auxv.size() - 2].val = argv_array_base;
983 auxv[auxv.size() - 1].val = aux_data_base + numRandomBytes;
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);
995 auxv_array_end +=
sizeof(zero);
997 initVirtMem->writeString(aux_data_base, platform.c_str());
1004 initVirtMem->writeBlob(argc_base, &guestArgc, intSize);
1023 X86Process::argsInit<uint64_t>(pageSize, extraAuxvs);
1034 X86Process::argsInit<uint32_t>(pageSize, extraAuxvs);