]> git.saurik.com Git - apple/xnu.git/blob - osfmk/vm/vm_fourk_pager.c
xnu-6153.81.5.tar.gz
[apple/xnu.git] / osfmk / vm / vm_fourk_pager.c
1 /*
2 * Copyright (c) 2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <sys/errno.h>
30
31 #include <mach/mach_types.h>
32 #include <mach/mach_traps.h>
33 #include <mach/host_priv.h>
34 #include <mach/kern_return.h>
35 #include <mach/memory_object_control.h>
36 #include <mach/memory_object_types.h>
37 #include <mach/port.h>
38 #include <mach/policy.h>
39 #include <mach/upl.h>
40 #include <mach/thread_act.h>
41 #include <mach/mach_vm.h>
42
43 #include <kern/host.h>
44 #include <kern/kalloc.h>
45 #include <kern/page_decrypt.h>
46 #include <kern/queue.h>
47 #include <kern/thread.h>
48 #include <kern/ipc_kobject.h>
49
50 #include <ipc/ipc_port.h>
51 #include <ipc/ipc_space.h>
52
53 #include <vm/vm_fault.h>
54 #include <vm/vm_map.h>
55 #include <vm/vm_pageout.h>
56 #include <vm/memory_object.h>
57 #include <vm/vm_pageout.h>
58 #include <vm/vm_protos.h>
59 #include <vm/vm_kern.h>
60
61
62 /*
63 * 4K MEMORY PAGER
64 *
65 * This external memory manager (EMM) handles memory mappings that are
66 * 4K-aligned but not page-aligned and can therefore not be mapped directly.
67 *
68 * It mostly handles page-in requests (from memory_object_data_request()) by
69 * getting the data needed to fill in each 4K-chunk. That can require
70 * getting data from one or two pages from its backing VM object
71 * (a file or a "apple-protected" pager backed by an encrypted file), and
72 * copies the data to another page so that it is aligned as expected by
73 * the mapping.
74 *
75 * Returned pages can never be dirtied and must always be mapped copy-on-write,
76 * so the memory manager does not need to handle page-out requests (from
77 * memory_object_data_return()).
78 *
79 */
80
81 /* forward declarations */
82 void fourk_pager_reference(memory_object_t mem_obj);
83 void fourk_pager_deallocate(memory_object_t mem_obj);
84 kern_return_t fourk_pager_init(memory_object_t mem_obj,
85 memory_object_control_t control,
86 memory_object_cluster_size_t pg_size);
87 kern_return_t fourk_pager_terminate(memory_object_t mem_obj);
88 kern_return_t fourk_pager_data_request(memory_object_t mem_obj,
89 memory_object_offset_t offset,
90 memory_object_cluster_size_t length,
91 vm_prot_t protection_required,
92 memory_object_fault_info_t fault_info);
93 kern_return_t fourk_pager_data_return(memory_object_t mem_obj,
94 memory_object_offset_t offset,
95 memory_object_cluster_size_t data_cnt,
96 memory_object_offset_t *resid_offset,
97 int *io_error,
98 boolean_t dirty,
99 boolean_t kernel_copy,
100 int upl_flags);
101 kern_return_t fourk_pager_data_initialize(memory_object_t mem_obj,
102 memory_object_offset_t offset,
103 memory_object_cluster_size_t data_cnt);
104 kern_return_t fourk_pager_data_unlock(memory_object_t mem_obj,
105 memory_object_offset_t offset,
106 memory_object_size_t size,
107 vm_prot_t desired_access);
108 kern_return_t fourk_pager_synchronize(memory_object_t mem_obj,
109 memory_object_offset_t offset,
110 memory_object_size_t length,
111 vm_sync_t sync_flags);
112 kern_return_t fourk_pager_map(memory_object_t mem_obj,
113 vm_prot_t prot);
114 kern_return_t fourk_pager_last_unmap(memory_object_t mem_obj);
115
116 /*
117 * Vector of VM operations for this EMM.
118 * These routines are invoked by VM via the memory_object_*() interfaces.
119 */
120 const struct memory_object_pager_ops fourk_pager_ops = {
121 .memory_object_reference = fourk_pager_reference,
122 .memory_object_deallocate = fourk_pager_deallocate,
123 .memory_object_init = fourk_pager_init,
124 .memory_object_terminate = fourk_pager_terminate,
125 .memory_object_data_request = fourk_pager_data_request,
126 .memory_object_data_return = fourk_pager_data_return,
127 .memory_object_data_initialize = fourk_pager_data_initialize,
128 .memory_object_data_unlock = fourk_pager_data_unlock,
129 .memory_object_synchronize = fourk_pager_synchronize,
130 .memory_object_map = fourk_pager_map,
131 .memory_object_last_unmap = fourk_pager_last_unmap,
132 .memory_object_data_reclaim = NULL,
133 .memory_object_pager_name = "fourk_pager"
134 };
135
136 /*
137 * The "fourk_pager" describes a memory object backed by
138 * the "4K" EMM.
139 */
140 #define FOURK_PAGER_SLOTS 4 /* 16K / 4K */
141 typedef struct fourk_pager_backing {
142 vm_object_t backing_object;
143 vm_object_offset_t backing_offset;
144 } *fourk_pager_backing_t;
145 typedef struct fourk_pager {
146 /* mandatory generic header */
147 struct memory_object fourk_pgr_hdr;
148
149 /* pager-specific data */
150 queue_chain_t pager_queue; /* next & prev pagers */
151 unsigned int ref_count; /* reference count */
152 int is_ready; /* is this pager ready ? */
153 int is_mapped; /* is this mem_obj mapped ? */
154 struct fourk_pager_backing slots[FOURK_PAGER_SLOTS]; /* backing for each
155 * 4K-chunk */
156 } *fourk_pager_t;
157 #define FOURK_PAGER_NULL ((fourk_pager_t) NULL)
158
159 /*
160 * List of memory objects managed by this EMM.
161 * The list is protected by the "fourk_pager_lock" lock.
162 */
163 int fourk_pager_count = 0; /* number of pagers */
164 int fourk_pager_count_mapped = 0; /* number of unmapped pagers */
165 queue_head_t fourk_pager_queue;
166 decl_lck_mtx_data(, fourk_pager_lock);
167
168 /*
169 * Maximum number of unmapped pagers we're willing to keep around.
170 */
171 int fourk_pager_cache_limit = 0;
172
173 /*
174 * Statistics & counters.
175 */
176 int fourk_pager_count_max = 0;
177 int fourk_pager_count_unmapped_max = 0;
178 int fourk_pager_num_trim_max = 0;
179 int fourk_pager_num_trim_total = 0;
180
181
182 lck_grp_t fourk_pager_lck_grp;
183 lck_grp_attr_t fourk_pager_lck_grp_attr;
184 lck_attr_t fourk_pager_lck_attr;
185
186
187 /* internal prototypes */
188 fourk_pager_t fourk_pager_lookup(memory_object_t mem_obj);
189 void fourk_pager_dequeue(fourk_pager_t pager);
190 void fourk_pager_deallocate_internal(fourk_pager_t pager,
191 boolean_t locked);
192 void fourk_pager_terminate_internal(fourk_pager_t pager);
193 void fourk_pager_trim(void);
194
195
196 #if DEBUG
197 int fourk_pagerdebug = 0;
198 #define PAGER_ALL 0xffffffff
199 #define PAGER_INIT 0x00000001
200 #define PAGER_PAGEIN 0x00000002
201
202 #define PAGER_DEBUG(LEVEL, A) \
203 MACRO_BEGIN \
204 if ((fourk_pagerdebug & LEVEL)==LEVEL) { \
205 printf A; \
206 } \
207 MACRO_END
208 #else
209 #define PAGER_DEBUG(LEVEL, A)
210 #endif
211
212
213 void
214 fourk_pager_bootstrap(void)
215 {
216 lck_grp_attr_setdefault(&fourk_pager_lck_grp_attr);
217 lck_grp_init(&fourk_pager_lck_grp, "4K-pager", &fourk_pager_lck_grp_attr);
218 lck_attr_setdefault(&fourk_pager_lck_attr);
219 lck_mtx_init(&fourk_pager_lock, &fourk_pager_lck_grp, &fourk_pager_lck_attr);
220 queue_init(&fourk_pager_queue);
221 }
222
223 /*
224 * fourk_pager_init()
225 *
226 * Initialize the memory object and makes it ready to be used and mapped.
227 */
228 kern_return_t
229 fourk_pager_init(
230 memory_object_t mem_obj,
231 memory_object_control_t control,
232 #if !DEBUG
233 __unused
234 #endif
235 memory_object_cluster_size_t pg_size)
236 {
237 fourk_pager_t pager;
238 kern_return_t kr;
239 memory_object_attr_info_data_t attributes;
240
241 PAGER_DEBUG(PAGER_ALL,
242 ("fourk_pager_init: %p, %p, %x\n",
243 mem_obj, control, pg_size));
244
245 if (control == MEMORY_OBJECT_CONTROL_NULL) {
246 return KERN_INVALID_ARGUMENT;
247 }
248
249 pager = fourk_pager_lookup(mem_obj);
250
251 memory_object_control_reference(control);
252
253 pager->fourk_pgr_hdr.mo_control = control;
254
255 attributes.copy_strategy = MEMORY_OBJECT_COPY_DELAY;
256 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
257 attributes.cluster_size = (1 << (PAGE_SHIFT));
258 attributes.may_cache_object = FALSE;
259 attributes.temporary = TRUE;
260
261 kr = memory_object_change_attributes(
262 control,
263 MEMORY_OBJECT_ATTRIBUTE_INFO,
264 (memory_object_info_t) &attributes,
265 MEMORY_OBJECT_ATTR_INFO_COUNT);
266 if (kr != KERN_SUCCESS) {
267 panic("fourk_pager_init: "
268 "memory_object_change_attributes() failed");
269 }
270
271 #if CONFIG_SECLUDED_MEMORY
272 if (secluded_for_filecache) {
273 memory_object_mark_eligible_for_secluded(control, TRUE);
274 }
275 #endif /* CONFIG_SECLUDED_MEMORY */
276
277 return KERN_SUCCESS;
278 }
279
280 /*
281 * fourk_pager_data_return()
282 *
283 * Handles page-out requests from VM. This should never happen since
284 * the pages provided by this EMM are not supposed to be dirty or dirtied
285 * and VM should simply discard the contents and reclaim the pages if it
286 * needs to.
287 */
288 kern_return_t
289 fourk_pager_data_return(
290 __unused memory_object_t mem_obj,
291 __unused memory_object_offset_t offset,
292 __unused memory_object_cluster_size_t data_cnt,
293 __unused memory_object_offset_t *resid_offset,
294 __unused int *io_error,
295 __unused boolean_t dirty,
296 __unused boolean_t kernel_copy,
297 __unused int upl_flags)
298 {
299 panic("fourk_pager_data_return: should never get called");
300 return KERN_FAILURE;
301 }
302
303 kern_return_t
304 fourk_pager_data_initialize(
305 __unused memory_object_t mem_obj,
306 __unused memory_object_offset_t offset,
307 __unused memory_object_cluster_size_t data_cnt)
308 {
309 panic("fourk_pager_data_initialize: should never get called");
310 return KERN_FAILURE;
311 }
312
313 kern_return_t
314 fourk_pager_data_unlock(
315 __unused memory_object_t mem_obj,
316 __unused memory_object_offset_t offset,
317 __unused memory_object_size_t size,
318 __unused vm_prot_t desired_access)
319 {
320 return KERN_FAILURE;
321 }
322
323 /*
324 * fourk_pager_reference()
325 *
326 * Get a reference on this memory object.
327 * For external usage only. Assumes that the initial reference count is not 0,
328 * i.e one should not "revive" a dead pager this way.
329 */
330 void
331 fourk_pager_reference(
332 memory_object_t mem_obj)
333 {
334 fourk_pager_t pager;
335
336 pager = fourk_pager_lookup(mem_obj);
337
338 lck_mtx_lock(&fourk_pager_lock);
339 assert(pager->ref_count > 0);
340 pager->ref_count++;
341 lck_mtx_unlock(&fourk_pager_lock);
342 }
343
344
345 /*
346 * fourk_pager_dequeue:
347 *
348 * Removes a pager from the list of pagers.
349 *
350 * The caller must hold "fourk_pager_lock".
351 */
352 void
353 fourk_pager_dequeue(
354 fourk_pager_t pager)
355 {
356 assert(!pager->is_mapped);
357
358 queue_remove(&fourk_pager_queue,
359 pager,
360 fourk_pager_t,
361 pager_queue);
362 pager->pager_queue.next = NULL;
363 pager->pager_queue.prev = NULL;
364
365 fourk_pager_count--;
366 }
367
368 /*
369 * fourk_pager_terminate_internal:
370 *
371 * Trigger the asynchronous termination of the memory object associated
372 * with this pager.
373 * When the memory object is terminated, there will be one more call
374 * to memory_object_deallocate() (i.e. fourk_pager_deallocate())
375 * to finish the clean up.
376 *
377 * "fourk_pager_lock" should not be held by the caller.
378 * We don't need the lock because the pager has already been removed from
379 * the pagers' list and is now ours exclusively.
380 */
381 void
382 fourk_pager_terminate_internal(
383 fourk_pager_t pager)
384 {
385 int i;
386
387 assert(pager->is_ready);
388 assert(!pager->is_mapped);
389
390 for (i = 0; i < FOURK_PAGER_SLOTS; i++) {
391 if (pager->slots[i].backing_object != VM_OBJECT_NULL &&
392 pager->slots[i].backing_object != (vm_object_t) -1) {
393 vm_object_deallocate(pager->slots[i].backing_object);
394 pager->slots[i].backing_object = (vm_object_t) -1;
395 pager->slots[i].backing_offset = (vm_object_offset_t) -1;
396 }
397 }
398
399 /* trigger the destruction of the memory object */
400 memory_object_destroy(pager->fourk_pgr_hdr.mo_control, 0);
401 }
402
403 /*
404 * fourk_pager_deallocate_internal()
405 *
406 * Release a reference on this pager and free it when the last
407 * reference goes away.
408 * Can be called with fourk_pager_lock held or not but always returns
409 * with it unlocked.
410 */
411 void
412 fourk_pager_deallocate_internal(
413 fourk_pager_t pager,
414 boolean_t locked)
415 {
416 boolean_t needs_trimming;
417 int count_unmapped;
418
419 if (!locked) {
420 lck_mtx_lock(&fourk_pager_lock);
421 }
422
423 count_unmapped = (fourk_pager_count -
424 fourk_pager_count_mapped);
425 if (count_unmapped > fourk_pager_cache_limit) {
426 /* we have too many unmapped pagers: trim some */
427 needs_trimming = TRUE;
428 } else {
429 needs_trimming = FALSE;
430 }
431
432 /* drop a reference on this pager */
433 pager->ref_count--;
434
435 if (pager->ref_count == 1) {
436 /*
437 * Only the "named" reference is left, which means that
438 * no one is really holding on to this pager anymore.
439 * Terminate it.
440 */
441 fourk_pager_dequeue(pager);
442 /* the pager is all ours: no need for the lock now */
443 lck_mtx_unlock(&fourk_pager_lock);
444 fourk_pager_terminate_internal(pager);
445 } else if (pager->ref_count == 0) {
446 /*
447 * Dropped the existence reference; the memory object has
448 * been terminated. Do some final cleanup and release the
449 * pager structure.
450 */
451 lck_mtx_unlock(&fourk_pager_lock);
452 if (pager->fourk_pgr_hdr.mo_control != MEMORY_OBJECT_CONTROL_NULL) {
453 memory_object_control_deallocate(pager->fourk_pgr_hdr.mo_control);
454 pager->fourk_pgr_hdr.mo_control = MEMORY_OBJECT_CONTROL_NULL;
455 }
456 kfree(pager, sizeof(*pager));
457 pager = FOURK_PAGER_NULL;
458 } else {
459 /* there are still plenty of references: keep going... */
460 lck_mtx_unlock(&fourk_pager_lock);
461 }
462
463 if (needs_trimming) {
464 fourk_pager_trim();
465 }
466 /* caution: lock is not held on return... */
467 }
468
469 /*
470 * fourk_pager_deallocate()
471 *
472 * Release a reference on this pager and free it when the last
473 * reference goes away.
474 */
475 void
476 fourk_pager_deallocate(
477 memory_object_t mem_obj)
478 {
479 fourk_pager_t pager;
480
481 PAGER_DEBUG(PAGER_ALL, ("fourk_pager_deallocate: %p\n", mem_obj));
482 pager = fourk_pager_lookup(mem_obj);
483 fourk_pager_deallocate_internal(pager, FALSE);
484 }
485
486 /*
487 *
488 */
489 kern_return_t
490 fourk_pager_terminate(
491 #if !DEBUG
492 __unused
493 #endif
494 memory_object_t mem_obj)
495 {
496 PAGER_DEBUG(PAGER_ALL, ("fourk_pager_terminate: %p\n", mem_obj));
497
498 return KERN_SUCCESS;
499 }
500
501 /*
502 *
503 */
504 kern_return_t
505 fourk_pager_synchronize(
506 __unused memory_object_t mem_obj,
507 __unused memory_object_offset_t offset,
508 __unused memory_object_size_t length,
509 __unused vm_sync_t sync_flags)
510 {
511 panic("fourk_pager_synchronize: memory_object_synchronize no longer supported\n");
512 return KERN_FAILURE;
513 }
514
515 /*
516 * fourk_pager_map()
517 *
518 * This allows VM to let us, the EMM, know that this memory object
519 * is currently mapped one or more times. This is called by VM each time
520 * the memory object gets mapped and we take one extra reference on the
521 * memory object to account for all its mappings.
522 */
523 kern_return_t
524 fourk_pager_map(
525 memory_object_t mem_obj,
526 __unused vm_prot_t prot)
527 {
528 fourk_pager_t pager;
529
530 PAGER_DEBUG(PAGER_ALL, ("fourk_pager_map: %p\n", mem_obj));
531
532 pager = fourk_pager_lookup(mem_obj);
533
534 lck_mtx_lock(&fourk_pager_lock);
535 assert(pager->is_ready);
536 assert(pager->ref_count > 0); /* pager is alive */
537 if (pager->is_mapped == FALSE) {
538 /*
539 * First mapping of this pager: take an extra reference
540 * that will remain until all the mappings of this pager
541 * are removed.
542 */
543 pager->is_mapped = TRUE;
544 pager->ref_count++;
545 fourk_pager_count_mapped++;
546 }
547 lck_mtx_unlock(&fourk_pager_lock);
548
549 return KERN_SUCCESS;
550 }
551
552 /*
553 * fourk_pager_last_unmap()
554 *
555 * This is called by VM when this memory object is no longer mapped anywhere.
556 */
557 kern_return_t
558 fourk_pager_last_unmap(
559 memory_object_t mem_obj)
560 {
561 fourk_pager_t pager;
562 int count_unmapped;
563
564 PAGER_DEBUG(PAGER_ALL,
565 ("fourk_pager_last_unmap: %p\n", mem_obj));
566
567 pager = fourk_pager_lookup(mem_obj);
568
569 lck_mtx_lock(&fourk_pager_lock);
570 if (pager->is_mapped) {
571 /*
572 * All the mappings are gone, so let go of the one extra
573 * reference that represents all the mappings of this pager.
574 */
575 fourk_pager_count_mapped--;
576 count_unmapped = (fourk_pager_count -
577 fourk_pager_count_mapped);
578 if (count_unmapped > fourk_pager_count_unmapped_max) {
579 fourk_pager_count_unmapped_max = count_unmapped;
580 }
581 pager->is_mapped = FALSE;
582 fourk_pager_deallocate_internal(pager, TRUE);
583 /* caution: deallocate_internal() released the lock ! */
584 } else {
585 lck_mtx_unlock(&fourk_pager_lock);
586 }
587
588 return KERN_SUCCESS;
589 }
590
591
592 /*
593 *
594 */
595 fourk_pager_t
596 fourk_pager_lookup(
597 memory_object_t mem_obj)
598 {
599 fourk_pager_t pager;
600
601 assert(mem_obj->mo_pager_ops == &fourk_pager_ops);
602 pager = (fourk_pager_t) mem_obj;
603 assert(pager->ref_count > 0);
604 return pager;
605 }
606
607 void
608 fourk_pager_trim(void)
609 {
610 fourk_pager_t pager, prev_pager;
611 queue_head_t trim_queue;
612 int num_trim;
613 int count_unmapped;
614
615 lck_mtx_lock(&fourk_pager_lock);
616
617 /*
618 * We have too many pagers, try and trim some unused ones,
619 * starting with the oldest pager at the end of the queue.
620 */
621 queue_init(&trim_queue);
622 num_trim = 0;
623
624 for (pager = (fourk_pager_t)
625 queue_last(&fourk_pager_queue);
626 !queue_end(&fourk_pager_queue,
627 (queue_entry_t) pager);
628 pager = prev_pager) {
629 /* get prev elt before we dequeue */
630 prev_pager = (fourk_pager_t)
631 queue_prev(&pager->pager_queue);
632
633 if (pager->ref_count == 2 &&
634 pager->is_ready &&
635 !pager->is_mapped) {
636 /* this pager can be trimmed */
637 num_trim++;
638 /* remove this pager from the main list ... */
639 fourk_pager_dequeue(pager);
640 /* ... and add it to our trim queue */
641 queue_enter_first(&trim_queue,
642 pager,
643 fourk_pager_t,
644 pager_queue);
645
646 count_unmapped = (fourk_pager_count -
647 fourk_pager_count_mapped);
648 if (count_unmapped <= fourk_pager_cache_limit) {
649 /* we have enough pagers to trim */
650 break;
651 }
652 }
653 }
654 if (num_trim > fourk_pager_num_trim_max) {
655 fourk_pager_num_trim_max = num_trim;
656 }
657 fourk_pager_num_trim_total += num_trim;
658
659 lck_mtx_unlock(&fourk_pager_lock);
660
661 /* terminate the trimmed pagers */
662 while (!queue_empty(&trim_queue)) {
663 queue_remove_first(&trim_queue,
664 pager,
665 fourk_pager_t,
666 pager_queue);
667 pager->pager_queue.next = NULL;
668 pager->pager_queue.prev = NULL;
669 assert(pager->ref_count == 2);
670 /*
671 * We can't call deallocate_internal() because the pager
672 * has already been dequeued, but we still need to remove
673 * a reference.
674 */
675 pager->ref_count--;
676 fourk_pager_terminate_internal(pager);
677 }
678 }
679
680
681
682
683
684
685 vm_object_t
686 fourk_pager_to_vm_object(
687 memory_object_t mem_obj)
688 {
689 fourk_pager_t pager;
690 vm_object_t object;
691
692 pager = fourk_pager_lookup(mem_obj);
693 if (pager == NULL) {
694 return VM_OBJECT_NULL;
695 }
696
697 assert(pager->ref_count > 0);
698 assert(pager->fourk_pgr_hdr.mo_control != MEMORY_OBJECT_CONTROL_NULL);
699 object = memory_object_control_to_vm_object(pager->fourk_pgr_hdr.mo_control);
700 assert(object != VM_OBJECT_NULL);
701 return object;
702 }
703
704 memory_object_t
705 fourk_pager_create(void)
706 {
707 fourk_pager_t pager;
708 memory_object_control_t control;
709 kern_return_t kr;
710 int i;
711
712 #if 00
713 if (PAGE_SIZE_64 == FOURK_PAGE_SIZE) {
714 panic("fourk_pager_create: page size is 4K !?");
715 }
716 #endif
717
718 pager = (fourk_pager_t) kalloc(sizeof(*pager));
719 if (pager == FOURK_PAGER_NULL) {
720 return MEMORY_OBJECT_NULL;
721 }
722 bzero(pager, sizeof(*pager));
723
724 /*
725 * The vm_map call takes both named entry ports and raw memory
726 * objects in the same parameter. We need to make sure that
727 * vm_map does not see this object as a named entry port. So,
728 * we reserve the first word in the object for a fake ip_kotype
729 * setting - that will tell vm_map to use it as a memory object.
730 */
731 pager->fourk_pgr_hdr.mo_ikot = IKOT_MEMORY_OBJECT;
732 pager->fourk_pgr_hdr.mo_pager_ops = &fourk_pager_ops;
733 pager->fourk_pgr_hdr.mo_control = MEMORY_OBJECT_CONTROL_NULL;
734
735 pager->ref_count = 2; /* existence + setup reference */
736 pager->is_ready = FALSE;/* not ready until it has a "name" */
737 pager->is_mapped = FALSE;
738
739 for (i = 0; i < FOURK_PAGER_SLOTS; i++) {
740 pager->slots[i].backing_object = (vm_object_t) -1;
741 pager->slots[i].backing_offset = (vm_object_offset_t) -1;
742 }
743
744 lck_mtx_lock(&fourk_pager_lock);
745
746 /* enter new pager at the head of our list of pagers */
747 queue_enter_first(&fourk_pager_queue,
748 pager,
749 fourk_pager_t,
750 pager_queue);
751 fourk_pager_count++;
752 if (fourk_pager_count > fourk_pager_count_max) {
753 fourk_pager_count_max = fourk_pager_count;
754 }
755 lck_mtx_unlock(&fourk_pager_lock);
756
757 kr = memory_object_create_named((memory_object_t) pager,
758 0,
759 &control);
760 assert(kr == KERN_SUCCESS);
761
762 memory_object_mark_trusted(control);
763
764 lck_mtx_lock(&fourk_pager_lock);
765 /* the new pager is now ready to be used */
766 pager->is_ready = TRUE;
767 lck_mtx_unlock(&fourk_pager_lock);
768
769 /* wakeup anyone waiting for this pager to be ready */
770 thread_wakeup(&pager->is_ready);
771
772 return (memory_object_t) pager;
773 }
774
775 /*
776 * fourk_pager_data_request()
777 *
778 * Handles page-in requests from VM.
779 */
780 int fourk_pager_data_request_debug = 0;
781 kern_return_t
782 fourk_pager_data_request(
783 memory_object_t mem_obj,
784 memory_object_offset_t offset,
785 memory_object_cluster_size_t length,
786 #if !DEBUG
787 __unused
788 #endif
789 vm_prot_t protection_required,
790 memory_object_fault_info_t mo_fault_info)
791 {
792 fourk_pager_t pager;
793 memory_object_control_t mo_control;
794 upl_t upl;
795 int upl_flags;
796 upl_size_t upl_size;
797 upl_page_info_t *upl_pl;
798 unsigned int pl_count;
799 vm_object_t dst_object;
800 kern_return_t kr, retval;
801 vm_map_offset_t kernel_mapping;
802 vm_offset_t src_vaddr, dst_vaddr;
803 vm_offset_t cur_offset;
804 int sub_page;
805 int sub_page_idx, sub_page_cnt;
806
807 pager = fourk_pager_lookup(mem_obj);
808 assert(pager->is_ready);
809 assert(pager->ref_count > 1); /* pager is alive and mapped */
810
811 PAGER_DEBUG(PAGER_PAGEIN, ("fourk_pager_data_request: %p, %llx, %x, %x, pager %p\n", mem_obj, offset, length, protection_required, pager));
812
813 retval = KERN_SUCCESS;
814 kernel_mapping = 0;
815
816 offset = memory_object_trunc_page(offset);
817
818 /*
819 * Gather in a UPL all the VM pages requested by VM.
820 */
821 mo_control = pager->fourk_pgr_hdr.mo_control;
822
823 upl_size = length;
824 upl_flags =
825 UPL_RET_ONLY_ABSENT |
826 UPL_SET_LITE |
827 UPL_NO_SYNC |
828 UPL_CLEAN_IN_PLACE | /* triggers UPL_CLEAR_DIRTY */
829 UPL_SET_INTERNAL;
830 pl_count = 0;
831 kr = memory_object_upl_request(mo_control,
832 offset, upl_size,
833 &upl, NULL, NULL, upl_flags, VM_KERN_MEMORY_NONE);
834 if (kr != KERN_SUCCESS) {
835 retval = kr;
836 goto done;
837 }
838 dst_object = mo_control->moc_object;
839 assert(dst_object != VM_OBJECT_NULL);
840
841 #if __x86_64__ || __arm__ || __arm64__
842 /* use the 1-to-1 mapping of physical memory */
843 #else /* __x86_64__ || __arm__ || __arm64__ */
844 /*
845 * Reserve 2 virtual pages in the kernel address space to map the
846 * source and destination physical pages when it's their turn to
847 * be processed.
848 */
849 vm_map_entry_t map_entry;
850
851 vm_object_reference(kernel_object); /* ref. for mapping */
852 kr = vm_map_find_space(kernel_map,
853 &kernel_mapping,
854 2 * PAGE_SIZE_64,
855 0,
856 0,
857 VM_MAP_KERNEL_FLAGS_NONE,
858 &map_entry);
859 if (kr != KERN_SUCCESS) {
860 vm_object_deallocate(kernel_object);
861 retval = kr;
862 goto done;
863 }
864 map_entry->object.vm_object = kernel_object;
865 map_entry->offset = kernel_mapping;
866 vm_map_unlock(kernel_map);
867 src_vaddr = CAST_DOWN(vm_offset_t, kernel_mapping);
868 dst_vaddr = CAST_DOWN(vm_offset_t, kernel_mapping + PAGE_SIZE_64);
869 #endif /* __x86_64__ || __arm__ || __arm64__ */
870
871 /*
872 * Fill in the contents of the pages requested by VM.
873 */
874 upl_pl = UPL_GET_INTERNAL_PAGE_LIST(upl);
875 pl_count = length / PAGE_SIZE;
876 for (cur_offset = 0;
877 retval == KERN_SUCCESS && cur_offset < length;
878 cur_offset += PAGE_SIZE) {
879 ppnum_t dst_pnum;
880 int num_subpg_signed, num_subpg_validated;
881 int num_subpg_tainted, num_subpg_nx;
882
883 if (!upl_page_present(upl_pl, (int)(cur_offset / PAGE_SIZE))) {
884 /* this page is not in the UPL: skip it */
885 continue;
886 }
887
888 /*
889 * Establish an explicit pmap mapping of the destination
890 * physical page.
891 * We can't do a regular VM mapping because the VM page
892 * is "busy".
893 */
894 dst_pnum = (ppnum_t)
895 upl_phys_page(upl_pl, (int)(cur_offset / PAGE_SIZE));
896 assert(dst_pnum != 0);
897 dst_vaddr = (vm_map_offset_t)
898 phystokv((pmap_paddr_t)dst_pnum << PAGE_SHIFT);
899
900 /* retrieve appropriate data for each 4K-page in this page */
901 if (PAGE_SHIFT == FOURK_PAGE_SHIFT &&
902 page_shift_user32 == SIXTEENK_PAGE_SHIFT) {
903 /*
904 * Find the slot for the requested 4KB page in
905 * the 16K page...
906 */
907 assert(PAGE_SHIFT == FOURK_PAGE_SHIFT);
908 assert(page_shift_user32 == SIXTEENK_PAGE_SHIFT);
909 sub_page_idx = ((offset & SIXTEENK_PAGE_MASK) /
910 PAGE_SIZE);
911 /*
912 * ... and provide only that one 4KB page.
913 */
914 sub_page_cnt = 1;
915 } else {
916 /*
917 * Iterate over all slots, i.e. retrieve all four 4KB
918 * pages in the requested 16KB page.
919 */
920 assert(PAGE_SHIFT == SIXTEENK_PAGE_SHIFT);
921 sub_page_idx = 0;
922 sub_page_cnt = FOURK_PAGER_SLOTS;
923 }
924
925 num_subpg_signed = 0;
926 num_subpg_validated = 0;
927 num_subpg_tainted = 0;
928 num_subpg_nx = 0;
929
930 /* retrieve appropriate data for each 4K-page in this page */
931 for (sub_page = sub_page_idx;
932 sub_page < sub_page_idx + sub_page_cnt;
933 sub_page++) {
934 vm_object_t src_object;
935 memory_object_offset_t src_offset;
936 vm_offset_t offset_in_src_page;
937 kern_return_t error_code;
938 vm_object_t src_page_object;
939 vm_page_t src_page;
940 vm_page_t top_page;
941 vm_prot_t prot;
942 int interruptible;
943 struct vm_object_fault_info fault_info;
944 boolean_t subpg_validated;
945 unsigned subpg_tainted;
946
947
948 if (offset < SIXTEENK_PAGE_SIZE) {
949 /*
950 * The 1st 16K-page can cover multiple
951 * sub-mappings, as described in the
952 * pager->slots[] array.
953 */
954 src_object =
955 pager->slots[sub_page].backing_object;
956 src_offset =
957 pager->slots[sub_page].backing_offset;
958 } else {
959 fourk_pager_backing_t slot;
960
961 /*
962 * Beyond the 1st 16K-page in the pager is
963 * an extension of the last "sub page" in
964 * the pager->slots[] array.
965 */
966 slot = &pager->slots[FOURK_PAGER_SLOTS - 1];
967 src_object = slot->backing_object;
968 src_offset = slot->backing_offset;
969 src_offset += FOURK_PAGE_SIZE;
970 src_offset +=
971 (vm_map_trunc_page(offset,
972 SIXTEENK_PAGE_MASK)
973 - SIXTEENK_PAGE_SIZE);
974 src_offset += sub_page * FOURK_PAGE_SIZE;
975 }
976 offset_in_src_page = src_offset & PAGE_MASK_64;
977 src_offset = vm_object_trunc_page(src_offset);
978
979 if (src_object == VM_OBJECT_NULL ||
980 src_object == (vm_object_t) -1) {
981 /* zero-fill */
982 bzero((char *)(dst_vaddr +
983 ((sub_page - sub_page_idx)
984 * FOURK_PAGE_SIZE)),
985 FOURK_PAGE_SIZE);
986 if (fourk_pager_data_request_debug) {
987 printf("fourk_pager_data_request"
988 "(%p,0x%llx+0x%lx+0x%04x): "
989 "ZERO\n",
990 pager,
991 offset,
992 cur_offset,
993 ((sub_page - sub_page_idx)
994 * FOURK_PAGE_SIZE));
995 }
996 continue;
997 }
998
999 /* fault in the source page from src_object */
1000 retry_src_fault:
1001 src_page = VM_PAGE_NULL;
1002 top_page = VM_PAGE_NULL;
1003 fault_info = *((struct vm_object_fault_info *)
1004 (uintptr_t)mo_fault_info);
1005 fault_info.stealth = TRUE;
1006 fault_info.io_sync = FALSE;
1007 fault_info.mark_zf_absent = FALSE;
1008 fault_info.batch_pmap_op = FALSE;
1009 interruptible = fault_info.interruptible;
1010 prot = VM_PROT_READ;
1011 error_code = 0;
1012
1013 vm_object_lock(src_object);
1014 vm_object_paging_begin(src_object);
1015 kr = vm_fault_page(src_object,
1016 src_offset,
1017 VM_PROT_READ,
1018 FALSE,
1019 FALSE, /* src_page not looked up */
1020 &prot,
1021 &src_page,
1022 &top_page,
1023 NULL,
1024 &error_code,
1025 FALSE,
1026 FALSE,
1027 &fault_info);
1028 switch (kr) {
1029 case VM_FAULT_SUCCESS:
1030 break;
1031 case VM_FAULT_RETRY:
1032 goto retry_src_fault;
1033 case VM_FAULT_MEMORY_SHORTAGE:
1034 if (vm_page_wait(interruptible)) {
1035 goto retry_src_fault;
1036 }
1037 /* fall thru */
1038 case VM_FAULT_INTERRUPTED:
1039 retval = MACH_SEND_INTERRUPTED;
1040 goto src_fault_done;
1041 case VM_FAULT_SUCCESS_NO_VM_PAGE:
1042 /* success but no VM page: fail */
1043 vm_object_paging_end(src_object);
1044 vm_object_unlock(src_object);
1045 /*FALLTHROUGH*/
1046 case VM_FAULT_MEMORY_ERROR:
1047 /* the page is not there! */
1048 if (error_code) {
1049 retval = error_code;
1050 } else {
1051 retval = KERN_MEMORY_ERROR;
1052 }
1053 goto src_fault_done;
1054 default:
1055 panic("fourk_pager_data_request: "
1056 "vm_fault_page() unexpected error 0x%x\n",
1057 kr);
1058 }
1059 assert(src_page != VM_PAGE_NULL);
1060 assert(src_page->vmp_busy);
1061
1062 src_page_object = VM_PAGE_OBJECT(src_page);
1063
1064 if ((!VM_PAGE_PAGEABLE(src_page)) &&
1065 !VM_PAGE_WIRED(src_page)) {
1066 vm_page_lockspin_queues();
1067 if ((!VM_PAGE_PAGEABLE(src_page)) &&
1068 !VM_PAGE_WIRED(src_page)) {
1069 vm_page_deactivate(src_page);
1070 }
1071 vm_page_unlock_queues();
1072 }
1073
1074 src_vaddr = (vm_map_offset_t)
1075 phystokv((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(src_page)
1076 << PAGE_SHIFT);
1077
1078 /*
1079 * Validate the 4K page we want from
1080 * this source page...
1081 */
1082 subpg_validated = FALSE;
1083 subpg_tainted = 0;
1084 if (src_page_object->code_signed) {
1085 vm_page_validate_cs_mapped_chunk(
1086 src_page,
1087 (const void *) src_vaddr,
1088 offset_in_src_page,
1089 FOURK_PAGE_SIZE,
1090 &subpg_validated,
1091 &subpg_tainted);
1092 num_subpg_signed++;
1093 if (subpg_validated) {
1094 num_subpg_validated++;
1095 }
1096 if (subpg_tainted & CS_VALIDATE_TAINTED) {
1097 num_subpg_tainted++;
1098 }
1099 if (subpg_tainted & CS_VALIDATE_NX) {
1100 /* subpg should not be executable */
1101 if (sub_page_cnt > 1) {
1102 /*
1103 * The destination page has
1104 * more than 1 subpage and its
1105 * other subpages might need
1106 * EXEC, so we do not propagate
1107 * CS_VALIDATE_NX to the
1108 * destination page...
1109 */
1110 } else {
1111 num_subpg_nx++;
1112 }
1113 }
1114 }
1115
1116 /*
1117 * Copy the relevant portion of the source page
1118 * into the appropriate part of the destination page.
1119 */
1120 bcopy((const char *)(src_vaddr + offset_in_src_page),
1121 (char *)(dst_vaddr +
1122 ((sub_page - sub_page_idx) *
1123 FOURK_PAGE_SIZE)),
1124 FOURK_PAGE_SIZE);
1125 if (fourk_pager_data_request_debug) {
1126 printf("fourk_data_request"
1127 "(%p,0x%llx+0x%lx+0x%04x): "
1128 "backed by [%p:0x%llx]: "
1129 "[0x%016llx 0x%016llx] "
1130 "code_signed=%d "
1131 "cs_valid=%d cs_tainted=%d cs_nx=%d\n",
1132 pager,
1133 offset, cur_offset,
1134 (sub_page - sub_page_idx) * FOURK_PAGE_SIZE,
1135 src_page_object,
1136 src_page->vmp_offset + offset_in_src_page,
1137 *(uint64_t *)(dst_vaddr +
1138 ((sub_page - sub_page_idx) *
1139 FOURK_PAGE_SIZE)),
1140 *(uint64_t *)(dst_vaddr +
1141 ((sub_page - sub_page_idx) *
1142 FOURK_PAGE_SIZE) +
1143 8),
1144 src_page_object->code_signed,
1145 subpg_validated,
1146 !!(subpg_tainted & CS_VALIDATE_TAINTED),
1147 !!(subpg_tainted & CS_VALIDATE_NX));
1148 }
1149
1150 #if __x86_64__ || __arm__ || __arm64__
1151 /* we used the 1-to-1 mapping of physical memory */
1152 src_vaddr = 0;
1153 #else /* __x86_64__ || __arm__ || __arm64__ */
1154 /*
1155 * Remove the pmap mapping of the source page
1156 * in the kernel.
1157 */
1158 pmap_remove(kernel_pmap,
1159 (addr64_t) src_vaddr,
1160 (addr64_t) src_vaddr + PAGE_SIZE_64);
1161 #endif /* __x86_64__ || __arm__ || __arm64__ */
1162
1163 src_fault_done:
1164 /*
1165 * Cleanup the result of vm_fault_page().
1166 */
1167 if (src_page) {
1168 assert(VM_PAGE_OBJECT(src_page) == src_page_object);
1169
1170 PAGE_WAKEUP_DONE(src_page);
1171 src_page = VM_PAGE_NULL;
1172 vm_object_paging_end(src_page_object);
1173 vm_object_unlock(src_page_object);
1174 if (top_page) {
1175 vm_object_t top_object;
1176
1177 top_object = VM_PAGE_OBJECT(top_page);
1178 vm_object_lock(top_object);
1179 VM_PAGE_FREE(top_page);
1180 top_page = VM_PAGE_NULL;
1181 vm_object_paging_end(top_object);
1182 vm_object_unlock(top_object);
1183 }
1184 }
1185 }
1186 if (num_subpg_signed > 0) {
1187 /* some code-signing involved with this 16K page */
1188 if (num_subpg_tainted > 0) {
1189 /* a tainted subpage taints entire 16K page */
1190 UPL_SET_CS_TAINTED(upl_pl,
1191 cur_offset / PAGE_SIZE,
1192 TRUE);
1193 /* also mark as "validated" for consisteny */
1194 UPL_SET_CS_VALIDATED(upl_pl,
1195 cur_offset / PAGE_SIZE,
1196 TRUE);
1197 } else if (num_subpg_validated == num_subpg_signed) {
1198 /*
1199 * All the code-signed 4K subpages of this
1200 * 16K page are validated: our 16K page is
1201 * considered validated.
1202 */
1203 UPL_SET_CS_VALIDATED(upl_pl,
1204 cur_offset / PAGE_SIZE,
1205 TRUE);
1206 }
1207 if (num_subpg_nx > 0) {
1208 UPL_SET_CS_NX(upl_pl,
1209 cur_offset / PAGE_SIZE,
1210 TRUE);
1211 }
1212 }
1213 }
1214
1215 done:
1216 if (upl != NULL) {
1217 /* clean up the UPL */
1218
1219 /*
1220 * The pages are currently dirty because we've just been
1221 * writing on them, but as far as we're concerned, they're
1222 * clean since they contain their "original" contents as
1223 * provided by us, the pager.
1224 * Tell the UPL to mark them "clean".
1225 */
1226 upl_clear_dirty(upl, TRUE);
1227
1228 /* abort or commit the UPL */
1229 if (retval != KERN_SUCCESS) {
1230 upl_abort(upl, 0);
1231 if (retval == KERN_ABORTED) {
1232 wait_result_t wait_result;
1233
1234 /*
1235 * We aborted the fault and did not provide
1236 * any contents for the requested pages but
1237 * the pages themselves are not invalid, so
1238 * let's return success and let the caller
1239 * retry the fault, in case it might succeed
1240 * later (when the decryption code is up and
1241 * running in the kernel, for example).
1242 */
1243 retval = KERN_SUCCESS;
1244 /*
1245 * Wait a little bit first to avoid using
1246 * too much CPU time retrying and failing
1247 * the same fault over and over again.
1248 */
1249 wait_result = assert_wait_timeout(
1250 (event_t) fourk_pager_data_request,
1251 THREAD_UNINT,
1252 10000, /* 10ms */
1253 NSEC_PER_USEC);
1254 assert(wait_result == THREAD_WAITING);
1255 wait_result = thread_block(THREAD_CONTINUE_NULL);
1256 assert(wait_result == THREAD_TIMED_OUT);
1257 }
1258 } else {
1259 boolean_t empty;
1260 upl_commit_range(upl, 0, upl->size,
1261 UPL_COMMIT_CS_VALIDATED | UPL_COMMIT_WRITTEN_BY_KERNEL,
1262 upl_pl, pl_count, &empty);
1263 }
1264
1265 /* and deallocate the UPL */
1266 upl_deallocate(upl);
1267 upl = NULL;
1268 }
1269 if (kernel_mapping != 0) {
1270 /* clean up the mapping of the source and destination pages */
1271 kr = vm_map_remove(kernel_map,
1272 kernel_mapping,
1273 kernel_mapping + (2 * PAGE_SIZE_64),
1274 VM_MAP_REMOVE_NO_FLAGS);
1275 assert(kr == KERN_SUCCESS);
1276 kernel_mapping = 0;
1277 src_vaddr = 0;
1278 dst_vaddr = 0;
1279 }
1280
1281 return retval;
1282 }
1283
1284
1285
1286 kern_return_t
1287 fourk_pager_populate(
1288 memory_object_t mem_obj,
1289 boolean_t overwrite,
1290 int index,
1291 vm_object_t new_backing_object,
1292 vm_object_offset_t new_backing_offset,
1293 vm_object_t *old_backing_object,
1294 vm_object_offset_t *old_backing_offset)
1295 {
1296 fourk_pager_t pager;
1297
1298 pager = fourk_pager_lookup(mem_obj);
1299 if (pager == NULL) {
1300 return KERN_INVALID_ARGUMENT;
1301 }
1302
1303 assert(pager->ref_count > 0);
1304 assert(pager->fourk_pgr_hdr.mo_control != MEMORY_OBJECT_CONTROL_NULL);
1305
1306 if (index < 0 || index > FOURK_PAGER_SLOTS) {
1307 return KERN_INVALID_ARGUMENT;
1308 }
1309
1310 if (!overwrite &&
1311 (pager->slots[index].backing_object != (vm_object_t) -1 ||
1312 pager->slots[index].backing_offset != (vm_object_offset_t) -1)) {
1313 return KERN_INVALID_ADDRESS;
1314 }
1315
1316 *old_backing_object = pager->slots[index].backing_object;
1317 *old_backing_offset = pager->slots[index].backing_offset;
1318
1319 pager->slots[index].backing_object = new_backing_object;
1320 pager->slots[index].backing_offset = new_backing_offset;
1321
1322 return KERN_SUCCESS;
1323 }