Monitor Mode in ARM Architecture (Context Switching - Monitor Mode and Secure World) (Part 4)
Reproduced from: https://2wisebit.wordpress.com/2025/01/09/monitor-mode-in-arm-architecture-context-switching-monitor-mode-and-secure-world-part-4/
In the world of ARM TrustZone, Monitor Mode is the critical gatekeeper that carefully manages transitions between the Secure World and the Normal World. This mechanism is important for ensuring system security as only authorized transitions are allowed. When entering from the Normal World into Monitor Mode, the process is strictly controlled and often triggered by specific events such as interrupts or SMCs (secure monitor calls). In contrast, entry from the Secure World is more flexible but remains secure and allows direct manipulation of the processor state. Processor's state is carefully saved and restores so that the system's security configuration registers, such as the Secure Configuration Register (SCR), are appropriately updated. The robust checks that should prevent unauthorized access to the system involve checking trigger sources and sanitization of inputs. Implementation of these is important because one weakness can cause vulnerability and access.
3. Secure World to Monitor Mode
Entry into Monitor Mode from the Secure World is more flexible and can be initiated through exceptions or direct manipulation of the processor state.
Conditions for Entry:
Security Considerations: Although more flexible, transitions from the Secure World to Monitor Mode still adhere to strict security policies. Monitor Mode validates the requested transition to prevent unintended behavior or system corruption.
Process of Entry:
+-----------------------------+
| Context Saving |
|-----------------------------|
| - Save General-Purpose |
| Registers (R0-R12) |
| - Save Stack Pointer (SP) |
| - Save Program Counter (PC) |
| - Save CPSR |
+-----------------------------+
|
v
+-----------------------------+
| Update of Security State |
|-----------------------------|
| - Update Secure Config |
| Register (SCR) |
| - NS bit remains 0 (Secure) |
+-----------------------------+
|
v
+-----------------------------+
| Exception Handling |
|-----------------------------|
| - Save exception state |
| - Switch to Monitor Mode |
| for secure handling |
+-----------------------------+
|
v
+-----------------------------+
| Execution in Monitor Mode |
|-----------------------------|
| - Execute Monitor Mode |
| handler |
| - Manage transition to |
| Normal World or handle |
| exception |
+-----------------------------+
4. Exit from Monitor Mode to Secure World
Exiting Monitor Mode to the Secure World involves restoring the Secure state context and ensuring that the Secure world resumes execution securely.
+-----------------------------+
| Restoration of |
| Secure Context |
|-----------------------------|
| - Restore General-Purpose |
| Registers (R0-R12) |
| - Restore Stack Pointer (SP)|
| - Restore Program Counter |
| (PC) |
| - Restore CPSR |
+-----------------------------+
|
v
+-----------------------------+
| Update of Security State |
|-----------------------------|
| - Update SCR to set NS bit |
| to 0 (Secure state) |
| - Restore CPSR using |
| SPSR_mon |
+-----------------------------+
|
v
+-----------------------------+
| Memory Partitioning |
|-----------------------------|
| - Reconfigure MMU/MPU to |
| enforce Secure memory |
| access permissions |
| - Restrict Non-secure memory|
| regions if required |
+-----------------------------+
|
v
+-----------------------------+
| Exception Handling |
|-----------------------------|
| - Clear exception state if |
| triggered by an exception |
| (e.g., interrupt or abort)|
+-----------------------------+
|
v
+-----------------------------+
| Execution Resume |
|-----------------------------|
| - Exit Monitor Mode |
| - Resume execution in Secure|
| state at restored PC |
+-----------------------------+
Key Differences Between Normal and Secure World Entry
--------------------------------------------------------
| Aspect | Normal World Entry |
|-----------------|------------------------------------|
| Triggers | Interrupts, External Aborts, SMC |
| | Instruction |
| Flexibility | Strictly controlled |
| Security Checks | Rigorously enforced to prevent |
| | unauthorized access |
| Primary Use Case| Requesting secure services, |
| | handling exceptions |
| | |
| Aspect | Secure World Entry |
|-----------------|------------------------------------|
| Triggers | Interrupts, External Aborts, CPSR |
| | Manipulation |
| Flexibility | More flexible |
| Security Checks | Assumes a trusted environment, but |
| | checks still apply |
| Primary Use Case| Managing Secure World state or |
| | handling exceptions |
--------------------------------------------------------
+-------------------+-----------------+------------------+
| Trigger Source | Trigger | Use Case |
| | Mechanism | |
+-------------------+-----------------+------------------+
| From Non-secure | SMC Instruction | Request Secure |
| World | | services or |
| | | resources. |
| |-----------------+------------------|
| | IRQ/FIQ | Handle urgent |
| | Interrupts | events needing |
| | | Secure access. |
| |-----------------+------------------|
| | External Abort | Resolve memory |
| | | violations or |
| | | faults. |
+-------------------+-----------------+------------------+
| From Secure World | Exceptions | Handle Secure |
| | | World faults or |
| | | events. |
| |-----------------+------------------|
| | Direct CPSR | Perform Secure |
| | Manipulation | ops or debugging.|
+-------------------+-----------------+------------------+
Security Checks
Before proceeding with a state transition,?Monitor Mode?performs a set of?security checks?to verify the request and ensure it complies with the system's security policies. This step is critical in preventing unauthorized access to sensitive data or resources in the?Secure World.
Key Steps in Security Checks:
Example minimal Monitor mode code
.section .text
.global monitor_entry
monitor_entry:
/* Save the processor state (Secure or Non-secure) */
MRS r0, CPSR /* Save Current Program Status Register */
STMFD sp!, {r0-r12, r14} /* Save general-purpose registers and PC */
MRS r1, SPSR /* Save the SPSR (Saved Program Status Register) */
STMFD sp!, {r1} /* Save SPSR to the stack */
/* Security configuration */
MRC p15, 0, r2, c1, c1, 0 /* Read SCR (Secure Configuration Register) */
TST r2, #1 /* Check NS bit */
BEQ switch_to_secure /* If NS=0, switch to Secure World */
switch_to_nonsecure:
/* Prepare to switch to Non-secure state */
ORR r2, r2, #1 /* Set NS bit to 1 */
MCR p15, 0, r2, c1, c1, 0 /* Write back SCR */
LDMFD sp!, {r1} /* Restore SPSR */
MSR SPSR, r1 /* Restore SPSR */
LDMFD sp!, {r0-r12, r14} /* Restore general-purpose registers and PC */
SUBS PC, LR, #4 /* Return from exception to Non-secure World */
switch_to_secure:
/* Prepare to switch to Secure state */
BIC r2, r2, #1 /* Clear NS bit to 0 */
MCR p15, 0, r2, c1, c1, 0 /* Write back SCR */
LDMFD sp!, {r1} /* Restore SPSR */
MSR SPSR, r1 /* Restore SPSR */
LDMFD sp!, {r0-r12, r14} /* Restore general-purpose registers and PC */
SUBS PC, LR, #4 /* Return from exception to Secure World */
.end
Previous parts
Part 1 - Monitor Mode in ARM Architecture (Part 1)
Part 2 - Monitor Mode in ARM Architecture (Context Switching)
Part 3 - Monitor Mode in ARM Architecture (Context Switching - Monitor Mode and Normal World)