1 /* 2 * Copyright (c) 2021 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_H_ 30 #define _VM_RECLAIM_H_ 31 32 #ifdef PRIVATE 33 #if defined(__LP64__) 34 35 #include <mach/mach_types.h> 36 #include <mach/kern_return.h> 37 #include <stdbool.h> 38 39 __BEGIN_DECLS 40 41 typedef struct mach_vm_reclaim_entry_v1_s { 42 mach_vm_address_t address; 43 uint32_t size; 44 uint16_t opcode; 45 uint16_t flags; 46 } mach_vm_reclaim_entry_v1_t; 47 48 typedef struct mach_vm_reclaim_indices_v1_s { 49 _Atomic uint64_t head; 50 _Atomic uint64_t tail; 51 _Atomic uint64_t busy; 52 } mach_vm_reclaim_indices_v1_t; 53 54 #if !KERNEL 55 #define VM_RECLAIM_INDEX_NULL UINT64_MAX 56 57 /* 58 * Userspace interface for placing items in the reclamation buffer and trying to take them back out. 59 * Note that these interfaces are NOT thread safe. It is the caller's responsibility to synchronize concurrent 60 * operations on the same buffer. 61 * 62 * These operations are implemented in libsyscall. 63 */ 64 65 typedef struct mach_vm_reclaim_ringbuffer_v1_s { 66 mach_vm_reclaim_entry_v1_t *buffer; 67 mach_vm_size_t buffer_len; 68 mach_vm_reclaim_indices_v1_t indices; 69 uint64_t va_in_buffer; 70 uint64_t last_accounting_given_to_kernel; 71 } *mach_vm_reclaim_ringbuffer_v1_t; 72 73 kern_return_t mach_vm_reclaim_ringbuffer_init(mach_vm_reclaim_ringbuffer_v1_t ringbuffer); 74 75 /* 76 * Mark the given range as free. 77 * Returns a unique identifier for the range that can be used by reclaim_mark_used 78 * This will update the userspace reclaim buffer accounting, but will not 79 * inform the kernel about the new bytes in the buffer. If the kernel should be informed, 80 * should_update_kernel_accounting will be set to true and the caller should call 81 * mach_vm_reclaim_update_kernel_accounting. That syscall might reclaim the buffer, so 82 * this gives the caller an opportunity to first drop any locks. 83 */ 84 uint64_t mach_vm_reclaim_mark_free( 85 mach_vm_reclaim_ringbuffer_v1_t buffer, 86 mach_vm_address_t start_addr, 87 uint32_t size, 88 bool *should_update_kernel_accounting); 89 90 /* 91 * Attempt to take back the range determined by id. 92 * Returns true iff range can now be used. 93 * Subsequent calls to reclaim_mark_used with the same id are not supported & may return true or false. 94 */ 95 bool mach_vm_reclaim_mark_used( 96 mach_vm_reclaim_ringbuffer_v1_t buffer, 97 uint64_t id, 98 mach_vm_address_t start_addr, 99 uint32_t size); 100 101 /* 102 * Check if the range has been reclaimed. 103 * Returns true if the range is still available. Note that this doesn't claim the range, so it may be reclaimed in parallel. 104 * Note that a return value of false does not guarantee that the kernel has reclaimed the range already (it may just be considering it). 105 */ 106 bool mach_vm_reclaim_is_available( 107 const mach_vm_reclaim_ringbuffer_v1_t buffer, 108 uint64_t id); 109 110 /* 111 * Force the kernel to reclaim at least num_entries_to_reclaim entries from the ringbuffer (if present). 112 * Note that mach_vm_reclaim_mark_free automatically handles the full ringbuffer case. 113 */ 114 kern_return_t mach_vm_reclaim_synchronize( 115 mach_vm_reclaim_ringbuffer_v1_t ringbuffer, 116 mach_vm_size_t num_entries_to_reclaim); 117 118 /* 119 * Let the kernel know how much VA is in the ringbuffer. 120 * The kernel may choose to reclaim from the ringbuffer on this thread. 121 * This should be called whenever mach_vm_reclaim_mark_free returns true in 122 * should_update_kernel_accounting. It may be called at any other time 123 * if the caller wants to update the kernel's accounting & is 124 * thread safe w.r.t. all other mach_vm_reclaim calls. 125 */ 126 kern_return_t mach_vm_reclaim_update_kernel_accounting( 127 const mach_vm_reclaim_ringbuffer_v1_t ring_buffer); 128 129 #endif /* !KENREL */ 130 131 __END_DECLS 132 133 #endif /* PRIVATE */ 134 135 #endif /* __LP64__ */ 136 137 #endif /* _VM_RECLAIM_H_ */ 138