Ken Liu | 4051016 | 2021-05-06 17:38:49 +0800 | [diff] [blame] | 1 | ######################## |
| 2 | Secure Partition Manager |
| 3 | ######################## |
| 4 | This document describes the Secure Partition Manager(`SPM`) implementation |
| 5 | design in Trusted Firmware-M (`TF-M`). |
| 6 | |
| 7 | .. note:: |
| 8 | - The FF-M in this document refers to the accumulated result of two |
| 9 | specifications: |
| 10 | `FF-M v1.1 Update <https://developer.arm.com/documentation/aes0039/latest>`_ |
| 11 | on |
| 12 | `FF-M v1.0 <https://developer.arm.com/-/media/Files/pdf/PlatformSecurityArchitecture/Architect/DEN0063-PSA_Firmware_Framework-1.0.0-2.pdf?revision=2d1429fa-4b5b-461a-a60e-4ef3d8f7f4b4&la=en&hash=BE8C59DBC98212591E1F935C2312D497011CD8C7>`_. |
| 13 | - The words marked as `interpreted` are defined terms. Find the terms in |
| 14 | referenced documents if it is not described in this document. |
| 15 | |
| 16 | ************ |
| 17 | Introduction |
| 18 | ************ |
| 19 | The service access process of FF-M: |
| 20 | |
| 21 | .. figure:: media/tfmdev.* |
| 22 | :align: center |
| 23 | :name: fig-tfmdev |
| 24 | :width: 80% |
| 25 | |
| 26 | FF-M service access process |
| 27 | |
| 28 | Secure services (aka `Service`) is the component providing secure |
| 29 | functionalities in `SPE`, and `Client` is the user of the `Service`. A service |
| 30 | act as a client when it is accessing its depending services. |
| 31 | |
| 32 | Services are grouped into `Secure Partition` (aka `partition`). A partition: |
| 33 | |
| 34 | - Contains services with the same purpose. |
| 35 | - Provides implementation required isolation boundaries. |
| 36 | - Is a software development unit. |
| 37 | |
| 38 | Each service exposes its `Service ID` (`SID`) and `Handle` for client access |
| 39 | usage. Clients access services by `SID` or `Handle` through FF-M `Client API`. |
| 40 | Partitions use FF-M `Secure Partition API` when it needs to operate on client |
| 41 | data or reply to a client. |
| 42 | |
| 43 | `SPM` is the centre of an FF-M compliant implementation, which set up and |
| 44 | maintains a firmware framework that: |
| 45 | |
| 46 | - Implements `Client API` and `Secure Partition API`. |
| 47 | - Manages partition runtime to follow FF-M. |
| 48 | - Involves necessary implementation-defined items to support the |
| 49 | implementation. |
| 50 | |
| 51 | SPM interfaces are consist of these two categories: |
| 52 | |
| 53 | - FF-M defined API. |
| 54 | - Extended API to support the implementation. |
| 55 | |
| 56 | Both API categories are compliant with FF-M concepts and guidelines. The core |
| 57 | concept of TF-M SPM surrounds the FF-M defined service management and access |
| 58 | process. Besides this, another important implementation part is partition |
| 59 | runtime management. |
| 60 | |
| 61 | Partition runtime model |
| 62 | ======================= |
| 63 | One partition must work under as `ONE` of the runtime models: |
| 64 | `Inter-process communication` (`IPC`) model or `Secure Function` (`SFN`) |
| 65 | model. |
| 66 | |
| 67 | A partition that runs under the `IPC` model looks like a classic `process`. |
| 68 | There is `ONE` thread inside the partition keeps waiting for signals. SPM |
| 69 | converts the service accessing info from the `Client API` call into messages |
| 70 | and assert a signal to the partition. The partition calls corresponded service |
| 71 | function indicated by the signal and its bound message, and reply service |
| 72 | returned result to the client. The advantages of this model: |
| 73 | |
| 74 | - It provides better isolation by limiting the interfaces on data interactive. |
| 75 | Data are preferred to be processed in a local buffer. |
| 76 | - It provides a mechanism for handling multiple service access. There is no |
| 77 | memory mapping mechanism in the MCU system, hence it is hard to provide |
| 78 | multiple function call contexts when serving multiple-threaded clients if |
| 79 | the service access is implemented in a function-call based mechanism. This |
| 80 | model converts multiple service accesses into messages, let the partition |
| 81 | handles the service access in messages one by one. |
| 82 | |
| 83 | The `Secure Function` (`SFN`) model partition is close to a `library`. Each |
| 84 | service is provided as a function entry inside the partition. SPM launches |
| 85 | the target service function after the service is found. The whole procedure |
| 86 | (from client to service function) is a function call. This model: |
| 87 | |
| 88 | - Saves the workloads spent on `IPC` scheduling. |
| 89 | |
| 90 | Meanwhile, it relaxes the data interactive mechanism, for example, allow |
| 91 | direct memory access (MMIOVEC). And it is hard to enable multiple-threaded |
| 92 | clients service access because of multiple call context-maintenance |
| 93 | difficulties. |
| 94 | |
| 95 | An implementation contains only `SFN` partitions fits better in the |
| 96 | resource-constrained devices, it is called an `SFN model implementation`. And |
| 97 | it is an `IPC model implementation` when `IPC` partitions exist in the system. |
| 98 | |
| 99 | .. note:: |
| 100 | `IPC model implementation` can handle access to the services in the `SFN` |
| 101 | partition. |
| 102 | |
| 103 | Components and isolation levels |
| 104 | =============================== |
| 105 | There are `THREE` isolation levels defined in `FF-M`. These levels can |
| 106 | fulfil different security requirements by defining different isolation |
| 107 | boundaries. |
| 108 | |
| 109 | .. figure:: media/modelisolation.* |
| 110 | :align: center |
| 111 | :name: fig-modelisolation |
| 112 | :width: 80% |
| 113 | |
| 114 | Components and isolation boundaries |
| 115 | |
| 116 | .. note:: |
| 117 | Concept `ARoT`, `PRoT`, `domain`, and boundaries are in the `FF-M` |
| 118 | specification. |
| 119 | |
| 120 | Not like an `SPE` client that can call `Client API` to access the secure |
| 121 | services in one step, an `NSPE` client needs to cross the secure boundaries |
| 122 | first before calling `Client API`. The component `NS Agent` in |
| 123 | :numref:`fig-modelisolation` represents `NSPE` clients after they crossed |
| 124 | the secure boundaries. This could help `SPM` handles the request in a |
| 125 | unified way instead of care about the special boundaries. |
| 126 | |
| 127 | .. note:: |
| 128 | `NS Agent` is a necessary implementation-defined component out of FF-M |
| 129 | specification. `NS Agent` has a dedicated stack because secure and |
| 130 | non-secure can not share the stack. It also has dedicated execution bodies. |
| 131 | For example, RPC-based `NS Agent` has a while loop that keeps waiting for |
| 132 | messages; and Trustzone-based `NS Agent` has veneer code to take over `NSPE` |
| 133 | secure call. This makes `NS Agent` is a component more like a `process`. |
| 134 | Hence in the simplest implementation (`SFN model implementation` mentioned |
| 135 | above), `NS Agent` is the only process in the system, the scheduling |
| 136 | logic can be extremely simplified since no other process execution needs to |
| 137 | be scheduled. But the scheduling interface is still necessary to SPM, this |
| 138 | could help SPM treat both `SFN` and `IPC` model implementation in a unified |
| 139 | way. |
| 140 | |
| 141 | Check `NS Agent`_ for details. |
| 142 | |
| 143 | Implementation principle |
| 144 | ======================== |
| 145 | The principles for TF-M SPM implementation: |
| 146 | |
| 147 | .. important:: |
| 148 | - SPM can treat these components as the client: NS Agent, SFN Partition, |
| 149 | and IPC partition. |
| 150 | - These components can provide services: SFN Partition, IPC partition, and |
| 151 | built-in services. A built-in service is built up with SPM together. |
| 152 | - All partition services must be accessed by `Client API`. |
| 153 | - Partitions interact with client data by `Secure Partition API`. |
| 154 | - Built-in services are strongly recommended to be accessed by `Client API`. |
| 155 | Customized interfaces are restricted. |
| 156 | - Built-in services can call SPM internal interfaces directly. |
| 157 | |
| 158 | ****************** |
| 159 | Runtime management |
| 160 | ****************** |
| 161 | The runtime execution runs among the components, there are **4** runtime |
| 162 | states: |
| 163 | |
| 164 | - `Initializing` state, to set up the SPM runtime environment after system |
| 165 | powers up |
| 166 | - `IDLE` state, when SPM runtime environment is set up and partitions are |
| 167 | ready for service access. |
| 168 | - `Serving` state, when partition is under initializing or service access |
| 169 | handling. |
| 170 | - `Background` state, such as the arrival of secure interrupt or unexpected |
| 171 | faults. `Background` state returns to the state it preempts. `Background` |
| 172 | state can be nested. |
| 173 | |
| 174 | The state transition diagram: |
| 175 | |
| 176 | .. figure:: media/spestate.* |
| 177 | :align: center |
| 178 | :name: fig-spestate |
| 179 | :width: 70% |
| 180 | |
| 181 | SPE runtime execution states |
| 182 | |
| 183 | Initializing |
| 184 | ============ |
| 185 | The goal of TF-M initializing is to perform necessary initialization and |
| 186 | move to the `Serving`_ state. This state starts with platform-specific power |
| 187 | on sequence, then `SPM` takes over the execution to perform these operations: |
| 188 | |
| 189 | #. A preparation initialization process before SPM runtime initialization. |
| 190 | #. SPM runtime initialization. |
| 191 | #. A post initialization happens after the SPM runtime initialization and |
| 192 | before the first partition gets launched. |
| 193 | |
| 194 | .. note:: |
| 195 | These procedures and their sub-routines are recommended to be applied with |
| 196 | execution measurement mechansim to mitigate the `Hardware Fault Injection` |
| 197 | attack. |
| 198 | |
| 199 | Preparation initialization |
| 200 | -------------------------- |
| 201 | The purpose of this preparation initialization is to provide a chance for |
| 202 | performing those security required but generic platform power-on skipped |
| 203 | operations, such as: |
| 204 | |
| 205 | - Restrict `SPM` execution, for example, set up memory overflow settings for |
| 206 | SPM runtime memory, or set code out of SPM as un-executable, even though |
| 207 | SPM is a privileged component in general. |
| 208 | |
| 209 | .. note:: |
| 210 | The ``logging``-related peripheral can be set up **AT THIS STEP**, if |
| 211 | logging is enabled and it needs peripheral support. There is no standalone |
| 212 | initializing HAL API proposed for logging, so here is an ideal place for |
| 213 | initializing them. |
| 214 | |
| 215 | This procedure is abstracted into one `HAL`, and a few example procedures |
| 216 | are implemented as its sub-routines for reference: |
| 217 | |
| 218 | - Architecture extensions initialization, Check chapter |
| 219 | `Architecture security settings`_ for detailed information. |
| 220 | - Isolation and lifecycle initialization. |
| 221 | |
| 222 | The load isolation boundaries need to be set up here, such as SPE/NSPE |
| 223 | boundary, and ARoT/PRoT boundary if isolation level 2 is applied. |
| 224 | |
| 225 | The lifecycle is initiated by a secure bootloader usually. And in this stage |
| 226 | of SPM initializing, SPM double-checks the lifecycle set up status (following |
| 227 | a specific lifecycle management guidelines). Note that the hardware debugger |
| 228 | setting can be part of lifecycle settings. |
| 229 | |
| 230 | .. important:: |
| 231 | Double-check debugger setting when performing a product release. |
| 232 | |
| 233 | SPM runtime initialization |
| 234 | -------------------------- |
| 235 | This procedure initializes necessary runtime operations such as memory |
| 236 | allocator, loading partitions and partition-specific initialization |
| 237 | (binding partitions with platform resources). |
| 238 | |
| 239 | The general processes: |
| 240 | |
| 241 | #. Initialize runtime functionalities, such as memory allocator. |
| 242 | #. Load partitions by repeating below steps: |
| 243 | |
| 244 | * Find a partition load information. |
| 245 | * Allocate runtime objects for this partition. |
| 246 | * Link the runtime objects with load information. |
| 247 | * Init partition contexts (Thread and call context). |
| 248 | * Init partition isolation boundaries (MMIO e.g.). |
| 249 | * Init partition interrupts. |
| 250 | |
| 251 | After no more partitions to be loaded, the SPM runtime is set up but |
| 252 | partitions' initialization routines have not run yet - the partition runtime |
| 253 | context is initialized for the routine call. |
| 254 | |
| 255 | The partition initialization routine is a special service that serves SPM |
| 256 | only, because: |
| 257 | |
| 258 | - SPM needs to call the initialization routine, just like it calls into |
| 259 | the service routine. |
| 260 | - The partition initialization routine can access its depending services. |
| 261 | Putting initialization routine in the same runtime environment as common |
| 262 | service routines can avoid special operations. |
| 263 | |
| 264 | Hence a `Partition initialization client` needs to be created to initialize |
| 265 | the SFN partitions, because: |
| 266 | |
| 267 | - `SPM runtime initialization` happen inside a special runtime environment |
| 268 | compare to the partition runtime execution, then an environment switching |
| 269 | is needed. |
| 270 | - IPC partitions are initialized by the scheduler and dependencies are |
| 271 | handled by signals and messages asynchronously, hence IPC partitions can |
| 272 | process the dependencies by their own. |
| 273 | |
| 274 | The `Partition initialization client` is created differently based on the |
| 275 | implementation runtime model: |
| 276 | |
| 277 | - A SFN client is created under the SFN model implementation. |
| 278 | - A IPC client is created under the IPC model implementation. This client |
| 279 | thread has the highest priority. |
| 280 | |
| 281 | As the other partitions, the client is created with context standby, and it |
| 282 | is executed after the `Post initialization`_ stage. |
| 283 | |
| 284 | Post initialization |
| 285 | ------------------- |
| 286 | Platform code can change specific partition settings in this procedure before |
| 287 | partitions start. A few SPM API is callable at this stage, such as set a |
| 288 | signal into a specific partition, or customized peripheral settings. |
| 289 | |
| 290 | Serving |
| 291 | ======= |
| 292 | Two execution categories work under this state: |
| 293 | |
| 294 | - `Partition initialization routine execution`_. |
| 295 | - `Secure service access`_. |
| 296 | |
| 297 | This state indicates the serving is ongoing. It is mainly the service routine |
| 298 | execution, plus a few SPM executions when SPM API gets called. |
| 299 | |
| 300 | .. important:: |
| 301 | The service access process introduce in this chapter |
| 302 | (Such as `Secure service access`_) is abstracted from the FF-M |
| 303 | specification. Reference the FF-M specification for the details of each |
| 304 | step. |
| 305 | |
| 306 | Partition initialization routine execution |
| 307 | ------------------------------------------ |
| 308 | The partition initialization routines get called. One partition may access its |
| 309 | depending services during initializing, then this procedure is a |
| 310 | `Secure service access`_. |
| 311 | |
| 312 | The initialization routine gets called initially by |
| 313 | `Partition initialization client`, also can be called by Client API before |
| 314 | service access, if the target partition is not initialized but a service |
| 315 | access request is raised by one client. |
| 316 | |
| 317 | Secure service access |
| 318 | --------------------- |
| 319 | The process of service access: |
| 320 | |
| 321 | #. A `client` calls an FF-M Client API. |
| 322 | #. `SPM` validates inputs and looks up for the targeted service. |
| 323 | #. `SPM` constructs the request to be delivered under a proper runtime |
| 324 | mechanism. |
| 325 | #. The target service gets executed. It can perform internal executions or |
| 326 | access depending services to prepare the response. It also can wait for |
| 327 | specific signals. |
| 328 | #. The target service calls FF-M Secure Partition API to request a reply to |
| 329 | the client. |
| 330 | #. SPM delivers the response to the client, and the API called by the client |
| 331 | returns. |
| 332 | |
| 333 | The mechanism of how SPM interact with the target partition depends on the |
| 334 | partition runtime model. |
| 335 | |
| 336 | - Access to a service in an SFN partition is a function call, which does not |
| 337 | switch the current process indicator. |
| 338 | - Access to a service in an IPC partition leads to scheduling, which switches |
| 339 | the current process indicator. |
| 340 | - When the execution roams between components because of a function call or |
| 341 | scheduling, the isolation boundaries NEED to be switched if there are |
| 342 | boundaries between components. |
| 343 | |
| 344 | .. figure:: media/hybridruntime.* |
| 345 | :align: center |
| 346 | :name: fig-hybridruntime |
| 347 | :width: 60% |
| 348 | |
| 349 | No matter what kind of partition a client is trying to access, the SPM API is |
| 350 | called firstly as it is the interface for service access. There are two ABI |
| 351 | types when calling SPM API: Cross-boundary or No-cross-boundary. |
| 352 | |
| 353 | Calling SPM API |
| 354 | --------------- |
| 355 | SPM is placed in the PRoT domain. It MAY have isolation boundaries under |
| 356 | particular isolation levels. For example: |
| 357 | |
| 358 | - There are boundaries between ARoT components and SPM under isolated level 2 |
| 359 | and 3. |
| 360 | |
| 361 | Then API SPM provided needs to support the function call (no boundary |
| 362 | switching) and cross-boundary call. A direct call reaches the API entrance |
| 363 | directly, while a cross-boundary call needs a mechanism (Supervisor call e.g.) |
| 364 | to cross the boundary first before reaching the API entrance. |
| 365 | |
| 366 | .. figure:: media/twocalltypes.* |
| 367 | :align: center |
| 368 | :name: fig-twocalltypes |
| 369 | :width: 60% |
| 370 | |
| 371 | SPM call types |
| 372 | |
| 373 | SPM internal execution flow |
| 374 | --------------------------- |
| 375 | SPM internal execution flow as shown in diagram: |
| 376 | |
| 377 | .. figure:: media/abi_scheduler.* |
| 378 | :align: center |
| 379 | :name: fig-abi_scheduler |
| 380 | :width: 60% |
| 381 | |
| 382 | SPM API runtime |
| 383 | |
| 384 | The process: |
| 385 | |
| 386 | - PSA API gets called by one of the ABI mentioned in the last chapter as |
| 387 | `ABI 1` in the diagram. |
| 388 | - The unified API Handler calls FF-M and backend subroutines in sequence. |
| 389 | - The `FF-M` subroutine performs `FF-M` defined operations. |
| 390 | - The backend operations perform target partition runtime model decided |
| 391 | operations. For example, enqueue message into the target partition under |
| 392 | the IPC runtime model, or prepare to call context with the message as the |
| 393 | parameters under the SFN runtime model. |
| 394 | - API Handler triggers different ABI based on the result of the backends. |
| 395 | |
| 396 | The API handler: |
| 397 | |
| 398 | - Can process the `PROGRAMMER_ERROR` in a unified place. |
| 399 | - Can see the prepared caller and callee context, with exited SPM context. It |
| 400 | is an ideal place for subsequent operations such as context switching. |
| 401 | |
| 402 | A example code: |
| 403 | |
| 404 | .. code-block:: c |
| 405 | |
| 406 | void abi(void *p) |
| 407 | { |
| 408 | status = spm_api(p); |
| 409 | /* |
| 410 | * Now both the caller and calle context are |
| 411 | * managed by spm_api. |
| 412 | */ |
| 413 | if (status == ACTION1) { |
| 414 | /* |
| 415 | * Check if extra operations are required |
| 416 | * instead of a direct return. |
| 417 | */ |
| 418 | exit_action1(); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | The explanation about `Scheduler Lock`: |
| 423 | |
| 424 | Some FF-M API runs as a generic thread to prevent long time exclusive |
| 425 | execution. When a preemption happens, a new partition thread can call SPM API |
| 426 | again, makes SPM API nested. It needs extra memory in SPM to be allocated to |
| 427 | store the preempted context. Lock the scheduler while SPM API is executing can |
| 428 | ensure SPM API complete execution after preemption is handled. There can be |
| 429 | multiple ways to lock the scheduler: |
| 430 | |
| 431 | - Set a scheduler lock. |
| 432 | - Set SPM API thread priority as the highest. |
| 433 | |
| 434 | Backend service messaging |
| 435 | ------------------------- |
| 436 | A message to service is created after the target service is found and the |
| 437 | target partition runtime model is known. The preparation before ABI triggers |
| 438 | the final accessing: |
| 439 | |
| 440 | - The message is pushed into partition memory under a specific ABI mechanism |
| 441 | if the target partition model is `SFN` and there are boundaries between SPM |
| 442 | and the target partition. After this, requests a specific call type to the |
| 443 | SPM ABI module. |
| 444 | - The target service routine is get called with the message parameter if |
| 445 | there are no boundaries between SPM and the target partition and the |
| 446 | partition runtime is `SFN`. |
| 447 | - The message is queued into the partition message list if the target |
| 448 | partition runtime model is `IPC`. |
| 449 | - IPC partition replies to the client by `psa_reply`, which is another SPM API |
| 450 | call procedure. |
| 451 | - SFN partition return triggers an implied `psa_reply`, which is also another |
| 452 | SPM API call procedure. |
| 453 | |
| 454 | .. note:: |
| 455 | The backends also handle the isolation boundary switching. |
| 456 | |
| 457 | Sessions and contexts |
| 458 | --------------------- |
| 459 | FF-M API allows multiple sessions for a service if the service is classic |
| 460 | connection-based. The service can maintain multiple local session data and use |
| 461 | `rhande` in the message body to identify which client this session is bound |
| 462 | with. |
| 463 | |
| 464 | But this does not mean when an ongoing service accessing is preempted, |
| 465 | another service access request can get a chance for new access. This is |
| 466 | because of the limited context storage - supporting multiple contexts in a |
| 467 | common service costs much memory, and runtime operations(allocation and |
| 468 | re-location). Limited the context content in the stack only can mitigate the |
| 469 | effort, but this requirement requires too much for the service development. |
| 470 | |
| 471 | The implementation-decisions are: |
| 472 | |
| 473 | - IPC partitions handles messages one by one, the client get blocked before |
| 474 | the service replying to the client. |
| 475 | - The client is blocked when accessing services are handling a service |
| 476 | request in an SFN partition. |
| 477 | |
| 478 | ABI type summary |
| 479 | ---------------- |
| 480 | The interface type is decided by the runtime model of the target component. |
| 481 | Hence PSA API has two types of ABI: `Cross-boundary ABI` and |
| 482 | `Function call ABI`. After SPM operations, one more component runtime type |
| 483 | shows up: The IPC partition, hence `schedule` is the mechanism when accessing |
| 484 | services inside an IPC partition. |
| 485 | |
| 486 | .. figure:: media/spmabitypes.* |
| 487 | :align: center |
| 488 | :name: fig-spmabi |
| 489 | :width: 60% |
| 490 | |
| 491 | ABI types |
| 492 | |
| 493 | .. note:: |
| 494 | The API that does not switch context returns directly, which is not |
| 495 | covered in the above diagram. |
| 496 | |
| 497 | IDLE state |
| 498 | ========== |
| 499 | The `IDLE state` can be represented by the `NS Agent` action: |
| 500 | |
| 501 | - Launching NSPE software (Trustzone case, e.g.), or send a signal to NSPE |
| 502 | software (RPC case, e.g.). |
| 503 | |
| 504 | It is because `NS Agent` is the last component being initialized in the |
| 505 | system. Its execution indicates other partitions' initialization has |
| 506 | accomplished. |
| 507 | |
| 508 | Background state |
| 509 | ================ |
| 510 | Background execution can happen at any time when the arrival of interrupts or |
| 511 | execution faults. An ongoing background execution indicates the state is a |
| 512 | `Background state`. The characteristics: |
| 513 | |
| 514 | - The background state has a higher execution priority than other states - |
| 515 | other states stall when the background state is executing. |
| 516 | - Background execution can be nested. For example, an |
| 517 | interrupt handler can preempt an ongoing interrupt execution. |
| 518 | - Particular partition code can be involved in the background state, for |
| 519 | example, the `First Level Interrupt Handler (FLIH)` of one partition. |
| 520 | - Background state MUST return to the state it preempts. |
| 521 | |
| 522 | .. note:: |
| 523 | Interrupt handling is a common background state example. Check Interrupt |
| 524 | design document for details. |
| 525 | |
| 526 | ****************************** |
| 527 | Practical implementation items |
| 528 | ****************************** |
| 529 | This chapter describes the practical implementation contents. |
| 530 | |
| 531 | .. important:: |
| 532 | Arm M-profile architecture is the default hardware architecture when |
| 533 | describing architecture-specific items. |
| 534 | |
| 535 | The general M-profile programming is not involved in this document. The |
| 536 | following chapters introduce the mandatory settings for security |
| 537 | requirements. |
| 538 | |
| 539 | Architecture security settings |
| 540 | ============================== |
| 541 | When an `Armv8m Security Extension` (Aka `Trustzone-M`) is available in the |
| 542 | system, these settings are required to be set: |
| 543 | |
| 544 | - The MSPLIM needs to be set correctly to prevent stack overflow. |
| 545 | - The exception handler priority needs to be decided. |
| 546 | - Boost the secure handler mode priority to prevent NSPE from preempting SPE |
| 547 | handler mode execution(`AIRCR.PRIS`). |
| 548 | - Disable NSPE hardware faults when a secure fault is happening. Trap in the |
| 549 | secure fault with the highest priority can be a valid option. |
| 550 | - Push seals on the stack top when a stack is allocated (`TFMV-1`). Also |
| 551 | check `Stack seal`_ chapter for details. |
| 552 | |
| 553 | Besides `Armv8m Security Extension`, these settings need to care when |
| 554 | `Floatpoint Extension` is enabled for partition usage: |
| 555 | |
| 556 | - `FPCCR.TS`, `FPCCR.CLRONRET` and `FPCCR.CLRONRETS` need to be set when |
| 557 | booting. |
| 558 | - `CPACR.CP10` and `CPACR.CP11` need to be set when booting. |
| 559 | |
| 560 | .. important:: |
| 561 | Floatpoint usage is prohibited in SPM and background execution. |
| 562 | |
| 563 | Stack seal |
| 564 | ---------- |
| 565 | When Trustzone-M is applied, the architecture specification recommends sealing |
| 566 | the secure stack by: |
| 567 | |
| 568 | - Push two `SEAL` values (`0xFEF5EDA5`) at the stack bottom, when a stack is |
| 569 | allocated. |
| 570 | - Push two `SEAL` values on the stack pointer which is going to be switched |
| 571 | out. |
| 572 | |
| 573 | Check architecture specification and vulnerability `TFMV-1` for details. |
| 574 | |
| 575 | Trustzone-M reentrant |
| 576 | --------------------- |
| 577 | The Trustzone-M has characteristics that: |
| 578 | |
| 579 | - SPE keeps the last assigned stack pointer value when execution leaves SPE. |
| 580 | - SPE execution can be preempted by NSPE which causes an execution left. |
| 581 | |
| 582 | It is possible that NSPE preemption caused a second thread calls into SPE and |
| 583 | re-uses the secure stack contains the first thread's context, which obviously |
| 584 | causes information leakage and runtime state inconsistent. |
| 585 | |
| 586 | Armv8.1-M provides the hardware setting `CCR_S.TRD` to prevent the reentrant. |
| 587 | On an Armv8.0-M architecture, extra software logic needs to be added at the |
| 588 | veneer entry: |
| 589 | |
| 590 | - Check if the local stack points to a `SEAL` when veneer code get executed. |
| 591 | |
| 592 | .. code-block:: c |
| 593 | |
| 594 | /* This is a theoretical code that is not in a real project. */ |
| 595 | veneer() { |
| 596 | content = get_sp_value(); |
| 597 | if (context != SEAL) /* Error if reentrant detected */ |
| 598 | error(); |
| 599 | } |
| 600 | |
| 601 | SPM Runtime ABI |
| 602 | =============== |
| 603 | This chapter describes the runtime implementation of SPM. |
| 604 | |
| 605 | Scheduling |
| 606 | ---------- |
| 607 | The scheduling logic is put inside the PendSV mode. PendSV mode's priority |
| 608 | is set as one level higher than the default thread mode priority. If |
| 609 | `Trustzone-M` is present, the priority is set as the lowest just above NS |
| 610 | exception priority to prevent a preemption in secure exceptions. |
| 611 | |
| 612 | PendSV is an ideal place for scheduling logic, because: |
| 613 | |
| 614 | - An interrupt triggered scheduling during PendSV execution lead to another |
| 615 | PendSV execution before exception return to the thread mode, which can find |
| 616 | the latest run-able thread. |
| 617 | |
| 618 | Function call ABI |
| 619 | ----------------- |
| 620 | In the diagram :numref:`fig-abi_scheduler`, the ABI can have two basic |
| 621 | types: cross-boundary and direct call (No-cross-boundary). |
| 622 | |
| 623 | When applying `SVCall` (`SVC`) as the cross-boundary mechanism, the |
| 624 | implementation can be straight like: |
| 625 | |
| 626 | - The SVC handler calls SPM internal routines, and eventually back to the |
| 627 | handler before an exit. |
| 628 | |
| 629 | Under the IPC model implementation, to re-use `ABI 2` in `No-cross-boundary`, |
| 630 | a software ABI needs to be provided. |
| 631 | |
| 632 | While under the SFN model plus isolation level 1, both `ABI 1` and `ABI 2` can |
| 633 | be a direct function call. |
| 634 | |
| 635 | NS Agent |
| 636 | ======== |
| 637 | The `NS Agent`(`NSA`) forwards NSPE service access request to SPM. It is a |
| 638 | special `partition` that: |
| 639 | |
| 640 | - It does not provide FF-M aligned secure services. |
| 641 | - It runs with the second-lowest priority under `IPC model implementation` |
| 642 | (The IDLE thread has the lowest priority). |
| 643 | - It has isolation boundaries and an individual stacks. |
| 644 | - It requires specific services and mechanisms compared to common partitions. |
| 645 | |
| 646 | There are two known types for NS Agent: |
| 647 | |
| 648 | - Trustzone-M based. |
| 649 | - Remote Procedure Call (RPC) based. |
| 650 | |
| 651 | This process is put inside the ARoT domain, to prevent assign unnecessary |
| 652 | PRoT permissions to the NSPE request parsing logic. |
| 653 | |
| 654 | Trustzone-M specific |
| 655 | -------------------- |
| 656 | The functionalities of a Truszone-M specific NSA is: |
| 657 | |
| 658 | - Launch NSPE when booting. |
| 659 | - Wait in the veneer code, and get executed when NSPE accesses services. |
| 660 | |
| 661 | As there may be multiple NSPE threads calling into SPE, and SPM wants to |
| 662 | identify them, special mechanisms can be proposed to provide the identification. |
| 663 | Check specific NS ID client ID or context related documents for details. |
| 664 | |
| 665 | .. figure:: media/tzcontext.* |
| 666 | :align: center |
| 667 | :name: fig-tzcontext |
| 668 | :width: 40% |
| 669 | |
| 670 | TZ NSA and specific service |
| 671 | |
| 672 | RPC specific |
| 673 | ------------ |
| 674 | Compare to Trustzone-M NSA, RPC NSA looks closer to a generic partition: |
| 675 | |
| 676 | - It has a message loop, keep waiting for RPC events. |
| 677 | - It converts received RPC events into FF-M API call to target services. |
| 678 | |
| 679 | And compared to generic partitions, the differences are: |
| 680 | |
| 681 | - It parses RPC messages to know which NSPE thread is accessing services. |
| 682 | Hence it needs special interfaces to help SPM to identify the NSPE clients. |
| 683 | - It needs to check NSPE client memory and map to local before calling SPM API. |
| 684 | - It cannot be blocked during API calls, which affects handling the RPC |
| 685 | requests. |
| 686 | |
| 687 | Partition |
| 688 | ========= |
| 689 | A partition is a set of services in the same scope. Services are generally |
| 690 | implemented as functions, and the partition exposes the services in different |
| 691 | ways bases on the partition model: `IPC` or `SFN`. |
| 692 | |
| 693 | A partition build generates these outputs: |
| 694 | |
| 695 | - A partition load information, used by SPM. |
| 696 | - A partition program containing service interface and logic, typically a |
| 697 | library. |
| 698 | - An optional service API set for easier client usage, by encapsulating |
| 699 | the low-level `FF-M` Client API. These API needs to be integrated |
| 700 | into client space. |
| 701 | |
| 702 | Partition loading |
| 703 | ----------------- |
| 704 | SPM needs to set up runtime objects to manage partitions by parsing the load |
| 705 | information of partitions. In general, the partition load information is |
| 706 | stored in a const memory are can be random read directly, hence SPM can direct |
| 707 | link runtime objects to the load information without a copy operation. This |
| 708 | is called a `Static Load` mechanism. |
| 709 | |
| 710 | Each partition has different numbers of dependencies and services, this makes |
| 711 | the load information size of each partition is different, it would be hard |
| 712 | to put such variable size elements in an array. The solution here is putting |
| 713 | these elements in a dedicated section, for SPM enumerating while loading. |
| 714 | Each partition can define variable size load information type bases on the |
| 715 | common load info type. |
| 716 | |
| 717 | The common load information: |
| 718 | |
| 719 | .. code-block:: c |
| 720 | |
| 721 | struct partition_load_info_t { |
| 722 | uint32_t psa_ff_ver; /* Encode the version with magic */ |
| 723 | int32_t pid; /* Partition ID */ |
| 724 | uint32_t flags; /* ARoT/PRoT, SFN/IPC, priority */ |
| 725 | uintptr_t entry; /* Entry point */ |
| 726 | size_t stack_size; /* Stack size */ |
| 727 | size_t heap_size; /* Heap size */ |
| 728 | uint32_t ndeps; /* Dependency number */ |
| 729 | uint32_t nservices; /* Service number */ |
| 730 | uint32_t nassets; /* Asset numbers */ |
| 731 | uint32_t nirqs; /* Number of IRQ owned by Partition */ |
| 732 | }; |
| 733 | |
| 734 | And the example for a specific partition load info: |
| 735 | struct partition_example_load_info_t { |
| 736 | struct partition_load_info_t ldi; /* Common info info */ |
| 737 | uint32_t deps[10]; /* Dependencies */ |
| 738 | /* ... other infos ... */ |
| 739 | }; |
| 740 | |
| 741 | Peripheral binding |
| 742 | ------------------ |
| 743 | A partition can declare multiple peripherals (Interrupts are part of |
| 744 | peripherals). The peripherals binding process: |
| 745 | |
| 746 | - The tooling references symbols in a fixed pattern in the partition load |
| 747 | information. |
| 748 | - The HAL implementation needs to provide the symbols being referenced. |
| 749 | - SPM calls HAL API to bind the partition info with devices When the partition |
| 750 | gets loading. |
| 751 | - The platform HAL acknowledges the binding if validation pass on SPM given |
| 752 | load information. |
| 753 | |
| 754 | *************************** |
| 755 | Integration and development |
| 756 | *************************** |
| 757 | These modules are expected to be object/library level modularised, each |
| 758 | module should be generated into object/library at build time: |
| 759 | |
| 760 | .. list-table:: Object level modularization |
| 761 | :header-rows: 1 |
| 762 | :widths: 40 60 |
| 763 | |
| 764 | * - Name |
| 765 | - Description |
| 766 | * - SPM |
| 767 | - All SPM related modules such as SPM, system, and so on. |
| 768 | * - Platform |
| 769 | - Platform sources are switchable. |
| 770 | * - Services and Secure Partition |
| 771 | - These items should be standalone. |
| 772 | * - Service Runtime Library |
| 773 | - This is a shared runtime library. |
| 774 | |
| 775 | HAL |
| 776 | === |
| 777 | The HAL here mainly refers to the SPM HAL. The SPM HAL implementation is |
| 778 | running with the same privilege level and hardware mode with SPM. The |
| 779 | implementation is object level modularized with SPM. |
| 780 | |
| 781 | Check the `HAL` design document for details. |
| 782 | |
| 783 | Configurations |
| 784 | ============== |
| 785 | The same TF-M code base is flexible to address different implementation |
| 786 | requirements, from the simplest device with isolation level 1 to the most |
| 787 | complicated device with isolation level 3 and optional isolation rules. |
| 788 | |
| 789 | These configurations are set by switches, during the build time, as runtime |
| 790 | support costs extra resources. The common configurations are named `profile`. |
| 791 | There are several profiles defined. |
| 792 | |
| 793 | ******* |
| 794 | History |
| 795 | ******* |
| 796 | |
| 797 | .. list-table:: Revision |
| 798 | :header-rows: 1 |
| 799 | :widths: 20 80 |
| 800 | |
| 801 | * - Date |
| 802 | - Description |
| 803 | * - 2021 Apr-Sep |
| 804 | - Updated to cover the implementation for `FF-M v1.1` features. |
| 805 | * - 2018 |
| 806 | - Created as 'TF-M Inter-Process Communication' which is deprecated as |
| 807 | this document covers whole SPM content. |
| 808 | |
| 809 | -------------- |
| 810 | |
| 811 | *Copyright (c) 2021, Arm Limited. All rights reserved.* |