Device getting reset when use printf() (or) printk() in secure functions while calling the secure functions from non-secure firmware

Hello,

We are developing a library which is intended to use the trustzone feature available with nrf5340 DK. We followed the instructions available in Using a custom Secure Partition Manager with your application and succeeded in adding custom secure services.

When we try to debug with secure services functions in SPM, we are facing the "device reset issue" when use the printf() (or) printk() function in secure functions while calling the secure function(spm_secure_function()) from non-secure firmware.

Is it a bug or a limitation imposed in SPM? Could you please help to clarify and also any solution to fix this problem for easy debugging with secure functions?

Device not reset case:

__TZ_NONSECURE_ENTRY_FUNC
void spm_secure_function(void)
{
    // DO SOMETHING
}

Device resetting case:

__TZ_NONSECURE_ENTRY_FUNC
void spm_secure_function(void)
{
   printf("spm_secure_function called\n");
}

  • I'm able to reproduce this. I added following to the prj.conf of the application:

    CONFIG_LOG=y
    CONFIG_RESET_ON_FATAL_ERROR=n
    CONFIG_THREAD_NAME=y

    Then I added a printk to the secure function, and built and flash the program (53 nonsecure). I got the following error:

    22 NRF_EGU2             Non-Secure	OK
    23 NRF_DPPIC            Non-Secure	OK
    24 NRF_GPIOTE1          Non-Secure	OK
    25 NRF_REGULATORS       Non-Secure	OK
    
    SPM: NS image at 0x8000
    SPM: NS MSP at 0x20008b58
    SPM: NS reset vector at 0x9ce1
    SPM: prepare to jump to Non-Secure image.
    *** Booting Zephyr OS build v2.4.0-ncs1  ***
    Hello World! nrf5340dk_nrf5340_cpuapp
    [00:00:00.007,751] [1B][1;31m<err> os: Exception occurred in Secure State[1B][0m
    [00:00:00.007,751] [1B][1;31m<err> os: ***** BUS FAULT *****[1B][0m
    [00:00:00.007,751] [1B][1;31m<err> os:   Precise data bus error[1B][0m
    [00:00:00.007,751] [1B][1;31m<err> os:   BFAR Address: 0x50008120[1B][0m
    [00:00:00.007,751] [1B][1;31m<err> os: r0/a1:  0x00000000  r1/a2:  0x0000c171  r2/a3:  0x00000000[1B][0m
    [00:00:00.007,781] [1B][1;31m<err> os: r3/a4:  0x00009ba3 r12/ip:  0x9284105e r14/lr:  0xb7e75c18[1B][0m
    [00:00:00.007,781] [1B][1;31m<err> os:  xpsr:  0x78854a00[1B][0m
    [00:00:00.007,781] [1B][1;31m<err> os: Faulting instruction address (r15/pc): 0xfbcb0978[1B][0m
    [00:00:00.007,781] [1B][1;31m<err> os: >>> ZEPHYR FATAL ERROR 0: CPU exception on CPU 0[1B][0m
    [00:00:00.007,781] [1B][1;31m<err> os: Current thread: 0x20008208 (main)[1B][0m
    [00:00:00.076,934] [1B][1;31m<err> os: Halting system[1B][0m
    

    By looking at BFAR Address: 0x50008120, you can see that faults when it tries to access UART0 secure

    I will get back to you later with more information

    Best regards,

    Simon

  • Sorry for the delay on this, a huge part of the support team is currently on summer vacation and that may cause some delayed answers.

    Could you try to use uart1 or uart2 (configured as secure) in the secure service for logging?

    I tried to pick up from where I was last time, but for some reason I'm not able to make the secure service sample in Using a custom Secure Partition Manager with your application work for NCS v1.9.0/v2.0.0 (I assume you're using one of these). Could you upload your secure service sample and let me know the NCS version.

    Best regards,

    Simon

  • Hello,

    We are using the NCS v1.9.1. We had done some modifications in secure services sample to make it with nrf5340 DK environment. Here, we have uploaded our sample code for your reference.

    As per our understanding, we have disabled the CONFIG_TFM_SECURE_UART1 configuration in prj.conf which is available in Non-secure application and enabled by default in spm module. We are observing the same device getting reset issue.

    Non-Secure application configuration:

    CONFIG_SPM_BUILD_STRATEGY_SKIP_BUILD=y
    CONFIG_SPM=y
    CONFIG_ARM_FIRMWARE_USES_SECURE_ENTRY_FUNCS=y
    CONFIG_ARM_ENTRY_VENEERS_LIB_NAME="spm/libspmsecureentries.a"
    CONFIG_TFM_SECURE_UART1=n

    Could you please confirm whether any other configuration need to be enabled/disabled in prj.conf file?

    custom_spm_app_modified.zip

    • I was able to make it work by configuring uart1 as nonsecure and using that as backend for the logging in the secure service.

      Apply the following patch to NCS v2.0.0:

      diff --git a/subsys/spm/spm.c b/subsys/spm/spm.c
      index ccb48288f..208bc71ea 100644
      --- a/subsys/spm/spm.c
      +++ b/subsys/spm/spm.c
      @@ -325,9 +325,6 @@ static void spm_config_peripherals(void)
       #ifdef NRF_NVMC
       		PERIPH("NRF_NVMC", NRF_NVMC, CONFIG_SPM_NRF_NVMC_NS),
       #endif
      -#ifdef NRF_UARTE1
      -		PERIPH("NRF_UARTE1", NRF_UARTE1, CONFIG_SPM_NRF_UARTE1_NS),
      -#endif
       #ifdef NRF_UARTE2
       		PERIPH("NRF_UARTE2", NRF_UARTE2, CONFIG_SPM_NRF_UARTE2_NS),
       #endif
      

      Then test the following sample:

      custom_spm_app_modified_v2.zip

      For my nRF5340 DK, I had two COM ports:

      @$ nrfjprog --com
      1050027939    COM20    VCOM0
      1050027939    COM19    VCOM1

      The SPM-specific logging was output on COM20 (interface MCU routes uart1 here):

      *** Booting Zephyr OS build v2.7.99-ncs1  ***
      Flash regions		Domain		Permissions
      00 03 0x00000 0x10000 	Secure		rwxl
      04 63 0x10000 0x100000 	Non-Secure	rwxl
      
      Non-secure callable region 0 placed in flash region 2 with size 32.
      
      SRAM region		Domain		Permissions
      00 03 0x00000 0x08000 	Secure		rwxl
      04 63 0x08000 0x80000 	Non-Secure	rwxl
      
      Peripheral		Domain		Status
      00 NRF_P0               Non-Secure	OK
      01 NRF_CLOCK            Non-Secure	OK
      02 NRF_RTC0             Non-Secure	OK
      03 NRF_RTC1             Non-Secure	OK
      04 NRF_NFCT             Non-Secure	OK
      05 NRF_NVMC             Non-Secure	OK
      06 NRF_UARTE2           Secure		SKIP
      07 NRF_TWIM2            Non-Secure	OK
      08 NRF_SPIM3            Non-Secure	OK
      09 NRF_TIMER0           Non-Secure	OK
      10 NRF_TIMER1           Non-Secure	OK
      11 NRF_TIMER2           Non-Secure	OK
      12 NRF_SAADC            Non-Secure	OK
      13 NRF_PWM0             Non-Secure	OK
      14 NRF_PWM1             Non-Secure	OK
      15 NRF_PWM2             Non-Secure	OK
      16 NRF_PWM3             Non-Secure	OK
      17 NRF_IPC              Non-Secure	OK
      18 NRF_VMC              Non-Secure	OK
      19 NRF_FPU              Non-Secure	OK
      20 NRF_EGU0             Non-Secure	OK
      21 NRF_EGU1             Non-Secure	OK
      22 NRF_EGU2             Non-Secure	OK
      23 NRF_EGU3             Non-Secure	OK
      24 NRF_EGU4             Non-Secure	OK
      25 NRF_EGU5             Non-Secure	OK
      26 NRF_DPPIC            Non-Secure	OK
      27 NRF_REGULATORS       Non-Secure	OK
      28 NRF_DCNF             Secure		SKIP
      29 NRF_CTRLAP           Secure		SKIP
      30 NRF_SPIM4            Non-Secure	OK
      31 NRF_WDT0             Non-Secure	OK
      32 NRF_WDT1             Non-Secure	OK
      33 NRF_COMP             Non-Secure	OK
      34 NRF_LPCOMP           Non-Secure	OK
      35 NRF_PDM0             Non-Secure	OK
      36 NRF_I2S0             Non-Secure	OK
      37 NRF_QSPI             Non-Secure	OK
      38 NRF_NFCT             Non-Secure	OK
      39 NRF_MUTEX            Non-Secure	OK
      40 NRF_QDEC0            Non-Secure	OK
      41 NRF_QDEC1            Non-Secure	OK
      42 NRF_USBD             Non-Secure	OK
      43 NRF_USBREGULATOR     Non-Secure	OK
      44 NRF_P1               Non-Secure	OK
      45 NRF_OSCILLATORS      Non-Secure	OK
      46 NRF_RESET            Non-Secure	OK
      47 NRF_GPIOTE1          Non-Secure	OK
      
      SPM: NS image at 0x10000
      SPM: NS MSP at 0x20008d18
      SPM: NS reset vector at 0x12321
      SPM: prepare to jump to Non-Secure image.
      spm_secure_function called

      The nonsecure appliation logging was output on COM19 (interface MCU routes uart0 here):

      *** Booting Zephyr OS build v2.7.99-ncs1  ***
      Hello World! nrf5340dk_nrf5340_cpuapp

      Best regards,

      Simon

    • It is working fine with this patch. Thanks for the patch updates.

    Related