From 0e6d0057a8c790358309465848225a5a91588782 Mon Sep 17 00:00:00 2001 From: fef Date: Thu, 5 Aug 2021 16:08:09 +0200 Subject: [PATCH] arch: call init and preinit array --- arch/at91sam3x8e/CMakeLists.txt | 1 + arch/at91sam3x8e/arch_init.c | 29 ++++++++++ arch/at91sam3x8e/handle_reset.c | 21 ++----- arch/at91sam3x8e/include/arch/linker.h | 78 ++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 15 deletions(-) create mode 100644 arch/at91sam3x8e/arch_init.c create mode 100644 arch/at91sam3x8e/include/arch/linker.h diff --git a/arch/at91sam3x8e/CMakeLists.txt b/arch/at91sam3x8e/CMakeLists.txt index 48a1e56..39f29e6 100644 --- a/arch/at91sam3x8e/CMakeLists.txt +++ b/arch/at91sam3x8e/CMakeLists.txt @@ -12,6 +12,7 @@ configure_file( ) target_sources(ardix_arch PRIVATE + arch_init.c atom_get_put.S atom.c atomic.c diff --git a/arch/at91sam3x8e/arch_init.c b/arch/at91sam3x8e/arch_init.c new file mode 100644 index 0000000..7ea4b3a --- /dev/null +++ b/arch/at91sam3x8e/arch_init.c @@ -0,0 +1,29 @@ +/* See the end of this file for copyright, license, and warranty information. */ + +#include +#include + +/* flash.ld */ +extern uint32_t _sheap; +extern uint32_t _eheap; + +#include + +void __preinit_malloc(void) +{ + malloc_init(&_sheap, (size_t)&_eheap - (size_t)&_sheap); +} +__preinit_call(__preinit_malloc); + +/* + * This file is part of Ardix. + * Copyright (c) 2020, 2021 Felix Kopp . + * + * Ardix is non-violent software: you may only use, redistribute, + * and/or modify it under the terms of the CNPLv6+ as found in + * the LICENSE file in the source code root directory or at + * . + * + * Ardix comes with ABSOLUTELY NO WARRANTY, to the extent + * permitted by applicable law. See the CNPLv6+ for details. + */ diff --git a/arch/at91sam3x8e/handle_reset.c b/arch/at91sam3x8e/handle_reset.c index f30c1c5..4ceaab3 100644 --- a/arch/at91sam3x8e/handle_reset.c +++ b/arch/at91sam3x8e/handle_reset.c @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -11,19 +12,6 @@ #include #include -/* from flash.ld */ -extern uint32_t _sfixed; /* fixed area start */ -extern uint32_t _efixed; /* fixed area end */ -extern uint32_t _etext; /* program (.text) end */ -extern uint32_t _srelocate; /* relocate (.data) start */ -extern uint32_t _erelocate; /* relocate end */ -extern uint32_t _szero; /* zero area (.bss) start */ -extern uint32_t _ezero; /* zero area end */ -extern uint32_t _sstack; /* stack start */ -extern uint32_t _estack; /* stack end */ -extern uint32_t _sheap; /* heap start */ -extern uint32_t _eheap; /* heap end */ - /* implementation in init/main.c */ extern int main(void); @@ -34,8 +22,11 @@ __naked __noreturn void handle_reset(void) memmove(&_srelocate, &_etext, (size_t)(&_erelocate) - (size_t)(&_srelocate)); memset(&_szero, 0, (size_t)(&_ezero) - (size_t)(&_szero)); - /* memory protection isn't implemented yet, heap is shared among all processes */ - malloc_init(&_sheap, (size_t)(&_eheap) - (size_t)(&_sheap)); + for (uint32_t *fn = &__preinit_array_start; fn != &__preinit_array_end; fn++) + ( (void (*)(void))fn )(); + + for (uint32_t *fn = &__init_array_start; fn != &__init_array_end; fn++) + ( (void (*)(void))fn )(); /* start the Kernel */ main(); diff --git a/arch/at91sam3x8e/include/arch/linker.h b/arch/at91sam3x8e/include/arch/linker.h new file mode 100644 index 0000000..efed353 --- /dev/null +++ b/arch/at91sam3x8e/include/arch/linker.h @@ -0,0 +1,78 @@ +/* See the end of this file for copyright, license, and warranty information. */ + +#pragma once + +#include + +/** + * @file linker.h + * @brief External symbols defined by the linker script (see flash.ld). + * The declarations in this file don't carry an actual value and cannot be + * accessed directly; they are merely there to get the *addresses* of. + */ + +/** @brief Start of fixed area */ +extern uint32_t _sfixed; +/** @brief End of fixed area */ +extern uint32_t _efixed; + +/** @brief End of program code (.text) */ +extern uint32_t _etext; + +/** @brief Start of relocation section (for global and static variables) */ +extern uint32_t _srelocate; +/** @brief End of relocation section */ +extern uint32_t _erelocate; + +/** @brief Start of zero area (.bss) */ +extern uint32_t _szero; +/** @brief End of zero area (.bss) */ +extern uint32_t _ezero; + +/** + * @brief Lowest address of the main (kernel) stack. + * This is technically the end of the stack, because it grows + * from the highest address downwards. + */ +extern uint32_t _sstack; +/** @brief Highest address if the main (kernel) stack. */ +extern uint32_t _estack; + +/** @brief Start of heap area */ +extern uint32_t _sheap; +/** @brief End of heap area */ +extern uint32_t _eheap; + +/** + * @brief Start of preinit array. + * The preinit array is an array of function pointers that are invoked before + * the ones in the init array and `main()` are called. Only the most critical + * setup routines, like initializing the memory allocator, should go here. + */ +extern uintptr_t __preinit_array_start; +/** @brief End of preinit array. */ +extern uintptr_t __preinit_array_end; + +/** + * @brief Start of init array. + * The init array is an array of function pointers that are invoked before + * calling `main()` and are there to initialize the various components of the + * standard C library. They are executed after the preinit array, so memory + * allocation is already available. + */ +extern uint32_t __init_array_start; +/** @brief End of init array. */ +extern uint32_t __init_array_end; + +/* + * This file is part of Ardix. + * Copyright (c) 2020, 2021 Felix Kopp . + * + * Ardix is non-violent software: you may only use, redistribute, + * and/or modify it under the terms of the CNPLv6+ as found in + * the LICENSE file in the source code root directory or at + * . + * + * Ardix comes with ABSOLUTELY NO WARRANTY, to the extent + * permitted by applicable law. See the CNPLv6+ for details. + */