kern_subr.c
来自「基于组件方式开发操作系统的OSKIT源代码」· C语言 代码 · 共 1,141 行 · 第 1/2 页
C
1,141 行
/* $NetBSD: kern_subr.c,v 1.74 2000/12/10 14:14:15 fvdl Exp $ *//*- * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, * NASA Ames Research Center, and by Luke Mewburn. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the NetBSD * Foundation, Inc. and its contributors. * 4. Neither the name of The NetBSD Foundation nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. *//* * Copyright (c) 1982, 1986, 1991, 1993 * The Regents of the University of California. All rights reserved. * (c) UNIX System Laboratories, Inc. * All or some portions of this file are derived from material licensed * to the University of California by American Telephone and Telegraph * Co. or Unix System Laboratories, Inc. and are reproduced herein with * the permission of UNIX System Laboratories, Inc. * * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * This software was developed by the Computer Systems Engineering group * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and * contributed to Berkeley. * * All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Lawrence Berkeley Laboratory. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)kern_subr.c 8.4 (Berkeley) 2/14/95 */#include "opt_md.h"#include <sys/param.h>#include <sys/systm.h>#include <sys/proc.h>#include <sys/malloc.h>#include <sys/mount.h>#include <sys/device.h>#include <sys/reboot.h>#include <sys/conf.h>#include <sys/disklabel.h>#include <sys/queue.h>#ifndef OSKIT#include <dev/cons.h>#include <net/if.h>/* XXX these should eventually move to subr_autoconf.c */static int findblkmajor __P((const char *));const char *findblkname __P((int));static struct device *finddevice __P((const char *));static struct device *getdisk __P((char *, int, int, dev_t *, int));static struct device *parsedisk __P((char *, int, int, dev_t *));intuiomove(buf, n, uio) void *buf; int n; struct uio *uio;{ struct iovec *iov; u_int cnt; int error = 0; char *cp = buf; struct proc *p = uio->uio_procp;#ifdef DIAGNOSTIC if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE) panic("uiomove: mode"); if (uio->uio_segflg == UIO_USERSPACE && p != curproc) panic("uiomove proc");#endif while (n > 0 && uio->uio_resid) { iov = uio->uio_iov; cnt = iov->iov_len; if (cnt == 0) { uio->uio_iov++; uio->uio_iovcnt--; continue; } if (cnt > n) cnt = n; switch (uio->uio_segflg) { case UIO_USERSPACE: KDASSERT(p->p_cpu != NULL); KDASSERT(p->p_cpu == curcpu()); if (p->p_cpu->ci_schedstate.spc_flags & SPCF_SHOULDYIELD) preempt(NULL); if (uio->uio_rw == UIO_READ) error = copyout(cp, iov->iov_base, cnt); else error = copyin(iov->iov_base, cp, cnt); if (error) return (error); break; case UIO_SYSSPACE: if (uio->uio_rw == UIO_READ) error = kcopy(cp, iov->iov_base, cnt); else error = kcopy(iov->iov_base, cp, cnt); if (error) return (error); break; } iov->iov_base = (caddr_t)iov->iov_base + cnt; iov->iov_len -= cnt; uio->uio_resid -= cnt; uio->uio_offset += cnt; cp += cnt; n -= cnt; } return (error);}/* * Give next character to user as result of read. */intureadc(c, uio) int c; struct uio *uio;{ struct iovec *iov; if (uio->uio_resid <= 0) panic("ureadc: non-positive resid");again: if (uio->uio_iovcnt <= 0) panic("ureadc: non-positive iovcnt"); iov = uio->uio_iov; if (iov->iov_len <= 0) { uio->uio_iovcnt--; uio->uio_iov++; goto again; } switch (uio->uio_segflg) { case UIO_USERSPACE: if (subyte(iov->iov_base, c) < 0) return (EFAULT); break; case UIO_SYSSPACE: *(char *)iov->iov_base = c; break; } iov->iov_base = (caddr_t)iov->iov_base + 1; iov->iov_len--; uio->uio_resid--; uio->uio_offset++; return (0);}#endif /*OSKIT*//* * General routine to allocate a hash table. * Allocate enough memory to hold at least `elements' list-head pointers. * Return a pointer to the allocated space and set *hashmask to a pattern * suitable for masking a value to use as an index into the returned array. */void *hashinit(elements, htype, mtype, mflags, hashmask) int elements; enum hashtype htype; int mtype, mflags; u_long *hashmask;{ long hashsize; LIST_HEAD(, generic) *hashtbl_list; TAILQ_HEAD(, generic) *hashtbl_tailq; int i, esize; void *p; if (elements <= 0) panic("hashinit: bad cnt"); for (hashsize = 1; hashsize < elements; hashsize <<= 1) continue; switch (htype) { case HASH_LIST: esize = sizeof(*hashtbl_list); break; case HASH_TAILQ: esize = sizeof(*hashtbl_tailq); break;#ifdef DIAGNOSTIC default: panic("hashinit: invalid table type");#endif } if ((p = malloc((u_long)hashsize * esize, mtype, mflags)) == NULL) return (NULL); switch (htype) { case HASH_LIST: hashtbl_list = p; for (i = 0; i < hashsize; i++) LIST_INIT(&hashtbl_list[i]); break; case HASH_TAILQ: hashtbl_tailq = p; for (i = 0; i < hashsize; i++) TAILQ_INIT(&hashtbl_tailq[i]); break; } *hashmask = hashsize - 1; return (p);}/* * Free memory from hash table previosly allocated via hashinit(). */voidhashdone(hashtbl, mtype) void *hashtbl; int mtype;{ free(hashtbl, mtype);}#ifndef OSKIT/* * "Shutdown hook" types, functions, and variables. */struct shutdownhook_desc { LIST_ENTRY(shutdownhook_desc) sfd_list; void (*sfd_fn) __P((void *)); void *sfd_arg;};LIST_HEAD(, shutdownhook_desc) shutdownhook_list;void *shutdownhook_establish(fn, arg) void (*fn) __P((void *)); void *arg;{ struct shutdownhook_desc *ndp; ndp = (struct shutdownhook_desc *) malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT); if (ndp == NULL) return (NULL); ndp->sfd_fn = fn; ndp->sfd_arg = arg; LIST_INSERT_HEAD(&shutdownhook_list, ndp, sfd_list); return (ndp);}voidshutdownhook_disestablish(vhook) void *vhook;{#ifdef DIAGNOSTIC struct shutdownhook_desc *dp; for (dp = shutdownhook_list.lh_first; dp != NULL; dp = dp->sfd_list.le_next) if (dp == vhook) break; if (dp == NULL) panic("shutdownhook_disestablish: hook not established");#endif LIST_REMOVE((struct shutdownhook_desc *)vhook, sfd_list); free(vhook, M_DEVBUF);}/* * Run shutdown hooks. Should be invoked immediately before the * system is halted or rebooted, i.e. after file systems unmounted, * after crash dump done, etc. * * Each shutdown hook is removed from the list before it's run, so that * it won't be run again. */voiddoshutdownhooks(){ struct shutdownhook_desc *dp; while ((dp = shutdownhook_list.lh_first) != NULL) { LIST_REMOVE(dp, sfd_list); (*dp->sfd_fn)(dp->sfd_arg);#if 0 /* * Don't bother freeing the hook structure,, since we may * be rebooting because of a memory corruption problem, * and this might only make things worse. It doesn't * matter, anyway, since the system is just about to * reboot. */ free(dp, M_DEVBUF);#endif }}/* * "Power hook" types, functions, and variables. * The list of power hooks is kept ordered with the last registered hook * first. * When running the hooks on power down the hooks are called in reverse * registration order, when powering up in registration order. */struct powerhook_desc { CIRCLEQ_ENTRY(powerhook_desc) sfd_list; void (*sfd_fn) __P((int, void *)); void *sfd_arg;};CIRCLEQ_HEAD(, powerhook_desc) powerhook_list = CIRCLEQ_HEAD_INITIALIZER(powerhook_list);void *powerhook_establish(fn, arg) void (*fn) __P((int, void *)); void *arg;{ struct powerhook_desc *ndp; ndp = (struct powerhook_desc *) malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT); if (ndp == NULL) return (NULL); ndp->sfd_fn = fn; ndp->sfd_arg = arg; CIRCLEQ_INSERT_HEAD(&powerhook_list, ndp, sfd_list); return (ndp);}voidpowerhook_disestablish(vhook) void *vhook;{#ifdef DIAGNOSTIC struct powerhook_desc *dp; CIRCLEQ_FOREACH(dp, &powerhook_list, sfd_list) if (dp == vhook) goto found; panic("powerhook_disestablish: hook not established"); found:#endif CIRCLEQ_REMOVE(&powerhook_list, (struct powerhook_desc *)vhook, sfd_list); free(vhook, M_DEVBUF);}/* * Run power hooks. */voiddopowerhooks(why) int why;{ struct powerhook_desc *dp; if (why == PWR_RESUME || why == PWR_SOFTRESUME) { CIRCLEQ_FOREACH_REVERSE(dp, &powerhook_list, sfd_list) { (*dp->sfd_fn)(why, dp->sfd_arg); } } else { CIRCLEQ_FOREACH(dp, &powerhook_list, sfd_list) { (*dp->sfd_fn)(why, dp->sfd_arg); } }}/* * "Mountroot hook" types, functions, and variables. */struct mountroothook_desc { LIST_ENTRY(mountroothook_desc) mrd_list; struct device *mrd_device; void (*mrd_func) __P((struct device *));};LIST_HEAD(, mountroothook_desc) mountroothook_list;void *mountroothook_establish(func, dev) void (*func) __P((struct device *)); struct device *dev;{ struct mountroothook_desc *mrd; mrd = (struct mountroothook_desc *) malloc(sizeof(*mrd), M_DEVBUF, M_NOWAIT); if (mrd == NULL) return (NULL); mrd->mrd_device = dev; mrd->mrd_func = func; LIST_INSERT_HEAD(&mountroothook_list, mrd, mrd_list); return (mrd);}voidmountroothook_disestablish(vhook) void *vhook;{#ifdef DIAGNOSTIC struct mountroothook_desc *mrd; for (mrd = mountroothook_list.lh_first; mrd != NULL; mrd = mrd->mrd_list.le_next) if (mrd == vhook) break; if (mrd == NULL) panic("mountroothook_disestablish: hook not established");#endif LIST_REMOVE((struct mountroothook_desc *)vhook, mrd_list); free(vhook, M_DEVBUF);}voidmountroothook_destroy(){ struct mountroothook_desc *mrd; while ((mrd = mountroothook_list.lh_first) != NULL) { LIST_REMOVE(mrd, mrd_list); free(mrd, M_DEVBUF); }}voiddomountroothook(){ struct mountroothook_desc *mrd; for (mrd = mountroothook_list.lh_first; mrd != NULL; mrd = mrd->mrd_list.le_next) { if (mrd->mrd_device == root_device) { (*mrd->mrd_func)(root_device); return; } }}/* * Exec hook code. */struct exechook_desc { LIST_ENTRY(exechook_desc) ehk_list; void (*ehk_fn) __P((struct proc *, void *)); void *ehk_arg;};LIST_HEAD(, exechook_desc) exechook_list;void *exechook_establish(fn, arg) void (*fn) __P((struct proc *, void *)); void *arg;{ struct exechook_desc *edp; edp = (struct exechook_desc *) malloc(sizeof(*edp), M_DEVBUF, M_NOWAIT); if (edp == NULL) return (NULL); edp->ehk_fn = fn; edp->ehk_arg = arg; LIST_INSERT_HEAD(&exechook_list, edp, ehk_list); return (edp);}voidexechook_disestablish(vhook) void *vhook;{#ifdef DIAGNOSTIC struct exechook_desc *edp; for (edp = exechook_list.lh_first; edp != NULL; edp = edp->ehk_list.le_next) if (edp == vhook) break; if (edp == NULL) panic("exechook_disestablish: hook not established");#endif LIST_REMOVE((struct exechook_desc *)vhook, ehk_list); free(vhook, M_DEVBUF);}/* * Run exec hooks. */void
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?