Skip to content

Commit

Permalink
relocatable test case is working
Browse files Browse the repository at this point in the history
  • Loading branch information
matgla committed May 31, 2024
1 parent b11afef commit 68a8ff8
Showing 12 changed files with 119 additions and 77 deletions.
2 changes: 1 addition & 1 deletion packages.json
Original file line number Diff line number Diff line change
@@ -88,7 +88,7 @@
{
"link": "https://github.com/matgla/Yasld.git",
"type": "git",
"version": "main",
"version": "0.0.4",
"target": "yasld",
"options": {
"cmake_variables": {
5 changes: 4 additions & 1 deletion source/main.cpp2
Original file line number Diff line number Diff line change
@@ -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
{
2 changes: 2 additions & 0 deletions source/ram_layout.cc
Original file line number Diff line number Diff line change
@@ -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;
43 changes: 27 additions & 16 deletions source/ram_manager.cpp2
Original file line number Diff line number Diff line change
@@ -27,37 +27,48 @@ 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 = {

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;
4 changes: 2 additions & 2 deletions tests/simulation/cmake/create_test_targets.cmake
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions tests/simulation/mspc/firmware/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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")
6 changes: 4 additions & 2 deletions tests/simulation/mspc/firmware/hello/main.cpp
Original file line number Diff line number Diff line change
@@ -22,10 +22,12 @@

#include <pico/stdlib.h>

int main() {
int main()
{
stdio_init_all();

while (true) {
while (true)
{
printf("This is first example that just prints!\n");
sleep_ms(1000);
}
Original file line number Diff line number Diff line change
@@ -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
50 changes: 32 additions & 18 deletions tests/simulation/mspc/firmware/yasld_firmwares/hello/executable.ld
Original file line number Diff line number Diff line change
@@ -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*)
16 changes: 3 additions & 13 deletions tests/simulation/mspc/firmware/yasld_firmwares/hello/main.cpp
Original file line number Diff line number Diff line change
@@ -2,24 +2,14 @@

#include <cstdio>

#include "vectors.hpp"

extern "C"
{
extern uint32_t app_stack_pointer;
// char *__StackTop = reinterpret_cast<char *>(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);
}
}
59 changes: 38 additions & 21 deletions tests/simulation/mspc/firmware/yasld_firmwares/hello/vector_table.S
Original file line number Diff line number Diff line change
@@ -3,8 +3,7 @@
//
// Copyright (C) 2024 Mateusz Stadnik <matgla@live.com>
//
// 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)
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 68a8ff8

Please sign in to comment.