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
181 lines
4.6 KiB
C
181 lines
4.6 KiB
C
/**
|
|
* @file syscalls.h
|
|
* @brief User-space system call wrappers for ClaudeOS.
|
|
*
|
|
* Provides inline functions that invoke INT 0x80 with the appropriate
|
|
* system call numbers and arguments.
|
|
*/
|
|
|
|
#ifndef USERSPACE_SYSCALLS_H
|
|
#define USERSPACE_SYSCALLS_H
|
|
|
|
typedef unsigned int uint32_t;
|
|
typedef int int32_t;
|
|
|
|
/* System call numbers (must match kernel's syscall.h) */
|
|
#define SYS_EXIT 0
|
|
#define SYS_WRITE 1
|
|
#define SYS_READ 2
|
|
#define SYS_FORK 3
|
|
#define SYS_GETPID 4
|
|
#define SYS_YIELD 5
|
|
#define SYS_WAITPID 6
|
|
#define SYS_EXEC 7
|
|
#define SYS_GETENV 8
|
|
#define SYS_SETENV 9
|
|
#define SYS_READDIR 10
|
|
#define SYS_OPEN 11
|
|
#define SYS_CLOSE 12
|
|
|
|
static inline int32_t syscall0(int num) {
|
|
int32_t ret;
|
|
__asm__ volatile("int $0x80" : "=a"(ret) : "a"(num));
|
|
return ret;
|
|
}
|
|
|
|
static inline int32_t syscall1(int num, uint32_t arg1) {
|
|
int32_t ret;
|
|
__asm__ volatile("int $0x80" : "=a"(ret) : "a"(num), "b"(arg1));
|
|
return ret;
|
|
}
|
|
|
|
static inline int32_t syscall2(int num, uint32_t arg1, uint32_t arg2) {
|
|
int32_t ret;
|
|
__asm__ volatile("int $0x80" : "=a"(ret)
|
|
: "a"(num), "b"(arg1), "c"(arg2));
|
|
return ret;
|
|
}
|
|
|
|
static inline int32_t syscall3(int num, uint32_t arg1, uint32_t arg2, uint32_t arg3) {
|
|
int32_t ret;
|
|
__asm__ volatile("int $0x80" : "=a"(ret)
|
|
: "a"(num), "b"(arg1), "c"(arg2), "d"(arg3));
|
|
return ret;
|
|
}
|
|
|
|
static inline void exit(int code) {
|
|
syscall1(SYS_EXIT, (uint32_t)code);
|
|
__builtin_unreachable();
|
|
}
|
|
|
|
static inline int32_t write(int fd, const void *buf, uint32_t len) {
|
|
return syscall3(SYS_WRITE, (uint32_t)fd, (uint32_t)buf, len);
|
|
}
|
|
|
|
static inline int32_t read(int fd, void *buf, uint32_t len) {
|
|
return syscall3(SYS_READ, (uint32_t)fd, (uint32_t)buf, len);
|
|
}
|
|
|
|
static inline int32_t fork(void) {
|
|
return syscall0(SYS_FORK);
|
|
}
|
|
|
|
static inline int32_t getpid(void) {
|
|
return syscall0(SYS_GETPID);
|
|
}
|
|
|
|
static inline void yield(void) {
|
|
syscall0(SYS_YIELD);
|
|
}
|
|
|
|
static inline int32_t waitpid(int32_t pid) {
|
|
return syscall1(SYS_WAITPID, (uint32_t)pid);
|
|
}
|
|
|
|
static inline int32_t exec(const char *path) {
|
|
return syscall1(SYS_EXEC, (uint32_t)path);
|
|
}
|
|
|
|
static inline int32_t getenv(const char *key, char *buf, uint32_t bufsize) {
|
|
return syscall3(SYS_GETENV, (uint32_t)key, (uint32_t)buf, bufsize);
|
|
}
|
|
|
|
static inline int32_t setenv(const char *key, const char *value) {
|
|
return syscall2(SYS_SETENV, (uint32_t)key, (uint32_t)value);
|
|
}
|
|
|
|
/**
|
|
* Read a directory entry.
|
|
* @param path Directory path.
|
|
* @param idx Entry index (0-based).
|
|
* @param name Buffer for entry name (128 bytes min).
|
|
* @return Entry type (1=file, 2=dir) on success, -1 at end.
|
|
*/
|
|
static inline int32_t readdir(const char *path, uint32_t idx, char *name) {
|
|
return syscall3(SYS_READDIR, (uint32_t)path, idx, (uint32_t)name);
|
|
}
|
|
|
|
/**
|
|
* Open a file by path.
|
|
* @param path File path.
|
|
* @param flags Open flags (currently unused, pass 0).
|
|
* @return File descriptor (>= 3) on success, -1 on failure.
|
|
*/
|
|
static inline int32_t open(const char *path, uint32_t flags) {
|
|
return syscall2(SYS_OPEN, (uint32_t)path, flags);
|
|
}
|
|
|
|
/**
|
|
* Close a file descriptor.
|
|
* @param fd File descriptor.
|
|
* @return 0 on success, -1 on failure.
|
|
*/
|
|
static inline int32_t close(int32_t fd) {
|
|
return syscall1(SYS_CLOSE, (uint32_t)fd);
|
|
}
|
|
|
|
/* Basic string operations for user-space */
|
|
static inline uint32_t strlen(const char *s) {
|
|
uint32_t len = 0;
|
|
while (s[len]) len++;
|
|
return len;
|
|
}
|
|
|
|
static inline int strcmp(const char *a, const char *b) {
|
|
while (*a && *a == *b) { a++; b++; }
|
|
return (unsigned char)*a - (unsigned char)*b;
|
|
}
|
|
|
|
static inline int strncmp(const char *a, const char *b, uint32_t n) {
|
|
while (n && *a && *a == *b) { a++; b++; n--; }
|
|
return n ? ((unsigned char)*a - (unsigned char)*b) : 0;
|
|
}
|
|
|
|
static inline char *strcpy(char *dst, const char *src) {
|
|
char *d = dst;
|
|
while ((*d++ = *src++));
|
|
return dst;
|
|
}
|
|
|
|
static inline char *strncpy(char *dst, const char *src, uint32_t n) {
|
|
char *d = dst;
|
|
while (n && (*d++ = *src++)) n--;
|
|
while (n--) *d++ = '\0';
|
|
return dst;
|
|
}
|
|
|
|
static inline void *memset(void *s, int c, uint32_t n) {
|
|
unsigned char *p = (unsigned char *)s;
|
|
while (n--) *p++ = (unsigned char)c;
|
|
return s;
|
|
}
|
|
|
|
static inline void *memcpy(void *dst, const void *src, uint32_t n) {
|
|
unsigned char *d = (unsigned char *)dst;
|
|
const unsigned char *s = (const unsigned char *)src;
|
|
while (n--) *d++ = *s++;
|
|
return dst;
|
|
}
|
|
|
|
/** Print a string to stdout. */
|
|
static inline void puts(const char *s) {
|
|
write(1, s, strlen(s));
|
|
}
|
|
|
|
/** Print a single character to stdout. */
|
|
static inline void putchar(char c) {
|
|
write(1, &c, 1);
|
|
}
|
|
|
|
#endif /* USERSPACE_SYSCALLS_H */
|