Skip to content
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
5 changes: 5 additions & 0 deletions lab3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tmp
book
build
*.elf
*.img
66 changes: 66 additions & 0 deletions lab3/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
shell = /bin/bash
CROSS_COMPILER := aarch64-linux-gnu-
CC := $(CROSS_COMPILER)gcc
LD := $(CROSS_COMPILER)ld
OC := $(CROSS_COMPILER)objcopy
BUILD_DIR := build
SRC_DIR := src
LIB_DIR := $(SRC_DIR)/lib
KERNEL_DIR := $(SRC_DIR)/kernel
BOOTLOADER_DIR := $(SRC_DIR)/bootloader
KERNEL_SRCS := $(shell cd src; find kernel lib -name '*.[cs]')
KERNEL_OBJS := $(KERNEL_SRCS:%=$(BUILD_DIR)/%.o)
BOOTLOADER_SRCS := $(shell cd src; find bootloader lib -name '*.[cs]')
BOOTLOADER_OBJS := $(BOOTLOADER_SRCS:%=$(BUILD_DIR)/%.o)

KERNEL_CFLAGS := -I $(KERNEL_DIR)/include
BOOTLOADER_CFLAGS := -I $(BOOTLOADER_DIR)/include
CFLAGS := -O0 -I $(LIB_DIR)/include -fno-stack-protector -ffreestanding -fdata-sections -ffunction-sections -g

.PHONY : all clean rootfs test debug

all: kernel8.img

clean:
rm -rf build kernel8.img kernel8.elf

rootfs:
cd rootfs && find . | cpio -o -H newc > ../raspi3b/initramfs.cpio

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

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

kernel8.img: $(KERNEL_OBJS)
$(LD) -T $(KERNEL_DIR)/linker.ld -o kernel8.elf $^
$(OC) -O binary kernel8.elf kernel8.img

bootloader.img: $(BOOTLOADER_OBJS)
$(LD) -T $(BOOTLOADER_DIR)/linker.ld -o bootloader.elf $^ --gc-sections
$(OC) -O binary bootloader.elf bootloader.img

$(BUILD_DIR)/kernel/%.c.o: $(KERNEL_DIR)/%.c
mkdir -p $(dir $@)
$(CC) $(KERNEL_CFLAGS) $(CFLAGS) -c $< -o $@

$(BUILD_DIR)/kernel/%.s.o: $(KERNEL_DIR)/%.s
mkdir -p $(dir $@)
$(CC) $(KERNEL_CFLAGS) $(CFLAGS) -c $< -o $@

$(BUILD_DIR)/bootloader/%.c.o: $(BOOTLOADER_DIR)/%.c
mkdir -p $(dir $@)
$(CC) $(BOOTLOADER_CFLAGS) $(CFLAGS) -c $< -o $@

$(BUILD_DIR)/bootloader/%.s.o: $(BOOTLOADER_DIR)/%.s
mkdir -p $(dir $@)
$(CC) $(BOOTLOADER_CFLAGS) $(CFLAGS) -c $< -o $@

$(BUILD_DIR)/lib/%.c.o: $(LIB_DIR)/%.c
mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c $< -o $@

$(BUILD_DIR)/lib/%.s.o: $(LIB_DIR)/%.s
mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c $< -o $@
15 changes: 15 additions & 0 deletions lab3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# OS

Physical Environment: Raspberry Pi 3b+

Simulate Environment: QEMU

Build Command:
```
make
```

Simulate Command:
```
qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial null -serial stdio -display none
```
Binary file added lab3/raspi3b/bcm2710-rpi-3-b-plus.dtb
Binary file not shown.
3 changes: 3 additions & 0 deletions lab3/raspi3b/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kernel=bootloader.img
arm_64bit=1
initramfs initramfs.cpio 0x20000000
Binary file added lab3/raspi3b/initramfs.cpio
Binary file not shown.
1 change: 1 addition & 0 deletions lab3/rootfs/file1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is file1.
1 change: 1 addition & 0 deletions lab3/rootfs/file2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is file2.
Binary file added lab3/rootfs/test_el0_exception
Binary file not shown.
40 changes: 40 additions & 0 deletions lab3/src/bootloader/bootloader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <bootloader.h>

#include <stdint.h>
#include <stddef.h>

#include <uart.h>
#include <utils.h>
#include <string.h>

extern uint32_t _bss_begin;
extern uint32_t _bss_end;
extern uint32_t _stack_top;

void _init(void){
for(uint32_t*addr = &_bss_begin; addr != &_bss_end; addr++){
*addr = 0;
}
uart_init();
load_kernel();
}

void load_kernel(){
char *kernel_addr = (char *)0x80000;
uart_print("Input Size: ");
char buf[BUF_SIZE];
size_t recvlen = uart_readline(buf);
buf[recvlen] = '\0';
uint32_t imagesize = atoi(buf);
uart_print("Input Kernel Image (Size: ");
uart_print_hex(imagesize, 32);
uart_print("): ");
uart_read(kernel_addr, imagesize);
uart_print("Go to kernel!");
newline();
asm volatile(
"mov x0, x10\n"
"ldr x1, =0x80000\n"
"br x1\n"
);
}
6 changes: 6 additions & 0 deletions lab3/src/bootloader/include/bootloader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __KERNEL__
#define __KERNEL__
#define BUF_SIZE 0x20
void load_kernel();
void _init();
#endif
41 changes: 41 additions & 0 deletions lab3/src/bootloader/linker.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
ENTRY(_start)
MEMORY
{
BOOTLOADER (rx) : ORIGIN = 0x20000, LENGTH = 128K
CODE (rx) : ORIGIN = 0x80000, LENGTH = 128K
ROM (r) : ORIGIN = 0xa0000, LENGTH = 128K
RAM (rw) : ORIGIN = 0xc0000, LENGTH = 512K
}
SECTIONS
{
.text :
{
_loader_begin = .;
*(.text.start)
*(.text.loader)
*(.text)
} >CODE
.rodata :
{
_rodata_begin = .;
*(.rodata)
_rodata_end = .;
} >ROM
.data :
{
_data_begin = .;
*(.data)
_data_end = .;
} >RAM
.bss : {
. = ALIGN(0x8);
_bss_begin = .;
*(.bss)
_bss_end = .;
_loader_end = .;
} >RAM
_stack_top = ORIGIN(RAM) + LENGTH(RAM);
_bootloader_begin = ORIGIN(BOOTLOADER);
_loader_size = _loader_end - _loader_begin;
_kernel_begin = ORIGIN(CODE);
}
20 changes: 20 additions & 0 deletions lab3/src/bootloader/start.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.section ".text.start"
.extern _stack_top
.extern _bootloader_begin
.extern _kernel_begin
.extern _loader_size
.extern _init
.global _start
_start:
mov x10, x0
adrp x0, _stack_top
mov sp, x0
adrp x0, _kernel_begin
adrp x1, _bootloader_begin
adrp x2, _loader_size
_loop:
ldr x4, [x0], #8
str x4, [x1], #8
subs x2, x2, #8
b.gt _loop
b #(_init - 0x60000)
18 changes: 18 additions & 0 deletions lab3/src/exec/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CROSS_COMPILE = aarch64-linux-gnu-
CC := $(CROSS_COMPILE)gcc
LD := $(CROSS_COMPILE)ld

.PHONY: clean

CFLAGS := -O0 -fno-stack-protector -ffreestanding -fdata-sections -ffunction-sections

test_el0_exception: test_el0_exception.s.o
$(LD) -T linker.ld $^ -o test_el0_exception.elf
$(CROSS_COMPILE)objcopy -O binary test_el0_exception.elf test_el0_exception
cp $@ ../../rootfs

test_el0_exception.s.o: main.s
$(CC) $(CFLAGS) -c $< -o $@

clean:
rm test_el0_exception.elf test_el0_exception test_el0_exception.s.o
18 changes: 18 additions & 0 deletions lab3/src/exec/linker.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ENTRY(_start)

SECTIONS
{
.text : {
*(.text.start)
*(.text)
}
.rodata : {
*(.rodata)
}
.data : {
*(.data)
}
.bss : {
*(.bss)
}
}
11 changes: 11 additions & 0 deletions lab3/src/exec/main.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.section ".text.start"
.global _start
_start:
mov x0, 0
1:
add x0, x0, 1
svc 0
cmp x0, 5
blt 1b
1:
b 1b
Binary file added lab3/src/exec/test_el0_exception
Binary file not shown.
Binary file added lab3/src/exec/test_el0_exception.s.o
Binary file not shown.
10 changes: 10 additions & 0 deletions lab3/src/kernel/include/kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef __KERNEL__
#define __KERNEL__
#define BUF_SIZE 256
void _init();
void boot_message();
void allocator_test();
void devicetree_check();
void spsr_el1_check();
void boottimer();
#endif
Loading