1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-02-19 02:52:11 +00:00

refactor(core): separate clear_unused_stack() from sys/linker module

Following https://github.com/trezor/trezor-firmware/pull/4595#pullrequestreview-2609833271.

[no changelog]
This commit is contained in:
Roman Zeyde 2025-02-12 10:48:35 +02:00 committed by Roman Zeyde
parent d38b52c0c4
commit ae9e0281df
10 changed files with 74 additions and 24 deletions

View File

@ -31,15 +31,6 @@ extern uint8_t _stack_section_end;
// to reinitialize these sections if necessary.
void init_linker_sections(void);
// Clears the unused portion of the stack.
//
// This includes memory between the start of the stack
// and the current stack pointer (SP).
//
// It's safe to call this function at any time, but it's
// recommended to call it only during the startup sequence.
void clear_unused_stack(void);
// Maximum number of memory blocks in a memory region
#define MEMREGION_MAX_BLOCKS 8

View File

@ -54,21 +54,6 @@ void init_linker_sections(void) {
}
}
__attribute((naked, no_stack_protector)) void clear_unused_stack(void) {
__asm__ volatile(
" MOV R0, #0 \n"
" LDR R1, =%[sstack] \n"
"1: \n"
" STR R0, [R1], #4 \n"
" CMP R1, SP \n"
" BNE 1b \n"
" BX LR \n"
: // no output
: [sstack] "i"((uint32_t)&_stack_section_start)
: // no clobber
);
}
static void memregion_remove_block(memregion_t* region, int idx) {
if (idx < 0 || idx >= MEMREGION_MAX_BLOCKS) {
return;

View File

@ -0,0 +1,29 @@
/*
* This file is part of the Trezor project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
// Clears the unused portion of the stack.
//
// This includes memory between the start of the stack
// and the current stack pointer (SP).
//
// It's safe to call this function at any time, but it's
// recommended to call it only during the startup sequence.
void clear_unused_stack(void);

View File

@ -0,0 +1,37 @@
/*
* This file is part of the Trezor project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <trezor_types.h>
#include <sys/linker_utils.h>
__attribute((naked, no_stack_protector)) void clear_unused_stack(void) {
__asm__ volatile(
" MOV R0, #0 \n"
" LDR R1, =%[sstack] \n"
"1: \n"
" STR R0, [R1], #4 \n"
" CMP R1, SP \n"
" BNE 1b \n"
" BX LR \n"
: // no output
: [sstack] "i"((uint32_t)&_stack_section_start)
: // no clobber
);
}

View File

@ -27,6 +27,7 @@
#include <sys/irq.h>
#include <sys/linker_utils.h>
#include <sys/mpu.h>
#include <sys/stack_utils.h>
#include <sys/systick.h>
#include <sys/sysutils.h>
#include <util/image.h>

View File

@ -24,6 +24,7 @@
#include <sys/bootargs.h>
#include <sys/bootutils.h>
#include <sys/linker_utils.h>
#include <sys/stack_utils.h>
#include <sys/system.h>
#include <sys/systick.h>
#include <sys/sysutils.h>

View File

@ -23,6 +23,7 @@
#include <sec/rng.h>
#include <sys/bootargs.h>
#include <sys/linker_utils.h>
#include <sys/stack_utils.h>
#include <sys/system.h>
#ifdef KERNEL_MODE

View File

@ -24,6 +24,7 @@
#include <sys/bootutils.h>
#include <sys/linker_utils.h>
#include <sys/mpu.h>
#include <sys/stack_utils.h>
#include <sys/systask.h>
#include <sys/system.h>
#include <sys/systick.h>

View File

@ -22,6 +22,7 @@ def stm32f4_common_files(env, defines, sources, paths):
"embed/sys/mpu/inc",
"embed/sys/pvd/inc",
"embed/sec/secret/inc",
"embed/sys/stack/inc",
"embed/sys/startup/inc",
"embed/sys/syscall/inc",
"embed/sys/task/inc",
@ -70,6 +71,7 @@ def stm32f4_common_files(env, defines, sources, paths):
"embed/sys/linker/linker_utils.c",
"embed/sys/mpu/stm32f4/mpu.c",
"embed/sys/pvd/stm32/pvd.c",
"embed/sys/stack/stm32/stack_utils.c",
"embed/sys/startup/stm32/bootutils.c",
"embed/sys/startup/stm32/sysutils.c",
"embed/sys/startup/stm32f4/reset_flags.c",

View File

@ -23,6 +23,7 @@ def stm32u5_common_files(env, defines, sources, paths):
"embed/sys/linker/inc",
"embed/sys/mpu/inc",
"embed/sys/pvd/inc",
"embed/sys/stack/inc",
"embed/sys/startup/inc",
"embed/sys/syscall/inc",
"embed/sys/tamper/inc",
@ -88,6 +89,7 @@ def stm32u5_common_files(env, defines, sources, paths):
"embed/sys/linker/linker_utils.c",
"embed/sys/mpu/stm32u5/mpu.c",
"embed/sys/pvd/stm32/pvd.c",
"embed/sys/stack/stm32/stack_utils.c",
"embed/sys/startup/stm32/bootutils.c",
"embed/sys/startup/stm32/sysutils.c",
"embed/sys/startup/stm32u5/reset_flags.c",