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
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
/*
 * Copyright (c) 2006-2020 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@
 */

#include <sys/errno.h>

#include <mach/mach_types.h>
#include <mach/mach_traps.h>
#include <mach/host_priv.h>
#include <mach/kern_return.h>
#include <mach/memory_object_control.h>
#include <mach/memory_object_types.h>
#include <mach/port.h>
#include <mach/policy.h>
#include <mach/upl.h>
#include <mach/thread_act.h>
#include <mach/mach_vm.h>

#include <kern/host.h>
#include <kern/kalloc.h>
#include <kern/page_decrypt.h>
#include <kern/queue.h>
#include <kern/thread.h>
#include <kern/ipc_kobject.h>
#include <os/refcnt.h>

#include <sys/kdebug_triage.h>

#include <vm/vm_fault_internal.h>
#include <vm/vm_map.h>
#include <vm/memory_object_internal.h>
#include <vm/vm_pageout_xnu.h>
#include <vm/vm_protos_internal.h>
#include <vm/vm_kern.h>
#include <vm/vm_ubc.h>
#include <vm/vm_page_internal.h>
#include <vm/vm_object_internal.h>

/*
 * APPLE PROTECT MEMORY PAGER
 *
 * This external memory manager (EMM) handles memory from the encrypted
 * sections of some executables protected by the DSMOS kernel extension.
 *
 * It mostly handles page-in requests (from memory_object_data_request()) by
 * getting the encrypted data from its backing VM object, itself backed by
 * the encrypted file, decrypting it and providing it to VM.
 *
 * The decrypted pages will never be dirtied, so the memory manager doesn't
 * need to handle page-out requests (from memory_object_data_return()).  The
 * pages need to be mapped copy-on-write, so that the originals stay clean.
 *
 * We don't expect to have to handle a large number of apple-protected
 * binaries, so the data structures are very simple (simple linked list)
 * for now.
 */

/* forward declarations */
void apple_protect_pager_reference(memory_object_t mem_obj);
void apple_protect_pager_deallocate(memory_object_t mem_obj);
kern_return_t apple_protect_pager_init(memory_object_t mem_obj,
    memory_object_control_t control,
    memory_object_cluster_size_t pg_size);
kern_return_t apple_protect_pager_terminate(memory_object_t mem_obj);
kern_return_t apple_protect_pager_data_request(memory_object_t mem_obj,
    memory_object_offset_t offset,
    memory_object_cluster_size_t length,
    vm_prot_t protection_required,
    memory_object_fault_info_t fault_info);
kern_return_t apple_protect_pager_data_return(memory_object_t mem_obj,
    memory_object_offset_t offset,
    memory_object_cluster_size_t      data_cnt,
    memory_object_offset_t *resid_offset,
    int *io_error,
    boolean_t dirty,
    boolean_t kernel_copy,
    int upl_flags);
kern_return_t apple_protect_pager_data_initialize(memory_object_t mem_obj,
    memory_object_offset_t offset,
    memory_object_cluster_size_t data_cnt);
kern_return_t apple_protect_pager_map(memory_object_t mem_obj,
    vm_prot_t prot);
kern_return_t apple_protect_pager_last_unmap(memory_object_t mem_obj);
boolean_t apple_protect_pager_backing_object(
	memory_object_t mem_obj,
	memory_object_offset_t mem_obj_offset,
	vm_object_t *backing_object,
	vm_object_offset_t *backing_offset);

#define CRYPT_INFO_DEBUG 0
void crypt_info_reference(struct pager_crypt_info *crypt_info);
void crypt_info_deallocate(struct pager_crypt_info *crypt_info);

/*
 * Vector of VM operations for this EMM.
 * These routines are invoked by VM via the memory_object_*() interfaces.
 */
const struct memory_object_pager_ops apple_protect_pager_ops = {
	.memory_object_reference = apple_protect_pager_reference,
	.memory_object_deallocate = apple_protect_pager_deallocate,
	.memory_object_init = apple_protect_pager_init,
	.memory_object_terminate = apple_protect_pager_terminate,
	.memory_object_data_request = apple_protect_pager_data_request,
	.memory_object_data_return = apple_protect_pager_data_return,
	.memory_object_data_initialize = apple_protect_pager_data_initialize,
	.memory_object_map = apple_protect_pager_map,
	.memory_object_last_unmap = apple_protect_pager_last_unmap,
	.memory_object_backing_object = apple_protect_pager_backing_object,
	.memory_object_pager_name = "apple_protect"
};

/*
 * The "apple_protect_pager" describes a memory object backed by
 * the "apple protect" EMM.
 */
typedef struct apple_protect_pager {
	/* mandatory generic header */
	struct memory_object    ap_pgr_hdr;

	/* pager-specific data */
	queue_chain_t           pager_queue;    /* next & prev pagers */
#if MEMORY_OBJECT_HAS_REFCOUNT
#define ap_pgr_hdr_ref          ap_pgr_hdr.mo_ref
#else
	os_ref_atomic_t         ap_pgr_hdr_ref;      /* reference count */
#endif
	bool                    is_ready;       /* is this pager ready ? */
	bool                    is_mapped;      /* is this mem_obj mapped ? */
	bool                    is_cached;      /* is this pager cached ? */
	vm_object_t             backing_object; /* VM obj w/ encrypted data */
	vm_object_offset_t      backing_offset;
	vm_object_offset_t      crypto_backing_offset; /* for key... */
	vm_object_offset_t      crypto_start;
	vm_object_offset_t      crypto_end;
	struct pager_crypt_info *crypt_info;
} *apple_protect_pager_t;
#define APPLE_PROTECT_PAGER_NULL        ((apple_protect_pager_t) NULL)

/*
 * List of memory objects managed by this EMM.
 * The list is protected by the "apple_protect_pager_lock" lock.
 */
unsigned int apple_protect_pager_count = 0;        /* number of pagers */
unsigned int apple_protect_pager_count_mapped = 0; /* number of unmapped pagers */
queue_head_t apple_protect_pager_queue = QUEUE_HEAD_INITIALIZER(apple_protect_pager_queue);
LCK_GRP_DECLARE(apple_protect_pager_lck_grp, "apple_protect");
LCK_MTX_DECLARE(apple_protect_pager_lock, &apple_protect_pager_lck_grp);

/*
 * Maximum number of unmapped pagers we're willing to keep around.
 */
unsigned int apple_protect_pager_cache_limit = 20;

/*
 * Statistics & counters.
 */
unsigned int apple_protect_pager_count_max = 0;
unsigned int apple_protect_pager_count_unmapped_max = 0;
unsigned int apple_protect_pager_num_trim_max = 0;
unsigned int apple_protect_pager_num_trim_total = 0;



/* internal prototypes */
apple_protect_pager_t apple_protect_pager_create(
	vm_object_t backing_object,
	vm_object_offset_t backing_offset,
	vm_object_offset_t crypto_backing_offset,
	struct pager_crypt_info *crypt_info,
	vm_object_offset_t crypto_start,
	vm_object_offset_t crypto_end,
	boolean_t cache_pager);
apple_protect_pager_t apple_protect_pager_lookup(memory_object_t mem_obj);
void apple_protect_pager_dequeue(apple_protect_pager_t pager);
void apple_protect_pager_deallocate_internal(apple_protect_pager_t pager,
    boolean_t locked);
void apple_protect_pager_terminate_internal(apple_protect_pager_t pager);
void apple_protect_pager_trim(void);


#if DEBUG
int apple_protect_pagerdebug = 0;
#define PAGER_ALL               0xffffffff
#define PAGER_INIT              0x00000001
#define PAGER_PAGEIN            0x00000002

#define PAGER_DEBUG(LEVEL, A)                                           \
	MACRO_BEGIN                                                     \
	if ((apple_protect_pagerdebug & LEVEL)==LEVEL) {                \
	        printf A;                                               \
	}                                                               \
	MACRO_END
#else
#define PAGER_DEBUG(LEVEL, A)
#endif

/*
 * apple_protect_pager_init()
 *
 * Initialize the memory object and makes it ready to be used and mapped.
 */
kern_return_t
apple_protect_pager_init(
	memory_object_t         mem_obj,
	memory_object_control_t control,
#if !DEBUG
	__unused
#endif
	memory_object_cluster_size_t pg_size)
{
	apple_protect_pager_t   pager;
	kern_return_t           kr;
	memory_object_attr_info_data_t  attributes;

	PAGER_DEBUG(PAGER_ALL,
	    ("apple_protect_pager_init: %p, %p, %x\n",
	    mem_obj, control, pg_size));

	if (control == MEMORY_OBJECT_CONTROL_NULL) {
		return KERN_INVALID_ARGUMENT;
	}

	pager = apple_protect_pager_lookup(mem_obj);

	memory_object_control_reference(control);

	pager->ap_pgr_hdr.mo_control = control;

	attributes.copy_strategy = MEMORY_OBJECT_COPY_DELAY;
	/* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
	attributes.cluster_size = (1 << (PAGE_SHIFT));
	attributes.may_cache_object = FALSE;
	attributes.temporary = TRUE;

	kr = memory_object_change_attributes(
		control,
		MEMORY_OBJECT_ATTRIBUTE_INFO,
		(memory_object_info_t) &attributes,
		MEMORY_OBJECT_ATTR_INFO_COUNT);
	if (kr != KERN_SUCCESS) {
		panic("apple_protect_pager_init: "
		    "memory_object_change_attributes() failed");
	}

#if CONFIG_SECLUDED_MEMORY
	if (secluded_for_filecache) {
		memory_object_mark_eligible_for_secluded(control, TRUE);
	}
#endif /* CONFIG_SECLUDED_MEMORY */

	return KERN_SUCCESS;
}

/*
 * apple_protect_data_return()
 *
 * Handles page-out requests from VM.  This should never happen since
 * the pages provided by this EMM are not supposed to be dirty or dirtied
 * and VM should simply discard the contents and reclaim the pages if it
 * needs to.
 */
kern_return_t
apple_protect_pager_data_return(
	__unused memory_object_t        mem_obj,
	__unused memory_object_offset_t offset,
	__unused memory_object_cluster_size_t           data_cnt,
	__unused memory_object_offset_t *resid_offset,
	__unused int                    *io_error,
	__unused boolean_t              dirty,
	__unused boolean_t              kernel_copy,
	__unused int                    upl_flags)
{
	panic("apple_protect_pager_data_return: should never get called");
	return KERN_FAILURE;
}

kern_return_t
apple_protect_pager_data_initialize(
	__unused memory_object_t        mem_obj,
	__unused memory_object_offset_t offset,
	__unused memory_object_cluster_size_t           data_cnt)
{
	panic("apple_protect_pager_data_initialize: should never get called");
	return KERN_FAILURE;
}

/*
 * apple_protect_pager_data_request()
 *
 * Handles page-in requests from VM.
 */
int apple_protect_pager_data_request_debug = 0;
kern_return_t
apple_protect_pager_data_request(
	memory_object_t         mem_obj,
	memory_object_offset_t  offset,
	memory_object_cluster_size_t            length,
#if !DEBUG
	__unused
#endif
	vm_prot_t               protection_required,
	memory_object_fault_info_t mo_fault_info)
{
	apple_protect_pager_t   pager;
	memory_object_control_t mo_control;
	upl_t                   upl;
	int                     upl_flags;
	upl_size_t              upl_size;
	upl_page_info_t         *upl_pl;
	unsigned int            pl_count;
	vm_object_t             src_top_object, src_page_object, dst_object;
	kern_return_t           kr, retval;
	vm_offset_t             src_vaddr, dst_vaddr;
	vm_offset_t             cur_offset;
	vm_offset_t             offset_in_page;
	kern_return_t           error_code;
	vm_prot_t               prot;
	vm_page_t               src_page, top_page;
	int                     interruptible;
	struct vm_object_fault_info     fault_info;
	vm_fault_return_t       vmfr;
	int                     ret;

	PAGER_DEBUG(PAGER_ALL, ("apple_protect_pager_data_request: %p, %llx, %x, %x\n", mem_obj, offset, length, protection_required));

	retval = KERN_SUCCESS;
	src_top_object = VM_OBJECT_NULL;
	src_page_object = VM_OBJECT_NULL;
	upl = NULL;
	upl_pl = NULL;
	fault_info = *((struct vm_object_fault_info *)(uintptr_t)mo_fault_info);
	fault_info.stealth = TRUE;
	fault_info.io_sync = FALSE;
	fault_info.mark_zf_absent = FALSE;
	fault_info.batch_pmap_op = FALSE;
	interruptible = fault_info.interruptible;

	pager = apple_protect_pager_lookup(mem_obj);
	assert(pager->is_ready);
	assert(os_ref_get_count_raw(&pager->ap_pgr_hdr_ref) > 1); /* pager is alive and mapped */

	PAGER_DEBUG(PAGER_PAGEIN, ("apple_protect_pager_data_request: %p, %llx, %x, %x, pager %p\n", mem_obj, offset, length, protection_required, pager));

	fault_info.lo_offset += pager->backing_offset;
	fault_info.hi_offset += pager->backing_offset;

	/*
	 * Gather in a UPL all the VM pages requested by VM.
	 */
	mo_control = pager->ap_pgr_hdr.mo_control;

	upl_size = length;
	upl_flags =
	    UPL_RET_ONLY_ABSENT |
	    UPL_SET_LITE |
	    UPL_NO_SYNC |
	    UPL_CLEAN_IN_PLACE |        /* triggers UPL_CLEAR_DIRTY */
	    UPL_SET_INTERNAL;
	pl_count = 0;
	kr = memory_object_upl_request(mo_control,
	    offset, upl_size,
	    &upl, NULL, NULL, upl_flags, VM_KERN_MEMORY_SECURITY);
	if (kr != KERN_SUCCESS) {
		retval = kr;
		goto done;
	}
	dst_object = memory_object_control_to_vm_object(mo_control);
	assert(dst_object != VM_OBJECT_NULL);

	/*
	 * We'll map the encrypted data in the kernel address space from the
	 * backing VM object (itself backed by the encrypted file via
	 * the vnode pager).
	 */
	src_top_object = pager->backing_object;
	assert(src_top_object != VM_OBJECT_NULL);
	vm_object_reference(src_top_object); /* keep the source object alive */

	/*
	 * Fill in the contents of the pages requested by VM.
	 */
	upl_pl = UPL_GET_INTERNAL_PAGE_LIST(upl);
	pl_count = length / PAGE_SIZE;
	for (cur_offset = 0;
	    retval == KERN_SUCCESS && cur_offset < length;
	    cur_offset += PAGE_SIZE) {
		ppnum_t dst_pnum;

		if (!upl_page_present(upl_pl, (int)(cur_offset / PAGE_SIZE))) {
			/* this page is not in the UPL: skip it */
			continue;
		}

		/*
		 * Map the source (encrypted) page in the kernel's
		 * virtual address space.
		 * We already hold a reference on the src_top_object.
		 */
retry_src_fault:
		vm_page_grab_prime();
		vm_object_lock(src_top_object);
		vm_object_paging_begin(src_top_object);
		error_code = 0;
		prot = VM_PROT_READ;
		src_page = VM_PAGE_NULL;
		vmfr = vm_fault_page(src_top_object,
		    pager->backing_offset + offset + cur_offset,
		    VM_PROT_READ,
		    FALSE,
		    FALSE,                /* src_page not looked up */
		    &prot,
		    &src_page,
		    &top_page,
		    NULL,
		    &error_code,
		    FALSE,
		    &fault_info,
		    NULL);
		switch (vmfr) {
		case VM_FAULT_SUCCESS:
			break;
		case VM_FAULT_RETRY:
			goto retry_src_fault;
		case VM_FAULT_MEMORY_SHORTAGE:
			if (vm_page_wait(interruptible)) {
				goto retry_src_fault;
			}
			ktriage_record(thread_tid(current_thread()),
			    KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_APPLE_PROTECT_PAGER,
			    KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_APPLE_PROTECT_PAGER_MEMORY_SHORTAGE),
			    0 /* arg */);
			OS_FALLTHROUGH;
		case VM_FAULT_INTERRUPTED:
			retval = MACH_SEND_INTERRUPTED;
			goto done;
		case VM_FAULT_SUCCESS_NO_VM_PAGE:
			/* success but no VM page: fail */
			vm_object_paging_end(src_top_object);
			vm_object_unlock(src_top_object);
			OS_FALLTHROUGH;
		case VM_FAULT_MEMORY_ERROR:
			/* the page is not there ! */
			if (error_code) {
				retval = error_code;
			} else {
				retval = KERN_MEMORY_ERROR;
			}
			goto done;
		case VM_FAULT_BUSY:
			retval = KERN_ALREADY_WAITING;
			goto done;
		default:
			panic("%s: "
			    "vm_fault_page() return unexpected error 0x%x\n",
			    __func__, vmfr);
		}
		assert(src_page != VM_PAGE_NULL);
		assert(src_page->vmp_busy);

		if (src_page->vmp_q_state != VM_PAGE_ON_SPECULATIVE_Q) {
			vm_page_lockspin_queues();

			if (src_page->vmp_q_state != VM_PAGE_ON_SPECULATIVE_Q) {
				vm_page_speculate(src_page, FALSE);
			}
			vm_page_unlock_queues();
		}

		/*
		 * Establish pointers to the source
		 * and destination physical pages.
		 */
		dst_pnum = (ppnum_t)
		    upl_phys_page(upl_pl, (int)(cur_offset / PAGE_SIZE));
		assert(dst_pnum != 0);

		src_vaddr = (vm_map_offset_t)
		    phystokv((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(src_page)
		        << PAGE_SHIFT);
		dst_vaddr = (vm_map_offset_t)
		    phystokv((pmap_paddr_t)dst_pnum << PAGE_SHIFT);

		src_page_object = VM_PAGE_OBJECT(src_page);

		/*
		 * Validate the original page...
		 */
		if (src_page_object->code_signed) {
			vm_page_validate_cs_mapped(
				src_page, PAGE_SIZE, 0,
				(const void *) src_vaddr);
		}
		/*
		 * ... and transfer the results to the destination page.
		 */
		UPL_SET_CS_VALIDATED(upl_pl, cur_offset / PAGE_SIZE,
		    src_page->vmp_cs_validated);
		UPL_SET_CS_TAINTED(upl_pl, cur_offset / PAGE_SIZE,
		    src_page->vmp_cs_tainted);
		UPL_SET_CS_NX(upl_pl, cur_offset / PAGE_SIZE,
		    src_page->vmp_cs_nx);

		/*
		 * page_decrypt() might access a mapped file, so let's release
		 * the object lock for the source page to avoid a potential
		 * deadlock.  The source page is kept busy and we have a
		 * "paging_in_progress" reference on its object, so it's safe
		 * to unlock the object here.
		 */
		assert(src_page->vmp_busy);
		assert(src_page_object->paging_in_progress > 0);
		vm_object_unlock(src_page_object);

		/*
		 * Decrypt the encrypted contents of the source page
		 * into the destination page.
		 */
		for (offset_in_page = 0;
		    offset_in_page < PAGE_SIZE;
		    offset_in_page += 4096) {
			if (offset + cur_offset + offset_in_page <
			    pager->crypto_start ||
			    offset + cur_offset + offset_in_page >=
			    pager->crypto_end) {
				/* not encrypted: just copy */
				bcopy((const char *)(src_vaddr +
				    offset_in_page),
				    (char *)(dst_vaddr + offset_in_page),
				    4096);

				if (apple_protect_pager_data_request_debug) {
					printf("apple_protect_data_request"
					    "(%p,0x%llx+0x%llx+0x%04llx): "
					    "out of crypto range "
					    "[0x%llx:0x%llx]: "
					    "COPY [0x%016llx 0x%016llx] "
					    "code_signed=%d "
					    "cs_validated=%d "
					    "cs_tainted=%d "
					    "cs_nx=%d\n",
					    pager,
					    offset,
					    (uint64_t) cur_offset,
					    (uint64_t) offset_in_page,
					    pager->crypto_start,
					    pager->crypto_end,
					    *(uint64_t *)(dst_vaddr +
					    offset_in_page),
					    *(uint64_t *)(dst_vaddr +
					    offset_in_page + 8),
					    src_page_object->code_signed,
					    src_page->vmp_cs_validated,
					    src_page->vmp_cs_tainted,
					    src_page->vmp_cs_nx);
				}
				ret = 0;
				continue;
			}
			ret = pager->crypt_info->page_decrypt(
				(const void *)(src_vaddr + offset_in_page),
				(void *)(dst_vaddr + offset_in_page),
				((pager->crypto_backing_offset -
				pager->crypto_start) +   /* XXX ? */
				offset +
				cur_offset +
				offset_in_page),
				pager->crypt_info->crypt_ops);

			if (apple_protect_pager_data_request_debug) {
				printf("apple_protect_data_request"
				    "(%p,0x%llx+0x%llx+0x%04llx): "
				    "in crypto range [0x%llx:0x%llx]: "
				    "DECRYPT offset 0x%llx="
				    "(0x%llx-0x%llx+0x%llx+0x%llx+0x%04llx)"
				    "[0x%016llx 0x%016llx] "
				    "code_signed=%d "
				    "cs_validated=%d "
				    "cs_tainted=%d "
				    "cs_nx=%d "
				    "ret=0x%x\n",
				    pager,
				    offset,
				    (uint64_t) cur_offset,
				    (uint64_t) offset_in_page,
				    pager->crypto_start, pager->crypto_end,
				    ((pager->crypto_backing_offset -
				    pager->crypto_start) +
				    offset +
				    cur_offset +
				    offset_in_page),
				    pager->crypto_backing_offset,
				    pager->crypto_start,
				    offset,
				    (uint64_t) cur_offset,
				    (uint64_t) offset_in_page,
				    *(uint64_t *)(dst_vaddr + offset_in_page),
				    *(uint64_t *)(dst_vaddr + offset_in_page + 8),
				    src_page_object->code_signed,
				    src_page->vmp_cs_validated,
				    src_page->vmp_cs_tainted,
				    src_page->vmp_cs_nx,
				    ret);
			}
			if (ret) {
				break;
			}
		}
		if (ret) {
			/*
			 * Decryption failed.  Abort the fault.
			 */
			retval = KERN_ABORTED;
		}

		assert(VM_PAGE_OBJECT(src_page) == src_page_object);
		assert(src_page->vmp_busy);
		assert(src_page_object->paging_in_progress > 0);
		vm_object_lock(src_page_object);

		/*
		 * Cleanup the result of vm_fault_page() of the source page.
		 */
		vm_page_wakeup_done(src_page_object, src_page);
		src_page = VM_PAGE_NULL;
		vm_object_paging_end(src_page_object);
		vm_object_unlock(src_page_object);

		if (top_page != VM_PAGE_NULL) {
			assert(VM_PAGE_OBJECT(top_page) == src_top_object);
			vm_object_lock(src_top_object);
			VM_PAGE_FREE(top_page);
			vm_object_paging_end(src_top_object);
			vm_object_unlock(src_top_object);
		}
	}

done:
	if (upl != NULL) {
		/* clean up the UPL */

		/*
		 * The pages are currently dirty because we've just been
		 * writing on them, but as far as we're concerned, they're
		 * clean since they contain their "original" contents as
		 * provided by us, the pager.
		 * Tell the UPL to mark them "clean".
		 */
		upl_clear_dirty(upl, TRUE);

		/* abort or commit the UPL */
		if (retval != KERN_SUCCESS) {
			upl_abort(upl, 0);
			if (retval == KERN_ABORTED) {
				wait_result_t   wait_result;

				/*
				 * We aborted the fault and did not provide
				 * any contents for the requested pages but
				 * the pages themselves are not invalid, so
				 * let's return success and let the caller
				 * retry the fault, in case it might succeed
				 * later (when the decryption code is up and
				 * running in the kernel, for example).
				 */
				retval = KERN_SUCCESS;
				/*
				 * Wait a little bit first to avoid using
				 * too much CPU time retrying and failing
				 * the same fault over and over again.
				 */
				wait_result = assert_wait_timeout(
					(event_t) apple_protect_pager_data_request,
					THREAD_UNINT,
					10000,  /* 10ms */
					NSEC_PER_USEC);
				assert(wait_result == THREAD_WAITING);
				wait_result = thread_block(THREAD_CONTINUE_NULL);
				assert(wait_result == THREAD_TIMED_OUT);
			}
		} else {
			boolean_t empty;
			assertf(page_aligned(upl->u_offset) && page_aligned(upl->u_size),
			    "upl %p offset 0x%llx size 0x%x",
			    upl, upl->u_offset, upl->u_size);
			upl_commit_range(upl, 0, upl->u_size,
			    UPL_COMMIT_CS_VALIDATED | UPL_COMMIT_WRITTEN_BY_KERNEL,
			    upl_pl, pl_count, &empty);
		}

		/* and deallocate the UPL */
		upl_deallocate(upl);
		upl = NULL;
	}
	if (src_top_object != VM_OBJECT_NULL) {
		vm_object_deallocate(src_top_object);
	}
	return retval;
}

/*
 * apple_protect_pager_reference()
 *
 * Get a reference on this memory object.
 * For external usage only.  Assumes that the initial reference count is not 0,
 * i.e one should not "revive" a dead pager this way.
 */
void
apple_protect_pager_reference(
	memory_object_t         mem_obj)
{
	apple_protect_pager_t   pager;

	pager = apple_protect_pager_lookup(mem_obj);

	lck_mtx_lock(&apple_protect_pager_lock);
	os_ref_retain_locked_raw(&pager->ap_pgr_hdr_ref, NULL);
	lck_mtx_unlock(&apple_protect_pager_lock);
}


/*
 * apple_protect_pager_dequeue:
 *
 * Removes a pager from the list of pagers.
 *
 * The caller must hold "apple_protect_pager_lock".
 */
void
apple_protect_pager_dequeue(
	apple_protect_pager_t pager)
{
	assert(!pager->is_mapped);

	queue_remove(&apple_protect_pager_queue,
	    pager,
	    apple_protect_pager_t,
	    pager_queue);
	pager->pager_queue.next = NULL;
	pager->pager_queue.prev = NULL;

	apple_protect_pager_count--;
}

/*
 * apple_protect_pager_terminate_internal:
 *
 * Trigger the asynchronous termination of the memory object associated
 * with this pager.
 * When the memory object is terminated, there will be one more call
 * to memory_object_deallocate() (i.e. apple_protect_pager_deallocate())
 * to finish the clean up.
 *
 * "apple_protect_pager_lock" should not be held by the caller.
 * We don't need the lock because the pager has already been removed from
 * the pagers' list and is now ours exclusively.
 */
void
apple_protect_pager_terminate_internal(
	apple_protect_pager_t pager)
{
	assert(pager->is_ready);
	assert(!pager->is_mapped);

	if (pager->backing_object != VM_OBJECT_NULL) {
		vm_object_deallocate(pager->backing_object);
		pager->backing_object = VM_OBJECT_NULL;
	}

	/* one less pager using this "pager_crypt_info" */
#if CRYPT_INFO_DEBUG
	printf("CRYPT_INFO %s: deallocate %p ref %d\n",
	    __FUNCTION__,
	    pager->crypt_info,
	    pager->crypt_info->crypt_refcnt);
#endif /* CRYPT_INFO_DEBUG */
	crypt_info_deallocate(pager->crypt_info);
	pager->crypt_info = NULL;

	/* trigger the destruction of the memory object */
	memory_object_destroy(pager->ap_pgr_hdr.mo_control, VM_OBJECT_DESTROY_PAGER);
}

/*
 * apple_protect_pager_deallocate_internal()
 *
 * Release a reference on this pager and free it when the last
 * reference goes away.
 * Can be called with apple_protect_pager_lock held or not but always returns
 * with it unlocked.
 */
void
apple_protect_pager_deallocate_internal(
	apple_protect_pager_t   pager,
	boolean_t               locked)
{
	boolean_t       needs_trimming;
	unsigned int    count_unmapped;
	os_ref_count_t  ref_count;

	if (!locked) {
		lck_mtx_lock(&apple_protect_pager_lock);
	}

	count_unmapped = (apple_protect_pager_count -
	    apple_protect_pager_count_mapped);
	if (count_unmapped > apple_protect_pager_cache_limit) {
		/* we have too many unmapped pagers:  trim some */
		needs_trimming = TRUE;
	} else {
		needs_trimming = FALSE;
	}

	/* drop a reference on this pager */
	ref_count = os_ref_release_locked_raw(&pager->ap_pgr_hdr_ref, NULL);

	if (ref_count == 1) {
		/*
		 * Only the "named" reference is left, which means that
		 * no one is really holding on to this pager anymore.
		 * Terminate it.
		 */
		apple_protect_pager_dequeue(pager);
		/* the pager is all ours: no need for the lock now */
		lck_mtx_unlock(&apple_protect_pager_lock);
		apple_protect_pager_terminate_internal(pager);
	} else if (ref_count == 0) {
		/*
		 * Dropped the existence reference;  the memory object has
		 * been terminated.  Do some final cleanup and release the
		 * pager structure.
		 */
		lck_mtx_unlock(&apple_protect_pager_lock);
		if (pager->ap_pgr_hdr.mo_control != MEMORY_OBJECT_CONTROL_NULL) {
			memory_object_control_deallocate(pager->ap_pgr_hdr.mo_control);
			pager->ap_pgr_hdr.mo_control = MEMORY_OBJECT_CONTROL_NULL;
		}
		kfree_type(struct apple_protect_pager, pager);
		pager = APPLE_PROTECT_PAGER_NULL;
	} else {
		/* there are still plenty of references:  keep going... */
		lck_mtx_unlock(&apple_protect_pager_lock);
	}

	if (needs_trimming) {
		apple_protect_pager_trim();
	}
	/* caution: lock is not held on return... */
}

/*
 * apple_protect_pager_deallocate()
 *
 * Release a reference on this pager and free it when the last
 * reference goes away.
 */
void
apple_protect_pager_deallocate(
	memory_object_t         mem_obj)
{
	apple_protect_pager_t   pager;

	PAGER_DEBUG(PAGER_ALL, ("apple_protect_pager_deallocate: %p\n", mem_obj));
	pager = apple_protect_pager_lookup(mem_obj);
	apple_protect_pager_deallocate_internal(pager, FALSE);
}

/*
 *
 */
kern_return_t
apple_protect_pager_terminate(
#if !DEBUG
	__unused
#endif
	memory_object_t mem_obj)
{
	PAGER_DEBUG(PAGER_ALL, ("apple_protect_pager_terminate: %p\n", mem_obj));

	return KERN_SUCCESS;
}

/*
 * apple_protect_pager_map()
 *
 * This allows VM to let us, the EMM, know that this memory object
 * is currently mapped one or more times.  This is called by VM each time
 * the memory object gets mapped and we take one extra reference on the
 * memory object to account for all its mappings.
 */
kern_return_t
apple_protect_pager_map(
	memory_object_t         mem_obj,
	__unused vm_prot_t      prot)
{
	apple_protect_pager_t   pager;

	PAGER_DEBUG(PAGER_ALL, ("apple_protect_pager_map: %p\n", mem_obj));

	pager = apple_protect_pager_lookup(mem_obj);

	lck_mtx_lock(&apple_protect_pager_lock);
	assert(pager->is_ready);
	assert(os_ref_get_count_raw(&pager->ap_pgr_hdr_ref) > 0); /* pager is alive */
	if (pager->is_mapped == FALSE) {
		/*
		 * First mapping of this pager:  take an extra reference
		 * that will remain until all the mappings of this pager
		 * are removed.
		 */
		pager->is_mapped = TRUE;
		os_ref_retain_locked_raw(&pager->ap_pgr_hdr_ref, NULL);
		apple_protect_pager_count_mapped++;
	}
	lck_mtx_unlock(&apple_protect_pager_lock);

	return KERN_SUCCESS;
}

/*
 * apple_protect_pager_last_unmap()
 *
 * This is called by VM when this memory object is no longer mapped anywhere.
 */
kern_return_t
apple_protect_pager_last_unmap(
	memory_object_t         mem_obj)
{
	apple_protect_pager_t   pager;
	unsigned int            count_unmapped;

	PAGER_DEBUG(PAGER_ALL,
	    ("apple_protect_pager_last_unmap: %p\n", mem_obj));

	pager = apple_protect_pager_lookup(mem_obj);

	lck_mtx_lock(&apple_protect_pager_lock);
	if (pager->is_mapped) {
		/*
		 * All the mappings are gone, so let go of the one extra
		 * reference that represents all the mappings of this pager.
		 */
		apple_protect_pager_count_mapped--;
		count_unmapped = (apple_protect_pager_count -
		    apple_protect_pager_count_mapped);
		if (count_unmapped > apple_protect_pager_count_unmapped_max) {
			apple_protect_pager_count_unmapped_max = count_unmapped;
		}
		pager->is_mapped = FALSE;
		apple_protect_pager_deallocate_internal(pager, TRUE);
		/* caution: deallocate_internal() released the lock ! */
	} else {
		lck_mtx_unlock(&apple_protect_pager_lock);
	}

	return KERN_SUCCESS;
}

boolean_t
apple_protect_pager_backing_object(
	memory_object_t mem_obj,
	memory_object_offset_t offset,
	vm_object_t *backing_object,
	vm_object_offset_t *backing_offset)
{
	apple_protect_pager_t   pager;

	PAGER_DEBUG(PAGER_ALL,
	    ("apple_protect_pager_backing_object: %p\n", mem_obj));

	pager = apple_protect_pager_lookup(mem_obj);

	*backing_object = pager->backing_object;
	*backing_offset = pager->backing_offset + offset;

	return TRUE;
}

/*
 *
 */
apple_protect_pager_t
apple_protect_pager_lookup(
	memory_object_t  mem_obj)
{
	apple_protect_pager_t   pager;

	assert(mem_obj->mo_pager_ops == &apple_protect_pager_ops);
	pager = (apple_protect_pager_t)(uintptr_t) mem_obj;
	assert(os_ref_get_count_raw(&pager->ap_pgr_hdr_ref) > 0);
	return pager;
}

apple_protect_pager_t
apple_protect_pager_create(
	vm_object_t             backing_object,
	vm_object_offset_t      backing_offset,
	vm_object_offset_t      crypto_backing_offset,
	struct pager_crypt_info *crypt_info,
	vm_object_offset_t      crypto_start,
	vm_object_offset_t      crypto_end,
	boolean_t               cache_pager)
{
	apple_protect_pager_t   pager, pager2;
	memory_object_control_t control;
	kern_return_t           kr;
	struct pager_crypt_info *old_crypt_info;

	pager = kalloc_type(struct apple_protect_pager, Z_WAITOK | Z_NOFAIL);

	/*
	 * The vm_map call takes both named entry ports and raw memory
	 * objects in the same parameter.  We need to make sure that
	 * vm_map does not see this object as a named entry port.  So,
	 * we reserve the first word in the object for a fake object type
	 * setting - that will tell vm_map to use it as a memory object.
	 */
	pager->ap_pgr_hdr.mo_ikot = IKOT_MEMORY_OBJECT;
	pager->ap_pgr_hdr.mo_pager_ops = &apple_protect_pager_ops;
	pager->ap_pgr_hdr.mo_control = MEMORY_OBJECT_CONTROL_NULL;
	pager->ap_pgr_hdr.mo_last_unmap_ctid = 0;

	pager->is_ready = FALSE;/* not ready until it has a "name" */
	/* one reference for the caller */
	os_ref_init_count_raw(&pager->ap_pgr_hdr_ref, NULL, 1);
	pager->is_mapped = FALSE;
	if (cache_pager) {
		/* extra reference for the cache */
		os_ref_retain_locked_raw(&pager->ap_pgr_hdr_ref, NULL);
		pager->is_cached = true;
	} else {
		pager->is_cached = false;
	}
	pager->backing_object = backing_object;
	pager->backing_offset = backing_offset;
	pager->crypto_backing_offset = crypto_backing_offset;
	pager->crypto_start = crypto_start;
	pager->crypto_end = crypto_end;
	pager->crypt_info = crypt_info; /* allocated by caller */

#if CRYPT_INFO_DEBUG
	printf("CRYPT_INFO %s: crypt_info %p [%p,%p,%p,%d]\n",
	    __FUNCTION__,
	    crypt_info,
	    crypt_info->page_decrypt,
	    crypt_info->crypt_end,
	    crypt_info->crypt_ops,
	    crypt_info->crypt_refcnt);
#endif /* CRYPT_INFO_DEBUG */

	vm_object_reference(backing_object);

	old_crypt_info = NULL;

	lck_mtx_lock(&apple_protect_pager_lock);
	/* see if anyone raced us to create a pager for the same object */
	queue_iterate(&apple_protect_pager_queue,
	    pager2,
	    apple_protect_pager_t,
	    pager_queue) {
		if ((pager2->crypt_info->page_decrypt !=
		    crypt_info->page_decrypt) ||
		    (pager2->crypt_info->crypt_end !=
		    crypt_info->crypt_end) ||
		    (pager2->crypt_info->crypt_ops !=
		    crypt_info->crypt_ops)) {
			/* crypt_info contents do not match: next pager */
			continue;
		}

		/* found a match for crypt_info ... */
		if (old_crypt_info) {
			/* ... already switched to that crypt_info */
			assert(old_crypt_info == pager2->crypt_info);
		} else if (pager2->crypt_info != crypt_info) {
			/* ... switch to that pager's crypt_info */
#if CRYPT_INFO_DEBUG
			printf("CRYPT_INFO %s: reference %p ref %d "
			    "(create match)\n",
			    __FUNCTION__,
			    pager2->crypt_info,
			    pager2->crypt_info->crypt_refcnt);
#endif /* CRYPT_INFO_DEBUG */
			old_crypt_info = pager2->crypt_info;
			crypt_info_reference(old_crypt_info);
			pager->crypt_info = old_crypt_info;
		}

		if (pager2->backing_object == backing_object &&
		    pager2->backing_offset == backing_offset &&
		    pager2->crypto_backing_offset == crypto_backing_offset &&
		    pager2->crypto_start == crypto_start &&
		    pager2->crypto_end == crypto_end) {
			/* full match: use that pager */
			break;
		}
	}
	if (!queue_end(&apple_protect_pager_queue,
	    (queue_entry_t) pager2)) {
		/* we lost the race, down with the loser... */
		lck_mtx_unlock(&apple_protect_pager_lock);
		vm_object_deallocate(pager->backing_object);
		pager->backing_object = VM_OBJECT_NULL;
#if CRYPT_INFO_DEBUG
		printf("CRYPT_INFO %s: %p ref %d (create pager match)\n",
		    __FUNCTION__,
		    pager->crypt_info,
		    pager->crypt_info->crypt_refcnt);
#endif /* CRYPT_INFO_DEBUG */
		crypt_info_deallocate(pager->crypt_info);
		pager->crypt_info = NULL;
		kfree_type(struct apple_protect_pager, pager);
		/* ... and go with the winner */
		pager = pager2;
		/* let the winner make sure the pager gets ready */
		return pager;
	}

	/* enter new pager at the head of our list of pagers */
	queue_enter_first(&apple_protect_pager_queue,
	    pager,
	    apple_protect_pager_t,
	    pager_queue);
	apple_protect_pager_count++;
	if (apple_protect_pager_count > apple_protect_pager_count_max) {
		apple_protect_pager_count_max = apple_protect_pager_count;
	}
	lck_mtx_unlock(&apple_protect_pager_lock);

	kr = memory_object_create_named((memory_object_t) pager,
	    0,
	    &control);
	assert(kr == KERN_SUCCESS);

	memory_object_mark_trusted(control);

	lck_mtx_lock(&apple_protect_pager_lock);
	/* the new pager is now ready to be used */
	pager->is_ready = TRUE;
	lck_mtx_unlock(&apple_protect_pager_lock);

	/* wakeup anyone waiting for this pager to be ready */
	thread_wakeup(&pager->is_ready);

	if (old_crypt_info != NULL &&
	    old_crypt_info != crypt_info) {
		/* we re-used an old crypt_info instead of using our new one */
#if CRYPT_INFO_DEBUG
		printf("CRYPT_INFO %s: deallocate %p ref %d "
		    "(create used old)\n",
		    __FUNCTION__,
		    crypt_info,
		    crypt_info->crypt_refcnt);
#endif /* CRYPT_INFO_DEBUG */
		crypt_info_deallocate(crypt_info);
		crypt_info = NULL;
	}

	return pager;
}

/*
 * apple_protect_pager_setup()
 *
 * Provide the caller with a memory object backed by the provided
 * "backing_object" VM object.  If such a memory object already exists,
 * re-use it, otherwise create a new memory object.
 */
memory_object_t
apple_protect_pager_setup(
	vm_object_t             backing_object,
	vm_object_offset_t      backing_offset,
	vm_object_offset_t      crypto_backing_offset,
	struct pager_crypt_info *crypt_info,
	vm_object_offset_t      crypto_start,
	vm_object_offset_t      crypto_end,
	boolean_t               cache_pager)
{
	apple_protect_pager_t   pager;
	struct pager_crypt_info *old_crypt_info, *new_crypt_info;

#if CRYPT_INFO_DEBUG
	printf("CRYPT_INFO %s: crypt_info=%p [%p,%p,%p,%d]\n",
	    __FUNCTION__,
	    crypt_info,
	    crypt_info->page_decrypt,
	    crypt_info->crypt_end,
	    crypt_info->crypt_ops,
	    crypt_info->crypt_refcnt);
#endif /* CRYPT_INFO_DEBUG */

	old_crypt_info = NULL;

	lck_mtx_lock(&apple_protect_pager_lock);

	queue_iterate(&apple_protect_pager_queue,
	    pager,
	    apple_protect_pager_t,
	    pager_queue) {
		if ((pager->crypt_info->page_decrypt !=
		    crypt_info->page_decrypt) ||
		    (pager->crypt_info->crypt_end !=
		    crypt_info->crypt_end) ||
		    (pager->crypt_info->crypt_ops !=
		    crypt_info->crypt_ops)) {
			/* no match for "crypt_info": next pager */
			continue;
		}
		/* found a match for crypt_info ... */
		if (old_crypt_info) {
			/* ... already switched to that crypt_info */
			assert(old_crypt_info == pager->crypt_info);
		} else {
			/* ... switch to that pager's crypt_info */
			old_crypt_info = pager->crypt_info;
#if CRYPT_INFO_DEBUG
			printf("CRYPT_INFO %s: "
			    "switching crypt_info from %p [%p,%p,%p,%d] "
			    "to %p [%p,%p,%p,%d] from pager %p\n",
			    __FUNCTION__,
			    crypt_info,
			    crypt_info->page_decrypt,
			    crypt_info->crypt_end,
			    crypt_info->crypt_ops,
			    crypt_info->crypt_refcnt,
			    old_crypt_info,
			    old_crypt_info->page_decrypt,
			    old_crypt_info->crypt_end,
			    old_crypt_info->crypt_ops,
			    old_crypt_info->crypt_refcnt,
			    pager);
			printf("CRYPT_INFO %s: %p ref %d (setup match)\n",
			    __FUNCTION__,
			    pager->crypt_info,
			    pager->crypt_info->crypt_refcnt);
#endif /* CRYPT_INFO_DEBUG */
			crypt_info_reference(pager->crypt_info);
		}

		if (pager->backing_object == backing_object &&
		    pager->backing_offset == backing_offset &&
		    pager->crypto_backing_offset == crypto_backing_offset &&
		    pager->crypto_start == crypto_start &&
		    pager->crypto_end == crypto_end) {
			/* full match: use that pager! */
			assert(old_crypt_info == pager->crypt_info);
			assert(old_crypt_info->crypt_refcnt > 1);
#if CRYPT_INFO_DEBUG
			printf("CRYPT_INFO %s: "
			    "pager match with %p crypt_info %p\n",
			    __FUNCTION__,
			    pager,
			    pager->crypt_info);
			printf("CRYPT_INFO %s: deallocate %p ref %d "
			    "(pager match)\n",
			    __FUNCTION__,
			    old_crypt_info,
			    old_crypt_info->crypt_refcnt);
#endif /* CRYPT_INFO_DEBUG */
			/* release the extra ref on crypt_info we got above */
			crypt_info_deallocate(old_crypt_info);
			assert(old_crypt_info->crypt_refcnt > 0);
			/* give extra reference on pager to the caller */
			os_ref_retain_locked_raw(&pager->ap_pgr_hdr_ref, NULL);
			break;
		}
	}
	if (queue_end(&apple_protect_pager_queue,
	    (queue_entry_t) pager)) {
		lck_mtx_unlock(&apple_protect_pager_lock);
		/* no existing pager for this backing object */
		pager = APPLE_PROTECT_PAGER_NULL;
		if (old_crypt_info) {
			/* use this old crypt_info for new pager */
			new_crypt_info = old_crypt_info;
#if CRYPT_INFO_DEBUG
			printf("CRYPT_INFO %s: "
			    "will use old_crypt_info %p for new pager\n",
			    __FUNCTION__,
			    old_crypt_info);
#endif /* CRYPT_INFO_DEBUG */
		} else {
			/* allocate a new crypt_info for new pager */
			new_crypt_info = kalloc_type(struct pager_crypt_info, Z_WAITOK);
			*new_crypt_info = *crypt_info;
			new_crypt_info->crypt_refcnt = 1;
#if CRYPT_INFO_DEBUG
			printf("CRYPT_INFO %s: "
			    "will use new_crypt_info %p for new pager\n",
			    __FUNCTION__,
			    new_crypt_info);
#endif /* CRYPT_INFO_DEBUG */
		}
		if (new_crypt_info == NULL) {
			/* can't create new pager without a crypt_info */
		} else {
			/* create new pager */
			pager = apple_protect_pager_create(
				backing_object,
				backing_offset,
				crypto_backing_offset,
				new_crypt_info,
				crypto_start,
				crypto_end,
				cache_pager);
		}
		if (pager == APPLE_PROTECT_PAGER_NULL) {
			/* could not create a new pager */
			if (new_crypt_info == old_crypt_info) {
				/* release extra reference on old_crypt_info */
#if CRYPT_INFO_DEBUG
				printf("CRYPT_INFO %s: deallocate %p ref %d "
				    "(create fail old_crypt_info)\n",
				    __FUNCTION__,
				    old_crypt_info,
				    old_crypt_info->crypt_refcnt);
#endif /* CRYPT_INFO_DEBUG */
				crypt_info_deallocate(old_crypt_info);
				old_crypt_info = NULL;
			} else {
				/* release unused new_crypt_info */
				assert(new_crypt_info->crypt_refcnt == 1);
#if CRYPT_INFO_DEBUG
				printf("CRYPT_INFO %s: deallocate %p ref %d "
				    "(create fail new_crypt_info)\n",
				    __FUNCTION__,
				    new_crypt_info,
				    new_crypt_info->crypt_refcnt);
#endif /* CRYPT_INFO_DEBUG */
				crypt_info_deallocate(new_crypt_info);
				new_crypt_info = NULL;
			}
			return MEMORY_OBJECT_NULL;
		}
		lck_mtx_lock(&apple_protect_pager_lock);
	} else {
		assert(old_crypt_info == pager->crypt_info);
	}

	while (!pager->is_ready) {
		lck_mtx_sleep(&apple_protect_pager_lock,
		    LCK_SLEEP_DEFAULT,
		    &pager->is_ready,
		    THREAD_UNINT);
	}
	lck_mtx_unlock(&apple_protect_pager_lock);

	return (memory_object_t) pager;
}

void
apple_protect_pager_trim(void)
{
	apple_protect_pager_t   pager, prev_pager;
	queue_head_t            trim_queue;
	unsigned int            num_trim;
	unsigned int            count_unmapped;

	lck_mtx_lock(&apple_protect_pager_lock);

	/*
	 * We have too many pagers, try and trim some unused ones,
	 * starting with the oldest pager at the end of the queue.
	 */
	queue_init(&trim_queue);
	num_trim = 0;

	for (pager = (apple_protect_pager_t)
	    queue_last(&apple_protect_pager_queue);
	    !queue_end(&apple_protect_pager_queue,
	    (queue_entry_t) pager);
	    pager = prev_pager) {
		/* get prev elt before we dequeue */
		prev_pager = (apple_protect_pager_t)
		    queue_prev(&pager->pager_queue);

		if (pager->is_cached &&
		    os_ref_get_count_raw(&pager->ap_pgr_hdr_ref) == 2 &&
		    pager->is_ready &&
		    !pager->is_mapped) {
			/* this pager can be trimmed */
			num_trim++;
			/* remove this pager from the main list ... */
			apple_protect_pager_dequeue(pager);
			/* ... and add it to our trim queue */
			queue_enter_first(&trim_queue,
			    pager,
			    apple_protect_pager_t,
			    pager_queue);

			count_unmapped = (apple_protect_pager_count -
			    apple_protect_pager_count_mapped);
			if (count_unmapped <= apple_protect_pager_cache_limit) {
				/* we have enough pagers to trim */
				break;
			}
		}
	}
	if (num_trim > apple_protect_pager_num_trim_max) {
		apple_protect_pager_num_trim_max = num_trim;
	}
	apple_protect_pager_num_trim_total += num_trim;

	lck_mtx_unlock(&apple_protect_pager_lock);

	/* terminate the trimmed pagers */
	while (!queue_empty(&trim_queue)) {
		queue_remove_first(&trim_queue,
		    pager,
		    apple_protect_pager_t,
		    pager_queue);
		assert(pager->is_cached);
		pager->is_cached = false;
		pager->pager_queue.next = NULL;
		pager->pager_queue.prev = NULL;
		/*
		 * We can't call deallocate_internal() because the pager
		 * has already been dequeued, but we still need to remove
		 * a reference.
		 */
		os_ref_count_t __assert_only count;
		count = os_ref_release_locked_raw(&pager->ap_pgr_hdr_ref, NULL);
		assert(count == 1);
		apple_protect_pager_terminate_internal(pager);
	}
}


void
crypt_info_reference(
	struct pager_crypt_info *crypt_info)
{
	assert(crypt_info->crypt_refcnt != 0);
#if CRYPT_INFO_DEBUG
	printf("CRYPT_INFO %s: %p ref %d -> %d\n",
	    __FUNCTION__,
	    crypt_info,
	    crypt_info->crypt_refcnt,
	    crypt_info->crypt_refcnt + 1);
#endif /* CRYPT_INFO_DEBUG */
	OSAddAtomic(+1, &crypt_info->crypt_refcnt);
}

void
crypt_info_deallocate(
	struct pager_crypt_info *crypt_info)
{
#if CRYPT_INFO_DEBUG
	printf("CRYPT_INFO %s: %p ref %d -> %d\n",
	    __FUNCTION__,
	    crypt_info,
	    crypt_info->crypt_refcnt,
	    crypt_info->crypt_refcnt - 1);
#endif /* CRYPT_INFO_DEBUG */
	OSAddAtomic(-1, &crypt_info->crypt_refcnt);
	if (crypt_info->crypt_refcnt == 0) {
		/* deallocate any crypt module data */
		if (crypt_info->crypt_end) {
			crypt_info->crypt_end(crypt_info->crypt_ops);
			crypt_info->crypt_end = NULL;
		}
#if CRYPT_INFO_DEBUG
		printf("CRYPT_INFO %s: freeing %p\n",
		    __FUNCTION__,
		    crypt_info);
#endif /* CRYPT_INFO_DEBUG */
		kfree_type(struct pager_crypt_info, crypt_info);
	}
}

static uint64_t
apple_protect_pager_purge(
	apple_protect_pager_t pager)
{
	uint64_t pages_purged;
	vm_object_t object;

	pages_purged = 0;
	object = memory_object_to_vm_object((memory_object_t) pager);
	assert(object != VM_OBJECT_NULL);
	vm_object_lock(object);
	pages_purged = object->resident_page_count;
	vm_object_reap_pages(object, REAP_DATA_FLUSH_CLEAN);
	pages_purged -= object->resident_page_count;
//	printf("     %s:%d pager %p object %p purged %llu left %d\n", __FUNCTION__, __LINE__, pager, object, pages_purged, object->resident_page_count);
	vm_object_unlock(object);
	return pages_purged;
}

uint64_t
apple_protect_pager_purge_all(void)
{
	uint64_t pages_purged;
	apple_protect_pager_t pager;

	pages_purged = 0;
	lck_mtx_lock(&apple_protect_pager_lock);
	queue_iterate(&apple_protect_pager_queue, pager, apple_protect_pager_t, pager_queue) {
		pages_purged += apple_protect_pager_purge(pager);
	}
	lck_mtx_unlock(&apple_protect_pager_lock);
#if DEVELOPMENT || DEBUG
	printf("   %s:%d pages purged: %llu\n", __FUNCTION__, __LINE__, pages_purged);
#endif /* DEVELOPMENT || DEBUG */
	return pages_purged;
}