Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

312551174 lab7 #249

Open
wants to merge 17 commits into
base: 312551174
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
*.img
*.d
*.elf
initramfs.cpio.test

**yuchang**
2 changes: 1 addition & 1 deletion lab05/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# OSC2023-Lab04
# OSC2023-Lab05

| Github Account | Student ID | Name |
|----------------|------------|---------------|
Expand Down
Binary file added lab05/initramfs.cpio.1
Binary file not shown.
2 changes: 1 addition & 1 deletion lab05/kernel/src/entry.S
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ ret_from_fork:
bl schedule_tail // preemptive enable
cbz x19, ret_to_user
mov x0, x20
blr x19 //should never return
blr x19 //should never return, except for kp_to_user

ret_to_user:
bl disable_irq
Expand Down
2 changes: 1 addition & 1 deletion lab05/kernel/src/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ int sys_fork() // [TODO]
{
unsigned long stack = (unsigned long)balloc(THREAD_SIZE);
if((void*)stack == NULL) return -1;
memzero_asm(stack, 4*THREAD_SIZE);
memzero_asm(stack, THREAD_SIZE);
return copy_process(0, 0, 0, stack);
}

Expand Down
49 changes: 49 additions & 0 deletions lab07/bootloader/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
ARMGNU ?= aarch64-linux-gnu

COPS = -Wall -ffreestanding -nostdinc -nostdlib -nostartfiles -fno-stack-protector -g -Iinclude
ASMOPS =

BUILD_DIR = build
SRC_DIR = src


all: clean bootloader.img

.PHONY: clean
clean:
rm -rf $(BUILD_DIR) *.img

$(BUILD_DIR)/%_c.o: $(SRC_DIR)/%.c
mkdir -p $(@D)
$(ARMGNU)-gcc $(COPS) -c $< -o $@

$(BUILD_DIR)/%_s.o: $(SRC_DIR)/%.s
$(ARMGNU)-gcc $(ASMOPS) -c $< -o $@


C_FILES = $(wildcard $(SRC_DIR)/*.c)
ASM_FILES = $(wildcard $(SRC_DIR)/*.S)
OBJ_FILES = $(C_FILES:$(SRC_DIR)/%.c=$(BUILD_DIR)/%_c.o)
OBJ_FILES += $(ASM_FILES:$(SRC_DIR)/%.S=$(BUILD_DIR)/%_s.o)

# DEP_FILES = $(OBJ_FILES:%.o=%.d)
# -include $(DEP_FILES)

bootloader.img: $(OBJ_FILES) $(SRC_DIR)/linker.ld
$(ARMGNU)-ld -T $(SRC_DIR)/linker.ld -o $(BUILD_DIR)/bootloader.elf $(OBJ_FILES)
$(ARMGNU)-objcopy $(BUILD_DIR)/bootloader.elf -O binary bootloader.img

.PHONY: run debug test
run: bootloader.img
qemu-system-aarch64 -M raspi3b -kernel bootloader.img -display none -serial null -serial pty \
-initrd ../initramfs.cpio \
-dtb ../bcm2710-rpi-3-b-plus.dtb

test: bootloader.img
qemu-system-aarch64 -M raspi3b -kernel bootloader.img -display none -serial null -serial stdio

dump: bootloader.img
qemu-system-aarch64 -M raspi3b -kernel bootloader.img -display none -d in_asm

debug: all
qemu-system-aarch64 -M raspi3b -kernel bootloader.img -display none -serial null -serial stdio -S -s
6 changes: 6 additions & 0 deletions lab07/bootloader/include/base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef _BASE_H
#define _BASE_H

#define MMIO_BASE 0x3F000000

#endif
16 changes: 16 additions & 0 deletions lab07/bootloader/include/bootload.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef __BOOTLOAD_H__
#define __BOOTLOAD_H__

#define KernelAddr 0x80000
#define BootAddr 0x60000

#include "mini_uart.h"
#include "io.h"
#include "type.h"

extern uint32_t __bss_end;

void reallocate();
void load_kernel();

#endif
12 changes: 12 additions & 0 deletions lab07/bootloader/include/gpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef _GPIO_H
#define _GPIO_H

#include "base.h"

#define GPFSEL1 ((volatile unsigned int*)(MMIO_BASE+0x00200004))
#define GPSET0 ((volatile unsigned int*)(MMIO_BASE+0x0020001C))
#define GPCLR0 ((volatile unsigned int*)(MMIO_BASE+0x00200028))
#define GPPUD ((volatile unsigned int*)(MMIO_BASE+0x00200094))
#define GPPUDCLK0 ((volatile unsigned int*)(MMIO_BASE+0x00200098))

#endif
11 changes: 11 additions & 0 deletions lab07/bootloader/include/io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef __IO_H__
#define __IO_H__

#include "mini_uart.h"

void printf(char* str);
void printfc(char c);
void printf_hex(unsigned int d);
char read_char();

#endif
26 changes: 26 additions & 0 deletions lab07/bootloader/include/mini_uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef _MINI_UART_H
#define _MINI_UART_H

#include "base.h"

#define AUX_ENABLES ((volatile unsigned int*)(MMIO_BASE+0x00215004))
#define AUX_MU_IO_REG ((volatile unsigned int*)(MMIO_BASE+0x00215040))
#define AUX_MU_IER_REG ((volatile unsigned int*)(MMIO_BASE+0x00215044))
#define AUX_MU_IIR_REG ((volatile unsigned int*)(MMIO_BASE+0x00215048))
#define AUX_MU_LCR_REG ((volatile unsigned int*)(MMIO_BASE+0x0021504C))
#define AUX_MU_MCR_REG ((volatile unsigned int*)(MMIO_BASE+0x00215050))
#define AUX_MU_LSR_REG ((volatile unsigned int*)(MMIO_BASE+0x00215054))
#define AUX_MU_MSR_REG ((volatile unsigned int*)(MMIO_BASE+0x00215058))
#define AUX_MU_SCRATCH ((volatile unsigned int*)(MMIO_BASE+0x0021505C))
#define AUX_MU_CNTL_REG ((volatile unsigned int*)(MMIO_BASE+0x00215060))
#define AUX_MU_STAT_REG ((volatile unsigned int*)(MMIO_BASE+0x00215064))
#define AUX_MU_BAUD_REG ((volatile unsigned int*)(MMIO_BASE+0x00215068))


void uart_init();
char uart_recv();
char uart_recv_io();
void uart_send(char c);
void uart_send_string(const char* str);

#endif
9 changes: 9 additions & 0 deletions lab07/bootloader/include/type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef __TYPE_H__
#define __TYPE_H__

typedef unsigned char uint8_t;
typedef unsigned int uint16_t;
typedef unsigned long int uint32_t;
typedef unsigned long long int uint64_t;

#endif
35 changes: 35 additions & 0 deletions lab07/bootloader/src/boot.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.section ".text.boot"

.global _start
_prologue:
mov x20, x0
mov x1, #0x0000
movk x1, #5, lsl #16 // 0x50000
str x20, [x1]

_start:
mrs x0, mpidr_el1
and x0, x0,#0xFF // Check processor id
cbz x0, master // if cpu_id == 0, then booting
b proc_hang // Otherwise, hang

proc_hang:
b proc_hang

master:
adr x0, __bss_start
adr x1, __bss_size
bl memzero

adr x1, _start
mov sp, x1
bl main // jump to kernel_main c code
b proc_hang // should never come here

memzero:
str xzr, [x0], #8 // xzr: a register holds zero
subs x1, x1, #8
b.gt memzero
ret

// adr: Load a label's relative address into the target register.
41 changes: 41 additions & 0 deletions lab07/bootloader/src/bootload.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "bootload.h"

// uint64_t* BootHead = (uint64_t*)KernelAddr;
// uint64_t* CopyHead = (uint64_t*)BootAddr;
// uint64_t* bss_end = (uint64_t*)(&__bss_end);

void reallocate()
{
uint64_t* BootHead = (uint64_t*)KernelAddr;
uint64_t* CopyHead = (uint64_t*)BootAddr;
uint64_t* bss_end = (uint64_t*)(&__bss_end);
int i = 0;
while(BootHead != bss_end){
printf("\n");
printf_hex(i);
i++;
*CopyHead = *BootHead;
CopyHead++;
BootHead++;
}
printf("\nCopy done.");
}

void load_kernel()
{
unsigned int kernel_size = 0;
for(int i=0; i<4; i++){
kernel_size <<= 8;
kernel_size |= (uart_recv() & 0xff);
}

printf("\nKernel Size: ");
printf_hex(kernel_size);
printf("\n");

unsigned char* kernel = (unsigned char*)(0x80000);
unsigned char* kernel_ptr = kernel;
for(unsigned int i=0; i<kernel_size; i++){
*kernel_ptr++ = uart_recv();
}
}
74 changes: 74 additions & 0 deletions lab07/bootloader/src/bootloader_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "mini_uart.h"
#include "io.h"
#include "bootload.h"

/*
0x60000: bootloader start address

0x!!!!!: load_kernel() -> 0x????? - 0x20000
***** need to branch hereh ***** -> 0x????? - 0x20000 + 4

.
.
.

0x80000: kernel start address

0x?????: load_kernel()
*/

void readcmd(char*);

void *dtb_addr = 0; // Due to the variable is used in the assembly code, must be declared as global variable


void main()
{
uart_init();

while(1)
{
printf("\nType load for bootloading: ");
char cmd[10];
readcmd(cmd);
if(cmd[0] == 'l' && cmd[1] == 'o' && cmd[2] == 'a' && cmd[3] == 'd')
{
break;
}
}

reallocate();
asm volatile("b -131068"); // jump to same place in bootloader
// 0x20000 = 131072
// 0x20000 - 4 = 131068
printf("\nListening New Kernel");
load_kernel();
printf("\nFinished Loading");

asm volatile("mov %0, x20" : "=r" (dtb_addr));
printf("\nDTB Address (Bootloader): ");
printf_hex(dtb_addr);

// asm volatile (
// "mov x30, 0x80000;"
// "ret"
// );
asm volatile("blr %0"
:
: "r" (0x80000));
}

void readcmd(char *x)
{
char input_char;
int input_index = 0;
x[0] = 0;
while( ((input_char = read_char()) != '\n'))
{
x[input_index] = input_char;
++input_index;
printfc(input_char);
}

x[input_index]=0; // null char
}
32 changes: 32 additions & 0 deletions lab07/bootloader/src/io.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "io.h"

void printf(char* str)
{
uart_send_string(str);
}

void printfc(char c)
{
uart_send(c);
}

void printf_hex(unsigned int d)
{
printf("0x");
unsigned int td = d;
for(int i=28; i>=0; i-=4){
int tmp = (td >> i) & 0xf;
if(tmp < 10){
printfc('0'+tmp);
}
else{
printfc('a'+tmp-10);
}
}
}

char read_char()
{
char c = uart_recv_io();
return c;
}
22 changes: 22 additions & 0 deletions lab07/bootloader/src/linker.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
SECTIONS
{
. = 0x80000;
.text :
{
KEEP(*(.text.boot))
*(.text)
}
.rodata :{ *(.rodata) }
.data : { *(.data) }
.bss :
{
. = ALIGN(0x8);
__bss_start = .;
*(.bss)
. = ALIGN(0x8);
__bss_end = .;
}
. = ALIGN(0x8);
_end = .;
}
__bss_size = (__bss_end - __bss_start)>>3;
Loading