Skip to content

Commit 8e6553f

Browse files
authored
Merge pull request #258 from cksystemsteaching/machine
Add Bachelor's Thesis "RISC-V Bare-Metal Library Operating System for Selfie"
2 parents 04265de + 89accb2 commit 8e6553f

File tree

14 files changed

+111
-46
lines changed

14 files changed

+111
-46
lines changed

machine/Makefile

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ HOSTCC:=$(CC)
22

33
PREFIX=riscv64-linux-gnu-
44
CC=$(PREFIX)gcc
5-
LD=$(PREFIX)ld
65
AS=$(CC)
76
OBJCOPY=$(PREFIX)objcopy
87
QEMU=qemu-system-riscv64
98

109
# Our ABI, architecture and memory model must match with OpenSBI
11-
CFLAGS=-g3 -mabi=lp64 -march=rv64imafdc -mcmodel=medany -ffreestanding -nostdlib -Iinclude -Iopensbi/include -Wl,--build-id=none -Wall -Wextra -MMD -MP
10+
CFLAGS=-mabi=lp64d -march=rv64imafdc -mcmodel=medany -ffreestanding -Iinclude -Wall -Wextra -MMD -MP -O3
1211
ASFLAGS=$(CFLAGS)
13-
LDFLAGS=-r -b elf64-littleriscv -m elf64lriscv
12+
LDFLAGS=-nostdlib -lgcc -Wl,--build-id=none
1413
QEMUFLAGS=-M virt -m 128M -nographic -bios none
1514
# for debugging, add '-gdb tcp::9000 -S' to the QEMUFLAGS
1615

@@ -22,7 +21,7 @@ OPENSBI_RELEASE=v0.7
2221
OPENSBI_VERSION_FILE=opensbi/opensbi_version
2322

2423
SBI_WRAPPER_SRCS_COMMON = bootstrap.c tinycstd.c console.c filesystem.c files_package.c syscalls.c asm/crt.S diag.c sbi_ecall.c
25-
SBI_WRAPPER_SRCS_LIBRARY = bootstrap_library.c selfie.c glue_libraryos.c
24+
SBI_WRAPPER_SRCS_LIBRARY = bootstrap_library.c selfie.c glue_libraryos.c asm/mem.S
2625
SBI_WRAPPER_SRCS_KERNEL = bootstrap_kernel.c mmu.c context.c trap.c elf.c asm/trap.S
2726

2827
SBI_WRAPPER_FILES_PACKAGE = $(SELFIE_PATH)/selfie.m $(SELFIE_PATH)/selfie.c $(SELFIE_PATH)/examples/hello-world.c $(SELFIE_PATH)/hello-world.m
@@ -51,7 +50,7 @@ $(eval $(call add-all-target-aliases))
5150
all: all-selfie-elf all-selfie-opensbi-elf
5251

5352
include/asm_offsets.h: $(ASM_STRUCT_HEADERS)
54-
$(HOSTCC) -I. tools/asm_struct_macro_generator.c -o tools/asm_struct_macro_generator
53+
$(HOSTCC) -Iinclude tools/asm_struct_macro_generator.c -o tools/asm_struct_macro_generator
5554
tools/asm_struct_macro_generator > "$@"
5655

5756

@@ -87,7 +86,7 @@ opensbi:
8786

8887
.PHONY: debug
8988
debug:
90-
$(eval CFLAGS += -DDEBUG)
89+
$(eval CFLAGS += -O0 -g3 -DDEBUG)
9190
@echo "Enabled debug macro"
9291

9392

machine/asm/crt.S

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
.text
1313

1414
.section .entry, "ax", %progbits
15-
.p2align 3
15+
.p2align 2
1616
.global _start
1717
_start:
1818
/* Set up the Global Pointer */
@@ -37,7 +37,7 @@ _start_warm:
3737
csrw sip, zero
3838

3939
/* Setup exception vectors */
40-
la a3, _start_hang
40+
la a3, hang_machine
4141
csrw stvec, a3
4242

4343
/* Setup stack */
@@ -48,14 +48,14 @@ _start_warm:
4848
call bootstrap
4949

5050
/* We don't expect to reach here hence just hang */
51-
j _start_hang
51+
j hang_machine
5252

5353
.section .entry, "ax", %progbits
54-
.p2align 3
55-
.global _start_hang
56-
_start_hang:
54+
.p2align 2
55+
.global hang_machine
56+
hang_machine:
5757
wfi
58-
j _start_hang
58+
j hang_machine
5959

6060
.global initial_stack_start
6161
initial_stack_start:

machine/asm/mem.S

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// a0: address
2+
// a1: size, in bytes
3+
.global zero_mem
4+
zero_mem:
5+
// Check whether we are 8 byte aligned
6+
addi t0, zero, 8
7+
remu t1, a0, t0
8+
beqz t1, after_align_addr
9+
10+
// Align if we are not
11+
sub t1, t0, t1
12+
align_addr:
13+
beqz t1, after_align_addr
14+
sb zero, 0(a0)
15+
addi a0, a0, 1
16+
addi a1, a1, -1
17+
addi t1, t1, -1
18+
j align_addr
19+
20+
21+
after_align_addr:
22+
23+
// Now that the address is 8 byte aligned, do the actual zeroing
24+
// using decreasing granularity: sd(8) -> sw(4) -> sh(2) -> sb(1)
25+
addi t0, zero, 8
26+
27+
zero_sd_loop:
28+
beqz a1, zero_done
29+
blt a1, t0, zero_sw
30+
sd zero, 0(a0)
31+
add a0, a0, t0
32+
sub a1, a1, t0
33+
j zero_sd_loop
34+
35+
zero_sw:
36+
addi t0, zero, 4
37+
zero_sw_loop:
38+
beqz a1, zero_done
39+
blt a1, t0, zero_sh
40+
sw zero, 0(a0)
41+
add a0, a0, t0
42+
sub a1, a1, t0
43+
j zero_sw_loop
44+
45+
zero_sh:
46+
addi t0, zero, 2
47+
zero_sh_loop:
48+
beqz a1, zero_done
49+
blt a1, t0, zero_sb
50+
sh zero, 0(a0)
51+
add a0, a0, t0
52+
sub a1, a1, t0
53+
j zero_sh_loop
54+
55+
zero_sb:
56+
addi t0, zero, 1
57+
zero_sb_loop:
58+
blt a1, t0, zero_done
59+
sb zero, 0(a0)
60+
add a0, a0, t0
61+
sub a1, a1, t0
62+
j zero_sb_loop
63+
64+
zero_done:
65+
ret
66+
67+

machine/bootstrap.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "bootstrap.h"
22
#include "config.h"
33
#include "console.h"
4+
#include "diag.h"
45
#include "tinycstd.h"
56

67
int main(int argc, char** argv);
@@ -36,5 +37,8 @@ void bootstrap() {
3637
}
3738
printf(" <END>\n\n");
3839

39-
start_init_process(argc, args);
40+
uint64_t result = start_init_process(argc, args);
41+
42+
printf("\nInit process terminated with exit code %d\n", result);
43+
shutdown();
4044
}

machine/defines.mk

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ endef
4444
define add-target
4545
$(call generate-target-vars,$(1),$(2))
4646
$(call generate-target-obj-rules,$(1),$(2))
47-
$(call generate-target-combine-rule,$(1),$(2))
4847
$(call generate-target-elf-bin,$(1),$(2))
4948
$(call generate-target-sbi-elf,$(1),$(2))
5049
endef
@@ -127,19 +126,6 @@ $$(TARGET_$(1)_$(2)_DIR)/selfie.o: $$(SELFIE_PATH)/selfie.c
127126

128127
endef
129128

130-
###############################################################################
131-
# $(call generate-target-combine-rule,board,profile)
132-
#
133-
# - board : The board
134-
# - profile : The build profile
135-
#
136-
define generate-target-combine-rule
137-
138-
$$(TARGET_$(1)_$(2)_DIR)/selfie_bare_metal.o: $$(TARGET_$(1)_$(2)_OBJS)
139-
$$(LD) $$(LDFLAGS) $$^ -o $$@
140-
141-
endef
142-
143129
###############################################################################
144130
# $(call generate-target-elf-bin,board,profile)
145131
#
@@ -152,8 +138,8 @@ $$(TARGET_$(1)_$(2)_DIR)/selfie.ld:
152138
SBI_START=$$(BOARD_$(1)_PAYLOAD_START) PAYLOAD_OFFSET=$$(BOARD_$(1)_PAYLOAD_OFFSET) envsubst '$$$$SBI_START$$$$PAYLOAD_OFFSET' <payload_template.ld >$$@
153139

154140

155-
$$(TARGET_$(1)_$(2)_DIR)/selfie.elf: $$(TARGET_$(1)_$(2)_DIR)/selfie_bare_metal.o | $$(TARGET_$(1)_$(2)_DIR)/selfie.ld
156-
$$(CC) $$(CFLAGS) -static-libgcc -lgcc $$^ -o $$@ -T $$(TARGET_$(1)_$(2)_DIR)/selfie.ld
141+
$$(TARGET_$(1)_$(2)_DIR)/selfie.elf: $$(TARGET_$(1)_$(2)_OBJS) | $$(TARGET_$(1)_$(2)_DIR)/selfie.ld
142+
$$(CC) $$(CFLAGS) $$(LDFLAGS) $$^ -o $$@ -T $$(TARGET_$(1)_$(2)_DIR)/selfie.ld
157143

158144
$$(TARGET_$(1)_$(2)_DIR)/selfie.bin: $$(TARGET_$(1)_$(2)_DIR)/selfie.elf
159145
$$(OBJCOPY) -S -O binary $$< $$@

machine/diag.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ void panic(const char* diagnostic_message, ...) {
2121

2222
void shutdown() {
2323
sbi_ecall_sbi_shutdown();
24+
25+
printf("shutdown failed - hanging machine...\n");
26+
hang_machine();
2427
}

machine/filesystem.mk

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ files_package.c: $(SBI_WRAPPER_FILES_PACKAGE)
1313
done;
1414

1515
@# Create a mapping to all packaged files
16-
@echo "static const KFILE file_defs[] = {" >>$@;
16+
@echo "const KFILE files[] = {" >>$@;
1717
@for file in $^;\
1818
do \
1919
BASENAME=`basename $$file` ;\
@@ -23,7 +23,6 @@ files_package.c: $(SBI_WRAPPER_FILES_PACKAGE)
2323
done;
2424
@echo " {\"\", (const char*) NULL, 0}," >>$@;
2525
@echo "};" >>$@;
26-
@echo "const KFILE* files = file_defs;" >>$@;
2726

2827
$(SELFIE_PATH)/selfie.m: $(SELFIE_PATH)/selfie.c
2928
$(MAKE) -C$(SELFIE_PATH) selfie.m

machine/glue_libraryos.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ uint64_t open(char* filename, uint64_t flags, uint64_t mode) {
2424
}
2525

2626

27-
27+
extern void zero_mem(uint64_t* addr, uint64_t size);
2828
void* malloc(unsigned long long size) {
2929
void* return_ptr;
3030

@@ -35,6 +35,8 @@ void* malloc(unsigned long long size) {
3535
printf("-- malloc: allocated 0x%x bytes at addr %p-%p\n", size, return_ptr, heap_head);
3636
#endif /* DEBUG */
3737

38+
zero_mem(return_ptr, size);
39+
3840
return return_ptr;
3941
}
4042

machine/include/diag.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#include <stdarg.h>
55

66
void panic(const char* diagnostic_message, ...);
7-
void shutdown();
7+
void shutdown() __attribute__((noreturn));
8+
9+
extern void hang_machine() __attribute__((noreturn));
810

911
#ifdef DEBUG
1012
#define assert(x) if (!(x)) panic("Assertion failed: " __FILE__ ":%u: " #x, __LINE__)

machine/include/filesystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ typedef struct FILEDESC {
1616
size_t pos;
1717
} FILEDESC;
1818

19-
extern const KFILE* files;
19+
extern const KFILE files[];
2020

2121
const KFILE* find_file(const char* filename);
2222

0 commit comments

Comments
 (0)