x32 still not working (TODO: do 1:1 page mapping, except for kernel)

This commit is contained in:
Alex
2022-10-18 21:16:12 +03:00
parent 348aa69dcb
commit cb349ae620
15 changed files with 480 additions and 94 deletions

View File

@ -59,7 +59,7 @@ namespace CrashHandler
{
#if defined(__amd64__)
CPU::x64::TrapFrame *Frame = (CPU::x64::TrapFrame *)Data;
error("Exception: %#lx", Frame->InterruptNumber);
error("Exception: %#llx", Frame->InterruptNumber);
if (Frame->cs != GDT_USER_CODE && Frame->cs != GDT_USER_DATA)
{
@ -215,18 +215,18 @@ namespace CrashHandler
}
}
EHPrint("\e7981FCTechnical Informations on CPU %ld:\n",
EHPrint("\e7981FCTechnical Informations on CPU %lld:\n",
CPU::x64::rdmsr(CPU::x64::MSR_FS_BASE));
EHPrint("FS=%#lx GS=%#lx SS=%#lx CS=%#lx DS=%#lx\n",
EHPrint("FS=%#llx GS=%#llx SS=%#llx CS=%#llx DS=%#llx\n",
CPU::x64::rdmsr(CPU::x64::MSR_FS_BASE), CPU::x64::rdmsr(CPU::x64::MSR_GS_BASE),
Frame->ss, Frame->cs, Frame->ds);
EHPrint("R8=%#lx R9=%#lx R10=%#lx R11=%#lx\n", Frame->r8, Frame->r9, Frame->r10, Frame->r11);
EHPrint("R12=%#lx R13=%#lx R14=%#lx R15=%#lx\n", Frame->r12, Frame->r13, Frame->r14, Frame->r15);
EHPrint("RAX=%#lx RBX=%#lx RCX=%#lx RDX=%#lx\n", Frame->rax, Frame->rbx, Frame->rcx, Frame->rdx);
EHPrint("RSI=%#lx RDI=%#lx RBP=%#lx RSP=%#lx\n", Frame->rsi, Frame->rdi, Frame->rbp, Frame->rsp);
EHPrint("RIP=%#lx RFL=%#lx INT=%#lx ERR=%#lx EFER=%#lx\n", Frame->rip, Frame->rflags.raw, Frame->InterruptNumber, Frame->ErrorCode, efer.raw);
EHPrint("CR0=%#lx CR2=%#lx CR3=%#lx CR4=%#lx CR8=%#lx\n", cr0.raw, cr2.raw, cr3.raw, cr4.raw, cr8.raw);
EHPrint("DR0=%#lx DR1=%#lx DR2=%#lx DR3=%#lx DR6=%#lx DR7=%#lx\n", dr0, dr1, dr2, dr3, dr6, dr7.raw);
EHPrint("R8=%#llx R9=%#llx R10=%#llx R11=%#llx\n", Frame->r8, Frame->r9, Frame->r10, Frame->r11);
EHPrint("R12=%#llx R13=%#llx R14=%#llx R15=%#llx\n", Frame->r12, Frame->r13, Frame->r14, Frame->r15);
EHPrint("RAX=%#llx RBX=%#llx RCX=%#llx RDX=%#llx\n", Frame->rax, Frame->rbx, Frame->rcx, Frame->rdx);
EHPrint("RSI=%#llx RDI=%#llx RBP=%#llx RSP=%#llx\n", Frame->rsi, Frame->rdi, Frame->rbp, Frame->rsp);
EHPrint("RIP=%#llx RFL=%#llx INT=%#llx ERR=%#llx EFER=%#llx\n", Frame->rip, Frame->rflags.raw, Frame->InterruptNumber, Frame->ErrorCode, efer.raw);
EHPrint("CR0=%#llx CR2=%#llx CR3=%#llx CR4=%#llx CR8=%#llx\n", cr0.raw, cr2.raw, cr3.raw, cr4.raw, cr8.raw);
EHPrint("DR0=%#llx DR1=%#llx DR2=%#llx DR3=%#llx DR6=%#llx DR7=%#llx\n", dr0, dr1, dr2, dr3, dr6, dr7.raw);
EHPrint("\eFC797BCR0: PE:%s MP:%s EM:%s TS:%s\n ET:%s NE:%s WP:%s AM:%s\n NW:%s CD:%s PG:%s\n R0:%#x R1:%#x R2:%#x\n",
cr0.PE ? "True " : "False", cr0.MP ? "True " : "False", cr0.EM ? "True " : "False", cr0.TS ? "True " : "False",
@ -234,10 +234,10 @@ namespace CrashHandler
cr0.NW ? "True " : "False", cr0.CD ? "True " : "False", cr0.PG ? "True " : "False",
cr0._reserved0, cr0._reserved1, cr0._reserved2);
EHPrint("\eFCBD79CR2: PFLA: %#lx\n",
EHPrint("\eFCBD79CR2: PFLA: %#llx\n",
cr2.PFLA);
EHPrint("\e79FC84CR3: PWT:%s PCD:%s PDBR:%#lx\n",
EHPrint("\e79FC84CR3: PWT:%s PCD:%s PDBR:%#llx\n",
cr3.PWT ? "True " : "False", cr3.PCD ? "True " : "False", cr3.PDBR);
EHPrint("\eBD79FCCR4: VME:%s PVI:%s TSD:%s DE:%s\n PSE:%s PAE:%s MCE:%s PGE:%s\n PCE:%s UMIP:%s OSFXSR:%s OSXMMEXCPT:%s\n LA57:%s VMXE:%s SMXE:%s PCIDE:%s\n OSXSAVE:%s SMEP:%s SMAP:%s PKE:%s\n R0:%#x R1:%#x R2:%#x\n",
@ -410,7 +410,7 @@ void StackFaultExceptionHandler(CPU::x64::TrapFrame *Frame)
break;
}
debug("external:%d table:%d idx:%#x", SelCode.External, SelCode.Table, SelCode.Idx);
sprintf_(descbuf, "Stack segment fault at address %#lx", Frame->rip);
sprintf_(descbuf, "Stack segment fault at address %#llx", Frame->rip);
CrashHandler::EHPrint(descbuf);
sprintf_(desc_ext, "External: %d", SelCode.External);
CrashHandler::EHPrint(desc_ext);
@ -452,7 +452,7 @@ void GeneralProtectionExceptionHandler(CPU::x64::TrapFrame *Frame)
// SET_PRINT_MID((char *)"System crashed!", FHeight(6));
// CurrentDisplay->ResetPrintColor();
// SET_PRINT_MID((char *)"More info about the exception:", FHeight(4));
// sprintf_(descbuf, "Kernel performed an illegal operation at address %#lx", RIP);
// sprintf_(descbuf, "Kernel performed an illegal operation at address %#llx", RIP);
// SET_PRINT_MID((char *)descbuf, FHeight(5));
// sprintf_(desc_ext, "External: %d", SelCode.External);
// SET_PRINT_MID((char *)desc_ext, FHeight(3));
@ -477,7 +477,7 @@ void PageFaultExceptionHandler(CPU::x64::TrapFrame *Frame)
staticbuffer(page_sgx);
CrashHandler::EHPrint("\eDD2920System crashed!\n\eFFFFFF");
sprintf_(ret_err, "An exception occurred at %#lx by %#lx\n", CPU::x64::readcr2().PFLA, Frame->rip);
sprintf_(ret_err, "An exception occurred at %#llx by %#llx\n", CPU::x64::readcr2().PFLA, Frame->rip);
CrashHandler::EHPrint(ret_err);
sprintf_(page_present, "Page: %s\n", params.P ? "Present" : "Not Present");
CrashHandler::EHPrint(page_present);

View File

@ -27,8 +27,9 @@ void tracepagetable(PageTable *pt)
{
for (int i = 0; i < 512; i++)
{
#if defined(__amd64__)
if (pt->Entries[i].Value.Present)
debug("Entry %03d: %x %x %x %x %x %x %x %x %x %x %x %p-%#lx", i,
debug("Entry %03d: %x %x %x %x %x %x %x %x %x %x %x %p-%#llx", i,
pt->Entries[i].Value.Present, pt->Entries[i].Value.ReadWrite,
pt->Entries[i].Value.UserSupervisor, pt->Entries[i].Value.WriteThrough,
pt->Entries[i].Value.CacheDisable, pt->Entries[i].Value.Accessed,
@ -36,16 +37,63 @@ void tracepagetable(PageTable *pt)
pt->Entries[i].Value.Global, pt->Entries[i].Value.PageAttributeTable,
pt->Entries[i].Value.ExecuteDisable, pt->Entries[i].GetAddress(),
pt->Entries[i].Value);
#elif defined(__i386__)
#elif defined(__aarch64__)
#endif
}
}
#endif
void InitializeMemoryManagement(BootInfo *Info)
{
for (uint64_t i = 0; i < Info->Memory.Entries; i++)
{
uint64_t Base = reinterpret_cast<uint64_t>(Info->Memory.Entry[i].BaseAddress);
uint64_t Length = Info->Memory.Entry[i].Length;
uint64_t End = Base + Length;
const char *Type = "Unknown";
switch (Info->Memory.Entry[i].Type)
{
case Usable:
Type = "Usable";
break;
case Reserved:
Type = "Reserved";
break;
case ACPIReclaimable:
Type = "ACPI Reclaimable";
break;
case ACPINVS:
Type = "ACPI NVS";
break;
case BadMemory:
Type = "Bad Memory";
break;
case BootloaderReclaimable:
Type = "Bootloader Reclaimable";
break;
case KernelAndModules:
Type = "Kernel and Modules";
break;
case Framebuffer:
Type = "Framebuffer";
break;
default:
break;
}
trace("%lld: %#016llx-%#016llx %s",
i,
Base,
End,
Type);
}
trace("Initializing Physical Memory Manager");
KernelAllocator = Physical();
KernelAllocator.Init(Info);
debug("Memory Info: %dMB / %dMB (%dMB reserved)",
debug("Memory Info: %lldMB / %lldMB (%lldMB reserved)",
TO_MB(KernelAllocator.GetUsedMemory()),
TO_MB(KernelAllocator.GetTotalMemory()),
TO_MB(KernelAllocator.GetReservedMemory()));
@ -207,8 +255,8 @@ void HeapFree(void *Address)
}
}
void *operator new(uint64_t Size) { return HeapMalloc(Size); }
void *operator new[](uint64_t Size) { return HeapMalloc(Size); }
void *operator new(size_t Size) { return HeapMalloc(Size); }
void *operator new[](size_t Size) { return HeapMalloc(Size); }
void operator delete(void *Pointer) { HeapFree(Pointer); }
void operator delete[](void *Pointer) { HeapFree(Pointer); }
void operator delete(void *Pointer, long unsigned int Size) { HeapFree(Pointer); }

View File

@ -237,27 +237,40 @@ namespace Memory
if (Info->Memory.Entry[i].Type == Usable)
if (Info->Memory.Entry[i].Length > LargestFreeMemorySegmentSize)
{
// We don't want to use 0 as a memory address.
if (Info->Memory.Entry[i].BaseAddress == nullptr)
continue;
LargestFreeMemorySegment = (void *)Info->Memory.Entry[i].BaseAddress;
LargestFreeMemorySegmentSize = Info->Memory.Entry[i].Length;
debug("Largest free memory segment: %p (%dKB)",
debug("Largest free memory segment: %llp (%lldMB)",
(void *)Info->Memory.Entry[i].BaseAddress,
TO_KB(Info->Memory.Entry[i].Length));
TO_MB(Info->Memory.Entry[i].Length));
}
TotalMemory = MemorySize;
FreeMemory = MemorySize;
uint64_t BitmapSize = ALIGN_UP((MemorySize / 0x1000) / 8, 0x1000);
trace("Initializing Bitmap (%p %dKB)", LargestFreeMemorySegment, TO_KB(BitmapSize));
if (LargestFreeMemorySegment == nullptr)
{
error("No free memory found!");
CPU::Stop();
}
uint64_t BitmapSize = ALIGN_UP((MemorySize / PAGE_SIZE) / 8, PAGE_SIZE);
trace("Initializing Bitmap at %llp-%llp (%lld Bytes)",
LargestFreeMemorySegment,
(void *)((uint64_t)LargestFreeMemorySegment + BitmapSize),
BitmapSize);
PageBitmap.Size = BitmapSize;
PageBitmap.Buffer = (uint8_t *)LargestFreeMemorySegment;
for (uint64_t i = 0; i < BitmapSize; i++)
*(uint8_t *)(PageBitmap.Buffer + i) = 0;
trace("Reserving pages...");
this->ReservePages(0, MemorySize / PAGE_SIZE + 1);
for (uint64_t i = 0; i < Info->Memory.Entries; i++)
if (Info->Memory.Entry[i].Type == Usable)
this->UnreservePages((void *)Info->Memory.Entry[i].BaseAddress, Info->Memory.Entry[i].Length / PAGE_SIZE + 1);
this->ReservePages(0, 0x100); // Reserve between 0 and 0x100000
this->ReservePages(0, 0x100); // Reserve between 0 and 0x100000.
this->LockPages(PageBitmap.Buffer, PageBitmap.Size / PAGE_SIZE + 1);
}

View File

@ -850,7 +850,7 @@ namespace PCI
{
DeviceConfig *NewDeviceConfig = (DeviceConfig *)((uint64_t)((ACPI::ACPI *)PowerManager->GetACPI())->MCFG + sizeof(ACPI::ACPI::MCFGHeader) + (sizeof(DeviceConfig) * t));
Memory::Virtual().Map((void *)NewDeviceConfig->BaseAddress, (void *)NewDeviceConfig->BaseAddress, Memory::PTFlag::RW);
trace("PCI Entry %d Address:%#lx BUS:%#lx-%#lx", t, NewDeviceConfig->BaseAddress,
trace("PCI Entry %d Address:%#llx BUS:%#llx-%#llx", t, NewDeviceConfig->BaseAddress,
NewDeviceConfig->StartBus, NewDeviceConfig->EndBus);
for (uint64_t Bus = NewDeviceConfig->StartBus; Bus < NewDeviceConfig->EndBus; Bus++)
EnumerateBus(NewDeviceConfig->BaseAddress, Bus);

View File

@ -57,7 +57,7 @@ namespace SymbolResolver
Symbols::Symbols(uint64_t Address)
{
debug("Solving symbols for address: %#lx", Address);
debug("Solving symbols for address: %#llx", Address);
Elf64_Ehdr *Header = (Elf64_Ehdr *)Address;
if (Header->e_ident[0] != 0x7F &&
Header->e_ident[1] != 'E' &&

View File

@ -5,9 +5,9 @@
extern "C" uint64_t SystemCallsHandler(SyscallsRegs *regs)
{
#if defined(__amd64__)
fixme("System call %ld", regs->rax);
fixme("System call %lld", regs->rax);
#elif defined(__i386__)
fixme("System call %ld", regs->eax);
fixme("System call %lld", regs->eax);
#elif defined(__aarch64__)
fixme("System call");
#endif

View File

@ -39,7 +39,7 @@ namespace UniversalAsynchronousReceiverTransmitter
{
static int once = 0;
if (!once++)
warn("Serial port %#lx is faulty.", Port);
warn("Serial port %#llx is faulty.", Port);
// serialports[Port] = false; // ignore for now
// return;
}