kernel/syscall/
mod.rs

1//! System call interface module.
2//! 
3//! This module provides the system call interface for the Scarlet kernel
4//! using a hybrid capability-based design that balances type safety with
5//! practical usability.
6//! 
7//! ## System Call Number Organization
8//! 
9//! The system calls are organized into logical ranges:
10//! 
11//! - **1-99**: Process and task management (exit, clone, exec, getpid, brk, etc.)
12//! - **100-199**: Handle management operations (handle_query, handle_close, dup)
13//! - **200-299**: StreamOps capability (stream_read, stream_write operations)
14//! - **300-399**: FileObject capability (file_seek, file_truncate, file_metadata)
15//! - **400-499**: VFS operations (vfs_open, vfs_remove, vfs_create_directory, vfs_change_directory, vfs_truncate)
16//! - **500-599**: Filesystem operations (fs_mount, fs_umount, fs_pivot_root)
17//! - **600-699**: IPC operations (pipe, shared memory, message queues)
18//! 
19//! Legacy POSIX-like system calls (20-35) are maintained for backward compatibility
20//! and redirect to the appropriate capability-based implementations.
21//! 
22//! ## Current Implementation Status
23//! 
24//! ### Process Management (1-99)
25//! - Exit (1), Clone (2), Execve (3), ExecveABI (4), Waitpid (5)
26//! - Getpid (7), Getppid (8), Brk (12), Sbrk (13)
27//! - Basic I/O: Putchar (16), Getchar (17)
28//! 
29//! ### Handle Management (100-199)
30//! - HandleQuery (100), HandleSetRole (101), HandleClose (102), HandleDuplicate (103)
31//! 
32//! ### StreamOps Capability (200-299)
33//! - StreamRead (200), StreamWrite (201)
34//! 
35//! ### FileObject Capability (300-399)
36//! - FileSeek (300), FileTruncate (301), FileMetadata (302)
37//! 
38//! ### VFS Operations (400-499)
39//! - VfsOpen (400), VfsRemove (401), VfsCreateFile (402), VfsCreateDirectory (403), VfsChangeDirectory (404), VfsTruncate (405)
40//! 
41//! ### Filesystem Operations (500-599)
42//! - FsMount (500), FsUmount (501), FsPivotRoot (502)
43//! 
44//! ### IPC Operations (600-699)
45//! - Pipe (600)
46//! 
47//! ## Design Principles
48//! 
49//! - **Capability-based security**: Objects expose specific capabilities
50//! - **Type safety**: Compile-time checking of valid operations
51//! - **Backward compatibility**: Legacy APIs redirect to new implementations
52//! - **Clear semantics**: Descriptive names (CreateDirectory vs mkdir)
53//! 
54//! ## System Call Table
55//! 
56//! The system call table maps numbers to handler functions using the
57//! `syscall_table!` macro for type safety and consistency.
58//! 
59
60use crate::arch::Trapframe;
61use crate::fs::vfs_v2::syscall::{sys_vfs_remove, sys_vfs_open, sys_vfs_create_file, sys_vfs_create_directory, sys_vfs_change_directory, sys_fs_mount, sys_fs_umount, sys_fs_pivot_root, sys_vfs_truncate};
62use crate::task::syscall::{sys_brk, sys_clone, sys_execve, sys_execve_abi, sys_exit, sys_getchar, sys_getpid, sys_getppid, sys_putchar, sys_sbrk, sys_waitpid};
63use crate::ipc::syscall::sys_pipe;
64use crate::object::handle::syscall::{sys_handle_query, sys_handle_set_role, sys_handle_close, sys_handle_duplicate};
65use crate::object::capability::stream::{sys_stream_read, sys_stream_write};
66use crate::object::capability::file::{sys_file_seek, sys_file_truncate};
67
68#[macro_use]
69mod macros;
70
71syscall_table! {
72    Invalid = 0 => |_: &mut Trapframe| {
73        0
74    },
75    Exit = 1 => sys_exit,
76    Clone = 2 => sys_clone,
77    Execve = 3 => sys_execve,
78    ExecveABI = 4 => sys_execve_abi,
79    Waitpid = 5 => sys_waitpid,
80    Getpid = 7 => sys_getpid,
81    Getppid = 8 => sys_getppid,
82    Brk = 12 => sys_brk,
83    Sbrk = 13 => sys_sbrk,
84    // BASIC I/O
85    Putchar = 16 => sys_putchar,
86    Getchar = 17 => sys_getchar,
87    
88    // === Handle Management ===
89    HandleQuery = 100 => sys_handle_query,     // Query handle metadata/capabilities
90    HandleSetRole = 101 => sys_handle_set_role, // Change handle role after creation
91    HandleClose = 102 => sys_handle_close,     // Close any handle (files, pipes, etc.)
92    HandleDuplicate = 103 => sys_handle_duplicate, // Duplicate any handle  
93    
94    // === StreamOps Capability ===
95    // Stream operations for any KernelObject with StreamOps capability
96    StreamRead = 200 => sys_stream_read,   // StreamOps::read
97    StreamWrite = 201 => sys_stream_write, // StreamOps::write
98    
99    // === FileObject Capability ===
100    // File operations for any KernelObject with FileObject capability
101    FileSeek = 300 => sys_file_seek,       // FileObject::seek
102    FileTruncate = 301 => sys_file_truncate, // FileObject::truncate
103    // FileMetadata = 302 => sys_file_metadata, // FileObject::metadata
104    
105    // === VFS Operations ===
106    VfsOpen = 400 => sys_vfs_open,             // VFS file/directory open
107    VfsRemove = 401 => sys_vfs_remove,         // Remove files or directories (unified)
108    VfsCreateFile = 402 => sys_vfs_create_file, // Create regular files through VFS
109    VfsCreateDirectory = 403 => sys_vfs_create_directory, // Create directories through VFS
110    VfsChangeDirectory = 404 => sys_vfs_change_directory, // Change current working directory
111    VfsTruncate = 405 => sys_vfs_truncate,     // Truncate file by path
112    
113    // === Filesystem Operations ===
114    FsMount = 500 => sys_fs_mount,         // Mount filesystem
115    FsUmount = 501 => sys_fs_umount,       // Unmount filesystem  
116    FsPivotRoot = 502 => sys_fs_pivot_root, // Change root filesystem
117    
118    // === IPC Operations ===
119    Pipe = 600 => sys_pipe,                // Create pipe handles
120}