MCPcopy Create free account
hub / github.com/LemonOSProject/LemonOS / PageFaultHandler

Function PageFaultHandler

Kernel/src/arch/x86_64/paging.cpp:578–657  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

576 }
577
578 void PageFaultHandler(void*, regs64_t* regs)
579 {
580 asm("cli");
581 write_serial_n("Page Fault\r\n", 12);
582 Log::SetVideoConsole(nullptr);
583
584 int err_code = IDT::GetErrCode();
585
586 uint64_t faultAddress;
587 asm volatile("movq %%cr2, %0" : "=r" (faultAddress));
588
589 int present = !(err_code & 0x1); // Page not present
590 int rw = err_code & 0x2; // Attempted write to read only page
591 int us = err_code & 0x4; // Processor was in user-mode and tried to access kernel page
592 int reserved = err_code & 0x8; // Overwritten CPU-reserved bits of page entry
593 int id = err_code & 0x10; // Caused by an instruction fetch
594
595 if (present)
596 Log::Info("Page not present"); // Print fault to serial
597 if (rw)
598 Log::Info("Read Only");
599 if (us)
600 Log::Info("User mode process tried to access kernel memory");
601 if (reserved)
602 Log::Info("Reserved");
603 if (id)
604 Log::Info("instruction fetch");
605
606 Log::Info("RIP:");
607
608 Log::Info(regs->rip);
609
610 Log::Info("Process:");
611 Log::Info(Scheduler::GetCurrentProcess()->pid);
612
613 Log::Info("\r\nFault address: ");
614 Log::Info(faultAddress);
615
616 Log::Info("Register Dump: a: ");
617 Log::Write(regs->rax);
618 Log::Write(", b:");
619 Log::Write(regs->rbx);
620 Log::Write(", c:");
621 Log::Write(regs->rcx);
622 Log::Write(", d:");
623 Log::Write(regs->rdx);
624 Log::Write(", S:");
625 Log::Write(regs->rsi);
626 Log::Write(", D:");
627 Log::Write(regs->rdi);
628 Log::Write(", sp:");
629 Log::Write(regs->rsp);
630 Log::Write(", bp:");
631 Log::Write(regs->rbp);
632
633 if((regs->ss & 0x3)){
634 Log::Warning("Process %s crashed, PID: ", Scheduler::GetCurrentProcess()->name);
635 Log::Write(Scheduler::GetCurrentProcess()->pid);

Callers

nothing calls this directly

Calls 13

write_serial_nFunction · 0.85
SetVideoConsoleFunction · 0.85
GetErrCodeFunction · 0.85
InfoFunction · 0.85
GetCurrentProcessFunction · 0.85
WarningFunction · 0.85
UserPrintStackTraceFunction · 0.85
EndProcessFunction · 0.85
SendIPIFunction · 0.85
PrintStackTraceFunction · 0.85
KernelPanicFunction · 0.85
WriteFunction · 0.70

Tested by

no test coverage detected