subr_copy.c revision 1.9 1 /* $NetBSD: subr_copy.c,v 1.9 2019/04/06 03:06:28 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008, 2019
5 * The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10 * NASA Ames Research Center.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
37 * (c) UNIX System Laboratories, Inc.
38 * All or some portions of this file are derived from material licensed
39 * to the University of California by American Telephone and Telegraph
40 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
41 * the permission of UNIX System Laboratories, Inc.
42 *
43 * Copyright (c) 1992, 1993
44 * The Regents of the University of California. All rights reserved.
45 *
46 * This software was developed by the Computer Systems Engineering group
47 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
48 * contributed to Berkeley.
49 *
50 * All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Lawrence Berkeley Laboratory.
54 *
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
57 * are met:
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
63 * 3. Neither the name of the University nor the names of its contributors
64 * may be used to endorse or promote products derived from this software
65 * without specific prior written permission.
66 *
67 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
68 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
71 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 * SUCH DAMAGE.
78 *
79 * @(#)kern_subr.c 8.4 (Berkeley) 2/14/95
80 */
81
82 #include <sys/cdefs.h>
83 __KERNEL_RCSID(0, "$NetBSD: subr_copy.c,v 1.9 2019/04/06 03:06:28 thorpej Exp $");
84
85 #define __UFETCHSTORE_PRIVATE
86 #define __UCAS_PRIVATE
87
88 #include <sys/param.h>
89 #include <sys/fcntl.h>
90 #include <sys/proc.h>
91 #include <sys/systm.h>
92
93 #include <uvm/uvm_extern.h>
94
95 void
96 uio_setup_sysspace(struct uio *uio)
97 {
98
99 uio->uio_vmspace = vmspace_kernel();
100 }
101
102 int
103 uiomove(void *buf, size_t n, struct uio *uio)
104 {
105 struct vmspace *vm = uio->uio_vmspace;
106 struct iovec *iov;
107 size_t cnt;
108 int error = 0;
109 char *cp = buf;
110
111 ASSERT_SLEEPABLE();
112
113 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE);
114 while (n > 0 && uio->uio_resid) {
115 iov = uio->uio_iov;
116 cnt = iov->iov_len;
117 if (cnt == 0) {
118 KASSERT(uio->uio_iovcnt > 0);
119 uio->uio_iov++;
120 uio->uio_iovcnt--;
121 continue;
122 }
123 if (cnt > n)
124 cnt = n;
125 if (!VMSPACE_IS_KERNEL_P(vm)) {
126 if (curcpu()->ci_schedstate.spc_flags &
127 SPCF_SHOULDYIELD)
128 preempt();
129 }
130
131 if (uio->uio_rw == UIO_READ) {
132 error = copyout_vmspace(vm, cp, iov->iov_base,
133 cnt);
134 } else {
135 error = copyin_vmspace(vm, iov->iov_base, cp,
136 cnt);
137 }
138 if (error) {
139 break;
140 }
141 iov->iov_base = (char *)iov->iov_base + cnt;
142 iov->iov_len -= cnt;
143 uio->uio_resid -= cnt;
144 uio->uio_offset += cnt;
145 cp += cnt;
146 KDASSERT(cnt <= n);
147 n -= cnt;
148 }
149
150 return (error);
151 }
152
153 /*
154 * Wrapper for uiomove() that validates the arguments against a known-good
155 * kernel buffer.
156 */
157 int
158 uiomove_frombuf(void *buf, size_t buflen, struct uio *uio)
159 {
160 size_t offset;
161
162 if (uio->uio_offset < 0 || /* uio->uio_resid < 0 || */
163 (offset = uio->uio_offset) != uio->uio_offset)
164 return (EINVAL);
165 if (offset >= buflen)
166 return (0);
167 return (uiomove((char *)buf + offset, buflen - offset, uio));
168 }
169
170 /*
171 * Give next character to user as result of read.
172 */
173 int
174 ureadc(int c, struct uio *uio)
175 {
176 struct iovec *iov;
177
178 if (uio->uio_resid <= 0)
179 panic("ureadc: non-positive resid");
180 again:
181 if (uio->uio_iovcnt <= 0)
182 panic("ureadc: non-positive iovcnt");
183 iov = uio->uio_iov;
184 if (iov->iov_len <= 0) {
185 uio->uio_iovcnt--;
186 uio->uio_iov++;
187 goto again;
188 }
189 if (!VMSPACE_IS_KERNEL_P(uio->uio_vmspace)) {
190 int error;
191 if ((error = ustore_char(iov->iov_base, c)) != 0)
192 return (error);
193 } else {
194 *(char *)iov->iov_base = c;
195 }
196 iov->iov_base = (char *)iov->iov_base + 1;
197 iov->iov_len--;
198 uio->uio_resid--;
199 uio->uio_offset++;
200 return (0);
201 }
202
203 /*
204 * Like copyin(), but operates on an arbitrary vmspace.
205 */
206 int
207 copyin_vmspace(struct vmspace *vm, const void *uaddr, void *kaddr, size_t len)
208 {
209 struct iovec iov;
210 struct uio uio;
211 int error;
212
213 if (len == 0)
214 return (0);
215
216 if (VMSPACE_IS_KERNEL_P(vm)) {
217 return kcopy(uaddr, kaddr, len);
218 }
219 if (__predict_true(vm == curproc->p_vmspace)) {
220 return copyin(uaddr, kaddr, len);
221 }
222
223 iov.iov_base = kaddr;
224 iov.iov_len = len;
225 uio.uio_iov = &iov;
226 uio.uio_iovcnt = 1;
227 uio.uio_offset = (off_t)(uintptr_t)uaddr;
228 uio.uio_resid = len;
229 uio.uio_rw = UIO_READ;
230 UIO_SETUP_SYSSPACE(&uio);
231 error = uvm_io(&vm->vm_map, &uio, 0);
232
233 return (error);
234 }
235
236 /*
237 * Like copyout(), but operates on an arbitrary vmspace.
238 */
239 int
240 copyout_vmspace(struct vmspace *vm, const void *kaddr, void *uaddr, size_t len)
241 {
242 struct iovec iov;
243 struct uio uio;
244 int error;
245
246 if (len == 0)
247 return (0);
248
249 if (VMSPACE_IS_KERNEL_P(vm)) {
250 return kcopy(kaddr, uaddr, len);
251 }
252 if (__predict_true(vm == curproc->p_vmspace)) {
253 return copyout(kaddr, uaddr, len);
254 }
255
256 iov.iov_base = __UNCONST(kaddr); /* XXXUNCONST cast away const */
257 iov.iov_len = len;
258 uio.uio_iov = &iov;
259 uio.uio_iovcnt = 1;
260 uio.uio_offset = (off_t)(uintptr_t)uaddr;
261 uio.uio_resid = len;
262 uio.uio_rw = UIO_WRITE;
263 UIO_SETUP_SYSSPACE(&uio);
264 error = uvm_io(&vm->vm_map, &uio, 0);
265
266 return (error);
267 }
268
269 /*
270 * Like copyin(), but operates on an arbitrary process.
271 */
272 int
273 copyin_proc(struct proc *p, const void *uaddr, void *kaddr, size_t len)
274 {
275 struct vmspace *vm;
276 int error;
277
278 error = proc_vmspace_getref(p, &vm);
279 if (error) {
280 return error;
281 }
282 error = copyin_vmspace(vm, uaddr, kaddr, len);
283 uvmspace_free(vm);
284
285 return error;
286 }
287
288 /*
289 * Like copyout(), but operates on an arbitrary process.
290 */
291 int
292 copyout_proc(struct proc *p, const void *kaddr, void *uaddr, size_t len)
293 {
294 struct vmspace *vm;
295 int error;
296
297 error = proc_vmspace_getref(p, &vm);
298 if (error) {
299 return error;
300 }
301 error = copyout_vmspace(vm, kaddr, uaddr, len);
302 uvmspace_free(vm);
303
304 return error;
305 }
306
307 /*
308 * Like copyin(), but operates on an arbitrary pid.
309 */
310 int
311 copyin_pid(pid_t pid, const void *uaddr, void *kaddr, size_t len)
312 {
313 struct proc *p;
314 struct vmspace *vm;
315 int error;
316
317 mutex_enter(proc_lock);
318 p = proc_find(pid);
319 if (p == NULL) {
320 mutex_exit(proc_lock);
321 return ESRCH;
322 }
323 mutex_enter(p->p_lock);
324 proc_vmspace_getref(p, &vm);
325 mutex_exit(p->p_lock);
326 mutex_exit(proc_lock);
327
328 error = copyin_vmspace(vm, uaddr, kaddr, len);
329
330 uvmspace_free(vm);
331 return error;
332 }
333
334 /*
335 * Like copyin(), except it operates on kernel addresses when the FKIOCTL
336 * flag is passed in `ioctlflags' from the ioctl call.
337 */
338 int
339 ioctl_copyin(int ioctlflags, const void *src, void *dst, size_t len)
340 {
341 if (ioctlflags & FKIOCTL)
342 return kcopy(src, dst, len);
343 return copyin(src, dst, len);
344 }
345
346 /*
347 * Like copyout(), except it operates on kernel addresses when the FKIOCTL
348 * flag is passed in `ioctlflags' from the ioctl call.
349 */
350 int
351 ioctl_copyout(int ioctlflags, const void *src, void *dst, size_t len)
352 {
353 if (ioctlflags & FKIOCTL)
354 return kcopy(src, dst, len);
355 return copyout(src, dst, len);
356 }
357
358 /*
359 * User-space CAS / fetch / store
360 */
361
362 #ifdef __NO_STRICT_ALIGNMENT
363 #define CHECK_ALIGNMENT(x) __nothing
364 #else /* ! __NO_STRICT_ALIGNMENT */
365 static bool
366 ufetchstore_aligned(uintptr_t uaddr, size_t size)
367 {
368 return (uaddr & (size - 1)) == 0;
369 }
370
371 #define CHECK_ALIGNMENT() \
372 do { \
373 if (!ufetchstore_aligned((uintptr_t)uaddr, sizeof(*uaddr))) \
374 return EFAULT; \
375 } while (/*CONSTCOND*/0)
376 #endif /* __NO_STRICT_ALIGNMENT */
377
378 #ifndef __HAVE_UCAS_FULL
379 #if !defined(__HAVE_UCAS_MP) && defined(MULTIPROCESSOR)
380 #include <sys/atomic.h>
381 #include <sys/cpu.h>
382 #include <sys/once.h>
383 #include <sys/mutex.h>
384 #include <sys/ipi.h>
385
386 static int ucas_critical_splcookie;
387 static volatile u_int ucas_critical_pausing_cpus;
388 static u_int ucas_critical_ipi;
389 static ONCE_DECL(ucas_critical_init_once)
390
391 static void
392 ucas_critical_cpu_gate(void *arg __unused)
393 {
394 int count = SPINLOCK_BACKOFF_MIN;
395
396 KASSERT(ucas_critical_pausing_cpus > 0);
397 atomic_dec_uint(&ucas_critical_pausing_cpus);
398 while (ucas_critical_pausing_cpus != (u_int)-1) {
399 SPINLOCK_BACKOFF(count);
400 }
401 }
402
403 static int
404 ucas_critical_init(void)
405 {
406 ucas_critical_ipi = ipi_register(ucas_critical_cpu_gate, NULL);
407 return 0;
408 }
409
410 static void
411 ucas_critical_wait(void)
412 {
413 int count = SPINLOCK_BACKOFF_MIN;
414
415 while (ucas_critical_pausing_cpus > 0) {
416 SPINLOCK_BACKOFF(count);
417 }
418 }
419 #endif /* ! __HAVE_UCAS_MP && MULTIPROCESSOR */
420
421 static inline void
422 ucas_critical_enter(lwp_t * const l)
423 {
424
425 #if !defined(__HAVE_UCAS_MP) && defined(MULTIPROCESSOR)
426 if (ncpu > 1) {
427 RUN_ONCE(&ucas_critical_init_once, ucas_critical_init);
428
429 /*
430 * Acquire the mutex first, then go to splhigh() and
431 * broadcast the IPI to lock all of the other CPUs
432 * behind the gate.
433 *
434 * N.B. Going to splhigh() implicitly disables preemption,
435 * so there's no need to do it explicitly.
436 */
437 mutex_enter(&cpu_lock);
438 ucas_critical_splcookie = splhigh();
439 ucas_critical_pausing_cpus = ncpu - 1;
440 membar_enter();
441
442 ipi_trigger_broadcast(ucas_critical_ipi, true);
443 ucas_critical_wait();
444 return;
445 }
446 #endif /* ! __HAVE_UCAS_MP && MULTIPROCESSOR */
447
448 KPREEMPT_DISABLE(l);
449 }
450
451 static inline void
452 ucas_critical_exit(lwp_t * const l)
453 {
454
455 #if !defined(__HAVE_UCAS_MP) && defined(MULTIPROCESSOR)
456 if (ncpu > 1) {
457 membar_exit();
458 ucas_critical_pausing_cpus = (u_int)-1;
459 splx(ucas_critical_splcookie);
460 mutex_exit(&cpu_lock);
461 return;
462 }
463 #endif /* ! __HAVE_UCAS_MP && MULTIPROCESSOR */
464
465 KPREEMPT_ENABLE(l);
466 }
467
468 int
469 _ucas_32(volatile uint32_t *uaddr, uint32_t old, uint32_t new, uint32_t *ret)
470 {
471 lwp_t * const l = curlwp;
472 uint32_t *uva = ((void *)(uintptr_t)uaddr);
473 int error;
474
475 /*
476 * Wire the user address down to avoid taking a page fault during
477 * the critical section.
478 */
479 error = uvm_vslock(l->l_proc->p_vmspace, uva, sizeof(*uaddr),
480 VM_PROT_READ | VM_PROT_WRITE);
481 if (error)
482 return error;
483
484 ucas_critical_enter(l);
485 error = _ufetch_32(uva, ret);
486 if (error == 0 && *ret == old) {
487 error = _ustore_32(uva, new);
488 }
489 ucas_critical_exit(l);
490
491 uvm_vsunlock(l->l_proc->p_vmspace, uva, sizeof(*uaddr));
492
493 return error;
494 }
495
496 #ifdef _LP64
497 int
498 _ucas_64(volatile uint64_t *uaddr, uint64_t old, uint64_t new, uint64_t *ret)
499 {
500 lwp_t * const l = curlwp;
501 uint64_t *uva = ((void *)(uintptr_t)uaddr);
502 int error;
503
504 /*
505 * Wire the user address down to avoid taking a page fault during
506 * the critical section.
507 */
508 error = uvm_vslock(l->l_proc->p_vmspace, uva, sizeof(*uaddr),
509 VM_PROT_READ | VM_PROT_WRITE);
510 if (error)
511 return error;
512
513 ucas_critical_enter(l);
514 error = _ufetch_64(uva, ret);
515 if (error == 0 && *ret == old) {
516 error = _ustore_64(uva, new);
517 }
518 ucas_critical_exit(l);
519
520 uvm_vsunlock(l->l_proc->p_vmspace, uva, sizeof(*uaddr));
521
522 return error;
523 }
524 #endif /* _LP64 */
525 #endif /* ! __HAVE_UCAS_FULL */
526
527 int
528 ucas_32(volatile uint32_t *uaddr, uint32_t old, uint32_t new, uint32_t *ret)
529 {
530
531 ASSERT_SLEEPABLE();
532 CHECK_ALIGNMENT();
533 #if defined(__HAVE_UCAS_MP) && defined(MULTIPROCESSOR)
534 if (ncpu > 1) {
535 return _ucas_32_mp(uaddr, old, new, ret);
536 }
537 #endif /* __HAVE_UCAS_MP && MULTIPROCESSOR */
538 return _ucas_32(uaddr, old, new, ret);
539 }
540
541 #ifdef _LP64
542 int
543 ucas_64(volatile uint64_t *uaddr, uint64_t old, uint64_t new, uint64_t *ret)
544 {
545
546 ASSERT_SLEEPABLE();
547 CHECK_ALIGNMENT();
548 #if defined(__HAVE_UCAS_MP) && defined(MULTIPROCESSOR)
549 if (ncpu > 1) {
550 return _ucas_64_mp(uaddr, old, new, ret);
551 }
552 #endif /* __HAVE_UCAS_MP && MULTIPROCESSOR */
553 return _ucas_64(uaddr, old, new, ret);
554 }
555 #endif /* _LP64 */
556
557 __strong_alias(ucas_int,ucas_32);
558 #ifdef _LP64
559 __strong_alias(ucas_ptr,ucas_64);
560 #else
561 __strong_alias(ucas_ptr,ucas_32);
562 #endif /* _LP64 */
563
564 int
565 ufetch_8(const uint8_t *uaddr, uint8_t *valp)
566 {
567
568 ASSERT_SLEEPABLE();
569 CHECK_ALIGNMENT();
570 return _ufetch_8(uaddr, valp);
571 }
572
573 int
574 ufetch_16(const uint16_t *uaddr, uint16_t *valp)
575 {
576
577 ASSERT_SLEEPABLE();
578 CHECK_ALIGNMENT();
579 return _ufetch_16(uaddr, valp);
580 }
581
582 int
583 ufetch_32(const uint32_t *uaddr, uint32_t *valp)
584 {
585
586 ASSERT_SLEEPABLE();
587 CHECK_ALIGNMENT();
588 return _ufetch_32(uaddr, valp);
589 }
590
591 #ifdef _LP64
592 int
593 ufetch_64(const uint64_t *uaddr, uint64_t *valp)
594 {
595
596 ASSERT_SLEEPABLE();
597 CHECK_ALIGNMENT();
598 return _ufetch_64(uaddr, valp);
599 }
600 #endif /* _LP64 */
601
602 __strong_alias(ufetch_char,ufetch_8);
603 __strong_alias(ufetch_short,ufetch_16);
604 __strong_alias(ufetch_int,ufetch_32);
605 #ifdef _LP64
606 __strong_alias(ufetch_long,ufetch_64);
607 __strong_alias(ufetch_ptr,ufetch_64);
608 #else
609 __strong_alias(ufetch_long,ufetch_32);
610 __strong_alias(ufetch_ptr,ufetch_32);
611 #endif /* _LP64 */
612
613 int
614 ustore_8(uint8_t *uaddr, uint8_t val)
615 {
616
617 ASSERT_SLEEPABLE();
618 CHECK_ALIGNMENT();
619 return _ustore_8(uaddr, val);
620 }
621
622 int
623 ustore_16(uint16_t *uaddr, uint16_t val)
624 {
625
626 ASSERT_SLEEPABLE();
627 CHECK_ALIGNMENT();
628 return _ustore_16(uaddr, val);
629 }
630
631 int
632 ustore_32(uint32_t *uaddr, uint32_t val)
633 {
634
635 ASSERT_SLEEPABLE();
636 CHECK_ALIGNMENT();
637 return _ustore_32(uaddr, val);
638 }
639
640 #ifdef _LP64
641 int
642 ustore_64(uint64_t *uaddr, uint64_t val)
643 {
644
645 ASSERT_SLEEPABLE();
646 CHECK_ALIGNMENT();
647 return _ustore_64(uaddr, val);
648 }
649 #endif /* _LP64 */
650
651 __strong_alias(ustore_char,ustore_8);
652 __strong_alias(ustore_short,ustore_16);
653 __strong_alias(ustore_int,ustore_32);
654 #ifdef _LP64
655 __strong_alias(ustore_long,ustore_64);
656 __strong_alias(ustore_ptr,ustore_64);
657 #else
658 __strong_alias(ustore_long,ustore_32);
659 __strong_alias(ustore_ptr,ustore_32);
660 #endif /* _LP64 */
661