]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 1998-2006 Apple Computer, 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 | /* | |
1c79356b A |
29 | * HISTORY |
30 | * | |
31 | * 17-Apr-91 Portions from libIO.m, Doug Mitchell at NeXT. | |
32 | * 17-Nov-98 cpp | |
33 | * | |
34 | */ | |
35 | ||
36 | #include <IOKit/system.h> | |
37 | #include <mach/sync_policy.h> | |
38 | #include <machine/machine_routines.h> | |
b0d623f7 | 39 | #include <vm/vm_kern.h> |
1c79356b A |
40 | #include <libkern/c++/OSCPPDebug.h> |
41 | ||
42 | #include <IOKit/assert.h> | |
43 | ||
44 | #include <IOKit/IOReturn.h> | |
45 | #include <IOKit/IOLib.h> | |
91447636 | 46 | #include <IOKit/IOLocks.h> |
55e303ae | 47 | #include <IOKit/IOMapper.h> |
0c530ab8 | 48 | #include <IOKit/IOBufferMemoryDescriptor.h> |
1c79356b A |
49 | #include <IOKit/IOKitDebug.h> |
50 | ||
91447636 A |
51 | #include "IOKitKernelInternal.h" |
52 | ||
2d21ac55 A |
53 | #ifdef IOALLOCDEBUG |
54 | #include <libkern/OSDebug.h> | |
55 | #include <sys/sysctl.h> | |
56 | #endif | |
57 | ||
0c530ab8 A |
58 | extern "C" |
59 | { | |
60 | ||
61 | ||
1c79356b A |
62 | mach_timespec_t IOZeroTvalspec = { 0, 0 }; |
63 | ||
55e303ae A |
64 | extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va); |
65 | ||
b0d623f7 A |
66 | int |
67 | __doprnt( | |
68 | const char *fmt, | |
69 | va_list argp, | |
70 | void (*putc)(int, void *), | |
71 | void *arg, | |
72 | int radix); | |
73 | ||
74 | extern void conslog_putc(char); | |
75 | ||
0c530ab8 | 76 | |
55e303ae | 77 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
9bccf70c | 78 | |
91447636 A |
79 | lck_grp_t *IOLockGroup; |
80 | ||
9bccf70c A |
81 | /* |
82 | * Global variables for use by iLogger | |
83 | * These symbols are for use only by Apple diagnostic code. | |
84 | * Binary compatibility is not guaranteed for kexts that reference these symbols. | |
85 | */ | |
86 | ||
87 | void *_giDebugLogInternal = NULL; | |
88 | void *_giDebugLogDataInternal = NULL; | |
89 | void *_giDebugReserved1 = NULL; | |
90 | void *_giDebugReserved2 = NULL; | |
91 | ||
92 | ||
1c79356b A |
93 | /* |
94 | * Static variables for this module. | |
95 | */ | |
96 | ||
55e303ae | 97 | static queue_head_t gIOMallocContiguousEntries; |
91447636 | 98 | static lck_mtx_t * gIOMallocContiguousEntriesLock; |
1c79356b A |
99 | |
100 | enum { kIOMaxPageableMaps = 16 }; | |
483a1d10 | 101 | enum { kIOPageableMapSize = 96 * 1024 * 1024 }; |
55e303ae | 102 | enum { kIOPageableMaxMapSize = 96 * 1024 * 1024 }; |
1c79356b A |
103 | |
104 | typedef struct { | |
b0d623f7 | 105 | vm_map_t map; |
1c79356b A |
106 | vm_offset_t address; |
107 | vm_offset_t end; | |
108 | } IOMapData; | |
109 | ||
110 | static struct { | |
111 | UInt32 count; | |
112 | UInt32 hint; | |
113 | IOMapData maps[ kIOMaxPageableMaps ]; | |
91447636 | 114 | lck_mtx_t * lock; |
1c79356b A |
115 | } gIOKitPageableSpace; |
116 | ||
55e303ae | 117 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
1c79356b A |
118 | |
119 | void IOLibInit(void) | |
120 | { | |
121 | kern_return_t ret; | |
122 | ||
123 | static bool libInitialized; | |
124 | ||
125 | if(libInitialized) | |
126 | return; | |
127 | ||
1c79356b A |
128 | gIOKitPageableSpace.maps[0].address = 0; |
129 | ret = kmem_suballoc(kernel_map, | |
130 | &gIOKitPageableSpace.maps[0].address, | |
131 | kIOPageableMapSize, | |
132 | TRUE, | |
91447636 | 133 | VM_FLAGS_ANYWHERE, |
1c79356b A |
134 | &gIOKitPageableSpace.maps[0].map); |
135 | if (ret != KERN_SUCCESS) | |
136 | panic("failed to allocate iokit pageable map\n"); | |
137 | ||
91447636 A |
138 | IOLockGroup = lck_grp_alloc_init("IOKit", LCK_GRP_ATTR_NULL); |
139 | ||
140 | gIOKitPageableSpace.lock = lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL); | |
1c79356b A |
141 | gIOKitPageableSpace.maps[0].end = gIOKitPageableSpace.maps[0].address + kIOPageableMapSize; |
142 | gIOKitPageableSpace.hint = 0; | |
143 | gIOKitPageableSpace.count = 1; | |
144 | ||
91447636 | 145 | gIOMallocContiguousEntriesLock = lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL); |
55e303ae A |
146 | queue_init( &gIOMallocContiguousEntries ); |
147 | ||
1c79356b A |
148 | libInitialized = true; |
149 | } | |
150 | ||
151 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
152 | ||
1c79356b A |
153 | IOThread IOCreateThread(IOThreadFunc fcn, void *arg) |
154 | { | |
91447636 A |
155 | kern_return_t result; |
156 | thread_t thread; | |
1c79356b | 157 | |
91447636 A |
158 | result = kernel_thread_start((thread_continue_t)fcn, arg, &thread); |
159 | if (result != KERN_SUCCESS) | |
160 | return (NULL); | |
1c79356b | 161 | |
91447636 | 162 | thread_deallocate(thread); |
1c79356b | 163 | |
91447636 | 164 | return (thread); |
1c79356b A |
165 | } |
166 | ||
167 | ||
0c530ab8 | 168 | void IOExitThread(void) |
1c79356b | 169 | { |
0c530ab8 | 170 | (void) thread_terminate(current_thread()); |
1c79356b A |
171 | } |
172 | ||
173 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
174 | ||
175 | ||
176 | void * IOMalloc(vm_size_t size) | |
177 | { | |
178 | void * address; | |
179 | ||
180 | address = (void *)kalloc(size); | |
181 | #if IOALLOCDEBUG | |
2d21ac55 A |
182 | if (address) { |
183 | debug_iomalloc_size += size; | |
184 | } | |
1c79356b A |
185 | #endif |
186 | return address; | |
187 | } | |
188 | ||
189 | void IOFree(void * address, vm_size_t size) | |
190 | { | |
191 | if (address) { | |
2d21ac55 | 192 | kfree(address, size); |
1c79356b | 193 | #if IOALLOCDEBUG |
2d21ac55 | 194 | debug_iomalloc_size -= size; |
1c79356b A |
195 | #endif |
196 | } | |
197 | } | |
198 | ||
199 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
200 | ||
201 | void * IOMallocAligned(vm_size_t size, vm_size_t alignment) | |
202 | { | |
203 | kern_return_t kr; | |
b0d623f7 A |
204 | vm_offset_t address; |
205 | vm_offset_t allocationAddress; | |
1c79356b | 206 | vm_size_t adjustedSize; |
b0d623f7 | 207 | uintptr_t alignMask; |
1c79356b A |
208 | |
209 | if (size == 0) | |
210 | return 0; | |
211 | if (alignment == 0) | |
212 | alignment = 1; | |
213 | ||
214 | alignMask = alignment - 1; | |
215 | adjustedSize = size + sizeof(vm_size_t) + sizeof(vm_address_t); | |
216 | ||
217 | if (adjustedSize >= page_size) { | |
218 | ||
219 | kr = kernel_memory_allocate(kernel_map, &address, | |
9bccf70c A |
220 | size, alignMask, 0); |
221 | if (KERN_SUCCESS != kr) | |
1c79356b | 222 | address = 0; |
1c79356b A |
223 | |
224 | } else { | |
225 | ||
226 | adjustedSize += alignMask; | |
9bccf70c A |
227 | |
228 | if (adjustedSize >= page_size) { | |
229 | ||
230 | kr = kernel_memory_allocate(kernel_map, &allocationAddress, | |
231 | adjustedSize, 0, 0); | |
232 | if (KERN_SUCCESS != kr) | |
233 | allocationAddress = 0; | |
234 | ||
235 | } else | |
236 | allocationAddress = (vm_address_t) kalloc(adjustedSize); | |
1c79356b A |
237 | |
238 | if (allocationAddress) { | |
239 | address = (allocationAddress + alignMask | |
240 | + (sizeof(vm_size_t) + sizeof(vm_address_t))) | |
241 | & (~alignMask); | |
242 | ||
b0d623f7 A |
243 | *((vm_size_t *)(address - sizeof(vm_size_t) - sizeof(vm_address_t))) |
244 | = adjustedSize; | |
1c79356b A |
245 | *((vm_address_t *)(address - sizeof(vm_address_t))) |
246 | = allocationAddress; | |
247 | } else | |
248 | address = 0; | |
249 | } | |
250 | ||
251 | assert(0 == (address & alignMask)); | |
252 | ||
253 | #if IOALLOCDEBUG | |
2d21ac55 A |
254 | if( address) { |
255 | debug_iomalloc_size += size; | |
256 | } | |
1c79356b A |
257 | #endif |
258 | ||
259 | return (void *) address; | |
260 | } | |
261 | ||
262 | void IOFreeAligned(void * address, vm_size_t size) | |
263 | { | |
264 | vm_address_t allocationAddress; | |
b0d623f7 | 265 | vm_size_t adjustedSize; |
1c79356b A |
266 | |
267 | if( !address) | |
268 | return; | |
269 | ||
270 | assert(size); | |
271 | ||
272 | adjustedSize = size + sizeof(vm_size_t) + sizeof(vm_address_t); | |
273 | if (adjustedSize >= page_size) { | |
274 | ||
b0d623f7 | 275 | kmem_free( kernel_map, (vm_offset_t) address, size); |
1c79356b A |
276 | |
277 | } else { | |
b0d623f7 | 278 | adjustedSize = *((vm_size_t *)( (vm_address_t) address |
1c79356b A |
279 | - sizeof(vm_address_t) - sizeof(vm_size_t))); |
280 | allocationAddress = *((vm_address_t *)( (vm_address_t) address | |
281 | - sizeof(vm_address_t) )); | |
282 | ||
9bccf70c | 283 | if (adjustedSize >= page_size) |
91447636 | 284 | kmem_free( kernel_map, allocationAddress, adjustedSize); |
9bccf70c | 285 | else |
91447636 | 286 | kfree((void *)allocationAddress, adjustedSize); |
1c79356b A |
287 | } |
288 | ||
289 | #if IOALLOCDEBUG | |
290 | debug_iomalloc_size -= size; | |
291 | #endif | |
292 | } | |
293 | ||
294 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
295 | ||
0c530ab8 | 296 | void |
0b4c1975 | 297 | IOKernelFreePhysical(mach_vm_address_t address, mach_vm_size_t size) |
55e303ae | 298 | { |
0c530ab8 A |
299 | mach_vm_address_t allocationAddress; |
300 | mach_vm_size_t adjustedSize; | |
4452a7af | 301 | |
0c530ab8 A |
302 | if (!address) |
303 | return; | |
304 | ||
305 | assert(size); | |
306 | ||
307 | adjustedSize = (2 * size) + sizeof(mach_vm_size_t) + sizeof(mach_vm_address_t); | |
308 | if (adjustedSize >= page_size) { | |
309 | ||
b0d623f7 | 310 | kmem_free( kernel_map, (vm_offset_t) address, size); |
0c530ab8 A |
311 | |
312 | } else { | |
313 | ||
314 | adjustedSize = *((mach_vm_size_t *) | |
315 | (address - sizeof(mach_vm_address_t) - sizeof(mach_vm_size_t))); | |
316 | allocationAddress = *((mach_vm_address_t *) | |
317 | (address - sizeof(mach_vm_address_t) )); | |
318 | kfree((void *)allocationAddress, adjustedSize); | |
319 | } | |
320 | ||
321 | #if IOALLOCDEBUG | |
322 | debug_iomalloc_size -= size; | |
323 | #endif | |
324 | } | |
325 | ||
326 | mach_vm_address_t | |
0b4c1975 A |
327 | IOKernelAllocateWithPhysicalRestrict(mach_vm_size_t size, mach_vm_address_t maxPhys, |
328 | mach_vm_size_t alignment, bool contiguous) | |
1c79356b A |
329 | { |
330 | kern_return_t kr; | |
0c530ab8 A |
331 | mach_vm_address_t address; |
332 | mach_vm_address_t allocationAddress; | |
333 | mach_vm_size_t adjustedSize; | |
334 | mach_vm_address_t alignMask; | |
1c79356b A |
335 | |
336 | if (size == 0) | |
0c530ab8 | 337 | return (0); |
1c79356b A |
338 | if (alignment == 0) |
339 | alignment = 1; | |
340 | ||
341 | alignMask = alignment - 1; | |
0c530ab8 | 342 | adjustedSize = (2 * size) + sizeof(mach_vm_size_t) + sizeof(mach_vm_address_t); |
1c79356b | 343 | |
0b4c1975 A |
344 | contiguous = (contiguous && (adjustedSize > page_size)) |
345 | || (alignment > page_size); | |
346 | ||
347 | if (contiguous || maxPhys) | |
55e303ae | 348 | { |
0b4c1975 | 349 | int options = 0; |
0c530ab8 | 350 | vm_offset_t virt; |
0b4c1975 | 351 | |
55e303ae | 352 | adjustedSize = size; |
0b4c1975 A |
353 | contiguous = (contiguous && (adjustedSize > page_size)) |
354 | || (alignment > page_size); | |
355 | ||
356 | if ((!contiguous) && (maxPhys <= 0xFFFFFFFF)) | |
357 | { | |
358 | maxPhys = 0; | |
359 | options |= KMA_LOMEM; | |
360 | } | |
361 | ||
362 | if (contiguous || maxPhys) | |
55e303ae | 363 | { |
0c530ab8 | 364 | kr = kmem_alloc_contig(kernel_map, &virt, size, |
b0d623f7 | 365 | alignMask, atop(maxPhys), atop(alignMask), 0); |
55e303ae A |
366 | } |
367 | else | |
368 | { | |
0c530ab8 | 369 | kr = kernel_memory_allocate(kernel_map, &virt, |
0b4c1975 | 370 | size, alignMask, options); |
55e303ae | 371 | } |
0c530ab8 A |
372 | if (KERN_SUCCESS == kr) |
373 | address = virt; | |
374 | else | |
1c79356b | 375 | address = 0; |
55e303ae A |
376 | } |
377 | else | |
378 | { | |
1c79356b | 379 | adjustedSize += alignMask; |
0c530ab8 | 380 | allocationAddress = (mach_vm_address_t) kalloc(adjustedSize); |
9bccf70c | 381 | |
1c79356b A |
382 | if (allocationAddress) { |
383 | ||
384 | address = (allocationAddress + alignMask | |
0c530ab8 | 385 | + (sizeof(mach_vm_size_t) + sizeof(mach_vm_address_t))) |
1c79356b A |
386 | & (~alignMask); |
387 | ||
55e303ae | 388 | if (atop_32(address) != atop_32(address + size - 1)) |
b0d623f7 | 389 | address = round_page(address); |
1c79356b | 390 | |
0c530ab8 A |
391 | *((mach_vm_size_t *)(address - sizeof(mach_vm_size_t) |
392 | - sizeof(mach_vm_address_t))) = adjustedSize; | |
393 | *((mach_vm_address_t *)(address - sizeof(mach_vm_address_t))) | |
1c79356b A |
394 | = allocationAddress; |
395 | } else | |
396 | address = 0; | |
397 | } | |
398 | ||
0c530ab8 | 399 | #if IOALLOCDEBUG |
2d21ac55 | 400 | if (address) { |
b0d623f7 A |
401 | debug_iomalloc_size += size; |
402 | } | |
0c530ab8 A |
403 | #endif |
404 | ||
405 | return (address); | |
406 | } | |
407 | ||
408 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
409 | ||
410 | struct _IOMallocContiguousEntry | |
411 | { | |
412 | mach_vm_address_t virtualAddr; | |
413 | IOBufferMemoryDescriptor * md; | |
414 | queue_chain_t link; | |
415 | }; | |
416 | typedef struct _IOMallocContiguousEntry _IOMallocContiguousEntry; | |
417 | ||
418 | void * IOMallocContiguous(vm_size_t size, vm_size_t alignment, | |
419 | IOPhysicalAddress * physicalAddress) | |
420 | { | |
421 | mach_vm_address_t address = 0; | |
422 | ||
423 | if (size == 0) | |
424 | return 0; | |
425 | if (alignment == 0) | |
426 | alignment = 1; | |
427 | ||
55e303ae | 428 | /* Do we want a physical address? */ |
0c530ab8 | 429 | if (!physicalAddress) |
c0fea474 | 430 | { |
0b4c1975 | 431 | address = IOKernelAllocateWithPhysicalRestrict(size, 0 /*maxPhys*/, alignment, true); |
0c530ab8 A |
432 | } |
433 | else do | |
434 | { | |
435 | IOBufferMemoryDescriptor * bmd; | |
436 | mach_vm_address_t physicalMask; | |
b0d623f7 | 437 | vm_offset_t alignMask; |
0c530ab8 A |
438 | |
439 | alignMask = alignment - 1; | |
b0d623f7 A |
440 | physicalMask = (0xFFFFFFFF ^ alignMask); |
441 | ||
0c530ab8 A |
442 | bmd = IOBufferMemoryDescriptor::inTaskWithPhysicalMask( |
443 | kernel_task, kIOMemoryPhysicallyContiguous, size, physicalMask); | |
444 | if (!bmd) | |
445 | break; | |
446 | ||
447 | _IOMallocContiguousEntry * | |
448 | entry = IONew(_IOMallocContiguousEntry, 1); | |
449 | if (!entry) | |
55e303ae | 450 | { |
0c530ab8 A |
451 | bmd->release(); |
452 | break; | |
55e303ae | 453 | } |
0c530ab8 A |
454 | entry->virtualAddr = (mach_vm_address_t) bmd->getBytesNoCopy(); |
455 | entry->md = bmd; | |
456 | lck_mtx_lock(gIOMallocContiguousEntriesLock); | |
457 | queue_enter( &gIOMallocContiguousEntries, entry, | |
458 | _IOMallocContiguousEntry *, link ); | |
459 | lck_mtx_unlock(gIOMallocContiguousEntriesLock); | |
460 | ||
461 | address = (mach_vm_address_t) entry->virtualAddr; | |
462 | *physicalAddress = bmd->getPhysicalAddress(); | |
55e303ae | 463 | } |
0c530ab8 | 464 | while (false); |
1c79356b A |
465 | |
466 | return (void *) address; | |
467 | } | |
468 | ||
0c530ab8 | 469 | void IOFreeContiguous(void * _address, vm_size_t size) |
1c79356b | 470 | { |
55e303ae | 471 | _IOMallocContiguousEntry * entry; |
0c530ab8 A |
472 | IOMemoryDescriptor * md = NULL; |
473 | ||
474 | mach_vm_address_t address = (mach_vm_address_t) _address; | |
1c79356b A |
475 | |
476 | if( !address) | |
477 | return; | |
478 | ||
479 | assert(size); | |
480 | ||
91447636 | 481 | lck_mtx_lock(gIOMallocContiguousEntriesLock); |
55e303ae A |
482 | queue_iterate( &gIOMallocContiguousEntries, entry, |
483 | _IOMallocContiguousEntry *, link ) | |
484 | { | |
0c530ab8 A |
485 | if( entry->virtualAddr == address ) { |
486 | md = entry->md; | |
55e303ae A |
487 | queue_remove( &gIOMallocContiguousEntries, entry, |
488 | _IOMallocContiguousEntry *, link ); | |
489 | break; | |
490 | } | |
491 | } | |
91447636 | 492 | lck_mtx_unlock(gIOMallocContiguousEntriesLock); |
55e303ae | 493 | |
0c530ab8 | 494 | if (md) |
55e303ae | 495 | { |
0c530ab8 | 496 | md->release(); |
55e303ae A |
497 | IODelete(entry, _IOMallocContiguousEntry, 1); |
498 | } | |
0c530ab8 A |
499 | else |
500 | { | |
0b4c1975 | 501 | IOKernelFreePhysical((mach_vm_address_t) address, size); |
1c79356b | 502 | } |
1c79356b A |
503 | } |
504 | ||
505 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
506 | ||
0b4e3aa0 A |
507 | kern_return_t IOIteratePageableMaps(vm_size_t size, |
508 | IOIteratePageableMapsCallback callback, void * ref) | |
1c79356b A |
509 | { |
510 | kern_return_t kr = kIOReturnNotReady; | |
1c79356b A |
511 | vm_size_t segSize; |
512 | UInt32 attempts; | |
513 | UInt32 index; | |
514 | vm_offset_t min; | |
515 | vm_map_t map; | |
516 | ||
1c79356b | 517 | if (size > kIOPageableMaxMapSize) |
0b4e3aa0 | 518 | return( kIOReturnBadArgument ); |
1c79356b A |
519 | |
520 | do { | |
521 | index = gIOKitPageableSpace.hint; | |
522 | attempts = gIOKitPageableSpace.count; | |
523 | while( attempts--) { | |
0b4e3aa0 | 524 | kr = (*callback)(gIOKitPageableSpace.maps[index].map, ref); |
1c79356b A |
525 | if( KERN_SUCCESS == kr) { |
526 | gIOKitPageableSpace.hint = index; | |
527 | break; | |
528 | } | |
529 | if( index) | |
530 | index--; | |
531 | else | |
532 | index = gIOKitPageableSpace.count - 1; | |
533 | } | |
534 | if( KERN_SUCCESS == kr) | |
535 | break; | |
536 | ||
91447636 | 537 | lck_mtx_lock( gIOKitPageableSpace.lock ); |
1c79356b A |
538 | |
539 | index = gIOKitPageableSpace.count; | |
540 | if( index >= (kIOMaxPageableMaps - 1)) { | |
91447636 | 541 | lck_mtx_unlock( gIOKitPageableSpace.lock ); |
1c79356b A |
542 | break; |
543 | } | |
544 | ||
545 | if( size < kIOPageableMapSize) | |
546 | segSize = kIOPageableMapSize; | |
547 | else | |
548 | segSize = size; | |
549 | ||
550 | min = 0; | |
551 | kr = kmem_suballoc(kernel_map, | |
552 | &min, | |
553 | segSize, | |
554 | TRUE, | |
91447636 | 555 | VM_FLAGS_ANYWHERE, |
1c79356b A |
556 | &map); |
557 | if( KERN_SUCCESS != kr) { | |
91447636 | 558 | lck_mtx_unlock( gIOKitPageableSpace.lock ); |
1c79356b A |
559 | break; |
560 | } | |
561 | ||
562 | gIOKitPageableSpace.maps[index].map = map; | |
563 | gIOKitPageableSpace.maps[index].address = min; | |
564 | gIOKitPageableSpace.maps[index].end = min + segSize; | |
565 | gIOKitPageableSpace.hint = index; | |
566 | gIOKitPageableSpace.count = index + 1; | |
567 | ||
91447636 | 568 | lck_mtx_unlock( gIOKitPageableSpace.lock ); |
1c79356b A |
569 | |
570 | } while( true ); | |
571 | ||
0b4e3aa0 A |
572 | return kr; |
573 | } | |
574 | ||
575 | struct IOMallocPageableRef | |
576 | { | |
b0d623f7 | 577 | vm_offset_t address; |
0b4e3aa0 A |
578 | vm_size_t size; |
579 | }; | |
580 | ||
581 | static kern_return_t IOMallocPageableCallback(vm_map_t map, void * _ref) | |
582 | { | |
583 | struct IOMallocPageableRef * ref = (struct IOMallocPageableRef *) _ref; | |
584 | kern_return_t kr; | |
585 | ||
586 | kr = kmem_alloc_pageable( map, &ref->address, ref->size ); | |
587 | ||
588 | return( kr ); | |
589 | } | |
590 | ||
591 | void * IOMallocPageable(vm_size_t size, vm_size_t alignment) | |
592 | { | |
593 | kern_return_t kr = kIOReturnNotReady; | |
594 | struct IOMallocPageableRef ref; | |
595 | ||
596 | if (alignment > page_size) | |
597 | return( 0 ); | |
598 | if (size > kIOPageableMaxMapSize) | |
599 | return( 0 ); | |
600 | ||
601 | ref.size = size; | |
602 | kr = IOIteratePageableMaps( size, &IOMallocPageableCallback, &ref ); | |
603 | if( kIOReturnSuccess != kr) | |
604 | ref.address = 0; | |
1c79356b A |
605 | |
606 | #if IOALLOCDEBUG | |
0b4e3aa0 | 607 | if( ref.address) |
b0d623f7 | 608 | debug_iomallocpageable_size += round_page(size); |
1c79356b A |
609 | #endif |
610 | ||
0b4e3aa0 | 611 | return( (void *) ref.address ); |
1c79356b A |
612 | } |
613 | ||
b0d623f7 | 614 | vm_map_t IOPageableMapForAddress( uintptr_t address ) |
1c79356b A |
615 | { |
616 | vm_map_t map = 0; | |
617 | UInt32 index; | |
618 | ||
619 | for( index = 0; index < gIOKitPageableSpace.count; index++) { | |
620 | if( (address >= gIOKitPageableSpace.maps[index].address) | |
621 | && (address < gIOKitPageableSpace.maps[index].end) ) { | |
622 | map = gIOKitPageableSpace.maps[index].map; | |
623 | break; | |
624 | } | |
625 | } | |
626 | if( !map) | |
b0d623f7 | 627 | panic("IOPageableMapForAddress: null"); |
1c79356b A |
628 | |
629 | return( map ); | |
630 | } | |
631 | ||
632 | void IOFreePageable(void * address, vm_size_t size) | |
633 | { | |
634 | vm_map_t map; | |
635 | ||
636 | map = IOPageableMapForAddress( (vm_address_t) address); | |
637 | if( map) | |
638 | kmem_free( map, (vm_offset_t) address, size); | |
639 | ||
640 | #if IOALLOCDEBUG | |
b0d623f7 | 641 | debug_iomallocpageable_size -= round_page(size); |
1c79356b A |
642 | #endif |
643 | } | |
b0d623f7 | 644 | |
1c79356b A |
645 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
646 | ||
1c79356b A |
647 | IOReturn IOSetProcessorCacheMode( task_t task, IOVirtualAddress address, |
648 | IOByteCount length, IOOptionBits cacheMode ) | |
649 | { | |
650 | IOReturn ret = kIOReturnSuccess; | |
55e303ae | 651 | ppnum_t pagenum; |
1c79356b A |
652 | |
653 | if( task != kernel_task) | |
654 | return( kIOReturnUnsupported ); | |
b0d623f7 A |
655 | if ((address | length) & PAGE_MASK) |
656 | { | |
657 | // OSReportWithBacktrace("IOSetProcessorCacheMode(0x%x, 0x%x, 0x%x) fails\n", address, length, cacheMode); | |
658 | return( kIOReturnUnsupported ); | |
659 | } | |
660 | length = round_page(address + length) - trunc_page( address ); | |
661 | address = trunc_page( address ); | |
1c79356b A |
662 | |
663 | // make map mode | |
664 | cacheMode = (cacheMode << kIOMapCacheShift) & kIOMapCacheMask; | |
665 | ||
666 | while( (kIOReturnSuccess == ret) && (length > 0) ) { | |
667 | ||
55e303ae A |
668 | // Get the physical page number |
669 | pagenum = pmap_find_phys(kernel_pmap, (addr64_t)address); | |
670 | if( pagenum) { | |
671 | ret = IOUnmapPages( get_task_map(task), address, page_size ); | |
0c530ab8 | 672 | ret = IOMapPages( get_task_map(task), address, ptoa_64(pagenum), page_size, cacheMode ); |
55e303ae | 673 | } else |
1c79356b A |
674 | ret = kIOReturnVMError; |
675 | ||
55e303ae | 676 | address += page_size; |
1c79356b A |
677 | length -= page_size; |
678 | } | |
679 | ||
680 | return( ret ); | |
681 | } | |
682 | ||
683 | ||
684 | IOReturn IOFlushProcessorCache( task_t task, IOVirtualAddress address, | |
685 | IOByteCount length ) | |
686 | { | |
687 | if( task != kernel_task) | |
688 | return( kIOReturnUnsupported ); | |
689 | ||
55e303ae | 690 | flush_dcache64( (addr64_t) address, (unsigned) length, false ); |
1c79356b A |
691 | |
692 | return( kIOReturnSuccess ); | |
693 | } | |
694 | ||
695 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
696 | ||
b0d623f7 | 697 | vm_offset_t OSKernelStackRemaining( void ) |
1c79356b | 698 | { |
b0d623f7 | 699 | return (ml_stack_remaining()); |
1c79356b A |
700 | } |
701 | ||
702 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
703 | ||
2d21ac55 A |
704 | /* |
705 | * Spin for indicated number of milliseconds. | |
706 | */ | |
1c79356b A |
707 | void IOSleep(unsigned milliseconds) |
708 | { | |
91447636 | 709 | delay_for_interval(milliseconds, kMillisecondScale); |
1c79356b A |
710 | } |
711 | ||
712 | /* | |
713 | * Spin for indicated number of microseconds. | |
714 | */ | |
715 | void IODelay(unsigned microseconds) | |
716 | { | |
91447636 | 717 | delay_for_interval(microseconds, kMicrosecondScale); |
1c79356b A |
718 | } |
719 | ||
2d21ac55 A |
720 | /* |
721 | * Spin for indicated number of nanoseconds. | |
722 | */ | |
723 | void IOPause(unsigned nanoseconds) | |
724 | { | |
725 | delay_for_interval(nanoseconds, kNanosecondScale); | |
726 | } | |
727 | ||
1c79356b A |
728 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
729 | ||
b0d623f7 A |
730 | static void _iolog_putc(int ch, void *arg __unused) |
731 | { | |
732 | conslog_putc(ch); | |
733 | } | |
734 | ||
1c79356b A |
735 | void IOLog(const char *format, ...) |
736 | { | |
737 | va_list ap; | |
1c79356b A |
738 | |
739 | va_start(ap, format); | |
b0d623f7 | 740 | __doprnt(format, ap, _iolog_putc, NULL, 16); |
1c79356b A |
741 | va_end(ap); |
742 | } | |
743 | ||
b0d623f7 A |
744 | void IOLogv(const char *format, va_list ap) |
745 | { | |
746 | __doprnt(format, ap, _iolog_putc, NULL, 16); | |
747 | } | |
748 | ||
749 | #if !__LP64__ | |
1c79356b A |
750 | void IOPanic(const char *reason) |
751 | { | |
2d21ac55 | 752 | panic("%s", reason); |
1c79356b | 753 | } |
b0d623f7 | 754 | #endif |
1c79356b A |
755 | |
756 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
757 | ||
758 | /* | |
759 | * Convert a integer constant (typically a #define or enum) to a string. | |
760 | */ | |
761 | static char noValue[80]; // that's pretty | |
762 | ||
763 | const char *IOFindNameForValue(int value, const IONamedValue *regValueArray) | |
764 | { | |
765 | for( ; regValueArray->name; regValueArray++) { | |
766 | if(regValueArray->value == value) | |
767 | return(regValueArray->name); | |
768 | } | |
2d21ac55 | 769 | snprintf(noValue, sizeof(noValue), "0x%x (UNDEFINED)", value); |
1c79356b A |
770 | return((const char *)noValue); |
771 | } | |
772 | ||
773 | IOReturn IOFindValueForName(const char *string, | |
774 | const IONamedValue *regValueArray, | |
775 | int *value) | |
776 | { | |
777 | for( ; regValueArray->name; regValueArray++) { | |
778 | if(!strcmp(regValueArray->name, string)) { | |
779 | *value = regValueArray->value; | |
780 | return kIOReturnSuccess; | |
781 | } | |
782 | } | |
783 | return kIOReturnBadArgument; | |
784 | } | |
785 | ||
2d21ac55 A |
786 | OSString * IOCopyLogNameForPID(int pid) |
787 | { | |
788 | char buf[128]; | |
789 | size_t len; | |
790 | snprintf(buf, sizeof(buf), "pid %d, ", pid); | |
791 | len = strlen(buf); | |
792 | proc_name(pid, buf + len, sizeof(buf) - len); | |
793 | return (OSString::withCString(buf)); | |
794 | } | |
795 | ||
1c79356b A |
796 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
797 | ||
798 | IOAlignment IOSizeToAlignment(unsigned int size) | |
799 | { | |
800 | register int shift; | |
801 | const int intsize = sizeof(unsigned int) * 8; | |
802 | ||
803 | for (shift = 1; shift < intsize; shift++) { | |
804 | if (size & 0x80000000) | |
805 | return (IOAlignment)(intsize - shift); | |
806 | size <<= 1; | |
807 | } | |
808 | return 0; | |
809 | } | |
810 | ||
811 | unsigned int IOAlignmentToSize(IOAlignment align) | |
812 | { | |
813 | unsigned int size; | |
814 | ||
815 | for (size = 1; align; align--) { | |
816 | size <<= 1; | |
817 | } | |
818 | return size; | |
819 | } | |
0c530ab8 A |
820 | |
821 | } /* extern "C" */ | |
2d21ac55 A |
822 | |
823 | ||
824 |