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