]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2007 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
31 | /* | |
32 | * Mach Operating System | |
33 | * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University | |
34 | * All Rights Reserved. | |
35 | * | |
36 | * Permission to use, copy, modify and distribute this software and its | |
37 | * documentation is hereby granted, provided that both the copyright | |
38 | * notice and this permission notice appear in all copies of the | |
39 | * software, derivative works or modified versions, and any portions | |
40 | * thereof, and that both notices appear in supporting documentation. | |
41 | * | |
42 | * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" | |
43 | * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR | |
44 | * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
45 | * | |
46 | * Carnegie Mellon requests users of this software to return to | |
47 | * | |
48 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
49 | * School of Computer Science | |
50 | * Carnegie Mellon University | |
51 | * Pittsburgh PA 15213-3890 | |
52 | * | |
53 | * any improvements or extensions that they make and grant Carnegie Mellon | |
54 | * the rights to redistribute these changes. | |
55 | */ | |
56 | /* | |
57 | */ | |
58 | /* | |
59 | * File: vm/vm_kern.c | |
60 | * Author: Avadis Tevanian, Jr., Michael Wayne Young | |
61 | * Date: 1985 | |
62 | * | |
63 | * Kernel memory management. | |
64 | */ | |
65 | ||
1c79356b A |
66 | #include <mach/kern_return.h> |
67 | #include <mach/vm_param.h> | |
68 | #include <kern/assert.h> | |
1c79356b A |
69 | #include <kern/thread.h> |
70 | #include <vm/vm_kern.h> | |
71 | #include <vm/vm_map.h> | |
72 | #include <vm/vm_object.h> | |
73 | #include <vm/vm_page.h> | |
5ba3f43e | 74 | #include <vm/vm_compressor.h> |
1c79356b A |
75 | #include <vm/vm_pageout.h> |
76 | #include <kern/misc_protos.h> | |
77 | #include <vm/cpm.h> | |
78 | ||
79 | #include <string.h> | |
2d21ac55 A |
80 | |
81 | #include <libkern/OSDebug.h> | |
5ba3f43e | 82 | #include <libkern/crypto/sha2.h> |
2d21ac55 A |
83 | #include <sys/kdebug.h> |
84 | ||
5ba3f43e A |
85 | #include <san/kasan.h> |
86 | ||
1c79356b A |
87 | /* |
88 | * Variables exported by this module. | |
89 | */ | |
90 | ||
91 | vm_map_t kernel_map; | |
92 | vm_map_t kernel_pageable_map; | |
93 | ||
2d21ac55 A |
94 | extern boolean_t vm_kernel_ready; |
95 | ||
1c79356b A |
96 | /* |
97 | * Forward declarations for internal functions. | |
98 | */ | |
99 | extern kern_return_t kmem_alloc_pages( | |
39037602 A |
100 | vm_object_t object, |
101 | vm_object_offset_t offset, | |
102 | vm_object_size_t size); | |
1c79356b | 103 | |
1c79356b A |
104 | kern_return_t |
105 | kmem_alloc_contig( | |
91447636 A |
106 | vm_map_t map, |
107 | vm_offset_t *addrp, | |
108 | vm_size_t size, | |
109 | vm_offset_t mask, | |
2d21ac55 | 110 | ppnum_t max_pnum, |
b0d623f7 | 111 | ppnum_t pnum_mask, |
3e170ce0 A |
112 | int flags, |
113 | vm_tag_t tag) | |
1c79356b A |
114 | { |
115 | vm_object_t object; | |
1c79356b | 116 | vm_object_offset_t offset; |
91447636 A |
117 | vm_map_offset_t map_addr; |
118 | vm_map_offset_t map_mask; | |
119 | vm_map_size_t map_size, i; | |
1c79356b | 120 | vm_map_entry_t entry; |
91447636 A |
121 | vm_page_t m, pages; |
122 | kern_return_t kr; | |
1c79356b | 123 | |
5ba3f43e A |
124 | assert(VM_KERN_MEMORY_NONE != tag); |
125 | ||
b0d623f7 | 126 | if (map == VM_MAP_NULL || (flags & ~(KMA_KOBJECT | KMA_LOMEM | KMA_NOPAGEWAIT))) |
1c79356b | 127 | return KERN_INVALID_ARGUMENT; |
316670eb | 128 | |
39236c6e A |
129 | map_size = vm_map_round_page(size, |
130 | VM_MAP_PAGE_MASK(map)); | |
316670eb | 131 | map_mask = (vm_map_offset_t)mask; |
1c79356b | 132 | |
316670eb A |
133 | /* Check for zero allocation size (either directly or via overflow) */ |
134 | if (map_size == 0) { | |
1c79356b A |
135 | *addrp = 0; |
136 | return KERN_INVALID_ARGUMENT; | |
137 | } | |
138 | ||
91447636 A |
139 | /* |
140 | * Allocate a new object (if necessary) and the reference we | |
141 | * will be donating to the map entry. We must do this before | |
142 | * locking the map, or risk deadlock with the default pager. | |
143 | */ | |
144 | if ((flags & KMA_KOBJECT) != 0) { | |
145 | object = kernel_object; | |
146 | vm_object_reference(object); | |
1c79356b | 147 | } else { |
91447636 | 148 | object = vm_object_allocate(map_size); |
1c79356b A |
149 | } |
150 | ||
5ba3f43e A |
151 | kr = vm_map_find_space(map, &map_addr, map_size, map_mask, 0, |
152 | VM_MAP_KERNEL_FLAGS_NONE, tag, &entry); | |
91447636 A |
153 | if (KERN_SUCCESS != kr) { |
154 | vm_object_deallocate(object); | |
1c79356b A |
155 | return kr; |
156 | } | |
157 | ||
3e170ce0 A |
158 | if (object == kernel_object) { |
159 | offset = map_addr; | |
160 | } else { | |
161 | offset = 0; | |
162 | } | |
163 | VME_OBJECT_SET(entry, object); | |
164 | VME_OFFSET_SET(entry, offset); | |
91447636 A |
165 | |
166 | /* Take an extra object ref in case the map entry gets deleted */ | |
167 | vm_object_reference(object); | |
1c79356b A |
168 | vm_map_unlock(map); |
169 | ||
b0d623f7 | 170 | kr = cpm_allocate(CAST_DOWN(vm_size_t, map_size), &pages, max_pnum, pnum_mask, FALSE, flags); |
1c79356b A |
171 | |
172 | if (kr != KERN_SUCCESS) { | |
39236c6e A |
173 | vm_map_remove(map, |
174 | vm_map_trunc_page(map_addr, | |
175 | VM_MAP_PAGE_MASK(map)), | |
176 | vm_map_round_page(map_addr + map_size, | |
177 | VM_MAP_PAGE_MASK(map)), | |
178 | 0); | |
91447636 | 179 | vm_object_deallocate(object); |
1c79356b A |
180 | *addrp = 0; |
181 | return kr; | |
182 | } | |
183 | ||
184 | vm_object_lock(object); | |
91447636 | 185 | for (i = 0; i < map_size; i += PAGE_SIZE) { |
1c79356b A |
186 | m = pages; |
187 | pages = NEXT_PAGE(m); | |
0c530ab8 | 188 | *(NEXT_PAGE_PTR(m)) = VM_PAGE_NULL; |
1c79356b A |
189 | m->busy = FALSE; |
190 | vm_page_insert(m, object, offset + i); | |
191 | } | |
192 | vm_object_unlock(object); | |
193 | ||
5ba3f43e | 194 | kr = vm_map_wire_kernel(map, |
39236c6e A |
195 | vm_map_trunc_page(map_addr, |
196 | VM_MAP_PAGE_MASK(map)), | |
197 | vm_map_round_page(map_addr + map_size, | |
198 | VM_MAP_PAGE_MASK(map)), | |
5ba3f43e | 199 | VM_PROT_DEFAULT, tag, |
39236c6e | 200 | FALSE); |
3e170ce0 | 201 | |
39236c6e | 202 | if (kr != KERN_SUCCESS) { |
1c79356b A |
203 | if (object == kernel_object) { |
204 | vm_object_lock(object); | |
91447636 | 205 | vm_object_page_remove(object, offset, offset + map_size); |
1c79356b A |
206 | vm_object_unlock(object); |
207 | } | |
39236c6e A |
208 | vm_map_remove(map, |
209 | vm_map_trunc_page(map_addr, | |
210 | VM_MAP_PAGE_MASK(map)), | |
211 | vm_map_round_page(map_addr + map_size, | |
212 | VM_MAP_PAGE_MASK(map)), | |
213 | 0); | |
91447636 | 214 | vm_object_deallocate(object); |
1c79356b A |
215 | return kr; |
216 | } | |
91447636 A |
217 | vm_object_deallocate(object); |
218 | ||
5ba3f43e | 219 | if (object == kernel_object) { |
91447636 | 220 | vm_map_simplify(map, map_addr); |
5ba3f43e A |
221 | vm_tag_update_size(tag, map_size); |
222 | } | |
b0d623f7 A |
223 | *addrp = (vm_offset_t) map_addr; |
224 | assert((vm_map_offset_t) *addrp == map_addr); | |
5ba3f43e | 225 | |
1c79356b A |
226 | return KERN_SUCCESS; |
227 | } | |
228 | ||
229 | /* | |
230 | * Master entry point for allocating kernel memory. | |
231 | * NOTE: this routine is _never_ interrupt safe. | |
232 | * | |
233 | * map : map to allocate into | |
234 | * addrp : pointer to start address of new memory | |
235 | * size : size of memory requested | |
236 | * flags : options | |
237 | * KMA_HERE *addrp is base address, else "anywhere" | |
238 | * KMA_NOPAGEWAIT don't wait for pages if unavailable | |
239 | * KMA_KOBJECT use kernel_object | |
0c530ab8 A |
240 | * KMA_LOMEM support for 32 bit devices in a 64 bit world |
241 | * if set and a lomemory pool is available | |
242 | * grab pages from it... this also implies | |
243 | * KMA_NOPAGEWAIT | |
1c79356b A |
244 | */ |
245 | ||
246 | kern_return_t | |
247 | kernel_memory_allocate( | |
39037602 A |
248 | vm_map_t map, |
249 | vm_offset_t *addrp, | |
250 | vm_size_t size, | |
251 | vm_offset_t mask, | |
3e170ce0 A |
252 | int flags, |
253 | vm_tag_t tag) | |
1c79356b | 254 | { |
91447636 A |
255 | vm_object_t object; |
256 | vm_object_offset_t offset; | |
b0d623f7 | 257 | vm_object_offset_t pg_offset; |
bd504ef0 | 258 | vm_map_entry_t entry = NULL; |
2d21ac55 | 259 | vm_map_offset_t map_addr, fill_start; |
91447636 | 260 | vm_map_offset_t map_mask; |
2d21ac55 | 261 | vm_map_size_t map_size, fill_size; |
39236c6e | 262 | kern_return_t kr, pe_result; |
2d21ac55 | 263 | vm_page_t mem; |
b0d623f7 A |
264 | vm_page_t guard_page_list = NULL; |
265 | vm_page_t wired_page_list = NULL; | |
266 | int guard_page_count = 0; | |
267 | int wired_page_count = 0; | |
268 | int i; | |
2d21ac55 | 269 | int vm_alloc_flags; |
5ba3f43e | 270 | vm_map_kernel_flags_t vmk_flags; |
316670eb | 271 | vm_prot_t kma_prot; |
2d21ac55 A |
272 | |
273 | if (! vm_kernel_ready) { | |
274 | panic("kernel_memory_allocate: VM is not ready"); | |
275 | } | |
1c79356b | 276 | |
39236c6e A |
277 | map_size = vm_map_round_page(size, |
278 | VM_MAP_PAGE_MASK(map)); | |
91447636 | 279 | map_mask = (vm_map_offset_t) mask; |
3e170ce0 | 280 | |
5ba3f43e A |
281 | vm_alloc_flags = 0; //VM_MAKE_TAG(tag); |
282 | vmk_flags = VM_MAP_KERNEL_FLAGS_NONE; | |
2d21ac55 | 283 | |
316670eb A |
284 | /* Check for zero allocation size (either directly or via overflow) */ |
285 | if (map_size == 0) { | |
286 | *addrp = 0; | |
287 | return KERN_INVALID_ARGUMENT; | |
288 | } | |
b0d623f7 A |
289 | |
290 | /* | |
291 | * limit the size of a single extent of wired memory | |
292 | * to try and limit the damage to the system if | |
293 | * too many pages get wired down | |
4bd07ac2 A |
294 | * limit raised to 2GB with 128GB max physical limit, |
295 | * but scaled by installed memory above this | |
b0d623f7 | 296 | */ |
4bd07ac2 | 297 | if ( !(flags & KMA_VAONLY) && map_size > MAX(1ULL<<31, sane_size/64)) { |
b0d623f7 A |
298 | return KERN_RESOURCE_SHORTAGE; |
299 | } | |
300 | ||
2d21ac55 A |
301 | /* |
302 | * Guard pages: | |
303 | * | |
304 | * Guard pages are implemented as ficticious pages. By placing guard pages | |
305 | * on either end of a stack, they can help detect cases where a thread walks | |
306 | * off either end of its stack. They are allocated and set up here and attempts | |
307 | * to access those pages are trapped in vm_fault_page(). | |
308 | * | |
309 | * The map_size we were passed may include extra space for | |
310 | * guard pages. If those were requested, then back it out of fill_size | |
311 | * since vm_map_find_space() takes just the actual size not including | |
312 | * guard pages. Similarly, fill_start indicates where the actual pages | |
313 | * will begin in the range. | |
314 | */ | |
315 | ||
316 | fill_start = 0; | |
317 | fill_size = map_size; | |
b0d623f7 | 318 | |
2d21ac55 | 319 | if (flags & KMA_GUARD_FIRST) { |
5ba3f43e | 320 | vmk_flags.vmkf_guard_before = TRUE; |
2d21ac55 A |
321 | fill_start += PAGE_SIZE_64; |
322 | fill_size -= PAGE_SIZE_64; | |
323 | if (map_size < fill_start + fill_size) { | |
324 | /* no space for a guard page */ | |
325 | *addrp = 0; | |
326 | return KERN_INVALID_ARGUMENT; | |
327 | } | |
b0d623f7 | 328 | guard_page_count++; |
2d21ac55 A |
329 | } |
330 | if (flags & KMA_GUARD_LAST) { | |
5ba3f43e | 331 | vmk_flags.vmkf_guard_after = TRUE; |
2d21ac55 A |
332 | fill_size -= PAGE_SIZE_64; |
333 | if (map_size <= fill_start + fill_size) { | |
334 | /* no space for a guard page */ | |
335 | *addrp = 0; | |
336 | return KERN_INVALID_ARGUMENT; | |
337 | } | |
b0d623f7 A |
338 | guard_page_count++; |
339 | } | |
340 | wired_page_count = (int) (fill_size / PAGE_SIZE_64); | |
341 | assert(wired_page_count * PAGE_SIZE_64 == fill_size); | |
342 | ||
343 | for (i = 0; i < guard_page_count; i++) { | |
344 | for (;;) { | |
345 | mem = vm_page_grab_guard(); | |
346 | ||
347 | if (mem != VM_PAGE_NULL) | |
348 | break; | |
349 | if (flags & KMA_NOPAGEWAIT) { | |
350 | kr = KERN_RESOURCE_SHORTAGE; | |
351 | goto out; | |
352 | } | |
353 | vm_page_more_fictitious(); | |
354 | } | |
39037602 | 355 | mem->snext = guard_page_list; |
b0d623f7 A |
356 | guard_page_list = mem; |
357 | } | |
358 | ||
39236c6e | 359 | if (! (flags & KMA_VAONLY)) { |
b0d623f7 A |
360 | for (i = 0; i < wired_page_count; i++) { |
361 | uint64_t unavailable; | |
362 | ||
363 | for (;;) { | |
364 | if (flags & KMA_LOMEM) | |
365 | mem = vm_page_grablo(); | |
366 | else | |
367 | mem = vm_page_grab(); | |
368 | ||
369 | if (mem != VM_PAGE_NULL) | |
370 | break; | |
371 | ||
372 | if (flags & KMA_NOPAGEWAIT) { | |
373 | kr = KERN_RESOURCE_SHORTAGE; | |
374 | goto out; | |
375 | } | |
0b4c1975 A |
376 | if ((flags & KMA_LOMEM) && (vm_lopage_needed == TRUE)) { |
377 | kr = KERN_RESOURCE_SHORTAGE; | |
378 | goto out; | |
379 | } | |
b0d623f7 A |
380 | unavailable = (vm_page_wire_count + vm_page_free_target) * PAGE_SIZE; |
381 | ||
382 | if (unavailable > max_mem || map_size > (max_mem - unavailable)) { | |
383 | kr = KERN_RESOURCE_SHORTAGE; | |
384 | goto out; | |
385 | } | |
386 | VM_PAGE_WAIT(); | |
387 | } | |
5ba3f43e | 388 | if (KMA_ZERO & flags) vm_page_zero_fill(mem); |
39037602 | 389 | mem->snext = wired_page_list; |
b0d623f7 | 390 | wired_page_list = mem; |
2d21ac55 | 391 | } |
39236c6e | 392 | } |
91447636 A |
393 | |
394 | /* | |
395 | * Allocate a new object (if necessary). We must do this before | |
396 | * locking the map, or risk deadlock with the default pager. | |
397 | */ | |
398 | if ((flags & KMA_KOBJECT) != 0) { | |
1c79356b | 399 | object = kernel_object; |
91447636 | 400 | vm_object_reference(object); |
39236c6e A |
401 | } else if ((flags & KMA_COMPRESSOR) != 0) { |
402 | object = compressor_object; | |
403 | vm_object_reference(object); | |
91447636 A |
404 | } else { |
405 | object = vm_object_allocate(map_size); | |
1c79356b | 406 | } |
91447636 | 407 | |
5ba3f43e A |
408 | if (flags & KMA_ATOMIC) |
409 | vmk_flags.vmkf_atomic_entry = TRUE; | |
410 | ||
2d21ac55 A |
411 | kr = vm_map_find_space(map, &map_addr, |
412 | fill_size, map_mask, | |
5ba3f43e | 413 | vm_alloc_flags, vmk_flags, tag, &entry); |
91447636 A |
414 | if (KERN_SUCCESS != kr) { |
415 | vm_object_deallocate(object); | |
b0d623f7 | 416 | goto out; |
1c79356b | 417 | } |
2d21ac55 | 418 | |
3e170ce0 A |
419 | if (object == kernel_object || object == compressor_object) { |
420 | offset = map_addr; | |
421 | } else { | |
422 | offset = 0; | |
423 | } | |
424 | VME_OBJECT_SET(entry, object); | |
425 | VME_OFFSET_SET(entry, offset); | |
39236c6e A |
426 | |
427 | if (object != compressor_object) | |
428 | entry->wired_count++; | |
b0d623f7 A |
429 | |
430 | if (flags & KMA_PERMANENT) | |
431 | entry->permanent = TRUE; | |
432 | ||
39236c6e | 433 | if (object != kernel_object && object != compressor_object) |
b0d623f7 | 434 | vm_object_reference(object); |
1c79356b A |
435 | |
436 | vm_object_lock(object); | |
b0d623f7 | 437 | vm_map_unlock(map); |
1c79356b | 438 | |
b0d623f7 A |
439 | pg_offset = 0; |
440 | ||
441 | if (fill_start) { | |
442 | if (guard_page_list == NULL) | |
443 | panic("kernel_memory_allocate: guard_page_list == NULL"); | |
444 | ||
445 | mem = guard_page_list; | |
39037602 A |
446 | guard_page_list = mem->snext; |
447 | mem->snext = NULL; | |
b0d623f7 A |
448 | |
449 | vm_page_insert(mem, object, offset + pg_offset); | |
2d21ac55 | 450 | |
2d21ac55 | 451 | mem->busy = FALSE; |
b0d623f7 | 452 | pg_offset += PAGE_SIZE_64; |
2d21ac55 | 453 | } |
316670eb A |
454 | |
455 | kma_prot = VM_PROT_READ | VM_PROT_WRITE; | |
456 | ||
5ba3f43e A |
457 | #if KASAN |
458 | if (!(flags & KMA_VAONLY)) { | |
459 | /* for VAONLY mappings we notify in populate only */ | |
460 | kasan_notify_address(map_addr, size); | |
461 | } | |
462 | #endif | |
463 | ||
39236c6e A |
464 | if (flags & KMA_VAONLY) { |
465 | pg_offset = fill_start + fill_size; | |
466 | } else { | |
b0d623f7 A |
467 | for (pg_offset = fill_start; pg_offset < fill_start + fill_size; pg_offset += PAGE_SIZE_64) { |
468 | if (wired_page_list == NULL) | |
469 | panic("kernel_memory_allocate: wired_page_list == NULL"); | |
2d21ac55 | 470 | |
b0d623f7 | 471 | mem = wired_page_list; |
39037602 A |
472 | wired_page_list = mem->snext; |
473 | mem->snext = NULL; | |
474 | ||
475 | assert(mem->wire_count == 0); | |
476 | assert(mem->vm_page_q_state == VM_PAGE_NOT_ON_Q); | |
477 | ||
478 | mem->vm_page_q_state = VM_PAGE_IS_WIRED; | |
b0d623f7 | 479 | mem->wire_count++; |
39037602 A |
480 | if (__improbable(mem->wire_count == 0)) { |
481 | panic("kernel_memory_allocate(%p): wire_count overflow", | |
482 | mem); | |
483 | } | |
2d21ac55 | 484 | |
3e170ce0 | 485 | vm_page_insert_wired(mem, object, offset + pg_offset, tag); |
0c530ab8 | 486 | |
1c79356b | 487 | mem->busy = FALSE; |
b0d623f7 A |
488 | mem->pmapped = TRUE; |
489 | mem->wpmapped = TRUE; | |
490 | ||
39236c6e A |
491 | PMAP_ENTER_OPTIONS(kernel_pmap, map_addr + pg_offset, mem, |
492 | kma_prot, VM_PROT_NONE, ((flags & KMA_KSTACK) ? VM_MEM_STACK : 0), TRUE, | |
493 | PMAP_OPTIONS_NOWAIT, pe_result); | |
494 | ||
495 | if (pe_result == KERN_RESOURCE_SHORTAGE) { | |
496 | vm_object_unlock(object); | |
0b4c1975 | 497 | |
39236c6e | 498 | PMAP_ENTER(kernel_pmap, map_addr + pg_offset, mem, |
5ba3f43e A |
499 | kma_prot, VM_PROT_NONE, ((flags & KMA_KSTACK) ? VM_MEM_STACK : 0), TRUE, |
500 | pe_result); | |
39236c6e A |
501 | |
502 | vm_object_lock(object); | |
503 | } | |
5ba3f43e A |
504 | |
505 | assert(pe_result == KERN_SUCCESS); | |
506 | ||
0b4c1975 A |
507 | if (flags & KMA_NOENCRYPT) { |
508 | bzero(CAST_DOWN(void *, (map_addr + pg_offset)), PAGE_SIZE); | |
509 | ||
39037602 | 510 | pmap_set_noencrypt(VM_PAGE_GET_PHYS_PAGE(mem)); |
0b4c1975 | 511 | } |
1c79356b | 512 | } |
5ba3f43e | 513 | if (kernel_object == object) vm_tag_update_size(tag, fill_size); |
39236c6e | 514 | } |
b0d623f7 A |
515 | if ((fill_start + fill_size) < map_size) { |
516 | if (guard_page_list == NULL) | |
517 | panic("kernel_memory_allocate: guard_page_list == NULL"); | |
1c79356b | 518 | |
b0d623f7 | 519 | mem = guard_page_list; |
39037602 A |
520 | guard_page_list = mem->snext; |
521 | mem->snext = NULL; | |
b0d623f7 A |
522 | |
523 | vm_page_insert(mem, object, offset + pg_offset); | |
2d21ac55 | 524 | |
2d21ac55 | 525 | mem->busy = FALSE; |
1c79356b | 526 | } |
b0d623f7 A |
527 | if (guard_page_list || wired_page_list) |
528 | panic("kernel_memory_allocate: non empty list\n"); | |
2d21ac55 | 529 | |
39236c6e | 530 | if (! (flags & KMA_VAONLY)) { |
b0d623f7 A |
531 | vm_page_lockspin_queues(); |
532 | vm_page_wire_count += wired_page_count; | |
533 | vm_page_unlock_queues(); | |
39236c6e | 534 | } |
2d21ac55 | 535 | |
b0d623f7 A |
536 | vm_object_unlock(object); |
537 | ||
538 | /* | |
539 | * now that the pages are wired, we no longer have to fear coalesce | |
540 | */ | |
39236c6e | 541 | if (object == kernel_object || object == compressor_object) |
91447636 | 542 | vm_map_simplify(map, map_addr); |
b0d623f7 A |
543 | else |
544 | vm_object_deallocate(object); | |
1c79356b A |
545 | |
546 | /* | |
547 | * Return the memory, not zeroed. | |
548 | */ | |
91447636 | 549 | *addrp = CAST_DOWN(vm_offset_t, map_addr); |
1c79356b | 550 | return KERN_SUCCESS; |
2d21ac55 | 551 | |
b0d623f7 A |
552 | out: |
553 | if (guard_page_list) | |
554 | vm_page_free_list(guard_page_list, FALSE); | |
555 | ||
556 | if (wired_page_list) | |
557 | vm_page_free_list(wired_page_list, FALSE); | |
558 | ||
559 | return kr; | |
1c79356b A |
560 | } |
561 | ||
39236c6e A |
562 | kern_return_t |
563 | kernel_memory_populate( | |
564 | vm_map_t map, | |
565 | vm_offset_t addr, | |
566 | vm_size_t size, | |
3e170ce0 A |
567 | int flags, |
568 | vm_tag_t tag) | |
39236c6e A |
569 | { |
570 | vm_object_t object; | |
571 | vm_object_offset_t offset, pg_offset; | |
572 | kern_return_t kr, pe_result; | |
573 | vm_page_t mem; | |
574 | vm_page_t page_list = NULL; | |
575 | int page_count = 0; | |
576 | int i; | |
577 | ||
578 | page_count = (int) (size / PAGE_SIZE_64); | |
579 | ||
580 | assert((flags & (KMA_COMPRESSOR|KMA_KOBJECT)) != (KMA_COMPRESSOR|KMA_KOBJECT)); | |
581 | ||
582 | if (flags & KMA_COMPRESSOR) { | |
583 | ||
3e170ce0 A |
584 | pg_offset = page_count * PAGE_SIZE_64; |
585 | ||
586 | do { | |
39236c6e A |
587 | for (;;) { |
588 | mem = vm_page_grab(); | |
589 | ||
590 | if (mem != VM_PAGE_NULL) | |
591 | break; | |
592 | ||
593 | VM_PAGE_WAIT(); | |
594 | } | |
5ba3f43e | 595 | if (KMA_ZERO & flags) vm_page_zero_fill(mem); |
39037602 | 596 | mem->snext = page_list; |
39236c6e | 597 | page_list = mem; |
3e170ce0 A |
598 | |
599 | pg_offset -= PAGE_SIZE_64; | |
600 | ||
601 | kr = pmap_enter_options(kernel_pmap, | |
39037602 | 602 | addr + pg_offset, VM_PAGE_GET_PHYS_PAGE(mem), |
3e170ce0 A |
603 | VM_PROT_READ | VM_PROT_WRITE, VM_PROT_NONE, 0, TRUE, |
604 | PMAP_OPTIONS_INTERNAL, NULL); | |
605 | assert(kr == KERN_SUCCESS); | |
606 | ||
607 | } while (pg_offset); | |
608 | ||
39236c6e A |
609 | offset = addr; |
610 | object = compressor_object; | |
611 | ||
612 | vm_object_lock(object); | |
613 | ||
614 | for (pg_offset = 0; | |
615 | pg_offset < size; | |
616 | pg_offset += PAGE_SIZE_64) { | |
617 | ||
618 | mem = page_list; | |
39037602 A |
619 | page_list = mem->snext; |
620 | mem->snext = NULL; | |
39236c6e A |
621 | |
622 | vm_page_insert(mem, object, offset + pg_offset); | |
623 | assert(mem->busy); | |
624 | ||
39236c6e A |
625 | mem->busy = FALSE; |
626 | mem->pmapped = TRUE; | |
627 | mem->wpmapped = TRUE; | |
39037602 | 628 | mem->vm_page_q_state = VM_PAGE_USED_BY_COMPRESSOR; |
39236c6e A |
629 | } |
630 | vm_object_unlock(object); | |
631 | ||
5ba3f43e A |
632 | #if KASAN |
633 | if (map == compressor_map) { | |
634 | kasan_notify_address_nopoison(addr, size); | |
635 | } else { | |
636 | kasan_notify_address(addr, size); | |
637 | } | |
638 | #endif | |
39236c6e A |
639 | return KERN_SUCCESS; |
640 | } | |
641 | ||
642 | for (i = 0; i < page_count; i++) { | |
643 | for (;;) { | |
644 | if (flags & KMA_LOMEM) | |
645 | mem = vm_page_grablo(); | |
646 | else | |
647 | mem = vm_page_grab(); | |
648 | ||
649 | if (mem != VM_PAGE_NULL) | |
650 | break; | |
651 | ||
652 | if (flags & KMA_NOPAGEWAIT) { | |
653 | kr = KERN_RESOURCE_SHORTAGE; | |
654 | goto out; | |
655 | } | |
656 | if ((flags & KMA_LOMEM) && | |
657 | (vm_lopage_needed == TRUE)) { | |
658 | kr = KERN_RESOURCE_SHORTAGE; | |
659 | goto out; | |
660 | } | |
661 | VM_PAGE_WAIT(); | |
662 | } | |
5ba3f43e | 663 | if (KMA_ZERO & flags) vm_page_zero_fill(mem); |
39037602 | 664 | mem->snext = page_list; |
39236c6e A |
665 | page_list = mem; |
666 | } | |
667 | if (flags & KMA_KOBJECT) { | |
668 | offset = addr; | |
669 | object = kernel_object; | |
670 | ||
671 | vm_object_lock(object); | |
672 | } else { | |
673 | /* | |
674 | * If it's not the kernel object, we need to: | |
675 | * lock map; | |
676 | * lookup entry; | |
677 | * lock object; | |
678 | * take reference on object; | |
679 | * unlock map; | |
680 | */ | |
681 | panic("kernel_memory_populate(%p,0x%llx,0x%llx,0x%x): " | |
682 | "!KMA_KOBJECT", | |
683 | map, (uint64_t) addr, (uint64_t) size, flags); | |
684 | } | |
685 | ||
686 | for (pg_offset = 0; | |
687 | pg_offset < size; | |
688 | pg_offset += PAGE_SIZE_64) { | |
689 | ||
690 | if (page_list == NULL) | |
691 | panic("kernel_memory_populate: page_list == NULL"); | |
692 | ||
693 | mem = page_list; | |
39037602 A |
694 | page_list = mem->snext; |
695 | mem->snext = NULL; | |
39236c6e | 696 | |
39037602 A |
697 | assert(mem->vm_page_q_state == VM_PAGE_NOT_ON_Q); |
698 | mem->vm_page_q_state = VM_PAGE_IS_WIRED; | |
39236c6e | 699 | mem->wire_count++; |
39037602 A |
700 | if (__improbable(mem->wire_count == 0)) { |
701 | panic("kernel_memory_populate(%p): wire_count overflow", | |
702 | mem); | |
703 | } | |
39236c6e | 704 | |
3e170ce0 | 705 | vm_page_insert_wired(mem, object, offset + pg_offset, tag); |
39236c6e A |
706 | |
707 | mem->busy = FALSE; | |
708 | mem->pmapped = TRUE; | |
709 | mem->wpmapped = TRUE; | |
710 | ||
711 | PMAP_ENTER_OPTIONS(kernel_pmap, addr + pg_offset, mem, | |
712 | VM_PROT_READ | VM_PROT_WRITE, VM_PROT_NONE, | |
713 | ((flags & KMA_KSTACK) ? VM_MEM_STACK : 0), TRUE, | |
714 | PMAP_OPTIONS_NOWAIT, pe_result); | |
715 | ||
716 | if (pe_result == KERN_RESOURCE_SHORTAGE) { | |
717 | ||
718 | vm_object_unlock(object); | |
719 | ||
720 | PMAP_ENTER(kernel_pmap, addr + pg_offset, mem, | |
721 | VM_PROT_READ | VM_PROT_WRITE, VM_PROT_NONE, | |
5ba3f43e A |
722 | ((flags & KMA_KSTACK) ? VM_MEM_STACK : 0), TRUE, |
723 | pe_result); | |
39236c6e A |
724 | |
725 | vm_object_lock(object); | |
726 | } | |
5ba3f43e A |
727 | |
728 | assert(pe_result == KERN_SUCCESS); | |
729 | ||
39236c6e A |
730 | if (flags & KMA_NOENCRYPT) { |
731 | bzero(CAST_DOWN(void *, (addr + pg_offset)), PAGE_SIZE); | |
39037602 | 732 | pmap_set_noencrypt(VM_PAGE_GET_PHYS_PAGE(mem)); |
39236c6e A |
733 | } |
734 | } | |
735 | vm_page_lock_queues(); | |
736 | vm_page_wire_count += page_count; | |
737 | vm_page_unlock_queues(); | |
738 | ||
5ba3f43e A |
739 | if (kernel_object == object) vm_tag_update_size(tag, size); |
740 | ||
39236c6e A |
741 | vm_object_unlock(object); |
742 | ||
5ba3f43e A |
743 | #if KASAN |
744 | if (map == compressor_map) { | |
745 | kasan_notify_address_nopoison(addr, size); | |
746 | } else { | |
747 | kasan_notify_address(addr, size); | |
748 | } | |
749 | #endif | |
39236c6e A |
750 | return KERN_SUCCESS; |
751 | ||
752 | out: | |
753 | if (page_list) | |
754 | vm_page_free_list(page_list, FALSE); | |
755 | ||
756 | return kr; | |
757 | } | |
758 | ||
759 | ||
760 | void | |
761 | kernel_memory_depopulate( | |
762 | vm_map_t map, | |
763 | vm_offset_t addr, | |
764 | vm_size_t size, | |
765 | int flags) | |
766 | { | |
767 | vm_object_t object; | |
768 | vm_object_offset_t offset, pg_offset; | |
769 | vm_page_t mem; | |
770 | vm_page_t local_freeq = NULL; | |
771 | ||
772 | assert((flags & (KMA_COMPRESSOR|KMA_KOBJECT)) != (KMA_COMPRESSOR|KMA_KOBJECT)); | |
773 | ||
774 | if (flags & KMA_COMPRESSOR) { | |
775 | offset = addr; | |
776 | object = compressor_object; | |
777 | ||
778 | vm_object_lock(object); | |
779 | } else if (flags & KMA_KOBJECT) { | |
780 | offset = addr; | |
781 | object = kernel_object; | |
39236c6e A |
782 | vm_object_lock(object); |
783 | } else { | |
784 | offset = 0; | |
785 | object = NULL; | |
786 | /* | |
787 | * If it's not the kernel object, we need to: | |
788 | * lock map; | |
789 | * lookup entry; | |
790 | * lock object; | |
791 | * unlock map; | |
792 | */ | |
793 | panic("kernel_memory_depopulate(%p,0x%llx,0x%llx,0x%x): " | |
794 | "!KMA_KOBJECT", | |
795 | map, (uint64_t) addr, (uint64_t) size, flags); | |
796 | } | |
797 | pmap_protect(kernel_map->pmap, offset, offset + size, VM_PROT_NONE); | |
798 | ||
799 | for (pg_offset = 0; | |
800 | pg_offset < size; | |
801 | pg_offset += PAGE_SIZE_64) { | |
802 | ||
803 | mem = vm_page_lookup(object, offset + pg_offset); | |
804 | ||
805 | assert(mem); | |
39037602 A |
806 | |
807 | if (mem->vm_page_q_state != VM_PAGE_USED_BY_COMPRESSOR) | |
808 | pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(mem)); | |
39236c6e A |
809 | |
810 | mem->busy = TRUE; | |
811 | ||
812 | assert(mem->tabled); | |
813 | vm_page_remove(mem, TRUE); | |
814 | assert(mem->busy); | |
815 | ||
39037602 A |
816 | assert(mem->pageq.next == 0 && mem->pageq.prev == 0); |
817 | assert((mem->vm_page_q_state == VM_PAGE_USED_BY_COMPRESSOR) || | |
818 | (mem->vm_page_q_state == VM_PAGE_NOT_ON_Q)); | |
819 | ||
820 | mem->vm_page_q_state = VM_PAGE_NOT_ON_Q; | |
821 | mem->snext = local_freeq; | |
39236c6e A |
822 | local_freeq = mem; |
823 | } | |
824 | vm_object_unlock(object); | |
825 | ||
826 | if (local_freeq) | |
827 | vm_page_free_list(local_freeq, TRUE); | |
828 | } | |
829 | ||
1c79356b A |
830 | /* |
831 | * kmem_alloc: | |
832 | * | |
833 | * Allocate wired-down memory in the kernel's address map | |
834 | * or a submap. The memory is not zero-filled. | |
835 | */ | |
836 | ||
837 | kern_return_t | |
3e170ce0 | 838 | kmem_alloc_external( |
1c79356b A |
839 | vm_map_t map, |
840 | vm_offset_t *addrp, | |
841 | vm_size_t size) | |
842 | { | |
3e170ce0 A |
843 | return (kmem_alloc(map, addrp, size, vm_tag_bt())); |
844 | } | |
845 | ||
39037602 | 846 | |
3e170ce0 A |
847 | kern_return_t |
848 | kmem_alloc( | |
849 | vm_map_t map, | |
850 | vm_offset_t *addrp, | |
851 | vm_size_t size, | |
39037602 A |
852 | vm_tag_t tag) |
853 | { | |
854 | return kmem_alloc_flags(map, addrp, size, tag, 0); | |
855 | } | |
856 | ||
857 | kern_return_t | |
858 | kmem_alloc_flags( | |
859 | vm_map_t map, | |
860 | vm_offset_t *addrp, | |
861 | vm_size_t size, | |
862 | vm_tag_t tag, | |
863 | int flags) | |
3e170ce0 | 864 | { |
39037602 | 865 | kern_return_t kr = kernel_memory_allocate(map, addrp, size, 0, flags, tag); |
2d21ac55 A |
866 | TRACE_MACHLEAKS(KMEM_ALLOC_CODE, KMEM_ALLOC_CODE_2, size, *addrp); |
867 | return kr; | |
1c79356b A |
868 | } |
869 | ||
870 | /* | |
871 | * kmem_realloc: | |
872 | * | |
873 | * Reallocate wired-down memory in the kernel's address map | |
874 | * or a submap. Newly allocated pages are not zeroed. | |
875 | * This can only be used on regions allocated with kmem_alloc. | |
876 | * | |
877 | * If successful, the pages in the old region are mapped twice. | |
878 | * The old region is unchanged. Use kmem_free to get rid of it. | |
879 | */ | |
880 | kern_return_t | |
881 | kmem_realloc( | |
91447636 A |
882 | vm_map_t map, |
883 | vm_offset_t oldaddr, | |
884 | vm_size_t oldsize, | |
885 | vm_offset_t *newaddrp, | |
3e170ce0 A |
886 | vm_size_t newsize, |
887 | vm_tag_t tag) | |
1c79356b | 888 | { |
91447636 A |
889 | vm_object_t object; |
890 | vm_object_offset_t offset; | |
891 | vm_map_offset_t oldmapmin; | |
892 | vm_map_offset_t oldmapmax; | |
893 | vm_map_offset_t newmapaddr; | |
894 | vm_map_size_t oldmapsize; | |
895 | vm_map_size_t newmapsize; | |
896 | vm_map_entry_t oldentry; | |
897 | vm_map_entry_t newentry; | |
898 | vm_page_t mem; | |
899 | kern_return_t kr; | |
1c79356b | 900 | |
39236c6e A |
901 | oldmapmin = vm_map_trunc_page(oldaddr, |
902 | VM_MAP_PAGE_MASK(map)); | |
903 | oldmapmax = vm_map_round_page(oldaddr + oldsize, | |
904 | VM_MAP_PAGE_MASK(map)); | |
91447636 | 905 | oldmapsize = oldmapmax - oldmapmin; |
39236c6e A |
906 | newmapsize = vm_map_round_page(newsize, |
907 | VM_MAP_PAGE_MASK(map)); | |
1c79356b | 908 | |
1c79356b A |
909 | |
910 | /* | |
911 | * Find the VM object backing the old region. | |
912 | */ | |
913 | ||
b4c24cb9 A |
914 | vm_map_lock(map); |
915 | ||
91447636 | 916 | if (!vm_map_lookup_entry(map, oldmapmin, &oldentry)) |
1c79356b | 917 | panic("kmem_realloc"); |
3e170ce0 | 918 | object = VME_OBJECT(oldentry); |
1c79356b A |
919 | |
920 | /* | |
921 | * Increase the size of the object and | |
922 | * fill in the new region. | |
923 | */ | |
924 | ||
925 | vm_object_reference(object); | |
b4c24cb9 A |
926 | /* by grabbing the object lock before unlocking the map */ |
927 | /* we guarantee that we will panic if more than one */ | |
928 | /* attempt is made to realloc a kmem_alloc'd area */ | |
1c79356b | 929 | vm_object_lock(object); |
b4c24cb9 | 930 | vm_map_unlock(map); |
6d2010ae | 931 | if (object->vo_size != oldmapsize) |
1c79356b | 932 | panic("kmem_realloc"); |
6d2010ae | 933 | object->vo_size = newmapsize; |
1c79356b A |
934 | vm_object_unlock(object); |
935 | ||
b4c24cb9 A |
936 | /* allocate the new pages while expanded portion of the */ |
937 | /* object is still not mapped */ | |
91447636 A |
938 | kmem_alloc_pages(object, vm_object_round_page(oldmapsize), |
939 | vm_object_round_page(newmapsize-oldmapsize)); | |
1c79356b A |
940 | |
941 | /* | |
b4c24cb9 | 942 | * Find space for the new region. |
1c79356b A |
943 | */ |
944 | ||
91447636 | 945 | kr = vm_map_find_space(map, &newmapaddr, newmapsize, |
5ba3f43e A |
946 | (vm_map_offset_t) 0, 0, |
947 | VM_MAP_KERNEL_FLAGS_NONE, | |
948 | tag, | |
949 | &newentry); | |
b4c24cb9 A |
950 | if (kr != KERN_SUCCESS) { |
951 | vm_object_lock(object); | |
91447636 A |
952 | for(offset = oldmapsize; |
953 | offset < newmapsize; offset += PAGE_SIZE) { | |
b4c24cb9 | 954 | if ((mem = vm_page_lookup(object, offset)) != VM_PAGE_NULL) { |
b0d623f7 | 955 | VM_PAGE_FREE(mem); |
b4c24cb9 A |
956 | } |
957 | } | |
6d2010ae | 958 | object->vo_size = oldmapsize; |
b4c24cb9 A |
959 | vm_object_unlock(object); |
960 | vm_object_deallocate(object); | |
961 | return kr; | |
962 | } | |
3e170ce0 A |
963 | VME_OBJECT_SET(newentry, object); |
964 | VME_OFFSET_SET(newentry, 0); | |
3e170ce0 | 965 | assert(newentry->wired_count == 0); |
b4c24cb9 A |
966 | |
967 | ||
968 | /* add an extra reference in case we have someone doing an */ | |
969 | /* unexpected deallocate */ | |
970 | vm_object_reference(object); | |
1c79356b A |
971 | vm_map_unlock(map); |
972 | ||
5ba3f43e A |
973 | kr = vm_map_wire_kernel(map, newmapaddr, newmapaddr + newmapsize, |
974 | VM_PROT_DEFAULT, tag, FALSE); | |
91447636 A |
975 | if (KERN_SUCCESS != kr) { |
976 | vm_map_remove(map, newmapaddr, newmapaddr + newmapsize, 0); | |
b4c24cb9 | 977 | vm_object_lock(object); |
91447636 | 978 | for(offset = oldsize; offset < newmapsize; offset += PAGE_SIZE) { |
b4c24cb9 | 979 | if ((mem = vm_page_lookup(object, offset)) != VM_PAGE_NULL) { |
b0d623f7 | 980 | VM_PAGE_FREE(mem); |
b4c24cb9 A |
981 | } |
982 | } | |
6d2010ae | 983 | object->vo_size = oldmapsize; |
b4c24cb9 A |
984 | vm_object_unlock(object); |
985 | vm_object_deallocate(object); | |
986 | return (kr); | |
987 | } | |
988 | vm_object_deallocate(object); | |
1c79356b | 989 | |
5ba3f43e A |
990 | if (kernel_object == object) vm_tag_update_size(tag, newmapsize); |
991 | ||
91447636 | 992 | *newaddrp = CAST_DOWN(vm_offset_t, newmapaddr); |
1c79356b A |
993 | return KERN_SUCCESS; |
994 | } | |
995 | ||
996 | /* | |
b0d623f7 | 997 | * kmem_alloc_kobject: |
1c79356b A |
998 | * |
999 | * Allocate wired-down memory in the kernel's address map | |
1000 | * or a submap. The memory is not zero-filled. | |
1001 | * | |
1002 | * The memory is allocated in the kernel_object. | |
1003 | * It may not be copied with vm_map_copy, and | |
1004 | * it may not be reallocated with kmem_realloc. | |
1005 | */ | |
1006 | ||
1007 | kern_return_t | |
3e170ce0 | 1008 | kmem_alloc_kobject_external( |
1c79356b A |
1009 | vm_map_t map, |
1010 | vm_offset_t *addrp, | |
1011 | vm_size_t size) | |
1012 | { | |
3e170ce0 A |
1013 | return (kmem_alloc_kobject(map, addrp, size, vm_tag_bt())); |
1014 | } | |
1015 | ||
1016 | kern_return_t | |
1017 | kmem_alloc_kobject( | |
1018 | vm_map_t map, | |
1019 | vm_offset_t *addrp, | |
1020 | vm_size_t size, | |
1021 | vm_tag_t tag) | |
1022 | { | |
1023 | return kernel_memory_allocate(map, addrp, size, 0, KMA_KOBJECT, tag); | |
1c79356b A |
1024 | } |
1025 | ||
1026 | /* | |
1027 | * kmem_alloc_aligned: | |
1028 | * | |
b0d623f7 | 1029 | * Like kmem_alloc_kobject, except that the memory is aligned. |
1c79356b A |
1030 | * The size should be a power-of-2. |
1031 | */ | |
1032 | ||
1033 | kern_return_t | |
1034 | kmem_alloc_aligned( | |
1035 | vm_map_t map, | |
1036 | vm_offset_t *addrp, | |
3e170ce0 A |
1037 | vm_size_t size, |
1038 | vm_tag_t tag) | |
1c79356b A |
1039 | { |
1040 | if ((size & (size - 1)) != 0) | |
1041 | panic("kmem_alloc_aligned: size not aligned"); | |
3e170ce0 | 1042 | return kernel_memory_allocate(map, addrp, size, size - 1, KMA_KOBJECT, tag); |
1c79356b A |
1043 | } |
1044 | ||
1045 | /* | |
1046 | * kmem_alloc_pageable: | |
1047 | * | |
1048 | * Allocate pageable memory in the kernel's address map. | |
1049 | */ | |
1050 | ||
1051 | kern_return_t | |
3e170ce0 | 1052 | kmem_alloc_pageable_external( |
1c79356b A |
1053 | vm_map_t map, |
1054 | vm_offset_t *addrp, | |
1055 | vm_size_t size) | |
3e170ce0 A |
1056 | { |
1057 | return (kmem_alloc_pageable(map, addrp, size, vm_tag_bt())); | |
1058 | } | |
1059 | ||
1060 | kern_return_t | |
1061 | kmem_alloc_pageable( | |
1062 | vm_map_t map, | |
1063 | vm_offset_t *addrp, | |
1064 | vm_size_t size, | |
1065 | vm_tag_t tag) | |
1c79356b | 1066 | { |
91447636 A |
1067 | vm_map_offset_t map_addr; |
1068 | vm_map_size_t map_size; | |
1c79356b A |
1069 | kern_return_t kr; |
1070 | ||
1071 | #ifndef normal | |
fe8ab488 | 1072 | map_addr = (vm_map_min(map)) + PAGE_SIZE; |
1c79356b | 1073 | #else |
91447636 | 1074 | map_addr = vm_map_min(map); |
1c79356b | 1075 | #endif |
39236c6e A |
1076 | map_size = vm_map_round_page(size, |
1077 | VM_MAP_PAGE_MASK(map)); | |
91447636 A |
1078 | |
1079 | kr = vm_map_enter(map, &map_addr, map_size, | |
3e170ce0 | 1080 | (vm_map_offset_t) 0, |
5ba3f43e A |
1081 | VM_FLAGS_ANYWHERE, |
1082 | VM_MAP_KERNEL_FLAGS_NONE, | |
1083 | tag, | |
1c79356b A |
1084 | VM_OBJECT_NULL, (vm_object_offset_t) 0, FALSE, |
1085 | VM_PROT_DEFAULT, VM_PROT_ALL, VM_INHERIT_DEFAULT); | |
91447636 | 1086 | |
1c79356b A |
1087 | if (kr != KERN_SUCCESS) |
1088 | return kr; | |
1089 | ||
5ba3f43e A |
1090 | #if KASAN |
1091 | kasan_notify_address(map_addr, map_size); | |
1092 | #endif | |
91447636 | 1093 | *addrp = CAST_DOWN(vm_offset_t, map_addr); |
1c79356b A |
1094 | return KERN_SUCCESS; |
1095 | } | |
1096 | ||
1097 | /* | |
1098 | * kmem_free: | |
1099 | * | |
1100 | * Release a region of kernel virtual memory allocated | |
b0d623f7 | 1101 | * with kmem_alloc, kmem_alloc_kobject, or kmem_alloc_pageable, |
1c79356b A |
1102 | * and return the physical pages associated with that region. |
1103 | */ | |
1104 | ||
1105 | void | |
1106 | kmem_free( | |
1107 | vm_map_t map, | |
1108 | vm_offset_t addr, | |
1109 | vm_size_t size) | |
1110 | { | |
1111 | kern_return_t kr; | |
1112 | ||
b0d623f7 A |
1113 | assert(addr >= VM_MIN_KERNEL_AND_KEXT_ADDRESS); |
1114 | ||
2d21ac55 A |
1115 | TRACE_MACHLEAKS(KMEM_FREE_CODE, KMEM_FREE_CODE_2, size, addr); |
1116 | ||
b0d623f7 A |
1117 | if(size == 0) { |
1118 | #if MACH_ASSERT | |
1119 | printf("kmem_free called with size==0 for map: %p with addr: 0x%llx\n",map,(uint64_t)addr); | |
1120 | #endif | |
1121 | return; | |
1122 | } | |
1123 | ||
39236c6e A |
1124 | kr = vm_map_remove(map, |
1125 | vm_map_trunc_page(addr, | |
1126 | VM_MAP_PAGE_MASK(map)), | |
1127 | vm_map_round_page(addr + size, | |
1128 | VM_MAP_PAGE_MASK(map)), | |
1129 | VM_MAP_REMOVE_KUNWIRE); | |
1c79356b A |
1130 | if (kr != KERN_SUCCESS) |
1131 | panic("kmem_free"); | |
1132 | } | |
1133 | ||
1134 | /* | |
b4c24cb9 | 1135 | * Allocate new pages in an object. |
1c79356b A |
1136 | */ |
1137 | ||
1138 | kern_return_t | |
1139 | kmem_alloc_pages( | |
39037602 A |
1140 | vm_object_t object, |
1141 | vm_object_offset_t offset, | |
1142 | vm_object_size_t size) | |
1c79356b | 1143 | { |
91447636 | 1144 | vm_object_size_t alloc_size; |
1c79356b | 1145 | |
91447636 | 1146 | alloc_size = vm_object_round_page(size); |
b4c24cb9 | 1147 | vm_object_lock(object); |
91447636 | 1148 | while (alloc_size) { |
39037602 | 1149 | vm_page_t mem; |
1c79356b | 1150 | |
1c79356b A |
1151 | |
1152 | /* | |
1153 | * Allocate a page | |
1154 | */ | |
91447636 A |
1155 | while (VM_PAGE_NULL == |
1156 | (mem = vm_page_alloc(object, offset))) { | |
1c79356b A |
1157 | vm_object_unlock(object); |
1158 | VM_PAGE_WAIT(); | |
1159 | vm_object_lock(object); | |
1160 | } | |
91447636 | 1161 | mem->busy = FALSE; |
1c79356b | 1162 | |
91447636 | 1163 | alloc_size -= PAGE_SIZE; |
b4c24cb9 | 1164 | offset += PAGE_SIZE; |
1c79356b | 1165 | } |
b4c24cb9 | 1166 | vm_object_unlock(object); |
1c79356b A |
1167 | return KERN_SUCCESS; |
1168 | } | |
1169 | ||
1c79356b A |
1170 | /* |
1171 | * kmem_suballoc: | |
1172 | * | |
1173 | * Allocates a map to manage a subrange | |
1174 | * of the kernel virtual address space. | |
1175 | * | |
1176 | * Arguments are as follows: | |
1177 | * | |
1178 | * parent Map to take range from | |
1179 | * addr Address of start of range (IN/OUT) | |
1180 | * size Size of range to find | |
1181 | * pageable Can region be paged | |
1182 | * anywhere Can region be located anywhere in map | |
1183 | * new_map Pointer to new submap | |
1184 | */ | |
1185 | kern_return_t | |
1186 | kmem_suballoc( | |
1187 | vm_map_t parent, | |
1188 | vm_offset_t *addr, | |
1189 | vm_size_t size, | |
1190 | boolean_t pageable, | |
91447636 | 1191 | int flags, |
5ba3f43e A |
1192 | vm_map_kernel_flags_t vmk_flags, |
1193 | vm_tag_t tag, | |
1c79356b A |
1194 | vm_map_t *new_map) |
1195 | { | |
91447636 A |
1196 | vm_map_t map; |
1197 | vm_map_offset_t map_addr; | |
1198 | vm_map_size_t map_size; | |
1199 | kern_return_t kr; | |
1c79356b | 1200 | |
39236c6e A |
1201 | map_size = vm_map_round_page(size, |
1202 | VM_MAP_PAGE_MASK(parent)); | |
1c79356b A |
1203 | |
1204 | /* | |
1205 | * Need reference on submap object because it is internal | |
1206 | * to the vm_system. vm_object_enter will never be called | |
1207 | * on it (usual source of reference for vm_map_enter). | |
1208 | */ | |
1209 | vm_object_reference(vm_submap_object); | |
1210 | ||
39236c6e A |
1211 | map_addr = ((flags & VM_FLAGS_ANYWHERE) |
1212 | ? vm_map_min(parent) | |
1213 | : vm_map_trunc_page(*addr, | |
1214 | VM_MAP_PAGE_MASK(parent))); | |
91447636 A |
1215 | |
1216 | kr = vm_map_enter(parent, &map_addr, map_size, | |
5ba3f43e | 1217 | (vm_map_offset_t) 0, flags, vmk_flags, tag, |
1c79356b A |
1218 | vm_submap_object, (vm_object_offset_t) 0, FALSE, |
1219 | VM_PROT_DEFAULT, VM_PROT_ALL, VM_INHERIT_DEFAULT); | |
1220 | if (kr != KERN_SUCCESS) { | |
1221 | vm_object_deallocate(vm_submap_object); | |
1222 | return (kr); | |
1223 | } | |
1224 | ||
1225 | pmap_reference(vm_map_pmap(parent)); | |
91447636 | 1226 | map = vm_map_create(vm_map_pmap(parent), map_addr, map_addr + map_size, pageable); |
1c79356b A |
1227 | if (map == VM_MAP_NULL) |
1228 | panic("kmem_suballoc: vm_map_create failed"); /* "can't happen" */ | |
39236c6e A |
1229 | /* inherit the parent map's page size */ |
1230 | vm_map_set_page_shift(map, VM_MAP_PAGE_SHIFT(parent)); | |
1c79356b | 1231 | |
91447636 | 1232 | kr = vm_map_submap(parent, map_addr, map_addr + map_size, map, map_addr, FALSE); |
1c79356b A |
1233 | if (kr != KERN_SUCCESS) { |
1234 | /* | |
1235 | * See comment preceding vm_map_submap(). | |
1236 | */ | |
91447636 | 1237 | vm_map_remove(parent, map_addr, map_addr + map_size, VM_MAP_NO_FLAGS); |
1c79356b A |
1238 | vm_map_deallocate(map); /* also removes ref to pmap */ |
1239 | vm_object_deallocate(vm_submap_object); | |
1240 | return (kr); | |
1241 | } | |
91447636 | 1242 | *addr = CAST_DOWN(vm_offset_t, map_addr); |
1c79356b A |
1243 | *new_map = map; |
1244 | return (KERN_SUCCESS); | |
1245 | } | |
1246 | ||
1247 | /* | |
1248 | * kmem_init: | |
1249 | * | |
1250 | * Initialize the kernel's virtual memory map, taking | |
1251 | * into account all memory allocated up to this time. | |
1252 | */ | |
1253 | void | |
1254 | kmem_init( | |
1255 | vm_offset_t start, | |
1256 | vm_offset_t end) | |
1257 | { | |
91447636 A |
1258 | vm_map_offset_t map_start; |
1259 | vm_map_offset_t map_end; | |
5ba3f43e A |
1260 | vm_map_kernel_flags_t vmk_flags; |
1261 | ||
1262 | vmk_flags = VM_MAP_KERNEL_FLAGS_NONE; | |
1263 | vmk_flags.vmkf_permanent = TRUE; | |
1264 | vmk_flags.vmkf_no_pmap_check = TRUE; | |
91447636 | 1265 | |
39236c6e A |
1266 | map_start = vm_map_trunc_page(start, |
1267 | VM_MAP_PAGE_MASK(kernel_map)); | |
1268 | map_end = vm_map_round_page(end, | |
1269 | VM_MAP_PAGE_MASK(kernel_map)); | |
91447636 | 1270 | |
5ba3f43e A |
1271 | #if defined(__arm__) || defined(__arm64__) |
1272 | kernel_map = vm_map_create(pmap_kernel(),VM_MIN_KERNEL_AND_KEXT_ADDRESS, | |
1273 | VM_MAX_KERNEL_ADDRESS, FALSE); | |
1274 | /* | |
1275 | * Reserve virtual memory allocated up to this time. | |
1276 | */ | |
1277 | { | |
1278 | unsigned int region_select = 0; | |
1279 | vm_map_offset_t region_start; | |
1280 | vm_map_size_t region_size; | |
1281 | vm_map_offset_t map_addr; | |
1282 | kern_return_t kr; | |
1283 | ||
1284 | while (pmap_virtual_region(region_select, ®ion_start, ®ion_size)) { | |
1285 | ||
1286 | map_addr = region_start; | |
1287 | kr = vm_map_enter(kernel_map, &map_addr, | |
1288 | vm_map_round_page(region_size, | |
1289 | VM_MAP_PAGE_MASK(kernel_map)), | |
1290 | (vm_map_offset_t) 0, | |
1291 | VM_FLAGS_FIXED, | |
1292 | vmk_flags, | |
1293 | VM_KERN_MEMORY_NONE, | |
1294 | VM_OBJECT_NULL, | |
1295 | (vm_object_offset_t) 0, FALSE, VM_PROT_NONE, VM_PROT_NONE, | |
1296 | VM_INHERIT_DEFAULT); | |
1297 | ||
1298 | if (kr != KERN_SUCCESS) { | |
1299 | panic("kmem_init(0x%llx,0x%llx): vm_map_enter(0x%llx,0x%llx) error 0x%x\n", | |
1300 | (uint64_t) start, (uint64_t) end, (uint64_t) region_start, | |
1301 | (uint64_t) region_size, kr); | |
1302 | } | |
1303 | ||
1304 | region_select++; | |
1305 | } | |
1306 | } | |
1307 | #else | |
6d2010ae | 1308 | kernel_map = vm_map_create(pmap_kernel(),VM_MIN_KERNEL_AND_KEXT_ADDRESS, |
0c530ab8 | 1309 | map_end, FALSE); |
1c79356b A |
1310 | /* |
1311 | * Reserve virtual memory allocated up to this time. | |
1312 | */ | |
6d2010ae | 1313 | if (start != VM_MIN_KERNEL_AND_KEXT_ADDRESS) { |
91447636 | 1314 | vm_map_offset_t map_addr; |
6d2010ae | 1315 | kern_return_t kr; |
0c530ab8 | 1316 | |
5ba3f43e A |
1317 | vmk_flags = VM_MAP_KERNEL_FLAGS_NONE; |
1318 | vmk_flags.vmkf_no_pmap_check = TRUE; | |
1319 | ||
6d2010ae A |
1320 | map_addr = VM_MIN_KERNEL_AND_KEXT_ADDRESS; |
1321 | kr = vm_map_enter(kernel_map, | |
5ba3f43e A |
1322 | &map_addr, |
1323 | (vm_map_size_t)(map_start - VM_MIN_KERNEL_AND_KEXT_ADDRESS), | |
1324 | (vm_map_offset_t) 0, | |
1325 | VM_FLAGS_FIXED, | |
1326 | vmk_flags, | |
1327 | VM_KERN_MEMORY_NONE, | |
1328 | VM_OBJECT_NULL, | |
1329 | (vm_object_offset_t) 0, FALSE, | |
1330 | VM_PROT_NONE, VM_PROT_NONE, | |
1331 | VM_INHERIT_DEFAULT); | |
6d2010ae A |
1332 | |
1333 | if (kr != KERN_SUCCESS) { | |
1334 | panic("kmem_init(0x%llx,0x%llx): vm_map_enter(0x%llx,0x%llx) error 0x%x\n", | |
1335 | (uint64_t) start, (uint64_t) end, | |
1336 | (uint64_t) VM_MIN_KERNEL_AND_KEXT_ADDRESS, | |
1337 | (uint64_t) (map_start - VM_MIN_KERNEL_AND_KEXT_ADDRESS), | |
1338 | kr); | |
1339 | } | |
1c79356b | 1340 | } |
5ba3f43e | 1341 | #endif |
6d2010ae | 1342 | |
2d21ac55 A |
1343 | /* |
1344 | * Set the default global user wire limit which limits the amount of | |
b0d623f7 A |
1345 | * memory that can be locked via mlock(). We set this to the total |
1346 | * amount of memory that are potentially usable by a user app (max_mem) | |
1347 | * minus a certain amount. This can be overridden via a sysctl. | |
2d21ac55 | 1348 | */ |
b0d623f7 A |
1349 | vm_global_no_user_wire_amount = MIN(max_mem*20/100, |
1350 | VM_NOT_USER_WIREABLE); | |
1351 | vm_global_user_wire_limit = max_mem - vm_global_no_user_wire_amount; | |
2d21ac55 | 1352 | |
b0d623f7 A |
1353 | /* the default per user limit is the same as the global limit */ |
1354 | vm_user_wire_limit = vm_global_user_wire_limit; | |
1c79356b A |
1355 | } |
1356 | ||
1c79356b | 1357 | |
1c79356b A |
1358 | /* |
1359 | * Routine: copyinmap | |
1360 | * Purpose: | |
1361 | * Like copyin, except that fromaddr is an address | |
1362 | * in the specified VM map. This implementation | |
1363 | * is incomplete; it handles the current user map | |
1364 | * and the kernel map/submaps. | |
1365 | */ | |
91447636 | 1366 | kern_return_t |
1c79356b | 1367 | copyinmap( |
91447636 A |
1368 | vm_map_t map, |
1369 | vm_map_offset_t fromaddr, | |
1370 | void *todata, | |
1371 | vm_size_t length) | |
1c79356b | 1372 | { |
91447636 A |
1373 | kern_return_t kr = KERN_SUCCESS; |
1374 | vm_map_t oldmap; | |
1375 | ||
1376 | if (vm_map_pmap(map) == pmap_kernel()) | |
1377 | { | |
1c79356b | 1378 | /* assume a correct copy */ |
91447636 A |
1379 | memcpy(todata, CAST_DOWN(void *, fromaddr), length); |
1380 | } | |
1381 | else if (current_map() == map) | |
1382 | { | |
1383 | if (copyin(fromaddr, todata, length) != 0) | |
1384 | kr = KERN_INVALID_ADDRESS; | |
1c79356b | 1385 | } |
91447636 A |
1386 | else |
1387 | { | |
1388 | vm_map_reference(map); | |
1389 | oldmap = vm_map_switch(map); | |
1390 | if (copyin(fromaddr, todata, length) != 0) | |
1391 | kr = KERN_INVALID_ADDRESS; | |
1392 | vm_map_switch(oldmap); | |
1393 | vm_map_deallocate(map); | |
1394 | } | |
1395 | return kr; | |
1c79356b A |
1396 | } |
1397 | ||
1398 | /* | |
1399 | * Routine: copyoutmap | |
1400 | * Purpose: | |
1401 | * Like copyout, except that toaddr is an address | |
1402 | * in the specified VM map. This implementation | |
1403 | * is incomplete; it handles the current user map | |
1404 | * and the kernel map/submaps. | |
1405 | */ | |
91447636 | 1406 | kern_return_t |
1c79356b | 1407 | copyoutmap( |
91447636 A |
1408 | vm_map_t map, |
1409 | void *fromdata, | |
1410 | vm_map_address_t toaddr, | |
1411 | vm_size_t length) | |
1c79356b A |
1412 | { |
1413 | if (vm_map_pmap(map) == pmap_kernel()) { | |
1414 | /* assume a correct copy */ | |
91447636 A |
1415 | memcpy(CAST_DOWN(void *, toaddr), fromdata, length); |
1416 | return KERN_SUCCESS; | |
1c79356b A |
1417 | } |
1418 | ||
91447636 A |
1419 | if (current_map() != map) |
1420 | return KERN_NOT_SUPPORTED; | |
1421 | ||
1422 | if (copyout(fromdata, toaddr, length) != 0) | |
1423 | return KERN_INVALID_ADDRESS; | |
1c79356b | 1424 | |
91447636 | 1425 | return KERN_SUCCESS; |
1c79356b | 1426 | } |
9bccf70c | 1427 | |
3e170ce0 A |
1428 | /* |
1429 | * | |
1430 | * The following two functions are to be used when exposing kernel | |
1431 | * addresses to userspace via any of the various debug or info | |
1432 | * facilities that exist. These are basically the same as VM_KERNEL_ADDRPERM() | |
1433 | * and VM_KERNEL_UNSLIDE_OR_PERM() except they use a different random seed and | |
1434 | * are exported to KEXTs. | |
1435 | * | |
1436 | * NOTE: USE THE MACRO VERSIONS OF THESE FUNCTIONS (in vm_param.h) FROM WITHIN THE KERNEL | |
1437 | */ | |
1438 | ||
5ba3f43e A |
1439 | static void |
1440 | vm_kernel_addrhash_internal( | |
3e170ce0 | 1441 | vm_offset_t addr, |
5ba3f43e A |
1442 | vm_offset_t *hash_addr, |
1443 | uint64_t salt) | |
3e170ce0 | 1444 | { |
5ba3f43e A |
1445 | assert(salt != 0); |
1446 | ||
3e170ce0 | 1447 | if (addr == 0) { |
5ba3f43e | 1448 | *hash_addr = 0; |
3e170ce0 A |
1449 | return; |
1450 | } | |
1451 | ||
5ba3f43e A |
1452 | if (VM_KERNEL_IS_SLID(addr)) { |
1453 | *hash_addr = VM_KERNEL_UNSLIDE(addr); | |
1454 | return; | |
1455 | } | |
1456 | ||
1457 | vm_offset_t sha_digest[SHA256_DIGEST_LENGTH/sizeof(vm_offset_t)]; | |
1458 | SHA256_CTX sha_ctx; | |
1459 | ||
1460 | SHA256_Init(&sha_ctx); | |
1461 | SHA256_Update(&sha_ctx, &salt, sizeof(salt)); | |
1462 | SHA256_Update(&sha_ctx, &addr, sizeof(addr)); | |
1463 | SHA256_Final(sha_digest, &sha_ctx); | |
1464 | ||
1465 | *hash_addr = sha_digest[0]; | |
1466 | } | |
1467 | ||
1468 | void | |
1469 | vm_kernel_addrhash_external( | |
1470 | vm_offset_t addr, | |
1471 | vm_offset_t *hash_addr) | |
1472 | { | |
1473 | return vm_kernel_addrhash_internal(addr, hash_addr, vm_kernel_addrhash_salt_ext); | |
1474 | } | |
1475 | ||
1476 | vm_offset_t | |
1477 | vm_kernel_addrhash(vm_offset_t addr) | |
1478 | { | |
1479 | vm_offset_t hash_addr; | |
1480 | vm_kernel_addrhash_internal(addr, &hash_addr, vm_kernel_addrhash_salt); | |
1481 | return hash_addr; | |
1482 | } | |
1483 | ||
1484 | void | |
1485 | vm_kernel_addrhide( | |
1486 | vm_offset_t addr, | |
1487 | vm_offset_t *hide_addr) | |
1488 | { | |
1489 | *hide_addr = VM_KERNEL_ADDRHIDE(addr); | |
3e170ce0 A |
1490 | } |
1491 | ||
1492 | /* | |
5ba3f43e | 1493 | * vm_kernel_addrperm_external: |
3e170ce0 A |
1494 | * vm_kernel_unslide_or_perm_external: |
1495 | * | |
5ba3f43e | 1496 | * Use these macros when exposing an address to userspace that could come from |
3e170ce0 A |
1497 | * either kernel text/data *or* the heap. |
1498 | */ | |
1499 | void | |
5ba3f43e | 1500 | vm_kernel_addrperm_external( |
3e170ce0 | 1501 | vm_offset_t addr, |
5ba3f43e | 1502 | vm_offset_t *perm_addr) |
3e170ce0 | 1503 | { |
39037602 | 1504 | if (VM_KERNEL_IS_SLID(addr)) { |
5ba3f43e A |
1505 | *perm_addr = VM_KERNEL_UNSLIDE(addr); |
1506 | } else if (VM_KERNEL_ADDRESS(addr)) { | |
1507 | *perm_addr = addr + vm_kernel_addrperm_ext; | |
1508 | } else { | |
1509 | *perm_addr = addr; | |
3e170ce0 | 1510 | } |
5ba3f43e | 1511 | } |
3e170ce0 | 1512 | |
5ba3f43e A |
1513 | void |
1514 | vm_kernel_unslide_or_perm_external( | |
1515 | vm_offset_t addr, | |
1516 | vm_offset_t *up_addr) | |
1517 | { | |
3e170ce0 | 1518 | vm_kernel_addrperm_external(addr, up_addr); |
3e170ce0 | 1519 | } |