1 /* 2 * Copyright (c) 2023 Apple Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28 29 #ifndef __VM_RECLAIM_XNU__ 30 #define __VM_RECLAIM_XNU__ 31 32 #if XNU_KERNEL_PRIVATE 33 #if CONFIG_DEFERRED_RECLAIM 34 #include <mach/mach_types.h> 35 #if BSD_KERNEL_PRIVATE 36 #include <mach/mach_vm.h> 37 #else /* BSD_KERNEL_PRIVATE */ 38 #include <mach/mach_vm_server.h> 39 #endif /* BSD_KERNEL_PRIVATE */ 40 41 typedef struct vm_deferred_reclamation_metadata_s *vm_deferred_reclamation_metadata_t; 42 43 __enum_closed_decl(vm_deferred_reclamation_gc_action_t, uint8_t, { 44 /* Trim all buffers */ 45 RECLAIM_GC_TRIM = 0, 46 /* Fully drain all buffers */ 47 RECLAIM_GC_DRAIN = 1, 48 /* Drain any buffers belonging to suspended tasks */ 49 RECLAIM_GC_SCAVENGE = 2, 50 }); 51 52 __options_closed_decl(vm_deferred_reclamation_options_t, uint8_t, { 53 RECLAIM_OPTIONS_NONE = 0x00, 54 /* Do not fault on the reclaim buffer if it is not resident */ 55 RECLAIM_NO_FAULT = 0x01, 56 /* Do not wait to acquire the buffer if it is owned by another thread */ 57 RECLAIM_NO_WAIT = 0x02, 58 }); 59 60 /* 61 * Deallocate the kernel metadata associated with this reclamation buffer 62 * Note that this does NOT free the memory in the buffer. 63 * This is called from the task_destroy path, so we're about to reclaim all of the task's memory 64 * anyways. 65 */ 66 void vm_deferred_reclamation_buffer_deallocate(vm_deferred_reclamation_metadata_t metadata); 67 68 /* 69 * Synchronously drain all reclamation ring's belonging to a task. 70 */ 71 kern_return_t vm_deferred_reclamation_task_drain( 72 task_t task, 73 vm_deferred_reclamation_options_t options); 74 75 /* 76 * Return true if this task has a reclamation ring 77 */ 78 bool vm_deferred_reclamation_task_has_ring(task_t task); 79 80 /* 81 * Create a fork of the given reclamation buffer for a new task. 82 * Parent buffer must be locked and will be unlocked on return. 83 * 84 * This must be called when forking a task that has a reclamation buffer 85 * to duplicate the parent's buffer, and the new buffer must later be 86 * registered by vm_deferred_reclamation_task_fork_register. 87 * 88 * The caller must lock the parent's reclamation buffer BEFORE forking 89 * the parent's vm_map. Otherwise the parent's buffer could get reclaimed 90 * in between the map fork and the buffer fork causing the child's 91 * data strucutres to be out of sync. 92 */ 93 vm_deferred_reclamation_metadata_t vm_deferred_reclamation_task_fork( 94 task_t task, 95 vm_deferred_reclamation_metadata_t parent); 96 97 /* 98 * Add a reclamation buffer returned by vm_deferred_reclamation_task_fork to 99 * the global reclamation queues. 100 * 101 * The child task should call this to ensure that the kernel knows about its 102 * reclamation buffer. This must happen after the child's address space is 103 * fully initialized and able to recieve VM API calls. 104 */ 105 void 106 vm_deferred_reclamation_task_fork_register(vm_deferred_reclamation_metadata_t metadata); 107 108 /* 109 * Set the current thread as the owner of a reclaim buffer. May block. Will 110 * propagate priority. Should be called before forking the owning task. 111 */ 112 void vm_deferred_reclamation_ring_own(vm_deferred_reclamation_metadata_t metadata); 113 114 /* 115 * Release ownership of a reclaim buffer and wakeup any threads waiting for 116 * ownership. Must be called from the thread that acquired ownership. 117 */ 118 void vm_deferred_reclamation_ring_disown(vm_deferred_reclamation_metadata_t metadata); 119 120 /* 121 * Should be called when a task is suspended -- will trigger asynchronous 122 * reclamation of any reclaim rings owned by the task. 123 */ 124 void vm_deferred_reclamation_task_suspend(task_t task); 125 126 /* 127 * Perform Garbage Collection on all reclaim rings 128 */ 129 void vm_deferred_reclamation_gc(vm_deferred_reclamation_gc_action_t action, 130 vm_deferred_reclamation_options_t options); 131 132 #endif /* CONFIG_DEFERRED_RECLAIM */ 133 #endif /* XNU_KERNEL_PRIVATE */ 134 #endif /* __VM_RECLAIM_XNU__ */ 135