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
46static 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 */
53static kern_return_t
54exclaves_frame_mint_init(void)
55{
56 exclaves_id_t id = exclaves_service_lookup(EXCLAVES_DOMAIN_KERNEL,
57 EXCLAVES_FRAME_MINT);
58 if (id == UINT64_C(~0)) {
59 exclaves_debug_printf(show_errors,
60 "frame mint init: no frame mint service found \n");
61 return KERN_NOT_FOUND;
62 }
63
64 tb_endpoint_t ep = tb_endpoint_create_with_value(
65 TB_TRANSPORT_TYPE_XNU, id, TB_ENDPOINT_OPTIONS_NONE);
66
67 tb_error_t tb_result = framemint_framemint__init(&frame_mint_client, ep);
68
69 if (tb_result != TB_ERROR_SUCCESS) {
70 exclaves_debug_printf(show_errors,
71 "frame mint init: failure %u\n", tb_result);
72 return KERN_FAILURE;
73 }
74
75 return KERN_SUCCESS;
76}
77
78kern_return_t
79exclaves_frame_mint_populate(void)
80{
81 __block bool success = false;
82 tb_error_t tb_result = TB_ERROR_SUCCESS;
83
84 kern_return_t kr = exclaves_frame_mint_init();
85 if (kr != KERN_SUCCESS) {
86 return kr;
87 }
88
89 /* BEGIN IGNORE CODESTYLE */
90 tb_result = framemint_framemint_populate(&frame_mint_client,
91 ^(framemint_framemint_populate__result_s result) {
92 if (framemint_framemint_populate__result_get_success(&result)) {
93 success = true;
94 return;
95 }
96
97 framemint_frameminterror_s *error = NULL;
98 error = framemint_framemint_populate__result_get_failure(&result);
99
100 assert3p(error, !=, NULL);
101 exclaves_debug_printf(show_errors,
102 "frame mint failure: failure %u\n", *error);
103 });
104 /* END IGNORE CODESTYLE */
105
106 if (tb_result != TB_ERROR_SUCCESS || !success) {
107 return KERN_FAILURE;
108 }
109
110 return KERN_SUCCESS;
111}
112
113#endif /* CONFIG_EXCLAVES */
114