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
5 changes: 3 additions & 2 deletions .github/workflows/makefile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,18 @@ jobs:
uses: actions/cache@v3
env:
cache-name: cache-build-tools
dadk_version: 0.1.1
with:
path: |
~/.cargo
~/.rustup
~/.bashrc
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('.github/workflows/makefile.yml') }}
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ env.dadk_version }}-${{ hashFiles('.github/workflows/makefile.yml') }}

- if: ${{ steps.cache-build-tools.outputs.cache-hit != 'true' }}
name: Install toolchain
continue-on-error: true
run: sudo apt install -y llvm-dev libclang-dev clang gcc-multilib && cargo install cargo-binutils && rustup toolchain install nightly && rustup default nightly && rustup component add rust-src && rustup component add llvm-tools-preview && rustup target add x86_64-unknown-none && rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu && cargo install --git https://github.com/DragonOS-Community/DADK.git
run: sudo sh -c "apt update && apt install -y llvm-dev libclang-dev clang gcc-multilib libssl-dev" && cargo install cargo-binutils && rustup toolchain install nightly && rustup default nightly && rustup component add rust-src && rustup component add llvm-tools-preview && rustup target add x86_64-unknown-none && rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu && cargo install dadk --version 0.1.1

- name: build the DragonOS
run: bash -c "source ~/.cargo/env && export DragonOS_GCC=$HOME/opt/dragonos-gcc/gcc-x86_64-unknown-none/bin && make -j $(nproc) "
7 changes: 7 additions & 0 deletions kernel/src/arch/x86_64/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ pub fn cpu_relax() {
asm!("pause");
}
}

/// 重置cpu
pub fn cpu_reset() -> ! {
// 重启计算机
unsafe { x86::io::outb(0x64, 0xfe) };
loop {}
}
1 change: 1 addition & 0 deletions kernel/src/arch/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ pub mod mm;
pub mod pci;
pub mod rand;
pub mod sched;
pub mod syscall;

pub use interrupt::X86_64InterruptArch as CurrentIrqArch;
113 changes: 113 additions & 0 deletions kernel/src/arch/x86_64/syscall.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use core::ffi::c_void;

use crate::{
include::bindings::bindings::{
pt_regs, set_system_trap_gate, verify_area, CLONE_FS, CLONE_SIGNAL, CLONE_VM, PAGE_4K_SIZE,
},
ipc::signal::sys_rt_sigreturn,
kinfo,
syscall::{Syscall, SystemError, SYS_EXECVE, SYS_FORK, SYS_RT_SIGRETURN, SYS_VFORK},
};

use super::{
asm::{ptrace::user_mode},
mm::barrier::mfence,
};

extern "C" {
fn do_fork(regs: *mut pt_regs, clone_flags: u64, stack_start: u64, stack_size: u64) -> u64;
fn c_sys_execve(
path: *const u8,
argv: *const *const u8,
envp: *const *const u8,
regs: &mut pt_regs,
) -> u64;

fn syscall_int();
}

macro_rules! syscall_return {
($val:expr, $regs:expr) => {{
let ret = $val;
$regs.rax = ret as u64;
return;
}};
}

#[no_mangle]
pub extern "C" fn syscall_handler(regs: &mut pt_regs) -> () {
let syscall_num = regs.rax as usize;
let args = [
regs.r8 as usize,
regs.r9 as usize,
regs.r10 as usize,
regs.r11 as usize,
regs.r12 as usize,
regs.r13 as usize,
regs.r14 as usize,
regs.r15 as usize,
];
mfence();
mfence();
let from_user = user_mode(regs);

// 由于进程管理未完成重构,有些系统调用需要在这里临时处理,以后这里的特殊处理要删掉。
match syscall_num {
SYS_FORK => unsafe {
syscall_return!(do_fork(regs, 0, regs.rsp, 0), regs);
},
SYS_VFORK => unsafe {
syscall_return!(
do_fork(
regs,
(CLONE_VM | CLONE_FS | CLONE_SIGNAL) as u64,
regs.rsp,
0,
),
regs
);
},
SYS_EXECVE => {
let path_ptr = args[0];
let argv_ptr = args[1];
let env_ptr = args[2];

// 权限校验
if from_user
&& (unsafe { !verify_area(path_ptr as u64, PAGE_4K_SIZE as u64) }
|| unsafe { !verify_area(argv_ptr as u64, PAGE_4K_SIZE as u64) })
|| unsafe { !verify_area(env_ptr as u64, PAGE_4K_SIZE as u64) }
{
syscall_return!(SystemError::EFAULT.to_posix_errno() as u64, regs);
} else {
syscall_return!(
unsafe {
c_sys_execve(
path_ptr as *const u8,
argv_ptr as *const *const u8,
env_ptr as *const *const u8,
regs,
)
},
regs
);
}
}

SYS_RT_SIGRETURN => {
syscall_return!(sys_rt_sigreturn(regs), regs);
}
// SYS_SCHED => {
// syscall_return!(sched(from_user) as u64, regs);
// }
_ => {}
}
syscall_return!(Syscall::handle(syscall_num, &args, from_user) as u64, regs);
}

/// 系统调用初始化
pub fn arch_syscall_init() -> Result<(), SystemError> {
kinfo!("arch_syscall_init\n");
unsafe { set_system_trap_gate(0x80, 0, syscall_int as *mut c_void) }; // 系统调用门
return Ok(());
}
3 changes: 1 addition & 2 deletions kernel/src/driver/disk/ahci/ahcidisk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ impl AhciDisk {
buf_ptr = kbuf.as_mut().unwrap().as_mut_ptr() as usize;
}


#[allow(unused_unsafe)]
let cmdtbl = unsafe {
(phys_2_virt(volatile_read!(cmdheader.ctba) as usize) as *mut HbaCmdTable)
Expand Down Expand Up @@ -259,7 +258,7 @@ impl AhciDisk {
// 设置数据存放地址
compiler_fence(core::sync::atomic::Ordering::SeqCst);
let mut buf_ptr = buf as *const [u8] as *mut usize as usize;

// 由于目前的内存管理机制无法把用户空间的内存地址转换为物理地址,所以只能先把数据拷贝到内核空间
// TODO:在内存管理重构后,可以直接使用用户空间的内存地址
let user_buf = if unsafe { verify_area(buf_ptr as u64, buf.len() as u64) } {
Expand Down
3 changes: 1 addition & 2 deletions kernel/src/driver/tty/tty_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
include::bindings::bindings::{textui_putchar, BLACK, WHITE},
kerror,
libs::rwlock::RwLock,
syscall::SystemError, kdebug, arch::asm::current::current_pcb,
syscall::SystemError,
};

use super::{TtyCore, TtyError, TtyFileFlag, TtyFilePrivateData};
Expand Down Expand Up @@ -263,7 +263,6 @@ impl IndexNode for TtyDevice {
}
return Ok(());
}

}

impl TtyDevicePrivateData {
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/exception/entry.S
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ ENTRY(virtualization_exception)
ENTRY(syscall_int)
pushq $0
pushq %rax
leaq do_syscall_int(%rip), %rax // 获取系统调用服务程序的地址
leaq syscall_handler(%rip), %rax // 获取系统调用服务程序的地址
xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
jmp Err_Code

Expand Down
111 changes: 1 addition & 110 deletions kernel/src/filesystem/vfs/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ use core::{
use alloc::{boxed::Box, format, string::ToString, sync::Arc};

use crate::{
arch::asm::current::current_pcb,
driver::disk::ahci::{self},
filesystem::{
devfs::DevFS,
fat::fs::FATFileSystem,
procfs::ProcFS,
ramfs::RamFS,
sysfs::SysFS,
vfs::{file::File, mount::MountFS, FileSystem, FileType},
vfs::{mount::MountFS, FileSystem, FileType},
},
include::bindings::bindings::PAGE_4K_SIZE,
io::SeekFrom,
kerror, kinfo,
syscall::SystemError,
};
Expand Down Expand Up @@ -201,113 +199,6 @@ pub extern "C" fn mount_root_fs() -> i32 {
return 0;
}

/// @brief 为当前进程打开一个文件
pub fn do_open(path: &str, mode: FileMode) -> Result<i32, SystemError> {
// 文件名过长
if path.len() > PAGE_4K_SIZE as usize {
return Err(SystemError::ENAMETOOLONG);
}

let inode: Result<Arc<dyn IndexNode>, SystemError> = ROOT_INODE().lookup(path);

let inode: Arc<dyn IndexNode> = if inode.is_err() {
let errno = inode.unwrap_err();
// 文件不存在,且需要创建
if mode.contains(FileMode::O_CREAT)
&& !mode.contains(FileMode::O_DIRECTORY)
&& errno == SystemError::ENOENT
{
let (filename, parent_path) = rsplit_path(path);
// 查找父目录
let parent_inode: Arc<dyn IndexNode> =
ROOT_INODE().lookup(parent_path.unwrap_or("/"))?;
// 创建文件
let inode: Arc<dyn IndexNode> = parent_inode.create(filename, FileType::File, 0o777)?;
inode
} else {
// 不需要创建文件,因此返回错误码
return Err(errno);
}
} else {
inode.unwrap()
};

let file_type: FileType = inode.metadata()?.file_type;
// 如果要打开的是文件夹,而目标不是文件夹
if mode.contains(FileMode::O_DIRECTORY) && file_type != FileType::Dir {
return Err(SystemError::ENOTDIR);
}

// 如果O_TRUNC,并且,打开模式包含O_RDWR或O_WRONLY,清空文件
if mode.contains(FileMode::O_TRUNC)
&& (mode.contains(FileMode::O_RDWR) || mode.contains(FileMode::O_WRONLY))
&& file_type == FileType::File
{
inode.truncate(0)?;
}

// 创建文件对象
let mut file: File = File::new(inode, mode)?;

// 打开模式为“追加”
if mode.contains(FileMode::O_APPEND) {
file.lseek(SeekFrom::SeekEnd(0))?;
}

// 把文件对象存入pcb
return current_pcb().alloc_fd(file, None);
}

/// @brief 根据文件描述符,读取文件数据。尝试读取的数据长度与buf的长度相同。
///
/// @param fd 文件描述符编号
/// @param buf 输出缓冲区。
///
/// @return Ok(usize) 成功读取的数据的字节数
/// @return Err(SystemError) 读取失败,返回posix错误码
pub fn do_read(fd: i32, buf: &mut [u8]) -> Result<usize, SystemError> {
let file: Option<&mut File> = current_pcb().get_file_mut_by_fd(fd);
if file.is_none() {
return Err(SystemError::EBADF);
}
let file: &mut File = file.unwrap();

return file.read(buf.len(), buf);
}

/// @brief 根据文件描述符,向文件写入数据。尝试写入的数据长度与buf的长度相同。
///
/// @param fd 文件描述符编号
/// @param buf 输入缓冲区。
///
/// @return Ok(usize) 成功写入的数据的字节数
/// @return Err(SystemError) 写入失败,返回posix错误码
pub fn do_write(fd: i32, buf: &[u8]) -> Result<usize, SystemError> {
let file: Option<&mut File> = current_pcb().get_file_mut_by_fd(fd);
if file.is_none() {
return Err(SystemError::EBADF);
}
let file: &mut File = file.unwrap();

return file.write(buf.len(), buf);
}

/// @brief 调整文件操作指针的位置
///
/// @param fd 文件描述符编号
/// @param seek 调整的方式
///
/// @return Ok(usize) 调整后,文件访问指针相对于文件头部的偏移量
/// @return Err(SystemError) 调整失败,返回posix错误码
pub fn do_lseek(fd: i32, seek: SeekFrom) -> Result<usize, SystemError> {
let file: Option<&mut File> = current_pcb().get_file_mut_by_fd(fd);
if file.is_none() {
return Err(SystemError::EBADF);
}
let file: &mut File = file.unwrap();
return file.lseek(seek);
}

/// @brief 创建文件/文件夹
pub fn do_mkdir(path: &str, _mode: FileMode) -> Result<u64, SystemError> {
// 文件名过长
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/filesystem/vfs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl File {
}
_ => {}
}

let pos: i64;
match origin {
SeekFrom::SeekSet(offset) => {
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/filesystem/vfs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use self::{core::generate_inode_id, file::FileMode};
pub use self::{core::ROOT_INODE, file::FilePrivateData, mount::MountFS};

/// vfs容许的最大的路径名称长度
pub const MAX_PATHLEN: u32 = 1024;
pub const MAX_PATHLEN: usize = 1024;

/// 定义inode号的类型为usize
pub type InodeId = usize;
Expand Down
Loading