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