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