]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IOLib.c
xnu-124.1.tar.gz
[apple/xnu.git] / iokit / Kernel / IOLib.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
24 *
25 * HISTORY
26 *
27 * 17-Apr-91 Portions from libIO.m, Doug Mitchell at NeXT.
28 * 17-Nov-98 cpp
29 *
30 */
31
32#include <IOKit/system.h>
33#include <mach/sync_policy.h>
34#include <machine/machine_routines.h>
35#include <libkern/c++/OSCPPDebug.h>
36
37#include <IOKit/assert.h>
38
39#include <IOKit/IOReturn.h>
40#include <IOKit/IOLib.h>
41#include <IOKit/IOKitDebug.h>
42
43mach_timespec_t IOZeroTvalspec = { 0, 0 };
44
45/*
46 * Static variables for this module.
47 */
48
49static IOThreadFunc threadArgFcn;
50static void * threadArgArg;
51static lock_t * threadArgLock;
52
53
54enum { kIOMaxPageableMaps = 16 };
55enum { kIOPageableMapSize = 16 * 1024 * 1024 };
56enum { kIOPageableMaxMapSize = 32 * 1024 * 1024 };
57
58typedef struct {
59 vm_map_t map;
60 vm_offset_t address;
61 vm_offset_t end;
62} IOMapData;
63
64static struct {
65 UInt32 count;
66 UInt32 hint;
67 IOMapData maps[ kIOMaxPageableMaps ];
68 mutex_t * lock;
69} gIOKitPageableSpace;
70
71
72void IOLibInit(void)
73{
74 kern_return_t ret;
75
76 static bool libInitialized;
77
78 if(libInitialized)
79 return;
80
81 threadArgLock = lock_alloc( true, NULL, NULL );
82
83 gIOKitPageableSpace.maps[0].address = 0;
84 ret = kmem_suballoc(kernel_map,
85 &gIOKitPageableSpace.maps[0].address,
86 kIOPageableMapSize,
87 TRUE,
88 TRUE,
89 &gIOKitPageableSpace.maps[0].map);
90 if (ret != KERN_SUCCESS)
91 panic("failed to allocate iokit pageable map\n");
92
93 gIOKitPageableSpace.lock = mutex_alloc( 0 );
94 gIOKitPageableSpace.maps[0].end = gIOKitPageableSpace.maps[0].address + kIOPageableMapSize;
95 gIOKitPageableSpace.hint = 0;
96 gIOKitPageableSpace.count = 1;
97
98 libInitialized = true;
99}
100
101/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
102
103/*
104 * We pass an argument to a new thread by saving fcn and arg in some
105 * locked variables and starting the thread at ioThreadStart(). This
106 * function retrives fcn and arg and makes the appropriate call.
107 *
108 */
109
110static void ioThreadStart( void )
111{
112 IOThreadFunc fcn;
113 void * arg;
114
115 fcn = threadArgFcn;
116 arg = threadArgArg;
117 lock_done( threadArgLock);
118
119 (*fcn)(arg);
120
121 IOExitThread();
122}
123
124IOThread IOCreateThread(IOThreadFunc fcn, void *arg)
125{
126 IOThread thread;
127
128 lock_write( threadArgLock);
129 threadArgFcn = fcn;
130 threadArgArg = arg;
131
132 thread = kernel_thread( kernel_task, ioThreadStart);
133
134 return(thread);
135}
136
137
138volatile void IOExitThread()
139{
140 (void) thread_terminate(current_act());
141}
142
143/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
144
145
146void * IOMalloc(vm_size_t size)
147{
148 void * address;
149
150 address = (void *)kalloc(size);
151#if IOALLOCDEBUG
152 if (address)
153 debug_iomalloc_size += size;
154#endif
155 return address;
156}
157
158void IOFree(void * address, vm_size_t size)
159{
160 if (address) {
161 kfree((vm_offset_t)address, size);
162#if IOALLOCDEBUG
163 debug_iomalloc_size -= size;
164#endif
165 }
166}
167
168/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
169
170void * IOMallocAligned(vm_size_t size, vm_size_t alignment)
171{
172 kern_return_t kr;
173 vm_address_t address;
174 vm_address_t allocationAddress;
175 vm_size_t adjustedSize;
176 vm_offset_t alignMask;
177
178 if (size == 0)
179 return 0;
180 if (alignment == 0)
181 alignment = 1;
182
183 alignMask = alignment - 1;
184 adjustedSize = size + sizeof(vm_size_t) + sizeof(vm_address_t);
185
186 if (adjustedSize >= page_size) {
187
188 kr = kernel_memory_allocate(kernel_map, &address,
189 size, alignMask, KMA_KOBJECT);
190 if (KERN_SUCCESS != kr) {
191 IOLog("Failed %08x, %08x\n", size, alignment);
192 address = 0;
193 }
194
195 } else {
196
197 adjustedSize += alignMask;
198 allocationAddress = (vm_address_t) kalloc(adjustedSize);
199
200 if (allocationAddress) {
201 address = (allocationAddress + alignMask
202 + (sizeof(vm_size_t) + sizeof(vm_address_t)))
203 & (~alignMask);
204
205 *((vm_size_t *)(address - sizeof(vm_size_t)
206 - sizeof(vm_address_t))) = adjustedSize;
207 *((vm_address_t *)(address - sizeof(vm_address_t)))
208 = allocationAddress;
209 } else
210 address = 0;
211 }
212
213 assert(0 == (address & alignMask));
214
215#if IOALLOCDEBUG
216 if( address)
217 debug_iomalloc_size += size;
218#endif
219
220 return (void *) address;
221}
222
223void IOFreeAligned(void * address, vm_size_t size)
224{
225 vm_address_t allocationAddress;
226 vm_size_t adjustedSize;
227
228 if( !address)
229 return;
230
231 assert(size);
232
233 adjustedSize = size + sizeof(vm_size_t) + sizeof(vm_address_t);
234 if (adjustedSize >= page_size) {
235
236 kmem_free( kernel_map, (vm_address_t) address, size);
237
238 } else {
239 adjustedSize = *((vm_size_t *)( (vm_address_t) address
240 - sizeof(vm_address_t) - sizeof(vm_size_t)));
241 allocationAddress = *((vm_address_t *)( (vm_address_t) address
242 - sizeof(vm_address_t) ));
243
244 kfree((vm_offset_t) allocationAddress, adjustedSize);
245 }
246
247#if IOALLOCDEBUG
248 debug_iomalloc_size -= size;
249#endif
250}
251
252/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
253
254void * IOMallocContiguous(vm_size_t size, vm_size_t alignment,
255 IOPhysicalAddress * physicalAddress)
256{
257 kern_return_t kr;
258 vm_address_t address;
259 vm_address_t allocationAddress;
260 vm_size_t adjustedSize;
261 vm_offset_t alignMask;
262
263 if (size == 0)
264 return 0;
265 if (alignment == 0)
266 alignment = 1;
267
268 alignMask = alignment - 1;
269 adjustedSize = (2 * size) + sizeof(vm_size_t) + sizeof(vm_address_t);
270
271 if (adjustedSize >= page_size) {
272
273 kr = kmem_alloc_contig(kernel_map, &address, size,
274 alignMask, KMA_KOBJECT);
275 if (KERN_SUCCESS != kr)
276 address = 0;
277
278 } else {
279
280 adjustedSize += alignMask;
281 allocationAddress = (vm_address_t)
282 kalloc(adjustedSize);
283 if (allocationAddress) {
284
285 address = (allocationAddress + alignMask
286 + (sizeof(vm_size_t) + sizeof(vm_address_t)))
287 & (~alignMask);
288
289 if (atop(address) != atop(address + size - 1))
290 address = round_page(address);
291
292 *((vm_size_t *)(address - sizeof(vm_size_t)
293 - sizeof(vm_address_t))) = adjustedSize;
294 *((vm_address_t *)(address - sizeof(vm_address_t)))
295 = allocationAddress;
296 } else
297 address = 0;
298 }
299
300 if( address && physicalAddress)
301 *physicalAddress = (IOPhysicalAddress) pmap_extract( kernel_pmap,
302 address );
303
304 assert(0 == (address & alignMask));
305
306#if IOALLOCDEBUG
307 if( address)
308 debug_iomalloc_size += size;
309#endif
310
311 return (void *) address;
312}
313
314void IOFreeContiguous(void * address, vm_size_t size)
315{
316 vm_address_t allocationAddress;
317 vm_size_t adjustedSize;
318
319 if( !address)
320 return;
321
322 assert(size);
323
324 adjustedSize = (2 * size) + sizeof(vm_size_t) + sizeof(vm_address_t);
325 if (adjustedSize >= page_size) {
326
327 kmem_free( kernel_map, (vm_address_t) address, size);
328
329 } else {
330 adjustedSize = *((vm_size_t *)( (vm_address_t) address
331 - sizeof(vm_address_t) - sizeof(vm_size_t)));
332 allocationAddress = *((vm_address_t *)( (vm_address_t) address
333 - sizeof(vm_address_t) ));
334
335 kfree((vm_offset_t) allocationAddress, adjustedSize);
336 }
337
338#if IOALLOCDEBUG
339 debug_iomalloc_size -= size;
340#endif
341}
342
343/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
344
345void * IOMallocPageable(vm_size_t size, vm_size_t alignment)
346{
347 kern_return_t kr = kIOReturnNotReady;
348 vm_address_t address;
349 vm_size_t segSize;
350 UInt32 attempts;
351 UInt32 index;
352 vm_offset_t min;
353 vm_map_t map;
354
355 if (alignment > page_size)
356 return( 0 );
357 if (size > kIOPageableMaxMapSize)
358 return( 0 );
359
360 do {
361 index = gIOKitPageableSpace.hint;
362 attempts = gIOKitPageableSpace.count;
363 while( attempts--) {
364 kr = kmem_alloc_pageable( gIOKitPageableSpace.maps[index].map,
365 &address, size);
366 if( KERN_SUCCESS == kr) {
367 gIOKitPageableSpace.hint = index;
368 break;
369 }
370 if( index)
371 index--;
372 else
373 index = gIOKitPageableSpace.count - 1;
374 }
375 if( KERN_SUCCESS == kr)
376 break;
377
378 mutex_lock( gIOKitPageableSpace.lock );
379
380 index = gIOKitPageableSpace.count;
381 if( index >= (kIOMaxPageableMaps - 1)) {
382 mutex_unlock( gIOKitPageableSpace.lock );
383 break;
384 }
385
386 if( size < kIOPageableMapSize)
387 segSize = kIOPageableMapSize;
388 else
389 segSize = size;
390
391 min = 0;
392 kr = kmem_suballoc(kernel_map,
393 &min,
394 segSize,
395 TRUE,
396 TRUE,
397 &map);
398 if( KERN_SUCCESS != kr) {
399 mutex_unlock( gIOKitPageableSpace.lock );
400 break;
401 }
402
403 gIOKitPageableSpace.maps[index].map = map;
404 gIOKitPageableSpace.maps[index].address = min;
405 gIOKitPageableSpace.maps[index].end = min + segSize;
406 gIOKitPageableSpace.hint = index;
407 gIOKitPageableSpace.count = index + 1;
408
409 mutex_unlock( gIOKitPageableSpace.lock );
410
411 } while( true );
412
413 if( KERN_SUCCESS != kr)
414 address = 0;
415
416#if IOALLOCDEBUG
417 if( address)
418 debug_iomalloc_size += round_page(size);
419#endif
420
421 return (void *) address;
422}
423
424vm_map_t IOPageableMapForAddress( vm_address_t address )
425{
426 vm_map_t map = 0;
427 UInt32 index;
428
429 for( index = 0; index < gIOKitPageableSpace.count; index++) {
430 if( (address >= gIOKitPageableSpace.maps[index].address)
431 && (address < gIOKitPageableSpace.maps[index].end) ) {
432 map = gIOKitPageableSpace.maps[index].map;
433 break;
434 }
435 }
436 if( !map)
437 IOPanic("IOPageableMapForAddress: null");
438
439 return( map );
440}
441
442void IOFreePageable(void * address, vm_size_t size)
443{
444 vm_map_t map;
445
446 map = IOPageableMapForAddress( (vm_address_t) address);
447 if( map)
448 kmem_free( map, (vm_offset_t) address, size);
449
450#if IOALLOCDEBUG
451 debug_iomalloc_size -= round_page(size);
452#endif
453}
454
455/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
456
457extern kern_return_t IOMapPages(vm_map_t map, vm_offset_t va, vm_offset_t pa,
458 vm_size_t length, unsigned int options);
459
460IOReturn IOSetProcessorCacheMode( task_t task, IOVirtualAddress address,
461 IOByteCount length, IOOptionBits cacheMode )
462{
463 IOReturn ret = kIOReturnSuccess;
464 vm_offset_t physAddr;
465
466 if( task != kernel_task)
467 return( kIOReturnUnsupported );
468
469 length = round_page(address + length) - trunc_page( address );
470 address = trunc_page( address );
471
472 // make map mode
473 cacheMode = (cacheMode << kIOMapCacheShift) & kIOMapCacheMask;
474
475 while( (kIOReturnSuccess == ret) && (length > 0) ) {
476
477 physAddr = pmap_extract( kernel_pmap, address );
478 if( physAddr)
479 ret = IOMapPages( get_task_map(task), address, physAddr, page_size, cacheMode );
480 else
481 ret = kIOReturnVMError;
482
483 length -= page_size;
484 }
485
486 return( ret );
487}
488
489
490IOReturn IOFlushProcessorCache( task_t task, IOVirtualAddress address,
491 IOByteCount length )
492{
493 if( task != kernel_task)
494 return( kIOReturnUnsupported );
495
496#if __ppc__
497 flush_dcache( (vm_offset_t) address, (unsigned) length, false );
498#endif
499
500 return( kIOReturnSuccess );
501}
502
503/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
504
505SInt32 OSKernelStackRemaining( void )
506{
507 SInt32 stack;
508
509 stack = (((SInt32) &stack) & (KERNEL_STACK_SIZE - 1));
510
511 return( stack );
512}
513
514/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
515
516void IOSleep(unsigned milliseconds)
517{
518 int wait_result;
519
520 assert_wait_timeout(milliseconds, THREAD_INTERRUPTIBLE);
521 wait_result = thread_block((void (*)(void))0);
522 if (wait_result != THREAD_TIMED_OUT)
523 thread_cancel_timer();
524}
525
526/*
527 * Spin for indicated number of microseconds.
528 */
529void IODelay(unsigned microseconds)
530{
531 extern void delay(int usec);
532
533 delay(microseconds);
534}
535
536/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
537
538void IOLog(const char *format, ...)
539{
540 va_list ap;
541 extern void conslog_putc(char);
542 extern void logwakeup();
543
544 va_start(ap, format);
545 _doprnt(format, &ap, conslog_putc, 16);
546 va_end(ap);
547}
548
549void IOPanic(const char *reason)
550{
551 panic(reason);
552}
553
554/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
555
556/*
557 * Convert a integer constant (typically a #define or enum) to a string.
558 */
559static char noValue[80]; // that's pretty
560
561const char *IOFindNameForValue(int value, const IONamedValue *regValueArray)
562{
563 for( ; regValueArray->name; regValueArray++) {
564 if(regValueArray->value == value)
565 return(regValueArray->name);
566 }
567 sprintf(noValue, "0x%x (UNDEFINED)", value);
568 return((const char *)noValue);
569}
570
571IOReturn IOFindValueForName(const char *string,
572 const IONamedValue *regValueArray,
573 int *value)
574{
575 for( ; regValueArray->name; regValueArray++) {
576 if(!strcmp(regValueArray->name, string)) {
577 *value = regValueArray->value;
578 return kIOReturnSuccess;
579 }
580 }
581 return kIOReturnBadArgument;
582}
583
584/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
585
586IOAlignment IOSizeToAlignment(unsigned int size)
587{
588 register int shift;
589 const int intsize = sizeof(unsigned int) * 8;
590
591 for (shift = 1; shift < intsize; shift++) {
592 if (size & 0x80000000)
593 return (IOAlignment)(intsize - shift);
594 size <<= 1;
595 }
596 return 0;
597}
598
599unsigned int IOAlignmentToSize(IOAlignment align)
600{
601 unsigned int size;
602
603 for (size = 1; align; align--) {
604 size <<= 1;
605 }
606 return size;
607}