From ad8cb92e78b8bf2999d3f0c7c497aef0a9721f10 Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Sat, 6 Apr 2024 18:07:00 +0800 Subject: [PATCH 01/18] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=86=85=E5=AD=98?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E9=87=8D=E6=9E=84=E5=90=8E=E7=9A=84=E4=B8=80?= =?UTF-8?q?=E4=BA=9Bbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/kernel/memm/memm.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/kernel/memm/memm.c b/src/kernel/memm/memm.c index c2ae788..1b7607b 100644 --- a/src/kernel/memm/memm.c +++ b/src/kernel/memm/memm.c @@ -37,7 +37,6 @@ allocator_t *memm_allocator_new(void *start, usize length, usize type, usize pid allocator_t *allocator = start; allocator->magic = MEMM_ALLOCATOR_MAGIC; allocator->full = false; - allocator->pid = 0; allocator->size = length; allocator->type = type; switch (type) @@ -71,10 +70,6 @@ void memm_free(void *mem) allocator_t *allocator = memory_manager.kernel_base_allocator; if (allocator->magic != MEMM_ALLOCATOR_MAGIC) return; - if (is_user_address((u64)mem)) - { - mem = mem - allocator->userspace + (void *)allocator; - } allocator->free(allocator->allocator_instance, mem); if (allocator->full) allocator->full = false; -- 2.43.0 From 77b8b4cd7cc32eaededd7852a672a5aeb29e7a3c Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Sun, 7 Apr 2024 05:10:42 +0800 Subject: [PATCH 02/18] =?UTF-8?q?=E6=9B=B4=E6=96=B0rust=20core=20crate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 2 ++ rustlib | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 44bc550..9a4c32c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,8 @@ name = "metaverse" version = "0.1.0" edition = "2021" + +[lib] crate-type = ["staticlib"] # 此Cargo.toml仅用于rust-analyzer识别rust部分的代码 diff --git a/rustlib b/rustlib index 6a2ca2a..bb4a5d1 160000 --- a/rustlib +++ b/rustlib @@ -1 +1 @@ -Subproject commit 6a2ca2afcd651e46f1c19f8d8a3f685fd982d824 +Subproject commit bb4a5d1b55c36070d629400bddd6d267d2f0714a -- 2.43.0 From 46b80996f730f204e7f48d5ca6665983e1b9e8f4 Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Sun, 7 Apr 2024 05:13:18 +0800 Subject: [PATCH 03/18] =?UTF-8?q?=E5=B0=86rustlib=E5=BC=95=E7=94=A8?= =?UTF-8?q?=E7=9A=84url=E5=88=87=E6=8D=A2=E5=88=B0github?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index 3712af8..f4f86c9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "rustlib"] path = rustlib - url = http://git.suthby.org:2024/metaverse/rustenv + url = https://github.com/metaverse-kernel/rustenv.git -- 2.43.0 From 8d7e2b1a889ee6164f9453046266644344e96d00 Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Mon, 15 Apr 2024 20:41:16 +0800 Subject: [PATCH 04/18] =?UTF-8?q?=E5=B0=86=E5=AD=97=E4=BD=93=E7=A7=BB?= =?UTF-8?q?=E5=87=BA=E5=86=85=E6=A0=B8=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + include/kernel/tty.h | 1 + scripts/tfgen | 57 ++ src/Makefile | 16 +- src/kernel/tty/font.c | 1896 +------------------------------------ src/ttyfonts/metamono.tfn | 1878 ++++++++++++++++++++++++++++++++++++ 6 files changed, 1960 insertions(+), 1889 deletions(-) create mode 100755 scripts/tfgen create mode 100644 src/ttyfonts/metamono.tfn diff --git a/.gitignore b/.gitignore index 6cd1797..903a407 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /test/metaverse.img *.o +*.tfo *.map *.elf metaverse_kernel diff --git a/include/kernel/tty.h b/include/kernel/tty.h index 6edf00d..1ee8b57 100644 --- a/include/kernel/tty.h +++ b/include/kernel/tty.h @@ -156,6 +156,7 @@ void tty_disable(tty *ttyx); typedef struct __tty_font_t { + bool initialized; u16 char_width, char_height; u64 **font; } tty_font_t; diff --git a/scripts/tfgen b/scripts/tfgen new file mode 100755 index 0000000..c2a91a9 --- /dev/null +++ b/scripts/tfgen @@ -0,0 +1,57 @@ +#!/usr/bin/python + +from io import TextIOWrapper +import sys +import struct + + +def main(infile: TextIOWrapper, outfile: TextIOWrapper): + size = infile.readline().strip().split(",") + size = (int(size[0].strip()), int(size[1].strip())) + outfile.write(struct.pack('q', size[0])) + outfile.write(struct.pack('q', size[1])) + for _ in range(62): + outfile.write(struct.pack('q', int(0))) + in_char = False + char_buffer = [] + char_count = 0 + for line in infile: + line = line.split("//")[0].strip() + if len(line) == 0: + continue + if line.startswith("{"): + char_buffer = [] + line = line[1:] + in_char = True + if line.endswith("},"): + i = 0 + for d in char_buffer: + outfile.write(struct.pack('q', d)) + i += 1 + while i < 64: + outfile.write(struct.pack('q', int(0))) + i += 1 + char_count += 1 + in_char = False + continue + if len(line) == 0: + continue + if in_char: + data = line.split(",")[0].strip() + data = int(data, 2) + char_buffer.append(data) + while char_count < 256: + for _ in range(64): + outfile.write(struct.pack('q', int(0))) + char_count += 1 + + +if __name__ == "__main__": + if len(sys.argv) < 3: + print("tfgen: Input file and Output file needed.") + exit(1) + infile_name = sys.argv[1] + outfile_name = sys.argv[2] + with open(infile_name) as infile: + with open(outfile_name, "wb") as outfile: + main(infile, outfile) diff --git a/src/Makefile b/src/Makefile index ffbdcc9..7532577 100644 --- a/src/Makefile +++ b/src/Makefile @@ -12,11 +12,15 @@ ifdef release release = 1 endif +ifndef ttyfont + ttyfont = metamono +endif + ALLOCATOR_MAGIC = $(shell "${SOURCE}/magicgen" | sha512sum | head -c 128 | md5sum | head -c 8) -BUILD_ID = $(shell "${SOURCE}/magicgen" | sha512sum | head -c 128 | md5sum | head -c 4) +BUILD_ID = $(shell "${SOURCE}/magicgen" | sha512sum | head -c 128 | md5sum | head -c 8) -SUBOBJS = kernel/kernel.o libk/libk.o rust.o +SUBOBJS = kernel/kernel.o libk/libk.o rust.o font_file.o DEFINES = ARCH="${ARCH}" ASM="${ASM}" ASMFLAGS="${ASMFLAGS}" SOURCE="${SOURCE}" PWD="${PWD}" \ ALLOCATOR_MAGIC="${ALLOCATOR_MAGIC}" BUILD_ID="${BUILD_ID}" @@ -50,7 +54,7 @@ RUST_LIBS = "${RUSTLIB_PATH}/liballoc.rlib" "${RUSTLIB_PATH}/libcompiler_builtin ################################ -metaverse.elf: kernel libk rust metaverse.lds +metaverse.elf: kernel libk rust metaverse.lds font_file.o @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" @@ -92,7 +96,11 @@ rust: postproc @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 +font_file.o: ttyfonts/${ttyfont}.tfn + @"${SOURCE}/tfgen" $< font_file.tfo + @ld -r -b binary -o $@ font_file.tfo + clear: @make -C kernel clear --no-print-directory ${DEFINES} @make -C libk clear --no-print-directory ${DEFINES} - @-rm metaverse.elf metaverse.map rust.o + @-rm metaverse.elf metaverse.map rust.o font_file.{o,tfo} diff --git a/src/kernel/tty/font.c b/src/kernel/tty/font.c index 57d78f5..80734a0 100644 --- a/src/kernel/tty/font.c +++ b/src/kernel/tty/font.c @@ -1,1892 +1,14 @@ #include #include +#include +#include -u16 font_width = 8, font_height = 16; - -u64 font[256][64] = { - // 带 ‘//' 的是基准线 - // 带 '///' 的是限位 - { - // 光标 - 0b00000000, - 0b01111110, - 0b01111110, /// - 0b01111110, - 0b01111110, - 0b01111110, - 0b01111110, - 0b01111110, - 0b01111110, - 0b01111110, - 0b01111110, - 0b01111110, // - 0b01111110, - 0b01111110, - 0b01111110, - 0b00000000, - }, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - { - // '\n' - // 一行不够长时,打印这个符号换行 - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b00000000, - 0b01111110, - 0b00000010, - 0b00000010, - 0b00000010, - 0b00010010, - 0b00100100, // - 0b01111000, - 0b00100000, - 0b00010000, - 0b00000000, - }, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - {}, - { - // space - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // ! - 0b00000000, - 0b00000000, - 0b00011000, /// - 0b00011000, - 0b00011000, - 0b00011000, - 0b00011000, - 0b00011000, - 0b00011000, - 0b00011000, - 0b00000000, - 0b00011000, // - 0b00011000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //" - 0b00000000, - 0b00110110, - 0b00110110, /// - 0b00110110, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // # - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00100100, - 0b00100100, - 0b01111110, - 0b00100100, - 0b00100100, - 0b01111110, - 0b00100100, - 0b00100100, - 0b00000000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //$ - 0b00000000, - 0b00001000, - 0b00011100, /// - 0b00101010, - 0b01001010, - 0b01001000, - 0b00111000, - 0b00011100, - 0b00010010, - 0b01010010, - 0b01010100, - 0b00111000, // - 0b00010000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //% - 0b00000000, - 0b00000000, - 0b00100010, /// - 0b01010100, - 0b00100100, - 0b00001000, - 0b00001000, - 0b00010000, - 0b00010000, - 0b00100100, - 0b00101010, - 0b01000100, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //& - 0b00000000, - 0b00000000, - 0b00011100, /// - 0b00100010, - 0b00100000, - 0b00010000, - 0b00010000, - 0b00101001, - 0b01000110, - 0b01000110, - 0b01000110, - 0b00111001, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //' - 0b00000000, - 0b00011000, - 0b00011000, /// - 0b00011000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //( - 0b00000000, - 0b00000100, - 0b00001000, /// - 0b00011000, - 0b00010000, - 0b00110000, - 0b00110000, - 0b00110000, - 0b00110000, - 0b00010000, - 0b00011000, - 0b00001000, // - 0b00000100, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //) - 0b00000000, - 0b00100000, - 0b00010000, /// - 0b00011000, - 0b00001000, - 0b00001100, - 0b00001100, - 0b00001100, - 0b00001100, - 0b00001000, - 0b00011000, - 0b00010000, // - 0b00100000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //* - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00001000, - 0b00001000, - 0b11011001, - 0b00111110, - 0b00011000, - 0b00100100, - 0b01000010, - 0b00000000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //+ - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b00011000, - 0b00011000, - 0b01111110, - 0b00011000, - 0b00011000, - 0b00000000, - 0b00000000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //, - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00001100, - 0b00011000, // - 0b00010000, - 0b00110000, - 0b00000000, - 0b00000000, - }, - { - //- - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b01111110, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //. - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00011000, - 0b00011000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //\/ - 0b00000000, - 0b00000010, - 0b00000010, /// - 0b00000100, - 0b00000100, - 0b00001000, - 0b00001000, - 0b00010000, - 0b00010000, - 0b00100000, - 0b00100000, - 0b01000000, // - 0b01000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // 0 - 0b00000000, - 0b00000000, - 0b00111100, /// - 0b00100100, - 0b01000010, - 0b01000010, - 0b01011010, - 0b01011010, - 0b01000010, - 0b01000010, - 0b00100100, - 0b00111100, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // 1 - 0b00000000, - 0b00000000, - 0b00011000, /// - 0b00101000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00111110, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // 2 - 0b00000000, - 0b00000000, - 0b00111100, /// - 0b01000010, - 0b01000010, - 0b00000010, - 0b00000100, - 0b00011000, - 0b00100000, - 0b01000000, - 0b01000000, - 0b01111110, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // 3 - 0b00000000, - 0b00000000, - 0b01111110, /// - 0b00000100, - 0b00001000, - 0b00011000, - 0b00000100, - 0b00000010, - 0b00000010, - 0b01000010, - 0b01100100, - 0b00011000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // 4 - 0b00000000, - 0b00000000, - 0b00000100, /// - 0b00001000, - 0b00001000, - 0b00010000, - 0b00010000, - 0b00100010, - 0b00100010, - 0b01111110, - 0b00000010, - 0b00000010, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // 5 - 0b00000000, - 0b00000000, - 0b01111100, /// - 0b01000000, - 0b01000000, - 0b01111000, - 0b01000100, - 0b00000010, - 0b00000010, - 0b00000010, - 0b01000100, - 0b00111000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // 6 - 0b00000000, - 0b00000000, - 0b00001000, /// - 0b00010000, - 0b00010000, - 0b00111000, - 0b00100100, - 0b01000010, - 0b01000010, - 0b01000010, - 0b00100100, - 0b00011000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // 7 - 0b00000000, - 0b00000000, - 0b01111110, /// - 0b01000010, - 0b00000100, - 0b00000100, - 0b00001000, - 0b00001000, - 0b00010000, - 0b00010000, - 0b00100000, - 0b00100000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // 8 - 0b00000000, - 0b00000000, - 0b00111100, /// - 0b01000010, - 0b01000010, - 0b00100100, - 0b00011000, - 0b00100100, - 0b01000010, - 0b01000010, - 0b01000010, - 0b00111100, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // 9 - 0b00000000, - 0b00000000, - 0b00111100, /// - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b00111100, - 0b00000100, - 0b00001000, - 0b00010000, - 0b00100000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //: - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00011000, - 0b00011000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00011000, - 0b00011000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //; - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00011000, - 0b00011000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00011000, - 0b00011000, // - 0b00010000, - 0b00100000, - 0b00000000, - 0b00000000, - }, - { - //< - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000110, - 0b00001000, - 0b00110000, - 0b01000000, - 0b01000000, - 0b00110000, - 0b00001000, - 0b00000110, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //= - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b01111110, - 0b00000000, - 0b00000000, - 0b00000000, - 0b01111110, - 0b00000000, - 0b00000000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //> - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b01100000, - 0b00010000, - 0b00001100, - 0b00000010, - 0b00000010, - 0b00001100, - 0b00010000, - 0b01100000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //? - 0b00000000, - 0b00000000, - 0b00111000, /// - 0b01000100, - 0b00000010, - 0b00000010, - 0b00000100, - 0b00011000, - 0b00100000, - 0b00000000, - 0b00110000, - 0b00110000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //@ - 0b00000000, - 0b00000000, - 0b00011100, /// - 0b00100010, - 0b01000010, - 0b01001110, - 0b01010010, - 0b01010010, - 0b01010010, - 0b01010010, - 0b01010010, - 0b01001110, // - 0b01000000, - 0b00100000, - 0b00011100, - 0b00000000, - }, - { - // A - 0b00000000, - 0b00000000, - 0b00011000, /// - 0b00011000, - 0b00101000, - 0b00100100, - 0b00100100, - 0b00111100, - 0b01000100, - 0b01000010, - 0b01000010, - 0b01000010, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // B - 0b00000000, - 0b00000000, - 0b01111000, /// - 0b01000100, - 0b01000010, - 0b01000100, - 0b01111000, - 0b01000100, - 0b01000010, - 0b01000010, - 0b01000100, - 0b01111000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // C - 0b00000000, - 0b00000000, - 0b00011000, /// - 0b00100100, - 0b01000010, - 0b01000000, - 0b01000000, - 0b01000000, - 0b01000000, - 0b01000010, - 0b00100100, - 0b00011000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // D - 0b00000000, - 0b00000000, - 0b01111000, /// - 0b01000100, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000100, - 0b01111000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // E - 0b00000000, - 0b00000000, - 0b01111110, /// - 0b01000000, - 0b01000000, - 0b01000000, - 0b01111110, - 0b01000000, - 0b01000000, - 0b01000000, - 0b01000000, - 0b01111110, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // F - 0b00000000, - 0b00000000, - 0b01111110, /// - 0b01000000, - 0b01000000, - 0b01000000, - 0b01111110, - 0b01000000, - 0b01000000, - 0b01000000, - 0b01000000, - 0b01000000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // G - 0b00000000, - 0b00000000, - 0b00011100, /// - 0b00100010, - 0b01000000, - 0b01000000, - 0b01000000, - 0b01001110, - 0b01000010, - 0b01000010, - 0b00100100, - 0b00011000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // H - 0b00000000, - 0b00000000, - 0b01000010, /// - 0b01000010, - 0b01000010, - 0b01000010, - 0b01111110, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // I - 0b00000000, - 0b00000000, - 0b00111110, /// - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00111110, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // J - 0b00000000, - 0b00000000, - 0b00111100, /// - 0b00000100, - 0b00000100, - 0b00000100, - 0b00000100, - 0b00000100, - 0b00000100, - 0b00000100, - 0b01000100, - 0b00111000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // K - 0b00000000, - 0b00000000, - 0b01000010, /// - 0b01000100, - 0b01001000, - 0b01110000, - 0b01010000, - 0b01001000, - 0b01001000, - 0b01000100, - 0b01000100, - 0b01000010, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // L - 0b00000000, - 0b00000000, - 0b01000000, /// - 0b01000000, - 0b01000000, - 0b01000000, - 0b01000000, - 0b01000000, - 0b01000000, - 0b01000000, - 0b01000000, - 0b01111110, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // M - 0b00000000, - 0b00000000, - 0b00100010, /// - 0b00100010, - 0b00100110, - 0b01010101, - 0b01001001, - 0b01001001, - 0b01001001, - 0b01000001, - 0b10000001, - 0b10000001, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // N - 0b00000000, - 0b00000000, - 0b01000010, /// - 0b01100010, - 0b01100010, - 0b01010010, - 0b01010010, - 0b01001010, - 0b01001010, - 0b01000110, - 0b01000110, - 0b01000010, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // O - 0b00000000, - 0b00000000, - 0b00011000, /// - 0b00100100, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b00100100, - 0b00011000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // P - 0b00000000, - 0b00000000, - 0b01111000, /// - 0b01000100, - 0b01000010, - 0b01000010, - 0b01000100, - 0b01111000, - 0b01000000, - 0b01000000, - 0b01000000, - 0b01000000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // Q - 0b00000000, - 0b00000000, - 0b00011000, /// - 0b00100100, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b00100100, - 0b00011000, // - 0b00000100, - 0b00000010, - 0b00000000, - 0b00000000, - }, - { - // R - 0b00000000, - 0b00000000, - 0b01111000, /// - 0b01000100, - 0b01000010, - 0b01000010, - 0b01111100, - 0b01001000, - 0b01000100, - 0b01000010, - 0b01000010, - 0b01000010, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // S - 0b00000000, - 0b00000000, - 0b00011100, /// - 0b00100010, - 0b01000010, - 0b01000000, - 0b00110000, - 0b00001100, - 0b00000010, - 0b01000010, - 0b01000100, - 0b00111000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // T - 0b00000000, - 0b00000000, - 0b01111111, /// - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // U - 0b00000000, - 0b00000000, - 0b01000010, /// - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b00100100, - 0b00011000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // V - 0b00000000, - 0b00000000, - 0b01000010, /// - 0b01000010, - 0b00100100, - 0b00100100, - 0b00100100, - 0b00100100, - 0b00100100, - 0b00011000, - 0b00011000, - 0b00011000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // W - 0b00000000, - 0b00000000, - 0b10000001, /// - 0b10000001, - 0b10000001, - 0b10011001, - 0b10011001, - 0b10011001, - 0b10100101, - 0b01100110, - 0b01100110, - 0b01000010, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // X - 0b00000000, - 0b00000000, - 0b01000010, /// - 0b00100100, - 0b00100100, - 0b00011000, - 0b00011000, - 0b00011000, - 0b00011000, - 0b00100100, - 0b00100100, - 0b01000010, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // Y - 0b00000000, - 0b00000000, - 0b01000010, /// - 0b01000010, - 0b01000010, - 0b00100100, - 0b00101000, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // Z - 0b00000000, - 0b00000000, - 0b01111110, /// - 0b00000100, - 0b00001000, - 0b00001000, - 0b00010000, - 0b00010000, - 0b00100000, - 0b00100000, - 0b01000000, - 0b01111110, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //[ - 0b00000000, - 0b00011100, - 0b00010000, /// - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, // - 0b00011100, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //'\' - 0b00000000, - 0b01000000, - 0b01000000, /// - 0b00100000, - 0b00100000, - 0b00010000, - 0b00010000, - 0b00001000, - 0b00001000, - 0b00000100, - 0b00000100, - 0b00000010, // - 0b00000010, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //] - 0b00000000, - 0b00111000, - 0b00001000, /// - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, // - 0b00111000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //^ - 0b00000000, - 0b00000000, - 0b00011000, /// - 0b00100100, - 0b01000010, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //_ - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, // - 0b01111110, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //` - 0b00000000, - 0b00011000, - 0b00001100, /// - 0b00000010, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // a - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b00111110, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000110, - 0b00111010, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // b - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b01000000, - 0b01000000, - 0b01011100, - 0b01100010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b00111100, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // c - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b00111100, - 0b01000010, - 0b01000000, - 0b01000000, - 0b01000000, - 0b01000010, - 0b00111100, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // d - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000010, - 0b00000010, - 0b00111010, - 0b01000110, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b00111110, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // e - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b00111100, - 0b01000010, - 0b01000010, - 0b01111100, - 0b01000000, - 0b01000010, - 0b00111100, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // f - 0b00000000, - 0b00000000, - 0b00001100, /// - 0b00010000, - 0b00010000, - 0b00010000, - 0b01111100, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, // - 0b00010000, - 0b00010000, - 0b01100000, - 0b00000000, - }, - { - // g - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b00111010, - 0b01000110, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000110, - 0b00111010, // - 0b00000010, - 0b00000010, - 0b00111100, - 0b00000000, - }, - { - // h - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b01000000, - 0b01000000, - 0b01011100, - 0b01100010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // i - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00011000, - 0b00011000, - 0b00000000, - 0b00011000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00111110, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // j - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00011000, - 0b00011000, - 0b00000000, - 0b00011000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, // - 0b00001000, - 0b00001000, - 0b01110000, - 0b00000000, - }, - { - // k - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b01000000, - 0b01000000, - 0b01000000, - 0b01000010, - 0b01100100, - 0b01011000, - 0b01001000, - 0b01000100, - 0b01000010, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // l - 0b00000000, - 0b00000000, - 0b00110000, /// - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00001100, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // m - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b01010110, - 0b01101001, - 0b01001001, - 0b01001001, - 0b01001001, - 0b01001001, - 0b01001001, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // n - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b01011100, - 0b01100010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // o - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b00111100, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b00111100, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // p - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b01011100, - 0b01100010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01100010, - 0b01011100, // - 0b01000000, - 0b01000000, - 0b01000000, - 0b00000000, - }, - { - // q - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b00111010, - 0b01000110, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000110, - 0b00111010, // - 0b00000010, - 0b00000010, - 0b00000010, - 0b00000000, - }, - { - // r - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b01011100, - 0b01100010, - 0b01000000, - 0b01000000, - 0b01000000, - 0b01000000, - 0b01000000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // s - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b00111100, - 0b01000010, - 0b01000000, - 0b00111100, - 0b00000010, - 0b01000010, - 0b00111100, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // t - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00010000, - 0b00010000, - 0b01111110, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00001110, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // u - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b00111100, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // v - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b01000010, - 0b01000010, - 0b01000010, - 0b00100100, - 0b00100100, - 0b00100100, - 0b00011000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // w - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b10001001, - 0b10001001, - 0b10001001, - 0b01011001, - 0b01010110, - 0b01100110, - 0b01000010, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // x - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b01000010, - 0b00100100, - 0b00011000, - 0b00011000, - 0b00100100, - 0b01000010, - 0b01000010, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // y - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000010, - 0b01000110, - 0b00111010, // - 0b00000010, - 0b00000010, - 0b00111100, - 0b00000000, - }, - { - // z - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b01111110, - 0b00000100, - 0b00001000, - 0b00010000, - 0b00100000, - 0b01000000, - 0b01111110, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - // { - 0b00000000, - 0b00000110, - 0b00001000, /// - 0b00001000, - 0b00001000, - 0b00001000, - 0b00010000, - 0b00010000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, // - 0b00000110, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //| - 0b00000000, - 0b00001000, - 0b00001000, /// - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, - 0b00001000, // - 0b00001000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //} - 0b00000000, - 0b01100000, - 0b00010000, /// - 0b00010000, - 0b00010000, - 0b00010000, - 0b00001000, - 0b00001000, - 0b00010000, - 0b00010000, - 0b00010000, - 0b00010000, // - 0b01100000, - 0b00000000, - 0b00000000, - 0b00000000, - }, - { - //~ - 0b00000000, - 0b00000000, - 0b00000000, /// - 0b00000000, - 0b00000000, - 0b00000000, - 0b00110010, - 0b01001100, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, // - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - }, -}; - -bool initialized = false; +extern void _binary_font_file_tfo_start; u64 *font_pointers[256]; tty_font_t refont = { + .initialized = false, .font = (u64 **)font_pointers, .char_width = 8, .char_height = 16, @@ -1894,13 +16,17 @@ tty_font_t refont = { tty_font_t *tty_get_font() { - if (initialized == false) + if (refont.initialized == false) { + void *buffer = &_binary_font_file_tfo_start; + refont.char_width = ((u64 *)buffer)[0]; + refont.char_height = ((u64 *)buffer)[1]; + void *font = buffer + sizeof(u64) * 64; for (usize i = 0; i < 256; i++) { - font_pointers[i] = font[i]; + font_pointers[i] = font + i * sizeof(u64) * 64; } - initialized = true; + refont.initialized = true; } return &refont; } diff --git a/src/ttyfonts/metamono.tfn b/src/ttyfonts/metamono.tfn new file mode 100644 index 0000000..a48d4fd --- /dev/null +++ b/src/ttyfonts/metamono.tfn @@ -0,0 +1,1878 @@ +8, 16 +// 带 ‘//' 的是基准线 +// 带 '///' 的是限位 +{ + // 光标 + 00000000, + 01111110, + 01111110, /// + 01111110, + 01111110, + 01111110, + 01111110, + 01111110, + 01111110, + 01111110, + 01111110, + 01111110, // + 01111110, + 01111110, + 01111110, + 00000000, +}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{ + // '\n' + // 一行不够长时,打印这个符号换行 + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 00000000, + 01111110, + 00000010, + 00000010, + 00000010, + 00010010, + 00100100, // + 01111000, + 00100000, + 00010000, + 00000000, +}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{}, +{ + // space + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // ! + 00000000, + 00000000, + 00011000, /// + 00011000, + 00011000, + 00011000, + 00011000, + 00011000, + 00011000, + 00011000, + 00000000, + 00011000, // + 00011000, + 00000000, + 00000000, + 00000000, +}, +{ + //" + 00000000, + 00110110, + 00110110, /// + 00110110, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // # + 00000000, + 00000000, + 00000000, /// + 00100100, + 00100100, + 01111110, + 00100100, + 00100100, + 01111110, + 00100100, + 00100100, + 00000000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + //$ + 00000000, + 00001000, + 00011100, /// + 00101010, + 01001010, + 01001000, + 00111000, + 00011100, + 00010010, + 01010010, + 01010100, + 00111000, // + 00010000, + 00000000, + 00000000, + 00000000, +}, +{ + //% + 00000000, + 00000000, + 00100010, /// + 01010100, + 00100100, + 00001000, + 00001000, + 00010000, + 00010000, + 00100100, + 00101010, + 01000100, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + //& + 00000000, + 00000000, + 00011100, /// + 00100010, + 00100000, + 00010000, + 00010000, + 00101001, + 01000110, + 01000110, + 01000110, + 00111001, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + //' + 00000000, + 00011000, + 00011000, /// + 00011000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + //( + 00000000, + 00000100, + 00001000, /// + 00011000, + 00010000, + 00110000, + 00110000, + 00110000, + 00110000, + 00010000, + 00011000, + 00001000, // + 00000100, + 00000000, + 00000000, + 00000000, +}, +{ + //) + 00000000, + 00100000, + 00010000, /// + 00011000, + 00001000, + 00001100, + 00001100, + 00001100, + 00001100, + 00001000, + 00011000, + 00010000, // + 00100000, + 00000000, + 00000000, + 00000000, +}, +{ + //* + 00000000, + 00000000, + 00000000, /// + 00000000, + 00001000, + 00001000, + 11011001, + 00111110, + 00011000, + 00100100, + 01000010, + 00000000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + //+ + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 00011000, + 00011000, + 01111110, + 00011000, + 00011000, + 00000000, + 00000000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + //, + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00001100, + 00011000, // + 00010000, + 00110000, + 00000000, + 00000000, +}, +{ + //- + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 00000000, + 00000000, + 01111110, + 00000000, + 00000000, + 00000000, + 00000000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + //. + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00011000, + 00011000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + //\/ + 00000000, + 00000010, + 00000010, /// + 00000100, + 00000100, + 00001000, + 00001000, + 00010000, + 00010000, + 00100000, + 00100000, + 01000000, // + 01000000, + 00000000, + 00000000, + 00000000, +}, +{ + // 0 + 00000000, + 00000000, + 00111100, /// + 00100100, + 01000010, + 01000010, + 01011010, + 01011010, + 01000010, + 01000010, + 00100100, + 00111100, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // 1 + 00000000, + 00000000, + 00011000, /// + 00101000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00111110, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // 2 + 00000000, + 00000000, + 00111100, /// + 01000010, + 01000010, + 00000010, + 00000100, + 00011000, + 00100000, + 01000000, + 01000000, + 01111110, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // 3 + 00000000, + 00000000, + 01111110, /// + 00000100, + 00001000, + 00011000, + 00000100, + 00000010, + 00000010, + 01000010, + 01100100, + 00011000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // 4 + 00000000, + 00000000, + 00000100, /// + 00001000, + 00001000, + 00010000, + 00010000, + 00100010, + 00100010, + 01111110, + 00000010, + 00000010, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // 5 + 00000000, + 00000000, + 01111100, /// + 01000000, + 01000000, + 01111000, + 01000100, + 00000010, + 00000010, + 00000010, + 01000100, + 00111000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // 6 + 00000000, + 00000000, + 00001000, /// + 00010000, + 00010000, + 00111000, + 00100100, + 01000010, + 01000010, + 01000010, + 00100100, + 00011000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // 7 + 00000000, + 00000000, + 01111110, /// + 01000010, + 00000100, + 00000100, + 00001000, + 00001000, + 00010000, + 00010000, + 00100000, + 00100000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // 8 + 00000000, + 00000000, + 00111100, /// + 01000010, + 01000010, + 00100100, + 00011000, + 00100100, + 01000010, + 01000010, + 01000010, + 00111100, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // 9 + 00000000, + 00000000, + 00111100, /// + 01000010, + 01000010, + 01000010, + 01000010, + 00111100, + 00000100, + 00001000, + 00010000, + 00100000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + //: + 00000000, + 00000000, + 00000000, /// + 00000000, + 00011000, + 00011000, + 00000000, + 00000000, + 00000000, + 00000000, + 00011000, + 00011000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + //; + 00000000, + 00000000, + 00000000, /// + 00000000, + 00011000, + 00011000, + 00000000, + 00000000, + 00000000, + 00000000, + 00011000, + 00011000, // + 00010000, + 00100000, + 00000000, + 00000000, +}, +{ + //< + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000110, + 00001000, + 00110000, + 01000000, + 01000000, + 00110000, + 00001000, + 00000110, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + //= + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 01111110, + 00000000, + 00000000, + 00000000, + 01111110, + 00000000, + 00000000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + //> + 00000000, + 00000000, + 00000000, /// + 00000000, + 01100000, + 00010000, + 00001100, + 00000010, + 00000010, + 00001100, + 00010000, + 01100000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + //? + 00000000, + 00000000, + 00111000, /// + 01000100, + 00000010, + 00000010, + 00000100, + 00011000, + 00100000, + 00000000, + 00110000, + 00110000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + //@ + 00000000, + 00000000, + 00011100, /// + 00100010, + 01000010, + 01001110, + 01010010, + 01010010, + 01010010, + 01010010, + 01010010, + 01001110, // + 01000000, + 00100000, + 00011100, + 00000000, +}, +{ + // A + 00000000, + 00000000, + 00011000, /// + 00011000, + 00101000, + 00100100, + 00100100, + 00111100, + 01000100, + 01000010, + 01000010, + 01000010, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // B + 00000000, + 00000000, + 01111000, /// + 01000100, + 01000010, + 01000100, + 01111000, + 01000100, + 01000010, + 01000010, + 01000100, + 01111000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // C + 00000000, + 00000000, + 00011000, /// + 00100100, + 01000010, + 01000000, + 01000000, + 01000000, + 01000000, + 01000010, + 00100100, + 00011000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // D + 00000000, + 00000000, + 01111000, /// + 01000100, + 01000010, + 01000010, + 01000010, + 01000010, + 01000010, + 01000010, + 01000100, + 01111000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // E + 00000000, + 00000000, + 01111110, /// + 01000000, + 01000000, + 01000000, + 01111110, + 01000000, + 01000000, + 01000000, + 01000000, + 01111110, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // F + 00000000, + 00000000, + 01111110, /// + 01000000, + 01000000, + 01000000, + 01111110, + 01000000, + 01000000, + 01000000, + 01000000, + 01000000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // G + 00000000, + 00000000, + 00011100, /// + 00100010, + 01000000, + 01000000, + 01000000, + 01001110, + 01000010, + 01000010, + 00100100, + 00011000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // H + 00000000, + 00000000, + 01000010, /// + 01000010, + 01000010, + 01000010, + 01111110, + 01000010, + 01000010, + 01000010, + 01000010, + 01000010, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // I + 00000000, + 00000000, + 00111110, /// + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00111110, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // J + 00000000, + 00000000, + 00111100, /// + 00000100, + 00000100, + 00000100, + 00000100, + 00000100, + 00000100, + 00000100, + 01000100, + 00111000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // K + 00000000, + 00000000, + 01000010, /// + 01000100, + 01001000, + 01110000, + 01010000, + 01001000, + 01001000, + 01000100, + 01000100, + 01000010, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // L + 00000000, + 00000000, + 01000000, /// + 01000000, + 01000000, + 01000000, + 01000000, + 01000000, + 01000000, + 01000000, + 01000000, + 01111110, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // M + 00000000, + 00000000, + 00100010, /// + 00100010, + 00100110, + 01010101, + 01001001, + 01001001, + 01001001, + 01000001, + 10000001, + 10000001, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // N + 00000000, + 00000000, + 01000010, /// + 01100010, + 01100010, + 01010010, + 01010010, + 01001010, + 01001010, + 01000110, + 01000110, + 01000010, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // O + 00000000, + 00000000, + 00011000, /// + 00100100, + 01000010, + 01000010, + 01000010, + 01000010, + 01000010, + 01000010, + 00100100, + 00011000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // P + 00000000, + 00000000, + 01111000, /// + 01000100, + 01000010, + 01000010, + 01000100, + 01111000, + 01000000, + 01000000, + 01000000, + 01000000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // Q + 00000000, + 00000000, + 00011000, /// + 00100100, + 01000010, + 01000010, + 01000010, + 01000010, + 01000010, + 01000010, + 00100100, + 00011000, // + 00000100, + 00000010, + 00000000, + 00000000, +}, +{ + // R + 00000000, + 00000000, + 01111000, /// + 01000100, + 01000010, + 01000010, + 01111100, + 01001000, + 01000100, + 01000010, + 01000010, + 01000010, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // S + 00000000, + 00000000, + 00011100, /// + 00100010, + 01000010, + 01000000, + 00110000, + 00001100, + 00000010, + 01000010, + 01000100, + 00111000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // T + 00000000, + 00000000, + 01111111, /// + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // U + 00000000, + 00000000, + 01000010, /// + 01000010, + 01000010, + 01000010, + 01000010, + 01000010, + 01000010, + 01000010, + 00100100, + 00011000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // V + 00000000, + 00000000, + 01000010, /// + 01000010, + 00100100, + 00100100, + 00100100, + 00100100, + 00100100, + 00011000, + 00011000, + 00011000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // W + 00000000, + 00000000, + 10000001, /// + 10000001, + 10000001, + 10011001, + 10011001, + 10011001, + 10100101, + 01100110, + 01100110, + 01000010, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // X + 00000000, + 00000000, + 01000010, /// + 00100100, + 00100100, + 00011000, + 00011000, + 00011000, + 00011000, + 00100100, + 00100100, + 01000010, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // Y + 00000000, + 00000000, + 01000010, /// + 01000010, + 01000010, + 00100100, + 00101000, + 00010000, + 00010000, + 00010000, + 00010000, + 00010000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // Z + 00000000, + 00000000, + 01111110, /// + 00000100, + 00001000, + 00001000, + 00010000, + 00010000, + 00100000, + 00100000, + 01000000, + 01111110, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + //[ + 00000000, + 00011100, + 00010000, /// + 00010000, + 00010000, + 00010000, + 00010000, + 00010000, + 00010000, + 00010000, + 00010000, + 00010000, // + 00011100, + 00000000, + 00000000, + 00000000, +}, +{ + //'\' + 00000000, + 01000000, + 01000000, /// + 00100000, + 00100000, + 00010000, + 00010000, + 00001000, + 00001000, + 00000100, + 00000100, + 00000010, // + 00000010, + 00000000, + 00000000, + 00000000, +}, +{ + //] + 00000000, + 00111000, + 00001000, /// + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, // + 00111000, + 00000000, + 00000000, + 00000000, +}, +{ + //^ + 00000000, + 00000000, + 00011000, /// + 00100100, + 01000010, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + //_ + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, // + 01111110, + 00000000, + 00000000, + 00000000, +}, +{ + //` + 00000000, + 00011000, + 00001100, /// + 00000010, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, + 00000000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // a + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 00111110, + 01000010, + 01000010, + 01000010, + 01000010, + 01000110, + 00111010, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // b + 00000000, + 00000000, + 00000000, /// + 01000000, + 01000000, + 01011100, + 01100010, + 01000010, + 01000010, + 01000010, + 01000010, + 00111100, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // c + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 00111100, + 01000010, + 01000000, + 01000000, + 01000000, + 01000010, + 00111100, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // d + 00000000, + 00000000, + 00000000, /// + 00000010, + 00000010, + 00111010, + 01000110, + 01000010, + 01000010, + 01000010, + 01000010, + 00111110, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // e + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 00111100, + 01000010, + 01000010, + 01111100, + 01000000, + 01000010, + 00111100, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // f + 00000000, + 00000000, + 00001100, /// + 00010000, + 00010000, + 00010000, + 01111100, + 00010000, + 00010000, + 00010000, + 00010000, + 00010000, // + 00010000, + 00010000, + 01100000, + 00000000, +}, +{ + // g + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 00111010, + 01000110, + 01000010, + 01000010, + 01000010, + 01000110, + 00111010, // + 00000010, + 00000010, + 00111100, + 00000000, +}, +{ + // h + 00000000, + 00000000, + 00000000, /// + 01000000, + 01000000, + 01011100, + 01100010, + 01000010, + 01000010, + 01000010, + 01000010, + 01000010, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // i + 00000000, + 00000000, + 00000000, /// + 00011000, + 00011000, + 00000000, + 00011000, + 00001000, + 00001000, + 00001000, + 00001000, + 00111110, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // j + 00000000, + 00000000, + 00000000, /// + 00011000, + 00011000, + 00000000, + 00011000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, // + 00001000, + 00001000, + 01110000, + 00000000, +}, +{ + // k + 00000000, + 00000000, + 00000000, /// + 01000000, + 01000000, + 01000000, + 01000010, + 01100100, + 01011000, + 01001000, + 01000100, + 01000010, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // l + 00000000, + 00000000, + 00110000, /// + 00010000, + 00010000, + 00010000, + 00010000, + 00010000, + 00010000, + 00010000, + 00010000, + 00001100, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // m + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 01010110, + 01101001, + 01001001, + 01001001, + 01001001, + 01001001, + 01001001, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // n + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 01011100, + 01100010, + 01000010, + 01000010, + 01000010, + 01000010, + 01000010, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // o + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 00111100, + 01000010, + 01000010, + 01000010, + 01000010, + 01000010, + 00111100, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // p + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 01011100, + 01100010, + 01000010, + 01000010, + 01000010, + 01100010, + 01011100, // + 01000000, + 01000000, + 01000000, + 00000000, +}, +{ + // q + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 00111010, + 01000110, + 01000010, + 01000010, + 01000010, + 01000110, + 00111010, // + 00000010, + 00000010, + 00000010, + 00000000, +}, +{ + // r + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 01011100, + 01100010, + 01000000, + 01000000, + 01000000, + 01000000, + 01000000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // s + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 00111100, + 01000010, + 01000000, + 00111100, + 00000010, + 01000010, + 00111100, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // t + 00000000, + 00000000, + 00000000, /// + 00010000, + 00010000, + 01111110, + 00010000, + 00010000, + 00010000, + 00010000, + 00010000, + 00001110, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // u + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 01000010, + 01000010, + 01000010, + 01000010, + 01000010, + 01000010, + 00111100, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // v + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 01000010, + 01000010, + 01000010, + 00100100, + 00100100, + 00100100, + 00011000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // w + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 10001001, + 10001001, + 10001001, + 01011001, + 01010110, + 01100110, + 01000010, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // x + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 01000010, + 00100100, + 00011000, + 00011000, + 00100100, + 01000010, + 01000010, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // y + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 01000010, + 01000010, + 01000010, + 01000010, + 01000010, + 01000110, + 00111010, // + 00000010, + 00000010, + 00111100, + 00000000, +}, +{ + // z + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 01111110, + 00000100, + 00001000, + 00010000, + 00100000, + 01000000, + 01111110, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{ + // { + 00000000, + 00000110, + 00001000, /// + 00001000, + 00001000, + 00001000, + 00010000, + 00010000, + 00001000, + 00001000, + 00001000, + 00001000, // + 00000110, + 00000000, + 00000000, + 00000000, +}, +{ + // | + 00000000, + 00001000, + 00001000, /// + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, + 00001000, // + 00001000, + 00000000, + 00000000, + 00000000, +}, +{ + // } + 00000000, + 01100000, + 00010000, /// + 00010000, + 00010000, + 00010000, + 00001000, + 00001000, + 00010000, + 00010000, + 00010000, + 00010000, // + 01100000, + 00000000, + 00000000, + 00000000, +}, +{ + // ~ + 00000000, + 00000000, + 00000000, /// + 00000000, + 00000000, + 00000000, + 00110010, + 01001100, + 00000000, + 00000000, + 00000000, + 00000000, // + 00000000, + 00000000, + 00000000, + 00000000, +}, +{}, \ No newline at end of file -- 2.43.0 From cfc961e5c300f3f85a480150a23ebe9856eff94e Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Wed, 17 Apr 2024 01:01:20 +0800 Subject: [PATCH 05/18] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=9C=A8vscode?= =?UTF-8?q?=E4=B8=AD=E5=87=AD=E7=A9=BA=E5=87=BA=E7=8E=B0=E7=9A=84=E6=8A=A5?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .cargo/config | 2 ++ .gitignore | 1 + .vscode/c_cpp_properties.json | 16 ---------------- .vscode/settings.json | 9 --------- Cargo.toml | 4 +--- 5 files changed, 4 insertions(+), 28 deletions(-) create mode 100644 .cargo/config delete mode 100644 .vscode/c_cpp_properties.json delete mode 100644 .vscode/settings.json diff --git a/.cargo/config b/.cargo/config new file mode 100644 index 0000000..5f4ec04 --- /dev/null +++ b/.cargo/config @@ -0,0 +1,2 @@ +[build] +target = ["x86_64-unknown-none"] diff --git a/.gitignore b/.gitignore index 903a407..c61cc2d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.vscode /test/metaverse.img *.o *.tfo diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index fe4e235..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "configurations": [ - { - "name": "Linux", - "includePath": [ - "${workspaceFolder}/include" - ], - "defines": [], - "compilerPath": "/usr/bin/gcc", - "cStandard": "c17", - "intelliSenseMode": "linux-gcc-x64", - "configurationProvider": "ms-vscode.makefile-tools" - } - ], - "version": 4 -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 6970732..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "C_Cpp.errorSquiggles": "disabled", - "files.associations": { - "*.s": "nasm", - "LICENCE": "plaintext", - "*.h": "c" - }, - "rust-analyzer.imports.preferNoStd" : true, -} \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 9a4c32c..a60f5ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,9 +5,7 @@ edition = "2021" [lib] crate-type = ["staticlib"] +test = false # 此Cargo.toml仅用于rust-analyzer识别rust部分的代码 # 不应使用cargo编译 - -[target.'cfg(target_arch = "x86_64")'] -target = "x86_64-unknown-none" -- 2.43.0 From fce4c49d3a55bca492e99138d14e2a2168e46f95 Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Thu, 18 Apr 2024 16:07:13 +0800 Subject: [PATCH 06/18] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9vscode=20rust-?= =?UTF-8?q?analyzer=E8=AF=AF=E6=8A=A5=E9=94=99=E8=AF=AF=E7=9A=84=E8=AF=B4?= =?UTF-8?q?=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/contribution.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/contribution.md b/docs/contribution.md index e66325d..b9d3015 100644 --- a/docs/contribution.md +++ b/docs/contribution.md @@ -12,6 +12,22 @@ 若你的代码通过审核,将会把你的PR合并到主分支中。 +## 开发引导 + +## vscode + +vscode下的`rust-analyzer`插件会在`src/lib.rs`中误报`can't find crate for test`,实际上在`Cargo.toml`中已经增加了`test = false`选项。 + +解决方法是在`.vscode/settings.json`中增加两项: + +```json +"rust-analyzer.cargo.allTargets": false, +"rust-analyzer.cargo.extraArgs": [ + "--target", + "开发的目标平台的unknown-none类型目标(如x86_64-unknown-none)" +] +``` + ## 需要注意的还未解决的问题 * rust中所有有关字符串格式化的宏中,出现超过一个不止是`{}`的格式化占位符时内核必然崩溃。 -- 2.43.0 From 3762b0245c4d90a19bf161838a5b1f0fd186c89f Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Sun, 21 Apr 2024 17:53:59 +0800 Subject: [PATCH 07/18] =?UTF-8?q?=E9=87=8D=E5=86=99=E9=83=A8=E5=88=86core?= =?UTF-8?q?=20crate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/kernel/clock/time.rs | 1 + src/kernel/klog.rs | 112 ++----------- src/kernel/sync/mod.rs | 1 - src/kernel/sync/rwlock.rs | 129 -------------- src/kernel/tty/tty.rs | 99 ++++++++++- src/lib.rs | 61 +------ src/libk/alloc/boxed.rs | 38 +++++ src/libk/alloc/mod.rs | 3 + src/libk/alloc/string.rs | 81 +++++++++ src/libk/alloc/vec.rs | 280 +++++++++++++++++++++++++++++++ src/libk/{string => core}/mod.rs | 0 src/libk/mod.rs | 3 +- 12 files changed, 511 insertions(+), 297 deletions(-) delete mode 100644 src/kernel/sync/mod.rs delete mode 100644 src/kernel/sync/rwlock.rs create mode 100644 src/libk/alloc/boxed.rs create mode 100644 src/libk/alloc/mod.rs create mode 100644 src/libk/alloc/string.rs create mode 100644 src/libk/alloc/vec.rs rename src/libk/{string => core}/mod.rs (100%) diff --git a/src/kernel/clock/time.rs b/src/kernel/clock/time.rs index d286202..993cedc 100644 --- a/src/kernel/clock/time.rs +++ b/src/kernel/clock/time.rs @@ -11,6 +11,7 @@ extern "C" { pub struct SystemTimeError(Duration); #[derive(Clone, Copy, Hash)] +#[derive(Default)] pub struct SystemTime { unix_time: usize, ns_time: usize, diff --git a/src/kernel/klog.rs b/src/kernel/klog.rs index 8c55f00..22cb64d 100644 --- a/src/kernel/klog.rs +++ b/src/kernel/klog.rs @@ -1,5 +1,4 @@ -use alloc::vec; -use alloc::vec::Vec; +use crate::libk::alloc::vec::Vec; use super::{ clock::time::SystemTime, @@ -28,12 +27,12 @@ pub struct KernelLogger { impl KernelLogger { pub fn new() -> Self { Self { - fatal_queue: vec![], - error_queue: vec![], - warning_queue: vec![], - info_queue: vec![], - debug_queue: vec![], - trace_queue: vec![], + fatal_queue: Vec::new(), + error_queue: Vec::new(), + warning_queue: Vec::new(), + info_queue: Vec::new(), + debug_queue: Vec::new(), + trace_queue: Vec::new(), } } @@ -91,105 +90,18 @@ impl KernelLogger { self.trace_queue.push((SystemTime::now(), msg)); } - pub fn iter(&self, level: LoggerLevel) -> LogIterator { - let mut logs = vec![]; - match level { - LoggerLevel::Fatal => { - logs.push(&self.fatal_queue); - } - LoggerLevel::Error => { - logs.push(&self.fatal_queue); - logs.push(&self.error_queue); - } - LoggerLevel::Warning => { - logs.push(&self.fatal_queue); - logs.push(&self.error_queue); - logs.push(&self.warning_queue); - } - LoggerLevel::Info => { - logs.push(&self.fatal_queue); - logs.push(&self.error_queue); - logs.push(&self.warning_queue); - logs.push(&self.info_queue); - } - LoggerLevel::Debug => { - logs.push(&self.fatal_queue); - logs.push(&self.error_queue); - logs.push(&self.warning_queue); - logs.push(&self.info_queue); - logs.push(&self.debug_queue); - } - LoggerLevel::Trace => { - logs.push(&self.fatal_queue); - logs.push(&self.error_queue); - logs.push(&self.warning_queue); - logs.push(&self.info_queue); - logs.push(&self.debug_queue); - logs.push(&self.trace_queue); - } - } - let mut res = vec![]; - let mut indeces = Vec::new(); - for _ in 0..logs.len() { - indeces.push(0usize); - } - let all_end = |indeces: &Vec, logs: &Vec<&Vec<(SystemTime, Message)>>| { - for i in 0..indeces.len() { - if indeces[i] < logs.len() { - return false; - } - } - true - }; - while !all_end(&indeces, &logs) { - let mut min_ind = None; - let mut min = None; - for i in 0..indeces.len() { - if indeces[i] >= logs[i].len() { - continue; - } - if let Some(minx) = min.as_mut() { - if logs[i][indeces[i]].0 < *minx { - *minx = logs[i][indeces[i]].0; - min_ind = Some(i); - } - } else { - min = Some(logs[i][indeces[i]].0); - min_ind = Some(i); - } - } - if let Some(mini) = min_ind { - res.push(&logs[mini][indeces[mini]]); - indeces[mini] += 1; - } else { - break; - } - } - LogIterator { logs: res } + pub fn iter(&self, _level: LoggerLevel) -> LogIterator { + todo!() } } -pub struct LogIterator<'a> { - logs: Vec<&'a (SystemTime, Message)>, +pub struct LogIterator { } -impl<'a> Iterator for LogIterator<'a> { +impl Iterator for LogIterator { type Item = Message; fn next(&mut self) -> Option { - let res = if let Some((time, msg)) = self.logs.first() { - Some( - MessageBuilder::new() - .message(time) - .append(MessageBuilder::from_message(msg.clone())) - .build(), - ) - } else { - None - }; - if let Some(_) = res { - self.logs.remove(0); - } - res + todo!() } } diff --git a/src/kernel/sync/mod.rs b/src/kernel/sync/mod.rs deleted file mode 100644 index 2ba968f..0000000 --- a/src/kernel/sync/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod rwlock; diff --git a/src/kernel/sync/rwlock.rs b/src/kernel/sync/rwlock.rs deleted file mode 100644 index 76d2f36..0000000 --- a/src/kernel/sync/rwlock.rs +++ /dev/null @@ -1,129 +0,0 @@ -use core::{ops::{Deref, DerefMut}, ptr::null_mut}; - -use alloc::{vec, vec::Vec}; - -/// ## RwLock -/// 读写锁 -/// -/// * 对于写入操作: -/// -/// 锁没有占用时,获取锁,并执行闭包。 -/// -/// 当锁已经被占用,将闭包悬挂。 -/// -/// 释放锁时,将所有悬挂的闭包都执行。 -/// -/// 正在释放锁时,`write`方法返回`Err(())` -/// -/// ``` -/// let num = 6; -/// let num = RwLock::new(num); -/// if let Err(()) = num.write( -/// |n| *n = 10 -/// ) {} -/// let numstr = format!("{}", num.read()); -/// ```` -pub struct RwLock<'a, T> { - obj: T, - locked: bool, - dealing_hanged: bool, - ptr: *mut RwLockWriteGuard<'a, T>, - hanging: Vec<&'a dyn Fn(&mut T)>, -} - -unsafe impl Send for RwLock<'_, T> {} -unsafe impl Sync for RwLock<'_, T> {} - -impl<'a, T> RwLock<'a, T> { - pub fn new(obj: T) -> Self { - Self { - obj, - locked: false, - dealing_hanged: false, - ptr: null_mut(), - hanging: vec![], - } - } - - pub fn read(&self) -> RwLockReadGuard { - RwLockReadGuard { obj: &self.obj } - } - - pub fn write(&'a mut self, f: &'a dyn Fn(&mut T)) -> Result, ()> { - if self.dealing_hanged { - Err(()) - } else if !self.locked { - self.locked = true; - f(&mut self.obj); - let ptr = { self as *mut RwLock<'a, T> }; - let obj = &mut self.obj; - let mut res = RwLockWriteGuard { - obj, - ptr, - must_deal_hang: false, - }; - self.ptr = &mut res as *mut RwLockWriteGuard<'_, T>; - Ok(res) - } else { - self.hanging.push(f); - let ptr = { self as *mut RwLock<'a, T> }; - let obj = &mut self.obj; - Ok(RwLockWriteGuard { - obj, - ptr, - must_deal_hang: false, - }) - } - } -} - -pub struct RwLockReadGuard<'a, T> { - obj: &'a T, -} - -impl<'a, T> Deref for RwLockReadGuard<'a, T> { - type Target = T; - - fn deref(&self) -> &Self::Target { - self.obj - } -} - -impl<'a, T> Drop for RwLockReadGuard<'a, T> { - fn drop(&mut self) {} -} - -pub struct RwLockWriteGuard<'a, T> { - obj: &'a mut T, - ptr: *mut RwLock<'a, T>, - must_deal_hang: bool, -} - -impl<'a, T> Deref for RwLockWriteGuard<'a, T> { - type Target = T; - - fn deref(&self) -> &Self::Target { - self.obj - } -} - -impl<'a, T> DerefMut for RwLockWriteGuard<'a, T> { - fn deref_mut(&mut self) -> &mut Self::Target { - self.obj - } -} - -impl<'a, T> Drop for RwLockWriteGuard<'a, T> { - fn drop(&mut self) { - if self.must_deal_hang { - let p = unsafe { &mut *self.ptr }; - p.dealing_hanged = true; - for f in p.hanging.iter() { - f(self.obj); - } - p.hanging.clear(); - p.dealing_hanged = false; - p.locked = false; - } - } -} diff --git a/src/kernel/tty/tty.rs b/src/kernel/tty/tty.rs index 3fe729e..248f6bf 100644 --- a/src/kernel/tty/tty.rs +++ b/src/kernel/tty/tty.rs @@ -1,8 +1,8 @@ use core::ptr::null_mut; -use alloc::{ +use crate::libk::alloc::{ + boxed::Box, string::{String, ToString}, - vec, vec::Vec, }; @@ -141,7 +141,7 @@ impl Tty { } } -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Default)] pub struct Color(pub u8, pub u8, pub u8); impl Color { @@ -169,7 +169,7 @@ pub struct Resolution { pub height: usize, } -#[derive(Clone)] +#[derive(Clone, Default)] pub struct MessageSection { msg: String, fgcolor: Color, @@ -179,14 +179,14 @@ pub struct MessageSection { /// ## Message /// /// 用`MessageBuilder`构造一个`Message`对象 -#[derive(Clone)] +#[derive(Clone, Default)] pub struct Message(Vec); impl ToString for Message { fn to_string(&self) -> String { let mut res = String::new(); for MessageSection { msg, .. } in self.0.iter() { - res += msg; + res += msg.clone(); } res } @@ -212,7 +212,7 @@ impl ToString for Message { /// ```rust /// use crate::kernel::tty::tty::BuilderFunctions::*; /// -/// message!( +/// message_raw!( /// Msg("Hello, "), /// Msg("Metaverse"), /// FgColor(Color::GREEN), @@ -242,10 +242,14 @@ pub enum BuilderFunctions<'a> { impl MessageBuilder { pub fn new() -> Self { Self { - msg: Message(vec![]), + msg: Message(Vec::new()), } } + pub fn is_empty(&self) -> bool { + self.msg.0.is_empty() + } + pub fn from_message(msg: Message) -> Self { Self { msg } } @@ -302,3 +306,82 @@ impl MessageBuilder { self.msg } } + +pub enum FmtMeta { + Color(Color), + String(String), + ToStringable(Box), + Hex(u8), + Pointer(usize), +} + +pub fn format_message(fmt: &mut Vec, meta: FmtMeta) -> MessageBuilder { + let mut msgbuilder = MessageBuilder::new(); + let mut fmt_start = None; + let mut fmt_end = None; + for i in 0..fmt.len() { + if fmt[i] == '{' { + fmt_start = Some(i); + } + if fmt[i] == '}' { + fmt_end = Some(i); + break; + } + } + let tohex = |n: u8| { + if n < 10 { + (b'0' + n) as char + } else { + (b'a' + n - 10) as char + } + }; + if fmt_start == None || fmt_end == None { + msgbuilder.message_mut(&String::from_iter(fmt.iter())); + } else { + let fmt_start = fmt_start.unwrap(); + let fmt_end = fmt_end.unwrap(); + let mut formatter = Vec::new(); + for _ in fmt_start..=fmt_end { + formatter.push(fmt.remove(fmt_start)); + } + formatter.remove(formatter.len() - 1); + formatter.remove(0); + match meta { + FmtMeta::Color(color) => { + let first = fmt.split_at(fmt_start).0; + msgbuilder.message_mut(&String::from_iter(first.iter())); + for _ in 0..fmt_start { + fmt.remove(0); + } + msgbuilder.message_mut(&String::from_iter(formatter.iter())); + msgbuilder.foreground_color_mut(color); + } + FmtMeta::String(s) => { + for c in s.chars().rev() { + fmt.insert(fmt_start, *c); + } + } + FmtMeta::ToStringable(s) => { + for c in s.to_string().chars().rev() { + fmt.insert(fmt_start, *c); + } + } + FmtMeta::Hex(num) => { + fmt.insert(fmt_start, tohex(num >> 4)); + fmt.insert(fmt_start, tohex(num & 0xf)); + } + FmtMeta::Pointer(mut p) => { + for _ in 0..16 { + fmt.insert(fmt_start, tohex((p & 0xf) as u8)); + p >>= 4; + } + } + } + } + let mut rests = Vec::new(); + while !fmt.is_empty() && fmt[0] != '{' { + rests.push(fmt.remove(0)); + } + msgbuilder.message_mut(&String::from_iter(rests.iter())); + msgbuilder +} diff --git a/src/lib.rs b/src/lib.rs index e8ac072..6872093 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,69 +1,14 @@ #![no_std] +#![feature(strict_provenance)] extern crate alloc; -use alloc::{ - string::{String, ToString}, - vec::Vec, -}; -use core::{panic::PanicInfo, ptr::null_mut}; - -use kernel::tty::tty::{self, tty_enable, tty_text_print, Color}; +use core::panic::PanicInfo; pub mod kernel; pub mod libk; #[panic_handler] -unsafe fn kernel_panic_handler(info: &PanicInfo) -> ! { - let line_in_file = if let Some(loca) = info.location() { - loca.line().to_string() - } else { - String::new() - }; - let info = { - let mut v = Vec::new(); - v.push(("Kernel Panic: ", (Color::RED, Color::BLACK))); - v.push(( - if let Some(loca) = info.location() { - loca.file() - } else { - "NoFile" - }, - (Color::GREEN, Color::BLACK), - )); - v.push((":", (Color::WHITE, Color::BLACK))); - v.push(( - if let Some(_) = info.location() { - line_in_file.as_str() - } else { - "NoLine" - }, - (Color::WHITE, Color::BLACK), - )); - v.push((": ", (Color::WHITE, Color::BLACK))); - v.push(( - if let Some(&s) = info.payload().downcast_ref::<&str>() { - s - } else { - "Unknown panic." - }, - (Color::BLUE, Color::BLACK), - )); - v.push(("\n", (Color::BLACK, Color::BLACK))); - v - }; - let tty = tty::tty_get(0); - if tty != null_mut() { - let tty = *tty; - tty_enable(tty); - for (msgo, (fgc, bgc)) in info.into_iter() { - let msg = String::from(msgo).as_bytes_mut() as *mut [u8] as *mut u8; - let p = msg.offset(msgo.len() as isize); - let swp = *p; - *p = 0; - tty_text_print(tty, msg, u32::from(fgc), u32::from(bgc)); - *p = swp; - } - } +unsafe fn kernel_panic_handler(_info: &PanicInfo) -> ! { loop {} } diff --git a/src/libk/alloc/boxed.rs b/src/libk/alloc/boxed.rs new file mode 100644 index 0000000..c8d448b --- /dev/null +++ b/src/libk/alloc/boxed.rs @@ -0,0 +1,38 @@ +use core::{ + alloc::Layout, + mem::transmute, + ops::{Deref, DerefMut}, + ptr::addr_of, +}; + +use alloc::alloc::alloc; + +pub struct Box { + inner: *mut T, +} + +impl Box { + pub fn new(t: T) -> Self { + unsafe { + let inner = alloc(Layout::for_value(&t)); + inner.copy_from(addr_of!(t).cast(), 1); + Self { + inner: transmute(inner), + } + } + } +} + +impl Deref for Box { + type Target = T; + + fn deref(&self) -> &Self::Target { + unsafe { self.inner.as_ref().unwrap() } + } +} + +impl DerefMut for Box { + fn deref_mut(&mut self) -> &mut Self::Target { + unsafe { self.inner.as_mut().unwrap() } + } +} diff --git a/src/libk/alloc/mod.rs b/src/libk/alloc/mod.rs new file mode 100644 index 0000000..2efd480 --- /dev/null +++ b/src/libk/alloc/mod.rs @@ -0,0 +1,3 @@ +pub mod boxed; +pub mod string; +pub mod vec; diff --git a/src/libk/alloc/string.rs b/src/libk/alloc/string.rs new file mode 100644 index 0000000..d365f27 --- /dev/null +++ b/src/libk/alloc/string.rs @@ -0,0 +1,81 @@ +use core::ops::{Add, AddAssign}; + +use super::vec::{Vec, VecIterator}; + +#[derive(Clone, Default)] +pub struct String { + data: Vec, + u8data: Vec, +} + +impl String { + pub fn new() -> Self { + Self { + data: Vec::new(), + u8data: Vec::new(), + } + } + + pub fn len(&self) -> usize { + self.data.len() + } + + pub fn chars(&self) -> VecIterator { + self.data.iter() + } + + pub fn as_bytes(&self) -> &[u8] { + &self.u8data[..] + } + + pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] { + &mut self.u8data[..] + } +} + +impl FromIterator for String { + fn from_iter>(iter: T) -> Self { + let data = Vec::from_iter(iter); + let u8data = Vec::from_iter(data.iter().map(|c| *c as u8)); + Self { data, u8data } + } +} + +impl<'a> FromIterator<&'a char> for String { + fn from_iter>(iter: T) -> Self { + let data = Vec::from_iter(iter.into_iter().map(|c| *c)); + let u8data = Vec::from_iter(data.iter().map(|c| *c as u8)); + Self { data, u8data } + } +} + +impl ToString for String { + fn to_string(&self) -> String { + self.clone() + } +} + +impl Add for String { + type Output = Self; + + fn add(mut self, rhs: Self) -> Self::Output { + self.data.append(&mut rhs.chars().map(|c| *c).collect()); + self + } +} + +impl AddAssign for String { + fn add_assign(&mut self, rhs: String) { + *self = self.clone() + rhs; + } +} + +pub trait ToString { + fn to_string(&self) -> String; +} + +impl ToString for str { + fn to_string(&self) -> String { + String::from_iter(self.chars()) + } +} diff --git a/src/libk/alloc/vec.rs b/src/libk/alloc/vec.rs new file mode 100644 index 0000000..1a65451 --- /dev/null +++ b/src/libk/alloc/vec.rs @@ -0,0 +1,280 @@ +use core::{ + alloc::Layout, + ops::{Index, IndexMut, Range, RangeFull}, + ptr::{addr_of_mut, null_mut}, + slice, +}; + +use alloc::alloc::{alloc, dealloc}; + +pub struct Vec { + pointer: *mut T, + length: usize, + capacity: usize, +} + +impl Vec { + pub fn new() -> Self { + Self { + pointer: null_mut(), + length: 0, + capacity: 0, + } + } + + unsafe fn extend_capacity(&mut self) { + let newp = alloc(Layout::array::(self.capacity * 2).unwrap()).cast(); + self.pointer.copy_to(newp, self.length); + dealloc( + self.pointer.cast(), + Layout::array::(self.capacity).unwrap(), + ); + self.pointer = newp; + self.capacity *= 2; + } + + pub fn push(&mut self, item: T) { + if self.pointer.is_null() { + self.pointer = unsafe { alloc(Layout::array::(4).unwrap()).cast() }; + self.capacity = 4; + self.length = 0; + } + if self.capacity == self.length { + unsafe { self.extend_capacity() } + } + unsafe { self.pointer.offset(self.length as isize).write(item) }; + self.length += 1; + } + + pub fn insert(&mut self, index: usize, item: T) { + if self.capacity == self.length { + unsafe { self.extend_capacity() } + } + let rearlen = self.length - index; + unsafe { + if rearlen != 0 { + let tmp = alloc(Layout::array::(rearlen).unwrap()).cast(); + self.pointer.offset(index as isize).copy_to(tmp, rearlen); + self.pointer.offset(index as isize).write(item); + self.pointer + .offset(index as isize + 1) + .copy_from(tmp, rearlen); + } else { + self.pointer.offset(self.length as isize).write(item); + } + } + self.length += 1; + } + + pub fn append(&mut self, v: &mut Self) { + while self.capacity < self.length + v.length { + unsafe { self.extend_capacity() } + } + unsafe { + self.pointer + .offset(self.length as isize) + .copy_from(v.pointer.cast_const(), v.length) + }; + self.length += v.length; + } + + pub fn split_at(&self, index: usize) -> (&[T], &[T]) { + if index >= self.length { + panic!("Index out of bound."); + } + (&self[0..index], &self[index..self.length]) + } + + pub fn len(&self) -> usize { + self.length + } + + pub fn is_empty(&self) -> bool { + self.length == 0 + } + + pub fn last_mut(&mut self) -> Option<&mut T> { + unsafe { self.pointer.offset(self.length as isize - 1).as_mut() } + } + + pub fn last(&self) -> Option<&T> { + unsafe { self.pointer.offset(self.length as isize - 1).as_ref() } + } + + pub fn remove(&mut self, index: usize) -> T { + if index >= self.length { + panic!("Index out of bound."); + } + let mut t: T = T::default(); + let pt = addr_of_mut!(t); + unsafe { + pt.copy_from(self.pointer.offset(index as isize).cast_const(), 1); + self.pointer.offset(index as isize).copy_from( + self.pointer.offset(index as isize + 1), + self.length - index - 1, + ); + } + t + } + + pub fn iter(&self) -> VecIterator { + VecIterator { + pointer: self.pointer, + index: 0, + length: self.length, + _phantom: &(), + } + } +} + +impl Default for Vec { + fn default() -> Self { + Self::new() + } +} + +impl Index for Vec { + type Output = T; + + fn index(&self, index: usize) -> &Self::Output { + unsafe { self.pointer.offset(index as isize).as_ref().unwrap() } + } +} + +impl Index> for Vec { + type Output = [T]; + + fn index(&self, index: Range) -> &Self::Output { + unsafe { &slice::from_raw_parts_mut(self.pointer, self.length)[index] } + } +} + +impl Index for Vec { + type Output = [T]; + + fn index(&self, _: RangeFull) -> &Self::Output { + unsafe { slice::from_raw_parts(self.pointer.cast(), self.length) } + } +} + +impl IndexMut for Vec { + fn index_mut(&mut self, _: RangeFull) -> &mut Self::Output { + unsafe { slice::from_raw_parts_mut(self.pointer.cast(), self.length) } + } +} + +impl Clone for Vec { + fn clone(&self) -> Self { + let res = Self { + pointer: unsafe { alloc(Layout::array::(self.capacity).unwrap()).cast() }, + length: self.length, + capacity: self.capacity, + }; + unsafe { + res.pointer + .copy_from(self.pointer.cast_const(), self.length) + }; + res + } +} + +impl Drop for Vec { + fn drop(&mut self) { + unsafe { + dealloc( + self.pointer.cast(), + Layout::array::(self.capacity).unwrap(), + ) + }; + } +} + +impl FromIterator for Vec { + fn from_iter>(iter: U) -> Self { + let mut res = Vec::new(); + for i in iter { + res.push(i); + } + res + } +} + +impl IntoIterator for Vec { + type Item = T; + + type IntoIter = VecIter; + + fn into_iter(self) -> Self::IntoIter { + todo!() + } +} + +pub struct VecIterator<'a, T> { + pointer: *mut T, + index: usize, + length: usize, + _phantom: &'a (), +} + +impl<'a, T: 'a> Iterator for VecIterator<'a, T> { + type Item = &'a T; + + fn next(&mut self) -> Option { + if self.index == self.length { + None + } else { + let res = unsafe { Some(self.pointer.offset(self.index as isize).as_ref().unwrap()) }; + self.index += 1; + res + } + } +} + +impl<'a, T: 'a> DoubleEndedIterator for VecIterator<'a, T> { + fn next_back(&mut self) -> Option { + if self.index == 0 { + None + } else { + self.index -= 1; + unsafe { Some(self.pointer.offset(self.index as isize).as_ref().unwrap()) } + } + } +} + +pub struct VecIter { + pointer: *mut T, + index: usize, + length: usize, + capacity: usize, +} + +impl Iterator for VecIter { + type Item = T; + + fn next(&mut self) -> Option { + if self.index == self.length { + None + } else { + let mut t = T::default(); + let res = unsafe { + self.pointer + .offset(self.index as isize) + .copy_to(addr_of_mut!(t), 1); + Some(t) + }; + self.index += 1; + res + } + } +} + +impl Drop for VecIter { + fn drop(&mut self) { + unsafe { + dealloc( + self.pointer.cast(), + Layout::array::(self.capacity).unwrap(), + ) + }; + } +} diff --git a/src/libk/string/mod.rs b/src/libk/core/mod.rs similarity index 100% rename from src/libk/string/mod.rs rename to src/libk/core/mod.rs diff --git a/src/libk/mod.rs b/src/libk/mod.rs index d245b85..9570646 100644 --- a/src/libk/mod.rs +++ b/src/libk/mod.rs @@ -1 +1,2 @@ -pub mod string; +pub mod alloc; +pub mod core; -- 2.43.0 From 0efc9d118ecd6421dc68f7d3108e97803387056e Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Sun, 21 Apr 2024 21:59:08 +0800 Subject: [PATCH 08/18] =?UTF-8?q?=E6=9B=B4=E6=8D=A2rust=E6=A0=87=E5=87=86?= =?UTF-8?q?=E5=BA=93=E6=9E=84=E5=BB=BA=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .cargo/{config => config.toml} | 0 .gitmodules | 3 --- rust-toolchain.toml | 2 +- rustlib | 1 - 4 files changed, 1 insertion(+), 5 deletions(-) rename .cargo/{config => config.toml} (100%) delete mode 160000 rustlib diff --git a/.cargo/config b/.cargo/config.toml similarity index 100% rename from .cargo/config rename to .cargo/config.toml diff --git a/.gitmodules b/.gitmodules index f4f86c9..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "rustlib"] - path = rustlib - url = https://github.com/metaverse-kernel/rustenv.git diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 292fe49..5d56faf 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,2 +1,2 @@ [toolchain] -channel = "stable" +channel = "nightly" diff --git a/rustlib b/rustlib deleted file mode 160000 index bb4a5d1..0000000 --- a/rustlib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit bb4a5d1b55c36070d629400bddd6d267d2f0714a -- 2.43.0 From f2240787b6ebdc26a8943de69d5ce6a08c8cc926 Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Thu, 25 Apr 2024 17:41:35 +0800 Subject: [PATCH 09/18] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=9B=B4=E5=A4=9Acore?= =?UTF-8?q?=20crate=E7=9A=84=E9=87=8D=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- debug/2024-04.txt | 29 ++++++ src/Makefile | 27 +++-- src/kernel/main.rs | 21 ++-- src/kernel/memm/memm.rs | 6 +- src/kernel/memm/mod.rs | 2 +- src/kernel/tty/mod.rs | 33 +++++-- src/kernel/tty/tty.rs | 4 +- src/lib.rs | 1 + src/libk/alloc/alloc.rs | 11 +++ src/libk/alloc/boxed.rs | 13 ++- src/libk/alloc/mod.rs | 1 + src/libk/alloc/string.rs | 209 +++++++++++++++++++++++++++++++++++++++ src/libk/alloc/vec.rs | 9 +- 13 files changed, 326 insertions(+), 40 deletions(-) create mode 100644 debug/2024-04.txt create mode 100644 src/libk/alloc/alloc.rs diff --git a/debug/2024-04.txt b/debug/2024-04.txt new file mode 100644 index 0000000..914d7c4 --- /dev/null +++ b/debug/2024-04.txt @@ -0,0 +1,29 @@ +======== 04-21 16:50 ======== +Hit null pointer at 0x100370a +Stack trace: + 1 0x0000000001007b90 -> interrupt_req_DE + 2 0x00000000010072b0 -> metaverse::kernel::tty::tty::MessageBuilder::message_mut + 3 0x0000000001007e60 -> >::from_iter + 4 0x0000000001005eb0 -> >::from_iter + 5 0x0000000001005d80 -> >::extend + 6 0x0000000001004840 -> alloc::vec::Vec::reserve + 7 0x0000000001005900 -> alloc::raw_vec::RawVec::reserve::{do_reserve_and_handle} + 8 0x00000000010055c0 -> alloc::raw_vec::RawVec::grow_amortized + 9 0x00000000010051e0 -> alloc::raw_vec::finish_grow + 10 0x0000000001005a90 -> ::allocate + 11 0x0000000001004900 -> alloc::alloc::Global::alloc_impl + 12 0x00000000010048a0 -> alloc::alloc::alloc + 13 0x00000000010036e0 -> core::ptr::read_volatile::precondition_check +使用了core crate中的String,导致最终调用了一个含有未正确链接的函数。 + +======== 04-23 13:55 ======== +Hit null pointer as 0x100351a. +Stack trace: + 0x00000000010054d0 -> metaverse::kernel::tty::tty::MessageBuilder::message_mut + 0x0000000001006170 -> ::to_string + 0x0000000001006080 -> >::from_iter + 0x0000000001007450 -> ::from_iter + 0x0000000001006a10 -> metaverse::libk::alloc::vec::Vec::push + 0x0000000001003f60 -> alloc::alloc::alloc + 0x00000000010034f0 -> core::ptr::read_volatile::precondition_check +使用了core crate的alloc函数,其中含有未正确链接的函数。 diff --git a/src/Makefile b/src/Makefile index 7532577..286153c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -31,16 +31,24 @@ endif ################################ # rust语言环境变量 +TARGET_TRIPLE = + +ifeq (${ARCH},x86_64) + TARGET_TRIPLE := x86_64-unknown-none +endif + +RUSTLIB_PATH = ../target/sysroot/lib/rustlib/${TARGET_TRIPLE}/lib +RUST_LIBS = $(shell ls -d ${RUSTLIB_PATH}/lib*.rlib) + RSCFLAGS = --emit obj --crate-type staticlib --verbose \ --crate-name=metaverse \ --edition 2021 \ - -L crate="${PWD}/../rustlib/src/" \ -C code-model=large \ -C relocation-model=static \ - -C embed-bitcode=no + -C embed-bitcode=no \ + -C panic=abort ifeq (${ARCH},x86_64) - RSCFLAGS := ${RSCFLAGS} --target x86_64-unknown-none RSCFLAGS := ${RSCFLAGS} -C target-feature=-sse endif @@ -48,18 +56,14 @@ ifdef release RSCFLAGS := ${RSCFLAGS} -O endif -RUSTLIB_PATH = ../rustlib/${ARCH} -RUST_LIBS = "${RUSTLIB_PATH}/liballoc.rlib" "${RUSTLIB_PATH}/libcompiler_builtins.rlib" \ - "${RUSTLIB_PATH}/libcore.rlib" "${RUSTLIB_PATH}/librustc_std_workspace_core.rlib" - ################################ metaverse.elf: kernel libk rust metaverse.lds font_file.o @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" + 2>&1 | "${SOURCE}/colorize" "warning:=lpink" "error:=red" "ld=lyellow" -.PHONY: kernel libk all clear postproc rust +.PHONY: kernel libk all clear postproc rust rustlib all: postproc metaverse.elf @echo -e "Build \e[1;32msucceeded\e[0m." @@ -92,7 +96,10 @@ libk: echo -e "\e[33m-------------------------\e[0m"; \ fi -rust: postproc +../target: + @-cargo xbuild --target ${TARGET_TRIPLE} --release + +rust: postproc ../target @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 diff --git a/src/kernel/main.rs b/src/kernel/main.rs index 4bb1227..2be7822 100644 --- a/src/kernel/main.rs +++ b/src/kernel/main.rs @@ -1,22 +1,13 @@ -use crate::message; - -use super::{ - klog::{KernelLogger, LoggerLevel}, - tty::tty::{BuilderFunctions::*, Color, Tty}, +use crate::{ + kernel::{ + klog::{KernelLogger, LoggerLevel}, + tty::tty::Tty, + }, + message_raw, }; #[no_mangle] extern "C" fn kmain_rust() -> ! { let tty = Tty::from_id(0).unwrap(); - let mut logger = KernelLogger::new(); - logger.info(message!( - Msg("Hello, "), - Msg("Metaverse"), - FgColor(Color::GREEN), - Msg("!\n") - )); - for msg in logger.iter(LoggerLevel::Info) { - tty.print(msg); - } loop {} } diff --git a/src/kernel/memm/memm.rs b/src/kernel/memm/memm.rs index fea17bf..8c5e045 100644 --- a/src/kernel/memm/memm.rs +++ b/src/kernel/memm/memm.rs @@ -6,9 +6,9 @@ use core::{ }; extern "C" { - pub fn memm_kernel_allocate(size: usize) -> *mut u8; + fn memm_kernel_allocate(size: usize) -> *mut u8; // pub fn memm_user_allocate(size: usize, pid: usize); - pub fn memm_free(mem: *mut u8); + fn memm_free(mem: *mut u8); } pub struct KernelAllocator {} @@ -31,4 +31,4 @@ unsafe impl GlobalAlloc for KernelAllocator { } #[global_allocator] -static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator {}; +pub static KERNEL_ALLOCATOR: KernelAllocator = KernelAllocator {}; diff --git a/src/kernel/memm/mod.rs b/src/kernel/memm/mod.rs index e2c5491..1954ffa 100644 --- a/src/kernel/memm/mod.rs +++ b/src/kernel/memm/mod.rs @@ -1 +1 @@ -mod memm; +pub mod memm; diff --git a/src/kernel/tty/mod.rs b/src/kernel/tty/mod.rs index ed058fa..cebad05 100644 --- a/src/kernel/tty/mod.rs +++ b/src/kernel/tty/mod.rs @@ -25,22 +25,43 @@ macro_rules! message_bgc { } #[macro_export] -macro_rules! message { +macro_rules! message_raw { ( $( $e : expr ),* ) => {{ use crate::{ - kernel::tty::tty::{MessageBuilder, BuilderFunctions}, + kernel::tty::tty::{MessageBuilder, BuilderFunctions::*, Color}, message_msg, message_fgc, message_bgc }; let mut tmp_builder = MessageBuilder::new(); $( - if let BuilderFunctions::Msg(e) = $e { - message_msg!(tmp_builder, e); - } else if let BuilderFunctions::FgColor(c) = $e { + if let Msg(e) = $e { + message_msg!(tmp_builder, &e); + } else if let FgColor(c) = $e { message_fgc!(tmp_builder, c); - } else if let BuilderFunctions::BgColor(c) = $e { + } else if let BgColor(c) = $e { message_bgc!(tmp_builder, c); } )* tmp_builder.build() }}; } + +#[macro_export] +macro_rules! message { + ( $fmtter : expr, $( $e : expr ),* ) => {{ + use crate::{ + kernel::tty::tty::{ + MessageBuilder, + FmtMeta, + Color, + format_message + }, + libk::alloc::vec::Vec, + }; + let mut formatter = $fmtter.chars().collect::>(); + let builder = MessageBuilder::new(); + $( + let builder = builder.append(format_message(&mut formatter, $e)); + )* + builder.build() + }}; +} diff --git a/src/kernel/tty/tty.rs b/src/kernel/tty/tty.rs index 248f6bf..875b222 100644 --- a/src/kernel/tty/tty.rs +++ b/src/kernel/tty/tty.rs @@ -233,8 +233,8 @@ pub struct MessageBuilder { msg: Message, } -pub enum BuilderFunctions<'a> { - Msg(&'a str), +pub enum BuilderFunctions { + Msg(T), FgColor(Color), BgColor(Color), } diff --git a/src/lib.rs b/src/lib.rs index 6872093..642c3f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #![no_std] #![feature(strict_provenance)] +#![feature(layout_for_ptr)] extern crate alloc; diff --git a/src/libk/alloc/alloc.rs b/src/libk/alloc/alloc.rs new file mode 100644 index 0000000..ca2ea72 --- /dev/null +++ b/src/libk/alloc/alloc.rs @@ -0,0 +1,11 @@ +use core::alloc::{GlobalAlloc, Layout}; + +use crate::kernel::memm::memm::KERNEL_ALLOCATOR; + +pub unsafe fn alloc(layout: Layout) -> *mut u8 { + KERNEL_ALLOCATOR.alloc(layout) +} + +pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) { + KERNEL_ALLOCATOR.dealloc(ptr, layout); +} diff --git a/src/libk/alloc/boxed.rs b/src/libk/alloc/boxed.rs index c8d448b..baff88d 100644 --- a/src/libk/alloc/boxed.rs +++ b/src/libk/alloc/boxed.rs @@ -5,7 +5,7 @@ use core::{ ptr::addr_of, }; -use alloc::alloc::alloc; +use crate::libk::alloc::alloc::{alloc, dealloc}; pub struct Box { inner: *mut T, @@ -36,3 +36,14 @@ impl DerefMut for Box { unsafe { self.inner.as_mut().unwrap() } } } + +impl Drop for Box { + fn drop(&mut self) { + unsafe { + dealloc( + self.inner.cast(), + Layout::for_value_raw(self.inner.cast_const()), + ) + } + } +} diff --git a/src/libk/alloc/mod.rs b/src/libk/alloc/mod.rs index 2efd480..3acd932 100644 --- a/src/libk/alloc/mod.rs +++ b/src/libk/alloc/mod.rs @@ -1,3 +1,4 @@ +pub mod alloc; pub mod boxed; pub mod string; pub mod vec; diff --git a/src/libk/alloc/string.rs b/src/libk/alloc/string.rs index d365f27..c106e0c 100644 --- a/src/libk/alloc/string.rs +++ b/src/libk/alloc/string.rs @@ -31,6 +31,11 @@ impl String { pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] { &mut self.u8data[..] } + + pub fn insert(&mut self, index: usize, item: char) { + self.data.insert(index, item); + self.u8data.insert(index, item as u8); + } } impl FromIterator for String { @@ -72,6 +77,12 @@ impl AddAssign for String { pub trait ToString { fn to_string(&self) -> String; + fn to_octal_string(&self) -> String { + String::new() + } + fn to_hex_string(&self) -> String { + String::new() + } } impl ToString for str { @@ -79,3 +90,201 @@ impl ToString for str { String::from_iter(self.chars()) } } + +impl ToString for &'static str { + fn to_string(&self) -> String { + (*self).to_string() + } +} + +impl ToString for u8 { + fn to_string(&self) -> String { + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, ((num % 10) as u8 + b'0') as char); + num /= 10; + } + res + } + + fn to_octal_string(&self) -> String { + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, ((num % 8) as u8 + b'0') as char); + num /= 8; + } + res + } + + fn to_hex_string(&self) -> String { + let tohex = |x| { + if x < 10 { + x as u8 + b'0' + } else { + x as u8 - 10 + b'a' + } + }; + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, (tohex(num % 16) + b'0') as char); + num /= 16; + } + res + } +} + +impl ToString for u16 { + fn to_string(&self) -> String { + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, ((num % 10) as u8 + b'0') as char); + num /= 10; + } + res + } + + fn to_octal_string(&self) -> String { + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, ((num % 8) as u8 + b'0') as char); + num /= 8; + } + res + } + + fn to_hex_string(&self) -> String { + let tohex = |x| { + if x < 10 { + x as u8 + b'0' + } else { + x as u8 - 10 + b'a' + } + }; + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, (tohex(num % 16) + b'0') as char); + num /= 16; + } + res + } +} +impl ToString for u32 { + fn to_string(&self) -> String { + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, ((num % 10) as u8 + b'0') as char); + num /= 10; + } + res + } + + fn to_octal_string(&self) -> String { + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, ((num % 8) as u8 + b'0') as char); + num /= 8; + } + res + } + + fn to_hex_string(&self) -> String { + let tohex = |x| { + if x < 10 { + x as u8 + b'0' + } else { + x as u8 - 10 + b'a' + } + }; + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, (tohex(num % 16) + b'0') as char); + num /= 16; + } + res + } +} +impl ToString for u64 { + fn to_string(&self) -> String { + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, ((num % 10) as u8 + b'0') as char); + num /= 10; + } + res + } + + fn to_octal_string(&self) -> String { + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, ((num % 8) as u8 + b'0') as char); + num /= 8; + } + res + } + + fn to_hex_string(&self) -> String { + let tohex = |x| { + if x < 10 { + x as u8 + b'0' + } else { + x as u8 - 10 + b'a' + } + }; + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, (tohex(num % 16) + b'0') as char); + num /= 16; + } + res + } +} +impl ToString for u128 { + fn to_string(&self) -> String { + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, ((num % 10) as u8 + b'0') as char); + num /= 10; + } + res + } + + fn to_octal_string(&self) -> String { + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, ((num % 8) as u8 + b'0') as char); + num /= 8; + } + res + } + + fn to_hex_string(&self) -> String { + let tohex = |x| { + if x < 10 { + x as u8 + b'0' + } else { + x as u8 - 10 + b'a' + } + }; + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, (tohex(num % 16) + b'0') as char); + num /= 16; + } + res + } +} diff --git a/src/libk/alloc/vec.rs b/src/libk/alloc/vec.rs index 1a65451..66df0b2 100644 --- a/src/libk/alloc/vec.rs +++ b/src/libk/alloc/vec.rs @@ -5,7 +5,7 @@ use core::{ slice, }; -use alloc::alloc::{alloc, dealloc}; +use crate::libk::alloc::alloc::{alloc, dealloc}; pub struct Vec { pointer: *mut T, @@ -205,7 +205,12 @@ impl IntoIterator for Vec { type IntoIter = VecIter; fn into_iter(self) -> Self::IntoIter { - todo!() + VecIter { + pointer: self.pointer, + index: 0, + length: self.length, + capacity: self.capacity, + } } } -- 2.43.0 From 8352069285400300fdc80a832feba51cd710fce4 Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Sat, 27 Apr 2024 19:02:37 +0800 Subject: [PATCH 10/18] =?UTF-8?q?=E8=A7=A3=E5=86=B3string=E5=92=8Cvec?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- debug/2024-04.txt | 28 +++++++++- src/libk/alloc/string.rs | 108 ++++++++++++++++++++++++++++++++++++--- src/libk/alloc/vec.rs | 34 ++++++------ src/libk/core/mod.rs | 1 + src/libk/core/ptr/mod.rs | 30 +++++++++++ 5 files changed, 176 insertions(+), 25 deletions(-) create mode 100644 src/libk/core/ptr/mod.rs diff --git a/debug/2024-04.txt b/debug/2024-04.txt index 914d7c4..565e054 100644 --- a/debug/2024-04.txt +++ b/debug/2024-04.txt @@ -17,7 +17,7 @@ Stack trace: 使用了core crate中的String,导致最终调用了一个含有未正确链接的函数。 ======== 04-23 13:55 ======== -Hit null pointer as 0x100351a. +Hit null pointer at 0x100351a. Stack trace: 0x00000000010054d0 -> metaverse::kernel::tty::tty::MessageBuilder::message_mut 0x0000000001006170 -> ::to_string @@ -27,3 +27,29 @@ Stack trace: 0x0000000001003f60 -> alloc::alloc::alloc 0x00000000010034f0 -> core::ptr::read_volatile::precondition_check 使用了core crate的alloc函数,其中含有未正确链接的函数。 + +======== 04-26 02:56 ======== +Hit null pointer in metaverse::libk::alloc::string::String::clone() +Stack trace: + 0x0000000001005520 -> metaverse::kernel::tty::tty::format_message + 0x00000000010052f0 -> metaverse::kernel::tty::tty::MessageBuilder::message_mut + 0x00000000010073f0 -> ::to_string + 0x0000000001009a90 -> ::clone +使用了Clone的derive宏,含有未正确链接的函数。 + +======== 04-26 15:55 ======== +Hit null pointer at 0x1003672. +Stack trace: + 0x0000000001007600 -> ::to_string + 0x0000000001007030 -> metaverse::libk::alloc::string::String::insert + 0x0000000001008330 -> metaverse::libk::alloc::vec::Vec::insert + 0x0000000001007950 -> metaverse::libk::alloc::vec::Vec::extend_capacity + 0x0000000001003630 -> core::intrinsics::copy::precondition_check +将*mut u8转换为*mut T时调用了此函数,导致调用了一个未正确链接的函数。 + +======== 04-27 16:06 ======== +Trapped into a infinite loop. +Stack trace: + 0x0000000001006c30 -> interrupt_req_DE + 0x0000000001005420 -> metaverse::kernel::tty::tty::MessageBuilder::append + 0x0000000001008410 -> metaverse::libk::alloc::vec::Vec::append diff --git a/src/libk/alloc/string.rs b/src/libk/alloc/string.rs index c106e0c..1676e4f 100644 --- a/src/libk/alloc/string.rs +++ b/src/libk/alloc/string.rs @@ -2,7 +2,7 @@ use core::ops::{Add, AddAssign}; use super::vec::{Vec, VecIterator}; -#[derive(Clone, Default)] +#[derive(Default)] pub struct String { data: Vec, u8data: Vec, @@ -75,6 +75,15 @@ impl AddAssign for String { } } +impl Clone for String { + fn clone(&self) -> Self { + Self { + data: self.data.clone(), + u8data: self.u8data.clone(), + } + } +} + pub trait ToString { fn to_string(&self) -> String; fn to_octal_string(&self) -> String { @@ -129,7 +138,7 @@ impl ToString for u8 { let mut num = *self; let mut res = String::new(); while num != 0 { - res.insert(0, (tohex(num % 16) + b'0') as char); + res.insert(0, tohex(num % 16) as char); num /= 16; } res @@ -144,6 +153,9 @@ impl ToString for u16 { res.insert(0, ((num % 10) as u8 + b'0') as char); num /= 10; } + if res.len() == 0 { + res.insert(0, '0'); + } res } @@ -154,6 +166,9 @@ impl ToString for u16 { res.insert(0, ((num % 8) as u8 + b'0') as char); num /= 8; } + if res.len() == 0 { + res.insert(0, '0'); + } res } @@ -168,12 +183,16 @@ impl ToString for u16 { let mut num = *self; let mut res = String::new(); while num != 0 { - res.insert(0, (tohex(num % 16) + b'0') as char); + res.insert(0, tohex(num % 16) as char); num /= 16; } + if res.len() == 0 { + res.insert(0, '0'); + } res } } + impl ToString for u32 { fn to_string(&self) -> String { let mut num = *self; @@ -182,6 +201,9 @@ impl ToString for u32 { res.insert(0, ((num % 10) as u8 + b'0') as char); num /= 10; } + if res.len() == 0 { + res.insert(0, '0'); + } res } @@ -192,6 +214,9 @@ impl ToString for u32 { res.insert(0, ((num % 8) as u8 + b'0') as char); num /= 8; } + if res.len() == 0 { + res.insert(0, '0'); + } res } @@ -206,12 +231,16 @@ impl ToString for u32 { let mut num = *self; let mut res = String::new(); while num != 0 { - res.insert(0, (tohex(num % 16) + b'0') as char); + res.insert(0, tohex(num % 16) as char); num /= 16; } + if res.len() == 0 { + res.insert(0, '0'); + } res } } + impl ToString for u64 { fn to_string(&self) -> String { let mut num = *self; @@ -220,6 +249,9 @@ impl ToString for u64 { res.insert(0, ((num % 10) as u8 + b'0') as char); num /= 10; } + if res.len() == 0 { + res.insert(0, '0'); + } res } @@ -230,6 +262,9 @@ impl ToString for u64 { res.insert(0, ((num % 8) as u8 + b'0') as char); num /= 8; } + if res.len() == 0 { + res.insert(0, '0'); + } res } @@ -244,12 +279,16 @@ impl ToString for u64 { let mut num = *self; let mut res = String::new(); while num != 0 { - res.insert(0, (tohex(num % 16) + b'0') as char); + res.insert(0, tohex(num % 16) as char); num /= 16; } + if res.len() == 0 { + res.insert(0, '0'); + } res } } + impl ToString for u128 { fn to_string(&self) -> String { let mut num = *self; @@ -258,6 +297,9 @@ impl ToString for u128 { res.insert(0, ((num % 10) as u8 + b'0') as char); num /= 10; } + if res.len() == 0 { + res.insert(0, '0'); + } res } @@ -268,6 +310,9 @@ impl ToString for u128 { res.insert(0, ((num % 8) as u8 + b'0') as char); num /= 8; } + if res.len() == 0 { + res.insert(0, '0'); + } res } @@ -282,9 +327,60 @@ impl ToString for u128 { let mut num = *self; let mut res = String::new(); while num != 0 { - res.insert(0, (tohex(num % 16) + b'0') as char); + res.insert(0, tohex(num % 16) as char); num /= 16; } + if res.len() == 0 { + res.insert(0, '0'); + } + res + } +} + +impl ToString for usize { + fn to_string(&self) -> String { + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, ((num % 10) as u8 + b'0') as char); + num /= 10; + } + if res.len() == 0 { + res.insert(0, '0'); + } + res + } + + fn to_octal_string(&self) -> String { + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, ((num % 8) as u8 + b'0') as char); + num /= 8; + } + if res.len() == 0 { + res.insert(0, '0'); + } + res + } + + fn to_hex_string(&self) -> String { + let tohex = |x| { + if x < 10 { + x as u8 + b'0' + } else { + x as u8 - 10 + b'a' + } + }; + let mut num = *self; + let mut res = String::new(); + while num != 0 { + res.insert(0, tohex(num % 16) as char); + num /= 16; + } + if res.len() == 0 { + res.insert(0, '0'); + } res } } diff --git a/src/libk/alloc/vec.rs b/src/libk/alloc/vec.rs index 66df0b2..a33e2dc 100644 --- a/src/libk/alloc/vec.rs +++ b/src/libk/alloc/vec.rs @@ -1,11 +1,14 @@ use core::{ alloc::Layout, ops::{Index, IndexMut, Range, RangeFull}, - ptr::{addr_of_mut, null_mut}, + ptr::addr_of_mut, slice, }; -use crate::libk::alloc::alloc::{alloc, dealloc}; +use crate::libk::{ + alloc::alloc::{alloc, dealloc}, + core::ptr::PtrOptions, +}; pub struct Vec { pointer: *mut T, @@ -16,15 +19,15 @@ pub struct Vec { impl Vec { pub fn new() -> Self { Self { - pointer: null_mut(), + pointer: unsafe { alloc(Layout::array::(4).unwrap()).cast() }, length: 0, - capacity: 0, + capacity: 4, } } unsafe fn extend_capacity(&mut self) { - let newp = alloc(Layout::array::(self.capacity * 2).unwrap()).cast(); - self.pointer.copy_to(newp, self.length); + let newp: *mut T = alloc(Layout::array::(self.capacity * 2).unwrap()).cast(); + self.pointer.kcopy_to(newp, self.length); dealloc( self.pointer.cast(), Layout::array::(self.capacity).unwrap(), @@ -34,11 +37,6 @@ impl Vec { } pub fn push(&mut self, item: T) { - if self.pointer.is_null() { - self.pointer = unsafe { alloc(Layout::array::(4).unwrap()).cast() }; - self.capacity = 4; - self.length = 0; - } if self.capacity == self.length { unsafe { self.extend_capacity() } } @@ -54,11 +52,11 @@ impl Vec { unsafe { if rearlen != 0 { let tmp = alloc(Layout::array::(rearlen).unwrap()).cast(); - self.pointer.offset(index as isize).copy_to(tmp, rearlen); + self.pointer.offset(index as isize).kcopy_to(tmp, rearlen); self.pointer.offset(index as isize).write(item); self.pointer .offset(index as isize + 1) - .copy_from(tmp, rearlen); + .kcopy_from(tmp, rearlen); } else { self.pointer.offset(self.length as isize).write(item); } @@ -73,7 +71,7 @@ impl Vec { unsafe { self.pointer .offset(self.length as isize) - .copy_from(v.pointer.cast_const(), v.length) + .kcopy_from(v.pointer.cast_const(), v.length) }; self.length += v.length; } @@ -108,8 +106,8 @@ impl Vec { let mut t: T = T::default(); let pt = addr_of_mut!(t); unsafe { - pt.copy_from(self.pointer.offset(index as isize).cast_const(), 1); - self.pointer.offset(index as isize).copy_from( + pt.kcopy_from(self.pointer.offset(index as isize).cast_const(), 1); + self.pointer.offset(index as isize).kcopy_from( self.pointer.offset(index as isize + 1), self.length - index - 1, ); @@ -172,7 +170,7 @@ impl Clone for Vec { }; unsafe { res.pointer - .copy_from(self.pointer.cast_const(), self.length) + .kcopy_from(self.pointer.cast_const(), self.length) }; res } @@ -264,7 +262,7 @@ impl Iterator for VecIter { let res = unsafe { self.pointer .offset(self.index as isize) - .copy_to(addr_of_mut!(t), 1); + .kcopy_to(addr_of_mut!(t), 1); Some(t) }; self.index += 1; diff --git a/src/libk/core/mod.rs b/src/libk/core/mod.rs index e69de29..d0511c6 100644 --- a/src/libk/core/mod.rs +++ b/src/libk/core/mod.rs @@ -0,0 +1 @@ +pub mod ptr; \ No newline at end of file diff --git a/src/libk/core/ptr/mod.rs b/src/libk/core/ptr/mod.rs new file mode 100644 index 0000000..4341346 --- /dev/null +++ b/src/libk/core/ptr/mod.rs @@ -0,0 +1,30 @@ +use core::mem::{size_of, transmute}; + +extern "C" { + pub fn memset(des: *const u8, src: u8, len: usize); + pub fn memcpy(des: *const u8, src: *const u8, len: usize); + pub fn strlen(des: *const u8) -> usize; +} + +pub trait PtrOptions { + unsafe fn kcopy_from(self, src: *const T, count: usize); + unsafe fn kcopy_to(self, des: *const T, count: usize); +} + +impl PtrOptions for *mut T { + unsafe fn kcopy_from(self, src: *const T, count: usize) { + memcpy( + transmute(self.cast_const()), + transmute(src), + count * size_of::(), + ); + } + + unsafe fn kcopy_to(self, des: *const T, count: usize) { + memcpy( + transmute(des), + transmute(self.cast_const()), + count * size_of::(), + ); + } +} -- 2.43.0 From 2d6b898c497836dbf0eb20b262fc41fbdb8f0d01 Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Sat, 27 Apr 2024 19:03:02 +0800 Subject: [PATCH 11/18] =?UTF-8?q?=E4=BD=BF=E4=B8=AD=E6=96=AD=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E9=87=8D=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/kernel/arch/x86_64/interrupt.in | 4 ++++ src/kernel/arch/x86_64/interrupt_x86_64.s | 27 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/kernel/arch/x86_64/interrupt.in b/src/kernel/arch/x86_64/interrupt.in index 9d9e9bf..1382588 100644 --- a/src/kernel/arch/x86_64/interrupt.in +++ b/src/kernel/arch/x86_64/interrupt.in @@ -51,7 +51,10 @@ add rsp, 4 %endmacro +; 此处的cli在进入rust的中断处理函数后才会对应sti +; 因为在保存栈帧前中断不可以被打断 %macro interrupt_entry_enter 0 + cli push rbp lea rbp, [rsp] store_regs @@ -62,6 +65,7 @@ retrieve_section retrieve_regs leave + sti %endmacro ; 用于带有错误码的中断,创建一个没有错误码的上下文拷贝 diff --git a/src/kernel/arch/x86_64/interrupt_x86_64.s b/src/kernel/arch/x86_64/interrupt_x86_64.s index 5ca55b7..5fb0bcc 100644 --- a/src/kernel/arch/x86_64/interrupt_x86_64.s +++ b/src/kernel/arch/x86_64/interrupt_x86_64.s @@ -9,3 +9,30 @@ interrupt_open: interrupt_close: cli ret + + global interrupt_rust_enter + extern TSS +; usize interrupt_rust_enter() +; 返回值用于为寄存器rax占位 +interrupt_rust_enter: + ; *(rsp - 8) = *(TSS + 36) + mov rax, [TSS + 36] + mov qword [rsp - 8], rax + ; *(TSS + 36) = rsp - 8 + mov rax, TSS + lea rax, [rax + 36] + mov [rax], rsp + sub qword [rax], 8 + sti + ret + + global interrupt_rust_leave + extern TSS +; usize interrupt_rust_leave() +; 返回值用于为寄存器rax占位 +interrupt_rust_leave: + cli + ; *(TSS + 36) = *(rsp - 8) + mov rax, [rsp - 8] + mov [TSS + 36], rax + ret -- 2.43.0 From 41dd0a0e00b0441660b06422cc2487284f0f54fc Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Sat, 27 Apr 2024 20:56:10 +0800 Subject: [PATCH 12/18] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=9F=BA=E4=BA=8Erust?= =?UTF-8?q?=E7=9A=84=E7=94=A8=E4=BA=8E=E6=B5=8B=E8=AF=95=E7=9A=84=E4=B8=AD?= =?UTF-8?q?=E6=96=AD=E5=A4=84=E7=90=86=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/kernel/arch/x86_64/interrupt_procs.h | 14 +- src/kernel/Makefile | 3 +- src/kernel/arch.rs | 2 + src/kernel/arch/x86_64/entry.s | 3 +- src/kernel/arch/x86_64/entry32.s | 5 +- src/kernel/arch/x86_64/interrupt.rs | 49 +++++ src/kernel/arch/x86_64/interrupt_procs.c | 190 ------------------- src/kernel/arch/x86_64/interrupt_procs.s | 14 +- src/kernel/arch/x86_64/interrupt_x86_64.c | 2 + src/kernel/arch/x86_64/mod.rs | 2 + src/kernel/arch/x86_64/proc.rs | 32 ++++ src/kernel/main.c | 2 + src/kernel/mod.rs | 2 +- 13 files changed, 117 insertions(+), 203 deletions(-) create mode 100644 src/kernel/arch.rs create mode 100644 src/kernel/arch/x86_64/interrupt.rs delete mode 100644 src/kernel/arch/x86_64/interrupt_procs.c create mode 100644 src/kernel/arch/x86_64/mod.rs create mode 100644 src/kernel/arch/x86_64/proc.rs diff --git a/include/kernel/arch/x86_64/interrupt_procs.h b/include/kernel/arch/x86_64/interrupt_procs.h index cd6d2a0..f4a124d 100644 --- a/include/kernel/arch/x86_64/interrupt_procs.h +++ b/include/kernel/arch/x86_64/interrupt_procs.h @@ -46,13 +46,13 @@ typedef void (*interrupt_entry)(); /** * @name interrupt_request * @addindex 平台定制函数 - * + * * 中断请求处理程序。 - * + * * ```c * void interrupt_request(u64 rip, u64 rsp); * ``` - * + * * 由中断入口程序调用。 */ typedef void (*interrupt_request)(u64 rip, u64 rsp, u64 errcode); @@ -77,11 +77,13 @@ typedef void (*interrupt_request)(u64 rip, u64 rsp, u64 errcode); #define DE #define NMI #define BP +#define OF interrupt_entry_gen(UNSUPPORTED); -interrupt_entry_gen(DE); -interrupt_entry_gen(NMI); -interrupt_entry_gen(BP); +interrupt_entry_gen(DE); // irq0 +interrupt_entry_gen(NMI); // irq2 +interrupt_entry_gen(BP); // irq3 +interrupt_entry_gen(OF); // ira4 #endif diff --git a/src/kernel/Makefile b/src/kernel/Makefile index 7346f5b..39d4401 100644 --- a/src/kernel/Makefile +++ b/src/kernel/Makefile @@ -13,8 +13,7 @@ ifdef release CCFLAGS := ${CCFLAGS} -O2 endif -C_SRCS = main.c tty.c font.c memm.c memm_${ARCH}.c raw.c time.c syscall_${ARCH}.c interrupt_${ARCH}.c \ - interrupt_procs.c +C_SRCS = main.c tty.c font.c memm.c memm_${ARCH}.c raw.c time.c syscall_${ARCH}.c interrupt_${ARCH}.c C_OBJS = ${C_SRCS:.c=.c.o} ################################ diff --git a/src/kernel/arch.rs b/src/kernel/arch.rs new file mode 100644 index 0000000..832557e --- /dev/null +++ b/src/kernel/arch.rs @@ -0,0 +1,2 @@ +#[cfg(target_arch = "x86_64")] +pub mod x86_64; diff --git a/src/kernel/arch/x86_64/entry.s b/src/kernel/arch/x86_64/entry.s index 2647c42..4e700fc 100644 --- a/src/kernel/arch/x86_64/entry.s +++ b/src/kernel/arch/x86_64/entry.s @@ -11,7 +11,8 @@ init64: mov rsp, rax mov rdi, rbx - lidt [0x104010] + ; 加载idt + lidt [0x104010] ; idt_ptr ; 加载系统调用相关寄存器 ; IA32_STAR = 0x0018_0008_0000_0000 diff --git a/src/kernel/arch/x86_64/entry32.s b/src/kernel/arch/x86_64/entry32.s index d6f99f6..ee6820f 100644 --- a/src/kernel/arch/x86_64/entry32.s +++ b/src/kernel/arch/x86_64/entry32.s @@ -21,17 +21,18 @@ init32: mov dword [edi], 0 ; 设置PD0中的PDE - mov ecx, 64 + ; 以2MB页映射前64MB + mov ecx, 32 mov eax, 0 mov edi, 0x103000 ; PD0 init32_loop0: mov edx, eax add edx, 0x183 mov dword [edi], edx - add eax, 0x200000 ; 2MB add edi, 4 mov dword [edi], 0 add edi, 4 + add eax, 0x200000 ; 2MB loop init32_loop0 ; 加载GDTR、段寄存器和TR寄存器 diff --git a/src/kernel/arch/x86_64/interrupt.rs b/src/kernel/arch/x86_64/interrupt.rs new file mode 100644 index 0000000..7fb3265 --- /dev/null +++ b/src/kernel/arch/x86_64/interrupt.rs @@ -0,0 +1,49 @@ +use crate::{kernel::tty::tty::Tty, libk::alloc::string::ToString, message, message_raw}; + +extern "C" { + pub fn interrupt_open(); + pub fn interrupt_close(); + // 以下两个函数签名的返回值用于使编译器保留rax寄存器 + pub fn interrupt_rust_enter() -> usize; + pub fn interrupt_rust_leave() -> usize; +} + +#[no_mangle] +unsafe extern "C" fn interrupt_req_UNSUPPORTED(rip: u64, rsp: u64, errcode: u64) -> ! { + interrupt_rust_enter(); + let tty = Tty::from_id(0).unwrap(); + tty.enable(); + let msg = message_raw!( + Msg("Panic:"), + FgColor::(Color::RED), + Msg(" Kernel hit an unsupported interrupt\n\twith %rip=0x"), + Msg(rip.to_hex_string()), + Msg(" and %rsp=0x"), + Msg(rsp.to_hex_string()) + ); + tty.print(msg); + loop {} +} + +#[no_mangle] +unsafe extern "C" fn interrupt_req_DE(rip: u64, rsp: u64, errcode: u64) { + interrupt_rust_enter(); + let tty = Tty::from_id(0).unwrap(); + tty.enable(); + tty.print(message!( + "{Panic}: divided by zero. rip=0x{}.\n", + FmtMeta::Color(Color::RED), + FmtMeta::Pointer(rip as usize) + )); + loop {} + interrupt_rust_leave(); +} + +#[no_mangle] +unsafe extern "C" fn interrupt_req_NMI(rip: u64, rsp: u64, errcode: u64) {} + +#[no_mangle] +unsafe extern "C" fn interrupt_req_BP(rip: u64, rsp: u64, errcode: u64) {} + +#[no_mangle] +unsafe extern "C" fn interrupt_req_OF(rip: u64, rsp: u64, errcode: u64) {} diff --git a/src/kernel/arch/x86_64/interrupt_procs.c b/src/kernel/arch/x86_64/interrupt_procs.c deleted file mode 100644 index 92359ac..0000000 --- a/src/kernel/arch/x86_64/interrupt_procs.c +++ /dev/null @@ -1,190 +0,0 @@ -#include -#include -#include -#include -#include -#include - -interrupt_req_gen(UNSUPPORTED) -{ - tty **tty0_option = tty_get(0); - if (tty0_option == nullptr) - { - KERNEL_TODO(); - } - tty *tty0 = *tty0_option; - tty_text_print(tty0, "Panic: Unsupported interrupt.", RED, BLACK); - KERNEL_TODO(); -} - -static void rip_not_cannonical(u64 rip, tty *tty0) -{ - char num[17]; - memset(num, 0, sizeof(num)); - pointer_to_string(rip, num); - tty_text_print(tty0, "Panic", RED, BLACK); - tty_text_print(tty0, ": Unexpected non-cannonical %rip value ", WHITE, BLACK); - tty_text_print(tty0, num, ORANGE, BLACK); - tty_text_print(tty0, ".\n", WHITE, BLACK); - KERNEL_TODO(); -} - -interrupt_req_gen(DE) -{ - tty **tty0_option = tty_get(0); - if (tty0_option == nullptr) - { - KERNEL_TODO(); - } - tty *tty0 = *tty0_option; - if (!is_cannonical(rip)) - { - rip_not_cannonical(rip, tty0); - } - if (!is_user_address(rip)) - { - char nums[34]; - memset(nums, 0, sizeof(nums)); - pointer_to_string(rip, nums); - pointer_to_string(rsp, nums + 17); - tty_text_print(tty0, "Panic", RED, BLACK); - tty_text_print(tty0, ": Divided by zero occurs in kernel,\n\t", WHITE, BLACK); - tty_text_print(tty0, "with %rip=", WHITE, BLACK); - tty_text_print(tty0, "0x", ORANGE, BLACK); - tty_text_print(tty0, nums, ORANGE, BLACK); - tty_text_print(tty0, ", %rsp=", WHITE, BLACK); - tty_text_print(tty0, "0x", ORANGE, BLACK); - tty_text_print(tty0, nums + 17, ORANGE, BLACK); - tty_text_print(tty0, ".\n", WHITE, BLACK); - KERNEL_TODO(); - } - else - { // TODO 转储并结束进程 - KERNEL_TODO(); - } -} - -interrupt_req_gen(BP) -{ - tty **tty0_option = tty_get(0); - if (tty0_option == nullptr) - { - KERNEL_TODO(); - } - tty *tty0 = *tty0_option; - if (!is_cannonical(rip)) - { - rip_not_cannonical(rip, tty0); - } - proc_texture_registers_t *texture = (void *)errcode; - if (!is_user_address(rip)) - { - char nums[34]; - memset(nums, 0, sizeof(nums)); - pointer_to_string(rip, nums); - pointer_to_string(rsp, nums + 17); - tty_text_print(tty0, "Debug", PURPLE, BLACK); - tty_text_print(tty0, ": Kernel hit a breakpoint,\n\t", WHITE, BLACK); - tty_text_print(tty0, "with %rip=", WHITE, BLACK); - tty_text_print(tty0, "0x", ORANGE, BLACK); - tty_text_print(tty0, nums, ORANGE, BLACK); - tty_text_print(tty0, ", %rsp=", WHITE, BLACK); - tty_text_print(tty0, "0x", ORANGE, BLACK); - tty_text_print(tty0, nums + 17, ORANGE, BLACK); - tty_text_print(tty0, ",\n\t", WHITE, BLACK); - tty_text_print(tty0, "on texture: \n", WHITE, BLACK); - pointer_to_string(texture->rax, nums); - tty_text_print(tty0, "rax\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\t", WHITE, BLACK); - pointer_to_string(texture->rcx, nums); - tty_text_print(tty0, "rcx\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\n", WHITE, BLACK); - pointer_to_string(texture->rbx, nums); - tty_text_print(tty0, "rbx\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\t", WHITE, BLACK); - pointer_to_string(texture->rdx, nums); - tty_text_print(tty0, "rdx\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\n", WHITE, BLACK); - pointer_to_string(texture->rsi, nums); - tty_text_print(tty0, "rsi\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\t", WHITE, BLACK); - pointer_to_string(texture->rdi, nums); - tty_text_print(tty0, "rdi\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\n", WHITE, BLACK); - pointer_to_string(texture->rbp, nums); - tty_text_print(tty0, "rbp\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\t", WHITE, BLACK); - pointer_to_string(texture->rsp, nums); - tty_text_print(tty0, "rsp\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\n", WHITE, BLACK); - pointer_to_string(texture->rip, nums); - tty_text_print(tty0, "rip\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\t", WHITE, BLACK); - pointer_to_string(texture->rflags, nums); - tty_text_print(tty0, "rflags\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\n", WHITE, BLACK); - pointer_to_string(texture->r8, nums); - tty_text_print(tty0, "r8\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\t", WHITE, BLACK); - pointer_to_string(texture->r9, nums); - tty_text_print(tty0, "r9\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\n", WHITE, BLACK); - pointer_to_string(texture->r10, nums); - tty_text_print(tty0, "r10\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\t", WHITE, BLACK); - pointer_to_string(texture->r11, nums); - tty_text_print(tty0, "r11\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\n", WHITE, BLACK); - pointer_to_string(texture->r12, nums); - tty_text_print(tty0, "r12\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\t", WHITE, BLACK); - pointer_to_string(texture->r13, nums); - tty_text_print(tty0, "r13\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\n", WHITE, BLACK); - pointer_to_string(texture->r14, nums); - tty_text_print(tty0, "r14\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\t", WHITE, BLACK); - pointer_to_string(texture->r15, nums); - tty_text_print(tty0, "r15\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\n", WHITE, BLACK); - pointer_to_string(texture->cs, nums); - tty_text_print(tty0, "cs\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\t", WHITE, BLACK); - pointer_to_string(texture->ss, nums); - tty_text_print(tty0, "ss\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\n", WHITE, BLACK); - pointer_to_string(texture->ds, nums); - tty_text_print(tty0, "ds\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\t", WHITE, BLACK); - pointer_to_string(texture->es, nums); - tty_text_print(tty0, "es\t", WHITE, BLACK); - tty_text_print(tty0, nums, BLUE, BLACK); - tty_text_print(tty0, "\n", WHITE, BLACK); - KERNEL_TODO(); - } - else - { // TODO 将当前进程的状态设置为暂停并通知当前进程的调试程序 - KERNEL_TODO(); - } -} diff --git a/src/kernel/arch/x86_64/interrupt_procs.s b/src/kernel/arch/x86_64/interrupt_procs.s index d83e19e..c5a3a02 100644 --- a/src/kernel/arch/x86_64/interrupt_procs.s +++ b/src/kernel/arch/x86_64/interrupt_procs.s @@ -77,7 +77,7 @@ interrupt_entry_DE: interrupt_entry_leave iret - global interrupt_entry_DE + global interrupt_entry_NMI interrupt_entry_NMI: ; TODO 暂时不需要为这个中断实现任何功能 iret @@ -95,3 +95,15 @@ interrupt_entry_BP: interrupt_entry_leave iret + + global interrupt_entry_OF + extern interrupt_req_OF +interrupt_entry_OF: + interrupt_entry_enter + + mov rdi, [rsp + 128] + mov rsi, [rsp + 152] + call interrupt_req_OF + + interrupt_entry_leave + iret diff --git a/src/kernel/arch/x86_64/interrupt_x86_64.c b/src/kernel/arch/x86_64/interrupt_x86_64.c index 6879f22..c2e2eb6 100644 --- a/src/kernel/arch/x86_64/interrupt_x86_64.c +++ b/src/kernel/arch/x86_64/interrupt_x86_64.c @@ -20,6 +20,8 @@ void interrupt_init() interrupt_register_gate(gate, 2); trap_gate_generate(gate, interrupt_entry_sym(BP)); interrupt_register_gate(gate, 3); + trap_gate_generate(gate, interrupt_entry_sym(OF)); + interrupt_register_gate(gate, 4); interrupt_open(); } diff --git a/src/kernel/arch/x86_64/mod.rs b/src/kernel/arch/x86_64/mod.rs new file mode 100644 index 0000000..9fe4e4d --- /dev/null +++ b/src/kernel/arch/x86_64/mod.rs @@ -0,0 +1,2 @@ +pub mod interrupt; +pub mod proc; diff --git a/src/kernel/arch/x86_64/proc.rs b/src/kernel/arch/x86_64/proc.rs new file mode 100644 index 0000000..1961f31 --- /dev/null +++ b/src/kernel/arch/x86_64/proc.rs @@ -0,0 +1,32 @@ +#[repr(C)] +#[derive(Debug)] +pub struct RegisterTexture { + es: u16, + ds: u16, + reserved: u32, + r15: u64, + r14: u64, + r13: u64, + r12: u64, + r11: u64, + r10: u64, + r9: u64, + r8: u64, + rdi: u64, + rsi: u64, + rdx: u64, + rbx: u64, + rcx: u64, + rax: u64, + rbp: u64, + rip: u64, + cs: u64, + rflags: u64, + rsp: u64, + ss: u64, +} + +#[derive(Debug)] +pub struct ProcessTexture { + register: RegisterTexture, +} diff --git a/src/kernel/main.c b/src/kernel/main.c index 495e4b2..161fa04 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -48,6 +48,8 @@ void kmain(void *mb2_bootinfo) // 初始化中断管理 interrupt_init(); + int i = 1 / 0; + // 初始化系统调用 syscall_init(); diff --git a/src/kernel/mod.rs b/src/kernel/mod.rs index 583319c..fc90c48 100644 --- a/src/kernel/mod.rs +++ b/src/kernel/mod.rs @@ -3,4 +3,4 @@ pub mod klog; pub mod main; pub mod memm; pub mod tty; -pub mod sync; +pub mod arch; -- 2.43.0 From 6a935dc2b712efb3cbb6f72750e9428a6a4fa0de Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Thu, 2 May 2024 20:49:58 +0800 Subject: [PATCH 13/18] =?UTF-8?q?=E8=A7=A3=E5=86=B3Vec=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E5=85=83=E7=B4=A0=E6=97=B6=E9=95=BF=E5=BA=A6=E4=B8=8D=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libk/alloc/vec.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libk/alloc/vec.rs b/src/libk/alloc/vec.rs index a33e2dc..effb584 100644 --- a/src/libk/alloc/vec.rs +++ b/src/libk/alloc/vec.rs @@ -112,6 +112,7 @@ impl Vec { self.length - index - 1, ); } + self.length -= 1; t } -- 2.43.0 From 47f16d060e38aac95eec25303ca5bbcd32a55e18 Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Thu, 2 May 2024 20:51:57 +0800 Subject: [PATCH 14/18] =?UTF-8?q?=E5=B0=86=E4=B8=AD=E6=96=AD=E4=B8=AD?= =?UTF-8?q?=E6=89=80=E6=9C=89=E6=B6=88=E6=81=AF=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E6=8D=A2=E4=B8=BA=E6=96=B0=E7=9A=84message!=E5=AE=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/kernel/arch/x86_64/interrupt.rs | 21 +++++++++------------ src/kernel/tty/mod.rs | 6 ++++++ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/kernel/arch/x86_64/interrupt.rs b/src/kernel/arch/x86_64/interrupt.rs index 7fb3265..e957a4f 100644 --- a/src/kernel/arch/x86_64/interrupt.rs +++ b/src/kernel/arch/x86_64/interrupt.rs @@ -13,15 +13,11 @@ unsafe extern "C" fn interrupt_req_UNSUPPORTED(rip: u64, rsp: u64, errcode: u64) interrupt_rust_enter(); let tty = Tty::from_id(0).unwrap(); tty.enable(); - let msg = message_raw!( - Msg("Panic:"), - FgColor::(Color::RED), - Msg(" Kernel hit an unsupported interrupt\n\twith %rip=0x"), - Msg(rip.to_hex_string()), - Msg(" and %rsp=0x"), - Msg(rsp.to_hex_string()) - ); - tty.print(msg); + tty.print(message!( + "{Panic}: Kernel hit an {Unsupported} interrupt. \n", + FmtMeta::Color(Color::RED), + FmtMeta::Color(Color::YELLOW) + )); loop {} } @@ -31,11 +27,12 @@ unsafe extern "C" fn interrupt_req_DE(rip: u64, rsp: u64, errcode: u64) { let tty = Tty::from_id(0).unwrap(); tty.enable(); tty.print(message!( - "{Panic}: divided by zero. rip=0x{}.\n", + "{Panic}: Kernel hit {Divid Error} on rip=0x{} and rsp=0x{}.\n", FmtMeta::Color(Color::RED), - FmtMeta::Pointer(rip as usize) + FmtMeta::Color(Color::YELLOW), + FmtMeta::Pointer(rip as usize), + FmtMeta::Pointer(rsp as usize) )); - loop {} interrupt_rust_leave(); } diff --git a/src/kernel/tty/mod.rs b/src/kernel/tty/mod.rs index cebad05..0a899bd 100644 --- a/src/kernel/tty/mod.rs +++ b/src/kernel/tty/mod.rs @@ -47,6 +47,12 @@ macro_rules! message_raw { #[macro_export] macro_rules! message { + ( $fmtter : expr ) => {{ + use crate::kernel::tty::tty::MessageBuilder; + MessageBuilder::new() + .message($fmtter) + .build() + }}; ( $fmtter : expr, $( $e : expr ),* ) => {{ use crate::{ kernel::tty::tty::{ -- 2.43.0 From ba36b60b6820d699cdff3bf9da81c4bf287472bf Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Thu, 2 May 2024 22:31:43 +0800 Subject: [PATCH 15/18] =?UTF-8?q?nasm=E7=9A=84iret=E6=8C=87=E4=BB=A4?= =?UTF-8?q?=E6=98=AF32=E4=BD=8Diret=EF=BC=8C=E5=BF=85=E9=A1=BB=E4=BD=BF?= =?UTF-8?q?=E7=94=A8iretq=E6=89=8D=E4=BC=9A=E7=BF=BB=E8=AF=91=E4=B8=BA64?= =?UTF-8?q?=E4=BD=8Diret=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/kernel/arch/x86_64/interrupt_procs.s | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/kernel/arch/x86_64/interrupt_procs.s b/src/kernel/arch/x86_64/interrupt_procs.s index c5a3a02..968ff49 100644 --- a/src/kernel/arch/x86_64/interrupt_procs.s +++ b/src/kernel/arch/x86_64/interrupt_procs.s @@ -63,7 +63,7 @@ interrupt_entry_UNSUPPORTED: call interrupt_req_UNSUPPORTED interrupt_entry_leave - iret + iretq global interrupt_entry_DE extern interrupt_req_DE @@ -75,12 +75,12 @@ interrupt_entry_DE: call interrupt_req_DE interrupt_entry_leave - iret + iretq global interrupt_entry_NMI interrupt_entry_NMI: ; TODO 暂时不需要为这个中断实现任何功能 - iret + iretq global interrupt_entry_BP extern interrupt_req_BP @@ -94,7 +94,7 @@ interrupt_entry_BP: call interrupt_req_BP interrupt_entry_leave - iret + iretq global interrupt_entry_OF extern interrupt_req_OF @@ -106,4 +106,4 @@ interrupt_entry_OF: call interrupt_req_OF interrupt_entry_leave - iret + iretq -- 2.43.0 From 8e2d61de4a4fc80ff6f092d8793fdcaee16c4a52 Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Thu, 2 May 2024 23:14:43 +0800 Subject: [PATCH 16/18] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B1=8F=E5=B9=95?= =?UTF-8?q?=E6=BB=9A=E5=8A=A8=E6=97=B6=E5=BA=95=E4=B8=8B=E6=9C=89=E4=B8=80?= =?UTF-8?q?=E6=9D=A1=E6=B2=A1=E6=9C=89=E5=88=B7=E6=96=B0=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/kernel/tty/tty.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/kernel/tty/tty.c b/src/kernel/tty/tty.c index 21e1418..c5d13c2 100644 --- a/src/kernel/tty/tty.c +++ b/src/kernel/tty/tty.c @@ -77,6 +77,7 @@ inline static void scroll_buffer( usize dist) { memcpy(buffer, buffer + width * pixsize * dist, (height - dist) * width * pixsize); + memset(buffer + (height - dist) * width * pixsize, 0, dist * width * pixsize); } inline static void putchar( -- 2.43.0 From e6da1be4f5e5d587b07d31acf2aa3af86ac4e262 Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Thu, 2 May 2024 23:16:08 +0800 Subject: [PATCH 17/18] =?UTF-8?q?=E7=A1=AE=E8=AE=A4=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=8E=9Fmessage=5Fraw!=E5=AE=8F=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=B9=B6=E7=A7=BB=E9=99=A4message=5Fraw!?= =?UTF-8?q?=E5=AE=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/kernel/arch/x86_64/interrupt.rs | 12 ++++---- src/kernel/main.rs | 8 +---- src/kernel/tty/mod.rs | 45 ----------------------------- 3 files changed, 8 insertions(+), 57 deletions(-) diff --git a/src/kernel/arch/x86_64/interrupt.rs b/src/kernel/arch/x86_64/interrupt.rs index e957a4f..7e8160b 100644 --- a/src/kernel/arch/x86_64/interrupt.rs +++ b/src/kernel/arch/x86_64/interrupt.rs @@ -1,4 +1,4 @@ -use crate::{kernel::tty::tty::Tty, libk::alloc::string::ToString, message, message_raw}; +use crate::{kernel::tty::tty::Tty, message}; extern "C" { pub fn interrupt_open(); @@ -14,9 +14,11 @@ unsafe extern "C" fn interrupt_req_UNSUPPORTED(rip: u64, rsp: u64, errcode: u64) let tty = Tty::from_id(0).unwrap(); tty.enable(); tty.print(message!( - "{Panic}: Kernel hit an {Unsupported} interrupt. \n", + "{Panic}: Kernel hit an {Unsupported} interrupt on rip=0x{} and rsp=0x{}.\n", FmtMeta::Color(Color::RED), - FmtMeta::Color(Color::YELLOW) + FmtMeta::Color(Color::YELLOW), + FmtMeta::Pointer(rip as usize), + FmtMeta::Pointer(rsp as usize) )); loop {} } @@ -27,8 +29,8 @@ unsafe extern "C" fn interrupt_req_DE(rip: u64, rsp: u64, errcode: u64) { let tty = Tty::from_id(0).unwrap(); tty.enable(); tty.print(message!( - "{Panic}: Kernel hit {Divid Error} on rip=0x{} and rsp=0x{}.\n", - FmtMeta::Color(Color::RED), + "{Warning}: Kernel hit {Divid Error} on rip=0x{} and rsp=0x{}.\n", + FmtMeta::Color(Color::PURPLE), FmtMeta::Color(Color::YELLOW), FmtMeta::Pointer(rip as usize), FmtMeta::Pointer(rsp as usize) diff --git a/src/kernel/main.rs b/src/kernel/main.rs index 2be7822..c3d29cd 100644 --- a/src/kernel/main.rs +++ b/src/kernel/main.rs @@ -1,10 +1,4 @@ -use crate::{ - kernel::{ - klog::{KernelLogger, LoggerLevel}, - tty::tty::Tty, - }, - message_raw, -}; +use crate::kernel::tty::tty::Tty; #[no_mangle] extern "C" fn kmain_rust() -> ! { diff --git a/src/kernel/tty/mod.rs b/src/kernel/tty/mod.rs index 0a899bd..0e8fc16 100644 --- a/src/kernel/tty/mod.rs +++ b/src/kernel/tty/mod.rs @@ -1,50 +1,5 @@ pub mod tty; -#[macro_export] -macro_rules! message_msg { - () => {}; - ( $builder : expr, $e : expr) => { - $builder.message_mut($e); - }; -} - -#[macro_export] -macro_rules! message_fgc { - () => {}; - ( $builder : expr, $e : expr) => { - $builder.foreground_color_mut($e); - }; -} - -#[macro_export] -macro_rules! message_bgc { - () => {}; - ( $builder : expr, $e : expr ) => { - $builder.background_color_mut($e); - }; -} - -#[macro_export] -macro_rules! message_raw { - ( $( $e : expr ),* ) => {{ - use crate::{ - kernel::tty::tty::{MessageBuilder, BuilderFunctions::*, Color}, - message_msg, message_fgc, message_bgc - }; - let mut tmp_builder = MessageBuilder::new(); - $( - if let Msg(e) = $e { - message_msg!(tmp_builder, &e); - } else if let FgColor(c) = $e { - message_fgc!(tmp_builder, c); - } else if let BgColor(c) = $e { - message_bgc!(tmp_builder, c); - } - )* - tmp_builder.build() - }}; -} - #[macro_export] macro_rules! message { ( $fmtter : expr ) => {{ -- 2.43.0 From dfa5a81c8d09a0f54ba706489f85827c620b529a Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Fri, 3 May 2024 05:40:56 +0800 Subject: [PATCH 18/18] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E4=BD=BF=E7=94=A8rust?= =?UTF-8?q?=20panic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/contribution.md | 7 ++----- src/lib.rs | 8 ++++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/docs/contribution.md b/docs/contribution.md index b9d3015..3b475b1 100644 --- a/docs/contribution.md +++ b/docs/contribution.md @@ -28,9 +28,6 @@ vscode下的`rust-analyzer`插件会在`src/lib.rs`中误报`can't find crate fo ] ``` -## 需要注意的还未解决的问题 +## 需要注意的问题 -* rust中所有有关字符串格式化的宏中,出现超过一个不止是`{}`的格式化占位符时内核必然崩溃。 -* rust中所有有关字符串格式化的宏中,出现需要调用自定义的`Debug trait`的类型时内核必然崩溃,推荐定义`ToString trait`并调用`.to_string()`后传入宏参数。 -* 鉴于以上两条原因,不建议在复杂的字符串格式化任务中使用`format!()`宏。推荐通过使用`::kernel::tty::tty::MessageBuilder`构造`kernel::tty::tty::Message`对象,或使用 - `message`宏,并调用此对象的`.to_string()`方法实现格式化字符串。 +* 在任何情况下不要使用Rust的panic。 diff --git a/src/lib.rs b/src/lib.rs index 642c3f5..9563a24 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,10 +6,18 @@ extern crate alloc; use core::panic::PanicInfo; +use kernel::tty::tty::Tty; + pub mod kernel; pub mod libk; #[panic_handler] unsafe fn kernel_panic_handler(_info: &PanicInfo) -> ! { + let tty = Tty::from_id(0).unwrap(); + tty.enable(); + tty.print(message!( + "Hit a {Rust} panic.", + FmtMeta::Color(Color::ORANGE) + )); loop {} } -- 2.43.0