Access to heap from unpriveleged task

Hi guys.

I’ve tried to look this up in the forum. I’ve come close and even saw an answer that allocating memory via the pvPortMalloc() function is possible from an unprivileged task. To give you an idea of my environment:

  • ARM Cortex M33 (nRF9160) running in the unsecure domain
  • Using gcc
  • MPU enabled
  • Heap 4 utilized
  • Running an unsecure and unprivileged task
  • Requesting a small amount of memory (16 bytes) from the heap

The code compiles with no errors. When I configure the task in question as a privileged task:

        TaskParameters_t task_parameters =
        {
            .pvTaskCode     = fs_Thread,
            .pcName         = "FS",
            .usStackDepth   = configMINIMAL_STACK_SIZE,
            .pvParameters   = NULL,
            .uxPriority     = tskIDLE_PRIORITY | portPRIVILEGE_BIT,
            .puxStackBuffer = task_stack,
            .xRegions       =
            {
                { ( void *)shared_mem,  SHAREDMEM_SIZE,            tskMPU_REGION_READ_WRITE | tskMPU_REGION_EXECUTE_NEVER },
                { ( void *)NRF_NVMC,    sizeof( NRF_NVMC_Type ),   tskMPU_REGION_READ_WRITE | tskMPU_REGION_EXECUTE_NEVER },
                { ( void *)nvdata,      NVDATA_SIZE,               tskMPU_REGION_READ_WRITE | tskMPU_REGION_EXECUTE_NEVER },
                { 0,                    0,                         0                                                      },
                { 0,                    0,                         0                                                      },
                { 0,                    0,                         0                                                      },
                { 0,                    0,                         0                                                      },
                { 0,                    0,                         0                                                      },
                { 0,                    0,                         0                                                      },
                { 0,                    0,                         0                                                      },
                { 0,                    0,                         0                                                      },
            }
        };

…all is well and the task executes as expected.

However, when the task is configured as unprivileged:

            .uxPriority     = tskIDLE_PRIORITY,

… I get a memory fault as soon as pvPortMalloc() is called. Looking at the heap_4.c file, I can see that ucHeap is declared as privileged data. I suspect that is the reason for the memory fault.

Please forgive my ignorance. I’m sure this has been brought up countless times.

Thanks,

Johnas

Unprivileged tasks can only access memory specifically allowed to them. Due to the way heaps work, to let an unprivileged task access the heap, it would need access to the WHOLE heap, which is against the normal desires for unprivileged tasks.

OK, thanks for verifying my suspicion. So, how do I allow unprivileged tasks access to a heap-like infrastructure in FreeRTOS without promoting the task to privileged mode?

You would need to create a heap in memory that you let all unprivileged tasks access. You could copy the code for one of the heap functions, rename the functions/data block and use that in the unprivileged code.