1 /*
2  * Copyright (c) 1998-2000 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 
30 #ifndef _IOKIT_KERNELINTERNAL_H
31 #define _IOKIT_KERNELINTERNAL_H
32 
33 #include <sys/cdefs.h>
34 
35 __BEGIN_DECLS
36 
37 #include <vm/vm_pageout.h>
38 #include <mach/memory_object_types.h>
39 #include <device/device_port.h>
40 #include <IOKit/IODMACommand.h>
41 #include <IOKit/IOKitServer.h>
42 #include <kern/socd_client.h>
43 
44 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
45 
46 typedef kern_return_t (*IOIteratePageableMapsCallback)(vm_map_t map, void * ref);
47 
48 void IOLibInit(void);
49 kern_return_t IOIteratePageableMaps(vm_size_t size,
50     IOIteratePageableMapsCallback callback, void * ref);
51 vm_map_t IOPageableMapForAddress(uintptr_t address);
52 
53 struct IOMemoryDescriptorMapAllocRef {
54 	vm_map_t          map;
55 	mach_vm_address_t mapped;
56 	mach_vm_size_t    size;
57 	vm_prot_t         prot;
58 	vm_tag_t          tag;
59 	IOOptionBits      options;
60 };
61 
62 kern_return_t
63 IOMemoryDescriptorMapAlloc(vm_map_t map, void * ref);
64 
65 
66 mach_vm_address_t
67 IOKernelAllocateWithPhysicalRestrict(
68 	kalloc_heap_t       kheap,
69 	mach_vm_size_t      size,
70 	mach_vm_address_t   maxPhys,
71 	mach_vm_size_t      alignment,
72 	bool                contiguous);
73 void
74 IOKernelFreePhysical(
75 	kalloc_heap_t       kheap,
76 	mach_vm_address_t   address,
77 	mach_vm_size_t      size);
78 
79 #if IOTRACKING
80 IOReturn
81 IOMemoryMapTracking(IOTrackingUser * tracking, task_t * task,
82     mach_vm_address_t * address, mach_vm_size_t * size);
83 #endif /* IOTRACKING */
84 
85 extern vm_size_t debug_iomallocpageable_size;
86 
87 extern ppnum_t gIOLastPage;
88 
89 extern IOSimpleLock * gIOPageAllocLock;
90 extern queue_head_t   gIOPageAllocList;
91 
92 /* Physical to physical copy (ints must be disabled) */
93 extern void bcopy_phys(addr64_t from, addr64_t to, vm_size_t size);
94 
95 __END_DECLS
96 
97 #define __IODEQUALIFY(type, expr)                               \
98    ({ typeof(expr) expr_ = (type)(uintptr_t)(expr);             \
99        (type)(uintptr_t)(expr_); })
100 
101 struct IODMACommandMapSegment {
102 	uint64_t fDMAOffset;       // The offset of this segment in DMA
103 	uint64_t fMapOffset;       // Offset of segment in mapping
104 	uint64_t fPageOffset;      // Offset within first page of segment
105 };
106 
107 struct IODMACommandInternal {
108 	IOMDDMAWalkSegmentState      fState;
109 	IOMDDMACharacteristics       fMDSummary;
110 
111 	UInt64 fPreparedOffset;
112 	UInt64 fPreparedLength;
113 
114 	UInt32 fSourceAlignMask;
115 
116 	UInt8  fCursor;
117 	UInt8  fCheckAddressing;
118 	UInt8  fIterateOnly;
119 	UInt8  fMisaligned;
120 	UInt8  fPrepared;
121 	UInt8  fDoubleBuffer;
122 	UInt8  fNewMD;
123 	UInt8  fLocalMapperAllocValid;
124 	UInt8  fIOVMAddrValid;
125 	UInt8  fForceDoubleBuffer;
126 	UInt8  fSetActiveNoMapper;
127 
128 	vm_page_t fCopyPageAlloc;
129 	vm_page_t fCopyNext;
130 	vm_page_t fNextRemapPage;
131 
132 	ppnum_t  fCopyPageCount;
133 
134 	uint64_t  fLocalMapperAlloc;
135 	uint64_t  fLocalMapperAllocLength;
136 
137 	OSPtr<IOBufferMemoryDescriptor> fCopyMD;
138 
139 	IOService * fDevice;
140 	IOLock * fDextLock;
141 
142 	// IODMAEventSource use
143 	IOReturn fStatus;
144 	UInt64   fActualByteCount;
145 	AbsoluteTime    fTimeStamp;
146 
147 	// Multisegment vars
148 	IODMACommandMapSegment * fMapSegments;
149 	uint32_t                 fMapSegmentsCount;
150 	uint64_t fLocalMapperAllocBase;
151 	uint64_t fOffset2Index;
152 	uint64_t fNextOffset;
153 	uint64_t fIndex;
154 };
155 
156 struct IOMemoryDescriptorDevicePager {
157 	void *                       devicePager;
158 	unsigned int             pagerContig:1;
159 	unsigned int             unused:31;
160 	IOMemoryDescriptor * memory;
161 };
162 
163 struct IOMemoryDescriptorReserved {
164 	IOMemoryDescriptorDevicePager dp;
165 	uint64_t                      descriptorID;
166 	uint64_t                      preparationID;
167 	// for kernel IOMD subclasses... they have no expansion
168 	uint64_t                      kernReserved[4];
169 	vm_tag_t                      kernelTag;
170 	vm_tag_t                      userTag;
171 	task_t                        creator;
172 	OSObject                    * contextObject;
173 };
174 
175 #if defined(__x86_64__)
176 struct iopa_t {
177 	IOLock       * lock;
178 	queue_head_t   list;
179 	vm_size_t      pagecount;
180 	vm_size_t      bytecount;
181 };
182 
183 struct iopa_page_t {
184 	queue_chain_t link;
185 	uint64_t      avail;
186 	uint32_t      signature;
187 };
188 typedef struct iopa_page_t iopa_page_t;
189 
190 typedef uintptr_t (*iopa_proc_t)(kalloc_heap_t kheap, iopa_t * a);
191 
192 enum{
193 	kIOPageAllocSignature  = 'iopa'
194 };
195 
196 extern "C" void      iopa_init(iopa_t * a);
197 extern "C" uintptr_t iopa_alloc(iopa_t * a, iopa_proc_t alloc, kalloc_heap_t kheap,
198     vm_size_t bytes, vm_size_t balign);
199 extern "C" uintptr_t iopa_free(iopa_t * a, uintptr_t addr, vm_size_t bytes);
200 extern "C" uint32_t  gIOPageAllocChunkBytes;
201 
202 extern "C" iopa_t    gIOBMDPageAllocator;
203 #endif /* defined(__x86_64__) */
204 
205 
206 extern "C" struct timeval gIOLastSleepTime;
207 extern "C" struct timeval gIOLastWakeTime;
208 
209 extern clock_sec_t gIOConsoleLockTime;
210 
211 extern bool gCPUsRunning;
212 
213 extern OSSet * gIORemoveOnReadProperties;
214 
215 extern uint32_t gHaltTimeMaxLog;
216 extern uint32_t gHaltTimeMaxPanic;
217 
218 extern "C" void IOKitInitializeTime( void );
219 extern void IOMachPortInitialize(void);
220 
221 extern "C" OSString * IOCopyLogNameForPID(int pid);
222 
223 extern "C" void IOKitKernelLogBuffer(const char * title, const void * buffer, size_t size,
224     void (*output)(const char *format, ...));
225 
226 #if defined(__i386__) || defined(__x86_64__)
227 #ifndef __cplusplus
228 #error xx
229 #endif
230 
231 extern const OSSymbol * gIOCreateEFIDevicePathSymbol;
232 extern "C" void IOSetKeyStoreData(LIBKERN_CONSUMED IOMemoryDescriptor * data);
233 extern "C" void IOSetAPFSKeyStoreData(LIBKERN_CONSUMED IOMemoryDescriptor* data);
234 #endif
235 extern const  OSSymbol * gAKSGetKey;
236 
237 void IOScreenLockTimeUpdate(clock_sec_t secs);
238 
239 void     IOCPUInitialize(void);
240 IOReturn IOInstallServicePlatformActions(IOService * service);
241 IOReturn IOInstallServiceSleepPlatformActions(IOService * service);
242 IOReturn IORemoveServicePlatformActions(IOService * service);
243 void     IOCPUSleepKernel(void);
244 void     IOPlatformActionsInitialize(void);
245 
246 class IOSystemStateNotification : public IOService
247 {
248 	OSDeclareDefaultStructors(IOSystemStateNotification);
249 public:
250 	static IOService * initialize(void);
251 	virtual IOReturn setProperties( OSObject * properties) APPLE_KEXT_OVERRIDE;
252 	virtual bool serializeProperties(OSSerialize * serialize) const APPLE_KEXT_OVERRIDE;
253 };
254 
255 #endif /* ! _IOKIT_KERNELINTERNAL_H */
256