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/src/include/kernel/arch/x86_64/kernel.h b/include/kernel/arch/x86_64/kernel.h similarity index 100% rename from src/include/kernel/arch/x86_64/kernel.h rename to include/kernel/arch/x86_64/kernel.h diff --git a/src/include/kernel/arch/x86_64/memm.h b/include/kernel/arch/x86_64/memm.h similarity index 100% rename from src/include/kernel/arch/x86_64/memm.h rename to include/kernel/arch/x86_64/memm.h diff --git a/src/include/kernel/kernel.h b/include/kernel/kernel.h similarity index 100% rename from src/include/kernel/kernel.h rename to include/kernel/kernel.h diff --git a/src/include/kernel/klog.h b/include/kernel/klog.h similarity index 100% rename from src/include/kernel/klog.h rename to include/kernel/klog.h diff --git a/src/include/kernel/memm.h b/include/kernel/memm.h similarity index 91% rename from src/include/kernel/memm.h rename to include/kernel/memm.h index c686151..27557a7 100644 --- a/src/include/kernel/memm.h +++ b/include/kernel/memm.h @@ -64,9 +64,6 @@ typedef struct __allocator_iterator_t { allocator_t *allocator; struct __allocator_iterator_t *left, *right; - - // 这个节点的内存空间处于的那个分配器 - allocator_t *owned_allocator; } allocator_iterator_t; /* @@ -137,13 +134,19 @@ pid=0时为内核分配 所有内存在内核空间都有对物理内存空间的直接映射,也就是线性地址与物理地址相同,称为内核地址 allocator对象在进程与内核之间传递时一律使用内核空间的映射地址 + +## 要求在返回的地址前16字节处保留8字节空间作为它所在的allocator地址,并且不需要分配器的具体实现做这个事 */ -void *memm_allocate(usize size, usize pid, allocator_t **allocator); +void *memm_allocate(usize size, usize pid); +#define memm_addr_set_allocator(mem, allocator) \ + *(allocator_t **)(mem - 16) = allocator; +#define memm_addr_get_allocator(mem) \ + ((*(allocator_t **)(mem - 16))) /* 释放内存 */ -void memm_free(allocator_t *allocator, void *mem); +void memm_free(void *mem); /* 寻找大小合适的一组页 diff --git a/src/include/kernel/memm/allocator/raw.h b/include/kernel/memm/allocator/raw.h similarity index 84% rename from src/include/kernel/memm/allocator/raw.h rename to include/kernel/memm/allocator/raw.h index b2f00b3..96fbe90 100644 --- a/src/include/kernel/memm/allocator/raw.h +++ b/include/kernel/memm/allocator/raw.h @@ -2,13 +2,16 @@ #define RAW_H 1 #include +#include #define MEMM_RAW_ALLOCATOR 1 typedef struct __raw_allocator_cell { - usize capacity; // 是content的长度 - usize length; // 是实际使用的长度 + usize capacity; // 是content的长度 + usize length; // 是实际使用的长度 + allocator_t *allocator; // 所在的分配器 + usize reserved; u8 content[0]; } raw_allocator_cell; #define raw_allocator_next_cell(cell) (raw_allocator_cell *)((void *)((cell)->content) + (cell)->capacity) diff --git a/src/include/kernel/tty.h b/include/kernel/tty.h similarity index 98% rename from src/include/kernel/tty.h rename to include/kernel/tty.h index 5d2ef22..d68689f 100644 --- a/src/include/kernel/tty.h +++ b/include/kernel/tty.h @@ -59,8 +59,6 @@ typedef struct __tty tty_typeinfo typeinfo; tty_mode mode; tty_text_state text; - - allocator_t *allocator; } tty; // tty控制器 diff --git a/src/include/libk/bits.h b/include/libk/bits.h similarity index 100% rename from src/include/libk/bits.h rename to include/libk/bits.h diff --git a/src/include/libk/lst.h b/include/libk/lst.h similarity index 83% rename from src/include/libk/lst.h rename to include/libk/lst.h index b57a9df..4cc7e4d 100644 --- a/src/include/libk/lst.h +++ b/include/libk/lst.h @@ -16,10 +16,6 @@ typedef struct __lst_line_t typedef struct __lst_iterator_t { lst_line_t line; - // 这个指针是kernel/memm.h中的allocator_t * - // 代表当前lst_line_index_t对象在allocate时使用的allocator - // 用的话会造成递归include - void *allocator; struct __lst_iterator_t *next; } lst_iterator_t; diff --git a/src/include/libk/math.h b/include/libk/math.h similarity index 100% rename from src/include/libk/math.h rename to include/libk/math.h diff --git a/src/include/libk/multiboot2.h b/include/libk/multiboot2.h similarity index 100% rename from src/include/libk/multiboot2.h rename to include/libk/multiboot2.h diff --git a/src/include/libk/multiboot2/bootinfo.h b/include/libk/multiboot2/bootinfo.h similarity index 100% rename from src/include/libk/multiboot2/bootinfo.h rename to include/libk/multiboot2/bootinfo.h diff --git a/src/include/libk/string.h b/include/libk/string.h similarity index 100% rename from src/include/libk/string.h rename to include/libk/string.h diff --git a/src/include/types.h b/include/types.h similarity index 100% rename from src/include/types.h rename to include/types.h diff --git a/src/include/utils.h b/include/utils.h similarity index 100% rename from src/include/utils.h rename to include/utils.h 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 39da335..e938645 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,5 +1,6 @@ ARCH := $(shell uname -m) -SOURCE := $(shell pwd)/scripts +PWD := $(shell pwd) +SOURCE := ${PWD}/scripts ifeq (${ARCH},x86_64) ASM = nasm ASMFLAGS = -f elf64 @@ -11,14 +12,27 @@ endif SUBOBJS = kernel/kernel.o libk/libk.o -DEFINES = ARCH="${ARCH}" ASM="${ASM}" ASMFLAGS="${ASMFLAGS}" SOURCE="${SOURCE}" +DEFINES = ARCH="${ARCH}" ASM="${ASM}" ASMFLAGS="${ASMFLAGS}" SOURCE="${SOURCE}" PWD="${PWD}" 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 969fe82..b00ac2d 100644 --- a/src/kernel/Makefile +++ b/src/kernel/Makefile @@ -1,15 +1,17 @@ +.SUFFIXES: + ################################ # C语言环境变量 CC = gcc -CCFLAGS = -m64 -mcmodel=large -I ../include \ +CCFLAGS = -m64 -mcmodel=large -I ../../include \ -fno-stack-protector -fno-exceptions \ -fno-builtin -nostdinc -nostdlib ifdef release CCFLAGS := ${CCFLAGS} -O2 endif -C_SRCS = main.c tty.c klog.c font.c memm.c memm_${ARCH}.c raw.c +C_SRCS = main.c tty.c font.c memm.c memm_${ARCH}.c raw.c C_OBJS = ${C_SRCS:.c=.c.o} ################################ @@ -17,9 +19,10 @@ C_OBJS = ${C_SRCS:.c=.c.o} ################################ # rust语音环境变量 -RSCFLAGS = --emit=obj --target x86_64-unknown-linux-gnu --crate-type=bin +RSCFLAGS = --emit obj --crate-type lib \ + -L crate="${PWD}/../rustlib/${ARCH}/src/" -RS_SRCS = +RS_SRCS = klog.rs memm.rs RS_OBJS = ${RS_SRCS:.rs=.rs.o} ################################ @@ -34,14 +37,14 @@ endif ASMFLAGS := ${ASMFLAGS} ASMFLAGS32 = -f elf32 -S_SRCS = entry32.s entry.s memm_${ARCH}_s.s +S_SRCS = entry32.s entry.s memm_${ARCH}.s 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/arch/x86_64/memm_x86_64_s.s b/src/kernel/arch/x86_64/memm_x86_64.s similarity index 100% rename from src/kernel/arch/x86_64/memm_x86_64_s.s rename to src/kernel/arch/x86_64/memm_x86_64.s diff --git a/src/kernel/klog/klog.c b/src/kernel/klog/klog.rs similarity index 100% rename from src/kernel/klog/klog.c rename to src/kernel/klog/klog.rs 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.c b/src/kernel/memm/memm.c index d722fe4..aecfe24 100644 --- a/src/kernel/memm/memm.c +++ b/src/kernel/memm/memm.c @@ -29,7 +29,6 @@ mem_manager_t *memm_new(usize mem_size) alcatr_ind->allocator = allocator0; alcatr_ind->left = nullptr; alcatr_ind->right = nullptr; - alcatr_ind->owned_allocator = allocator0; memory_manager.allocators = alcatr_ind; @@ -159,11 +158,12 @@ static void insert_allocator(allocator_iterator_t *iter, allocator_iterator_t *i } } -void *memm_allocate(usize size, usize pid, allocator_t **allocator) +void *memm_allocate(usize size, usize pid) { usize orgsize = size; // 从分配器树中分配内存 - void *ptr = memm_find_and_allocate(memory_manager.allocators, size, pid, allocator); + allocator_t *allocator; + void *ptr = memm_find_and_allocate(memory_manager.allocators, size, pid, &allocator); if (ptr != nullptr) goto after_allocation; @@ -190,11 +190,9 @@ void *memm_allocate(usize size, usize pid, allocator_t **allocator) allocator_t *new_allocator = memm_allocator_new((void *)(allocator_start * MEMM_PAGE_SIZE), size * MEMM_PAGE_SIZE, MEMM_RAW_ALLOCATOR, pid); - *allocator = new_allocator; + allocator = new_allocator; - allocator_t *all; - allocator_iterator_t *allind = memm_allocate(sizeof(allocator_iterator_t), 0, &all); - allind->owned_allocator = all; + allocator_iterator_t *allind = memm_allocate(sizeof(allocator_iterator_t), 0); allind->allocator = new_allocator; allind->left = nullptr; allind->right = nullptr; @@ -202,6 +200,7 @@ void *memm_allocate(usize size, usize pid, allocator_t **allocator) ptr = new_allocator->allocate(&new_allocator->allocator_instance, orgsize, 0); after_allocation: + memm_addr_set_allocator(ptr, allocator); if (pid != 0) { // TODO 进程管理中应该有一个用户地址-内核地址映射表 // 在进程分配时将页映射到用户空间中,并将这个映射关系记录进这个表中 @@ -210,8 +209,9 @@ after_allocation: return ptr; } -void memm_free(allocator_t *allocator, void *mem) +void memm_free(void *mem) { + allocator_t *allocator = memm_addr_get_allocator(mem); if (is_user_address((u64)mem)) { // TODO 对于用户空间的地址需要先转换到内核地址后释放 } 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/kernel/tty/tty.c b/src/kernel/tty/tty.c index 7c208d2..86a1b46 100644 --- a/src/kernel/tty/tty.c +++ b/src/kernel/tty/tty.c @@ -16,12 +16,10 @@ tty_controller_t *tty_controller_new() tty *tty_new(tty_type type, tty_mode mode) { - allocator_t *allocator; - tty *__tty = memm_allocate(sizeof(tty), 0, &allocator); + tty *__tty = memm_allocate(sizeof(tty), 0); memset(__tty, 0, sizeof(tty)); __tty->type = type; __tty->mode = mode; - __tty->allocator = allocator; tty *res = nullptr; for (usize i = 0; i < TTY_MAX_NUM; ++i) { 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 9a200e3..e5a5601 100644 --- a/src/libk/Makefile +++ b/src/libk/Makefile @@ -1,13 +1,10 @@ -ARCH := $(shell uname -m) -ifdef release - release = 1 -endif +.SUFFIXES: ################################ # C语言环境变量 CC = gcc -CCFLAGS = -m64 -mcmodel=large -I ../include \ +CCFLAGS = -m64 -mcmodel=large -I ../../include \ -fno-stack-protector -fno-exceptions \ -fno-builtin -nostdinc -nostdlib ifdef release @@ -22,7 +19,8 @@ C_OBJS = ${C_SRCS:.c=.c.o} ################################ # rust语音环境变量 -RSCFLAGS = --emit=obj --target x86_64-unknown-linux-gnu --crate-type=bin +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} @@ -45,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/lst.c b/src/libk/lst.c index 262ceb9..cbfcce0 100644 --- a/src/libk/lst.c +++ b/src/libk/lst.c @@ -7,11 +7,9 @@ lst_iterator_t *lst_new(usize start, usize end) { - allocator_t *allocator; - lst_iterator_t *lst = memm_allocate(sizeof(lst_iterator_t), 0, &allocator); + lst_iterator_t *lst = memm_allocate(sizeof(lst_iterator_t), 0); lst->line.left = start; lst->line.right = end; - lst->allocator = allocator; lst->next = nullptr; return lst; } @@ -130,9 +128,7 @@ bool lst_add(lst_iterator_t *lst, usize left, usize right, bool force) lst->line.left = left; else { - allocator_t *alloctr; - lst_iterator_t *new_node = memm_allocate(sizeof(lst_iterator_t), 0, &alloctr); - new_node->allocator = alloctr; + lst_iterator_t *new_node = memm_allocate(sizeof(lst_iterator_t), 0); new_node->line = line; new_node->next = lst; if (last != nullptr) @@ -169,7 +165,7 @@ bool lst_add(lst_iterator_t *lst, usize left, usize right, bool force) } tmplast = *tmpnode; lst_iterator_t *t = lst_next(tmpnode); - memm_free(tmpnode->allocator, tmpnode); + memm_free(tmpnode); tmpnode = t; } lst->line.right = max(tmplast.line.right, right); 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], }