加入rust的原生支持

This commit is contained in:
pointer-to-bios 2024-01-16 23:58:11 +08:00
parent b5a65fb341
commit 90cbd846ba
16 changed files with 66 additions and 11 deletions

1
.gitignore vendored
View File

@ -5,4 +5,5 @@
*.elf
metaverse_kernel
kerndisass.txt
orgprof
/build

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "rustlib"]
path = rustlib
url = https://github.com/metaverse-kernel/rustenv.git

View File

@ -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开源许可证开源。详细信息请参阅许可证文件。

1
rustlib Submodule

@ -0,0 +1 @@
Subproject commit f8d0d53a23b5a91848d8f6491122b69f79994537

View File

@ -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

View File

@ -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}

0
src/kernel/klog/klog.rs Normal file
View File

View File

@ -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)
{
}

23
src/kernel/memm/memm.rs Normal file
View File

@ -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 {};

3
src/kernel/memm/mod.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod memm;

1
src/kernel/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod memm;

2
src/lib.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod kernel;
pub mod libk;

View File

@ -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}

0
src/libk/mod.rs Normal file
View File

View File

@ -40,6 +40,10 @@ SECTIONS {
{
*(.bss)
}
.comment :
{
*(.comment)
}
.eh_frame :
{
*(.eh_frame)

View File

@ -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],
}