Skip to content

Commit

Permalink
[cortex-m] Use fast 64-bit copy and zero tables
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Jan 24, 2025
1 parent 5b4125f commit 6c4e0f5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
15 changes: 9 additions & 6 deletions src/modm/platform/core/cortex/linker.macros
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ EXCEPTION_FRAME_SIZE = {{ exception_frame_size }};
__vector_table_rom_start = .;
__vector_table_ram_load = .;
KEEP(*(.vector_rom))
. = ALIGN(8);
__vector_table_rom_end = .;
} >{{memory}}
%% endmacro
Expand All @@ -65,7 +66,7 @@ EXCEPTION_FRAME_SIZE = {{ exception_frame_size }};
{
__vector_table_ram_start = .;
KEEP(*(.vector_ram))
. = ALIGN(4);
. = ALIGN(8);
__vector_table_ram_end = .;
} >{{memory}}
%% endmacro
Expand Down Expand Up @@ -144,10 +145,11 @@ EXCEPTION_FRAME_SIZE = {{ exception_frame_size }};
%#
.{{section}} :
{
. = ALIGN(8);
__{{section}}_load = LOADADDR(.{{section}});
__{{section}}_start = .;
*(.{{section}} .{{section}}.*)
. = ALIGN(4);
. = ALIGN(8);
__{{section}}_end = .;
} >{{memory}}
%% endfor
Expand Down Expand Up @@ -284,11 +286,11 @@ EXCEPTION_FRAME_SIZE = {{ exception_frame_size }};
%% do table_copy.append("data")
.data :
{
. = ALIGN(4);
. = ALIGN(8);
__data_load = LOADADDR(.data);
__data_start = .;
*(.data .data.* .gnu.linkonce.d.*)
. = ALIGN(4);
. = ALIGN(8);
__data_end = .;
} >{{memory}} AT >{{rom}}
%% do table_copy.extend(sections_data)
Expand All @@ -299,7 +301,7 @@ EXCEPTION_FRAME_SIZE = {{ exception_frame_size }};
__{{section}}_load = LOADADDR(.{{section}});
__{{section}}_start = .;
*(.{{section}} .{{section}}.*)
. = ALIGN(4);
. = ALIGN(8);
__{{section}}_end = .;
} >{{memory}} AT >{{rom}}
%% endfor
Expand All @@ -320,6 +322,7 @@ EXCEPTION_FRAME_SIZE = {{ exception_frame_size }};
. = ALIGN(4);
__{{section}}_end = .;
%% endfor
. = ALIGN(8);
__bss_end = .;
} >{{memory}}
%#
Expand All @@ -330,7 +333,7 @@ EXCEPTION_FRAME_SIZE = {{ exception_frame_size }};
{
__{{section}}_start = . ;
*(.{{section}} .{{section}}.*)
. = ALIGN(4);
. = ALIGN(8);
__{{section}}_end = .;
} >{{memory}}
%% endfor
Expand Down
7 changes: 5 additions & 2 deletions src/modm/platform/core/cortex/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,13 @@ def build(env):

# startup script
env.template("reset_handler.sx.in")
env.template("startup.c.in")
ops = env.template("startup.c.in")
# Prevent use of slow bytewise memcpy and memset in startup code
env.collect(":build:cflags", "-fno-builtin", operations=ops)
env.collect(":build:linkflags", "-nostartfiles")

env.template("vectors.c.in")
env.template("vectors.hpp.in")
env.collect(":build:linkflags", "-nostartfiles")

# dealing with runtime assertions
if env.has_module(":architecture:assert"):
Expand Down
34 changes: 17 additions & 17 deletions src/modm/platform/core/cortex/startup.c.in
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ extern int main(void);

// ----------------------------------------------------------------------------
// Linker section start and end pointers
extern const uint32_t __table_copy_intern_start[];
extern const uint32_t __table_copy_intern_end[];
extern const uint64_t __table_copy_intern_start[];
extern const uint64_t __table_copy_intern_end[];

extern const uint32_t __table_zero_intern_start[];
extern const uint32_t __table_zero_intern_end[];
extern const uint64_t __table_zero_intern_start[];
extern const uint64_t __table_zero_intern_end[];

extern const uint32_t __table_copy_extern_start[];
extern const uint32_t __table_copy_extern_end[];
extern const uint64_t __table_copy_extern_start[];
extern const uint64_t __table_copy_extern_end[];

extern const uint32_t __table_zero_extern_start[];
extern const uint32_t __table_zero_extern_end[];
extern const uint64_t __table_zero_extern_start[];
extern const uint64_t __table_zero_extern_end[];

extern const uint32_t __vector_table_{{ vector_table_location }}_start[];

Expand All @@ -64,13 +64,13 @@ table_call(const FunctionPointer *const start, const FunctionPointer *const end)

// Copies the section defined by a table of {loadaddr, dest start, dest end}
static inline void
table_copy(const uint32_t *const start, const uint32_t *const end)
table_copy(const uint64_t *const start, const uint64_t *const end)
{
uint32_t **table = (uint32_t **)start;
while(table < (uint32_t **)end)
uint64_t **table = (uint64_t **)start;
while(table < (uint64_t **)end)
{
const uint32_t *src = table[0]; // load address
uint32_t *dest = table[1]; // destination start
const uint64_t *src = table[0]; // load address
uint64_t *dest = table[1]; // destination start
while (dest < table[2]) // destination end
*(dest++) = *(src++);
table += 3;
Expand All @@ -79,12 +79,12 @@ table_copy(const uint32_t *const start, const uint32_t *const end)

// Zeros the section defined by a table of {start, end}
static inline void
table_zero(const uint32_t *const start, const uint32_t *const end)
table_zero(const uint64_t *const start, const uint64_t *const end)
{
uint32_t **table = (uint32_t **)start;
while(table < (uint32_t **)end)
uint64_t **table = (uint64_t **)start;
while(table < (uint64_t **)end)
{
uint32_t *dest = table[0]; // destination start
uint64_t *dest = table[0]; // destination start
while (dest < table[1]) // destination end
*(dest++) = 0;
table += 2;
Expand Down

0 comments on commit 6c4e0f5

Please sign in to comment.