xref: /xnu-11215.81.4/osfmk/kern/exclaves_frame_mint.c (revision d4514f0bc1d3f944c22d92e68b646ac3fb40d452)
1 /*
2  * Copyright (c) 2023 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 #if CONFIG_EXCLAVES
30 
31 #include <stdint.h>
32 #include <mach/exclaves.h>
33 #include <mach/kern_return.h>
34 
35 #include "kern/exclaves.tightbeam.h"
36 
37 #include "exclaves_frame_mint.h"
38 #include "exclaves_resource.h"
39 #include "exclaves_debug.h"
40 
41 /* -------------------------------------------------------------------------- */
42 #pragma mark Frame Mint
43 
44 #define EXCLAVES_FRAME_MINT "com.apple.service.FrameMint"
45 
46 static framemint_framemint_s frame_mint_client;
47 
48 /*
49  * Called as part of the populate call. As we can't cleanup tightbeam
50  * connections it just sticks around. If we ever need to make any other calls to
51  * FrameMint, having it a separate function makes that easier.
52  */
53 static kern_return_t
exclaves_frame_mint_init(void)54 exclaves_frame_mint_init(void)
55 {
56 	exclaves_id_t id = exclaves_service_lookup(EXCLAVES_DOMAIN_KERNEL,
57 	    EXCLAVES_FRAME_MINT);
58 	if (id == EXCLAVES_INVALID_ID) {
59 		return KERN_NOT_FOUND;
60 	}
61 
62 	tb_endpoint_t ep = tb_endpoint_create_with_value(
63 		TB_TRANSPORT_TYPE_XNU, id, TB_ENDPOINT_OPTIONS_NONE);
64 
65 	tb_error_t tb_result = framemint_framemint__init(&frame_mint_client, ep);
66 
67 	if (tb_result != TB_ERROR_SUCCESS) {
68 		exclaves_debug_printf(show_errors,
69 		    "frame mint init: failure %u\n", tb_result);
70 		return KERN_FAILURE;
71 	}
72 
73 	return KERN_SUCCESS;
74 }
75 
76 kern_return_t
exclaves_frame_mint_populate(void)77 exclaves_frame_mint_populate(void)
78 {
79 	__block bool success = false;
80 	tb_error_t tb_result = TB_ERROR_SUCCESS;
81 
82 	kern_return_t kr = exclaves_frame_mint_init();
83 	if (kr != KERN_SUCCESS) {
84 		return kr;
85 	}
86 
87 	/* BEGIN IGNORE CODESTYLE */
88 	tb_result = framemint_framemint_populate(&frame_mint_client,
89 	    ^(framemint_framemint_populate__result_s result) {
90 		if (framemint_framemint_populate__result_get_success(&result)) {
91 			success = true;
92 			return;
93 		}
94 
95 		framemint_frameminterror_s *error = NULL;
96 		error = framemint_framemint_populate__result_get_failure(&result);
97 
98 		assert3p(error, !=, NULL);
99 		exclaves_debug_printf(show_errors,
100 		    "frame mint failure: failure %u\n", *error);
101 	});
102 	/* END IGNORE CODESTYLE */
103 
104 	if (tb_result != TB_ERROR_SUCCESS || !success) {
105 		return KERN_FAILURE;
106 	}
107 
108 	return KERN_SUCCESS;
109 }
110 
111 #endif /* CONFIG_EXCLAVES */
112