]>
Commit | Line | Data |
---|---|---|
6d2010ae | 1 | /* |
39037602 | 2 | * Copyright (c) 2009-2016 Apple Inc. All rights reserved. |
6d2010ae A |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
0a7de745 | 5 | * |
6d2010ae A |
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. | |
0a7de745 | 14 | * |
6d2010ae A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
6d2010ae A |
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. | |
0a7de745 | 25 | * |
6d2010ae A |
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> | |
cb323159 | 34 | #include <i386/machine_cpu.h> |
6d2010ae A |
35 | #include <i386/machine_routines.h> |
36 | #include <i386/cpuid.h> | |
37 | #include <i386/vmx.h> | |
38 | #include <vm/pmap.h> | |
39 | #include <vm/vm_map.h> | |
40 | #include <vm/vm_kern.h> | |
41 | #include <vm/vm_fault.h> | |
5ba3f43e | 42 | #include <san/kasan.h> |
6d2010ae A |
43 | |
44 | #include <sys/kdebug.h> | |
45 | ||
5ba3f43e A |
46 | #include <kern/copyout_shim.h> |
47 | ||
d9a64523 A |
48 | #undef copyin |
49 | #undef copyout | |
5ba3f43e | 50 | |
6d2010ae A |
51 | static int copyio(int, user_addr_t, char *, vm_size_t, vm_size_t *, int); |
52 | static int copyio_phys(addr64_t, addr64_t, vm_size_t, int); | |
53 | ||
3e170ce0 A |
54 | /* |
55 | * Copy sizes bigger than this value will cause a kernel panic. | |
56 | * | |
57 | * Yes, this is an arbitrary fixed limit, but it's almost certainly | |
58 | * a programming error to be copying more than this amount between | |
59 | * user and wired kernel memory in a single invocation on this | |
60 | * platform. | |
61 | */ | |
5ba3f43e | 62 | const int copysize_limit_panic = (64 * MB); |
3e170ce0 | 63 | |
6d2010ae A |
64 | /* |
65 | * The copy engine has the following characteristics | |
66 | * - copyio() handles copies to/from user or kernel space | |
67 | * - copypv() deals with physical or virtual addresses | |
68 | * | |
69 | * Readers familiar with the 32-bit kernel will expect Joe's thesis at this | |
70 | * point describing the full glory of the copy window implementation. In K64, | |
71 | * however, there is no need for windowing. Thanks to the vast shared address | |
72 | * space, the kernel has direct access to userspace and to physical memory. | |
73 | * | |
74 | * User virtual addresses are accessible provided the user's cr3 is loaded. | |
75 | * Physical addresses are accessible via the direct map and the PHYSMAP_PTOV() | |
76 | * translation. | |
77 | * | |
78 | * Copyin/out variants all boil done to just these 2 routines in locore.s which | |
79 | * provide fault-recoverable copying: | |
80 | */ | |
81 | extern int _bcopy(const void *, void *, vm_size_t); | |
82 | extern int _bcopystr(const void *, void *, vm_size_t, vm_size_t *); | |
cb323159 A |
83 | extern int _copyin_atomic32(const char *src, uint32_t *dst); |
84 | extern int _copyin_atomic64(const char *src, uint64_t *dst); | |
85 | extern int _copyout_atomic32(const uint32_t *u32, char *src); | |
86 | extern int _copyout_atomic64(const uint64_t *u64, char *src); | |
6d2010ae | 87 | |
d9a64523 A |
88 | /* On by default, optionally disabled by boot-arg */ |
89 | extern boolean_t copyio_zalloc_check; | |
6d2010ae A |
90 | |
91 | /* | |
92 | * Types of copies: | |
93 | */ | |
0a7de745 A |
94 | #define COPYIN 0 /* from user virtual to kernel virtual */ |
95 | #define COPYOUT 1 /* from kernel virtual to user virtual */ | |
96 | #define COPYINSTR 2 /* string variant of copyout */ | |
97 | #define COPYINPHYS 3 /* from user virtual to kernel physical */ | |
98 | #define COPYOUTPHYS 4 /* from kernel physical to user virtual */ | |
cb323159 A |
99 | #define COPYINATOMIC32 5 /* from user virtual to kernel virtual */ |
100 | #define COPYINATOMIC64 6 /* from user virtual to kernel virtual */ | |
101 | #define COPYOUTATOMIC32 7 /* from user virtual to kernel virtual */ | |
102 | #define COPYOUTATOMIC64 8 /* from user virtual to kernel virtual */ | |
6d2010ae | 103 | |
39037602 | 104 | #if ENABLE_SMAPLOG |
04b8595b | 105 | typedef struct { |
0a7de745 A |
106 | uint64_t timestamp; |
107 | thread_t thread; | |
108 | uintptr_t cr4; | |
109 | uint8_t cpuid; | |
110 | uint8_t smap_state; | |
111 | uint8_t copyio_active; | |
04b8595b A |
112 | } smaplog_entry_t; |
113 | ||
114 | #define SMAPLOG_BUFFER_SIZE (50) | |
0a7de745 A |
115 | static smaplog_entry_t smaplog_cbuf[SMAPLOG_BUFFER_SIZE]; |
116 | static uint32_t smaplog_head = 0; | |
04b8595b A |
117 | |
118 | static void | |
119 | smaplog_add_entry(boolean_t enabling) | |
120 | { | |
121 | uint32_t index = 0; | |
122 | thread_t thread = current_thread(); | |
123 | ||
124 | do { | |
125 | index = smaplog_head; | |
126 | } while (!OSCompareAndSwap(index, (index + 1) % SMAPLOG_BUFFER_SIZE, &smaplog_head)); | |
127 | ||
128 | assert(index < SMAPLOG_BUFFER_SIZE); | |
129 | assert(smaplog_head < SMAPLOG_BUFFER_SIZE); | |
130 | assert(thread); | |
131 | ||
132 | smaplog_cbuf[index].timestamp = mach_absolute_time(); | |
133 | smaplog_cbuf[index].thread = thread; | |
134 | smaplog_cbuf[index].cpuid = cpu_number(); | |
135 | smaplog_cbuf[index].cr4 = get_cr4(); | |
136 | smaplog_cbuf[index].smap_state = enabling; | |
137 | smaplog_cbuf[index].copyio_active = (thread->machine.specFlags & CopyIOActive) ? 1 : 0; | |
138 | } | |
39037602 | 139 | #endif /* ENABLE_SMAPLOG */ |
04b8595b A |
140 | |
141 | extern boolean_t pmap_smap_enabled; | |
0a7de745 A |
142 | static inline void |
143 | user_access_enable(void) | |
144 | { | |
04b8595b A |
145 | if (pmap_smap_enabled) { |
146 | stac(); | |
39037602 | 147 | #if ENABLE_SMAPLOG |
04b8595b A |
148 | smaplog_add_entry(TRUE); |
149 | #endif | |
150 | } | |
151 | } | |
0a7de745 A |
152 | static inline void |
153 | user_access_disable(void) | |
154 | { | |
04b8595b A |
155 | if (pmap_smap_enabled) { |
156 | clac(); | |
39037602 | 157 | #if ENABLE_SMAPLOG |
04b8595b A |
158 | smaplog_add_entry(FALSE); |
159 | #endif | |
160 | } | |
161 | } | |
a1c7dba1 | 162 | |
39037602 A |
163 | #if COPYIO_TRACE_ENABLED |
164 | #define COPYIO_TRACE(x, a, b, c, d, e) KERNEL_DEBUG_CONSTANT(x, a, b, c, d, e) | |
165 | #else | |
166 | #define COPYIO_TRACE(x, a, b, c, d, e) do { } while(0) | |
167 | #endif | |
168 | ||
6d2010ae A |
169 | static int |
170 | copyio(int copy_type, user_addr_t user_addr, char *kernel_addr, | |
0a7de745 | 171 | vm_size_t nbytes, vm_size_t *lencopied, int use_kernel_map) |
6d2010ae | 172 | { |
0a7de745 A |
173 | thread_t thread = current_thread(); |
174 | pmap_t pmap; | |
175 | vm_size_t bytes_copied; | |
176 | int error = 0; | |
177 | boolean_t istate = FALSE; | |
178 | boolean_t recursive_CopyIOActive; | |
179 | #if COPYIO_TRACE_ENABLED | |
180 | int debug_type = 0xeff70010; | |
6d2010ae A |
181 | debug_type += (copy_type << 2); |
182 | #endif | |
d9a64523 | 183 | vm_size_t kernel_buf_size = 0; |
6d2010ae | 184 | |
0a7de745 | 185 | if (__improbable(nbytes > copysize_limit_panic)) { |
5ba3f43e | 186 | panic("%s(%p, %p, %lu) - transfer too large", __func__, |
0a7de745 A |
187 | (void *)user_addr, (void *)kernel_addr, nbytes); |
188 | } | |
3e170ce0 | 189 | |
39037602 A |
190 | COPYIO_TRACE(debug_type | DBG_FUNC_START, |
191 | user_addr, kernel_addr, nbytes, use_kernel_map, 0); | |
6d2010ae | 192 | |
0a7de745 | 193 | if (__improbable(nbytes == 0)) { |
6d2010ae | 194 | goto out; |
0a7de745 | 195 | } |
6d2010ae | 196 | |
d9a64523 A |
197 | pmap = thread->map->pmap; |
198 | boolean_t nopagezero = thread->map->pmap->pagezero_accessible; | |
6d2010ae | 199 | |
d9a64523 | 200 | if ((copy_type != COPYINPHYS) && (copy_type != COPYOUTPHYS)) { |
0a7de745 | 201 | if (__improbable((vm_offset_t)kernel_addr < VM_MIN_KERNEL_AND_KEXT_ADDRESS)) { |
d9a64523 | 202 | panic("Invalid copy parameter, copy type: %d, kernel address: %p", copy_type, kernel_addr); |
0a7de745 | 203 | } |
d9a64523 A |
204 | if (__probable(copyio_zalloc_check)) { |
205 | kernel_buf_size = zone_element_size(kernel_addr, NULL); | |
0a7de745 | 206 | if (__improbable(kernel_buf_size && kernel_buf_size < nbytes)) { |
d9a64523 | 207 | panic("copyio: kernel buffer %p has size %lu < nbytes %lu", kernel_addr, kernel_buf_size, nbytes); |
0a7de745 | 208 | } |
d9a64523 | 209 | } |
6d2010ae | 210 | } |
0a7de745 | 211 | |
6d2010ae A |
212 | /* Sanity and security check for addresses to/from a user */ |
213 | ||
39037602 | 214 | if (__improbable(((pmap != kernel_pmap) && (use_kernel_map == 0)) && |
0a7de745 | 215 | ((nbytes && (user_addr + nbytes <= user_addr)) || ((user_addr + nbytes) > vm_map_max(thread->map))))) { |
6d2010ae A |
216 | error = EFAULT; |
217 | goto out; | |
218 | } | |
219 | ||
cb323159 A |
220 | if (copy_type >= COPYINATOMIC32 && copy_type <= COPYOUTATOMIC64) { |
221 | if (__improbable(pmap == kernel_pmap)) { | |
222 | error = EFAULT; | |
223 | goto out; | |
224 | } | |
225 | } | |
226 | ||
5ba3f43e | 227 | #if KASAN |
cb323159 A |
228 | switch (copy_type) { |
229 | case COPYIN: | |
230 | case COPYINSTR: | |
231 | case COPYINATOMIC32: | |
232 | case COPYINATOMIC64: | |
5ba3f43e | 233 | __asan_storeN((uptr)kernel_addr, nbytes); |
cb323159 A |
234 | break; |
235 | case COPYOUT: | |
236 | case COPYOUTATOMIC32: | |
237 | case COPYOUTATOMIC64: | |
5ba3f43e | 238 | __asan_loadN((uptr)kernel_addr, nbytes); |
cb323159 A |
239 | kasan_check_uninitialized((vm_address_t)kernel_addr, nbytes); |
240 | break; | |
5ba3f43e A |
241 | } |
242 | #endif | |
243 | ||
6d2010ae | 244 | /* |
0a7de745 | 245 | * If the no_shared_cr3 boot-arg is set (true), the kernel runs on |
6d2010ae A |
246 | * its own pmap and cr3 rather than the user's -- so that wild accesses |
247 | * from kernel or kexts can be trapped. So, during copyin and copyout, | |
248 | * we need to switch back to the user's map/cr3. The thread is flagged | |
249 | * "CopyIOActive" at this time so that if the thread is pre-empted, | |
250 | * we will later restore the correct cr3. | |
251 | */ | |
252 | recursive_CopyIOActive = thread->machine.specFlags & CopyIOActive; | |
39037602 A |
253 | |
254 | boolean_t pdswitch = no_shared_cr3 || nopagezero; | |
255 | ||
256 | if (__improbable(pdswitch)) { | |
6d2010ae | 257 | istate = ml_set_interrupts_enabled(FALSE); |
39037602 A |
258 | if (nopagezero && pmap_pcid_ncpus) { |
259 | pmap_pcid_activate(pmap, cpu_number(), TRUE, TRUE); | |
260 | } else if (get_cr3_base() != pmap->pm_cr3) { | |
6d2010ae | 261 | set_cr3_raw(pmap->pm_cr3); |
39037602 A |
262 | } |
263 | thread->machine.specFlags |= CopyIOActive; | |
264 | } else { | |
265 | thread->machine.specFlags |= CopyIOActive; | |
6d2010ae A |
266 | } |
267 | ||
39037602 A |
268 | user_access_enable(); |
269 | ||
0a7de745 | 270 | #if DEVELOPMENT || DEBUG |
6d2010ae A |
271 | /* |
272 | * Ensure that we're running on the target thread's cr3. | |
273 | */ | |
274 | if ((pmap != kernel_pmap) && !use_kernel_map && | |
275 | (get_cr3_base() != pmap->pm_cr3)) { | |
276 | panic("copyio(%d,%p,%p,%ld,%p,%d) cr3 is %p expects %p", | |
0a7de745 A |
277 | copy_type, (void *)user_addr, kernel_addr, nbytes, lencopied, use_kernel_map, |
278 | (void *) get_cr3_raw(), (void *) pmap->pm_cr3); | |
6d2010ae | 279 | } |
39037602 A |
280 | #endif |
281 | ||
282 | if (__improbable(pdswitch)) { | |
6d2010ae | 283 | (void) ml_set_interrupts_enabled(istate); |
39037602 | 284 | } |
6d2010ae | 285 | |
39037602 | 286 | COPYIO_TRACE(0xeff70044 | DBG_FUNC_NONE, user_addr, |
0a7de745 | 287 | kernel_addr, nbytes, 0, 0); |
6d2010ae | 288 | |
0a7de745 | 289 | switch (copy_type) { |
6d2010ae | 290 | case COPYIN: |
0a7de745 A |
291 | error = _bcopy((const void *) user_addr, |
292 | kernel_addr, | |
293 | nbytes); | |
6d2010ae | 294 | break; |
0a7de745 | 295 | |
6d2010ae | 296 | case COPYOUT: |
0a7de745 A |
297 | error = _bcopy(kernel_addr, |
298 | (void *) user_addr, | |
299 | nbytes); | |
6d2010ae A |
300 | break; |
301 | ||
302 | case COPYINPHYS: | |
0a7de745 A |
303 | error = _bcopy((const void *) user_addr, |
304 | PHYSMAP_PTOV(kernel_addr), | |
305 | nbytes); | |
6d2010ae A |
306 | break; |
307 | ||
308 | case COPYOUTPHYS: | |
0a7de745 A |
309 | error = _bcopy((const void *) PHYSMAP_PTOV(kernel_addr), |
310 | (void *) user_addr, | |
311 | nbytes); | |
6d2010ae A |
312 | break; |
313 | ||
cb323159 A |
314 | case COPYINATOMIC32: |
315 | error = _copyin_atomic32((const void *) user_addr, | |
316 | (void *) kernel_addr); | |
317 | break; | |
318 | ||
319 | case COPYINATOMIC64: | |
320 | error = _copyin_atomic64((const void *) user_addr, | |
321 | (void *) kernel_addr); | |
322 | break; | |
323 | ||
324 | case COPYOUTATOMIC32: | |
325 | error = _copyout_atomic32((const void *) kernel_addr, | |
326 | (void *) user_addr); | |
327 | break; | |
328 | ||
329 | case COPYOUTATOMIC64: | |
330 | error = _copyout_atomic64((const void *) kernel_addr, | |
331 | (void *) user_addr); | |
39037602 A |
332 | break; |
333 | ||
6d2010ae | 334 | case COPYINSTR: |
0a7de745 A |
335 | error = _bcopystr((const void *) user_addr, |
336 | kernel_addr, | |
337 | (int) nbytes, | |
338 | &bytes_copied); | |
6d2010ae A |
339 | |
340 | /* | |
341 | * lencopied should be updated on success | |
342 | * or ENAMETOOLONG... but not EFAULT | |
343 | */ | |
0a7de745 A |
344 | if (error != EFAULT) { |
345 | *lencopied = bytes_copied; | |
346 | } | |
6d2010ae A |
347 | |
348 | if (error) { | |
349 | #if KDEBUG | |
0a7de745 | 350 | nbytes = *lencopied; |
6d2010ae | 351 | #endif |
0a7de745 | 352 | break; |
6d2010ae A |
353 | } |
354 | if (*(kernel_addr + bytes_copied - 1) == 0) { | |
0a7de745 | 355 | /* |
6d2010ae A |
356 | * we found a NULL terminator... we're done |
357 | */ | |
358 | #if KDEBUG | |
0a7de745 | 359 | nbytes = *lencopied; |
6d2010ae A |
360 | #endif |
361 | break; | |
362 | } else { | |
0a7de745 | 363 | /* |
6d2010ae A |
364 | * no more room in the buffer and we haven't |
365 | * yet come across a NULL terminator | |
366 | */ | |
367 | #if KDEBUG | |
0a7de745 | 368 | nbytes = *lencopied; |
6d2010ae | 369 | #endif |
0a7de745 | 370 | error = ENAMETOOLONG; |
6d2010ae A |
371 | break; |
372 | } | |
6d2010ae A |
373 | } |
374 | ||
04b8595b | 375 | user_access_disable(); |
39037602 A |
376 | |
377 | if (__improbable(pdswitch)) { | |
6d2010ae | 378 | istate = ml_set_interrupts_enabled(FALSE); |
0a7de745 | 379 | if (!recursive_CopyIOActive && (get_cr3_raw() != kernel_pmap->pm_cr3)) { |
39037602 A |
380 | if (nopagezero && pmap_pcid_ncpus) { |
381 | pmap_pcid_activate(pmap, cpu_number(), TRUE, FALSE); | |
382 | } else { | |
383 | set_cr3_raw(kernel_pmap->pm_cr3); | |
384 | } | |
385 | } | |
386 | ||
387 | if (!recursive_CopyIOActive) { | |
388 | thread->machine.specFlags &= ~CopyIOActive; | |
389 | } | |
6d2010ae | 390 | (void) ml_set_interrupts_enabled(istate); |
39037602 A |
391 | } else if (!recursive_CopyIOActive) { |
392 | thread->machine.specFlags &= ~CopyIOActive; | |
6d2010ae A |
393 | } |
394 | ||
395 | out: | |
39037602 | 396 | COPYIO_TRACE(debug_type | DBG_FUNC_END, user_addr, kernel_addr, nbytes, error, 0); |
6d2010ae | 397 | |
0a7de745 | 398 | return error; |
6d2010ae A |
399 | } |
400 | ||
401 | ||
402 | static int | |
403 | copyio_phys(addr64_t source, addr64_t sink, vm_size_t csize, int which) | |
404 | { | |
0a7de745 | 405 | char *paddr; |
6d2010ae A |
406 | user_addr_t vaddr; |
407 | int ctype; | |
408 | ||
409 | if (which & cppvPsnk) { | |
410 | paddr = (char *)sink; | |
0a7de745 | 411 | vaddr = (user_addr_t)source; |
6d2010ae A |
412 | ctype = COPYINPHYS; |
413 | } else { | |
0a7de745 | 414 | paddr = (char *)source; |
6d2010ae A |
415 | vaddr = (user_addr_t)sink; |
416 | ctype = COPYOUTPHYS; | |
0a7de745 | 417 | CALL_COPYOUT_SHIM_PHYS((void *)PHYSMAP_PTOV(source), sink, csize) |
6d2010ae A |
418 | } |
419 | return copyio(ctype, vaddr, paddr, csize, NULL, which & cppvKmap); | |
420 | } | |
421 | ||
422 | int | |
423 | copyinmsg(const user_addr_t user_addr, char *kernel_addr, mach_msg_size_t nbytes) | |
424 | { | |
0a7de745 A |
425 | return copyio(COPYIN, user_addr, kernel_addr, nbytes, NULL, 0); |
426 | } | |
6d2010ae A |
427 | |
428 | int | |
d9a64523 | 429 | copyin(const user_addr_t user_addr, void *kernel_addr, vm_size_t nbytes) |
6d2010ae | 430 | { |
0a7de745 | 431 | return copyio(COPYIN, user_addr, kernel_addr, nbytes, NULL, 0); |
6d2010ae A |
432 | } |
433 | ||
39037602 | 434 | /* |
cb323159 A |
435 | * copy{in,out}_atomic{32,64} |
436 | * Read or store an aligned value from userspace as a single memory transaction. | |
437 | * These functions support userspace synchronization features | |
39037602 A |
438 | */ |
439 | int | |
cb323159 A |
440 | copyin_atomic32(const user_addr_t user_addr, uint32_t *kernel_addr) |
441 | { | |
442 | /* Test alignment */ | |
443 | if (user_addr & 3) { | |
444 | return EINVAL; | |
445 | } | |
446 | return copyio(COPYINATOMIC32, user_addr, (char *)(uintptr_t)kernel_addr, 4, NULL, 0); | |
447 | } | |
448 | ||
449 | int | |
450 | copyin_atomic32_wait_if_equals(const user_addr_t user_addr, uint32_t value) | |
451 | { | |
452 | uint32_t u32; | |
453 | int result = copyin_atomic32(user_addr, &u32); | |
454 | if (__improbable(result)) { | |
455 | return result; | |
456 | } | |
457 | if (u32 != value) { | |
458 | return ESTALE; | |
459 | } | |
460 | cpu_pause(); | |
461 | return 0; | |
462 | } | |
463 | ||
464 | int | |
465 | copyin_atomic64(const user_addr_t user_addr, uint64_t *kernel_addr) | |
466 | { | |
467 | /* Test alignment */ | |
468 | if (user_addr & 7) { | |
469 | return EINVAL; | |
470 | } | |
471 | return copyio(COPYINATOMIC64, user_addr, (char *)(uintptr_t)kernel_addr, 8, NULL, 0); | |
472 | } | |
473 | ||
474 | int | |
475 | copyout_atomic32(uint32_t value, user_addr_t user_addr) | |
39037602 | 476 | { |
cb323159 A |
477 | /* Test alignment */ |
478 | if (user_addr & 3) { | |
39037602 | 479 | return EINVAL; |
0a7de745 | 480 | } |
cb323159 A |
481 | return copyio(COPYOUTATOMIC32, user_addr, (char *)&value, 4, NULL, 0); |
482 | } | |
39037602 | 483 | |
cb323159 A |
484 | int |
485 | copyout_atomic64(uint64_t value, user_addr_t user_addr) | |
486 | { | |
39037602 | 487 | /* Test alignment */ |
cb323159 | 488 | if (user_addr & 7) { |
39037602 | 489 | return EINVAL; |
0a7de745 | 490 | } |
cb323159 | 491 | return copyio(COPYOUTATOMIC64, user_addr, (char *)&value, 8, NULL, 0); |
39037602 A |
492 | } |
493 | ||
6d2010ae | 494 | int |
0a7de745 | 495 | copyinstr(const user_addr_t user_addr, char *kernel_addr, vm_size_t nbytes, vm_size_t *lencopied) |
6d2010ae | 496 | { |
0a7de745 | 497 | *lencopied = 0; |
6d2010ae | 498 | |
0a7de745 | 499 | return copyio(COPYINSTR, user_addr, kernel_addr, nbytes, lencopied, 0); |
6d2010ae A |
500 | } |
501 | ||
502 | int | |
503 | copyoutmsg(const char *kernel_addr, user_addr_t user_addr, mach_msg_size_t nbytes) | |
504 | { | |
0a7de745 A |
505 | CALL_COPYOUT_SHIM_MSG(kernel_addr, user_addr, (vm_size_t)nbytes) |
506 | return copyio(COPYOUT, user_addr, (char *)(uintptr_t)kernel_addr, nbytes, NULL, 0); | |
6d2010ae A |
507 | } |
508 | ||
509 | int | |
510 | copyout(const void *kernel_addr, user_addr_t user_addr, vm_size_t nbytes) | |
511 | { | |
0a7de745 A |
512 | CALL_COPYOUT_SHIM_NRML(kernel_addr, user_addr, nbytes) |
513 | return copyio(COPYOUT, user_addr, (char *)(uintptr_t)kernel_addr, nbytes, NULL, 0); | |
6d2010ae A |
514 | } |
515 | ||
516 | ||
517 | kern_return_t | |
518 | copypv(addr64_t src64, addr64_t snk64, unsigned int size, int which) | |
519 | { | |
520 | unsigned int lop, csize; | |
521 | int bothphys = 0; | |
6d2010ae | 522 | |
0a7de745 A |
523 | KERNEL_DEBUG(0xeff7004c | DBG_FUNC_START, (unsigned)src64, |
524 | (unsigned)snk64, size, which, 0); | |
6d2010ae | 525 | |
0a7de745 A |
526 | if ((which & (cppvPsrc | cppvPsnk)) == 0) { /* Make sure that only one is virtual */ |
527 | panic("copypv: no more than 1 parameter may be virtual\n"); /* Not allowed */ | |
528 | } | |
529 | if ((which & (cppvPsrc | cppvPsnk)) == (cppvPsrc | cppvPsnk)) { | |
530 | bothphys = 1; /* both are physical */ | |
531 | } | |
6d2010ae | 532 | while (size) { |
0a7de745 A |
533 | if (bothphys) { |
534 | lop = (unsigned int)(PAGE_SIZE - (snk64 & (PAGE_SIZE - 1))); /* Assume sink smallest */ | |
6d2010ae | 535 | |
0a7de745 A |
536 | if (lop > (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1)))) { |
537 | lop = (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1))); /* No, source is smaller */ | |
538 | } | |
6d2010ae | 539 | } else { |
0a7de745 | 540 | /* |
6d2010ae A |
541 | * only need to compute the resid for the physical page |
542 | * address... we don't care about where we start/finish in | |
543 | * the virtual since we just call the normal copyin/copyout | |
544 | */ | |
0a7de745 A |
545 | if (which & cppvPsrc) { |
546 | lop = (unsigned int)(PAGE_SIZE - (src64 & (PAGE_SIZE - 1))); | |
547 | } else { | |
548 | lop = (unsigned int)(PAGE_SIZE - (snk64 & (PAGE_SIZE - 1))); | |
549 | } | |
6d2010ae | 550 | } |
0a7de745 A |
551 | csize = size; /* Assume we can copy it all */ |
552 | if (lop < size) { | |
553 | csize = lop; /* Nope, we can't do it all */ | |
554 | } | |
555 | #if 0 | |
6d2010ae | 556 | /* |
0a7de745 | 557 | * flush_dcache64 is currently a nop on the i386... |
6d2010ae A |
558 | * it's used when copying to non-system memory such |
559 | * as video capture cards... on PPC there was a need | |
560 | * to flush due to how we mapped this memory... not | |
561 | * sure if it's needed on i386. | |
562 | */ | |
0a7de745 A |
563 | if (which & cppvFsrc) { |
564 | flush_dcache64(src64, csize, 1); /* If requested, flush source before move */ | |
565 | } | |
566 | if (which & cppvFsnk) { | |
567 | flush_dcache64(snk64, csize, 1); /* If requested, flush sink before move */ | |
568 | } | |
6d2010ae | 569 | #endif |
0a7de745 A |
570 | if (bothphys) { |
571 | bcopy_phys(src64, snk64, csize); /* Do a physical copy, virtually */ | |
572 | } else { | |
573 | if (copyio_phys(src64, snk64, csize, which)) { | |
574 | return KERN_FAILURE; | |
575 | } | |
6d2010ae A |
576 | } |
577 | #if 0 | |
0a7de745 A |
578 | if (which & cppvFsrc) { |
579 | flush_dcache64(src64, csize, 1); /* If requested, flush source after move */ | |
580 | } | |
581 | if (which & cppvFsnk) { | |
582 | flush_dcache64(snk64, csize, 1); /* If requested, flush sink after move */ | |
583 | } | |
6d2010ae | 584 | #endif |
0a7de745 A |
585 | size -= csize; /* Calculate what is left */ |
586 | snk64 += csize; /* Bump sink to next physical address */ | |
587 | src64 += csize; /* Bump source to next physical address */ | |
6d2010ae A |
588 | } |
589 | KERNEL_DEBUG(0xeff7004c | DBG_FUNC_END, (unsigned)src64, | |
0a7de745 | 590 | (unsigned)snk64, size, which, 0); |
6d2010ae A |
591 | |
592 | return KERN_SUCCESS; | |
593 | } |