From f2f6dc396da7627a1272eca2f679a1d7c003ccbb Mon Sep 17 00:00:00 2001
From: qiuzhi <992130522@qq.com>
Date: Thu, 21 Nov 2024 02:00:29 +0800
Subject: [PATCH] 11.21

---
 .gitignore                 |  2 ++
 Makefile                   | 35 ++++++++++++++++++++++++++
 include.mk                 | 50 ++++++++++++++++++++++++++++++++++++++
 include/arch/riscv/types.h | 24 ++++++++++++++++++
 include/arch/target.h      |  6 +++++
 kernel/boot/kernel_enter.S | 27 ++++++++++++++++++++
 kernel/boot/kernel_main.c  |  7 ++++++
 linker/kernel.ld           | 34 ++++++++++++++++++++++++++
 8 files changed, 185 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 Makefile
 create mode 100644 include.mk
 create mode 100644 include/arch/riscv/types.h
 create mode 100644 include/arch/target.h
 create mode 100644 kernel/boot/kernel_enter.S
 create mode 100644 kernel/boot/kernel_main.c
 create mode 100644 linker/kernel.ld

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..01f9cb9
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+build/
+.vscode/
\ No newline at end of file
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..9723195
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,35 @@
+include include.mk
+
+build : ${KERNEL} 
+
+clean :
+	rm -rf ${BUILD_DIR}/* ${KERNEL}
+
+run :
+	${QEMU} ${QEMUOPS}
+
+debug :
+	${QEMU} ${QEMUOPS} ${QEMU_DEBUG_OPS}
+
+test :
+	@echo "KERNEL_C_FILES = ${KERNEL_C_FILES}"
+	@echo "KERNEL_ASM_FILES = ${KERNEL_ASM_FILES}"
+	@echo "KERNEL_OBJ_FILES = ${KERNEL_OBJ_FILES}"
+
+define c_files_compile_rule
+${BUILD_DIR}/$(dir $(1))$(basename $(notdir $(1)))_c.o : $(1)
+	mkdir -p $${@D}
+	${CC} $(COPS) -o $${@} $${<}
+endef
+
+define asm_files_compile_rule
+${BUILD_DIR}/$(dir $(1))$(basename $(notdir $(1)))_s.o : $(1)
+	mkdir -p $${@D}
+	${CC} $(COPS) -o $${@} $${<}
+endef
+
+$(foreach c_file,${KERNEL_C_FILES},$(eval $(call c_files_compile_rule,${c_file})))
+$(foreach asm_file,${KERNEL_ASM_FILES},$(eval $(call asm_files_compile_rule,${asm_file})))
+
+${KERNEL} : ${LINKER_DIR}/kernel.ld ${KERNEL_OBJ_FILES}
+	${LD} -T $< -o ${KERNEL} ${KERNEL_OBJ_FILES}
\ No newline at end of file
diff --git a/include.mk b/include.mk
new file mode 100644
index 0000000..4a6a1e5
--- /dev/null
+++ b/include.mk
@@ -0,0 +1,50 @@
+# 机器配置
+MACHINE = virt
+NCPU = 8
+MEMORY = 1G
+BIOS = default
+KERNEL = Stars-kernel
+# IMAGE := sdcard.img
+
+# 编译配置
+O = O0
+
+# qemu
+QEMU = qemu-system-riscv64
+QEMUOPS += -machine ${MACHINE}
+QEMUOPS += -smp ${NCPU}
+QEMUOPS += -m ${MEMORY}
+QEMUOPS += -nographic
+QEMUOPS += -kernel ${KERNEL}
+QEMUOPS += -bios ${BIOS}
+
+QEMU_DEBUG_OPS += -S
+QEMU_DEBUG_OPS += -s	
+
+# GNU Linux riscv64工具链
+GNU = riscv64-linux-gnu
+CC = ${GNU}-gcc
+LD = ${GNU}-ld
+COPS += -save-temps=obj
+COPS += -c
+COPS += -nostdlib
+COPS += -nostdinc
+COPS += -Iinclude
+COPS += -mabi=lp64
+COPS += -march=rv64imafd
+COPS += -mcmodel=medany
+# COPS += -g
+COPS += -${O}
+COPS += -Wall
+COPS += -DNCPU=${NCPU}
+
+LDOPS += 
+
+# 路径配置
+BUILD_DIR = build
+LINKER_DIR = linker
+KERNEL_DIR = kernel
+KERNEL_C_FILES = $(shell find ${KERNEL_DIR} -type f -name "*.c")
+KERNEL_ASM_FILES = $(shell find ${KERNEL_DIR} -type f -name "*.S")
+KERNEL_OBJ_FILES += $(KERNEL_C_FILES:%.c=${BUILD_DIR}/%_c.o)
+KERNEL_OBJ_FILES += $(KERNEL_ASM_FILES:%.S=${BUILD_DIR}/%_s.o)
\ No newline at end of file
diff --git a/include/arch/riscv/types.h b/include/arch/riscv/types.h
new file mode 100644
index 0000000..f2ae8b0
--- /dev/null
+++ b/include/arch/riscv/types.h
@@ -0,0 +1,24 @@
+#ifndef _ARCH_RISCV_TYPES_H
+#define _ARCH_RISCV_TYPES_H
+
+// RISC-V架构下StarsOS内类型定义
+
+typedef char i8_t;
+typedef short i16_t;
+typedef int i32_t;
+typedef long i64_t;
+
+typedef unsigned char u8_t;
+typedef unsigned short u16_t;
+typedef unsigned int u32_t;
+typedef unsigned long u64_t;
+
+enum bool
+{ 
+	false = 0, 
+	true = 1 
+};
+
+#define NULL ((void *)0)
+
+#endif /* _ARCH_RISCV_TYPES_H */
\ No newline at end of file
diff --git a/include/arch/target.h b/include/arch/target.h
new file mode 100644
index 0000000..9be52e0
--- /dev/null
+++ b/include/arch/target.h
@@ -0,0 +1,6 @@
+#ifndef _ARCH_TARGET_H
+#define _ARCH_TARGET_H
+
+#include<arch/riscv/types.h>
+
+#endif /* _ARCH_TARGET_H */
\ No newline at end of file
diff --git a/kernel/boot/kernel_enter.S b/kernel/boot/kernel_enter.S
new file mode 100644
index 0000000..e7949ef
--- /dev/null
+++ b/kernel/boot/kernel_enter.S
@@ -0,0 +1,27 @@
+
+# SBI 固件跳转位置
+# a0 = hartid
+# a1 = dtb_entry
+.section .text.boot
+.global kernel_enter
+kernel_enter:
+
+    # 为每一个核分配栈空间
+    la sp, kernel_stack
+    li t0, 4096
+    mv t1, a0
+    addi t1, t1, 1
+    mul t0, t0, t1
+    add sp, sp, t0
+
+    # 传参,永不返回
+    # a0 = hartid
+    # a1 = dtb_entry
+    tail kernel_main
+
+.section .data.kernel.stack
+.align 12
+kernel_stack:
+
+    # 每个核分配一个页大小的栈
+    .skip NCPU * 4096
\ No newline at end of file
diff --git a/kernel/boot/kernel_main.c b/kernel/boot/kernel_main.c
new file mode 100644
index 0000000..cc7a310
--- /dev/null
+++ b/kernel/boot/kernel_main.c
@@ -0,0 +1,7 @@
+// 架构信息
+#include<arch/target.h>
+
+void kernel_main()
+{
+    while(1);
+}
\ No newline at end of file
diff --git a/linker/kernel.ld b/linker/kernel.ld
new file mode 100644
index 0000000..8b16493
--- /dev/null
+++ b/linker/kernel.ld
@@ -0,0 +1,34 @@
+OUTPUT_ARCH( "riscv" ) /* 目标架构 */
+ENTRY( kernel_enter ) /* 入口 */
+
+SECTIONS
+{
+    . = 0x80200000;
+
+    .text :
+    {
+        *(.text.boot)
+        *(.text .text.*)
+    }
+
+    . = ALIGN(0x1000);
+
+    .rodata :
+    {
+        *(.rodata .rodata.*)
+    }
+
+    . = ALIGN(0x1000);
+
+    .data :
+    {
+        *(.data .data.*)
+    }
+
+    . = ALIGN(0x1000);
+
+    .bss :
+    {
+        *(.bss .bss.*)
+    }
+}
\ No newline at end of file
-- 
GitLab