blob: f7740c292c669a5db6cfa1487ba4d8d46dcb9008 [file] [log] [blame]
David Hu50711e32019-06-12 18:32:30 +08001/*
Summer Qindea1f2c2021-01-11 14:46:34 +08002 * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
David Hu50711e32019-06-12 18:32:30 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7#ifndef __TFM_ARCH_H__
8#define __TFM_ARCH_H__
9
10/* This header file collects the architecture related operations. */
11
Ken Liu1d96c132019-12-31 15:51:30 +080012#include <stddef.h>
David Hu50711e32019-06-12 18:32:30 +080013#include <inttypes.h>
Kevin Pengbc5e5aa2019-10-16 10:55:17 +080014#include "tfm_hal_device_header.h"
David Hu50711e32019-06-12 18:32:30 +080015#include "cmsis_compiler.h"
16
Ronald Cron312be682019-09-23 09:27:33 +020017#if defined(__ARM_ARCH_8_1M_MAIN__) || \
18 defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
David Hu50711e32019-06-12 18:32:30 +080019#include "tfm_arch_v8m.h"
David Hu40455c92019-07-02 14:31:34 +080020#elif defined(__ARM_ARCH_6M__) || defined(__ARM_ARCH_7M__) || \
21 defined(__ARM_ARCH_7EM__)
22#include "tfm_arch_v6m_v7m.h"
David Hu50711e32019-06-12 18:32:30 +080023#else
24#error "Unsupported ARM Architecture."
25#endif
26
27#define XPSR_T32 0x01000000
28
Ken Liu5d73c872021-08-19 19:23:17 +080029/* State context defined by architecture */
Ken Liu5a2b9052019-08-15 19:03:29 +080030struct tfm_state_context_t {
David Hu50711e32019-06-12 18:32:30 +080031 uint32_t r0;
32 uint32_t r1;
33 uint32_t r2;
34 uint32_t r3;
35 uint32_t r12;
Ken Liu5a2b9052019-08-15 19:03:29 +080036 uint32_t lr;
David Hu50711e32019-06-12 18:32:30 +080037 uint32_t ra;
38 uint32_t xpsr;
Ken Liu5d73c872021-08-19 19:23:17 +080039};
David Hu50711e32019-06-12 18:32:30 +080040
Ken Liu5d73c872021-08-19 19:23:17 +080041/* Context addition to state context */
42struct tfm_additional_context_t {
43 uint32_t callee[8]; /* R4-R11. NOT ORDERED!! */
44};
45
46/* Full thread context */
47struct full_context_t {
48 struct tfm_additional_context_t addi_ctx;
49 struct tfm_state_context_t stat_ctx;
50};
51
52/* Context control */
53struct context_ctrl_t {
54 uint32_t sp; /* Stack pointer (higher address) */
55 uint32_t sp_limit; /* Stack limit (lower address) */
56 uint32_t reserved; /* Reserved */
57 uint32_t exc_ret; /* EXC_RETURN pattern. */
58};
59
60/*
61 * The context on MSP when de-privileged FLIH Function calls SVC to return.
62 * It is the same when de-privileged FLIH Function is ready to run.
63 */
64struct context_flih_ret_t {
65 uint64_t stack_seal; /* Two words stack seal */
66 struct tfm_additional_context_t addi_ctx;
67 uint32_t exc_ret; /* EXC_RETURN value when interrupt exception ocurrs */
68 uint32_t psp; /* PSP when interrupt exception ocurrs */
69 struct tfm_state_context_t state_ctx; /* ctx on SVC_PREPARE_DEPRIV_FLIH */
70};
David Hu50711e32019-06-12 18:32:30 +080071
72__attribute__ ((always_inline))
73__STATIC_INLINE void tfm_arch_trigger_pendsv(void)
74{
75 SCB->ICSR = SCB_ICSR_PENDSVSET_Msk;
76}
77
78/**
79 * \brief Get Link Register
80 * \details Returns the value of the Link Register (LR)
81 * \return LR value
82 */
TTornblomdd233d12020-11-05 11:44:28 +010083#if !defined ( __ICCARM__ )
David Hu50711e32019-06-12 18:32:30 +080084__attribute__ ((always_inline)) __STATIC_INLINE uint32_t __get_LR(void)
85{
86 register uint32_t result;
87
88 __ASM volatile ("MOV %0, LR\n" : "=r" (result));
89 return result;
90}
TTornblomdd233d12020-11-05 11:44:28 +010091#endif
David Hu50711e32019-06-12 18:32:30 +080092
93__attribute__ ((always_inline))
94__STATIC_INLINE uint32_t __get_active_exc_num(void)
95{
96 IPSR_Type IPSR;
97
98 /* if non-zero, exception is active. NOT banked S/NS */
99 IPSR.w = __get_IPSR();
100 return IPSR.b.ISR;
101}
102
103__attribute__ ((always_inline))
104__STATIC_INLINE void __set_CONTROL_SPSEL(uint32_t SPSEL)
105{
106 CONTROL_Type ctrl;
107
108 ctrl.w = __get_CONTROL();
109 ctrl.b.SPSEL = SPSEL;
110 __set_CONTROL(ctrl.w);
111 __ISB();
112}
113
Ken Liu5d73c872021-08-19 19:23:17 +0800114/* Set secure exceptions priority. */
Ken Liu50e21092020-10-14 16:42:15 +0800115void tfm_arch_set_secure_exception_priorities(void);
Jamie Fox3ede9712020-09-28 23:14:54 +0100116
Ken Liu5d73c872021-08-19 19:23:17 +0800117/* Configure various extensions. */
Summer Qindea1f2c2021-01-11 14:46:34 +0800118void tfm_arch_config_extensions(void);
Jamie Fox45587672020-08-17 18:31:14 +0100119
Ken Liu5d73c872021-08-19 19:23:17 +0800120/* Clear float point status. */
Ken Liuce2692d2020-02-11 12:39:36 +0800121void tfm_arch_clear_fp_status(void);
122
Kevin Peng300c68d2021-08-12 17:40:17 +0800123/*
124 * This function is called after SPM has initialized.
125 * It frees the stack used by SPM initialization and do Exception Return.
126 * It does not return.
127 */
128void tfm_arch_free_msp_and_exc_ret(uint32_t exc_return);
129
Ken Liu5d73c872021-08-19 19:23:17 +0800130/*
131 * This function sets return value on APIs that cause scheduling, for example
132 * psa_wait(), by manipulating the control context - this is usaully setting the
133 * R0 register of the thread context.
134 */
135void tfm_arch_set_context_ret_code(void *p_ctx_ctrl, uintptr_t ret_code);
136
137/* Init a thread context on thread stack and update the control context. */
138void tfm_arch_init_context(void *p_ctx_ctrl,
139 uintptr_t pfn, void *param, uintptr_t pfnlr,
140 uintptr_t sp_limit, uintptr_t sp);
141
142/*
143 * Refresh the HW (sp, splimit) according to the given control context and
144 * returns the EXC_RETURN payload (caller might need it for following codes).
145 *
146 * The p_ctx_ctrl must have been initialized by tfm_arch_init_context
147 */
148uint32_t tfm_arch_refresh_hardware_context(void *p_ctx_ctrl);
149
David Hu50711e32019-06-12 18:32:30 +0800150#endif