]> git.saurik.com Git - apple/xnu.git/blame_incremental - iokit/IOKit/IOLib.h
xnu-7195.101.1.tar.gz
[apple/xnu.git] / iokit / IOKit / IOLib.h
... / ...
CommitLineData
1/*
2 * Copyright (c) 1998-2016 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 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
30 *
31 * HISTORY
32 *
33 */
34
35#ifndef __IOKIT_IOLIB_H
36#define __IOKIT_IOLIB_H
37
38#ifndef KERNEL
39#error IOLib.h is for kernel use only
40#endif
41
42#include <stdarg.h>
43#include <sys/cdefs.h>
44#include <os/overflow.h>
45
46#include <sys/appleapiopts.h>
47
48#include <IOKit/system.h>
49
50#include <IOKit/IOReturn.h>
51#include <IOKit/IOTypes.h>
52#include <IOKit/IOLocks.h>
53
54#include <libkern/OSAtomic.h>
55
56__BEGIN_DECLS
57
58#include <kern/thread_call.h>
59#include <kern/clock.h>
60
61/*
62 * min/max macros.
63 */
64
65#define min(a, b) ((a) < (b) ? (a) : (b))
66#define max(a, b) ((a) > (b) ? (a) : (b))
67
68/*
69 * These are opaque to the user.
70 */
71typedef thread_t IOThread;
72typedef void (*IOThreadFunc)(void *argument);
73
74/*
75 * Memory allocation functions.
76 */
77
78/*! @function IOMalloc
79 * @abstract Allocates general purpose, wired memory in the kernel map.
80 * @discussion This is a general purpose utility to allocate memory in the kernel. There are no alignment guarantees given on the returned memory, and alignment may vary depending on the kernel configuration. This function may block and so should not be called from interrupt level or while a simple lock is held.
81 * @param size Size of the memory requested.
82 * @result Pointer to the allocated memory, or zero on failure. */
83
84#if defined(XNU_KERNEL_PRIVATE)
85
86/*
87 * IOMalloc_internal allocates memory from the specifed kalloc heap, which can be:
88 * - KHEAP_DATA_BUFFERS: Should be used for data buffers
89 * - KHEAP_KEXT: Should be used for non core kernel allocations
90 * - KHEAP_DEFAULT: Should be used for all core kernel allocations that
91 * aren't data buffers.
92 *
93 * The kalloc heap used by IOMalloc calls from core kernel is KHEAP_DEFAULT
94 * and that used by kexts (accessed via IOMalloc_external) is KHEAP_KEXT.
95 *
96 * For more details on kalloc_heaps see kalloc.h
97 */
98
99extern void *
100IOMalloc_internal(
101 struct kalloc_heap * kalloc_heap_cfg,
102 vm_size_t size) __attribute__((alloc_size(2)));
103
104extern void *
105IOMallocZero_internal(
106 struct kalloc_heap * kalloc_heap_cfg,
107 vm_size_t size) __attribute__((alloc_size(2)));
108
109#define IOMalloc(size) IOMalloc_internal(KHEAP_DEFAULT, size)
110
111#define IOMallocZero(size) IOMallocZero_internal(KHEAP_DEFAULT, size)
112
113#else/* defined(XNU_KERNEL_PRIVATE) */
114
115void * IOMalloc(vm_size_t size) __attribute__((alloc_size(1)));
116void * IOMallocZero(vm_size_t size) __attribute__((alloc_size(1)));
117
118#endif /* !defined(XNU_KERNEL_PRIVATE) */
119
120/*! @function IOFree
121 * @abstract Frees memory allocated with IOMalloc.
122 * @discussion This function frees memory allocated with IOMalloc, it may block and so should not be called from interrupt level or while a simple lock is held.
123 * @param address Pointer to the allocated memory. Must be identical to result
124 * @of a prior IOMalloc.
125 * @param size Size of the memory allocated. Must be identical to size of
126 * @the corresponding IOMalloc */
127
128void IOFree(void * address, vm_size_t size);
129
130/*! @function IOMallocAligned
131 * @abstract Allocates wired memory in the kernel map, with an alignment restriction.
132 * @discussion This is a utility to allocate memory in the kernel, with an alignment restriction which is specified as a byte count. This function may block and so should not be called from interrupt level or while a simple lock is held.
133 * @param size Size of the memory requested.
134 * @param alignment Byte count of the alignment for the memory. For example, pass 256 to get memory allocated at an address with bit 0-7 zero.
135 * @result Pointer to the allocated memory, or zero on failure. */
136
137#if defined(XNU_KERNEL_PRIVATE)
138
139extern void *
140IOMallocAligned_internal(
141 struct kalloc_heap * kalloc_heap_cfg,
142 vm_size_t size,
143 vm_size_t alignment) __attribute__((alloc_size(2)));
144
145#define IOMallocAligned(size, alignment) IOMallocAligned_internal(KHEAP_DEFAULT, size, alignment)
146
147#else/* defined(XNU_KERNEL_PRIVATE) */
148
149void * IOMallocAligned(vm_size_t size, vm_offset_t alignment) __attribute__((alloc_size(1)));
150
151#endif /* !defined(XNU_KERNEL_PRIVATE) */
152
153/*! @function IOFreeAligned
154 * @abstract Frees memory allocated with IOMallocAligned.
155 * @discussion This function frees memory allocated with IOMallocAligned, it may block and so should not be called from interrupt level or while a simple lock is held.
156 * @param address Pointer to the allocated memory.
157 * @param size Size of the memory allocated. */
158
159void IOFreeAligned(void * address, vm_size_t size);
160
161/*! @function IOMallocContiguous
162 * @abstract Deprecated - use IOBufferMemoryDescriptor. Allocates wired memory in the kernel map, with an alignment restriction and physically contiguous.
163 * @discussion This is a utility to allocate memory in the kernel, with an alignment restriction which is specified as a byte count, and will allocate only physically contiguous memory. The request may fail if memory is fragmented, and may cause large amounts of paging activity. This function may block and so should not be called from interrupt level or while a simple lock is held.
164 * @param size Size of the memory requested.
165 * @param alignment Byte count of the alignment for the memory. For example, pass 256 to get memory allocated at an address with bits 0-7 zero.
166 * @param physicalAddress IOMallocContiguous returns the physical address of the allocated memory here, if physicalAddress is a non-zero pointer. The physicalAddress argument is deprecated and should be passed as NULL. To obtain the physical address for a memory buffer, use the IODMACommand class in conjunction with the IOMemoryDescriptor or IOBufferMemoryDescriptor classes.
167 * @result Virtual address of the allocated memory, or zero on failure. */
168
169void * IOMallocContiguous(vm_size_t size, vm_size_t alignment,
170 IOPhysicalAddress * physicalAddress) __attribute__((deprecated)) __attribute__((alloc_size(1)));
171
172/*! @function IOFreeContiguous
173 * @abstract Deprecated - use IOBufferMemoryDescriptor. Frees memory allocated with IOMallocContiguous.
174 * @discussion This function frees memory allocated with IOMallocContiguous, it may block and so should not be called from interrupt level or while a simple lock is held.
175 * @param address Virtual address of the allocated memory.
176 * @param size Size of the memory allocated. */
177
178void IOFreeContiguous(void * address, vm_size_t size) __attribute__((deprecated));
179
180
181/*! @function IOMallocPageable
182 * @abstract Allocates pageable memory in the kernel map.
183 * @discussion This is a utility to allocate pageable memory in the kernel. This function may block and so should not be called from interrupt level or while a simple lock is held.
184 * @param size Size of the memory requested.
185 * @param alignment Byte count of the alignment for the memory. For example, pass 256 to get memory allocated at an address with bits 0-7 zero.
186 * @result Pointer to the allocated memory, or zero on failure. */
187
188void * IOMallocPageable(vm_size_t size, vm_size_t alignment) __attribute__((alloc_size(1)));
189
190/*! @function IOMallocPageableZero
191 * @abstract Allocates pageable, zeroed memory in the kernel map.
192 * @discussion Same as IOMallocPageable but guarantees the returned memory will be zeroed.
193 * @param size Size of the memory requested.
194 * @param alignment Byte count of the alignment for the memory. For example, pass 256 to get memory allocated at an address with bits 0-7 zero.
195 * @result Pointer to the allocated memory, or zero on failure. */
196
197void * IOMallocPageableZero(vm_size_t size, vm_size_t alignment) __attribute__((alloc_size(1)));
198
199/*! @function IOFreePageable
200 * @abstract Frees memory allocated with IOMallocPageable.
201 * @discussion This function frees memory allocated with IOMallocPageable, it may block and so should not be called from interrupt level or while a simple lock is held.
202 * @param address Virtual address of the allocated memory.
203 * @param size Size of the memory allocated. */
204
205void IOFreePageable(void * address, vm_size_t size);
206
207/*
208 * Typed memory allocation macros. All may block.
209 */
210
211#define IONew(type, count) \
212({ \
213 size_t __size; \
214 (os_mul_overflow(sizeof(type), (count), &__size) \
215 ? ((type *) NULL) \
216 : ((type *) IOMalloc(__size))); \
217})
218
219#define IONewZero(type, count) \
220({ \
221 size_t __size; \
222 (os_mul_overflow(sizeof(type), (count), &__size) \
223 ? ((type *) NULL) \
224 : ((type *) IOMallocZero(__size))); \
225})
226
227#define IODelete(ptr, type, count) \
228({ \
229 size_t __size; \
230 if (!os_mul_overflow(sizeof(type), (count), &__size)) { \
231 IOFree(ptr, __size); \
232 } \
233})
234
235#define IOSafeDeleteNULL(ptr, type, count) \
236 do { \
237 if (NULL != (ptr)) { \
238 IODelete((ptr), type, count); \
239 (ptr) = NULL; \
240 } \
241 } while (0) \
242
243/////////////////////////////////////////////////////////////////////////////
244//
245//
246// These functions are now implemented in IOMapper.cpp
247//
248//
249/////////////////////////////////////////////////////////////////////////////
250
251/*! @function IOMappedRead8
252 * @abstract Read one byte from the desired "Physical" IOSpace address.
253 * @discussion Read one byte from the desired "Physical" IOSpace address. This function allows the developer to read an address returned from any memory descriptor's getPhysicalSegment routine. It can then be used by segmenting a physical page slightly to tag the physical page with its kernel space virtual address.
254 * @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
255 * @result Data contained at that location */
256
257UInt8 IOMappedRead8(IOPhysicalAddress address);
258
259/*! @function IOMappedRead16
260 * @abstract Read two bytes from the desired "Physical" IOSpace address.
261 * @discussion Read two bytes from the desired "Physical" IOSpace address. This function allows the developer to read an address returned from any memory descriptor's getPhysicalSegment routine. It can then be used by segmenting a physical page slightly to tag the physical page with its kernel space virtual address.
262 * @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
263 * @result Data contained at that location */
264
265UInt16 IOMappedRead16(IOPhysicalAddress address);
266
267/*! @function IOMappedRead32
268 * @abstract Read four bytes from the desired "Physical" IOSpace address.
269 * @discussion Read four bytes from the desired "Physical" IOSpace address. This function allows the developer to read an address returned from any memory descriptor's getPhysicalSegment routine. It can then be used by segmenting a physical page slightly to tag the physical page with its kernel space virtual address.
270 * @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
271 * @result Data contained at that location */
272
273UInt32 IOMappedRead32(IOPhysicalAddress address);
274
275/*! @function IOMappedRead64
276 * @abstract Read eight bytes from the desired "Physical" IOSpace address.
277 * @discussion Read eight bytes from the desired "Physical" IOSpace address. This function allows the developer to read an address returned from any memory descriptor's getPhysicalSegment routine. It can then be used by segmenting a physical page slightly to tag the physical page with its kernel space virtual address.
278 * @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
279 * @result Data contained at that location */
280
281UInt64 IOMappedRead64(IOPhysicalAddress address);
282
283/*! @function IOMappedWrite8
284 * @abstract Write one byte to the desired "Physical" IOSpace address.
285 * @discussion Write one byte to the desired "Physical" IOSpace address. This function allows the developer to write to an address returned from any memory descriptor's getPhysicalSegment routine.
286 * @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
287 * @param value Data to be writen to the desired location */
288
289void IOMappedWrite8(IOPhysicalAddress address, UInt8 value);
290
291/*! @function IOMappedWrite16
292 * @abstract Write two bytes to the desired "Physical" IOSpace address.
293 * @discussion Write two bytes to the desired "Physical" IOSpace address. This function allows the developer to write to an address returned from any memory descriptor's getPhysicalSegment routine.
294 * @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
295 * @param value Data to be writen to the desired location */
296
297void IOMappedWrite16(IOPhysicalAddress address, UInt16 value);
298
299/*! @function IOMappedWrite32
300 * @abstract Write four bytes to the desired "Physical" IOSpace address.
301 * @discussion Write four bytes to the desired "Physical" IOSpace address. This function allows the developer to write to an address returned from any memory descriptor's getPhysicalSegment routine.
302 * @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
303 * @param value Data to be writen to the desired location */
304
305void IOMappedWrite32(IOPhysicalAddress address, UInt32 value);
306
307/*! @function IOMappedWrite64
308 * @abstract Write eight bytes to the desired "Physical" IOSpace address.
309 * @discussion Write eight bytes to the desired "Physical" IOSpace address. This function allows the developer to write to an address returned from any memory descriptor's getPhysicalSegment routine.
310 * @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
311 * @param value Data to be writen to the desired location */
312
313void IOMappedWrite64(IOPhysicalAddress address, UInt64 value);
314
315/* This function is deprecated. Cache settings may be set for allocated memory with the IOBufferMemoryDescriptor api. */
316
317IOReturn IOSetProcessorCacheMode( task_t task, IOVirtualAddress address,
318 IOByteCount length, IOOptionBits cacheMode ) __attribute__((deprecated));
319
320/*! @function IOFlushProcessorCache
321 * @abstract Flushes the processor cache for mapped memory.
322 * @discussion This function flushes the processor cache of an already mapped memory range. Note in most cases it is preferable to use IOMemoryDescriptor::prepare and complete to manage cache coherency since they are aware of the architecture's requirements. Flushing the processor cache is not required for coherency in most situations.
323 * @param task Task the memory is mapped into.
324 * @param address Virtual address of the memory.
325 * @param length Length of the range to set.
326 * @result An IOReturn code. */
327
328IOReturn IOFlushProcessorCache( task_t task, IOVirtualAddress address,
329 IOByteCount length );
330
331/*! @function IOThreadSelf
332 * @abstract Returns the osfmk identifier for the currently running thread.
333 * @discussion This function returns the current thread (a pointer to the currently active osfmk thread_shuttle). */
334
335#define IOThreadSelf() (current_thread())
336
337/*! @function IOCreateThread
338 * @abstract Deprecated function - use kernel_thread_start(). Create a kernel thread.
339 * @discussion This function creates a kernel thread, and passes the caller supplied argument to the new thread. Warning: the value returned by this function is not 100% reliable. There is a race condition where it is possible that the new thread has already terminated before this call returns. Under that circumstance the IOThread returned will be invalid. In general there is little that can be done with this value except compare it against 0. The thread itself can call IOThreadSelf() 100% reliably and that is the prefered mechanism to manipulate the IOThreads state.
340 * @param function A C-function pointer where the thread will begin execution.
341 * @param argument Caller specified data to be passed to the new thread.
342 * @result An IOThread identifier for the new thread, equivalent to an osfmk thread_t. */
343
344IOThread IOCreateThread(IOThreadFunc function, void *argument) __attribute__((deprecated));
345
346/*! @function IOExitThread
347 * @abstract Deprecated function - use thread_terminate(). Terminate execution of current thread.
348 * @discussion This function destroys the currently running thread, and does not return. */
349
350void IOExitThread(void) __attribute__((deprecated));
351
352/*! @function IOSleep
353 * @abstract Sleep the calling thread for a number of milliseconds.
354 * @discussion This function blocks the calling thread for at least the number of specified milliseconds, giving time to other processes.
355 * @param milliseconds The integer number of milliseconds to wait. */
356
357void IOSleep(unsigned milliseconds);
358
359/*! @function IOSleepWithLeeway
360 * @abstract Sleep the calling thread for a number of milliseconds, with a specified leeway the kernel may use for timer coalescing.
361 * @discussion This function blocks the calling thread for at least the number of specified milliseconds, giving time to other processes. The kernel may also coalesce any timers involved in the delay, using the leeway given as a guideline.
362 * @param intervalMilliseconds The integer number of milliseconds to wait.
363 * @param leewayMilliseconds The integer number of milliseconds to use as a timer coalescing guideline. */
364
365void IOSleepWithLeeway(unsigned intervalMilliseconds, unsigned leewayMilliseconds);
366
367/*! @function IODelay
368 * @abstract Spin delay for a number of microseconds.
369 * @discussion This function spins to delay for at least the number of specified microseconds. Since the CPU is busy spinning no time is made available to other processes; this method of delay should be used only for short periods. Also, the AbsoluteTime based APIs of kern/clock.h provide finer grained and lower cost delays.
370 * @param microseconds The integer number of microseconds to spin wait. */
371
372void IODelay(unsigned microseconds);
373
374/*! @function IOPause
375 * @abstract Spin delay for a number of nanoseconds.
376 * @discussion This function spins to delay for at least the number of specified nanoseconds. Since the CPU is busy spinning no time is made available to other processes; this method of delay should be used only for short periods.
377 * @param nanoseconds The integer number of nanoseconds to spin wait. */
378
379void IOPause(unsigned nanoseconds);
380
381/*! @function IOLog
382 * @abstract Log a message to console in text mode, and /var/log/system.log.
383 * @discussion This function allows a driver to log diagnostic information to the screen during verbose boots, and to a log file found at /var/log/system.log. IOLog should not be called from interrupt context.
384 * @param format A printf() style format string (see printf(3) documentation).
385 */
386
387void IOLog(const char *format, ...)
388__attribute__((format(printf, 1, 2)));
389
390/*! @function IOLogv
391 * @abstract Log a message to console in text mode, and /var/log/system.log.
392 * @discussion This function allows a driver to log diagnostic information to the screen during verbose boots, and to a log file found at /var/log/system.log. IOLogv should not be called from interrupt context.
393 * @param format A printf() style format string (see printf(3) documentation).
394 * @param ap stdarg(3) style variable arguments. */
395
396void IOLogv(const char *format, va_list ap)
397__attribute__((format(printf, 1, 0)));
398
399#ifndef _FN_KPRINTF
400#define _FN_KPRINTF
401void kprintf(const char *format, ...) __printflike(1, 2);
402#endif
403#ifndef _FN_KPRINTF_DECLARED
404#define _FN_KPRINTF_DECLARED
405#endif
406
407/*
408 * Convert a integer constant (typically a #define or enum) to a string
409 * via an array of IONamedValue.
410 */
411const char *IOFindNameForValue(int value,
412 const IONamedValue *namedValueArray);
413
414/*
415 * Convert a string to an int via an array of IONamedValue. Returns
416 * kIOReturnSuccess of string found, else returns kIOReturnBadArgument.
417 */
418IOReturn IOFindValueForName(const char *string,
419 const IONamedValue *regValueArray,
420 int *value); /* RETURNED */
421
422/*! @function Debugger
423 * @abstract Enter the kernel debugger.
424 * @discussion This function freezes the kernel and enters the builtin debugger. It may not be possible to exit the debugger without a second machine.
425 * @param reason A C-string to describe why the debugger is being entered. */
426
427void Debugger(const char * reason);
428#if __LP64__
429#define IOPanic(reason) panic("%s", reason)
430#else
431void IOPanic(const char *reason) __attribute__((deprecated)) __abortlike;
432#endif
433
434#ifdef __cplusplus
435class OSDictionary;
436#endif
437
438#ifdef __cplusplus
439OSDictionary *
440#else
441struct OSDictionary *
442#endif
443IOBSDNameMatching( const char * name );
444
445#ifdef __cplusplus
446OSDictionary *
447#else
448struct OSDictionary *
449#endif
450IOOFPathMatching( const char * path, char * buf, int maxLen ) __attribute__((deprecated));
451
452/*
453 * Convert between size and a power-of-two alignment.
454 */
455IOAlignment IOSizeToAlignment(unsigned int size);
456unsigned int IOAlignmentToSize(IOAlignment align);
457
458/*
459 * Multiply and divide routines for IOFixed datatype.
460 */
461
462static inline IOFixed
463IOFixedMultiply(IOFixed a, IOFixed b)
464{
465 return (IOFixed)((((SInt64) a) * ((SInt64) b)) >> 16);
466}
467
468static inline IOFixed
469IOFixedDivide(IOFixed a, IOFixed b)
470{
471 return (IOFixed)((((SInt64) a) << 16) / ((SInt64) b));
472}
473
474/*
475 * IORound and IOTrunc convenience functions, in the spirit
476 * of vm's round_page() and trunc_page().
477 */
478#define IORound(value, multiple) \
479 ((((value) + (multiple) - 1) / (multiple)) * (multiple))
480
481#define IOTrunc(value, multiple) \
482 (((value) / (multiple)) * (multiple));
483
484
485#if defined(__APPLE_API_OBSOLETE)
486
487/* The following API is deprecated */
488
489/* The API exported by kern/clock.h
490 * should be used for high resolution timing. */
491
492void IOGetTime( mach_timespec_t * clock_time) __attribute__((deprecated));
493
494#if !defined(__LP64__)
495
496#undef eieio
497#define eieio() \
498 OSSynchronizeIO()
499
500extern mach_timespec_t IOZeroTvalspec;
501
502#endif /* !defined(__LP64__) */
503
504#endif /* __APPLE_API_OBSOLETE */
505
506#if XNU_KERNEL_PRIVATE
507vm_tag_t
508IOMemoryTag(vm_map_t map);
509#endif
510
511__END_DECLS
512
513#endif /* !__IOKIT_IOLIB_H */