xref: /xnu-11417.101.15/iokit/Kernel/IOGuardPageMemoryDescriptor.cpp (revision e3723e1f17661b24996789d8afc084c0c3303b26)
1*e3723e1fSApple OSS Distributions /*
2*e3723e1fSApple OSS Distributions  * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3*e3723e1fSApple OSS Distributions  *
4*e3723e1fSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*e3723e1fSApple OSS Distributions  *
6*e3723e1fSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*e3723e1fSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*e3723e1fSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*e3723e1fSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*e3723e1fSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*e3723e1fSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*e3723e1fSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*e3723e1fSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*e3723e1fSApple OSS Distributions  *
15*e3723e1fSApple OSS Distributions  * Please obtain a copy of the License at
16*e3723e1fSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*e3723e1fSApple OSS Distributions  *
18*e3723e1fSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*e3723e1fSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*e3723e1fSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*e3723e1fSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*e3723e1fSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*e3723e1fSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*e3723e1fSApple OSS Distributions  * limitations under the License.
25*e3723e1fSApple OSS Distributions  *
26*e3723e1fSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*e3723e1fSApple OSS Distributions  */
28*e3723e1fSApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
29*e3723e1fSApple OSS Distributions #include <IOKit/IOGuardPageMemoryDescriptor.h>
30*e3723e1fSApple OSS Distributions #include <IOKit/IOMemoryDescriptor.h>
31*e3723e1fSApple OSS Distributions #include <IOKit/IOLib.h>
32*e3723e1fSApple OSS Distributions #include <vm/vm_kern_xnu.h>
33*e3723e1fSApple OSS Distributions #include <mach/mach_vm.h>
34*e3723e1fSApple OSS Distributions 
35*e3723e1fSApple OSS Distributions #define super IOGeneralMemoryDescriptor
36*e3723e1fSApple OSS Distributions 
37*e3723e1fSApple OSS Distributions OSDefineMetaClassAndStructorsWithZone(IOGuardPageMemoryDescriptor, IOGeneralMemoryDescriptor, ZC_ZFREE_CLEARMEM);
38*e3723e1fSApple OSS Distributions 
39*e3723e1fSApple OSS Distributions OSSharedPtr<IOGuardPageMemoryDescriptor>
withSize(vm_size_t size)40*e3723e1fSApple OSS Distributions IOGuardPageMemoryDescriptor::withSize(vm_size_t size)
41*e3723e1fSApple OSS Distributions {
42*e3723e1fSApple OSS Distributions 	OSSharedPtr<IOGuardPageMemoryDescriptor> me = OSMakeShared<IOGuardPageMemoryDescriptor>();
43*e3723e1fSApple OSS Distributions 
44*e3723e1fSApple OSS Distributions 	if (me && !me->initWithSize(size)) {
45*e3723e1fSApple OSS Distributions 		me.reset();
46*e3723e1fSApple OSS Distributions 	}
47*e3723e1fSApple OSS Distributions 	return me;
48*e3723e1fSApple OSS Distributions }
49*e3723e1fSApple OSS Distributions 
50*e3723e1fSApple OSS Distributions bool
initWithSize(vm_size_t size)51*e3723e1fSApple OSS Distributions IOGuardPageMemoryDescriptor::initWithSize(vm_size_t size)
52*e3723e1fSApple OSS Distributions {
53*e3723e1fSApple OSS Distributions 	mach_vm_address_t address;
54*e3723e1fSApple OSS Distributions 	kern_return_t kr;
55*e3723e1fSApple OSS Distributions 	IOOptionBits  iomdOptions = kIOMemoryTypeVirtual64 | kIOMemoryAsReference | kIODirectionOutIn;
56*e3723e1fSApple OSS Distributions 
57*e3723e1fSApple OSS Distributions 	size = round_page(size);
58*e3723e1fSApple OSS Distributions 
59*e3723e1fSApple OSS Distributions 	_ranges.v64 = IOMallocType(IOAddressRange);
60*e3723e1fSApple OSS Distributions 	if (!_ranges.v64) {
61*e3723e1fSApple OSS Distributions 		return false;
62*e3723e1fSApple OSS Distributions 	}
63*e3723e1fSApple OSS Distributions 
64*e3723e1fSApple OSS Distributions 	kr = mach_vm_allocate_kernel(kernel_map, &address, size,
65*e3723e1fSApple OSS Distributions 	    VM_MAP_KERNEL_FLAGS_ANYWHERE(.vm_tag = VM_KERN_MEMORY_IOKIT));
66*e3723e1fSApple OSS Distributions 	if (kr != KERN_SUCCESS) {
67*e3723e1fSApple OSS Distributions 		return false;
68*e3723e1fSApple OSS Distributions 	}
69*e3723e1fSApple OSS Distributions 
70*e3723e1fSApple OSS Distributions 
71*e3723e1fSApple OSS Distributions 	_ranges.v64->address = address;
72*e3723e1fSApple OSS Distributions 	_ranges.v64->length  = size;
73*e3723e1fSApple OSS Distributions 
74*e3723e1fSApple OSS Distributions 	if (!super::initWithOptions(_ranges.v64, 1, 0, kernel_task, iomdOptions, NULL)) {
75*e3723e1fSApple OSS Distributions 		return false;
76*e3723e1fSApple OSS Distributions 	}
77*e3723e1fSApple OSS Distributions 
78*e3723e1fSApple OSS Distributions 	_size = size;
79*e3723e1fSApple OSS Distributions 	_buffer = (vm_offset_t)address;
80*e3723e1fSApple OSS Distributions 
81*e3723e1fSApple OSS Distributions 	return true;
82*e3723e1fSApple OSS Distributions }
83*e3723e1fSApple OSS Distributions 
84*e3723e1fSApple OSS Distributions void
free()85*e3723e1fSApple OSS Distributions IOGuardPageMemoryDescriptor::free()
86*e3723e1fSApple OSS Distributions {
87*e3723e1fSApple OSS Distributions 	if (_buffer) {
88*e3723e1fSApple OSS Distributions 		vm_deallocate(kernel_map, _buffer, _size);
89*e3723e1fSApple OSS Distributions 		_buffer = 0;
90*e3723e1fSApple OSS Distributions 	}
91*e3723e1fSApple OSS Distributions 
92*e3723e1fSApple OSS Distributions 	if (_ranges.v64) {
93*e3723e1fSApple OSS Distributions 		IOFreeType(_ranges.v64, IOAddressRange);
94*e3723e1fSApple OSS Distributions 	}
95*e3723e1fSApple OSS Distributions 
96*e3723e1fSApple OSS Distributions 	super::free();
97*e3723e1fSApple OSS Distributions }
98*e3723e1fSApple OSS Distributions 
99*e3723e1fSApple OSS Distributions IOReturn
doMap(vm_map_t addressMap,IOVirtualAddress * atAddress,IOOptionBits options,IOByteCount sourceOffset,IOByteCount length)100*e3723e1fSApple OSS Distributions IOGuardPageMemoryDescriptor::doMap(vm_map_t           addressMap,
101*e3723e1fSApple OSS Distributions     IOVirtualAddress * atAddress,
102*e3723e1fSApple OSS Distributions     IOOptionBits       options,
103*e3723e1fSApple OSS Distributions     IOByteCount        sourceOffset,
104*e3723e1fSApple OSS Distributions     IOByteCount        length)
105*e3723e1fSApple OSS Distributions {
106*e3723e1fSApple OSS Distributions 	IOReturn ret = super::doMap(addressMap, atAddress, options, sourceOffset, length);
107*e3723e1fSApple OSS Distributions 	if (ret == kIOReturnSuccess) {
108*e3723e1fSApple OSS Distributions 		IOMemoryMap *     mapping = (IOMemoryMap *) *atAddress;
109*e3723e1fSApple OSS Distributions 		vm_map_t          map     = mapping->fAddressMap;
110*e3723e1fSApple OSS Distributions 		mach_vm_size_t    length  = mapping->fLength;
111*e3723e1fSApple OSS Distributions 		mach_vm_address_t address = mapping->fAddress;
112*e3723e1fSApple OSS Distributions 		kern_return_t kr = mach_vm_protect(map, address, length, true, VM_PROT_NONE);
113*e3723e1fSApple OSS Distributions 		if (kr != KERN_SUCCESS) {
114*e3723e1fSApple OSS Distributions 			doUnmap(map, (IOVirtualAddress) mapping, 0);
115*e3723e1fSApple OSS Distributions 			return kIOReturnError;
116*e3723e1fSApple OSS Distributions 		}
117*e3723e1fSApple OSS Distributions 	}
118*e3723e1fSApple OSS Distributions 	return ret;
119*e3723e1fSApple OSS Distributions }
120