1 /* 2 * Copyright (c) 2022 Apple Computer, Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * The contents of this file constitute Original Code as defined in and 7 * are subject to the Apple Public Source License Version 1.1 (the 8 * "License"). You may not use this file except in compliance with the 9 * License. Please obtain a copy of the License at 10 * http://www.apple.com/publicsource and read it before using this file. 11 * 12 * This Original Code and all software distributed under the License are 13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER 14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the 17 * License for the specific language governing rights and limitations 18 * under the License. 19 * 20 * @APPLE_LICENSE_HEADER_END@ 21 */ 22 23 #ifndef _SYS_CODE_SIGNING_TXM_H_ 24 #define _SYS_CODE_SIGNING_TXM_H_ 25 26 #if CONFIG_SPTM 27 28 #include <libkern/section_keywords.h> 29 #include <kern/locks.h> 30 #include <kern/lock_rw.h> 31 #include <vm/pmap.h> 32 #include <sys/queue.h> 33 #include <TrustedExecutionMonitor/API.h> 34 35 #ifndef kTXMImage4APIVersion 36 #define kTXMImage4APIVersion 0 37 #endif 38 39 /* These are hidden behind MACH_KERNEL_PRIVATE in other files */ 40 typedef uint64_t pmap_paddr_t __kernel_ptr_semantics; 41 pmap_paddr_t kvtophys_nofail(vm_offset_t va); 42 43 /* 44 * The runtime lock used to enforce concurrency on all trust cache operations 45 * within the kernel for TXM. This is needed because TXM only enforces concurrency 46 * through try-locks, which means the kernel also needs to enforce concurrency 47 * on its side in order to ensure the try-locks within TXM never fail. 48 */ 49 extern decl_lck_rw_data(, txm_trust_cache_lck); 50 51 /* Global read-only data of TXM */ 52 extern const TXMReadOnlyData_t *txm_ro_data; 53 54 /* Code signing configuration of TXM */ 55 extern const CSConfig_t *txm_cs_config; 56 57 /* All statistical data collected from TXM */ 58 extern const TXMStatistics_t *txm_stats; 59 60 /* All static trust cache information collected from TXM */ 61 extern uint32_t num_static_trust_caches; 62 extern TCCapabilities_t static_trust_cache_capabilities0; 63 extern TCCapabilities_t static_trust_cache_capabilities1; 64 65 typedef struct _txm_thread_stack { 66 /* Virtual mapping of the thread stack page */ 67 uintptr_t thread_stack_papt; 68 69 /* Physical page used for the thread stack */ 70 uintptr_t thread_stack_phys; 71 72 /* Pointer to the thread stack structure on the thread stack page */ 73 TXMThreadStack_t *thread_stack_data; 74 75 /* Linkage for the singly-linked-list */ 76 SLIST_ENTRY(_txm_thread_stack) link; 77 } txm_thread_stack_t; 78 79 typedef struct _txm_call { 80 /* Input arguments */ 81 TXMKernelSelector_t selector; 82 TXMReturnCode_t failure_code_silent; 83 bool failure_fatal; 84 bool failure_silent; 85 bool skip_logs; 86 uint32_t num_input_args; 87 uint32_t num_output_args; 88 89 /* Output arguments */ 90 TXMReturn_t txm_ret; 91 uint64_t num_return_words; 92 uint64_t return_words[kTXMStackReturnWords]; 93 } txm_call_t; 94 95 /** 96 * The main function to use for calling into the TrustedExecutionMonitor. This 97 * function handles all the bits required, including allocation/deallocation of 98 * the thread stack pages, the CPU instructions required to reach TXM, and also 99 * going through the TXM buffer and capturing any logs left by the monitor. 100 */ 101 kern_return_t 102 txm_kernel_call( 103 txm_call_t *parameters, ...); 104 105 /** 106 * Go through the TrustedExecutionMonitor logging buffer and print all the logs 107 * which TXM has added to it since the kernel last looked. 108 */ 109 void 110 txm_print_logs(void); 111 112 /** 113 * Pages which need to be locked down by the TrustedExecutionMonitor need to made 114 * owned by TXM. This function can be used to go through each physical page in a 115 * range and transfer it to the relevant TXM type. 116 */ 117 void 118 txm_transfer_region( 119 vm_address_t addr, 120 vm_size_t size); 121 122 /** 123 * As part of transferring a page to the TrustedExecutionMonitor, the range of 124 * memory is always made read-only. This function can be used to go through all 125 * of the mappings and make them read-write again. This can only be done when TXM 126 * has transferred control of the pages back to the kernel. 127 */ 128 void 129 txm_reclaim_region( 130 vm_address_t addr, 131 vm_size_t size); 132 133 /** 134 * Register an address space with the TrustedExecutionMonitor based on an address 135 * space ID. This needs to be done AFTER the SPTM has made its call into TXM for 136 * registering an address space ID otherwise the system will panic. 137 */ 138 kern_return_t 139 txm_register_address_space( 140 pmap_t pmap, 141 uint16_t addr_space_id, 142 TXMAddressSpaceFlags_t flags); 143 144 /** 145 * Unregister an address space from the TrustedExecutionMonitor using the address 146 * space object which was previously returned from TXM. This needs to be done 147 * AFTER the SPTM has unregistered the address space ID from TXM otherwise the 148 * system will panic. 149 */ 150 kern_return_t 151 txm_unregister_address_space( 152 pmap_t pmap); 153 154 #endif /* CONFIG_SPTM */ 155 #endif /* _SYS_CODE_SIGNING_TXM_H_ */ 156