]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOLib.cpp
a5415e71cb12f573ee57695c3cf3ecc496030fb4
[apple/xnu.git] / iokit / Kernel / IOLib.cpp
1 /*
2 * Copyright (c) 1998-2006 Apple Computer, 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 * 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>
39 #include <vm/vm_kern.h>
40 #include <libkern/c++/OSCPPDebug.h>
41
42 #include <IOKit/assert.h>
43
44 #include <IOKit/IOReturn.h>
45 #include <IOKit/IOLib.h>
46 #include <IOKit/IOLocks.h>
47 #include <IOKit/IOMapper.h>
48 #include <IOKit/IOBufferMemoryDescriptor.h>
49 #include <IOKit/IOKitDebug.h>
50
51 #include "IOKitKernelInternal.h"
52
53 #ifdef IOALLOCDEBUG
54 #include <libkern/OSDebug.h>
55 #include <sys/sysctl.h>
56 #endif
57
58 extern "C"
59 {
60
61
62 mach_timespec_t IOZeroTvalspec = { 0, 0 };
63
64 extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va);
65
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
76
77 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
78
79 lck_grp_t *IOLockGroup;
80
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
93 /*
94 * Static variables for this module.
95 */
96
97 static queue_head_t gIOMallocContiguousEntries;
98 static lck_mtx_t * gIOMallocContiguousEntriesLock;
99
100 enum { kIOMaxPageableMaps = 16 };
101 enum { kIOPageableMapSize = 96 * 1024 * 1024 };
102 enum { kIOPageableMaxMapSize = 96 * 1024 * 1024 };
103
104 typedef struct {
105 vm_map_t map;
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 ];
114 lck_mtx_t * lock;
115 } gIOKitPageableSpace;
116
117 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
118
119 void IOLibInit(void)
120 {
121 kern_return_t ret;
122
123 static bool libInitialized;
124
125 if(libInitialized)
126 return;
127
128 gIOKitPageableSpace.maps[0].address = 0;
129 ret = kmem_suballoc(kernel_map,
130 &gIOKitPageableSpace.maps[0].address,
131 kIOPageableMapSize,
132 TRUE,
133 VM_FLAGS_ANYWHERE,
134 &gIOKitPageableSpace.maps[0].map);
135 if (ret != KERN_SUCCESS)
136 panic("failed to allocate iokit pageable map\n");
137
138 IOLockGroup = lck_grp_alloc_init("IOKit", LCK_GRP_ATTR_NULL);
139
140 gIOKitPageableSpace.lock = lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL);
141 gIOKitPageableSpace.maps[0].end = gIOKitPageableSpace.maps[0].address + kIOPageableMapSize;
142 gIOKitPageableSpace.hint = 0;
143 gIOKitPageableSpace.count = 1;
144
145 gIOMallocContiguousEntriesLock = lck_mtx_alloc_init(IOLockGroup, LCK_ATTR_NULL);
146 queue_init( &gIOMallocContiguousEntries );
147
148 libInitialized = true;
149 }
150
151 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
152
153 IOThread IOCreateThread(IOThreadFunc fcn, void *arg)
154 {
155 kern_return_t result;
156 thread_t thread;
157
158 result = kernel_thread_start((thread_continue_t)fcn, arg, &thread);
159 if (result != KERN_SUCCESS)
160 return (NULL);
161
162 thread_deallocate(thread);
163
164 return (thread);
165 }
166
167
168 void IOExitThread(void)
169 {
170 (void) thread_terminate(current_thread());
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
182 if (address) {
183 debug_iomalloc_size += size;
184 }
185 #endif
186 return address;
187 }
188
189 void IOFree(void * address, vm_size_t size)
190 {
191 if (address) {
192 kfree(address, size);
193 #if IOALLOCDEBUG
194 debug_iomalloc_size -= size;
195 #endif
196 }
197 }
198
199 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
200
201 void * IOMallocAligned(vm_size_t size, vm_size_t alignment)
202 {
203 kern_return_t kr;
204 vm_offset_t address;
205 vm_offset_t allocationAddress;
206 vm_size_t adjustedSize;
207 uintptr_t alignMask;
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,
220 size, alignMask, 0);
221 if (KERN_SUCCESS != kr)
222 address = 0;
223
224 } else {
225
226 adjustedSize += alignMask;
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);
237
238 if (allocationAddress) {
239 address = (allocationAddress + alignMask
240 + (sizeof(vm_size_t) + sizeof(vm_address_t)))
241 & (~alignMask);
242
243 *((vm_size_t *)(address - sizeof(vm_size_t) - sizeof(vm_address_t)))
244 = adjustedSize;
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
254 if( address) {
255 debug_iomalloc_size += size;
256 }
257 #endif
258
259 return (void *) address;
260 }
261
262 void IOFreeAligned(void * address, vm_size_t size)
263 {
264 vm_address_t allocationAddress;
265 vm_size_t adjustedSize;
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
275 kmem_free( kernel_map, (vm_offset_t) address, size);
276
277 } else {
278 adjustedSize = *((vm_size_t *)( (vm_address_t) address
279 - sizeof(vm_address_t) - sizeof(vm_size_t)));
280 allocationAddress = *((vm_address_t *)( (vm_address_t) address
281 - sizeof(vm_address_t) ));
282
283 if (adjustedSize >= page_size)
284 kmem_free( kernel_map, allocationAddress, adjustedSize);
285 else
286 kfree((void *)allocationAddress, adjustedSize);
287 }
288
289 #if IOALLOCDEBUG
290 debug_iomalloc_size -= size;
291 #endif
292 }
293
294 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
295
296 void
297 IOKernelFreePhysical(mach_vm_address_t address, mach_vm_size_t size)
298 {
299 mach_vm_address_t allocationAddress;
300 mach_vm_size_t adjustedSize;
301
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
310 kmem_free( kernel_map, (vm_offset_t) address, size);
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
327 IOKernelAllocateWithPhysicalRestrict(mach_vm_size_t size, mach_vm_address_t maxPhys,
328 mach_vm_size_t alignment, bool contiguous)
329 {
330 kern_return_t kr;
331 mach_vm_address_t address;
332 mach_vm_address_t allocationAddress;
333 mach_vm_size_t adjustedSize;
334 mach_vm_address_t alignMask;
335
336 if (size == 0)
337 return (0);
338 if (alignment == 0)
339 alignment = 1;
340
341 alignMask = alignment - 1;
342 adjustedSize = (2 * size) + sizeof(mach_vm_size_t) + sizeof(mach_vm_address_t);
343
344 contiguous = (contiguous && (adjustedSize > page_size))
345 || (alignment > page_size);
346
347 if (contiguous || maxPhys)
348 {
349 int options = 0;
350 vm_offset_t virt;
351
352 adjustedSize = size;
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)
363 {
364 kr = kmem_alloc_contig(kernel_map, &virt, size,
365 alignMask, atop(maxPhys), atop(alignMask), 0);
366 }
367 else
368 {
369 kr = kernel_memory_allocate(kernel_map, &virt,
370 size, alignMask, options);
371 }
372 if (KERN_SUCCESS == kr)
373 address = virt;
374 else
375 address = 0;
376 }
377 else
378 {
379 adjustedSize += alignMask;
380 allocationAddress = (mach_vm_address_t) kalloc(adjustedSize);
381
382 if (allocationAddress) {
383
384 address = (allocationAddress + alignMask
385 + (sizeof(mach_vm_size_t) + sizeof(mach_vm_address_t)))
386 & (~alignMask);
387
388 if (atop_32(address) != atop_32(address + size - 1))
389 address = round_page(address);
390
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)))
394 = allocationAddress;
395 } else
396 address = 0;
397 }
398
399 #if IOALLOCDEBUG
400 if (address) {
401 debug_iomalloc_size += size;
402 }
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
428 /* Do we want a physical address? */
429 if (!physicalAddress)
430 {
431 address = IOKernelAllocateWithPhysicalRestrict(size, 0 /*maxPhys*/, alignment, true);
432 }
433 else do
434 {
435 IOBufferMemoryDescriptor * bmd;
436 mach_vm_address_t physicalMask;
437 vm_offset_t alignMask;
438
439 alignMask = alignment - 1;
440 physicalMask = (0xFFFFFFFF ^ alignMask);
441
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)
450 {
451 bmd->release();
452 break;
453 }
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();
463 }
464 while (false);
465
466 return (void *) address;
467 }
468
469 void IOFreeContiguous(void * _address, vm_size_t size)
470 {
471 _IOMallocContiguousEntry * entry;
472 IOMemoryDescriptor * md = NULL;
473
474 mach_vm_address_t address = (mach_vm_address_t) _address;
475
476 if( !address)
477 return;
478
479 assert(size);
480
481 lck_mtx_lock(gIOMallocContiguousEntriesLock);
482 queue_iterate( &gIOMallocContiguousEntries, entry,
483 _IOMallocContiguousEntry *, link )
484 {
485 if( entry->virtualAddr == address ) {
486 md = entry->md;
487 queue_remove( &gIOMallocContiguousEntries, entry,
488 _IOMallocContiguousEntry *, link );
489 break;
490 }
491 }
492 lck_mtx_unlock(gIOMallocContiguousEntriesLock);
493
494 if (md)
495 {
496 md->release();
497 IODelete(entry, _IOMallocContiguousEntry, 1);
498 }
499 else
500 {
501 IOKernelFreePhysical((mach_vm_address_t) address, size);
502 }
503 }
504
505 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
506
507 kern_return_t IOIteratePageableMaps(vm_size_t size,
508 IOIteratePageableMapsCallback callback, void * ref)
509 {
510 kern_return_t kr = kIOReturnNotReady;
511 vm_size_t segSize;
512 UInt32 attempts;
513 UInt32 index;
514 vm_offset_t min;
515 vm_map_t map;
516
517 if (size > kIOPageableMaxMapSize)
518 return( kIOReturnBadArgument );
519
520 do {
521 index = gIOKitPageableSpace.hint;
522 attempts = gIOKitPageableSpace.count;
523 while( attempts--) {
524 kr = (*callback)(gIOKitPageableSpace.maps[index].map, ref);
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
537 lck_mtx_lock( gIOKitPageableSpace.lock );
538
539 index = gIOKitPageableSpace.count;
540 if( index >= (kIOMaxPageableMaps - 1)) {
541 lck_mtx_unlock( gIOKitPageableSpace.lock );
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,
555 VM_FLAGS_ANYWHERE,
556 &map);
557 if( KERN_SUCCESS != kr) {
558 lck_mtx_unlock( gIOKitPageableSpace.lock );
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
568 lck_mtx_unlock( gIOKitPageableSpace.lock );
569
570 } while( true );
571
572 return kr;
573 }
574
575 struct IOMallocPageableRef
576 {
577 vm_offset_t address;
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;
605
606 #if IOALLOCDEBUG
607 if( ref.address)
608 debug_iomallocpageable_size += round_page(size);
609 #endif
610
611 return( (void *) ref.address );
612 }
613
614 vm_map_t IOPageableMapForAddress( uintptr_t address )
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)
627 panic("IOPageableMapForAddress: null");
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
641 debug_iomallocpageable_size -= round_page(size);
642 #endif
643 }
644
645 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
646
647 IOReturn IOSetProcessorCacheMode( task_t task, IOVirtualAddress address,
648 IOByteCount length, IOOptionBits cacheMode )
649 {
650 IOReturn ret = kIOReturnSuccess;
651 ppnum_t pagenum;
652
653 if( task != kernel_task)
654 return( kIOReturnUnsupported );
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 );
662
663 // make map mode
664 cacheMode = (cacheMode << kIOMapCacheShift) & kIOMapCacheMask;
665
666 while( (kIOReturnSuccess == ret) && (length > 0) ) {
667
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 );
672 ret = IOMapPages( get_task_map(task), address, ptoa_64(pagenum), page_size, cacheMode );
673 } else
674 ret = kIOReturnVMError;
675
676 address += page_size;
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
690 flush_dcache64( (addr64_t) address, (unsigned) length, false );
691
692 return( kIOReturnSuccess );
693 }
694
695 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
696
697 vm_offset_t OSKernelStackRemaining( void )
698 {
699 return (ml_stack_remaining());
700 }
701
702 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
703
704 /*
705 * Spin for indicated number of milliseconds.
706 */
707 void IOSleep(unsigned milliseconds)
708 {
709 delay_for_interval(milliseconds, kMillisecondScale);
710 }
711
712 /*
713 * Spin for indicated number of microseconds.
714 */
715 void IODelay(unsigned microseconds)
716 {
717 delay_for_interval(microseconds, kMicrosecondScale);
718 }
719
720 /*
721 * Spin for indicated number of nanoseconds.
722 */
723 void IOPause(unsigned nanoseconds)
724 {
725 delay_for_interval(nanoseconds, kNanosecondScale);
726 }
727
728 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
729
730 static void _iolog_putc(int ch, void *arg __unused)
731 {
732 conslog_putc(ch);
733 }
734
735 void IOLog(const char *format, ...)
736 {
737 va_list ap;
738
739 va_start(ap, format);
740 __doprnt(format, ap, _iolog_putc, NULL, 16);
741 va_end(ap);
742 }
743
744 void IOLogv(const char *format, va_list ap)
745 {
746 __doprnt(format, ap, _iolog_putc, NULL, 16);
747 }
748
749 #if !__LP64__
750 void IOPanic(const char *reason)
751 {
752 panic("%s", reason);
753 }
754 #endif
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 }
769 snprintf(noValue, sizeof(noValue), "0x%x (UNDEFINED)", value);
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
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
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 }
820
821 } /* extern "C" */
822
823
824