Add missing va_end(ap);
[dragonfly.git] / sys / kern / kern_sysmsg.c
1 /*
2  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/kern/Attic/kern_sysmsg.c,v 1.3 2004/07/16 05:51:10 dillon Exp $
35  */
36
37 /*
38  * SYSMSG is our system call message encapsulation and handling subsystem.
39  * System calls are now encapsulated in messages.  A system call can execute
40  * synchronously or asynchronously.  If a system call wishes to run 
41  * asynchronously it returns EASYNC and the process records the pending system
42  * call message in p_sysmsgq.
43  *
44  * SYSMSGs work similarly to LWKT messages in that the originator can request
45  * a synchronous or asynchronous operation in isolation from the actual system
46  * call which can choose to run the system call synchronous or asynchronously
47  * (independant of what was requested).  Like LWKT messages, the synchronous
48  * path avoids all queueing operations and is almost as fast as making a 
49  * direct procedure call.
50  */
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/proc.h>
55 #include <sys/pioctl.h>
56 #include <sys/kernel.h>
57 #include <sys/resourcevar.h>
58 #include <sys/signalvar.h>
59 #include <sys/syscall.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysent.h>
62 #include <sys/uio.h>
63 #include <sys/vmmeter.h>
64 #include <sys/malloc.h>
65 #ifdef KTRACE
66 #include <sys/ktrace.h>
67 #endif
68 #include <sys/upcall.h>
69 #include <sys/sysproto.h>
70 #include <sys/sysunion.h>
71
72 #include <vm/vm.h>
73 #include <vm/vm_param.h>
74 #include <sys/lock.h>
75 #include <vm/pmap.h>
76 #include <vm/vm_kern.h>
77 #include <vm/vm_map.h>
78 #include <vm/vm_page.h>
79 #include <vm/vm_extern.h>
80
81 #include <sys/msgport2.h>
82 #include <sys/thread2.h>
83
84 /*
85  * Wait for a system call message to be returned.  If NULL is passed we
86  * wait for the next ready sysmsg and return it.  We return NULL if there
87  * are no pending sysmsgs queued.
88  *
89  * NOTE: proc must be curproc.
90  */
91 struct sysmsg *
92 sysmsg_wait(struct proc *p, struct sysmsg *sysmsg, int nonblock)
93 {
94         thread_t td = p->p_thread;
95
96         /*
97          * Get the next finished system call or the specified system call,
98          * blocking until it is finished (if requested).
99          */
100         if (sysmsg == NULL) {
101                 if (TAILQ_FIRST(&p->p_sysmsgq) == NULL)
102                         return(NULL);
103                 if (nonblock) {
104                         if ((sysmsg = lwkt_getport(&td->td_msgport)) == NULL)
105                                 return(NULL);
106                 } else {
107                         sysmsg = lwkt_waitport(&td->td_msgport, NULL);
108                 }
109         } else {
110                 if (nonblock && !lwkt_checkmsg(&sysmsg->lmsg))
111                         return(NULL);
112                 lwkt_waitport(&td->td_msgport, &sysmsg->lmsg);
113         }
114
115         /*
116          * sysmsg is not NULL here
117          */
118         TAILQ_REMOVE(&p->p_sysmsgq, sysmsg, msgq);
119         return(sysmsg);
120 }
121
122 /*
123  * Wait for all pending asynchronous system calls to complete, aborting them
124  * if requested (XXX).
125  */
126 void
127 sysmsg_rundown(struct proc *p, int doabort)
128 {
129         struct sysmsg *sysmsg;
130         thread_t td = p->p_thread;
131         globaldata_t gd = td->td_gd;
132
133         while (TAILQ_FIRST(&p->p_sysmsgq) != NULL) {
134                 printf("WAITSYSMSG\n");
135                 sysmsg = sysmsg_wait(p, NULL, 0);
136                 printf("WAITSYSMSG %p\n", sysmsg);
137                 KKASSERT(sysmsg != NULL);
138                 /* XXX don't bother with pending copyouts */
139                 /* XXX we really should do pending copyouts */
140                 crit_enter_quick(td);
141                 sysmsg->lmsg.opaque.ms_sysunnext = gd->gd_freesysun;
142                 gd->gd_freesysun = (void *)sysmsg;
143                 crit_exit_quick(td);
144         }
145 }
146