Skip to content

Commit

Permalink
使用rust重写了apic的驱动 (#425)
Browse files Browse the repository at this point in the history
* 使用rust重写了apic的驱动。

* 修正signal和调度器的部分加锁逻辑,增加回退策略。

* 把pcb的flags字段替换为无锁的

* 使用cargo管理apic的编译

* 删除makefile中指定PIC的变量

---------

Co-authored-by: Gou Ngai <[email protected]>
Co-authored-by: 櫻井桃華 <[email protected]>
  • Loading branch information
3 people authored Nov 7, 2023
1 parent 4935c74 commit 70a4e55
Show file tree
Hide file tree
Showing 63 changed files with 2,637 additions and 1,366 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,8 @@
"./kernel/Cargo.toml",
"./kernel/src/libs/ida/Cargo.toml"
],
"rust-analyzer.check.overrideCommand": [
"make",
"check"
],
}
1 change: 1 addition & 0 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ backtrace = []
[dependencies]
x86 = "0.52.0"
x86_64 = "0.14.10"
bit_field = "0.10"
bitflags = "1.3.2"
bitfield-struct = "0.5.3"
virtio-drivers = { git = "https://git.mirrors.dragonos.org/DragonOS-Community/virtio-drivers.git", rev = "f1d1cbb" }
Expand Down
4 changes: 4 additions & 0 deletions kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ clean:
.PHONY: fmt
fmt:
cargo fmt --all $(FMT_CHECK)


check:
cargo +nightly-2023-01-21 check --workspace --message-format=json --target ./src/arch/x86_64/x86_64-unknown-none.json
73 changes: 71 additions & 2 deletions kernel/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ impl CFilesBuilder {
c.define("__x86_64__", None);
}

c.define("PIC", "_INTR_APIC_");
}

fn setup_global_include_dir(c: &mut Build) {
Expand All @@ -105,6 +104,76 @@ impl CFilesBuilder {

/// 设置需要编译的文件
fn setup_files(c: &mut Build) {
c.file("src/arch/x86_64/driver/hpet.c");
let mut files = Vec::new();

#[cfg(target_arch = "x86_64")]
Self::setup_files_x86_64(&mut files);

Self::set_rerun_if_files_changed(&files);
c.files(files.as_slice());
}

/// 设置x86_64架构下需要编译的C文件
fn setup_files_x86_64(files: &mut Vec<PathBuf>) {
files.push(PathBuf::from("src/arch/x86_64/driver/hpet.c"));
// 获取`kernel/src/arch/x86_64/driver/apic`下的所有C文件
files.append(&mut FileUtils::list_all_files(
&PathBuf::from("src/arch/x86_64/driver/apic"),
Some("c"),
true,
));
}

/// 设置Cargo对文件更改的监听
fn set_rerun_if_files_changed(files: &Vec<PathBuf>) {
for f in files {
println!("cargo:rerun-if-changed={}", f.to_str().unwrap());
}
}
}

struct FileUtils;

impl FileUtils {
/// 列出指定目录下的所有文件
///
/// ## 参数
///
/// - `path` - 指定的目录
/// - `ext_name` - 文件的扩展名,如果为None,则列出所有文件
/// - `recursive` - 是否递归列出所有文件
pub fn list_all_files(path: &PathBuf, ext_name: Option<&str>, recursive: bool) -> Vec<PathBuf> {
let mut queue: Vec<PathBuf> = Vec::new();
let mut result = Vec::new();
queue.push(path.clone());

while !queue.is_empty() {
let path = queue.pop().unwrap();
let d = std::fs::read_dir(path);
if d.is_err() {
continue;
}
let d = d.unwrap();

d.for_each(|ent| {
if let Ok(ent) = ent {
if let Ok(file_type) = ent.file_type() {
if file_type.is_file() {
if let Some(e) = ext_name {
if let Some(ext) = ent.path().extension() {
if ext == e {
result.push(ent.path());
}
}
}
} else if file_type.is_dir() && recursive {
queue.push(ent.path());
}
}
}
});
}

return result;
}
}
12 changes: 4 additions & 8 deletions kernel/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ lib_patterns := *.a
LIB_FILES := $(foreach DIR,$(DIR_LIB),$(addprefix $(DIR)/,$(lib_patterns)))


# 控制操作系统使用的中断控制器 _INTR_8259A_ _INTR_APIC_
PIC := _INTR_APIC_

# unwind/backtrace related
UNWIND_ENABLE ?= yes
CFLAGS_UNWIND =
Expand All @@ -22,14 +19,14 @@ ifeq ($(UNWIND_ENABLE), yes)
RUSTFLAGS_UNWIND = -Cforce-unwind-tables -Clink-arg=-Wl,eh_frame.ld
endif

CFLAGS = $(GLOBAL_CFLAGS) -fno-pie $(CFLAGS_UNWIND) -D $(PIC) -I $(shell pwd) -I $(shell pwd)/include -I $(shell pwd)/arch/x86_64/include
CFLAGS = $(GLOBAL_CFLAGS) -fno-pie $(CFLAGS_UNWIND) -I $(shell pwd) -I $(shell pwd)/include -I $(shell pwd)/arch/x86_64/include

export ASFLAGS := --64

LD_LIST := head.o


kernel_subdirs := common driver debug arch exception smp sched syscall ktest libs time
kernel_subdirs := common driver debug arch exception smp syscall ktest libs time


head.o: head.S
Expand All @@ -43,9 +40,8 @@ main.o: main.c
$(CC) $(CFLAGS) -c main.c -o main.o

kernel_rust:
rustup default nightly

RUSTFLAGS="$(RUSTFLAGS_UNWIND)" cargo +nightly-2023-01-21 build --release --target ./arch/x86_64/x86_64-unknown-none.json

all: kernel

@echo "Linking kernel..."
Expand Down Expand Up @@ -78,7 +74,7 @@ ECHO:

$(kernel_subdirs): ECHO

$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" PIC="$(PIC)" kernel_root_path="$(shell pwd)"
$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" kernel_root_path="$(shell pwd)"

kernel: head.o main.o $(kernel_subdirs) kernel_rust

Expand Down
2 changes: 1 addition & 1 deletion kernel/src/arch/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ all:
@list='$(kernel_arch_subdirs)'; for subdir in $$list; do \
echo "make all in $$subdir";\
cd $$subdir;\
$(MAKE) all CFLAGS="$(CFLAGS)" PIC="$(PIC)";\
$(MAKE) all CFLAGS="$(CFLAGS)" ;\
cd ..;\
done

Expand Down
2 changes: 1 addition & 1 deletion kernel/src/arch/x86_64/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $(kernel_arch_x86_64_objs): ECHO
$(CC) $(CFLAGS) -c $@ -o $@.o

$(kernel_arch_x86_64_subdirs): ECHO
$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" PIC="$(PIC)"
$(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)"

all: $(kernel_arch_x86_64_objs) $(kernel_arch_x86_64_subdirs)

Expand Down
2 changes: 1 addition & 1 deletion kernel/src/arch/x86_64/asm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $(kernel_arch_x86_64_asm_objs): ECHO
$(CC) $(CFLAGS) -c $@ -o $@.o

# $(kernel_arch_x86_64_asm_subdirs): ECHO
# $(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)" PIC="$(PIC)"
# $(MAKE) -C $@ all CFLAGS="$(CFLAGS)" ASFLAGS="$(ASFLAGS)"

all: $(kernel_arch_x86_64_asm_objs)

Expand Down
Loading

0 comments on commit 70a4e55

Please sign in to comment.