This commit is contained in:
pointer-to-bios 2024-01-21 17:17:58 +08:00
commit 7c08af1103
21 changed files with 246 additions and 58 deletions

2
.gitignore vendored
View File

@ -5,7 +5,7 @@
*.elf
metaverse_kernel
kerndisass.txt
orgprof
/orgprof
/build
/target
/sources

2
.gitmodules vendored
View File

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

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "metaverse"
version = "0.1.0"

4
Cargo.toml Normal file
View File

@ -0,0 +1,4 @@
[package]
name = "metaverse"
version = "0.1.0"
edition = "2021"

View File

@ -1,4 +1,6 @@
# Metaverse内核
# Metaverse
![logo](https://avatars.githubusercontent.com/u/156706034?s=400&u=d971c0c8990c8c49ff4863e2366ecbebc00cf9ae&v=4)
基于multiboot2引导的64位内核。

View File

@ -3,5 +3,7 @@
#include <types.h>
// 具有返回值是为了留出一个寄存器用于调整栈顶
extern usize prepare_stack();
#endif

View File

@ -24,6 +24,8 @@
}
#define simple_lock_unlock(lock) (lock) = false;
extern void kmain_rust();
extern void *kend; // 内核结束的标记
#endif

View File

@ -6,13 +6,13 @@
typedef enum __tty_type
{
invalid,
invalid = 0,
// 用于在内核刚刚被引导只有bootloader提供的显示功能时使用
tty_type_raw_framebuffer,
tty_type_raw_framebuffer = 1,
// 用于图形功能初始化后,直接连接图形接口
tty_type_display,
tty_type_display = 2,
// 用于图形终端的终端模拟器
tty_type_vtty,
tty_type_vtty = 3,
} tty_type;
typedef enum __framebuffer_pixel_type
@ -40,8 +40,8 @@ typedef struct __framebuffer framebuffer;
// 文本模式中的字符由tty模块渲染
typedef enum __tty_mode
{
tty_mode_text,
tty_mode_graphics,
tty_mode_text = 0,
tty_mode_graphics = 1,
} tty_mode;
typedef struct __tty_text_state
@ -93,6 +93,13 @@ tty *tty_new(tty_type type, tty_mode mode);
*/
tty **tty_get(usize id);
/**
* @brief tty的id
*
* @return usize
*/
usize tty_get_id(tty *__tty);
/**
* @brief type为raw_framebuffer时设置帧缓冲区
*

View File

@ -1,3 +1,5 @@
.SUFFIXES:
ARCH := $(shell uname -m)
PWD := $(shell pwd)
SOURCE := ${PWD}/scripts
@ -10,13 +12,27 @@ ifdef release
release = 1
endif
SUBOBJS = kernel/kernel.o libk/libk.o
SUBOBJS = kernel/kernel.o libk/libk.o rust.o
DEFINES = ARCH="${ARCH}" ASM="${ASM}" ASMFLAGS="${ASMFLAGS}" SOURCE="${SOURCE}" PWD="${PWD}"
ifdef release
DEFINES := ${DEFINES} release=1
endif
################################
# rust语言环境变量
RSCFLAGS = --emit obj --crate-type lib \
-L crate="${PWD}/../rustlib/${ARCH}/src/" \
-C code-model=large \
-C relocation-model=static \
-C embed-bitcode=no \
-C opt-level=z
ifeq (${ARCH},x86_64)
RSCFLAGS := ${RSCFLAGS} -C target-feature=-sse,-avx
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" \
@ -29,12 +45,14 @@ RUST_LIBS = "${RUSTLIB_PATH}/libaddr2line.rlib" "${RUSTLIB_PATH}/libadler.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
################################
metaverse.elf: kernel libk rust 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 -unresolved-symbols=ignore-all -o $@ ${SUBOBJS} ${RUST_LIBS} \
2>&1 | "${SOURCE}/colorize" "warning:=yellow" "error:=red" "ld=lyellow"
.PHONY: kernel libk all clear postproc
.PHONY: kernel libk all clear postproc rust
all: postproc metaverse.elf
@ -57,7 +75,11 @@ libk:
@make -C libk all --no-print-directory ${DEFINES}
@echo -e "\e[33m-------------------------\e[0m"
rust:
@echo -e "\e[1m\e[33mrustc\e[0m \e[34m-->\e[0m \e[1m\e[32m$@.o\e[0m"
@rustc ${RSCFLAGS} lib.rs -o rust.o
clear:
@make -C kernel clear --no-print-directory ${DEFINES}
@make -C libk clear --no-print-directory ${DEFINES}
@-rm metaverse.elf metaverse.map
@-rm metaverse.elf metaverse.map rust.o

View File

@ -16,17 +16,6 @@ C_OBJS = ${C_SRCS:.c=.c.o}
################################
################################
# rust语音环境变量
RSCFLAGS = --emit obj --crate-type lib \
-L crate="${PWD}/../rustlib/${ARCH}/src/"
RS_SRCS = klog.rs memm.rs
RS_OBJS = ${RS_SRCS:.rs=.rs.o}
################################
################################
# 汇编语言环境变量
@ -37,12 +26,12 @@ endif
ASMFLAGS := ${ASMFLAGS}
ASMFLAGS32 = -f elf32
S_SRCS = entry32.s entry.s memm_${ARCH}.s
S_SRCS = entry32.s entry.s memm_${ARCH}.s kernel.s
S_OBJS = ${S_SRCS:.s=.s.o}
################################
OBJS = ${S_OBJS} ${C_OBJS} ${RS_OBJS}
OBJS = ${S_OBJS} ${C_OBJS}
STRIP_SECS = -R .note.GNU-stack
@ -55,10 +44,6 @@ VPATH = memm/ memm/allocator tty/ klog/ arch/${ARCH}
@echo -e "\e[1m\e[33m${CC}\e[0m \e[32m$<\e[0m \e[34m-->\e[0m \e[1m\e[32m$@\e[0m"
@${CC} -c ${CCFLAGS} $< -o $@
%.rs.o: %.rs
@echo -e "\e[1m\e[33mrustc\e[0m \e[32m$<\e[0m \e[34m-->\e[0m \e[1m\e[32m$@\e[0m"
@rustc ${RSCFLAGS} $< -o $@
%32.s.o: arch/${ARCH}/%32.s
@echo -e "\e[1m\e[33m${ASM}\e[0m \e[32m$<\e[0m \e[34m-->\e[0m \e[1m\e[32m$@\e[0m"
@${ASM} ${ASMFLAGS32} -o $@ $< 2>&1 \

View File

@ -0,0 +1,21 @@
section .text
global prepare_stack
prepare_stack:
push rdx
push rbx
mov rax, rsp
add rax, 16
xor rdx, rdx
mov rbx, 16
div rbx
mov rax, rdx
pop rbx
pop rdx
sub rsp, rax
add rax, rsp
sub rsp, 16
mov rax, [rax]
mov [rsp], rax
ret

View File

@ -37,19 +37,15 @@ void kmain(void *mb2_bootinfo)
// 初始化tty模块
tty_controller_t *tty_controler = tty_controller_new();
framebuffer fb;
get_frame_buffer_with_bootinfo(&fb, &bootinfo);
tty *tty0 = tty_new(tty_type_raw_framebuffer, tty_mode_text);
tty_set_framebuffer(tty0, &fb);
tty_text_print(tty0, "Hello ", gen_color(0xee, 0xee, 0xee), gen_color(0, 0, 0));
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));
// 为rust准备正确对齐的栈
prepare_stack();
while (true)
{
}
kmain_rust();
}
void get_frame_buffer_with_bootinfo(framebuffer *fb, bootinfo_t *bootinfo)

18
src/kernel/main.rs Normal file
View File

@ -0,0 +1,18 @@
use std::panic;
use crate::kernel::tty::tty::{Color, MessageBuilder, Tty};
#[no_mangle]
extern "C" fn kmain_rust() {
let tty = Tty::from_id(0).unwrap();
// let ttyptr = unsafe { tty.get_c_tty_t() };
let hello = MessageBuilder::new()
.message("Hello, ".to_string())
.message("Metaverse".to_string())
.foreground_color(Color(0xa, 0xee, 0xa))
.message("!\n".to_string())
.build();
tty.print(hello);
loop {}
}

View File

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

View File

@ -1 +1,3 @@
pub mod memm;
pub mod memm;
pub mod tty;
pub mod main;

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

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

View File

@ -41,6 +41,11 @@ tty **tty_get(usize id)
return &tty_ctrler.ttys[id];
}
usize tty_get_id(tty *__tty)
{
return __tty->id;
}
void tty_set_framebuffer(tty *ttyx, framebuffer *fb)
{
if (ttyx->type != tty_type_raw_framebuffer)

129
src/kernel/tty/tty.rs Normal file
View File

@ -0,0 +1,129 @@
use std::{
io::{BufWriter, Write},
ptr::null_mut,
};
extern "C" {
fn tty_new(tty_type: u8, mode: u8) -> *mut u8;
fn tty_get(id: usize) -> *mut *mut u8;
pub fn tty_text_print(ttyx: *mut u8, string: *mut u8, color: u32, bgcolor: u32);
fn tty_get_id(tty: *mut u8) -> usize;
}
pub enum Type {
Invalid = 0,
RawFramebuffer = 1,
Display = 2,
VirtualTty = 3,
}
pub enum Mode {
Text = 0,
Graphics = 1,
}
pub struct Tty {
tty_pointer: *mut u8,
}
impl Tty {
pub fn new(tty_type: Type, tty_mode: Mode) -> Self {
let tty = unsafe { tty_new(tty_type as u8, tty_mode as u8) };
Self { tty_pointer: tty }
}
pub fn from_id(id: usize) -> Option<Self> {
let tty = unsafe { tty_get(id) };
if tty == null_mut() {
None
} else {
let tty = unsafe { *tty };
Some(Self { tty_pointer: tty })
}
}
pub unsafe fn get_c_tty_t(&self) -> *mut u8 {
self.tty_pointer
}
pub fn id(&self) -> usize {
unsafe { tty_get_id(self.tty_pointer) }
}
pub fn print(&self, msg: Message) {
for MessageSection {
mut msg,
fgcolor,
bgcolor,
} in msg.into_iter()
{
unsafe {
let string = msg.as_bytes_mut() as *mut [u8] as *mut u8;
let string = string.offset(msg.len() as isize);
let swp = *string;
*string = 0;
tty_text_print(
self.tty_pointer,
msg.as_bytes_mut() as *mut [u8] as *mut u8,
u32::from(fgcolor),
u32::from(bgcolor),
);
*string = swp;
};
}
}
}
pub struct Color(pub u8, pub u8, pub u8);
impl From<Color> for u32 {
fn from(value: Color) -> Self {
let res = (value.0 as u32) << 16 | (value.1 as u32) << 8 | (value.2 as u32);
res
}
}
pub struct MessageSection {
msg: String,
fgcolor: Color,
bgcolor: Color,
}
type Message = Vec<MessageSection>;
pub struct MessageBuilder {
msg: Message,
}
impl MessageBuilder {
pub fn new() -> Self {
Self { msg: vec![] }
}
pub fn message(mut self, msg: String) -> Self {
self.msg.push(MessageSection {
msg,
fgcolor: Color(0xee, 0xee, 0xee),
bgcolor: Color(0, 0, 0),
});
self
}
pub fn background_color(mut self, color: Color) -> Self {
if let Some(msg) = self.msg.last_mut() {
msg.bgcolor = color;
}
self
}
pub fn foreground_color(mut self, color: Color) -> Self {
if let Some(msg) = self.msg.last_mut() {
msg.fgcolor = color;
}
self
}
pub fn build(self) -> Message {
self.msg
}
}

View File

@ -1,2 +1,4 @@
extern crate core;
pub mod kernel;
pub mod libk;

View File

@ -16,17 +16,6 @@ C_OBJS = ${C_SRCS:.c=.c.o}
################################
################################
# rust语音环境变量
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}
################################
################################
# 汇编语言环境变量
@ -41,7 +30,7 @@ S_OBJS = ${S_SRCS:.s=.s.o}
################################
OBJS = ${S_OBJS} ${C_OBJS} ${RS_OBJS}
OBJS = ${S_OBJS} ${C_OBJS}
STRIP_SECS = -R .note.GNU-stack
@ -54,10 +43,6 @@ VPATH = multiboot2/ string/
@echo -e "\e[1m\e[33m${CC}\e[0m \e[32m$<\e[0m \e[34m-->\e[0m \e[1m\e[32m$@\e[0m"
@${CC} -c ${CCFLAGS} $< -o $@
%.rs.o: %.rs
@echo -e "\e[1m\e[33mrustc\e[0m \e[32m$<\e[0m \e[34m-->\e[0m \e[1m\e[32m$@\e[0m"
@rustc ${RSCFLAGS} $< -o $@
%.s.o: arch/${ARCH}/%.s
@echo -e "\e[1m\e[33m${ASM}\e[0m \e[32m$<\e[0m \e[34m-->\e[0m \e[1m\e[32m$@\e[0m"
@${ASM} ${ASMFLAGS} -o $@ $< 2>&1 | "${SOURCE}/colorize" "warning:=pink" "error:=red"

View File

@ -8,7 +8,7 @@ BIOS = bios/${ARCH}/OVMF_CODE.fd
run:
@doas modprobe nbd
@make load
@qemu-system-${ARCH} -m 4G metaverse.img -bios ${BIOS}
@qemu-system-${ARCH} -accel kvm -m 4G metaverse.img -bios ${BIOS}
debug:
@echo "在gdb中连接远程目标'localhost:1234'即可"