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