]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/kext_alloc.c
1d3aea127e0a62347e1c7ccb734dd9d0eb57b525
[apple/xnu.git] / osfmk / kern / kext_alloc.c
1 /*
2 * Copyright (c) 2008 Apple 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 #include <kern/assert.h>
29 #include <kern/debug.h>
30 #include <kern/kext_alloc.h>
31 #include <kern/misc_protos.h>
32
33 #include <mach/host_priv_server.h>
34 #include <mach/kern_return.h>
35 #include <mach/mach_vm.h>
36 #include <mach/vm_map.h>
37 #include <mach/vm_types.h>
38
39 #include <mach-o/loader.h>
40 #include <libkern/kernel_mach_header.h>
41
42
43 vm_map_t g_kext_map = 0;
44 static mach_vm_offset_t kext_alloc_base = 0;
45 static mach_vm_offset_t kext_alloc_max = 0;
46
47 /*
48 * On x86_64 systems, kernel extension text must remain within 2GB of the
49 * kernel's text segment. To ensure this happens, we snag 2GB of kernel VM
50 * as early as possible for kext allocations.
51 */
52 void
53 kext_alloc_init(void)
54 {
55 #if __x86_64__
56 kern_return_t rval = 0;
57 kernel_segment_command_t *text = NULL;
58 mach_vm_offset_t text_end, text_start;
59 mach_vm_size_t text_size;
60 mach_vm_size_t kext_alloc_size;
61
62 /* Determine the start of the kernel's __TEXT segment and determine the
63 * lower bound of the allocated submap for kext allocations.
64 */
65
66 text = getsegbyname(SEG_TEXT);
67 text_start = vm_map_trunc_page(text->vmaddr);
68 text_start &= ~((512ULL * 1024 * 1024 * 1024) - 1);
69 text_end = vm_map_round_page(text->vmaddr + text->vmsize);
70 text_size = text_end - text_start;
71
72 kext_alloc_base = KEXT_ALLOC_BASE(text_end);
73 kext_alloc_size = KEXT_ALLOC_SIZE(text_size);
74 kext_alloc_max = kext_alloc_base + kext_alloc_size;
75
76 /* Allocate the subblock of the kernel map */
77
78 rval = kmem_suballoc(kernel_map, (vm_offset_t *) &kext_alloc_base,
79 kext_alloc_size, /* pageable */ TRUE,
80 VM_FLAGS_FIXED|VM_FLAGS_OVERWRITE,
81 &g_kext_map);
82 if (rval != KERN_SUCCESS) {
83 panic("kext_alloc_init: kmem_suballoc failed 0x%x\n", rval);
84 }
85
86 if ((kext_alloc_base + kext_alloc_size) > kext_alloc_max) {
87 panic("kext_alloc_init: failed to get first 2GB\n");
88 }
89
90 if (kernel_map->min_offset > kext_alloc_base) {
91 kernel_map->min_offset = kext_alloc_base;
92 }
93
94 printf("kext submap [0x%llx - 0x%llx], kernel text [0x%llx - 0x%llx]\n",
95 kext_alloc_base, kext_alloc_max, text->vmaddr,
96 text->vmaddr + text->vmsize);
97 #else
98 g_kext_map = kernel_map;
99 kext_alloc_base = VM_MIN_KERNEL_ADDRESS;
100 kext_alloc_max = VM_MAX_KERNEL_ADDRESS;
101 #endif /* __x86_64__ */
102 }
103
104 kern_return_t
105 kext_alloc(vm_offset_t *_addr, vm_size_t size, boolean_t fixed)
106 {
107 kern_return_t rval = 0;
108 mach_vm_offset_t addr = (fixed) ? *_addr : kext_alloc_base;
109 int flags = (fixed) ? VM_FLAGS_FIXED : VM_FLAGS_ANYWHERE;
110
111 /* Allocate the kext virtual memory */
112 rval = mach_vm_allocate(g_kext_map, &addr, size, flags);
113 if (rval != KERN_SUCCESS) {
114 printf("vm_allocate failed - %d\n", rval);
115 goto finish;
116 }
117
118 /* Check that the memory is reachable by kernel text */
119 if ((addr + size) > kext_alloc_max) {
120 kext_free((vm_offset_t)addr, size);
121 goto finish;
122 }
123
124 *_addr = (vm_offset_t)addr;
125 rval = KERN_SUCCESS;
126
127 finish:
128 return rval;
129 }
130
131 void
132 kext_free(vm_offset_t addr, vm_size_t size)
133 {
134 kern_return_t rval;
135
136 rval = mach_vm_deallocate(g_kext_map, addr, size);
137 assert(rval == KERN_SUCCESS);
138 }
139