Skip to content

Commit a35e640

Browse files
committed
Chapter 1
0 parents  commit a35e640

22 files changed

+2146
-0
lines changed

.clang-format

Lines changed: 561 additions & 0 deletions
Large diffs are not rendered by default.

.gdbinit

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
set confirm off
2+
set architecture riscv:rv64
3+
target remote 127.0.0.1:15234
4+
symbol-file build/kernel
5+
display/12i $pc-8
6+
set riscv use-compressed-breakpoints yes
7+
break *0x1000

.github/workflows/gitlab-mirror.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Mirror and run GitLab CI
2+
3+
on:
4+
push:
5+
branches:
6+
- 'ch[0-9]'
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v1
13+
- name: Mirror + trigger CI
14+
uses: Gallium70/gitlab-mirror-and-ci-action@master
15+
with:
16+
args: "https://git.tsinghua.edu.cn/os-lab/2023s/public/ucore-tutorial-code-2023s"
17+
env:
18+
GITLAB_HOSTNAME: "git.tsinghua.edu.cn"
19+
GITLAB_PROJECT_ID: "20789"
20+
GITLAB_PROJECT_NAME: "ucore-tutorial-code-2023s"
21+
GITLAB_PROJECT_TOKEN: ${{secrets.GITLAB_PROJECT_TOKEN}}
22+
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.DS_Store
2+
.vscode
3+
.idea
4+
build
5+
target
6+
/user
7+
link_app.S
8+
kernel_app.ld
9+
*.o
10+
*.d
11+
*.asm
12+
*.sym

.gitlab-ci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
default:
2+
image: duskmoon/dev-env:ucore-ci
3+
4+
stages:
5+
- test
6+
7+
test-code-job:
8+
stage: test
9+
script:
10+
- git clone https://token:${UCORE_CHECKER_REPO_READ_TOKEN_2023S}@git.tsinghua.edu.cn/os-lab/2023s/ta/ucore-tutorial-checker-2023s.git ucore-tutorial-ci
11+
- git clone https://token:${UCORE_TEST_REPO_READ_TOKEN_2023S}@git.tsinghua.edu.cn/os-lab/2023s/public/ucore-tutorial-test-2023s.git ucore-tutorial-ci/workplace/user
12+
- cd ucore-tutorial-ci && make test CHAPTER=`echo $CI_COMMIT_REF_NAME | grep -oP 'ch\K[0-9]'`

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

Makefile

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
.PHONY: clean build user
2+
all: build_kernel
3+
4+
LOG ?= error
5+
6+
K = os
7+
8+
TOOLPREFIX = riscv64-unknown-elf-
9+
CC = $(TOOLPREFIX)gcc
10+
AS = $(TOOLPREFIX)gcc
11+
LD = $(TOOLPREFIX)ld
12+
OBJCOPY = $(TOOLPREFIX)objcopy
13+
OBJDUMP = $(TOOLPREFIX)objdump
14+
PY = python3
15+
GDB = $(TOOLPREFIX)gdb
16+
CP = cp
17+
MKDIR_P = mkdir -p
18+
19+
BUILDDIR = build
20+
C_SRCS = $(wildcard $K/*.c)
21+
AS_SRCS = $(wildcard $K/*.S)
22+
C_OBJS = $(addprefix $(BUILDDIR)/, $(addsuffix .o, $(basename $(C_SRCS))))
23+
AS_OBJS = $(addprefix $(BUILDDIR)/, $(addsuffix .o, $(basename $(AS_SRCS))))
24+
OBJS = $(C_OBJS) $(AS_OBJS)
25+
26+
HEADER_DEP = $(addsuffix .d, $(basename $(C_OBJS)))
27+
28+
-include $(HEADER_DEP)
29+
30+
CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb
31+
CFLAGS += -MD
32+
CFLAGS += -mcmodel=medany
33+
CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax
34+
CFLAGS += -I$K
35+
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
36+
37+
ifeq ($(LOG), error)
38+
CFLAGS += -D LOG_LEVEL_ERROR
39+
else ifeq ($(LOG), warn)
40+
CFLAGS += -D LOG_LEVEL_WARN
41+
else ifeq ($(LOG), info)
42+
CFLAGS += -D LOG_LEVEL_INFO
43+
else ifeq ($(LOG), debug)
44+
CFLAGS += -D LOG_LEVEL_DEBUG
45+
else ifeq ($(LOG), trace)
46+
CFLAGS += -D LOG_LEVEL_TRACE
47+
endif
48+
49+
# Disable PIE when possible (for Ubuntu 16.10 toolchain)
50+
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),)
51+
CFLAGS += -fno-pie -no-pie
52+
endif
53+
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]nopie'),)
54+
CFLAGS += -fno-pie -nopie
55+
endif
56+
57+
LDFLAGS = -z max-page-size=4096
58+
59+
$(AS_OBJS): $(BUILDDIR)/$K/%.o : $K/%.S
60+
@mkdir -p $(@D)
61+
$(CC) $(CFLAGS) -c $< -o $@
62+
63+
$(C_OBJS): $(BUILDDIR)/$K/%.o : $K/%.c $(BUILDDIR)/$K/%.d
64+
@mkdir -p $(@D)
65+
$(CC) $(CFLAGS) -c $< -o $@
66+
67+
$(HEADER_DEP): $(BUILDDIR)/$K/%.d : $K/%.c
68+
@mkdir -p $(@D)
69+
@set -e; rm -f $@; $(CC) -MM $< $(INCLUDEFLAGS) > $@.$$$$; \
70+
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
71+
rm -f $@.$$$$
72+
73+
build: build/kernel
74+
75+
build/kernel: $(OBJS)
76+
$(LD) $(LDFLAGS) -T os/kernel.ld -o $(BUILDDIR)/kernel $(OBJS)
77+
$(OBJDUMP) -S $(BUILDDIR)/kernel > $(BUILDDIR)/kernel.asm
78+
$(OBJDUMP) -t $(BUILDDIR)/kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $(BUILDDIR)/kernel.sym
79+
@echo 'Build kernel done'
80+
81+
clean:
82+
rm -rf $(BUILDDIR)
83+
84+
# BOARD
85+
BOARD ?= qemu
86+
SBI ?= rustsbi
87+
BOOTLOADER := ./bootloader/rustsbi-qemu.bin
88+
89+
QEMU = qemu-system-riscv64
90+
QEMUOPTS = \
91+
-nographic \
92+
-machine virt \
93+
-bios $(BOOTLOADER) \
94+
-kernel build/kernel \
95+
96+
run: build/kernel
97+
$(QEMU) $(QEMUOPTS)
98+
99+
# QEMU's gdb stub command line changed in 0.11
100+
QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \
101+
then echo "-gdb tcp::15234"; \
102+
else echo "-s -p 15234"; fi)
103+
104+
debug: build/kernel .gdbinit
105+
$(QEMU) $(QEMUOPTS) -S $(QEMUGDB) &
106+
sleep 1
107+
$(GDB)

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# uCore-Tutorial-Code
2+
test-20:34
3+
Course project for THU-OS.
4+
5+
对标 [rCore-Tutorial-v3](https://github.com/rcore-os/rCore-Tutorial-v3/) 的 C 版本代码。
6+
7+
主要参考 [xv6-riscv](https://github.com/mit-pdos/xv6-riscv), [uCore-SMP](https://github.com/TianhuaTao/uCore-SMP)
8+
9+
实验 lab1-lab5 基准代码分别位于 ch3-ch8 分支下。
10+
11+
注:为了兼容清华 Git 的需求、避免同学在主分支写代码、明确主分支的功能性,特意单独建了仅包含 README 与 LICENSE 的 master 分支,完成课程实验时请在 clone 仓库后先 push master 分支到清华 Git,然后切到自己开发所需的分支进行后续操作。
12+
13+
## 本地开发测试
14+
15+
在本地开发并测试时,需要拉取 uCore-Tutorial-Test-2022A 到 `user` 文件夹。你可以根据网络情况和个人偏好选择下列一项执行:
16+
17+
```bash
18+
# 清华 git 使用 https
19+
git clone https://git.tsinghua.edu.cn/os-lab/public/ucore-tutorial-test-2022a.git user
20+
# 清华 git 使用 ssh
21+
git clone [email protected]:os-lab/public/ucore-tutorial-test-2022a.git user
22+
# GitHub 使用 https
23+
git clone https://github.com/LearningOS/uCore-Tutorial-Test-2022A.git user
24+
# GitHub 使用 ssh
25+
git clone [email protected]:LearningOS/uCore-Tutorial-Test-2022A.git user
26+
```
27+
28+
注意:`user` 已添加至 `.gitignore`,你无需将其提交,ci 也不会使用它

bootloader/rustsbi-qemu.bin

37.7 KB
Binary file not shown.

os/console.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "console.h"
2+
#include "sbi.h"
3+
4+
void consputc(int c)
5+
{
6+
console_putchar(c);
7+
}
8+
9+
void console_init()
10+
{
11+
// DO NOTHING
12+
}

os/console.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef CONSOLE_H
2+
#define CONSOLE_H
3+
4+
void consputc(int);
5+
void console_init();
6+
7+
#endif // CONSOLE_H

os/defs.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef DEFS_H
2+
#define DEFS_H
3+
4+
#include "log.h"
5+
#include "printf.h"
6+
#include "riscv.h"
7+
#include "sbi.h"
8+
#include "types.h"
9+
10+
// number of elements in fixed-size array
11+
#define NELEM(x) (sizeof(x) / sizeof((x)[0]))
12+
13+
#endif // DEF_H

os/entry.S

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.section .text.entry
2+
.globl _entry
3+
_entry:
4+
la sp, boot_stack_top
5+
call main
6+
7+
.section .bss.stack
8+
.globl boot_stack
9+
boot_stack:
10+
.space 4096 * 16
11+
.globl boot_stack_top
12+
boot_stack_top:

os/kernel.ld

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
OUTPUT_ARCH(riscv)
2+
ENTRY(_entry)
3+
BASE_ADDRESS = 0x80200000;
4+
5+
SECTIONS
6+
{
7+
. = BASE_ADDRESS;
8+
skernel = .;
9+
10+
s_text = .;
11+
.text : {
12+
*(.text.entry)
13+
*(.text .text.*)
14+
. = ALIGN(0x1000);
15+
*(trampsec)
16+
. = ALIGN(0x1000);
17+
}
18+
19+
. = ALIGN(4K);
20+
e_text = .;
21+
s_rodata = .;
22+
.rodata : {
23+
*(.rodata .rodata.*)
24+
}
25+
26+
. = ALIGN(4K);
27+
e_rodata = .;
28+
s_data = .;
29+
.data : {
30+
*(.data.apps)
31+
*(.data .data.*)
32+
*(.sdata .sdata.*)
33+
}
34+
35+
. = ALIGN(4K);
36+
e_data = .;
37+
.bss : {
38+
*(.bss.stack)
39+
s_bss = .;
40+
*(.bss .bss.*)
41+
*(.sbss .sbss.*)
42+
}
43+
44+
. = ALIGN(4K);
45+
e_bss = .;
46+
ekernel = .;
47+
48+
/DISCARD/ : {
49+
*(.eh_frame)
50+
}
51+
}

0 commit comments

Comments
 (0)