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

311551158 lab2 #256

Open
wants to merge 2 commits into
base: 311551158
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
File renamed without changes.
3 changes: 3 additions & 0 deletions Lab1/kernel code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| Github Account | Student ID | Name |
|----------------|------------|---------------|
|Hsu0208 | 311551158 | Wei Ling Hsu |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion start.S → Lab1/kernel code/start.S
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ _start:
cbz x1, 2f
// cpu id > 0, stop
1: wfe
b 1b
b 1b // b uncinditional branch 1-label b- backward
2: // cpu id == 0

// set stack before our code
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions Lab2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ARMGNU = aarch64-linux-gnu

CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles
ASMFLAGS = -Iinclude
QEMUFLAGS = -M raspi3b -display none -serial null -serial pty

BUILD_DIR = build
SRC_DIR = src

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

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

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

all: clean kernel8.img

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

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

qemu: kernel8.img
qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial null -serial stdio -display none -initrd initramfs.cpio

25 changes: 25 additions & 0 deletions Lab2/bash_send_kernel.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

## rapspi -> bash send_kernel.sh
## qemu -> bash send_kernel.sh i

## Construct the device path
if [ $# -eq 0 ]; then
device="/dev/ttyUSB0"
else
index="$1"
device="/dev/pts/$index"
fi

# Check if the device file exists
if [ ! -c "$device" ]; then
echo "Device not found: $device"
exit 1
fi

# Change permissions of the device file
echo chmod a+rw "$device"
sudo chmod a+rw "$device"

# Execute send_kernel.py with the appropriate argument
python3 send_kernel.py $1
33 changes: 33 additions & 0 deletions Lab2/bootloader/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
ARMGNU = aarch64-linux-gnu

CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles
ASMFLAGS = -Iinclude
QEMUFLAGS = -M raspi3b -display none -serial null -serial pty

BUILD_DIR = build
SRC_DIR = src

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

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

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

all: bootloader.img

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

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

qemu: bootloader.img
qemu-system-aarch64 $(QEMUFLAGS) -kernel bootloader.img -initrd ../initramfs.cpio
25 changes: 25 additions & 0 deletions Lab2/bootloader/include/gpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef GPIO_H
#define GPIO_H

#define MMIO_BASE 0x3F000000

#define GPFSEL0 ((volatile unsigned int*)(MMIO_BASE+0x00200000))
#define GPFSEL1 ((volatile unsigned int*)(MMIO_BASE+0x00200004))
#define GPFSEL2 ((volatile unsigned int*)(MMIO_BASE+0x00200008))
#define GPFSEL3 ((volatile unsigned int*)(MMIO_BASE+0x0020000C))
#define GPFSEL4 ((volatile unsigned int*)(MMIO_BASE+0x00200010))
#define GPFSEL5 ((volatile unsigned int*)(MMIO_BASE+0x00200014))
#define GPSET0 ((volatile unsigned int*)(MMIO_BASE+0x0020001C))
#define GPSET1 ((volatile unsigned int*)(MMIO_BASE+0x00200020))
#define GPCLR0 ((volatile unsigned int*)(MMIO_BASE+0x00200028))
#define GPLEV0 ((volatile unsigned int*)(MMIO_BASE+0x00200034))
#define GPLEV1 ((volatile unsigned int*)(MMIO_BASE+0x00200038))
#define GPEDS0 ((volatile unsigned int*)(MMIO_BASE+0x00200040))
#define GPEDS1 ((volatile unsigned int*)(MMIO_BASE+0x00200044))
#define GPHEN0 ((volatile unsigned int*)(MMIO_BASE+0x00200064))
#define GPHEN1 ((volatile unsigned int*)(MMIO_BASE+0x00200068))
#define GPPUD ((volatile unsigned int*)(MMIO_BASE+0x00200094))
#define GPPUDCLK0 ((volatile unsigned int*)(MMIO_BASE+0x00200098))
#define GPPUDCLK1 ((volatile unsigned int*)(MMIO_BASE+0x0020009C))

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

typedef signed char int8_t;
typedef short int16_t;
typedef int int32_t;
typedef long long int64_t;

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

typedef unsigned int size_t;

#endif /* _STDINT_H */
41 changes: 41 additions & 0 deletions Lab2/bootloader/include/uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "../include/stdint.h"

#ifndef UART_H
#define UART_H

/* Auxilary mini UART registers */
#define AUX_ENABLE ((volatile unsigned int*)(MMIO_BASE+0x00215004))
#define AUX_MU_IO ((volatile unsigned int*)(MMIO_BASE+0x00215040))
#define AUX_MU_IER ((volatile unsigned int*)(MMIO_BASE+0x00215044))
#define AUX_MU_IIR ((volatile unsigned int*)(MMIO_BASE+0x00215048))
#define AUX_MU_LCR ((volatile unsigned int*)(MMIO_BASE+0x0021504C))
#define AUX_MU_MCR ((volatile unsigned int*)(MMIO_BASE+0x00215050))
#define AUX_MU_LSR ((volatile unsigned int*)(MMIO_BASE+0x00215054))
#define AUX_MU_MSR ((volatile unsigned int*)(MMIO_BASE+0x00215058))
#define AUX_MU_SCRATCH ((volatile unsigned int*)(MMIO_BASE+0x0021505C))
#define AUX_MU_CNTL ((volatile unsigned int*)(MMIO_BASE+0x00215060))
#define AUX_MU_STAT ((volatile unsigned int*)(MMIO_BASE+0x00215064))
#define AUX_MU_BAUD ((volatile unsigned int*)(MMIO_BASE+0x00215068))


/**
* Set baud rate and characteristics (115200 8N1) and map to GPIO
*/
void uart_init();


/**
* Receive a character
*/
char uart_getc();

/**
* Display a string
*/
void uart_puts(char *s);


char uart_recv();

void itos(uint32_t value, char *str);
#endif
21 changes: 21 additions & 0 deletions Lab2/bootloader/src/link.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
SECTIONS
{
. = 0x60000;
__bootloader_start = .;
.text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) }
.rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) }
.data : { *(.data .data.* .gnu.linkonce.d*) }
.bss (NOLOAD) : {
. = ALIGN(16);
__bss_start = .;
*(.bss .bss.*)
*(COMMON)
__bss_end = .;
}
_end = .;
__bootloader_end = .;

/DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) }
}
__bss_size = (__bss_end - __bss_start)>>3;
__bootloader_size = (__bootloader_end - __bootloader_start) >> 3;
58 changes: 58 additions & 0 deletions Lab2/bootloader/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "../include/uart.h"
#include "../include/stdint.h"

void main()
{
// set up serial console
uart_init();
uart_puts("bootloader is coming~\n");

uint32_t file_size = 0;
size_t index = 0;
// Buffer to store received data
uint8_t byte;

while(1) {
if(uart_recv() == ((uint8_t)77)) break;
}
uart_puts("ready for receiving kernel size.\n");


// Receive the file size as a 4-byte integer in little-endian format
for (int i = 0; i < 4; i++) {
byte = uart_recv();
file_size |= ((uint32_t)byte) << (8 * i);
}

char file_size_str[32]; // Adjust the buffer size as needed
itos(file_size, file_size_str);

uart_puts("kernel size :");
uart_puts(file_size_str);
uart_puts(" bytes.\n");

while(1) {
uart_puts("ready for receiving kernel.\n");
if(uart_recv() == ((uint8_t)77)) break;
}


char *kernel = (char *)0x80000;
while (index < file_size) {
// Receive a byte
byte = uart_recv();
// Write the byte to memory
*(kernel + index) = byte;
// Increment the index
index++;
}

uart_puts("finished loading kernel~\n");


// restore arguments and jump to the new kernel.
asm volatile (
"mov x30, 0x80000;" // Move the immediate value 0x80000 to the link register (x30)
"ret;" // Return from subroutine (this will jump to the address in x30)
);
}
40 changes: 40 additions & 0 deletions Lab2/bootloader/src/start.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.section ".text.boot"

.global _start

_start:
/* relocate bootloader */
ldr x1, =0x80000 //address for relocation.
ldr x2, =__bootloader_start
ldr w3, =__bootloader_size

relocate:
ldr x4, [x1], #8
str x4, [x2], #8
sub w3, w3, #1
cbnz w3, relocate

/* set stack pointer */
ldr x1, =_start
mov sp, x1


bss_init:
// clear bss
ldr x1, =__bss_start
ldr w2, =__bss_size

clear_bss_memory:
cbz w2, jump_to_main
str xzr, [x1], #8
sub w2, w2, #1
cbnz w2, clear_bss_memory

// jump to C code, should not return
jump_to_main:
bl main-0x20000
// for failsafe, halt this core too
b halt

halt:
b halt
Loading