Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# -- Project information -----------------------------------------------------

project = 'DragonOS'
copyright = '2022-2024, DragonOS Community'
copyright = '2022-2025, DragonOS Community'
author = 'longjin'
github_org = 'DragonOS-Community'
github_repo = 'DragonOS'
Expand All @@ -31,7 +31,12 @@
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ['myst_parser', 'sphinx_multiversion']
extensions = [
'myst_parser',
'sphinx_multiversion',
'sphinxcontrib.mermaid',
'sphinx.ext.extlinks',
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
Expand Down
6 changes: 1 addition & 5 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
kernel/container/index
kernel/libs/index
kernel/trace/index
kernel/syscall/index



Expand All @@ -42,11 +43,6 @@

userland/appdev/index

.. toctree::
:maxdepth: 1
:caption: 系统调用api文档

syscall_api/index

.. toctree::
:maxdepth: 1
Expand Down
2 changes: 1 addition & 1 deletion docs/kernel/filesystem/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Syscall: │ sys_open, sys_read, sys_write, sys_close, │
- `sys_fchmod`:修改文件权限(未实现)
- 其他系统调用接口(未实现)

  关于接口的具体含义,可以参考 [DragonOS系统调用接口](../../syscall_api/index.rst)
  关于接口的具体含义,可以参考Linux的相关文档

## 虚拟文件系统(VFS)

Expand Down
6 changes: 3 additions & 3 deletions docs/syscall_api/index.rst → docs/kernel/syscall/index.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
.. _syscall_api:
.. _syscall:

系统调用API
系统调用
====================================

.. toctree::
:maxdepth: 1
:caption: 目录

intro
syscall_table
120 changes: 120 additions & 0 deletions docs/kernel/syscall/syscall_table.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
系统调用表实现方案
====================

.. note::
Author: longjin <[email protected]>

Date: 2025/05/13

概述
----

.. mermaid::
:align: center
:caption: 系统调用表架构

classDiagram
class Syscall {
<<trait>>
+num_args() usize
+handle(args, from_user) Result<usize, SystemError>
+entry_format(args) Vec<FormattedSyscallParam>
}

class SyscallHandle {
+nr: usize
+inner_handle: &dyn Syscall
}

class SyscallTable {
-entries: [Option<&SyscallHandle>; 512]
+get(nr) Option<&dyn Syscall>
}

Syscall <|.. SysXXXXXXHandle
SyscallHandle "1" *-- "1" Syscall
SyscallTable "1" *-- "512" SyscallHandle

相比于将原本集中在一个大match中的系统调用分发,本方案采用基于trait和系统调用表的实现。主要优势包括:

- 降低栈内存使用:避免单个大函数占用过多栈空间
- 支持参数打印:通过统一的参数格式化接口
- 更好的扩展性:新增系统调用无需修改分发逻辑

核心设计
--------

Syscall Trait
~~~~~~~~~~~~~

所有系统调用处理函数都需要实现 `Syscall` trait:

.. code-block:: rust

pub trait Syscall: Send + Sync + 'static {
fn num_args(&self) -> usize;
fn handle(&self, args: &[usize], from_user: bool) -> Result<usize, SystemError>;
fn entry_format(&self, args: &[usize]) -> Vec<FormattedSyscallParam>;
}

- `num_args()`: 返回该系统调用需要的参数数量
- `handle()`: 实际执行系统调用处理
- `entry_format()`: 格式化参数用于调试打印

SyscallHandle
~~~~~~~~~~~~~

`SyscallHandle` 结构体将系统调用号与处理函数关联:

.. code-block:: rust

pub struct SyscallHandle {
pub nr: usize, // 系统调用号
pub inner_handle: &'static dyn Syscall, // 处理函数
pub name: &'static str,
}

SyscallTable
~~~~~~~~~~~~

`SyscallTable` 管理所有系统调用:

- 固定大小512项
- 编译时初始化
- 通过系统调用号快速查找处理函数

使用方式
--------

实现系统调用
~~~~~~~~~~~~

1. 定义实现``Syscall`` trait的结构体
2. 实现``handle()``和``entry_format()``方法
3. 使用``declare_syscall!``宏注册

参考实现:`sys_write.rs <sys_write_>`_

.. _sys_write:
https://github.com/DragonOS-Community/DragonOS/blob/master/kernel/src/filesystem/vfs/syscall/sys_write.rs

注册系统调用
~~~~~~~~~~~~

使用``declare_syscall!``宏注册系统调用:

.. code-block:: rust

syscall_table_macros::declare_syscall!(SYS_WRITE, SysWriteHandle);

参数说明:

1. 系统调用名称(用于生成符号)
2. 实现``Syscall`` trait的结构体

初始化流程
----------

1. 内核启动时调用``syscall_table_init()``
2. 从链接器符号``_syscall_table``加载所有注册的系统调用
3. 填充系统调用表
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
sphinx==5.0.2
myst-parser==0.18.0
sphinx-rtd-theme
sphinxcontrib-mermaid==1.0.0
git+https://git.mirrors.dragonos.org.cn/DragonOS-Community/sphinx-multiversion.git@5858b75#egg=sphinx-multiversion
1 change: 0 additions & 1 deletion docs/syscall_api/intro.md

This file was deleted.

5 changes: 5 additions & 0 deletions kernel/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ smoltcp = { version = "=0.11.0", default-features = false, features = [
"proto-ipv4",
"proto-ipv6",
] }
syscall_table_macros = { path = "crates/syscall_table_macros" }
system_error = { path = "crates/system_error" }
uefi = { version = "=0.26.0", features = ["alloc"] }
uefi-raw = "=0.5.0"
Expand Down
7 changes: 7 additions & 0 deletions kernel/crates/syscall_table_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "syscall_table_macros"
version = "0.1.0"
edition = "2021"

[dependencies]

19 changes: 19 additions & 0 deletions kernel/crates/syscall_table_macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![no_std]
#![deny(clippy::all)]

#[macro_export]
#[allow(clippy::crate_in_macro_def)]
macro_rules! declare_syscall {
($nr:ident, $inner_handle:ident) => {
paste::paste! {
#[allow(non_upper_case_globals)]
#[link_section = ".syscall_table"]
#[used]
pub static [<HANDLE_ $nr>]: crate::syscall::table::SyscallHandle = crate::syscall::table::SyscallHandle {
nr: $nr,
inner_handle: &$inner_handle,
name: stringify!($nr),
};
}
};
}
12 changes: 11 additions & 1 deletion kernel/src/arch/loongarch64/link.ld
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,18 @@ SECTIONS
*(.tracepoint.*)
_etracepoint = .;
}
. = ALIGN(32768);

. = ALIGN(4096);
syscall_table_start_pa = .;
.syscall_table (syscall_table_start_pa):
{
_syscall_table = .;
*(.syscall_table)
*(.syscall_table.*)
_esyscall_table = .;
}

. = ALIGN(32768);
init_proc_union_start_pa = .;
.data.init_proc_union (init_proc_union_start_pa):
{ *(.data.init_proc_union) }
Expand Down
13 changes: 12 additions & 1 deletion kernel/src/arch/riscv64/link.ld
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,19 @@ SECTIONS
*(.tracepoint.*)
_etracepoint = .;
}
. = ALIGN(32768);

. = ALIGN(4096);

syscall_table_start_pa = .;
.syscall_table (syscall_table_start_pa): AT(syscall_table_start_pa - KERNEL_VMA)
{
_syscall_table = .;
*(.syscall_table)
*(.syscall_table.*)
_esyscall_table = .;
}

. = ALIGN(32768);
init_proc_union_start_pa = .;
.data.init_proc_union (init_proc_union_start_pa): AT(init_proc_union_start_pa - KERNEL_VMA)
{ *(.data.init_proc_union) }
Expand Down
18 changes: 14 additions & 4 deletions kernel/src/arch/x86_64/link.lds
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ SECTIONS
_etext = .;
__etext = .;
}
. = ALIGN(32768);
. = ALIGN(4096);
data_start_pa = .;
.data (data_start_pa): AT(data_start_pa - KERNEL_VMA)
{
Expand All @@ -51,7 +51,7 @@ SECTIONS
_edata = .;
}

. = ALIGN(32768);
. = ALIGN(4096);

rodata_start_pa = .;
.rodata (rodata_start_pa): AT(rodata_start_pa - KERNEL_VMA)
Expand All @@ -65,7 +65,7 @@ SECTIONS
_erodata = .;
}

. = ALIGN(32768);
. = ALIGN(4096);

trace_point_start_pa = .;
.tracepoint (trace_point_start_pa): AT(trace_point_start_pa - KERNEL_VMA)
Expand All @@ -75,8 +75,18 @@ SECTIONS
*(.tracepoint.*)
_etracepoint = .;
}
. = ALIGN(32768);
. = ALIGN(4096);

syscall_table_start_pa = .;
.syscall_table (syscall_table_start_pa): AT(syscall_table_start_pa - KERNEL_VMA)
{
_syscall_table = .;
*(.syscall_table)
*(.syscall_table.*)
_esyscall_table = .;
}

. = ALIGN(32768);
init_proc_union_start_pa = .;
.data.init_proc_union (init_proc_union_start_pa): AT(init_proc_union_start_pa - KERNEL_VMA)
{ *(.data.init_proc_union) }
Expand Down
3 changes: 1 addition & 2 deletions kernel/src/filesystem/mbr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use alloc::{
sync::{Arc, Weak},
vec::Vec,
};
use log::debug;
use system_error::SystemError;

use crate::{
Expand Down Expand Up @@ -106,7 +105,7 @@ impl MbrDiskPartionTable {
table.dpte[i].starting_lba = cursor.read_u32()?;
table.dpte[i].total_sectors = cursor.read_u32()?;

debug!("dpte[{i}] = {:?}", table.dpte[i]);
// debug!("dpte[{i}] = {:?}", table.dpte[i]);
}
table.bs_trailsig = cursor.read_u16()?;
// debug!("bs_trailsig = {}", unsafe {
Expand Down
Loading