]> git.saurik.com Git - apple/xnu.git/blame - osfmk/x86_64/copyio.c
xnu-3789.70.16.tar.gz
[apple/xnu.git] / osfmk / x86_64 / copyio.c
CommitLineData
6d2010ae 1/*
39037602 2 * Copyright (c) 2009-2016 Apple Inc. All rights reserved.
6d2010ae
A
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
44static int copyio(int, user_addr_t, char *, vm_size_t, vm_size_t *, int);
45static int copyio_phys(addr64_t, addr64_t, vm_size_t, int);
46
3e170ce0
A
47/*
48 * Copy sizes bigger than this value will cause a kernel panic.
49 *
50 * Yes, this is an arbitrary fixed limit, but it's almost certainly
51 * a programming error to be copying more than this amount between
52 * user and wired kernel memory in a single invocation on this
53 * platform.
54 */
55#define COPYSIZELIMIT_PANIC (64*MB)
56
6d2010ae
A
57/*
58 * The copy engine has the following characteristics
59 * - copyio() handles copies to/from user or kernel space
60 * - copypv() deals with physical or virtual addresses
61 *
62 * Readers familiar with the 32-bit kernel will expect Joe's thesis at this
63 * point describing the full glory of the copy window implementation. In K64,
64 * however, there is no need for windowing. Thanks to the vast shared address
65 * space, the kernel has direct access to userspace and to physical memory.
66 *
67 * User virtual addresses are accessible provided the user's cr3 is loaded.
68 * Physical addresses are accessible via the direct map and the PHYSMAP_PTOV()
69 * translation.
70 *
71 * Copyin/out variants all boil done to just these 2 routines in locore.s which
72 * provide fault-recoverable copying:
73 */
74extern int _bcopy(const void *, void *, vm_size_t);
75extern int _bcopystr(const void *, void *, vm_size_t, vm_size_t *);
39037602 76extern int _copyin_word(const char *src, uint64_t *dst, vm_size_t len);
6d2010ae
A
77
78
79/*
80 * Types of copies:
81 */
82#define COPYIN 0 /* from user virtual to kernel virtual */
83#define COPYOUT 1 /* from kernel virtual to user virtual */
84#define COPYINSTR 2 /* string variant of copyout */
85#define COPYINPHYS 3 /* from user virtual to kernel physical */
86#define COPYOUTPHYS 4 /* from kernel physical to user virtual */
39037602 87#define COPYINWORD 5 /* from user virtual to kernel virtual */
6d2010ae 88
39037602 89#if ENABLE_SMAPLOG
04b8595b
A
90typedef struct {
91 uint64_t timestamp;
92 thread_t thread;
93 uintptr_t cr4;
94 uint8_t cpuid;
95 uint8_t smap_state;
96 uint8_t copyio_active;
97} smaplog_entry_t;
98
99#define SMAPLOG_BUFFER_SIZE (50)
100static smaplog_entry_t smaplog_cbuf[SMAPLOG_BUFFER_SIZE];
101static uint32_t smaplog_head = 0;
102
103static void
104smaplog_add_entry(boolean_t enabling)
105{
106 uint32_t index = 0;
107 thread_t thread = current_thread();
108
109 do {
110 index = smaplog_head;
111 } while (!OSCompareAndSwap(index, (index + 1) % SMAPLOG_BUFFER_SIZE, &smaplog_head));
112
113 assert(index < SMAPLOG_BUFFER_SIZE);
114 assert(smaplog_head < SMAPLOG_BUFFER_SIZE);
115 assert(thread);
116
117 smaplog_cbuf[index].timestamp = mach_absolute_time();
118 smaplog_cbuf[index].thread = thread;
119 smaplog_cbuf[index].cpuid = cpu_number();
120 smaplog_cbuf[index].cr4 = get_cr4();
121 smaplog_cbuf[index].smap_state = enabling;
122 smaplog_cbuf[index].copyio_active = (thread->machine.specFlags & CopyIOActive) ? 1 : 0;
123}
39037602 124#endif /* ENABLE_SMAPLOG */
04b8595b
A
125
126extern boolean_t pmap_smap_enabled;
127static inline void user_access_enable(void) {
128 if (pmap_smap_enabled) {
129 stac();
39037602 130#if ENABLE_SMAPLOG
04b8595b
A
131 smaplog_add_entry(TRUE);
132#endif
133 }
134}
135static inline void user_access_disable(void) {
136 if (pmap_smap_enabled) {
137 clac();
39037602 138#if ENABLE_SMAPLOG
04b8595b
A
139 smaplog_add_entry(FALSE);
140#endif
141 }
142}
a1c7dba1 143
39037602
A
144#if COPYIO_TRACE_ENABLED
145#define COPYIO_TRACE(x, a, b, c, d, e) KERNEL_DEBUG_CONSTANT(x, a, b, c, d, e)
146#else
147#define COPYIO_TRACE(x, a, b, c, d, e) do { } while(0)
148#endif
149
6d2010ae
A
150static int
151copyio(int copy_type, user_addr_t user_addr, char *kernel_addr,
152 vm_size_t nbytes, vm_size_t *lencopied, int use_kernel_map)
153{
39037602 154 thread_t thread = current_thread();
6d2010ae
A
155 pmap_t pmap;
156 vm_size_t bytes_copied;
157 int error = 0;
158 boolean_t istate = FALSE;
159 boolean_t recursive_CopyIOActive;
39037602 160#if COPYIO_TRACE_ENABLED
6d2010ae
A
161 int debug_type = 0xeff70010;
162 debug_type += (copy_type << 2);
163#endif
39037602 164 boolean_t nopagezero = thread->map->pmap->pagezero_accessible;
6d2010ae 165
3e170ce0
A
166 assert(nbytes < COPYSIZELIMIT_PANIC);
167
39037602
A
168 COPYIO_TRACE(debug_type | DBG_FUNC_START,
169 user_addr, kernel_addr, nbytes, use_kernel_map, 0);
6d2010ae 170
39037602 171 if (__improbable(nbytes == 0))
6d2010ae
A
172 goto out;
173
174 pmap = thread->map->pmap;
175
39037602 176 if (__improbable((copy_type != COPYINPHYS) && (copy_type != COPYOUTPHYS) && ((vm_offset_t)kernel_addr < VM_MIN_KERNEL_AND_KEXT_ADDRESS))) {
6d2010ae
A
177 panic("Invalid copy parameter, copy type: %d, kernel address: %p", copy_type, kernel_addr);
178 }
179
180 /* Sanity and security check for addresses to/from a user */
181
39037602
A
182 if (__improbable(((pmap != kernel_pmap) && (use_kernel_map == 0)) &&
183 ((nbytes && (user_addr+nbytes <= user_addr)) || ((user_addr + nbytes) > vm_map_max(thread->map))))) {
6d2010ae
A
184 error = EFAULT;
185 goto out;
186 }
187
188 /*
189 * If the no_shared_cr3 boot-arg is set (true), the kernel runs on
190 * its own pmap and cr3 rather than the user's -- so that wild accesses
191 * from kernel or kexts can be trapped. So, during copyin and copyout,
192 * we need to switch back to the user's map/cr3. The thread is flagged
193 * "CopyIOActive" at this time so that if the thread is pre-empted,
194 * we will later restore the correct cr3.
195 */
196 recursive_CopyIOActive = thread->machine.specFlags & CopyIOActive;
39037602
A
197
198 boolean_t pdswitch = no_shared_cr3 || nopagezero;
199
200 if (__improbable(pdswitch)) {
6d2010ae 201 istate = ml_set_interrupts_enabled(FALSE);
39037602
A
202 if (nopagezero && pmap_pcid_ncpus) {
203 pmap_pcid_activate(pmap, cpu_number(), TRUE, TRUE);
204 } else if (get_cr3_base() != pmap->pm_cr3) {
6d2010ae 205 set_cr3_raw(pmap->pm_cr3);
39037602
A
206 }
207 thread->machine.specFlags |= CopyIOActive;
208 } else {
209 thread->machine.specFlags |= CopyIOActive;
6d2010ae
A
210 }
211
39037602
A
212 user_access_enable();
213
214#if DEVELOPMENT || DEBUG
6d2010ae
A
215 /*
216 * Ensure that we're running on the target thread's cr3.
217 */
218 if ((pmap != kernel_pmap) && !use_kernel_map &&
219 (get_cr3_base() != pmap->pm_cr3)) {
220 panic("copyio(%d,%p,%p,%ld,%p,%d) cr3 is %p expects %p",
221 copy_type, (void *)user_addr, kernel_addr, nbytes, lencopied, use_kernel_map,
222 (void *) get_cr3_raw(), (void *) pmap->pm_cr3);
223 }
39037602
A
224#endif
225
226 if (__improbable(pdswitch)) {
6d2010ae 227 (void) ml_set_interrupts_enabled(istate);
39037602 228 }
6d2010ae 229
39037602
A
230 COPYIO_TRACE(0xeff70044 | DBG_FUNC_NONE, user_addr,
231 kernel_addr, nbytes, 0, 0);
6d2010ae
A
232
233 switch (copy_type) {
234
235 case COPYIN:
236 error = _bcopy((const void *) user_addr,
237 kernel_addr,
238 nbytes);
239 break;
240
241 case COPYOUT:
242 error = _bcopy(kernel_addr,
243 (void *) user_addr,
244 nbytes);
245 break;
246
247 case COPYINPHYS:
248 error = _bcopy((const void *) user_addr,
249 PHYSMAP_PTOV(kernel_addr),
250 nbytes);
251 break;
252
253 case COPYOUTPHYS:
254 error = _bcopy((const void *) PHYSMAP_PTOV(kernel_addr),
255 (void *) user_addr,
256 nbytes);
257 break;
258
39037602
A
259 case COPYINWORD:
260 error = _copyin_word((const void *) user_addr,
261 (void *) kernel_addr,
262 nbytes);
263 break;
264
6d2010ae
A
265 case COPYINSTR:
266 error = _bcopystr((const void *) user_addr,
267 kernel_addr,
268 (int) nbytes,
269 &bytes_copied);
270
271 /*
272 * lencopied should be updated on success
273 * or ENAMETOOLONG... but not EFAULT
274 */
275 if (error != EFAULT)
276 *lencopied = bytes_copied;
277
278 if (error) {
279#if KDEBUG
280 nbytes = *lencopied;
281#endif
282 break;
283 }
284 if (*(kernel_addr + bytes_copied - 1) == 0) {
285 /*
286 * we found a NULL terminator... we're done
287 */
288#if KDEBUG
289 nbytes = *lencopied;
290#endif
291 break;
292 } else {
293 /*
294 * no more room in the buffer and we haven't
295 * yet come across a NULL terminator
296 */
297#if KDEBUG
298 nbytes = *lencopied;
299#endif
300 error = ENAMETOOLONG;
301 break;
302 }
6d2010ae
A
303 }
304
04b8595b 305 user_access_disable();
39037602
A
306
307 if (__improbable(pdswitch)) {
6d2010ae 308 istate = ml_set_interrupts_enabled(FALSE);
39037602
A
309 if (!recursive_CopyIOActive && (get_cr3_raw() != kernel_pmap->pm_cr3)) {
310 if (nopagezero && pmap_pcid_ncpus) {
311 pmap_pcid_activate(pmap, cpu_number(), TRUE, FALSE);
312 } else {
313 set_cr3_raw(kernel_pmap->pm_cr3);
314 }
315 }
316
317 if (!recursive_CopyIOActive) {
318 thread->machine.specFlags &= ~CopyIOActive;
319 }
6d2010ae 320 (void) ml_set_interrupts_enabled(istate);
39037602
A
321 } else if (!recursive_CopyIOActive) {
322 thread->machine.specFlags &= ~CopyIOActive;
6d2010ae
A
323 }
324
325out:
39037602 326 COPYIO_TRACE(debug_type | DBG_FUNC_END, user_addr, kernel_addr, nbytes, error, 0);
6d2010ae
A
327
328 return (error);
329}
330
331
332static int
333copyio_phys(addr64_t source, addr64_t sink, vm_size_t csize, int which)
334{
335 char *paddr;
336 user_addr_t vaddr;
337 int ctype;
338
339 if (which & cppvPsnk) {
340 paddr = (char *)sink;
341 vaddr = (user_addr_t)source;
342 ctype = COPYINPHYS;
343 } else {
344 paddr = (char *)source;
345 vaddr = (user_addr_t)sink;
346 ctype = COPYOUTPHYS;
347 }
348 return copyio(ctype, vaddr, paddr, csize, NULL, which & cppvKmap);
349}
350
351int
352copyinmsg(const user_addr_t user_addr, char *kernel_addr, mach_msg_size_t nbytes)
353{
354 return copyio(COPYIN, user_addr, kernel_addr, nbytes, NULL, 0);
355}
356
357int
358copyin(const user_addr_t user_addr, char *kernel_addr, vm_size_t nbytes)
359{
360 return copyio(COPYIN, user_addr, kernel_addr, nbytes, NULL, 0);
361}
362
39037602
A
363/*
364 * copyin_word
365 * Read an aligned value from userspace as a single memory transaction.
366 * This function supports userspace synchronization features
367 */
368int
369copyin_word(const user_addr_t user_addr, uint64_t *kernel_addr, vm_size_t nbytes)
370{
371 /* Verify sizes */
372 if ((nbytes != 4) && (nbytes != 8))
373 return EINVAL;
374
375 /* Test alignment */
376 if (user_addr & (nbytes - 1))
377 return EINVAL;
378 return copyio(COPYINWORD, user_addr, (char *)(uintptr_t)kernel_addr, nbytes, NULL, 0);
379}
380
6d2010ae
A
381int
382copyinstr(const user_addr_t user_addr, char *kernel_addr, vm_size_t nbytes, vm_size_t *lencopied)
383{
384 *lencopied = 0;
385
386 return copyio(COPYINSTR, user_addr, kernel_addr, nbytes, lencopied, 0);
387}
388
389int
390copyoutmsg(const char *kernel_addr, user_addr_t user_addr, mach_msg_size_t nbytes)
391{
392 return copyio(COPYOUT, user_addr, (char *)(uintptr_t)kernel_addr, nbytes, NULL, 0);
393}
394
395int
396copyout(const void *kernel_addr, user_addr_t user_addr, vm_size_t nbytes)
397{
398 return copyio(COPYOUT, user_addr, (char *)(uintptr_t)kernel_addr, nbytes, NULL, 0);
399}
400
401
402kern_return_t
403copypv(addr64_t src64, addr64_t snk64, unsigned int size, int which)
404{
405 unsigned int lop, csize;
406 int bothphys = 0;
407
408 KERNEL_DEBUG(0xeff7004c | DBG_FUNC_START, (unsigned)src64,
409 (unsigned)snk64, size, which, 0);
410
411 if ((which & (cppvPsrc | cppvPsnk)) == 0 ) /* Make sure that only one is virtual */
412 panic("copypv: no more than 1 parameter may be virtual\n"); /* Not allowed */
413
414 if ((which & (cppvPsrc | cppvPsnk)) == (cppvPsrc | cppvPsnk))
415 bothphys = 1; /* both are physical */
416
417 while (size) {
418
419 if (bothphys) {
420 lop = (unsigned int)(PAGE_SIZE - (snk64 & (PAGE_SIZE - 1))); /* Assume sink smallest */
421
422 if (lop > (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1))))
423 lop = (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1))); /* No, source is smaller */
424 } else {
425 /*
426 * only need to compute the resid for the physical page
427 * address... we don't care about where we start/finish in
428 * the virtual since we just call the normal copyin/copyout
429 */
430 if (which & cppvPsrc)
431 lop = (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1)));
432 else
433 lop = (unsigned int)(PAGE_SIZE - (snk64 & (PAGE_SIZE - 1)));
434 }
435 csize = size; /* Assume we can copy it all */
436 if (lop < size)
437 csize = lop; /* Nope, we can't do it all */
438#if 0
439 /*
440 * flush_dcache64 is currently a nop on the i386...
441 * it's used when copying to non-system memory such
442 * as video capture cards... on PPC there was a need
443 * to flush due to how we mapped this memory... not
444 * sure if it's needed on i386.
445 */
446 if (which & cppvFsrc)
447 flush_dcache64(src64, csize, 1); /* If requested, flush source before move */
448 if (which & cppvFsnk)
449 flush_dcache64(snk64, csize, 1); /* If requested, flush sink before move */
450#endif
451 if (bothphys)
452 bcopy_phys(src64, snk64, csize); /* Do a physical copy, virtually */
453 else {
454 if (copyio_phys(src64, snk64, csize, which))
455 return (KERN_FAILURE);
456 }
457#if 0
458 if (which & cppvFsrc)
459 flush_dcache64(src64, csize, 1); /* If requested, flush source after move */
460 if (which & cppvFsnk)
461 flush_dcache64(snk64, csize, 1); /* If requested, flush sink after move */
462#endif
463 size -= csize; /* Calculate what is left */
464 snk64 += csize; /* Bump sink to next physical address */
465 src64 += csize; /* Bump source to next physical address */
466 }
467 KERNEL_DEBUG(0xeff7004c | DBG_FUNC_END, (unsigned)src64,
468 (unsigned)snk64, size, which, 0);
469
470 return KERN_SUCCESS;
471}