xref: /xnu-12377.1.9/osfmk/arm64/sptm/sptm.h (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
1 /*
2  * Copyright (c) 2022 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 #pragma once
29 
30 /**
31  * This file is meant to be the main header that XNU uses to get access to all
32  * of the exported SPTM types, declarations, and function prototypes. Wrappers
33  * around some of the SPTM library functions are also located in here.
34  */
35 #include <sptm/debug_header.h>
36 #include <sptm/sptm_xnu.h>
37 #include <kern/debug.h>
38 
39 #include <stdbool.h>
40 
41 /* Bootstrapping arguments passed from the SPTM to XNU. */
42 extern const sptm_bootstrap_args_xnu_t *SPTMArgs;
43 
44 typedef struct arm_physrange {
45 	uint64_t        start_phys;     /* Starting physical address */
46 	uint64_t        end_phys;       /* Ending physical address (EXCLUSIVE) */
47 } arm_physrange_t;
48 
49 /**
50  * Convenience function for checking whether an SPTM operation on the given page
51  * is in-flight.
52  *
53  * @note This is just a wrapper around the SPTM library.
54  *
55  * @param paddr The physical address of the managed page against which to check
56  *              for in-flight operations.
57  *
58  * @return True if an operation is in-flight, false otherwise.
59  */
60 static inline bool
sptm_paddr_is_inflight(sptm_paddr_t paddr)61 sptm_paddr_is_inflight(sptm_paddr_t paddr)
62 {
63 	bool is_inflight = false;
64 	if (sptm_check_inflight(paddr, &is_inflight) != LIBSPTM_SUCCESS) {
65 		panic("%s: sptm_check_inflight returned failure for paddr 0x%llx",
66 		    __func__, (uint64_t)paddr);
67 	}
68 
69 	return is_inflight;
70 }
71 
72 /**
73  * Convenience function for determining the SPTM frame type for a given
74  * SPTM-managed page.
75  *
76  * @note This is just a wrapper around the SPTM library.
77  *
78  * @param paddr The physical address of the managed page to get the type of.
79  *
80  * @return The SPTM type for the given frame. If the page passed in is not an
81  *         SPTM-managed page, then a panic will get triggered.
82  */
83 static inline sptm_frame_type_t
sptm_get_frame_type(sptm_paddr_t paddr)84 sptm_get_frame_type(sptm_paddr_t paddr)
85 {
86 	sptm_frame_type_t frame_type;
87 	if (sptm_get_paddr_type(paddr, &frame_type) != LIBSPTM_SUCCESS) {
88 		panic("%s: sptm_get_paddr_type returned failure for paddr 0x%llx",
89 		    __func__, (uint64_t)paddr);
90 	}
91 
92 	return frame_type;
93 }
94 
95 /**
96  * Convenience function for checking if a given SPTM-managed
97  * page has any mappings.
98  *
99  * @note This is just a wrapper around the SPTM library.
100  *
101  * @param paddr The physical address of the managed page to query.
102  *
103  */
104 static inline bool
sptm_frame_is_last_mapping(sptm_paddr_t paddr,libsptm_refcnt_type_t refcnt_type)105 sptm_frame_is_last_mapping(sptm_paddr_t paddr, libsptm_refcnt_type_t refcnt_type)
106 {
107 	bool is_last;
108 	if (sptm_paddr_is_last_mapping(paddr, refcnt_type, &is_last) != LIBSPTM_SUCCESS) {
109 		panic("%s: sptm_paddr_is_last_mapping returned failure for paddr 0x%llx",
110 		    __func__, (uint64_t)paddr);
111 	}
112 
113 	return is_last;
114 }
115 
116 /**
117  * Convenience function for retrieving the SPTM page table mapping reference
118  * count.
119  *
120  * @note This is just a wrapper around the SPTM library.
121  *
122  * @param table_paddr The physical address of the page table page for which to
123  *                    obtain the mapping reference count.
124  *
125  * @return The SPTM mapping reference count for the page table page.  If the page
126  *         passed in is not an SPTM-managed page table page, then a panic will be
127  *         triggered.
128  */
129 static inline uint16_t
sptm_get_page_table_refcnt(sptm_paddr_t table_paddr)130 sptm_get_page_table_refcnt(sptm_paddr_t table_paddr)
131 {
132 	uint16_t refcnt;
133 	if (sptm_get_table_mapping_count(table_paddr, &refcnt) != LIBSPTM_SUCCESS) {
134 		panic("%s: sptm_get_table_mapping_count returned failure for paddr 0x%llx",
135 		    __func__, (uint64_t)table_paddr);
136 	}
137 
138 	return refcnt;
139 }
140 
141 /**
142  * Convenience function for determining whether a frame type allows userspace
143  * executable mapping permissions.
144  *
145  * @param frame_type the frame type to query
146  *
147  * @return True If [frame_type] allows userspace mappings with executable
148  *         privileges, false otherwise.
149  */
150 static inline bool
sptm_type_is_user_executable(sptm_frame_type_t frame_type)151 sptm_type_is_user_executable(sptm_frame_type_t frame_type)
152 {
153 	return (frame_type == XNU_USER_EXEC) || (frame_type == XNU_USER_DEBUG) || (frame_type == XNU_USER_JIT);
154 }
155