1 /* $OpenBSD: kern_subr.c,v 1.51 2022/08/14 01:58:27 jsg Exp $ */ 2 /* $NetBSD: kern_subr.c,v 1.15 1996/04/09 17:21:56 ragge Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/proc.h> 43 #include <sys/sched.h> 44 #include <sys/malloc.h> 45 #include <sys/queue.h> 46 47 int 48 uiomove(void *cp, size_t n, struct uio *uio) 49 { 50 struct iovec *iov; 51 size_t cnt; 52 int error = 0; 53 54 #ifdef DIAGNOSTIC 55 if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE) 56 panic("uiomove: mode"); 57 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc) 58 panic("uiomove: proc"); 59 #endif 60 61 if (n > uio->uio_resid) 62 n = uio->uio_resid; 63 64 while (n > 0) { 65 iov = uio->uio_iov; 66 cnt = iov->iov_len; 67 if (cnt == 0) { 68 KASSERT(uio->uio_iovcnt > 0); 69 uio->uio_iov++; 70 uio->uio_iovcnt--; 71 continue; 72 } 73 if (cnt > n) 74 cnt = n; 75 switch (uio->uio_segflg) { 76 77 case UIO_USERSPACE: 78 sched_pause(preempt); 79 if (uio->uio_rw == UIO_READ) 80 error = copyout(cp, iov->iov_base, cnt); 81 else 82 error = copyin(iov->iov_base, cp, cnt); 83 if (error) 84 return (error); 85 break; 86 87 case UIO_SYSSPACE: 88 if (uio->uio_rw == UIO_READ) 89 error = kcopy(cp, iov->iov_base, cnt); 90 else 91 error = kcopy(iov->iov_base, cp, cnt); 92 if (error) 93 return(error); 94 } 95 iov->iov_base = (caddr_t)iov->iov_base + cnt; 96 iov->iov_len -= cnt; 97 uio->uio_resid -= cnt; 98 uio->uio_offset += cnt; 99 cp = (caddr_t)cp + cnt; 100 n -= cnt; 101 } 102 return (error); 103 } 104 105 /* 106 * Give next character to user as result of read. 107 */ 108 int 109 ureadc(int c, struct uio *uio) 110 { 111 struct iovec *iov; 112 113 if (uio->uio_resid == 0) 114 #ifdef DIAGNOSTIC 115 panic("ureadc: zero resid"); 116 #else 117 return (EINVAL); 118 #endif 119 again: 120 if (uio->uio_iovcnt <= 0) 121 #ifdef DIAGNOSTIC 122 panic("ureadc: non-positive iovcnt"); 123 #else 124 return (EINVAL); 125 #endif 126 iov = uio->uio_iov; 127 if (iov->iov_len <= 0) { 128 uio->uio_iovcnt--; 129 uio->uio_iov++; 130 goto again; 131 } 132 switch (uio->uio_segflg) { 133 134 case UIO_USERSPACE: 135 { 136 char tmp = c; 137 138 if (copyout(&tmp, iov->iov_base, sizeof(char)) != 0) 139 return (EFAULT); 140 } 141 break; 142 143 case UIO_SYSSPACE: 144 *(char *)iov->iov_base = c; 145 break; 146 } 147 iov->iov_base = (caddr_t)iov->iov_base + 1; 148 iov->iov_len--; 149 uio->uio_resid--; 150 uio->uio_offset++; 151 return (0); 152 } 153 154 /* 155 * General routine to allocate a hash table. 156 */ 157 void * 158 hashinit(int elements, int type, int flags, u_long *hashmask) 159 { 160 u_long hashsize, i; 161 LIST_HEAD(generic, generic) *hashtbl; 162 163 if (elements <= 0) 164 panic("hashinit: bad cnt"); 165 if ((elements & (elements - 1)) == 0) 166 hashsize = elements; 167 else 168 for (hashsize = 1; hashsize < elements; hashsize <<= 1) 169 continue; 170 hashtbl = mallocarray(hashsize, sizeof(*hashtbl), type, flags); 171 if (hashtbl == NULL) 172 return NULL; 173 for (i = 0; i < hashsize; i++) 174 LIST_INIT(&hashtbl[i]); 175 *hashmask = hashsize - 1; 176 return (hashtbl); 177 } 178 179 void 180 hashfree(void *hash, int elements, int type) 181 { 182 u_long hashsize; 183 LIST_HEAD(generic, generic) *hashtbl = hash; 184 185 if (elements <= 0) 186 panic("hashfree: bad cnt"); 187 if ((elements & (elements - 1)) == 0) 188 hashsize = elements; 189 else 190 for (hashsize = 1; hashsize < elements; hashsize <<= 1) 191 continue; 192 193 free(hashtbl, type, sizeof(*hashtbl) * hashsize); 194 } 195 196 /* 197 * "startup hook" types, functions, and variables. 198 */ 199 200 struct hook_desc_head startuphook_list = 201 TAILQ_HEAD_INITIALIZER(startuphook_list); 202 203 void * 204 hook_establish(struct hook_desc_head *head, int tail, void (*fn)(void *), 205 void *arg) 206 { 207 struct hook_desc *hdp; 208 209 hdp = malloc(sizeof(*hdp), M_DEVBUF, M_NOWAIT); 210 if (hdp == NULL) 211 return (NULL); 212 213 hdp->hd_fn = fn; 214 hdp->hd_arg = arg; 215 if (tail) 216 TAILQ_INSERT_TAIL(head, hdp, hd_list); 217 else 218 TAILQ_INSERT_HEAD(head, hdp, hd_list); 219 220 return (hdp); 221 } 222 223 void 224 hook_disestablish(struct hook_desc_head *head, void *vhook) 225 { 226 struct hook_desc *hdp; 227 228 #ifdef DIAGNOSTIC 229 for (hdp = TAILQ_FIRST(head); hdp != NULL; 230 hdp = TAILQ_NEXT(hdp, hd_list)) 231 if (hdp == vhook) 232 break; 233 if (hdp == NULL) 234 return; 235 #endif 236 hdp = vhook; 237 TAILQ_REMOVE(head, hdp, hd_list); 238 free(hdp, M_DEVBUF, sizeof(*hdp)); 239 } 240 241 /* 242 * Run hooks. Startup hooks are invoked right after scheduler_start but 243 * before root is mounted. Shutdown hooks are invoked immediately before the 244 * system is halted or rebooted, i.e. after file systems unmounted, 245 * after crash dump done, etc. 246 */ 247 void 248 dohooks(struct hook_desc_head *head, int flags) 249 { 250 struct hook_desc *hdp, *hdp_temp; 251 252 if ((flags & HOOK_REMOVE) == 0) { 253 TAILQ_FOREACH_SAFE(hdp, head, hd_list, hdp_temp) { 254 (*hdp->hd_fn)(hdp->hd_arg); 255 } 256 } else { 257 while ((hdp = TAILQ_FIRST(head)) != NULL) { 258 TAILQ_REMOVE(head, hdp, hd_list); 259 (*hdp->hd_fn)(hdp->hd_arg); 260 if ((flags & HOOK_FREE) != 0) 261 free(hdp, M_DEVBUF, sizeof(*hdp)); 262 } 263 } 264 } 265