1433d6423SLionel Sambuc /* This task handles the interface between the kernel and user-level servers.
2433d6423SLionel Sambuc * System services can be accessed by doing a system call. System calls are
3433d6423SLionel Sambuc * transformed into request messages, which are handled by this task. By
4433d6423SLionel Sambuc * convention, a sys_call() is transformed in a SYS_CALL request message that
5433d6423SLionel Sambuc * is handled in a function named do_call().
6433d6423SLionel Sambuc *
7433d6423SLionel Sambuc * A private call vector is used to map all system calls to the functions that
8433d6423SLionel Sambuc * handle them. The actual handler functions are contained in separate files
9433d6423SLionel Sambuc * to keep this file clean. The call vector is used in the system task's main
10433d6423SLionel Sambuc * loop to handle all incoming requests.
11433d6423SLionel Sambuc *
12433d6423SLionel Sambuc * In addition to the main sys_task() entry point, which starts the main loop,
13433d6423SLionel Sambuc * there are several other minor entry points:
14433d6423SLionel Sambuc * get_priv: assign privilege structure to user or system process
15433d6423SLionel Sambuc * set_sendto_bit: allow a process to send messages to a new target
16433d6423SLionel Sambuc * unset_sendto_bit: disallow a process from sending messages to a target
17433d6423SLionel Sambuc * fill_sendto_mask: fill the target mask of a given process
18433d6423SLionel Sambuc * send_sig: send a signal directly to a system process
19433d6423SLionel Sambuc * cause_sig: take action to cause a signal to occur via a signal mgr
20433d6423SLionel Sambuc * sig_delay_done: tell PM that a process is not sending
21433d6423SLionel Sambuc * send_diag_sig: send a diagnostics signal to interested processes
22433d6423SLionel Sambuc * get_randomness: accumulate randomness in a buffer
23433d6423SLionel Sambuc * clear_endpoint: remove a process' ability to send and receive messages
24433d6423SLionel Sambuc * sched_proc: schedule a process
25433d6423SLionel Sambuc *
26433d6423SLionel Sambuc * Changes:
27433d6423SLionel Sambuc * Nov 22, 2009 get_priv supports static priv ids (Cristiano Giuffrida)
28433d6423SLionel Sambuc * Aug 04, 2005 check if system call is allowed (Jorrit N. Herder)
29433d6423SLionel Sambuc * Jul 20, 2005 send signal to services with message (Jorrit N. Herder)
30433d6423SLionel Sambuc * Jan 15, 2005 new, generalized virtual copy function (Jorrit N. Herder)
31433d6423SLionel Sambuc * Oct 10, 2004 dispatch system calls from call vector (Jorrit N. Herder)
32433d6423SLionel Sambuc * Sep 30, 2004 source code documentation updated (Jorrit N. Herder)
33433d6423SLionel Sambuc */
34433d6423SLionel Sambuc
35433d6423SLionel Sambuc #include "kernel/system.h"
36433d6423SLionel Sambuc #include "kernel/vm.h"
37433d6423SLionel Sambuc #include "kernel/clock.h"
38433d6423SLionel Sambuc #include <stdlib.h>
39c8a9900bSCristiano Giuffrida #include <stddef.h>
40433d6423SLionel Sambuc #include <assert.h>
41433d6423SLionel Sambuc #include <signal.h>
42433d6423SLionel Sambuc #include <unistd.h>
43433d6423SLionel Sambuc #include <minix/endpoint.h>
44433d6423SLionel Sambuc #include <minix/safecopies.h>
45433d6423SLionel Sambuc
46433d6423SLionel Sambuc /* Declaration of the call vector that defines the mapping of system calls
47433d6423SLionel Sambuc * to handler functions. The vector is initialized in sys_init() with map(),
48433d6423SLionel Sambuc * which makes sure the system call numbers are ok. No space is allocated,
49433d6423SLionel Sambuc * because the dummy is declared extern. If an illegal call is given, the
50433d6423SLionel Sambuc * array size will be negative and this won't compile.
51433d6423SLionel Sambuc */
52433d6423SLionel Sambuc static int (*call_vec[NR_SYS_CALLS])(struct proc * caller, message *m_ptr);
53433d6423SLionel Sambuc
54433d6423SLionel Sambuc #define map(call_nr, handler) \
55433d6423SLionel Sambuc { int call_index = call_nr-KERNEL_CALL; \
56433d6423SLionel Sambuc assert(call_index >= 0 && call_index < NR_SYS_CALLS); \
57433d6423SLionel Sambuc call_vec[call_index] = (handler) ; }
58433d6423SLionel Sambuc
kernel_call_finish(struct proc * caller,message * msg,int result)59433d6423SLionel Sambuc static void kernel_call_finish(struct proc * caller, message *msg, int result)
60433d6423SLionel Sambuc {
61433d6423SLionel Sambuc if(result == VMSUSPEND) {
62433d6423SLionel Sambuc /* Special case: message has to be saved for handling
63433d6423SLionel Sambuc * until VM tells us it's allowed. VM has been notified
64433d6423SLionel Sambuc * and we must wait for its reply to restart the call.
65433d6423SLionel Sambuc */
66433d6423SLionel Sambuc assert(RTS_ISSET(caller, RTS_VMREQUEST));
67433d6423SLionel Sambuc assert(caller->p_vmrequest.type == VMSTYPE_KERNELCALL);
68433d6423SLionel Sambuc caller->p_vmrequest.saved.reqmsg = *msg;
69433d6423SLionel Sambuc caller->p_misc_flags |= MF_KCALL_RESUME;
70433d6423SLionel Sambuc } else {
71433d6423SLionel Sambuc /*
72433d6423SLionel Sambuc * call is finished, we could have been suspended because of VM,
73433d6423SLionel Sambuc * remove the request message
74433d6423SLionel Sambuc */
75433d6423SLionel Sambuc caller->p_vmrequest.saved.reqmsg.m_source = NONE;
76433d6423SLionel Sambuc if (result != EDONTREPLY) {
77433d6423SLionel Sambuc /* copy the result as a message to the original user buffer */
78433d6423SLionel Sambuc msg->m_source = SYSTEM;
79433d6423SLionel Sambuc msg->m_type = result; /* report status of call */
80433d6423SLionel Sambuc #if DEBUG_IPC_HOOK
81433d6423SLionel Sambuc hook_ipc_msgkresult(msg, caller);
82433d6423SLionel Sambuc #endif
83433d6423SLionel Sambuc if (copy_msg_to_user(msg, (message *)caller->p_delivermsg_vir)) {
84433d6423SLionel Sambuc printf("WARNING wrong user pointer 0x%08x from "
85433d6423SLionel Sambuc "process %s / %d\n",
86433d6423SLionel Sambuc caller->p_delivermsg_vir,
87433d6423SLionel Sambuc caller->p_name,
88433d6423SLionel Sambuc caller->p_endpoint);
89433d6423SLionel Sambuc cause_sig(proc_nr(caller), SIGSEGV);
90433d6423SLionel Sambuc }
91433d6423SLionel Sambuc }
92433d6423SLionel Sambuc }
93433d6423SLionel Sambuc }
94433d6423SLionel Sambuc
kernel_call_dispatch(struct proc * caller,message * msg)95433d6423SLionel Sambuc static int kernel_call_dispatch(struct proc * caller, message *msg)
96433d6423SLionel Sambuc {
97433d6423SLionel Sambuc int result = OK;
98433d6423SLionel Sambuc int call_nr;
99433d6423SLionel Sambuc
100433d6423SLionel Sambuc #if DEBUG_IPC_HOOK
101433d6423SLionel Sambuc hook_ipc_msgkcall(msg, caller);
102433d6423SLionel Sambuc #endif
103433d6423SLionel Sambuc call_nr = msg->m_type - KERNEL_CALL;
104433d6423SLionel Sambuc
105433d6423SLionel Sambuc /* See if the caller made a valid request and try to handle it. */
106433d6423SLionel Sambuc if (call_nr < 0 || call_nr >= NR_SYS_CALLS) { /* check call number */
107433d6423SLionel Sambuc printf("SYSTEM: illegal request %d from %d.\n",
108433d6423SLionel Sambuc call_nr,msg->m_source);
109433d6423SLionel Sambuc result = EBADREQUEST; /* illegal message type */
110433d6423SLionel Sambuc }
111433d6423SLionel Sambuc else if (!GET_BIT(priv(caller)->s_k_call_mask, call_nr)) {
112433d6423SLionel Sambuc printf("SYSTEM: denied request %d from %d.\n",
113433d6423SLionel Sambuc call_nr,msg->m_source);
114433d6423SLionel Sambuc result = ECALLDENIED; /* illegal message type */
115433d6423SLionel Sambuc } else {
116433d6423SLionel Sambuc /* handle the system call */
117433d6423SLionel Sambuc if (call_vec[call_nr])
118433d6423SLionel Sambuc result = (*call_vec[call_nr])(caller, msg);
119433d6423SLionel Sambuc else {
120433d6423SLionel Sambuc printf("Unused kernel call %d from %d\n",
121433d6423SLionel Sambuc call_nr, caller->p_endpoint);
122433d6423SLionel Sambuc result = EBADREQUEST;
123433d6423SLionel Sambuc }
124433d6423SLionel Sambuc }
125433d6423SLionel Sambuc
126433d6423SLionel Sambuc return result;
127433d6423SLionel Sambuc }
128433d6423SLionel Sambuc
129433d6423SLionel Sambuc /*===========================================================================*
130433d6423SLionel Sambuc * kernel_call *
131433d6423SLionel Sambuc *===========================================================================*/
132433d6423SLionel Sambuc /*
133433d6423SLionel Sambuc * this function checks the basic syscall parameters and if accepted it
134433d6423SLionel Sambuc * dispatches its handling to the right handler
135433d6423SLionel Sambuc */
kernel_call(message * m_user,struct proc * caller)136433d6423SLionel Sambuc void kernel_call(message *m_user, struct proc * caller)
137433d6423SLionel Sambuc {
138433d6423SLionel Sambuc int result = OK;
139433d6423SLionel Sambuc message msg;
140433d6423SLionel Sambuc
141433d6423SLionel Sambuc caller->p_delivermsg_vir = (vir_bytes) m_user;
142433d6423SLionel Sambuc /*
143433d6423SLionel Sambuc * the ldt and cr3 of the caller process is loaded because it just've trapped
144433d6423SLionel Sambuc * into the kernel or was already set in switch_to_user() before we resume
145433d6423SLionel Sambuc * execution of an interrupted kernel call
146433d6423SLionel Sambuc */
147433d6423SLionel Sambuc if (copy_msg_from_user(m_user, &msg) == 0) {
148433d6423SLionel Sambuc msg.m_source = caller->p_endpoint;
149433d6423SLionel Sambuc result = kernel_call_dispatch(caller, &msg);
150433d6423SLionel Sambuc }
151433d6423SLionel Sambuc else {
152433d6423SLionel Sambuc printf("WARNING wrong user pointer 0x%08x from process %s / %d\n",
153433d6423SLionel Sambuc m_user, caller->p_name, caller->p_endpoint);
154433d6423SLionel Sambuc cause_sig(proc_nr(caller), SIGSEGV);
155433d6423SLionel Sambuc return;
156433d6423SLionel Sambuc }
157433d6423SLionel Sambuc
158433d6423SLionel Sambuc
159433d6423SLionel Sambuc /* remember who invoked the kcall so we can bill it its time */
160433d6423SLionel Sambuc kbill_kcall = caller;
161433d6423SLionel Sambuc
162433d6423SLionel Sambuc kernel_call_finish(caller, &msg, result);
163433d6423SLionel Sambuc }
164433d6423SLionel Sambuc
165433d6423SLionel Sambuc /*===========================================================================*
166433d6423SLionel Sambuc * initialize *
167433d6423SLionel Sambuc *===========================================================================*/
system_init(void)168433d6423SLionel Sambuc void system_init(void)
169433d6423SLionel Sambuc {
170433d6423SLionel Sambuc register struct priv *sp;
171433d6423SLionel Sambuc int i;
172433d6423SLionel Sambuc
173433d6423SLionel Sambuc /* Initialize IRQ handler hooks. Mark all hooks available. */
174433d6423SLionel Sambuc for (i=0; i<NR_IRQ_HOOKS; i++) {
175433d6423SLionel Sambuc irq_hooks[i].proc_nr_e = NONE;
176433d6423SLionel Sambuc }
177433d6423SLionel Sambuc
178433d6423SLionel Sambuc /* Initialize all alarm timers for all processes. */
179433d6423SLionel Sambuc for (sp=BEG_PRIV_ADDR; sp < END_PRIV_ADDR; sp++) {
180433d6423SLionel Sambuc tmr_inittimer(&(sp->s_alarm_timer));
181433d6423SLionel Sambuc }
182433d6423SLionel Sambuc
183433d6423SLionel Sambuc /* Initialize the call vector to a safe default handler. Some system calls
184433d6423SLionel Sambuc * may be disabled or nonexistant. Then explicitly map known calls to their
185433d6423SLionel Sambuc * handler functions. This is done with a macro that gives a compile error
186433d6423SLionel Sambuc * if an illegal call number is used. The ordering is not important here.
187433d6423SLionel Sambuc */
188433d6423SLionel Sambuc for (i=0; i<NR_SYS_CALLS; i++) {
189433d6423SLionel Sambuc call_vec[i] = NULL;
190433d6423SLionel Sambuc }
191433d6423SLionel Sambuc
192433d6423SLionel Sambuc /* Process management. */
193433d6423SLionel Sambuc map(SYS_FORK, do_fork); /* a process forked a new process */
194433d6423SLionel Sambuc map(SYS_EXEC, do_exec); /* update process after execute */
195433d6423SLionel Sambuc map(SYS_CLEAR, do_clear); /* clean up after process exit */
196433d6423SLionel Sambuc map(SYS_EXIT, do_exit); /* a system process wants to exit */
197433d6423SLionel Sambuc map(SYS_PRIVCTL, do_privctl); /* system privileges control */
198433d6423SLionel Sambuc map(SYS_TRACE, do_trace); /* request a trace operation */
199433d6423SLionel Sambuc map(SYS_SETGRANT, do_setgrant); /* get/set own parameters */
200433d6423SLionel Sambuc map(SYS_RUNCTL, do_runctl); /* set/clear stop flag of a process */
201433d6423SLionel Sambuc map(SYS_UPDATE, do_update); /* update a process into another */
202433d6423SLionel Sambuc map(SYS_STATECTL, do_statectl); /* let a process control its state */
203433d6423SLionel Sambuc
204433d6423SLionel Sambuc /* Signal handling. */
205433d6423SLionel Sambuc map(SYS_KILL, do_kill); /* cause a process to be signaled */
206433d6423SLionel Sambuc map(SYS_GETKSIG, do_getksig); /* signal manager checks for signals */
207433d6423SLionel Sambuc map(SYS_ENDKSIG, do_endksig); /* signal manager finished signal */
208433d6423SLionel Sambuc map(SYS_SIGSEND, do_sigsend); /* start POSIX-style signal */
209433d6423SLionel Sambuc map(SYS_SIGRETURN, do_sigreturn); /* return from POSIX-style signal */
210433d6423SLionel Sambuc
211433d6423SLionel Sambuc /* Device I/O. */
212433d6423SLionel Sambuc map(SYS_IRQCTL, do_irqctl); /* interrupt control operations */
213433d6423SLionel Sambuc #if defined(__i386__)
214433d6423SLionel Sambuc map(SYS_DEVIO, do_devio); /* inb, inw, inl, outb, outw, outl */
215433d6423SLionel Sambuc map(SYS_VDEVIO, do_vdevio); /* vector with devio requests */
216433d6423SLionel Sambuc #endif
217433d6423SLionel Sambuc
218433d6423SLionel Sambuc /* Memory management. */
219433d6423SLionel Sambuc map(SYS_MEMSET, do_memset); /* write char to memory area */
220433d6423SLionel Sambuc map(SYS_VMCTL, do_vmctl); /* various VM process settings */
221433d6423SLionel Sambuc
222433d6423SLionel Sambuc /* Copying. */
223433d6423SLionel Sambuc map(SYS_UMAP, do_umap); /* map virtual to physical address */
224433d6423SLionel Sambuc map(SYS_UMAP_REMOTE, do_umap_remote); /* do_umap for non-caller process */
225433d6423SLionel Sambuc map(SYS_VUMAP, do_vumap); /* vectored virtual to physical map */
226433d6423SLionel Sambuc map(SYS_VIRCOPY, do_vircopy); /* use pure virtual addressing */
227433d6423SLionel Sambuc map(SYS_PHYSCOPY, do_copy); /* use physical addressing */
228433d6423SLionel Sambuc map(SYS_SAFECOPYFROM, do_safecopy_from);/* copy with pre-granted permission */
229433d6423SLionel Sambuc map(SYS_SAFECOPYTO, do_safecopy_to); /* copy with pre-granted permission */
230433d6423SLionel Sambuc map(SYS_VSAFECOPY, do_vsafecopy); /* vectored safecopy */
231433d6423SLionel Sambuc
232433d6423SLionel Sambuc /* safe memset */
233433d6423SLionel Sambuc map(SYS_SAFEMEMSET, do_safememset); /* safememset */
234433d6423SLionel Sambuc
235433d6423SLionel Sambuc /* Clock functionality. */
236433d6423SLionel Sambuc map(SYS_TIMES, do_times); /* get uptime and process times */
237433d6423SLionel Sambuc map(SYS_SETALARM, do_setalarm); /* schedule a synchronous alarm */
238433d6423SLionel Sambuc map(SYS_STIME, do_stime); /* set the boottime */
239433d6423SLionel Sambuc map(SYS_SETTIME, do_settime); /* set the system time (realtime) */
240433d6423SLionel Sambuc map(SYS_VTIMER, do_vtimer); /* set or retrieve a virtual timer */
241433d6423SLionel Sambuc
242433d6423SLionel Sambuc /* System control. */
243433d6423SLionel Sambuc map(SYS_ABORT, do_abort); /* abort MINIX */
244433d6423SLionel Sambuc map(SYS_GETINFO, do_getinfo); /* request system information */
245433d6423SLionel Sambuc map(SYS_DIAGCTL, do_diagctl); /* diagnostics-related functionality */
246433d6423SLionel Sambuc
247433d6423SLionel Sambuc /* Profiling. */
248433d6423SLionel Sambuc map(SYS_SPROF, do_sprofile); /* start/stop statistical profiling */
249433d6423SLionel Sambuc
250433d6423SLionel Sambuc /* arm-specific. */
251433d6423SLionel Sambuc #if defined(__arm__)
252433d6423SLionel Sambuc map(SYS_PADCONF, do_padconf); /* configure pinmux */
253433d6423SLionel Sambuc #endif
254433d6423SLionel Sambuc
255433d6423SLionel Sambuc /* i386-specific. */
256433d6423SLionel Sambuc #if defined(__i386__)
257433d6423SLionel Sambuc map(SYS_READBIOS, do_readbios); /* read from BIOS locations */
258433d6423SLionel Sambuc map(SYS_IOPENABLE, do_iopenable); /* Enable I/O */
259433d6423SLionel Sambuc map(SYS_SDEVIO, do_sdevio); /* phys_insb, _insw, _outsb, _outsw */
260433d6423SLionel Sambuc #endif
261433d6423SLionel Sambuc
262433d6423SLionel Sambuc /* Machine state switching. */
263433d6423SLionel Sambuc map(SYS_SETMCONTEXT, do_setmcontext); /* set machine context */
264433d6423SLionel Sambuc map(SYS_GETMCONTEXT, do_getmcontext); /* get machine context */
265433d6423SLionel Sambuc
266433d6423SLionel Sambuc /* Scheduling */
267433d6423SLionel Sambuc map(SYS_SCHEDULE, do_schedule); /* reschedule a process */
268433d6423SLionel Sambuc map(SYS_SCHEDCTL, do_schedctl); /* change process scheduler */
269433d6423SLionel Sambuc
270433d6423SLionel Sambuc }
271433d6423SLionel Sambuc /*===========================================================================*
272433d6423SLionel Sambuc * get_priv *
273433d6423SLionel Sambuc *===========================================================================*/
get_priv(register struct proc * rc,int priv_id)274*6077d1adSDr. Florian Grätz int get_priv(
275*6077d1adSDr. Florian Grätz register struct proc *rc, /* new (child) process pointer */
276*6077d1adSDr. Florian Grätz int priv_id /* privilege id */
277*6077d1adSDr. Florian Grätz )
278433d6423SLionel Sambuc {
279433d6423SLionel Sambuc /* Allocate a new privilege structure for a system process. Privilege ids
280433d6423SLionel Sambuc * can be assigned either statically or dynamically.
281433d6423SLionel Sambuc */
282433d6423SLionel Sambuc register struct priv *sp; /* privilege structure */
283433d6423SLionel Sambuc
284433d6423SLionel Sambuc if(priv_id == NULL_PRIV_ID) { /* allocate slot dynamically */
285433d6423SLionel Sambuc for (sp = BEG_DYN_PRIV_ADDR; sp < END_DYN_PRIV_ADDR; ++sp)
286433d6423SLionel Sambuc if (sp->s_proc_nr == NONE) break;
287433d6423SLionel Sambuc if (sp >= END_DYN_PRIV_ADDR) return(ENOSPC);
288433d6423SLionel Sambuc }
289433d6423SLionel Sambuc else { /* allocate slot from id */
290433d6423SLionel Sambuc if(!is_static_priv_id(priv_id)) {
291433d6423SLionel Sambuc return EINVAL; /* invalid static priv id */
292433d6423SLionel Sambuc }
293433d6423SLionel Sambuc if(priv[priv_id].s_proc_nr != NONE) {
294433d6423SLionel Sambuc return EBUSY; /* slot already in use */
295433d6423SLionel Sambuc }
296433d6423SLionel Sambuc sp = &priv[priv_id];
297433d6423SLionel Sambuc }
298433d6423SLionel Sambuc rc->p_priv = sp; /* assign new slot */
299433d6423SLionel Sambuc rc->p_priv->s_proc_nr = proc_nr(rc); /* set association */
300433d6423SLionel Sambuc
301433d6423SLionel Sambuc return(OK);
302433d6423SLionel Sambuc }
303433d6423SLionel Sambuc
304433d6423SLionel Sambuc /*===========================================================================*
305433d6423SLionel Sambuc * set_sendto_bit *
306433d6423SLionel Sambuc *===========================================================================*/
set_sendto_bit(const struct proc * rp,int id)307433d6423SLionel Sambuc void set_sendto_bit(const struct proc *rp, int id)
308433d6423SLionel Sambuc {
309433d6423SLionel Sambuc /* Allow a process to send messages to the process(es) associated with the
310433d6423SLionel Sambuc * system privilege structure with the given ID.
311433d6423SLionel Sambuc */
312433d6423SLionel Sambuc
313433d6423SLionel Sambuc /* Disallow the process from sending to a process privilege structure with no
314433d6423SLionel Sambuc * associated process, and disallow the process from sending to itself.
315433d6423SLionel Sambuc */
316433d6423SLionel Sambuc if (id_to_nr(id) == NONE || priv_id(rp) == id) {
317433d6423SLionel Sambuc unset_sys_bit(priv(rp)->s_ipc_to, id);
318433d6423SLionel Sambuc return;
319433d6423SLionel Sambuc }
320433d6423SLionel Sambuc
321433d6423SLionel Sambuc set_sys_bit(priv(rp)->s_ipc_to, id);
322433d6423SLionel Sambuc
323433d6423SLionel Sambuc /* The process that this process can now send to, must be able to reply (or
324433d6423SLionel Sambuc * vice versa). Therefore, its send mask should be updated as well. Ignore
325433d6423SLionel Sambuc * receivers that don't support traps other than RECEIVE, they can't reply
326433d6423SLionel Sambuc * or send messages anyway.
327433d6423SLionel Sambuc */
328433d6423SLionel Sambuc if (priv_addr(id)->s_trap_mask & ~((1 << RECEIVE)))
329433d6423SLionel Sambuc set_sys_bit(priv_addr(id)->s_ipc_to, priv_id(rp));
330433d6423SLionel Sambuc }
331433d6423SLionel Sambuc
332433d6423SLionel Sambuc /*===========================================================================*
333433d6423SLionel Sambuc * unset_sendto_bit *
334433d6423SLionel Sambuc *===========================================================================*/
unset_sendto_bit(const struct proc * rp,int id)335433d6423SLionel Sambuc void unset_sendto_bit(const struct proc *rp, int id)
336433d6423SLionel Sambuc {
337433d6423SLionel Sambuc /* Prevent a process from sending to another process. Retain the send mask
338433d6423SLionel Sambuc * symmetry by also unsetting the bit for the other direction.
339433d6423SLionel Sambuc */
340433d6423SLionel Sambuc
341433d6423SLionel Sambuc unset_sys_bit(priv(rp)->s_ipc_to, id);
342433d6423SLionel Sambuc
343433d6423SLionel Sambuc unset_sys_bit(priv_addr(id)->s_ipc_to, priv_id(rp));
344433d6423SLionel Sambuc }
345433d6423SLionel Sambuc
346433d6423SLionel Sambuc /*===========================================================================*
347433d6423SLionel Sambuc * fill_sendto_mask *
348433d6423SLionel Sambuc *===========================================================================*/
fill_sendto_mask(const struct proc * rp,sys_map_t * map)349433d6423SLionel Sambuc void fill_sendto_mask(const struct proc *rp, sys_map_t *map)
350433d6423SLionel Sambuc {
351433d6423SLionel Sambuc int i;
352433d6423SLionel Sambuc
353433d6423SLionel Sambuc for (i=0; i < NR_SYS_PROCS; i++) {
354433d6423SLionel Sambuc if (get_sys_bit(*map, i))
355433d6423SLionel Sambuc set_sendto_bit(rp, i);
356433d6423SLionel Sambuc else
357433d6423SLionel Sambuc unset_sendto_bit(rp, i);
358433d6423SLionel Sambuc }
359433d6423SLionel Sambuc }
360433d6423SLionel Sambuc
361433d6423SLionel Sambuc /*===========================================================================*
362433d6423SLionel Sambuc * send_sig *
363433d6423SLionel Sambuc *===========================================================================*/
send_sig(endpoint_t ep,int sig_nr)364433d6423SLionel Sambuc int send_sig(endpoint_t ep, int sig_nr)
365433d6423SLionel Sambuc {
366433d6423SLionel Sambuc /* Notify a system process about a signal. This is straightforward. Simply
367433d6423SLionel Sambuc * set the signal that is to be delivered in the pending signals map and
368433d6423SLionel Sambuc * send a notification with source SYSTEM.
369433d6423SLionel Sambuc */
370433d6423SLionel Sambuc register struct proc *rp;
371433d6423SLionel Sambuc struct priv *priv;
372433d6423SLionel Sambuc int proc_nr;
373433d6423SLionel Sambuc
374433d6423SLionel Sambuc if(!isokendpt(ep, &proc_nr) || isemptyn(proc_nr))
375433d6423SLionel Sambuc return EINVAL;
376433d6423SLionel Sambuc
377433d6423SLionel Sambuc rp = proc_addr(proc_nr);
378433d6423SLionel Sambuc priv = priv(rp);
379433d6423SLionel Sambuc if(!priv) return ENOENT;
380433d6423SLionel Sambuc sigaddset(&priv->s_sig_pending, sig_nr);
381433d6423SLionel Sambuc mini_notify(proc_addr(SYSTEM), rp->p_endpoint);
382433d6423SLionel Sambuc
383433d6423SLionel Sambuc return OK;
384433d6423SLionel Sambuc }
385433d6423SLionel Sambuc
386433d6423SLionel Sambuc /*===========================================================================*
387433d6423SLionel Sambuc * cause_sig *
388433d6423SLionel Sambuc *===========================================================================*/
cause_sig(proc_nr_t proc_nr,int sig_nr)389*6077d1adSDr. Florian Grätz void cause_sig(proc_nr_t proc_nr, int sig_nr)
390433d6423SLionel Sambuc {
391*6077d1adSDr. Florian Grätz /* A system process wants to send signal 'sig_nr' to process 'proc_nr'.
392*6077d1adSDr. Florian Grätz * Examples are:
393433d6423SLionel Sambuc * - HARDWARE wanting to cause a SIGSEGV after a CPU exception
394433d6423SLionel Sambuc * - TTY wanting to cause SIGINT upon getting a DEL
395433d6423SLionel Sambuc * - FS wanting to cause SIGPIPE for a broken pipe
396433d6423SLionel Sambuc * Signals are handled by sending a message to the signal manager assigned to
397433d6423SLionel Sambuc * the process. This function handles the signals and makes sure the signal
398433d6423SLionel Sambuc * manager gets them by sending a notification. The process being signaled
399433d6423SLionel Sambuc * is blocked while the signal manager has not finished all signals for it.
400433d6423SLionel Sambuc * Race conditions between calls to this function and the system calls that
401433d6423SLionel Sambuc * process pending kernel signals cannot exist. Signal related functions are
402433d6423SLionel Sambuc * only called when a user process causes a CPU exception and from the kernel
403433d6423SLionel Sambuc * process level, which runs to completion.
404433d6423SLionel Sambuc */
405433d6423SLionel Sambuc register struct proc *rp, *sig_mgr_rp;
406433d6423SLionel Sambuc endpoint_t sig_mgr;
407433d6423SLionel Sambuc int sig_mgr_proc_nr;
408433d6423SLionel Sambuc int s;
409433d6423SLionel Sambuc
410433d6423SLionel Sambuc /* Lookup signal manager. */
411433d6423SLionel Sambuc rp = proc_addr(proc_nr);
412433d6423SLionel Sambuc sig_mgr = priv(rp)->s_sig_mgr;
413433d6423SLionel Sambuc if(sig_mgr == SELF) sig_mgr = rp->p_endpoint;
414433d6423SLionel Sambuc
415433d6423SLionel Sambuc /* If the target is the signal manager of itself, send the signal directly. */
416433d6423SLionel Sambuc if(rp->p_endpoint == sig_mgr) {
417433d6423SLionel Sambuc if(SIGS_IS_LETHAL(sig_nr)) {
418433d6423SLionel Sambuc /* If the signal is lethal, see if a backup signal manager exists. */
419433d6423SLionel Sambuc sig_mgr = priv(rp)->s_bak_sig_mgr;
420433d6423SLionel Sambuc if(sig_mgr != NONE && isokendpt(sig_mgr, &sig_mgr_proc_nr)) {
421433d6423SLionel Sambuc priv(rp)->s_sig_mgr = sig_mgr;
422433d6423SLionel Sambuc priv(rp)->s_bak_sig_mgr = NONE;
423433d6423SLionel Sambuc sig_mgr_rp = proc_addr(sig_mgr_proc_nr);
424433d6423SLionel Sambuc RTS_UNSET(sig_mgr_rp, RTS_NO_PRIV);
425433d6423SLionel Sambuc cause_sig(proc_nr, sig_nr); /* try again with the new sig mgr. */
426433d6423SLionel Sambuc return;
427433d6423SLionel Sambuc }
428433d6423SLionel Sambuc /* We are out of luck. Time to panic. */
429433d6423SLionel Sambuc proc_stacktrace(rp);
430433d6423SLionel Sambuc panic("cause_sig: sig manager %d gets lethal signal %d for itself",
431433d6423SLionel Sambuc rp->p_endpoint, sig_nr);
432433d6423SLionel Sambuc }
433433d6423SLionel Sambuc sigaddset(&priv(rp)->s_sig_pending, sig_nr);
434433d6423SLionel Sambuc if(OK != send_sig(rp->p_endpoint, SIGKSIGSM))
435433d6423SLionel Sambuc panic("send_sig failed");
436433d6423SLionel Sambuc return;
437433d6423SLionel Sambuc }
438433d6423SLionel Sambuc
4390a6a1f1dSLionel Sambuc s = sigismember(&rp->p_pending, sig_nr);
440433d6423SLionel Sambuc /* Check if the signal is already pending. Process it otherwise. */
441433d6423SLionel Sambuc if (!s) {
442433d6423SLionel Sambuc sigaddset(&rp->p_pending, sig_nr);
443433d6423SLionel Sambuc if (! (RTS_ISSET(rp, RTS_SIGNALED))) { /* other pending */
444433d6423SLionel Sambuc RTS_SET(rp, RTS_SIGNALED | RTS_SIG_PENDING);
445433d6423SLionel Sambuc if(OK != send_sig(sig_mgr, SIGKSIG))
446433d6423SLionel Sambuc panic("send_sig failed");
447433d6423SLionel Sambuc }
448433d6423SLionel Sambuc }
449433d6423SLionel Sambuc }
450433d6423SLionel Sambuc
451433d6423SLionel Sambuc /*===========================================================================*
452433d6423SLionel Sambuc * sig_delay_done *
453433d6423SLionel Sambuc *===========================================================================*/
sig_delay_done(struct proc * rp)454433d6423SLionel Sambuc void sig_delay_done(struct proc *rp)
455433d6423SLionel Sambuc {
456433d6423SLionel Sambuc /* A process is now known not to send any direct messages.
457433d6423SLionel Sambuc * Tell PM that the stop delay has ended, by sending a signal to the process.
458433d6423SLionel Sambuc * Used for actual signal delivery.
459433d6423SLionel Sambuc */
460433d6423SLionel Sambuc
461433d6423SLionel Sambuc rp->p_misc_flags &= ~MF_SIG_DELAY;
462433d6423SLionel Sambuc
463433d6423SLionel Sambuc cause_sig(proc_nr(rp), SIGSNDELAY);
464433d6423SLionel Sambuc }
465433d6423SLionel Sambuc
466433d6423SLionel Sambuc /*===========================================================================*
467433d6423SLionel Sambuc * send_diag_sig *
468433d6423SLionel Sambuc *===========================================================================*/
send_diag_sig(void)469433d6423SLionel Sambuc void send_diag_sig(void)
470433d6423SLionel Sambuc {
471433d6423SLionel Sambuc /* Send a SIGKMESS signal to all processes in receiving updates about new
472433d6423SLionel Sambuc * diagnostics messages.
473433d6423SLionel Sambuc */
474433d6423SLionel Sambuc struct priv *privp;
475433d6423SLionel Sambuc endpoint_t ep;
476433d6423SLionel Sambuc
477433d6423SLionel Sambuc for (privp = BEG_PRIV_ADDR; privp < END_PRIV_ADDR; privp++) {
478433d6423SLionel Sambuc if (privp->s_proc_nr != NONE && privp->s_diag_sig == TRUE) {
479433d6423SLionel Sambuc ep = proc_addr(privp->s_proc_nr)->p_endpoint;
480433d6423SLionel Sambuc send_sig(ep, SIGKMESS);
481433d6423SLionel Sambuc }
482433d6423SLionel Sambuc }
483433d6423SLionel Sambuc }
484433d6423SLionel Sambuc
485433d6423SLionel Sambuc /*===========================================================================*
4863779ed93SDavid van Moolenbroek * clear_memreq *
4873779ed93SDavid van Moolenbroek *===========================================================================*/
clear_memreq(struct proc * rp)4883779ed93SDavid van Moolenbroek static void clear_memreq(struct proc *rp)
4893779ed93SDavid van Moolenbroek {
4903779ed93SDavid van Moolenbroek struct proc **rpp;
4913779ed93SDavid van Moolenbroek
4923779ed93SDavid van Moolenbroek if (!RTS_ISSET(rp, RTS_VMREQUEST))
4933779ed93SDavid van Moolenbroek return; /* nothing to do */
4943779ed93SDavid van Moolenbroek
4953779ed93SDavid van Moolenbroek for (rpp = &vmrequest; *rpp != NULL;
4963779ed93SDavid van Moolenbroek rpp = &(*rpp)->p_vmrequest.nextrequestor) {
4973779ed93SDavid van Moolenbroek if (*rpp == rp) {
4983779ed93SDavid van Moolenbroek *rpp = rp->p_vmrequest.nextrequestor;
4993779ed93SDavid van Moolenbroek break;
5003779ed93SDavid van Moolenbroek }
5013779ed93SDavid van Moolenbroek }
5023779ed93SDavid van Moolenbroek
5033779ed93SDavid van Moolenbroek RTS_UNSET(rp, RTS_VMREQUEST);
5043779ed93SDavid van Moolenbroek }
5053779ed93SDavid van Moolenbroek
5063779ed93SDavid van Moolenbroek /*===========================================================================*
507433d6423SLionel Sambuc * clear_ipc *
508433d6423SLionel Sambuc *===========================================================================*/
clear_ipc(register struct proc * rc)509433d6423SLionel Sambuc static void clear_ipc(
510433d6423SLionel Sambuc register struct proc *rc /* slot of process to clean up */
511433d6423SLionel Sambuc )
512433d6423SLionel Sambuc {
513433d6423SLionel Sambuc /* Clear IPC data for a given process slot. */
514433d6423SLionel Sambuc struct proc **xpp; /* iterate over caller queue */
515433d6423SLionel Sambuc
516433d6423SLionel Sambuc if (RTS_ISSET(rc, RTS_SENDING)) {
517433d6423SLionel Sambuc int target_proc;
518433d6423SLionel Sambuc
519433d6423SLionel Sambuc okendpt(rc->p_sendto_e, &target_proc);
520433d6423SLionel Sambuc xpp = &proc_addr(target_proc)->p_caller_q; /* destination's queue */
521433d6423SLionel Sambuc while (*xpp) { /* check entire queue */
522433d6423SLionel Sambuc if (*xpp == rc) { /* process is on the queue */
523433d6423SLionel Sambuc *xpp = (*xpp)->p_q_link; /* replace by next process */
524433d6423SLionel Sambuc #if DEBUG_ENABLE_IPC_WARNINGS
525433d6423SLionel Sambuc printf("endpoint %d / %s removed from queue at %d\n",
526433d6423SLionel Sambuc rc->p_endpoint, rc->p_name, rc->p_sendto_e);
527433d6423SLionel Sambuc #endif
528433d6423SLionel Sambuc break; /* can only be queued once */
529433d6423SLionel Sambuc }
530433d6423SLionel Sambuc xpp = &(*xpp)->p_q_link; /* proceed to next queued */
531433d6423SLionel Sambuc }
532433d6423SLionel Sambuc RTS_UNSET(rc, RTS_SENDING);
533433d6423SLionel Sambuc }
534433d6423SLionel Sambuc RTS_UNSET(rc, RTS_RECEIVING);
535433d6423SLionel Sambuc }
536433d6423SLionel Sambuc
537433d6423SLionel Sambuc /*===========================================================================*
538433d6423SLionel Sambuc * clear_endpoint *
539433d6423SLionel Sambuc *===========================================================================*/
clear_endpoint(struct proc * rc)540*6077d1adSDr. Florian Grätz void clear_endpoint(struct proc * rc)
541433d6423SLionel Sambuc {
542*6077d1adSDr. Florian Grätz /* Clean up the slot of the process given as 'rc'. */
543433d6423SLionel Sambuc if(isemptyp(rc)) panic("clear_proc: empty process: %d", rc->p_endpoint);
544433d6423SLionel Sambuc
545433d6423SLionel Sambuc
546433d6423SLionel Sambuc #if DEBUG_IPC_HOOK
547433d6423SLionel Sambuc hook_ipc_clear(rc);
548433d6423SLionel Sambuc #endif
549433d6423SLionel Sambuc
550433d6423SLionel Sambuc /* Make sure that the exiting process is no longer scheduled. */
551433d6423SLionel Sambuc RTS_SET(rc, RTS_NO_ENDPOINT);
552433d6423SLionel Sambuc if (priv(rc)->s_flags & SYS_PROC)
553433d6423SLionel Sambuc {
554433d6423SLionel Sambuc priv(rc)->s_asynsize= 0;
555433d6423SLionel Sambuc }
556433d6423SLionel Sambuc
557433d6423SLionel Sambuc /* If the process happens to be queued trying to send a
558433d6423SLionel Sambuc * message, then it must be removed from the message queues.
559433d6423SLionel Sambuc */
560433d6423SLionel Sambuc clear_ipc(rc);
561433d6423SLionel Sambuc
562433d6423SLionel Sambuc /* Likewise, if another process was sending or receive a message to or from
563433d6423SLionel Sambuc * the exiting process, it must be alerted that process no longer is alive.
564433d6423SLionel Sambuc * Check all processes.
565433d6423SLionel Sambuc */
566433d6423SLionel Sambuc clear_ipc_refs(rc, EDEADSRCDST);
567433d6423SLionel Sambuc
5683779ed93SDavid van Moolenbroek /* Finally, if the process was blocked on a VM request, remove it from the
5693779ed93SDavid van Moolenbroek * queue of processes waiting to be processed by VM.
5703779ed93SDavid van Moolenbroek */
5713779ed93SDavid van Moolenbroek clear_memreq(rc);
572433d6423SLionel Sambuc }
573433d6423SLionel Sambuc
574433d6423SLionel Sambuc /*===========================================================================*
575433d6423SLionel Sambuc * clear_ipc_refs *
576433d6423SLionel Sambuc *===========================================================================*/
clear_ipc_refs(register struct proc * rc,int caller_ret)577*6077d1adSDr. Florian Grätz void clear_ipc_refs(
578*6077d1adSDr. Florian Grätz register struct proc *rc, /* slot of process to clean up */
579*6077d1adSDr. Florian Grätz int caller_ret /* code to return on callers */
580*6077d1adSDr. Florian Grätz )
581433d6423SLionel Sambuc {
582433d6423SLionel Sambuc /* Clear IPC references for a given process slot. */
583433d6423SLionel Sambuc struct proc *rp; /* iterate over process table */
584433d6423SLionel Sambuc int src_id;
585433d6423SLionel Sambuc
586433d6423SLionel Sambuc /* Tell processes that sent asynchronous messages to 'rc' they are not
587433d6423SLionel Sambuc * going to be delivered */
588433d6423SLionel Sambuc while ((src_id = has_pending_asend(rc, ANY)) != NULL_PRIV_ID)
589433d6423SLionel Sambuc cancel_async(proc_addr(id_to_nr(src_id)), rc);
590433d6423SLionel Sambuc
591433d6423SLionel Sambuc for (rp = BEG_PROC_ADDR; rp < END_PROC_ADDR; rp++) {
592433d6423SLionel Sambuc if(isemptyp(rp))
593433d6423SLionel Sambuc continue;
594433d6423SLionel Sambuc
595433d6423SLionel Sambuc /* Unset pending notification bits. */
596433d6423SLionel Sambuc unset_sys_bit(priv(rp)->s_notify_pending, priv(rc)->s_id);
597433d6423SLionel Sambuc
598433d6423SLionel Sambuc /* Unset pending asynchronous messages */
599433d6423SLionel Sambuc unset_sys_bit(priv(rp)->s_asyn_pending, priv(rc)->s_id);
600433d6423SLionel Sambuc
601433d6423SLionel Sambuc /* Check if process depends on given process. */
602433d6423SLionel Sambuc if (P_BLOCKEDON(rp) == rc->p_endpoint) {
603433d6423SLionel Sambuc rp->p_reg.retreg = caller_ret; /* return requested code */
604433d6423SLionel Sambuc clear_ipc(rp);
605433d6423SLionel Sambuc }
606433d6423SLionel Sambuc }
607433d6423SLionel Sambuc }
608433d6423SLionel Sambuc
609433d6423SLionel Sambuc /*===========================================================================*
610433d6423SLionel Sambuc * kernel_call_resume *
611433d6423SLionel Sambuc *===========================================================================*/
kernel_call_resume(struct proc * caller)612433d6423SLionel Sambuc void kernel_call_resume(struct proc *caller)
613433d6423SLionel Sambuc {
614433d6423SLionel Sambuc int result;
615433d6423SLionel Sambuc
616433d6423SLionel Sambuc assert(!RTS_ISSET(caller, RTS_SLOT_FREE));
617433d6423SLionel Sambuc assert(!RTS_ISSET(caller, RTS_VMREQUEST));
618433d6423SLionel Sambuc
619433d6423SLionel Sambuc assert(caller->p_vmrequest.saved.reqmsg.m_source == caller->p_endpoint);
620433d6423SLionel Sambuc
621433d6423SLionel Sambuc /*
622433d6423SLionel Sambuc printf("KERNEL_CALL restart from %s / %d rts 0x%08x misc 0x%08x\n",
623433d6423SLionel Sambuc caller->p_name, caller->p_endpoint,
624433d6423SLionel Sambuc caller->p_rts_flags, caller->p_misc_flags);
625433d6423SLionel Sambuc */
626433d6423SLionel Sambuc
627433d6423SLionel Sambuc /* re-execute the kernel call, with MF_KCALL_RESUME still set so
628433d6423SLionel Sambuc * the call knows this is a retry.
629433d6423SLionel Sambuc */
630433d6423SLionel Sambuc result = kernel_call_dispatch(caller, &caller->p_vmrequest.saved.reqmsg);
631433d6423SLionel Sambuc /*
632433d6423SLionel Sambuc * we are resuming the kernel call so we have to remove this flag so it
633433d6423SLionel Sambuc * can be set again
634433d6423SLionel Sambuc */
635433d6423SLionel Sambuc caller->p_misc_flags &= ~MF_KCALL_RESUME;
636433d6423SLionel Sambuc kernel_call_finish(caller, &caller->p_vmrequest.saved.reqmsg, result);
637433d6423SLionel Sambuc }
638433d6423SLionel Sambuc
639433d6423SLionel Sambuc /*===========================================================================*
640433d6423SLionel Sambuc * sched_proc *
641433d6423SLionel Sambuc *===========================================================================*/
sched_proc(struct proc * p,int priority,int quantum,int cpu,int niced)642366d18b2SDavid van Moolenbroek int sched_proc(struct proc *p, int priority, int quantum, int cpu, int niced)
643433d6423SLionel Sambuc {
644433d6423SLionel Sambuc /* Make sure the values given are within the allowed range.*/
645433d6423SLionel Sambuc if ((priority < TASK_Q && priority != -1) || priority > NR_SCHED_QUEUES)
646433d6423SLionel Sambuc return(EINVAL);
647433d6423SLionel Sambuc
648433d6423SLionel Sambuc if (quantum < 1 && quantum != -1)
649433d6423SLionel Sambuc return(EINVAL);
650433d6423SLionel Sambuc
651433d6423SLionel Sambuc #ifdef CONFIG_SMP
652433d6423SLionel Sambuc if ((cpu < 0 && cpu != -1) || (cpu > 0 && (unsigned) cpu >= ncpus))
653433d6423SLionel Sambuc return(EINVAL);
654433d6423SLionel Sambuc if (cpu != -1 && !(cpu_is_ready(cpu)))
655433d6423SLionel Sambuc return EBADCPU;
656433d6423SLionel Sambuc #endif
657433d6423SLionel Sambuc
658433d6423SLionel Sambuc /* In some cases, we might be rescheduling a runnable process. In such
659433d6423SLionel Sambuc * a case (i.e. if we are updating the priority) we set the NO_QUANTUM
660433d6423SLionel Sambuc * flag before the generic unset to dequeue/enqueue the process
661433d6423SLionel Sambuc */
662433d6423SLionel Sambuc
663433d6423SLionel Sambuc /* FIXME this preempts the process, do we really want to do that ?*/
664433d6423SLionel Sambuc
665433d6423SLionel Sambuc /* FIXME this is a problem for SMP if the processes currently runs on a
666433d6423SLionel Sambuc * different CPU */
667433d6423SLionel Sambuc if (proc_is_runnable(p)) {
668433d6423SLionel Sambuc #ifdef CONFIG_SMP
669433d6423SLionel Sambuc if (p->p_cpu != cpuid && cpu != -1 && cpu != p->p_cpu) {
670433d6423SLionel Sambuc smp_schedule_migrate_proc(p, cpu);
671433d6423SLionel Sambuc }
672433d6423SLionel Sambuc #endif
673433d6423SLionel Sambuc
674433d6423SLionel Sambuc RTS_SET(p, RTS_NO_QUANTUM);
675433d6423SLionel Sambuc }
676433d6423SLionel Sambuc
677433d6423SLionel Sambuc if (proc_is_runnable(p))
678433d6423SLionel Sambuc RTS_SET(p, RTS_NO_QUANTUM);
679433d6423SLionel Sambuc
680433d6423SLionel Sambuc if (priority != -1)
681433d6423SLionel Sambuc p->p_priority = priority;
682433d6423SLionel Sambuc if (quantum != -1) {
683433d6423SLionel Sambuc p->p_quantum_size_ms = quantum;
684433d6423SLionel Sambuc p->p_cpu_time_left = ms_2_cpu_time(quantum);
685433d6423SLionel Sambuc }
686433d6423SLionel Sambuc #ifdef CONFIG_SMP
687433d6423SLionel Sambuc if (cpu != -1)
688433d6423SLionel Sambuc p->p_cpu = cpu;
689433d6423SLionel Sambuc #endif
690433d6423SLionel Sambuc
691366d18b2SDavid van Moolenbroek if (niced)
692366d18b2SDavid van Moolenbroek p->p_misc_flags |= MF_NICED;
693366d18b2SDavid van Moolenbroek else
694366d18b2SDavid van Moolenbroek p->p_misc_flags &= ~MF_NICED;
695366d18b2SDavid van Moolenbroek
696433d6423SLionel Sambuc /* Clear the scheduling bit and enqueue the process */
697433d6423SLionel Sambuc RTS_UNSET(p, RTS_NO_QUANTUM);
698433d6423SLionel Sambuc
699433d6423SLionel Sambuc return OK;
700433d6423SLionel Sambuc }
701433d6423SLionel Sambuc
702c8a9900bSCristiano Giuffrida /*===========================================================================*
703c8a9900bSCristiano Giuffrida * add_ipc_filter *
704c8a9900bSCristiano Giuffrida *===========================================================================*/
add_ipc_filter(struct proc * rp,int type,vir_bytes address,size_t length)705c8a9900bSCristiano Giuffrida int add_ipc_filter(struct proc *rp, int type, vir_bytes address,
706c8a9900bSCristiano Giuffrida size_t length)
707c8a9900bSCristiano Giuffrida {
708c8a9900bSCristiano Giuffrida int num_elements, r;
709c8a9900bSCristiano Giuffrida ipc_filter_t *ipcf, **ipcfp;
710c8a9900bSCristiano Giuffrida
711c8a9900bSCristiano Giuffrida /* Validate arguments. */
712c8a9900bSCristiano Giuffrida if (type != IPCF_BLACKLIST && type != IPCF_WHITELIST)
713c8a9900bSCristiano Giuffrida return EINVAL;
714c8a9900bSCristiano Giuffrida
715c8a9900bSCristiano Giuffrida if (length % sizeof(ipc_filter_el_t) != 0)
716c8a9900bSCristiano Giuffrida return EINVAL;
717c8a9900bSCristiano Giuffrida
718c8a9900bSCristiano Giuffrida num_elements = length / sizeof(ipc_filter_el_t);
719c8a9900bSCristiano Giuffrida if (num_elements <= 0 || num_elements > IPCF_MAX_ELEMENTS)
720c8a9900bSCristiano Giuffrida return E2BIG;
721c8a9900bSCristiano Giuffrida
722c8a9900bSCristiano Giuffrida /* Allocate a new IPC filter slot. */
723c8a9900bSCristiano Giuffrida IPCF_POOL_ALLOCATE_SLOT(type, &ipcf);
724c8a9900bSCristiano Giuffrida if (ipcf == NULL)
725c8a9900bSCristiano Giuffrida return ENOMEM;
726c8a9900bSCristiano Giuffrida
727c8a9900bSCristiano Giuffrida /* Fill details. */
728c8a9900bSCristiano Giuffrida ipcf->num_elements = num_elements;
729c8a9900bSCristiano Giuffrida ipcf->next = NULL;
730c8a9900bSCristiano Giuffrida r = data_copy(rp->p_endpoint, address,
731c8a9900bSCristiano Giuffrida KERNEL, (vir_bytes)ipcf->elements, length);
732c8a9900bSCristiano Giuffrida if (r == OK)
733c8a9900bSCristiano Giuffrida r = check_ipc_filter(ipcf, TRUE /*fill_flags*/);
734c8a9900bSCristiano Giuffrida if (r != OK) {
735c8a9900bSCristiano Giuffrida IPCF_POOL_FREE_SLOT(ipcf);
736c8a9900bSCristiano Giuffrida return r;
737c8a9900bSCristiano Giuffrida }
738c8a9900bSCristiano Giuffrida
739c8a9900bSCristiano Giuffrida /* Add the new filter at the end of the IPC filter chain. */
740c8a9900bSCristiano Giuffrida for (ipcfp = &priv(rp)->s_ipcf; *ipcfp != NULL;
741c8a9900bSCristiano Giuffrida ipcfp = &(*ipcfp)->next)
742c8a9900bSCristiano Giuffrida ;
743c8a9900bSCristiano Giuffrida *ipcfp = ipcf;
744c8a9900bSCristiano Giuffrida
745c8a9900bSCristiano Giuffrida return OK;
746c8a9900bSCristiano Giuffrida }
747c8a9900bSCristiano Giuffrida
748c8a9900bSCristiano Giuffrida /*===========================================================================*
749c8a9900bSCristiano Giuffrida * clear_ipc_filters *
750c8a9900bSCristiano Giuffrida *===========================================================================*/
clear_ipc_filters(struct proc * rp)751c8a9900bSCristiano Giuffrida void clear_ipc_filters(struct proc *rp)
752c8a9900bSCristiano Giuffrida {
753c8a9900bSCristiano Giuffrida ipc_filter_t *curr_ipcf, *ipcf;
754c8a9900bSCristiano Giuffrida
755c8a9900bSCristiano Giuffrida ipcf = priv(rp)->s_ipcf;
756c8a9900bSCristiano Giuffrida while (ipcf != NULL) {
757c8a9900bSCristiano Giuffrida curr_ipcf = ipcf;
758c8a9900bSCristiano Giuffrida ipcf = ipcf->next;
759c8a9900bSCristiano Giuffrida IPCF_POOL_FREE_SLOT(curr_ipcf);
760c8a9900bSCristiano Giuffrida }
761c8a9900bSCristiano Giuffrida
762c8a9900bSCristiano Giuffrida priv(rp)->s_ipcf = NULL;
7633779ed93SDavid van Moolenbroek
7643779ed93SDavid van Moolenbroek /* VM is a special case here: since the cleared IPC filter may have
7653779ed93SDavid van Moolenbroek * blocked memory handling requests, we may now have to tell VM that
7663779ed93SDavid van Moolenbroek * there are "new" requests pending.
7673779ed93SDavid van Moolenbroek */
7683779ed93SDavid van Moolenbroek if (rp->p_endpoint == VM_PROC_NR && vmrequest != NULL)
7693779ed93SDavid van Moolenbroek if (send_sig(VM_PROC_NR, SIGKMEM) != OK)
7703779ed93SDavid van Moolenbroek panic("send_sig failed");
771c8a9900bSCristiano Giuffrida }
772c8a9900bSCristiano Giuffrida
773c8a9900bSCristiano Giuffrida /*===========================================================================*
774c8a9900bSCristiano Giuffrida * check_ipc_filter *
775c8a9900bSCristiano Giuffrida *===========================================================================*/
check_ipc_filter(ipc_filter_t * ipcf,int fill_flags)776c8a9900bSCristiano Giuffrida int check_ipc_filter(ipc_filter_t *ipcf, int fill_flags)
777c8a9900bSCristiano Giuffrida {
778c8a9900bSCristiano Giuffrida ipc_filter_el_t *ipcf_el;
779c8a9900bSCristiano Giuffrida int i, num_elements, flags;
780c8a9900bSCristiano Giuffrida
781c8a9900bSCristiano Giuffrida if (ipcf == NULL)
782c8a9900bSCristiano Giuffrida return OK;
783c8a9900bSCristiano Giuffrida
784c8a9900bSCristiano Giuffrida num_elements = ipcf->num_elements;
785c8a9900bSCristiano Giuffrida flags = 0;
786c8a9900bSCristiano Giuffrida for (i = 0; i < num_elements; i++) {
787c8a9900bSCristiano Giuffrida ipcf_el = &ipcf->elements[i];
788c8a9900bSCristiano Giuffrida if (!IPCF_EL_CHECK(ipcf_el))
789c8a9900bSCristiano Giuffrida return EINVAL;
790c8a9900bSCristiano Giuffrida flags |= ipcf_el->flags;
791c8a9900bSCristiano Giuffrida }
792c8a9900bSCristiano Giuffrida
793c8a9900bSCristiano Giuffrida if (fill_flags)
794c8a9900bSCristiano Giuffrida ipcf->flags = flags;
795c8a9900bSCristiano Giuffrida else if (ipcf->flags != flags)
796c8a9900bSCristiano Giuffrida return EINVAL;
797c8a9900bSCristiano Giuffrida return OK;
798c8a9900bSCristiano Giuffrida }
799c8a9900bSCristiano Giuffrida
800c8a9900bSCristiano Giuffrida /*===========================================================================*
801c8a9900bSCristiano Giuffrida * allow_ipc_filtered_msg *
802c8a9900bSCristiano Giuffrida *===========================================================================*/
allow_ipc_filtered_msg(struct proc * rp,endpoint_t src_e,vir_bytes m_src_v,message * m_src_p)803c8a9900bSCristiano Giuffrida int allow_ipc_filtered_msg(struct proc *rp, endpoint_t src_e,
804c8a9900bSCristiano Giuffrida vir_bytes m_src_v, message *m_src_p)
805c8a9900bSCristiano Giuffrida {
806c8a9900bSCristiano Giuffrida int i, r, num_elements, get_mtype, allow;
807c8a9900bSCristiano Giuffrida ipc_filter_t *ipcf;
808c8a9900bSCristiano Giuffrida ipc_filter_el_t *ipcf_el;
809c8a9900bSCristiano Giuffrida message m_buff;
810c8a9900bSCristiano Giuffrida
811c8a9900bSCristiano Giuffrida ipcf = priv(rp)->s_ipcf;
812c8a9900bSCristiano Giuffrida if (ipcf == NULL)
813c8a9900bSCristiano Giuffrida return TRUE; /* no IPC filters, always allow */
814c8a9900bSCristiano Giuffrida
815c8a9900bSCristiano Giuffrida if (m_src_p == NULL) {
816c8a9900bSCristiano Giuffrida assert(m_src_v != 0);
817c8a9900bSCristiano Giuffrida
818c8a9900bSCristiano Giuffrida /* Should we copy in the message type? */
819c8a9900bSCristiano Giuffrida get_mtype = FALSE;
820c8a9900bSCristiano Giuffrida do {
821c8a9900bSCristiano Giuffrida #if DEBUG_DUMPIPCF
822c8a9900bSCristiano Giuffrida if (TRUE) {
823c8a9900bSCristiano Giuffrida #else
824c8a9900bSCristiano Giuffrida if (ipcf->flags & IPCF_MATCH_M_TYPE) {
825c8a9900bSCristiano Giuffrida #endif
826c8a9900bSCristiano Giuffrida get_mtype = TRUE;
827c8a9900bSCristiano Giuffrida break;
828c8a9900bSCristiano Giuffrida }
829c8a9900bSCristiano Giuffrida ipcf = ipcf->next;
830c8a9900bSCristiano Giuffrida } while (ipcf);
831c8a9900bSCristiano Giuffrida ipcf = priv(rp)->s_ipcf; /* reset to start */
832c8a9900bSCristiano Giuffrida
833c8a9900bSCristiano Giuffrida /* If so, copy it in from the process. */
834c8a9900bSCristiano Giuffrida if (get_mtype) {
835c8a9900bSCristiano Giuffrida r = data_copy(src_e,
836c8a9900bSCristiano Giuffrida m_src_v + offsetof(message, m_type), KERNEL,
837c8a9900bSCristiano Giuffrida (vir_bytes)&m_buff.m_type, sizeof(m_buff.m_type));
838c8a9900bSCristiano Giuffrida if (r != OK) {
839c8a9900bSCristiano Giuffrida /* allow for now, this will fail later anyway */
840c8a9900bSCristiano Giuffrida #if DEBUG_DUMPIPCF
841c8a9900bSCristiano Giuffrida printf("KERNEL: allow_ipc_filtered_msg: data "
842c8a9900bSCristiano Giuffrida "copy error %d, allowing message...\n", r);
843c8a9900bSCristiano Giuffrida #endif
844c8a9900bSCristiano Giuffrida return TRUE;
845c8a9900bSCristiano Giuffrida }
846c8a9900bSCristiano Giuffrida }
847c8a9900bSCristiano Giuffrida m_src_p = &m_buff;
848c8a9900bSCristiano Giuffrida }
849c8a9900bSCristiano Giuffrida
850c8a9900bSCristiano Giuffrida m_src_p->m_source = src_e;
851c8a9900bSCristiano Giuffrida
852c8a9900bSCristiano Giuffrida /* See if the message is allowed. */
853c8a9900bSCristiano Giuffrida allow = (ipcf->type == IPCF_BLACKLIST);
854c8a9900bSCristiano Giuffrida do {
855c8a9900bSCristiano Giuffrida if (allow != (ipcf->type == IPCF_WHITELIST)) {
856c8a9900bSCristiano Giuffrida num_elements = ipcf->num_elements;
857c8a9900bSCristiano Giuffrida for (i = 0; i < num_elements; i++) {
858c8a9900bSCristiano Giuffrida ipcf_el = &ipcf->elements[i];
859c8a9900bSCristiano Giuffrida if (IPCF_EL_MATCH(ipcf_el, m_src_p)) {
860c8a9900bSCristiano Giuffrida allow = (ipcf->type == IPCF_WHITELIST);
861c8a9900bSCristiano Giuffrida break;
862c8a9900bSCristiano Giuffrida }
863c8a9900bSCristiano Giuffrida }
864c8a9900bSCristiano Giuffrida }
865c8a9900bSCristiano Giuffrida ipcf = ipcf->next;
866c8a9900bSCristiano Giuffrida } while (ipcf);
867c8a9900bSCristiano Giuffrida
868c8a9900bSCristiano Giuffrida #if DEBUG_DUMPIPCF
869c8a9900bSCristiano Giuffrida printmsg(m_src_p, proc_addr(_ENDPOINT_P(src_e)), rp, allow ? '+' : '-',
870c8a9900bSCristiano Giuffrida TRUE /*printparams*/);
871c8a9900bSCristiano Giuffrida #endif
872c8a9900bSCristiano Giuffrida
873c8a9900bSCristiano Giuffrida return allow;
874c8a9900bSCristiano Giuffrida }
875c8a9900bSCristiano Giuffrida
87656e56d2aSCristiano Giuffrida /*===========================================================================*
8773779ed93SDavid van Moolenbroek * allow_ipc_filtered_memreq *
8783779ed93SDavid van Moolenbroek *===========================================================================*/
8793779ed93SDavid van Moolenbroek int allow_ipc_filtered_memreq(struct proc *src_rp, struct proc *dst_rp)
8803779ed93SDavid van Moolenbroek {
8813779ed93SDavid van Moolenbroek /* Determine whether VM should receive a request to handle memory
8823779ed93SDavid van Moolenbroek * that is the result of process 'src_rp' trying to access currently
8833779ed93SDavid van Moolenbroek * unavailable memory in process 'dst_rp'. Return TRUE if VM should
8843779ed93SDavid van Moolenbroek * be given the request, FALSE otherwise.
8853779ed93SDavid van Moolenbroek */
8863779ed93SDavid van Moolenbroek
8873779ed93SDavid van Moolenbroek struct proc *vmp;
8883779ed93SDavid van Moolenbroek message m_buf;
8893779ed93SDavid van Moolenbroek
8903779ed93SDavid van Moolenbroek vmp = proc_addr(VM_PROC_NR);
8913779ed93SDavid van Moolenbroek
8923779ed93SDavid van Moolenbroek /* If VM has no filter in place, all requests should go through. */
8933779ed93SDavid van Moolenbroek if (priv(vmp)->s_ipcf == NULL)
8943779ed93SDavid van Moolenbroek return TRUE;
8953779ed93SDavid van Moolenbroek
8963779ed93SDavid van Moolenbroek /* VM obtains memory requests in response to a SIGKMEM signal, which
8973779ed93SDavid van Moolenbroek * is a notification sent from SYSTEM. Thus, if VM blocks such
8983779ed93SDavid van Moolenbroek * notifications, it also should not get any memory requests. Of
8993779ed93SDavid van Moolenbroek * course, VM should not be asking for requests in that case either,
9003779ed93SDavid van Moolenbroek * but the extra check doesn't hurt.
9013779ed93SDavid van Moolenbroek */
9023779ed93SDavid van Moolenbroek m_buf.m_type = NOTIFY_MESSAGE;
9033779ed93SDavid van Moolenbroek if (!allow_ipc_filtered_msg(vmp, SYSTEM, 0, &m_buf))
9043779ed93SDavid van Moolenbroek return FALSE;
9053779ed93SDavid van Moolenbroek
9063779ed93SDavid van Moolenbroek /* A more refined policy may be implemented here, for example to
9073779ed93SDavid van Moolenbroek * ensure that both the source and the destination (if different)
9083779ed93SDavid van Moolenbroek * are in the group of processes that VM wants to talk to. Since VM
9093779ed93SDavid van Moolenbroek * is basically not able to handle any memory requests during an
9103779ed93SDavid van Moolenbroek * update, we will not get here, and none of that is needed.
9113779ed93SDavid van Moolenbroek */
9123779ed93SDavid van Moolenbroek return TRUE;
9133779ed93SDavid van Moolenbroek }
9143779ed93SDavid van Moolenbroek
9153779ed93SDavid van Moolenbroek /*===========================================================================*
91656e56d2aSCristiano Giuffrida * priv_add_irq *
91756e56d2aSCristiano Giuffrida *===========================================================================*/
91856e56d2aSCristiano Giuffrida int priv_add_irq(struct proc *rp, int irq)
91956e56d2aSCristiano Giuffrida {
92056e56d2aSCristiano Giuffrida struct priv *priv = priv(rp);
92156e56d2aSCristiano Giuffrida int i;
92256e56d2aSCristiano Giuffrida
92356e56d2aSCristiano Giuffrida priv->s_flags |= CHECK_IRQ; /* Check IRQ */
92456e56d2aSCristiano Giuffrida
92556e56d2aSCristiano Giuffrida /* When restarting a driver, check if it already has the permission */
92656e56d2aSCristiano Giuffrida for (i = 0; i < priv->s_nr_irq; i++) {
92756e56d2aSCristiano Giuffrida if (priv->s_irq_tab[i] == irq)
92856e56d2aSCristiano Giuffrida return OK;
92956e56d2aSCristiano Giuffrida }
93056e56d2aSCristiano Giuffrida
93156e56d2aSCristiano Giuffrida i= priv->s_nr_irq;
93256e56d2aSCristiano Giuffrida if (i >= NR_IRQ) {
93356e56d2aSCristiano Giuffrida printf("do_privctl: %d already has %d irq's.\n",
93456e56d2aSCristiano Giuffrida rp->p_endpoint, i);
93556e56d2aSCristiano Giuffrida return ENOMEM;
93656e56d2aSCristiano Giuffrida }
93756e56d2aSCristiano Giuffrida priv->s_irq_tab[i]= irq;
93856e56d2aSCristiano Giuffrida priv->s_nr_irq++;
93956e56d2aSCristiano Giuffrida return OK;
94056e56d2aSCristiano Giuffrida }
94156e56d2aSCristiano Giuffrida
94256e56d2aSCristiano Giuffrida /*===========================================================================*
94356e56d2aSCristiano Giuffrida * priv_add_io *
94456e56d2aSCristiano Giuffrida *===========================================================================*/
94556e56d2aSCristiano Giuffrida int priv_add_io(struct proc *rp, struct io_range *ior)
94656e56d2aSCristiano Giuffrida {
94756e56d2aSCristiano Giuffrida struct priv *priv = priv(rp);
94856e56d2aSCristiano Giuffrida int i;
94956e56d2aSCristiano Giuffrida
95056e56d2aSCristiano Giuffrida priv->s_flags |= CHECK_IO_PORT; /* Check I/O accesses */
95156e56d2aSCristiano Giuffrida
95256e56d2aSCristiano Giuffrida for (i = 0; i < priv->s_nr_io_range; i++) {
95356e56d2aSCristiano Giuffrida if (priv->s_io_tab[i].ior_base == ior->ior_base &&
95456e56d2aSCristiano Giuffrida priv->s_io_tab[i].ior_limit == ior->ior_limit)
95556e56d2aSCristiano Giuffrida return OK;
95656e56d2aSCristiano Giuffrida }
95756e56d2aSCristiano Giuffrida
95856e56d2aSCristiano Giuffrida i= priv->s_nr_io_range;
95956e56d2aSCristiano Giuffrida if (i >= NR_IO_RANGE) {
96056e56d2aSCristiano Giuffrida printf("do_privctl: %d already has %d i/o ranges.\n",
96156e56d2aSCristiano Giuffrida rp->p_endpoint, i);
96256e56d2aSCristiano Giuffrida return ENOMEM;
96356e56d2aSCristiano Giuffrida }
96456e56d2aSCristiano Giuffrida
96556e56d2aSCristiano Giuffrida priv->s_io_tab[i] = *ior;
96656e56d2aSCristiano Giuffrida priv->s_nr_io_range++;
96756e56d2aSCristiano Giuffrida return OK;
96856e56d2aSCristiano Giuffrida }
96956e56d2aSCristiano Giuffrida
97056e56d2aSCristiano Giuffrida /*===========================================================================*
97156e56d2aSCristiano Giuffrida * priv_add_mem *
97256e56d2aSCristiano Giuffrida *===========================================================================*/
97356e56d2aSCristiano Giuffrida int priv_add_mem(struct proc *rp, struct minix_mem_range *memr)
97456e56d2aSCristiano Giuffrida {
97556e56d2aSCristiano Giuffrida struct priv *priv = priv(rp);
97656e56d2aSCristiano Giuffrida int i;
97756e56d2aSCristiano Giuffrida
97856e56d2aSCristiano Giuffrida priv->s_flags |= CHECK_MEM; /* Check memory mappings */
97956e56d2aSCristiano Giuffrida
98056e56d2aSCristiano Giuffrida /* When restarting a driver, check if it already has the permission */
98156e56d2aSCristiano Giuffrida for (i = 0; i < priv->s_nr_mem_range; i++) {
98256e56d2aSCristiano Giuffrida if (priv->s_mem_tab[i].mr_base == memr->mr_base &&
98356e56d2aSCristiano Giuffrida priv->s_mem_tab[i].mr_limit == memr->mr_limit)
98456e56d2aSCristiano Giuffrida return OK;
98556e56d2aSCristiano Giuffrida }
98656e56d2aSCristiano Giuffrida
98756e56d2aSCristiano Giuffrida i= priv->s_nr_mem_range;
98856e56d2aSCristiano Giuffrida if (i >= NR_MEM_RANGE) {
98956e56d2aSCristiano Giuffrida printf("do_privctl: %d already has %d mem ranges.\n",
99056e56d2aSCristiano Giuffrida rp->p_endpoint, i);
99156e56d2aSCristiano Giuffrida return ENOMEM;
99256e56d2aSCristiano Giuffrida }
99356e56d2aSCristiano Giuffrida priv->s_mem_tab[i]= *memr;
99456e56d2aSCristiano Giuffrida priv->s_nr_mem_range++;
99556e56d2aSCristiano Giuffrida return OK;
99656e56d2aSCristiano Giuffrida }
99756e56d2aSCristiano Giuffrida
998