Skip to content

Commit

Permalink
Sync lab5 to lab7 // Don't need VA in this lab
Browse files Browse the repository at this point in the history
  • Loading branch information
JusticeLeee committed May 30, 2022
1 parent 972e6ad commit d6548b2
Show file tree
Hide file tree
Showing 81 changed files with 4,886 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lab5/kernel/src/exception.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void sync_handler_lowerEL_64(uint64_t sp) {
uint32_t ec = (esr_el1 >> 26) & 0x3f;
// printf("EC: %x\n", ec);
if (ec == 0b010101) { // SVC instruction
// printf("pid = %d, ",get_current()->pid);
uint64_t iss;
asm volatile("mov %0, x8" : "=r"(iss));
// printf("syscall number: %d\n", iss);
Expand All @@ -47,31 +48,36 @@ void sync_handler_lowerEL_64(uint64_t sp) {
uint32_t pid = get_current()->pid;
trap_frame->x[0] = pid;
} else if (iss == 1) { // uartread
// printf("[read]\n");
disable_uart_interrupt();
enable_interrupt();
char *str = (char *)(trap_frame->x[0]);
uint32_t size = (uint32_t)(trap_frame->x[1]);
size = uart_gets(str, size);
trap_frame->x[0] = size;
} else if (iss == 2) { // uartwrite
// printf("[write]\n");
char *str = (char *)(trap_frame->x[0]);
trap_frame->x[0] = uart_write(str,trap_frame->x[1]);
} else if (iss == 3) { // exec
const char *program_name = (const char *)trap_frame->x[0];
const char **argv = (const char **)trap_frame->x[1];
exec(program_name, argv);
} else if (iss == 4) { // fork
// printf("[fork]\n");
fork(sp);
} else if (iss == 5) { // exit
exit();
} else if (iss == 6) { // mbox_call
// printf("[mbox_call]\n");
trap_frame->x[0] = mbox_call(trap_frame->x[0],(unsigned int *)trap_frame->x[1]);
} else if (iss == 7) { // kill
kill((int)trap_frame->x[0]);
}
}
}


void irq_handler_currentEL_ELx() {
// printf("====irq_handler_currentEL_ELx=====\n");

Expand Down
25 changes: 25 additions & 0 deletions lab7/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# osc2022
## Author

| 學號 | GitHub 帳號 | 姓名 | Email |
| --- | ----------- | --- | --- |
|`309554042`| `JusticeLeee` | `李政毅` | [email protected] |

## How to build

```bash
./make.sh
```
## How to run

```bash
make run
```
## How to burn it into pi3
```bash
./rus.sh # write kernel to rpi3 through uart
sudo screen /dev/ttyUSB0 115200
```
## Note

copy some string.h source for use strcmp
48 changes: 48 additions & 0 deletions lab7/bootloader/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
# Copyright (C) 2018 bzt (bztsrc@github)
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy,
# modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
#

CFILE = $(wildcard *.c)
ASMFILE = $(wildcard *.S)
OBJS = $(CFILE:.c=_c.o)
OBJS += $(ASMFILE:.S=_s.o)
CFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles -fno-stack-protector -Iinclude -Iinclude

all: kernel8.img

%_s.o: %.S
aarch64-elf-gcc $(CFLAGS) -c $< -o $@

%_c.o: %.c
aarch64-elf-gcc $(CFLAGS) -c $< -o $@

kernel8.img: $(OBJS)
aarch64-elf-ld -nostdlib -nostartfiles $(OBJS) -T link.ld -o bootloader.elf
aarch64-elf-objcopy -O binary bootloader.elf bootloader.img

clean:
rm kernel8.elf *.o >/dev/null 2>/dev/null || true

run:
qemu-system-aarch64 -M raspi3 -kernel kernel8.img -serial null -serial stdio
Binary file added lab7/bootloader/bootloader.elf
Binary file not shown.
Binary file added lab7/bootloader/bootloader.img
Binary file not shown.
9 changes: 9 additions & 0 deletions lab7/bootloader/include/mini_uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _MINI_UART_H
#define _MINI_UART_H

void uart_init(void);
char uart_getc(void);
void uart_putc(char c);
void uart_puts(char* str);

#endif
17 changes: 17 additions & 0 deletions lab7/bootloader/include/mm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#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
6 changes: 6 additions & 0 deletions lab7/bootloader/include/peripherals/base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef _PER_BASE_H_
#define _PER_BASE_H_

#define PHY_BASE_ADDR 0x3F000000

#endif
12 changes: 12 additions & 0 deletions lab7/bootloader/include/peripherals/gpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef _PER_GPIO_H
#define _PER_GPIO_H

#include "peripherals/base.h"

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

#endif
18 changes: 18 additions & 0 deletions lab7/bootloader/include/peripherals/mini_uart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef _PER_MINI_UART_H_
#define _PER_MINI_UART_H_

#include "peripherals/base.h"
#define AUX_ENABLES (PHY_BASE_ADDR + 0x00215004)
#define AUX_MU_IO_REG (PHY_BASE_ADDR + 0x00215040)
#define AUX_MU_IER_REG (PHY_BASE_ADDR + 0x00215044)
#define AUX_MU_IIR_REG (PHY_BASE_ADDR + 0x00215048)
#define AUX_MU_LCR_REG (PHY_BASE_ADDR + 0x0021504C)
#define AUX_MU_MCR_REG (PHY_BASE_ADDR + 0x00215050)
#define AUX_MU_LSR_REG (PHY_BASE_ADDR + 0x00215054)
#define AUX_MU_MSR_REG (PHY_BASE_ADDR + 0x00215058)
#define AUX_MU_SCRATCH (PHY_BASE_ADDR + 0x0021505C)
#define AUX_MU_CNTL_REG (PHY_BASE_ADDR + 0x00215060)
#define AUX_MU_STAT_REG (PHY_BASE_ADDR + 0x00215064)
#define AUX_MU_BAUD_REG (PHY_BASE_ADDR + 0x00215068)

#endif
26 changes: 26 additions & 0 deletions lab7/bootloader/include/shell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef _SHELL_H
#define _SHELL_H

void buffer_clear();
void execute_command(char *);
void init_shell();

void print_input_prompt();
void get_input();

void del_key();
void left_key();
void right_key();
void up_key();
void down_key();

void simple_shell();

void shell_hello();
void shell_help();
void shell_reboot();
void shell_boot_uart();

void shell_ls();

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

typedef unsigned long long int uint64_t;
typedef signed long long int int64_t;

typedef unsigned int uint32_t;
typedef signed int int32_t;

typedef unsigned short int uint16_t;
typedef signed short int int16_t;

typedef unsigned char uint8_t;
typedef signed char int8_t;

uint8_t hex_to_int64(char*);
uint64_t hex_to_int8(char);
#endif
24 changes: 24 additions & 0 deletions lab7/bootloader/include/str_tool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef _STR_TOOL_H
#define _STR_TOOL_H
#include "stdint.h"

typedef struct {
char c_magic[6];
char c_ino[8];
char c_mode[8];
char c_uid[8];
char c_gid[8];
char c_nlink[8];
char c_mtime[8];
char c_filesize[8];
char c_devmajor[8];
char c_devminor[8];
char c_rdevmajor[8];
char c_rdevminor[8];
char c_namesize[8];
char c_check[8];
} cpio_newc_header;

int strcmp(char*, char*);
char* itoa(int64_t, int);
#endif
8 changes: 8 additions & 0 deletions lab7/bootloader/include/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef _UTILS_H_
#define _UTILS_H_

void delay(unsigned long);
void put32(unsigned long, unsigned int);
unsigned int get32(unsigned long);

#endif
13 changes: 13 additions & 0 deletions lab7/bootloader/link.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SECTIONS
{
. = 0x50000;
.text : { KEEP(*(.text.boot)) *(.text) }
.rodata : { *(.rodata) }
.data : { *(.data) }

. = ALIGN(0x8);
bss_begin = .;
.bss : { *(.bss*) }
bss_end = .;
_end = .;
}
31 changes: 31 additions & 0 deletions lab7/bootloader/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "mini_uart.h"
#include "utils.h"
#include "stdint.h"

union large_int_by_byte{
uint8_t int_by_byte[8];
uint64_t larg_int;
};

void boot_main(){
uart_init();

union large_int_by_byte program;
volatile uint8_t *program_addr = (volatile uint8_t *)0x80000;

// read dirty message until end
while(1) {
uint8_t data;
data = uart_getc();
if(data==4)break;
}

// read kernel size
for(int i=0; i<8; i++)
program.int_by_byte[i] = uart_getc();

for(uint64_t offset=0; offset<program.larg_int; offset++)
program_addr[offset] = uart_getc();

asm volatile("br %x[program_addr]"::[program_addr]"r"(program_addr));
}
47 changes: 47 additions & 0 deletions lab7/bootloader/mini_uart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "utils.h"
#include "peripherals/gpio.h"
#include "peripherals/mini_uart.h"

void uart_init(){
unsigned int selector;

selector = get32(GPFSEL1);
selector &= ~(7<<12); // clean gpio 14
selector |= 2<<12; // set alt5 at gpio14
selector &= ~(7<<15); // clean gpio 15
selector |= 2<<15; // set alt5 at gpio 15
put32(GPFSEL1, selector);

put32(GPPUD, 0); // disable pull-up/down
delay(150); // wait 150 cycles
put32(GPPUDCLK0, (1<<14)|(1<<15)); // only modify GPIO 14 and 15
delay(150);
put32(GPPUDCLK0, 0); // remove the clock

put32(AUX_ENABLES, 1); // enable mini UART
put32(AUX_MU_CNTL_REG, 0); // Disable transmitter and receiver during configuration
put32(AUX_MU_IER_REG, 0); // Disable interrupt
put32(AUX_MU_LCR_REG, 3); // Set the data size to 8 bit
put32(AUX_MU_MCR_REG, 0); // Don’t need auto flow control
put32(AUX_MU_BAUD_REG, 270); // Set baud rate to 115200

put32(AUX_MU_IIR_REG, 6); // No FIFO
put32(AUX_MU_CNTL_REG, 3); // Enable the transmitter and receiver
}

void uart_putc(char c){
while(!(get32(AUX_MU_LSR_REG)&0x20));
put32(AUX_MU_IO_REG, c);
}

char uart_getc(void){
while(!(get32(AUX_MU_LSR_REG)&0x01));
return get32(AUX_MU_IO_REG);
}

void uart_puts(char* str){
while(*str){
uart_putc(*str);
str += 1;
}
}
6 changes: 6 additions & 0 deletions lab7/bootloader/mm.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.globl memzero
memzero:
str xzr, [x0], #8
subs x1, x1, #8
b.gt memzero
ret
Loading

0 comments on commit d6548b2

Please sign in to comment.