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 lab6/bcm2710-rpi-3-b-plus.dtb
Binary file not shown.
3 changes: 3 additions & 0 deletions lab6/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 0x8000000
27 changes: 27 additions & 0 deletions lab6/create_cpio.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh

# Make rootfs folder
# Select all files and put into archives
# '-o, --create' Run in copy-out mode
# '-H FORMAT' archive format,
# newc: SVR4 portable format

mkdir -p rootfs

cat <<EOL > rootfs/Hello
Hello
EOL

cat <<EOL > rootfs/World
W
o
r
l
d
EOL

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

rm -rf rootfs
Binary file added lab6/create_fs/FromTA/initramfs.cpio
Binary file not shown.
11 changes: 11 additions & 0 deletions lab6/create_fs/create_cpio.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

# Go to rootfs folder
# Select all files and put into archives
# '-o, --create' Run in copy-out mode
# '-H FORMAT' archive format,
# newc: SVR4 portable format

cd rootfs
find . | cpio -o -H newc > ../initramfs.cpio
cd ..
Binary file added lab6/create_fs/initramfs.cpio
Binary file not shown.
5 changes: 5 additions & 0 deletions lab6/create_fs/lab3_user_proc/linker.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SECTIONS
{
. = 0x80000;
.text : { *(.text) }
}
23 changes: 23 additions & 0 deletions lab6/create_fs/lab3_user_proc/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ARMGNU ?= aarch64-linux-gnu

BUILD_DIR = build
#---------------------------------------------------------------------------------------

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

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

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

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

all: user_proc.img

clean:
rm -rf $(BUILD_DIR) *.img
12 changes: 12 additions & 0 deletions lab6/create_fs/lab3_user_proc/user_proc.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Run `svc 0` 5 times; svc (el0 -> el1) system calls
.section ".text"
.global _start
_start:
mov x0, 0
1:
add x0, x0, 1
svc 0
cmp x0, 5
blt 1b
1:
b 1b
28 changes: 28 additions & 0 deletions lab6/create_fs/lab5_user_fork/fork_test.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
void fork_test(){
printf("\nFork Test, pid %d\n", get_pid());
int cnt = 1;
int ret = 0;
if ((ret = fork()) == 0) { // child
long long cur_sp;
asm volatile("mov %0, sp" : "=r"(cur_sp));
printf("first child pid: %d, cnt: %d, ptr: %x, sp : %x\n", get_pid(), cnt, &cnt, cur_sp);
++cnt;

if ((ret = fork()) != 0){
asm volatile("mov %0, sp" : "=r"(cur_sp));
printf("first child pid: %d, cnt: %d, ptr: %x, sp : %x\n", get_pid(), cnt, &cnt, cur_sp);
}
else{
while (cnt < 5) {
asm volatile("mov %0, sp" : "=r"(cur_sp));
printf("second child pid: %d, cnt: %d, ptr: %x, sp : %x\n", get_pid(), cnt, &cnt, cur_sp);
delay(1000000);
++cnt;
}
}
exit();
}
else {
printf("parent here, pid %d, child %d\n", get_pid(), ret);
}
}
Binary file not shown.
23 changes: 23 additions & 0 deletions lab6/create_fs/lab5_user_fork/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ARMGNU ?= aarch64-linux-gnu

BUILD_DIR = build
#---------------------------------------------------------------------------------------

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

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

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

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

all: fork_test.img

clean:
rm -rf $(BUILD_DIR) *.img
2 changes: 2 additions & 0 deletions lab6/create_fs/rootfs/file0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[This is file 0]
[Hello World!]
11 changes: 11 additions & 0 deletions lab6/create_fs/rootfs/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{this is file1}
.
.F
.I
.L
.E
.
.1
.


Binary file added lab6/initramfs.cpio
Binary file not shown.
89 changes: 89 additions & 0 deletions lab6/kernel/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
CC := aarch64-linux-gnu-gcc
LD := aarch64-linux-gnu-ld
OBJCOPY := aarch64-linux-gnu-objcopy
GDB := gdb-multiarch
QEMU := qemu-system-aarch64
KILL_QEMU := killall $(QEMU)

COPS = -Wall -nostdlib -nostartfiles -ffreestanding -Iinclude -mgeneral-regs-only $(cops)
ASMOPS = -Iinclude
QOPS = -M raspi3b -kernel $(NAME).img -dtb ../bcm2710-rpi-3-b-plus.dtb -initrd ../initramfs.cpio
GDBOPS = --init-eval-command="file $(BUILD_DIR)/$(NAME).elf" --init-eval-command="target remote :1234"

RED=\033[0;31m
GREEN=\033[0;32m
YELLOW=\033[0;33m
BLUE=\033[0;34m
PURPLE=\033[0;35m
CYAN=\033[0;36m
RESET=\033[0m

# Set the TARGET_PLATFORM and _DEBUG macro to be passed to the GCC definition later.
ifdef DEBUG
COPS+=-D_DEBUG=$(DEBUG)
endif

ifneq (,$(findstring pi,$(MAKECMDGOALS)))
COPS+=-DRPI
FINISH_STR = "\r\n-------------------------------------------------------------\r\n The $(CYAN)kernel8.img$(RESET) intended for the $(BLUE)Raspberry Pi 3B+$(RESET) is ready.\r\n-------------------------------------------------------------"
else # Default to QEMU
COPS += -DQEMU
FINISH_STR = "\r\n-------------------------------------------------\r\n The $(CYAN)kernel8.img$(RESET) intended for the $(BLUE)QEMU$(RESET) is ready.\r\n-------------------------------------------------"
endif

NAME = kernel8
BUILD_DIR = build
SRC_DIR = src
LK_SCRIPT = linker.ld
SESSION_NAME := my-os

.PHONY: all
all : $(NAME).img

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

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

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

$(NAME).img: $(SRC_DIR)/$(LK_SCRIPT) $(OBJ_FILES)
$(LD) -T $(SRC_DIR)/$(LK_SCRIPT) -o $(BUILD_DIR)/$(NAME).elf $(OBJ_FILES)
$(OBJCOPY) $(BUILD_DIR)/$(NAME).elf -O binary $(NAME).img
@echo -e $(FINISH_STR)

#This target compiles and runs the kernel.img of the QEMU platform by default.
# You can use "make pi run" to run this image in QEMU.
.PHONY: run
run: clean $(NAME).img
$(QEMU) $(QOPS) -serial null -serial stdio

# This target compiles kernel.img to run on a QEMU.
.PHONY: qemu
qemu: clean $(NAME).img

# This target compiles kernel.img to run on a Raspberry 3B.
.PHONY: pi
pi: clean $(NAME).img

# This target will run kernel in QEMU with GDB attached in a tmux session.
.PHONY: debug
debug: clean $(NAME).img
tmux new-session -d -s $(SESSION_NAME) '$(QEMU) $(QOPS) -S -s -serial null -serial stdio' \; split-window -h '$(GDB) $(GDBOPS)' \; attach -t $(SESSION_NAME)

# This target will kill the QEMU session.
.PHONY: kill
kill:
$(KILL_QEMU)
7 changes: 7 additions & 0 deletions lab6/kernel/include/bcm2837/rpi_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef _RPI_BASE_H_
#define _RPI_BASE_H_

#include "bcm2837/rpi_mmu.h"
#define PERIPHERAL_BASE PHYS_TO_KERNEL_VIRT(0x3F000000)

#endif /*_RPI_BASE_H_ */
25 changes: 25 additions & 0 deletions lab6/kernel/include/bcm2837/rpi_gpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef _RPI_GPIO_H_
#define _RPI_GPIO_H_

#include "bcm2837/rpi_base.h"

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

#endif /*_RPI_GPIO_H_*/
24 changes: 24 additions & 0 deletions lab6/kernel/include/bcm2837/rpi_irq.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef _RPI_IRQ_H_
#define _RPI_IRQ_H_

#include "bcm2837/rpi_base.h"

/*
The basic pending register shows which interrupt are pending. To speed up interrupts processing, a
number of 'normal' interrupt status bits have been added to this register. This makes the 'IRQ
pending base' register different from the other 'base' interrupt registers
p112-115 https://cs140e.sergio.bz/docs/BCM2837-ARM-Peripherals.pdf
*/

#define IRQ_BASIC_PENDING ((volatile unsigned int*)(PERIPHERAL_BASE+0x0000B200))
#define IRQ_PENDING_1 ((volatile unsigned int*)(PERIPHERAL_BASE+0x0000B204))
#define IRQ_PENDING_2 ((volatile unsigned int*)(PERIPHERAL_BASE+0x0000B208))
#define FIQ_CONTROL ((volatile unsigned int*)(PERIPHERAL_BASE+0x0000B20C))
#define ENABLE_IRQS_1 ((volatile unsigned int*)(PERIPHERAL_BASE+0x0000B210))
#define ENABLE_IRQS_2 ((volatile unsigned int*)(PERIPHERAL_BASE+0x0000B214))
#define ENABLE_BASIC_IRQS ((volatile unsigned int*)(PERIPHERAL_BASE+0x0000B218))
#define DISABLE_IRQS_1 ((volatile unsigned int*)(PERIPHERAL_BASE+0x0000B21C))
#define DISABLE_IRQS_2 ((volatile unsigned int*)(PERIPHERAL_BASE+0x0000B220))
#define DISABLE_BASIC_IRQS ((volatile unsigned int*)(PERIPHERAL_BASE+0x0000B224))

#endif /*_RPI_IRQ_H_*/
17 changes: 17 additions & 0 deletions lab6/kernel/include/bcm2837/rpi_mbox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef _RPI_MBOX_H_
#define _RPI_MBOX_H_

#include "bcm2837/rpi_base.h"

#define MBOX_BASE (PERIPHERAL_BASE+0x0000B880)

// The register access to a mailbox
// https://jsandler18.github.io/extra/mailbox.html
#define MBOX_READ ((volatile unsigned int*)(MBOX_BASE+0x00))
#define MBOX_POLL ((volatile unsigned int*)(MBOX_BASE+0x10))
#define MBOX_SENDER ((volatile unsigned int*)(MBOX_BASE+0x14))
#define MBOX_STATUS ((volatile unsigned int*)(MBOX_BASE+0x18))
#define MBOX_CONFIG ((volatile unsigned int*)(MBOX_BASE+0x1C))
#define MBOX_WRITE ((volatile unsigned int*)(MBOX_BASE+0x20))

#endif /*_RPI_MBOX_H_ */
18 changes: 18 additions & 0 deletions lab6/kernel/include/bcm2837/rpi_mmu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef _RPI_MMU_H_
#define _RPI_MMU_H_

#define PHYS_TO_VIRT(x) (x + 0xffff000000000000)
#define VIRT_TO_PHYS(x) (x - 0xffff000000000000)
#define ENTRY_ADDR_MASK 0x0000fffffffff000L

#define PHYS_TO_KERNEL_VIRT(x) (((unsigned long)(x)) | 0xffff000000000000)
#define KERNEL_VIRT_TO_PHYS(x) (((unsigned long)(x)) & ~0xffff000000000000)

// e.g. size=0x13200, alignment=0x1000 -> 0x14000
#define ALIGN_UP(size, alignment) (((size) + ((alignment) - 1)) & ~((alignment) - 1))
// e.g. size=0x13200, alignment=0x1000 -> 0x13000
#define ALIGN_DOWN(size, alignment) ((size) & ~((alignment) - 1))

#define IS_NOT_ALIGN(ptr, alignment) (((unsigned long)ptr & ((alignment) - 1)) != 0)

#endif /*_RPI_MMU_H_ */
19 changes: 19 additions & 0 deletions lab6/kernel/include/bcm2837/rpi_uart1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef _RPI_UART1_H_
#define _RPI_UART1_H_

#include "bcm2837/rpi_base.h"

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

#endif /*_RPI_UART1_H_ */
Loading