This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
vm_shared_region.c
3489 lines (3104 loc) · 105 KB
/
vm_shared_region.c
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
/*
* Copyright (c) 2007-2020 Apple Inc. All rights reserved.
*
* @APPLE_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. 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_LICENSE_HEADER_END@
*/
/*
* Shared region (... and comm page)
*
* This file handles the VM shared region and comm page.
*
*/
/*
* SHARED REGIONS
* --------------
*
* A shared region is a submap that contains the most common system shared
* libraries for a given environment which is defined by:
* - cpu-type
* - 64-bitness
* - root directory
* - Team ID - when we have pointer authentication.
*
* The point of a shared region is to reduce the setup overhead when exec'ing
* a new process. A shared region uses a shared VM submap that gets mapped
* automatically at exec() time, see vm_map_exec(). The first process of a given
* environment sets up the shared region and all further processes in that
* environment can re-use that shared region without having to re-create
* the same mappings in their VM map. All they need is contained in the shared
* region.
*
* The region can also share a pmap (mostly for read-only parts but also for the
* initial version of some writable parts), which gets "nested" into the
* process's pmap. This reduces the number of soft faults: once one process
* brings in a page in the shared region, all the other processes can access
* it without having to enter it in their own pmap.
*
* When a process is being exec'ed, vm_map_exec() calls vm_shared_region_enter()
* to map the appropriate shared region in the process's address space.
* We look up the appropriate shared region for the process's environment.
* If we can't find one, we create a new (empty) one and add it to the list.
* Otherwise, we just take an extra reference on the shared region we found.
*
* The "dyld" runtime, mapped into the process's address space at exec() time,
* will then use the shared_region_check_np() and shared_region_map_and_slide_np()
* system calls to validate and/or populate the shared region with the
* appropriate dyld_shared_cache file.
*
* The shared region is inherited on fork() and the child simply takes an
* extra reference on its parent's shared region.
*
* When the task terminates, we release the reference on its shared region.
* When the last reference is released, we destroy the shared region.
*
* After a chroot(), the calling process keeps using its original shared region,
* since that's what was mapped when it was started. But its children
* will use a different shared region, because they need to use the shared
* cache that's relative to the new root directory.
*/
/*
* COMM PAGE
*
* A "comm page" is an area of memory that is populated by the kernel with
* the appropriate platform-specific version of some commonly used code.
* There is one "comm page" per platform (cpu-type, 64-bitness) but only
* for the native cpu-type. No need to overly optimize translated code
* for hardware that is not really there !
*
* The comm pages are created and populated at boot time.
*
* The appropriate comm page is mapped into a process's address space
* at exec() time, in vm_map_exec(). It is then inherited on fork().
*
* The comm page is shared between the kernel and all applications of
* a given platform. Only the kernel can modify it.
*
* Applications just branch to fixed addresses in the comm page and find
* the right version of the code for the platform. There is also some
* data provided and updated by the kernel for processes to retrieve easily
* without having to do a system call.
*/
#include <debug.h>
#include <kern/ipc_tt.h>
#include <kern/kalloc.h>
#include <kern/thread_call.h>
#include <mach/mach_vm.h>
#include <vm/vm_map.h>
#include <vm/vm_shared_region.h>
#include <vm/vm_protos.h>
#include <machine/commpage.h>
#include <machine/cpu_capabilities.h>
#include <sys/random.h>
#if defined (__arm__) || defined(__arm64__)
#include <arm/cpu_data_internal.h>
#include <arm/misc_protos.h>
#endif
/*
* the following codes are used in the subclass
* of the DBG_MACH_SHAREDREGION class
*/
#define PROCESS_SHARED_CACHE_LAYOUT 0x00
#if __has_feature(ptrauth_calls)
#include <ptrauth.h>
#endif /* __has_feature(ptrauth_calls) */
/* "dyld" uses this to figure out what the kernel supports */
int shared_region_version = 3;
/* trace level, output is sent to the system log file */
int shared_region_trace_level = SHARED_REGION_TRACE_ERROR_LVL;
/* should local (non-chroot) shared regions persist when no task uses them ? */
int shared_region_persistence = 0; /* no by default */
/* delay in seconds before reclaiming an unused shared region */
TUNABLE_WRITEABLE(int, shared_region_destroy_delay, "vm_shared_region_destroy_delay", 120);
struct vm_shared_region *init_task_shared_region = NULL;
#ifndef CONFIG_EMBEDDED
/*
* Only one cache gets to slide on Desktop, since we can't
* tear down slide info properly today and the desktop actually
* produces lots of shared caches.
*/
boolean_t shared_region_completed_slide = FALSE;
#endif
/* this lock protects all the shared region data structures */
static LCK_GRP_DECLARE(vm_shared_region_lck_grp, "vm shared region");
static LCK_MTX_DECLARE(vm_shared_region_lock, &vm_shared_region_lck_grp);
#define vm_shared_region_lock() lck_mtx_lock(&vm_shared_region_lock)
#define vm_shared_region_unlock() lck_mtx_unlock(&vm_shared_region_lock)
#define vm_shared_region_sleep(event, interruptible) \
lck_mtx_sleep(&vm_shared_region_lock, \
LCK_SLEEP_DEFAULT, \
(event_t) (event), \
(interruptible))
/* the list of currently available shared regions (one per environment) */
queue_head_t vm_shared_region_queue = QUEUE_HEAD_INITIALIZER(vm_shared_region_queue);
int vm_shared_region_count = 0;
int vm_shared_region_peak = 0;
/*
* the number of times an event has forced the recalculation of the reslide
* shared region slide.
*/
#if __has_feature(ptrauth_calls)
int vm_shared_region_reslide_count = 0;
#endif /* __has_feature(ptrauth_calls) */
static void vm_shared_region_reference_locked(vm_shared_region_t shared_region);
static vm_shared_region_t vm_shared_region_create(
void *root_dir,
cpu_type_t cputype,
cpu_subtype_t cpu_subtype,
boolean_t is_64bit,
boolean_t reslide);
static void vm_shared_region_destroy(vm_shared_region_t shared_region);
static kern_return_t vm_shared_region_slide_sanity_check(vm_shared_region_slide_info_entry_t entry, mach_vm_size_t size);
static void vm_shared_region_timeout(thread_call_param_t param0,
thread_call_param_t param1);
static kern_return_t vm_shared_region_slide_mapping(
vm_shared_region_t sr,
user_addr_t slide_info_addr,
mach_vm_size_t slide_info_size,
mach_vm_offset_t start,
mach_vm_size_t size,
mach_vm_offset_t slid_mapping,
uint32_t slide,
memory_object_control_t,
vm_prot_t prot); /* forward */
static int __commpage_setup = 0;
#if !CONFIG_EMBEDDED
static int __system_power_source = 1; /* init to extrnal power source */
static void post_sys_powersource_internal(int i, int internal);
#endif
extern u_int32_t random(void);
/*
* Retrieve a task's shared region and grab an extra reference to
* make sure it doesn't disappear while the caller is using it.
* The caller is responsible for consuming that extra reference if
* necessary.
*/
vm_shared_region_t
vm_shared_region_get(
task_t task)
{
vm_shared_region_t shared_region;
SHARED_REGION_TRACE_DEBUG(
("shared_region: -> get(%p)\n",
(void *)VM_KERNEL_ADDRPERM(task)));
task_lock(task);
vm_shared_region_lock();
shared_region = task->shared_region;
if (shared_region) {
assert(shared_region->sr_ref_count > 0);
vm_shared_region_reference_locked(shared_region);
}
vm_shared_region_unlock();
task_unlock(task);
SHARED_REGION_TRACE_DEBUG(
("shared_region: get(%p) <- %p\n",
(void *)VM_KERNEL_ADDRPERM(task),
(void *)VM_KERNEL_ADDRPERM(shared_region)));
return shared_region;
}
/*
* Get the base address of the shared region.
* That's the address at which it needs to be mapped in the process's address
* space.
* No need to lock since this data is set when the shared region is
* created and is never modified after that. The caller must hold an extra
* reference on the shared region to prevent it from being destroyed.
*/
mach_vm_offset_t
vm_shared_region_base_address(
vm_shared_region_t shared_region)
{
SHARED_REGION_TRACE_DEBUG(
("shared_region: -> base_address(%p)\n",
(void *)VM_KERNEL_ADDRPERM(shared_region)));
assert(shared_region->sr_ref_count > 1);
SHARED_REGION_TRACE_DEBUG(
("shared_region: base_address(%p) <- 0x%llx\n",
(void *)VM_KERNEL_ADDRPERM(shared_region),
(long long)shared_region->sr_base_address));
return shared_region->sr_base_address;
}
/*
* Get the size of the shared region.
* That's the size that needs to be mapped in the process's address
* space.
* No need to lock since this data is set when the shared region is
* created and is never modified after that. The caller must hold an extra
* reference on the shared region to prevent it from being destroyed.
*/
mach_vm_size_t
vm_shared_region_size(
vm_shared_region_t shared_region)
{
SHARED_REGION_TRACE_DEBUG(
("shared_region: -> size(%p)\n",
(void *)VM_KERNEL_ADDRPERM(shared_region)));
assert(shared_region->sr_ref_count > 1);
SHARED_REGION_TRACE_DEBUG(
("shared_region: size(%p) <- 0x%llx\n",
(void *)VM_KERNEL_ADDRPERM(shared_region),
(long long)shared_region->sr_size));
return shared_region->sr_size;
}
/*
* Get the memory entry of the shared region.
* That's the "memory object" that needs to be mapped in the process's address
* space.
* No need to lock since this data is set when the shared region is
* created and is never modified after that. The caller must hold an extra
* reference on the shared region to prevent it from being destroyed.
*/
ipc_port_t
vm_shared_region_mem_entry(
vm_shared_region_t shared_region)
{
SHARED_REGION_TRACE_DEBUG(
("shared_region: -> mem_entry(%p)\n",
(void *)VM_KERNEL_ADDRPERM(shared_region)));
assert(shared_region->sr_ref_count > 1);
SHARED_REGION_TRACE_DEBUG(
("shared_region: mem_entry(%p) <- %p\n",
(void *)VM_KERNEL_ADDRPERM(shared_region),
(void *)VM_KERNEL_ADDRPERM(shared_region->sr_mem_entry)));
return shared_region->sr_mem_entry;
}
vm_map_t
vm_shared_region_vm_map(
vm_shared_region_t shared_region)
{
ipc_port_t sr_handle;
vm_named_entry_t sr_mem_entry;
vm_map_t sr_map;
SHARED_REGION_TRACE_DEBUG(
("shared_region: -> vm_map(%p)\n",
(void *)VM_KERNEL_ADDRPERM(shared_region)));
assert(shared_region->sr_ref_count > 1);
sr_handle = shared_region->sr_mem_entry;
sr_mem_entry = (vm_named_entry_t) ip_get_kobject(sr_handle);
sr_map = sr_mem_entry->backing.map;
assert(sr_mem_entry->is_sub_map);
SHARED_REGION_TRACE_DEBUG(
("shared_region: vm_map(%p) <- %p\n",
(void *)VM_KERNEL_ADDRPERM(shared_region),
(void *)VM_KERNEL_ADDRPERM(sr_map)));
return sr_map;
}
/*
* Set the shared region the process should use.
* A NULL new shared region means that we just want to release the old
* shared region.
* The caller should already have an extra reference on the new shared region
* (if any). We release a reference on the old shared region (if any).
*/
void
vm_shared_region_set(
task_t task,
vm_shared_region_t new_shared_region)
{
vm_shared_region_t old_shared_region;
SHARED_REGION_TRACE_DEBUG(
("shared_region: -> set(%p, %p)\n",
(void *)VM_KERNEL_ADDRPERM(task),
(void *)VM_KERNEL_ADDRPERM(new_shared_region)));
task_lock(task);
vm_shared_region_lock();
old_shared_region = task->shared_region;
if (new_shared_region) {
assert(new_shared_region->sr_ref_count > 0);
}
task->shared_region = new_shared_region;
vm_shared_region_unlock();
task_unlock(task);
if (old_shared_region) {
assert(old_shared_region->sr_ref_count > 0);
vm_shared_region_deallocate(old_shared_region);
}
SHARED_REGION_TRACE_DEBUG(
("shared_region: set(%p) <- old=%p new=%p\n",
(void *)VM_KERNEL_ADDRPERM(task),
(void *)VM_KERNEL_ADDRPERM(old_shared_region),
(void *)VM_KERNEL_ADDRPERM(new_shared_region)));
}
/*
* Lookup up the shared region for the desired environment.
* If none is found, create a new (empty) one.
* Grab an extra reference on the returned shared region, to make sure
* it doesn't get destroyed before the caller is done with it. The caller
* is responsible for consuming that extra reference if necessary.
*/
vm_shared_region_t
vm_shared_region_lookup(
void *root_dir,
cpu_type_t cputype,
cpu_subtype_t cpu_subtype,
boolean_t is_64bit,
boolean_t reslide)
{
vm_shared_region_t shared_region;
vm_shared_region_t new_shared_region;
SHARED_REGION_TRACE_DEBUG(
("shared_region: -> lookup(root=%p,cpu=<%d,%d>,64bit=%d,reslide=%d)\n",
(void *)VM_KERNEL_ADDRPERM(root_dir),
cputype, cpu_subtype, is_64bit, reslide));
shared_region = NULL;
new_shared_region = NULL;
vm_shared_region_lock();
for (;;) {
queue_iterate(&vm_shared_region_queue,
shared_region,
vm_shared_region_t,
sr_q) {
assert(shared_region->sr_ref_count > 0);
if (shared_region->sr_cpu_type == cputype &&
#if !__has_feature(ptrauth_calls) /* arm64e/arm64 use same region */
shared_region->sr_cpu_subtype == cpu_subtype &&
#endif /* !__has_feature(ptrauth_calls) */
shared_region->sr_root_dir == root_dir &&
shared_region->sr_64bit == is_64bit &&
#if __has_feature(ptrauth_calls)
shared_region->sr_reslide == reslide &&
#endif /* __has_feature(ptrauth_calls) */
!shared_region->sr_stale) {
/* found a match ! */
vm_shared_region_reference_locked(shared_region);
goto done;
}
}
if (new_shared_region == NULL) {
/* no match: create a new one */
vm_shared_region_unlock();
new_shared_region = vm_shared_region_create(root_dir,
cputype,
cpu_subtype,
is_64bit,
reslide);
/* do the lookup again, in case we lost a race */
vm_shared_region_lock();
continue;
}
/* still no match: use our new one */
shared_region = new_shared_region;
new_shared_region = NULL;
queue_enter(&vm_shared_region_queue,
shared_region,
vm_shared_region_t,
sr_q);
vm_shared_region_count++;
if (vm_shared_region_count > vm_shared_region_peak) {
vm_shared_region_peak = vm_shared_region_count;
}
break;
}
done:
vm_shared_region_unlock();
if (new_shared_region) {
/*
* We lost a race with someone else to create a new shared
* region for that environment. Get rid of our unused one.
*/
assert(new_shared_region->sr_ref_count == 1);
new_shared_region->sr_ref_count--;
vm_shared_region_destroy(new_shared_region);
new_shared_region = NULL;
}
SHARED_REGION_TRACE_DEBUG(
("shared_region: lookup(root=%p,cpu=<%d,%d>,64bit=%d,reslide=%d) <- %p\n",
(void *)VM_KERNEL_ADDRPERM(root_dir),
cputype, cpu_subtype, is_64bit, reslide,
(void *)VM_KERNEL_ADDRPERM(shared_region)));
assert(shared_region->sr_ref_count > 0);
return shared_region;
}
/*
* Take an extra reference on a shared region.
* The vm_shared_region_lock should already be held by the caller.
*/
static void
vm_shared_region_reference_locked(
vm_shared_region_t shared_region)
{
LCK_MTX_ASSERT(&vm_shared_region_lock, LCK_MTX_ASSERT_OWNED);
SHARED_REGION_TRACE_DEBUG(
("shared_region: -> reference_locked(%p)\n",
(void *)VM_KERNEL_ADDRPERM(shared_region)));
assert(shared_region->sr_ref_count > 0);
shared_region->sr_ref_count++;
assert(shared_region->sr_ref_count != 0);
if (shared_region->sr_timer_call != NULL) {
boolean_t cancelled;
/* cancel and free any pending timeout */
cancelled = thread_call_cancel(shared_region->sr_timer_call);
if (cancelled) {
thread_call_free(shared_region->sr_timer_call);
shared_region->sr_timer_call = NULL;
/* release the reference held by the cancelled timer */
shared_region->sr_ref_count--;
} else {
/* the timer will drop the reference and free itself */
}
}
SHARED_REGION_TRACE_DEBUG(
("shared_region: reference_locked(%p) <- %d\n",
(void *)VM_KERNEL_ADDRPERM(shared_region),
shared_region->sr_ref_count));
}
/*
* Take a reference on a shared region.
*/
void
vm_shared_region_reference(vm_shared_region_t shared_region)
{
SHARED_REGION_TRACE_DEBUG(
("shared_region: -> reference(%p)\n",
(void *)VM_KERNEL_ADDRPERM(shared_region)));
vm_shared_region_lock();
vm_shared_region_reference_locked(shared_region);
vm_shared_region_unlock();
SHARED_REGION_TRACE_DEBUG(
("shared_region: reference(%p) <- %d\n",
(void *)VM_KERNEL_ADDRPERM(shared_region),
shared_region->sr_ref_count));
}
/*
* Release a reference on the shared region.
* Destroy it if there are no references left.
*/
void
vm_shared_region_deallocate(
vm_shared_region_t shared_region)
{
SHARED_REGION_TRACE_DEBUG(
("shared_region: -> deallocate(%p)\n",
(void *)VM_KERNEL_ADDRPERM(shared_region)));
vm_shared_region_lock();
assert(shared_region->sr_ref_count > 0);
if (shared_region->sr_root_dir == NULL) {
/*
* Local (i.e. based on the boot volume) shared regions
* can persist or not based on the "shared_region_persistence"
* sysctl.
* Make sure that this one complies.
*
* See comments in vm_shared_region_slide() for notes about
* shared regions we have slid (which are not torn down currently).
*/
if (shared_region_persistence &&
!shared_region->sr_persists) {
/* make this one persistent */
shared_region->sr_ref_count++;
shared_region->sr_persists = TRUE;
} else if (!shared_region_persistence &&
shared_region->sr_persists) {
/* make this one no longer persistent */
assert(shared_region->sr_ref_count > 1);
shared_region->sr_ref_count--;
shared_region->sr_persists = FALSE;
}
}
assert(shared_region->sr_ref_count > 0);
shared_region->sr_ref_count--;
SHARED_REGION_TRACE_DEBUG(
("shared_region: deallocate(%p): ref now %d\n",
(void *)VM_KERNEL_ADDRPERM(shared_region),
shared_region->sr_ref_count));
if (shared_region->sr_ref_count == 0) {
uint64_t deadline;
/*
* Even though a shared region is unused, delay a while before
* tearing it down, in case a new app launch can use it.
*/
if (shared_region->sr_timer_call == NULL &&
shared_region_destroy_delay != 0 &&
!shared_region->sr_stale) {
/* hold one reference for the timer */
assert(!shared_region->sr_mapping_in_progress);
shared_region->sr_ref_count++;
/* set up the timer */
shared_region->sr_timer_call = thread_call_allocate(
(thread_call_func_t) vm_shared_region_timeout,
(thread_call_param_t) shared_region);
/* schedule the timer */
clock_interval_to_deadline(shared_region_destroy_delay,
NSEC_PER_SEC,
&deadline);
thread_call_enter_delayed(shared_region->sr_timer_call,
deadline);
SHARED_REGION_TRACE_DEBUG(
("shared_region: deallocate(%p): armed timer\n",
(void *)VM_KERNEL_ADDRPERM(shared_region)));
vm_shared_region_unlock();
} else {
/* timer expired: let go of this shared region */
/*
* Remove it from the queue first, so no one can find
* it...
*/
queue_remove(&vm_shared_region_queue,
shared_region,
vm_shared_region_t,
sr_q);
vm_shared_region_count--;
vm_shared_region_unlock();
/* ... and destroy it */
vm_shared_region_destroy(shared_region);
shared_region = NULL;
}
} else {
vm_shared_region_unlock();
}
SHARED_REGION_TRACE_DEBUG(
("shared_region: deallocate(%p) <-\n",
(void *)VM_KERNEL_ADDRPERM(shared_region)));
}
void
vm_shared_region_timeout(
thread_call_param_t param0,
__unused thread_call_param_t param1)
{
vm_shared_region_t shared_region;
shared_region = (vm_shared_region_t) param0;
vm_shared_region_deallocate(shared_region);
}
/*
* Create a new (empty) shared region for a new environment.
*/
static vm_shared_region_t
vm_shared_region_create(
void *root_dir,
cpu_type_t cputype,
cpu_subtype_t cpu_subtype,
boolean_t is_64bit,
#if !__has_feature(ptrauth_calls)
__unused
#endif /* __has_feature(ptrauth_calls) */
boolean_t reslide)
{
kern_return_t kr;
vm_named_entry_t mem_entry;
ipc_port_t mem_entry_port;
vm_shared_region_t shared_region;
vm_map_t sub_map;
mach_vm_offset_t base_address, pmap_nesting_start;
mach_vm_size_t size, pmap_nesting_size;
SHARED_REGION_TRACE_INFO(
("shared_region: -> create(root=%p,cpu=<%d,%d>,64bit=%d,reslide=%d)\n",
(void *)VM_KERNEL_ADDRPERM(root_dir),
cputype, cpu_subtype, is_64bit, reslide));
base_address = 0;
size = 0;
mem_entry = NULL;
mem_entry_port = IPC_PORT_NULL;
sub_map = VM_MAP_NULL;
/* create a new shared region structure... */
shared_region = kalloc(sizeof(*shared_region));
if (shared_region == NULL) {
SHARED_REGION_TRACE_ERROR(
("shared_region: create: couldn't allocate\n"));
goto done;
}
/* figure out the correct settings for the desired environment */
if (is_64bit) {
switch (cputype) {
#if defined(__arm64__)
case CPU_TYPE_ARM64:
base_address = SHARED_REGION_BASE_ARM64;
size = SHARED_REGION_SIZE_ARM64;
pmap_nesting_start = SHARED_REGION_NESTING_BASE_ARM64;
pmap_nesting_size = SHARED_REGION_NESTING_SIZE_ARM64;
break;
#elif !defined(__arm__)
case CPU_TYPE_I386:
base_address = SHARED_REGION_BASE_X86_64;
size = SHARED_REGION_SIZE_X86_64;
pmap_nesting_start = SHARED_REGION_NESTING_BASE_X86_64;
pmap_nesting_size = SHARED_REGION_NESTING_SIZE_X86_64;
break;
case CPU_TYPE_POWERPC:
base_address = SHARED_REGION_BASE_PPC64;
size = SHARED_REGION_SIZE_PPC64;
pmap_nesting_start = SHARED_REGION_NESTING_BASE_PPC64;
pmap_nesting_size = SHARED_REGION_NESTING_SIZE_PPC64;
break;
#endif
default:
SHARED_REGION_TRACE_ERROR(
("shared_region: create: unknown cpu type %d\n",
cputype));
kfree(shared_region, sizeof(*shared_region));
shared_region = NULL;
goto done;
}
} else {
switch (cputype) {
#if defined(__arm__) || defined(__arm64__)
case CPU_TYPE_ARM:
base_address = SHARED_REGION_BASE_ARM;
size = SHARED_REGION_SIZE_ARM;
pmap_nesting_start = SHARED_REGION_NESTING_BASE_ARM;
pmap_nesting_size = SHARED_REGION_NESTING_SIZE_ARM;
break;
#else
case CPU_TYPE_I386:
base_address = SHARED_REGION_BASE_I386;
size = SHARED_REGION_SIZE_I386;
pmap_nesting_start = SHARED_REGION_NESTING_BASE_I386;
pmap_nesting_size = SHARED_REGION_NESTING_SIZE_I386;
break;
case CPU_TYPE_POWERPC:
base_address = SHARED_REGION_BASE_PPC;
size = SHARED_REGION_SIZE_PPC;
pmap_nesting_start = SHARED_REGION_NESTING_BASE_PPC;
pmap_nesting_size = SHARED_REGION_NESTING_SIZE_PPC;
break;
#endif
default:
SHARED_REGION_TRACE_ERROR(
("shared_region: create: unknown cpu type %d\n",
cputype));
kfree(shared_region, sizeof(*shared_region));
shared_region = NULL;
goto done;
}
}
/* create a memory entry structure and a Mach port handle */
kr = mach_memory_entry_allocate(&mem_entry, &mem_entry_port);
if (kr != KERN_SUCCESS) {
kfree(shared_region, sizeof(*shared_region));
shared_region = NULL;
SHARED_REGION_TRACE_ERROR(
("shared_region: create: "
"couldn't allocate mem_entry\n"));
goto done;
}
#if defined(__arm__) || defined(__arm64__)
{
struct pmap *pmap_nested;
int pmap_flags = 0;
pmap_flags |= is_64bit ? PMAP_CREATE_64BIT : 0;
pmap_nested = pmap_create_options(NULL, 0, pmap_flags);
if (pmap_nested != PMAP_NULL) {
pmap_set_nested(pmap_nested);
sub_map = vm_map_create(pmap_nested, 0, (vm_map_offset_t)size, TRUE);
#if defined(__arm64__)
if (is_64bit ||
page_shift_user32 == SIXTEENK_PAGE_SHIFT) {
/* enforce 16KB alignment of VM map entries */
vm_map_set_page_shift(sub_map,
SIXTEENK_PAGE_SHIFT);
}
#elif (__ARM_ARCH_7K__ >= 2)
/* enforce 16KB alignment for watch targets with new ABI */
vm_map_set_page_shift(sub_map, SIXTEENK_PAGE_SHIFT);
#endif /* __arm64__ */
} else {
sub_map = VM_MAP_NULL;
}
}
#else
/* create a VM sub map and its pmap */
sub_map = vm_map_create(pmap_create_options(NULL, 0, is_64bit), 0, size, TRUE);
#endif
if (sub_map == VM_MAP_NULL) {
ipc_port_release_send(mem_entry_port);
kfree(shared_region, sizeof(*shared_region));
shared_region = NULL;
SHARED_REGION_TRACE_ERROR(("shared_region: create: couldn't allocate map\n"));
goto done;
}
/* shared regions should always enforce code-signing */
vm_map_cs_enforcement_set(sub_map, true);
assert(vm_map_cs_enforcement(sub_map));
assert(pmap_get_vm_map_cs_enforced(vm_map_pmap(sub_map)));
assert(!sub_map->disable_vmentry_reuse);
sub_map->is_nested_map = TRUE;
/* make the memory entry point to the VM sub map */
mem_entry->is_sub_map = TRUE;
mem_entry->backing.map = sub_map;
mem_entry->size = size;
mem_entry->protection = VM_PROT_ALL;
/* make the shared region point at the memory entry */
shared_region->sr_mem_entry = mem_entry_port;
/* fill in the shared region's environment and settings */
shared_region->sr_base_address = base_address;
shared_region->sr_size = size;
shared_region->sr_pmap_nesting_start = pmap_nesting_start;
shared_region->sr_pmap_nesting_size = pmap_nesting_size;
shared_region->sr_cpu_type = cputype;
shared_region->sr_cpu_subtype = cpu_subtype;
shared_region->sr_64bit = (uint8_t)is_64bit;
shared_region->sr_root_dir = root_dir;
queue_init(&shared_region->sr_q);
shared_region->sr_mapping_in_progress = FALSE;
shared_region->sr_slide_in_progress = FALSE;
shared_region->sr_persists = FALSE;
shared_region->sr_stale = FALSE;
shared_region->sr_timer_call = NULL;
shared_region->sr_first_mapping = (mach_vm_offset_t) -1;
/* grab a reference for the caller */
shared_region->sr_ref_count = 1;
shared_region->sr_slide = 0; /* not slid yet */
/* Initialize UUID and other metadata */
memset(&shared_region->sr_uuid, '\0', sizeof(shared_region->sr_uuid));
shared_region->sr_uuid_copied = FALSE;
shared_region->sr_images_count = 0;
shared_region->sr_images = NULL;
#if __has_feature(ptrauth_calls)
shared_region->sr_reslide = reslide;
shared_region->sr_num_auth_section = 0;
for (uint_t i = 0; i < NUM_SR_AUTH_SECTIONS; ++i) {
shared_region->sr_auth_section[i] = NULL;
}
shared_region->sr_num_auth_section = 0;
#endif /* __has_feature(ptrauth_calls) */
done:
if (shared_region) {
SHARED_REGION_TRACE_INFO(
("shared_region: create(root=%p,cpu=<%d,%d>,64bit=%d,reslide=%d"
"base=0x%llx,size=0x%llx) <- "
"%p mem=(%p,%p) map=%p pmap=%p\n",
(void *)VM_KERNEL_ADDRPERM(root_dir),
cputype, cpu_subtype, is_64bit, reslide,
(long long)base_address,
(long long)size,
(void *)VM_KERNEL_ADDRPERM(shared_region),
(void *)VM_KERNEL_ADDRPERM(mem_entry_port),
(void *)VM_KERNEL_ADDRPERM(mem_entry),
(void *)VM_KERNEL_ADDRPERM(sub_map),
(void *)VM_KERNEL_ADDRPERM(sub_map->pmap)));
} else {
SHARED_REGION_TRACE_INFO(
("shared_region: create(root=%p,cpu=<%d,%d>,64bit=%d,"
"base=0x%llx,size=0x%llx) <- NULL",
(void *)VM_KERNEL_ADDRPERM(root_dir),
cputype, cpu_subtype, is_64bit,
(long long)base_address,
(long long)size));
}
return shared_region;
}
/*
* Destroy a now-unused shared region.
* The shared region is no longer in the queue and can not be looked up.
*/
static void
vm_shared_region_destroy(
vm_shared_region_t shared_region)
{
vm_named_entry_t mem_entry;
vm_map_t map;
SHARED_REGION_TRACE_INFO(
("shared_region: -> destroy(%p) (root=%p,cpu=<%d,%d>,64bit=%d)\n",
(void *)VM_KERNEL_ADDRPERM(shared_region),
(void *)VM_KERNEL_ADDRPERM(shared_region->sr_root_dir),
shared_region->sr_cpu_type,
shared_region->sr_cpu_subtype,
shared_region->sr_64bit));
assert(shared_region->sr_ref_count == 0);
assert(!shared_region->sr_persists);
mem_entry = (vm_named_entry_t) ip_get_kobject(shared_region->sr_mem_entry);
assert(mem_entry->is_sub_map);
assert(!mem_entry->internal);
assert(!mem_entry->is_copy);
map = mem_entry->backing.map;
/*
* Clean up the pmap first. The virtual addresses that were
* entered in this possibly "nested" pmap may have different values
* than the VM map's min and max offsets, if the VM sub map was
* mapped at a non-zero offset in the processes' main VM maps, which
* is usually the case, so the clean-up we do in vm_map_destroy() would
* not be enough.
*/
if (map->pmap) {
pmap_remove(map->pmap,
(vm_map_offset_t)shared_region->sr_base_address,
(vm_map_offset_t)(shared_region->sr_base_address + shared_region->sr_size));
}
/*
* Release our (one and only) handle on the memory entry.
* This will generate a no-senders notification, which will be processed
* by ipc_kobject_notify(), which will release the one and only
* reference on the memory entry and cause it to be destroyed, along
* with the VM sub map and its pmap.
*/
mach_memory_entry_port_release(shared_region->sr_mem_entry);
mem_entry = NULL;
shared_region->sr_mem_entry = IPC_PORT_NULL;
if (shared_region->sr_timer_call) {
thread_call_free(shared_region->sr_timer_call);
}
#if __has_feature(ptrauth_calls)
/*
* Free the cached copies of slide_info for the AUTH regions.
*/
for (uint_t i = 0; i < shared_region->sr_num_auth_section; ++i) {
vm_shared_region_slide_info_t si = shared_region->sr_auth_section[i];
if (si != NULL) {
vm_object_deallocate(si->si_slide_object);
kheap_free(KHEAP_DATA_BUFFERS, si->si_slide_info_entry, si->si_slide_info_size);
kfree(si, sizeof *si);
shared_region->sr_auth_section[i] = NULL;
}
}
shared_region->sr_num_auth_section = 0;
#endif /* __has_feature(ptrauth_calls) */
/* release the shared region structure... */
kfree(shared_region, sizeof(*shared_region));
SHARED_REGION_TRACE_DEBUG(
("shared_region: destroy(%p) <-\n",
(void *)VM_KERNEL_ADDRPERM(shared_region)));
shared_region = NULL;
}
/*
* Gets the address of the first (in time) mapping in the shared region.
*/
kern_return_t
vm_shared_region_start_address(
vm_shared_region_t shared_region,
mach_vm_offset_t *start_address)
{
kern_return_t kr;
mach_vm_offset_t sr_base_address;
mach_vm_offset_t sr_first_mapping;
SHARED_REGION_TRACE_DEBUG(
("shared_region: -> start_address(%p)\n",
(void *)VM_KERNEL_ADDRPERM(shared_region)));
assert(shared_region->sr_ref_count > 1);
vm_shared_region_lock();
/*
* Wait if there's another thread establishing a mapping
* in this shared region right when we're looking at it.
* We want a consistent view of the map...