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