0
0
Cnc-programmingComparisonBeginner · 4 min read

Privileged vs Unprivileged Mode in Cortex-M: Key Differences and Usage

In Cortex-M processors, privileged mode allows full access to all system resources and special registers, while unprivileged mode restricts access to protect critical system functions. This separation helps improve system security and stability by limiting what user-level code can do.
⚖️

Quick Comparison

This table summarizes the main differences between privileged and unprivileged modes in Cortex-M processors.

AspectPrivileged ModeUnprivileged Mode
Access LevelFull access to all system resourcesRestricted access, limited to user-level resources
Register AccessCan access special control and system registersCannot access certain system registers
Memory AccessCan access all memory regionsMay be restricted from certain memory areas
Interrupt ControlCan enable/disable interruptsCannot disable interrupts
Use CaseOperating system, kernel, or trusted codeApplication code or user tasks
SecurityHigher privilege, can affect system stabilityLower privilege, safer for user code
⚖️

Key Differences

Privileged mode in Cortex-M processors is designed for trusted code such as operating system kernels or system handlers. It has unrestricted access to all CPU features, including special registers like the CONTROL register, system control block, and interrupt priority registers. This mode can configure system settings, manage memory protection, and control interrupts.

In contrast, unprivileged mode is intended for application-level code that should not interfere with critical system functions. It has limited access rights, preventing it from modifying system control registers or disabling interrupts. This restriction helps protect the system from accidental or malicious code that could destabilize the processor.

The switch between these modes is controlled by the processor's CONTROL register. Typically, the system starts in privileged mode and switches to unprivileged mode when running user applications. This separation enforces a security boundary and improves system reliability by isolating user code from core system functions.

⚖️

Code Comparison

Here is an example showing how privileged mode can modify the CONTROL register to switch to unprivileged mode.

c
/* Privileged mode code to switch to unprivileged mode */
#include "core_cm3.h"  // CMSIS header for Cortex-M3

void switch_to_unprivileged(void) {
    // Set bit 0 of CONTROL register to 1 to enter unprivileged mode
    __set_CONTROL(__get_CONTROL() | 1);
    __ISB(); // Instruction Synchronization Barrier
}

int main(void) {
    // Initially in privileged mode
    switch_to_unprivileged();
    // Now running in unprivileged mode
    while(1) {}
}
Output
No output; processor switches to unprivileged mode
↔️

Unprivileged Mode Equivalent

In unprivileged mode, attempts to access restricted registers or perform privileged operations will cause a fault. Here is an example where unprivileged code tries to disable interrupts, which is not allowed.

c
/* Unprivileged mode code attempting to disable interrupts */
#include "core_cm3.h"

void try_disable_interrupts(void) {
    // Attempt to disable interrupts (privileged operation)
    __disable_irq();
}

int main(void) {
    // Assume already in unprivileged mode
    try_disable_interrupts(); // This will cause a UsageFault
    while(1) {}
}
Output
UsageFault exception triggered due to privileged instruction in unprivileged mode
🎯

When to Use Which

Choose privileged mode when your code needs full control over the processor, such as managing hardware, configuring system settings, or handling interrupts. This mode is suitable for operating system kernels or trusted firmware.

Choose unprivileged mode for application-level code that should run safely without risking system stability. This mode limits access to critical resources, protecting the system from accidental or malicious faults caused by user code.

Key Takeaways

Privileged mode has full system access; unprivileged mode restricts access to protect the system.
Switching between modes is controlled by the CONTROL register in Cortex-M processors.
Use privileged mode for system-level code and unprivileged mode for user applications.
Unprivileged code cannot perform privileged operations like disabling interrupts.
This separation improves security and system stability by isolating user code.