Compare commits

..

5 Commits

7 changed files with 129 additions and 1 deletions

8
.clang-format Normal file
View File

@@ -0,0 +1,8 @@
BasedOnStyle: LLVM
UseTab: Always
TabWidth: 4
IndentWidth: 4
IndentCaseLabels: true
ColumnLimit: 120
PointerAlignment: Right
AllowShortFunctionsOnASingleLine: None

2
.clangd Normal file
View File

@@ -0,0 +1,2 @@
Completion:
HeaderInsertion: Never

11
.gitignore vendored
View File

@@ -1,3 +1,13 @@
/disk/*
!/disk/.gitkeep
*.map
*.tar
*.o
*.elf
*.bin
*.log
*.pcap
# ---> C
# Prerequisites
*.d
@@ -51,4 +61,3 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf

View File

@@ -1,2 +1,3 @@
# rivos
This toy os is made following the [OS in 1,000 lines](https://1000os.seiya.me/en/) project.

49
kernel.ld Normal file
View File

@@ -0,0 +1,49 @@
/*
* This is the entry point
* `boot` should be a defined label
*/
ENTRY(boot)
/* SECTIONS tells to linker how to arrange sections inside the memory */
SECTIONS {
/*
* This is the base address (start address of the kernel)
* OpenSBI takes little ram
*/
. = 0x80200000;
/* This section contains the code of the program */
.text :{
/*
* Take the .text.boot before everything
* KEEP() prevents the linker to mark it as "unused"
*/
KEEP(*(.text.boot));
/* Include everything else inside the .text section */
*(.text .text.*);
}
/*
* Contains constant read-only data
* Align the start of the section at 4 byte
*/
.rodata : ALIGN(4) {
*(.rodata .rodata.*);
}
/* Contains read/write data */
.data : ALIGN(4) {
*(.data .data.*);
}
/* Contains read/data with an initial value of zero */
.bss : ALIGN(4) {
__bss = .;
*(.bss .bss.* .sbss .sbss.*);
__bss_end = .;
}
. = ALIGN(4);
. += 128 * 1024; /* 128KB */
__stack_top = .;
}

31
run.sh Executable file
View File

@@ -0,0 +1,31 @@
#!/bin/bash
# -x -> trace: print every command
# -u -> exit if undefined variables are used
# -e -> exit if an error occurs
set -xue
QEMU=qemu-system-riscv32
# Compile
CC=clang
# -ffreestanding: do not use stdlib of the host environment
# -fuse-ld=lld: LLVM linker
# -fno-stack-protector: disable stack protection
# -Wl,-Tkernel.ld: linker script
# -Wl,-Map=kernel.map: output a map file (linker allocationr result)
CFLAGS="-std=c11 -O2 -g3 -Wall -Wextra --target=riscv32-unknown-elf -fuse-ld=lld -fno-stack-protector -ffreestanding -nostdlib"
$CC $CFLAGS -Wl,-Tkernel.ld -Wl,-Map=kernel.map -o kernel.elf \
src/kernel.c
# Run QEMU
# -machine virt: generic riscv platform
# -bios default: load the default firmware (OpenSBI)
# -nographic: disable VGA, everything on console
# -serial mon:stdio: serial port is connected to terminal's stdin/stdout
# --no-reboot: do not reboot if there's panic/shutdown/crash
$QEMU -machine virt -bios default -nographic -serial mon:stdio --no-reboot \
-kernel kernel.elf
# Press Ctrl+A and then C to enter QEMU debug console and quit with `q`

28
src/kernel.c Normal file
View File

@@ -0,0 +1,28 @@
typedef unsigned char u8;
typedef unsigned int u32;
typedef u32 size_t;
extern char __bss[], __bss_end[], __stack_top[];
void *memset(void *buf, char c, size_t n) {
u8 *p = (u8 *)buf;
while (n--) {
*p++ = c;
}
return buf;
}
void kernel_main(void) {
memset(__bss, 0, __bss_end - __bss);
for (;;)
;
}
__attribute__((section(".text.boot"))) __attribute__((naked)) void boot(void) {
__asm__ __volatile__("mv sp, %[stack_top]\n"
"j kernel_main\n"
:
: [stack_top] "r"(__stack_top));
}