diff --git a/packages.json b/packages.json index 68da45c..0cb0831 100644 --- a/packages.json +++ b/packages.json @@ -88,7 +88,7 @@ { "link": "https://github.com/matgla/Yasld.git", "type": "git", - "version": "main", + "version": "0.0.4", "target": "yasld", "options": { "cmake_variables": { diff --git a/source/main.cpp2 b/source/main.cpp2 index 6ae5bc1..bee3604 100644 --- a/source/main.cpp2 +++ b/source/main.cpp2 @@ -176,7 +176,10 @@ AppData: @copyable type = { else if (type == yasld::AllocationType::Data) { p = ram.allocate_data(size); - printf("Allocated data at: %p\n", p); + } + else if (type == yasld::AllocationType::Init) + { + p = ram.allocate_init(size); } else { diff --git a/source/ram_layout.cc b/source/ram_layout.cc index f0f08d7..cfb7d7d 100644 --- a/source/ram_layout.cc +++ b/source/ram_layout.cc @@ -37,6 +37,8 @@ struct ApplicationData { void *lot_address; uint32_t lot_size; + void *init_address; + uint32_t init_size; void *data_address; uint32_t data_size; void *modules_root; diff --git a/source/ram_manager.cpp2 b/source/ram_manager.cpp2 index e6c44ae..24a9219 100644 --- a/source/ram_manager.cpp2 +++ b/source/ram_manager.cpp2 @@ -27,6 +27,8 @@ yasboot: namespace = { get_metadata_address: () -> *void = { return std::align(alignof(std::max_align_t), sizeof(ApplicationData), application_ram_start, application_ram_size); } + + current_memory_pointer: *void = reinterpret_cast<*void>(0); } export yasboot: namespace = { @@ -34,30 +36,39 @@ export yasboot: namespace = { RamManager: @copyable type = { operator=: (out this) = { meta = cpp2::unsafe_narrow<*ApplicationData>(get_metadata_address()); + if (current_memory_pointer == nullptr) { + current_memory_pointer = reinterpret_cast<*u8>(meta) + sizeof(ApplicationData); + } memory_left = application_ram_size - sizeof(ApplicationData) - yasboot::pointer_offset(get_metadata_address(), application_ram_start); printf("Creating metadata in memory block (%p, size: %d), at: %p\n", application_ram_start, application_ram_size, cpp2::unsafe_narrow<*void>(meta)); } - allocate_offset_table: (inout this, size: std::size_t) -> *void = { - p: *void = reinterpret_cast<*u8>(meta) + sizeof(ApplicationData); - printf("Sizeof appdata: %d, %p\n", sizeof(ApplicationData), p); - aligned_p := std::align(alignof(std::max_align_t), size, p, memory_left); - printf("Offset table allocated at: %p\n", aligned_p); - meta*.lot_address = aligned_p; - meta*.lot_size = size + yasboot::pointer_offset(aligned_p, p); - memory_left -= meta*.lot_size; + allocate_aligned: (inout this, size: std::size_t, alignment: std::size_t) -> *void = { + aligned_p := std::align(alignment, size, current_memory_pointer, memory_left); + memory_left -= (yasboot::pointer_offset(aligned_p, current_memory_pointer) + size); + current_memory_pointer = cpp2::unsafe_narrow<*u8>(aligned_p) + size; return aligned_p; } + allocate_offset_table: (inout this, size: std::size_t) -> *void = { + meta*.lot_address = allocate_aligned(size, alignof(std::max_align_t)); + printf("Offset table allocated at: %p\n", meta*.lot_address); + meta*.lot_size = size; + return meta*.lot_address; + } + allocate_data: (inout this, size: std::size_t) -> *void = { - p: *void = cpp2::unsafe_narrow<*u8>(meta*.lot_address) + meta*.lot_size; - aligned_p := std::align(128, size, p, memory_left); - // VTOR must be aligned to 0x80 - printf("Allocated data at: %p\n", aligned_p); - meta*.data_address = aligned_p; - meta*.data_size = size + yasboot::pointer_offset(aligned_p, p); - memory_left -= meta*.lot_size; - return aligned_p; + meta*.data_address = allocate_aligned(size, 128); + printf("Data allocated at: %p\n", meta*.data_address); + meta*.data_size = size; + return meta*.data_address; + } + + allocate_init: (inout this, size: std::size_t) -> *void = { + meta*.init_address = allocate_aligned(size, 128); + printf("Init allocated at: %p\n", meta*.init_address); + meta*.init_size = size; + return meta*.init_address; } meta: *ApplicationData; diff --git a/tests/simulation/cmake/create_test_targets.cmake b/tests/simulation/cmake/create_test_targets.cmake index 4106b0c..b872d57 100644 --- a/tests/simulation/cmake/create_test_targets.cmake +++ b/tests/simulation/cmake/create_test_targets.cmake @@ -41,7 +41,7 @@ macro (create_test_targets) yasboot ${TEST_NAME} ${PROJECT_SOURCE_DIR}/scripts/create_disk.py ) - add_custom_target(run_test_${TEST_NAME} + add_custom_target(${TEST_NAME}_run_test COMMAND renode-test --debug-on-error ${CMAKE_CURRENT_BINARY_DIR}/${TEST_NAME}.robot VERBATIM DEPENDS create_disk_for_${TEST_NAME} @@ -57,7 +57,7 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/../common.robot ${CMAKE_CURRENT_BINAR configure_file(${CMAKE_CURRENT_SOURCE_DIR}/../execute.resc ${CMAKE_CURRENT_BINARY_DIR}/execute.resc) add_test( NAME ${TEST_NAME}_test - COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} -- run_${TEST_NAME}) + COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} -- ${TEST_NAME}_run_test) add_custom_target(${TEST_NAME}_run COMMAND ${renode_executable} --console --disable-xwt ${CMAKE_CURRENT_BINARY_DIR}/execute.resc diff --git a/tests/simulation/mspc/firmware/CMakeLists.txt b/tests/simulation/mspc/firmware/CMakeLists.txt index 721bb33..6fe1e6c 100644 --- a/tests/simulation/mspc/firmware/CMakeLists.txt +++ b/tests/simulation/mspc/firmware/CMakeLists.txt @@ -41,6 +41,8 @@ pico_sdk_init() project (mspc_firmware ASM C CXX) +message(STATUS "MSPC Firmware Build Type: ${CMAKE_BUILD_TYPE}") + set(common_flags "-Werror -Wall -Wextra -Wconversion -Wunused -Wshadow -Wpointer-arith -Wcast-qual -Wdouble-promotion -Wmissing-braces ") set(common_release_flags "") set(common_debug_flags "-g") diff --git a/tests/simulation/mspc/firmware/hello/main.cpp b/tests/simulation/mspc/firmware/hello/main.cpp index 242b18b..3079af1 100644 --- a/tests/simulation/mspc/firmware/hello/main.cpp +++ b/tests/simulation/mspc/firmware/hello/main.cpp @@ -22,10 +22,12 @@ #include -int main() { +int main() +{ stdio_init_all(); - while (true) { + while (true) + { printf("This is first example that just prints!\n"); sleep_ms(1000); } diff --git a/tests/simulation/mspc/firmware/yasld_firmwares/hello/CMakeLists.txt b/tests/simulation/mspc/firmware/yasld_firmwares/hello/CMakeLists.txt index a42f1ca..6c15eac 100644 --- a/tests/simulation/mspc/firmware/yasld_firmwares/hello/CMakeLists.txt +++ b/tests/simulation/mspc/firmware/yasld_firmwares/hello/CMakeLists.txt @@ -37,6 +37,7 @@ target_link_libraries(hello_from_relocatable_module target_link_options(hello_from_relocatable_module PUBLIC -T${CMAKE_CURRENT_SOURCE_DIR}/executable.ld + -Wl,--entry=_entry ) set_target_properties(hello_from_relocatable_module diff --git a/tests/simulation/mspc/firmware/yasld_firmwares/hello/executable.ld b/tests/simulation/mspc/firmware/yasld_firmwares/hello/executable.ld index 07925b5..b71f70c 100644 --- a/tests/simulation/mspc/firmware/yasld_firmwares/hello/executable.ld +++ b/tests/simulation/mspc/firmware/yasld_firmwares/hello/executable.ld @@ -38,19 +38,20 @@ SECTIONS *(.rel.text*) *(.glue_7) *(.glue_7t) - *(.eh_frame) + *crtbegin.o(.ctors) + *crtbegin?.o(.ctors) + *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors) + *(SORT(.ctors.*)) + *(.ctors) + /* Followed by destructors */ + *crtbegin.o(.dtors) + *crtbegin?.o(.dtors) + *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors) + *(SORT(.dtors.*)) + *(.dtors) - . = ALIGN(4); - PROVIDE_HIDDEN (__mutex_array_start = .); - KEEP(*(SORT(.mutex_array.*))) - KEEP(*(.mutex_array)) - PROVIDE_HIDDEN (__mutex_array_end = .); - . = ALIGN(4); - PROVIDE_HIDDEN(__preinit_array_start = .); - KEEP(*(SORT(.preinit_array.*))) - KEEP(*(.preinit_array)) - PROVIDE_HIDDEN(__preinit_array_end = .); - + *(.eh_frame*) + KEEP(*(.init)) KEEP(*(.fini)) *(.rodata) @@ -61,11 +62,8 @@ SECTIONS . = ALIGN(16); } > image - .data : + .init_arrays : { - . = ALIGN(16); - KEEP(*(.ram_vector_table)) - . = ALIGN(4); PROVIDE_HIDDEN(__init_array_start = .); KEEP(*(SORT(.init_array.*))) KEEP(*(.init_array)) @@ -75,8 +73,24 @@ SECTIONS *(SORT(.fini_array.*)) *(.fini_array) PROVIDE_HIDDEN(__fini_array_end = .); - - __data_start__ = .; + . = ALIGN(4); + PROVIDE_HIDDEN(__preinit_array_start = .); + KEEP(*(SORT(.preinit_array.*))) + KEEP(*(.preinit_array)) + PROVIDE_HIDDEN(__preinit_array_end = .); + } > image + + .data : + { + . = ALIGN(4); + KEEP(*(.ram_vector_table)) + . = ALIGN(4); + PROVIDE_HIDDEN (__mutex_array_start = .); + KEEP(*(SORT(.mutex_array.*))) + KEEP(*(.mutex_array)) + PROVIDE_HIDDEN (__mutex_array_end = .); + . = ALIGN(4); + __data_start__ = .; *(.data) *(.data*) diff --git a/tests/simulation/mspc/firmware/yasld_firmwares/hello/main.cpp b/tests/simulation/mspc/firmware/yasld_firmwares/hello/main.cpp index 9249a43..9eabc33 100644 --- a/tests/simulation/mspc/firmware/yasld_firmwares/hello/main.cpp +++ b/tests/simulation/mspc/firmware/yasld_firmwares/hello/main.cpp @@ -2,24 +2,14 @@ #include -#include "vectors.hpp" - -extern "C" -{ - extern uint32_t app_stack_pointer; - // char *__StackTop = reinterpret_cast(app_stack_pointer); - void runtime_init(); - void initialize_on_entry(); -} - int main() { - initialize_on_entry(); - runtime_init(); stdio_init_all(); + int counter = 0; while (true) { - printf("Hello from standalone relocatable module!\n"); + ++counter; + printf("Hello from standalone relocatable module %d!\n", counter); sleep_ms(500); } } diff --git a/tests/simulation/mspc/firmware/yasld_firmwares/hello/vector_table.S b/tests/simulation/mspc/firmware/yasld_firmwares/hello/vector_table.S index 4409762..6dbbe7c 100644 --- a/tests/simulation/mspc/firmware/yasld_firmwares/hello/vector_table.S +++ b/tests/simulation/mspc/firmware/yasld_firmwares/hello/vector_table.S @@ -3,8 +3,7 @@ // // Copyright (C) 2024 Mateusz Stadnik // -// This program is free software: you can redistribute it and/or -// modify it under the terms of the GNU General Public License +// This program is free software: you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation, either version // 3 of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be // useful, but WITHOUT ANY WARRANTY; without even the implied // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. See the GNU General Public License for more details. @@ -101,29 +100,47 @@ __register_frame_info: __libc_fini: mov pc, lr -.global initialize_on_entry -.type initialize_on_entry,%function +.global _entry +.type _entry,%function .thumb_func -initialize_on_entry: - mov r2, lr - adr r0, app_stack_pointer_symbol - ldr r0, [r0] - mov r1, r9 - ldr r0, [r1, r0] - ldr r0, [r0] - msr msp, r0 - adr r0, __vector_table_symbol - ldr r0, [r0] - ldr r0, [r1, r0] - ldr r1, =(PPB_BASE + M0PLUS_VTOR_OFFSET) - str r0, [r1] - mov pc, r2 +_entry: + adr r4, app_stack_pointer_symbol + ldr r4, [r4] + mov r5, r9 + ldr r4, [r5, r4] + ldr r4, [r4] + msr msp, r4 + adr r4, __vector_table_symbol + ldr r4, [r4] + ldr r4, [r5, r4] + ldr r5, =(PPB_BASE + M0PLUS_VTOR_OFFSET) + str r4, [r5] + mov r5, r9 + adr r4, runtime_init_symbol + ldr r4, [r4] + ldr r4, [r5, r4] + blx r4 + adr r4, main_symbol + ldr r4, [r4] + ldr r4, [r5, r4] + blx r4 + + // we can't safely return from here + mov r0, r9 + adr r1, _exit_symbol + ldr r1, [r1] + ldr r1, [r0, r1] + blx r1 + .align 4 app_stack_pointer_symbol: .word __app_stack_pointer(GOT) __vector_table_symbol: .word __vector_table(GOT) - - - +main_symbol: + .word main(GOT) +_exit_symbol: + .word _exit(GOT) +runtime_init_symbol: + .word runtime_init(GOT) diff --git a/tests/simulation/mspc/tests/boot_relocatable_hello/boot_relocatable_hello.robot b/tests/simulation/mspc/tests/boot_relocatable_hello/boot_relocatable_hello.robot index 6e09aba..22504bb 100644 --- a/tests/simulation/mspc/tests/boot_relocatable_hello/boot_relocatable_hello.robot +++ b/tests/simulation/mspc/tests/boot_relocatable_hello/boot_relocatable_hello.robot @@ -12,9 +12,9 @@ ${TEST_DISK_IMAGE} @renode_test_image@ ${RP2040_RENODE_BOARD_DIR} @renode_rp2040_board_dir@ *** Test Cases *** -Boot standalone non relocatable PICO SDK binary +Boot standalone relocatable PICO SDK binary Prepare Machine - Wait For Line On Uart This is first example that just prints! timeout=1 - Wait For Line On Uart This is first example that just prints! timeout=2 + Wait For Line On Uart Hello from standalone relocatable module 1! timeout=1 + Wait For Line On Uart Hello from standalone relocatable module 2! timeout=2