]> git.saurik.com Git - apple/xnu.git/blob - osfmk/x86_64/copyio.c
6411418061d88e562eae15aeee01d98e3d2bc7a5
[apple/xnu.git] / osfmk / x86_64 / copyio.c
1 /*
2 * Copyright (c) 2009 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 <mach_assert.h>
29
30 #include <sys/errno.h>
31 #include <i386/param.h>
32 #include <i386/misc_protos.h>
33 #include <i386/cpu_data.h>
34 #include <i386/machine_routines.h>
35 #include <i386/cpuid.h>
36 #include <i386/vmx.h>
37 #include <vm/pmap.h>
38 #include <vm/vm_map.h>
39 #include <vm/vm_kern.h>
40 #include <vm/vm_fault.h>
41
42 #include <sys/kdebug.h>
43
44 static int copyio(int, user_addr_t, char *, vm_size_t, vm_size_t *, int);
45 static int copyio_phys(addr64_t, addr64_t, vm_size_t, int);
46
47 /*
48 * The copy engine has the following characteristics
49 * - copyio() handles copies to/from user or kernel space
50 * - copypv() deals with physical or virtual addresses
51 *
52 * Readers familiar with the 32-bit kernel will expect Joe's thesis at this
53 * point describing the full glory of the copy window implementation. In K64,
54 * however, there is no need for windowing. Thanks to the vast shared address
55 * space, the kernel has direct access to userspace and to physical memory.
56 *
57 * User virtual addresses are accessible provided the user's cr3 is loaded.
58 * Physical addresses are accessible via the direct map and the PHYSMAP_PTOV()
59 * translation.
60 *
61 * Copyin/out variants all boil done to just these 2 routines in locore.s which
62 * provide fault-recoverable copying:
63 */
64 extern int _bcopy(const void *, void *, vm_size_t);
65 extern int _bcopystr(const void *, void *, vm_size_t, vm_size_t *);
66
67
68 /*
69 * Types of copies:
70 */
71 #define COPYIN 0 /* from user virtual to kernel virtual */
72 #define COPYOUT 1 /* from kernel virtual to user virtual */
73 #define COPYINSTR 2 /* string variant of copyout */
74 #define COPYINPHYS 3 /* from user virtual to kernel physical */
75 #define COPYOUTPHYS 4 /* from kernel physical to user virtual */
76
77
78 static int
79 copyio(int copy_type, user_addr_t user_addr, char *kernel_addr,
80 vm_size_t nbytes, vm_size_t *lencopied, int use_kernel_map)
81 {
82 thread_t thread;
83 pmap_t pmap;
84 vm_size_t bytes_copied;
85 int error = 0;
86 boolean_t istate = FALSE;
87 boolean_t recursive_CopyIOActive;
88 #if KDEBUG
89 int debug_type = 0xeff70010;
90 debug_type += (copy_type << 2);
91 #endif
92
93 thread = current_thread();
94
95 KERNEL_DEBUG(debug_type | DBG_FUNC_START,
96 (unsigned)(user_addr >> 32), (unsigned)user_addr,
97 nbytes, thread->machine.copyio_state, 0);
98
99 if (nbytes == 0)
100 goto out;
101
102 pmap = thread->map->pmap;
103
104 if ((copy_type != COPYINPHYS) && (copy_type != COPYOUTPHYS) && ((vm_offset_t)kernel_addr < VM_MIN_KERNEL_AND_KEXT_ADDRESS)) {
105 panic("Invalid copy parameter, copy type: %d, kernel address: %p", copy_type, kernel_addr);
106 }
107
108 /* Sanity and security check for addresses to/from a user */
109
110 if (((pmap != kernel_pmap) && (use_kernel_map == 0)) &&
111 ((nbytes && (user_addr+nbytes <= user_addr)) || ((user_addr + nbytes) > vm_map_max(thread->map)))) {
112 error = EFAULT;
113 goto out;
114 }
115
116 /*
117 * If the no_shared_cr3 boot-arg is set (true), the kernel runs on
118 * its own pmap and cr3 rather than the user's -- so that wild accesses
119 * from kernel or kexts can be trapped. So, during copyin and copyout,
120 * we need to switch back to the user's map/cr3. The thread is flagged
121 * "CopyIOActive" at this time so that if the thread is pre-empted,
122 * we will later restore the correct cr3.
123 */
124 recursive_CopyIOActive = thread->machine.specFlags & CopyIOActive;
125 thread->machine.specFlags |= CopyIOActive;
126 if (no_shared_cr3) {
127 istate = ml_set_interrupts_enabled(FALSE);
128 if (get_cr3_base() != pmap->pm_cr3)
129 set_cr3_raw(pmap->pm_cr3);
130 }
131
132 /*
133 * Ensure that we're running on the target thread's cr3.
134 */
135 if ((pmap != kernel_pmap) && !use_kernel_map &&
136 (get_cr3_base() != pmap->pm_cr3)) {
137 panic("copyio(%d,%p,%p,%ld,%p,%d) cr3 is %p expects %p",
138 copy_type, (void *)user_addr, kernel_addr, nbytes, lencopied, use_kernel_map,
139 (void *) get_cr3_raw(), (void *) pmap->pm_cr3);
140 }
141 if (no_shared_cr3)
142 (void) ml_set_interrupts_enabled(istate);
143
144 KERNEL_DEBUG(0xeff70044 | DBG_FUNC_NONE, (unsigned)user_addr,
145 (unsigned)kernel_addr, nbytes, 0, 0);
146
147 switch (copy_type) {
148
149 case COPYIN:
150 error = _bcopy((const void *) user_addr,
151 kernel_addr,
152 nbytes);
153 break;
154
155 case COPYOUT:
156 error = _bcopy(kernel_addr,
157 (void *) user_addr,
158 nbytes);
159 break;
160
161 case COPYINPHYS:
162 error = _bcopy((const void *) user_addr,
163 PHYSMAP_PTOV(kernel_addr),
164 nbytes);
165 break;
166
167 case COPYOUTPHYS:
168 error = _bcopy((const void *) PHYSMAP_PTOV(kernel_addr),
169 (void *) user_addr,
170 nbytes);
171 break;
172
173 case COPYINSTR:
174 error = _bcopystr((const void *) user_addr,
175 kernel_addr,
176 (int) nbytes,
177 &bytes_copied);
178
179 /*
180 * lencopied should be updated on success
181 * or ENAMETOOLONG... but not EFAULT
182 */
183 if (error != EFAULT)
184 *lencopied = bytes_copied;
185
186 if (error) {
187 #if KDEBUG
188 nbytes = *lencopied;
189 #endif
190 break;
191 }
192 if (*(kernel_addr + bytes_copied - 1) == 0) {
193 /*
194 * we found a NULL terminator... we're done
195 */
196 #if KDEBUG
197 nbytes = *lencopied;
198 #endif
199 break;
200 } else {
201 /*
202 * no more room in the buffer and we haven't
203 * yet come across a NULL terminator
204 */
205 #if KDEBUG
206 nbytes = *lencopied;
207 #endif
208 error = ENAMETOOLONG;
209 break;
210 }
211 break;
212 }
213
214 if (!recursive_CopyIOActive) {
215 thread->machine.specFlags &= ~CopyIOActive;
216 }
217 if (no_shared_cr3) {
218 istate = ml_set_interrupts_enabled(FALSE);
219 if (get_cr3_raw() != kernel_pmap->pm_cr3)
220 set_cr3_raw(kernel_pmap->pm_cr3);
221 (void) ml_set_interrupts_enabled(istate);
222 }
223
224 out:
225 KERNEL_DEBUG(debug_type | DBG_FUNC_END, (unsigned)user_addr,
226 (unsigned)kernel_addr, (unsigned)nbytes, error, 0);
227
228 return (error);
229 }
230
231
232 static int
233 copyio_phys(addr64_t source, addr64_t sink, vm_size_t csize, int which)
234 {
235 char *paddr;
236 user_addr_t vaddr;
237 int ctype;
238
239 if (which & cppvPsnk) {
240 paddr = (char *)sink;
241 vaddr = (user_addr_t)source;
242 ctype = COPYINPHYS;
243 } else {
244 paddr = (char *)source;
245 vaddr = (user_addr_t)sink;
246 ctype = COPYOUTPHYS;
247 }
248 return copyio(ctype, vaddr, paddr, csize, NULL, which & cppvKmap);
249 }
250
251 int
252 copyinmsg(const user_addr_t user_addr, char *kernel_addr, mach_msg_size_t nbytes)
253 {
254 return copyio(COPYIN, user_addr, kernel_addr, nbytes, NULL, 0);
255 }
256
257 int
258 copyin(const user_addr_t user_addr, char *kernel_addr, vm_size_t nbytes)
259 {
260 return copyio(COPYIN, user_addr, kernel_addr, nbytes, NULL, 0);
261 }
262
263 int
264 copyinstr(const user_addr_t user_addr, char *kernel_addr, vm_size_t nbytes, vm_size_t *lencopied)
265 {
266 *lencopied = 0;
267
268 return copyio(COPYINSTR, user_addr, kernel_addr, nbytes, lencopied, 0);
269 }
270
271 int
272 copyoutmsg(const char *kernel_addr, user_addr_t user_addr, mach_msg_size_t nbytes)
273 {
274 return copyio(COPYOUT, user_addr, (char *)(uintptr_t)kernel_addr, nbytes, NULL, 0);
275 }
276
277 int
278 copyout(const void *kernel_addr, user_addr_t user_addr, vm_size_t nbytes)
279 {
280 return copyio(COPYOUT, user_addr, (char *)(uintptr_t)kernel_addr, nbytes, NULL, 0);
281 }
282
283
284 kern_return_t
285 copypv(addr64_t src64, addr64_t snk64, unsigned int size, int which)
286 {
287 unsigned int lop, csize;
288 int bothphys = 0;
289
290 KERNEL_DEBUG(0xeff7004c | DBG_FUNC_START, (unsigned)src64,
291 (unsigned)snk64, size, which, 0);
292
293 if ((which & (cppvPsrc | cppvPsnk)) == 0 ) /* Make sure that only one is virtual */
294 panic("copypv: no more than 1 parameter may be virtual\n"); /* Not allowed */
295
296 if ((which & (cppvPsrc | cppvPsnk)) == (cppvPsrc | cppvPsnk))
297 bothphys = 1; /* both are physical */
298
299 while (size) {
300
301 if (bothphys) {
302 lop = (unsigned int)(PAGE_SIZE - (snk64 & (PAGE_SIZE - 1))); /* Assume sink smallest */
303
304 if (lop > (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1))))
305 lop = (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1))); /* No, source is smaller */
306 } else {
307 /*
308 * only need to compute the resid for the physical page
309 * address... we don't care about where we start/finish in
310 * the virtual since we just call the normal copyin/copyout
311 */
312 if (which & cppvPsrc)
313 lop = (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1)));
314 else
315 lop = (unsigned int)(PAGE_SIZE - (snk64 & (PAGE_SIZE - 1)));
316 }
317 csize = size; /* Assume we can copy it all */
318 if (lop < size)
319 csize = lop; /* Nope, we can't do it all */
320 #if 0
321 /*
322 * flush_dcache64 is currently a nop on the i386...
323 * it's used when copying to non-system memory such
324 * as video capture cards... on PPC there was a need
325 * to flush due to how we mapped this memory... not
326 * sure if it's needed on i386.
327 */
328 if (which & cppvFsrc)
329 flush_dcache64(src64, csize, 1); /* If requested, flush source before move */
330 if (which & cppvFsnk)
331 flush_dcache64(snk64, csize, 1); /* If requested, flush sink before move */
332 #endif
333 if (bothphys)
334 bcopy_phys(src64, snk64, csize); /* Do a physical copy, virtually */
335 else {
336 if (copyio_phys(src64, snk64, csize, which))
337 return (KERN_FAILURE);
338 }
339 #if 0
340 if (which & cppvFsrc)
341 flush_dcache64(src64, csize, 1); /* If requested, flush source after move */
342 if (which & cppvFsnk)
343 flush_dcache64(snk64, csize, 1); /* If requested, flush sink after move */
344 #endif
345 size -= csize; /* Calculate what is left */
346 snk64 += csize; /* Bump sink to next physical address */
347 src64 += csize; /* Bump source to next physical address */
348 }
349 KERNEL_DEBUG(0xeff7004c | DBG_FUNC_END, (unsigned)src64,
350 (unsigned)snk64, size, which, 0);
351
352 return KERN_SUCCESS;
353 }