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 #pragma mark Trust Evaluations 99 #undef IMAGE4_TRUST_EVALUATION_EXEC 100 #define IMAGE4_TRUST_EVALUATION_EXEC \ 101 image4_xnu_const(trust_evaluation_exec, 0) 102 103 #undef IMAGE4_TRUST_EVALUATION_PREFLIGHT 104 #define IMAGE4_TRUST_EVALUATION_PREFLIGHT \ 105 image4_xnu_const(trust_evaluation_preflight, 0) 106 107 #undef IMAGE4_TRUST_EVALUATION_SIGN 108 #define IMAGE4_TRUST_EVALUATION_SIGN \ 109 image4_xnu_const(trust_evaluation_sign, 0) 110 111 #undef IMAGE4_TRUST_EVALUATION_BOOT 112 #define IMAGE4_TRUST_EVALUATION_BOOT \ 113 image4_xnu_const(trust_evaluation_boot, 0) 114 115 #pragma mark Environment 116 #define _image4_environment_init(...) \ 117 image4_xnu_callable_ptr(environment_init, 0, ## __VA_ARGS__) 118 #define image4_environment_new(...) \ 119 image4_xnu_callable_ptr(environment_new, 0, ## __VA_ARGS__) 120 #define image4_environment_set_secure_boot(...) \ 121 image4_xnu_callable_void(environment_set_secure_boot, 0, ## __VA_ARGS__) 122 #define image4_environment_set_callbacks(...) \ 123 image4_xnu_callable_void(environment_set_callbacks, 0, ## __VA_ARGS__) 124 #define image4_environment_copy_nonce_digest(...) \ 125 image4_xnu_callable_posix(environment_copy_nonce_digest, 0, ## __VA_ARGS__) 126 #define image4_environment_roll_nonce(...) \ 127 image4_xnu_callable_posix(environment_roll_nonce, 0, ## __VA_ARGS__) 128 #define image4_environment_generate_nonce_proposal(...) \ 129 image4_xnu_callable_posix(environment_generate_nonce_proposal, \ 130 0, ## __VA_ARGS__) 131 #define image4_environment_commit_nonce_proposal(...) \ 132 image4_xnu_callable_posix(environment_commit_nonce_proposal, \ 133 0, ## __VA_ARGS__) 134 #define image4_environment_get_nonce_handle(...) \ 135 image4_xnu_callable_posix(environment_get_nonce_handle, 0, ## __VA_ARGS__) 136 #define image4_environment_destroy(...) \ 137 image4_xnu_callable_void(environment_destroy, 0, ## __VA_ARGS__) 138 139 #pragma mark Trust 140 #define _image4_trust_init(...) \ 141 image4_xnu_callable_ptr(trust_init, 0, ## __VA_ARGS__) 142 #define image4_trust_new(...) \ 143 image4_xnu_callable_ptr(trust_new, 0, ## __VA_ARGS__) 144 #define image4_trust_set_payload(...) \ 145 image4_xnu_callable_void(trust_set_payload, 0, ## __VA_ARGS__) 146 #define image4_trust_set_booter(...) \ 147 image4_xnu_callable_void(trust_set_booter, 0, ## __VA_ARGS__) 148 #define image4_trust_record_property_bool(...) \ 149 image4_xnu_callable_void(trust_record_property_bool, 0, ## __VA_ARGS__) 150 #define image4_trust_record_property_integer(...) \ 151 image4_xnu_callable_void(trust_record_property_integer, 0, ## __VA_ARGS__) 152 #define image4_trust_record_property_data(...) \ 153 image4_xnu_callable_void(trust_record_property_data, 0, ## __VA_ARGS__) 154 #define image4_trust_evaluate(...) \ 155 image4_xnu_callable_void(trust_evaluate, 0, ## __VA_ARGS__) 156 #define image4_trust_destroy(...) \ 157 image4_xnu_callable_void(trust_destroy, 0, ## __VA_ARGS__) 158 159 #pragma mark Kernel-Specific 160 #define image4_cs_trap_resolve_handler(...) \ 161 image4_xnu_callable_ptr(cs_trap_resolve_handler, 0, ## __VA_ARGS__) 162 #define image4_cs_trap_vector_size(...) \ 163 image4_xnu_callable(cs_trap_vector_size, 0, -1, ## __VA_ARGS__) 164 165 #endif // __IMAGE4_DLXK_API_H 166