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 extern vm_map_address_t phystokv(pmap_paddr_t pa);
42 extern pmap_paddr_t kvtophys_nofail(vm_offset_t va);
43 
44 /* Global read-only data of TXM */
45 extern const TXMReadOnlyData_t *txm_ro_data;
46 
47 /* Code signing configuration of TXM */
48 extern const CSConfig_t *txm_cs_config;
49 
50 /* All statistical data collected from TXM */
51 extern const TXMStatistics_t *txm_stats;
52 
53 /* All static trust cache information collected from TXM */
54 extern uint32_t num_static_trust_caches;
55 extern TCCapabilities_t static_trust_cache_capabilities0;
56 extern TCCapabilities_t static_trust_cache_capabilities1;
57 
58 typedef struct _txm_thread_stack {
59 	/* Virtual mapping of the thread stack page */
60 	uintptr_t thread_stack_papt;
61 
62 	/* Physical page used for the thread stack */
63 	uintptr_t thread_stack_phys;
64 
65 	/* Pointer to the thread stack structure on the thread stack page */
66 	TXMThreadStack_t *thread_stack_data;
67 
68 	/* Linkage for the singly-linked-list */
69 	SLIST_ENTRY(_txm_thread_stack) link;
70 } txm_thread_stack_t;
71 
72 typedef struct _txm_call {
73 	/* Input arguments */
74 	TXMKernelSelector_t selector;
75 	TXMReturnCode_t failure_code_silent;
76 	bool failure_fatal;
77 	bool failure_silent;
78 	bool skip_logs;
79 	uint32_t num_input_args;
80 	uint32_t num_output_args;
81 
82 	/* Output arguments */
83 	TXMReturn_t txm_ret;
84 	uint64_t num_return_words;
85 	uint64_t return_words[kTXMStackReturnWords];
86 } txm_call_t;
87 
88 /**
89  * The main function to use for calling into the TrustedExecutionMonitor. This
90  * function handles all the bits required, including allocation/deallocation of
91  * the thread stack pages, the CPU instructions required to reach TXM, and also
92  * going through the TXM buffer and capturing any logs left by the monitor.
93  */
94 kern_return_t
95 txm_kernel_call(
96 	txm_call_t *parameters, ...);
97 
98 /**
99  * Go through the TrustedExecutionMonitor logging buffer and print all the logs
100  * which TXM has added to it since the kernel last looked.
101  */
102 void
103 txm_print_logs(void);
104 
105 /**
106  * Pages which need to be locked down by the TrustedExecutionMonitor need to made
107  * owned by TXM. This function can be used to go through each physical page in a
108  * range and transfer it to the relevant TXM type.
109  */
110 void
111 txm_transfer_region(
112 	vm_address_t addr,
113 	vm_size_t size);
114 
115 /**
116  * As part of transferring a page to the TrustedExecutionMonitor, the range of
117  * memory is always made read-only. This function can be used to go through all
118  * of the mappings and make them read-write again. This can only be done when TXM
119  * has transferred control of the pages back to the kernel.
120  */
121 void
122 txm_reclaim_region(
123 	vm_address_t addr,
124 	vm_size_t size);
125 
126 /**
127  * Register an address space with the TrustedExecutionMonitor based on an address
128  * space ID. This needs to be done AFTER the SPTM has made its call into TXM for
129  * registering an address space ID otherwise the system will panic.
130  */
131 kern_return_t
132 txm_register_address_space(
133 	pmap_t pmap,
134 	uint16_t addr_space_id,
135 	TXMAddressSpaceFlags_t flags);
136 
137 /**
138  * Unregister an address space from the TrustedExecutionMonitor using the address
139  * space object which was previously returned from TXM. This needs to be done
140  * AFTER the SPTM has unregistered the address space ID from TXM otherwise the
141  * system will panic.
142  */
143 kern_return_t
144 txm_unregister_address_space(
145 	pmap_t pmap);
146 
147 #endif /* CONFIG_SPTM */
148 #endif /* _SYS_CODE_SIGNING_TXM_H_ */
149