Skip to content

Commit c8209b6

Browse files
zhangwenxiangzhangwenxiang
zhangwenxiang
authored and
zhangwenxiang
committed
riscv operating system
0 parents  commit c8209b6

File tree

189 files changed

+14313
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

189 files changed

+14313
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
out
2+
*.o
3+
*.out
4+
*.elf

.vscode/launch.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "riscv-debug",
9+
"type": "cppdbg",
10+
"request": "launch",
11+
"cwd": "${workspaceFolder}/${relativeFileDirname}",
12+
"program": "out/os.elf",
13+
"stopAtEntry": false,
14+
"MIMode": "gdb",
15+
"miDebuggerPath": "/usr/bin/gdb-multiarch",
16+
"miDebuggerServerAddress": "localhost:1234",
17+
"preLaunchTask": "qemu-run",
18+
},
19+
]
20+
}

.vscode/settings.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"os.h": "c"
4+
}
5+
}

.vscode/tasks.json

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "makefile-make",
6+
"type": "shell",
7+
"options": {
8+
"cwd": "${workspaceFolder}/${relativeFileDirname}"
9+
},
10+
"command": "make",
11+
// "group": {
12+
// "kind": "build",
13+
// "isDefault": true
14+
// },
15+
"problemMatcher": ["$gcc"]
16+
},
17+
18+
{
19+
"label": "qemu-run",
20+
"type": "shell",
21+
"options": {
22+
"cwd": "${workspaceFolder}/${relativeFileDirname}"
23+
},
24+
"command": "echo 'QEMU started'; qemu-system-riscv32 -nographic -smp 1 -machine virt -bios none -serial mon:stdio -gdb tcp::1234 -S -kernel out/os.elf",
25+
"args": [],
26+
"dependsOn": ["makefile-make"],
27+
"isBackground": true,
28+
"problemMatcher": [
29+
{
30+
"pattern": [
31+
{
32+
"regexp": ".",
33+
"file": 1,
34+
"location": 2,
35+
"message": 3
36+
}
37+
],
38+
"background": {
39+
"activeOnStart": true,
40+
"beginsPattern": ".",
41+
"endsPattern": "QEMU started",
42+
}
43+
}
44+
]
45+
},
46+
47+
{
48+
// lsof -i tcp:1234 -> kill [pid]
49+
"type": "shell",
50+
"label": "Kill Qemu Server",
51+
"command": "ps -ef | grep qemu-riscv64 | grep -v grep | awk '{print $2}' | xargs -i kill -9 {}"
52+
},
53+
]
54+
}

00-bootstrap/Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
USE_LINKER_SCRIPT = false
2+
3+
SRCS_ASM = \
4+
start.S \
5+
6+
SRCS_C = \
7+
kernel.c \
8+
9+
include ../common.mk

00-bootstrap/kernel.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// called in start.S
2+
// j start_kernel
3+
void start_kernel(void)
4+
{
5+
while (1) {}; // stop here!
6+
}
7+

00-bootstrap/platform.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef __PLATFORM_H__
2+
#define __PLATFORM_H__
3+
4+
/*
5+
* QEMU RISC-V Virt machine with 16550a UART and VirtIO MMIO
6+
*/
7+
8+
/*
9+
* maximum number of CPUs
10+
* see https://github.com/qemu/qemu/blob/master/include/hw/riscv/virt.h
11+
* #define VIRT_CPUS_MAX 8
12+
*/
13+
#define MAXNUM_CPU 8
14+
15+
#endif /* __PLATFORM_H__ */

00-bootstrap/start.S

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include "platform.h"
2+
3+
# size of each hart's stack is 1024 bytes
4+
.equ STACK_SIZE, 1024
5+
6+
.global _start
7+
8+
.text
9+
_start: # <- asm program start from the 1st instructions
10+
# park harts with id != 0
11+
# csrr (csr read), csrr rd, csr
12+
# csrw (csr write), csrw csr, rs
13+
# m-hard-id
14+
csrr t0, mhartid # read current m-hart-id to t0
15+
mv tp, t0 # keep CPU's hartid in its tp for later usage.
16+
bnez t0, park # branch to park if t0 not equal to zero
17+
18+
# Setup stacks, the stack grows from bottom to top, so we put the
19+
# stack pointer to the very end of the stack range.
20+
# slli (shift left logical immediate): slli rd, rs, imm: rd = rs1 << imm
21+
slli t0, t0, 10 # shift left the hart id by 1024
22+
# la (load address): la rd, label
23+
la sp, stacks + STACK_SIZE # set the initial stack pointer
24+
# to the end of the first stack space
25+
# add rd, rs1, rs2: rd = rs1 + rs2
26+
add sp, sp, t0 # move the current hart stack pointer
27+
# to its place in the stack space
28+
# jump
29+
j start_kernel # hart 0 jump to kernal.c
30+
31+
park:
32+
wfi # wait for interrupt
33+
j park # jump to park (dead loops)
34+
35+
# In the standard RISC-V calling convention, the stack pointer sp
36+
# is always 16-byte aligned.
37+
.balign 16
38+
stacks:
39+
.skip STACK_SIZE * MAXNUM_CPU # allocate space for all the harts stacks
40+
41+
.end # End of file

01-helloRVOS/Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
USE_LINKER_SCRIPT = false
2+
3+
SRCS_ASM = \
4+
start.S \
5+
6+
SRCS_C = \
7+
kernel.c \
8+
uart.c \
9+
10+
include ../common.mk

01-helloRVOS/core

10.9 MB
Binary file not shown.

01-helloRVOS/kernel.c

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extern void uart_init(void);
2+
extern void uart_puts(char *s);
3+
4+
void start_kernel(void)
5+
{
6+
uart_init();
7+
uart_puts("Hello, RVOS!\n");
8+
9+
while (1) {}; // stop here!
10+
}
11+

01-helloRVOS/platform.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef __PLATFORM_H__
2+
#define __PLATFORM_H__
3+
4+
/*
5+
* QEMU RISC-V Virt machine with 16550a UART and VirtIO MMIO
6+
*/
7+
8+
/*
9+
* maximum number of CPUs
10+
* see https://github.com/qemu/qemu/blob/master/include/hw/riscv/virt.h
11+
* #define VIRT_CPUS_MAX 8
12+
*/
13+
#define MAXNUM_CPU 8
14+
15+
/*
16+
* MemoryMap
17+
* see https://github.com/qemu/qemu/blob/master/hw/riscv/virt.c, virt_memmap[]
18+
* 0x00001000 -- boot ROM, provided by qemu
19+
* 0x02000000 -- CLINT
20+
* 0x0C000000 -- PLIC
21+
* 0x10000000 -- UART0
22+
* 0x10001000 -- virtio disk
23+
* 0x80000000 -- boot ROM jumps here in machine mode, where we load our kernel
24+
*/
25+
26+
/* This machine puts UART registers here in physical memory. */
27+
#define UART0 0x10000000L
28+
29+
#endif /* __PLATFORM_H__ */

01-helloRVOS/start.S

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include "platform.h"
2+
3+
# size of each hart's stack is 1024 bytes
4+
.equ STACK_SIZE, 1024
5+
6+
.global _start
7+
8+
.text
9+
_start:
10+
# park harts with id != 0
11+
csrr t0, mhartid # read current hart id
12+
mv tp, t0 # keep CPU's hartid in its tp for later usage.
13+
bnez t0, park # if we're not on the hart 0
14+
# we park the hart
15+
# Setup stacks, the stack grows from bottom to top, so we put the
16+
# stack pointer to the very end of the stack range.
17+
slli t0, t0, 10 # shift left the hart id by 1024
18+
la sp, stacks + STACK_SIZE # set the initial stack pointer
19+
# to the end of the first stack space
20+
add sp, sp, t0 # move the current hart stack pointer
21+
# to its place in the stack space
22+
23+
j start_kernel # hart 0 jump to c
24+
25+
park:
26+
wfi
27+
j park
28+
29+
# In the standard RISC-V calling convention, the stack pointer sp
30+
# is always 16-byte aligned.
31+
.balign 16
32+
stacks:
33+
.skip STACK_SIZE * MAXNUM_CPU # allocate space for all the harts stacks
34+
35+
.end # End of file

01-helloRVOS/types.h

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef __TYPES_H__
2+
#define __TYPES_H__
3+
4+
typedef unsigned char uint8_t;
5+
typedef unsigned short uint16_t;
6+
typedef unsigned int uint32_t;
7+
typedef unsigned long long uint64_t;
8+
9+
#endif /* __TYPES_H__ */

0 commit comments

Comments
 (0)