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

311552045 lab3 #91

Open
wants to merge 22 commits into
base: 311552045
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
34 changes: 32 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,36 @@
"utility": "c",
"typeinfo": "c",
"stdlib.h": "c",
"mini_uart.h": "c"
}
"mini_uart.h": "c",
"gpio.h": "c",
"base.h": "c",
"stddef.h": "c",
"deque": "c",
"forward_list": "c",
"list": "c",
"vector": "c",
"math.h": "c",
"functional": "c",
"istream": "c",
"ostream": "c",
"tuple": "c",
"type_traits": "c",
"dynamic_alloc.h": "c",
"page_alloc.h": "c",
"stdint.h": "c",
"device_tree.h": "c",
"list.h": "c",
"thread.h": "c",
"signal.h": "c",
"limits": "c",
"mbox.h": "c",
"virtual_mem.h": "c",
"string.h": "c",
"tmpfs.h": "c",
"vfs.h": "c",
"string": "c",
"fat32.h": "c",
"random": "c"
},
"C_Cpp.errorSquiggles": "enabled"
}
58 changes: 58 additions & 0 deletions Lab3/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
ARMGNU ?= aarch64-linux-gnu

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

BUILD_DIR = build
SRC_DIR = src

all : kernel8.img bootloader.img userprogram.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
@mkdir -p $(@D)
$(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)/kernel/link.ld $(OBJ_FILES)
$(ARMGNU)-ld -T $(SRC_DIR)/kernel/link.ld -o $(BUILD_DIR)/kernel/kernel8.elf $(filter $(BUILD_DIR)/kernel/%_c.o $(BUILD_DIR)/kernel/%_s.o $(BUILD_DIR)/lib/%_c.o $(BUILD_DIR)/lib/%_s.o, $(OBJ_FILES))
$(ARMGNU)-objcopy $(BUILD_DIR)/kernel/kernel8.elf -O binary kernel8.img

run:
qemu-system-aarch64 -M raspi3 -kernel kernel8.img -serial null -serial stdio -display none -initrd initramfs.cpio -dtb bcm2710-rpi-3-b-plus.dtb -d int

debug:
qemu-system-aarch64 -M raspi3 -kernel kernel8.img -serial null -serial stdio -display none -s -S -initrd initramfs.cpio -dtb bcm2710-rpi-3-b-plus.dtb -d int

bootloader.img: $(SRC_DIR)/bootloader/link.ld $(OBJ_FILES)
$(ARMGNU)-ld -T $(SRC_DIR)/bootloader/link.ld -o $(BUILD_DIR)/bootloader/bootloader.elf $(filter $(BUILD_DIR)/bootloader/%_c.o $(BUILD_DIR)/bootloader/%_s.o $(BUILD_DIR)/lib/%_c.o $(BUILD_DIR)/lib/%_s.o, $(OBJ_FILES))
$(ARMGNU)-objcopy -O binary $(BUILD_DIR)/bootloader/bootloader.elf bootloader.img

userprogram.img: $(SRC_DIR)/userprogram/link.ld $(OBJ_FILES)
$(ARMGNU)-ld -T $(SRC_DIR)/userprogram/link.ld -o $(BUILD_DIR)/userprogram/userprogram.elf $(filter $(BUILD_DIR)/userprogram/%_c.o $(BUILD_DIR)/userprogram/%_s.o $(BUILD_DIR)/lib/%_c.o $(BUILD_DIR)/lib/%_s.o, $(OBJ_FILES))
$(ARMGNU)-objcopy -O binary $(BUILD_DIR)/userprogram/userprogram.elf userprogram.img

test:
@echo Hello

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

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

no_int_qemu:
qemu-system-aarch64 -M raspi3 -kernel kernel8.img -serial null -serial stdio -display none -initrd initramfs.cpio -dtb bcm2710-rpi-3-b-plus.dtb
Binary file added Lab3/bcm2710-rpi-3-b-plus.dtb
Binary file not shown.
Binary file added Lab3/bootloader.img
Binary file not shown.
25 changes: 25 additions & 0 deletions Lab3/bootloader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
import time

file_size = os.path.getsize('kernel8.img')
print("File Size is :", file_size, "bytes")

# with open('/dev/pts/35', "wb", buffering=0) as tty:
with open('/dev/ttyUSB0', "wb", buffering=0) as tty:
# send Start
tty.write(b"Start")
time.sleep(1)

# send kernel size
tty.write(file_size.to_bytes(4, 'little'))
time.sleep(1)

# send kernel
with open('kernel8.img', 'rb') as kernel_file:
while True:
data = kernel_file.read()
if not data:
break
tty.write(data)
time.sleep(1)

9 changes: 9 additions & 0 deletions Lab3/debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
qemu-system-aarch64 -M raspi3 -kernel kernel8.img -serial null -serial stdio -display none -initrd initramfs.cpio -dtb bcm2710-rpi-3-b-plus.dtb -d int
Board model: 00000000
Board revision: 00A02082
Serial number: 0000000000000000
ARM memory base address: 00000000
ARM memory size: 3C000000
initramfs_addr at 0000000008000000
Starting shell...
#
Expand Down
9 changes: 9 additions & 0 deletions Lab3/include/device_tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _DEVICE_TREE_H
#define _DEVICE_TREE_H

typedef int (*fdt_callback)(void *);
int initramfs_callback(void *dtb);
int dtb_parser(void *dtb);
void fdt_traverse(fdt_callback cb, char *dtb);

#endif /*_DEVICE_TREE_H */
7 changes: 7 additions & 0 deletions Lab3/include/load_kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef _LOAD_KERNEL_H
#define _LOAD_KERNEL_H

void load_kernel(char *dest);
void relocate(char *from_dest, char *to_dest);

#endif /*_LOAD_KERNEL_H */
6 changes: 6 additions & 0 deletions Lab3/include/mbox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef _MBOX_H
#define _MBOX_H

void mbox_main();

#endif /*_MBOX_H */
9 changes: 9 additions & 0 deletions Lab3/include/mbox_call.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _MBOX_CALL_H
#define _MBOX_CALL_H

/* a properly aligned buffer */
extern volatile unsigned int mbox[36];

int mbox_call(unsigned char ch);

#endif /*_MBOX_CALL_H */
17 changes: 17 additions & 0 deletions Lab3/include/mini_uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef _MINI_UART_H
#define _MINI_UART_H

void uart_init(void);
char uart_recv(void);
void uart_send(char c);
void uart_send_string(char *str);
void uart_send_string_of_size(char *str, int size);
void uart_hex(unsigned int d);
void uart_send_space(int size);

void asyn_read();
void asyn_write();
void uart_rx_handler();
void uart_tx_handler();

#endif /*_MINI_UART_H */
19 changes: 19 additions & 0 deletions Lab3/include/mm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef _MM_H
#define _MM_H

#define PAGE_SHIFT 12
#define TABLE_SHIFT 9
#define SECTION_SHIFT (PAGE_SHIFT + TABLE_SHIFT)

#define PAGE_SIZE (1 << PAGE_SHIFT)
#define SECTION_SIZE (1 << SECTION_SHIFT)

#define LOW_MEMORY (2 * SECTION_SIZE)

#ifndef __ASSEMBLER__

void memzero(unsigned long src, unsigned long n);

#endif

#endif /*_MM_H */
6 changes: 6 additions & 0 deletions Lab3/include/peripherals/base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef _P_BASE_H
#define _P_BASE_H

#define PBASE 0x3F000000

#endif /*_P_BASE_H */
16 changes: 16 additions & 0 deletions Lab3/include/peripherals/device_tree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef _P_DEVICE_TREE_H
#define _P_DEVICE_TREE_H

#define FDT_MAGIC 0xD00DFEED
#define FDT_VERSION 0x00000011
#define FDT_TK_NULL 0X00000000

#define FDT_BEGIN_NODE 0X00000001
#define FDT_END_NODE 0X00000002
#define FDT_PROP 0X00000003
#define FDT_NOP 0X00000004
#define FDT_END 0X00000009

#define FDT_CPIO_INITRAMFS_PROPNAME "linux,initrd-start"

#endif /*_P_DEVICE_TREE_H */
25 changes: 25 additions & 0 deletions Lab3/include/peripherals/gpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef _P_GPIO_H
#define _P_GPIO_H

#include "peripherals/base.h"

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

#endif /*_P_GPIO_H */
27 changes: 27 additions & 0 deletions Lab3/include/peripherals/irq.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef _P_IRQ_H
#define _P_IRQ_H

#include "peripherals/base.h"

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

#define SYSTEM_TIMER_IRQ_0 (1 << 0)
#define SYSTEM_TIMER_IRQ_1 (1 << 1)
#define SYSTEM_TIMER_IRQ_2 (1 << 2)
#define SYSTEM_TIMER_IRQ_3 (1 << 3)

#define CORE0_INTR_SRC 0x40000060
#define CORE1_INTR_SRC 0x40000064
#define CORE2_INTR_SRC 0x40000068
#define CORE3_INTR_SRC 0x4000006C

#endif /*_P_IRQ_H */
40 changes: 40 additions & 0 deletions Lab3/include/peripherals/mbox_call.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef _P_MBOX_CALL_H
#define _P_MBOX_CALL_H

#include "peripherals/base.h"

#define VIDEOCORE_MBOX (PBASE + 0x0000B880)
#define MBOX_READ (VIDEOCORE_MBOX + 0x0)
#define MBOX_POLL (VIDEOCORE_MBOX + 0x10)
#define MBOX_SENDER (VIDEOCORE_MBOX + 0x14)
#define MBOX_STATUS (VIDEOCORE_MBOX + 0x18)
#define MBOX_CONFIG (VIDEOCORE_MBOX + 0x1C)
#define MBOX_WRITE (VIDEOCORE_MBOX + 0x20)
#define MBOX_RESPONSE 0x80000000
#define MBOX_FULL 0x80000000
#define MBOX_EMPTY 0x40000000

#define MBOX_REQUEST 0

/* channels */
#define MBOX_CH_POWER 0
#define MBOX_CH_FB 1
#define MBOX_CH_VUART 2
#define MBOX_CH_VCHIQ 3
#define MBOX_CH_LEDS 4
#define MBOX_CH_BTNS 5
#define MBOX_CH_TOUCH 6
#define MBOX_CH_COUNT 7
#define MBOX_CH_PROP 8

/* tags */
#define MBOX_TAG_MODEL 0x10001
#define MBOX_TAG_REVISION 0x10002
#define MBOX_TAG_MAC_ADDRESS 0x10003
#define MBOX_TAG_GETSERIAL 0x10004
#define MBOX_TAG_ARM_MEMORY 0x10005
#define MBOX_TAG_VC_MEMORY 0x10006
#define MBOX_TAG_CLOCKS 0x10007
#define MBOX_TAG_LAST 0

#endif /* _P_MBOX_CALL_H */
19 changes: 19 additions & 0 deletions Lab3/include/peripherals/mini_uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef _P_MINI_UART_H
#define _P_MINI_UART_H

#include "peripherals/base.h"

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

#endif /*_P_MINI_UART_H */
8 changes: 8 additions & 0 deletions Lab3/include/peripherals/reboot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef _P_REBOOT_H
#define _P_REBOOT_H

#define PM_PASSWORD 0x5a000000
#define PM_RSTC 0x3F10001c
#define PM_WDOG 0x3F100024

#endif /*_P_REBOOT_H */
Loading