Add sysfs VFS driver, SYS_OPEN/CLOSE syscalls, cat app

Sysfs:
- New VFS driver mounted at /sys that lets kernel drivers expose
  virtual text files via namespace registration
- Drivers call sysfs_register(name, ops, ctx) with list/read/write
  callbacks for their namespace
- IDE driver registers 'ide' namespace exposing per-device attributes:
  model, type, channel, drive, sectors, sector_size
- Tested: ls /sys -> ide, ls /sys/ide -> hdd1 cd1,
  cat /sys/ide/hdd1/model -> QEMU HARDDISK

Syscalls:
- Added SYS_OPEN (11) and SYS_CLOSE (12) for file I/O from userspace
- Extended SYS_READ/SYS_WRITE to handle VFS file descriptors (fd >= 3)
- Updated userspace syscalls.h with open()/close() wrappers

Apps:
- New 'cat' app: reads and displays file contents via open/read/close
- Updated 'ls' to accept path argument via ARG1 env var
- Updated shell to pass ARG1 env var to external commands
This commit is contained in:
AI
2026-02-23 14:26:52 +00:00
parent d064e67a8f
commit d1bf69ce0d
12 changed files with 729 additions and 10 deletions

View File

@@ -61,6 +61,11 @@ static int32_t sys_write(registers_t *regs) {
return (int32_t)len;
}
/* VFS file descriptors (fd >= 3) */
if (fd >= 3) {
return vfs_write(fd, buf, len);
}
return -1; /* Invalid fd */
}
@@ -83,6 +88,11 @@ static int32_t sys_read(registers_t *regs) {
return 0; /* No data available */
}
/* VFS file descriptors (fd >= 3) */
if (fd >= 3) {
return vfs_read(fd, buf, len);
}
return -1; /* Invalid fd */
}
@@ -282,6 +292,29 @@ static int32_t sys_setenv(registers_t *regs) {
return env_set(&cur->env, key, value);
}
/**
* Handle SYS_OPEN: open a file by path.
* EBX = path string, ECX = flags.
* Returns file descriptor (>= 3) on success, -1 on failure.
*/
static int32_t sys_open(registers_t *regs) {
const char *path = (const char *)regs->ebx;
uint32_t flags = regs->ecx;
return (int32_t)vfs_open(path, flags);
}
/**
* Handle SYS_CLOSE: close a file descriptor.
* EBX = fd.
* Returns 0 on success, -1 on failure.
*/
static int32_t sys_close(registers_t *regs) {
int fd = (int)regs->ebx;
if (fd < 3) return -1; /* Don't close stdin/stdout/stderr */
vfs_close(fd);
return 0;
}
/**
* Handle SYS_READDIR: read a directory entry.
* EBX = path, ECX = index, EDX = name buffer (128 bytes min).
@@ -320,6 +353,8 @@ static syscall_fn syscall_table[NUM_SYSCALLS] = {
[SYS_GETENV] = sys_getenv,
[SYS_SETENV] = sys_setenv,
[SYS_READDIR] = sys_readdir,
[SYS_OPEN] = sys_open,
[SYS_CLOSE] = sys_close,
};
void syscall_handler(registers_t *regs) {