diff --git a/Lab1/Makefile b/Lab1/Makefile new file mode 100755 index 000000000..0174802a0 --- /dev/null +++ b/Lab1/Makefile @@ -0,0 +1,25 @@ +SRCS = $(wildcard *.c) +OBJS = $(SRCS:.c=.o) +CCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles +CC = aarch64-linux-gnu-gcc +LINKER = aarch64-linux-gnu-ld +OBJ_CPY = aarch64-linux-gnu-objcopy + +all: clean kernel8.img + +start.o: start.S + $(CC) $(CCFLAGS) -c start.S -o start.o + +%.o: %.c + $(CC) $(CCFLAGS) -c $< -o $@ + +kernel8.img: start.o $(OBJS) + $(LINKER) start.o $(OBJS) -T link.ld -o kernel8.elf + $(OBJ_CPY) -O binary kernel8.elf kernel8.img + +clean: + rm kernel8.elf kernel8.img *.o >/dev/null 2>/dev/null || true + +run: + qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial null -serial stdio + diff --git a/Lab1/READ.md b/Lab1/READ.md new file mode 100644 index 000000000..724016432 --- /dev/null +++ b/Lab1/READ.md @@ -0,0 +1,22 @@ +# OSC2023 + +| Github Account | Student ID | Name | +|----------------|------------|---------------| +| linlianen | 311553047 | Lin Lian En | + +## Requirements + +* a cross-compiler for aarch64 +* (optional) qemu-system-arm + +## Build + +``` +make kernel.img +``` + +## Test With QEMU + +``` +qemu-system-aarch64 -M raspi3b -kernel kernel.img -initrd initramfs.cpio -serial null -serial stdio -dtb bcm2710-rpi-3-b-plus.dtb +``` diff --git a/Lab1/command.c b/Lab1/command.c new file mode 100644 index 000000000..d559135ee --- /dev/null +++ b/Lab1/command.c @@ -0,0 +1,62 @@ +#include "uart.h" +#include "string.h" + +void input_buffer_overflow_message ( char cmd[] ) +{ + uart_puts("Follow command: \""); + uart_puts(cmd); + uart_puts("\"... is too long to process.\n"); + + uart_puts("The maximum length of input is 64."); +} + +void command_help () +{ + uart_puts("\n"); + uart_puts("Valid Command:\n"); + uart_puts("\thelp:\t\tprint this help.\n"); + uart_puts("\thello:\t\tprint \"Hello World!\".\n"); + uart_puts("\ttimestamp:\tget current timestamp.\n"); + uart_puts("\n"); +} + +void command_hello () +{ + uart_puts("Hello World!\n"); +} + +void command_timestamp () +{ + unsigned long int cnt_freq, cnt_tpct; + char str[20]; + + asm volatile( + "mrs %0, cntfrq_el0 \n\t" + "mrs %1, cntpct_el0 \n\t" + : "=r" (cnt_freq), "=r" (cnt_tpct) + : + ); + + ftoa( ((float)cnt_tpct) / cnt_freq, str, 6); + + uart_send('['); + uart_puts(str); + uart_puts("]\n"); +} + +void command_not_found (char * s) +{ + uart_puts("Err: command "); + uart_puts(s); + uart_puts(" not found, try \n"); +} + +void command_reboot () +{ + uart_puts("Start Rebooting...\n"); + + *PM_WDOG = PM_PASSWORD | 0x20; + *PM_RSTC = PM_PASSWORD | 100; + + while(1); +} diff --git a/Lab1/command.h b/Lab1/command.h new file mode 100644 index 000000000..50bf38467 --- /dev/null +++ b/Lab1/command.h @@ -0,0 +1,12 @@ +#ifndef COMMAND_H +#define COMMAND_H + +void input_buffer_overflow_message ( char [] ); + +void command_help (); +void command_hello (); +void command_timestamp (); +void command_not_found ( char * ); +void command_reboot (); + +#endif diff --git a/Lab1/command.o b/Lab1/command.o new file mode 100644 index 000000000..72a73c258 Binary files /dev/null and b/Lab1/command.o differ diff --git a/Lab1/gpio.h b/Lab1/gpio.h new file mode 100755 index 000000000..b89cd98db --- /dev/null +++ b/Lab1/gpio.h @@ -0,0 +1,50 @@ +/* + * 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. + * + */ + +#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 diff --git a/Lab1/kernel8.elf b/Lab1/kernel8.elf new file mode 100755 index 000000000..ae3d4f47e Binary files /dev/null and b/Lab1/kernel8.elf differ diff --git a/Lab1/kernel8.img b/Lab1/kernel8.img new file mode 100755 index 000000000..1baf8be48 Binary files /dev/null and b/Lab1/kernel8.img differ diff --git a/Lab1/link.ld b/Lab1/link.ld new file mode 100755 index 000000000..066df7b90 --- /dev/null +++ b/Lab1/link.ld @@ -0,0 +1,19 @@ +SECTIONS +{ + . = 0x80000; + .text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) } + .rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) } + PROVIDE(_data = .); + .data : { *(.data .data.* .gnu.linkonce.d*) } + .bss (NOLOAD) : { + . = ALIGN(16); + __bss_start = .; + *(.bss .bss.*) + *(COMMON) + __bss_end = .; + } + _end = .; + + /DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) } +} +__bss_size = (__bss_end - __bss_start)>>3; diff --git a/Lab1/main.c b/Lab1/main.c new file mode 100755 index 000000000..155b817a5 --- /dev/null +++ b/Lab1/main.c @@ -0,0 +1,16 @@ +#include "uart.h" +#include "shell.h" + +int main() +{ + // set up serial console + uart_init(); + + // say hello + uart_puts("Hello World!\n"); + + // start shell + shell_start(); + + return 0; +} diff --git a/Lab1/main.o b/Lab1/main.o new file mode 100644 index 000000000..1322ca810 Binary files /dev/null and b/Lab1/main.o differ diff --git a/Lab1/math.c b/Lab1/math.c new file mode 100644 index 000000000..eb4c193b1 --- /dev/null +++ b/Lab1/math.c @@ -0,0 +1,11 @@ + + +int pow(int base, int exponent) +{ + int result = 1; + for ( ; exponent > 0; exponent--) + { + result = result * base; + } + return result; +} diff --git a/Lab1/math.h b/Lab1/math.h new file mode 100644 index 000000000..67b900028 --- /dev/null +++ b/Lab1/math.h @@ -0,0 +1,7 @@ + +#ifndef MATH_H +#define MATH_H + +int pow(int base, int exponent); + +#endif diff --git a/Lab1/math.o b/Lab1/math.o new file mode 100644 index 000000000..5176c141f Binary files /dev/null and b/Lab1/math.o differ diff --git a/Lab1/shell.c b/Lab1/shell.c new file mode 100755 index 000000000..af19823b9 --- /dev/null +++ b/Lab1/shell.c @@ -0,0 +1,112 @@ + + + +#include "shell.h" +#include "string.h" +#include "command.h" +#include "uart.h" + +void shell_start () +{ + int buffer_counter = 0; + char input_char; + char buffer[MAX_BUFFER_LEN]; + enum SPECIAL_CHARACTER input_parse; + + strset (buffer, 0, MAX_BUFFER_LEN); + + // new line head + uart_puts("# "); + + // read input + while(1) + { + input_char = uart_getc(); + + + + input_parse = parse ( input_char ); + + command_controller ( input_parse, input_char, buffer, &buffer_counter); + } +} + +enum SPECIAL_CHARACTER parse ( char c ) +{ + if ( !(c < 128 && c >= 0) ) + return UNKNOWN; + + if ( c == 127 ) + return BACK_SPACE; + else if ( c == LINE_FEED || c == CARRIAGE_RETURN ) + return NEW_LINE; + else + return REGULAR_INPUT; +} + +void command_controller ( enum SPECIAL_CHARACTER input_parse, char c, char buffer[], int * counter ) +{ + + + if ( input_parse == UNKNOWN ) + return; + + // Special key + if ( input_parse == BACK_SPACE ) + { + + if ( (*counter) > 0 ) + { + (*counter)--; + buffer[(*counter)] = '\0'; + + // int* end = counter; + for (int i = 0; i < (counter) ; i++) { + buffer[i] = buffer[i+1]; + } + + uart_puts(buffer); + + } + + + + + + } + else if ( input_parse == NEW_LINE ) + { + uart_send(c); + + if ( (*counter) == MAX_BUFFER_LEN ) + { + input_buffer_overflow_message(buffer); + } + else + { + buffer[(*counter)] = '\0'; + + if ( !strcmp(buffer, "help" ) ) command_help(); + else if ( !strcmp(buffer, "hello" ) ) command_hello(); + else if ( !strcmp(buffer, "timestamp" ) ) command_timestamp(); + else if ( !strcmp(buffer, "reboot" ) ) command_reboot(); + else command_not_found(buffer); + } + + (*counter) = 0; + strset (buffer, 0, MAX_BUFFER_LEN); + + // new line head; + uart_puts("# "); + } + else if ( input_parse == REGULAR_INPUT ) + { + uart_send(c); + + if ( *counter < MAX_BUFFER_LEN) + { + buffer[*counter] = c; + (*counter) ++; + } + } +} diff --git a/Lab1/shell.h b/Lab1/shell.h new file mode 100755 index 000000000..60dc89a2d --- /dev/null +++ b/Lab1/shell.h @@ -0,0 +1,24 @@ +#ifndef SHELL_H +#define SHELL_H + +#define MAX_BUFFER_LEN 128 + +enum SPECIAL_CHARACTER +{ + BACK_SPACE = 8, + LINE_FEED = 10, + CARRIAGE_RETURN = 13, + + + REGULAR_INPUT = 1000, + NEW_LINE = 1001, + + UNKNOWN = -1, + +}; + +void shell_start () ; +enum SPECIAL_CHARACTER parse ( char ); +void command_controller ( enum SPECIAL_CHARACTER, char c, char [], int *); + +#endif diff --git a/Lab1/shell.o b/Lab1/shell.o new file mode 100644 index 000000000..438761fb2 Binary files /dev/null and b/Lab1/shell.o differ diff --git a/Lab1/start.S b/Lab1/start.S new file mode 100755 index 000000000..e28b48ab5 --- /dev/null +++ b/Lab1/start.S @@ -0,0 +1,30 @@ +.section ".text.boot" + +.global _start + +_start: + // read cpu id, stop slave cores + mrs x1, mpidr_el1 + and x1, x1, #3 + cbz x1, 2f + // cpu id > 0, stop +1: wfe + b 1b +2: // cpu id == 0 + + // set top of stack just before our code (stack grows to a lower address per AAPCS64) + ldr x1, =_start + mov sp, x1 + + // clear bss + ldr x1, =__bss_start + ldr w2, =__bss_size +3: cbz w2, 4f + str xzr, [x1], #8 + sub w2, w2, #1 + cbnz w2, 3b + + // jump to C code, should not return +4: bl main + // for failsafe, halt this core too + b 1b diff --git a/Lab1/start.o b/Lab1/start.o new file mode 100644 index 000000000..b8ad155a6 Binary files /dev/null and b/Lab1/start.o differ diff --git a/Lab1/string.c b/Lab1/string.c new file mode 100644 index 000000000..307b99317 --- /dev/null +++ b/Lab1/string.c @@ -0,0 +1,95 @@ +#include "string.h" +#include "math.h" + +int strcmp ( char * s1, char * s2 ) +{ + int i; + + for (i = 0; i < strlen(s1); i ++) + { + if ( s1[i] != s2[i]) + { + return s1[i] - s2[i]; + } + } + + return s1[i] - s2[i]; +} + +void strset (char * s1, int c, int size ) +{ + int i; + + for ( i = 0; i < size; i ++) + s1[i] = c; +} + +int strlen ( char * s ) +{ + int i = 0; + while ( 1 ) + { + if ( *(s+i) == '\0' ) + break; + i++; + } + + return i; +} + +// https://www.geeksforgeeks.org/convert-floating-point-number-string/ +void itoa (int x, char str[], int d) +{ + int i = 0; + while (x) { + str[i++] = (x % 10) + '0'; + x = x / 10; + } + + // If number of digits required is more, then + // add 0s at the beginning + while (i < d) + str[i++] = '0'; + + str[i] = '\0'; + reverse(str); +} + +// https://www.geeksforgeeks.org/convert-floating-point-number-string/ +void ftoa(float n, char* res, int afterpoint) +{ + // Extract integer part + int ipart = (int)n; + + // Extract floating part + float fpart = n - (float)ipart; + + // convert integer part to string + itoa(ipart, res, 0); + int i = strlen(res); + + // check for display option after point + if (afterpoint != 0) { + res[i] = '.'; // add dot + + // Get the value of fraction part upto given no. + // of points after dot. The third parameter + // is needed to handle cases like 233.007 + fpart = fpart * pow(10, afterpoint); + + itoa((int)fpart, res + i + 1, afterpoint); + } +} + +void reverse ( char * s ) +{ + int i; + char temp; + + for ( i = 0; i < strlen(s) / 2; i++ ) + { + temp = s[strlen(s) - i - 1]; + s[strlen(s) - i - 1] = s[0]; + s[0] = temp; + } +} diff --git a/Lab1/string.h b/Lab1/string.h new file mode 100644 index 000000000..763fd2b05 --- /dev/null +++ b/Lab1/string.h @@ -0,0 +1,13 @@ + +#ifndef STRING_H +#define STRING_H + +int strcmp ( char * s1, char * s2 ); +void strset ( char * s1, int c, int size ); +int strlen ( char * s ); +void itoa ( int x, char str[], int d); +void ftoa ( float n, char* res, int afterpoint ); +void reverse ( char *s ); + + +#endif diff --git a/Lab1/string.o b/Lab1/string.o new file mode 100644 index 000000000..0b4e28fd9 Binary files /dev/null and b/Lab1/string.o differ diff --git a/Lab1/uart.c b/Lab1/uart.c new file mode 100755 index 000000000..7f3871325 --- /dev/null +++ b/Lab1/uart.c @@ -0,0 +1,115 @@ +#include "gpio.h" +#include "uart.h" +void uart_init() +{ + register unsigned int reg; + + /*initialize UART*/ + *AUX_ENABLE |=1; + *AUX_MU_CNTL = 0; + + *AUX_MU_IER = 0; + *AUX_MU_LCR = 3; /* Set the data size to 8 bit. */ + *AUX_MU_MCR = 0; /* Don’t need auto flow control. */ + *AUX_MU_BAUD = 270; /* 115200 baud */ + *AUX_MU_IIR = 6; /* No FIFO */ + + /* map UART1 to GPIO pins*/ + reg = *GPFSEL1; + reg &= ~((7<<12)|(7<<15)); + reg |= (2<<12)|(2<<15); + + + /* map UART1 to GPIO pins */ + reg = *GPFSEL1; + reg &= ~((7<<12)|(7<<15)); /* address of gpio 14, 15 */ + reg |= (2<<12)|(2<<15); /* set to alt5 */ + + *GPFSEL1 = reg; + + *GPPUD = 0; /* enable gpio 14 and 15 */ + reg=150; + while ( reg-- ) + { + asm volatile("nop"); + } + + *GPPUDCLK0 = (1<<14)|(1<<15); + reg=150; + while ( reg-- ) + { + asm volatile("nop"); + } + + *GPPUDCLK0 = 0; /* flush GPIO setup */ + + *AUX_MU_CNTL = 3; // Enable the transmitter and receiver. +} + + +/* Send a character*/ + +void uart_send(unsigned int c) +{ + + // Wait until we can send + + do{ + asm volatile("nop"); + + }while(!( *AUX_MU_LSR&0x20)); + + //write the character to the buffer + *AUX_MU_IO = c ; + + if (c == '\n') + { + do { + + asm volatile("nop"); + + } while( ! ( *AUX_MU_LSR&0x20 )); + + *AUX_MU_IO = '\r'; + } +} + + + +/** + * Receive a character + */ +char uart_getc() { + + char r; + + /* wait until something is in the buffer */ + do{ + + asm volatile("nop"); + + } while ( ! ( *AUX_MU_LSR&0x01 ) ); + + /* read it and return */ + r = ( char )( *AUX_MU_IO ); + + /* convert carrige return to newline */ + return r == '\r' ? '\n' : r; +} + +/** + * Display a string + */ +void uart_puts(char *s) +{ + while( *s ) + { + /* convert newline to carrige return + newline */ + + //if(*s=='\n') + // uart_send('\r'); + + uart_send(*s++); + + } +} diff --git a/Lab1/uart.h b/Lab1/uart.h new file mode 100755 index 000000000..89f23637e --- /dev/null +++ b/Lab1/uart.h @@ -0,0 +1,65 @@ +/* + * 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. + * + */ + +#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)) + +#define PM_RSTC ((volatile unsigned int*)0x3F10001C) +#define PM_WDOG ((volatile unsigned int*)0x3F100024) +#define PM_PASSWORD (0x5a000000) +/** + * Set baud rate and characteristics (115200 8N1) and map to GPIO + */ +void uart_init(); + +/** + * Send a character + */ +void uart_send(unsigned int c); +/** + * Receive a character + */ +char uart_getc(); + +/** + * Display a string + */ +void uart_puts(char *s); + +#endif diff --git a/Lab1/uart.o b/Lab1/uart.o new file mode 100644 index 000000000..c82e3f814 Binary files /dev/null and b/Lab1/uart.o differ