Skip to content

Commit

Permalink
initial versioin
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornx committed Apr 1, 2021
1 parent 8ad78e0 commit ad15280
Show file tree
Hide file tree
Showing 277 changed files with 15,119 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.o
*.bin
*.elf

25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
BSD 2-Clause License

Copyright (c) 2021, plctlab
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SECTIONS = \
code/asm \
code/os \

.DEFAULT_GOAL := all
all :
@echo "begin compile ALL exercises for assembly samples ......................."
for dir in $(SECTIONS); do $(MAKE) -C $$dir || exit "$$?"; done
@echo "compile ALL exercises finished successfully! ......"

.PHONY : clean
clean:
for dir in $(SECTIONS); do $(MAKE) -C $$dir clean || exit "$$?"; done

.PHONY : slides
slides:
rm -f ./slides/*.pdf
soffice --headless --convert-to pdf:writer_pdf_Export --outdir ./slides ./docs/ppts/*.pptx

78 changes: 77 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,77 @@
# riscv-operating-system-mooc
**Step by step, learn to develop an operating system on RISC-V**

<!-- TOC -->

- [Introduction](#introduction)
- [Operating environment](#operating-environment)
- [Construction and usage](#construction-and-usage)
- [References](#references)

<!-- /TOC -->
# Introduction

This course is used to teach and demonstrate how to write a simple operating system kernel for the RISC-V platform from scratch. Released under the BSD 2-Clause license (For details, please read the [LICENSE file](./LICENSE) under the root directory of this repository).

# Operating environment

All demo codes have been verified under the following equipment environment:

```
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.2 LTS
Release: 20.04
Codename: focal
$ uname -r
5.8.0-45-generic
```

There may be dependent libraries that need to be installed manually. If you are prompted that other libraries and dependencies are missing during the operation, please install them by yourself according to the prompts.

```
$ sudo apt update
$ sudo apt install build-essential git gitk vim libfdt-dev libsdl2-dev
```

The experiment requires some running tools, pre-compiled binary files have been provided, the specific installation steps are described as follows:

First, create a working directory, and then enter the directory.

```
$ mkdir $HOME/ws
$ cd $HOME/ws
```

Download the development tool package `tools.tar.gz`, the download address is:<https://share.weiyun.com/nyTqAGKh>

After downloading, copy the file to `$HOME/ws` and unzip it.

```
$ tar xzf tools.tar.gz
```

Add the following path to `$HOME/.bashrc`
```
export PATH="$PATH:$HOME/ws/tools/gcc/bin:$HOME/ws/tools/qemu/bin"
```

Re-import `$HOME/.bashrc` or restart the system to make the configuration effective.

# Construction and usage

- make:Compile and build
- make run:Start qemu and run
- make debug:Start debugging
- make code:Disassemble to view binary code
- make clean:cleanup

For specific use, please refer to the Makefile under the specific sub-project.

# References

The design of this course refers to the following network resources, thank you :)

- The Adventures of OS:<https://osblog.stephenmarz.com/index.html>
- mini-riscv-os: <https://github.com/cccriscv/mini-riscv-os>
- Xv6, a simple Unix-like teaching operating system:<https://pdos.csail.mit.edu/6.828/2020/xv6.html>
77 changes: 77 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
**循序渐进,学习开发一个 RISC-V 上的操作系统**

<!-- TOC -->

- [简介](#简介)
- [运行环境](#运行环境)
- [构建和使用说明](#构建和使用说明)
- [参考文献](#参考文献)

<!-- /TOC -->
# 简介

本课程用于教学演示如何从零开始为 RISC-V 平台编写一个简单的操作系统内核。采用 BSD 2-Clause 许可证发布(具体请阅读本仓库根目录下的 [LICENSE 文件](./LICENSE))。

# 运行环境

所有演示代码在以下设备环境下验证通过:

```
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.2 LTS
Release: 20.04
Codename: focal
$ uname -r
5.8.0-45-generic
```

有可能需要手动安装的依赖库,如果运行过程中提示缺少其他的库和依赖,请按照提示自行安装。

```
$ sudo apt update
$ sudo apt install build-essential git gitk vim libfdt-dev libsdl2-dev
```

实验需要一些运行工具,已经提供预先编译好的二进制文件,具体安装步骤描述如下:

首先,创建一个工作目录,然后进入该目录。

```
$ mkdir $HOME/ws
$ cd $HOME/ws
```

下载开发工具软件包 `tools.tar.gz`,下载地址为:<https://share.weiyun.com/nyTqAGKh>

下载完毕后将该文件拷贝到 `$HOME/ws` 下并解压。

```
$ tar xzf tools.tar.gz
```

将以下路径加入 `$HOME/.bashrc`
```
export PATH="$PATH:$HOME/ws/tools/gcc/bin:$HOME/ws/tools/qemu/bin"
```

重新导入 `$HOME/.bashrc` 或者重启系统使配置生效即可。

# 构建和使用说明

- make:编译构建
- make run:启动 qemu 并运行
- make debug:启动调试
- make code:反汇编查看二进制代码
- make clean:清理

具体使用请参考具体子项目下的 Makefile 文件。

# 参考文献

本课程的设计参考了如下网络资源,在此表示感谢 :)

- The Adventures of OS:<https://osblog.stephenmarz.com/index.html>
- mini-riscv-os: <https://github.com/cccriscv/mini-riscv-os>
- Xv6, a simple Unix-like teaching operating system:<https://pdos.csail.mit.edu/6.828/2020/xv6.html>
63 changes: 63 additions & 0 deletions code/asm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
SECTIONS_Arithmetic = \
add \
sub \
addi \
subi \
neg \
nop \
mv \
lui \
li

SECTIONS_Logical = \
and \
andi \
not

SECTIONS_Shifting = \
slli \
srli \
srai

SECTIONS_Load_Store = \
lb \
lbu \
lw \
sb \
auipc \
la

SECTIONS_Branch = \
bne

SECTIONS_Jump = \
jalr \

SECTIONS_CallingConventions = \
cc_leaf \
cc_nested \

SECTIONS_others = \
asm2c \
c2asm

SECTIONS = \
_first \
$(SECTIONS_Arithmetic) \
$(SECTIONS_Logical) \
$(SECTIONS_Shifting) \
$(SECTIONS_Load_Store) \
$(SECTIONS_Branch) \
$(SECTIONS_Jump) \
$(SECTIONS_CallingConventions) \
$(SECTIONS_others)

.DEFAULT_GOAL := all
all :
@echo "begin compile ALL exercises for assembly samples ......................."
for dir in $(SECTIONS); do $(MAKE) -C $$dir || exit "$$?"; done
@echo "compile ALL exercises finished successfully! ......"

.PHONY : clean
clean:
for dir in $(SECTIONS); do $(MAKE) -C $$dir clean || exit "$$?"; done
1 change: 1 addition & 0 deletions code/asm/_first/Makefile
10 changes: 10 additions & 0 deletions code/asm/_first/test.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# First RISC-V Assemble Sample
.text # Define beginning of text section
.global _start # Define entry _start
_start: # Label, not really required
li x6, 5 # Load register x6 with the value 5
li x7, 4 # Load register x7 with the value 4
add x5, x6, x7 # Add x6 and x7 and store result in x5
stop: j stop # Infinite loop to stop execution
.end # End of file

1 change: 1 addition & 0 deletions code/asm/add/Makefile
19 changes: 19 additions & 0 deletions code/asm/add/test.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Add
# Format:
# ADD RD, RS1, RS2
# Description:
# The contents of RS1 is added to the contents of RS2 and the result is
# placed in RD.

.text # Define beginning of text section
.global _start # Define entry _start

_start: # Label, not really required
li x6, 1 # x6 = 1
li x7, 2 # x7 = 2
add x5, x6, x7 # x5 = x6 + x7

stop:
j stop # Infinite loop to stop execution

.end # End of file
1 change: 1 addition & 0 deletions code/asm/addi/Makefile
18 changes: 18 additions & 0 deletions code/asm/addi/test.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Add Immediate
# Format:
# ADDI RD, RS1, IMM
# Description:
# The immediate value (a sign-extended 12-bit value, i.e., -2,048 .. +2,047)
# is added to the contents of RS1 and the result is placed in RD.

.text # Define beginning of text section
.global _start # Define entry _start

_start: # Label, not really required
li x6, 2 # x6 = 2
addi x5, x6, 1 # x5 = x6 + 1

stop:
j stop # Infinite loop to stop execution

.end # End of file
1 change: 1 addition & 0 deletions code/asm/and/Makefile
18 changes: 18 additions & 0 deletions code/asm/and/test.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# And
# Format:
# AND RD, RS1, RS2
# The contents of RS1 is logically ANDed with the contents of RS2 and the
# result is placed in RD.

.text # Define beginning of text section
.global _start # Define entry _start

_start: # Label, not really required
li x6, 0x10 # x6 = b1000-0000
li x7, 0x01 # x7 = b0000-0001
and x5, x6, x7 # x5 = x6 & x7

stop:
j stop # Infinite loop to stop execution

.end # End of file
1 change: 1 addition & 0 deletions code/asm/andi/Makefile
18 changes: 18 additions & 0 deletions code/asm/andi/test.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# And Immediate
# Format:
# ANDI RD, RS1, IMM
# Description:
# The immediate value (a sign-extended 12-bit value, i.e., -2,048 .. +2,047)
# is logically ANDed with the contents of RD1 and the result is placed in RD.

.text # Define beginning of text section
.global _start # Define entry _start

_start: # Label, not really required
li x6, 0x10 # x6 = b1000-0000
andi x5, x6, 0x01 # x5 = x6 & 0x01

stop:
j stop # Infinite loop to stop execution

.end # End of file
7 changes: 7 additions & 0 deletions code/asm/asm2c/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
EXEC = test

SRC = $(EXEC).s $(EXEC).c

GDBINIT = ./gdbinit

include ../rule.mk
9 changes: 9 additions & 0 deletions code/asm/asm2c/gdbinit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
display/z $sp
display/z $ra
display/z $a0
display/z $a1

set disassemble-next-line on
b _start
target remote : 1234
c
Loading

0 comments on commit ad15280

Please sign in to comment.