From 90cbd846baf34103354ab770be08fbdd84183e25 Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Tue, 16 Jan 2024 23:58:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5rust=E7=9A=84=E5=8E=9F?= =?UTF-8?q?=E7=94=9F=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .gitmodules | 3 +++ README.md | 8 ++++++-- rustlib | 1 + src/Makefile | 15 ++++++++++++++- src/kernel/Makefile | 7 ++++--- src/kernel/klog/klog.rs | 0 src/kernel/main.c | 2 -- src/kernel/memm/memm.rs | 23 +++++++++++++++++++++++ src/kernel/memm/mod.rs | 3 +++ src/kernel/mod.rs | 1 + src/lib.rs | 2 ++ src/libk/Makefile | 5 +++-- src/libk/mod.rs | 0 src/metaverse.lds | 4 ++++ src/scripts/depcheck | 2 +- 16 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 .gitmodules create mode 160000 rustlib create mode 100644 src/kernel/klog/klog.rs create mode 100644 src/kernel/memm/memm.rs create mode 100644 src/kernel/memm/mod.rs create mode 100644 src/kernel/mod.rs create mode 100644 src/lib.rs create mode 100644 src/libk/mod.rs diff --git a/.gitignore b/.gitignore index c91dffe..0797d65 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ *.elf metaverse_kernel kerndisass.txt +orgprof /build diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..f4f86c9 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "rustlib"] + path = rustlib + url = https://github.com/metaverse-kernel/rustenv.git diff --git a/README.md b/README.md index 35b2a91..5fbad58 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,11 @@ 基于multiboot2引导的64位内核。 -## 试用 +## 获取 + +```bash +git clone --recursive https://github.com/metaverse-kernel/kernel-dev.git +``` * 编译 @@ -66,4 +70,4 @@ make debug 版权所有 (c) 2023 - 现在 Random World Studio 保留所有权利。 -本软件使用MIT开源许可证进行许可。详细信息请参阅许可证文件。 +本软件通过MIT开源许可证开源。详细信息请参阅许可证文件。 diff --git a/rustlib b/rustlib new file mode 160000 index 0000000..f8d0d53 --- /dev/null +++ b/rustlib @@ -0,0 +1 @@ +Subproject commit f8d0d53a23b5a91848d8f6491122b69f79994537 diff --git a/src/Makefile b/src/Makefile index b647f1e..e938645 100644 --- a/src/Makefile +++ b/src/Makefile @@ -17,9 +17,22 @@ ifdef release DEFINES := ${DEFINES} release=1 endif +RUSTLIB_PATH = ../rustlib/${ARCH}/lib +RUST_LIBS = "${RUSTLIB_PATH}/libaddr2line.rlib" "${RUSTLIB_PATH}/libadler.rlib" \ + "${RUSTLIB_PATH}/liballoc.rlib" "${RUSTLIB_PATH}/libcfg_if.rlib" "${RUSTLIB_PATH}/libcompiler_builtins.rlib" \ + "${RUSTLIB_PATH}/libcore.rlib" "${RUSTLIB_PATH}/libgetopts.rlib" "${RUSTLIB_PATH}/libgimli.rlib" \ + "${RUSTLIB_PATH}/libhashbrown.rlib" "${RUSTLIB_PATH}/libmemchr.rlib" "${RUSTLIB_PATH}/libminiz_oxide.rlib" \ + "${RUSTLIB_PATH}/libobject.rlib" "${RUSTLIB_PATH}/libpanic_abort.rlib" "${RUSTLIB_PATH}/libpanic_unwind.rlib" \ + "${RUSTLIB_PATH}/libproc_macro.rlib" "${RUSTLIB_PATH}/libprofiler_builtins.rlib" \ + "${RUSTLIB_PATH}/librustc_demangle.rlib" "${RUSTLIB_PATH}/librustc_std_workspace_alloc.rlib" \ + "${RUSTLIB_PATH}/librustc_std_workspace_core.rlib" "${RUSTLIB_PATH}/librustc_std_workspace_std.rlib" \ + "${RUSTLIB_PATH}/libstd_detect.rlib" "${RUSTLIB_PATH}/libstd.rlib" "${RUSTLIB_PATH}/libsysroot.rlib" \ + "${RUSTLIB_PATH}/libtest.rlib" "${RUSTLIB_PATH}/libunicode_width.rlib" "${RUSTLIB_PATH}/libunwind.rlib" + metaverse.elf: kernel libk metaverse.lds @echo -e "\e[1;33mld\e[0m \e[1;32m$@\e[0m \e[34m<--\e[0m \e[32m${SUBOBJS}\e[0m" - @ld -T metaverse.lds -Map=metaverse.map -o $@ ${SUBOBJS} 2>&1 | "${SOURCE}/colorize" "warning:=yellow" "error:=red" "ld=lyellow" + @ld -T metaverse.lds -Map=metaverse.map -unresolved-symbols=ignore-all -o $@ ${SUBOBJS} ${RUST_LIBS} \ + 2>&1 | "${SOURCE}/colorize" "warning:=yellow" "error:=red" "ld=lyellow" .PHONY: kernel libk all clear postproc diff --git a/src/kernel/Makefile b/src/kernel/Makefile index 4bdad92..b00ac2d 100644 --- a/src/kernel/Makefile +++ b/src/kernel/Makefile @@ -19,9 +19,10 @@ C_OBJS = ${C_SRCS:.c=.c.o} ################################ # rust语音环境变量 -RSCFLAGS = --emit obj --crate-type lib -L crate="${PWD}/src/" +RSCFLAGS = --emit obj --crate-type lib \ + -L crate="${PWD}/../rustlib/${ARCH}/src/" -RS_SRCS = klog.rs +RS_SRCS = klog.rs memm.rs RS_OBJS = ${RS_SRCS:.rs=.rs.o} ################################ @@ -43,7 +44,7 @@ S_OBJS = ${S_SRCS:.s=.s.o} OBJS = ${S_OBJS} ${C_OBJS} ${RS_OBJS} -STRIP_SECS = -R .comment -R .note.GNU-stack +STRIP_SECS = -R .note.GNU-stack OBJCOPY_FLAGS = ${STRIP_SECS} diff --git a/src/kernel/klog/klog.rs b/src/kernel/klog/klog.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/kernel/main.c b/src/kernel/main.c index 42ebb71..aa211a2 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -47,8 +47,6 @@ void kmain(void *mb2_bootinfo) tty_text_print(tty0, "Metaverse", gen_color(0x0a, 0xee, 0x0a), gen_color(0, 0, 0)); tty_text_print(tty0, "!\n", gen_color(0xee, 0xee, 0xee), gen_color(0, 0, 0)); - // 初始化内核日志模块 - while (true) { } diff --git a/src/kernel/memm/memm.rs b/src/kernel/memm/memm.rs new file mode 100644 index 0000000..471c332 --- /dev/null +++ b/src/kernel/memm/memm.rs @@ -0,0 +1,23 @@ +extern crate core; + +use core::alloc::{GlobalAlloc, Layout}; + +extern "C" { + pub fn memm_allocate(size: usize, pid: usize) -> *mut u8; + pub fn memm_free(mem: *mut u8); +} + +pub struct KernelAllocator {} + +unsafe impl GlobalAlloc for KernelAllocator { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + memm_allocate(layout.size(), 0) + } + + unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { + memm_free(ptr); + } +} + +#[global_allocator] +static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator {}; diff --git a/src/kernel/memm/mod.rs b/src/kernel/memm/mod.rs new file mode 100644 index 0000000..e8e3ff8 --- /dev/null +++ b/src/kernel/memm/mod.rs @@ -0,0 +1,3 @@ +pub mod memm; + + diff --git a/src/kernel/mod.rs b/src/kernel/mod.rs new file mode 100644 index 0000000..4361596 --- /dev/null +++ b/src/kernel/mod.rs @@ -0,0 +1 @@ +pub mod memm; \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..e08d1f9 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,2 @@ +pub mod kernel; +pub mod libk; diff --git a/src/libk/Makefile b/src/libk/Makefile index aec1bdb..e5a5601 100644 --- a/src/libk/Makefile +++ b/src/libk/Makefile @@ -19,7 +19,8 @@ C_OBJS = ${C_SRCS:.c=.c.o} ################################ # rust语音环境变量 -RSCFLAGS = --emit obj --crate-type lib -L crate="${PWD}/src/" +RSCFLAGS = --emit obj --crate-type lib \ + -L crate="~/.rustup/toolchains/stable-${ARCH}-unknown-linux-gnu/lib/rustlib/src/rust/library/" RS_SRCS = RS_OBJS = ${RS_SRCS:.rs=.rs.o} @@ -42,7 +43,7 @@ S_OBJS = ${S_SRCS:.s=.s.o} OBJS = ${S_OBJS} ${C_OBJS} ${RS_OBJS} -STRIP_SECS = -R .comment -R .note.GNU-stack +STRIP_SECS = -R .note.GNU-stack OBJCOPY_FLAGS = ${STRIP_SECS} diff --git a/src/libk/mod.rs b/src/libk/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/metaverse.lds b/src/metaverse.lds index c5296d2..7bb371b 100644 --- a/src/metaverse.lds +++ b/src/metaverse.lds @@ -40,6 +40,10 @@ SECTIONS { { *(.bss) } + .comment : + { + *(.comment) + } .eh_frame : { *(.eh_frame) diff --git a/src/scripts/depcheck b/src/scripts/depcheck index fc9ecf0..9b20fda 100755 --- a/src/scripts/depcheck +++ b/src/scripts/depcheck @@ -34,7 +34,7 @@ softwares = { "doas": ["doas"], "gcc": ["gcc"], "nasm": ["nasm"], - "rust": ["rustc", "rustdoc", "rust-lldb"], + "rust": ["rustup", "rustc", "rustdoc", "rust-lldb"], "qemu": ["qemu-img", "qemu-nbd", "qemu-system-"+arch], }