Skip to content

Commit f43c700

Browse files
committed
Chapter 2
1 parent a35e640 commit f43c700

21 files changed

+1088
-38
lines changed

Makefile

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ all: build_kernel
44
LOG ?= error
55

66
K = os
7+
U = user
78

89
TOOLPREFIX = riscv64-unknown-elf-
910
CC = $(TOOLPREFIX)gcc
@@ -14,8 +15,6 @@ OBJDUMP = $(TOOLPREFIX)objdump
1415
PY = python3
1516
GDB = $(TOOLPREFIX)gdb
1617
CP = cp
17-
MKDIR_P = mkdir -p
18-
1918
BUILDDIR = build
2019
C_SRCS = $(wildcard $K/*.c)
2120
AS_SRCS = $(wildcard $K/*.S)
@@ -25,6 +24,10 @@ OBJS = $(C_OBJS) $(AS_OBJS)
2524

2625
HEADER_DEP = $(addsuffix .d, $(basename $(C_OBJS)))
2726

27+
ifeq (,$(findstring link_app.o,$(OBJS)))
28+
AS_OBJS += $(BUILDDIR)/$K/link_app.o
29+
endif
30+
2831
-include $(HEADER_DEP)
2932

3033
CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb
@@ -70,16 +73,23 @@ $(HEADER_DEP): $(BUILDDIR)/$K/%.d : $K/%.c
7073
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
7174
rm -f $@.$$$$
7275

76+
os/link_app.o: $K/link_app.S
77+
os/link_app.S: scripts/pack.py
78+
@$(PY) scripts/pack.py
79+
os/kernel_app.ld: scripts/kernelld.py
80+
@$(PY) scripts/kernelld.py
81+
7382
build: build/kernel
7483

75-
build/kernel: $(OBJS)
76-
$(LD) $(LDFLAGS) -T os/kernel.ld -o $(BUILDDIR)/kernel $(OBJS)
84+
build/kernel: $(OBJS) os/kernel_app.ld
85+
$(LD) $(LDFLAGS) -T os/kernel_app.ld -o $(BUILDDIR)/kernel $(OBJS)
7786
$(OBJDUMP) -S $(BUILDDIR)/kernel > $(BUILDDIR)/kernel.asm
7887
$(OBJDUMP) -t $(BUILDDIR)/kernel | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $(BUILDDIR)/kernel.sym
7988
@echo 'Build kernel done'
8089

8190
clean:
82-
rm -rf $(BUILDDIR)
91+
rm -rf $(BUILDDIR) os/kernel_app.ld os/link_app.S
92+
make -C $(U) clean
8393

8494
# BOARD
8595
BOARD ?= qemu
@@ -105,3 +115,11 @@ debug: build/kernel .gdbinit
105115
$(QEMU) $(QEMUOPTS) -S $(QEMUGDB) &
106116
sleep 1
107117
$(GDB)
118+
119+
CHAPTER ?= $(shell git rev-parse --abbrev-ref HEAD | grep -oP 'ch\K[0-9]')
120+
121+
user:
122+
make -C $(U) CHAPTER=$(CHAPTER) BASE=$(BASE)
123+
124+
test: user run
125+

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# uCore-Tutorial-Code
2-
test-20:34
2+
33
Course project for THU-OS.
44

55
对标 [rCore-Tutorial-v3](https://github.com/rcore-os/rCore-Tutorial-v3/) 的 C 版本代码。

os/const.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef CONST_H
2+
#define CONST_H
3+
4+
#define PAGE_SIZE (0x1000)
5+
6+
enum {
7+
STDIN = 0,
8+
STDOUT = 1,
9+
STDERR = 2,
10+
};
11+
12+
#endif // CONST_H

os/defs.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
#ifndef DEFS_H
22
#define DEFS_H
33

4+
#include "const.h"
45
#include "log.h"
56
#include "printf.h"
67
#include "riscv.h"
78
#include "sbi.h"
9+
#include "string.h"
810
#include "types.h"
911

1012
// number of elements in fixed-size array
1113
#define NELEM(x) (sizeof(x) / sizeof((x)[0]))
14+
#define MIN(a, b) (a < b ? a : b)
15+
#define MAX(a, b) (a > b ? a : b)
1216

13-
#endif // DEF_H
17+
#endif // DEF_H

os/kernelld.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import os
2+
3+
TARGET_DIR = "../user/target/"
4+
5+
if __name__ == '__main__':
6+
f = open("kernel_app.ld", mode="w")
7+
apps = os.listdir(TARGET_DIR)
8+
f.write(
9+
'''OUTPUT_ARCH(riscv)
10+
ENTRY(_entry)
11+
BASE_ADDRESS = 0x80200000;
12+
13+
SECTIONS
14+
{
15+
. = BASE_ADDRESS;
16+
skernel = .;
17+
18+
s_text = .;
19+
.text : {
20+
*(.text.entry)
21+
*(.text .text.*)
22+
. = ALIGN(0x1000);
23+
*(trampsec)
24+
. = ALIGN(0x1000);
25+
}
26+
27+
. = ALIGN(4K);
28+
e_text = .;
29+
s_rodata = .;
30+
.rodata : {
31+
*(.rodata .rodata.*)
32+
}
33+
34+
. = ALIGN(4K);
35+
e_rodata = .;
36+
s_data = .;
37+
.data : {
38+
*(.data)
39+
''')
40+
for (idx, _) in enumerate(apps):
41+
f.write(' . = ALIGN(0x1000);\n')
42+
f.write(' *(.data.app{})\n'.format(idx))
43+
f.write(
44+
'''
45+
*(.data.*)
46+
*(.sdata .sdata.*)
47+
}
48+
49+
. = ALIGN(4K);
50+
e_data = .;
51+
.bss : {
52+
*(.bss.stack)
53+
s_bss = .;
54+
*(.bss .bss.*)
55+
*(.sbss .sbss.*)
56+
}
57+
58+
. = ALIGN(4K);
59+
e_bss = .;
60+
ekernel = .;
61+
62+
/DISCARD/ : {
63+
*(.eh_frame)
64+
}
65+
}
66+
''')
67+
f.close()
68+

os/loader.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "loader.h"
2+
#include "defs.h"
3+
#include "trap.h"
4+
5+
static int app_cur, app_num;
6+
static uint64 *app_info_ptr;
7+
extern char _app_num[], userret[], boot_stack_top[], ekernel[];
8+
9+
void loader_init()
10+
{
11+
if ((uint64)ekernel >= BASE_ADDRESS) {
12+
panic("kernel too large...\n");
13+
}
14+
app_info_ptr = (uint64 *)_app_num;
15+
app_cur = -1;
16+
app_num = *app_info_ptr;
17+
}
18+
19+
__attribute__((aligned(4096))) char user_stack[USER_STACK_SIZE];
20+
__attribute__((aligned(4096))) char trap_page[TRAP_PAGE_SIZE];
21+
22+
int load_app(uint64 *info)
23+
{
24+
uint64 start = info[0], end = info[1], length = end - start;
25+
memset((void *)BASE_ADDRESS, 0, MAX_APP_SIZE);
26+
memmove((void *)BASE_ADDRESS, (void *)start, length);
27+
return length;
28+
}
29+
30+
int run_next_app()
31+
{
32+
struct trapframe *trapframe = (struct trapframe *)trap_page;
33+
app_cur++;
34+
app_info_ptr++;
35+
if (app_cur >= app_num) {
36+
return -1;
37+
}
38+
infof("load and run app %d", app_cur);
39+
uint64 length = load_app(app_info_ptr);
40+
debugf("bin range = [%p, %p)", *app_info_ptr, *app_info_ptr + length);
41+
memset(trapframe, 0, 4096);
42+
trapframe->epc = BASE_ADDRESS;
43+
trapframe->sp = (uint64)user_stack + USER_STACK_SIZE;
44+
usertrapret(trapframe, (uint64)boot_stack_top);
45+
return 0;
46+
}

os/loader.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef BATCH_H
2+
#define BATCH_H
3+
4+
#include "const.h"
5+
#include "types.h"
6+
7+
void loader_init();
8+
int run_next_app();
9+
10+
#define BASE_ADDRESS (0x80400000)
11+
#define MAX_APP_SIZE (0x20000)
12+
#define USER_STACK_SIZE PAGE_SIZE
13+
#define TRAP_PAGE_SIZE PAGE_SIZE
14+
15+
#endif // BATCH_H

os/log.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
extern void printf(char *, ...);
55
extern int threadid();
6-
extern void shutdown();
6+
extern void dummy(int, ...);
77

88
#if defined(LOG_LEVEL_ERROR)
99

@@ -61,7 +61,7 @@ enum LOG_COLOR {
6161
##__VA_ARGS__); \
6262
} while (0)
6363
#else
64-
#define errorf(fmt, ...)
64+
#define errorf(fmt, ...) dummy(0, ##__VA_ARGS__)
6565
#endif // USE_LOG_ERROR
6666

6767
#if defined(USE_LOG_WARN)
@@ -72,7 +72,7 @@ enum LOG_COLOR {
7272
##__VA_ARGS__); \
7373
} while (0)
7474
#else
75-
#define warnf(fmt, ...)
75+
#define warnf(fmt, ...) dummy(0, ##__VA_ARGS__)
7676
#endif // USE_LOG_WARN
7777

7878
#if defined(USE_LOG_INFO)
@@ -83,7 +83,7 @@ enum LOG_COLOR {
8383
##__VA_ARGS__); \
8484
} while (0)
8585
#else
86-
#define infof(fmt, ...)
86+
#define infof(fmt, ...) dummy(0, ##__VA_ARGS__)
8787
#endif // USE_LOG_INFO
8888

8989
#if defined(USE_LOG_DEBUG)
@@ -94,7 +94,7 @@ enum LOG_COLOR {
9494
##__VA_ARGS__); \
9595
} while (0)
9696
#else
97-
#define debugf(fmt, ...)
97+
#define debugf(fmt, ...) dummy(0, ##__VA_ARGS__)
9898
#endif // USE_LOG_DEBUG
9999

100100
#if defined(USE_LOG_TRACE)
@@ -105,16 +105,14 @@ enum LOG_COLOR {
105105
##__VA_ARGS__); \
106106
} while (0)
107107
#else
108-
#define tracef(fmt, ...)
108+
#define tracef(fmt, ...) dummy(0, ##__VA_ARGS__)
109109
#endif // USE_LOG_TRACE
110110

111111
#define panic(fmt, ...) \
112112
do { \
113113
int tid = threadid(); \
114114
printf("\x1b[%dm[%s %d] %s:%d: " fmt "\x1b[0m\n", RED, \
115115
"PANIC", tid, __FILE__, __LINE__, ##__VA_ARGS__); \
116-
shutdown(); \
117-
\
118116
} while (0)
119117

120118
#endif //! LOG_H

os/main.c

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
#include "console.h"
22
#include "defs.h"
3-
4-
extern char s_text[];
5-
extern char e_text[];
6-
extern char s_rodata[];
7-
extern char e_rodata[];
8-
extern char s_data[];
9-
extern char e_data[];
10-
extern char s_bss[];
11-
extern char e_bss[];
3+
#include "loader.h"
4+
#include "trap.h"
125

136
int threadid()
147
{
@@ -17,24 +10,16 @@ int threadid()
1710

1811
void clean_bss()
1912
{
20-
char *p;
21-
for (p = s_bss; p < e_bss; ++p)
22-
*p = 0;
13+
extern char s_bss[];
14+
extern char e_bss[];
15+
memset(s_bss, 0, e_bss - s_bss);
2316
}
2417

2518
void main()
2619
{
2720
clean_bss();
28-
console_init();
29-
printf("\n");
3021
printf("hello wrold!\n");
31-
errorf("stext: %p", s_text);
32-
warnf("etext: %p", e_text);
33-
infof("sroda: %p", s_rodata);
34-
debugf("eroda: %p", e_rodata);
35-
debugf("sdata: %p", s_data);
36-
infof("edata: %p", e_data);
37-
warnf("sbss : %p", s_bss);
38-
errorf("ebss : %p", e_bss);
39-
panic("ALL DONE");
22+
trap_init();
23+
loader_init();
24+
run_next_app();
4025
}

os/pack.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
3+
TARGET_DIR = "../user/target"
4+
5+
if __name__ == '__main__':
6+
f = open("link_app.S", mode="w")
7+
apps = os.listdir(TARGET_DIR)
8+
apps.sort()
9+
f.write(
10+
''' .align 4
11+
.section .data
12+
.global _app_num
13+
_app_num:
14+
.quad {}
15+
'''.format(len(apps))
16+
)
17+
18+
for (idx, _) in enumerate(apps):
19+
f.write(' .quad app_{}_start\n'.format(idx))
20+
f.write(' .quad app_{}_end\n'.format(len(apps) - 1))
21+
22+
f.write(
23+
'''
24+
.global _app_names
25+
_app_names:
26+
''');
27+
28+
for app in apps:
29+
app = app[:app.find('.')]
30+
f.write(" .string \"" + app + "\"\n")
31+
32+
for (idx, app) in enumerate(apps):
33+
f.write(
34+
'''
35+
.section .data.app{0}
36+
.global app_{0}_start
37+
app_{0}_start:
38+
.incbin "{1}"
39+
'''.format(idx, TARGET_DIR + app)
40+
)
41+
f.write('app_{}_end:\n\n'.format(len(apps) - 1))
42+
f.close()

0 commit comments

Comments
 (0)