1*27b03b36SApple OSS Distributions /*
2*27b03b36SApple OSS Distributions * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3*27b03b36SApple OSS Distributions *
4*27b03b36SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*27b03b36SApple OSS Distributions *
6*27b03b36SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*27b03b36SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*27b03b36SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*27b03b36SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*27b03b36SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*27b03b36SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*27b03b36SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*27b03b36SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*27b03b36SApple OSS Distributions *
15*27b03b36SApple OSS Distributions * Please obtain a copy of the License at
16*27b03b36SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*27b03b36SApple OSS Distributions *
18*27b03b36SApple OSS Distributions * The Original Code and all software distributed under the License are
19*27b03b36SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*27b03b36SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*27b03b36SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*27b03b36SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*27b03b36SApple OSS Distributions * Please see the License for the specific language governing rights and
24*27b03b36SApple OSS Distributions * limitations under the License.
25*27b03b36SApple OSS Distributions *
26*27b03b36SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*27b03b36SApple OSS Distributions */
28*27b03b36SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
29*27b03b36SApple OSS Distributions
30*27b03b36SApple OSS Distributions #define _IOMEMORYDESCRIPTOR_INTERNAL_
31*27b03b36SApple OSS Distributions
32*27b03b36SApple OSS Distributions #include <IOKit/assert.h>
33*27b03b36SApple OSS Distributions #include <IOKit/system.h>
34*27b03b36SApple OSS Distributions
35*27b03b36SApple OSS Distributions #include <IOKit/IOLib.h>
36*27b03b36SApple OSS Distributions #include <IOKit/IOMapper.h>
37*27b03b36SApple OSS Distributions #include <IOKit/IOBufferMemoryDescriptor.h>
38*27b03b36SApple OSS Distributions #include <libkern/OSDebug.h>
39*27b03b36SApple OSS Distributions #include <mach/mach_vm.h>
40*27b03b36SApple OSS Distributions
41*27b03b36SApple OSS Distributions #include "IOKitKernelInternal.h"
42*27b03b36SApple OSS Distributions
43*27b03b36SApple OSS Distributions #ifdef IOALLOCDEBUG
44*27b03b36SApple OSS Distributions #include <libkern/c++/OSCPPDebug.h>
45*27b03b36SApple OSS Distributions #endif
46*27b03b36SApple OSS Distributions #include <IOKit/IOStatisticsPrivate.h>
47*27b03b36SApple OSS Distributions
48*27b03b36SApple OSS Distributions #if IOKITSTATS
49*27b03b36SApple OSS Distributions #define IOStatisticsAlloc(type, size) \
50*27b03b36SApple OSS Distributions do { \
51*27b03b36SApple OSS Distributions IOStatistics::countAlloc(type, size); \
52*27b03b36SApple OSS Distributions } while (0)
53*27b03b36SApple OSS Distributions #else
54*27b03b36SApple OSS Distributions #define IOStatisticsAlloc(type, size)
55*27b03b36SApple OSS Distributions #endif /* IOKITSTATS */
56*27b03b36SApple OSS Distributions
57*27b03b36SApple OSS Distributions
58*27b03b36SApple OSS Distributions __BEGIN_DECLS
59*27b03b36SApple OSS Distributions void ipc_port_release_send(ipc_port_t port);
60*27b03b36SApple OSS Distributions #include <vm/pmap.h>
61*27b03b36SApple OSS Distributions
62*27b03b36SApple OSS Distributions __END_DECLS
63*27b03b36SApple OSS Distributions
64*27b03b36SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
65*27b03b36SApple OSS Distributions
66*27b03b36SApple OSS Distributions enum{
67*27b03b36SApple OSS Distributions kInternalFlagPhysical = 0x00000001,
68*27b03b36SApple OSS Distributions kInternalFlagPageSized = 0x00000002,
69*27b03b36SApple OSS Distributions kInternalFlagPageAllocated = 0x00000004,
70*27b03b36SApple OSS Distributions kInternalFlagInit = 0x00000008,
71*27b03b36SApple OSS Distributions kInternalFlagHasPointers = 0x00000010,
72*27b03b36SApple OSS Distributions kInternalFlagGuardPages = 0x00000020,
73*27b03b36SApple OSS Distributions };
74*27b03b36SApple OSS Distributions
75*27b03b36SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
76*27b03b36SApple OSS Distributions
77*27b03b36SApple OSS Distributions #define super IOGeneralMemoryDescriptor
78*27b03b36SApple OSS Distributions OSDefineMetaClassAndStructorsWithZone(IOBufferMemoryDescriptor,
79*27b03b36SApple OSS Distributions IOGeneralMemoryDescriptor, ZC_ZFREE_CLEARMEM);
80*27b03b36SApple OSS Distributions
81*27b03b36SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
82*27b03b36SApple OSS Distributions
83*27b03b36SApple OSS Distributions #if defined(__x86_64__)
84*27b03b36SApple OSS Distributions static uintptr_t
IOBMDPageProc(kalloc_heap_t kheap,iopa_t * a)85*27b03b36SApple OSS Distributions IOBMDPageProc(kalloc_heap_t kheap, iopa_t * a)
86*27b03b36SApple OSS Distributions {
87*27b03b36SApple OSS Distributions kern_return_t kr;
88*27b03b36SApple OSS Distributions vm_address_t vmaddr = 0;
89*27b03b36SApple OSS Distributions kma_flags_t kma_flags = KMA_ZERO;
90*27b03b36SApple OSS Distributions
91*27b03b36SApple OSS Distributions if (kheap == KHEAP_DATA_BUFFERS) {
92*27b03b36SApple OSS Distributions kma_flags = (kma_flags_t) (kma_flags | KMA_DATA);
93*27b03b36SApple OSS Distributions }
94*27b03b36SApple OSS Distributions kr = kmem_alloc(kernel_map, &vmaddr, page_size,
95*27b03b36SApple OSS Distributions kma_flags, VM_KERN_MEMORY_IOKIT);
96*27b03b36SApple OSS Distributions
97*27b03b36SApple OSS Distributions if (KERN_SUCCESS != kr) {
98*27b03b36SApple OSS Distributions vmaddr = 0;
99*27b03b36SApple OSS Distributions }
100*27b03b36SApple OSS Distributions
101*27b03b36SApple OSS Distributions return (uintptr_t) vmaddr;
102*27b03b36SApple OSS Distributions }
103*27b03b36SApple OSS Distributions #endif /* defined(__x86_64__) */
104*27b03b36SApple OSS Distributions
105*27b03b36SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
106*27b03b36SApple OSS Distributions
107*27b03b36SApple OSS Distributions #ifndef __LP64__
108*27b03b36SApple OSS Distributions bool
initWithOptions(IOOptionBits options,vm_size_t capacity,vm_offset_t alignment,task_t inTask)109*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::initWithOptions(
110*27b03b36SApple OSS Distributions IOOptionBits options,
111*27b03b36SApple OSS Distributions vm_size_t capacity,
112*27b03b36SApple OSS Distributions vm_offset_t alignment,
113*27b03b36SApple OSS Distributions task_t inTask)
114*27b03b36SApple OSS Distributions {
115*27b03b36SApple OSS Distributions mach_vm_address_t physicalMask = 0;
116*27b03b36SApple OSS Distributions return initWithPhysicalMask(inTask, options, capacity, alignment, physicalMask);
117*27b03b36SApple OSS Distributions }
118*27b03b36SApple OSS Distributions #endif /* !__LP64__ */
119*27b03b36SApple OSS Distributions
120*27b03b36SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
withCopy(task_t inTask,IOOptionBits options,vm_map_t sourceMap,mach_vm_address_t source,mach_vm_size_t size)121*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::withCopy(
122*27b03b36SApple OSS Distributions task_t inTask,
123*27b03b36SApple OSS Distributions IOOptionBits options,
124*27b03b36SApple OSS Distributions vm_map_t sourceMap,
125*27b03b36SApple OSS Distributions mach_vm_address_t source,
126*27b03b36SApple OSS Distributions mach_vm_size_t size)
127*27b03b36SApple OSS Distributions {
128*27b03b36SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor> inst;
129*27b03b36SApple OSS Distributions kern_return_t err;
130*27b03b36SApple OSS Distributions vm_map_copy_t copy;
131*27b03b36SApple OSS Distributions vm_map_address_t address;
132*27b03b36SApple OSS Distributions
133*27b03b36SApple OSS Distributions copy = NULL;
134*27b03b36SApple OSS Distributions do {
135*27b03b36SApple OSS Distributions err = kIOReturnNoMemory;
136*27b03b36SApple OSS Distributions inst = OSMakeShared<IOBufferMemoryDescriptor>();
137*27b03b36SApple OSS Distributions if (!inst) {
138*27b03b36SApple OSS Distributions break;
139*27b03b36SApple OSS Distributions }
140*27b03b36SApple OSS Distributions inst->_ranges.v64 = IOMallocType(IOAddressRange);
141*27b03b36SApple OSS Distributions
142*27b03b36SApple OSS Distributions err = vm_map_copyin(sourceMap, source, size,
143*27b03b36SApple OSS Distributions false /* src_destroy */, ©);
144*27b03b36SApple OSS Distributions if (KERN_SUCCESS != err) {
145*27b03b36SApple OSS Distributions break;
146*27b03b36SApple OSS Distributions }
147*27b03b36SApple OSS Distributions
148*27b03b36SApple OSS Distributions err = vm_map_copyout(get_task_map(inTask), &address, copy);
149*27b03b36SApple OSS Distributions if (KERN_SUCCESS != err) {
150*27b03b36SApple OSS Distributions break;
151*27b03b36SApple OSS Distributions }
152*27b03b36SApple OSS Distributions copy = NULL;
153*27b03b36SApple OSS Distributions
154*27b03b36SApple OSS Distributions inst->_ranges.v64->address = address;
155*27b03b36SApple OSS Distributions inst->_ranges.v64->length = size;
156*27b03b36SApple OSS Distributions
157*27b03b36SApple OSS Distributions if (!inst->initWithPhysicalMask(inTask, options, size, page_size, 0)) {
158*27b03b36SApple OSS Distributions err = kIOReturnError;
159*27b03b36SApple OSS Distributions }
160*27b03b36SApple OSS Distributions } while (false);
161*27b03b36SApple OSS Distributions
162*27b03b36SApple OSS Distributions if (KERN_SUCCESS == err) {
163*27b03b36SApple OSS Distributions return inst;
164*27b03b36SApple OSS Distributions }
165*27b03b36SApple OSS Distributions
166*27b03b36SApple OSS Distributions if (copy) {
167*27b03b36SApple OSS Distributions vm_map_copy_discard(copy);
168*27b03b36SApple OSS Distributions }
169*27b03b36SApple OSS Distributions
170*27b03b36SApple OSS Distributions return nullptr;
171*27b03b36SApple OSS Distributions }
172*27b03b36SApple OSS Distributions
173*27b03b36SApple OSS Distributions
174*27b03b36SApple OSS Distributions bool
initWithPhysicalMask(task_t inTask,IOOptionBits options,mach_vm_size_t capacity,mach_vm_address_t alignment,mach_vm_address_t physicalMask)175*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::initWithPhysicalMask(
176*27b03b36SApple OSS Distributions task_t inTask,
177*27b03b36SApple OSS Distributions IOOptionBits options,
178*27b03b36SApple OSS Distributions mach_vm_size_t capacity,
179*27b03b36SApple OSS Distributions mach_vm_address_t alignment,
180*27b03b36SApple OSS Distributions mach_vm_address_t physicalMask)
181*27b03b36SApple OSS Distributions {
182*27b03b36SApple OSS Distributions task_t mapTask = NULL;
183*27b03b36SApple OSS Distributions kalloc_heap_t kheap = KHEAP_DATA_BUFFERS;
184*27b03b36SApple OSS Distributions mach_vm_address_t highestMask = 0;
185*27b03b36SApple OSS Distributions IOOptionBits iomdOptions = kIOMemoryTypeVirtual64 | kIOMemoryAsReference;
186*27b03b36SApple OSS Distributions IODMAMapSpecification mapSpec;
187*27b03b36SApple OSS Distributions bool mapped = false;
188*27b03b36SApple OSS Distributions bool withCopy = false;
189*27b03b36SApple OSS Distributions bool mappedOrShared = false;
190*27b03b36SApple OSS Distributions
191*27b03b36SApple OSS Distributions if (!capacity) {
192*27b03b36SApple OSS Distributions return false;
193*27b03b36SApple OSS Distributions }
194*27b03b36SApple OSS Distributions
195*27b03b36SApple OSS Distributions /*
196*27b03b36SApple OSS Distributions * The IOKit constructor requests the allocator for zeroed memory
197*27b03b36SApple OSS Distributions * so the members of the class do not need to be explicitly zeroed.
198*27b03b36SApple OSS Distributions */
199*27b03b36SApple OSS Distributions _options = options;
200*27b03b36SApple OSS Distributions _capacity = capacity;
201*27b03b36SApple OSS Distributions
202*27b03b36SApple OSS Distributions if (!_ranges.v64) {
203*27b03b36SApple OSS Distributions _ranges.v64 = IOMallocType(IOAddressRange);
204*27b03b36SApple OSS Distributions _ranges.v64->address = 0;
205*27b03b36SApple OSS Distributions _ranges.v64->length = 0;
206*27b03b36SApple OSS Distributions } else {
207*27b03b36SApple OSS Distributions if (!_ranges.v64->address) {
208*27b03b36SApple OSS Distributions return false;
209*27b03b36SApple OSS Distributions }
210*27b03b36SApple OSS Distributions if (!(kIOMemoryPageable & options)) {
211*27b03b36SApple OSS Distributions return false;
212*27b03b36SApple OSS Distributions }
213*27b03b36SApple OSS Distributions if (!inTask) {
214*27b03b36SApple OSS Distributions return false;
215*27b03b36SApple OSS Distributions }
216*27b03b36SApple OSS Distributions _buffer = (void *) _ranges.v64->address;
217*27b03b36SApple OSS Distributions withCopy = true;
218*27b03b36SApple OSS Distributions }
219*27b03b36SApple OSS Distributions
220*27b03b36SApple OSS Distributions /*
221*27b03b36SApple OSS Distributions * Set kalloc_heap to default if allocation contains pointers
222*27b03b36SApple OSS Distributions */
223*27b03b36SApple OSS Distributions if (kInternalFlagHasPointers & _internalFlags) {
224*27b03b36SApple OSS Distributions kheap = KHEAP_DEFAULT;
225*27b03b36SApple OSS Distributions }
226*27b03b36SApple OSS Distributions
227*27b03b36SApple OSS Distributions // make sure super::free doesn't dealloc _ranges before super::init
228*27b03b36SApple OSS Distributions _flags = kIOMemoryAsReference;
229*27b03b36SApple OSS Distributions
230*27b03b36SApple OSS Distributions // Grab IOMD bits from the Buffer MD options
231*27b03b36SApple OSS Distributions iomdOptions |= (options & kIOBufferDescriptorMemoryFlags);
232*27b03b36SApple OSS Distributions
233*27b03b36SApple OSS Distributions if (!(kIOMemoryMapperNone & options)) {
234*27b03b36SApple OSS Distributions IOMapper::checkForSystemMapper();
235*27b03b36SApple OSS Distributions mapped = (NULL != IOMapper::gSystem);
236*27b03b36SApple OSS Distributions }
237*27b03b36SApple OSS Distributions
238*27b03b36SApple OSS Distributions if (physicalMask && (alignment <= 1)) {
239*27b03b36SApple OSS Distributions alignment = ((physicalMask ^ (-1ULL)) & (physicalMask - 1));
240*27b03b36SApple OSS Distributions highestMask = (physicalMask | alignment);
241*27b03b36SApple OSS Distributions alignment++;
242*27b03b36SApple OSS Distributions if (alignment < page_size) {
243*27b03b36SApple OSS Distributions alignment = page_size;
244*27b03b36SApple OSS Distributions }
245*27b03b36SApple OSS Distributions }
246*27b03b36SApple OSS Distributions
247*27b03b36SApple OSS Distributions if ((options & (kIOMemorySharingTypeMask | kIOMapCacheMask | kIOMemoryClearEncrypt)) && (alignment < page_size)) {
248*27b03b36SApple OSS Distributions alignment = page_size;
249*27b03b36SApple OSS Distributions }
250*27b03b36SApple OSS Distributions
251*27b03b36SApple OSS Distributions if (alignment >= page_size) {
252*27b03b36SApple OSS Distributions if (round_page_overflow(capacity, &capacity)) {
253*27b03b36SApple OSS Distributions return false;
254*27b03b36SApple OSS Distributions }
255*27b03b36SApple OSS Distributions }
256*27b03b36SApple OSS Distributions
257*27b03b36SApple OSS Distributions if (alignment > page_size) {
258*27b03b36SApple OSS Distributions options |= kIOMemoryPhysicallyContiguous;
259*27b03b36SApple OSS Distributions }
260*27b03b36SApple OSS Distributions
261*27b03b36SApple OSS Distributions _alignment = alignment;
262*27b03b36SApple OSS Distributions
263*27b03b36SApple OSS Distributions if ((capacity + alignment) < _capacity) {
264*27b03b36SApple OSS Distributions return false;
265*27b03b36SApple OSS Distributions }
266*27b03b36SApple OSS Distributions
267*27b03b36SApple OSS Distributions if ((inTask != kernel_task) && !(options & kIOMemoryPageable)) {
268*27b03b36SApple OSS Distributions return false;
269*27b03b36SApple OSS Distributions }
270*27b03b36SApple OSS Distributions
271*27b03b36SApple OSS Distributions bzero(&mapSpec, sizeof(mapSpec));
272*27b03b36SApple OSS Distributions mapSpec.alignment = _alignment;
273*27b03b36SApple OSS Distributions mapSpec.numAddressBits = 64;
274*27b03b36SApple OSS Distributions if (highestMask && mapped) {
275*27b03b36SApple OSS Distributions if (highestMask <= 0xFFFFFFFF) {
276*27b03b36SApple OSS Distributions mapSpec.numAddressBits = (uint8_t)(32 - __builtin_clz((unsigned int) highestMask));
277*27b03b36SApple OSS Distributions } else {
278*27b03b36SApple OSS Distributions mapSpec.numAddressBits = (uint8_t)(64 - __builtin_clz((unsigned int) (highestMask >> 32)));
279*27b03b36SApple OSS Distributions }
280*27b03b36SApple OSS Distributions highestMask = 0;
281*27b03b36SApple OSS Distributions }
282*27b03b36SApple OSS Distributions
283*27b03b36SApple OSS Distributions // set memory entry cache mode, pageable, purgeable
284*27b03b36SApple OSS Distributions iomdOptions |= ((options & kIOMapCacheMask) >> kIOMapCacheShift) << kIOMemoryBufferCacheShift;
285*27b03b36SApple OSS Distributions if (options & kIOMemoryPageable) {
286*27b03b36SApple OSS Distributions if (_internalFlags & kInternalFlagGuardPages) {
287*27b03b36SApple OSS Distributions printf("IOBMD: Unsupported use of guard pages with pageable memory.\n");
288*27b03b36SApple OSS Distributions return false;
289*27b03b36SApple OSS Distributions }
290*27b03b36SApple OSS Distributions iomdOptions |= kIOMemoryBufferPageable;
291*27b03b36SApple OSS Distributions if (options & kIOMemoryPurgeable) {
292*27b03b36SApple OSS Distributions iomdOptions |= kIOMemoryBufferPurgeable;
293*27b03b36SApple OSS Distributions }
294*27b03b36SApple OSS Distributions } else {
295*27b03b36SApple OSS Distributions // Buffer shouldn't auto prepare they should be prepared explicitly
296*27b03b36SApple OSS Distributions // But it never was enforced so what are you going to do?
297*27b03b36SApple OSS Distributions iomdOptions |= kIOMemoryAutoPrepare;
298*27b03b36SApple OSS Distributions
299*27b03b36SApple OSS Distributions /* Allocate a wired-down buffer inside kernel space. */
300*27b03b36SApple OSS Distributions
301*27b03b36SApple OSS Distributions bool contig = (0 != (options & kIOMemoryHostPhysicallyContiguous));
302*27b03b36SApple OSS Distributions
303*27b03b36SApple OSS Distributions if (!contig && (0 != (options & kIOMemoryPhysicallyContiguous))) {
304*27b03b36SApple OSS Distributions contig |= (!mapped);
305*27b03b36SApple OSS Distributions contig |= (0 != (kIOMemoryMapperNone & options));
306*27b03b36SApple OSS Distributions #if 0
307*27b03b36SApple OSS Distributions // treat kIOMemoryPhysicallyContiguous as kIOMemoryHostPhysicallyContiguous for now
308*27b03b36SApple OSS Distributions contig |= true;
309*27b03b36SApple OSS Distributions #endif
310*27b03b36SApple OSS Distributions }
311*27b03b36SApple OSS Distributions
312*27b03b36SApple OSS Distributions mappedOrShared = (mapped || (0 != (kIOMemorySharingTypeMask & options)));
313*27b03b36SApple OSS Distributions if (contig || highestMask || (alignment > page_size)) {
314*27b03b36SApple OSS Distributions if (_internalFlags & kInternalFlagGuardPages) {
315*27b03b36SApple OSS Distributions printf("IOBMD: Unsupported use of guard pages with physical mask or contiguous memory.\n");
316*27b03b36SApple OSS Distributions return false;
317*27b03b36SApple OSS Distributions }
318*27b03b36SApple OSS Distributions _internalFlags |= kInternalFlagPhysical;
319*27b03b36SApple OSS Distributions if (highestMask) {
320*27b03b36SApple OSS Distributions _internalFlags |= kInternalFlagPageSized;
321*27b03b36SApple OSS Distributions if (round_page_overflow(capacity, &capacity)) {
322*27b03b36SApple OSS Distributions return false;
323*27b03b36SApple OSS Distributions }
324*27b03b36SApple OSS Distributions }
325*27b03b36SApple OSS Distributions _buffer = (void *) IOKernelAllocateWithPhysicalRestrict(kheap,
326*27b03b36SApple OSS Distributions capacity, highestMask, alignment, contig);
327*27b03b36SApple OSS Distributions } else if (_internalFlags & kInternalFlagGuardPages) {
328*27b03b36SApple OSS Distributions vm_offset_t address = 0;
329*27b03b36SApple OSS Distributions kern_return_t kr;
330*27b03b36SApple OSS Distributions uintptr_t alignMask;
331*27b03b36SApple OSS Distributions kma_flags_t kma_flags = (kma_flags_t) (KMA_GUARD_FIRST |
332*27b03b36SApple OSS Distributions KMA_GUARD_LAST | KMA_ZERO);
333*27b03b36SApple OSS Distributions
334*27b03b36SApple OSS Distributions if (((uint32_t) alignment) != alignment) {
335*27b03b36SApple OSS Distributions return NULL;
336*27b03b36SApple OSS Distributions }
337*27b03b36SApple OSS Distributions if (kheap == KHEAP_DATA_BUFFERS) {
338*27b03b36SApple OSS Distributions kma_flags = (kma_flags_t) (kma_flags | KMA_DATA);
339*27b03b36SApple OSS Distributions }
340*27b03b36SApple OSS Distributions
341*27b03b36SApple OSS Distributions alignMask = (1UL << log2up((uint32_t) alignment)) - 1;
342*27b03b36SApple OSS Distributions kr = kernel_memory_allocate(kernel_map, &address,
343*27b03b36SApple OSS Distributions capacity + page_size * 2, alignMask, kma_flags,
344*27b03b36SApple OSS Distributions IOMemoryTag(kernel_map));
345*27b03b36SApple OSS Distributions if (kr != KERN_SUCCESS || address == 0) {
346*27b03b36SApple OSS Distributions return false;
347*27b03b36SApple OSS Distributions }
348*27b03b36SApple OSS Distributions #if IOALLOCDEBUG
349*27b03b36SApple OSS Distributions OSAddAtomicLong(capacity, &debug_iomalloc_size);
350*27b03b36SApple OSS Distributions #endif
351*27b03b36SApple OSS Distributions IOStatisticsAlloc(kIOStatisticsMallocAligned, capacity);
352*27b03b36SApple OSS Distributions _buffer = (void *)(address + page_size);
353*27b03b36SApple OSS Distributions #if defined(__x86_64__)
354*27b03b36SApple OSS Distributions } else if (mappedOrShared
355*27b03b36SApple OSS Distributions && (capacity + alignment) <= (page_size - gIOPageAllocChunkBytes)) {
356*27b03b36SApple OSS Distributions _internalFlags |= kInternalFlagPageAllocated;
357*27b03b36SApple OSS Distributions _buffer = (void *) iopa_alloc(&gIOBMDPageAllocator,
358*27b03b36SApple OSS Distributions &IOBMDPageProc, kheap, capacity, alignment);
359*27b03b36SApple OSS Distributions if (_buffer) {
360*27b03b36SApple OSS Distributions bzero(_buffer, capacity);
361*27b03b36SApple OSS Distributions IOStatisticsAlloc(kIOStatisticsMallocAligned, capacity);
362*27b03b36SApple OSS Distributions #if IOALLOCDEBUG
363*27b03b36SApple OSS Distributions OSAddAtomicLong(capacity, &debug_iomalloc_size);
364*27b03b36SApple OSS Distributions #endif
365*27b03b36SApple OSS Distributions }
366*27b03b36SApple OSS Distributions #endif /* defined(__x86_64__) */
367*27b03b36SApple OSS Distributions } else if (alignment > 1) {
368*27b03b36SApple OSS Distributions _buffer = IOMallocAligned_internal(kheap, capacity, alignment,
369*27b03b36SApple OSS Distributions Z_ZERO_VM_TAG_BT_BIT);
370*27b03b36SApple OSS Distributions } else {
371*27b03b36SApple OSS Distributions _buffer = IOMalloc_internal(kheap, capacity, Z_ZERO_VM_TAG_BT_BIT);
372*27b03b36SApple OSS Distributions }
373*27b03b36SApple OSS Distributions if (!_buffer) {
374*27b03b36SApple OSS Distributions return false;
375*27b03b36SApple OSS Distributions }
376*27b03b36SApple OSS Distributions }
377*27b03b36SApple OSS Distributions
378*27b03b36SApple OSS Distributions if ((options & (kIOMemoryPageable | kIOMapCacheMask))) {
379*27b03b36SApple OSS Distributions vm_size_t size = round_page(capacity);
380*27b03b36SApple OSS Distributions
381*27b03b36SApple OSS Distributions // initWithOptions will create memory entry
382*27b03b36SApple OSS Distributions if (!withCopy) {
383*27b03b36SApple OSS Distributions iomdOptions |= kIOMemoryPersistent;
384*27b03b36SApple OSS Distributions }
385*27b03b36SApple OSS Distributions
386*27b03b36SApple OSS Distributions if (options & kIOMemoryPageable) {
387*27b03b36SApple OSS Distributions #if IOALLOCDEBUG
388*27b03b36SApple OSS Distributions OSAddAtomicLong(size, &debug_iomallocpageable_size);
389*27b03b36SApple OSS Distributions #endif
390*27b03b36SApple OSS Distributions if (!withCopy) {
391*27b03b36SApple OSS Distributions mapTask = inTask;
392*27b03b36SApple OSS Distributions }
393*27b03b36SApple OSS Distributions if (NULL == inTask) {
394*27b03b36SApple OSS Distributions inTask = kernel_task;
395*27b03b36SApple OSS Distributions }
396*27b03b36SApple OSS Distributions } else if (options & kIOMapCacheMask) {
397*27b03b36SApple OSS Distributions // Prefetch each page to put entries into the pmap
398*27b03b36SApple OSS Distributions volatile UInt8 * startAddr = (UInt8 *)_buffer;
399*27b03b36SApple OSS Distributions volatile UInt8 * endAddr = (UInt8 *)_buffer + capacity;
400*27b03b36SApple OSS Distributions
401*27b03b36SApple OSS Distributions while (startAddr < endAddr) {
402*27b03b36SApple OSS Distributions UInt8 dummyVar = *startAddr;
403*27b03b36SApple OSS Distributions (void) dummyVar;
404*27b03b36SApple OSS Distributions startAddr += page_size;
405*27b03b36SApple OSS Distributions }
406*27b03b36SApple OSS Distributions }
407*27b03b36SApple OSS Distributions }
408*27b03b36SApple OSS Distributions
409*27b03b36SApple OSS Distributions _ranges.v64->address = (mach_vm_address_t) pgz_decode(_buffer, _capacity);
410*27b03b36SApple OSS Distributions _ranges.v64->length = _capacity;
411*27b03b36SApple OSS Distributions
412*27b03b36SApple OSS Distributions if (!super::initWithOptions(_ranges.v64, 1, 0,
413*27b03b36SApple OSS Distributions inTask, iomdOptions, /* System mapper */ NULL)) {
414*27b03b36SApple OSS Distributions return false;
415*27b03b36SApple OSS Distributions }
416*27b03b36SApple OSS Distributions
417*27b03b36SApple OSS Distributions _internalFlags |= kInternalFlagInit;
418*27b03b36SApple OSS Distributions #if IOTRACKING
419*27b03b36SApple OSS Distributions if (!(options & kIOMemoryPageable)) {
420*27b03b36SApple OSS Distributions trackingAccumSize(capacity);
421*27b03b36SApple OSS Distributions }
422*27b03b36SApple OSS Distributions #endif /* IOTRACKING */
423*27b03b36SApple OSS Distributions
424*27b03b36SApple OSS Distributions // give any system mapper the allocation params
425*27b03b36SApple OSS Distributions if (kIOReturnSuccess != dmaCommandOperation(kIOMDAddDMAMapSpec,
426*27b03b36SApple OSS Distributions &mapSpec, sizeof(mapSpec))) {
427*27b03b36SApple OSS Distributions return false;
428*27b03b36SApple OSS Distributions }
429*27b03b36SApple OSS Distributions
430*27b03b36SApple OSS Distributions if (mapTask) {
431*27b03b36SApple OSS Distributions if (!reserved) {
432*27b03b36SApple OSS Distributions reserved = IOMallocType(ExpansionData);
433*27b03b36SApple OSS Distributions if (!reserved) {
434*27b03b36SApple OSS Distributions return false;
435*27b03b36SApple OSS Distributions }
436*27b03b36SApple OSS Distributions }
437*27b03b36SApple OSS Distributions reserved->map = createMappingInTask(mapTask, 0,
438*27b03b36SApple OSS Distributions kIOMapAnywhere | (options & kIOMapPrefault) | (options & kIOMapCacheMask), 0, 0).detach();
439*27b03b36SApple OSS Distributions if (!reserved->map) {
440*27b03b36SApple OSS Distributions _buffer = NULL;
441*27b03b36SApple OSS Distributions return false;
442*27b03b36SApple OSS Distributions }
443*27b03b36SApple OSS Distributions release(); // map took a retain on this
444*27b03b36SApple OSS Distributions reserved->map->retain();
445*27b03b36SApple OSS Distributions removeMapping(reserved->map);
446*27b03b36SApple OSS Distributions mach_vm_address_t buffer = reserved->map->getAddress();
447*27b03b36SApple OSS Distributions _buffer = (void *) buffer;
448*27b03b36SApple OSS Distributions if (kIOMemoryTypeVirtual64 == (kIOMemoryTypeMask & iomdOptions)) {
449*27b03b36SApple OSS Distributions _ranges.v64->address = buffer;
450*27b03b36SApple OSS Distributions }
451*27b03b36SApple OSS Distributions }
452*27b03b36SApple OSS Distributions
453*27b03b36SApple OSS Distributions setLength(_capacity);
454*27b03b36SApple OSS Distributions
455*27b03b36SApple OSS Distributions return true;
456*27b03b36SApple OSS Distributions }
457*27b03b36SApple OSS Distributions
458*27b03b36SApple OSS Distributions bool
initControlWithPhysicalMask(task_t inTask,IOOptionBits options,mach_vm_size_t capacity,mach_vm_address_t alignment,mach_vm_address_t physicalMask)459*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::initControlWithPhysicalMask(
460*27b03b36SApple OSS Distributions task_t inTask,
461*27b03b36SApple OSS Distributions IOOptionBits options,
462*27b03b36SApple OSS Distributions mach_vm_size_t capacity,
463*27b03b36SApple OSS Distributions mach_vm_address_t alignment,
464*27b03b36SApple OSS Distributions mach_vm_address_t physicalMask)
465*27b03b36SApple OSS Distributions {
466*27b03b36SApple OSS Distributions _internalFlags = kInternalFlagHasPointers;
467*27b03b36SApple OSS Distributions return initWithPhysicalMask(inTask, options, capacity, alignment,
468*27b03b36SApple OSS Distributions physicalMask);
469*27b03b36SApple OSS Distributions }
470*27b03b36SApple OSS Distributions
471*27b03b36SApple OSS Distributions bool
initWithGuardPages(task_t inTask,IOOptionBits options,mach_vm_size_t capacity)472*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::initWithGuardPages(
473*27b03b36SApple OSS Distributions task_t inTask,
474*27b03b36SApple OSS Distributions IOOptionBits options,
475*27b03b36SApple OSS Distributions mach_vm_size_t capacity)
476*27b03b36SApple OSS Distributions {
477*27b03b36SApple OSS Distributions mach_vm_size_t roundedCapacity;
478*27b03b36SApple OSS Distributions
479*27b03b36SApple OSS Distributions _internalFlags = kInternalFlagGuardPages;
480*27b03b36SApple OSS Distributions
481*27b03b36SApple OSS Distributions if (round_page_overflow(capacity, &roundedCapacity)) {
482*27b03b36SApple OSS Distributions return false;
483*27b03b36SApple OSS Distributions }
484*27b03b36SApple OSS Distributions
485*27b03b36SApple OSS Distributions return initWithPhysicalMask(inTask, options, roundedCapacity, page_size,
486*27b03b36SApple OSS Distributions (mach_vm_address_t)0);
487*27b03b36SApple OSS Distributions }
488*27b03b36SApple OSS Distributions
489*27b03b36SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
inTaskWithOptions(task_t inTask,IOOptionBits options,vm_size_t capacity,vm_offset_t alignment)490*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::inTaskWithOptions(
491*27b03b36SApple OSS Distributions task_t inTask,
492*27b03b36SApple OSS Distributions IOOptionBits options,
493*27b03b36SApple OSS Distributions vm_size_t capacity,
494*27b03b36SApple OSS Distributions vm_offset_t alignment)
495*27b03b36SApple OSS Distributions {
496*27b03b36SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
497*27b03b36SApple OSS Distributions
498*27b03b36SApple OSS Distributions if (me && !me->initWithPhysicalMask(inTask, options, capacity, alignment, 0)) {
499*27b03b36SApple OSS Distributions me.reset();
500*27b03b36SApple OSS Distributions }
501*27b03b36SApple OSS Distributions return me;
502*27b03b36SApple OSS Distributions }
503*27b03b36SApple OSS Distributions
504*27b03b36SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
inTaskWithOptions(task_t inTask,IOOptionBits options,vm_size_t capacity,vm_offset_t alignment,uint32_t kernTag,uint32_t userTag)505*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::inTaskWithOptions(
506*27b03b36SApple OSS Distributions task_t inTask,
507*27b03b36SApple OSS Distributions IOOptionBits options,
508*27b03b36SApple OSS Distributions vm_size_t capacity,
509*27b03b36SApple OSS Distributions vm_offset_t alignment,
510*27b03b36SApple OSS Distributions uint32_t kernTag,
511*27b03b36SApple OSS Distributions uint32_t userTag)
512*27b03b36SApple OSS Distributions {
513*27b03b36SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
514*27b03b36SApple OSS Distributions
515*27b03b36SApple OSS Distributions if (me) {
516*27b03b36SApple OSS Distributions me->setVMTags(kernTag, userTag);
517*27b03b36SApple OSS Distributions
518*27b03b36SApple OSS Distributions if (!me->initWithPhysicalMask(inTask, options, capacity, alignment, 0)) {
519*27b03b36SApple OSS Distributions me.reset();
520*27b03b36SApple OSS Distributions }
521*27b03b36SApple OSS Distributions }
522*27b03b36SApple OSS Distributions return me;
523*27b03b36SApple OSS Distributions }
524*27b03b36SApple OSS Distributions
525*27b03b36SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
inTaskWithPhysicalMask(task_t inTask,IOOptionBits options,mach_vm_size_t capacity,mach_vm_address_t physicalMask)526*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
527*27b03b36SApple OSS Distributions task_t inTask,
528*27b03b36SApple OSS Distributions IOOptionBits options,
529*27b03b36SApple OSS Distributions mach_vm_size_t capacity,
530*27b03b36SApple OSS Distributions mach_vm_address_t physicalMask)
531*27b03b36SApple OSS Distributions {
532*27b03b36SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
533*27b03b36SApple OSS Distributions
534*27b03b36SApple OSS Distributions if (me && !me->initWithPhysicalMask(inTask, options, capacity, 1, physicalMask)) {
535*27b03b36SApple OSS Distributions me.reset();
536*27b03b36SApple OSS Distributions }
537*27b03b36SApple OSS Distributions return me;
538*27b03b36SApple OSS Distributions }
539*27b03b36SApple OSS Distributions
540*27b03b36SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
inTaskWithGuardPages(task_t inTask,IOOptionBits options,mach_vm_size_t capacity)541*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::inTaskWithGuardPages(
542*27b03b36SApple OSS Distributions task_t inTask,
543*27b03b36SApple OSS Distributions IOOptionBits options,
544*27b03b36SApple OSS Distributions mach_vm_size_t capacity)
545*27b03b36SApple OSS Distributions {
546*27b03b36SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
547*27b03b36SApple OSS Distributions
548*27b03b36SApple OSS Distributions if (me && !me->initWithGuardPages(inTask, options, capacity)) {
549*27b03b36SApple OSS Distributions me.reset();
550*27b03b36SApple OSS Distributions }
551*27b03b36SApple OSS Distributions return me;
552*27b03b36SApple OSS Distributions }
553*27b03b36SApple OSS Distributions
554*27b03b36SApple OSS Distributions #ifndef __LP64__
555*27b03b36SApple OSS Distributions bool
initWithOptions(IOOptionBits options,vm_size_t capacity,vm_offset_t alignment)556*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::initWithOptions(
557*27b03b36SApple OSS Distributions IOOptionBits options,
558*27b03b36SApple OSS Distributions vm_size_t capacity,
559*27b03b36SApple OSS Distributions vm_offset_t alignment)
560*27b03b36SApple OSS Distributions {
561*27b03b36SApple OSS Distributions return initWithPhysicalMask(kernel_task, options, capacity, alignment, (mach_vm_address_t)0);
562*27b03b36SApple OSS Distributions }
563*27b03b36SApple OSS Distributions #endif /* !__LP64__ */
564*27b03b36SApple OSS Distributions
565*27b03b36SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
withOptions(IOOptionBits options,vm_size_t capacity,vm_offset_t alignment)566*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::withOptions(
567*27b03b36SApple OSS Distributions IOOptionBits options,
568*27b03b36SApple OSS Distributions vm_size_t capacity,
569*27b03b36SApple OSS Distributions vm_offset_t alignment)
570*27b03b36SApple OSS Distributions {
571*27b03b36SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
572*27b03b36SApple OSS Distributions
573*27b03b36SApple OSS Distributions if (me && !me->initWithPhysicalMask(kernel_task, options, capacity, alignment, 0)) {
574*27b03b36SApple OSS Distributions me.reset();
575*27b03b36SApple OSS Distributions }
576*27b03b36SApple OSS Distributions return me;
577*27b03b36SApple OSS Distributions }
578*27b03b36SApple OSS Distributions
579*27b03b36SApple OSS Distributions
580*27b03b36SApple OSS Distributions /*
581*27b03b36SApple OSS Distributions * withCapacity:
582*27b03b36SApple OSS Distributions *
583*27b03b36SApple OSS Distributions * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
584*27b03b36SApple OSS Distributions * hold capacity bytes. The descriptor's length is initially set to the capacity.
585*27b03b36SApple OSS Distributions */
586*27b03b36SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
withCapacity(vm_size_t inCapacity,IODirection inDirection,bool inContiguous)587*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::withCapacity(vm_size_t inCapacity,
588*27b03b36SApple OSS Distributions IODirection inDirection,
589*27b03b36SApple OSS Distributions bool inContiguous)
590*27b03b36SApple OSS Distributions {
591*27b03b36SApple OSS Distributions return IOBufferMemoryDescriptor::withOptions(
592*27b03b36SApple OSS Distributions inDirection | kIOMemoryUnshared
593*27b03b36SApple OSS Distributions | (inContiguous ? kIOMemoryPhysicallyContiguous : 0),
594*27b03b36SApple OSS Distributions inCapacity, inContiguous ? inCapacity : 1 );
595*27b03b36SApple OSS Distributions }
596*27b03b36SApple OSS Distributions
597*27b03b36SApple OSS Distributions #ifndef __LP64__
598*27b03b36SApple OSS Distributions /*
599*27b03b36SApple OSS Distributions * initWithBytes:
600*27b03b36SApple OSS Distributions *
601*27b03b36SApple OSS Distributions * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
602*27b03b36SApple OSS Distributions * The descriptor's length and capacity are set to the input buffer's size.
603*27b03b36SApple OSS Distributions */
604*27b03b36SApple OSS Distributions bool
initWithBytes(const void * inBytes,vm_size_t inLength,IODirection inDirection,bool inContiguous)605*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::initWithBytes(const void * inBytes,
606*27b03b36SApple OSS Distributions vm_size_t inLength,
607*27b03b36SApple OSS Distributions IODirection inDirection,
608*27b03b36SApple OSS Distributions bool inContiguous)
609*27b03b36SApple OSS Distributions {
610*27b03b36SApple OSS Distributions if (!initWithPhysicalMask(kernel_task, inDirection | kIOMemoryUnshared
611*27b03b36SApple OSS Distributions | (inContiguous ? kIOMemoryPhysicallyContiguous : 0),
612*27b03b36SApple OSS Distributions inLength, inLength, (mach_vm_address_t)0)) {
613*27b03b36SApple OSS Distributions return false;
614*27b03b36SApple OSS Distributions }
615*27b03b36SApple OSS Distributions
616*27b03b36SApple OSS Distributions // start out with no data
617*27b03b36SApple OSS Distributions setLength(0);
618*27b03b36SApple OSS Distributions
619*27b03b36SApple OSS Distributions if (!appendBytes(inBytes, inLength)) {
620*27b03b36SApple OSS Distributions return false;
621*27b03b36SApple OSS Distributions }
622*27b03b36SApple OSS Distributions
623*27b03b36SApple OSS Distributions return true;
624*27b03b36SApple OSS Distributions }
625*27b03b36SApple OSS Distributions #endif /* !__LP64__ */
626*27b03b36SApple OSS Distributions
627*27b03b36SApple OSS Distributions /*
628*27b03b36SApple OSS Distributions * withBytes:
629*27b03b36SApple OSS Distributions *
630*27b03b36SApple OSS Distributions * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
631*27b03b36SApple OSS Distributions * The descriptor's length and capacity are set to the input buffer's size.
632*27b03b36SApple OSS Distributions */
633*27b03b36SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
withBytes(const void * inBytes,vm_size_t inLength,IODirection inDirection,bool inContiguous)634*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::withBytes(const void * inBytes,
635*27b03b36SApple OSS Distributions vm_size_t inLength,
636*27b03b36SApple OSS Distributions IODirection inDirection,
637*27b03b36SApple OSS Distributions bool inContiguous)
638*27b03b36SApple OSS Distributions {
639*27b03b36SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
640*27b03b36SApple OSS Distributions
641*27b03b36SApple OSS Distributions if (me && !me->initWithPhysicalMask(
642*27b03b36SApple OSS Distributions kernel_task, inDirection | kIOMemoryUnshared
643*27b03b36SApple OSS Distributions | (inContiguous ? kIOMemoryPhysicallyContiguous : 0),
644*27b03b36SApple OSS Distributions inLength, inLength, 0 )) {
645*27b03b36SApple OSS Distributions me.reset();
646*27b03b36SApple OSS Distributions }
647*27b03b36SApple OSS Distributions
648*27b03b36SApple OSS Distributions if (me) {
649*27b03b36SApple OSS Distributions // start out with no data
650*27b03b36SApple OSS Distributions me->setLength(0);
651*27b03b36SApple OSS Distributions
652*27b03b36SApple OSS Distributions if (!me->appendBytes(inBytes, inLength)) {
653*27b03b36SApple OSS Distributions me.reset();
654*27b03b36SApple OSS Distributions }
655*27b03b36SApple OSS Distributions }
656*27b03b36SApple OSS Distributions return me;
657*27b03b36SApple OSS Distributions }
658*27b03b36SApple OSS Distributions
659*27b03b36SApple OSS Distributions /*
660*27b03b36SApple OSS Distributions * free:
661*27b03b36SApple OSS Distributions *
662*27b03b36SApple OSS Distributions * Free resources
663*27b03b36SApple OSS Distributions */
664*27b03b36SApple OSS Distributions void
free()665*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::free()
666*27b03b36SApple OSS Distributions {
667*27b03b36SApple OSS Distributions // Cache all of the relevant information on the stack for use
668*27b03b36SApple OSS Distributions // after we call super::free()!
669*27b03b36SApple OSS Distributions IOOptionBits flags = _flags;
670*27b03b36SApple OSS Distributions IOOptionBits internalFlags = _internalFlags;
671*27b03b36SApple OSS Distributions IOOptionBits options = _options;
672*27b03b36SApple OSS Distributions vm_size_t size = _capacity;
673*27b03b36SApple OSS Distributions void * buffer = _buffer;
674*27b03b36SApple OSS Distributions IOMemoryMap * map = NULL;
675*27b03b36SApple OSS Distributions IOAddressRange * range = _ranges.v64;
676*27b03b36SApple OSS Distributions vm_offset_t alignment = _alignment;
677*27b03b36SApple OSS Distributions kalloc_heap_t kheap = KHEAP_DATA_BUFFERS;
678*27b03b36SApple OSS Distributions
679*27b03b36SApple OSS Distributions if (alignment >= page_size) {
680*27b03b36SApple OSS Distributions size = round_page(size);
681*27b03b36SApple OSS Distributions }
682*27b03b36SApple OSS Distributions
683*27b03b36SApple OSS Distributions if (reserved) {
684*27b03b36SApple OSS Distributions map = reserved->map;
685*27b03b36SApple OSS Distributions IOFreeType(reserved, ExpansionData);
686*27b03b36SApple OSS Distributions if (map) {
687*27b03b36SApple OSS Distributions map->release();
688*27b03b36SApple OSS Distributions }
689*27b03b36SApple OSS Distributions }
690*27b03b36SApple OSS Distributions
691*27b03b36SApple OSS Distributions if ((options & kIOMemoryPageable)
692*27b03b36SApple OSS Distributions || (kInternalFlagPageSized & internalFlags)) {
693*27b03b36SApple OSS Distributions size = round_page(size);
694*27b03b36SApple OSS Distributions }
695*27b03b36SApple OSS Distributions
696*27b03b36SApple OSS Distributions if (internalFlags & kInternalFlagHasPointers) {
697*27b03b36SApple OSS Distributions kheap = KHEAP_DEFAULT;
698*27b03b36SApple OSS Distributions }
699*27b03b36SApple OSS Distributions
700*27b03b36SApple OSS Distributions #if IOTRACKING
701*27b03b36SApple OSS Distributions if (!(options & kIOMemoryPageable)
702*27b03b36SApple OSS Distributions && buffer
703*27b03b36SApple OSS Distributions && (kInternalFlagInit & _internalFlags)) {
704*27b03b36SApple OSS Distributions trackingAccumSize(-size);
705*27b03b36SApple OSS Distributions }
706*27b03b36SApple OSS Distributions #endif /* IOTRACKING */
707*27b03b36SApple OSS Distributions
708*27b03b36SApple OSS Distributions /* super::free may unwire - deallocate buffer afterwards */
709*27b03b36SApple OSS Distributions super::free();
710*27b03b36SApple OSS Distributions
711*27b03b36SApple OSS Distributions if (options & kIOMemoryPageable) {
712*27b03b36SApple OSS Distributions #if IOALLOCDEBUG
713*27b03b36SApple OSS Distributions OSAddAtomicLong(-size, &debug_iomallocpageable_size);
714*27b03b36SApple OSS Distributions #endif
715*27b03b36SApple OSS Distributions } else if (buffer) {
716*27b03b36SApple OSS Distributions if (kInternalFlagPhysical & internalFlags) {
717*27b03b36SApple OSS Distributions IOKernelFreePhysical(kheap, (mach_vm_address_t) buffer, size);
718*27b03b36SApple OSS Distributions } else if (kInternalFlagPageAllocated & internalFlags) {
719*27b03b36SApple OSS Distributions #if defined(__x86_64__)
720*27b03b36SApple OSS Distributions uintptr_t page;
721*27b03b36SApple OSS Distributions page = iopa_free(&gIOBMDPageAllocator, (uintptr_t) buffer, size);
722*27b03b36SApple OSS Distributions if (page) {
723*27b03b36SApple OSS Distributions kmem_free(kernel_map, page, page_size);
724*27b03b36SApple OSS Distributions }
725*27b03b36SApple OSS Distributions #if IOALLOCDEBUG
726*27b03b36SApple OSS Distributions OSAddAtomicLong(-size, &debug_iomalloc_size);
727*27b03b36SApple OSS Distributions #endif
728*27b03b36SApple OSS Distributions IOStatisticsAlloc(kIOStatisticsFreeAligned, size);
729*27b03b36SApple OSS Distributions #else /* !defined(__x86_64__) */
730*27b03b36SApple OSS Distributions /* should be unreachable */
731*27b03b36SApple OSS Distributions panic("Attempting to free IOBMD with page allocated flag");
732*27b03b36SApple OSS Distributions #endif /* defined(__x86_64__) */
733*27b03b36SApple OSS Distributions } else if (kInternalFlagGuardPages & internalFlags) {
734*27b03b36SApple OSS Distributions vm_offset_t allocation = (vm_offset_t)buffer - page_size;
735*27b03b36SApple OSS Distributions kmem_free(kernel_map, allocation, size + page_size * 2);
736*27b03b36SApple OSS Distributions #if IOALLOCDEBUG
737*27b03b36SApple OSS Distributions OSAddAtomicLong(-size, &debug_iomalloc_size);
738*27b03b36SApple OSS Distributions #endif
739*27b03b36SApple OSS Distributions IOStatisticsAlloc(kIOStatisticsFreeAligned, size);
740*27b03b36SApple OSS Distributions } else if (alignment > 1) {
741*27b03b36SApple OSS Distributions IOFreeAligned_internal(kheap, buffer, size);
742*27b03b36SApple OSS Distributions } else {
743*27b03b36SApple OSS Distributions IOFree_internal(kheap, buffer, size);
744*27b03b36SApple OSS Distributions }
745*27b03b36SApple OSS Distributions }
746*27b03b36SApple OSS Distributions if (range && (kIOMemoryAsReference & flags)) {
747*27b03b36SApple OSS Distributions IOFreeType(range, IOAddressRange);
748*27b03b36SApple OSS Distributions }
749*27b03b36SApple OSS Distributions }
750*27b03b36SApple OSS Distributions
751*27b03b36SApple OSS Distributions /*
752*27b03b36SApple OSS Distributions * getCapacity:
753*27b03b36SApple OSS Distributions *
754*27b03b36SApple OSS Distributions * Get the buffer capacity
755*27b03b36SApple OSS Distributions */
756*27b03b36SApple OSS Distributions vm_size_t
getCapacity() const757*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::getCapacity() const
758*27b03b36SApple OSS Distributions {
759*27b03b36SApple OSS Distributions return _capacity;
760*27b03b36SApple OSS Distributions }
761*27b03b36SApple OSS Distributions
762*27b03b36SApple OSS Distributions /*
763*27b03b36SApple OSS Distributions * setLength:
764*27b03b36SApple OSS Distributions *
765*27b03b36SApple OSS Distributions * Change the buffer length of the memory descriptor. When a new buffer
766*27b03b36SApple OSS Distributions * is created, the initial length of the buffer is set to be the same as
767*27b03b36SApple OSS Distributions * the capacity. The length can be adjusted via setLength for a shorter
768*27b03b36SApple OSS Distributions * transfer (there is no need to create more buffer descriptors when you
769*27b03b36SApple OSS Distributions * can reuse an existing one, even for different transfer sizes). Note
770*27b03b36SApple OSS Distributions * that the specified length must not exceed the capacity of the buffer.
771*27b03b36SApple OSS Distributions */
772*27b03b36SApple OSS Distributions void
setLength(vm_size_t length)773*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::setLength(vm_size_t length)
774*27b03b36SApple OSS Distributions {
775*27b03b36SApple OSS Distributions assert(length <= _capacity);
776*27b03b36SApple OSS Distributions if (length > _capacity) {
777*27b03b36SApple OSS Distributions return;
778*27b03b36SApple OSS Distributions }
779*27b03b36SApple OSS Distributions
780*27b03b36SApple OSS Distributions _length = length;
781*27b03b36SApple OSS Distributions _ranges.v64->length = length;
782*27b03b36SApple OSS Distributions }
783*27b03b36SApple OSS Distributions
784*27b03b36SApple OSS Distributions /*
785*27b03b36SApple OSS Distributions * setDirection:
786*27b03b36SApple OSS Distributions *
787*27b03b36SApple OSS Distributions * Change the direction of the transfer. This method allows one to redirect
788*27b03b36SApple OSS Distributions * the descriptor's transfer direction. This eliminates the need to destroy
789*27b03b36SApple OSS Distributions * and create new buffers when different transfer directions are needed.
790*27b03b36SApple OSS Distributions */
791*27b03b36SApple OSS Distributions void
setDirection(IODirection direction)792*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::setDirection(IODirection direction)
793*27b03b36SApple OSS Distributions {
794*27b03b36SApple OSS Distributions _flags = (_flags & ~kIOMemoryDirectionMask) | direction;
795*27b03b36SApple OSS Distributions #ifndef __LP64__
796*27b03b36SApple OSS Distributions _direction = (IODirection) (_flags & kIOMemoryDirectionMask);
797*27b03b36SApple OSS Distributions #endif /* !__LP64__ */
798*27b03b36SApple OSS Distributions }
799*27b03b36SApple OSS Distributions
800*27b03b36SApple OSS Distributions /*
801*27b03b36SApple OSS Distributions * appendBytes:
802*27b03b36SApple OSS Distributions *
803*27b03b36SApple OSS Distributions * Add some data to the end of the buffer. This method automatically
804*27b03b36SApple OSS Distributions * maintains the memory descriptor buffer length. Note that appendBytes
805*27b03b36SApple OSS Distributions * will not copy past the end of the memory descriptor's current capacity.
806*27b03b36SApple OSS Distributions */
807*27b03b36SApple OSS Distributions bool
appendBytes(const void * bytes,vm_size_t withLength)808*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::appendBytes(const void * bytes, vm_size_t withLength)
809*27b03b36SApple OSS Distributions {
810*27b03b36SApple OSS Distributions vm_size_t actualBytesToCopy = min(withLength, _capacity - _length);
811*27b03b36SApple OSS Distributions IOByteCount offset;
812*27b03b36SApple OSS Distributions
813*27b03b36SApple OSS Distributions assert(_length <= _capacity);
814*27b03b36SApple OSS Distributions
815*27b03b36SApple OSS Distributions offset = _length;
816*27b03b36SApple OSS Distributions _length += actualBytesToCopy;
817*27b03b36SApple OSS Distributions _ranges.v64->length += actualBytesToCopy;
818*27b03b36SApple OSS Distributions
819*27b03b36SApple OSS Distributions if (_task == kernel_task) {
820*27b03b36SApple OSS Distributions bcopy(/* from */ bytes, (void *)(_ranges.v64->address + offset),
821*27b03b36SApple OSS Distributions actualBytesToCopy);
822*27b03b36SApple OSS Distributions } else {
823*27b03b36SApple OSS Distributions writeBytes(offset, bytes, actualBytesToCopy);
824*27b03b36SApple OSS Distributions }
825*27b03b36SApple OSS Distributions
826*27b03b36SApple OSS Distributions return true;
827*27b03b36SApple OSS Distributions }
828*27b03b36SApple OSS Distributions
829*27b03b36SApple OSS Distributions /*
830*27b03b36SApple OSS Distributions * getBytesNoCopy:
831*27b03b36SApple OSS Distributions *
832*27b03b36SApple OSS Distributions * Return the virtual address of the beginning of the buffer
833*27b03b36SApple OSS Distributions */
834*27b03b36SApple OSS Distributions void *
getBytesNoCopy()835*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::getBytesNoCopy()
836*27b03b36SApple OSS Distributions {
837*27b03b36SApple OSS Distributions if (kIOMemoryTypePhysical64 == (_flags & kIOMemoryTypeMask)) {
838*27b03b36SApple OSS Distributions return _buffer;
839*27b03b36SApple OSS Distributions } else {
840*27b03b36SApple OSS Distributions return (void *)_ranges.v64->address;
841*27b03b36SApple OSS Distributions }
842*27b03b36SApple OSS Distributions }
843*27b03b36SApple OSS Distributions
844*27b03b36SApple OSS Distributions
845*27b03b36SApple OSS Distributions /*
846*27b03b36SApple OSS Distributions * getBytesNoCopy:
847*27b03b36SApple OSS Distributions *
848*27b03b36SApple OSS Distributions * Return the virtual address of an offset from the beginning of the buffer
849*27b03b36SApple OSS Distributions */
850*27b03b36SApple OSS Distributions void *
getBytesNoCopy(vm_size_t start,vm_size_t withLength)851*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::getBytesNoCopy(vm_size_t start, vm_size_t withLength)
852*27b03b36SApple OSS Distributions {
853*27b03b36SApple OSS Distributions IOVirtualAddress address;
854*27b03b36SApple OSS Distributions
855*27b03b36SApple OSS Distributions if ((start + withLength) < start) {
856*27b03b36SApple OSS Distributions return NULL;
857*27b03b36SApple OSS Distributions }
858*27b03b36SApple OSS Distributions
859*27b03b36SApple OSS Distributions if (kIOMemoryTypePhysical64 == (_flags & kIOMemoryTypeMask)) {
860*27b03b36SApple OSS Distributions address = (IOVirtualAddress) _buffer;
861*27b03b36SApple OSS Distributions } else {
862*27b03b36SApple OSS Distributions address = _ranges.v64->address;
863*27b03b36SApple OSS Distributions }
864*27b03b36SApple OSS Distributions
865*27b03b36SApple OSS Distributions if (start < _length && (start + withLength) <= _length) {
866*27b03b36SApple OSS Distributions return (void *)(address + start);
867*27b03b36SApple OSS Distributions }
868*27b03b36SApple OSS Distributions return NULL;
869*27b03b36SApple OSS Distributions }
870*27b03b36SApple OSS Distributions
871*27b03b36SApple OSS Distributions #ifndef __LP64__
872*27b03b36SApple OSS Distributions void *
getVirtualSegment(IOByteCount offset,IOByteCount * lengthOfSegment)873*27b03b36SApple OSS Distributions IOBufferMemoryDescriptor::getVirtualSegment(IOByteCount offset,
874*27b03b36SApple OSS Distributions IOByteCount * lengthOfSegment)
875*27b03b36SApple OSS Distributions {
876*27b03b36SApple OSS Distributions void * bytes = getBytesNoCopy(offset, 0);
877*27b03b36SApple OSS Distributions
878*27b03b36SApple OSS Distributions if (bytes && lengthOfSegment) {
879*27b03b36SApple OSS Distributions *lengthOfSegment = _length - offset;
880*27b03b36SApple OSS Distributions }
881*27b03b36SApple OSS Distributions
882*27b03b36SApple OSS Distributions return bytes;
883*27b03b36SApple OSS Distributions }
884*27b03b36SApple OSS Distributions #endif /* !__LP64__ */
885*27b03b36SApple OSS Distributions
886*27b03b36SApple OSS Distributions #ifdef __LP64__
887*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 0);
888*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 1);
889*27b03b36SApple OSS Distributions #else /* !__LP64__ */
890*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOBufferMemoryDescriptor, 0);
891*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOBufferMemoryDescriptor, 1);
892*27b03b36SApple OSS Distributions #endif /* !__LP64__ */
893*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 2);
894*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 3);
895*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 4);
896*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 5);
897*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 6);
898*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 7);
899*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 8);
900*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 9);
901*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 10);
902*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 11);
903*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 12);
904*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 13);
905*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 14);
906*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 15);
907