Jelajahi Sumber

Add shell tab completion; grow the kernel region to 0x44000

terminal.c: Tab completes the token at the cursor.  The first token
completes against the command names derived from the help table (a
unique match inserts the name + space; several extend to the longest
common prefix, or are listed below the line and the prompt is redrawn).
Argument tokens complete as VFS paths: the text up to the last '/'
selects the directory (the mount list at '/', otherwise vfs_resolve +
fat_resolve_dir on the cwd-joined path), entries match
case-insensitively, directories get a '/' suffix and files a trailing
space.  Ambiguous sets print as a wrapped list below the line.

Kernel region: the completion code pushed _end past 0x42000, so grow
by another 0x2000 -- bound 0x44000, stack 0x44800, API 0x44F00, user
load 0x45000 (kernel.ld, vesa_stage.S, kexec_tramp.S, tss.c, process.c,
run.c, serial_recv.c, run_api.h, user.ld, bootstrap.c, comments in
kexec.*, colors.c, radeon.c, uhci.c, netcmd.c).  Kexec staging and the
host-side netkexec_send cap 0x3A200 -> 0x3C200; PROGRAM_LOAD_MAX is
clamped to 0x9000 so a program cannot run into the 0x50000 stack pool.

AGENTS.md: tab completion and the new addresses.

Verified on QEMU with a USB keyboard: command completion, absolute and
relative path completion, mount-level completion, ambiguous lists
(ls/lsusb/lspci, dir contents), and running the completed commands.
Johannes Zellner 1 bulan lalu
induk
melakukan
e272babce4
19 mengubah file dengan 408 tambahan dan 37 penghapusan
  1. 10 9
      AGENTS.md
  2. 5 5
      bootstrap.c
  3. 1 1
      colors.c
  4. 1 1
      kernel.ld
  5. 1 1
      kexec.c
  6. 2 2
      kexec.h
  7. 1 1
      kexec_tramp.S
  8. 1 1
      netcmd.c
  9. 1 1
      netkexec_send.c
  10. 1 1
      process.c
  11. 1 1
      radeon.c
  12. 4 4
      run.c
  13. 1 1
      run_api.h
  14. 3 3
      serial_recv.c
  15. 370 0
      terminal.c
  16. 1 1
      tss.c
  17. 1 1
      uhci.c
  18. 2 2
      user.ld
  19. 1 1
      vesa_stage.S

+ 10 - 9
AGENTS.md

@@ -47,12 +47,12 @@ make clean
 | `0x1000`  | E820 memory map (`struct e820_map`, written by `vesa_stage.S`, parsed by `mm.c`) |
 | `0x3000`  | UHCI frame list (1024 × 4 B, 4 KB-aligned; `uhci.c`) |
 | `0x4000`  | ISA DMA buffer, one sector (`disk.c`) |
-| `0x42000` | Kernel upper bound (`kernel.ld` ASSERT) |
-| `0x42800` | Protected-mode stack top (`vesa_stage.S`, `kexec_tramp.S`) |
-| `0x42F00` | Program API struct (`PROGRAM_API_ADDR`, `run_api.h`) |
-| `0x43000` | User program load address (`run.c`, `user.ld`) |
+| `0x44000` | Kernel upper bound (`kernel.ld` ASSERT) |
+| `0x44800` | Protected-mode stack top (`vesa_stage.S`, `kexec_tramp.S`) |
+| `0x44F00` | Program API struct (`PROGRAM_API_ADDR`, `run_api.h`) |
+| `0x45000` | User program load address (`run.c`, `user.ld`) |
 | `0x50000` | Fallback process stack pool (8 × 4 KB; primary stacks come from the frame allocator, ≥ `0x900000`, see `process.c`/`mm.c`) |
-| `0x58000` | kexec staging buffer (received kernel image, up to 0x3A200 B; ends below the VGA window) |
+| `0x58000` | kexec staging buffer (received kernel image, up to 0x3C200 B; ends below the VGA window) |
 | `0x100000`| VGA shadow buffer (`VGA_SHADOW_BASE`, `vga.h`) |
 
 ## Key files
@@ -94,8 +94,9 @@ make clean
 - **On-screen debug**: `kdbg.c/h` — framebuffer debug overlay (live status rows,
   heartbeat, boot info rows, exception banner) for machines without serial; see
   the kdbg section below.
-- **Shell**: `terminal.c` — history, line editing, command dispatch (hand-rolled
-  `is_cmd_*` parsers). Runs in a window (`term_run(struct term *)`); reads keys
+- **Shell**: `terminal.c` — history, line editing (Tab completes command
+  names from the help table and VFS paths via `fat_list_dir`), command
+  dispatch (hand-rolled `is_cmd_*` parsers). Runs in a window (`term_run(struct term *)`); reads keys
   from its per-terminal queue (filled by the WM).
 - **Filesystem**: `fat.c` (FAT12/16/32 read/write, device-parameterized),
   `vfs.c` (mounts every FAT device under `/`), `fatdetect.c`/`fatformat.c`,
@@ -147,7 +148,7 @@ make clean
   scan generation counter.
 - **Power/idle**: `idle.c` — `mwait` C-states, `hlt` fallback.
 - **User programs**: `run.c`, `run_api.h`, `prog.c`, `space.c` — raw binaries at
-  `0x43000`, API function-pointer struct at `0x42F00`.  `prog`/`space` are
+  `0x45000`, API function-pointer struct at `0x44F00`.  `prog`/`space` are
   **embedded in the kernel image** (`prog_embed.o`/`space_embed.o`, like
   `hello.elf`), so `make dev`/`make net-dev` update them too and a board with
   no readable filesystem can still run them: `run <name>` and the `prog`/
@@ -477,7 +478,7 @@ Caveats:
 
 - Pixels are written directly to the LFB; WM full redraws overwrite them but
   the 1 Hz refresh brings them back.
-- Info rows cost ~0.5 KB BSS; the kernel must stay under the `0x42000`
+- Info rows cost ~0.5 KB BSS; the kernel must stay under the `0x44000`
   boundary (`kernel.ld` ASSERT).
 
 ## Conventions & gotchas

+ 5 - 5
bootstrap.c

@@ -3,8 +3,8 @@
  * full hardware access: it switches COM1 to 115200, receives a new kernel image,
  * copies it over 0x7E00, and jumps to its kernel_main.
  *
- * Self-contained: no kernel symbols, no run_api dependency. We live at 0x43000
- * (outside the 0x7E00..0x42000 kernel region), so we can overwrite the kernel
+ * Self-contained: no kernel symbols, no run_api dependency. We live at 0x45000
+ * (outside the 0x7E00..0x44000 kernel region), so we can overwrite the kernel
  * directly from our own code without a relocated trampoline.
  *
  * Wire protocol (host -> us), after we emit 'R':
@@ -18,9 +18,9 @@
 #define UART_BASE        0x3F8
 #define STAGE_ADDR       0x58000
 #define KERNEL_LOAD_ADDR 0x7E00
-#define BOOT_STACK       0x42800
+#define BOOT_STACK       0x44800
 #define MAGIC            0x4345584B /* "KXEC" little-endian */
-#define MAX_SIZE         0x3A200
+#define MAX_SIZE         0x3C200
 
 static inline void outb(unsigned short port, unsigned char v)
 {
@@ -148,7 +148,7 @@ void _start(void)
 	}
 
 	/* Copy staged kernel over 0x7E00, then jump to its kernel_main.
-	 * We live at 0x43000, outside the overwritten region, so this is safe. */
+	 * We live at 0x45000, outside the overwritten region, so this is safe. */
 	setup_flat_gdt();
 	for (i = 0; i < size; i++)
 		((volatile unsigned char *)KERNEL_LOAD_ADDR)[i] =

+ 1 - 1
colors.c

@@ -16,7 +16,7 @@
 /* Precomputed hue-to-RGB lookup table. Packed as 0x00BBGGRR for fast 32-bit load.
  * The hue cycle is RAINBOW_FULL entries, but the table is decimated by
  * LUT_STEP: a 4-hue step is ~1.5% of a color segment and invisible, and the
- * smaller table keeps the kernel image under the 0x42000 memory-bound. */
+ * smaller table keeps the kernel image under the 0x44000 memory-bound. */
 #define LUT_STEP      4
 #define LUT_SIZE      (RAINBOW_FULL / LUT_STEP)
 

+ 1 - 1
kernel.ld

@@ -17,4 +17,4 @@ SECTIONS
 	}
 	_end = .;
 }
-ASSERT(_end <= 0x42000, "Kernel too large would overwrite stack/DMA at 0x42000");
+ASSERT(_end <= 0x44000, "Kernel too large would overwrite stack/DMA at 0x44000");

+ 1 - 1
kexec.c

@@ -5,7 +5,7 @@
  * LFB are still valid) and jump straight to the new kernel's kernel_main,
  * which re-inits IDT/PIC/PIT/mouse/process/VGA-shadow/terminal from scratch.
  *
- * Because this code lives at 0x7E00..0x42000 (inside the region we overwrite),
+ * Because this code lives at 0x7E00..0x44000 (inside the region we overwrite),
  * the final "copy image to 0x7E00 and jump" is done by a small
  * position-independent trampoline relocated to 0x800 (below the kernel).
  *

+ 2 - 2
kexec.h

@@ -5,10 +5,10 @@
 void program_kexec_main(void);
 
 /* Staging buffer for a received kernel image, and the maximum image size
- * (the kernel region 0x7E00..0x42000).  Below the VGA window; see AGENTS.md. */
+ * (the kernel region 0x7E00..0x44000).  Below the VGA window; see AGENTS.md. */
 #define KEXEC_STAGE 0x58000
 #define KEXEC_LOAD  0x7E00
-#define KEXEC_MAX   0x3A200
+#define KEXEC_MAX   0x3C200
 
 /* Copy the staged image (KEXEC_STAGE, `size` bytes) over the kernel region,
  * relocate the trampoline and jump to `entry`.  Never returns. */

+ 1 - 1
kexec_tramp.S

@@ -66,7 +66,7 @@ kexec_trampoline:
 	 * this the jump below can execute stale instructions. */
 	wbinvd
 
-	movl  $0x42800, %esp
+	movl  $0x44800, %esp
 	jmp   *%edx
 
 .globl kexec_trampoline_end

+ 1 - 1
netcmd.c

@@ -10,7 +10,7 @@
 
 /* Shared frame buffers (file scope, not stack: the 4 KB process stack can't
    hold a full-size frame, and keeping them out of BSS-as-much-as-possible
-   avoids bloating the kernel past its 0x42000 limit). Max Ethernet frame is
+   avoids bloating the kernel past its 0x44000 limit). Max Ethernet frame is
    1518 bytes (14 header + 1500 payload + 4 VLAN). */
 #define NET_FRAME_MAX 1518
 static unsigned char net_rx_buf[NET_FRAME_MAX];

+ 1 - 1
netkexec_send.c

@@ -97,7 +97,7 @@ int main(int argc, char **argv)
 	fseek(f, 0, SEEK_END);
 	size = ftell(f);
 	fseek(f, 0, SEEK_SET);
-	if (size <= 0 || size > 0x38200) {
+	if (size <= 0 || size > 0x3C200) {
 		fprintf(stderr, "netkexec_send: bad size %ld\n", size);
 		fclose(f);
 		return 1;

+ 1 - 1
process.c

@@ -118,7 +118,7 @@ void process_init(void)
 	procs[0].screen     = (void *)0;
 	procs[0].stack_base = 0;  /* bootstrap stack, never freed */
 	procs[0].cr3        = paging_current_cr3();
-	procs[0].kstack_top = 0x42800;  /* bootstrap stack top */
+	procs[0].kstack_top = 0x44800;  /* bootstrap stack top */
 	name_copy(procs[0].name, "wm");
 	current_pid = 0;
 }

+ 1 - 1
radeon.c

@@ -17,7 +17,7 @@
  *  - Cursor mode is RADEON_CRTC_CUR_MODE_24BPP (2) in bits 22:20.
  *
  * NOTE: the bitmap is written straight into VRAM.  Do not use a local
- * [16 KB] array -- the kernel stack is only 2 KB (0x42800..0x42000) and a
+ * [16 KB] array -- the kernel stack is only 2 KB (0x44800..0x44000) and a
  * 16 KB stack allocation overflows it.
  */
 

+ 4 - 4
run.c

@@ -1,4 +1,4 @@
-/* run command: load raw binary from a FAT filesystem to 0x43000 and execute.
+/* run command: load raw binary from a FAT filesystem to 0x45000 and execute.
  * Usage: run /<device>/<file>. */
 
 #include "fat.h"
@@ -10,8 +10,8 @@
 #include "vga.h"
 #include "vfs.h"
 
-#define PROGRAM_LOAD_ADDR 0x43000
-#define PROGRAM_LOAD_MAX  0x10000
+#define PROGRAM_LOAD_ADDR 0x45000
+#define PROGRAM_LOAD_MAX  0x9000   /* up to 0x4E000, below the 0x50000 stack pool */
 
 /* User programs embedded in the kernel image (like hello.elf): they travel
  * with `make dev`/`make net-dev`, so a board with no readable filesystem can
@@ -147,7 +147,7 @@ void program_run_main(const char *filename)
 	((void (*)(void))PROGRAM_LOAD_ADDR)();
 }
 
-/* run serial: execute the binary already loaded at 0x43000 via serial_recv. */
+/* run serial: execute the binary already loaded at 0x45000 via serial_recv. */
 
 void program_run_serial_main(const char *arg)
 {

+ 1 - 1
run_api.h

@@ -3,7 +3,7 @@
 #ifndef RUN_API_H
 #define RUN_API_H
 
-#define PROGRAM_API_ADDR 0x42F00
+#define PROGRAM_API_ADDR 0x44F00
 #define IO_KEY_ESC       0x1B
 #define IO_KEY_SPACE     0x20
 #define IO_KEY_LEFT      0x100

+ 3 - 3
serial_recv.c

@@ -1,11 +1,11 @@
-/* Serial receive command: receives a binary via serial into 0x43000. */
+/* Serial receive command: receives a binary via serial into 0x45000. */
 
 #include "io.h"
 #include "serial.h"
 #include "bin_recv.h"
 
-#define PROGRAM_LOAD_ADDR 0x43000
-#define PROGRAM_LOAD_MAX  0x10000
+#define PROGRAM_LOAD_ADDR 0x45000
+#define PROGRAM_LOAD_MAX  0x9000   /* up to 0x4E000, below the 0x50000 stack pool */
 
 unsigned int serial_recv_size;
 

+ 370 - 0
terminal.c

@@ -1038,6 +1038,374 @@ static void history_load_into_line(struct term *t, unsigned int browse)
 	redraw_line_clear(t->line, len, len, t->line_start, old_len > len ? old_len : len);
 }
 
+/* ---- Tab completion ---- */
+
+/* Command names come from the help table ("ls [path]" -> "ls"). */
+static unsigned int help_name_len(const char *s)
+{
+	unsigned int n = 0;
+	while (s[n] && s[n] != ' ' && s[n] != '[' && s[n] != '<' && s[n] != '|')
+		n++;
+	return n;
+}
+
+static char lower_ch(char c)
+{
+	return (c >= 'A' && c <= 'Z') ? (char)(c + 32) : c;
+}
+
+static int prefix_ci(const char *s, const char *prefix, unsigned int plen)
+{
+	unsigned int i;
+	for (i = 0; i < plen; i++)
+		if (lower_ch(s[i]) != lower_ch(prefix[i]))
+			return 0;
+	return 1;
+}
+
+/* "HELLO   TXT" -> "hello.txt" (a completion-ready 8.3 display name). */
+static void tab_display_name(const char *n, char *out)
+{
+	unsigned int bl = 0, el = 0, i, j = 0;
+
+	for (i = 0; i < 8 && n[i] && n[i] != ' '; i++)
+		bl = i + 1;
+	for (i = 8; i < 11 && n[i] && n[i] != ' '; i++)
+		el = i - 8 + 1;
+	for (i = 0; i < bl; i++)
+		out[j++] = lower_ch(n[i]);
+	if (el) {
+		out[j++] = '.';
+		for (i = 0; i < el; i++)
+			out[j++] = lower_ch(n[8 + i]);
+	}
+	out[j] = '\0';
+}
+
+/* Shared completion state (fat_list_dir takes no context pointer). */
+static char tab_prefix[16];
+static unsigned int tab_prefix_len;
+static unsigned int tab_count;
+static unsigned int tab_lcp_len;
+static char tab_lcp[16];
+static int tab_single_dir;
+static fat_fs_t *tab_fs;
+static unsigned int tab_dir_cluster;
+static unsigned int tab_col;
+static unsigned int tab_width;
+
+static void tab_collect_cb(const char *name, int is_dir, unsigned int size)
+{
+	char disp[16];
+	unsigned int i, n;
+
+	(void)size;
+	if (name[0] == '.')                 /* "." / ".." */
+		return;
+	tab_display_name(name, disp);
+	if (!prefix_ci(disp, tab_prefix, tab_prefix_len))
+		return;
+	n = 0;
+	while (disp[n])
+		n++;
+	if (tab_count == 0) {
+		for (i = 0; i < n; i++)
+			tab_lcp[i] = disp[i];
+		tab_lcp_len = n;
+		tab_single_dir = is_dir;
+	} else {
+		i = 0;
+		while (i < tab_lcp_len && i < n && tab_lcp[i] == disp[i])
+			i++;
+		tab_lcp_len = i;
+	}
+	tab_count++;
+}
+
+static void tab_print_one(const char *name, int is_dir)
+{
+	unsigned int n = 0, w;
+
+	while (name[n])
+		n++;
+	w = n + (is_dir ? 1 : 0);
+	if (tab_col > 0 && tab_col + 2 + w > tab_width) {
+		io_putchar('\n');
+		tab_col = 0;
+	}
+	if (tab_col > 0) {
+		io_putstr("  ");
+		tab_col += 2;
+	}
+	io_putstr(name);
+	if (is_dir)
+		io_putchar('/');
+	tab_col += w;
+}
+
+static void tab_print_cb(const char *name, int is_dir, unsigned int size)
+{
+	char disp[16];
+
+	(void)size;
+	if (name[0] == '.')
+		return;
+	tab_display_name(name, disp);
+	if (!prefix_ci(disp, tab_prefix, tab_prefix_len))
+		return;
+	tab_print_one(disp, is_dir);
+}
+
+static int tab_scan_dir(const char *abs)
+{
+	fat_fs_t *fs;
+	const char *fs_path;
+
+	if (vfs_resolve(abs, &fs, &fs_path) != 0)
+		return 0;
+	if (fat_resolve_dir(fs, fs_path, &tab_dir_cluster) != 0)
+		return 0;
+	tab_fs = fs;
+	tab_count = 0;
+	tab_lcp_len = 0;
+	fat_list_dir(fs, tab_dir_cluster, tab_collect_cb);
+	return (int)tab_count;
+}
+
+static int tab_scan_mounts(void)
+{
+	unsigned int i;
+
+	tab_count = 0;
+	tab_lcp_len = 0;
+	for (i = 0; i < vfs_mount_count; i++) {
+		const char *name = vfs_mounts[i].name;
+		unsigned int n = 0, j;
+
+		while (name[n])
+			n++;
+		if (!prefix_ci(name, tab_prefix, tab_prefix_len))
+			continue;
+		if (tab_count == 0) {
+			for (j = 0; j < n; j++)
+				tab_lcp[j] = name[j];
+			tab_lcp_len = n;
+		} else {
+			j = 0;
+			while (j < tab_lcp_len && j < n && tab_lcp[j] == name[j])
+				j++;
+			tab_lcp_len = j;
+		}
+		tab_count++;
+	}
+	return (int)tab_count;
+}
+
+/* Replace line[start,end) with ins, keep the tail and redraw. */
+static void term_replace(struct term *t, unsigned int start, unsigned int end,
+			 const char *ins)
+{
+	unsigned int inslen = 0, tail, i, old_len = t->line_len;
+
+	while (ins[inslen])
+		inslen++;
+	tail = old_len - end;
+	if (start + inslen + tail >= LINE_MAX)
+		return;
+	for (i = 0; i < tail; i++)
+		t->line[start + inslen + i] = t->line[end + i];
+	for (i = 0; i < inslen; i++)
+		t->line[start + i] = ins[i];
+	t->line_len = start + inslen + tail;
+	t->line[t->line_len] = '\0';
+	t->line_pos = start + inslen;
+	redraw_line_clear(t->line, t->line_len, t->line_pos, t->line_start,
+			  old_len > t->line_len ? old_len : t->line_len);
+}
+
+/* List the ambiguous matches below the line, then redraw prompt + line. */
+static void term_tab_ambiguous(struct term *t, int mounts)
+{
+	unsigned int i;
+
+	io_putchar('\n');
+	tab_col = 0;
+	tab_width = t->screen.cols;
+	if (mounts) {
+		for (i = 0; i < vfs_mount_count; i++) {
+			const char *name = vfs_mounts[i].name;
+			if (prefix_ci(name, tab_prefix, tab_prefix_len))
+				tab_print_one(name, 1);
+		}
+	} else {
+		fat_list_dir(tab_fs, tab_dir_cluster, tab_print_cb);
+	}
+	io_putchar('\n');
+	term_print_prompt(t, 1);
+	redraw_line_clear(t->line, t->line_len, t->line_pos, t->line_start, t->line_len);
+}
+
+/* Complete the token at the cursor: command name in the first token, a VFS
+ * path afterwards.  One match completes, several extend to the common
+ * prefix or are listed below the line. */
+static void term_complete(struct term *t)
+{
+	char token[LINE_MAX + 1];
+	char ins[LINE_MAX + 1];
+	char dir[VFS_PATH_MAX];
+	unsigned int start = t->line_pos, end, tl, i, j;
+
+	while (start > 0 && t->line[start - 1] != ' ')
+		start--;
+	end = t->line_pos;
+	while (end < t->line_len && t->line[end] != ' ')
+		end++;
+	tl = end - start;
+	for (i = 0; i < tl; i++)
+		token[i] = t->line[start + i];
+	token[tl] = '\0';
+
+	/* First token -> command name. */
+	{
+		int first = 1;
+		for (i = 0; i < start; i++)
+			if (t->line[i] != ' ') {
+				first = 0;
+				break;
+			}
+		if (first) {
+			unsigned int nitems = sizeof(help_items) / sizeof(help_items[0]);
+			unsigned int count = 0, lcp_len = 0, best = 0;
+			char lcp[16];
+
+			for (i = 0; i < nitems; i++) {
+				unsigned int nl = help_name_len(help_items[i]);
+
+				if (nl < tl || !prefix_ci(help_items[i], token, tl))
+					continue;
+				if (count == 0) {
+					for (j = 0; j < nl; j++)
+						lcp[j] = help_items[i][j];
+					lcp_len = nl;
+					best = i;
+				} else {
+					for (j = 0; j < lcp_len && j < nl &&
+						     lcp[j] == help_items[i][j]; j++)
+						;
+					lcp_len = j;
+				}
+				count++;
+			}
+			if (count == 0)
+				return;
+			if (count == 1) {
+				unsigned int nl = help_name_len(help_items[best]);
+				for (j = 0; j < nl; j++)
+					ins[j] = help_items[best][j];
+				ins[nl] = ' ';
+				ins[nl + 1] = '\0';
+				term_replace(t, start, end, ins);
+				return;
+			}
+			if (lcp_len > tl) {
+				for (j = 0; j < lcp_len; j++)
+					ins[j] = lcp[j];
+				ins[lcp_len] = '\0';
+				term_replace(t, start, end, ins);
+				return;
+			}
+			io_putchar('\n');
+			tab_col = 0;
+			tab_width = t->screen.cols;
+			for (i = 0; i < nitems; i++) {
+				unsigned int nl = help_name_len(help_items[i]);
+				char name[16];
+				if (nl < tl || !prefix_ci(help_items[i], token, tl))
+					continue;
+				for (j = 0; j < nl && j < 15; j++)
+					name[j] = help_items[i][j];
+				name[j] = '\0';
+				tab_print_one(name, 0);
+			}
+			io_putchar('\n');
+			term_print_prompt(t, 1);
+			redraw_line_clear(t->line, t->line_len, t->line_pos,
+					  t->line_start, t->line_len);
+			return;
+		}
+	}
+
+	/* Argument token -> VFS path. */
+	{
+		char dirpart[LINE_MAX + 1];
+		const char *last = 0, *p;
+		unsigned int dp, n;
+		int mounts = 0;
+
+		for (p = token; *p; p++)
+			if (*p == '/')
+				last = p;
+		if (last) {
+			dp = (unsigned int)(last - token) + 1;
+			for (i = 0; i < dp; i++)
+				dirpart[i] = token[i];
+			dirpart[dp] = '\0';
+			p = last + 1;
+		} else {
+			dp = 0;
+			dirpart[0] = '\0';
+			p = token;
+		}
+		{
+			unsigned int k = 0;
+			while (*p && k < 15)
+				tab_prefix[k++] = *p++;
+			tab_prefix[k] = '\0';
+			tab_prefix_len = k;
+		}
+
+		if (dirpart[0] == '/' && dirpart[1] == '\0')
+			mounts = 1;
+		if (dirpart[0] == '/') {
+			if (dp >= VFS_PATH_MAX)
+				return;
+			for (i = 0; dirpart[i]; i++)
+				dir[i] = dirpart[i];
+			dir[i] = '\0';
+		} else {
+			if (vfs_join_path(t->cwd, dirpart, dir, sizeof(dir)) != 0)
+				return;
+		}
+
+		n = mounts ? (unsigned int)tab_scan_mounts()
+			   : (unsigned int)tab_scan_dir(dir);
+		if (n == 0)
+			return;
+		if (dp + tab_lcp_len + 2 > LINE_MAX)
+			return;
+		if (n == 1) {
+			for (i = 0; i < dp; i++)
+				ins[i] = dirpart[i];
+			for (i = 0; i < tab_lcp_len; i++)
+				ins[dp + i] = tab_lcp[i];
+			ins[dp + tab_lcp_len] = (mounts || tab_single_dir) ? '/' : ' ';
+			ins[dp + tab_lcp_len + 1] = '\0';
+			term_replace(t, start, end, ins);
+			return;
+		}
+		if (tab_lcp_len > tab_prefix_len) {
+			for (i = 0; i < dp; i++)
+				ins[i] = dirpart[i];
+			for (i = 0; i < tab_lcp_len; i++)
+				ins[dp + i] = tab_lcp[i];
+			ins[dp + tab_lcp_len] = '\0';
+			term_replace(t, start, end, ins);
+			return;
+		}
+		term_tab_ambiguous(t, mounts);
+	}
+}
+
 /* Push current line to history if non-empty (and not duplicate of newest). */
 static void history_push(struct term *t, const char *line, unsigned int line_len)
 {
@@ -1258,6 +1626,8 @@ void term_run(struct term *t)
 						t->line[t->line_len] = '\0';
 						redraw_line_from(t->line, t->line_len, t->line_pos, t->line_start, t->line_pos);
 					}
+				} else if (k == '\t') {
+					term_complete(t);
 				} else if (k == '\n') {
 					char cat_fname[CAT_FNAME_MAX];
 					char save_args[SAVE_ARGS_MAX];

+ 1 - 1
tss.c

@@ -57,7 +57,7 @@ void gdt_tss_init(void)
 	__asm__ volatile ("lgdt %0" : : "m"(gdtp));
 
 	tss.ss0  = 0x10;    /* kernel data segment */
-	tss.esp0 = 0x42800; /* bootstrap stack; updated by switch.S per process */
+	tss.esp0 = 0x44800; /* bootstrap stack; updated by switch.S per process */
 	tss.iomap_base = 0xFFFF;  /* no I/O permission bitmap (deny ring-3 I/O) */
 	__asm__ volatile ("ltr %0" : : "r"((unsigned short)0x28));
 }

+ 1 - 1
uhci.c

@@ -72,7 +72,7 @@ struct uhci_qh {
 #define UHCI_NUM_TD 16
 
 /* Frame list lives in low memory (below the kernel at 0x7E00) to avoid
-   bloating the kernel's BSS past the 0x42000 linker budget. It must be
+   bloating the kernel's BSS past the 0x44000 linker budget. It must be
    4 KB aligned; 0x3000 is free after boot (see AGENTS.md memory map). */
 #define UHCI_FRAME_LIST_ADDR 0x3000u
 

+ 2 - 2
user.ld

@@ -1,9 +1,9 @@
-/* User program: load at 0x43000, same as run.c PROGRAM_LOAD_ADDR. */
+/* User program: load at 0x45000, same as run.c PROGRAM_LOAD_ADDR. */
 ENTRY(_start)
 
 SECTIONS
 {
-	. = 0x43000;
+	. = 0x45000;
 	.text : { *(.text .text.*) }
 	.rodata : { *(.rodata .rodata.*) }
 	.data : { *(.data .data.*) }

+ 1 - 1
vesa_stage.S

@@ -223,7 +223,7 @@ stage2_pm:
 	movw  %ax, %fs
 	movw  %ax, %gs
 	movw  %ax, %ss
-	movl  $0x42800, %esp
+	movl  $0x44800, %esp
 	jmp   kernel_main
 
 .align 4