1 /* 2 * Copyright © 2017-2024 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 /*! 29 * @header 30 * Trampolines for the APIs which are useable by the kernel-proper. These take 31 * the form of macros which override the symbol names and redirect them to the 32 * interface structure that gets registered at runtime in order to preserve most 33 * source compatibility. 34 * 35 * The exception to this is when referencing API constants. Because API constant 36 * macros get indirected through an expression that cannot be evaluated at 37 * compile-time, you cannot statically initialize a variable to e.g. 38 * IMAGE4_COPROCESSOR_HOST in xnu. Nor can you statically initialize pointers to 39 * API functions. 40 * 41 * This header relies entirely on transitive inclusion from dlxk.h to satisfy 42 * its dependencies. 43 */ 44 #ifndef __IMAGE4_DLXK_API_H 45 #define __IMAGE4_DLXK_API_H 46 47 #if !defined(__IMAGE4_XNU_INDIRECT) 48 #error "Please include <libkern/image4/dlxk.h> instead of this file" 49 #endif 50 51 52 #pragma mark Macros 53 #define image4_xnu_callable(_f, _v, _rv, ...) ({ \ 54 const image4_dlxk_interface_t *dlxk = NULL; \ 55 dlxk = image4_dlxk_get(_v); \ 56 dlxk ? (dlxk->dlxk_ ## _f(__VA_ARGS__)) : (_rv); \ 57 }) 58 59 #define image4_xnu_callable_ptr(_f, _v, ...) \ 60 image4_xnu_callable(_f, _v, NULL, ## __VA_ARGS__) 61 62 #define image4_xnu_callable_posix(_f, _v, ...) \ 63 image4_xnu_callable(_f, _v, ENOSYS, ## __VA_ARGS__) 64 65 #define image4_xnu_callable_void(_f, _v, ...) ({ \ 66 const image4_dlxk_interface_t *dlxk = NULL; \ 67 dlxk = image4_dlxk_get(_v); \ 68 if (dlxk) { \ 69 dlxk->dlxk_ ## _f(__VA_ARGS__); \ 70 } \ 71 }) 72 73 #define image4_xnu_const(_s, _v) ({ \ 74 const image4_dlxk_interface_t *dlxk = NULL; \ 75 dlxk = image4_dlxk_get(_v); \ 76 dlxk ? (dlxk->dlxk_ ## _s) : NULL; \ 77 }) 78 79 #pragma mark Coprocessors 80 #undef IMAGE4_COPROCESSOR_HOST 81 #define IMAGE4_COPROCESSOR_HOST image4_xnu_const(coprocessor_host, 0) 82 83 #undef IMAGE4_COPROCESSOR_AP 84 #define IMAGE4_COPROCESSOR_AP image4_xnu_const(coprocessor_ap, 0) 85 86 #undef IMAGE4_COPROCESSOR_AP_LOCAL 87 #define IMAGE4_COPROCESSOR_AP_LOCAL image4_xnu_const(coprocessor_ap_local, 0) 88 89 #undef IMAGE4_COPROCESSOR_CRYPTEX1 90 #define IMAGE4_COPROCESSOR_CRYPTEX1 image4_xnu_const(coprocessor_cryptex1, 0) 91 92 #undef IMAGE4_COPROCESSOR_SEP 93 #define IMAGE4_COPROCESSOR_SEP image4_xnu_const(coprocessor_sep, 0) 94 95 #undef IMAGE4_COPROCESSOR_X86 96 #define IMAGE4_COPROCESSOR_X86 image4_xnu_const(coprocessor_x86, 0) 97 98 #undef IMAGE4_COPROCESSOR_BOOTPC 99 #define IMAGE4_COPROCESSOR_BOOTPC image4_xnu_const(coprocessor_bootpc, 1) 100 101 #undef IMAGE4_COPROCESSOR_VMA2 102 #define IMAGE4_COPROCESSOR_VMA2 image4_xnu_const(coprocessor_vma2, 2) 103 104 #undef IMAGE4_COPROCESSOR_VMA3 105 #define IMAGE4_COPROCESSOR_VMA3 image4_xnu_const(coprocessor_vma3, 2) 106 107 #define image4_coprocessor_resolve_from_manifest(...) \ 108 image4_xnu_callable_posix(coprocessor_resolve_from_manifest, \ 109 1, ## __VA_ARGS__) 110 111 #pragma mark Trust Evaluations 112 #undef IMAGE4_TRUST_EVALUATION_EXEC 113 #define IMAGE4_TRUST_EVALUATION_EXEC \ 114 image4_xnu_const(trust_evaluation_exec, 0) 115 116 #undef IMAGE4_TRUST_EVALUATION_PREFLIGHT 117 #define IMAGE4_TRUST_EVALUATION_PREFLIGHT \ 118 image4_xnu_const(trust_evaluation_preflight, 0) 119 120 #undef IMAGE4_TRUST_EVALUATION_SIGN 121 #define IMAGE4_TRUST_EVALUATION_SIGN \ 122 image4_xnu_const(trust_evaluation_sign, 0) 123 124 #undef IMAGE4_TRUST_EVALUATION_BOOT 125 #define IMAGE4_TRUST_EVALUATION_BOOT \ 126 image4_xnu_const(trust_evaluation_boot, 0) 127 128 #undef IMAGE4_TRUST_EVALUATION_NORMALIZE 129 #define IMAGE4_TRUST_EVALUATION_NORMALIZE \ 130 image4_xnu_const(trust_evaluation_boot, 1) 131 132 #pragma mark Environment 133 #define _image4_environment_init(...) \ 134 image4_xnu_callable_ptr(environment_init, 0, ## __VA_ARGS__) 135 #define image4_environment_new(...) \ 136 image4_xnu_callable_ptr(environment_new, 0, ## __VA_ARGS__) 137 #define image4_environment_set_secure_boot(...) \ 138 image4_xnu_callable_void(environment_set_secure_boot, 0, ## __VA_ARGS__) 139 #define image4_environment_set_callbacks(...) \ 140 image4_xnu_callable_void(environment_set_callbacks, 0, ## __VA_ARGS__) 141 #define image4_environment_identify(...) \ 142 image4_xnu_callable_posix(environment_identify, 1, ## __VA_ARGS__) 143 #define image4_environment_get_digest_info(...) \ 144 image4_xnu_callable_posix(environment_get_digest_info, 1, ## __VA_ARGS__) 145 #define image4_environment_copy_nonce_digest(...) \ 146 image4_xnu_callable_posix(environment_copy_nonce_digest, 0, ## __VA_ARGS__) 147 #define image4_environment_roll_nonce(...) \ 148 image4_xnu_callable_posix(environment_roll_nonce, 0, ## __VA_ARGS__) 149 #define image4_environment_generate_nonce_proposal(...) \ 150 image4_xnu_callable_posix(environment_generate_nonce_proposal, \ 151 0, ## __VA_ARGS__) 152 #define image4_environment_commit_nonce_proposal(...) \ 153 image4_xnu_callable_posix(environment_commit_nonce_proposal, \ 154 0, ## __VA_ARGS__) 155 #define image4_environment_get_nonce_handle(...) \ 156 image4_xnu_callable_posix(environment_get_nonce_handle, 0, ## __VA_ARGS__) 157 #define image4_environment_flash(...) \ 158 image4_xnu_callable_posix(environment_flash, 1, ## __VA_ARGS__) 159 #define image4_environment_destroy(...) \ 160 image4_xnu_callable_void(environment_destroy, 0, ## __VA_ARGS__) 161 162 #pragma mark Trust 163 #define _image4_trust_init(...) \ 164 image4_xnu_callable_ptr(trust_init, 0, ## __VA_ARGS__) 165 #define image4_trust_new(...) \ 166 image4_xnu_callable_ptr(trust_new, 0, ## __VA_ARGS__) 167 #define image4_trust_set_payload(...) \ 168 image4_xnu_callable_void(trust_set_payload, 0, ## __VA_ARGS__) 169 #define image4_trust_set_booter(...) \ 170 image4_xnu_callable_void(trust_set_booter, 0, ## __VA_ARGS__) 171 #define image4_trust_set_result_buffer(...) \ 172 image4_xnu_callable_void(trust_set_result_buffer, 2, ## __VA_ARGS__) 173 #define image4_trust_record_property_bool(...) \ 174 image4_xnu_callable_void(trust_record_property_bool, 0, ## __VA_ARGS__) 175 #define image4_trust_record_property_integer(...) \ 176 image4_xnu_callable_void(trust_record_property_integer, 0, ## __VA_ARGS__) 177 #define image4_trust_record_property_data(...) \ 178 image4_xnu_callable_void(trust_record_property_data, 0, ## __VA_ARGS__) 179 #define image4_trust_evaluate(...) \ 180 image4_xnu_callable_void(trust_evaluate, 0, ## __VA_ARGS__) 181 #define image4_trust_destroy(...) \ 182 image4_xnu_callable_void(trust_destroy, 0, ## __VA_ARGS__) 183 184 #pragma mark Kernel-Specific 185 #define image4_cs_trap_resolve_handler(...) \ 186 image4_xnu_callable_ptr(cs_trap_resolve_handler, 0, ## __VA_ARGS__) 187 #define image4_cs_trap_vector_size(...) \ 188 image4_xnu_callable(cs_trap_vector_size, 0, -1, ## __VA_ARGS__) 189 190 #endif // __IMAGE4_DLXK_API_H 191