1c1dac77fSApple OSS Distributions /*
2c1dac77fSApple OSS Distributions * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3c1dac77fSApple OSS Distributions *
4e13b1fa5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5c1dac77fSApple OSS Distributions *
6e13b1fa5SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7e13b1fa5SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8e13b1fa5SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9e13b1fa5SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10e13b1fa5SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11e13b1fa5SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12e13b1fa5SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13e13b1fa5SApple OSS Distributions * terms of an Apple operating system software license agreement.
14c1dac77fSApple OSS Distributions *
15e13b1fa5SApple OSS Distributions * Please obtain a copy of the License at
16e13b1fa5SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17e13b1fa5SApple OSS Distributions *
18e13b1fa5SApple OSS Distributions * The Original Code and all software distributed under the License are
19e13b1fa5SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20c1dac77fSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21c1dac77fSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22e13b1fa5SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23e13b1fa5SApple OSS Distributions * Please see the License for the specific language governing rights and
24e13b1fa5SApple OSS Distributions * limitations under the License.
25c1dac77fSApple OSS Distributions *
26e13b1fa5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27c1dac77fSApple OSS Distributions */
28bb611c8fSApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
293ca3bd55SApple OSS Distributions
303ca3bd55SApple OSS Distributions #define _IOMEMORYDESCRIPTOR_INTERNAL_
313ca3bd55SApple OSS Distributions
32c1dac77fSApple OSS Distributions #include <IOKit/assert.h>
33c1dac77fSApple OSS Distributions #include <IOKit/system.h>
34c1dac77fSApple OSS Distributions
35c1dac77fSApple OSS Distributions #include <IOKit/IOLib.h>
36e13b1fa5SApple OSS Distributions #include <IOKit/IOMapper.h>
37c1dac77fSApple OSS Distributions #include <IOKit/IOBufferMemoryDescriptor.h>
383ca3bd55SApple OSS Distributions #include <libkern/OSDebug.h>
39186b8fceSApple OSS Distributions #include <mach/mach_vm.h>
40c1dac77fSApple OSS Distributions
41*8d741a5dSApple OSS Distributions #include <vm/vm_kern_xnu.h>
42*8d741a5dSApple OSS Distributions
4314e3d835SApple OSS Distributions #include "IOKitKernelInternal.h"
4414e3d835SApple OSS Distributions
45186b8fceSApple OSS Distributions #ifdef IOALLOCDEBUG
46186b8fceSApple OSS Distributions #include <libkern/c++/OSCPPDebug.h>
47186b8fceSApple OSS Distributions #endif
48186b8fceSApple OSS Distributions #include <IOKit/IOStatisticsPrivate.h>
49186b8fceSApple OSS Distributions
50186b8fceSApple OSS Distributions #if IOKITSTATS
51186b8fceSApple OSS Distributions #define IOStatisticsAlloc(type, size) \
52186b8fceSApple OSS Distributions do { \
53186b8fceSApple OSS Distributions IOStatistics::countAlloc(type, size); \
54186b8fceSApple OSS Distributions } while (0)
55186b8fceSApple OSS Distributions #else
56186b8fceSApple OSS Distributions #define IOStatisticsAlloc(type, size)
57186b8fceSApple OSS Distributions #endif /* IOKITSTATS */
58186b8fceSApple OSS Distributions
59186b8fceSApple OSS Distributions
60c1dac77fSApple OSS Distributions __BEGIN_DECLS
61c1dac77fSApple OSS Distributions void ipc_port_release_send(ipc_port_t port);
62fad439e7SApple OSS Distributions #include <vm/pmap.h>
63c1dac77fSApple OSS Distributions
641031c584SApple OSS Distributions KALLOC_HEAP_DEFINE(KHEAP_IOBMD_CONTROL, "IOBMD_control", KHEAP_ID_KT_VAR);
65368ad365SApple OSS Distributions __END_DECLS
66c1dac77fSApple OSS Distributions
67e13b1fa5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
68e13b1fa5SApple OSS Distributions
69a5e72196SApple OSS Distributions enum{
70855239e5SApple OSS Distributions kInternalFlagPhysical = 0x00000001,
71186b8fceSApple OSS Distributions kInternalFlagPageSized = 0x00000002,
7276e12aa3SApple OSS Distributions kInternalFlagPageAllocated = 0x00000004,
73e6231be0SApple OSS Distributions kInternalFlagInit = 0x00000008,
74e6231be0SApple OSS Distributions kInternalFlagHasPointers = 0x00000010,
75e6231be0SApple OSS Distributions kInternalFlagGuardPages = 0x00000020,
763ca3bd55SApple OSS Distributions };
773ca3bd55SApple OSS Distributions
78e13b1fa5SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
79e13b1fa5SApple OSS Distributions
80c1dac77fSApple OSS Distributions #define super IOGeneralMemoryDescriptor
81bb611c8fSApple OSS Distributions OSDefineMetaClassAndStructorsWithZone(IOBufferMemoryDescriptor,
82bb611c8fSApple OSS Distributions IOGeneralMemoryDescriptor, ZC_ZFREE_CLEARMEM);
83c1dac77fSApple OSS Distributions
843ca3bd55SApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
85c1dac77fSApple OSS Distributions
86e7776783SApple OSS Distributions #if defined(__x86_64__)
87a5e72196SApple OSS Distributions static uintptr_t
IOBMDPageProc(kalloc_heap_t kheap,iopa_t * a)88e6231be0SApple OSS Distributions IOBMDPageProc(kalloc_heap_t kheap, iopa_t * a)
89186b8fceSApple OSS Distributions {
90186b8fceSApple OSS Distributions kern_return_t kr;
91186b8fceSApple OSS Distributions vm_address_t vmaddr = 0;
925c2921b0SApple OSS Distributions kma_flags_t kma_flags = KMA_ZERO;
93186b8fceSApple OSS Distributions
945c2921b0SApple OSS Distributions if (kheap == KHEAP_DATA_BUFFERS) {
955c2921b0SApple OSS Distributions kma_flags = (kma_flags_t) (kma_flags | KMA_DATA);
965c2921b0SApple OSS Distributions }
975c2921b0SApple OSS Distributions kr = kmem_alloc(kernel_map, &vmaddr, page_size,
985c2921b0SApple OSS Distributions kma_flags, VM_KERN_MEMORY_IOKIT);
99186b8fceSApple OSS Distributions
100a5e72196SApple OSS Distributions if (KERN_SUCCESS != kr) {
101a5e72196SApple OSS Distributions vmaddr = 0;
102a5e72196SApple OSS Distributions }
103186b8fceSApple OSS Distributions
104a5e72196SApple OSS Distributions return (uintptr_t) vmaddr;
105186b8fceSApple OSS Distributions }
106e7776783SApple OSS Distributions #endif /* defined(__x86_64__) */
107186b8fceSApple OSS Distributions
108186b8fceSApple OSS Distributions /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
109186b8fceSApple OSS Distributions
1103ca3bd55SApple OSS Distributions #ifndef __LP64__
111a5e72196SApple OSS Distributions bool
initWithOptions(IOOptionBits options,vm_size_t capacity,vm_offset_t alignment,task_t inTask)112a5e72196SApple OSS Distributions IOBufferMemoryDescriptor::initWithOptions(
113c1dac77fSApple OSS Distributions IOOptionBits options,
114c1dac77fSApple OSS Distributions vm_size_t capacity,
115fad439e7SApple OSS Distributions vm_offset_t alignment,
116fad439e7SApple OSS Distributions task_t inTask)
117c1dac77fSApple OSS Distributions {
118e13b1fa5SApple OSS Distributions mach_vm_address_t physicalMask = 0;
119a5e72196SApple OSS Distributions return initWithPhysicalMask(inTask, options, capacity, alignment, physicalMask);
120e13b1fa5SApple OSS Distributions }
1213ca3bd55SApple OSS Distributions #endif /* !__LP64__ */
122e13b1fa5SApple OSS Distributions
123bb611c8fSApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
withCopy(task_t inTask,IOOptionBits options,vm_map_t sourceMap,mach_vm_address_t source,mach_vm_size_t size)124a5e72196SApple OSS Distributions IOBufferMemoryDescriptor::withCopy(
125a5e72196SApple OSS Distributions task_t inTask,
126a5e72196SApple OSS Distributions IOOptionBits options,
127a5e72196SApple OSS Distributions vm_map_t sourceMap,
128a5e72196SApple OSS Distributions mach_vm_address_t source,
129a5e72196SApple OSS Distributions mach_vm_size_t size)
130a5e72196SApple OSS Distributions {
131bb611c8fSApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor> inst;
132a5e72196SApple OSS Distributions kern_return_t err;
133a5e72196SApple OSS Distributions vm_map_copy_t copy;
134a5e72196SApple OSS Distributions vm_map_address_t address;
135a5e72196SApple OSS Distributions
136a5e72196SApple OSS Distributions copy = NULL;
137a5e72196SApple OSS Distributions do {
138a5e72196SApple OSS Distributions err = kIOReturnNoMemory;
139bb611c8fSApple OSS Distributions inst = OSMakeShared<IOBufferMemoryDescriptor>();
140a5e72196SApple OSS Distributions if (!inst) {
141a5e72196SApple OSS Distributions break;
142a5e72196SApple OSS Distributions }
143e6231be0SApple OSS Distributions inst->_ranges.v64 = IOMallocType(IOAddressRange);
144a5e72196SApple OSS Distributions
145a5e72196SApple OSS Distributions err = vm_map_copyin(sourceMap, source, size,
146a5e72196SApple OSS Distributions false /* src_destroy */, ©);
147a5e72196SApple OSS Distributions if (KERN_SUCCESS != err) {
148a5e72196SApple OSS Distributions break;
149a5e72196SApple OSS Distributions }
150a5e72196SApple OSS Distributions
151a5e72196SApple OSS Distributions err = vm_map_copyout(get_task_map(inTask), &address, copy);
152a5e72196SApple OSS Distributions if (KERN_SUCCESS != err) {
153a5e72196SApple OSS Distributions break;
154a5e72196SApple OSS Distributions }
155a5e72196SApple OSS Distributions copy = NULL;
156a5e72196SApple OSS Distributions
157a5e72196SApple OSS Distributions inst->_ranges.v64->address = address;
158a5e72196SApple OSS Distributions inst->_ranges.v64->length = size;
159a5e72196SApple OSS Distributions
160a5e72196SApple OSS Distributions if (!inst->initWithPhysicalMask(inTask, options, size, page_size, 0)) {
161a5e72196SApple OSS Distributions err = kIOReturnError;
162a5e72196SApple OSS Distributions }
163a5e72196SApple OSS Distributions } while (false);
164a5e72196SApple OSS Distributions
165a5e72196SApple OSS Distributions if (KERN_SUCCESS == err) {
166a5e72196SApple OSS Distributions return inst;
167a5e72196SApple OSS Distributions }
168a5e72196SApple OSS Distributions
169a5e72196SApple OSS Distributions if (copy) {
170a5e72196SApple OSS Distributions vm_map_copy_discard(copy);
171a5e72196SApple OSS Distributions }
172bb611c8fSApple OSS Distributions
173bb611c8fSApple OSS Distributions return nullptr;
174a5e72196SApple OSS Distributions }
175a5e72196SApple OSS Distributions
176a5e72196SApple OSS Distributions
177a5e72196SApple OSS Distributions bool
initWithPhysicalMask(task_t inTask,IOOptionBits options,mach_vm_size_t capacity,mach_vm_address_t alignment,mach_vm_address_t physicalMask)178a5e72196SApple OSS Distributions IOBufferMemoryDescriptor::initWithPhysicalMask(
179e13b1fa5SApple OSS Distributions task_t inTask,
180e13b1fa5SApple OSS Distributions IOOptionBits options,
181e13b1fa5SApple OSS Distributions mach_vm_size_t capacity,
182e13b1fa5SApple OSS Distributions mach_vm_address_t alignment,
183e13b1fa5SApple OSS Distributions mach_vm_address_t physicalMask)
184e13b1fa5SApple OSS Distributions {
185e13b1fa5SApple OSS Distributions task_t mapTask = NULL;
186e6231be0SApple OSS Distributions kalloc_heap_t kheap = KHEAP_DATA_BUFFERS;
1873ca3bd55SApple OSS Distributions mach_vm_address_t highestMask = 0;
1883ca3bd55SApple OSS Distributions IOOptionBits iomdOptions = kIOMemoryTypeVirtual64 | kIOMemoryAsReference;
189186b8fceSApple OSS Distributions IODMAMapSpecification mapSpec;
190186b8fceSApple OSS Distributions bool mapped = false;
191a5e72196SApple OSS Distributions bool withCopy = false;
192bb611c8fSApple OSS Distributions bool mappedOrShared = false;
193fad439e7SApple OSS Distributions
194a5e72196SApple OSS Distributions if (!capacity) {
195a5e72196SApple OSS Distributions return false;
196a5e72196SApple OSS Distributions }
197c1dac77fSApple OSS Distributions
198e6231be0SApple OSS Distributions /*
199e6231be0SApple OSS Distributions * The IOKit constructor requests the allocator for zeroed memory
200e6231be0SApple OSS Distributions * so the members of the class do not need to be explicitly zeroed.
201e6231be0SApple OSS Distributions */
202c1dac77fSApple OSS Distributions _options = options;
203c1dac77fSApple OSS Distributions _capacity = capacity;
204c1dac77fSApple OSS Distributions
205a5e72196SApple OSS Distributions if (!_ranges.v64) {
206e6231be0SApple OSS Distributions _ranges.v64 = IOMallocType(IOAddressRange);
2073ca3bd55SApple OSS Distributions _ranges.v64->address = 0;
2083ca3bd55SApple OSS Distributions _ranges.v64->length = 0;
209a5e72196SApple OSS Distributions } else {
210a5e72196SApple OSS Distributions if (!_ranges.v64->address) {
211a5e72196SApple OSS Distributions return false;
212a5e72196SApple OSS Distributions }
213a5e72196SApple OSS Distributions if (!(kIOMemoryPageable & options)) {
214a5e72196SApple OSS Distributions return false;
215a5e72196SApple OSS Distributions }
216a5e72196SApple OSS Distributions if (!inTask) {
217a5e72196SApple OSS Distributions return false;
218a5e72196SApple OSS Distributions }
219a5e72196SApple OSS Distributions _buffer = (void *) _ranges.v64->address;
220a5e72196SApple OSS Distributions withCopy = true;
221a5e72196SApple OSS Distributions }
222e6231be0SApple OSS Distributions
223e6231be0SApple OSS Distributions /*
2241031c584SApple OSS Distributions * Set kalloc_heap to KHEAP_IOBMD_CONTROL if allocation contains pointers
225e6231be0SApple OSS Distributions */
226e6231be0SApple OSS Distributions if (kInternalFlagHasPointers & _internalFlags) {
2271031c584SApple OSS Distributions kheap = KHEAP_IOBMD_CONTROL;
228e6231be0SApple OSS Distributions }
229e6231be0SApple OSS Distributions
230d0c1fef6SApple OSS Distributions // make sure super::free doesn't dealloc _ranges before super::init
231d0c1fef6SApple OSS Distributions _flags = kIOMemoryAsReference;
2323ca3bd55SApple OSS Distributions
2333ca3bd55SApple OSS Distributions // Grab IOMD bits from the Buffer MD options
2343ca3bd55SApple OSS Distributions iomdOptions |= (options & kIOBufferDescriptorMemoryFlags);
2353ca3bd55SApple OSS Distributions
236a5e72196SApple OSS Distributions if (!(kIOMemoryMapperNone & options)) {
237186b8fceSApple OSS Distributions IOMapper::checkForSystemMapper();
238a5e72196SApple OSS Distributions mapped = (NULL != IOMapper::gSystem);
239186b8fceSApple OSS Distributions }
240186b8fceSApple OSS Distributions
241a5e72196SApple OSS Distributions if (physicalMask && (alignment <= 1)) {
2423ca3bd55SApple OSS Distributions alignment = ((physicalMask ^ (-1ULL)) & (physicalMask - 1));
2433ca3bd55SApple OSS Distributions highestMask = (physicalMask | alignment);
2443ca3bd55SApple OSS Distributions alignment++;
245a5e72196SApple OSS Distributions if (alignment < page_size) {
246a5e72196SApple OSS Distributions alignment = page_size;
247a5e72196SApple OSS Distributions }
248a5e72196SApple OSS Distributions }
249a5e72196SApple OSS Distributions
250a5e72196SApple OSS Distributions if ((options & (kIOMemorySharingTypeMask | kIOMapCacheMask | kIOMemoryClearEncrypt)) && (alignment < page_size)) {
251855239e5SApple OSS Distributions alignment = page_size;
2523ca3bd55SApple OSS Distributions }
253368ad365SApple OSS Distributions
254a5e72196SApple OSS Distributions if (alignment >= page_size) {
255bb611c8fSApple OSS Distributions if (round_page_overflow(capacity, &capacity)) {
256bb611c8fSApple OSS Distributions return false;
257bb611c8fSApple OSS Distributions }
258a5e72196SApple OSS Distributions }
2593ca3bd55SApple OSS Distributions
260a5e72196SApple OSS Distributions if (alignment > page_size) {
2613ca3bd55SApple OSS Distributions options |= kIOMemoryPhysicallyContiguous;
262a5e72196SApple OSS Distributions }
263fad439e7SApple OSS Distributions
264c1dac77fSApple OSS Distributions _alignment = alignment;
265fad439e7SApple OSS Distributions
266a5e72196SApple OSS Distributions if ((capacity + alignment) < _capacity) {
267e13b1fa5SApple OSS Distributions return false;
268a5e72196SApple OSS Distributions }
269a5e72196SApple OSS Distributions
270a5e72196SApple OSS Distributions if ((inTask != kernel_task) && !(options & kIOMemoryPageable)) {
271a5e72196SApple OSS Distributions return false;
272a5e72196SApple OSS Distributions }
273c1dac77fSApple OSS Distributions
274186b8fceSApple OSS Distributions bzero(&mapSpec, sizeof(mapSpec));
275186b8fceSApple OSS Distributions mapSpec.alignment = _alignment;
276186b8fceSApple OSS Distributions mapSpec.numAddressBits = 64;
277a5e72196SApple OSS Distributions if (highestMask && mapped) {
278a5e72196SApple OSS Distributions if (highestMask <= 0xFFFFFFFF) {
279bb611c8fSApple OSS Distributions mapSpec.numAddressBits = (uint8_t)(32 - __builtin_clz((unsigned int) highestMask));
280a5e72196SApple OSS Distributions } else {
281bb611c8fSApple OSS Distributions mapSpec.numAddressBits = (uint8_t)(64 - __builtin_clz((unsigned int) (highestMask >> 32)));
282a5e72196SApple OSS Distributions }
283186b8fceSApple OSS Distributions highestMask = 0;
284186b8fceSApple OSS Distributions }
285186b8fceSApple OSS Distributions
286a3bb9fccSApple OSS Distributions // set memory entry cache mode, pageable, purgeable
287a3bb9fccSApple OSS Distributions iomdOptions |= ((options & kIOMapCacheMask) >> kIOMapCacheShift) << kIOMemoryBufferCacheShift;
288a5e72196SApple OSS Distributions if (options & kIOMemoryPageable) {
289e6231be0SApple OSS Distributions if (_internalFlags & kInternalFlagGuardPages) {
290e6231be0SApple OSS Distributions printf("IOBMD: Unsupported use of guard pages with pageable memory.\n");
291e6231be0SApple OSS Distributions return false;
292e6231be0SApple OSS Distributions }
293e13b1fa5SApple OSS Distributions iomdOptions |= kIOMemoryBufferPageable;
294a5e72196SApple OSS Distributions if (options & kIOMemoryPurgeable) {
295a5e72196SApple OSS Distributions iomdOptions |= kIOMemoryBufferPurgeable;
296e13b1fa5SApple OSS Distributions }
297a5e72196SApple OSS Distributions } else {
298e13b1fa5SApple OSS Distributions // Buffer shouldn't auto prepare they should be prepared explicitly
299e13b1fa5SApple OSS Distributions // But it never was enforced so what are you going to do?
300e13b1fa5SApple OSS Distributions iomdOptions |= kIOMemoryAutoPrepare;
301e13b1fa5SApple OSS Distributions
302e13b1fa5SApple OSS Distributions /* Allocate a wired-down buffer inside kernel space. */
303855239e5SApple OSS Distributions
304186b8fceSApple OSS Distributions bool contig = (0 != (options & kIOMemoryHostPhysicallyContiguous));
305186b8fceSApple OSS Distributions
306a5e72196SApple OSS Distributions if (!contig && (0 != (options & kIOMemoryPhysicallyContiguous))) {
307186b8fceSApple OSS Distributions contig |= (!mapped);
308186b8fceSApple OSS Distributions contig |= (0 != (kIOMemoryMapperNone & options));
309186b8fceSApple OSS Distributions #if 0
310186b8fceSApple OSS Distributions // treat kIOMemoryPhysicallyContiguous as kIOMemoryHostPhysicallyContiguous for now
311186b8fceSApple OSS Distributions contig |= true;
312186b8fceSApple OSS Distributions #endif
313186b8fceSApple OSS Distributions }
314186b8fceSApple OSS Distributions
315bb611c8fSApple OSS Distributions mappedOrShared = (mapped || (0 != (kIOMemorySharingTypeMask & options)));
316a5e72196SApple OSS Distributions if (contig || highestMask || (alignment > page_size)) {
317e6231be0SApple OSS Distributions if (_internalFlags & kInternalFlagGuardPages) {
318e6231be0SApple OSS Distributions printf("IOBMD: Unsupported use of guard pages with physical mask or contiguous memory.\n");
319e6231be0SApple OSS Distributions return false;
320e6231be0SApple OSS Distributions }
321855239e5SApple OSS Distributions _internalFlags |= kInternalFlagPhysical;
322a5e72196SApple OSS Distributions if (highestMask) {
323855239e5SApple OSS Distributions _internalFlags |= kInternalFlagPageSized;
324bb611c8fSApple OSS Distributions if (round_page_overflow(capacity, &capacity)) {
325bb611c8fSApple OSS Distributions return false;
326bb611c8fSApple OSS Distributions }
327855239e5SApple OSS Distributions }
328e6231be0SApple OSS Distributions _buffer = (void *) IOKernelAllocateWithPhysicalRestrict(kheap,
329186b8fceSApple OSS Distributions capacity, highestMask, alignment, contig);
330e6231be0SApple OSS Distributions } else if (_internalFlags & kInternalFlagGuardPages) {
331e6231be0SApple OSS Distributions vm_offset_t address = 0;
332e6231be0SApple OSS Distributions kern_return_t kr;
333e6231be0SApple OSS Distributions uintptr_t alignMask;
3345c2921b0SApple OSS Distributions kma_flags_t kma_flags = (kma_flags_t) (KMA_GUARD_FIRST |
3355c2921b0SApple OSS Distributions KMA_GUARD_LAST | KMA_ZERO);
336e6231be0SApple OSS Distributions
337e6231be0SApple OSS Distributions if (((uint32_t) alignment) != alignment) {
3385c2921b0SApple OSS Distributions return false;
3395c2921b0SApple OSS Distributions }
3405c2921b0SApple OSS Distributions if (kheap == KHEAP_DATA_BUFFERS) {
3415c2921b0SApple OSS Distributions kma_flags = (kma_flags_t) (kma_flags | KMA_DATA);
342e6231be0SApple OSS Distributions }
343e6231be0SApple OSS Distributions
344e6231be0SApple OSS Distributions alignMask = (1UL << log2up((uint32_t) alignment)) - 1;
3455c2921b0SApple OSS Distributions kr = kernel_memory_allocate(kernel_map, &address,
3465c2921b0SApple OSS Distributions capacity + page_size * 2, alignMask, kma_flags,
3475c2921b0SApple OSS Distributions IOMemoryTag(kernel_map));
348e6231be0SApple OSS Distributions if (kr != KERN_SUCCESS || address == 0) {
349e6231be0SApple OSS Distributions return false;
350e6231be0SApple OSS Distributions }
351e6231be0SApple OSS Distributions #if IOALLOCDEBUG
352e6231be0SApple OSS Distributions OSAddAtomicLong(capacity, &debug_iomalloc_size);
353e6231be0SApple OSS Distributions #endif
354e6231be0SApple OSS Distributions IOStatisticsAlloc(kIOStatisticsMallocAligned, capacity);
355e6231be0SApple OSS Distributions _buffer = (void *)(address + page_size);
356e7776783SApple OSS Distributions #if defined(__x86_64__)
357bb611c8fSApple OSS Distributions } else if (mappedOrShared
358bb611c8fSApple OSS Distributions && (capacity + alignment) <= (page_size - gIOPageAllocChunkBytes)) {
359186b8fceSApple OSS Distributions _internalFlags |= kInternalFlagPageAllocated;
360e6231be0SApple OSS Distributions _buffer = (void *) iopa_alloc(&gIOBMDPageAllocator,
361e6231be0SApple OSS Distributions &IOBMDPageProc, kheap, capacity, alignment);
362a5e72196SApple OSS Distributions if (_buffer) {
3635c2921b0SApple OSS Distributions bzero(_buffer, capacity);
364186b8fceSApple OSS Distributions IOStatisticsAlloc(kIOStatisticsMallocAligned, capacity);
365186b8fceSApple OSS Distributions #if IOALLOCDEBUG
366bb611c8fSApple OSS Distributions OSAddAtomicLong(capacity, &debug_iomalloc_size);
367186b8fceSApple OSS Distributions #endif
368186b8fceSApple OSS Distributions }
369e7776783SApple OSS Distributions #endif /* defined(__x86_64__) */
370a5e72196SApple OSS Distributions } else if (alignment > 1) {
3715c2921b0SApple OSS Distributions /* BEGIN IGNORE CODESTYLE */
3725c2921b0SApple OSS Distributions __typed_allocators_ignore_push
3735c2921b0SApple OSS Distributions _buffer = IOMallocAligned_internal(kheap, capacity, alignment,
3745c2921b0SApple OSS Distributions Z_ZERO_VM_TAG_BT_BIT);
375a5e72196SApple OSS Distributions } else {
3765c2921b0SApple OSS Distributions _buffer = IOMalloc_internal(kheap, capacity, Z_ZERO_VM_TAG_BT_BIT);
3775c2921b0SApple OSS Distributions __typed_allocators_ignore_pop
3785c2921b0SApple OSS Distributions /* END IGNORE CODESTYLE */
3793ca3bd55SApple OSS Distributions }
380a5e72196SApple OSS Distributions if (!_buffer) {
381e13b1fa5SApple OSS Distributions return false;
382e13b1fa5SApple OSS Distributions }
383a5e72196SApple OSS Distributions }
384e13b1fa5SApple OSS Distributions
385855239e5SApple OSS Distributions if ((options & (kIOMemoryPageable | kIOMapCacheMask))) {
3863ca3bd55SApple OSS Distributions vm_size_t size = round_page(capacity);
387e13b1fa5SApple OSS Distributions
388a3bb9fccSApple OSS Distributions // initWithOptions will create memory entry
389a5e72196SApple OSS Distributions if (!withCopy) {
390a3bb9fccSApple OSS Distributions iomdOptions |= kIOMemoryPersistent;
391a5e72196SApple OSS Distributions }
392e13b1fa5SApple OSS Distributions
393e13b1fa5SApple OSS Distributions if (options & kIOMemoryPageable) {
39414e3d835SApple OSS Distributions #if IOALLOCDEBUG
3950f3703acSApple OSS Distributions OSAddAtomicLong(size, &debug_iomallocpageable_size);
39614e3d835SApple OSS Distributions #endif
397a5e72196SApple OSS Distributions if (!withCopy) {
398e13b1fa5SApple OSS Distributions mapTask = inTask;
399a5e72196SApple OSS Distributions }
400a5e72196SApple OSS Distributions if (NULL == inTask) {
40114e3d835SApple OSS Distributions inTask = kernel_task;
402e13b1fa5SApple OSS Distributions }
403a5e72196SApple OSS Distributions } else if (options & kIOMapCacheMask) {
404e13b1fa5SApple OSS Distributions // Prefetch each page to put entries into the pmap
405e13b1fa5SApple OSS Distributions volatile UInt8 * startAddr = (UInt8 *)_buffer;
406e13b1fa5SApple OSS Distributions volatile UInt8 * endAddr = (UInt8 *)_buffer + capacity;
407e13b1fa5SApple OSS Distributions
408a5e72196SApple OSS Distributions while (startAddr < endAddr) {
409186b8fceSApple OSS Distributions UInt8 dummyVar = *startAddr;
410186b8fceSApple OSS Distributions (void) dummyVar;
411e13b1fa5SApple OSS Distributions startAddr += page_size;
412e13b1fa5SApple OSS Distributions }
413e13b1fa5SApple OSS Distributions }
414e13b1fa5SApple OSS Distributions }
415e13b1fa5SApple OSS Distributions
416e7776783SApple OSS Distributions _ranges.v64->address = (mach_vm_address_t) pgz_decode(_buffer, _capacity);
4173ca3bd55SApple OSS Distributions _ranges.v64->length = _capacity;
418e13b1fa5SApple OSS Distributions
4193ca3bd55SApple OSS Distributions if (!super::initWithOptions(_ranges.v64, 1, 0,
420a5e72196SApple OSS Distributions inTask, iomdOptions, /* System mapper */ NULL)) {
421e13b1fa5SApple OSS Distributions return false;
422a5e72196SApple OSS Distributions }
423e13b1fa5SApple OSS Distributions
42476e12aa3SApple OSS Distributions _internalFlags |= kInternalFlagInit;
42576e12aa3SApple OSS Distributions #if IOTRACKING
426a5e72196SApple OSS Distributions if (!(options & kIOMemoryPageable)) {
427a5e72196SApple OSS Distributions trackingAccumSize(capacity);
428a5e72196SApple OSS Distributions }
42976e12aa3SApple OSS Distributions #endif /* IOTRACKING */
43076e12aa3SApple OSS Distributions
431186b8fceSApple OSS Distributions // give any system mapper the allocation params
432186b8fceSApple OSS Distributions if (kIOReturnSuccess != dmaCommandOperation(kIOMDAddDMAMapSpec,
433a5e72196SApple OSS Distributions &mapSpec, sizeof(mapSpec))) {
434186b8fceSApple OSS Distributions return false;
435a5e72196SApple OSS Distributions }
436186b8fceSApple OSS Distributions
437a5e72196SApple OSS Distributions if (mapTask) {
43814e3d835SApple OSS Distributions if (!reserved) {
439e6231be0SApple OSS Distributions reserved = IOMallocType(ExpansionData);
440a5e72196SApple OSS Distributions if (!reserved) {
441a5e72196SApple OSS Distributions return false;
442a5e72196SApple OSS Distributions }
44314e3d835SApple OSS Distributions }
4443ca3bd55SApple OSS Distributions reserved->map = createMappingInTask(mapTask, 0,
445bb611c8fSApple OSS Distributions kIOMapAnywhere | (options & kIOMapPrefault) | (options & kIOMapCacheMask), 0, 0).detach();
446a5e72196SApple OSS Distributions if (!reserved->map) {
447a5e72196SApple OSS Distributions _buffer = NULL;
448a5e72196SApple OSS Distributions return false;
44914e3d835SApple OSS Distributions }
450e13b1fa5SApple OSS Distributions release(); // map took a retain on this
4513ca3bd55SApple OSS Distributions reserved->map->retain();
4523ca3bd55SApple OSS Distributions removeMapping(reserved->map);
453e13b1fa5SApple OSS Distributions mach_vm_address_t buffer = reserved->map->getAddress();
454e13b1fa5SApple OSS Distributions _buffer = (void *) buffer;
455a5e72196SApple OSS Distributions if (kIOMemoryTypeVirtual64 == (kIOMemoryTypeMask & iomdOptions)) {
456e13b1fa5SApple OSS Distributions _ranges.v64->address = buffer;
457fad439e7SApple OSS Distributions }
458a5e72196SApple OSS Distributions }
459c1dac77fSApple OSS Distributions
4603ca3bd55SApple OSS Distributions setLength(_capacity);
461c1dac77fSApple OSS Distributions
462c1dac77fSApple OSS Distributions return true;
463c1dac77fSApple OSS Distributions }
464c1dac77fSApple OSS Distributions
465e6231be0SApple OSS Distributions bool
initControlWithPhysicalMask(task_t inTask,IOOptionBits options,mach_vm_size_t capacity,mach_vm_address_t alignment,mach_vm_address_t physicalMask)466e6231be0SApple OSS Distributions IOBufferMemoryDescriptor::initControlWithPhysicalMask(
467e6231be0SApple OSS Distributions task_t inTask,
468e6231be0SApple OSS Distributions IOOptionBits options,
469e6231be0SApple OSS Distributions mach_vm_size_t capacity,
470e6231be0SApple OSS Distributions mach_vm_address_t alignment,
471e6231be0SApple OSS Distributions mach_vm_address_t physicalMask)
472e6231be0SApple OSS Distributions {
473e6231be0SApple OSS Distributions _internalFlags = kInternalFlagHasPointers;
474e6231be0SApple OSS Distributions return initWithPhysicalMask(inTask, options, capacity, alignment,
475e6231be0SApple OSS Distributions physicalMask);
476e6231be0SApple OSS Distributions }
477e6231be0SApple OSS Distributions
478e6231be0SApple OSS Distributions bool
initWithGuardPages(task_t inTask,IOOptionBits options,mach_vm_size_t capacity)479e6231be0SApple OSS Distributions IOBufferMemoryDescriptor::initWithGuardPages(
480e6231be0SApple OSS Distributions task_t inTask,
481e6231be0SApple OSS Distributions IOOptionBits options,
482e6231be0SApple OSS Distributions mach_vm_size_t capacity)
483e6231be0SApple OSS Distributions {
484e6231be0SApple OSS Distributions mach_vm_size_t roundedCapacity;
485e6231be0SApple OSS Distributions
486e6231be0SApple OSS Distributions _internalFlags = kInternalFlagGuardPages;
487e6231be0SApple OSS Distributions
488e6231be0SApple OSS Distributions if (round_page_overflow(capacity, &roundedCapacity)) {
489e6231be0SApple OSS Distributions return false;
490e6231be0SApple OSS Distributions }
491e6231be0SApple OSS Distributions
492e6231be0SApple OSS Distributions return initWithPhysicalMask(inTask, options, roundedCapacity, page_size,
493e6231be0SApple OSS Distributions (mach_vm_address_t)0);
494e6231be0SApple OSS Distributions }
495e6231be0SApple OSS Distributions
496bb611c8fSApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
inTaskWithOptions(task_t inTask,IOOptionBits options,vm_size_t capacity,vm_offset_t alignment)497a5e72196SApple OSS Distributions IOBufferMemoryDescriptor::inTaskWithOptions(
498fad439e7SApple OSS Distributions task_t inTask,
499fad439e7SApple OSS Distributions IOOptionBits options,
500fad439e7SApple OSS Distributions vm_size_t capacity,
501368ad365SApple OSS Distributions vm_offset_t alignment)
502fad439e7SApple OSS Distributions {
503bb611c8fSApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
504fad439e7SApple OSS Distributions
5053ca3bd55SApple OSS Distributions if (me && !me->initWithPhysicalMask(inTask, options, capacity, alignment, 0)) {
506bb611c8fSApple OSS Distributions me.reset();
507e13b1fa5SApple OSS Distributions }
508e13b1fa5SApple OSS Distributions return me;
509e13b1fa5SApple OSS Distributions }
510e13b1fa5SApple OSS Distributions
511bb611c8fSApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
inTaskWithOptions(task_t inTask,IOOptionBits options,vm_size_t capacity,vm_offset_t alignment,uint32_t kernTag,uint32_t userTag)512bb611c8fSApple OSS Distributions IOBufferMemoryDescriptor::inTaskWithOptions(
513bb611c8fSApple OSS Distributions task_t inTask,
514bb611c8fSApple OSS Distributions IOOptionBits options,
515bb611c8fSApple OSS Distributions vm_size_t capacity,
516bb611c8fSApple OSS Distributions vm_offset_t alignment,
517bb611c8fSApple OSS Distributions uint32_t kernTag,
518bb611c8fSApple OSS Distributions uint32_t userTag)
519bb611c8fSApple OSS Distributions {
520bb611c8fSApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
521bb611c8fSApple OSS Distributions
522bb611c8fSApple OSS Distributions if (me) {
523bb611c8fSApple OSS Distributions me->setVMTags(kernTag, userTag);
524bb611c8fSApple OSS Distributions
525bb611c8fSApple OSS Distributions if (!me->initWithPhysicalMask(inTask, options, capacity, alignment, 0)) {
526bb611c8fSApple OSS Distributions me.reset();
527bb611c8fSApple OSS Distributions }
528bb611c8fSApple OSS Distributions }
529bb611c8fSApple OSS Distributions return me;
530bb611c8fSApple OSS Distributions }
531bb611c8fSApple OSS Distributions
532bb611c8fSApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
inTaskWithPhysicalMask(task_t inTask,IOOptionBits options,mach_vm_size_t capacity,mach_vm_address_t physicalMask)533a5e72196SApple OSS Distributions IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
534e13b1fa5SApple OSS Distributions task_t inTask,
535e13b1fa5SApple OSS Distributions IOOptionBits options,
536e13b1fa5SApple OSS Distributions mach_vm_size_t capacity,
537e13b1fa5SApple OSS Distributions mach_vm_address_t physicalMask)
538e13b1fa5SApple OSS Distributions {
539bb611c8fSApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
540e13b1fa5SApple OSS Distributions
541a5e72196SApple OSS Distributions if (me && !me->initWithPhysicalMask(inTask, options, capacity, 1, physicalMask)) {
542bb611c8fSApple OSS Distributions me.reset();
543fad439e7SApple OSS Distributions }
544fad439e7SApple OSS Distributions return me;
545fad439e7SApple OSS Distributions }
546fad439e7SApple OSS Distributions
547e6231be0SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
inTaskWithGuardPages(task_t inTask,IOOptionBits options,mach_vm_size_t capacity)548e6231be0SApple OSS Distributions IOBufferMemoryDescriptor::inTaskWithGuardPages(
549e6231be0SApple OSS Distributions task_t inTask,
550e6231be0SApple OSS Distributions IOOptionBits options,
551e6231be0SApple OSS Distributions mach_vm_size_t capacity)
552e6231be0SApple OSS Distributions {
553e6231be0SApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
554e6231be0SApple OSS Distributions
555e6231be0SApple OSS Distributions if (me && !me->initWithGuardPages(inTask, options, capacity)) {
556e6231be0SApple OSS Distributions me.reset();
557e6231be0SApple OSS Distributions }
558e6231be0SApple OSS Distributions return me;
559e6231be0SApple OSS Distributions }
560e6231be0SApple OSS Distributions
5613ca3bd55SApple OSS Distributions #ifndef __LP64__
562a5e72196SApple OSS Distributions bool
initWithOptions(IOOptionBits options,vm_size_t capacity,vm_offset_t alignment)563a5e72196SApple OSS Distributions IOBufferMemoryDescriptor::initWithOptions(
564fad439e7SApple OSS Distributions IOOptionBits options,
565fad439e7SApple OSS Distributions vm_size_t capacity,
566fad439e7SApple OSS Distributions vm_offset_t alignment)
567fad439e7SApple OSS Distributions {
568a5e72196SApple OSS Distributions return initWithPhysicalMask(kernel_task, options, capacity, alignment, (mach_vm_address_t)0);
569fad439e7SApple OSS Distributions }
5703ca3bd55SApple OSS Distributions #endif /* !__LP64__ */
571fad439e7SApple OSS Distributions
572bb611c8fSApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
withOptions(IOOptionBits options,vm_size_t capacity,vm_offset_t alignment)573a5e72196SApple OSS Distributions IOBufferMemoryDescriptor::withOptions(
574c1dac77fSApple OSS Distributions IOOptionBits options,
575c1dac77fSApple OSS Distributions vm_size_t capacity,
576368ad365SApple OSS Distributions vm_offset_t alignment)
577c1dac77fSApple OSS Distributions {
578bb611c8fSApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
5793ca3bd55SApple OSS Distributions
5803ca3bd55SApple OSS Distributions if (me && !me->initWithPhysicalMask(kernel_task, options, capacity, alignment, 0)) {
581bb611c8fSApple OSS Distributions me.reset();
5823ca3bd55SApple OSS Distributions }
5833ca3bd55SApple OSS Distributions return me;
584c1dac77fSApple OSS Distributions }
585c1dac77fSApple OSS Distributions
586c1dac77fSApple OSS Distributions
587c1dac77fSApple OSS Distributions /*
588c1dac77fSApple OSS Distributions * withCapacity:
589c1dac77fSApple OSS Distributions *
590c1dac77fSApple OSS Distributions * Returns a new IOBufferMemoryDescriptor with a buffer large enough to
591c1dac77fSApple OSS Distributions * hold capacity bytes. The descriptor's length is initially set to the capacity.
592c1dac77fSApple OSS Distributions */
593bb611c8fSApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
withCapacity(vm_size_t inCapacity,IODirection inDirection,bool inContiguous)594c1dac77fSApple OSS Distributions IOBufferMemoryDescriptor::withCapacity(vm_size_t inCapacity,
595c1dac77fSApple OSS Distributions IODirection inDirection,
596c1dac77fSApple OSS Distributions bool inContiguous)
597c1dac77fSApple OSS Distributions {
598a5e72196SApple OSS Distributions return IOBufferMemoryDescriptor::withOptions(
599c1dac77fSApple OSS Distributions inDirection | kIOMemoryUnshared
600c1dac77fSApple OSS Distributions | (inContiguous ? kIOMemoryPhysicallyContiguous : 0),
601a5e72196SApple OSS Distributions inCapacity, inContiguous ? inCapacity : 1 );
602c1dac77fSApple OSS Distributions }
603c1dac77fSApple OSS Distributions
6043ca3bd55SApple OSS Distributions #ifndef __LP64__
605c1dac77fSApple OSS Distributions /*
606c1dac77fSApple OSS Distributions * initWithBytes:
607c1dac77fSApple OSS Distributions *
608c1dac77fSApple OSS Distributions * Initialize a new IOBufferMemoryDescriptor preloaded with bytes (copied).
609c1dac77fSApple OSS Distributions * The descriptor's length and capacity are set to the input buffer's size.
610c1dac77fSApple OSS Distributions */
611a5e72196SApple OSS Distributions bool
initWithBytes(const void * inBytes,vm_size_t inLength,IODirection inDirection,bool inContiguous)612a5e72196SApple OSS Distributions IOBufferMemoryDescriptor::initWithBytes(const void * inBytes,
613c1dac77fSApple OSS Distributions vm_size_t inLength,
614c1dac77fSApple OSS Distributions IODirection inDirection,
615c1dac77fSApple OSS Distributions bool inContiguous)
616c1dac77fSApple OSS Distributions {
6173ca3bd55SApple OSS Distributions if (!initWithPhysicalMask(kernel_task, inDirection | kIOMemoryUnshared
618c1dac77fSApple OSS Distributions | (inContiguous ? kIOMemoryPhysicallyContiguous : 0),
619a5e72196SApple OSS Distributions inLength, inLength, (mach_vm_address_t)0)) {
620c1dac77fSApple OSS Distributions return false;
621a5e72196SApple OSS Distributions }
622c1dac77fSApple OSS Distributions
623c1dac77fSApple OSS Distributions // start out with no data
624c1dac77fSApple OSS Distributions setLength(0);
625c1dac77fSApple OSS Distributions
626a5e72196SApple OSS Distributions if (!appendBytes(inBytes, inLength)) {
627c1dac77fSApple OSS Distributions return false;
628a5e72196SApple OSS Distributions }
629c1dac77fSApple OSS Distributions
630c1dac77fSApple OSS Distributions return true;
631c1dac77fSApple OSS Distributions }
6323ca3bd55SApple OSS Distributions #endif /* !__LP64__ */
633c1dac77fSApple OSS Distributions
634c1dac77fSApple OSS Distributions /*
635c1dac77fSApple OSS Distributions * withBytes:
636c1dac77fSApple OSS Distributions *
637c1dac77fSApple OSS Distributions * Returns a new IOBufferMemoryDescriptor preloaded with bytes (copied).
638c1dac77fSApple OSS Distributions * The descriptor's length and capacity are set to the input buffer's size.
639c1dac77fSApple OSS Distributions */
640bb611c8fSApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor>
withBytes(const void * inBytes,vm_size_t inLength,IODirection inDirection,bool inContiguous)641c1dac77fSApple OSS Distributions IOBufferMemoryDescriptor::withBytes(const void * inBytes,
642c1dac77fSApple OSS Distributions vm_size_t inLength,
643c1dac77fSApple OSS Distributions IODirection inDirection,
644c1dac77fSApple OSS Distributions bool inContiguous)
645c1dac77fSApple OSS Distributions {
646bb611c8fSApple OSS Distributions OSSharedPtr<IOBufferMemoryDescriptor> me = OSMakeShared<IOBufferMemoryDescriptor>();
6471031c584SApple OSS Distributions mach_vm_address_t alignment;
648c1dac77fSApple OSS Distributions
6491031c584SApple OSS Distributions alignment = (inLength <= page_size) ? inLength : page_size;
6503ca3bd55SApple OSS Distributions if (me && !me->initWithPhysicalMask(
6513ca3bd55SApple OSS Distributions kernel_task, inDirection | kIOMemoryUnshared
6523ca3bd55SApple OSS Distributions | (inContiguous ? kIOMemoryPhysicallyContiguous : 0),
6531031c584SApple OSS Distributions inLength, alignment, 0 )) {
654bb611c8fSApple OSS Distributions me.reset();
655c1dac77fSApple OSS Distributions }
6563ca3bd55SApple OSS Distributions
657a5e72196SApple OSS Distributions if (me) {
6583ca3bd55SApple OSS Distributions // start out with no data
6593ca3bd55SApple OSS Distributions me->setLength(0);
6603ca3bd55SApple OSS Distributions
661a5e72196SApple OSS Distributions if (!me->appendBytes(inBytes, inLength)) {
662bb611c8fSApple OSS Distributions me.reset();
6633ca3bd55SApple OSS Distributions }
6643ca3bd55SApple OSS Distributions }
665c1dac77fSApple OSS Distributions return me;
666c1dac77fSApple OSS Distributions }
667c1dac77fSApple OSS Distributions
668c1dac77fSApple OSS Distributions /*
669c1dac77fSApple OSS Distributions * free:
670c1dac77fSApple OSS Distributions *
671c1dac77fSApple OSS Distributions * Free resources
672c1dac77fSApple OSS Distributions */
673a5e72196SApple OSS Distributions void
free()674a5e72196SApple OSS Distributions IOBufferMemoryDescriptor::free()
675c1dac77fSApple OSS Distributions {
676368ad365SApple OSS Distributions // Cache all of the relevant information on the stack for use
677368ad365SApple OSS Distributions // after we call super::free()!
678e13b1fa5SApple OSS Distributions IOOptionBits flags = _flags;
679855239e5SApple OSS Distributions IOOptionBits internalFlags = _internalFlags;
680c1dac77fSApple OSS Distributions IOOptionBits options = _options;
681c1dac77fSApple OSS Distributions vm_size_t size = _capacity;
682c1dac77fSApple OSS Distributions void * buffer = _buffer;
683a5e72196SApple OSS Distributions IOMemoryMap * map = NULL;
6843ca3bd55SApple OSS Distributions IOAddressRange * range = _ranges.v64;
685c1dac77fSApple OSS Distributions vm_offset_t alignment = _alignment;
686e6231be0SApple OSS Distributions kalloc_heap_t kheap = KHEAP_DATA_BUFFERS;
687aca3beaaSApple OSS Distributions vm_size_t rsize;
688e6231be0SApple OSS Distributions
689a5e72196SApple OSS Distributions if (alignment >= page_size) {
690aca3beaaSApple OSS Distributions if (!round_page_overflow(size, &rsize)) {
691aca3beaaSApple OSS Distributions size = rsize;
692aca3beaaSApple OSS Distributions }
693a5e72196SApple OSS Distributions }
6943ca3bd55SApple OSS Distributions
695a5e72196SApple OSS Distributions if (reserved) {
696e13b1fa5SApple OSS Distributions map = reserved->map;
697e6231be0SApple OSS Distributions IOFreeType(reserved, ExpansionData);
698a5e72196SApple OSS Distributions if (map) {
699e13b1fa5SApple OSS Distributions map->release();
700fad439e7SApple OSS Distributions }
701a5e72196SApple OSS Distributions }
702fad439e7SApple OSS Distributions
70376e12aa3SApple OSS Distributions if ((options & kIOMemoryPageable)
704a5e72196SApple OSS Distributions || (kInternalFlagPageSized & internalFlags)) {
705aca3beaaSApple OSS Distributions if (!round_page_overflow(size, &rsize)) {
706aca3beaaSApple OSS Distributions size = rsize;
707aca3beaaSApple OSS Distributions }
708a5e72196SApple OSS Distributions }
70976e12aa3SApple OSS Distributions
710e6231be0SApple OSS Distributions if (internalFlags & kInternalFlagHasPointers) {
7111031c584SApple OSS Distributions kheap = KHEAP_IOBMD_CONTROL;
712e6231be0SApple OSS Distributions }
713e6231be0SApple OSS Distributions
71476e12aa3SApple OSS Distributions #if IOTRACKING
71576e12aa3SApple OSS Distributions if (!(options & kIOMemoryPageable)
71676e12aa3SApple OSS Distributions && buffer
717a5e72196SApple OSS Distributions && (kInternalFlagInit & _internalFlags)) {
718a5e72196SApple OSS Distributions trackingAccumSize(-size);
719a5e72196SApple OSS Distributions }
72076e12aa3SApple OSS Distributions #endif /* IOTRACKING */
72176e12aa3SApple OSS Distributions
722c1dac77fSApple OSS Distributions /* super::free may unwire - deallocate buffer afterwards */
723c1dac77fSApple OSS Distributions super::free();
724c1dac77fSApple OSS Distributions
725a5e72196SApple OSS Distributions if (options & kIOMemoryPageable) {
72614e3d835SApple OSS Distributions #if IOALLOCDEBUG
72776e12aa3SApple OSS Distributions OSAddAtomicLong(-size, &debug_iomallocpageable_size);
72814e3d835SApple OSS Distributions #endif
729a5e72196SApple OSS Distributions } else if (buffer) {
730a5e72196SApple OSS Distributions if (kInternalFlagPhysical & internalFlags) {
731e6231be0SApple OSS Distributions IOKernelFreePhysical(kheap, (mach_vm_address_t) buffer, size);
732a5e72196SApple OSS Distributions } else if (kInternalFlagPageAllocated & internalFlags) {
733e7776783SApple OSS Distributions #if defined(__x86_64__)
734186b8fceSApple OSS Distributions uintptr_t page;
735186b8fceSApple OSS Distributions page = iopa_free(&gIOBMDPageAllocator, (uintptr_t) buffer, size);
736a5e72196SApple OSS Distributions if (page) {
7375c2921b0SApple OSS Distributions kmem_free(kernel_map, page, page_size);
738186b8fceSApple OSS Distributions }
739186b8fceSApple OSS Distributions #if IOALLOCDEBUG
740bb611c8fSApple OSS Distributions OSAddAtomicLong(-size, &debug_iomalloc_size);
741186b8fceSApple OSS Distributions #endif
742186b8fceSApple OSS Distributions IOStatisticsAlloc(kIOStatisticsFreeAligned, size);
743e7776783SApple OSS Distributions #else /* !defined(__x86_64__) */
744e7776783SApple OSS Distributions /* should be unreachable */
745e7776783SApple OSS Distributions panic("Attempting to free IOBMD with page allocated flag");
746e7776783SApple OSS Distributions #endif /* defined(__x86_64__) */
747e6231be0SApple OSS Distributions } else if (kInternalFlagGuardPages & internalFlags) {
748e6231be0SApple OSS Distributions vm_offset_t allocation = (vm_offset_t)buffer - page_size;
7495c2921b0SApple OSS Distributions kmem_free(kernel_map, allocation, size + page_size * 2);
750e6231be0SApple OSS Distributions #if IOALLOCDEBUG
751e6231be0SApple OSS Distributions OSAddAtomicLong(-size, &debug_iomalloc_size);
752e6231be0SApple OSS Distributions #endif
753e6231be0SApple OSS Distributions IOStatisticsAlloc(kIOStatisticsFreeAligned, size);
754a5e72196SApple OSS Distributions } else if (alignment > 1) {
7555c2921b0SApple OSS Distributions /* BEGIN IGNORE CODESTYLE */
7565c2921b0SApple OSS Distributions __typed_allocators_ignore_push
757e6231be0SApple OSS Distributions IOFreeAligned_internal(kheap, buffer, size);
758a5e72196SApple OSS Distributions } else {
759e6231be0SApple OSS Distributions IOFree_internal(kheap, buffer, size);
7605c2921b0SApple OSS Distributions __typed_allocators_ignore_pop
7615c2921b0SApple OSS Distributions /* END IGNORE CODESTYLE */
762c1dac77fSApple OSS Distributions }
763186b8fceSApple OSS Distributions }
764a5e72196SApple OSS Distributions if (range && (kIOMemoryAsReference & flags)) {
765e6231be0SApple OSS Distributions IOFreeType(range, IOAddressRange);
766c1dac77fSApple OSS Distributions }
767a5e72196SApple OSS Distributions }
768c1dac77fSApple OSS Distributions
769c1dac77fSApple OSS Distributions /*
770c1dac77fSApple OSS Distributions * getCapacity:
771c1dac77fSApple OSS Distributions *
772c1dac77fSApple OSS Distributions * Get the buffer capacity
773c1dac77fSApple OSS Distributions */
774a5e72196SApple OSS Distributions vm_size_t
getCapacity() const775a5e72196SApple OSS Distributions IOBufferMemoryDescriptor::getCapacity() const
776c1dac77fSApple OSS Distributions {
777c1dac77fSApple OSS Distributions return _capacity;
778c1dac77fSApple OSS Distributions }
779c1dac77fSApple OSS Distributions
780c1dac77fSApple OSS Distributions /*
781c1dac77fSApple OSS Distributions * setLength:
782c1dac77fSApple OSS Distributions *
783c1dac77fSApple OSS Distributions * Change the buffer length of the memory descriptor. When a new buffer
784c1dac77fSApple OSS Distributions * is created, the initial length of the buffer is set to be the same as
785c1dac77fSApple OSS Distributions * the capacity. The length can be adjusted via setLength for a shorter
786c1dac77fSApple OSS Distributions * transfer (there is no need to create more buffer descriptors when you
787c1dac77fSApple OSS Distributions * can reuse an existing one, even for different transfer sizes). Note
788c1dac77fSApple OSS Distributions * that the specified length must not exceed the capacity of the buffer.
789c1dac77fSApple OSS Distributions */
790a5e72196SApple OSS Distributions void
setLength(vm_size_t length)791a5e72196SApple OSS Distributions IOBufferMemoryDescriptor::setLength(vm_size_t length)
792c1dac77fSApple OSS Distributions {
793c1dac77fSApple OSS Distributions assert(length <= _capacity);
794a5e72196SApple OSS Distributions if (length > _capacity) {
795a5e72196SApple OSS Distributions return;
796a5e72196SApple OSS Distributions }
797c1dac77fSApple OSS Distributions
798c1dac77fSApple OSS Distributions _length = length;
799e13b1fa5SApple OSS Distributions _ranges.v64->length = length;
800c1dac77fSApple OSS Distributions }
801c1dac77fSApple OSS Distributions
802c1dac77fSApple OSS Distributions /*
803c1dac77fSApple OSS Distributions * setDirection:
804c1dac77fSApple OSS Distributions *
805c1dac77fSApple OSS Distributions * Change the direction of the transfer. This method allows one to redirect
806c1dac77fSApple OSS Distributions * the descriptor's transfer direction. This eliminates the need to destroy
807c1dac77fSApple OSS Distributions * and create new buffers when different transfer directions are needed.
808c1dac77fSApple OSS Distributions */
809a5e72196SApple OSS Distributions void
setDirection(IODirection direction)810a5e72196SApple OSS Distributions IOBufferMemoryDescriptor::setDirection(IODirection direction)
811c1dac77fSApple OSS Distributions {
8123ca3bd55SApple OSS Distributions _flags = (_flags & ~kIOMemoryDirectionMask) | direction;
8133ca3bd55SApple OSS Distributions #ifndef __LP64__
8143ca3bd55SApple OSS Distributions _direction = (IODirection) (_flags & kIOMemoryDirectionMask);
8153ca3bd55SApple OSS Distributions #endif /* !__LP64__ */
816c1dac77fSApple OSS Distributions }
817c1dac77fSApple OSS Distributions
818c1dac77fSApple OSS Distributions /*
819c1dac77fSApple OSS Distributions * appendBytes:
820c1dac77fSApple OSS Distributions *
821c1dac77fSApple OSS Distributions * Add some data to the end of the buffer. This method automatically
822c1dac77fSApple OSS Distributions * maintains the memory descriptor buffer length. Note that appendBytes
823c1dac77fSApple OSS Distributions * will not copy past the end of the memory descriptor's current capacity.
824c1dac77fSApple OSS Distributions */
825c1dac77fSApple OSS Distributions bool
appendBytes(const void * bytes,vm_size_t withLength)826c1dac77fSApple OSS Distributions IOBufferMemoryDescriptor::appendBytes(const void * bytes, vm_size_t withLength)
827c1dac77fSApple OSS Distributions {
828c1dac77fSApple OSS Distributions vm_size_t actualBytesToCopy = min(withLength, _capacity - _length);
829e13b1fa5SApple OSS Distributions IOByteCount offset;
830c1dac77fSApple OSS Distributions
831c1dac77fSApple OSS Distributions assert(_length <= _capacity);
832e13b1fa5SApple OSS Distributions
833e13b1fa5SApple OSS Distributions offset = _length;
834c1dac77fSApple OSS Distributions _length += actualBytesToCopy;
835e13b1fa5SApple OSS Distributions _ranges.v64->length += actualBytesToCopy;
836e13b1fa5SApple OSS Distributions
837a5e72196SApple OSS Distributions if (_task == kernel_task) {
838e13b1fa5SApple OSS Distributions bcopy(/* from */ bytes, (void *)(_ranges.v64->address + offset),
839e13b1fa5SApple OSS Distributions actualBytesToCopy);
840a5e72196SApple OSS Distributions } else {
841e13b1fa5SApple OSS Distributions writeBytes(offset, bytes, actualBytesToCopy);
842a5e72196SApple OSS Distributions }
843c1dac77fSApple OSS Distributions
844c1dac77fSApple OSS Distributions return true;
845c1dac77fSApple OSS Distributions }
846c1dac77fSApple OSS Distributions
847c1dac77fSApple OSS Distributions /*
848c1dac77fSApple OSS Distributions * getBytesNoCopy:
849c1dac77fSApple OSS Distributions *
850c1dac77fSApple OSS Distributions * Return the virtual address of the beginning of the buffer
851c1dac77fSApple OSS Distributions */
852a5e72196SApple OSS Distributions void *
getBytesNoCopy()853a5e72196SApple OSS Distributions IOBufferMemoryDescriptor::getBytesNoCopy()
854c1dac77fSApple OSS Distributions {
855a5e72196SApple OSS Distributions if (kIOMemoryTypePhysical64 == (_flags & kIOMemoryTypeMask)) {
856e13b1fa5SApple OSS Distributions return _buffer;
857a5e72196SApple OSS Distributions } else {
858e13b1fa5SApple OSS Distributions return (void *)_ranges.v64->address;
859c1dac77fSApple OSS Distributions }
860a5e72196SApple OSS Distributions }
861c1dac77fSApple OSS Distributions
862e13b1fa5SApple OSS Distributions
863c1dac77fSApple OSS Distributions /*
864c1dac77fSApple OSS Distributions * getBytesNoCopy:
865c1dac77fSApple OSS Distributions *
866c1dac77fSApple OSS Distributions * Return the virtual address of an offset from the beginning of the buffer
867c1dac77fSApple OSS Distributions */
868c1dac77fSApple OSS Distributions void *
getBytesNoCopy(vm_size_t start,vm_size_t withLength)869c1dac77fSApple OSS Distributions IOBufferMemoryDescriptor::getBytesNoCopy(vm_size_t start, vm_size_t withLength)
870c1dac77fSApple OSS Distributions {
871e13b1fa5SApple OSS Distributions IOVirtualAddress address;
87288cc0b97SApple OSS Distributions
873a5e72196SApple OSS Distributions if ((start + withLength) < start) {
874a5e72196SApple OSS Distributions return NULL;
875a5e72196SApple OSS Distributions }
87688cc0b97SApple OSS Distributions
877a5e72196SApple OSS Distributions if (kIOMemoryTypePhysical64 == (_flags & kIOMemoryTypeMask)) {
878e13b1fa5SApple OSS Distributions address = (IOVirtualAddress) _buffer;
879a5e72196SApple OSS Distributions } else {
880e13b1fa5SApple OSS Distributions address = _ranges.v64->address;
881a5e72196SApple OSS Distributions }
882e13b1fa5SApple OSS Distributions
883a5e72196SApple OSS Distributions if (start < _length && (start + withLength) <= _length) {
884e13b1fa5SApple OSS Distributions return (void *)(address + start);
885a5e72196SApple OSS Distributions }
886a5e72196SApple OSS Distributions return NULL;
887c1dac77fSApple OSS Distributions }
888c1dac77fSApple OSS Distributions
8893ca3bd55SApple OSS Distributions #ifndef __LP64__
890a5e72196SApple OSS Distributions void *
getVirtualSegment(IOByteCount offset,IOByteCount * lengthOfSegment)891a5e72196SApple OSS Distributions IOBufferMemoryDescriptor::getVirtualSegment(IOByteCount offset,
8923ca3bd55SApple OSS Distributions IOByteCount * lengthOfSegment)
893e13b1fa5SApple OSS Distributions {
894e13b1fa5SApple OSS Distributions void * bytes = getBytesNoCopy(offset, 0);
895e13b1fa5SApple OSS Distributions
896a5e72196SApple OSS Distributions if (bytes && lengthOfSegment) {
897e13b1fa5SApple OSS Distributions *lengthOfSegment = _length - offset;
898a5e72196SApple OSS Distributions }
899e13b1fa5SApple OSS Distributions
900e13b1fa5SApple OSS Distributions return bytes;
901e13b1fa5SApple OSS Distributions }
9023ca3bd55SApple OSS Distributions #endif /* !__LP64__ */
903e13b1fa5SApple OSS Distributions
9043ca3bd55SApple OSS Distributions #ifdef __LP64__
9053ca3bd55SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 0);
9063ca3bd55SApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 1);
9073ca3bd55SApple OSS Distributions #else /* !__LP64__ */
908bb611c8fSApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOBufferMemoryDescriptor, 0);
909bb611c8fSApple OSS Distributions OSMetaClassDefineReservedUsedX86(IOBufferMemoryDescriptor, 1);
9103ca3bd55SApple OSS Distributions #endif /* !__LP64__ */
911c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 2);
912c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 3);
913c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 4);
914c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 5);
915c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 6);
916c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 7);
917c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 8);
918c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 9);
919c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 10);
920c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 11);
921c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 12);
922c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 13);
923c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 14);
924c1dac77fSApple OSS Distributions OSMetaClassDefineReservedUnused(IOBufferMemoryDescriptor, 15);
925