docs/plan/debugger.md, milestones D0, D1 and D2. What they replace is the
ninety-second loop -- add a cprintf, rebuild, boot, read the serial log --
which answers exactly the one question you thought of before the rebuild.
The rule the shape follows: **it observes, it does not control.** No
stepping, no software breakpoints, no stopping the world. Half the bugs
this kernel produces are timing -- an interrupt storm, a level-triggered
line nobody acknowledges, a driver that faults only on its second start --
and every one of them disappears when you halt the machine to look. And
the layering rule, which is the circularity rule the display already has:
a debugger that needs lwIP cannot debug lwIP.
D0 -- symbols. tools/ksyms.pl turns the first link pass into a table the
second pass carries. Two passes is enough because .ksym is placed after
.text and .rodata, so carrying the table cannot move anything recorded in
it; the build lists both passes' text symbols and refuses to link if they
ever disagree, because a symbol table that is subtly wrong gives confident
false answers. ksym.c reads it through weak symbols, so pass one -- which
has no table -- still links. A crash chain now reads panic+0x5f <-
proc_sysrq_cmd+0x99 <- ... <- syscall+0xf6.
D1 -- a log that outlives the kernel. Every character through consputc()
lands in a byte ring with a timestamp per line, readable as /proc/kmsg.
It sits in a region carved before the page allocator gets anything, with a
magic header, so a kernel finds the previous kernel's log still there --
including the whole PSOGM report. panic.c's own comment said "there is no
core dump and no log to go back to"; that is no longer true.
The obvious place for it is the top of RAM beside the DMA arena. That
was the first attempt and the magic came back as ASCII on the reboot,
because the top of usable memory is exactly where the firmware makes its
own allocations. It lives above the kernel image instead, which is a
stable address across boots of the same build.
D2 -- the introspection registry. A subsystem publishes its state with one
line at the bottom of the file that owns the data:
dbg_register("dma", dma_dump, "the DMA arena's free extents");
Seven views -- dma, mem, proc, vma, claims, pci, sock -- reachable three
ways: sysrq 'g' (always, no process and no filesystem), /dev/dbg (echo a
line, cat the answer), and schdbg. The protocol is lines of text because
the same bytes have to work where nothing can allocate or frame a record,
and because a protocol you cannot drive by hand is one you cannot debug.
struct sbuf moved out of kernel/dev/procfs.c into include/sbuf.h rather
than being written a second time.
Several views exist because of a specific bug. `vma` is the table that
decides whether exit() frees a page, and the DMA ownership bug that made
this worth building was a block that was never in it.
Also fixed, found by building with clang for the first time in a while:
struct iovec was defined in both posix.h and socket.h, and every file that
included both was a redefinition. GCC 15 defaults to C23, which permits an
identical struct redeclaration, so the gcc path never noticed -- while
clang defaults to gnu17 and rejected it, breaking the one toolchain
shell.nix exists to provide and the UEFI loader requires. Both are now
guarded.
D3 (`whose <addr>`, the cross-subsystem ownership query) and D4 (hardware
watchpoints on DR0-DR3) are not built and are described honestly as
missing in the architecture doc's "What it does not do".
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
123 lines
4.4 KiB
Text
123 lines
4.4 KiB
Text
/* Simple linker script for the JOS kernel.
|
|
See the GNU ld 'info' manual ("info ld") to learn the syntax. */
|
|
|
|
/* OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") */
|
|
/* OUTPUT_FORMAT("elf64-x86-64", "elf64-x86-64", "elf64-x86-64") */
|
|
OUTPUT_FORMAT("elf64-x86-64")
|
|
OUTPUT_ARCH(i386:x86-64)
|
|
ENTRY(_start)
|
|
|
|
mboot_load_addr = 0x00100000;
|
|
|
|
SECTIONS
|
|
{
|
|
/* Link the kernel at this address: "." means the current address */
|
|
/* Must be equal to KERNLINK */
|
|
. = 0xFFFF800000100000;
|
|
|
|
PROVIDE(begin = .);
|
|
|
|
/* The .l* section names are the x86-64 psABI's "large" sections, and they
|
|
* are not optional decoration: with -mcmodel=large both LLVM and GCC 13+
|
|
* put nearly all code and data into .ltext/.lrodata/.ldata/.lbss rather
|
|
* than the classic names.
|
|
*
|
|
* A script that does not name them still links. The large sections become
|
|
* orphans placed after everything the script did name, which puts them past
|
|
* PROVIDE(end = .) -- so kinit1(end, ...) hands the page allocator a megabyte
|
|
* of live .lbss and the kernel destroys itself within a few instructions of
|
|
* its first printf. That is exactly how this was found: the boot printed one
|
|
* line and triple-faulted in a loop.
|
|
*
|
|
* Matched alongside their small counterparts so the layout is unchanged for
|
|
* a toolchain that does not use them. */
|
|
.text : AT(mboot_load_addr) {
|
|
*(.text .rela.text .stub .text.* .gnu.linkonce.t.*)
|
|
*(.ltext .ltext.*)
|
|
}
|
|
|
|
PROVIDE(etext = .); /* Define the 'etext' symbol to this value */
|
|
|
|
.rodata : {
|
|
*(.rodata .rodata.* .gnu.linkonce.r.*)
|
|
*(.lrodata .lrodata.*)
|
|
}
|
|
|
|
/* Thread-local storage.
|
|
*
|
|
* The kernel really does use TLS: `__thread struct cpu *cpu` and
|
|
* `__thread struct proc *proc` (kernel/arch/x86_64/vm.c), reached through
|
|
* FS_BASE, which seginit() points at kernel_fs_base.
|
|
*
|
|
* These output sections are what make the linker emit a PT_TLS segment, and
|
|
* without a PT_TLS the local-exec relocations have nothing to resolve
|
|
* against: lld left every access as a bare `movq %fs:0x0`, so `cpu` and
|
|
* `proc` both aliased the thread pointer itself and the kernel died a few
|
|
* instructions into seginit. GNU ld happened to place an orphan .tbss well
|
|
* enough to work; that was luck, not correctness.
|
|
*
|
|
* Order matters and is checked by more than C: trapasm.S hardcodes `proc` at
|
|
* %fs:(-8). With cpu at TLS offset 0 and proc at 8 in a 16-byte block, the
|
|
* local-exec offsets come out as -16 and -8, which is the layout that file
|
|
* assumes. */
|
|
.tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) }
|
|
.tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }
|
|
|
|
/* Adjust the address for the data segment to the next page */
|
|
. = ALIGN(0x1000);
|
|
|
|
/* Conventionally, Unix linkers provide pseudo-symbols
|
|
* etext, edata, and end, at the end of the text, data, and bss.
|
|
* For the kernel mapping, we need the address at the beginning
|
|
* of the data section, but that's not one of the conventional
|
|
* symbols, because the convention started before there was a
|
|
* read-only rodata section between text and data. */
|
|
PROVIDE(data = .);
|
|
|
|
/* The data segment */
|
|
.data : {
|
|
*(.data .data.*)
|
|
*(.ldata .ldata.*)
|
|
}
|
|
|
|
/* The kernel symbol table, generated by tools/ksyms.pl between the two link
|
|
* passes. See kernel/core/ksym.c.
|
|
*
|
|
* The position is the whole trick and it is not cosmetic: everything the
|
|
* table records is a *text* address, and .ksym sits after .text and .rodata,
|
|
* so adding several tens of kilobytes here cannot move a single entry in it.
|
|
* That is what makes two passes enough -- pass one is read to build the
|
|
* table, pass two carries it, and pass two's text is identical to pass one's.
|
|
* Put this before .text and the table describes a kernel that no longer
|
|
* exists, subtly, with every address off by the size of the table.
|
|
*
|
|
* It is inside the loaded image (before edata), not in .bss, because it has
|
|
* contents. mboot_load_end is computed from edata and would otherwise not
|
|
* cover it. */
|
|
. = ALIGN(8);
|
|
.ksym : {
|
|
*(.ksym .ksym.*)
|
|
}
|
|
|
|
. = ALIGN(0x1000);
|
|
|
|
PROVIDE(edata = .);
|
|
|
|
.bss : {
|
|
*(.bss .bss.*)
|
|
*(.lbss .lbss.*)
|
|
*(COMMON)
|
|
}
|
|
|
|
. = ALIGN(0x1000);
|
|
|
|
PROVIDE(end = .);
|
|
|
|
/DISCARD/ : {
|
|
*(.eh_frame .rela.eh_frame .note.GNU-stack)
|
|
}
|
|
}
|
|
|
|
mboot_load_end = mboot_load_addr + (edata - begin);
|
|
mboot_bss_end = mboot_load_addr + (end - begin);
|
|
mboot_entry_addr = mboot_load_addr + (mboot_entry - begin);
|