From 2d91eebbf1fc9335269207602b360ec485aaf7bb Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 17 Dec 2019 17:39:03 +0100 Subject: don't embed stage2 of bootloader as binary blob - use smart section naming --- Makefile | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index d1d66fa..35cc48c 100644 --- a/Makefile +++ b/Makefile @@ -30,17 +30,14 @@ PL_0_test.elf : PL0_test.o uart.o kernel.elf : boot.o kernel.o uart.o PL_0_test_embeddable.o demo_functionality.o paging.o libkernel_renamed.o interrupt_vector.o interrupts.o arm-none-eabi-gcc -T linker.ld -o $@ $(ELFFLAGS) $^ -loader_stage2.elf : loader_stage2.o uart.o - arm-none-eabi-gcc -T loader_stage2.ld -o $@ $(ELFFLAGS) $^ +loader.elf : boot.o loader_stage1.o loader_stage2.o uart.o + arm-none-eabi-gcc -T loader_linker.ld -o $@ $(ELFFLAGS) $^ -loader_stage2.img : loader_stage2.elf +loader.img : loader.elf arm-none-eabi-objcopy $^ -O binary $@ +# check if the resulting image is not too big test -n "$$(find $@ -size -16384c)" || exit -1 -loader.elf : boot.o loader_stage1.o loader_stage2_embeddable.o - arm-none-eabi-gcc -T loader_stage1.ld -o $@ $(ELFFLAGS) $^ - - qemu-elf : kernel.elf qemu-system-arm -m 256 -M raspi2 -serial stdio -kernel $^ -- cgit v1.2.3 From 4c110d2802742fe1d37ba7280f374174fc45cbe1 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 24 Dec 2019 22:43:07 +0100 Subject: simplify linking of libkernel --- Makefile | 7 +----- demo_functionality.c | 12 +++++----- linker.ld | 63 +++++++++++++++------------------------------------- 3 files changed, 25 insertions(+), 57 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 35cc48c..d782eda 100644 --- a/Makefile +++ b/Makefile @@ -5,8 +5,6 @@ ARM_OBJECTS=kernel.o paging.o demo_functionality.o PL0_test.o uart.o loader_stag EMBEDDABLE_OBJECTS=PL_0_test_embeddable.o loader_stage2_embeddable.o -RENAME_FLAGS=--rename-section .data=.renamed_data --rename-section .rodata=.renamed_rodata --rename-section .text=.renamed_text --rename-section .bss=.renamed_bss - all : kernel7.img %.o : %.c @@ -21,13 +19,10 @@ all : kernel7.img %_embeddable.o : %.img arm-none-eabi-objcopy -I binary -O elf32-littlearm -B arm --rename-section .data=.rodata $^ $@ -libkernel_renamed.o : libkernel.o - arm-none-eabi-objcopy $(RENAME_FLAGS) $^ $@ - PL_0_test.elf : PL0_test.o uart.o arm-none-eabi-gcc -T PL0_test.ld -o $@ $(ELFFLAGS) $^ -kernel.elf : boot.o kernel.o uart.o PL_0_test_embeddable.o demo_functionality.o paging.o libkernel_renamed.o interrupt_vector.o interrupts.o +kernel.elf : boot.o kernel.o uart.o PL_0_test_embeddable.o demo_functionality.o paging.o libkernel.o interrupt_vector.o interrupts.o arm-none-eabi-gcc -T linker.ld -o $@ $(ELFFLAGS) $^ loader.elf : boot.o loader_stage1.o loader_stage2.o uart.o diff --git a/demo_functionality.c b/demo_functionality.c index 19a5c38..aedab08 100644 --- a/demo_functionality.c +++ b/demo_functionality.c @@ -57,9 +57,9 @@ void demo_current_mode(void) ((short_section_descriptor_t volatile*) TRANSLATION_TABLE_BASE) extern char - __renamed_start, - __renamed_end, - __renamed_size; + __libkernel_start, + __libkernel_end, + __libkernel_size; void demo_setup_libkernel(void) { short_section_descriptor_t volatile *libkernel_section_entry = @@ -82,9 +82,9 @@ void demo_setup_libkernel(void) { "isb" :: "r" (0) : "memory"); // copy libkernel code to libkernel section - for (size_t i = 0; i < (size_t) &__renamed_size; i++) + for (size_t i = 0; i < (size_t) &__libkernel_size; i++) ((volatile char*) LIBKERNEL_SECTION_START)[i] = - (&__renamed_start)[i]; + (&__libkernel_start)[i]; } extern char @@ -146,7 +146,7 @@ void demo_setup_PL0(void) void demo_go_unprivileged(void) { size_t call_unprivileged_offset = - (size_t) &call_unprivileged - (size_t) &__renamed_start; + (size_t) &call_unprivileged - (size_t) &__libkernel_start; void *call_unprivileged_new_location = (void*) (LIBKERNEL_SECTION_START + call_unprivileged_offset); diff --git a/linker.ld b/linker.ld index 01eda35..387e7c7 100644 --- a/linker.ld +++ b/linker.ld @@ -1,18 +1,21 @@ -ENTRY(_start) +ENTRY(_start) /* defined in boot.S */ SECTIONS { - /* Starts at LOADER_ADDR, which is 0x8000 - that's where RPis in */ - /* 32-bit mode load kernel at. */ + /* Starts at 0x8000 - that's where RPis in 32-bit mode load kernel at. */ /* My experiments do, however, show, that qemu emulating RPi2 */ /* loads the kernel at 0x10000! (took some pain to find out). */ - /* Since we're using a bootloader now, we can compile the kernel */ - /* for 0x8000 and bootloader will load it properly (although it */ - /* itself still has to be compiled for 0x10000) */ - /* rpi-open-firmware, ont he other hand, loads kernel at 0x2000000 */ - /* This issue is also to be avoided by the use of bootloader */ + /* rpi-open-firmware, on the other hand, loads kernel at 0x2000000! */ + /* This is not really a problem, since: */ + /* 1. We can use out bootloader to load the kernel at 0x8000 */ + /* 2. We compile kernel with -fPIC, so it should be able to work with */ + /* any load addr. + /* 3. In qemu, we can load kernel.elf instead of raw binary */ + /* (qemu will do the loading then) */ + . = 0x8000; - /* For AArch64, use . = 0x80000; Unless this too is wrong in qemu… */ + + /* For AArch64, use . = 0x80000 */ __start = .; __text_start = .; @@ -51,44 +54,14 @@ SECTIONS __bss_size = __bss_end - __bss_start; - - /* Here come the definitions for renamed sections */ - __renamed_start = .; - __renamed_text_start = .; - .renamed_text : - { - *(.renamed_text) - } - . = ALIGN(4096); /* align to page size */ - __renamed_text_end = .; - - __renamed_rodata_start = .; - .renamed_rodata : - { - *(.renamed_rodata) - } - . = ALIGN(4096); /* align to page size */ - __renamed_rodata_end = .; - - __renamed_data_start = .; - .renamed_data : - { - *(.renamed_data) - } - . = ALIGN(4096); /* align to page size */ - __renamed_data_end = .; - - __renamed_bss_start = .; - .renamed_bss : + /* libkernel */ + .libkernel : { - renamed_bss = .; - *(.renamed_bss) + __libkernel_start = .; + libkernel.o + __libkernel_end = .; } - . = ALIGN(4096); /* align to page size */ - __renamed_bss_end = .; - __renamed_bss_size = __renamed_bss_end - __renamed_bss_start; - __renamed_end = .; - __renamed_size = __renamed_end - __renamed_start; + __libkernel_size = __libkernel_end - __libkernel_start; . = ALIGN(4096); /* align to page size */ -- cgit v1.2.3 From 6bdfd7c992b2345f964dc6bc5dbb4189e36a5e99 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Fri, 27 Dec 2019 22:09:21 +0100 Subject: add userspace tool for creating ramfs image --- .gitignore | 1 + Makefile | 3 +++ makefs.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 makefs.c (limited to 'Makefile') diff --git a/.gitignore b/.gitignore index db6eb43..480597b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.elf *.img pipe_image +makefs diff --git a/Makefile b/Makefile index d782eda..3d6b095 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,9 @@ run-on-rpi : kernel.img pipe_image pipe_image : pipe_image.c lib/rs232/rs232.c gcc -Wall -std=gnu99 -O3 $^ -o $@ +makefs : makefs.c + gcc -Wall -std=gnu99 -O3 $^ -o $@ + clean : -rm *.img *.elf *.o pipe_image diff --git a/makefs.c b/makefs.c new file mode 100644 index 0000000..7d5b148 --- /dev/null +++ b/makefs.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ANSI_FG_RED "\033[0;31m" +#define ANSI_FG_DEFAULT "\033[0;39m" + +int main(int argc, char **argv) +{ + for (int i = 1; i < argc; i++) + { + struct stat fileinfo; + + if (stat(argv[i], &fileinfo)) + err(-1, "couldn't stat " ANSI_FG_RED "%s" ANSI_FG_DEFAULT, + argv[i]); + + if (!S_ISREG(fileinfo.st_mode)) + errx(-1, ANSI_FG_RED "%s" ANSI_FG_DEFAULT + " is not a regular file.", argv[i]); + + if (fileinfo.st_size > UINT32_MAX) + errx(-1, ANSI_FG_RED "%s" ANSI_FG_DEFAULT + " is too big.", argv[i]); + + uint32_t file_size = fileinfo.st_size; + uint32_t name_size = strlen(argv[i]); + + if (fwrite(&name_size, 4, 1, stdout) != 1) + errx(-1, "error writing to stdout"); + + if (printf("%s", argv[i]) != name_size) + errx(-1, "error writing to stdout"); + + if (fwrite(&file_size, 4, 1, stdout) != 1) + errx(-1, "error writing to stdout"); + + if (fflush(stdout)) + err(-1, "couldn't flush stdout"); + + pid_t pid; + int wstatus; + switch (pid = fork()) + { + case -1: + err(-1, "couldn't fork"); + case 0: + if (execlp("cat", "cat", argv[i], NULL)) + err(-1, "couldn't execute cat"); + default: + if (wait(&wstatus) == -1) + err(-1, "error waiting for child"); + + if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus)) + exit(-1); + } + } + + return 0; +} + -- cgit v1.2.3 From 826d5b3ba04a8b830c8a92a6f69ca4e223257c5b Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Sat, 28 Dec 2019 14:01:14 +0100 Subject: place PL_0_test.img in ramfs instead of embedding it directly --- Makefile | 7 ++++++- demo_functionality.c | 21 ++++++++++++++++----- linker.ld | 4 +++- 3 files changed, 25 insertions(+), 7 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 3d6b095..b8800cc 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,8 @@ ARM_OBJECTS=kernel.o paging.o demo_functionality.o PL0_test.o uart.o loader_stag EMBEDDABLE_OBJECTS=PL_0_test_embeddable.o loader_stage2_embeddable.o +RAMFS_FILES=PL_0_test.img + all : kernel7.img %.o : %.c @@ -22,7 +24,7 @@ all : kernel7.img PL_0_test.elf : PL0_test.o uart.o arm-none-eabi-gcc -T PL0_test.ld -o $@ $(ELFFLAGS) $^ -kernel.elf : boot.o kernel.o uart.o PL_0_test_embeddable.o demo_functionality.o paging.o libkernel.o interrupt_vector.o interrupts.o +kernel.elf : boot.o kernel.o uart.o demo_functionality.o paging.o libkernel.o interrupt_vector.o interrupts.o ramfs_embeddable.o ramfs.o arm-none-eabi-gcc -T linker.ld -o $@ $(ELFFLAGS) $^ loader.elf : boot.o loader_stage1.o loader_stage2.o uart.o @@ -49,6 +51,9 @@ pipe_image : pipe_image.c lib/rs232/rs232.c makefs : makefs.c gcc -Wall -std=gnu99 -O3 $^ -o $@ +ramfs.img : makefs $(RAMFS_FILES) + ./makefs $(RAMFS_FILES) > $@ + clean : -rm *.img *.elf *.o pipe_image diff --git a/demo_functionality.c b/demo_functionality.c index c6f7e5c..206dd2b 100644 --- a/demo_functionality.c +++ b/demo_functionality.c @@ -3,6 +3,7 @@ #include "memory.h" #include "translation_table_descriptors.h" #include "libkernel.h" +#include "ramfs.h" void demo_paging_support(void) { @@ -88,12 +89,22 @@ void demo_setup_libkernel(void) { } extern char - _binary_PL_0_test_img_start, - _binary_PL_0_test_img_end, - _binary_PL_0_test_img_size; + _binary_ramfs_img_start, + _binary_ramfs_img_end, + _binary_ramfs_img_size; void demo_setup_PL0(void) { + // find PL_0_test.img im ramfs + struct ramfile PL_0_test_img; + + if (find_file(&_binary_ramfs_img_start, "PL_0_test.img", + &PL_0_test_img)) + { + uart_puts("PL_0_test.img not found :(\r\n"); + asm volatile ("wfi"); + } + short_section_descriptor_t volatile *PL0_section_entry = &TRANSLATION_TABLE[PL0_SECTION_NUMBER]; short_section_descriptor_t volatile *UART_memory_section_entry = @@ -136,9 +147,9 @@ void demo_setup_PL0(void) uart_puts((char*) VIRTUAL_PL0_MEMORY_START); // now paste a userspace program to that section - for (size_t i = 0; i < (size_t) &_binary_PL_0_test_img_size; i++) + for (uint32_t i = 0; i < PL_0_test_img.file_size; i++) ((volatile char*) VIRTUAL_PL0_MEMORY_START)[i] = - (&_binary_PL_0_test_img_start)[i]; + PL_0_test_img.file_contents[i]; uart_puts("copied PL0 and libkernel code to their sections\n\r"); } diff --git a/linker.ld b/linker.ld index e0e72b2..7b585dc 100644 --- a/linker.ld +++ b/linker.ld @@ -23,7 +23,9 @@ SECTIONS { __kernel_start = .; KEEP(boot.o) - *(EXCLUDE_FILE (libkernel.o interrupt_vector.o interrupts.o) *) + *(EXCLUDE_FILE (ramfs_embeddable.o libkernel.o interrupt_vector.o interrupts.o) *) + . = ALIGN(4); + ramfs_embeddable.o __kernel_end = .; } __kernel_size = __kernel_end - __kernel_start; -- cgit v1.2.3 From c374eaacbc0d16983bbd1112fefa74dbe88e1e53 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Sat, 28 Dec 2019 15:55:29 +0100 Subject: use ldm instruction instead of libkernel for entering user mode; get rid of libkernel (no longer needed) --- Makefile | 2 +- demo_functionality.c | 69 +++++++++++++++++----------------------------------- kernel.c | 2 -- libkernel.c | 12 --------- libkernel.h | 6 ----- linker.ld | 11 --------- memory.h | 13 ++-------- psr.h | 15 ++++++++++++ 8 files changed, 40 insertions(+), 90 deletions(-) delete mode 100644 libkernel.c delete mode 100644 libkernel.h (limited to 'Makefile') diff --git a/Makefile b/Makefile index b8800cc..55d0d1b 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ all : kernel7.img PL_0_test.elf : PL0_test.o uart.o arm-none-eabi-gcc -T PL0_test.ld -o $@ $(ELFFLAGS) $^ -kernel.elf : boot.o kernel.o uart.o demo_functionality.o paging.o libkernel.o interrupt_vector.o interrupts.o ramfs_embeddable.o ramfs.o +kernel.elf : boot.o kernel.o uart.o demo_functionality.o paging.o interrupt_vector.o interrupts.o ramfs_embeddable.o ramfs.o arm-none-eabi-gcc -T linker.ld -o $@ $(ELFFLAGS) $^ loader.elf : boot.o loader_stage1.o loader_stage2.o uart.o diff --git a/demo_functionality.c b/demo_functionality.c index 206dd2b..4b002d6 100644 --- a/demo_functionality.c +++ b/demo_functionality.c @@ -2,7 +2,6 @@ #include "psr.h" #include "memory.h" #include "translation_table_descriptors.h" -#include "libkernel.h" #include "ramfs.h" void demo_paging_support(void) @@ -57,37 +56,6 @@ void demo_current_mode(void) #define TRANSLATION_TABLE \ ((short_section_descriptor_t volatile*) TRANSLATION_TABLE_BASE) -extern char - __libkernel_start, - __libkernel_end, - __libkernel_size; - -void demo_setup_libkernel(void) { - short_section_descriptor_t volatile *libkernel_section_entry = - &TRANSLATION_TABLE[LIBKERNEL_SECTION_NUMBER]; - - short_section_descriptor_t - libkernel_section = *libkernel_section_entry; - - // make libkernel section executable and read-only for - // unprivileged code - libkernel_section.ACCESS_PERMISSIONS_2 = - AP_2_0_MODEL_RW_PL1_RO_PL0 >> 2; - libkernel_section.ACCESS_PERMISSIONS_1_0 = - AP_2_0_MODEL_RW_PL1_RO_PL0 & 0b011; - - *libkernel_section_entry = libkernel_section; - - // invalidate main Translation Lookup Buffer (just in case) - asm("mcr p15, 0, %0, c8, c7, 0\n\r" - "isb" :: "r" (0) : "memory"); - - // copy libkernel code to libkernel section - for (size_t i = 0; i < (size_t) &__libkernel_size; i++) - ((volatile char*) LIBKERNEL_SECTION_START)[i] = - (&__libkernel_start)[i]; -} - extern char _binary_ramfs_img_start, _binary_ramfs_img_end, @@ -151,27 +119,34 @@ void demo_setup_PL0(void) ((volatile char*) VIRTUAL_PL0_MEMORY_START)[i] = PL_0_test_img.file_contents[i]; - uart_puts("copied PL0 and libkernel code to their sections\n\r"); + uart_puts("copied PL0 code to it's section\n\r"); +} + +// needed for array initialization in demo_go_unprivileged() +void *memset(void *s, int c, size_t n) +{ + char *mem = s; + + for (size_t i = 0; i < n; i++) + mem[i] = c; + + return s; } void demo_go_unprivileged(void) { - size_t call_unprivileged_offset = - (size_t) &call_unprivileged - (size_t) &__libkernel_start; + uint32_t PL0_regs[16] = {0}; + PL0_regs[15] = VIRTUAL_PL0_MEMORY_START; // the new pc + PL0_regs[13] = (PL0_SECTION_NUMBER + 1) << 20; // the new sp + + PSR_t new_SPSR = read_CPSR(); + new_SPSR.fields.PSR_MODE_4_0 = MODE_USER; + write_SPSR(new_SPSR); - void *call_unprivileged_new_location = - (void*) (LIBKERNEL_SECTION_START + call_unprivileged_offset); + uart_puts("All ready, jumping to PL0 code\n\r"); - // call call_unprivileged from libkernel - asm volatile("mov r5, %0\n\r" - "mov r0, %1\n\r" - "mov r4, #0\n\r" - "movt r4, #"PL0_SECTION_NUMBER_STR"1111\n\r" - "mov sp, r4\n\r" // setting stack is important :D - "blx r5\n\r" :: - "r" (call_unprivileged_new_location), - "r" (VIRTUAL_PL0_MEMORY_START) - : "memory", "r4", "r5", "r0"); + asm volatile("ldm %0, {r0 - r15} ^" :: + "r" (PL0_regs)); } extern char diff --git a/kernel.c b/kernel.c index fc629e1..9cdb82a 100644 --- a/kernel.c +++ b/kernel.c @@ -27,8 +27,6 @@ void kernel_main(uint32_t r0, uint32_t r1, uint32_t atags) // prints some info and sets upp translation table, turns on MMU setup_flat_map(); - demo_setup_libkernel(); - demo_setup_PL0(); demo_setup_interrupts(); diff --git a/libkernel.c b/libkernel.c deleted file mode 100644 index 0955e0e..0000000 --- a/libkernel.c +++ /dev/null @@ -1,12 +0,0 @@ -// This is the privileged code, that gets placed somewhere in -// unprivileged process' address space. It's compiled as relocatable, -// so it can be loaded at different address for different processes. - -void call_unprivileged(void (*address) (void)) { - // switch to PL0 - asm("cps #0b10000\n\r" - "isb" ::: "memory"); - - // jump to that unprivileged code - address(); -} diff --git a/libkernel.h b/libkernel.h deleted file mode 100644 index a8af348..0000000 --- a/libkernel.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef LIBKERNEL_H -#define LIBKERNEL_H - -void call_unprivileged(void (*address) (void)); - -#endif // LIBKERNEL_H diff --git a/linker.ld b/linker.ld index 7b585dc..673d7e2 100644 --- a/linker.ld +++ b/linker.ld @@ -30,17 +30,6 @@ SECTIONS } __kernel_size = __kernel_end - __kernel_start; - - /* libkernel */ - .libkernel : - { - __libkernel_start = .; - libkernel.o - __libkernel_end = .; - } - __libkernel_size = __libkernel_end - __libkernel_start; - - .interrupts : { __interrupts_start = .; diff --git a/memory.h b/memory.h index 1c9ae35..d369694 100644 --- a/memory.h +++ b/memory.h @@ -24,24 +24,15 @@ extern const char __start; #define TRANSLATION_TABLE_END \ (TRANSLATION_TABLE_BASE + (uint32_t) (4096 * 4)) -#define LIBKERNEL_SECTION_START \ - (((TRANSLATION_TABLE_END - (uint32_t) 1) & ~((uint32_t) 0xfffff)) \ +#define PRIVILEGED_MEMORY_END \ + (((TRANSLATION_TABLE_END - (uint32_t) 1) & ~((uint32_t) 0xfffff)) \ + SECTION_SIZE) -#define LIBKERNEL_SECTION_END \ - (LIBKERNEL_SECTION_START + SECTION_SIZE) - -// section for libkernel is flat-mapped -#define LIBKERNEL_SECTION_NUMBER (LIBKERNEL_SECTION_START >> 20) - -#define PRIVILEGED_MEMORY_END LIBKERNEL_SECTION_END - #define UNPRIVILEGED_MEMORY_START PRIVILEGED_MEMORY_END #define UNPRIVILEGED_MEMORY_END \ (UNPRIVILEGED_MEMORY_START + SECTION_SIZE) #define PL0_SECTION_NUMBER ((uint32_t) 0b101010101010) -#define PL0_SECTION_NUMBER_STR "0b101010101010" #define VIRTUAL_PL0_MEMORY_START (PL0_SECTION_NUMBER << 20) diff --git a/psr.h b/psr.h index 9809fc4..b2adafb 100644 --- a/psr.h +++ b/psr.h @@ -64,4 +64,19 @@ inline static PSR_t read_CPSR(void) return CPSR; } +inline static PSR_t read_SPSR(void) +{ + PSR_t SPSR; + // get content of saved program status register + asm("mrs %0, spsr" : "=r" (SPSR.raw) :: "memory"); + + return SPSR; +} + +inline static void write_SPSR(PSR_t SPSR) +{ + // set content of saved program status register + asm("msr spsr, %0" :: "r" (SPSR.raw)); +} + #endif // PSR_H -- cgit v1.2.3 From f05be08f8ef6c9f0f698c14a93559968044c987c Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Sat, 28 Dec 2019 16:48:25 +0100 Subject: remove makefs executable when running `make clean` --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 55d0d1b..1311773 100644 --- a/Makefile +++ b/Makefile @@ -55,6 +55,6 @@ ramfs.img : makefs $(RAMFS_FILES) ./makefs $(RAMFS_FILES) > $@ clean : - -rm *.img *.elf *.o pipe_image + -rm *.img *.elf *.o pipe_image makefs .PHONY: all qemu-elf qemu-bin clean -- cgit v1.2.3 From 6321cd922cb665ca5bfb9bc2025701b07c076894 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Sat, 28 Dec 2019 16:51:03 +0100 Subject: minor changes to Makefile --- Makefile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 1311773..92d200f 100644 --- a/Makefile +++ b/Makefile @@ -3,11 +3,9 @@ ELFFLAGS=-nostdlib -lgcc ARM_OBJECTS=kernel.o paging.o demo_functionality.o PL0_test.o uart.o loader_stage1.o loader_stage2.o -EMBEDDABLE_OBJECTS=PL_0_test_embeddable.o loader_stage2_embeddable.o - RAMFS_FILES=PL_0_test.img -all : kernel7.img +all : kernel.img %.o : %.c arm-none-eabi-gcc $(CFLAGS) -c $^ -o $@ -- cgit v1.2.3 From f7ddd1a356f58298d703b11e862d3d6127e67080 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Sat, 28 Dec 2019 18:09:09 +0100 Subject: rewrite the stage 1 of bootloader in a more compact and (mostly) load-addr-independent way --- Makefile | 12 ++++++--- loader_linker.ld | 72 ------------------------------------------------- loader_stage1.S | 55 +++++++++++++++++++++++++++++++++++++ loader_stage1.c | 26 ------------------ loader_stage1_linker.ld | 16 +++++++++++ loader_stage2.c | 3 +-- loader_stage2_linker.ld | 16 +++++++++++ 7 files changed, 97 insertions(+), 103 deletions(-) delete mode 100644 loader_linker.ld create mode 100644 loader_stage1.S delete mode 100644 loader_stage1.c create mode 100644 loader_stage1_linker.ld create mode 100644 loader_stage2_linker.ld (limited to 'Makefile') diff --git a/Makefile b/Makefile index 92d200f..29efa86 100644 --- a/Makefile +++ b/Makefile @@ -25,8 +25,14 @@ PL_0_test.elf : PL0_test.o uart.o kernel.elf : boot.o kernel.o uart.o demo_functionality.o paging.o interrupt_vector.o interrupts.o ramfs_embeddable.o ramfs.o arm-none-eabi-gcc -T linker.ld -o $@ $(ELFFLAGS) $^ -loader.elf : boot.o loader_stage1.o loader_stage2.o uart.o - arm-none-eabi-gcc -T loader_linker.ld -o $@ $(ELFFLAGS) $^ +loader_stage2.elf : loader_stage2.o uart.o + arm-none-eabi-gcc -T loader_stage2_linker.ld -o $@ $(ELFFLAGS) $^ + +loader_stage1.o : loader_stage1.S loader_stage2.img + arm-none-eabi-as -mcpu=cortex-a7 $< -o $@ + +loader.elf : loader_stage1.o + arm-none-eabi-gcc -T loader_stage1_linker.ld -o $@ $(ELFFLAGS) $^ loader.img : loader.elf arm-none-eabi-objcopy $^ -O binary $@ @@ -36,7 +42,7 @@ loader.img : loader.elf qemu-elf : kernel.elf qemu-system-arm -m 256 -M raspi2 -serial stdio -kernel $^ -qemu-bin : loader.elf kernel.img pipe_image +qemu-bin : loader.img kernel.img pipe_image ./pipe_image --stdout | qemu-system-arm -m 256 -M raspi2 -serial stdio -kernel $< run-on-rpi : kernel.img pipe_image diff --git a/loader_linker.ld b/loader_linker.ld deleted file mode 100644 index 45c88ff..0000000 --- a/loader_linker.ld +++ /dev/null @@ -1,72 +0,0 @@ -ENTRY(_boot) - -SECTIONS -{ - /* Starts at LOADER_ADDR. */ - /* Warning! Internet says RPis in 32-bit mode load binary at 0x8000! */ - /* My experiments do, however, show, that qemu emulating RPi2 */ - /* loads it at 0x10000! (took some pain to find out) */ - - /* . = 0x10000; */ - - /* rpi-open-firmware, on the other hand, loads it at 0x2000000 */ - /* (and this should be not-so-hard to change by modifying the */ - /* firmware) */ - - . = 0x2000000; - - /* For AArch64, use . = 0x80000; Unless this too is wrong */ - - __start = .; - __text_stage1_start = .; - .text_stage1 : - { - KEEP(*(.text.boot)) - *(.text.boot) - *(.text.stage1main) - } - . = ALIGN(4096); /* align to page size */ - __text_stage1_end = .; - - __stage2_start = .; - __text_start = .; - .text : - { - KEEP(*(.text.stage2main)) - *(.text.boot) - } - . = ALIGN(4096); /* align to page size */ - __text_end = .; - - __rodata_start = .; - .rodata : - { - *(.rodata) - } - . = ALIGN(4096); /* align to page size */ - __rodata_end = .; - - __data_start = .; - .data : - { - *(.data) - } - . = ALIGN(4096); /* align to page size */ - __data_end = .; - - __bss_start = .; - .bss : - { - bss = .; - *(.bss) - } - . = ALIGN(4096); /* align to page size */ - __bss_end = .; - __bss_size = __bss_end - __bss_start; - - __stage2_end = .; - __stage2_size = __stage2_end - __stage2_start; - __end = .; - - _stack_top = 0x4000; -} diff --git a/loader_stage1.S b/loader_stage1.S new file mode 100644 index 0000000..adf2e12 --- /dev/null +++ b/loader_stage1.S @@ -0,0 +1,55 @@ +// armv7 mode + +// Entry point for the kernel. +// r15 -> should begin execution at 0x8000. +// r0 -> 0x00000000 +// r1 -> 0x00000C42 +// r2 -> 0x00000100 - start of ATAGS +// preserve these registers as argument for kernel + +.global _boot +_boot: + // Only let the first core execute + mrc p15, 0, r3, c0, c0, 5 + and r3, r3, #3 + cmp r3, #0 + beq proceed + // this is a kind of blef - races can theoretically still occur + // when the main core overwrites this part of memory + wfe + +proceed: + // copy stage2 of the loader to address 0x4000 + + // first, load address of stage2_start to r3 (a PIC way) + adr r3, stage2_start + + // load destination address for stage2 code to r4 + mov r4, #0x4000 + + // load blob size to r5 + mov r5, #(stage2_end - stage2_start) + + // r6 is the counter - counts the bytes copied + mov r6, #0 + + // each word of the blob is loaded to r7 and stored + // from r7 to it's destination in a loop +loop: + ldr r7, [r3, r6] + str r7, [r4, r6] + add r6, r6, #4 + cmp r6, r5 + blo loop + + // Initialize the stack + // _stack_top is defined in loader_stage1_linker.ld + ldr sp, =_stack_top + + // Call stage2 of the loader (branch to 0x4000) + bx r4 + +.align 4 +stage2_start: + .incbin "loader_stage2.img" +stage2_end: diff --git a/loader_stage1.c b/loader_stage1.c deleted file mode 100644 index aca439c..0000000 --- a/loader_stage1.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include -#include - -#define STAGE2_ADDR ((volatile char*) 0x4000) - -// There's one tricky thing about managing executable's own code. -// Mainly, symbols are visible to c code as extern chars, but the actual -// values are their adresses... see the code below -extern char - __stage2_start, - __stage2_end, - __stage2_size; - -__attribute__((section(".text.stage1main"))) -void kernel_main(uint32_t r0, uint32_t r1, uint32_t atags) -{ - // stage2 of the bootloader is part of executable; - // copy it over to it's destination place - // TODO implement a memcpy() somewhere and use it instead of loops - for (size_t i = 0; i < (size_t) &__stage2_size; i++) - STAGE2_ADDR[i] = (&__stage2_start)[i]; - - // jump to stage2 - ((void(*)(uint32_t, uint32_t, uint32_t))STAGE2_ADDR)(r0, r1, atags); -} diff --git a/loader_stage1_linker.ld b/loader_stage1_linker.ld new file mode 100644 index 0000000..711fcbf --- /dev/null +++ b/loader_stage1_linker.ld @@ -0,0 +1,16 @@ +ENTRY(_boot) + +SECTIONS +{ + /* see linker.ld for details */ + . = 0x2000000; + + __start = .; + loader_stage1 : + { + KEEP(loader_stage1.o) + } + __end = .; + + _stack_top = 0x8000; +} diff --git a/loader_stage2.c b/loader_stage2.c index 7b5087a..65dda0a 100644 --- a/loader_stage2.c +++ b/loader_stage2.c @@ -5,8 +5,7 @@ void *const kernel_load_addr = ((void*) 0x8000); -void __attribute__((section(".text.stage2main"))) -stage2(uint32_t r0, uint32_t r1, uint32_t atags) +void _stage2_main(uint32_t r0, uint32_t r1, uint32_t atags) { // Declare as unused (void) r0; diff --git a/loader_stage2_linker.ld b/loader_stage2_linker.ld new file mode 100644 index 0000000..33e79e9 --- /dev/null +++ b/loader_stage2_linker.ld @@ -0,0 +1,16 @@ +ENTRY(_stage2_main) + +SECTIONS +{ + /* see loader_stage1.S for details */ + . = 0x4000; + + __start = .; + loader_stage2 : + { + KEEP(loader_stage2.o(.text)) + loader_stage2.o + uart.o + } + __end = .; +} -- cgit v1.2.3 From 700f4c412d42c9b9811269045c0e363a0331bba9 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Sat, 28 Dec 2019 21:54:42 +0100 Subject: split kernel into 2 stages; second stage gets copied to 0x0 and runs from there --- Makefile | 29 ++++++++++++++++++----- boot.S | 27 --------------------- demo_functionality.c | 26 ++------------------ interrupt_vector.S | 53 +++++++++++++++++++++++++++++------------ interrupts.c | 55 +++++++++++++++++++++++++++++++++++------- kernel.c | 44 ---------------------------------- kernel_stage1.S | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ kernel_stage1.ld | 27 +++++++++++++++++++++ kernel_stage2.ld | 52 ++++++++++++++++++++++++++++++++++++++++ linker.ld | 47 ------------------------------------ memory.h | 43 ++++++++++++++++++++++----------- setup.c | 41 ++++++++++++++++++++++++++++++++ 12 files changed, 325 insertions(+), 186 deletions(-) delete mode 100644 boot.S delete mode 100644 kernel.c create mode 100644 kernel_stage1.S create mode 100644 kernel_stage1.ld create mode 100644 kernel_stage2.ld delete mode 100644 linker.ld create mode 100644 setup.c (limited to 'Makefile') diff --git a/Makefile b/Makefile index 29efa86..68368a5 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,10 @@ -CFLAGS=-mcpu=cortex-a7 -ffreestanding -std=gnu11 -Wall -Wextra -O2 -fPIC -I. +CFLAGS=-mcpu=cortex-a7 -ffreestanding -std=gnu11 -Wall -Wextra -O2 -I. ELFFLAGS=-nostdlib -lgcc ARM_OBJECTS=kernel.o paging.o demo_functionality.o PL0_test.o uart.o loader_stage1.o loader_stage2.o +KERNEL_STAGE2_OBJECTS=setup.o interrupt_vector.o interrupts.o uart.o demo_functionality.o paging.o ramfs_embeddable.o ramfs.o + RAMFS_FILES=PL_0_test.img all : kernel.img @@ -10,20 +12,32 @@ all : kernel.img %.o : %.c arm-none-eabi-gcc $(CFLAGS) -c $^ -o $@ -%.o : %.S - arm-none-eabi-as -mcpu=cortex-a7 $^ -o $@ - %.img : %.elf arm-none-eabi-objcopy $^ -O binary $@ +%.o : %.S + arm-none-eabi-as -mcpu=cortex-a7 $^ -o $@ + %_embeddable.o : %.img arm-none-eabi-objcopy -I binary -O elf32-littlearm -B arm --rename-section .data=.rodata $^ $@ PL_0_test.elf : PL0_test.o uart.o arm-none-eabi-gcc -T PL0_test.ld -o $@ $(ELFFLAGS) $^ -kernel.elf : boot.o kernel.o uart.o demo_functionality.o paging.o interrupt_vector.o interrupts.o ramfs_embeddable.o ramfs.o - arm-none-eabi-gcc -T linker.ld -o $@ $(ELFFLAGS) $^ +kernel_stage1.o : kernel_stage1.S kernel_stage2.img + arm-none-eabi-as -mcpu=cortex-a7 $< -o $@ + +kernel.elf : kernel_stage1.ld kernel_stage1.o + arm-none-eabi-gcc -T $< -o $@ $(ELFFLAGS) kernel_stage1.o + +kernel.img : kernel.elf + arm-none-eabi-objcopy $^ -O binary $@ + +kernel_stage2.elf : kernel_stage2.ld $(KERNEL_STAGE2_OBJECTS) + arm-none-eabi-gcc -T $< -o $@ $(ELFFLAGS) $(KERNEL_STAGE2_OBJECTS) + +#kernel.elf : boot.o kernel.o uart.o demo_functionality.o paging.o interrupt_vector.o interrupts.o ramfs_embeddable.o ramfs.o +# arm-none-eabi-gcc -T linker.ld -o $@ $(ELFFLAGS) $^ loader_stage2.elf : loader_stage2.o uart.o arm-none-eabi-gcc -T loader_stage2_linker.ld -o $@ $(ELFFLAGS) $^ @@ -42,6 +56,9 @@ loader.img : loader.elf qemu-elf : kernel.elf qemu-system-arm -m 256 -M raspi2 -serial stdio -kernel $^ +qemu-img : kernel.img + qemu-system-arm -m 256 -M raspi2 -serial stdio -kernel $^ + qemu-bin : loader.img kernel.img pipe_image ./pipe_image --stdout | qemu-system-arm -m 256 -M raspi2 -serial stdio -kernel $< diff --git a/boot.S b/boot.S deleted file mode 100644 index 593ed11..0000000 --- a/boot.S +++ /dev/null @@ -1,27 +0,0 @@ -// armv7 mode - -// Entry point for the kernel. -// r15 -> should begin execution at 0x8000. -// r0 -> 0x00000000 -// r1 -> 0x00000C42 -// r2 -> 0x00000100 - start of ATAGS -// preserve these registers as argument for kernel_main - -.global _boot // make entry point label global -_boot: - // Only let the first core execute - mrc p15, 0, r3, c0, c0, 5 - and r3, r3, #3 - cmp r3, #0 - beq proceed - // this is a kind of blef - races can theoretically still occur - // when the main core overwrites this part of memory - wfe - -proceed: - // Initialize the stack (_stack_top is defined in linker.ld) - ldr sp, =_stack_top - - // Call kernel_main - ldr r3, =kernel_main - bx r3 diff --git a/demo_functionality.c b/demo_functionality.c index 4b002d6..420639b 100644 --- a/demo_functionality.c +++ b/demo_functionality.c @@ -53,7 +53,7 @@ void demo_current_mode(void) uart_puts(mode_name); } -#define TRANSLATION_TABLE \ +#define TRANSLATION_TABLE \ ((short_section_descriptor_t volatile*) TRANSLATION_TABLE_BASE) extern char @@ -144,29 +144,7 @@ void demo_go_unprivileged(void) write_SPSR(new_SPSR); uart_puts("All ready, jumping to PL0 code\n\r"); - + asm volatile("ldm %0, {r0 - r15} ^" :: "r" (PL0_regs)); } - -extern char - __interrupts_start, - __interrupts_end, - __interrupts_size; - -extern void (*volatile system_reentry_point)(void); - -void system_reentry(void) -{ - uart_puts("re-entered system"); - while(1); -} - -void demo_setup_interrupts(void) -{ - system_reentry_point = system_reentry; - - for (size_t i = 0; i < (size_t) &__interrupts_size; i++) - ((volatile char*) 0)[i] = - (&__interrupts_start)[i]; -} diff --git a/interrupt_vector.S b/interrupt_vector.S index d20bf6d..6037b7c 100644 --- a/interrupt_vector.S +++ b/interrupt_vector.S @@ -1,22 +1,45 @@ -.section ".interrupts.vector" - -.global abort_handler -.local generic_handler -.global _interrupt_vectors _interrupt_vectors: - b generic_handler - b generic_handler - b generic_handler + b reset_handler_caller + b undef_handler_caller + b svc_handler_caller b abort_handler_caller b abort_handler_caller - b generic_handler - b generic_handler - -.section ".interrupts.text" + b generic_handler_caller + b irq_handler_caller + b fiq_handler_caller + +reset_handler_caller: + ldr sp, =_stack_top + ldr r5, =reset_handler + bx r5 + +undef_handler_caller: + ldr sp, =_stack_top + ldr r5, =undefined_instruction_vector + bx r5 -generic_handler: - b generic_handler +svc_handler_caller: + ldr sp, =_stack_top + ldr r5, =supervisor_call_handler + bx r5 + abort_handler_caller: - mov sp, #0x8000 + ldr sp, =_stack_top ldr r5, =abort_handler bx r5 + +generic_handler_caller: + ldr sp, =_stack_top + ldr r5, =generic_handler + bx r5 + +irq_handler_caller: + ldr sp, =_stack_top + ldr r5, =irq_handler + bx r5 + +fiq_handler_caller: + ldr sp, =_stack_top + ldr r5, =fiq_handler + bx r5 + diff --git a/interrupts.c b/interrupts.c index 6952f89..1b0590a 100644 --- a/interrupts.c +++ b/interrupts.c @@ -1,10 +1,20 @@ #include "uart.h" -/** - @brief The undefined instruction interrupt handler - If an undefined instruction is encountered, the CPU will start - executing this function. Just trap here as a debug solution. -*/ +void setup(void); + +void reset_handler(void) +{ + static _Bool setup_done; + + if (!setup_done) + setup(); + + setup_done = 1; + + // TODO do something here + while(1); +} + void __attribute__((interrupt("UNDEF"))) __attribute__((section(".interrupts.text"))) @@ -17,13 +27,40 @@ undefined_instruction_vector(void) } } -void __attribute__((section(".interrupts.data"))) -(*system_reentry_point) (void); +void supervisor_call_handler(void) +{ + uart_puts("something svc happened\n\r"); + + while(1); +} void __attribute__((interrupt("ABORT"))) -__attribute__((section(".interrupts.text"))) abort_handler(void) { - system_reentry_point(); + uart_puts("re-entered system\n\r"); + + while(1); +} + +void generic_handler(void) +{ + uart_puts("something weird happened\n\r"); + + while(1); +} + +void irq_handler(void) +{ + uart_puts("irq happened\n\r"); + + while(1); +} + +void fiq_handler(void) +{ + uart_puts("fiq happened\n\r"); + + while(1); } + diff --git a/kernel.c b/kernel.c deleted file mode 100644 index 719ceff..0000000 --- a/kernel.c +++ /dev/null @@ -1,44 +0,0 @@ -#include "uart.h" -#include "demo_functionality.h" -#include "paging.h" - -void kernel_main(void) -{ - uart_init(); - - // When we attach screen session after loading kernel with socat - // we miss kernel's greeting... So we'll make the kernel wait for - // one char we're going to send from within screen - uart_getc(); - - uart_puts("Hello, kernel World!\r\n"); - - // prints some info - demo_paging_support(); - - // prints some info - demo_current_mode(); - - // prints some info and sets upp translation table, turns on MMU - setup_flat_map(); - - demo_setup_PL0(); - - demo_setup_interrupts(); - - // prints some info and sets up a section for PL0 code, loads a blob - // there and jumps to it... never, ever, ever returns - demo_go_unprivileged(); - - while(1); - - while (1) - { - char c = uart_getc(); - - uart_putc(c); - - if (c == '\r') - uart_putc('\n'); - } -} diff --git a/kernel_stage1.S b/kernel_stage1.S new file mode 100644 index 0000000..1e0f614 --- /dev/null +++ b/kernel_stage1.S @@ -0,0 +1,67 @@ +/* arm mode, cortex-a7 compatibility + * + * _boot is entry point for the kernel. + * + * Kernel copies it's embedded stage 2 to address 0x0 and jumps to + * it (to the reset handler). Registers r0 - r2 are arguments for + * the kernel, but we're not using them for now. + * + * This file is based on (and almost identical with) loader_stage1.S + */ + +.global _boot +_boot: + // Only let the first core execute + mrc p15, 0, r3, c0, c0, 5 + and r3, r3, #3 + cmp r3, #0 + beq proceed + // this is a kind of blef - races can theoretically still + // occur when the main core overwrites this part of memory + wfe + +proceed: + // copy stage2 of the kernel to address 0x0 + + // first, load address of stage2_start to r3 (a PIC way) + adr r3, stage2_start + + // load destination address for stage2 code to r4 + mov r4, #0 + + // load blob size to r5 + // The size might get too big for an immediate value, so + // we load it from memory. + adr r5, blob_size + ldr r5, [r5] + + // r6 is the counter - counts the bytes copied + mov r6, #0 + + // This initial piece of code might get overwritten when we + // copy stage2, so the actual copying loop shall be after + // stage2 blob. We want this asm code to be PIC, so we're + // computing address of stage2_end into r7. + add r7, r3, r5 + bx r7 + +blob_size: + .word stage2_end - stage2_start + +.align 4 +stage2_start: + .incbin "kernel_stage2.img" +stage2_end: + + // each word of the blob is loaded to r7 and stored + // from r7 to it's destination in a loop +loop: + ldr r7, [r3, r6] + str r7, [r4, r6] + add r6, r6, #4 + cmp r6, r5 + blo loop + + // Call stage2 of the kernel (branch to 0x0, + // which is the reset handler). + bx r4 diff --git a/kernel_stage1.ld b/kernel_stage1.ld new file mode 100644 index 0000000..3130634 --- /dev/null +++ b/kernel_stage1.ld @@ -0,0 +1,27 @@ +ENTRY(_boot) /* defined in boot.S; qemu needs it to run elf file */ + +/* Code starts at 0x8000 - that's where RPis in 32-bit mode load + * kernel at. My experiments do, however, show, that qemu emulating + * RPi2 loads the kernel at 0x10000! (took some pain to find out). + * rpi-open-firmware, on the other hand, loads kernel at 0x2000000! + * This is not really a problem, since: + * 1. We can use our bootloader to load the kernel at 0x8000 + * 2. We've rewritten stage 1 of both bootloader and kernel in + * careful assembly, so that they should work regardless of + * where they are loaded. + * 3. In qemu, we can load kernel.elf instead of raw binary + * (qemu will do the right thing then) + */ + +SECTIONS +{ + + . = 0x8000; + + __start = .; + .kernel_stage1 : + { + KEEP(kernel_stage1.o) + } + __end = .; +} diff --git a/kernel_stage2.ld b/kernel_stage2.ld new file mode 100644 index 0000000..d3a23bf --- /dev/null +++ b/kernel_stage2.ld @@ -0,0 +1,52 @@ +/* This sesond stage of the kernel is run from address 0x0 */ + +TRANSLATION_TABLE_SIZE = 4096 * 4; +MMU_SECTION_SIZE = 1 << 20; + +SECTIONS +{ + + . = 0x0; + + __start = .; + .kernel_stage2 : + { + KEEP(interrupt_vector.o) + . = ALIGN(4); + ramfs_embeddable.o + (*) + } + __end = .; + + . = ALIGN(1 << 14); + + .translation_table (NOLOAD) : + { + _translation_table_start = .; + + . = . + TRANSLATION_TABLE_SIZE; + + _translation_table_end = .; + } + + . = ALIGN(1 << 20); + . = . + MMU_SECTION_SIZE; + + .stack (NOLOAD) : + { + _stack_start = .; + + . = . + MMU_SECTION_SIZE; + + _stack_top = .; + } + + .unprivileged_memory (NOLOAD) : + { + _unprivileged_memory_start = .; + + . = . + MMU_SECTION_SIZE; + + _unprivileged_memory_end = .; + } +} diff --git a/linker.ld b/linker.ld deleted file mode 100644 index 444bbf6..0000000 --- a/linker.ld +++ /dev/null @@ -1,47 +0,0 @@ -ENTRY(_boot) /* defined in boot.S; qemu needs it to run elf file */ - -SECTIONS -{ - /* Starts at 0x8000 - that's where RPis in 32-bit mode load */ - /* kernel at. My experiments do, however, show, that qemu */ - /* emulating RPi2 loads the kernel at 0x10000! (took some pain */ - /* to find out). rpi-open-firmware, on the other hand, loads */ - /* kernel at 0x2000000! */ - /* This is not really a problem, since: */ - /* 1. We can use our bootloader to load the kernel at 0x8000 */ - /* 2. Stage 1 of the bootloader is written in careful */ - /* assembly, so that the loader itself should work */ - /* regardless of where it is loaded. */ - /* 3. In qemu, we can load kernel.elf instead of raw binary */ - /* (qemu will do the right thing then) */ - - . = 0x8000; - - /* RPi in 64-bit mode uses address 0x80000 instead */ - - __start = .; - .kernel : - { - __kernel_start = .; - KEEP(boot.o) - . = ALIGN(4); - ramfs_embeddable.o - *(EXCLUDE_FILE (libkernel.o interrupt_vector.o interrupts.o) *) - __kernel_end = .; - } - __kernel_size = __kernel_end - __kernel_start; - - .interrupts : - { - __interrupts_start = .; - KEEP(*(.interrupts.vector)) - interrupt_vector.o - interrupts.o - __interrupts_end = .; - } - __interrupts_size = __interrupts_end - __interrupts_start; - - __end = .; - - _stack_top = __start; -} diff --git a/memory.h b/memory.h index e4493e2..adc3bc0 100644 --- a/memory.h +++ b/memory.h @@ -1,7 +1,10 @@ #ifndef MEMORY_H #define MEMORY_H -#define POWER_OF_2(EXP) (((uint32_t) 1) << EXP) +// These macros were heavily used b4 I moved all the address +// computation to the linker script. Now I'm just keeping them +// in case they're needed for something else :) +#define POWER_OF_2(EXP) (((size_t) 1) << EXP) #define ALIGN_POWER_OF_2(ADDR, EXP) \ (((ADDR - 1) & ~(POWER_OF_2(EXP) - 1)) + POWER_OF_2(EXP)) @@ -10,33 +13,45 @@ #define ALIGN_SECTION(ADDR) ALIGN_POWER_OF_2(ADDR, 20) -#define INTERRUPT_VECTOR_TABLE_START ((uint32_t) 0x0) -#define STACK_START ((uint32_t) 0x4000) -#define STACK_END ((uint32_t) 0x8000) +// memory layout + +#define INTERRUPT_VECTOR_TABLE_START ((uint32_t) 0x0) +// all those symbols are defined in the linker script extern const char __end; extern const char __start; +extern const char _translation_table_start; +extern const char _translation_table_end; +extern const char _stack_start; +extern const char _stack_top; +extern const char _unprivileged_memory_start; +extern const char _unprivileged_memory_end; -#define KERNEL_START ((uint32_t) &__start) // this is 0x8000 -#define KERNEL_END ((uint32_t) &__end) +#define KERNEL_START ((size_t) &__start) // this is 0x0 +#define KERNEL_END ((size_t) &__end) // first 2^14 aligned address after the kernel -#define TRANSLATION_TABLE_BASE ALIGN_POWER_OF_2(KERNEL_END, 14) - -#define TRANSLATION_TABLE_END \ - (TRANSLATION_TABLE_BASE + (uint32_t) (4096 * 4)) +#define TRANSLATION_TABLE_BASE ((size_t) &_translation_table_start) +#define TRANSLATION_TABLE_END ((size_t) &_translation_table_end) -#define PRIVILEGED_MEMORY_END ALIGN_SECTION(TRANSLATION_TABLE_END) +// first section after the translation table is left unused; +// the next section is used as the stack +#define STACK_START ((size_t) &_stack_start) +#define STACK_END ((size_t) &_stack_top) -#define UNPRIVILEGED_MEMORY_START PRIVILEGED_MEMORY_END +#define PRIVILEGED_MEMORY_END STACK_END +#define UNPRIVILEGED_MEMORY_START \ + ((size_t) &_unprivileged_memory_start) // equal to STACK_END #define UNPRIVILEGED_MEMORY_END \ - (UNPRIVILEGED_MEMORY_START + SECTION_SIZE) + ((size_t) &_unprivileged_memory_end) -#define PL0_SECTION_NUMBER ((uint32_t) 0b101010101010) +#define PL0_SECTION_NUMBER ((size_t) 0xaaa) #define VIRTUAL_PL0_MEMORY_START (PL0_SECTION_NUMBER << 20) +#define VIRTUAL_PL0_MEMORY_END \ + (VIRTUAL_PL0_MEMORY_START + SECTION_SIZE) #endif // MEMORY_H diff --git a/setup.c b/setup.c new file mode 100644 index 0000000..48df825 --- /dev/null +++ b/setup.c @@ -0,0 +1,41 @@ +#include "uart.h" +#include "demo_functionality.h" +#include "paging.h" + +void setup(void) +{ + uart_init(); + + // When we attach screen session after loading kernel with socat + // we miss kernel's greeting... So we'll make the kernel wait for + // one char we're going to send from within screen + uart_getc(); + + uart_puts("Hello, kernel World!\r\n"); + + // prints some info + demo_paging_support(); + + // prints some info + demo_current_mode(); + + // prints some info and sets upp translation table, turns on MMU + setup_flat_map(); + + // prints some info and sets up a section for PL0 code, + // loads a blob there + demo_setup_PL0(); + + // jumps to unprivileged code... never, ever, ever returns + demo_go_unprivileged(); + + while (1) + { + char c = uart_getc(); + + uart_putc(c); + + if (c == '\r') + uart_putc('\n'); + } +} -- cgit v1.2.3 From dab51b1932cb82af152b0b6cbdb687083f79a290 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Sat, 28 Dec 2019 23:37:17 +0100 Subject: improve linking of PL0_test --- Makefile | 4 ++-- PL0_test.ld | 48 +++++++++--------------------------------------- 2 files changed, 11 insertions(+), 41 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 68368a5..e48dd2d 100644 --- a/Makefile +++ b/Makefile @@ -21,8 +21,8 @@ all : kernel.img %_embeddable.o : %.img arm-none-eabi-objcopy -I binary -O elf32-littlearm -B arm --rename-section .data=.rodata $^ $@ -PL_0_test.elf : PL0_test.o uart.o - arm-none-eabi-gcc -T PL0_test.ld -o $@ $(ELFFLAGS) $^ +PL_0_test.elf : PL0_test.ld PL0_test.o uart.o + arm-none-eabi-gcc -T $< -o $@ $(ELFFLAGS) PL0_test.o uart.o kernel_stage1.o : kernel_stage1.S kernel_stage2.img arm-none-eabi-as -mcpu=cortex-a7 $< -o $@ diff --git a/PL0_test.ld b/PL0_test.ld index 45bee5e..b1d06f4 100644 --- a/PL0_test.ld +++ b/PL0_test.ld @@ -1,49 +1,19 @@ -ENTRY(_start) - +/* linker script for creating the example userspace program PL0_test + */ + +/* no ENTRY() statement - this executable is run by jumping to it */ + SECTIONS { - /* 0b10101010101000000000000000000000 */ + /* my thought up address userspace programs should run from */ . = 0xaaa00000; - /* For some reason ld warns when _start is not defined. */ - /* Other .elf files link ok - only PL0_test.elf seems to */ - /* expect _start to be deifned. */ - _start = .; __start = .; - __text_start = .; - .text : + .another_weird_section_name_that_doesnt_matter : { /* have entry point at the beginning */ - KEEP(*(.text.PL0main)) - *(.text) + KEEP(PL0_test.o) + *(*) } - . = ALIGN(4096); /* align to page size */ - __text_end = .; - - __rodata_start = .; - .rodata : - { - *(.rodata) - } - . = ALIGN(4096); /* align to page size */ - __rodata_end = .; - - __data_start = .; - .data : - { - *(.data) - } - . = ALIGN(4096); /* align to page size */ - __data_end = .; - - __bss_start = .; - .bss : - { - bss = .; - *(.bss) - } - . = ALIGN(4096); /* align to page size */ - __bss_end = .; - __bss_size = __bss_end - __bss_start; __end = .; } -- cgit v1.2.3 From 48a3742d8f453f46da9ed5f375777788ca4d87a7 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Mon, 30 Dec 2019 13:27:52 +0100 Subject: don't perform unneeded section renaming for embedded files --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index e48dd2d..d44fc2b 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ all : kernel.img arm-none-eabi-as -mcpu=cortex-a7 $^ -o $@ %_embeddable.o : %.img - arm-none-eabi-objcopy -I binary -O elf32-littlearm -B arm --rename-section .data=.rodata $^ $@ + arm-none-eabi-objcopy -I binary -O elf32-littlearm -B arm $^ $@ PL_0_test.elf : PL0_test.ld PL0_test.o uart.o arm-none-eabi-gcc -T $< -o $@ $(ELFFLAGS) PL0_test.o uart.o -- cgit v1.2.3 From 26685f5203bc38cfa082b96182a406f4f7e6435a Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Mon, 30 Dec 2019 14:36:31 +0100 Subject: implement getchar() and putchar() in terms of supervisor call --- Makefile | 6 ++++-- PL0_test.c | 27 +++++++++++---------------- PL0_utils.c | 27 +++++++++++++++++++++++++++ PL0_utils.h | 10 ++++++++++ interrupts.c | 22 ++++++++++++++++++++-- svc.S | 5 +++++ svc_interface.h | 11 +++++++++++ 7 files changed, 88 insertions(+), 20 deletions(-) create mode 100644 PL0_utils.c create mode 100644 PL0_utils.h create mode 100644 svc.S create mode 100644 svc_interface.h (limited to 'Makefile') diff --git a/Makefile b/Makefile index d44fc2b..48bbd14 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,8 @@ ARM_OBJECTS=kernel.o paging.o demo_functionality.o PL0_test.o uart.o loader_stag KERNEL_STAGE2_OBJECTS=setup.o interrupt_vector.o interrupts.o uart.o demo_functionality.o paging.o ramfs_embeddable.o ramfs.o +PL_0_TEST_OBJECTS=PL0_utils.o svc.o PL0_test.o uart.o + RAMFS_FILES=PL_0_test.img all : kernel.img @@ -21,8 +23,8 @@ all : kernel.img %_embeddable.o : %.img arm-none-eabi-objcopy -I binary -O elf32-littlearm -B arm $^ $@ -PL_0_test.elf : PL0_test.ld PL0_test.o uart.o - arm-none-eabi-gcc -T $< -o $@ $(ELFFLAGS) PL0_test.o uart.o +PL_0_test.elf : PL0_test.ld $(PL_0_TEST_OBJECTS) + arm-none-eabi-gcc -T $< -o $@ $(ELFFLAGS) $(PL_0_TEST_OBJECTS) kernel_stage1.o : kernel_stage1.S kernel_stage2.img arm-none-eabi-as -mcpu=cortex-a7 $< -o $@ diff --git a/PL0_test.c b/PL0_test.c index 2e008a2..f476eb7 100644 --- a/PL0_test.c +++ b/PL0_test.c @@ -1,34 +1,29 @@ -#include "uart.h" -#include "psr.h" +#include "PL0_utils.h" // entry point - must remain the only function in the file! void PL0_main(void) { - // If all went correct, Success! gets printed - uart_puts("Success!\n\r"); - - uart_puts("calling supervisor\n\r"); - - asm volatile("svc #0"); - - uart_puts("back from supervisor call\n\r"); + // If loading program to userspace and handling of svc are + // implemented correctly, this shall get printed + puts("Hello userspace!"); - // if we're indeed in PL0, we should crash now, when trying to access - // memory we're not allowed to + // if we're indeed in PL0, we should trigger the abort handler now, + // when trying to access memory we're not allowed to + puts("Attempting to meddle with kernel memory from userspace :d"); char first_kernel_byte[2]; - first_kernel_byte[0] = *(char*) ((uint32_t) 0x0); + first_kernel_byte[0] = *(char*) 0x0; first_kernel_byte[1] = '\0'; uart_puts(first_kernel_byte); while (1) { - char c = uart_getc(); + char c = getchar(); - uart_putc(c); + putchar(c); if (c == '\r') - uart_putc('\n'); + putchar('\n'); } } diff --git a/PL0_utils.c b/PL0_utils.c new file mode 100644 index 0000000..2cede90 --- /dev/null +++ b/PL0_utils.c @@ -0,0 +1,27 @@ +#include +#include + +#include "svc_interface.h" + +// most generic definition possible +// the actual function defined in svc.S +uint32_t svc(enum svc_type, ...); + +void putchar(int character) +{ + svc(UART_PUTCHAR, character); +} + +int getchar(void) +{ + return svc(UART_GETCHAR); +} + +void puts(char *string) +{ + for (size_t i = 0; string[i]; i++) + putchar(string[i]); + + putchar('\n'); + putchar('\r'); +} diff --git a/PL0_utils.h b/PL0_utils.h new file mode 100644 index 0000000..ba72fd3 --- /dev/null +++ b/PL0_utils.h @@ -0,0 +1,10 @@ +#ifndef PL0_UTILS_H +#define PL0_UTILS_H + +void putchar(int character); + +int getchar(void); + +void puts(char *string); + +#endif // PL0_UTILS_H diff --git a/interrupts.c b/interrupts.c index 7932d23..f47bc1d 100644 --- a/interrupts.c +++ b/interrupts.c @@ -1,4 +1,5 @@ #include "uart.h" +#include "svc_interface.h" void setup(void); @@ -24,9 +25,26 @@ void undefined_instruction_vector(void) } } -void supervisor_call_handler(void) +uint32_t supervisor_call_handler(enum svc_type request, uint32_t arg1, + uint32_t arg2, uint32_t arg3) { - uart_puts("something svc happened\n\r"); + (void) arg2; (void) arg3; // unused for now + + switch(request) { + case UART_PUTCHAR: + uart_putc(arg1); + break; + case UART_GETCHAR: + return uart_getc(); + case UART_WRITE: + uart_puts("UART_WRITE not implemented!!!!!\n\r"); + break; + default: + // perhaps we should kill the process now? + uart_puts("unknown supervisor call type!!!!!\n\r"); + } + + return 0; // a dummy value } void abort_handler(void) diff --git a/svc.S b/svc.S new file mode 100644 index 0000000..65200d8 --- /dev/null +++ b/svc.S @@ -0,0 +1,5 @@ +.global svc + +svc: + svc #0 + mov pc, lr diff --git a/svc_interface.h b/svc_interface.h new file mode 100644 index 0000000..aa478ce --- /dev/null +++ b/svc_interface.h @@ -0,0 +1,11 @@ +#ifndef SVC_INTERFACE_H +#define SVC_INTERFACE_H + +enum svc_type + { + UART_PUTCHAR, + UART_GETCHAR, + UART_WRITE + }; + +#endif // SVC_INTERFACE_H -- cgit v1.2.3 From eae54c24e2e2b89f399bc2d3be195468c2e462a5 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Mon, 30 Dec 2019 14:40:26 +0100 Subject: stop direct uart usage from PL0 --- Makefile | 2 +- demo_functionality.c | 14 ++------------ 2 files changed, 3 insertions(+), 13 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 48bbd14..c3fc2f1 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ ARM_OBJECTS=kernel.o paging.o demo_functionality.o PL0_test.o uart.o loader_stag KERNEL_STAGE2_OBJECTS=setup.o interrupt_vector.o interrupts.o uart.o demo_functionality.o paging.o ramfs_embeddable.o ramfs.o -PL_0_TEST_OBJECTS=PL0_utils.o svc.o PL0_test.o uart.o +PL_0_TEST_OBJECTS=PL0_utils.o svc.o PL0_test.o RAMFS_FILES=PL_0_test.img diff --git a/demo_functionality.c b/demo_functionality.c index 12bba9b..d8fbdb5 100644 --- a/demo_functionality.c +++ b/demo_functionality.c @@ -75,30 +75,20 @@ void demo_setup_PL0(void) short_section_descriptor_t volatile *PL0_section_entry = &TRANSLATION_TABLE[PL0_SECTION_NUMBER]; - short_section_descriptor_t volatile *UART_memory_section_entry = - &TRANSLATION_TABLE[((uint32_t) GPIO_BASE) >> 20]; - short_section_descriptor_t - PL0_section = *PL0_section_entry, - UART_memory_section = *UART_memory_section_entry; + short_section_descriptor_t PL0_section = *PL0_section_entry; // set up address of PL0 section PL0_section.SECTION_BASE_ADDRESS_31_20 = UNPRIVILEGED_MEMORY_START >> 20; - // make the selected section and uart section available for PL0 + // make the selected section available for PL0 PL0_section.ACCESS_PERMISSIONS_2 = AP_2_0_MODEL_RW_ALL >> 2; PL0_section.ACCESS_PERMISSIONS_1_0 = AP_2_0_MODEL_RW_ALL & 0b011; - UART_memory_section.ACCESS_PERMISSIONS_2 = - AP_2_0_MODEL_RW_ALL >> 2; - UART_memory_section.ACCESS_PERMISSIONS_1_0 = - AP_2_0_MODEL_RW_ALL & 0b011; - *PL0_section_entry = PL0_section; - *UART_memory_section_entry = UART_memory_section; // invalidate main Translation Lookup Buffer (just in case) asm("mcr p15, 0, %0, c8, c7, 0\n\r" -- cgit v1.2.3 From c9e045dc2170a99c9f32386e3e53aee9e01a8e7c Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Mon, 30 Dec 2019 17:34:23 +0100 Subject: io api rework --- Makefile | 10 ++++--- PL0_test.c | 6 ++--- PL0_utils.c | 14 +++------- PL0_utils.h | 6 +---- demo_functionality.c | 52 ++++++++++++++++++------------------- interrupts.c | 20 +++++++------- io.c | 64 +++++++++++++++++++++++++++++++++++++++++++++ io.h | 26 +++++++++++++++++++ loader_stage2.c | 11 ++++---- memory.h | 2 ++ paging.c | 27 ++++++++----------- setup.c | 13 +++++----- strings.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ strings.h | 22 +++++++++++----- uart.c | 10 ++----- uart.h | 5 ++-- 16 files changed, 258 insertions(+), 103 deletions(-) create mode 100644 io.c create mode 100644 io.h create mode 100644 strings.c (limited to 'Makefile') diff --git a/Makefile b/Makefile index c3fc2f1..1e8f8fe 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,11 @@ ELFFLAGS=-nostdlib -lgcc ARM_OBJECTS=kernel.o paging.o demo_functionality.o PL0_test.o uart.o loader_stage1.o loader_stage2.o -KERNEL_STAGE2_OBJECTS=setup.o interrupt_vector.o interrupts.o uart.o demo_functionality.o paging.o ramfs_embeddable.o ramfs.o +KERNEL_STAGE2_OBJECTS=setup.o interrupt_vector.o interrupts.o uart.o demo_functionality.o paging.o ramfs_embeddable.o ramfs.o strings.o io.o -PL_0_TEST_OBJECTS=PL0_utils.o svc.o PL0_test.o +PL_0_TEST_OBJECTS=PL0_utils.o svc.o PL0_test.o strings.o io.o + +LOADER_STAGE2_OBJECTS=uart.o strings.o io.o loader_stage2.o RAMFS_FILES=PL_0_test.img @@ -41,8 +43,8 @@ kernel_stage2.elf : kernel_stage2.ld $(KERNEL_STAGE2_OBJECTS) #kernel.elf : boot.o kernel.o uart.o demo_functionality.o paging.o interrupt_vector.o interrupts.o ramfs_embeddable.o ramfs.o # arm-none-eabi-gcc -T linker.ld -o $@ $(ELFFLAGS) $^ -loader_stage2.elf : loader_stage2.o uart.o - arm-none-eabi-gcc -T loader_stage2_linker.ld -o $@ $(ELFFLAGS) $^ +loader_stage2.elf : loader_stage2_linker.ld $(LOADER_STAGE2_OBJECTS) + arm-none-eabi-gcc -T $< -o $@ $(ELFFLAGS) $(LOADER_STAGE2_OBJECTS) loader_stage1.o : loader_stage1.S loader_stage2.img arm-none-eabi-as -mcpu=cortex-a7 $< -o $@ diff --git a/PL0_test.c b/PL0_test.c index f476eb7..ac74ad4 100644 --- a/PL0_test.c +++ b/PL0_test.c @@ -15,15 +15,15 @@ void PL0_main(void) first_kernel_byte[0] = *(char*) 0x0; first_kernel_byte[1] = '\0'; - uart_puts(first_kernel_byte); + puts(first_kernel_byte); while (1) { char c = getchar(); - - putchar(c); if (c == '\r') putchar('\n'); + + putchar(c); } } diff --git a/PL0_utils.c b/PL0_utils.c index 2cede90..d83edb9 100644 --- a/PL0_utils.c +++ b/PL0_utils.c @@ -2,26 +2,18 @@ #include #include "svc_interface.h" +#include "PL0_utils.h" // most generic definition possible // the actual function defined in svc.S uint32_t svc(enum svc_type, ...); -void putchar(int character) +void putchar(char character) { svc(UART_PUTCHAR, character); } -int getchar(void) +char getchar(void) { return svc(UART_GETCHAR); } - -void puts(char *string) -{ - for (size_t i = 0; string[i]; i++) - putchar(string[i]); - - putchar('\n'); - putchar('\r'); -} diff --git a/PL0_utils.h b/PL0_utils.h index ba72fd3..c26a100 100644 --- a/PL0_utils.h +++ b/PL0_utils.h @@ -1,10 +1,6 @@ #ifndef PL0_UTILS_H #define PL0_UTILS_H -void putchar(int character); - -int getchar(void); - -void puts(char *string); +#include "io.h" #endif // PL0_UTILS_H diff --git a/demo_functionality.c b/demo_functionality.c index d8fbdb5..2db40f8 100644 --- a/demo_functionality.c +++ b/demo_functionality.c @@ -1,4 +1,4 @@ -#include "uart.h" +#include "io.h" #include "psr.h" #include "memory.h" #include "translation_table_descriptors.h" @@ -14,16 +14,16 @@ void demo_paging_support(void) switch(ID_MMFR0 & 0xf) /* lowest 4 bits indicate VMSA support */ { - case 0 : paging = "no paging\n\r"; break; - case 1 : paging = "implementation defined paging\n\r"; break; - case 2 : paging = "VMSAv6, with cache and TLB type registers\n\r"; break; - case 3 : paging = "VMSAv7, with support for remapping and access flag\n\r"; break; - case 4 : paging = "VMSAv7 with PXN bit supported\n\r"; break; - case 5 : paging = "VMSAv7, PXN and long format descriptors. EPAE is supported.\n\r"; break; - default : paging = "?_? unknown paging ?_?\n\r"; + case 0 : paging = "no paging"; break; + case 1 : paging = "implementation defined paging"; break; + case 2 : paging = "VMSAv6, with cache and TLB type registers"; break; + case 3 : paging = "VMSAv7, with support for remapping and access flag"; break; + case 4 : paging = "VMSAv7 with PXN bit supported"; break; + case 5 : paging = "VMSAv7, PXN and long format descriptors. EPAE is supported."; break; + default : paging = "?_? unknown paging ?_?"; } - uart_puts(paging); + puts(paging); } void demo_current_mode(void) @@ -37,20 +37,20 @@ void demo_current_mode(void) switch(CPSR.fields.PSR_MODE_4_0) { - case MODE_USER : mode_name = "User (PL0)\r\n"; break; - case MODE_FIQ : mode_name = "FIQ (PL1)\r\n"; break; - case MODE_IRQ : mode_name = "IRQ (PL1)\r\n"; break; - case MODE_SUPERVISOR : mode_name = "Supervisor (PL1)\r\n"; break; - case MODE_MONITOR : mode_name = "Monitor (PL1)\r\n"; break; - case MODE_ABORT : mode_name = "Abort (PL1)\r\n"; break; - case MODE_HYPERVISOR : mode_name = "Hyp (PL2)\r\n"; break; - case MODE_UNDEFINED : mode_name = "Undefined (PL1)\r\n"; break; - case MODE_SYSTEM : mode_name = "System (PL1)\r\n"; break; - default : mode_name = "Unknown mode\r\n"; break; + case MODE_USER : mode_name = "User (PL0)"; break; + case MODE_FIQ : mode_name = "FIQ (PL1)"; break; + case MODE_IRQ : mode_name = "IRQ (PL1)"; break; + case MODE_SUPERVISOR : mode_name = "Supervisor (PL1)"; break; + case MODE_MONITOR : mode_name = "Monitor (PL1)"; break; + case MODE_ABORT : mode_name = "Abort (PL1)"; break; + case MODE_HYPERVISOR : mode_name = "Hyp (PL2)"; break; + case MODE_UNDEFINED : mode_name = "Undefined (PL1)"; break; + case MODE_SYSTEM : mode_name = "System (PL1)"; break; + default : mode_name = "Unknown mode"; break; } - uart_puts("current mode: "); - uart_puts(mode_name); + prints("current mode: "); + puts(mode_name); } #define TRANSLATION_TABLE \ @@ -69,7 +69,7 @@ void demo_setup_PL0(void) if (find_file(&_binary_ramfs_img_start, "PL_0_test.img", &PL_0_test_img)) { - uart_puts("PL_0_test.img not found :(\r\n"); + puts("PL_0_test.img not found :("); asm volatile ("wfi"); } @@ -96,20 +96,20 @@ void demo_setup_PL0(void) // check that translation works... by copying a string using one // mapping and reading it using other :D - char message[] = "mapped sections for PL0 code\n\r"; + char message[] = "mapped sections for PL0 code"; unsigned int i; for (i = 0; i < sizeof(message); i++) ((volatile char*) UNPRIVILEGED_MEMORY_START)[i] = message[i]; - uart_puts((char*) VIRTUAL_PL0_MEMORY_START); + puts((char*) VIRTUAL_PL0_MEMORY_START); // now paste a userspace program to that section for (uint32_t i = 0; i < PL_0_test_img.file_size; i++) ((volatile char*) VIRTUAL_PL0_MEMORY_START)[i] = PL_0_test_img.file_contents[i]; - uart_puts("copied PL0 code to it's section\n\r"); + puts("copied PL0 code to it's section"); } // needed for array initialization in demo_go_unprivileged() @@ -132,7 +132,7 @@ void demo_go_unprivileged(void) new_SPSR.fields.PSR_MODE_4_0 = MODE_USER; write_SPSR(new_SPSR); - uart_puts("All ready, jumping to PL0 code\n\r"); + puts("All ready, jumping to PL0 code"); asm volatile("cps %[sysmode]\n\r" "mov sp, %[stackaddr]\n\r" diff --git a/interrupts.c b/interrupts.c index f47bc1d..3102761 100644 --- a/interrupts.c +++ b/interrupts.c @@ -1,4 +1,4 @@ -#include "uart.h" +#include "io.h" #include "svc_interface.h" void setup(void); @@ -18,7 +18,7 @@ void reset_handler(void) void undefined_instruction_vector(void) { - uart_puts("Undefined instruction occured"); + puts("Undefined instruction occured"); while( 1 ) { /* Do Nothing! */ @@ -32,16 +32,16 @@ uint32_t supervisor_call_handler(enum svc_type request, uint32_t arg1, switch(request) { case UART_PUTCHAR: - uart_putc(arg1); + putchar(arg1); break; case UART_GETCHAR: - return uart_getc(); + return getchar(); case UART_WRITE: - uart_puts("UART_WRITE not implemented!!!!!\n\r"); + puts("UART_WRITE not implemented!!!!!"); break; default: // perhaps we should kill the process now? - uart_puts("unknown supervisor call type!!!!!\n\r"); + puts("unknown supervisor call type!!!!!"); } return 0; // a dummy value @@ -49,28 +49,28 @@ uint32_t supervisor_call_handler(enum svc_type request, uint32_t arg1, void abort_handler(void) { - uart_puts("re-entered system\n\r"); + puts("re-entered system"); while(1); } void generic_handler(void) { - uart_puts("something weird happened\n\r"); + puts("something weird happened"); while(1); } void irq_handler(void) { - uart_puts("irq happened\n\r"); + puts("irq happened"); while(1); } void fiq_handler(void) { - uart_puts("fiq happened\n\r"); + puts("fiq happened"); while(1); } diff --git a/io.c b/io.c new file mode 100644 index 0000000..f35fda9 --- /dev/null +++ b/io.c @@ -0,0 +1,64 @@ +#include + +#include "io.h" +#include "strings.h" + +void puts(char string[]) +{ + prints(string); + + putchar('\n'); + putchar('\r'); +} + +void prints(char string[]) +{ + for (size_t i = 0; string[i]; i++) + putchar(string[i]); +} + +void printdec(uint32_t number) +{ + char buf[11]; + + uint32_to_decstring(number, buf); + + prints(buf); +} + +void printhex(uint32_t number) +{ + char buf[9]; + + uint32_to_hexstring(number, buf); + + prints(buf); +} + +void printbin(uint32_t number) +{ + char buf[33]; + + uint32_to_binstring(number, buf); + + prints(buf); +} + +void printdect(uint32_t number) +{ + char buf[11]; + + uint32_to_decstringt(number, buf); + + prints(buf); +} + +void printhext(uint32_t number) +{ + char buf[9]; + + uint32_to_hexstringt(number, buf); + + prints(buf); +} + diff --git a/io.h b/io.h new file mode 100644 index 0000000..161728a --- /dev/null +++ b/io.h @@ -0,0 +1,26 @@ +#ifndef IO_H +#define IO_H + +#include + +// putchar() and getchar() are not part of io.c, but it's useful to +// have those symbols declared here +void putchar(char c); + +char getchar(void); + +void puts(char string[]); + +void prints(char string[]); + +void printdec(uint32_t number); + +void printhex(uint32_t number); + +void printbin(uint32_t number); + +void printdect(uint32_t number); + +void printhext(uint32_t number); + +#endif // IO_H diff --git a/loader_stage2.c b/loader_stage2.c index f49d94c..e05de51 100644 --- a/loader_stage2.c +++ b/loader_stage2.c @@ -1,6 +1,7 @@ #include #include #include +#include #include void *const kernel_load_addr = ((void*) 0x8000); @@ -17,10 +18,10 @@ void _stage2_main(uint32_t r0, uint32_t r1, uint32_t atags) // get kernel size via uart (little endian) uint32_t b0, b1, b2, b3; - b0 = uart_getc(); - b1 = uart_getc(); - b2 = uart_getc(); - b3 = uart_getc(); + b0 = getchar(); + b1 = getchar(); + b2 = getchar(); + b3 = getchar(); uint32_t kernel_size = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24); @@ -28,7 +29,7 @@ void _stage2_main(uint32_t r0, uint32_t r1, uint32_t atags) char *dst = kernel_load_addr, *end = dst + kernel_size; while (dst < end) - *(dst++) = uart_getc(); + *(dst++) = getchar(); // jump to kernel ((void(*)(uint32_t, uint32_t, uint32_t)) kernel_load_addr) diff --git a/memory.h b/memory.h index adc3bc0..f6ece42 100644 --- a/memory.h +++ b/memory.h @@ -1,6 +1,8 @@ #ifndef MEMORY_H #define MEMORY_H +#include + // These macros were heavily used b4 I moved all the address // computation to the linker script. Now I'm just keeping them // in case they're needed for something else :) diff --git a/paging.c b/paging.c index 7c2a9de..2985e7e 100644 --- a/paging.c +++ b/paging.c @@ -1,24 +1,21 @@ #include "cp_regs.h" -#include "uart.h" #include "strings.h" #include "memory.h" #include "translation_table_descriptors.h" +#include "io.h" void setup_flat_map(void) { - char bits[33]; // for printing uint32_t bit values - // compute translation table base address // translation table shall start at first 2^14-bytes aligned // address after the kernel image - uint32_to_bits(TRANSLATION_TABLE_BASE, bits); - uart_puts("binary representation of chosen" - " lvl1 translation table address: "); - uart_puts(bits); uart_puts("\n\r"); + prints("chosen lvl1 translation table address: 0x"); + printhex(TRANSLATION_TABLE_BASE); + puts(""); // flat map all memory - uart_puts("preparing translation table\n\r"); + puts("preparing translation table"); short_descriptor_lvl1_t volatile *translation_table = (short_descriptor_lvl1_t*) TRANSLATION_TABLE_BASE; @@ -35,8 +32,7 @@ void setup_flat_map(void) }; // meddle with domain settings - uart_puts("setting domain0 to client access" - " and blocking other domains\n\r"); + puts("setting domain0 to client access and blocking other domains"); DACR_t DACR = 0; DACR = set_domain_permissions(DACR, 0, DOMAIN_CLIENT_ACCESS); @@ -55,7 +51,7 @@ void setup_flat_map(void) // we also disable data and instruction caches and the MMU // some of this is redundant (i.e. MMU should already be disabled) - uart_puts("setting C, I, AFE and TRE to 0 in SCTLR\n\r"); + puts("setting C, I, AFE and TRE to 0 in SCTLR"); SCTLR_t SCTLR; asm("mrc p15, 0, %0, c1, c0, 0" : "=r" (SCTLR.raw)); @@ -70,8 +66,8 @@ void setup_flat_map(void) // TODO: move invalidation instructions to some header as inlines - uart_puts("invalidating instruction cache, branch prediction," - " and entire main TLB\n\r"); + puts("invalidating instruction cache, branch prediction," + " and entire main TLB"); // invalidate instruction cache asm("mcr p15, 0, r0, c7, c5, 0\n\r" // r0 gets ignored @@ -86,8 +82,7 @@ void setup_flat_map(void) "isb" :: "r" (0) : "memory"); // now set TTBCR to use TTBR0 exclusively - uart_puts("Setting TTBCR.N to 0, so that" - " TTBR0 is used everywhere\n\r"); + puts("Setting TTBCR.N to 0, so that TTBR0 is used everywhere"); uint32_t TTBCR = 0; asm("mcr p15, 0, %0, c2, c0, 2" :: "r" (TTBCR)); @@ -102,7 +97,7 @@ void setup_flat_map(void) asm("mcr p15, 0, %0, c2, c0, 0" :: "r" (TTBR0.raw)); // enable MMU - uart_puts("enabling the MMU\n\r"); + puts("enabling the MMU"); // redundant - we already have SCTLR contents in the variable // asm("mrc p15, 0, %0, c1, c0, 0" : "=r" (SCTLR.raw)); diff --git a/setup.c b/setup.c index 48df825..3542043 100644 --- a/setup.c +++ b/setup.c @@ -1,4 +1,5 @@ #include "uart.h" +#include "io.h" #include "demo_functionality.h" #include "paging.h" @@ -9,9 +10,9 @@ void setup(void) // When we attach screen session after loading kernel with socat // we miss kernel's greeting... So we'll make the kernel wait for // one char we're going to send from within screen - uart_getc(); + getchar(); - uart_puts("Hello, kernel World!\r\n"); + puts("Hello, kernel World!"); // prints some info demo_paging_support(); @@ -31,11 +32,11 @@ void setup(void) while (1) { - char c = uart_getc(); + char c = getchar(); - uart_putc(c); - if (c == '\r') - uart_putc('\n'); + putchar('\n'); + + putchar(c); } } diff --git a/strings.c b/strings.c new file mode 100644 index 0000000..55aff97 --- /dev/null +++ b/strings.c @@ -0,0 +1,73 @@ +#include + +#include "strings.h" + +void uint32_to_dec(uint32_t number, char buf[10]) +{ + for (int i = 0; i < 10; i++) + { + buf[10 - 1 - i] = number % 10; + number /= 10; + } +} + +void uint32_to_hex(uint32_t number, char buf[8]) +{ + for (int i = 0; i < 8; i++) + { + unsigned char quadbit = (number >> ((8 - i - 1) * 4)) & 0xf; + buf[i] = quadbit > 9 ? quadbit - 10 + 'a' : quadbit + '0'; + } +} + +void uint32_to_bin(uint32_t number, char buf[32]) +{ + for (int i = 0; i < 32; i++) + buf[i] = ((number >> (32 - i - 1)) & 1) ? '1' : '0'; +} + +void uint32_to_decstring(uint32_t number, char buf[11]) +{ + uint32_to_dec(number, buf); + buf[10] = '\0'; +} + +void uint32_to_hexstring(uint32_t number, char buf[9]) +{ + uint32_to_hex(number, buf); + buf[9] = '\0'; +} + +void uint32_to_binstring(uint32_t number, char buf[33]) +{ + uint32_to_bin(number, buf); + buf[32] = '\0'; +} + +void trim_0s(char string[]) +{ + size_t i; + for (i = 0; string[i] != '\0' && string[i] != '0'; i++); + + size_t j = 0; + + if (string[i] == '\0') + string[j++] = string[i--]; + + do + string[j] = string[i + j]; + while (string[j++]); +} + +void uint32_to_decstringt(uint32_t number, char buf[11]) +{ + uint32_to_decstring(number, buf); + trim_0s(buf); +} + +void uint32_to_hexstringt(uint32_t number, char buf[9]) +{ + uint32_to_hexstring(number, buf); + trim_0s(buf); +} + diff --git a/strings.h b/strings.h index 8d4b511..bfe6fd0 100644 --- a/strings.h +++ b/strings.h @@ -3,12 +3,22 @@ #include -static inline void uint32_to_bits(uint32_t number, char *buf) -{ - for (int i = 0; i < 32; i++) - buf[i] = ((number >> (32 - i - 1)) & 1) ? '1' : '0'; +void uint32_to_dec(uint32_t number, char buf[10]); - buf[32] = '\0'; -} +void uint32_to_hex(uint32_t number, char buf[8]); + +void uint32_to_bin(uint32_t number, char buf[32]); + +void uint32_to_decstring(uint32_t number, char buf[11]); + +void uint32_to_hexstring(uint32_t number, char buf[9]); + +void uint32_to_binstring(uint32_t number, char buf[33]); + +void trim_0s(char string[]); + +void uint32_to_decstringt(uint32_t number, char buf[11]); + +void uint32_to_hexstringt(uint32_t number, char buf[9]); #endif // STRINGS_H diff --git a/uart.c b/uart.c index 139cfe0..2030538 100644 --- a/uart.c +++ b/uart.c @@ -63,22 +63,16 @@ void uart_init() mmio_write(UART0_CR, (1 << 0) | (1 << 8) | (1 << 9)); } -void uart_putc(unsigned char c) +void putchar(char c) { // Wait for UART to become ready to transmit. while ( mmio_read(UART0_FR) & (1 << 5) ) { } mmio_write(UART0_DR, c); } -unsigned char uart_getc() +char getchar(void) { // Wait for UART to have received something. while ( mmio_read(UART0_FR) & (1 << 4) ) { } return mmio_read(UART0_DR); } - -void uart_puts(const char* str) -{ - for (size_t i = 0; str[i] != '\0'; i ++) - uart_putc((unsigned char)str[i]); -} diff --git a/uart.h b/uart.h index ec29d47..d5e931b 100644 --- a/uart.h +++ b/uart.h @@ -39,8 +39,7 @@ enum }; void uart_init(); -void uart_putc(unsigned char c); -unsigned char uart_getc(); -void uart_puts(const char* str); +void putchar(char c); +char getchar(void); #endif // UART_H -- cgit v1.2.3 From 68d11d4c42d6e82629e7a38e34c358d7d501e865 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Mon, 30 Dec 2019 17:36:51 +0100 Subject: minor improvements to Makefile --- Makefile | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 1e8f8fe..6963722 100644 --- a/Makefile +++ b/Makefile @@ -34,23 +34,17 @@ kernel_stage1.o : kernel_stage1.S kernel_stage2.img kernel.elf : kernel_stage1.ld kernel_stage1.o arm-none-eabi-gcc -T $< -o $@ $(ELFFLAGS) kernel_stage1.o -kernel.img : kernel.elf - arm-none-eabi-objcopy $^ -O binary $@ - kernel_stage2.elf : kernel_stage2.ld $(KERNEL_STAGE2_OBJECTS) arm-none-eabi-gcc -T $< -o $@ $(ELFFLAGS) $(KERNEL_STAGE2_OBJECTS) -#kernel.elf : boot.o kernel.o uart.o demo_functionality.o paging.o interrupt_vector.o interrupts.o ramfs_embeddable.o ramfs.o -# arm-none-eabi-gcc -T linker.ld -o $@ $(ELFFLAGS) $^ - loader_stage2.elf : loader_stage2_linker.ld $(LOADER_STAGE2_OBJECTS) arm-none-eabi-gcc -T $< -o $@ $(ELFFLAGS) $(LOADER_STAGE2_OBJECTS) loader_stage1.o : loader_stage1.S loader_stage2.img arm-none-eabi-as -mcpu=cortex-a7 $< -o $@ -loader.elf : loader_stage1.o - arm-none-eabi-gcc -T loader_stage1_linker.ld -o $@ $(ELFFLAGS) $^ +loader.elf : loader_stage1_linker.ld loader_stage1.o + arm-none-eabi-gcc -T $< -o $@ $(ELFFLAGS) loader_stage1.o loader.img : loader.elf arm-none-eabi-objcopy $^ -O binary $@ -- cgit v1.2.3 From 7745bb422f98730d6b555e4ab135ba6ca7479d7a Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Mon, 30 Dec 2019 17:38:24 +0100 Subject: better naming of qemu-* targets --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 6963722..ecf2b2f 100644 --- a/Makefile +++ b/Makefile @@ -54,10 +54,10 @@ loader.img : loader.elf qemu-elf : kernel.elf qemu-system-arm -m 256 -M raspi2 -serial stdio -kernel $^ -qemu-img : kernel.img +qemu-bin : kernel.img qemu-system-arm -m 256 -M raspi2 -serial stdio -kernel $^ -qemu-bin : loader.img kernel.img pipe_image +qemu-loader : loader.img kernel.img pipe_image ./pipe_image --stdout | qemu-system-arm -m 256 -M raspi2 -serial stdio -kernel $< run-on-rpi : kernel.img pipe_image -- cgit v1.2.3 From 254e035cc27d5787348849b65619a702ae0d42e3 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 31 Dec 2019 10:42:17 +0100 Subject: handle atags in C code, print (some of) it's contents --- Makefile | 2 +- atags.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ atags.h | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ setup.c | 14 +++++++-- 4 files changed, 213 insertions(+), 4 deletions(-) create mode 100644 atags.c create mode 100644 atags.h (limited to 'Makefile') diff --git a/Makefile b/Makefile index ecf2b2f..feedd79 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ ELFFLAGS=-nostdlib -lgcc ARM_OBJECTS=kernel.o paging.o demo_functionality.o PL0_test.o uart.o loader_stage1.o loader_stage2.o -KERNEL_STAGE2_OBJECTS=setup.o interrupt_vector.o interrupts.o uart.o demo_functionality.o paging.o ramfs_embeddable.o ramfs.o strings.o io.o +KERNEL_STAGE2_OBJECTS=setup.o interrupt_vector.o interrupts.o uart.o demo_functionality.o paging.o ramfs_embeddable.o ramfs.o strings.o io.o atags.o PL_0_TEST_OBJECTS=PL0_utils.o svc.o PL0_test.o strings.o io.o diff --git a/atags.c b/atags.c new file mode 100644 index 0000000..9b88724 --- /dev/null +++ b/atags.c @@ -0,0 +1,99 @@ +#include "atags.h" +#include "io.h" + +static inline struct atag_header *next_tag(struct atag_header *tag) +{ + return (struct atag_header*) (((uint32_t*) tag) + tag->size); +} + +#define TAG_CONTENTS_FUN(tagname) \ + static inline struct atag_##tagname *tagname##_tag_contents \ + (struct atag_header *tag) \ + { \ + return (struct atag_##tagname*) (tag + 1); \ + } + +TAG_CONTENTS_FUN(header) +TAG_CONTENTS_FUN(core) +TAG_CONTENTS_FUN(mem) +TAG_CONTENTS_FUN(videotext) +TAG_CONTENTS_FUN(ramdisk) +TAG_CONTENTS_FUN(initrd2) +TAG_CONTENTS_FUN(serialnr) +TAG_CONTENTS_FUN(revision) +TAG_CONTENTS_FUN(videolfb) +TAG_CONTENTS_FUN(cmdline) + +uint32_t find_memory_size(struct atag_header *atags) +{ + // we silently assume there will only be one mem atag + while (atags->tag != ATAG_MEM && atags->tag != ATAG_NONE) + atags = next_tag(atags); + + if (atags->tag == ATAG_NONE) + return 0; + + struct atag_mem *mem_tag = mem_tag_contents(atags); + + // our design assumes address 0x0 is available, so we reject mem + // atag saying otherwise + if (mem_tag->start != 0) + return 0; + + return mem_tag->size; +} + +void print_tag(struct atag_header *tag) +{ +#define TAG_CASE(tagname_upcase, tagname_locase, instructions) \ + case ATAG_##tagname_upcase: \ + puts("ATAG_" #tagname_upcase ":"); \ + { \ + struct atag_##tagname_locase *contents = \ + tagname_locase##_tag_contents(tag); \ + instructions; \ + } \ + break + + switch (tag->tag) + { + TAG_CASE(CORE, core, + prints(" flags: 0x"); + printhex(contents->flags); puts(""); + prints(" page size: "); + printdec(contents->pagesize); puts(""); + prints(" root device: "); + printdec(contents->rootdev); puts("");); + TAG_CASE(MEM, mem, + prints(" memory size: 0x"); + printhex(contents->size); puts(""); + prints(" memory start: 0x"); + printhex(contents->start); puts("");); + // the rest are unimportant for now, + // as they're not passed by qemu + TAG_CASE(VIDEOTEXT, videotext, (void) contents;); + TAG_CASE(RAMDISK, ramdisk, (void) contents;); + TAG_CASE(INITRD2, initrd2, (void) contents;); + TAG_CASE(SERIAL, serialnr, (void) contents;); + TAG_CASE(REVISION, revision, (void) contents;); + TAG_CASE(VIDEOLFB, videolfb, (void) contents;); + TAG_CASE(CMDLINE, cmdline, (void) contents;); + + case ATAG_NONE: + puts("ATAG_NONE"); + break; + default: + prints("!! unknown tag: 0x"); printhex(tag->tag); puts(" !!"); + } +} + +void print_atags(struct atag_header *atags) +{ + while (atags->tag != ATAG_NONE) + { + print_tag(atags); + atags = next_tag(atags); + } + + print_tag(atags); // also print ATAG_NONE +} diff --git a/atags.h b/atags.h new file mode 100644 index 0000000..4b6879f --- /dev/null +++ b/atags.h @@ -0,0 +1,102 @@ +#ifndef ATAGS_H +#define ATAGS_H + +#include + +#define ATAG_NONE 0x00000000 +#define ATAG_CORE 0x54410001 +#define ATAG_MEM 0x54410002 +#define ATAG_VIDEOTEXT 0x54410003 +#define ATAG_RAMDISK 0x54410004 +#define ATAG_INITRD2 0x54420005 +#define ATAG_SERIAL 0x54410006 +#define ATAG_REVISION 0x54410007 +#define ATAG_VIDEOLFB 0x54410008 +#define ATAG_CMDLINE 0x54410009 + +struct atag_header +{ + uint32_t size; + uint32_t tag; +}; + +struct atag_core +{ + uint32_t flags; + uint32_t pagesize; + uint32_t rootdev; +}; + +struct atag_mem +{ + uint32_t size; + uint32_t start; +}; + +struct atag_videotext +{ + uint8_t x; + uint8_t y; + uint16_t video_page; + uint8_t video_mode; + uint8_t video_cols; + uint16_t video_ega_bx; + uint8_t video_lines; + uint8_t video_isvga; + uint16_t video_points; +}; + +struct atag_ramdisk +{ + uint32_t flags; + uint32_t size; + uint32_t start; +}; + +struct atag_initrd2 +{ + uint32_t start; + uint32_t size; +}; + +struct atag_serialnr +{ + uint32_t low; + uint32_t high; +}; + +struct atag_revision +{ + uint32_t rev; +}; + +struct atag_videolfb +{ + uint16_t lfb_width; + uint16_t lfb_height; + uint16_t lfb_depth; + uint16_t lfb_linelength; + uint32_t lfb_base; + uint32_t lfb_size; + uint8_t red_size; + uint8_t red_pos; + uint8_t green_size; + uint8_t green_pos; + uint8_t blue_size; + uint8_t blue_pos; + uint8_t rsvd_size; + uint8_t rsvd_pos; +}; + +struct atag_cmdline +{ + char cmdline[1]; +}; + +uint32_t find_memory_size(struct atag_header *atags); + +void print_tag(struct atag_header *tag); + +void print_atags(struct atag_header *atags); + +#endif // ATAGS_H diff --git a/setup.c b/setup.c index be71547..f0a9d0a 100644 --- a/setup.c +++ b/setup.c @@ -2,9 +2,10 @@ #include "io.h" #include "demo_functionality.h" #include "paging.h" +#include "atags.h" void setup(uint32_t r0, uint32_t machine_type, - uint32_t atags) + struct atag_header *atags) { uart_init(); @@ -17,12 +18,19 @@ void setup(uint32_t r0, uint32_t machine_type, prints("ARM machine type: 0x"); printhext(machine_type); puts(""); - // value 3 introduced by stage1 code, means no atags was found + // value 3 introduced by stage1 code means no atags was found if (r0 == 3) puts("No ATAGS was found!"); else { - prints("ATAGS copied to 0x"); printhex(atags); puts(""); + prints("ATAGS copied to 0x"); + printhex((uint32_t) atags); puts(""); + + puts("__ ATAGS contents __"); + + print_atags(atags); + + puts("__ end of ATAGS contents __"); } // prints some info -- cgit v1.2.3