Skip to content

Commit 83a3fc2

Browse files
author
Artem Lisnevskiy
committed
[Init]
0 parents  commit 83a3fc2

File tree

7 files changed

+228
-0
lines changed

7 files changed

+228
-0
lines changed

.gdbinit.tmpl-riscv

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
set confirm off
2+
set architecture riscv:rv64
3+
target remote 127.0.0.1:1234
4+
symbol-file kernel_img
5+
set disassemble-next-line auto
6+
set riscv use-compressed-breakpoints yes

.gitirnore

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
*~
2+
_*
3+
*.o
4+
*.d
5+
*.asm
6+
*.sym
7+
*.img
8+
vectors.S
9+
bootblock
10+
entryother
11+
initcode
12+
initcode.out
13+
kernelmemfs
14+
mkfs
15+
kernel/kernel
16+
user/usys.S
17+
debug/
18+
target/
19+
Cargo.lock
20+
**/*.rs.bk
21+
*.pdb

Makefile

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# riscv64-unknown-elf- or riscv64-linux-gnu-
2+
# perhaps in /opt/riscv/bin
3+
TOOLPREFIX = riscv64-unknown-elf-
4+
5+
QEMU = qemu-system-riscv64
6+
7+
CC = $(TOOLPREFIX)gcc
8+
AS = $(TOOLPREFIX)as
9+
LD = $(TOOLPREFIX)ld
10+
OBJCOPY = $(TOOLPREFIX)objcopy
11+
OBJDUMP = $(TOOLPREFIX)objdump
12+
13+
CFLAGS = -Wall -Werror -O -fno-omit-frame-pointer -ggdb -gdwarf-2
14+
CFLAGS += -MD
15+
CFLAGS += -Wl,--gc-sections -mcmodel=medany -march=rv64gc
16+
CFLAGS += -Wl,--no-warn-rwx-segments
17+
CFLAGS += -ffreestanding -nostartfiles -nostdlib -nodefaultlibs -fno-common -mno-relax
18+
CFLAGS += -I.
19+
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
20+
21+
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]no-pie'),)
22+
CFLAGS += -fno-pie -no-pie
23+
endif
24+
ifneq ($(shell $(CC) -dumpspecs 2>/dev/null | grep -e '[^f]nopie'),)
25+
CFLAGS += -fno-pie -nopie
26+
endif
27+
28+
LDFLAGS = -z max-page-size=4096
29+
30+
OBJS = \
31+
entry.o \
32+
start.o \
33+
main.o
34+
35+
kernel_img: $(OBJS) kernel.ld
36+
$(LD) $(LDFLAGS) -T kernel.ld -o kernel_img $(OBJS)
37+
$(OBJDUMP) -S kernel_img > kernel.asm
38+
$(OBJDUMP) -t kernel_img | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > kernel.sym
39+
40+
tags: $(OBJS) _init
41+
etags *.S *.c
42+
43+
# Prevent deletion of intermediate files, e.g. cat.o, after first build, so
44+
# that disk image changes after first build are persistent until clean. More
45+
# details:
46+
# http://www.gnu.org/software/make/manual/html_node/Chained-Rules.html
47+
.PRECIOUS: %.o
48+
49+
-include *.d
50+
51+
clean:
52+
rm -f *.tex *.dvi *.idx *.aux *.log *.ind *.ilg \
53+
*.o */*.o *.d */*.d *.asm */*.asm *.sym */*.sym \
54+
kernel_img
55+
56+
# try to generate a unique GDB port
57+
GDBPORT = 1234
58+
59+
# QEMU's gdb stub command line changed in 0.11
60+
QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \
61+
then echo "-gdb tcp::$(GDBPORT)"; \
62+
else echo "-s -p $(GDBPORT)"; fi)
63+
64+
QEMUOPTS = \
65+
-machine virt \
66+
-bios none \
67+
-kernel kernel_img \
68+
-m 128M \
69+
-cpu rv64 \
70+
-smp 1 \
71+
-serial mon:stdio \
72+
-device virtio-rng-device \
73+
-device virtio-gpu-device \
74+
-device virtio-net-device \
75+
-device virtio-tablet-device \
76+
-device virtio-keyboard-device
77+
78+
qemu: kernel_img
79+
$(QEMU) $(QEMUOPTS)
80+
81+
qemu-gdb: kernel_img .gdbinit.tmpl-riscv
82+
@echo "*** Now run 'gdb' in another window." 1>&2
83+
$(QEMU) $(QEMUOPTS) -S $(QEMUGDB)
84+

entry.s

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
.option norvc
2+
.section .text._entry
3+
.type _entry, @function
4+
.global _entry
5+
_entry:
6+
7+
/* Отключение всех прерываний */
8+
csrw sie, zero
9+
csrw sscratch, zero
10+
mv tp, zero
11+
12+
csrw mideleg, zero
13+
csrw medeleg, zero
14+
csrw mie, zero
15+
csrw mip, zero
16+
17+
la t5, stack0
18+
lui t6, 2
19+
add t5, t5, t6
20+
mv sp, t5
21+
22+
.option push
23+
.option norelax
24+
la gp, __global_pointer$
25+
.option pop
26+
27+
csrw satp, zero
28+
29+
la t5, bss_start
30+
la t6, bss_end
31+
bss_clear:
32+
sd zero, (t5)
33+
addi t5, t5, 8
34+
bltu t5, t6, bss_clear
35+
36+
la t0, kmain
37+
csrw mepc, t0
38+
39+
call kmain
40+
41+
finish:
42+
j finish
43+
44+
debug_message:
45+
.string "[DEBUG]\n\r"
46+
47+
.size _entry, . - _entry

kernel.ld

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
OUTPUT_ARCH( "riscv" )
2+
ENTRY( _entry )
3+
4+
SECTIONS
5+
{
6+
/*
7+
* ensure that entry.S / _entry is at 0x80000000,
8+
* where qemu's -kernel jumps.
9+
*/
10+
. = 0x80000000;
11+
12+
.text : {
13+
*(.text .text.*)
14+
. = ALIGN(0x1000);
15+
_trampoline = .;
16+
*(trampsec)
17+
. = ALIGN(0x1000);
18+
PROVIDE(etext = .);
19+
}
20+
21+
.rodata : {
22+
. = ALIGN(16);
23+
*(.srodata .srodata.*) /* do not need to distinguish this from .rodata */
24+
. = ALIGN(16);
25+
*(.rodata .rodata.*)
26+
}
27+
28+
.data : {
29+
. = ALIGN(16);
30+
*(.sdata .sdata.*) /* do not need to distinguish this from .data */
31+
. = ALIGN(16);
32+
*(.data .data.*)
33+
}
34+
35+
.bss : {
36+
PROVIDE(bss_start = .);
37+
. = ALIGN(16);
38+
*(.sbss .sbss.*) /* do not need to distinguish this from .bss */
39+
. = ALIGN(16);
40+
*(.bss .bss.*)
41+
PROVIDE(__global_pointer$ = .);
42+
PROVIDE(bss_end = .);
43+
}
44+
45+
PROVIDE(end = .);
46+
}

main.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
unsigned char * uart = (unsigned char *)0x10000000;
2+
3+
void putchar(unsigned char c) {
4+
*uart = c;
5+
return;
6+
}
7+
8+
void print(const char * str) {
9+
while(*str != '\0') {
10+
putchar(*str);
11+
str++;
12+
}
13+
return;
14+
}
15+
16+
void kmain(void) {
17+
print("Hello world!\r\n");
18+
while(1) {
19+
// Read input from the UART
20+
putchar(*uart);
21+
}
22+
return;
23+
}

start.c

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__attribute__ ((aligned (16))) char stack0[4096];

0 commit comments

Comments
 (0)