Sfoglia il codice sorgente

Move the info commands to ring 3 (meminfo, mouseinfo, storage)

The three state-reporting commands become ring-3 ELFs like the file
commands; the kernel now exposes small read-only snapshots instead of
printing.

- syscall.h adds SYS_MEMINFO (total/free/pages/E820 count), SYS_E820
  (one entry, low 32 bits as meminfo always printed), SYS_MOUSEINFO (the
  PS/2 and USB details, cursor backend), SYS_STORAGE_INFO (refreshes the
  mount cache and reports the AHCI flag + device count) and SYS_BLKDEV
  (one device: name, model, kind, present, sectors).  sys_info.c
  implements them; the storage refresh runs under the fs-busy gate since
  it re-probes the disks.
- ucmd_meminfo.c, ucmd_mouseinfo.c and ucmd_storage.c keep the old
  output formats; the mount-belongs-to-device matching and the size
  formatting move to userland, and the kernel mem.c, mousecmd.c and
  storage.c are gone.
- sys_fs.c exports sys_fs_enter/sys_fs_exit for other disk-touching
  services.

Verified on QEMU (QMP keyboard + the netconsole mirror): meminfo prints
the same totals and E820 dump, mouseinfo the same PS/2+USB details, and
storage the device/mount list ("floppy1: FAT12 1.4 MB "ROOTDISK"",
"ide0: not present", "sata: no AHCI controller").

AGENTS.md: the command lists and the sys_info.c mention.
Johannes Zellner 1 mese fa
parent
commit
bab35c4c54
15 ha cambiato i file con 471 aggiunte e 324 eliminazioni
  1. 14 11
      AGENTS.md
  2. 7 12
      Makefile
  3. 0 56
      mem.c
  4. 0 86
      mousecmd.c
  5. 0 139
      storage.c
  6. 14 14
      sys_fs.c
  7. 5 0
      sys_fs.h
  8. 105 0
      sys_info.c
  9. 13 0
      sys_info.h
  10. 11 0
      syscall.c
  11. 52 0
      syscall.h
  12. 12 6
      terminal.c
  13. 43 0
      ucmd_meminfo.c
  14. 66 0
      ucmd_mouseinfo.c
  15. 129 0
      ucmd_storage.c

+ 14 - 11
AGENTS.md

@@ -184,14 +184,16 @@ below the run area), so adding code no longer moves any address.
   ELFs linked at `USER_BASE` (`0x40000000`), built by the Makefile, embedded
   in the kernel image (`*_embed.o`) and listed as `CMD_USER` rows in the
   terminal's command registry (`cmds[]` + `ucmds[]`).  Moved so far:
-  `cpuinfo`, `ls`, `cat`, `save`, `rm`, `mkdir`, `rmdir`.  They talk to the
-  kernel through `int 0x80` (`syscall.h`: putstr/ticks/sleep/exit plus the
-  whole-file service in `sys_fs.c`) and inherit the terminal's screen, so
-  their output lands in the owning window.  New commands: add `ucmd_<name>.c`,
-  a `CMD_USER` row in `cmds[]`, and a blob in `ucmds[]` (the Makefile picks
-  the file up via its `ucmd_%` pattern rules).  The one argument is passed as
-  a C string on the top of the user stack (`process_create_user(..., arg)`),
-  so `_start(const char *arg)` needs no crt0.
+  `cpuinfo`, `ls`, `cat`, `save`, `rm`, `mkdir`, `rmdir`, `meminfo`,
+  `mouseinfo`, `storage`.  They talk to the kernel through `int 0x80`
+  (`syscall.h`: putstr/ticks/sleep/exit, the whole-file service in
+  `sys_fs.c` and the small state snapshots in `sys_info.c`) and inherit the
+  terminal's screen, so their output lands in the owning window.  New
+  commands: add `ucmd_<name>.c`, a `CMD_USER` row in `cmds[]`, and a blob in
+  `ucmds[]` (the Makefile picks the file up via its `ucmd_%` pattern rules).
+  The one argument is passed as a C string on the top of the user stack
+  (`process_create_user(..., arg)`), so `_start(const char *arg)` needs no
+  crt0.
 
 ## Shell commands
 
@@ -226,9 +228,10 @@ changed with `cd`; paths may be absolute (`/floppy1/…`) or relative (`.`, `..`
 nested). `/` is the read-only VFS root listing the mounted filesystems (the
 filesystem commands operate inside a mount, e.g. `cat /floppy1/HELLO.TXT`).
 
-`cpuinfo`, `ls`, `cat`, `save`, `rm`, `mkdir` and `rmdir` run as ring-3
-processes (embedded `ucmd_*.c`); the rest are kernel commands spawned as
-processes, or shell builtins (`cd`, `clear`, `help`).
+`cpuinfo`, `ls`, `cat`, `save`, `rm`, `mkdir`, `rmdir`, `meminfo`,
+`mouseinfo` and `storage` run as ring-3 processes (embedded `ucmd_*.c`); the
+rest are kernel commands spawned as processes, or shell builtins (`cd`,
+`clear`, `help`).
 
 ## Serial hot-reload (kexec) workflow
 

+ 7 - 12
Makefile

@@ -113,9 +113,6 @@ colors.o: colors.c fbtext.h io.h timer.h vga.h
 display.o: display.c io.h vga.h gpu.h ati.h nvidia.h radeon.h mouse.h fbtext.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-mousecmd.o: mousecmd.c io.h mouse.h usb.h usbhid.h
-	$(CC) $(CFLAGS) -o $@ $<
-
 uhci.o: uhci.c uhci.h usb_hcd.h pci.h serial.h timer.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -185,9 +182,6 @@ ahci.o: ahci.c ahci.h ata.h mm.h paging.h pci.h process.h serial.h timer.h
 fatdetect.o: fatdetect.c fatdetect.h fat.h blkdev.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-storage.o: storage.c ahci.h blkdev.h vfs.h io.h
-	$(CC) $(CFLAGS) -o $@ $<
-
 fatformat.o: fatformat.c fatformat.h blkdev.h mm.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -251,13 +245,13 @@ syscall.o: syscall.c syscall.h io.h process.h timer.h
 sys_fs.o: sys_fs.c sys_fs.h syscall.h fat.h vfs.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+sys_info.o: sys_info.c sys_info.h syscall.h ahci.h blkdev.h mm.h mouse.h usbhid.h vfs.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 syscall_stub.o: syscall_stub.S irqret.inc
 	$(AS) $(ASFLAGS) -o $@ $<
 
-mem.o: mem.c mm.h io.h
-	$(CC) $(CFLAGS) -o $@ $<
-
-kernel.elf: vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o mem.o screen.o io.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.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 cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o mouse.o mousecmd.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o storage.o fatformat.o format.o vfs.o sys_fs.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o curl.o hello_embed.o prog_embed.o space_embed.o ucmd_cpuinfo_embed.o ucmd_ls_embed.o ucmd_cat_embed.o ucmd_save_embed.o ucmd_rm_embed.o ucmd_mkdir_embed.o ucmd_rmdir_embed.o
+kernel.elf: vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.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 cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o curl.o hello_embed.o prog_embed.o space_embed.o ucmd_cpuinfo_embed.o ucmd_ls_embed.o ucmd_cat_embed.o ucmd_save_embed.o ucmd_rm_embed.o ucmd_mkdir_embed.o ucmd_rmdir_embed.o ucmd_meminfo_embed.o ucmd_mouseinfo_embed.o ucmd_storage_embed.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -342,7 +336,8 @@ prog: prog.bin
 # ELFs at USER_BASE and embedded in the kernel image so `make dev`/`net-dev`
 # deliver them too.  New command: ucmd_<name>.c + a CMD_USER row in cmds[] and a
 # blob in ucmds[].
-UCMDS = ucmd_cpuinfo ucmd_ls ucmd_cat ucmd_save ucmd_rm ucmd_mkdir ucmd_rmdir
+UCMDS = ucmd_cpuinfo ucmd_ls ucmd_cat ucmd_save ucmd_rm ucmd_mkdir ucmd_rmdir \
+        ucmd_meminfo ucmd_mouseinfo ucmd_storage
 
 # Keep the linked ELFs around (make would treat them as disposable
 # intermediates between the pattern rules).
@@ -528,4 +523,4 @@ net-dev: kernel.bin netkexec_send
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 
 clean:
-	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o mem.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.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 cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o mouse.o mousecmd.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o storage.o fatformat.o format.o vfs.o sys_fs.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o curl.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 hello.elf hello_embed.o prog_embed.o space_embed.o $(addprefix $(UCMDS),.o) $(addprefix $(UCMDS),.elf) $(addsuffix _embed.o,$(UCMDS)) bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor
+	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.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 cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o netcmd.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o curl.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 hello.elf hello_embed.o prog_embed.o space_embed.o $(addprefix $(UCMDS),.o) $(addprefix $(UCMDS),.elf) $(addsuffix _embed.o,$(UCMDS)) bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor

+ 0 - 56
mem.c

@@ -1,56 +0,0 @@
-/* meminfo command: report total/free physical memory and the E820 map. */
-
-#include "io.h"
-#include "mm.h"
-
-static void put_dec(unsigned int n)
-{
-	char buf[12];
-	unsigned int i = sizeof(buf) - 1;
-	buf[i] = '\0';
-	if (n == 0) {
-		io_putchar('0');
-		return;
-	}
-	while (n && i > 0) {
-		buf[--i] = (char)('0' + (n % 10));
-		n /= 10;
-	}
-	io_putstr(buf + i);
-}
-
-void program_mem_main(void)
-{
-	unsigned int total_kb = mm_total_ram_kb();
-	unsigned int free_pages = mm_free_pages();
-	unsigned int free_kb = free_pages * (PAGE_SIZE / 1024);
-	unsigned int n = mm_e820_count();
-	unsigned int i;
-
-	io_putstr("memory: ");
-	put_dec(total_kb);
-	io_putstr(" KB total, ");
-	put_dec(free_kb);
-	io_putstr(" KB free (");
-	put_dec(free_pages);
-	io_putstr(" pages)\n");
-
-	if (n == 0) {
-		io_putstr("E820 map unavailable\n");
-		return;
-	}
-
-	io_putstr("E820 map (");
-	put_dec(n);
-	io_putstr(" entries):\n");
-	for (i = 0; i < n; i++) {
-		const struct e820_entry *e = mm_e820_entry(i);
-		io_putstr("  [0x");
-		io_puthex((unsigned int)e->base);
-		io_putstr(" +0x");
-		io_puthex((unsigned int)e->length);
-		io_putstr("] type ");
-		put_dec(e->type);
-		io_putchar('\n');
-	}
-}

+ 0 - 86
mousecmd.c

@@ -1,86 +0,0 @@
-/* mouseinfo command: print mouse hardware/protocol info and exit. */
-
-#include "io.h"
-#include "mouse.h"
-#include "usb.h"
-#include "usbhid.h"
-
-static void put_uint(unsigned int n)
-{
-	char buf[12];
-	int i = 10;
-	buf[11] = '\0';
-	if (n == 0) {
-		io_putchar('0');
-		return;
-	}
-	while (n && i >= 0) {
-		buf[i--] = (char)('0' + (n % 10u));
-		n /= 10u;
-	}
-	io_putstr(buf + i + 1);
-}
-
-static void put_hex8(unsigned int x)
-{
-	static const char hex[] = "0123456789ABCDEF";
-	io_putchar(hex[(x >> 4) & 0xF]);
-	io_putchar(hex[x & 0xF]);
-}
-
-void program_mouseinfo_main(void)
-{
-	io_putstr("mouse present ");
-	io_putstr(mouse_is_present() ? "yes\n" : "no\n");
-	if (!mouse_is_present())
-		return;
-
-	if (mouse_ps2_present()) {
-		unsigned int id = mouse_device_id();
-
-		io_putstr("mouse ps/2 id 0x");
-		put_hex8(id & 0xFFu);
-		io_putchar('\n');
-		io_putstr("mouse protocol ");
-		if (id == 0x03)
-			io_putstr("intellimouse (wheel)\n");
-		else if (id == 0x04)
-			io_putstr("intellimouse explorer (5-button)\n");
-		else
-			io_putstr("standard ps/2\n");
-		io_putstr("mouse packet bytes ");
-		put_uint(mouse_packet_size());
-		io_putchar('\n');
-		io_putstr("mouse streaming ");
-		io_putstr(mouse_streaming_enabled() ? "on\n" : "off\n");
-	}
-
-	if (usbhid_mouse_attached()) {
-		unsigned int ctrl, addr, ep, mps;
-		int low;
-
-		usbhid_get_info(&ctrl, &addr, &ep, &mps, &low);
-		io_putstr("mouse usb ");
-		io_putstr(usb_hcd_name());
-		io_putstr(" hid boot\n");
-		io_putstr("mouse usb ctrl ");
-		put_uint(ctrl);
-		io_putstr(" addr ");
-		put_uint(addr);
-		io_putstr(" ep 0x");
-		put_hex8(0x80u | ep);
-		io_putchar('\n');
-		io_putstr("mouse usb mps ");
-		put_uint(mps);
-		io_putstr(low ? " (low speed)\n" : " (full speed)\n");
-	}
-
-	io_putstr("mouse usb keyboard ");
-	io_putstr(usbhid_keyboard_attached() ? "yes\n" : "no\n");
-
-	io_putstr("mouse cursor ");
-	io_putstr(mouse_cursor_visible() ? "visible\n" : "hidden\n");
-	io_putstr("mouse cursor backend ");
-	io_putstr(mouse_cursor_backend_name());
-	io_putchar('\n');
-}

+ 0 - 139
storage.c

@@ -1,139 +0,0 @@
-/* storage command: refresh the filesystem cache and report devices + mounts. */
-
-#include "ahci.h"
-#include "blkdev.h"
-#include "io.h"
-#include "vfs.h"
-
-static void put_dec(unsigned int n)
-{
-	char buf[12];
-	unsigned int i = sizeof(buf) - 1;
-	buf[i] = '\0';
-	if (n == 0) {
-		io_putchar('0');
-		return;
-	}
-	while (n && i > 0) {
-		buf[--i] = (char)('0' + (n % 10));
-		n /= 10;
-	}
-	io_putstr(buf + i);
-}
-
-/* Print a size given in 512-byte sectors (shifts avoid 32-bit overflow). */
-static void put_size_sectors(unsigned int sectors)
-{
-	if (sectors >= 2u * 1024u * 1024u) {           /* >= 1 GiB */
-		unsigned int gb = sectors >> 21;
-		unsigned int tenths = ((sectors & 0x1FFFFFu) * 10u) >> 21;
-		put_dec(gb);
-		if (tenths) {
-			io_putchar('.');
-			put_dec(tenths);
-		}
-		io_putstr(" GB");
-	} else if (sectors >= 2048u) {                 /* >= 1 MiB */
-		unsigned int mb = sectors >> 11;
-		unsigned int tenths = ((sectors & 0x7FFu) * 10u) >> 11;
-		put_dec(mb);
-		if (tenths) {
-			io_putchar('.');
-			put_dec(tenths);
-		}
-		io_putstr(" MB");
-	} else {
-		put_dec(sectors / 2u);
-		io_putstr(" KB");
-	}
-}
-
-static const char *fat_name(fat_type_t t)
-{
-	switch (t) {
-	case FAT12: return "FAT12";
-	case FAT16: return "FAT16";
-	case FAT32: return "FAT32";
-	default:    return "FAT";
-	}
-}
-
-static void report_fs(const char *name, const fat_fs_t *fs)
-{
-	io_putstr(name);
-	io_putstr(": ");
-	io_putstr(fat_name(fs->type));
-	io_putchar(' ');
-	put_size_sectors(fs->total_sectors);
-	if (fs->label[0]) {
-		io_putstr("  \"");
-		io_putstr(fs->label);
-		io_putchar('"');
-	}
-	io_putchar('\n');
-}
-
-/* Does mount `mount_name` belong to block device `dev_name`?
- * True for an exact match ("floppy0") or a partition ("ide0p1" for "ide0"). */
-static int mount_belongs_to(const char *mount_name, const char *dev_name)
-{
-	unsigned int i = 0;
-	while (dev_name[i] && mount_name[i] == dev_name[i])
-		i++;
-	if (dev_name[i] == '\0' && mount_name[i] == '\0')
-		return 1;
-	if (dev_name[i] == '\0' && mount_name[i] == 'p')
-		return 1;
-	return 0;
-}
-
-void program_storage_main(void)
-{
-	unsigned int i, j;
-
-	/* Refresh the filesystem cache (re-probes all devices). */
-	vfs_mount_all();
-
-	for (i = 0; i < blk_dev_count; i++) {
-		blk_dev_t *d = &blk_devs[i];
-		int found = 0;
-
-		if (d->kind != BLK_FLOPPY && !d->present) {
-			io_putstr(d->name);
-			io_putstr(": not present\n");
-			continue;
-		}
-
-		for (j = 0; j < vfs_mount_count; j++)
-			if (mount_belongs_to(vfs_mounts[j].name, d->name)) {
-				found = 1;
-				break;
-			}
-
-		/* Real disks get a header line with model and size, floppies
-		 * keep the old single-line output. */
-		if (d->kind != BLK_FLOPPY) {
-			io_putstr(d->name);
-			io_putstr(": ");
-			if (d->model[0]) {
-				io_putstr(d->model);
-				io_putchar(' ');
-			}
-			put_size_sectors(d->total_sectors);
-			if (!found)
-				io_putstr("  no valid FAT filesystem");
-			io_putchar('\n');
-		} else if (!found) {
-			io_putstr(d->name);
-			io_putstr(": no valid FAT filesystem\n");
-		}
-
-		if (found)
-			for (j = 0; j < vfs_mount_count; j++)
-				if (mount_belongs_to(vfs_mounts[j].name, d->name))
-					report_fs(vfs_mounts[j].name, &vfs_mounts[j].fs);
-	}
-
-	if (!ahci_controller_present())
-		io_putstr("sata: no AHCI controller\n");
-}

+ 14 - 14
sys_fs.c

@@ -19,13 +19,13 @@
 
 volatile int sys_fs_busy;
 
-static void fs_enter(void)
+void sys_fs_enter(void)
 {
 	sys_fs_busy = 1;
 	__asm__ volatile ("sti");   /* the disk may need its IRQ */
 }
 
-static void fs_exit(void)
+void sys_fs_exit(void)
 {
 	__asm__ volatile ("cli");
 	sys_fs_busy = 0;
@@ -101,7 +101,7 @@ int sys_read_file(const char *path, void *buf, unsigned int max)
 	char name[12];
 	int r;
 
-	fs_enter();
+	sys_fs_enter();
 	r = fs_parent(path, &fs, &dir, name);
 	if (r == 0) {
 		r = fat_read_file_in(fs, dir, name, buf, max);
@@ -110,7 +110,7 @@ int sys_read_file(const char *path, void *buf, unsigned int max)
 		else if (r < 0)
 			r = SYS_ENOENT;
 	}
-	fs_exit();
+	sys_fs_exit();
 	return r;
 }
 
@@ -121,7 +121,7 @@ int sys_write_file(const char *path, const void *buf, unsigned int len)
 	char name[12];
 	int r;
 
-	fs_enter();
+	sys_fs_enter();
 	r = fs_parent(path, &fs, &dir, name);
 	if (r == 0) {
 		r = fat_write_file_in(fs, dir, name, buf, len);
@@ -130,7 +130,7 @@ int sys_write_file(const char *path, const void *buf, unsigned int len)
 		else if (r != 0)
 			r = SYS_EWRITE;
 	}
-	fs_exit();
+	sys_fs_exit();
 	return r;
 }
 
@@ -141,7 +141,7 @@ int sys_unlink(const char *path)
 	char name[12];
 	int r;
 
-	fs_enter();
+	sys_fs_enter();
 	r = fs_parent(path, &fs, &dir, name);
 	if (r == 0) {
 		r = fat_delete_file_in(fs, dir, name);
@@ -150,7 +150,7 @@ int sys_unlink(const char *path)
 		else if (r != 0)
 			r = SYS_ENOENT;
 	}
-	fs_exit();
+	sys_fs_exit();
 	return r;
 }
 
@@ -161,7 +161,7 @@ int sys_mkdir(const char *path)
 	char name[12];
 	int r;
 
-	fs_enter();
+	sys_fs_enter();
 	r = fs_parent(path, &fs, &dir, name);
 	if (r == 0) {
 		r = fat_mkdir(fs, dir, name);
@@ -170,7 +170,7 @@ int sys_mkdir(const char *path)
 		else if (r != 0)
 			r = SYS_EWRITE;
 	}
-	fs_exit();
+	sys_fs_exit();
 	return r;
 }
 
@@ -181,7 +181,7 @@ int sys_rmdir(const char *path)
 	char name[12];
 	int r;
 
-	fs_enter();
+	sys_fs_enter();
 	r = fs_parent(path, &fs, &dir, name);
 	if (r == 0) {
 		r = fat_rmdir(fs, dir, name);
@@ -190,7 +190,7 @@ int sys_rmdir(const char *path)
 		else if (r != 0)
 			r = SYS_EWRITE;
 	}
-	fs_exit();
+	sys_fs_exit();
 	return r;
 }
 
@@ -224,7 +224,7 @@ int sys_list_dir(const char *path, unsigned int index, struct sys_dirent *out)
 	unsigned int dir;
 	int r;
 
-	fs_enter();
+	sys_fs_enter();
 	r = fs_dir(path, &fs, &dir);
 	if (r == 0) {
 		list_index = index;
@@ -237,7 +237,7 @@ int sys_list_dir(const char *path, unsigned int index, struct sys_dirent *out)
 		else
 			r = list_done ? 1 : 0;   /* 1 = entry returned, 0 = end */
 	}
-	fs_exit();
+	sys_fs_exit();
 	return r;
 }
 

+ 5 - 0
sys_fs.h

@@ -8,6 +8,11 @@
  * away from the running process (see sys_fs.c). */
 extern volatile int sys_fs_busy;
 
+/* Enter/leave the fs-busy section (used by other disk-touching services,
+ * e.g. the storage info refresh). */
+void sys_fs_enter(void);
+void sys_fs_exit(void);
+
 int sys_read_file(const char *path, void *buf, unsigned int max);
 int sys_write_file(const char *path, const void *buf, unsigned int len);
 int sys_unlink(const char *path);

+ 105 - 0
sys_info.c

@@ -0,0 +1,105 @@
+/* Kernel-state info for the info commands (meminfo/mouseinfo/storage).
+ * Small, read-only snapshots; storage additionally refreshes the mount cache,
+ * which reads disks and therefore runs under the fs-busy gate (see sys_fs.c). */
+
+#include "syscall.h"
+#include "ahci.h"
+#include "blkdev.h"
+#include "mm.h"
+#include "mouse.h"
+#include "sys_fs.h"
+#include "usb.h"
+#include "usbhid.h"
+#include "vfs.h"
+
+static void copy_str(char *dst, const char *src, unsigned int size)
+{
+	unsigned int i = 0;
+
+	while (i + 1 < size && src[i]) {
+		dst[i] = src[i];
+		i++;
+	}
+	dst[i] = '\0';
+}
+
+int sys_meminfo(struct sys_meminfo *out)
+{
+	out->total_kb = mm_total_ram_kb();
+	out->free_pages = mm_free_pages();
+	out->page_size = PAGE_SIZE;
+	out->e820_count = mm_e820_count();
+	return 0;
+}
+
+int sys_e820(unsigned int index, struct sys_e820 *out)
+{
+	const struct e820_entry *e = mm_e820_entry(index);
+
+	if (!e)
+		return 0;
+	out->base = (unsigned int)e->base;
+	out->length = (unsigned int)e->length;
+	out->type = e->type;
+	return 1;
+}
+
+int sys_mouseinfo(struct sys_mouseinfo *out)
+{
+	unsigned int i;
+
+	for (i = 0; i < sizeof(*out); i++)
+		((unsigned char *)out)[i] = 0;
+
+	out->present = mouse_is_present() ? 1 : 0;
+	out->cursor_visible = mouse_cursor_visible() ? 1 : 0;
+	copy_str(out->backend, mouse_cursor_backend_name(), sizeof(out->backend));
+	if (!out->present)
+		return 0;
+
+	out->ps2 = mouse_ps2_present() ? 1 : 0;
+	if (out->ps2) {
+		out->ps2_id = mouse_device_id() & 0xFFu;
+		out->ps2_packet_size = mouse_packet_size();
+		out->ps2_streaming = mouse_streaming_enabled() ? 1 : 0;
+	}
+	out->usb = usbhid_mouse_attached() ? 1 : 0;
+	if (out->usb) {
+		unsigned int ep, mps;
+		int low;
+
+		usbhid_get_info(&out->usb_ctrl, &out->usb_addr, &ep, &mps, &low);
+		out->usb_ep = ep;
+		out->usb_mps = mps;
+		out->usb_low = (unsigned int)low;
+		copy_str(out->hcd, usb_hcd_name(), sizeof(out->hcd));
+	}
+	out->usb_keyboard = usbhid_keyboard_attached() ? 1 : 0;
+	return 1;
+}
+
+int sys_storage_info(struct sys_storage_info *out)
+{
+	sys_fs_enter();                 /* vfs_mount_all() reads disks */
+	vfs_mount_all();
+	sys_fs_exit();
+
+	out->ahci_present = ahci_controller_present() ? 1 : 0;
+	out->blkdev_count = blk_dev_count;
+	return 0;
+}
+
+int sys_blkdev(unsigned int index, struct sys_blkdev *out)
+{
+	const blk_dev_t *d;
+
+	if (index >= blk_dev_count)
+		return 0;
+	d = &blk_devs[index];
+	copy_str(out->name, d->name, sizeof(out->name));
+	copy_str(out->model, d->model, sizeof(out->model));
+	out->kind = (unsigned int)d->kind;
+	out->present = d->present ? 1 : 0;
+	out->total_sectors = d->total_sectors;
+	return 1;
+}

+ 13 - 0
sys_info.h

@@ -0,0 +1,13 @@
+/* Kernel-state info syscalls (see sys_info.c). */
+#ifndef SYS_INFO_H
+#define SYS_INFO_H
+
+#include "syscall.h"
+
+int sys_meminfo(struct sys_meminfo *out);
+int sys_e820(unsigned int index, struct sys_e820 *out);
+int sys_mouseinfo(struct sys_mouseinfo *out);
+int sys_storage_info(struct sys_storage_info *out);
+int sys_blkdev(unsigned int index, struct sys_blkdev *out);
+
+#endif

+ 11 - 0
syscall.c

@@ -5,6 +5,7 @@
 #include "process.h"
 #include "serial.h"
 #include "sys_fs.h"
+#include "sys_info.h"
 #include "timer.h"
 
 int syscall_dispatch(int num, int a1, int a2, int a3)
@@ -37,6 +38,16 @@ int syscall_dispatch(int num, int a1, int a2, int a3)
 		return sys_rmdir((const char *)a1);
 	case SYS_LIST_MOUNTS:
 		return sys_list_mounts((unsigned int)a1, (struct sys_mount *)a2);
+	case SYS_MEMINFO:
+		return sys_meminfo((struct sys_meminfo *)a1);
+	case SYS_E820:
+		return sys_e820((unsigned int)a1, (struct sys_e820 *)a2);
+	case SYS_MOUSEINFO:
+		return sys_mouseinfo((struct sys_mouseinfo *)a1);
+	case SYS_STORAGE_INFO:
+		return sys_storage_info((struct sys_storage_info *)a1);
+	case SYS_BLKDEV:
+		return sys_blkdev((unsigned int)a1, (struct sys_blkdev *)a2);
 	default:
 		return -1;
 	}

+ 52 - 0
syscall.h

@@ -22,6 +22,14 @@
 #define SYS_LIST_MOUNTS 11   /* (unsigned int index, struct sys_mount *out)
 				-> 1 mount, 0 end */
 
+/* Kernel-state info for the info commands (see sys_info.c). */
+#define SYS_MEMINFO      12  /* (struct sys_meminfo *out) -> 0 */
+#define SYS_E820         13  /* (unsigned int index, struct sys_e820 *out)-> 1, 0 */
+#define SYS_MOUSEINFO    14  /* (struct sys_mouseinfo *out) -> 1 when present */
+#define SYS_STORAGE_INFO 15  /* (struct sys_storage_info *out) -> 0;
+				refreshes the mount cache like `storage` did */
+#define SYS_BLKDEV       16  /* (unsigned int index, struct sys_blkdev *out) */
+
 /* Error codes returned by the file syscalls. */
 #define SYS_EIO     (-1)     /* disk I/O error */
 #define SYS_ENODEV  (-2)     /* no such device / unusable path prefix */
@@ -45,6 +53,50 @@ struct sys_mount {
 	char label[12];
 };
 
+struct sys_meminfo {
+	unsigned int total_kb;
+	unsigned int free_pages;
+	unsigned int page_size;
+	unsigned int e820_count;
+};
+
+struct sys_e820 {
+	unsigned int base;      /* low 32 bits, as meminfo always printed */
+	unsigned int length;
+	unsigned int type;
+};
+
+struct sys_mouseinfo {
+	unsigned int present;
+	unsigned int ps2;
+	unsigned int ps2_id;
+	unsigned int ps2_packet_size;
+	unsigned int ps2_streaming;
+	unsigned int usb;
+	unsigned int usb_ctrl;
+	unsigned int usb_addr;
+	unsigned int usb_ep;
+	unsigned int usb_mps;
+	unsigned int usb_low;
+	unsigned int usb_keyboard;
+	unsigned int cursor_visible;
+	char hcd[8];
+	char backend[16];
+};
+
+struct sys_storage_info {
+	unsigned int ahci_present;
+	unsigned int blkdev_count;
+};
+
+struct sys_blkdev {
+	char name[8];
+	char model[41];
+	unsigned int kind;       /* 0 = floppy, 1 = IDE, 2 = SATA */
+	unsigned int present;
+	unsigned int total_sectors;
+};
+
 int syscall_dispatch(int num, int a1, int a2, int a3);
 
 #endif

+ 12 - 6
terminal.c

@@ -17,14 +17,11 @@ extern void program_run_main(const char *filename);
 extern void program_clock_main(void);
 extern void program_colors_main(void);
 extern void program_display_main(void);
-extern void program_mouseinfo_main(void);
 extern void program_serial_loop_main(const char *arg);
 extern void program_serial_recv_main(const char *arg);
 extern void program_run_serial_main(const char *arg);
 extern void program_run_embedded_main(const char *name);
 extern void program_lsusb_main(void);
-extern void program_storage_main(void);
-extern void program_mem_main(void);
 extern void program_format_main(const char *arg);
 extern void program_lspci_main(const char *filter);
 extern void program_peek_main(const char *arg);
@@ -132,9 +129,9 @@ static const struct kcmd cmds[] = {
 	{ "display",    "display",                       CK_SPAWN, 0, program_display_main, 0, 0, 0 },
 	{ "cpuinfo",    "cpuinfo",                       CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
 	{ "lsusb",      "lsusb",                         CK_SPAWN, 0, program_lsusb_main, 0, 0, 0 },
-	{ "mouseinfo",  "mouseinfo",                     CK_SPAWN, 0, program_mouseinfo_main, 0, 0, 0 },
-	{ "storage",    "storage",                       CK_SPAWN, 0, program_storage_main, 0, 0, 0 },
-	{ "meminfo",    "meminfo",                       CK_SPAWN, 0, program_mem_main, 0, 0, 0 },
+	{ "mouseinfo",  "mouseinfo",                     CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
+	{ "storage",    "storage",                       CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
+	{ "meminfo",    "meminfo",                       CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
 	{ "format",     "format <device>",               CK_CUSTOM, 0, 0, 0, 0, cmd_format },
 	{ "lspci",      "lspci [id]",                    CK_SPAWN_ARG, 0, 0, program_lspci_main, 0, 0 },
 	{ "peek",       "peek <addr> [len]",             CK_SPAWN_ARG, 0, 0, program_peek_main, 0, 0 },
@@ -182,6 +179,12 @@ extern unsigned char _binary_ucmd_mkdir_elf_start[];
 extern unsigned char _binary_ucmd_mkdir_elf_end[];
 extern unsigned char _binary_ucmd_rmdir_elf_start[];
 extern unsigned char _binary_ucmd_rmdir_elf_end[];
+extern unsigned char _binary_ucmd_meminfo_elf_start[];
+extern unsigned char _binary_ucmd_meminfo_elf_end[];
+extern unsigned char _binary_ucmd_mouseinfo_elf_start[];
+extern unsigned char _binary_ucmd_mouseinfo_elf_end[];
+extern unsigned char _binary_ucmd_storage_elf_start[];
+extern unsigned char _binary_ucmd_storage_elf_end[];
 
 struct ucmd_blob {
 	const char *name;
@@ -197,6 +200,9 @@ static const struct ucmd_blob ucmds[] = {
 	{ "rm",      _binary_ucmd_rm_elf_start,      _binary_ucmd_rm_elf_end },
 	{ "mkdir",   _binary_ucmd_mkdir_elf_start,   _binary_ucmd_mkdir_elf_end },
 	{ "rmdir",   _binary_ucmd_rmdir_elf_start,   _binary_ucmd_rmdir_elf_end },
+	{ "meminfo", _binary_ucmd_meminfo_elf_start, _binary_ucmd_meminfo_elf_end },
+	{ "mouseinfo", _binary_ucmd_mouseinfo_elf_start, _binary_ucmd_mouseinfo_elf_end },
+	{ "storage", _binary_ucmd_storage_elf_start, _binary_ucmd_storage_elf_end },
 };
 
 static const struct ucmd_blob *ucmd_lookup(const char *name)

+ 43 - 0
ucmd_meminfo.c

@@ -0,0 +1,43 @@
+/* meminfo: ring-3 command.  Total/free physical memory and the E820 map. */
+
+#include "ucmd_lib.h"
+
+void _start(const char *arg)
+{
+	struct sys_meminfo m;
+	unsigned int i;
+
+	(void)arg;
+	u_syscall1(SYS_MEMINFO, (int)&m);
+
+	u_puts("memory: ");
+	u_putdec(m.total_kb);
+	u_puts(" KB total, ");
+	u_putdec(m.free_pages * (m.page_size / 1024));
+	u_puts(" KB free (");
+	u_putdec(m.free_pages);
+	u_puts(" pages)\n");
+
+	if (m.e820_count == 0) {
+		u_puts("E820 map unavailable\n");
+		u_exit(0);
+	}
+
+	u_puts("E820 map (");
+	u_putdec(m.e820_count);
+	u_puts(" entries):\n");
+	for (i = 0; i < m.e820_count; i++) {
+		struct sys_e820 e;
+
+		if (u_syscall2(SYS_E820, (int)i, (int)&e) != 1)
+			break;
+		u_puts("  [0x");
+		u_puthex(e.base);
+		u_puts(" +0x");
+		u_puthex(e.length);
+		u_puts("] type ");
+		u_putdec(e.type);
+		u_putc('\n');
+	}
+	u_exit(0);
+}

+ 66 - 0
ucmd_mouseinfo.c

@@ -0,0 +1,66 @@
+/* mouseinfo: ring-3 command.  Mouse hardware/protocol info. */
+
+#include "ucmd_lib.h"
+
+static void put_hex8(unsigned int x)
+{
+	static const char hex[] = "0123456789ABCDEF";
+
+	u_putc(hex[(x >> 4) & 0xF]);
+	u_putc(hex[x & 0xF]);
+}
+
+void _start(const char *arg)
+{
+	struct sys_mouseinfo m;
+
+	(void)arg;
+	if (u_syscall1(SYS_MOUSEINFO, (int)&m) != 1) {
+		u_puts("mouse present no\n");
+		u_exit(0);
+	}
+
+	u_puts("mouse present yes\n");
+	if (m.ps2) {
+		u_puts("mouse ps/2 id 0x");
+		put_hex8(m.ps2_id & 0xFFu);
+		u_putc('\n');
+		u_puts("mouse protocol ");
+		if ((m.ps2_id & 0xFFu) == 0x03)
+			u_puts("intellimouse (wheel)\n");
+		else if ((m.ps2_id & 0xFFu) == 0x04)
+			u_puts("intellimouse explorer (5-button)\n");
+		else
+			u_puts("standard ps/2\n");
+		u_puts("mouse packet bytes ");
+		u_putdec(m.ps2_packet_size);
+		u_putc('\n');
+		u_puts("mouse streaming ");
+		u_puts(m.ps2_streaming ? "on\n" : "off\n");
+	}
+
+	if (m.usb) {
+		u_puts("mouse usb ");
+		u_puts(m.hcd);
+		u_puts(" hid boot\n");
+		u_puts("mouse usb ctrl ");
+		u_putdec(m.usb_ctrl);
+		u_puts(" addr ");
+		u_putdec(m.usb_addr);
+		u_puts(" ep 0x");
+		put_hex8(0x80u | m.usb_ep);
+		u_putc('\n');
+		u_puts("mouse usb mps ");
+		u_putdec(m.usb_mps);
+		u_puts(m.usb_low ? " (low speed)\n" : " (full speed)\n");
+	}
+
+	u_puts("mouse usb keyboard ");
+	u_puts(m.usb_keyboard ? "yes\n" : "no\n");
+	u_puts("mouse cursor ");
+	u_puts(m.cursor_visible ? "visible\n" : "hidden\n");
+	u_puts("mouse cursor backend ");
+	u_puts(m.backend);
+	u_putc('\n');
+	u_exit(0);
+}

+ 129 - 0
ucmd_storage.c

@@ -0,0 +1,129 @@
+/* storage: ring-3 command.  Refresh the filesystem cache and report devices
+ * and mounts (the kernel service re-probes the disks, like the old command). */
+
+#include "ucmd_lib.h"
+
+#define MAX_MOUNTS 24   /* VFS_MAX_MOUNTS */
+
+static struct sys_mount mounts[MAX_MOUNTS];
+
+/* Print a size given in 512-byte sectors (shifts avoid 32-bit overflow). */
+static void put_size_sectors(unsigned int sectors)
+{
+	if (sectors >= 2u * 1024u * 1024u) {           /* >= 1 GiB */
+		unsigned int gb = sectors >> 21;
+		unsigned int tenths = ((sectors & 0x1FFFFFu) * 10u) >> 21;
+		u_putdec(gb);
+		if (tenths) {
+			u_putc('.');
+			u_putdec(tenths);
+		}
+		u_puts(" GB");
+	} else if (sectors >= 2048u) {                 /* >= 1 MiB */
+		unsigned int mb = sectors >> 11;
+		unsigned int tenths = ((sectors & 0x7FFu) * 10u) >> 11;
+		u_putdec(mb);
+		if (tenths) {
+			u_putc('.');
+			u_putdec(tenths);
+		}
+		u_puts(" MB");
+	} else {
+		u_putdec(sectors / 2u);
+		u_puts(" KB");
+	}
+}
+
+static void report_fs(const struct sys_mount *m)
+{
+	u_puts(m->name);
+	u_puts(": ");
+	u_puts(m->type);
+	u_putc(' ');
+	put_size_sectors(m->total_sectors);
+	if (m->label[0]) {
+		u_puts("  \"");
+		u_puts(m->label);
+		u_putc('"');
+	}
+	u_putc('\n');
+}
+
+/* Does mount `mount_name` belong to block device `dev_name`?
+ * True for an exact match ("floppy0") or a partition ("ide0p1" for "ide0"). */
+static int mount_belongs_to(const char *mount_name, const char *dev_name)
+{
+	unsigned int i = 0;
+
+	while (dev_name[i] && mount_name[i] == dev_name[i])
+		i++;
+	if (dev_name[i] == '\0' && mount_name[i] == '\0')
+		return 1;
+	if (dev_name[i] == '\0' && mount_name[i] == 'p')
+		return 1;
+	return 0;
+}
+
+void _start(const char *arg)
+{
+	struct sys_storage_info si;
+	unsigned int nmounts = 0, i, j;
+
+	(void)arg;
+
+	/* Refresh the filesystem cache (re-probes all devices). */
+	u_syscall1(SYS_STORAGE_INFO, (int)&si);
+
+	for (i = 0; i < MAX_MOUNTS; i++) {
+		if (u_syscall2(SYS_LIST_MOUNTS, (int)i, (int)&mounts[nmounts]) != 1)
+			break;
+		nmounts++;
+	}
+
+	for (i = 0; i < si.blkdev_count; i++) {
+		struct sys_blkdev d;
+		int found = 0;
+
+		if (u_syscall2(SYS_BLKDEV, (int)i, (int)&d) != 1)
+			break;
+
+		if (d.kind != 0 /* BLK_FLOPPY */ && !d.present) {
+			u_puts(d.name);
+			u_puts(": not present\n");
+			continue;
+		}
+
+		for (j = 0; j < nmounts; j++)
+			if (mount_belongs_to(mounts[j].name, d.name)) {
+				found = 1;
+				break;
+			}
+
+		/* Real disks get a header line with model and size, floppies
+		 * keep the old single-line output. */
+		if (d.kind != 0) {
+			u_puts(d.name);
+			u_puts(": ");
+			if (d.model[0]) {
+				u_puts(d.model);
+				u_putc(' ');
+			}
+			put_size_sectors(d.total_sectors);
+			if (!found)
+				u_puts("  no valid FAT filesystem");
+			u_putc('\n');
+		} else if (!found) {
+			u_puts(d.name);
+			u_puts(": no valid FAT filesystem\n");
+		}
+
+		if (found)
+			for (j = 0; j < nmounts; j++)
+				if (mount_belongs_to(mounts[j].name, d.name))
+					report_fs(&mounts[j]);
+	}
+
+	if (!si.ahci_present)
+		u_puts("sata: no AHCI controller\n");
+	u_exit(0);
+}