kern_kthread.c revision 1.1
1/* $NetBSD: kern_kthread.c,v 1.1 1998/11/11 22:44:24 thorpej Exp $ */ 2 3/*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/kernel.h> 43#include <sys/kthread.h> 44#include <sys/proc.h> 45#include <sys/wait.h> 46 47/* 48 * note that stdarg.h and the ansi style va_start macro is used for both 49 * ansi and traditional c complers. 50 * XXX: this requires that stdarg.h define: va_alist and va_dcl 51 */ 52#include <machine/stdarg.h> 53 54/* 55 * Fork a kernel thread. Any process can request this to be done. 56 * The VM space and limits, etc. will be shared with proc0. 57 */ 58int 59#ifdef __STDC__ 60kthread_create(void (*func)(void *), void *arg, 61 struct proc **newpp, const char *fmt, ...) 62#else 63kthread_create(func, arg, newpp, fmt, va_alist) 64 void (*func) __P((void *)); 65 void *arg; 66 struct proc **newpp; 67 const char *fmt; 68 va_dcl 69#endif 70{ 71 struct proc *p2; 72 int error; 73 va_list ap; 74 75 /* First, create the new process. */ 76 error = fork1(&proc0, FORK_SHAREVM, NULL, &p2); 77 if (error) 78 return (error); 79 80 /* 81 * Mark it as a system process and not a candidate for 82 * swapping. Set P_NOCLDWAIT so that children are reparented 83 * to init(8) when they exit. init(8) can easily wait them 84 * out for us. 85 */ 86 p2->p_flag |= P_INMEM | P_SYSTEM | P_NOCLDWAIT; /* XXX */ 87 88 /* Name it as specified. */ 89 va_start(ap, fmt); 90 vsprintf(p2->p_comm, fmt, ap); 91 va_end(ap); 92 93 /* Arrange for it to start at the specified function. */ 94 cpu_set_kpc(p2, func, arg); 95 96 /* All done! */ 97 if (newpp != NULL) 98 *newpp = p2; 99 return (0); 100} 101 102/* 103 * Cause a kernel thread to exit. Assumes the exiting thread is the 104 * current context. 105 */ 106void 107kthread_exit(ecode) 108 int ecode; 109{ 110 111 /* 112 * XXX What do we do with the exit code? Should we even bother 113 * XXX with it? The parent (proc0) isn't going to do much with 114 * XXX it. 115 */ 116 if (ecode != 0) 117 printf("WARNING: thread `%s' (%d) exits with status %d\n", 118 curproc->p_comm, curproc->p_pid, ecode); 119 120 exit1(curproc, W_EXITCODE(ecode, 0)); 121 122 /* 123 * XXX Fool the compiler. Making exit1() __noreturn__ is a can 124 * XXX of worms right now. 125 */ 126 for (;;); 127} 128