diff --git a/Makefile b/Makefile index 15aea07..59ac6f0 100644 --- a/Makefile +++ b/Makefile @@ -6,14 +6,13 @@ AARCH64_TOOLCHAIN ?= aarch64-linux-gnu # Shortcuts CC=clang --target=aarch64-none-elf -mcpu=cortex-a57 -# CC=$(AARCH64_TOOLCHAIN)-gcc -LD=$(AARCH64_TOOLCHAIN)-ld +LD=ld.lld OBJCOPY=$(AARCH64_TOOLCHAIN)-objcopy --target elf64-littleaarch64 CFLAGS += -O0 CFLAGS += -g -CFLAGS += -nostdlib -CFLAGS += -march=armv8-a +#CFLAGS += -nostdlib +#CFLAGS += -march=armv8-a #CFLAGS += -std=c99 #CFLAGS += -pedantic #CFLAGS += -Wall @@ -26,7 +25,7 @@ BUILD_DIR=build # Files -KERNEL_MEMMAP = sys/memmap +KERNEL_MEMMAP = aarch64/memmap KERNEL_SOURCES = \ aarch64/boot.s \ @@ -45,7 +44,7 @@ kernel: $(BUILD_DIR)/kernel.elf # $(BUILD_DIR)/kernel.sym $(BUILD_DIR)/kernel.elf: $(KERNEL_OBJS) $(info linking target: $@) - $(LD) -T$(KERNEL_MEMMAP) $^ -o $@ + $(LD) -T$(KERNEL_MEMMAP) -nostdlib $^ -o $@ $(info done: $@) $(BUILD_DIR)/kernel.bin: $(BUILD_DIR)/kernel.elf @@ -66,14 +65,15 @@ $(BUILD_DIR)/%.o: % # helpers QEMU_CMD = \ qemu-system-aarch64 \ + -M virt \ -cpu cortex-a57 \ - -machine type=virt \ -nographic \ -smp 4 \ - -m 4098 \ + -m 4096 \ -kernel build/kernel.elf \ -serial stdio \ - -monitor none + -monitor none \ + clean: rm -Rf ./$(BUILD_DIR)/* @@ -83,4 +83,8 @@ run: kernel debug: kernel - $(QEMU_CMD) -gdb tcp::1234 -S + $(QEMU_CMD) -S -gdb tcp::1234 & \ + gdb-multiarch -q \ + -ex 'file build/kernel.elf' \ + -ex 'target remote localhost:1234' + kill %1 diff --git a/aarch64/boot.S b/aarch64/boot.S deleted file mode 100644 index 8a0fb75..0000000 --- a/aarch64/boot.S +++ /dev/null @@ -1,22 +0,0 @@ - .global _boot - .extern k_main - - -_boot: - mrs x0, mpidr_el1 // Check CPU ID - mov x1, 0xC1000000 - bic x0, x0, x1 - cbz x0, set_stack - b idle - -set_stack: - ldr x30, =0x40001000 - mov sp, x30 - bl start - -start: - mov x0, x30 - bl k_main - -idle: b idle - diff --git a/aarch64/boot.s b/aarch64/boot.s index 9406bfe..fd6ebf5 100644 --- a/aarch64/boot.s +++ b/aarch64/boot.s @@ -1,10 +1,7 @@ - .text - .globl _start - .extern k_main - .extern stack_ptr _start: + mrs x0, mpidr_el1 // Check CPU ID mov x1, 0xC1000000 bic x0, x0, x1 @@ -12,13 +9,9 @@ _start: b idle set_stack: - ldr x30, stack_ptr // defined in sys/memmap + ldr x30, =stack_ptr // defined in sys/memmap mov sp, x30 - bl start - -start: - mov x0, x30 - bl k_main + bl k_main idle: b idle diff --git a/aarch64/memmap b/aarch64/memmap new file mode 100644 index 0000000..89d09c1 --- /dev/null +++ b/aarch64/memmap @@ -0,0 +1,16 @@ +ENTRY(_start) + +SECTIONS +{ + /* For some reason qemu UART doesn't work with smaller address */ + . = 0x40000000; + + .boot . : { boot.o(.text) } + .text : { *(.text) } + .data : { *(.data) } + .bss : { *(.bss COMMON) } + + . = ALIGN(8); + . = . + 0x1000; + stack_ptr = . ; +} diff --git a/sys/main.c b/sys/main.c index 1fcf79d..19b4ad4 100644 --- a/sys/main.c +++ b/sys/main.c @@ -25,13 +25,10 @@ k_main(void) { uart_init(); - uart_putc('L'); - uart_putc('o'); - uart_putc('w'); - uart_putc('e'); - uart_putc('\n'); + uart_puts((unsigned char *) "Lowe OS\n"); for (;;) { + __asm__("WFE"); } } diff --git a/sys/memmap b/sys/memmap deleted file mode 100644 index eb6c0cf..0000000 --- a/sys/memmap +++ /dev/null @@ -1,12 +0,0 @@ - ENTRY(_start) - - SECTIONS - { - . = 0x40000000; - .text : { *(.text) } - .data : { *(.data) } - .bss : { *(.bss COMMON) } - . = ALIGN(8); - . = . + 0x1000; - stack_ptr = . ; - } diff --git a/sys/uart.c b/sys/uart.c index 67b69bf..da09b6c 100644 --- a/sys/uart.c +++ b/sys/uart.c @@ -15,7 +15,7 @@ #include "uart.h" -unsigned int *uart_base = (unsigned int *)UART_BASE; +volatile unsigned int *UART0 = (unsigned int *)UART_BASE; void @@ -24,10 +24,18 @@ uart_init(void) } + void uart_putc(unsigned char c) { - *uart_base = c; - if(c == '\n') - *uart_base = '\r'; + *UART0 = (unsigned int) c; +} + + +void +uart_puts(const unsigned char *s) +{ + while(*s) { + *UART0 = (unsigned int) *(s++); + } } diff --git a/sys/uart.h b/sys/uart.h index 4954fee..2c7c571 100644 --- a/sys/uart.h +++ b/sys/uart.h @@ -22,9 +22,12 @@ void uart_init(void); + void uart_putc(unsigned char c); +void +uart_puts(const unsigned char *s); #endif /* !UART_H */