Przeglądaj źródła

Add serial debug monitor (dbg): read/write memory and I/O ports

- dbg command (runs inline; blocks until 'q') reads ASCII hex commands over
  the serial link: r/w (memory), i/o (ports), h, q
- fix serial_puthex hex table (missing '6' and '7')
Johannes Zellner 1 miesiąc temu
rodzic
commit
9dc8b78116
6 zmienionych plików z 257 dodań i 5 usunięć
  1. 20 1
      AGENTS.md
  2. 5 2
      Makefile
  3. 204 0
      dbg.c
  4. 8 0
      dbg.h
  5. 1 1
      serial.c
  6. 19 1
      terminal.c

+ 20 - 1
AGENTS.md

@@ -74,7 +74,7 @@ make clean
 
 `hello`, `reboot`, `poweroff`, `ls`, `cat <file>`, `run <file>`, `space`, `clock`,
 `colors`, `display`, `cpuinfo`, `mouseinfo`, `power [info|set …]`, `drive <0|1>`,
-`ps`, `kill <pid>`, `help`, `clear`, `serial test|recv`, `kexec`.
+`ps`, `kill <pid>`, `help`, `clear`, `serial test|recv`, `kexec`, `dbg`.
 
 ## Serial hot-reload (kexec) workflow
 
@@ -123,6 +123,25 @@ lives inside the overwritten kernel region), so it's safe to edit any file,
 including `vesa_stage.S`/`boot.S` layout — but the video *mode* itself is only
 re-applied on a full floppy/USB boot (C-light reuses the current LFB).
 
+## Serial debug monitor (`dbg`)
+
+A ring-0 debugger driven over the serial link. On the PC type `dbg`, then drive
+commands from the host with `make monitor` (picocom):
+
+```
+r <addr> <len>          read len bytes at addr -> hex dump
+w <addr> <b0> [b1 ...]  write bytes at addr
+i <port>                inb (read I/O port)
+o <port> <val>          outb (write I/O port)
+h                       help
+q                       quit
+```
+
+The monitor runs inline (blocks the terminal), so it owns the serial ring buffer
+and doesn't conflict with the kexec auto-detect. Reading arbitrary physical
+memory is safe (no paging); writing can corrupt the kernel, so the usual loop is
+inspect → `q` → `make dev` a fresh build.
+
 ## Conventions & gotchas
 
 - **Freestanding C** — no libc, no `malloc`, no `printf`. Use `io_putstr` /

+ 5 - 2
Makefile

@@ -119,6 +119,9 @@ kexec.o: kexec.c kexec.h io.h serial.h
 kexec_tramp.o: kexec_tramp.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
+dbg.o: dbg.c dbg.h serial.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 boot.elf: boot.o
 	$(LD) $(BOOT_LDFLAGS) -o $@ $<
 
@@ -134,7 +137,7 @@ switch.o: switch.S
 process.o: process.c process.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o idle.o kexec.o kexec_tramp.o
+kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o disk.o fat.o ls.o cat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o idle.o kexec.o kexec_tramp.o dbg.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -283,4 +286,4 @@ dev: kernel.bin kexec_send
 	./kexec_send $(SERIAL_DEV) $(SERIAL_BAUD) $$entry kernel.bin
 
 clean:
-	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o idle.o kexec.o kexec_tramp.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send
+	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o poweroff.o fat.o ls.o cat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o mouse.o mousecmd.o idle.o kexec.o kexec_tramp.o dbg.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send

+ 204 - 0
dbg.c

@@ -0,0 +1,204 @@
+/* Serial debug monitor. Entered via the `dbg` command and driven from the host
+ * with `make monitor` (picocom). ASCII commands, newline-terminated, hex args:
+ *
+ *   r <addr> <len>           read len bytes at addr -> hex dump
+ *   w <addr> <b0> [b1 ...]   write bytes at addr
+ *   i <port>                 inb (read I/O port)
+ *   o <port> <val>           outb (write I/O port)
+ *   h                        help
+ *   q                        quit
+ *
+ * Runs inline (blocks the terminal), so it owns the serial ring buffer and
+ * doesn't conflict with the kexec auto-detect in the terminal idle loop.
+ */
+
+#include "dbg.h"
+#include "serial.h"
+
+#define DBG_LINE_MAX 64
+#define DBG_NARGS    32
+#define DBG_READ_MAX 256
+
+static inline unsigned char dbg_inb(unsigned short port)
+{
+	unsigned char v;
+	__asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
+	return v;
+}
+
+static inline void dbg_outb(unsigned short port, unsigned char v)
+{
+	__asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
+}
+
+static void dbg_putbyte(unsigned char b)
+{
+	static const char hex[] = "0123456789ABCDEF";
+	serial_putc(SERIAL_COM1, hex[b >> 4]);
+	serial_putc(SERIAL_COM1, hex[b & 0xF]);
+}
+
+/* Block until a byte arrives (echoing it so picocom works without local echo). */
+static void dbg_getc(unsigned char *c)
+{
+	for (;;) {
+		if (!serial_ring_wait(0xFFFFFFFFu, c))
+			continue;              /* timeout (effectively never) */
+		if (*c == '\r')
+			;                      /* ignore bare CR */
+		else if (*c == '\n')
+			serial_puts(SERIAL_COM1, "\n");
+		else
+			serial_putc(SERIAL_COM1, (char)*c);
+		return;
+	}
+}
+
+static void dbg_read_line(char *buf, unsigned int max)
+{
+	unsigned int i = 0;
+	unsigned char c;
+	for (;;) {
+		dbg_getc(&c);
+		if (c == '\n' || c == '\r')
+			break;
+		if (i + 1 < max)
+			buf[i++] = (char)c;
+	}
+	buf[i] = '\0';
+}
+
+/* Parse a hex number at *s (optional 0x prefix); advance *s past it. */
+static int dbg_parse_hex(const char **s, unsigned int *out)
+{
+	const char *p = *s;
+	unsigned int v = 0;
+	int digits = 0;
+
+	if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
+		p += 2;
+	for (;; p++) {
+		unsigned int d;
+		char c = *p;
+		if (c >= '0' && c <= '9')       d = (unsigned int)(c - '0');
+		else if (c >= 'a' && c <= 'f')  d = (unsigned int)(c - 'a' + 10);
+		else if (c >= 'A' && c <= 'F')  d = (unsigned int)(c - 'A' + 10);
+		else break;
+		v = (v << 4) | d;
+		digits++;
+	}
+	if (!digits)
+		return 0;
+	*out = v;
+	*s = p;
+	return 1;
+}
+
+/* Returns 1 to quit. */
+static int dbg_command(const char *line)
+{
+	const char *p = line;
+	unsigned int args[DBG_NARGS];
+	unsigned int nargs = 0;
+	char cmd;
+
+	while (*p == ' ')
+		p++;
+	if (!*p)
+		return 0;
+	cmd = *p++;
+
+	while (nargs < DBG_NARGS) {
+		while (*p == ' ')
+			p++;
+		if (!*p)
+			break;
+		if (!dbg_parse_hex(&p, &args[nargs]))
+			break;
+		nargs++;
+	}
+
+	switch (cmd) {
+	case 'r':
+		if (nargs < 2) {
+			serial_puts(SERIAL_COM1, "usage: r <addr> <len>\n");
+		} else {
+			unsigned int addr = args[0];
+			unsigned int len = args[1];
+			unsigned int i;
+			if (len > DBG_READ_MAX)
+				len = DBG_READ_MAX;
+			serial_puthex(SERIAL_COM1, addr);
+			serial_putc(SERIAL_COM1, ':');
+			for (i = 0; i < len; i++) {
+				serial_putc(SERIAL_COM1, ' ');
+				dbg_putbyte(((volatile unsigned char *)addr)[i]);
+			}
+			serial_putc(SERIAL_COM1, '\n');
+		}
+		return 0;
+
+	case 'w':
+		if (nargs < 2) {
+			serial_puts(SERIAL_COM1, "usage: w <addr> <b0> [b1 ...]\n");
+		} else {
+			unsigned int addr = args[0];
+			unsigned int i;
+			for (i = 1; i < nargs; i++)
+				((volatile unsigned char *)addr)[i - 1] = (unsigned char)args[i];
+			serial_puts(SERIAL_COM1, "ok\n");
+		}
+		return 0;
+
+	case 'i':
+		if (nargs < 1) {
+			serial_puts(SERIAL_COM1, "usage: i <port>\n");
+		} else {
+			serial_puthex(SERIAL_COM1, args[0]);
+			serial_puts(SERIAL_COM1, " = ");
+			dbg_putbyte(dbg_inb((unsigned short)args[0]));
+			serial_putc(SERIAL_COM1, '\n');
+		}
+		return 0;
+
+	case 'o':
+		if (nargs < 2) {
+			serial_puts(SERIAL_COM1, "usage: o <port> <val>\n");
+		} else {
+			dbg_outb((unsigned short)args[0], (unsigned char)args[1]);
+			serial_puts(SERIAL_COM1, "ok\n");
+		}
+		return 0;
+
+	case 'h':
+		serial_puts(SERIAL_COM1,
+			"r <addr> <len>   read memory (hex dump)\n"
+			"w <addr> <b>...  write bytes\n"
+			"i <port>         inb\n"
+			"o <port> <val>   outb\n"
+			"h                help\n"
+			"q                quit\n");
+		return 0;
+
+	case 'q':
+		return 1;
+
+	default:
+		serial_puts(SERIAL_COM1, "unknown cmd (h for help)\n");
+		return 0;
+	}
+}
+
+void program_dbg_main(void)
+{
+	char line[DBG_LINE_MAX];
+
+	serial_puts(SERIAL_COM1, "\ndbg: ready (h for help)\n");
+	for (;;) {
+		serial_puts(SERIAL_COM1, "dbg> ");
+		dbg_read_line(line, DBG_LINE_MAX);
+		if (dbg_command(line))
+			break;
+	}
+	serial_puts(SERIAL_COM1, "dbg: bye\n");
+}

+ 8 - 0
dbg.h

@@ -0,0 +1,8 @@
+/* Serial debug monitor: read/write memory and I/O ports over the serial link.
+ * Entered via the `dbg` command (runs inline; blocks until 'q'). */
+#ifndef DBG_H
+#define DBG_H
+
+void program_dbg_main(void);
+
+#endif

+ 1 - 1
serial.c

@@ -32,7 +32,7 @@ void serial_puts(unsigned int port, const char *s)
 
 void serial_puthex(unsigned int port, unsigned int n)
 {
-	static const char hex[] = "01234589ABCDEF";
+	static const char hex[] = "0123456789ABCDEF";
 	int i;
 	serial_puts(port, "0x");
 	for (i = 28; i >= 0; i -= 4)

+ 19 - 1
terminal.c

@@ -2,6 +2,7 @@
    Commands are spawned as separate processes for preemptive multitasking. */
 
 #include "io.h"
+#include "dbg.h"
 #include "fbtext.h"
 #include "idle.h"
 #include "kexec.h"
@@ -404,6 +405,21 @@ static int is_cmd_kexec(const char *line, unsigned int line_len)
 	return (i == line_len);
 }
 
+static int is_cmd_dbg(const char *line, unsigned int line_len)
+{
+	unsigned int i = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 3 > line_len)
+		return 0;
+	if (line[i] != 'd' || line[i+1] != 'b' || line[i+2] != 'g')
+		return 0;
+	i += 3;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
 static int is_cmd_serial(const char *line, unsigned int line_len, char *arg_buf, unsigned int buf_size)
 {
 	unsigned int i = 0, j = 0;
@@ -682,6 +698,8 @@ void terminal_run(void)
 								io_putstr("error: too many processes\n");
 							}
 						}
+					} else if (is_cmd_dbg(line, line_len)) {
+						program_dbg_main();
 					} else if (is_cmd_help(line, line_len)) {
 						io_putstr("hello     reboot    poweroff\n");
 						io_putstr("ls        cat <file>\n");
@@ -692,7 +710,7 @@ void terminal_run(void)
 						io_putstr("power [info|set <low|balanced|max>]\n");
 						io_putstr("drive <0|1>\n");
 					io_putstr("ps        kill <pid>\n");
-					io_putstr("kexec     serial test|recv  clear\n");
+					io_putstr("kexec     dbg       serial test|recv  clear\n");
 				} else if (is_cmd_clear(line, line_len)) {
 					io_clear_screen();
 					draw_header_row();