gcc41: install mm_malloc.h
[dragonfly.git] / lib / libcaps / sysport.c
blob34212d124cae8636a71a91b5afe28b3cb4b9226b
1 /*
2 * Copyright (c) 2003 Galen Sampson <galen_sampson@yahoo.com>
3 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
27 * $DragonFly: src/lib/libcaps/sysport.c,v 1.4 2004/07/29 08:55:02 dillon Exp $
29 #include "defs.h"
31 #include "sendsys.h"
32 #include <sys/syscall.h>
33 #include <sys/sysproto.h>
34 #include <sys/sysunion.h>
36 /* XXX Temporary */
37 #include <unistd.h>
39 #ifdef DEBUG
40 #include <stdio.h>
41 #endif
43 static int sysport_putport(lwkt_port_t port, lwkt_msg_t msg);
44 static void sysport_loop(void *dummy);
46 struct thread sys_td;
47 lwkt_port_t sysport;
49 void
50 sysport_init(void)
52 lwkt_init_thread(&sys_td, libcaps_alloc_stack(LWKT_THREAD_STACK),
53 LWKT_THREAD_STACK, TDF_SYSTHREAD, mycpu);
54 sysport = &sys_td.td_msgport;
55 sysport->mp_putport = sysport_putport;
56 sysport->mp_flags = MSGPORTF_WAITING; /* XXX temporary */
57 cpu_set_thread_handler(&sys_td, lwkt_exit, sysport_loop, NULL);
60 /************************************************************************
61 * PORT FUNCTIONS *
62 ************************************************************************/
65 * XXX We might need to separte this function in the way *lwkt_putport*
66 * is separated in the case of multiple cpus. Secifically we will need
67 * a lwkt_sysputport_remote().
69 static
70 int
71 sysport_putport(lwkt_port_t port, lwkt_msg_t msg)
73 int error = 0;
74 thread_t td = port->mp_td;
76 /**
77 * XXX sendsys() will only allow asynchronous messages from uid 0. This
78 * is a kernel issue. See note below.
80 if(msg->ms_flags & MSGF_ASYNC)
82 /**
83 * The message is not done.
85 msg->ms_flags &= ~MSGF_DONE;
86 error = sendsys(NULL, msg, msg->ms_msgsize);
87 if(error == EASYNC)
89 TAILQ_INSERT_TAIL(&port->mp_msgq, msg, ms_node);
90 msg->ms_flags |= MSGF_QUEUED;
91 /**
92 * Shouldn't need this check, we are always waiting
94 if(port->mp_flags & MSGPORTF_WAITING)
96 lwkt_schedule(td);
99 #ifdef DEBUG
100 printf("async return error %d\n", error);
101 #endif
105 * XXX this is a temporary hack until the kernel changes to implement
106 * the desired asynchronous goals.
108 * The current asynchronous messaging systemcall interface that sendsys
109 * uses has some potential security issues and is limited to use by the
110 * superuser only. Synchronous messages are allowed by anyone. Sendsys
111 * returns EPERM in the case where you are not the superuser but tried to
112 * send an asynchonous message.
114 * If you are not the super user then the system call will be made again,
115 * but without MSGF_ASYNC set.
117 if(error != EASYNC && error == EPERM)
119 msg->ms_flags &= ~MSGF_ASYNC;
120 #ifdef DEBUG
121 printf("Warning, only super user can send asynchonous system messages\n");
122 #endif
126 * The message is synchronous. Send it sychronously.
128 if((msg->ms_flags & MSGF_ASYNC) == 0)
130 error = sendsys(NULL, msg, msg->ms_msgsize);
131 msg->ms_flags |= MSGF_DONE;
134 return(error);
137 void *
138 lwkt_syswaitport(lwkt_msg_t msg)
140 lwkt_msg_t rmsg;
143 * Block awaiting a return from the kernel.
145 for(rmsg = (lwkt_msg_t)sendsys(NULL, NULL, -1); rmsg != msg; )
147 usleep(1000000 / 10);
148 rmsg = (lwkt_msg_t)sendsys(NULL, NULL, -1);
149 #ifdef DEBUG
150 printf(" rmsg %p\n", rmsg);
151 #endif
154 return msg;
157 /************************************************************************
158 * THREAD FUNCTIONS *
159 ************************************************************************/
161 * XXX Temporary function that provides a mechanism to return an asynchronous
162 * message completed by the kernel to be returned to the port it originated
163 * from.
165 static
166 void
167 sysport_loop(void *dummy)
169 lwkt_msg_t msg;
171 for(;;)
173 msg = lwkt_waitport(&curthread->td_msgport, NULL);
175 msg = lwkt_syswaitport(msg);
178 * The message was asynchronous
180 if(msg->ms_flags & MSGF_ASYNC)
182 lwkt_replymsg(msg, msg->ms_error);
184 else
186 msg->ms_flags |= MSGF_DONE;