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//! - **700-799**: Memory mapping operations (memory_map, memory_unmap)
19//! - **900-999**: Socket operations (socket_create, bind, connect, accept, etc.)
20//!
21//! Legacy POSIX-like system calls (20-35) are maintained for backward compatibility
22//! and redirect to the appropriate capability-based implementations.
23//!
24//! ## Current Implementation Status
25//!
26//! ### Process Management (1-99)
27//! - Exit (1), Clone (2), Execve (3), ExecveABI (4), Waitpid (5)
28//! - Getpid (7), Getppid (8), Brk (12), Sbrk (13)
29//! - Basic I/O: Putchar (16), Getchar (17)
30//! - ABI Zone: RegisterAbiZone (90), UnregisterAbiZone (91)
31//! - Namespace: CreateNamespace (92) - Smart syscall for task/VFS isolation
32//!
33//! ### Handle Management (100-199)
34//! - HandleQuery (100), HandleSetRole (101), HandleClose (102), HandleDuplicate (103)
35//!
36//! ### StreamOps Capability (200-299)
37//! - StreamRead (200), StreamWrite (201)
38//!
39//! ### FileObject Capability (300-399)
40//! - FileSeek (300), FileTruncate (301), FileMetadata (302)
41//!
42//! ### VFS Operations (400-499)
43//! - VfsOpen (400), VfsRemove (401), VfsCreateFile (402), VfsCreateDirectory (403), VfsChangeDirectory (404), VfsTruncate (405), VfsCreateSymlink (406), VfsReadlink (407), VfsGetCwdPath (408)
44//!
45//! ### Filesystem Operations (500-599)
46//! - FsMount (500), FsUmount (501), FsPivotRoot (502)
47//!
48//! ### IPC Operations (600-699)
49//! - Pipe (600)
50//! - Event Channels: Subscribe (610), Unsubscribe (611), Publish (612)
51//! - Shared Memory: Create (620)
52//! - Process Groups: not yet implemented
53//!
54//! ### Memory Mapping Operations (700-799)
55//! - MemoryMap (700), MemoryUnmap (701)
56//!
57//! ### Socket Operations (900-999)
58//! - SocketCreate (900), SocketBind (901), SocketListen (902), SocketConnect (903)
59//! - SocketAccept (904), Socketpair (905), SocketShutdown (906)
60//!
61//! ### Task Event Operations (800-899)  
62//! - Basic Events: Send (800), SetAction (801), Block (802)
63//! - Event Status: GetPending (803), HasPending (804)
64//! - Signal-like Operations: Terminate, Kill, Interrupt, etc.
65//!
66//! ## Design Principles
67//!
68//! - **Capability-based security**: Objects expose specific capabilities
69//! - **Type safety**: Compile-time checking of valid operations
70//! - **Backward compatibility**: Legacy APIs redirect to new implementations
71//! - **Clear semantics**: Descriptive names (CreateDirectory vs mkdir)
72//!
73//! ## System Call Table
74//!
75//! The system call table maps numbers to handler functions using the
76//! `syscall_table!` macro for type safety and consistency.
77//!
78
79use crate::arch::Trapframe;
80use crate::fs::vfs_v2::syscall::{
81    sys_fs_mount, sys_fs_pivot_root, sys_fs_umount, sys_vfs_change_directory,
82    sys_vfs_create_directory, sys_vfs_create_file, sys_vfs_create_symlink, sys_vfs_get_cwd_path,
83    sys_vfs_open, sys_vfs_readlink, sys_vfs_remove, sys_vfs_truncate,
84};
85use crate::ipc::syscall::{
86    sys_event_channel_create, sys_event_handler_register, sys_event_publish, sys_event_send_direct,
87    sys_event_subscribe, sys_event_unsubscribe, sys_pipe, sys_shared_memory_create,
88    sys_shared_memory_resize, sys_socket_recv_handle, sys_socket_recv_handle_and_data,
89    sys_socket_send_handle, sys_socket_send_handle_and_data,
90};
91use crate::network::syscall::{
92    sys_network_list_interfaces, sys_network_set_dns, sys_network_set_gateway,
93    sys_network_set_ipv4, sys_network_set_netmask,
94};
95use crate::network::syscall::{
96    sys_socket_accept, sys_socket_bind, sys_socket_connect, sys_socket_create, sys_socket_listen,
97    sys_socket_recvfrom, sys_socket_sendto, sys_socket_shutdown, sys_socketpair,
98};
99use crate::object::capability::file::{sys_file_seek, sys_file_truncate};
100use crate::object::capability::memory_mapping::{sys_memory_map, sys_memory_unmap};
101use crate::object::capability::stream::{sys_stream_read, sys_stream_write};
102use crate::object::handle::syscall::{
103    sys_handle_close, sys_handle_control, sys_handle_duplicate, sys_handle_query,
104    sys_handle_set_role,
105};
106use crate::task::syscall::{
107    sys_brk, sys_clone, sys_create_namespace, sys_execve, sys_execve_abi, sys_exit, sys_exit_group,
108    sys_get_tls, sys_getchar, sys_getpid, sys_getppid, sys_putchar, sys_register_abi_zone,
109    sys_sbrk, sys_set_tid_address, sys_set_tls, sys_sleep, sys_unregister_abi_zone, sys_waitpid,
110    sys_yield,
111};
112
113#[macro_use]
114mod macros;
115
116/// Debug/Profiler system call to dump profiler statistics
117#[cfg(feature = "profiler")]
118fn sys_profiler_dump(tf: &mut Trapframe) -> usize {
119    use crate::task::mytask;
120    tf.increment_pc_next(mytask().unwrap());
121    crate::profiler::print_profiling_results();
122    0
123}
124
125/// Stub implementation when profiler feature is disabled
126#[cfg(not(feature = "profiler"))]
127fn sys_profiler_dump(tf: &mut Trapframe) -> usize {
128    use crate::task::mytask;
129    tf.increment_pc_next(mytask().unwrap());
130    crate::println!("[Profiler] Not available (feature disabled)");
131    0
132}
133
134syscall_table! {
135    Invalid = 0 => |_: &mut Trapframe| {
136        0
137    },
138    Exit = 1 => sys_exit,
139    Clone = 2 => sys_clone,
140    Execve = 3 => sys_execve,
141    ExecveABI = 4 => sys_execve_abi,
142    Waitpid = 5 => sys_waitpid,
143    Kill = 6 => |_: &mut Trapframe| {
144        // Kill syscall is not implemented yet
145        usize::MAX // -1
146    },
147    Getpid = 7 => sys_getpid,
148    Getppid = 8 => sys_getppid,
149    Brk = 12 => sys_brk,
150    Sbrk = 13 => sys_sbrk,
151    // BASIC I/O
152    Putchar = 16 => sys_putchar,
153    Getchar = 17 => sys_getchar,
154
155    Sleep = 20 => sys_sleep,
156
157    Yield = 21 => sys_yield,
158
159    ExitGroup = 23 => sys_exit_group, // Exit all tasks in thread group
160    // TLS (Thread Local Storage) Management
161    SetTls = 30 => sys_set_tls,
162    GetTls = 31 => sys_get_tls,
163    SetTidAddress = 32 => sys_set_tid_address,
164
165    // ABI Zone Management
166    RegisterAbiZone = 90 => sys_register_abi_zone,
167    UnregisterAbiZone = 91 => sys_unregister_abi_zone,
168
169    // Namespace Management (Scarlet-style smart syscall)
170    CreateNamespace = 92 => sys_create_namespace,
171
172    // === Handle Management ===
173    HandleQuery = 100 => sys_handle_query,     // Query handle metadata/capabilities
174    HandleSetRole = 101 => sys_handle_set_role, // Change handle role after creation
175    HandleClose = 102 => sys_handle_close,     // Close any handle (files, pipes, etc.)
176    HandleDuplicate = 103 => sys_handle_duplicate, // Duplicate any handle
177    HandleControl = 110 => sys_handle_control,  // Control operations on handles (ioctl-equivalent)
178
179    // === StreamOps Capability ===
180    // Stream operations for any KernelObject with StreamOps capability
181    StreamRead = 200 => sys_stream_read,   // StreamOps::read
182    StreamWrite = 201 => sys_stream_write, // StreamOps::write
183
184    // === FileObject Capability ===
185    // File operations for any KernelObject with FileObject capability
186    FileSeek = 300 => sys_file_seek,       // FileObject::seek
187    FileTruncate = 301 => sys_file_truncate, // FileObject::truncate
188    // FileMetadata = 302 => sys_file_metadata, // FileObject::metadata
189
190    // === VFS Operations ===
191    VfsOpen = 400 => sys_vfs_open,             // VFS file/directory open
192    VfsRemove = 401 => sys_vfs_remove,         // Remove files or directories (unified)
193    VfsCreateFile = 402 => sys_vfs_create_file, // Create regular files through VFS
194    VfsCreateDirectory = 403 => sys_vfs_create_directory, // Create directories through VFS
195    VfsChangeDirectory = 404 => sys_vfs_change_directory, // Change current working directory
196    VfsTruncate = 405 => sys_vfs_truncate,     // Truncate file by path
197    VfsCreateSymlink = 406 => sys_vfs_create_symlink, // Create symbolic links through VFS
198    VfsReadlink = 407 => sys_vfs_readlink,     // Read symbolic link target through VFS
199    VfsGetCwdPath = 408 => sys_vfs_get_cwd_path, // Get current working directory path
200
201    // === Filesystem Operations ===
202    FsMount = 500 => sys_fs_mount,         // Mount filesystem
203    FsUmount = 501 => sys_fs_umount,       // Unmount filesystem
204    FsPivotRoot = 502 => sys_fs_pivot_root, // Change root filesystem
205
206    // === IPC Operations ===
207    Pipe = 600 => sys_pipe,                // Create pipe handles
208
209    // Event System (Handle-based, ABI-layer only)
210    EventChannelCreate = 610 => sys_event_channel_create,      // Create/open event channel (ABI use)
211    EventSubscribe = 611 => sys_event_subscribe,               // Subscribe to channel (ABI use)
212    EventUnsubscribe = 612 => sys_event_unsubscribe,           // Unsubscribe from channel (ABI use)
213    EventPublish = 613 => sys_event_publish,                   // Publish event to channel (ABI use)
214    EventHandlerRegister = 614 => sys_event_handler_register,  // Register event filter (ABI use)
215    EventSendDirect = 615 => sys_event_send_direct,            // Send direct event to task (ABI use)
216
217    // Shared Memory
218    SharedMemoryCreate = 620 => sys_shared_memory_create,      // Create shared memory region
219    SharedMemoryResize = 621 => sys_shared_memory_resize,      // Resize shared memory region
220
221    // Socket Handle Transfer (similar to SCM_RIGHTS)
222    SocketSendHandle = 630 => sys_socket_send_handle,          // Send kernel object handle through socket
223    SocketRecvHandle = 631 => sys_socket_recv_handle,          // Receive kernel object handle from socket
224    SocketSendHandleAndData = 632 => sys_socket_send_handle_and_data, // Send handle and data atomically
225    SocketRecvHandleAndData = 633 => sys_socket_recv_handle_and_data, // Receive handle and data atomically
226
227
228    // === Memory Mapping Operations ===
229    MemoryMap = 700 => sys_memory_map,     // Memory map operation (mmap)
230    MemoryUnmap = 701 => sys_memory_unmap, // Memory unmap operation (munmap)
231
232    // === Socket Operations (Scarlet Native) ===
233    SocketCreate = 900 => sys_socket_create,     // Create a socket (domain/type/protocol)
234    SocketBind = 901 => sys_socket_bind,         // Bind socket to path
235    SocketListen = 902 => sys_socket_listen,     // Start listening
236    SocketConnect = 903 => sys_socket_connect,   // Connect to socket
237    SocketAccept = 904 => sys_socket_accept,     // Accept connection
238    Socketpair = 905 => sys_socketpair,          // Create socket pair
239    SocketShutdown = 906 => sys_socket_shutdown, // Shutdown socket
240
241    // === Datagram Operations (UDP/Local datagram) ===
242    SocketRecvFrom = 907 => sys_socket_recvfrom, // Receive datagram with sender address
243    SocketSendTo = 908 => sys_socket_sendto,     // Send datagram to specified address
244
245    // === Network Configuration ===
246    NetworkSetIpv4 = 910 => sys_network_set_ipv4,       // Set interface IPv4 address
247    NetworkSetGateway = 911 => sys_network_set_gateway, // Set default gateway
248    NetworkSetDns = 912 => sys_network_set_dns,         // Set DNS server
249    NetworkSetNetmask = 913 => sys_network_set_netmask, // Set subnet mask
250    NetworkListInterfaces = 914 => sys_network_list_interfaces, // List network interfaces
251
252    // === Task Event Operations ===
253
254    // === Debug/Profiler Operations ===
255    ProfilerDump = 999 => sys_profiler_dump, // Dump profiler statistics (debug only)
256}