-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit da40fff
Showing
14 changed files
with
1,133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
target/ | ||
|
||
.gdb_history |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# syntax=docker/dockerfile:1 | ||
FROM ubuntu:20.04 | ||
|
||
ARG QEMU_VERSION=7.0.0 | ||
ARG HOME=/root | ||
|
||
# 0. Install general tools | ||
ARG DEBIAN_FRONTEND=noninteractive | ||
RUN apt-get update && \ | ||
apt-get install -y \ | ||
curl \ | ||
git \ | ||
python3 \ | ||
wget | ||
|
||
# 1. Set up QEMU RISC-V | ||
# - https://learningos.github.io/rust-based-os-comp2022/0setup-devel-env.html#qemu | ||
# - https://www.qemu.org/download/ | ||
# - https://wiki.qemu.org/Documentation/Platforms/RISCV | ||
# - https://risc-v-getting-started-guide.readthedocs.io/en/latest/linux-qemu.html | ||
|
||
# 1.1. Download source | ||
WORKDIR ${HOME} | ||
RUN wget https://download.qemu.org/qemu-${QEMU_VERSION}.tar.xz && \ | ||
tar xvJf qemu-${QEMU_VERSION}.tar.xz | ||
|
||
# 1.2. Install dependencies | ||
# - https://risc-v-getting-started-guide.readthedocs.io/en/latest/linux-qemu.html#prerequisites | ||
RUN apt-get install -y \ | ||
autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev \ | ||
gawk build-essential bison flex texinfo gperf libtool patchutils bc \ | ||
zlib1g-dev libexpat-dev git \ | ||
ninja-build pkg-config libglib2.0-dev libpixman-1-dev libsdl2-dev | ||
|
||
# 1.3. Build and install from source | ||
WORKDIR ${HOME}/qemu-${QEMU_VERSION} | ||
RUN ./configure --target-list=riscv64-softmmu,riscv64-linux-user && \ | ||
make -j$(nproc) && \ | ||
make install | ||
|
||
# 1.4. Clean up | ||
WORKDIR ${HOME} | ||
RUN rm -rf qemu-${QEMU_VERSION} qemu-${QEMU_VERSION}.tar.xz | ||
|
||
# 1.5. Sanity checking | ||
RUN qemu-system-riscv64 --version && \ | ||
qemu-riscv64 --version | ||
|
||
# 2. Set up Rust | ||
# - https://learningos.github.io/rust-based-os-comp2022/0setup-devel-env.html#qemu | ||
# - https://www.rust-lang.org/tools/install | ||
# - https://github.com/rust-lang/docker-rust/blob/master/Dockerfile-debian.template | ||
|
||
# 2.1. Install | ||
ENV RUSTUP_HOME=/usr/local/rustup \ | ||
CARGO_HOME=/usr/local/cargo \ | ||
PATH=/usr/local/cargo/bin:$PATH \ | ||
RUST_VERSION=nightly-2024-02-01 \ | ||
PROFILE=default | ||
RUN set -eux; \ | ||
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs -o rustup-init; \ | ||
chmod +x rustup-init; \ | ||
./rustup-init -y --no-modify-path --profile $PROFILE --default-toolchain $RUST_VERSION; \ | ||
rm rustup-init; \ | ||
chmod -R a+w $RUSTUP_HOME $CARGO_HOME; | ||
|
||
# 2.2. Sanity checking | ||
RUN rustup --version && \ | ||
cargo --version && \ | ||
rustc --version | ||
|
||
# 3. Build env for labs | ||
RUN rustup target add riscv64gc-unknown-none-elf && \ | ||
rustup component add rust-src && \ | ||
rustup component add llvm-tools && \ | ||
cargo install cargo-binutils | ||
|
||
# Ready to go | ||
WORKDIR ${HOME} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
DOCKER_NAME ?= my-os | ||
.PHONY: docker build_docker | ||
|
||
docker: | ||
docker run --rm -it --network="host" -v ${PWD}:/mnt -w /mnt ${DOCKER_NAME} bash | ||
|
||
build_docker: | ||
docker build -t ${DOCKER_NAME} . | ||
|
||
run: | ||
cd kernel && make run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[build] | ||
target = "riscv64gc-unknown-none-elf" | ||
|
||
[target.riscv64gc-unknown-none-elf] | ||
rustflags = [ | ||
"-Clink-arg=-Tsrc/linker.ld", | ||
"-Cforce-frame-pointers=yes" | ||
] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "kernel" | ||
version = "0.1.0" | ||
authors = ["ChenRuiwei <[email protected]>"] | ||
edition = "2021" | ||
|
||
[dependencies] | ||
log = "0.4" | ||
riscv-rt = "0.12.2" | ||
sbi-rt = { version = "0.0.3", features = ["legacy"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Building | ||
PACKAGE_NAME=kernel | ||
TARGET := riscv64gc-unknown-none-elf | ||
MODE := debug | ||
KERNEL_ELF := target/$(TARGET)/$(MODE)/$(PACKAGE_NAME) | ||
DISASM_ARGS_TMP := target/$(TARGET)/$(MODE)/asm | ||
|
||
# Building mode argument | ||
ifeq ($(MODE), release) | ||
MODE_ARG := --release | ||
endif | ||
|
||
# Board | ||
BOARD := qemu | ||
BOOTLOADER := default | ||
|
||
# Tools | ||
TOOLPREFIX := riscv64-elf- | ||
QEMU := qemu-system-riscv64 | ||
GDB := $(TOOLPREFIX)gdb | ||
OBJDUMP := rust-objdump --arch-name=riscv64 | ||
OBJCOPY := rust-objcopy --binary-architecture=riscv64 | ||
|
||
DISASM_ARGS := -x | ||
|
||
QEMU_ARGS := -machine virt \ | ||
-nographic \ | ||
-bios $(BOOTLOADER) \ | ||
-kernel $(KERNEL_ELF) | ||
|
||
$(KERNEL_ELF): build | ||
|
||
PHONY := all | ||
all: build | ||
|
||
PHONY += env | ||
env: | ||
@(cargo install --list | grep "cargo-binutils" > /dev/null 2>&1) || cargo install cargo-binutils | ||
|
||
PHONY += build | ||
build: env | ||
@echo Platform: $(BOARD) | ||
@cp src/linker-$(BOARD).ld src/linker.ld | ||
@cargo build $(MODE_ARG) | ||
@rm src/linker.ld | ||
|
||
PHONY += run | ||
run: build | ||
@$(QEMU) $(QEMU_ARGS) | ||
|
||
PHONY += clean | ||
clean: | ||
@cargo clean | ||
|
||
PHONY += disasm | ||
disasm: build | ||
@$(OBJDUMP) $(DISASM_ARGS) $(KERNEL_ELF) | less | ||
|
||
PHONY += disasm-nvim | ||
disasm-nvim: build | ||
@$(OBJDUMP) $(DISASM_ARGS) $(KERNEL_ELF) > $(DISASM_TMP) | ||
@nvim $(DISASM_TMP) | ||
@rm $(DISASM_TMP) | ||
|
||
PHONY += gdbserver | ||
gdbserver: build | ||
@$(QEMU) $(QEMU_ARGS) -s -S | ||
|
||
PHONY += gdbclient | ||
gdbclient: | ||
@$(GDB) -ex 'file $(KERNEL_ELF)' \ | ||
-ex 'set arch riscv:rv64' \ | ||
-ex 'target remote localhost:1234' | ||
|
||
.PHONY: $(PHONY) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use crate::sbi::console_putchar; | ||
use core::fmt::{self, Write}; | ||
|
||
struct Stdout; | ||
|
||
impl Write for Stdout { | ||
fn write_str(&mut self, s: &str) -> fmt::Result { | ||
for c in s.chars() { | ||
console_putchar(c as usize); | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn print(args: fmt::Arguments) { | ||
Stdout.write_fmt(args).unwrap(); | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! print { | ||
($fmt: literal $(, $($arg: tt)+)?) => { | ||
$crate::console::print(format_args!($fmt $(, $($arg)+)?)) | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! println { | ||
($fmt: literal $(, $($arg: tt)+)?) => { | ||
$crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
.section .text.entry | ||
.globl _start | ||
_start: | ||
la sp, boot_stack_top | ||
call rust_main | ||
|
||
.section .bss.stack | ||
.globl boot_stack_lower_bound | ||
boot_stack_lower_bound: | ||
.space 4096 * 16 | ||
.globl boot_stack_top | ||
boot_stack_top: |
Oops, something went wrong.