Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
/*
 * Copyright (c) 2024 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */

#if CONFIG_EXCLAVES

#include <stdint.h>
#include <mach/kern_return.h>
#include <kern/assert.h>
#include <kern/misc_protos.h>
#include <kern/locks.h>
#include <kern/thread.h>
#include <vm/pmap.h>
#include <mach/exclaves_l4.h>

#include "exclaves_debug.h"
#include "exclaves_xnuproxy.h"
#include "exclaves_resource.h"
#include "exclaves_upcalls.h"
#include "exclaves_internal.h"

#include "kern/exclaves.tightbeam.h"

#include <xnuproxy/messages.h>

/* -------------------------------------------------------------------------- */
#pragma mark IPC bootstrap

/* Lock protecting the use of the bootstrap scheduling context */
static LCK_MTX_DECLARE(exclaves_xnuproxy_lock, &exclaves_lck_grp);

/*
 * Bootstrap context. Used for context allocate/free. Initialized in
 * exclaves_xnuproxy_init().
 */
static exclaves_ctx_t exclaves_bootstrap_ctx = {};

/*
 * Switch the current thread to use the bootstrap context. Stash the old context
 * into the supplied arguments.
 * Returns with exclaves_xnuproxy_lock held.
 */
static void
exclaves_bootstrap_context_acquire(exclaves_ctx_t *save_ctx)
{
	assert3p(exclaves_bootstrap_ctx.ipcb, !=, NULL);
	assert3u(save_ctx->scid, !=, exclaves_bootstrap_ctx.scid);

	lck_mtx_lock(&exclaves_xnuproxy_lock);

	thread_t thread = current_thread();

	*save_ctx = thread->th_exclaves_ipc_ctx;

	thread->th_exclaves_ipc_ctx = exclaves_bootstrap_ctx;

	LCK_MTX_ASSERT(&exclaves_xnuproxy_lock, LCK_MTX_ASSERT_OWNED);
}

/*
 * Restore the scheduling context of the current thread.
 * Returns with exclaves_xnuproxy_lock released.
 */
static void
exclaves_bootstrap_context_release(const exclaves_ctx_t *restore_ctx)
{
	assert3u(restore_ctx->scid, !=, exclaves_bootstrap_ctx.scid);

	LCK_MTX_ASSERT(&exclaves_xnuproxy_lock, LCK_MTX_ASSERT_OWNED);

	thread_t thread = current_thread();
	assert3p(thread->th_exclaves_ipc_ctx.ipcb, ==, exclaves_bootstrap_ctx.ipcb);

	/* Reset */
	thread->th_exclaves_ipc_ctx = *restore_ctx;

	lck_mtx_unlock(&exclaves_xnuproxy_lock);
}

/* -------------------------------------------------------------------------- */
#pragma mark IPC buffer count

/*
 * Number of allocated ipcb buffers. Estimates the number of active exclave
 * threads.
 */
static _Atomic size_t exclaves_ipcb_cnt;

size_t
exclaves_ipc_buffer_count(void)
{
	return os_atomic_load(&exclaves_ipcb_cnt, relaxed);
}

static void
exclaves_ipc_buffer_count_inc(void)
{
	os_atomic_inc(&exclaves_ipcb_cnt, relaxed);
}

static void
exclaves_ipc_buffer_count_dec(void)
{
	__assert_only size_t orig_ipcb_cnt =
	    os_atomic_dec_orig(&exclaves_ipcb_cnt, relaxed);
	assert3u(orig_ipcb_cnt, >=, 1);
}

/* -------------------------------------------------------------------------- */
#pragma mark IPC buffer cache

/*
 * A (simple, for now...) cache of IPC buffers for communicating with XNU-Proxy.
 * The cache itself is realtime safe and relies on a spin lock for
 * synchronization. However, if there's no cached buffer available, the calling
 * code will fallback to doing a full IPC buffer allocation with xnu-proxy. This
 * involves taking a mutex and is not realtime safe.
 */

/*
 * Determines the maximum size of the buffer cache. Can be overriden via an EDT
 * entry or boot-arg.
 */
TUNABLE_DEV_WRITEABLE(unsigned int, exclaves_ipc_buffer_cache_max,
    "exclaves_ipcb_cache", 16);

/* Current count of entries in the buffer cache. */
static unsigned int exclaves_ipc_buffer_cache_count = 0;

/* Intrusive linked list within the unused IPC buffer */
typedef struct exclaves_ipc_buffer_cache_item {
	struct exclaves_ipc_buffer_cache_item *next;
	Exclaves_L4_Word_t scid;
}__attribute__((__packed__)) exclaves_ipc_buffer_cache_item_t;

static_assert(Exclaves_L4_IpcBuffer_Size >=
    sizeof(exclaves_ipc_buffer_cache_item_t),
    "Invalid Exclaves_L4_IpcBuffer_Size");

static LCK_SPIN_DECLARE(exclaves_ipc_buffer_cache_lock, &exclaves_lck_grp);
static exclaves_ipc_buffer_cache_item_t *exclaves_ipc_buffer_cache;

static bool
exclaves_ipc_buffer_cache_alloc(exclaves_ctx_t *ctx)
{
	lck_spin_lock(&exclaves_ipc_buffer_cache_lock);

	if (exclaves_ipc_buffer_cache_count == 0) {
		lck_spin_unlock(&exclaves_ipc_buffer_cache_lock);
		return false;
	}

	assert3p(exclaves_ipc_buffer_cache, !=, NULL);

	exclaves_ipc_buffer_cache_item_t *cached_buffer = exclaves_ipc_buffer_cache;
	exclaves_ipc_buffer_cache = cached_buffer->next;

	exclaves_ipc_buffer_cache_count--;

	lck_spin_unlock(&exclaves_ipc_buffer_cache_lock);

	ctx->ipcb = (void *)cached_buffer;
	ctx->scid = cached_buffer->scid;
	ctx->usecnt = 0;

	/*
	 * Zero out this usage of the buffer to avoid any confusion in
	 * xnu-proxy.
	 */
	cached_buffer->next = NULL;
	cached_buffer->scid = 0;

	return true;
}

static bool
exclaves_ipc_buffer_cache_free(exclaves_ctx_t *ctx)
{
	assert3u(ctx->scid, !=, exclaves_bootstrap_ctx.scid);

	/* Zero out the IPC buffer to avoid having old IPC data lying around. */
	bzero(ctx->ipcb, Exclaves_L4_IpcBuffer_Size);

	lck_spin_lock(&exclaves_ipc_buffer_cache_lock);

#if 0 /* Removed with the fix for rdar://126257712 */
	/* Don't free into the cache if the cache has hit its limit. */
	if (exclaves_ipc_buffer_cache_count == exclaves_ipc_buffer_cache_max) {
		lck_spin_unlock(&exclaves_ipc_buffer_cache_lock);
		return false;
	}
#endif

	exclaves_ipc_buffer_cache_item_t *cached_buffer = NULL;

	cached_buffer = (void *)ctx->ipcb;
	cached_buffer->scid = ctx->scid;

	ctx->ipcb = NULL;
	ctx->scid = 0;
	ctx->usecnt = 0;

	cached_buffer->next = exclaves_ipc_buffer_cache;
	exclaves_ipc_buffer_cache = cached_buffer;

	exclaves_ipc_buffer_cache_count++;

	lck_spin_unlock(&exclaves_ipc_buffer_cache_lock);

	return true;
}

static kern_return_t
exclaves_ipc_buffer_cache_init(void)
{
	if (exclaves_ipc_buffer_cache_max == 0) {
		return KERN_SUCCESS;
	}

	kern_return_t kr = KERN_FAILURE;

	assert3p(exclaves_ipc_buffer_cache, ==, NULL);

	exclaves_ctx_t *ctx = kalloc_type(exclaves_ctx_t,
	    exclaves_ipc_buffer_cache_max, Z_WAITOK | Z_ZERO | Z_NOFAIL);

	/*
	 * Pre-warm the cache by allocating up to cache_max and then releasing
	 * the allocated contexts back into the cache.
	 */
	for (unsigned int i = 0; i < exclaves_ipc_buffer_cache_max; i++) {
		kr = exclaves_xnuproxy_ctx_alloc(&ctx[i]);
		if (kr != KERN_SUCCESS) {
			kfree_type(exclaves_ctx_t,
			    exclaves_ipc_buffer_cache_max, ctx);
			return kr;
		}
	}

	/*
	 * Release the newly allocated contexts so they ends up in the cache. We
	 * know this will succeed because the only failure modes of
	 * exclaves_xnuproxy_ctx_free are if the downcall fails. The downcall
	 * won't be used here as we *know* that the buffer cache is active.
	 */
	for (unsigned int i = 0; i < exclaves_ipc_buffer_cache_max; i++) {
		kr = exclaves_xnuproxy_ctx_free(&ctx[i]);
		assert3u(kr, ==, KERN_SUCCESS);
	}

	kfree_type(exclaves_ctx_t, exclaves_ipc_buffer_cache_max, ctx);

	return KERN_SUCCESS;
}


/* -------------------------------------------------------------------------- */
#pragma mark xnu-proxy calls

static xnuproxy_cmd_s xnuproxy_cmd_client = {0};

kern_return_t
exclaves_xnuproxy_ctx_alloc(exclaves_ctx_t *ctx)
{
	assert3p(ctx, !=, NULL);

	/* Try to allocate it from the cache. */
	if (exclaves_ipc_buffer_cache_alloc(ctx)) {
		assert(ctx->usecnt == 0);
		return KERN_SUCCESS;
	}

	/*
	 * Fallback to a full allocation with xnuproxy. This must be done in the
	 * context of the bootstrap scheduling context.
	 */
	exclaves_ctx_t stash_ctx = {};
	__block exclaves_ctx_t local_ctx = {};

	exclaves_bootstrap_context_acquire(&stash_ctx);

	/* This may spawn a new exclaves thread. */
	thread_exclaves_state_flags_t state = current_thread()->th_exclaves_state;
	current_thread()->th_exclaves_state |= TH_EXCLAVES_SPAWN_EXPECTED;

	tb_error_t ret = xnuproxy_cmd_ipccontextallocate(&xnuproxy_cmd_client,
	    ^(xnuproxy_ipccontext_s c) {
		local_ctx.ipcb = (Exclaves_L4_IpcBuffer_t *)phystokv(c.buffer);
		local_ctx.scid = c.scid;
	});

	/* Restore the old state (which itself may have set the SPAWN flag).  */
	current_thread()->th_exclaves_state = state;

	exclaves_bootstrap_context_release(&stash_ctx);

	if (ret != TB_ERROR_SUCCESS) {
		exclaves_debug_printf(show_errors,
		    "allocate context: failure %u\n", ret);
		return KERN_FAILURE;
	}

	/* Update count. */
	exclaves_ipc_buffer_count_inc();

	*ctx = local_ctx;

	assert(ctx->usecnt == 0);
	return KERN_SUCCESS;
}

kern_return_t
exclaves_xnuproxy_ctx_free(exclaves_ctx_t *ctx)
{
	assert3p(ctx, !=, NULL);

	/* exclaves_bootstrap_ctx.scid should never be freed. */
	if (ctx->scid == exclaves_bootstrap_ctx.scid) {
		return KERN_SUCCESS;
	}

	assert(ctx->usecnt == 0);
	/* Try to free it back to the cache. */
	if (exclaves_ipc_buffer_cache_free(ctx)) {
		return KERN_SUCCESS;
	}

	/*
	 * Fallback to a full free with xnuproxy. This must be done in the
	 * context of the bootstrap scheduling context.
	 */
	exclaves_ctx_t stash_ctx = {};
	__block exclaves_ctx_t local_ctx = *ctx;

	exclaves_bootstrap_context_acquire(&stash_ctx);

	xnuproxy_ipccontext_s c = {
		.scid = local_ctx.scid,
	};

	tb_error_t ret = xnuproxy_cmd_ipccontextfree(&xnuproxy_cmd_client, &c);

	exclaves_bootstrap_context_release(&stash_ctx);

	if (ret != TB_ERROR_SUCCESS) {
		exclaves_debug_printf(show_errors,
		    "free context: failure %u\n", ret);
		return KERN_FAILURE;
	}

	ctx->ipcb = NULL;
	ctx->scid = 0;
	ctx->usecnt = 0;

	/* Update count. */
	exclaves_ipc_buffer_count_dec();

	return KERN_SUCCESS;
}

static size_t
countof_char_v(const char_v_s *cv)
{
	assert3p(cv, !=, NULL);

	__block size_t count = 0;

	char__v_visit(cv,
	    ^( __unused size_t i, __unused const xnuproxy_char_s item) {
		count++;
	});

	return count;
}

static void
copy_char_v(const char_v_s *src, char *dst)
{
	assert3p(src, !=, NULL);
	assert3p(dst, !=, NULL);

	char__v_visit(src,
	    ^(size_t i, const xnuproxy_char_s item) {
		dst[i] = item;
	});
}

/*
 * Iterate over all the resources calling cb for each one.
 */
kern_return_t
exclaves_xnuproxy_resource_info(void (^cb)(const char *name, const char *domain,
    xnuproxy_resourcetype_s, uint64_t id, bool))
{
	/* BEGIN IGNORE CODESTYLE */
	tb_error_t ret = xnuproxy_cmd_resourceinfo(&xnuproxy_cmd_client,
	    ^(xnuproxy_resourceinfo_v_s ri) {
		xnuproxy_resourceinfo__v_visit(&ri,
		    ^(__unused size_t i, const xnuproxy_resourceinfo_s *item) {
			char name_copy[countof_char_v(&item->name)];
			copy_char_v(&item->name, name_copy);

			char domain_copy[countof_char_v(&item->domain)];
			copy_char_v(&item->domain, domain_copy);

			cb(name_copy, domain_copy,
			    (xnuproxy_resourcetype_s)item->type, item->id,
			    item->connected);
		});
	});
	/* END IGNORE CODESTYLE */

	if (ret != TB_ERROR_SUCCESS) {
		exclaves_debug_printf(show_errors,
		    "resource info: failure %u\n", ret);
		return KERN_FAILURE;
	}

	return KERN_SUCCESS;
}

kern_return_t
exclaves_xnuproxy_pmm_usage(void)
{
	tb_error_t ret = xnuproxy_cmd_pmmmemusage(&xnuproxy_cmd_client);
	if (ret != TB_ERROR_SUCCESS) {
		exclaves_debug_printf(show_errors,
		    "pmm usage: failure %u\n", ret);
		return KERN_FAILURE;
	}

	return KERN_SUCCESS;
}

kern_return_t
exclaves_xnuproxy_panic_setup(uint64_t *phys, uint64_t *scid)
{
	current_thread()->th_exclaves_state |= TH_EXCLAVES_SPAWN_EXPECTED;

	tb_error_t ret = xnuproxy_cmd_panicsetup(&xnuproxy_cmd_client,
	    ^(xnuproxy_panicinfo_s panic_data) {
		*phys = panic_data.buffer;
		*scid = panic_data.scid;
	});

	current_thread()->th_exclaves_state &= ~TH_EXCLAVES_SPAWN_EXPECTED;

	if (ret != TB_ERROR_SUCCESS) {
		exclaves_debug_printf(show_errors,
		    "panic setup: failure %u\n", ret);
		return KERN_FAILURE;
	}

	return KERN_SUCCESS;
}


/* -------------------------------------------------------------------------- */
#pragma mark legacy xnu-proxy calls


kern_return_t
exclaves_xnuproxy_audio_buffer_copyout(uint64_t id,
    uint64_t size1, uint64_t offset1, uint64_t size2, uint64_t offset2)
{
	__block xnuproxy_namedbufferstatus_s status =
	    XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS;

	/* BEGIN IGNORE CODESTYLE */
	tb_error_t ret = xnuproxy_cmd_audiobuffercopyout(&xnuproxy_cmd_client,
	    id, size1, offset1, size2, offset2,
	    ^(xnuproxy_cmd_audiobuffercopyout__result_s result) {
		if (xnuproxy_cmd_audiobuffercopyout__result_get_success(&result)) {
			status = XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS;
			return;
		}

		xnuproxy_namedbufferstatus_s *status_p = NULL;
		status_p = xnuproxy_cmd_audiobuffercopyout__result_get_failure(&result);
		assert3p(status_p, !=, NULL);
		status = *status_p;

		exclaves_debug_printf(show_errors,
		    "audio buffer copyout: failure %u\n", status);
	});

	if (ret != TB_ERROR_SUCCESS) {
		return KERN_FAILURE;
	}

	if (status != XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS) {
		return status == XNUPROXY_NAMEDBUFFERSTATUS_INVALIDARGUMENT ?
		    KERN_INVALID_ARGUMENT : KERN_FAILURE;
	}
	/* END IGNORE CODESTYLE */

	return KERN_SUCCESS;
}

kern_return_t
exclaves_xnuproxy_audio_buffer_delete(uint64_t id)
{
	__block xnuproxy_namedbufferstatus_s status =
	    XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS;

	/* BEGIN IGNORE CODESTYLE */
	tb_error_t ret = xnuproxy_cmd_audiobufferdelete(&xnuproxy_cmd_client, id,
	    ^(xnuproxy_cmd_audiobufferdelete__result_s result) {
		if (xnuproxy_cmd_audiobufferdelete__result_get_success(&result)) {
			status = XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS;
			return;
		}

		xnuproxy_namedbuffererror_s *error = NULL;
		error = xnuproxy_cmd_audiobufferdelete__result_get_failure(&result);

		assert3p(error, !=, NULL);
		exclaves_debug_printf(show_errors,
		    "audio buffer delete: failure %u, %u\n",
		    error->status, error->substatus);
		status = error->status;
	});

	if (ret != TB_ERROR_SUCCESS) {
		return KERN_FAILURE;
	}

	if (status != XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS) {
		return status == XNUPROXY_NAMEDBUFFERSTATUS_INVALIDARGUMENT ?
		    KERN_INVALID_ARGUMENT : KERN_FAILURE;
	}
	/* END IGNORE CODESTYLE */

	return KERN_SUCCESS;
}

kern_return_t
exclaves_xnuproxy_audio_buffer_map(uint64_t id, size_t size, bool *read_only)
{
	__block xnuproxy_namedbufferstatus_s status =
	    XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS;

	/* BEGIN IGNORE CODESTYLE */
	tb_error_t ret = xnuproxy_cmd_audiobuffermap(&xnuproxy_cmd_client, id,
	    size, ^(xnuproxy_cmd_audiobuffermap__result_s result) {
		xnuproxy_mapinfo_s *map_info;
		map_info = xnuproxy_cmd_audiobuffermap__result_get_success(&result);
		if (map_info != NULL) {
			*read_only = map_info->readonly;
			status = XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS;
			return;
		}

		xnuproxy_namedbuffererror_s *error;
		error = xnuproxy_cmd_audiobuffermap__result_get_failure(&result);
		assert3p(error, !=, NULL);

		exclaves_debug_printf(show_errors,
		    "audio buffer map: failure %u, %u\n",
		    error->status, error->substatus);
		status = error->status;
	});

	if (ret != TB_ERROR_SUCCESS) {
		return KERN_FAILURE;
	}

	if (status != XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS) {
		return status == XNUPROXY_NAMEDBUFFERSTATUS_INVALIDARGUMENT ?
		    KERN_INVALID_ARGUMENT : KERN_FAILURE;
	}

	/* END IGNORE CODESTYLE */

	return KERN_SUCCESS;
}

kern_return_t
exclaves_xnuproxy_audio_buffer_layout(uint64_t id, uint32_t start,
    uint32_t npages, kern_return_t (^cb)(uint64_t base, uint32_t npages))
{
	__block xnuproxy_namedbufferstatus_s status = 0;
	__block kern_return_t kret = XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS;

	/* BEGIN IGNORE CODESTYLE */
	tb_error_t ret = xnuproxy_cmd_audiobufferlayout(&xnuproxy_cmd_client,
	    id, start, npages,
	    ^(xnuproxy_cmd_audiobufferlayout__result_s result) {
		xnuproxy_namedbufferrange_v_s *ranges;
		ranges = xnuproxy_cmd_audiobufferlayout__result_get_success(&result);
		status = XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS;
		if (ranges != NULL) {
			xnuproxy_namedbufferrange__v_visit(ranges,
			    ^(__unused size_t i, const xnuproxy_namedbufferrange_s *item) {
				/*
				 * Only fail once. TB want to iterate over the
				 * entire array so we have to keep going but
				 * once the first failure is seen, don't bother
				 * calling cb.
				 */
				if (kret == KERN_SUCCESS) {
					kret = cb(item->address, item->numpages);
				}
			});

			return;
		}

		xnuproxy_namedbuffererror_s *error;
		error = xnuproxy_cmd_audiobufferlayout__result_get_failure(&result);
		assert3p(error, !=, NULL);

		exclaves_debug_printf(show_errors,
		    "audio buffer layout: failure %u, %u\n",
		    error->status, error->substatus);
		status = error->status;
	});

	if (ret != TB_ERROR_SUCCESS) {
		return KERN_FAILURE;
	}

	if (kret != KERN_SUCCESS) {
		return kret;
	}

	if (status != XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS) {
		return status == XNUPROXY_NAMEDBUFFERSTATUS_INVALIDARGUMENT ?
		    KERN_INVALID_ARGUMENT : KERN_FAILURE;
	}
	/* END IGNORE CODESTYLE */

	return KERN_SUCCESS;
}

kern_return_t
exclaves_xnuproxy_named_buffer_delete(uint64_t id)
{
	__block xnuproxy_namedbufferstatus_s status =
	    XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS;

	/* BEGIN IGNORE CODESTYLE */
	tb_error_t ret = xnuproxy_cmd_namedbufferdelete(&xnuproxy_cmd_client, id,
	    ^(xnuproxy_cmd_namedbufferdelete__result_s result) {
		if (xnuproxy_cmd_namedbufferdelete__result_get_success(&result)) {
			status = XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS;
			return;
		}

		xnuproxy_namedbuffererror_s *error = NULL;
		error = xnuproxy_cmd_namedbufferdelete__result_get_failure(&result);

		assert3p(error, !=, NULL);
		exclaves_debug_printf(show_errors,
		    "named buffer delete: failure %u, %u\n",
		    error->status, error->substatus);
		status = error->status;
	});


	if (ret != TB_ERROR_SUCCESS) {
		return KERN_FAILURE;
	}

	if (status != XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS) {
		return status == XNUPROXY_NAMEDBUFFERSTATUS_INVALIDARGUMENT ?
		    KERN_INVALID_ARGUMENT : KERN_FAILURE;
	}
	/* END IGNORE CODESTYLE */

	return KERN_SUCCESS;
}

kern_return_t
exclaves_xnuproxy_named_buffer_map(uint64_t id, size_t size, bool *read_only)
{
	__block xnuproxy_namedbufferstatus_s status =
	    XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS;

	/* BEGIN IGNORE CODESTYLE */
	tb_error_t ret = xnuproxy_cmd_namedbuffermap(&xnuproxy_cmd_client, id,
	    size, ^(xnuproxy_cmd_namedbuffermap__result_s result) {
		xnuproxy_mapinfo_s *map_info;
		map_info = xnuproxy_cmd_namedbuffermap__result_get_success(&result);
		if (map_info != NULL) {
			*read_only = map_info->readonly;
			status = XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS;
			return;
		}

		xnuproxy_namedbuffererror_s *error;
		error = xnuproxy_cmd_namedbuffermap__result_get_failure(&result);
		assert3p(error, !=, NULL);

		exclaves_debug_printf(show_errors,
		    "named buffer map: failure %u, %u\n",
		    error->status, error->substatus);
		status = error->status;
	});

	if (ret != TB_ERROR_SUCCESS) {
		return KERN_FAILURE;
	}

	if (status != XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS) {
		return status == XNUPROXY_NAMEDBUFFERSTATUS_INVALIDARGUMENT ?
		    KERN_INVALID_ARGUMENT : KERN_FAILURE;
	}
	/* END IGNORE CODESTYLE */

	return KERN_SUCCESS;
}

kern_return_t
exclaves_xnuproxy_named_buffer_layout(uint64_t id, uint32_t start,
    uint32_t npages, kern_return_t (^cb)(uint64_t base, uint32_t npages))
{
	__block xnuproxy_namedbufferstatus_s status =
	    XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS;
	__block kern_return_t kret = KERN_SUCCESS;

	/* BEGIN IGNORE CODESTYLE */
	tb_error_t ret = xnuproxy_cmd_namedbufferlayout(&xnuproxy_cmd_client,
	    id, start, npages,
	    ^(xnuproxy_cmd_namedbufferlayout__result_s result) {
		xnuproxy_namedbufferrange_v_s *ranges;
		ranges = xnuproxy_cmd_namedbufferlayout__result_get_success(&result);
		status = XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS;
		if (ranges != NULL) {
			xnuproxy_namedbufferrange__v_visit(ranges,
			    ^(__unused size_t i,
			    const xnuproxy_namedbufferrange_s *item) {
				/*
				 * Only fail once. TB want to iterate over the
				 * entire array so we have to keep going but
				 * once the first failure is seen, don't bother
				 * calling cb.
				 */
				if (kret == KERN_SUCCESS) {
					kret = cb(item->address, item->numpages);
				}
			});

			return;
		}

		xnuproxy_namedbuffererror_s *error;
		error = xnuproxy_cmd_namedbufferlayout__result_get_failure(&result);
		assert3p(error, !=, NULL);

		exclaves_debug_printf(show_errors,
		    "named buffer layout: failure %u, %u\n",
		    error->status, error->substatus);
		status = error->status;
	});

	if (ret != TB_ERROR_SUCCESS) {
		return KERN_FAILURE;
	}

	if (kret != KERN_SUCCESS) {
		return kret;
	}

	if (status != XNUPROXY_NAMEDBUFFERSTATUS_SUCCESS) {
		return status == XNUPROXY_NAMEDBUFFERSTATUS_INVALIDARGUMENT ?
		    KERN_INVALID_ARGUMENT : KERN_FAILURE;
	}
	/* END IGNORE CODESTYLE */

	return KERN_SUCCESS;
}

/* -------------------------------------------------------------------------- */
#pragma mark exclaves xnu-proxy downcall

#define exclaves_xnuproxy_endpoint_call_show_progress(operation, step, \
	    eid, scid, status) \
	exclaves_debug_printf(show_progress, \
	    "exclaves: xnu proxy endpoint " #operation " " #step ":\t" \
	    "endpoint id %ld scid 0x%lx status %u\n", \
	    (eid), (scid), (status))
OS_NOINLINE
kern_return_t
exclaves_xnuproxy_endpoint_call(Exclaves_L4_Word_t endpoint_id)
{
	kern_return_t kr = KERN_SUCCESS;
	thread_t thread = current_thread();
	bool interrupted = false;

	Exclaves_L4_Word_t scid = thread->th_exclaves_ipc_ctx.scid;
	Exclaves_L4_IpcBuffer_t *ipcb = thread->th_exclaves_ipc_ctx.ipcb;
	xnuproxy_msg_status_t status =
	    XNUPROXY_MSG_STATUS_PROCESSING;

	XNUPROXY_CR_ENDPOINT_ID(ipcb) = endpoint_id;
	XNUPROXY_CR_STATUS(ipcb) = status;

	exclaves_xnuproxy_endpoint_call_show_progress(call, entry,
	    endpoint_id, scid, status);

	assert3u(thread->th_exclaves_state & TH_EXCLAVES_STATE_ANY, ==, 0);
	thread->th_exclaves_state |= TH_EXCLAVES_RPC;
	KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_EXCLAVES, MACH_EXCLAVES_RPC)
	    | DBG_FUNC_START, scid, endpoint_id);

	while (1) {
		kr = exclaves_scheduler_resume_scheduling_context(
			&thread->th_exclaves_ipc_ctx, interrupted);
		assert(kr == KERN_SUCCESS || kr == KERN_ABORTED);

		/* A wait was interrupted. */
		interrupted = kr == KERN_ABORTED;

		status = (xnuproxy_msg_status_t)
		    XNUPROXY_CR_STATUS(ipcb);

		switch (status) {
		case XNUPROXY_MSG_STATUS_PROCESSING:
			exclaves_xnuproxy_endpoint_call_show_progress(call, yielded,
			    endpoint_id, scid, status);
			continue;

		case XNUPROXY_MSG_STATUS_REPLY:
			exclaves_xnuproxy_endpoint_call_show_progress(call, returned,
			    endpoint_id, scid, status);
			kr = KERN_SUCCESS;
			break;

		case XNUPROXY_MSG_STATUS_UPCALL:
			thread->th_exclaves_state |= TH_EXCLAVES_UPCALL;
			endpoint_id = XNUPROXY_CR_ENDPOINT_ID(ipcb);
			KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_EXCLAVES, MACH_EXCLAVES_UPCALL)
			    | DBG_FUNC_START, scid, endpoint_id);
			exclaves_xnuproxy_endpoint_call_show_progress(upcall, entry,
			    endpoint_id, scid, status);
			kr = exclaves_call_upcall_handler(endpoint_id);
			XNUPROXY_CR_STATUS(ipcb) =
			    XNUPROXY_MSG_STATUS_PROCESSING;
			/* TODO: More state returned than Success or OperationInvalid? */
			XNUPROXY_CR_RETVAL(ipcb) =
			    (kr == KERN_SUCCESS) ? Exclaves_L4_Success :
			    Exclaves_L4_ErrorOperationInvalid;
			KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_EXCLAVES, MACH_EXCLAVES_UPCALL)
			    | DBG_FUNC_END);
			thread->th_exclaves_state &= ~TH_EXCLAVES_UPCALL;
			exclaves_xnuproxy_endpoint_call_show_progress(upcall, returned,
			    endpoint_id, scid,
			    (unsigned int)XNUPROXY_CR_RETVAL(ipcb));
			continue;

		default:
			// Should we have an assert(valid return) here?
			exclaves_xnuproxy_endpoint_call_show_progress(call, failed,
			    endpoint_id, scid, status);
			kr = KERN_FAILURE;
			break;
		}
		break;
	}

	KDBG_RELEASE(MACHDBG_CODE(DBG_MACH_EXCLAVES, MACH_EXCLAVES_RPC)
	    | DBG_FUNC_END);
	thread->th_exclaves_state &= ~TH_EXCLAVES_RPC;

	return kr;
}


/* -------------------------------------------------------------------------- */
#pragma mark exclaves xnu-proxy initialisation

kern_return_t
exclaves_xnuproxy_init(uint64_t bootinfo_pa)
{
	assert3u(bootinfo_pa, !=, 0);

	kern_return_t kr = KERN_FAILURE;

	void *bootinfo_va = (void *)phystokv(bootinfo_pa);
	assert3p(bootinfo_va, !=, NULL);
	const size_t bootinfo_size =
	    xnuproxy_bootinfo__marshal_sizeof(&(xnuproxy_bootinfo_s){});

	__block uint64_t endpoint = 0;

	/* BEGIN IGNORE CODESTYLE */
	tb_error_t ret = xnuproxy_bootinfo__unmarshal(bootinfo_va,
	    bootinfo_size, ^(xnuproxy_bootinfo_s bootinfo) {

		/* Do the version check. */
		if (bootinfo.version != XNUPROXY_VERSION_CURRENT) {
			exclaves_debug_printf(show_errors,
			    "exclaves: mismatched xnuproxy message version, "
			    "xnuproxy: %u, xnu: %u\n", bootinfo.version,
			    XNUPROXY_VERSION_CURRENT);
			return;
		}

		exclaves_debug_printf(show_progress,
		    "exclaves: xnuproxy message version: 0x%u\n",
		    XNUPROXY_VERSION_CURRENT);


		if (!pmap_valid_address(bootinfo.buffer)) {
			exclaves_debug_printf(show_errors,
			    "exclaves: invalid bootstrap IPC buffer address: "
			    "0x%llx\n", bootinfo.buffer);
			return;
		}

		exclaves_bootstrap_ctx.scid = bootinfo.scid;
		exclaves_bootstrap_ctx.ipcb =
		    (Exclaves_L4_IpcBuffer_t *)phystokv(bootinfo.buffer);
		assert3p(exclaves_bootstrap_ctx.ipcb, !=, NULL);
		exclaves_bootstrap_ctx.usecnt = 1;

		endpoint = bootinfo.endpointid;
	});

	/* END IGNORE CODESTYLE */
	if (ret != TB_ERROR_SUCCESS) {
		exclaves_debug_printf(show_errors,
		    "failed to unmarshal bootinfo\n");
		return KERN_FAILURE;
	}

	/*
	 * Check to see if we bailed out of the unmarshal block early which
	 * would indicate a failure (for example the version check may have
	 * failed).
	 */
	if (endpoint == 0) {
		return KERN_FAILURE;
	}

	if (exclaves_bootstrap_ctx.ipcb == NULL) {
		return KERN_FAILURE;
	}

	/* BEGIN IGNORE CODESTYLE */
	tb_endpoint_t ep = tb_endpoint_create_with_value(
	    TB_TRANSPORT_TYPE_XNU, endpoint, TB_ENDPOINT_OPTIONS_NONE);
	/* END IGNORE CODESTYLE */
	ret = xnuproxy_cmd__init(&xnuproxy_cmd_client, ep);
	if (ret != TB_ERROR_SUCCESS) {
		exclaves_debug_printf(show_errors,
		    "failed to create xnuproxy endpoint\n");
		return KERN_FAILURE;
	}
	/* Downcalls to xnu-proxy now supported. */

	kr = exclaves_ipc_buffer_cache_init();
	if (kr != KERN_SUCCESS) {
		return kr;
	}

	return KERN_SUCCESS;
}

#endif /* CONFIG_EXCLAVES */