AMD64 - Refactor uio_resid and size_t assumptions.
[dragonfly.git] / sys / kern / kern_subr.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/kern_subr.c,v 1.31.2.2 2002/04/21 08:09:37 bde Exp $
fe57a30b 40 * $DragonFly: src/sys/kern/kern_subr.c,v 1.27 2007/01/29 20:44:02 tgen Exp $
984263bc
MD
41 */
42
6bd9bbba
HP
43#include "opt_ddb.h"
44
984263bc
MD
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/kernel.h>
48#include <sys/proc.h>
49#include <sys/malloc.h>
50#include <sys/lock.h>
51#include <sys/resourcevar.h>
cbd0496d
JS
52#include <sys/sysctl.h>
53#include <sys/uio.h>
984263bc 54#include <sys/vnode.h>
a358fd45
MD
55#include <sys/sfbuf.h>
56#include <sys/thread2.h>
25e80b06 57#include <machine/limits.h>
984263bc
MD
58
59#include <vm/vm.h>
60#include <vm/vm_page.h>
61#include <vm/vm_map.h>
62
cbd0496d
JS
63SYSCTL_INT(_kern, KERN_IOV_MAX, iov_max, CTLFLAG_RD, NULL, UIO_MAXIOV,
64 "Maximum number of elements in an I/O vector; sysconf(_SC_IOV_MAX)");
65
1301b4d8
MD
66/*
67 * UIO_READ: copy the kernelspace cp to the user or kernelspace UIO
8ddc6004 68 * UIO_WRITE: copy the user or kernelspace UIO to the kernelspace cp
1301b4d8
MD
69 *
70 * For userspace UIO's, uio_td must be the current thread.
e54488bb
MD
71 *
72 * The syscall interface is responsible for limiting the length to
73 * ssize_t for things like read() or write() which return the bytes
74 * read or written as ssize_t. These functions work with unsigned
75 * lengths.
1301b4d8 76 */
984263bc 77int
e54488bb 78uiomove(caddr_t cp, size_t n, struct uio *uio)
984263bc 79{
1fd87d54 80 struct iovec *iov;
e54488bb 81 size_t cnt;
984263bc
MD
82 int error = 0;
83 int save = 0;
d16a8831 84 int baseticks = ticks;
984263bc
MD
85
86 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
87 ("uiomove: mode"));
dadab5e9 88 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
984263bc
MD
89 ("uiomove proc"));
90
91 if (curproc) {
92 save = curproc->p_flag & P_DEADLKTREAT;
93 curproc->p_flag |= P_DEADLKTREAT;
94 }
95
96 while (n > 0 && uio->uio_resid) {
97 iov = uio->uio_iov;
98 cnt = iov->iov_len;
99 if (cnt == 0) {
100 uio->uio_iov++;
101 uio->uio_iovcnt--;
102 continue;
103 }
104 if (cnt > n)
105 cnt = n;
106
107 switch (uio->uio_segflg) {
108
109 case UIO_USERSPACE:
d16a8831 110 if (ticks - baseticks >= hogticks) {
984263bc 111 uio_yield();
d16a8831
MD
112 baseticks = ticks;
113 }
984263bc
MD
114 if (uio->uio_rw == UIO_READ)
115 error = copyout(cp, iov->iov_base, cnt);
116 else
117 error = copyin(iov->iov_base, cp, cnt);
118 if (error)
119 break;
120 break;
121
122 case UIO_SYSSPACE:
123 if (uio->uio_rw == UIO_READ)
124 bcopy((caddr_t)cp, iov->iov_base, cnt);
125 else
126 bcopy(iov->iov_base, (caddr_t)cp, cnt);
127 break;
128 case UIO_NOCOPY:
129 break;
130 }
656849c6 131 iov->iov_base = (char *)iov->iov_base + cnt;
984263bc
MD
132 iov->iov_len -= cnt;
133 uio->uio_resid -= cnt;
134 uio->uio_offset += cnt;
135 cp += cnt;
136 n -= cnt;
137 }
138 if (curproc)
139 curproc->p_flag = (curproc->p_flag & ~P_DEADLKTREAT) | save;
140 return (error);
141}
25e80b06
DR
142/*
143 * Wrapper for uiomove() that validates the arguments against a known-good
e54488bb 144 * kernel buffer.
25e80b06
DR
145 */
146int
e54488bb 147uiomove_frombuf(void *buf, size_t buflen, struct uio *uio)
25e80b06 148{
e54488bb 149 size_t offset;
25e80b06 150
e54488bb
MD
151 offset = (size_t)uio->uio_offset;
152 if ((off_t)offset != uio->uio_offset)
25e80b06 153 return (EINVAL);
e54488bb 154 if (buflen == 0 || offset >= buflen)
25e80b06 155 return (0);
e54488bb 156 return (uiomove((char *)buf + offset, buflen - offset, uio));
984263bc
MD
157}
158
984263bc
MD
159/*
160 * Give next character to user as result of read.
161 */
162int
c972a82f 163ureadc(int c, struct uio *uio)
984263bc 164{
1fd87d54 165 struct iovec *iov;
656849c6 166 char *iov_base;
984263bc
MD
167
168again:
169 if (uio->uio_iovcnt == 0 || uio->uio_resid == 0)
170 panic("ureadc");
171 iov = uio->uio_iov;
172 if (iov->iov_len == 0) {
173 uio->uio_iovcnt--;
174 uio->uio_iov++;
175 goto again;
176 }
177 switch (uio->uio_segflg) {
178
179 case UIO_USERSPACE:
180 if (subyte(iov->iov_base, c) < 0)
181 return (EFAULT);
182 break;
183
184 case UIO_SYSSPACE:
656849c6
SW
185 iov_base = iov->iov_base;
186 *iov_base = c;
187 iov->iov_base = iov_base;
984263bc
MD
188 break;
189
984263bc
MD
190 case UIO_NOCOPY:
191 break;
192 }
656849c6 193 iov->iov_base = (char *)iov->iov_base + 1;
984263bc
MD
194 iov->iov_len--;
195 uio->uio_resid--;
196 uio->uio_offset++;
197 return (0);
198}
199
984263bc 200/*
1c86b25b
MD
201 * General routine to allocate a hash table. Make the hash table size a
202 * power of 2 greater or equal to the number of elements requested, and
203 * store the masking value in *hashmask.
984263bc
MD
204 */
205void *
c972a82f 206hashinit(int elements, struct malloc_type *type, u_long *hashmask)
984263bc
MD
207{
208 long hashsize;
209 LIST_HEAD(generic, generic) *hashtbl;
210 int i;
211
212 if (elements <= 0)
213 panic("hashinit: bad elements");
8f17cbc6 214 for (hashsize = 2; hashsize < elements; hashsize <<= 1)
984263bc 215 continue;
efda3bd0 216 hashtbl = kmalloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
984263bc
MD
217 for (i = 0; i < hashsize; i++)
218 LIST_INIT(&hashtbl[i]);
219 *hashmask = hashsize - 1;
220 return (hashtbl);
221}
222
223static int primes[] = { 1, 13, 31, 61, 127, 251, 509, 761, 1021, 1531, 2039,
224 2557, 3067, 3583, 4093, 4603, 5119, 5623, 6143, 6653,
225 7159, 7673, 8191, 12281, 16381, 24571, 32749 };
226#define NPRIMES (sizeof(primes) / sizeof(primes[0]))
227
228/*
229 * General routine to allocate a prime number sized hash table.
230 */
231void *
c972a82f 232phashinit(int elements, struct malloc_type *type, u_long *nentries)
984263bc
MD
233{
234 long hashsize;
235 LIST_HEAD(generic, generic) *hashtbl;
236 int i;
237
238 if (elements <= 0)
239 panic("phashinit: bad elements");
240 for (i = 1, hashsize = primes[1]; hashsize <= elements;) {
241 i++;
242 if (i == NPRIMES)
243 break;
244 hashsize = primes[i];
245 }
246 hashsize = primes[i - 1];
efda3bd0 247 hashtbl = kmalloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
984263bc
MD
248 for (i = 0; i < hashsize; i++)
249 LIST_INIT(&hashtbl[i]);
250 *nentries = hashsize;
251 return (hashtbl);
252}
75a872f8
DRJ
253
254/*
255 * Copyin an iovec. If the iovec array fits, use the preallocated small
256 * iovec structure. If it is too big, dynamically allocate an iovec array
257 * of sufficient size.
f832287e
MD
258 *
259 * MPSAFE
75a872f8
DRJ
260 */
261int
262iovec_copyin(struct iovec *uiov, struct iovec **kiov, struct iovec *siov,
e54488bb 263 size_t iov_cnt, size_t *iov_len)
75a872f8
DRJ
264{
265 struct iovec *iovp;
266 int error, i;
e54488bb 267 size_t len;
75a872f8 268
fe57a30b 269 if (iov_cnt > UIO_MAXIOV)
75a872f8 270 return EMSGSIZE;
fe57a30b 271 if (iov_cnt > UIO_SMALLIOV) {
75a872f8
DRJ
272 MALLOC(*kiov, struct iovec *, sizeof(struct iovec) * iov_cnt,
273 M_IOV, M_WAITOK);
274 } else {
275 *kiov = siov;
276 }
277 error = copyin(uiov, *kiov, iov_cnt * sizeof(struct iovec));
ef5c76d7
MD
278 if (error == 0) {
279 *iov_len = 0;
280 for (i = 0, iovp = *kiov; i < iov_cnt; i++, iovp++) {
281 /*
282 * Check for both *iov_len overflows and out of
283 * range iovp->iov_len's. We limit to the
284 * capabilities of signed integers.
e54488bb
MD
285 *
286 * GCC4 - overflow check opt requires assign/test.
ef5c76d7 287 */
e54488bb
MD
288 len = *iov_len + iovp->iov_len;
289 if (len < *iov_len)
ef5c76d7 290 error = EINVAL;
e54488bb 291 *iov_len = len;
ef5c76d7
MD
292 }
293 }
e54488bb
MD
294
295 /*
296 * From userland disallow iovec's which exceed the sized size
297 * limit as the system calls return ssize_t.
298 *
299 * NOTE: Internal kernel interfaces can handle the unsigned
300 * limit.
301 */
302 if (error == 0 && (ssize_t)*iov_len < 0)
303 error = EINVAL;
304
75a872f8
DRJ
305 if (error)
306 iovec_free(kiov, siov);
307 return (error);
308}
a358fd45
MD
309
310
311/*
312 * Copyright (c) 2004 Alan L. Cox <alc@cs.rice.edu>
313 * Copyright (c) 1982, 1986, 1991, 1993
314 * The Regents of the University of California. All rights reserved.
315 * (c) UNIX System Laboratories, Inc.
316 * All or some portions of this file are derived from material licensed
317 * to the University of California by American Telephone and Telegraph
318 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
319 * the permission of UNIX System Laboratories, Inc.
320 *
321 * Redistribution and use in source and binary forms, with or without
322 * modification, are permitted provided that the following conditions
323 * are met:
324 * 1. Redistributions of source code must retain the above copyright
325 * notice, this list of conditions and the following disclaimer.
326 * 2. Redistributions in binary form must reproduce the above copyright
327 * notice, this list of conditions and the following disclaimer in the
328 * documentation and/or other materials provided with the distribution.
329 * 4. Neither the name of the University nor the names of its contributors
330 * may be used to endorse or promote products derived from this software
331 * without specific prior written permission.
332 *
333 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
334 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
335 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
336 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
337 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
338 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
339 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
340 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
342 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
343 * SUCH DAMAGE.
344 *
345 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
346 * $FreeBSD: src/sys/i386/i386/uio_machdep.c,v 1.1 2004/03/21 20:28:36 alc Exp $
347 */
348
349/*
350 * Implement uiomove(9) from physical memory using sf_bufs to reduce
351 * the creation and destruction of ephemeral mappings.
352 */
353int
e54488bb 354uiomove_fromphys(vm_page_t *ma, vm_offset_t offset, size_t n, struct uio *uio)
a358fd45
MD
355{
356 struct sf_buf *sf;
357 struct thread *td = curthread;
358 struct iovec *iov;
359 void *cp;
360 vm_offset_t page_offset;
361 vm_page_t m;
362 size_t cnt;
363 int error = 0;
364 int save = 0;
365
366 KASSERT(uio->uio_rw == UIO_READ || uio->uio_rw == UIO_WRITE,
367 ("uiomove_fromphys: mode"));
368 KASSERT(uio->uio_segflg != UIO_USERSPACE || uio->uio_td == curthread,
369 ("uiomove_fromphys proc"));
370
371 crit_enter();
372 save = td->td_flags & TDF_DEADLKTREAT;
373 td->td_flags |= TDF_DEADLKTREAT;
374 crit_exit();
375
376 while (n > 0 && uio->uio_resid) {
377 iov = uio->uio_iov;
378 cnt = iov->iov_len;
379 if (cnt == 0) {
380 uio->uio_iov++;
381 uio->uio_iovcnt--;
382 continue;
383 }
384 if (cnt > n)
385 cnt = n;
386 page_offset = offset & PAGE_MASK;
387 cnt = min(cnt, PAGE_SIZE - page_offset);
388 m = ma[offset >> PAGE_SHIFT];
389 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
390 cp = (char *)sf_buf_kva(sf) + page_offset;
391 switch (uio->uio_segflg) {
392 case UIO_USERSPACE:
393 /*
394 * note: removed uioyield (it was the wrong place to
395 * put it).
396 */
397 if (uio->uio_rw == UIO_READ)
398 error = copyout(cp, iov->iov_base, cnt);
399 else
400 error = copyin(iov->iov_base, cp, cnt);
401 if (error) {
402 sf_buf_free(sf);
403 goto out;
404 }
405 break;
406 case UIO_SYSSPACE:
407 if (uio->uio_rw == UIO_READ)
408 bcopy(cp, iov->iov_base, cnt);
409 else
410 bcopy(iov->iov_base, cp, cnt);
411 break;
412 case UIO_NOCOPY:
413 break;
414 }
415 sf_buf_free(sf);
416 iov->iov_base = (char *)iov->iov_base + cnt;
417 iov->iov_len -= cnt;
418 uio->uio_resid -= cnt;
419 uio->uio_offset += cnt;
420 offset += cnt;
421 n -= cnt;
422 }
423out:
424 if (save == 0) {
425 crit_enter();
426 td->td_flags &= ~TDF_DEADLKTREAT;
427 crit_exit();
428 }
429 return (error);
430}
431