Merge pull request #1 from pointertobios/main

加入Rust语言原生支持
This commit is contained in:
pointer-to-bios 2024-01-17 00:11:25 +08:00 committed by GitHub
commit b74ea601a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
35 changed files with 101 additions and 52 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开源许可证开源。详细信息请参阅许可证文件。

View File

@ -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);
/*

View File

@ -2,6 +2,7 @@
#define RAW_H 1
#include <types.h>
#include <kernel/memm.h>
#define MEMM_RAW_ALLOCATOR 1
@ -9,6 +10,8 @@ typedef struct __raw_allocator_cell
{
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)

View File

@ -59,8 +59,6 @@ typedef struct __tty
tty_typeinfo typeinfo;
tty_mode mode;
tty_text_state text;
allocator_t *allocator;
} tty;
// tty控制器

View File

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

1
rustlib Submodule

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

View File

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

View File

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

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

View File

@ -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 对于用户空间的地址需要先转换到内核地址后释放
}

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;

View File

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

2
src/lib.rs Normal file
View File

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

View File

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

View File

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

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