1 /**************************************************************************//**
2  * @file     core_cmSecureAccess.h
3  * @brief    CMSIS Cortex-M Core Secure Access Header File
4  * @version  XXX
5  * @date     10. June 2016
6  *
7  * @note
8  *
9  ******************************************************************************/
10 /* Copyright (c) 2016 ARM LIMITED
11 
12    All rights reserved.
13    Redistribution and use in source and binary forms, with or without
14    modification, are permitted provided that the following conditions are met:
15    - Redistributions of source code must retain the above copyright
16      notice, this list of conditions and the following disclaimer.
17    - Redistributions in binary form must reproduce the above copyright
18      notice, this list of conditions and the following disclaimer in the
19      documentation and/or other materials provided with the distribution.
20    - Neither the name of ARM nor the names of its contributors may be used
21      to endorse or promote products derived from this software without
22      specific prior written permission.
23    *
24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27    ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34    POSSIBILITY OF SUCH DAMAGE.
35    ---------------------------------------------------------------------------*/
36 
37 
38 #ifndef __CORE_CM_SECURE_ACCESS_H
39 #define __CORE_CM_SECURE_ACCESS_H
40 
41 
42 /* ###########################  Core Secure Access  ########################### */
43 
44 #ifdef FEATURE_UVISOR
45 #include "uvisor-lib/uvisor-lib.h"
46 
47 /* Secure uVisor implementation. */
48 
49 /** Set the value at the target address.
50  *
51  * Equivalent to: `*address = value`.
52  * @param address[in]  Target address
53  * @param value[in]    Value to write at the address location.
54  */
55 #define SECURE_WRITE(address, value) \
56     uvisor_write(public_box, UVISOR_RGW_SHARED, address, value, UVISOR_RGW_OP_WRITE, 0xFFFFFFFFUL)
57 
58 /** Get the value at the target address.
59  *
60  * @param address[in]  Target address
61  * @returns The value `*address`.
62  */
63 #define SECURE_READ(address) \
64     uvisor_read(public_box, UVISOR_RGW_SHARED, address, UVISOR_RGW_OP_READ, 0xFFFFFFFFUL)
65 
66 /** Get the selected bits at the target address.
67  *
68  * @param address[in]  Target address
69  * @param mask[in]     Bits to select out of the target address
70  * @returns The value `*address & mask`.
71  */
72 #define SECURE_BITS_GET(address, mask) \
73     UVISOR_BITS_GET(public_box, UVISOR_RGW_SHARED, address, mask)
74 
75 /** Check the selected bits at the target address.
76  *
77  * @param address[in]  Address at which to check the bits
78  * @param mask[in]     Bits to select out of the target address
79  * @returns The value `((*address & mask) == mask)`.
80  */
81 #define SECURE_BITS_CHECK(address, mask) \
82     UVISOR_BITS_CHECK(public_box, UVISOR_RGW_SHARED, address, mask)
83 
84 /** Set the selected bits to 1 at the target address.
85  *
86  * Equivalent to: `*address |= mask`.
87  * @param address[in]  Target address
88  * @param mask[in]     Bits to select out of the target address
89  */
90 #define SECURE_BITS_SET(address, mask) \
91     UVISOR_BITS_SET(public_box, UVISOR_RGW_SHARED, address, mask)
92 
93 /** Clear the selected bits at the target address.
94  *
95  * Equivalent to: `*address &= ~mask`.
96  * @param address[in]  Target address
97  * @param mask[in]     Bits to select out of the target address
98  */
99 #define SECURE_BITS_CLEAR(address, mask) \
100     UVISOR_BITS_CLEAR(public_box, UVISOR_RGW_SHARED, address, mask)
101 
102 /** Set the selected bits at the target address to the given value.
103  *
104  * Equivalent to: `*address = (*address & ~mask) | (value & mask)`.
105  * @param address[in]  Target address
106  * @param mask[in]     Bits to select out of the target address
107  * @param value[in]    Value to write at the address location. Note: The value
108  *                     must be already shifted to the correct bit position
109  */
110 #define SECURE_BITS_SET_VALUE(address, mask, value) \
111     UVISOR_BITS_SET_VALUE(public_box, UVISOR_RGW_SHARED, address, mask, value)
112 
113 /** Toggle the selected bits at the target address.
114  *
115  * Equivalent to: `*address ^= mask`.
116  * @param address[in]  Target address
117  * @param mask[in]     Bits to select out of the target address
118  */
119 #define SECURE_BITS_TOGGLE(address, mask) \
120     UVISOR_BITS_TOGGLE(public_box, UVISOR_RGW_SHARED, address, mask)
121 
122 #else
123 
124 /* Insecure fallback implementation. */
125 
126 /** Set the value at the target address.
127  *
128  * Equivalent to: `*address = value`.
129  * @param address[in]  Target address
130  * @param value[in]    Value to write at the address location.
131  */
132 #define SECURE_WRITE(address, value) \
133     *(address) = (value)
134 
135 /** Get the value at the target address.
136  *
137  * @param address[in]  Target address
138  * @returns The value `*address`.
139  */
140 #define SECURE_READ(address) \
141     (*(address))
142 
143 /** Get the selected bits at the target address.
144  *
145  * @param address[in]  Target address
146  * @param mask[in]     Bits to select out of the target address
147  * @returns The value `*address & mask`.
148  */
149 #define SECURE_BITS_GET(address, mask) \
150     (*(address) & (mask))
151 
152 /** Check the selected bits at the target address.
153  *
154  * @param address[in]  Address at which to check the bits
155  * @param mask[in]     Bits to select out of the target address
156  * @returns The value `((*address & mask) == mask)`.
157  */
158 #define SECURE_BITS_CHECK(address, mask) \
159     ((*(address) & (mask)) == (mask))
160 
161 /** Set the selected bits to 1 at the target address.
162  *
163  * Equivalent to: `*address |= mask`.
164  * @param address[in]  Target address
165  * @param mask[in]     Bits to select out of the target address
166  */
167 #define SECURE_BITS_SET(address, mask) \
168     *(address) |= (mask)
169 
170 /** Clear the selected bits at the target address.
171  *
172  * Equivalent to: `*address &= ~mask`.
173  * @param address[in]  Target address
174  * @param mask[in]     Bits to select out of the target address
175  */
176 #define SECURE_BITS_CLEAR(address, mask) \
177     *(address) &= ~(mask)
178 
179 /** Set the selected bits at the target address to the given value.
180  *
181  * Equivalent to: `*address = (*address & ~mask) | (value & mask)`.
182  * @param address[in]  Target address
183  * @param mask[in]     Bits to select out of the target address
184  * @param value[in]    Value to write at the address location. Note: The value
185  *                     must be already shifted to the correct bit position
186  */
187 #define SECURE_BITS_SET_VALUE(address, mask, value) \
188     *(address) = (*(address) & ~(mask)) | ((value) & (mask))
189 
190 /** Toggle the selected bits at the target address.
191  *
192  * Equivalent to: `*address ^= mask`.
193  * @param address[in]  Target address
194  * @param mask[in]     Bits to select out of the target address
195  */
196 #define SECURE_BITS_TOGGLE(address, mask) \
197     *(address) ^= (mask)
198 
199 #endif
200 
201 #endif /* __CORE_CM_SECURE_ACCESS_H */
202