重构系统调用模块 (#267)

* 完成系统调用模块重构

* 更新github workflow
This commit is contained in:
login
2023-05-24 17:05:33 +08:00
committed by GitHub
parent 660a04cef8
commit ab5c8ca46d
35 changed files with 1870 additions and 1760 deletions

View File

@ -3,3 +3,4 @@ pub mod initial_proc;
pub mod pid;
pub mod preempt;
pub mod process;
pub mod syscall;

View File

@ -1,4 +1,4 @@
use crate::{arch::asm::current::current_pcb, include::bindings::bindings::pt_regs};
#[allow(dead_code)]
#[derive(Debug, Clone, Copy)]
@ -18,11 +18,3 @@ impl PartialEq for PidType {
*self as u8 == *other as u8
}
}
/**
* @brief 获取当前进程的pid
*/
#[no_mangle]
pub extern "C" fn sys_getpid(_regs: &pt_regs) -> u64 {
return current_pcb().pid as u64;
}

View File

@ -51,13 +51,13 @@ extern int process_init_files();
extern int rs_init_stdio();
// 设置初始进程的PCB
#define INITIAL_PROC(proc) \
{ \
.state = PROC_UNINTERRUPTIBLE, .flags = PF_KTHREAD, .preempt_count = 0, .signal = 0, .cpu_id = 0, \
.mm = &initial_mm, .thread = &initial_thread, .addr_limit = 0xffffffffffffffff, .pid = 0, .priority = 2, \
.virtual_runtime = 0, .fds = {0}, .next_pcb = &proc, .prev_pcb = &proc, .parent_pcb = &proc, .exit_code = 0, \
.wait_child_proc_exit = 0, .worker_private = NULL, .policy = SCHED_NORMAL, .sig_blocked = 0, \
.signal = &INITIAL_SIGNALS, .sighand = &INITIAL_SIGHAND, \
#define INITIAL_PROC(proc) \
{ \
.state = PROC_UNINTERRUPTIBLE, .flags = PF_KTHREAD, .preempt_count = 0, .signal = 0, .cpu_id = 0, \
.mm = &initial_mm, .thread = &initial_thread, .addr_limit = 0xffffffffffffffff, .pid = 0, .priority = 2, \
.virtual_runtime = 0, .fds = {0}, .next_pcb = &proc, .prev_pcb = &proc, .parent_pcb = &proc, .exit_code = 0, \
.wait_child_proc_exit = 0, .worker_private = NULL, .policy = SCHED_NORMAL, .sig_blocked = 0, \
.signal = &INITIAL_SIGNALS, .sighand = &INITIAL_SIGHAND, \
}
struct thread_struct initial_thread = {
@ -114,10 +114,8 @@ void __switch_to(struct process_control_block *prev, struct process_control_bloc
// initial_tss[0].ist2, initial_tss[0].ist3, initial_tss[0].ist4, initial_tss[0].ist5,
// initial_tss[0].ist6, initial_tss[0].ist7);
__asm__ __volatile__("movq %%fs, %0 \n\t"
: "=a"(prev->thread->fs));
__asm__ __volatile__("movq %%gs, %0 \n\t"
: "=a"(prev->thread->gs));
__asm__ __volatile__("movq %%fs, %0 \n\t" : "=a"(prev->thread->fs));
__asm__ __volatile__("movq %%gs, %0 \n\t" : "=a"(prev->thread->gs));
__asm__ __volatile__("movq %0, %%fs \n\t" ::"a"(next->thread->fs));
__asm__ __volatile__("movq %0, %%gs \n\t" ::"a"(next->thread->gs));
@ -144,10 +142,7 @@ void process_switch_fsgs(uint64_t fs, uint64_t gs)
*/
int process_open_exec_file(char *path)
{
struct pt_regs tmp = {0};
tmp.r8 = (uint64_t)path;
tmp.r9 = O_RDONLY;
int fd = sys_open(&tmp);
int fd = enter_syscall_int(SYS_OPEN, (uint64_t)path, O_RDONLY, 0, 0, 0, 0, 0, 0);
return fd;
}
@ -172,22 +167,12 @@ static int process_load_elf_file(struct pt_regs *regs, char *path)
void *buf = kzalloc(PAGE_4K_SIZE, 0);
uint64_t pos = 0;
struct pt_regs tmp_use_fs = {0};
tmp_use_fs.r8 = fd;
tmp_use_fs.r9 = 0;
tmp_use_fs.r10 = SEEK_SET;
retval = sys_lseek(&tmp_use_fs);
retval = enter_syscall_int(SYS_LSEEK, fd, 0, SEEK_SET, 0, 0, 0, 0, 0);
// 读取 Elf64_Ehdr
tmp_use_fs.r8 = fd;
tmp_use_fs.r9 = (uint64_t)buf;
tmp_use_fs.r10 = sizeof(Elf64_Ehdr);
retval = sys_read(&tmp_use_fs);
retval = enter_syscall_int(SYS_READ, fd, (uint64_t)buf, sizeof(Elf64_Ehdr), 0, 0, 0, 0, 0);
tmp_use_fs.r8 = fd;
tmp_use_fs.r9 = 0;
tmp_use_fs.r10 = SEEK_CUR;
pos = sys_lseek(&tmp_use_fs);
pos = enter_syscall_int(SYS_LSEEK, fd, 0, SEEK_CUR, 0, 0, 0, 0, 0);
if (retval != sizeof(Elf64_Ehdr))
{
@ -235,21 +220,14 @@ static int process_load_elf_file(struct pt_regs *regs, char *path)
// 读取所有的phdr
pos = ehdr.e_phoff;
tmp_use_fs.r8 = fd;
tmp_use_fs.r9 = pos;
tmp_use_fs.r10 = SEEK_SET;
pos = sys_lseek(&tmp_use_fs);
pos = enter_syscall_int(SYS_LSEEK, fd, pos, SEEK_SET, 0, 0, 0, 0, 0);
memset(buf, 0, PAGE_4K_SIZE);
tmp_use_fs.r8 = fd;
tmp_use_fs.r9 = (uint64_t)buf;
tmp_use_fs.r10 = (uint64_t)ehdr.e_phentsize * (uint64_t)ehdr.e_phnum;
sys_read(&tmp_use_fs);
tmp_use_fs.r8 = fd;
tmp_use_fs.r9 = 0;
tmp_use_fs.r10 = SEEK_CUR;
pos = sys_lseek(&tmp_use_fs);
enter_syscall_int(SYS_READ, fd, (uint64_t)buf, (uint64_t)ehdr.e_phentsize * (uint64_t)ehdr.e_phnum, 0, 0, 0, 0, 0);
pos = enter_syscall_int(SYS_LSEEK, fd, 0, SEEK_CUR, 0, 0, 0, 0, 0);
if ((long)retval < 0)
{
@ -328,10 +306,7 @@ static int process_load_elf_file(struct pt_regs *regs, char *path)
}
}
tmp_use_fs.r8 = fd;
tmp_use_fs.r9 = pos;
tmp_use_fs.r10 = SEEK_SET;
pos = sys_lseek(&tmp_use_fs);
pos = enter_syscall_int(SYS_LSEEK, fd, pos, SEEK_SET, 0, 0, 0, 0, 0);
int64_t val = 0;
if (remain_file_size > 0)
@ -342,17 +317,13 @@ static int process_load_elf_file(struct pt_regs *regs, char *path)
while (to_trans > 0)
{
int64_t x = 0;
tmp_use_fs.r8 = fd;
tmp_use_fs.r9 = (uint64_t)buf3;
tmp_use_fs.r10 = to_trans;
x = sys_read(&tmp_use_fs);
x = enter_syscall_int(SYS_READ, fd, (uint64_t)buf3, to_trans, 0, 0, 0, 0, 0);
memcpy(virt_base + beginning_offset + val, buf3, x);
val += x;
to_trans -= x;
tmp_use_fs.r8 = fd;
tmp_use_fs.r9 = 0;
tmp_use_fs.r10 = SEEK_CUR;
pos = sys_lseek(&tmp_use_fs);
pos = enter_syscall_int(SYS_LSEEK, fd, 0, SEEK_CUR, 0, 0, 0, 0, 0);
}
kfree(buf3);
@ -391,9 +362,7 @@ static int process_load_elf_file(struct pt_regs *regs, char *path)
load_elf_failed:;
{
struct pt_regs tmp = {0};
tmp.r8 = fd;
sys_close(&tmp);
enter_syscall_int(SYS_CLOSE, fd, 0, 0, 0, 0, 0, 0, 0);
}
if (buf != NULL)
@ -460,6 +429,7 @@ ul do_execve(struct pt_regs *regs, char *path, char *argv[], char *envp[])
// 加载elf格式的可执行文件
int tmp = process_load_elf_file(regs, path);
if (tmp < 0)
goto exec_failed;
@ -623,7 +593,7 @@ ul initial_kernel_thread(ul arg)
"m"(current_pcb->thread->rsp), "m"(current_pcb->thread->rip), "S"("/bin/shell.elf"), "c"(NULL),
"d"(NULL)
: "memory");
return 1;
}
#pragma GCC pop_options

View File

@ -0,0 +1,64 @@
use core::ffi::{c_int, c_void};
use crate::{
arch::asm::current::current_pcb,
include::bindings::bindings::{pid_t, process_do_exit},
syscall::{Syscall, SystemError},
};
extern "C" {
fn c_sys_wait4(pid: pid_t, wstatus: *mut c_int, options: c_int, rusage: *mut c_void) -> c_int;
}
impl Syscall {
#[allow(dead_code)]
pub fn fork(&self) -> Result<usize, SystemError> {
// 由于进程管理未完成重构fork调用暂时在arch/x86_64/syscall.rs中调用以后会移动到这里。
todo!()
}
#[allow(dead_code)]
pub fn vfork(&self) -> Result<usize, SystemError> {
// 由于进程管理未完成重构vfork调用暂时在arch/x86_64/syscall.rs中调用以后会移动到这里。
todo!()
}
#[allow(dead_code)]
pub fn execve(
_path: *const c_void,
_argv: *const *const c_void,
_envp: *const *const c_void,
) -> Result<usize, SystemError> {
// 由于进程管理未完成重构execve调用暂时在arch/x86_64/syscall.rs中调用以后会移动到这里。
todo!()
}
pub fn wait4(
pid: pid_t,
wstatus: *mut c_int,
options: c_int,
rusage: *mut c_void,
) -> Result<usize, SystemError> {
let ret = unsafe { c_sys_wait4(pid, wstatus, options, rusage) };
if (ret as isize) < 0 {
return Err(SystemError::from_posix_errno(-(ret as isize) as i32)
.expect("wait4: Invalid errno"));
}
return Ok(ret as usize);
}
/// # 退出进程
///
/// ## 参数
///
/// - status: 退出状态
pub fn exit(status: usize) -> ! {
unsafe { process_do_exit(status as u64) };
loop {}
}
/// # 获取进程ID
pub fn getpid() -> Result<usize, SystemError> {
return Ok(current_pcb().pid as usize);
}
}