Files
ViperOS/vpsdk/fs.vp
T
TermiNexus f4c653958e feat(devfs): S1 —— 暴露 /dev/fb0 并让用户态可达 /dev/*(含 ioctl)
devfs:新增 /dev/fb0(framebuffer_device),提供 ioctl(FB_GET_INFO) 回填几何/格式/物理区间,并新增 devfs_file.mmap_info 虚方法(默认不可映射)把物理区间交给内核 mmap 用;main.vp 在 devfs_init 后登记它。输入设备(/dev/input/keyboard、/dev/input/mouse)此前已存在,本轮起从用户态可达。

用户态可达性:Process 增 fd_kind[](0=FAT32 fileobj*,1=devfs_fd+1),OPEN 对 "/dev/" 前缀改走 devfs,READ/WRITE/CLOSE 按 kind 分派;Process.exit 只对 kind=0 调 fat32.close(把 devfs fd 当 fileobj* 会跑飞)。fd 种类常量放在 process.vp 供 syscall.vp 引用,避免 process 反向 import devfs(devfs→keyboard/mouse→intr.idt→sched→process 会成环)。

新增 IOCTL syscall(Linux 号 16,本轮开始实现):只放给 devfs 设备,SDK 侧 vpsdk/fs.vp 加 ioctl() 助手。
2026-10-16 18:38:21 +08:00

142 lines
4.3 KiB
Plaintext

from . import syscall as syscall
define O_RDONLY = 0x01
define O_WRONLY = 0x02
define O_RDWR = 0x03
define O_CREAT = 0x04
define O_TRUNC = 0x08
define O_APPEND = 0x30
define SEEK_SET = 0
define SEEK_CUR = 1
define SEEK_END = 2
define DT_UNKNOWN = 0
define DT_FILE = 1
define DT_DIR = 2
def open(path: const str, flags: uint32) -> int:
return int(syscall._syscall3(uint64(syscall.Syscall.OPEN), uint64(path), uint64(flags), 0))
def close(fd: int) -> int:
return int(syscall._syscall1(uint64(syscall.Syscall.CLOSE), uint64(fd)))
def read(fd: int, buf: rawptr, count: uint32) -> int:
return int(syscall._syscall3(uint64(syscall.Syscall.READ), uint64(fd), uint64(buf), uint64(count)))
def write(fd: int, buf: rawptr, count: uint32) -> int:
return int(syscall._syscall3(uint64(syscall.Syscall.WRITE), uint64(fd), uint64(buf), uint64(count)))
def seek(fd: int, offset: uint32) -> int:
return int(syscall._syscall3(uint64(syscall.Syscall.SEEK), uint64(fd), uint64(offset), 0))
def tell(fd: int) -> uint32:
return uint32(syscall._syscall1(uint64(syscall.Syscall.TELL), uint64(fd)))
def fsize(fd: int) -> uint32:
return uint32(syscall._syscall1(uint64(syscall.Syscall.SIZE), uint64(fd)))
def mkdir(path: const str) -> int:
return int(syscall._syscall3(uint64(syscall.Syscall.MKDIR), uint64(path), 0, 0))
def remove(path: const str) -> int:
return int(syscall._syscall3(uint64(syscall.Syscall.REMOVE), uint64(path), 0, 0))
def opendir(path: const str) -> int:
return int(syscall._syscall3(uint64(syscall.Syscall.OPENDIR), uint64(path), 0, 0))
def readdir(dd: int, info: rawptr) -> int:
return int(syscall._syscall3(uint64(syscall.Syscall.READDIR), uint64(dd), uint64(info), 0))
def closedir(dd: int) -> int:
return int(syscall._syscall1(uint64(syscall.Syscall.CLOSEDIR), uint64(dd)))
def stat(path: const str, info: rawptr) -> int:
return int(syscall._syscall3(uint64(syscall.Syscall.STAT), uint64(path), uint64(info), 0))
def ioctl(fd: int, request: uint32, arg: rawptr) -> int:
"""设备控制(Linux ioctl 子集)。当前只有 devfs 设备(如 /dev/fb0)实现了它。"""
return int(syscall._syscall3(uint64(syscall.Syscall.IOCTL), uint64(fd), uint64(request), uint64(arg)))
class FileIO:
_fd: int
_pos: uint32
_size: uint32
_flags: uint32
_closed: int
def __init__(self, fd: int, flags: uint32):
self._fd = fd
self._flags = flags
self._pos = 0
self._size = fsize(fd)
self._closed = 0
def read(self, buf: rawptr, count: uint32) -> int:
if self._closed: return -1
n: int = read(self._fd, buf, count)
if n > 0:
self._pos = self._pos + uint32(n)
return n
def write(self, buf: rawptr, count: uint32) -> int:
if self._closed: return -1
n: int = write(self._fd, buf, count)
if n > 0:
self._pos = self._pos + uint32(n)
if self._pos > self._size:
self._size = self._pos
return n
def seek(self, offset: uint32) -> int:
if self._closed: return -1
res: int = seek(self._fd, offset)
if res == 0:
self._pos = offset
return res
def tell(self) -> uint32:
return self._pos
def size(self) -> uint32:
return self._size
def close(self) -> int:
if self._closed: return 0
self._closed = 1
return close(self._fd)
def is_closed(self) -> int:
return self._closed
class DirIO:
_dd: int
_closed: int
def __init__(self, dd: int):
self._dd = dd
self._closed = 0
def read(self, info: rawptr) -> int:
if self._closed: return -1
return readdir(self._dd, info)
def close(self) -> int:
if self._closed: return 0
self._closed = 1
return closedir(self._dd)
def is_closed(self) -> int:
return self._closed
def open_file(path: const str, flags: uint32) -> FileIO:
fd: int = open(path, flags)
if fd < 0: return None
return FileIO(fd, flags)
def open_dir(path: const str) -> DirIO:
dd: int = opendir(path)
if dd < 0: return None
return DirIO(dd)