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
Binary file added initramfs/syscall.img
Binary file not shown.
Binary file added initramfs/user.img
Binary file not shown.
11 changes: 11 additions & 0 deletions initramfs/usr_prog.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.section ".text"
.global _start
_start:
mov x0, 0
1:
add x0, x0, 1
svc 0
cmp x0, 5
blt 1b
1:
b 1b
Binary file added initramfs/usr_prog.elf
Binary file not shown.
Binary file added initramfs/usr_prog.img
Binary file not shown.
31 changes: 31 additions & 0 deletions lab6/kernel/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
ARMGNU ?= aarch64-linux-gnu

COPS = -Wall -nostdlib -nostartfiles -ffreestanding -Iinclude -mgeneral-regs-only -O0
ASMOPS = -Iinclude

BUILD_DIR = build
SRC_DIR = src

all : kernel8.img

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

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

$(BUILD_DIR)/%_s.o: $(SRC_DIR)/%.S
$(ARMGNU)-gcc $(ASMOPS) -MMD -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)

kernel8.img: $(SRC_DIR)/linker.ld $(OBJ_FILES)
$(ARMGNU)-ld -T $(SRC_DIR)/linker.ld -o $(BUILD_DIR)/kernel8.elf $(OBJ_FILES)
$(ARMGNU)-objcopy $(BUILD_DIR)/kernel8.elf -O binary kernel8.img
217 changes: 217 additions & 0 deletions lab6/kernel/\
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
#include <stdint.h>
#include <stddef.h>
#include "cpio.h"
#include "mini_uart.h"
#include "helper.h"
#include "alloc.h"
#include "thread.h"
#include "mmu.h"
#include "utils.h"

extern char* _cpio_file;
char buff[1024];

uint8_t hex_char_to_bin(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return 0; // Not a valid hexadecimal character
}

uint64_t hex_to_bin(const char* hex) {
uint64_t result = 0;
for(int i = 0; i < 8; i ++) {
result = (result << 4) | hex_char_to_bin(hex[i]);
}
return result;
}

void* cpio_find(char* name) {
char* cpio_start = _cpio_file;
struct cpio_newc_header* header = (struct cpio_newc_header*)cpio_start;
substr(buff, header -> c_magic, 0, 5);
while (same(buff, "070701")) {
// Convert c_namesize and c_filesize from ASCII hex to binary
uint64_t namesize = hex_to_bin(header->c_namesize);
uint64_t filesize = hex_to_bin(header->c_filesize);

// Calculate the start of the file name and file data
char* filename = (char*)(header + 1);
char* filedata = filename + namesize;
filedata = (char*)((uintptr_t)filedata + ((4 - ((uintptr_t)filedata & 3)) & 3)); // Align to next 4-byte boundary

substr(buff, filename, 0, namesize - 1);
if (same(buff, "TRAILER!!!")) {
break; // End of archive
}

if (same(buff, name)) {
return filedata;
}
substr(buff, filename, 0, namesize - 1);

// Move to the next header, aligning as necessary
header = (struct cpio_newc_header*)(filedata + filesize);
header = (struct cpio_newc_header*)((uintptr_t)header + ((4 - ((uintptr_t)header & 3)) & 3)); // Align to next 4-byte boundary
substr(buff, header -> c_magic, 0, 5);
}
return 0;
}

void cpio_parse_ls() {
char* cpio_start = _cpio_file;
struct cpio_newc_header* header = (struct cpio_newc_header*)cpio_start;
substr(buff, header -> c_magic, 0, 5);
while (same(buff, "070701")) {

// Convert c_namesize and c_filesize from ASCII hex to binary
uint64_t namesize = hex_to_bin(header->c_namesize);
uint64_t filesize = hex_to_bin(header->c_filesize);

// Calculate the start of the file name and file data
char* filename = (char*)(header + 1);
char* filedata = filename + namesize;
filedata = (char*)((uintptr_t)filedata + ((4 - ((uintptr_t)filedata & 3)) & 3)); // Align to next 4-byte boundary

substr(buff, filename, 0, namesize - 1);
if (same(buff, "TRAILER!!!")) {
break; // End of archive
}
uart_printf("%s\r\n", buff);

// Move to the next header, aligning as necessary
header = (struct cpio_newc_header*)(filedata + filesize);
header = (struct cpio_newc_header*)((uintptr_t)header + ((4 - ((uintptr_t)header & 3)) & 3)); // Align to next 4-byte boundary
substr(buff, header -> c_magic, 0, 5);
}
}

void cpio_parse_cat(char* name) {
char* cpio_start = _cpio_file;
struct cpio_newc_header* header = (struct cpio_newc_header*)cpio_start;
substr(buff, header -> c_magic, 0, 5);
while (same(buff, "070701")) {
// Convert c_namesize and c_filesize from ASCII hex to binary
uint64_t namesize = hex_to_bin(header->c_namesize);
uint64_t filesize = hex_to_bin(header->c_filesize);

// Calculate the start of the file name and file data
char* filename = (char*)(header + 1);
char* filedata = filename + namesize;
filedata = (char*)((uintptr_t)filedata + ((4 - ((uintptr_t)filedata & 3)) & 3)); // Align to next 4-byte boundary

substr(buff, filename, 0, namesize - 1);
if (same(buff, "TRAILER!!!")) {
break; // End of archive
}

if (same(buff, name)) {
uart_printf ("Cat %d\r\n", filesize);
substr(buff, filedata, 0, filesize - 1);
uart_printf("%s\r\n", buff);
}
substr(buff, filename, 0, namesize - 1);

// Move to the next header, aligning as necessary
header = (struct cpio_newc_header*)(filedata + filesize);
header = (struct cpio_newc_header*)((uintptr_t)header + ((4 - ((uintptr_t)header & 3)) & 3)); // Align to next 4-byte boundary
substr(buff, header -> c_magic, 0, 5);
}
}

extern thread* get_current();

void cpio_load(char* str) {
irq(0);
thread_init();
thread* cur = get_current();
uart_printf ("cur: %llx\r\n", cur);
long xxx = read_sysreg(tpidr_el1);
uart_printf ("xxx: %llx\r\n", xxx);

void* pos = cpio_find(str);
cur -> code = my_malloc(4096 * 64);
cur -> code_size = 4096 * 64;
for (int i = 0; i < cur -> code_size; i ++) {
((char*)cur -> code)[i] = ((char*)pos)[i];
}

uart_printf ("%llx, %llx, %llx\r\n", cur -> PGD, pa2va(cur -> PGD), &(cur -> PGD));

setup_peripheral_identity(pa2va(cur -> PGD));
for (int i = 0; i < cur -> code_size; i += 4096) {
map_page(pa2va(cur -> PGD), i, va2pa(cur -> code) + i, (1 << 6));
}
for (int i = 0; i < 4; i ++) {
map_page (pa2va(cur -> PGD), 0xffffffffb000L + i * 4096, va2pa(cur -> stack_start) + i * 4096, (1 << 6));
}
cur -> code = 0;
cur -> stack_start = 0xffffffffb000L;
void* sp = 0xfffffffff000L - 16;
void* el1_sp = cur -> sp;

long ttbr1 = read_sysreg(ttbr1_el1);
long ttbr0 = read_sysreg(ttbr0_el1);
uart_printf ("TTBR0: %llx, TTBR1: %llx\r\n", ttbr0, ttbr1);

uart_printf ("cur -> PGD %llx\r\n", cur -> PGD);

// uart_printf("Running code from %llx, which is %llx\n", cur -> code, trans(cur -> code));

asm volatile("dsb ish");
write_sysreg(ttbr0_el1, cur -> PGD);
asm volatile("tlbi vmalle1is");
asm volatile("dsb ish");
asm volatile("isb");


uart_printf ("uart printf: %llx\r\n", uart_printf);
while (1);

uart_printf("Running code from %llx, which is %llx\n", cur -> code, trans_el0(cur -> code));
ttbr1 = read_sysreg(ttbr1_el1);
ttbr0 = read_sysreg(ttbr0_el1);
uart_printf ("TTBR0: %llx, TTBR1: %llx, &TTBR0: %llx\r\n", ttbr0, ttbr1, &ttbr0);

asm volatile(
"mov x1, 0;"
"msr spsr_el1, x1;"
"mov x1, %[code];"
"mov x2, %[sp];"
"msr elr_el1, x1;"
"msr sp_el0, x2;"
"msr DAIFclr, 0xf;"
"mov sp, %[sp_el1];"
"eret;"
:
: [code] "r" (cur -> code), [sp] "r" (sp), [sp_el1] "r" (el1_sp)
: "x1", "x2", "sp"
);
}

char* get_cpio_end() {
char* cpio_start = _cpio_file;
struct cpio_newc_header* header = (struct cpio_newc_header*)cpio_start;
substr(buff, header -> c_magic, 0, 5);
while (same(buff, "070701")) {
uint64_t namesize = hex_to_bin(header->c_namesize);
uint64_t filesize = hex_to_bin(header->c_filesize);

char* filename = (char*)(header + 1);
char* filedata = filename + namesize;
filedata = (char*)((uintptr_t)filedata + ((4 - ((uintptr_t)filedata & 3)) & 3)); // Align to next 4-byte boundary

substr(buff, filename, 0, namesize - 1);
if (same(buff, "TRAILER!!!")) {
return filedata + filesize;
}

substr(buff, filename, 0, namesize - 1);

// Move to the next header, aligning as necessary
header = (struct cpio_newc_header*)(filedata + filesize);
header = (struct cpio_newc_header*)((uintptr_t)header + ((4 - ((uintptr_t)header & 3)) & 3)); // Align to next 4-byte boundary
substr(buff, header -> c_magic, 0, 5);
}
return header;
}
1 change: 1 addition & 0 deletions lab6/kernel/asm_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial null -serial stdio -dtb ../../bcm2710-rpi-3-b-plus.dtb -initrd ../../initramfs.cpio -display vnc=0.0.0.0:0 -vga std -d in_asm
3 changes: 3 additions & 0 deletions lab6/kernel/build/alloc_c.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/alloc_c.o: src/alloc.c include/alloc.h include/mini_uart.h \
include/fdt.h include/set.h include/mini_uart.h include/alloc.h \
include/cpio.h include/mmu.h
Binary file added lab6/kernel/build/alloc_c.o
Binary file not shown.
1 change: 1 addition & 0 deletions lab6/kernel/build/boot_s.d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/boot_s.o: src/boot.S include/mmu_regs.h
Binary file added lab6/kernel/build/boot_s.o
Binary file not shown.
3 changes: 3 additions & 0 deletions lab6/kernel/build/cpio_c.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/cpio_c.o: src/cpio.c include/cpio.h include/mini_uart.h \
include/helper.h include/alloc.h include/thread.h include/mmu.h \
include/utils.h
Binary file added lab6/kernel/build/cpio_c.o
Binary file not shown.
4 changes: 4 additions & 0 deletions lab6/kernel/build/exception_c.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build/exception_c.o: src/exception.c include/exception.h \
include/mini_uart.h include/utils.h include/mmio.h include/DEFINE.h \
include/timer.h include/task.h include/system_call.h include/exception.h \
include/thread.h
Binary file added lab6/kernel/build/exception_c.o
Binary file not shown.
2 changes: 2 additions & 0 deletions lab6/kernel/build/fdt_c.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/fdt_c.o: src/fdt.c include/fdt.h include/mini_uart.h \
include/utils.h include/helper.h
Binary file added lab6/kernel/build/fdt_c.o
Binary file not shown.
1 change: 1 addition & 0 deletions lab6/kernel/build/helper_c.d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/helper_c.o: src/helper.c include/mini_uart.h
Binary file added lab6/kernel/build/helper_c.o
Binary file not shown.
Binary file added lab6/kernel/build/kernel8.elf
Binary file not shown.
3 changes: 3 additions & 0 deletions lab6/kernel/build/kernel_start_c.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/kernel_start_c.o: src/kernel_start.c include/mini_uart.h \
include/shell.h include/fdt.h include/cpio.h include/alloc.h \
include/thread.h include/mmu.h
Binary file added lab6/kernel/build/kernel_start_c.o
Binary file not shown.
1 change: 1 addition & 0 deletions lab6/kernel/build/mail_c.d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/mail_c.o: src/mail.c include/mini_uart.h include/mail.h
Binary file added lab6/kernel/build/mail_c.o
Binary file not shown.
2 changes: 2 additions & 0 deletions lab6/kernel/build/mini_uart_c.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/mini_uart_c.o: src/mini_uart.c include/utils.h include/helper.h \
include/mmio.h include/DEFINE.h
Binary file added lab6/kernel/build/mini_uart_c.o
Binary file not shown.
1 change: 1 addition & 0 deletions lab6/kernel/build/mm_s.d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/mm_s.o: src/mm.S
Binary file added lab6/kernel/build/mm_s.o
Binary file not shown.
1 change: 1 addition & 0 deletions lab6/kernel/build/mmio_c.d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/mmio_c.o: src/mmio.c include/mmio.h
Binary file added lab6/kernel/build/mmio_c.o
Binary file not shown.
2 changes: 2 additions & 0 deletions lab6/kernel/build/mmu_c.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/mmu_c.o: src/mmu.c include/mmu.h include/helper.h include/utils.h \
include/alloc.h include/thread.h include/mmu_regs.h include/mini_uart.h
Binary file added lab6/kernel/build/mmu_c.o
Binary file not shown.
5 changes: 5 additions & 0 deletions lab6/kernel/build/shell_c.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build/shell_c.o: src/shell.c include/shell.h include/mini_uart.h \
include/reboot.h include/mail.h include/helper.h include/loader.h \
include/cpio.h include/alloc.h include/fdt.h include/timer.h \
include/exception.h include/thread.h include/system_call.h \
include/exception.h include/signal.h include/mmu.h
Binary file added lab6/kernel/build/shell_c.o
Binary file not shown.
1 change: 1 addition & 0 deletions lab6/kernel/build/signal_c.d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/signal_c.o: src/signal.c include/thread.h include/signal.h
Binary file added lab6/kernel/build/signal_c.o
Binary file not shown.
4 changes: 4 additions & 0 deletions lab6/kernel/build/system_call_c.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build/system_call_c.o: src/system_call.c include/system_call.h \
include/exception.h include/alloc.h include/thread.h include/mail.h \
include/cpio.h include/mini_uart.h include/helper.h include/exception.h \
include/utils.h include/mmu.h
Binary file added lab6/kernel/build/system_call_c.o
Binary file not shown.
1 change: 1 addition & 0 deletions lab6/kernel/build/system_call_s.d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/system_call_s.o: src/system_call.S
Binary file added lab6/kernel/build/system_call_s.o
Binary file not shown.
2 changes: 2 additions & 0 deletions lab6/kernel/build/task_c.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/task_c.o: src/task.c include/task.h include/alloc.h \
include/mini_uart.h include/utils.h
Binary file added lab6/kernel/build/task_c.o
Binary file not shown.
3 changes: 3 additions & 0 deletions lab6/kernel/build/thread_c.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/thread_c.o: src/thread.c include/thread.h include/alloc.h \
include/mini_uart.h include/helper.h include/exception.h \
include/signal.h include/mmu.h include/utils.h
Binary file added lab6/kernel/build/thread_c.o
Binary file not shown.
2 changes: 2 additions & 0 deletions lab6/kernel/build/timer_c.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/timer_c.o: src/timer.c include/timer.h include/alloc.h \
include/mini_uart.h
Binary file added lab6/kernel/build/timer_c.o
Binary file not shown.
1 change: 1 addition & 0 deletions lab6/kernel/build/utils_s.d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/utils_s.o: src/utils.S
Binary file added lab6/kernel/build/utils_s.o
Binary file not shown.
58 changes: 58 additions & 0 deletions lab6/kernel/include/DEFINE.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#define MMIO_BASE 0xffff00003F000000
#define PBASE MMIO_BASE

#define AUX_IRQ ((volatile unsigned int*)(MMIO_BASE + 0x00215000))
// Auxiliary enables
#define AUX_ENABLE ((volatile unsigned int*)(MMIO_BASE + 0x00215004))
// Mini Uart I/O Data
#define AUX_MU_IO_REG ((volatile unsigned int*)(MMIO_BASE + 0x00215040))
// Mini Uart Interrupt Enable
#define AUX_MU_IER_REG ((volatile unsigned int*)(MMIO_BASE + 0x00215044))
// Mini Uart Interrupt Identity
#define AUX_MU_IIR_REG ((volatile unsigned int*)(MMIO_BASE + 0x00215048))
// Mini Uart Line Control
#define AUX_MU_LCR_REG ((volatile unsigned int*)(MMIO_BASE + 0x0021504C))
// Mini Uart Modem Control
#define AUX_MU_MCR_REG ((volatile unsigned int*)(MMIO_BASE + 0x00215050))
// Mini Uart Line Status
#define AUX_MU_LSR_REG ((volatile unsigned int*)(MMIO_BASE + 0x00215054))
// Mini Uart Modem Status
#define AUX_MU_MSR_REG ((volatile unsigned int*)(MMIO_BASE + 0x00215058))
// Mini Uart Scratch
#define AUX_MU_SCRATCH ((volatile unsigned int*)(MMIO_BASE + 0x0021505C))
// Mini Uart Scratch
#define AUX_MU_CNTL_REG ((volatile unsigned int*)(MMIO_BASE + 0x00215060))
// Mini Uart Extra Status
#define AUX_MU_STAT_REG ((volatile unsigned int*)(MMIO_BASE + 0x00215064))
// Mini Uart Baudrate
#define AUX_MU_BAUD_REG ((volatile unsigned int*)(MMIO_BASE + 0x00215068))

#define IRQ_basic_pending ((volatile unsigned int*)(MMIO_BASE + 0x0000B200))
#define IRQ_pending_1 ((volatile unsigned int*)(MMIO_BASE + 0x0000B204))
#define IRQ_pending_2 ((volatile unsigned int*)(MMIO_BASE + 0x0000B208))
#define FIQ_control ((volatile unsigned int*)(MMIO_BASE + 0x0000B20C))
#define Enable_IRQs_1 ((volatile unsigned int*)(MMIO_BASE + 0x0000B210))
#define Enable_IRQs_2 ((volatile unsigned int*)(MMIO_BASE + 0x0000B214))
#define Enable_Basic_IRQs ((volatile unsigned int*)(MMIO_BASE + 0x0000B218))
#define Disable_IRQs_1 ((volatile unsigned int*)(MMIO_BASE + 0x0000B21C))
#define Disable_IRQs_2 ((volatile unsigned int*)(MMIO_BASE + 0x0000B220))
#define Disable_Basic_IRQs ((volatile unsigned int*)(MMIO_BASE + 0x0000B224))
#define CORE0_INT_SRC (volatile unsigned int*)(0xffff000040000060)
#define CORE0_TIMER_IRQ_CTRL (volatile unsigned int*)(0xffff000040000040)

#define GPFSEL1 (PBASE+0x00200004)
#define GPSET0 (PBASE+0x0020001C)
#define GPCLR0 (PBASE+0x00200028)
#define GPPUD (PBASE+0x00200094)
#define GPPUDCLK0 (PBASE+0x00200098)

#define UART_DR ( (volatile unsigned int *) ( MMIO_BASE + 0x00201000 ) ) /* Data Register */
#define UART_FR ( (volatile unsigned int *) ( MMIO_BASE + 0x00201018 ) ) /* Flag register */
#define UART_IBRD ( (volatile unsigned int *) ( MMIO_BASE + 0x00201024 ) ) /* Integer Baud Rate Divisor */
#define UART_FBRD ( (volatile unsigned int *) ( MMIO_BASE + 0x00201028 ) ) /* RFractional Baud Rate Divisor */
#define UART_LCRH ( (volatile unsigned int *) ( MMIO_BASE + 0x0020102C ) ) /* Line Control Register */
#define UART_CR ( (volatile unsigned int *) ( MMIO_BASE + 0x00201030 ) ) /* Control Register */
#define UART_RIS ( (volatile unsigned int *) ( MMIO_BASE + 0x0020103C ) )
#define UART_IMSC ( (volatile unsigned int *) ( MMIO_BASE + 0x00201038 ) ) /* Interupt FIFO Level Select Register */
#define UART_MIS ( (volatile unsigned int *) ( MMIO_BASE + 0x00201040 ) )
#define UART_ICR ( (volatile unsigned int *) ( MMIO_BASE + 0x00201044 ) ) /* Interupt Clear Register */
Loading