Skip to content

Commit 6b4989d

Browse files
committed
task1が動く
1 parent aaed2ed commit 6b4989d

File tree

8 files changed

+116
-11
lines changed

8 files changed

+116
-11
lines changed

.clang-format

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BasedOnStyle: Google
2+
IndentWidth: 4

Makefile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ LD=$(PREFIX)ld
44
AS=$(PREFIX)as
55

66
BASE_CFLAGS=-fno-stack-protector -fno-zero-initialized-in-bss -ffreestanding
7-
OPT_CFLAGS=-fno-builtin -nostdlib -nodefaultlibs -nostartfiles -mstrict-align
7+
OPT_CFLAGS=-fno-builtin -nostdlib -nodefaultlibs -nostartfiles -mstrict-align -g -O0
88
WARN_CFLAGS=-Wall -Wextra
99
ARCH_CFLAGS=-march=rv32im
1010

@@ -16,19 +16,24 @@ C_SRC=$(wildcard kernel/*.c kernel/rv32/*.c)
1616
S_SRC=$(wildcard kernel/*.S kernel/rv32/*.S)
1717
OBJ_FILES=$(C_SRC:.c=.o) $(S_SRC:.S=.o)
1818

19+
INCLUDES=-Ikernel -Ikernel/rv32
20+
1921
kernel/kernel: $(OBJ_FILES)
2022
$(LD) $(LDFLAGS) -o $@ $^
2123

2224
%.o: %.c Makefile
23-
$(CC) $(CFLAGS) -c -o $@ $<
25+
$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
2426

2527
%.o: %.S Makefile
26-
$(AS) $(ASFLAGS) -c -o $@ $<
28+
$(AS) $(ASFLAGS) $(INCLUDES) -c -o $@ $<
2729

2830
clean:
2931
rm -rf $(OBJ_FILES) kernel/kernel
3032

3133
run: kernel/kernel
3234
qemu-system-riscv32 -smp 1 -nographic -serial mon:stdio --no-reboot -m 128 -machine virt,aclint=on -bios none -kernel kernel/kernel
3335

36+
debug: kernel/kernel
37+
qemu-system-riscv32 -smp 1 -nographic -serial mon:stdio --no-reboot -m 128 -machine virt,aclint=on -bios none -kernel kernel/kernel -S -gdb tcp::1234
38+
3439
.PHONY: clean run

kernel/main.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,45 @@
1-
# define UARTADR 0x10000000
1+
#include "task.h"
2+
#include "switch.h"
3+
4+
task_info task_list[2];
5+
6+
#define UARTADR 0x10000000
27

38
void print_int(int i) {
49
i %= 10;
5-
volatile unsigned int * const UART0DR = (unsigned int *)UARTADR;
10+
volatile unsigned int *const UART0DR = (unsigned int *)UARTADR;
611
*UART0DR = (unsigned int)(i + '0');
712
}
813

914
void print_uart0(const char *s) {
10-
volatile unsigned int * const UART0DR = (unsigned int *)UARTADR;
15+
volatile unsigned int *const UART0DR = (unsigned int *)UARTADR;
1116
while (*s != '\0') {
1217
*UART0DR = (unsigned int)(*s);
1318
s++;
1419
}
1520
}
1621

17-
int main(){
22+
void clear_bss() {
23+
extern unsigned int* __bss_start, __bss_end;
24+
unsigned int start = (unsigned int)__bss_start;
25+
unsigned int end = (unsigned int)__bss_end;
26+
while (start < end) {
27+
*(unsigned int*)start = 0;
28+
start ++;
29+
}
30+
}
31+
32+
void task_1() {
33+
while (1) {
34+
print_uart0("task_1\n");
35+
}
36+
}
37+
38+
int main() {
39+
clear_bss();
1840
print_uart0("Hello world!\n");
1941
print_int(1);
42+
task_create(&task_list[0], task_1, 0);
43+
task_load_rv32(&task_list[0].sp);
2044
return 0;
2145
}

kernel/rv32/link.ld

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ SECTIONS
2828
*(.sdata .sdata.*);
2929
}
3030
.bss : {
31-
__bss = .;
31+
__bss_start = .;
3232
*(.bss .bss.*);
3333
. = ALIGN(16);
3434
*(.sbss .sbss.*);
3535
__bss_end = .;
3636
. = ALIGN(4096);
37-
stack_top = .;
3837
. = . + 16384;
38+
stack_top = .;
3939
}
4040

4141
. = ALIGN(4096);

kernel/rv32/switch.S

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
.align 4
2-
.global task_switch
2+
.global task_switch_rv32
3+
.global task_load_rv32
34

45
# a0には切り替え元のspアドレスが入っている
56
# a1には切り替え先のspアドレスが入っている
67

7-
task_switch:
8+
task_switch_rv32:
89
# レジスタを保存する分だけスタックを確保する
910
addi sp, sp, -13*4
1011

@@ -49,3 +50,21 @@ task_switch:
4950

5051
# 戻る
5152
ret
53+
54+
task_load_rv32:
55+
lw sp, (a0)
56+
lw ra, 0*4(sp)
57+
lw s0, 1*4(sp)
58+
lw s1, 2*4(sp)
59+
lw s2, 3*4(sp)
60+
lw s3, 4*4(sp)
61+
lw s4, 5*4(sp)
62+
lw s5, 6*4(sp)
63+
lw s6, 7*4(sp)
64+
lw s7, 8*4(sp)
65+
lw s8, 9*4(sp)
66+
lw s9, 10*4(sp)
67+
lw s10, 11*4(sp)
68+
lw s11, 12*4(sp)
69+
addi sp, sp, 13*4
70+
ret

kernel/rv32/switch.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
3+
void task_switch_rv32(unsigned int* old_sp, unsigned int new_sp);
4+
void task_load_rv32(unsigned int* sp);

kernel/task.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "switch.h"
2+
#include "task.h"
3+
4+
void task_switch(task_info* current, task_info* next) {
5+
task_switch_rv32(&current->sp, (unsigned int)&next->sp);
6+
}
7+
8+
void task_create(task_info* task, void (*entry)(void), int pid) {
9+
task->pid = pid;
10+
//タスクのスタックポインタを設定
11+
task->sp = (unsigned int)&(task->stack[4096]);
12+
// stack pointerをずらしながら、レジスタの初期値を設定していく
13+
task->sp -= sizeof(unsigned int);
14+
*((unsigned int*)(task->sp)) = 0; // s11
15+
task->sp -= sizeof(unsigned int);
16+
*((unsigned int*)(task->sp)) = 0; // s10
17+
task->sp -= sizeof(unsigned int);
18+
*((unsigned int*)(task->sp)) = 0; // s9
19+
task->sp -= sizeof(unsigned int);
20+
*((unsigned int*)(task->sp)) = 0; // s8
21+
task->sp -= sizeof(unsigned int);
22+
*((unsigned int*)(task->sp)) = 0; // s7
23+
task->sp -= sizeof(unsigned int);
24+
*((unsigned int*)(task->sp)) = 0; // s6
25+
task->sp -= sizeof(unsigned int);
26+
*((unsigned int*)(task->sp)) = 0; // s5
27+
task->sp -= sizeof(unsigned int);
28+
*((unsigned int*)(task->sp)) = 0; // s4
29+
task->sp -= sizeof(unsigned int);
30+
*((unsigned int*)(task->sp)) = 0; // s3
31+
task->sp -= sizeof(unsigned int);
32+
*((unsigned int*)(task->sp)) = 0; // s2
33+
task->sp -= sizeof(unsigned int);
34+
*((unsigned int*)(task->sp)) = 0; // s1
35+
task->sp -= sizeof(unsigned int);
36+
*((unsigned int*)(task->sp)) = 0; // s0
37+
task->sp -= sizeof(unsigned int);
38+
*((unsigned int*)(task->sp)) = (unsigned int)entry; // ra
39+
return;
40+
}

kernel/task.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
typedef struct task_info {
4+
int pid;
5+
unsigned int stack[4096];
6+
unsigned int sp;
7+
} task_info;
8+
9+
void task_switch(task_info* current, task_info* next);
10+
11+
void task_create(task_info* task, void (*entry)(void), int pid);

0 commit comments

Comments
 (0)