Skip to content

Commit

Permalink
feat(core): reduce overhead of syscall invocation
Browse files Browse the repository at this point in the history
[no changelog]
  • Loading branch information
cepetr committed Feb 26, 2025
1 parent 2f48120 commit b1435fa
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 6 deletions.
13 changes: 13 additions & 0 deletions core/embed/gfx/bitblt/gfx_bitblt_rgb565.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,26 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// Turning off the stack protector for this file improves
// the performance of drawing operations when called frequently.
#pragma GCC optimize("no-stack-protector")

#include <gfx/gfx_bitblt.h>

#if USE_DMA2D
#include <gfx/dma2d_bitblt.h>
#endif

#include <io/display.h>
#include <sys/systick.h>

volatile uint64_t g_gfx_cycles;

void gfx_rgb565_fill(const gfx_bitblt_t* bb) {
// g_gfx_cycles = systick_cycles();
// display_get_orientation();
// g_gfx_cycles = systick_cycles() - g_gfx_cycles;

#if defined(USE_DMA2D) && !defined(TREZOR_EMULATOR)
if (!dma2d_rgb565_fill(bb))
#endif
Expand Down
4 changes: 4 additions & 0 deletions core/embed/gfx/bitblt/stm32/dma2d_bitblt.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// Turning off the stack protector for this file improves
// the performance of drawing operations when called frequently.
#pragma GCC optimize("no-stack-protector")

#ifdef KERNEL_MODE

#include <trezor_bsp.h>
Expand Down
23 changes: 20 additions & 3 deletions core/embed/sys/mpu/stm32f4/mpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// Turning off the stack protector for this file significantly improves
// the performance of the syscall dispatching and interrupt handling.
#pragma GCC optimize("no-stack-protector")

#include <trezor_bsp.h>
#include <trezor_model.h>
#include <trezor_rtl.h>
Expand Down Expand Up @@ -71,6 +75,19 @@ _Static_assert(NORCOW_SECTOR_SIZE == STORAGE_1_MAXSIZE, "norcow misconfigured");
_Static_assert(NORCOW_SECTOR_SIZE == STORAGE_2_MAXSIZE, "norcow misconfigured");
_Static_assert(NORCOW_SECTOR_SIZE == SIZE_64K, "norcow misconfigured");

static inline void mpu_disable(void) {
__DMB();
SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
MPU->CTRL = 0;
}

static inline void mpu_enable(void) {
MPU->CTRL = LL_MPU_CTRL_HARDFAULT_NMI | MPU_CTRL_ENABLE_Msk;
SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
__DSB();
__ISB();
}

static void mpu_init_fixed_regions(void) {
// Regions #0 to #4 are fixed for all targets

Expand Down Expand Up @@ -178,7 +195,7 @@ void mpu_init(void) {

irq_key_t irq_key = irq_lock();

HAL_MPU_Disable();
mpu_disable();

mpu_init_fixed_regions();

Expand Down Expand Up @@ -235,7 +252,7 @@ mpu_mode_t mpu_reconfig(mpu_mode_t mode) {

irq_key_t irq_key = irq_lock();

HAL_MPU_Disable();
mpu_disable();

// Region #5 and #6 are banked

Expand Down Expand Up @@ -366,7 +383,7 @@ mpu_mode_t mpu_reconfig(mpu_mode_t mode) {
// clang-format on

if (mode != MPU_MODE_DISABLED) {
HAL_MPU_Enable(LL_MPU_CTRL_HARDFAULT_NMI);
mpu_enable();
}

mpu_mode_t prev_mode = drv->mode;
Expand Down
23 changes: 20 additions & 3 deletions core/embed/sys/mpu/stm32u5/mpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// Turning off the stack protector for this file significantly improves
// the performance of the syscall dispatching and interrupt handling.
#pragma GCC optimize("no-stack-protector")

#include <trezor_bsp.h>
#include <trezor_model.h>
#include <trezor_rtl.h>
Expand Down Expand Up @@ -175,6 +179,19 @@ mpu_driver_t g_mpu_driver = {
.mode = MPU_MODE_DISABLED,
};

static inline void mpu_disable(void) {
__DMB();
SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
MPU->CTRL = 0;
}

static inline void mpu_enable(void) {
MPU->CTRL = LL_MPU_CTRL_HARDFAULT_NMI | MPU_CTRL_ENABLE_Msk;
SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
__DSB();
__ISB();
}

static void mpu_init_fixed_regions(void) {
// Regions #0 to #5 are fixed for all targets

Expand Down Expand Up @@ -240,7 +257,7 @@ void mpu_init(void) {

irq_key_t irq_key = irq_lock();

HAL_MPU_Disable();
mpu_disable();

mpu_set_attributes();

Expand Down Expand Up @@ -308,7 +325,7 @@ mpu_mode_t mpu_reconfig(mpu_mode_t mode) {

irq_key_t irq_key = irq_lock();

HAL_MPU_Disable();
mpu_disable();

// Region #5 is banked

Expand Down Expand Up @@ -398,7 +415,7 @@ mpu_mode_t mpu_reconfig(mpu_mode_t mode) {
// clang-format on

if (mode != MPU_MODE_DISABLED) {
HAL_MPU_Enable(LL_MPU_CTRL_HARDFAULT_NMI);
mpu_enable();
}

mpu_mode_t prev_mode = drv->mode;
Expand Down
4 changes: 4 additions & 0 deletions core/embed/sys/syscall/stm32/syscall_probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// Turning off the stack protector for this file improves
// the performance of syscall dispatching.
#pragma GCC optimize("no-stack-protector")

#include <trezor_model.h>

#include <sys/applet.h>
Expand Down
4 changes: 4 additions & 0 deletions core/embed/sys/syscall/stm32/syscall_verifiers.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

// Turning off the stack protector for this file improves
// the performance of syscall dispatching.
#pragma GCC optimize("no-stack-protector")

#include <trezor_rtl.h>

#include <sys/systask.h>
Expand Down

0 comments on commit b1435fa

Please # to comment.