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