57 #include "debug/Stack.hh"
60 #include "params/Process.hh"
80 Process(params, params->useArchPT ?
108 Addr stack_base = 0x7FFFFFFFF000
ULL;
109 Addr max_stack_size = 8 * 1024 * 1024;
110 Addr next_thread_stack_base = stack_base - max_stack_size;
111 Addr mmap_end = 0x7FFFF7FFF000
ULL;
113 memState = make_shared<MemState>(
this, brk_point, stack_base,
114 max_stack_size, next_thread_stack_base,
124 panic(
"KVM CPU model does not support 32 bit processes");
136 Addr max_stack_size = 8 * 1024 * 1024;
137 Addr next_thread_stack_base = stack_base - max_stack_size;
140 memState = make_shared<MemState>(
this, brk_point, stack_base,
141 max_stack_size, next_thread_stack_base,
157 uint8_t vtimeBlob[] = {
158 0x48,0xc7,0xc0,0xc9,0x00,0x00,0x00,
163 vtimeBlob,
sizeof(vtimeBlob));
165 uint8_t vgettimeofdayBlob[] = {
166 0x48,0xc7,0xc0,0x60,0x00,0x00,0x00,
172 vgettimeofdayBlob,
sizeof(vgettimeofdayBlob));
187 uint8_t numGDTEntries = 0;
188 uint64_t nullDescriptor = 0;
189 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
193 SegDescriptor initDesc = 0;
194 initDesc.type.codeOrData = 0;
203 initDesc.limit = 0xFFFFFFFF;
207 SegDescriptor csLowPLDesc = initDesc;
208 csLowPLDesc.type.codeOrData = 1;
210 uint64_t csLowPLDescVal = csLowPLDesc;
211 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
216 SegSelector csLowPL = 0;
217 csLowPL.si = numGDTEntries - 1;
221 SegDescriptor dsLowPLDesc = initDesc;
222 dsLowPLDesc.type.codeOrData = 0;
224 uint64_t dsLowPLDescVal = dsLowPLDesc;
225 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
230 SegSelector dsLowPL = 0;
231 dsLowPL.si = numGDTEntries - 1;
235 SegDescriptor dsDesc = initDesc;
236 dsDesc.type.codeOrData = 0;
238 uint64_t dsDescVal = dsDesc;
239 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
245 ds.si = numGDTEntries - 1;
249 SegDescriptor csDesc = initDesc;
250 csDesc.type.codeOrData = 1;
252 uint64_t csDescVal = csDesc;
253 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
259 cs.si = numGDTEntries - 1;
262 SegSelector scall = 0;
263 scall.si = csLowPL.si;
266 SegSelector sret = 0;
267 sret.si = dsLowPL.si;
271 TSSlow TSSDescLow = 0;
272 TSSDescLow.type = 0xB;
275 TSSDescLow.limit = 0xFFFFFFFF;
278 TSShigh TSSDescHigh = 0;
284 } tssDescVal = {TSSDescLow, TSSDescHigh};
286 physProxy.
writeBlob(gdtPhysAddr + numGDTEntries * 8,
287 &tssDescVal,
sizeof(tssDescVal));
291 SegSelector tssSel = 0;
292 tssSel.si = numGDTEntries - 1;
294 uint64_t tss_base_addr = (TSSDescHigh.base << 32) | TSSDescLow.base;
295 uint64_t tss_limit = TSSDescLow.limit;
297 SegAttr tss_attr = 0;
299 tss_attr.type = TSSDescLow.type;
300 tss_attr.dpl = TSSDescLow.dpl;
301 tss_attr.present = TSSDescLow.p;
302 tss_attr.granularity = TSSDescLow.g;
303 tss_attr.unusable = 0;
406 RegVal sfmask = (1 << 8) | (1 << 10);
444 tss.IST1_low = IST_start;
445 tss.IST1_high = IST_start >> 32;
446 tss.RSP0_low = tss.IST1_low;
447 tss.RSP0_high = tss.IST1_high;
448 tss.RSP1_low = tss.IST1_low;
449 tss.RSP1_high = tss.IST1_high;
450 tss.RSP2_low = tss.IST1_low;
451 tss.RSP2_high = tss.IST1_high;
452 physProxy.
writeBlob(tssPhysAddr, &tss,
sizeof(tss));
455 GateDescriptorLow PFGateLow = 0;
458 PFGateLow.selector = csLowPL;
461 PFGateLow.type = 0xe;
464 GateDescriptorHigh PFGateHigh = 0;
470 } PFGate = {PFGateLow, PFGateHigh};
472 physProxy.
writeBlob(idtPhysAddr + 0xE0, &PFGate,
sizeof(PFGate));
475 uint8_t syscallBlob[] = {
477 0x48, 0xa3, 0x00, 0x60, 0x00,
478 0x00, 0x00, 0xc9, 0xff, 0xff,
484 syscallBlob,
sizeof(syscallBlob));
487 uint8_t faultBlob[] = {
489 0x48, 0xa3, 0x00, 0x61, 0x00,
490 0x00, 0x00, 0xc9, 0xff, 0xff,
492 0x48, 0x83, 0xc4, 0x08,
497 physProxy.
writeBlob(pfHandlerPhysAddr, faultBlob,
sizeof(faultBlob));
519 SegAttr dataAttr = 0;
521 dataAttr.unusable = 0;
522 dataAttr.defaultSize = 1;
523 dataAttr.longMode = 1;
525 dataAttr.granularity = 1;
526 dataAttr.present = 1;
528 dataAttr.writable = 1;
529 dataAttr.readable = 1;
530 dataAttr.expandDown = 0;
543 csAttr.defaultSize = 0;
546 csAttr.granularity = 1;
551 csAttr.expandDown = 0;
600 assert(
_gdtSize %
sizeof(zero) == 0);
608 uint8_t vsyscallBlob[] = {
616 vsyscallBlob,
sizeof(vsyscallBlob));
618 uint8_t vsysexitBlob[] = {
625 vsysexitBlob,
sizeof(vsysexitBlob));
630 SegAttr dataAttr = 0;
632 dataAttr.unusable = 0;
633 dataAttr.defaultSize = 1;
634 dataAttr.longMode = 0;
636 dataAttr.granularity = 1;
637 dataAttr.present = 1;
639 dataAttr.writable = 1;
640 dataAttr.readable = 1;
641 dataAttr.expandDown = 0;
656 csAttr.defaultSize = 1;
659 csAttr.granularity = 1;
664 csAttr.expandDown = 0;
706 template<
class IntType>
711 int intSize =
sizeof(IntType);
725 X86_OnboardFPU = 1 << 0,
726 X86_VirtualModeExtensions = 1 << 1,
727 X86_DebuggingExtensions = 1 << 2,
728 X86_PageSizeExtensions = 1 << 3,
730 X86_TimeStampCounter = 1 << 4,
731 X86_ModelSpecificRegisters = 1 << 5,
732 X86_PhysicalAddressExtensions = 1 << 6,
733 X86_MachineCheckExtensions = 1 << 7,
735 X86_CMPXCHG8Instruction = 1 << 8,
736 X86_OnboardAPIC = 1 << 9,
737 X86_SYSENTER_SYSEXIT = 1 << 11,
739 X86_MemoryTypeRangeRegisters = 1 << 12,
740 X86_PageGlobalEnable = 1 << 13,
741 X86_MachineCheckArchitecture = 1 << 14,
742 X86_CMOVInstruction = 1 << 15,
744 X86_PageAttributeTable = 1 << 16,
745 X86_36BitPSEs = 1 << 17,
746 X86_ProcessorSerialNumber = 1 << 18,
747 X86_CLFLUSHInstruction = 1 << 19,
749 X86_DebugTraceStore = 1 << 21,
750 X86_ACPIViaMSR = 1 << 22,
751 X86_MultimediaExtensions = 1 << 23,
753 X86_FXSAVE_FXRSTOR = 1 << 24,
754 X86_StreamingSIMDExtensions = 1 << 25,
755 X86_StreamingSIMDExtensions2 = 1 << 26,
756 X86_CPUSelfSnoop = 1 << 27,
758 X86_HyperThreading = 1 << 28,
759 X86_AutomaticClockControl = 1 << 29,
760 X86_IA64Processor = 1 << 30
771 X86_VirtualModeExtensions |
772 X86_DebuggingExtensions |
773 X86_PageSizeExtensions |
774 X86_TimeStampCounter |
775 X86_ModelSpecificRegisters |
776 X86_PhysicalAddressExtensions |
777 X86_MachineCheckExtensions |
778 X86_CMPXCHG8Instruction |
780 X86_SYSENTER_SYSEXIT |
781 X86_MemoryTypeRangeRegisters |
782 X86_PageGlobalEnable |
783 X86_MachineCheckArchitecture |
784 X86_CMOVInstruction |
785 X86_PageAttributeTable |
788 X86_CLFLUSHInstruction |
791 X86_MultimediaExtensions |
793 X86_StreamingSIMDExtensions |
794 X86_StreamingSIMDExtensions2 |
811 auxv.emplace_back(
M5_AT_PHDR, elfObject->programHeaderTable());
813 auxv.emplace_back(
M5_AT_PHENT, elfObject->programHeaderSize());
815 auxv.emplace_back(
M5_AT_PHNUM, elfObject->programHeaderCount());
842 int sentry_size = intSize;
846 int file_name_size = filename.size() + 1;
848 const int numRandomBytes = 16;
849 int aux_data_size = numRandomBytes;
851 string platform =
"x86_64";
852 aux_data_size += platform.size() + 1;
854 int env_data_size = 0;
855 for (
int i = 0;
i <
envp.size(); ++
i)
856 env_data_size +=
envp[
i].size() + 1;
857 int arg_data_size = 0;
858 for (
int i = 0;
i <
argv.size(); ++
i)
859 arg_data_size +=
argv[
i].size() + 1;
865 int base_info_block_size =
866 sentry_size + file_name_size + env_data_size + arg_data_size;
868 int info_block_size =
roundUp(base_info_block_size,
align);
870 int info_block_padding = info_block_size - base_info_block_size;
873 int aux_array_size = intSize * 2 * (auxv.size() + 1);
875 int envp_array_size = intSize * (
envp.size() + 1);
876 int argv_array_size = intSize * (
argv.size() + 1);
878 int argc_size = intSize;
889 int partial_size = frame_size + aux_data_size;
890 int aligned_partial_size =
roundUp(partial_size,
align);
891 int aux_padding = aligned_partial_size - partial_size;
901 Addr stack_min = stack_base - space_needed;
904 unsigned stack_size = stack_base - stack_min;
905 stack_size =
roundUp(stack_size, pageSize);
909 Addr stack_end =
roundDown(stack_base - stack_size, pageSize);
911 DPRINTF(Stack,
"Mapping the stack: 0x%x %dB\n", stack_end, stack_size);
912 memState->mapRegion(stack_end, stack_size,
"stack");
915 IntType sentry_base = stack_base - sentry_size;
916 IntType file_name_base = sentry_base - file_name_size;
917 IntType env_data_base = file_name_base - env_data_size;
918 IntType arg_data_base = env_data_base - arg_data_size;
919 IntType aux_data_base = arg_data_base - info_block_padding - aux_data_size;
920 IntType auxv_array_base = aux_data_base - aux_array_size - aux_padding;
921 IntType envp_array_base = auxv_array_base - envp_array_size;
922 IntType argv_array_base = envp_array_base - argv_array_size;
923 IntType argc_base = argv_array_base - argc_size;
925 DPRINTF(Stack,
"The addresses of items on the initial stack:\n");
926 DPRINTF(Stack,
"0x%x - file name\n", file_name_base);
927 DPRINTF(Stack,
"0x%x - env data\n", env_data_base);
928 DPRINTF(Stack,
"0x%x - arg data\n", arg_data_base);
929 DPRINTF(Stack,
"0x%x - aux data\n", aux_data_base);
930 DPRINTF(Stack,
"0x%x - auxv array\n", auxv_array_base);
931 DPRINTF(Stack,
"0x%x - envp array\n", envp_array_base);
932 DPRINTF(Stack,
"0x%x - argv array\n", argv_array_base);
933 DPRINTF(Stack,
"0x%x - argc \n", argc_base);
934 DPRINTF(Stack,
"0x%x - stack min\n", stack_min);
939 IntType argc =
argv.size();
940 IntType guestArgc =
htole(argc);
943 IntType sentry_NULL = 0;
944 initVirtMem->writeBlob(sentry_base, &sentry_NULL, sentry_size);
947 initVirtMem->writeString(file_name_base, filename.c_str());
951 auxv[auxv.size() - 3].val = aux_data_base;
953 auxv[auxv.size() - 2].val = argv_array_base;
955 auxv[auxv.size() - 1].val = aux_data_base + numRandomBytes;
959 Addr auxv_array_end = auxv_array_base;
960 for (
const auto &aux: auxv) {
962 auxv_array_end +=
sizeof(aux);
967 auxv_array_end +=
sizeof(zero);
969 initVirtMem->writeString(aux_data_base, platform.c_str());
976 initVirtMem->writeBlob(argc_base, &guestArgc, intSize);
995 X86Process::argsInit<uint64_t>(pageSize, extraAuxvs);
1006 X86Process::argsInit<uint32_t>(pageSize, extraAuxvs);