2 * Copyright (c) 2003 Galen Sampson <galen_sampson@yahoo.com>
3 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
27 * $DragonFly: src/lib/libcaps/sysport.c,v 1.4 2004/07/29 08:55:02 dillon Exp $
32 #include <sys/syscall.h>
33 #include <sys/sysproto.h>
34 #include <sys/sysunion.h>
43 static int sysport_putport(lwkt_port_t port
, lwkt_msg_t msg
);
44 static void sysport_loop(void *dummy
);
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 /************************************************************************
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().
71 sysport_putport(lwkt_port_t port
, lwkt_msg_t msg
)
74 thread_t td
= port
->mp_td
;
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
)
83 * The message is not done.
85 msg
->ms_flags
&= ~MSGF_DONE
;
86 error
= sendsys(NULL
, msg
, msg
->ms_msgsize
);
89 TAILQ_INSERT_TAIL(&port
->mp_msgq
, msg
, ms_node
);
90 msg
->ms_flags
|= MSGF_QUEUED
;
92 * Shouldn't need this check, we are always waiting
94 if(port
->mp_flags
& MSGPORTF_WAITING
)
100 printf("async return error %d\n", error
);
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
;
121 printf("Warning, only super user can send asynchonous system messages\n");
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
;
138 lwkt_syswaitport(lwkt_msg_t msg
)
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);
150 printf(" rmsg %p\n", rmsg
);
157 /************************************************************************
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
167 sysport_loop(void *dummy
)
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
);
186 msg
->ms_flags
|= MSGF_DONE
;