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