syscall_table.rst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. .. note:: AI Translation Notice
  2. This document was automatically translated by `Qwen/Qwen3-8B` model, for reference only.
  3. - Source document: kernel/syscall/syscall_table.rst
  4. - Translation time: 2025-05-19 01:41:32
  5. - Translation model: `Qwen/Qwen3-8B`
  6. Please report issues via `Community Channel <https://github.com/DragonOS-Community/DragonOS/issues>`_
  7. System Call Table Implementation Plan
  8. =====================================
  9. .. note::
  10. Author: longjin <longjin@dragonos.org>
  11. Date: 2025/05/13
  12. Overview
  13. --------
  14. .. mermaid::
  15. :align: center
  16. :caption: System Call Table Architecture
  17. classDiagram
  18. class Syscall {
  19. <<trait>>
  20. +num_args() usize
  21. +handle(args, from_user) Result<usize, SystemError>
  22. +entry_format(args) Vec<FormattedSyscallParam>
  23. }
  24. class SyscallHandle {
  25. +nr: usize
  26. +inner_handle: &dyn Syscall
  27. }
  28. class SyscallTable {
  29. -entries: [Option<&SyscallHandle>; 512]
  30. +get(nr) Option<&dyn Syscall>
  31. }
  32. Syscall <|.. SysXXXXXXHandle
  33. SyscallHandle "1" *-- "1" Syscall
  34. SyscallTable "1" *-- "512" SyscallHandle
  35. Compared to the original approach of dispatching system calls in a single large match statement, this approach uses a trait-based and system call table-based implementation. The main advantages include:
  36. - Reduced stack memory usage: Avoids a single large function consuming excessive stack space
  37. - Support for parameter printing: Through a unified parameter formatting interface
  38. - Better extensibility: Adding new system calls does not require modifying the dispatch logic
  39. Core Design
  40. -----------
  41. Syscall Trait
  42. ~~~~~~~~~~~~~
  43. All system call handler functions must implement the `Syscall` trait:
  44. .. code-block:: rust
  45. pub trait Syscall: Send + Sync + 'static {
  46. fn num_args(&self) -> usize;
  47. fn handle(&self, args: &[usize], from_user: bool) -> Result<usize, SystemError>;
  48. fn entry_format(&self, args: &[usize]) -> Vec<FormattedSyscallParam>;
  49. }
  50. - `num_args()`: Returns the number of arguments required by the system call
  51. - `handle()`: Executes the actual system call handling
  52. - `entry_format()`: Formats the parameters for debugging printing
  53. SyscallHandle
  54. ~~~~~~~~~~~~~
  55. The `SyscallHandle` struct associates a system call number with its handler:
  56. .. code-block:: rust
  57. pub struct SyscallHandle {
  58. pub nr: usize, // System call number
  59. pub inner_handle: &'static dyn Syscall, // Handler function
  60. pub name: &'static str,
  61. }
  62. SyscallTable
  63. ~~~~~~~~~~~~
  64. The `SyscallTable` manages all system calls:
  65. - Fixed size of 512 entries
  66. - Initialized at compile time
  67. - Allows quick lookup of the handler function by system call number
  68. Usage
  69. -----
  70. Implementing a System Call
  71. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  72. 1. Define the implementation of `Syscall` for the specific system call
  73. 2. Register the system call in the system call table
  74. 3. Load all registered system calls during system initialization