1 /* ARMv8-M Security Extensions routines.
2 Copyright (C) 2015-2022 Free Software Foundation, Inc.
3 Contributed by ARM Ltd.
5 This file is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
10 This file is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 Under Section 7 of GPL version 3, you are granted additional
16 permissions described in the GCC Runtime Library Exception, version
17 3.1, as published by the Free Software Foundation.
19 You should have received a copy of the GNU General Public License and
20 a copy of the GCC Runtime Library Exception along with this program;
21 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
22 <http://www.gnu.org/licenses/>. */
25 #if __ARM_FEATURE_CMSE & 1
29 /* ARM intrinsic function to perform a permission check on a given
30 address range. See ACLE changes for ARMv8-M. */
33 __attribute__ ((warn_unused_result
))
34 cmse_check_address_range (void *p
, size_t size
, int flags
)
36 cmse_address_info_t permb
, perme
;
37 char *pb
= (char *) p
, *pe
;
39 /* Check if the range wraps around. */
40 if (__UINTPTR_MAX__
- (__UINTPTR_TYPE__
) p
< size
)
43 /* Check if an unknown flag is present. */
44 int known
= CMSE_MPU_UNPRIV
| CMSE_MPU_READWRITE
| CMSE_MPU_READ
;
45 int known_secure_level
= CMSE_MPU_UNPRIV
;
46 #if __ARM_FEATURE_CMSE & 2
47 known
|= CMSE_AU_NONSECURE
| CMSE_MPU_NONSECURE
;
48 known_secure_level
|= CMSE_MPU_NONSECURE
;
53 /* Execute the right variant of the TT instructions. */
56 = (((__UINTPTR_TYPE__
) pb
^ (__UINTPTR_TYPE__
) pe
) < 32);
57 switch (flags
& known_secure_level
)
61 perme
= singleCheck
? permb
: cmse_TT (pe
);
64 permb
= cmse_TTT (pb
);
65 perme
= singleCheck
? permb
: cmse_TTT (pe
);
67 #if __ARM_FEATURE_CMSE & 2
68 case CMSE_MPU_NONSECURE
:
69 permb
= cmse_TTA (pb
);
70 perme
= singleCheck
? permb
: cmse_TTA (pe
);
72 case CMSE_MPU_UNPRIV
| CMSE_MPU_NONSECURE
:
73 permb
= cmse_TTAT (pb
);
74 perme
= singleCheck
? permb
: cmse_TTAT (pe
);
78 /* Invalid flag, eg. CMSE_MPU_NONSECURE specified but
79 __ARM_FEATURE_CMSE & 2 == 0. */
83 /* Check that the range does not cross MPU, SAU, or IDAU boundaries. */
84 if (permb
.value
!= perme
.value
)
87 /* Check the permissions on the range. */
88 switch (flags
& (~known_secure_level
))
90 #if __ARM_FEATURE_CMSE & 2
91 case CMSE_MPU_READ
| CMSE_MPU_READWRITE
| CMSE_AU_NONSECURE
:
92 case CMSE_MPU_READWRITE
| CMSE_AU_NONSECURE
:
93 return permb
.flags
.nonsecure_readwrite_ok
? p
: NULL
;
94 case CMSE_MPU_READ
| CMSE_AU_NONSECURE
:
95 return permb
.flags
.nonsecure_read_ok
? p
: NULL
;
96 case CMSE_AU_NONSECURE
:
97 return permb
.flags
.secure
? NULL
: p
;
99 case CMSE_MPU_READ
| CMSE_MPU_READWRITE
:
100 case CMSE_MPU_READWRITE
:
101 return permb
.flags
.readwrite_ok
? p
: NULL
;
103 return permb
.flags
.read_ok
? p
: NULL
;
110 #endif /* __ARM_FEATURE_CMSE & 1. */