From 89bacd7f88ed7a30c8a3949c378792a35fe01469 Mon Sep 17 00:00:00 2001 From: Jim Huang Date: Tue, 5 Jan 2016 13:27:51 +0800 Subject: [PATCH] initial import of armv8-hello --- AUTHORS | 5 +++++ LICENSE | 26 ++++++++++++++++++++++++++ Makefile | 37 +++++++++++++++++++++++++++++++++++++ README.md | 26 ++++++++++++++++++++++++++ boot.S | 29 +++++++++++++++++++++++++++++ hello.c | 14 ++++++++++++++ kernel.ld | 16 ++++++++++++++++ reg.h | 6 ++++++ 8 files changed, 159 insertions(+) create mode 100644 AUTHORS create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 boot.S create mode 100644 hello.c create mode 100644 kernel.ld create mode 100644 reg.h diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..ab206fc --- /dev/null +++ b/AUTHORS @@ -0,0 +1,5 @@ +armv8-hello is written by: + Jim Huang + +Copyrighted by: + National Cheng Kung University, Taiwan diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..68a1be0 --- /dev/null +++ b/LICENSE @@ -0,0 +1,26 @@ +armv8-hello is freely redistributable under the two-clause BSD License: + +Copyright (C) 2016 National Cheng Kung University, Taiwan. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b39e00a --- /dev/null +++ b/Makefile @@ -0,0 +1,37 @@ +IMAGE := kernel.elf + +CROSS_COMPILE = aarch64-linux-gnu- + +CC = $(CROSS_COMPILE)gcc +LD = $(CROSS_COMPILE)ld +OBJDUMP = $(CROSS_COMPILE)objdump + +CFLAGS = -Wall -fno-common -O0 -g \ + -nostdlib -nostartfiles -ffreestanding \ + -march=armv8-a + +OBJS = hello.o + +all: $(IMAGE) + +boot.o: boot.S + $(CC) boot.S -c -o boot.o + +$(IMAGE): kernel.ld boot.o $(OBJS) + $(LD) boot.o $(OBJS) -T kernel.ld -o $(IMAGE) + $(OBJDUMP) -d kernel.elf > kernel.list + $(OBJDUMP) -t kernel.elf | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > kernel.sym + +qemu: $(IMAGE) + @qemu-system-aarch64 -M ? | grep virt >/dev/null || exit + @echo "Press Ctrl-A and then X to exit QEMU" + @echo + qemu-system-aarch64 -machine virt -cpu cortex-a57 \ + -smp 4 -m 4096 \ + -nographic -serial mon:stdio \ + -kernel $(IMAGE) + +clean: + rm -f $(IMAGE) *.o *.list *.sym + +.PHONY: all qemu clean diff --git a/README.md b/README.md new file mode 100644 index 0000000..ee7ad62 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +Hello World for bare metal ARM using QEMU + +Prerequisites +------------- +- Install [Linaro toolchain](http://www.linaro.org/downloads/) for Aarch64 little-endian +``` +sudo apt-get install gcc-aarch64-linux-gnu +``` +- Install recent QEMU with Aarch64 target +``` +sudo apt-get install qemu-system-arm +``` + +Building and Verification +------------------------- +* Changes the current working directory to the specified one and then +``` +make +make qemu +``` + +Licensing +--------- +`mini-arm-os` is freely redistributable under the two-clause BSD License. +Use of this source code is governed by a BSD-style license that can be found +in the `LICENSE` file. diff --git a/boot.S b/boot.S new file mode 100644 index 0000000..dfac012 --- /dev/null +++ b/boot.S @@ -0,0 +1,29 @@ +#include "reg.h" + +.text + +.globl _start +_start: + /* init UART (38400 8N1) */ + ldr x4, =UART_BASE // UART base + mov w5, #0x10 // IBRD + str w5, [x4, #0x24] + mov w5, #0xc300 + orr w5, w5, #0x0001 // CR + str w5, [x4, #0x30] + + /* check CPU ID */ + mrs x0, mpidr_el1 + tst x0, #15 + b.ne other_cpu + b.eq boot_cpu + +boot_cpu: + /* configure stack */ + adrp x0, stacktop + mov sp, x0 + + b main + +other_cpu: + b other_cpu diff --git a/hello.c b/hello.c new file mode 100644 index 0000000..ce0ad5a --- /dev/null +++ b/hello.c @@ -0,0 +1,14 @@ +#include "reg.h" + +int puts(const char *str) +{ + while (*str) + *((unsigned int *) UART_BASE) = *str++; + return 0; +} + +void main(void) +{ + puts("Hello\n"); + while (1); +} diff --git a/kernel.ld b/kernel.ld new file mode 100644 index 0000000..ddd71a4 --- /dev/null +++ b/kernel.ld @@ -0,0 +1,16 @@ +OUTPUT_FORMAT("elf64-littleaarch64") +OUTPUT_ARCH(aarch64) +TARGET(binary) + +STACKTOP = 0x51000000; + +SECTIONS +{ + . = 0x40010000; + .text : { *(.text) } + .data : { *(.data) } + .bss : { *(.bss) } + + . = STACKTOP; + stacktop = .; +} diff --git a/reg.h b/reg.h new file mode 100644 index 0000000..f1f7749 --- /dev/null +++ b/reg.h @@ -0,0 +1,6 @@ +#ifndef __REG_H__ +#define __REG_H__ + +#define UART_BASE 0x09000000 + +#endif