Skip to content

Commit

Permalink
task1が動く
Browse files Browse the repository at this point in the history
  • Loading branch information
yutyan0119 committed Jun 7, 2023
1 parent aaed2ed commit 6b4989d
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BasedOnStyle: Google
IndentWidth: 4
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ LD=$(PREFIX)ld
AS=$(PREFIX)as

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

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

INCLUDES=-Ikernel -Ikernel/rv32

kernel/kernel: $(OBJ_FILES)
$(LD) $(LDFLAGS) -o $@ $^

%.o: %.c Makefile
$(CC) $(CFLAGS) -c -o $@ $<
$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<

%.o: %.S Makefile
$(AS) $(ASFLAGS) -c -o $@ $<
$(AS) $(ASFLAGS) $(INCLUDES) -c -o $@ $<

clean:
rm -rf $(OBJ_FILES) kernel/kernel

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

debug: kernel/kernel
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

.PHONY: clean run
32 changes: 28 additions & 4 deletions kernel/main.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
# define UARTADR 0x10000000
#include "task.h"
#include "switch.h"

task_info task_list[2];

#define UARTADR 0x10000000

void print_int(int i) {
i %= 10;
volatile unsigned int * const UART0DR = (unsigned int *)UARTADR;
volatile unsigned int *const UART0DR = (unsigned int *)UARTADR;
*UART0DR = (unsigned int)(i + '0');
}

void print_uart0(const char *s) {
volatile unsigned int * const UART0DR = (unsigned int *)UARTADR;
volatile unsigned int *const UART0DR = (unsigned int *)UARTADR;
while (*s != '\0') {
*UART0DR = (unsigned int)(*s);
s++;
}
}

int main(){
void clear_bss() {
extern unsigned int* __bss_start, __bss_end;
unsigned int start = (unsigned int)__bss_start;
unsigned int end = (unsigned int)__bss_end;
while (start < end) {
*(unsigned int*)start = 0;
start ++;
}
}

void task_1() {
while (1) {
print_uart0("task_1\n");
}
}

int main() {
clear_bss();
print_uart0("Hello world!\n");
print_int(1);
task_create(&task_list[0], task_1, 0);
task_load_rv32(&task_list[0].sp);
return 0;
}
4 changes: 2 additions & 2 deletions kernel/rv32/link.ld
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ SECTIONS
*(.sdata .sdata.*);
}
.bss : {
__bss = .;
__bss_start = .;
*(.bss .bss.*);
. = ALIGN(16);
*(.sbss .sbss.*);
__bss_end = .;
. = ALIGN(4096);
stack_top = .;
. = . + 16384;
stack_top = .;
}

. = ALIGN(4096);
Expand Down
23 changes: 21 additions & 2 deletions kernel/rv32/switch.S
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
.align 4
.global task_switch
.global task_switch_rv32
.global task_load_rv32

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

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

Expand Down Expand Up @@ -49,3 +50,21 @@ task_switch:

# 戻る
ret

task_load_rv32:
lw sp, (a0)
lw ra, 0*4(sp)
lw s0, 1*4(sp)
lw s1, 2*4(sp)
lw s2, 3*4(sp)
lw s3, 4*4(sp)
lw s4, 5*4(sp)
lw s5, 6*4(sp)
lw s6, 7*4(sp)
lw s7, 8*4(sp)
lw s8, 9*4(sp)
lw s9, 10*4(sp)
lw s10, 11*4(sp)
lw s11, 12*4(sp)
addi sp, sp, 13*4
ret
4 changes: 4 additions & 0 deletions kernel/rv32/switch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

void task_switch_rv32(unsigned int* old_sp, unsigned int new_sp);
void task_load_rv32(unsigned int* sp);
40 changes: 40 additions & 0 deletions kernel/task.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "switch.h"
#include "task.h"

void task_switch(task_info* current, task_info* next) {
task_switch_rv32(&current->sp, (unsigned int)&next->sp);
}

void task_create(task_info* task, void (*entry)(void), int pid) {
task->pid = pid;
//タスクのスタックポインタを設定
task->sp = (unsigned int)&(task->stack[4096]);
// stack pointerをずらしながら、レジスタの初期値を設定していく
task->sp -= sizeof(unsigned int);
*((unsigned int*)(task->sp)) = 0; // s11
task->sp -= sizeof(unsigned int);
*((unsigned int*)(task->sp)) = 0; // s10
task->sp -= sizeof(unsigned int);
*((unsigned int*)(task->sp)) = 0; // s9
task->sp -= sizeof(unsigned int);
*((unsigned int*)(task->sp)) = 0; // s8
task->sp -= sizeof(unsigned int);
*((unsigned int*)(task->sp)) = 0; // s7
task->sp -= sizeof(unsigned int);
*((unsigned int*)(task->sp)) = 0; // s6
task->sp -= sizeof(unsigned int);
*((unsigned int*)(task->sp)) = 0; // s5
task->sp -= sizeof(unsigned int);
*((unsigned int*)(task->sp)) = 0; // s4
task->sp -= sizeof(unsigned int);
*((unsigned int*)(task->sp)) = 0; // s3
task->sp -= sizeof(unsigned int);
*((unsigned int*)(task->sp)) = 0; // s2
task->sp -= sizeof(unsigned int);
*((unsigned int*)(task->sp)) = 0; // s1
task->sp -= sizeof(unsigned int);
*((unsigned int*)(task->sp)) = 0; // s0
task->sp -= sizeof(unsigned int);
*((unsigned int*)(task->sp)) = (unsigned int)entry; // ra
return;
}
11 changes: 11 additions & 0 deletions kernel/task.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

typedef struct task_info {
int pid;
unsigned int stack[4096];
unsigned int sp;
} task_info;

void task_switch(task_info* current, task_info* next);

void task_create(task_info* task, void (*entry)(void), int pid);

0 comments on commit 6b4989d

Please sign in to comment.