Internal Kernel Debugger
|
|
An Internal Kernel Debugger (IKD) is a debug shell but built into ones kernel, usually the exception handler.
Preexisting Tools
Minidbg
Minidbg is a “minimal, easy to integrate, multiplatform ANSI C debugger with serial interface” which works with AARCH64 and X86_64 devices. Minidbg can be easily implemented into a kernel with very little time or effort to actually implement it compared to what it can do. It does not have any dependencies other than the assembler you use (and a C compiler), and the architecture you target. It it also licensed under a permissive license.
Making Your Own IKD
If you wish to develop your own IKD, you will first need to have some form of input; this could be via a PS/2 keyboard or a serial port. The method does not matter as long as it works. The next thing you need to know is when to trigger the IKD. Possible methods to trigger the IKD could be CPU exceptions, serial interrupts, or anything else.
What Does an IKD Do?
The IKD can dump memory, dump registers, and read most critical parts of the system such as the GDT, IDT, kernel heap allocation info, the PMM, VMM, and so on. An IKD is usually able to dump anything and everything, including ACPI tables, however some may want some security limits (such as dumping CSPRNG state, or things which would cause an exception).
How might one implements an IKD
On many systems one may simply poll the keyboard and decode its outputs into strings for commands, parse them, and then run them.
Commands one may impement
One may wish to have a kernel which can dump only whats needed (usually for security, or stability), and others may wish to dump as much as possible. Though a common limit is to not allow the kernel debugger to dump secrets, and unmapped pages/invalid to access pages (if the kernel uses SMAP it must be disabled to access user pages else another fault will happen). Otherwise an IKD may want to implement the following few commands:
1. quit: Quits the IKD
2. clear: Clears the display (optional)
3. dumpmem: Dumps memory to display (XXD like output is recommended)
4. dumptable: Dumps an ACPI table (optional, only if you have/implement ACPI and AML, though it would be best to format this dump)
5. dumpregs: Dumps system registers at the time of the fault
Though this is a very minimal set of commands to work with, and one may wish to expand this, this is often a good base, stack tracing would also be another good choice to implement.
