Privileged vs Unprivileged Mode in Cortex-M: Key Differences and Usage
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.
| Aspect | Privileged Mode | Unprivileged Mode |
|---|---|---|
| Access Level | Full access to all system resources | Restricted access, limited to user-level resources |
| Register Access | Can access special control and system registers | Cannot access certain system registers |
| Memory Access | Can access all memory regions | May be restricted from certain memory areas |
| Interrupt Control | Can enable/disable interrupts | Cannot disable interrupts |
| Use Case | Operating system, kernel, or trusted code | Application code or user tasks |
| Security | Higher privilege, can affect system stability | Lower 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.
/* 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) {} }
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.
/* 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) {} }
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.