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

310551061 Lab6 & Lab7 & Lab8 #177

Open
wants to merge 2 commits into
base: 310551061
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
55 changes: 55 additions & 0 deletions lab6/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# OSC 2022
CC = aarch64-linux-gnu-gcc
LD = aarch64-linux-gnu-ld
OBJCOPY = aarch64-linux-gnu-objcopy


IMG = kernel8.img
ELF = kernel8.elf
LINKER_FILE = kern/linker.ld

OUT_DIR = out
INC_DIR = include

SRCS := $(wildcard kern/*.S)
SRCS += $(wildcard kern/*.c)
SRCS += $(wildcard lib/*.c)
SRCS := $(notdir $(SRCS))
OBJS := $(patsubst %.c, $(OUT_DIR)/%_c.o, $(SRCS))
OBJS := $(patsubst %.S, $(OUT_DIR)/%_s.o, $(OBJS))

# -fno-stack-protector: to disable stack protection
# -fno-builtin is required to avoid refs to undefined functions in the kernel.
# Only optimize to -O1 to discourage inlining, which complicates backtraces.
CFLAGS := -O1 -fno-builtin -nostdinc
CFLAGS += -Wall -I$(INC_DIR) -c -fno-stack-protector

.PHONY: asm debug clean run


$(IMG): $(ELF)
$(OBJCOPY) -O binary $(ELF) $(IMG)
$(ELF): $(OBJS) $(LINKER_FILE $(OUT_DIR)/boot.o
$(LD) -T $(LINKER_FILE) -o $(ELF) $(OBJS)

$(OUT_DIR)/%_s.o: kern/%.S $(OUT_DIR)
$(CC) $(CFLAGS) $< -o $@
$(OUT_DIR)/%_c.o: kern/%.c $(OUT_DIR)
$(CC) $(CFLAGS) $< -o $@
$(OUT_DIR)/%_c.o: lib/%.c $(OUT_DIR)
$(CC) $(CFLAGS) $< -o $@

$(OUT_DIR):
@mkdir -p $(OUT_DIR)


asm: $(IMG)
qemu-system-aarch64 -M raspi3 -kernel $(IMG) -display none -d in_asm
debug: $(IMG)
qemu-system-aarch64 -M raspi3 -kernel $(IMG) -display none -serial null -serial stdio -initrd initramfs.cpio -S -s
run: $(IMG)
qemu-system-aarch64 -M raspi3 -kernel $(IMG) -display none -serial null -serial stdio -initrd initramfs.cpio
run-display: $(IMG)
qemu-system-aarch64 -M raspi3 -kernel $(IMG) -serial null -serial stdio -initrd initramfs.cpio
clean:
rm -rf $(OUT_DIR) $(ELF) $(IMG)
54 changes: 54 additions & 0 deletions lab6/boot/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# bootloader
CC = aarch64-linux-gnu-gcc
LD = aarch64-linux-gnu-ld
OBJCOPY = aarch64-linux-gnu-objcopy


IMG = bootloader.img
ELF = bootloader.elf

OUT_DIR = out
INC_DIR = ../include

SRCS := boot.S \
relocate.c \
main.c \
../lib/uart.c \
../lib/string.c
SRCS := $(notdir $(SRCS))
OBJS := $(patsubst %.c, $(OUT_DIR)/%.o, $(SRCS))
OBJS := $(patsubst %.S, $(OUT_DIR)/%.o, $(OBJS))

# -fno-stack-protector: to disable stack protection
CFLAGS := -O1 -fno-builtin -nostdinc
CFLAGS += -Wall -I$(INC_DIR) -c -fno-stack-protector

.PHONY: asm debug clean run pty


$(IMG): $(ELF)
$(OBJCOPY) -O binary $(ELF) $(IMG)
$(ELF): $(OBJS) linker.ld $(OUT_DIR)/boot.o
$(LD) -T linker.ld -o $(ELF) $(OBJS)

$(OUT_DIR)/boot.o: boot.S $(OUT_DIR)
$(CC) $(CFLAGS) $< -o $@
$(OUT_DIR)/%.o: %.c $(OUT_DIR)
$(CC) $(CFLAGS) $< -o $@
$(OUT_DIR)/%.o: ../lib/%.c $(OUT_DIR)
$(CC) $(CFLAGS) $< -o $@

$(OUT_DIR):
@mkdir -p $(OUT_DIR)


asm: $(IMG)
qemu-system-aarch64 -M raspi3 -kernel $(IMG) -display none -d in_asm
debug: $(IMG)
qemu-system-aarch64 -M raspi3 -kernel $(IMG) -display none -S -s
run: $(IMG)
qemu-system-aarch64 -M raspi3 -kernel $(IMG) -display none -serial null -serial stdio
pty:
qemu-system-aarch64 -M raspi3 -kernel $(IMG) -serial null -serial pty -initrd ../initramfs.cpio -dtb ../raspi3/bcm2710-rpi-3-b-plus.dtb
clean:
rm -rf $(OUT_DIR) $(ELF) $(IMG)
30 changes: 30 additions & 0 deletions lab6/boot/boot.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.section ".text.relocate"

.global _relocate
_relocate:
// save dtb address
mov x23, x0
2:
ldr x0, = __stack_top
mov sp, x0

// clear bss
ldr x0, =__bss_start
ldr x1, =__bss_size
3: cbz x1, 4f
// [x0] = 0, x0 = x0 + 8
str xzr, [x0], #8
// x1 = x1 - 1
sub x1, x1, #1
cbnz x1, 3b
4: bl relocate



.section ".text.boot"

.global _start
_start:
bl main
1: wfe
b 1b
43 changes: 43 additions & 0 deletions lab6/boot/linker.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
SECTIONS
{
. = 0x80000;
.relocate : {
KEEP(*(.text.relocate))
}

/* Adjust the address for the data segment to the next page */
. = ALIGN(0x1000);

__begin = .;
.text : {
KEEP(*(.text.boot))
*(.text .text.* .gnu.linkonce.t.*)
}

.rodata : {
*(.rodata .rodata.* .gnu.linkonce.r.*)
}

. = ALIGN(0x1000);

/* The data segment */
.data : {
*(.data)
}

.bss ALIGN(16) (NOLOAD) : {
PROVIDE(__bss_start = .);
*(.bss)
PROVIDE(__bss_end = .);
BYTE(0)
}
__end = .;

/DISCARD/ : {
*(.eh_frame .note.GNU-stack)
}
}

__stack_top = 0x20000000;
__bss_size = (__bss_end - __bss_start) >> 3;
__bootloader = 0x60000;
42 changes: 42 additions & 0 deletions lab6/boot/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "peripheral/uart.h"

#define KERNEL_ADDR 0x80000
#define READ_INT(var) \
var = 0; \
for (i=0 ; i<4 ; i++) {\
var <<= 8; \
var |= (int)uart_sync_read_raw(); \
} \

int main() {
int img_size;
int img_checksum;
int i;

uart_init();
uart_flush();

READ_INT(img_size);
READ_INT(img_checksum);

uart_sync_printNum(img_size, 10);
uart_sync_puts("\n");
uart_sync_printNum(img_checksum, 10);
uart_sync_puts("\n");

for (i=0 ; i<img_size ; i++) {
char b = uart_sync_read_raw();
*((char*)KERNEL_ADDR + i) = b;
img_checksum -= (int)b;
}

if (img_checksum != 0) {
uart_sync_puts("Send img failed...\n");
} else {
uart_sync_puts("Send img success!\n");
void (*start)(void) = (void *)KERNEL_ADDR;
start();
}

return 0;
}
17 changes: 17 additions & 0 deletions lab6/boot/relocate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

extern unsigned char __begin;
extern unsigned char __end;
extern unsigned char __bootloader;

__attribute__((section(".text.relocate"))) void relocate() {
unsigned long size = (&__end - &__begin);
unsigned char *from = (unsigned char *)&__begin;
unsigned char *to = (unsigned char *)&__bootloader;

while (size--) {
*to++ = *from++;
}

void (*start)(void) = (void *)&__bootloader;
start();
}
29 changes: 29 additions & 0 deletions lab6/documentation/mm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

## memory layout

```
+------------------+
| Arm Peripherals |
+------------------+ <- 0x40000000
| GPU Peripherals |
+------------------+ <- 0x3f000000
| reserved |
+------------------+ <- 0x3C000000
| . |
| User stack |
| . |
+------------------+
| Kernel frame[n] | <- statically declared
+------------------+
| . |
| . |
+------------------+
| Kernel frame[0] |
+------------------+
| Int stack | <- statically declared
+------------------+
| kernel/user data |
+------------------+
| kernel/user text |
+------------------+ <- kernel_base
```
5 changes: 5 additions & 0 deletions lab6/documentation/sched.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

## priority

* privilege task: 1 ~ 20
* user task: 21 ~ 127
66 changes: 66 additions & 0 deletions lab6/include/bitmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#ifndef BITMAP_H
#define BITMAP_H

#define BITS_PER_BYTE 8
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))

#define DECLARE_BITMAP(name,bits) \
unsigned long name[BITS_TO_LONGS(bits)]

static inline void bitmap_zero(unsigned long *dst, unsigned int nbits) {
int i;
for(i=0 ; i<BITS_TO_LONGS(nbits) ; i++) {
dst[i] = 0;
}
}

#define BITS_PER_LONG 64
#define BIT_MASK(nr) (1UL << ((nr % BITS_PER_LONG)))
#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)

static inline unsigned long __ffs(unsigned long word) {
int num = 0;
#if BITS_PER_LONG == 64
if ((word & 0xffffffff) == 0) {
num += 32;
word >>= 32;
}
#endif
if ((word & 0xffff) == 0) {
num += 16;
word >>= 16;
}
if ((word & 0xff) == 0) {
num += 8;
word >>= 8;
}
if ((word & 0xf) == 0) {
num += 4;
word >>= 4;
}
if ((word & 0x3) == 0) {
num += 2;
word >>= 2;
}
if ((word & 0x1) == 0)
num += 1;
return num;
}

/*
bitops
*/
static inline void __set_bit(int nr, volatile unsigned long *addr) {
unsigned long mask = BIT_MASK(nr);
unsigned long *p = ((unsigned long *) addr) + BIT_WORD(nr);
*p |= mask;
}

static inline void __clear_bit(int nr, volatile unsigned long *addr) {
unsigned long mask = BIT_MASK(nr);
unsigned long *p = ((unsigned long *) addr) + BIT_WORD(nr);
*p &= ~mask;
}

#endif
9 changes: 9 additions & 0 deletions lab6/include/byteswap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef BYTESWAP_H
#define BYTESWAP_H

/* Swap bytes in 32-bit value. */
#define __bswap_32(x) \
((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >> 8) \
| (((x) & 0x0000ff00u) << 8) | (((x) & 0x000000ffu) << 24))

#endif
Loading