1 /* This file contains essentially all of the process and message handling.
2 * Together with "mpx.s" it forms the lowest layer of the MINIX kernel.
3 * There is one entry point from the outside:
5 * sys_call: a system call, i.e., the kernel is trapped with an INT
7 * As well as several entry points used from the interrupt and task level:
9 * lock_notify: notify a process of a system event
10 * lock_send: send a message to a process
11 * lock_enqueue: put a process on one of the scheduling queues
12 * lock_dequeue: remove a process from the scheduling queues
15 * Aug 19, 2005 rewrote scheduling code (Jorrit N. Herder)
16 * Jul 25, 2005 rewrote system call handling (Jorrit N. Herder)
17 * May 26, 2005 rewrote message passing functions (Jorrit N. Herder)
18 * May 24, 2005 new notification system call (Jorrit N. Herder)
19 * Oct 28, 2004 nonblocking send and receive calls (Jorrit N. Herder)
21 * The code here is critical to make everything work and is important for the
22 * overall performance of the system. A large fraction of the code deals with
23 * list manipulation. To make this both easy to understand and fast to execute
24 * pointer pointers are used throughout the code. Pointer pointers prevent
25 * exceptions for the head or tail of a linked list.
27 * node_t *queue, *new_node; // assume these as global variables
28 * node_t **xpp = &queue; // get pointer pointer to head of queue
29 * while (*xpp != NULL) // find last pointer of the linked list
30 * xpp = &(*xpp)->next; // get pointer to next pointer
31 * *xpp = new_node; // now replace the end (the NULL pointer)
32 * new_node->next = NULL; // and mark the new end of the list
34 * For example, when adding a new node to the end of the list, one normally
35 * makes an exception for an empty list and looks up the end of the list for
36 * nonempty lists. As shown above, this is not required with pointer pointers.
39 #include <minix/com.h>
40 #include <minix/callnr.h>
41 #include <minix/endpoint.h>
47 #include <minix/portio.h>
49 /* Scheduling and message passing functions. The functions are available to
50 * other parts of the kernel through lock_...(). The lock temporarily disables
51 * interrupts to prevent race conditions.
53 FORWARD
_PROTOTYPE( int mini_send
, (struct proc
*caller_ptr
, int dst_e
,
54 message
*m_ptr
, unsigned flags
));
55 FORWARD
_PROTOTYPE( int mini_receive
, (struct proc
*caller_ptr
, int src
,
56 message
*m_ptr
, unsigned flags
));
57 FORWARD
_PROTOTYPE( int mini_notify
, (struct proc
*caller_ptr
, int dst
));
58 FORWARD
_PROTOTYPE( int mini_senda
, (struct proc
*caller_ptr
,
59 asynmsg_t
*table
, size_t size
));
60 FORWARD
_PROTOTYPE( int deadlock
, (int function
,
61 register struct proc
*caller
, int src_dst
));
62 FORWARD
_PROTOTYPE( int try_async
, (struct proc
*caller_ptr
));
63 FORWARD
_PROTOTYPE( int try_one
, (struct proc
*src_ptr
, struct proc
*dst_ptr
));
64 FORWARD
_PROTOTYPE( void enqueue
, (struct proc
*rp
));
65 FORWARD
_PROTOTYPE( void dequeue
, (struct proc
*rp
));
66 FORWARD
_PROTOTYPE( void sched
, (struct proc
*rp
, int *queue
, int *front
));
67 FORWARD
_PROTOTYPE( void pick_proc
, (void));
69 #define BuildMess(m_ptr, src, dst_ptr) \
70 (m_ptr)->m_source = proc_addr(src)->p_endpoint; \
71 (m_ptr)->m_type = NOTIFY_FROM(src); \
72 (m_ptr)->NOTIFY_TIMESTAMP = get_uptime(); \
75 (m_ptr)->NOTIFY_ARG = priv(dst_ptr)->s_int_pending; \
76 priv(dst_ptr)->s_int_pending = 0; \
79 (m_ptr)->NOTIFY_ARG = priv(dst_ptr)->s_sig_pending; \
80 priv(dst_ptr)->s_sig_pending = 0; \
84 #define CopyMess(s,sp,sm,dp,dm) \
85 cp_mess(proc_addr(s)->p_endpoint, \
86 (sp)->p_memmap[D].mem_phys, \
87 (vir_bytes)sm, (dp)->p_memmap[D].mem_phys, (vir_bytes)dm)
89 /*===========================================================================*
91 *===========================================================================*/
92 PUBLIC
int sys_call(call_nr
, src_dst_e
, m_ptr
, bit_map
)
93 int call_nr
; /* system call number and flags */
94 int src_dst_e
; /* src to receive from or dst to send to */
95 message
*m_ptr
; /* pointer to message in the caller's space */
96 long bit_map
; /* notification event set or flags */
98 /* System calls are done by trapping to the kernel with an INT instruction.
99 * The trap is caught and sys_call() is called to send or receive a message
100 * (or both). The caller is always given by 'proc_ptr'.
102 register struct proc
*caller_ptr
= proc_ptr
; /* get pointer to caller */
103 int mask_entry
; /* bit to check in send mask */
104 int group_size
; /* used for deadlock check */
105 int result
; /* the system call's result */
106 int src_dst_p
; /* Process slot number */
107 vir_clicks vlo
, vhi
; /* virtual clicks containing message to send */
110 if (RTS_ISSET(caller_ptr
, SLOT_FREE
))
112 kprintf("called by the dead?!?\n");
117 /* Check destination. SENDA is special because its argument is a table and
118 * not a single destination. RECEIVE is the only call that accepts ANY (in
119 * addition to a real endpoint). The other calls (SEND, SENDNB, SENDREC,
120 * and NOTIFY) require an endpoint to corresponds to a process. In addition,
121 * it is necessary to check whether a process is allow to send to a given
122 * destination. For SENDREC we check s_ipc_sendrec, and for SEND, SENDNB,
123 * and NOTIFY we check s_ipc_to.
125 if (call_nr
== SENDA
)
127 /* No destination argument */
129 else if (src_dst_e
== ANY
)
131 if (call_nr
!= RECEIVE
)
133 #if DEBUG_ENABLE_IPC_WARNINGS
134 kprintf("sys_call: trap %d by %d with bad endpoint %d\n",
135 call_nr
, proc_nr(caller_ptr
), src_dst_e
);
139 src_dst_p
= src_dst_e
;
143 /* Require a valid source and/or destination process. */
144 if(!isokendpt(src_dst_e
, &src_dst_p
)) {
145 if (src_dst_e
== 0) panic("sys_call: no PM", NO_NUM
);
146 #if DEBUG_ENABLE_IPC_WARNINGS
147 kprintf("sys_call: trap %d by %d with bad endpoint %d\n",
148 call_nr
, proc_nr(caller_ptr
), src_dst_e
);
153 /* If the call is to send to a process, i.e., for SEND, SENDNB,
154 * SENDREC or NOTIFY, verify that the caller is allowed to send to
155 * the given destination.
157 if (call_nr
== SENDREC
)
159 if (! get_sys_bit(priv(caller_ptr
)->s_ipc_sendrec
,
160 nr_to_id(src_dst_p
))) {
161 #if DEBUG_ENABLE_IPC_WARNINGS
163 "sys_call: ipc sendrec mask denied trap %d from %d to %d\n",
164 call_nr
, proc_nr(caller_ptr
), src_dst_p
);
166 return(ECALLDENIED
); /* call denied by ipc mask */
169 else if (call_nr
== SEND
|| call_nr
== SENDNB
|| call_nr
== NOTIFY
)
171 if (! get_sys_bit(priv(caller_ptr
)->s_ipc_to
,
172 nr_to_id(src_dst_p
))) {
173 #if DEBUG_ENABLE_IPC_WARNINGS
175 "sys_call: ipc mask denied trap %d from %d to %d\n",
176 call_nr
, proc_nr(caller_ptr
), src_dst_p
);
178 return(ECALLDENIED
); /* call denied by ipc mask */
183 /* Only allow non-negative call_nr values less than 32 */
184 if (call_nr
< 0 || call_nr
>= 32)
186 #if DEBUG_ENABLE_IPC_WARNINGS
187 kprintf("sys_call: trap %d not allowed, caller %d, src_dst %d\n",
188 call_nr
, proc_nr(caller_ptr
), src_dst_p
);
190 return(ETRAPDENIED
); /* trap denied by mask or kernel */
193 /* Check if the process has privileges for the requested call. Calls to the
194 * kernel may only be SENDREC, because tasks always reply and may not block
195 * if the caller doesn't do receive().
197 if (!(priv(caller_ptr
)->s_trap_mask
& (1 << call_nr
))) {
198 #if DEBUG_ENABLE_IPC_WARNINGS
199 kprintf("sys_call: trap %d not allowed, caller %d, src_dst %d\n",
200 call_nr
, proc_nr(caller_ptr
), src_dst_p
);
202 return(ETRAPDENIED
); /* trap denied by mask or kernel */
206 if ((iskerneln(src_dst_p
) && _function
!= SENDREC
207 && _function
!= RECEIVE
)) {
208 #if DEBUG_ENABLE_IPC_WARNINGS
209 kprintf("sys_call: trap %d not allowed, caller %d, src_dst %d\n",
210 function
, proc_nr(caller_ptr
), src_dst
);
212 return(ETRAPDENIED
); /* trap denied by mask or kernel */
216 /* If the call involves a message buffer, i.e., for SEND, SENDNB, SENDREC,
217 * or RECEIVE, check the message pointer. This check allows a message to be
218 * anywhere in data or stack or gap. It will have to be made more elaborate
219 * for machines which don't have the gap mapped.
221 if (call_nr
== SEND
|| call_nr
== SENDNB
|| call_nr
== SENDREC
||
222 call_nr
== RECEIVE
) {
223 vlo
= (vir_bytes
) m_ptr
>> CLICK_SHIFT
;
224 vhi
= ((vir_bytes
) m_ptr
+ MESS_SIZE
- 1) >> CLICK_SHIFT
;
225 if (vlo
< caller_ptr
->p_memmap
[D
].mem_vir
|| vlo
> vhi
||
226 vhi
>= caller_ptr
->p_memmap
[S
].mem_vir
+
227 caller_ptr
->p_memmap
[S
].mem_len
) {
228 #if DEBUG_ENABLE_IPC_WARNINGS
230 "sys_call: invalid message pointer, trap %d, caller %d\n",
231 call_nr
, proc_nr(caller_ptr
));
233 return(EFAULT
); /* invalid message pointer */
237 /* Check for a possible deadlock for blocking SEND(REC) and RECEIVE. */
238 if (call_nr
== SEND
|| call_nr
== SENDREC
|| call_nr
== RECEIVE
) {
239 if (group_size
= deadlock(call_nr
, caller_ptr
, src_dst_p
)) {
240 #if DEBUG_ENABLE_IPC_WARNINGS
241 kprintf("sys_call: trap %d from %d to %d deadlocked, group size %d\n",
242 call_nr
, proc_nr(caller_ptr
), src_dst_p
, group_size
);
248 /* Now check if the call is known and try to perform the request. The only
249 * system calls that exist in MINIX are sending and receiving messages.
250 * - SENDREC: combines SEND and RECEIVE in a single system call
251 * - SEND: sender blocks until its message has been delivered
252 * - RECEIVE: receiver blocks until an acceptable message has arrived
253 * - NOTIFY: asynchronous call; deliver notification or mark pending
254 * - SENDNB: nonblocking send
255 * - SENDA: list of asynchronous send requests
259 /* A flag is set so that notifications cannot interrupt SENDREC. */
260 caller_ptr
->p_misc_flags
|= REPLY_PENDING
;
263 result
= mini_send(caller_ptr
, src_dst_e
, m_ptr
, 0 /*flags*/);
264 if (call_nr
== SEND
|| result
!= OK
)
265 break; /* done, or SEND failed */
266 /* fall through for SENDREC */
268 if (call_nr
== RECEIVE
)
269 caller_ptr
->p_misc_flags
&= ~REPLY_PENDING
;
270 result
= mini_receive(caller_ptr
, src_dst_e
, m_ptr
, 0 /*flags*/);
273 result
= mini_notify(caller_ptr
, src_dst_p
);
276 result
= mini_send(caller_ptr
, src_dst_e
, m_ptr
, NON_BLOCKING
);
279 result
= mini_senda(caller_ptr
, (asynmsg_t
*)m_ptr
, (size_t)src_dst_e
);
282 result
= EBADCALL
; /* illegal system call */
285 /* Now, return the result of the system call to the caller. */
289 /*===========================================================================*
291 *===========================================================================*/
292 PRIVATE
int deadlock(function
, cp
, src_dst
)
293 int function
; /* trap number */
294 register struct proc
*cp
; /* pointer to caller */
295 int src_dst
; /* src or dst process */
297 /* Check for deadlock. This can happen if 'caller_ptr' and 'src_dst' have
298 * a cyclic dependency of blocking send and receive calls. The only cyclic
299 * depency that is not fatal is if the caller and target directly SEND(REC)
300 * and RECEIVE to each other. If a deadlock is found, the group size is
301 * returned. Otherwise zero is returned.
303 register struct proc
*xp
; /* process pointer */
304 int group_size
= 1; /* start with only caller */
307 while (src_dst
!= ANY
) { /* check while process nr */
309 xp
= proc_addr(src_dst
); /* follow chain of processes */
310 group_size
++; /* extra process in group */
312 /* Check whether the last process in the chain has a dependency. If it
313 * has not, the cycle cannot be closed and we are done.
315 if (RTS_ISSET(xp
, RECEIVING
)) { /* xp has dependency */
316 if(xp
->p_getfrom_e
== ANY
) src_dst
= ANY
;
317 else okendpt(xp
->p_getfrom_e
, &src_dst
);
318 } else if (RTS_ISSET(xp
, SENDING
)) { /* xp has dependency */
319 okendpt(xp
->p_sendto_e
, &src_dst
);
321 return(0); /* not a deadlock */
324 /* Now check if there is a cyclic dependency. For group sizes of two,
325 * a combination of SEND(REC) and RECEIVE is not fatal. Larger groups
326 * or other combinations indicate a deadlock.
328 if (src_dst
== proc_nr(cp
)) { /* possible deadlock */
329 if (group_size
== 2) { /* caller and src_dst */
330 /* The function number is magically converted to flags. */
331 if ((xp
->p_rts_flags
^ (function
<< 2)) & SENDING
) {
332 return(0); /* not a deadlock */
335 return(group_size
); /* deadlock found */
338 return(0); /* not a deadlock */
342 /*===========================================================================*
344 *===========================================================================*/
345 PRIVATE
int mini_send(caller_ptr
, dst_e
, m_ptr
, flags
)
346 register struct proc
*caller_ptr
; /* who is trying to send a message? */
347 int dst_e
; /* to whom is message being sent? */
348 message
*m_ptr
; /* pointer to message buffer */
349 unsigned flags
; /* system call flags */
351 /* Send a message from 'caller_ptr' to 'dst'. If 'dst' is blocked waiting
352 * for this message, copy the message to it and unblock 'dst'. If 'dst' is
353 * not waiting at all, or is waiting for another source, queue 'caller_ptr'.
355 register struct proc
*dst_ptr
;
356 register struct proc
**xpp
;
359 dst_p
= _ENDPOINT_P(dst_e
);
360 dst_ptr
= proc_addr(dst_p
);
362 if (RTS_ISSET(dst_ptr
, NO_ENDPOINT
)) return EDSTDIED
;
364 /* Check if 'dst' is blocked waiting for this message. The destination's
365 * SENDING flag may be set when its SENDREC call blocked while sending.
367 if ( (RTS_ISSET(dst_ptr
, RECEIVING
) && !RTS_ISSET(dst_ptr
, SENDING
)) &&
368 (dst_ptr
->p_getfrom_e
== ANY
369 || dst_ptr
->p_getfrom_e
== caller_ptr
->p_endpoint
)) {
370 /* Destination is indeed waiting for this message. */
371 CopyMess(caller_ptr
->p_nr
, caller_ptr
, m_ptr
, dst_ptr
,
373 RTS_UNSET(dst_ptr
, RECEIVING
);
374 } else if ( ! (flags
& NON_BLOCKING
)) {
375 /* Destination is not waiting. Block and dequeue caller. */
376 caller_ptr
->p_messbuf
= m_ptr
;
377 RTS_SET(caller_ptr
, SENDING
);
378 caller_ptr
->p_sendto_e
= dst_e
;
380 /* Process is now blocked. Put in on the destination's queue. */
381 xpp
= &dst_ptr
->p_caller_q
; /* find end of list */
382 while (*xpp
!= NIL_PROC
) xpp
= &(*xpp
)->p_q_link
;
383 *xpp
= caller_ptr
; /* add caller to end */
384 caller_ptr
->p_q_link
= NIL_PROC
; /* mark new end of list */
391 /*===========================================================================*
393 *===========================================================================*/
394 PRIVATE
int mini_receive(caller_ptr
, src_e
, m_ptr
, flags
)
395 register struct proc
*caller_ptr
; /* process trying to get message */
396 int src_e
; /* which message source is wanted */
397 message
*m_ptr
; /* pointer to message buffer */
398 unsigned flags
; /* system call flags */
400 /* A process or task wants to get a message. If a message is already queued,
401 * acquire it and deblock the sender. If no message from the desired source
402 * is available block the caller, unless the flags don't allow blocking.
404 register struct proc
**xpp
;
405 register struct notification
**ntf_q_pp
;
410 int i
, r
, src_id
, src_proc_nr
, src_p
;
412 if(src_e
== ANY
) src_p
= ANY
;
415 okendpt(src_e
, &src_p
);
416 if (RTS_ISSET(proc_addr(src_p
), NO_ENDPOINT
)) return ESRCDIED
;
420 /* Check to see if a message from desired source is already available.
421 * The caller's SENDING flag may be set if SENDREC couldn't send. If it is
422 * set, the process should be blocked.
424 if (!RTS_ISSET(caller_ptr
, SENDING
)) {
426 /* Check if there are pending notifications, except for SENDREC. */
427 if (! (caller_ptr
->p_misc_flags
& REPLY_PENDING
)) {
429 map
= &priv(caller_ptr
)->s_notify_pending
;
430 for (chunk
=&map
->chunk
[0]; chunk
<&map
->chunk
[NR_SYS_CHUNKS
]; chunk
++) {
432 /* Find a pending notification from the requested source. */
433 if (! *chunk
) continue; /* no bits in chunk */
434 for (i
=0; ! (*chunk
& (1<<i
)); ++i
) {} /* look up the bit */
435 src_id
= (chunk
- &map
->chunk
[0]) * BITCHUNK_BITS
+ i
;
436 if (src_id
>= NR_SYS_PROCS
) break; /* out of range */
437 src_proc_nr
= id_to_nr(src_id
); /* get source proc */
438 #if DEBUG_ENABLE_IPC_WARNINGS
439 if(src_proc_nr
== NONE
) {
440 kprintf("mini_receive: sending notify from NONE\n");
443 if (src_e
!=ANY
&& src_p
!= src_proc_nr
) continue;/* source not ok */
444 *chunk
&= ~(1 << i
); /* no longer pending */
446 /* Found a suitable source, deliver the notification message. */
447 BuildMess(&m
, src_proc_nr
, caller_ptr
); /* assemble message */
448 CopyMess(src_proc_nr
, proc_addr(HARDWARE
), &m
, caller_ptr
, m_ptr
);
449 return(OK
); /* report success */
453 /* Check caller queue. Use pointer pointers to keep code simple. */
454 xpp
= &caller_ptr
->p_caller_q
;
455 while (*xpp
!= NIL_PROC
) {
456 if (src_e
== ANY
|| src_p
== proc_nr(*xpp
)) {
458 if (RTS_ISSET(*xpp
, SLOT_FREE
))
460 kprintf("listening to the dead?!?\n");
465 /* Found acceptable message. Copy it and update status. */
466 CopyMess((*xpp
)->p_nr
, *xpp
, (*xpp
)->p_messbuf
, caller_ptr
, m_ptr
);
467 RTS_UNSET(*xpp
, SENDING
);
468 *xpp
= (*xpp
)->p_q_link
; /* remove from queue */
469 return(OK
); /* report success */
471 xpp
= &(*xpp
)->p_q_link
; /* proceed to next */
474 if (caller_ptr
->p_misc_flags
& MF_ASYNMSG
)
479 kprintf("mini_receive: should try async from %d\n", src_e
);
485 caller_ptr
->p_messbuf
= m_ptr
;
486 r
= try_async(caller_ptr
);
489 return OK
; /* Got a message */
493 /* No suitable message is available or the caller couldn't send in SENDREC.
494 * Block the process trying to receive, unless the flags tell otherwise.
496 if ( ! (flags
& NON_BLOCKING
)) {
497 caller_ptr
->p_getfrom_e
= src_e
;
498 caller_ptr
->p_messbuf
= m_ptr
;
499 RTS_SET(caller_ptr
, RECEIVING
);
506 /*===========================================================================*
508 *===========================================================================*/
509 PRIVATE
int mini_notify(caller_ptr
, dst
)
510 register struct proc
*caller_ptr
; /* sender of the notification */
511 int dst
; /* which process to notify */
513 register struct proc
*dst_ptr
= proc_addr(dst
);
514 int src_id
; /* source id for late delivery */
515 message m
; /* the notification message */
517 /* Check to see if target is blocked waiting for this message. A process
518 * can be both sending and receiving during a SENDREC system call.
520 if ( (RTS_ISSET(dst_ptr
, RECEIVING
) && !RTS_ISSET(dst_ptr
, SENDING
)) &&
521 ! (dst_ptr
->p_misc_flags
& REPLY_PENDING
) &&
522 (dst_ptr
->p_getfrom_e
== ANY
||
523 dst_ptr
->p_getfrom_e
== caller_ptr
->p_endpoint
)) {
525 /* Destination is indeed waiting for a message. Assemble a notification
526 * message and deliver it. Copy from pseudo-source HARDWARE, since the
527 * message is in the kernel's address space.
529 BuildMess(&m
, proc_nr(caller_ptr
), dst_ptr
);
530 CopyMess(proc_nr(caller_ptr
), proc_addr(HARDWARE
), &m
,
531 dst_ptr
, dst_ptr
->p_messbuf
);
532 RTS_UNSET(dst_ptr
, RECEIVING
);
536 /* Destination is not ready to receive the notification. Add it to the
537 * bit map with pending notifications. Note the indirectness: the system id
538 * instead of the process number is used in the pending bit map.
540 src_id
= priv(caller_ptr
)->s_id
;
541 set_sys_bit(priv(dst_ptr
)->s_notify_pending
, src_id
);
546 /*===========================================================================*
548 *===========================================================================*/
549 PRIVATE
int mini_senda(caller_ptr
, table
, size
)
550 struct proc
*caller_ptr
;
554 int i
, dst_p
, done
, do_notify
;
557 struct proc
*dst_ptr
;
562 privp
= priv(caller_ptr
);
563 if (!(privp
->s_flags
& SYS_PROC
))
566 "mini_senda: warning caller has no privilege structure\n");
571 privp
->s_asyntab
= -1;
572 privp
->s_asynsize
= 0;
576 /* Nothing to do, just return */
580 /* Limit size to something reasonable. An arbitrary choice is 16
581 * times the number of process table entries.
583 if (size
> 16*(NR_TASKS
+ NR_PROCS
))
587 tab_phys
= umap_local(caller_ptr
, D
, (vir_bytes
)table
,
588 size
*sizeof(table
[0]));
591 kprintf("mini_senda: got bad table pointer/size\n");
598 for (i
= 0; i
<size
; i
++)
600 /* Read status word */
601 phys_copy(tab_phys
+ i
*sizeof(table
[0]) +
602 offsetof(struct asynmsg
, flags
),
603 vir2phys(&tabent
.flags
), sizeof(tabent
.flags
));
606 /* Skip empty entries */
610 /* Check for reserved bits in the flags field */
611 if (flags
& ~(AMF_VALID
|AMF_DONE
|AMF_NOTIFY
) ||
612 !(flags
& AMF_VALID
))
617 /* Skip entry is AMF_DONE is already set */
618 if (flags
& AMF_DONE
)
621 /* Get destination */
622 phys_copy(tab_phys
+ i
*sizeof(table
[0]) +
623 offsetof(struct asynmsg
, dst
),
624 vir2phys(&tabent
.dst
), sizeof(tabent
.dst
));
626 if (!isokendpt(tabent
.dst
, &dst_p
))
628 /* Bad destination, report the error */
629 tabent
.result
= EDEADSRCDST
;
630 phys_copy(vir2phys(&tabent
.result
),
631 tab_phys
+ i
*sizeof(table
[0]) +
632 offsetof(struct asynmsg
, result
),
633 sizeof(tabent
.result
));
634 tabent
.flags
= flags
| AMF_DONE
;
635 phys_copy(vir2phys(&tabent
.flags
),
636 tab_phys
+ i
*sizeof(table
[0]) +
637 offsetof(struct asynmsg
, flags
),
638 sizeof(tabent
.flags
));
640 if (flags
& AMF_NOTIFY
)
646 kprintf("mini_senda: entry[%d]: flags 0x%x dst %d/%d\n",
647 i
, tabent
.flags
, tabent
.dst
, dst_p
);
650 dst_ptr
= proc_addr(dst_p
);
652 /* NO_ENDPOINT should be removed */
653 if (dst_ptr
->p_rts_flags
& NO_ENDPOINT
)
655 tabent
.result
= EDSTDIED
;
656 phys_copy(vir2phys(&tabent
.result
),
657 tab_phys
+ i
*sizeof(table
[0]) +
658 offsetof(struct asynmsg
, result
),
659 sizeof(tabent
.result
));
660 tabent
.flags
= flags
| AMF_DONE
;
661 phys_copy(vir2phys(&tabent
.flags
),
662 tab_phys
+ i
*sizeof(table
[0]) +
663 offsetof(struct asynmsg
, flags
),
664 sizeof(tabent
.flags
));
666 if (flags
& AMF_NOTIFY
)
671 /* Check if 'dst' is blocked waiting for this message. The
672 * destination's SENDING flag may be set when its SENDREC call
673 * blocked while sending.
675 if ( (dst_ptr
->p_rts_flags
& (RECEIVING
| SENDING
)) ==
677 (dst_ptr
->p_getfrom_e
== ANY
||
678 dst_ptr
->p_getfrom_e
== caller_ptr
->p_endpoint
))
680 /* Destination is indeed waiting for this message. */
681 m_ptr
= &table
[i
].msg
; /* Note: pointer in the
682 * caller's address space.
684 CopyMess(caller_ptr
->p_nr
, caller_ptr
, m_ptr
, dst_ptr
,
687 if ((dst_ptr
->p_rts_flags
&= ~RECEIVING
) == 0)
691 phys_copy(vir2phys(&tabent
.result
),
692 tab_phys
+ i
*sizeof(table
[0]) +
693 offsetof(struct asynmsg
, result
),
694 sizeof(tabent
.result
));
695 tabent
.flags
= flags
| AMF_DONE
;
696 phys_copy(vir2phys(&tabent
.flags
),
697 tab_phys
+ i
*sizeof(table
[0]) +
698 offsetof(struct asynmsg
, flags
),
699 sizeof(tabent
.flags
));
701 if (flags
& AMF_NOTIFY
)
707 /* Should inform receiver that something is pending */
708 dst_ptr
->p_misc_flags
|= MF_ASYNMSG
;
714 kprintf("mini_senda: should notifiy caller\n");
717 privp
->s_asyntab
= (vir_bytes
)table
;
718 privp
->s_asynsize
= size
;
724 /*===========================================================================*
726 *===========================================================================*/
727 PRIVATE
int try_async(caller_ptr
)
728 struct proc
*caller_ptr
;
732 struct proc
*src_ptr
;
734 /* Try all privilege structures */
735 for (privp
= BEG_PRIV_ADDR
; privp
< END_PRIV_ADDR
; ++privp
)
737 if (privp
->s_proc_nr
== NONE
|| privp
->s_id
== USER_PRIV_ID
)
739 if (privp
->s_asynsize
== 0)
742 kprintf("try_async: found asyntable for proc %d\n",
745 src_ptr
= proc_addr(privp
->s_proc_nr
);
746 r
= try_one(src_ptr
, caller_ptr
);
751 /* Nothing found, clear MF_ASYNMSG */
752 caller_ptr
->p_misc_flags
&= ~MF_ASYNMSG
;
758 /*===========================================================================*
760 *===========================================================================*/
761 PRIVATE
int try_one(src_ptr
, dst_ptr
)
762 struct proc
*src_ptr
;
763 struct proc
*dst_ptr
;
765 int i
, do_notify
, done
;
770 asynmsg_t
*table_ptr
;
775 privp
= priv(src_ptr
);
776 size
= privp
->s_asynsize
;
778 dst_e
= dst_ptr
->p_endpoint
;
781 tab_phys
= umap_local(src_ptr
, D
, privp
->s_asyntab
,
782 size
*sizeof(tabent
));
785 kprintf("try_one: got bad table pointer/size\n");
786 privp
->s_asynsize
= 0;
793 for (i
= 0; i
<size
; i
++)
795 /* Read status word */
796 phys_copy(tab_phys
+ i
*sizeof(tabent
) +
797 offsetof(struct asynmsg
, flags
),
798 vir2phys(&tabent
.flags
), sizeof(tabent
.flags
));
801 /* Skip empty entries */
807 /* Check for reserved bits in the flags field */
808 if (flags
& ~(AMF_VALID
|AMF_DONE
|AMF_NOTIFY
) ||
809 !(flags
& AMF_VALID
))
811 kprintf("try_one: bad bits in table\n");
812 privp
->s_asynsize
= 0;
816 /* Skip entry is AMF_DONE is already set */
817 if (flags
& AMF_DONE
)
822 /* Clear done. We are done when all entries are either empty
823 * or done at the start of the call.
827 /* Get destination */
828 phys_copy(tab_phys
+ i
*sizeof(tabent
) +
829 offsetof(struct asynmsg
, dst
),
830 vir2phys(&tabent
.dst
), sizeof(tabent
.dst
));
832 if (tabent
.dst
!= dst_e
)
834 kprintf("try_one: wrong dst %d, looking for %d\n",
840 kprintf("try_one: entry[%d]: flags 0x%x dst %d\n",
841 i
, tabent
.flags
, tabent
.dst
);
844 /* Deliver message */
845 table_ptr
= (asynmsg_t
*)privp
->s_asyntab
;
846 m_ptr
= &table_ptr
[i
].msg
; /* Note: pointer in the
847 * caller's address space.
849 CopyMess(src_ptr
->p_nr
, src_ptr
, m_ptr
, dst_ptr
,
853 phys_copy(vir2phys(&tabent
.result
),
854 tab_phys
+ i
*sizeof(tabent
) +
855 offsetof(struct asynmsg
, result
),
856 sizeof(tabent
.result
));
857 tabent
.flags
= flags
| AMF_DONE
;
858 phys_copy(vir2phys(&tabent
.flags
),
859 tab_phys
+ i
*sizeof(tabent
) +
860 offsetof(struct asynmsg
, flags
),
861 sizeof(tabent
.flags
));
863 if (flags
& AMF_NOTIFY
)
865 kprintf("try_one: should notify caller\n");
870 privp
->s_asynsize
= 0;
874 /*===========================================================================*
876 *===========================================================================*/
877 PUBLIC
int lock_notify(src_e
, dst_e
)
878 int src_e
; /* (endpoint) sender of the notification */
879 int dst_e
; /* (endpoint) who is to be notified */
881 /* Safe gateway to mini_notify() for tasks and interrupt handlers. The sender
882 * is explicitely given to prevent confusion where the call comes from. MINIX
883 * kernel is not reentrant, which means to interrupts are disabled after
884 * the first kernel entry (hardware interrupt, trap, or exception). Locking
885 * is done by temporarily disabling interrupts.
887 int result
, src
, dst
;
889 if(!isokendpt(src_e
, &src
) || !isokendpt(dst_e
, &dst
))
892 /* Exception or interrupt occurred, thus already locked. */
893 if (k_reenter
>= 0) {
894 result
= mini_notify(proc_addr(src
), dst
);
897 /* Call from task level, locking is required. */
900 result
= mini_notify(proc_addr(src
), dst
);
906 /*===========================================================================*
908 *===========================================================================*/
909 PRIVATE
void enqueue(rp
)
910 register struct proc
*rp
; /* this process is now runnable */
912 /* Add 'rp' to one of the queues of runnable processes. This function is
913 * responsible for inserting a process into one of the scheduling queues.
914 * The mechanism is implemented here. The actual scheduling policy is
915 * defined in sched() and pick_proc().
917 int q
; /* scheduling queue to use */
918 int front
; /* add to front or back */
920 #if DEBUG_SCHED_CHECK
921 check_runqueues("enqueue1");
922 if (rp
->p_ready
) kprintf("enqueue() already ready process\n");
925 /* Determine where to insert to process. */
926 sched(rp
, &q
, &front
);
928 /* Now add the process to the queue. */
929 if (rdy_head
[q
] == NIL_PROC
) { /* add to empty queue */
930 rdy_head
[q
] = rdy_tail
[q
] = rp
; /* create a new queue */
931 rp
->p_nextready
= NIL_PROC
; /* mark new end */
933 else if (front
) { /* add to head of queue */
934 rp
->p_nextready
= rdy_head
[q
]; /* chain head of queue */
935 rdy_head
[q
] = rp
; /* set new queue head */
937 else { /* add to tail of queue */
938 rdy_tail
[q
]->p_nextready
= rp
; /* chain tail of queue */
939 rdy_tail
[q
] = rp
; /* set new queue tail */
940 rp
->p_nextready
= NIL_PROC
; /* mark new end */
943 /* Now select the next process to run, if there isn't a current
944 * process yet or current process isn't ready any more, or
947 if(!proc_ptr
|| proc_ptr
->p_rts_flags
||
948 (priv(proc_ptr
)->s_flags
& PREEMPTIBLE
)) {
952 #if DEBUG_SCHED_CHECK
954 check_runqueues("enqueue2");
958 /*===========================================================================*
960 *===========================================================================*/
961 PRIVATE
void dequeue(rp
)
962 register struct proc
*rp
; /* this process is no longer runnable */
964 /* A process must be removed from the scheduling queues, for example, because
965 * it has blocked. If the currently active process is removed, a new process
966 * is picked to run by calling pick_proc().
968 register int q
= rp
->p_priority
; /* queue to use */
969 register struct proc
**xpp
; /* iterate over queue */
970 register struct proc
*prev_xp
;
972 /* Side-effect for kernel: check if the task's stack still is ok? */
974 if (*priv(rp
)->s_stack_guard
!= STACK_GUARD
)
975 panic("stack overrun by task", proc_nr(rp
));
978 #if DEBUG_SCHED_CHECK
979 check_runqueues("dequeue1");
980 if (! rp
->p_ready
) kprintf("dequeue() already unready process\n");
983 /* Now make sure that the process is not in its ready queue. Remove the
984 * process if it is found. A process can be made unready even if it is not
985 * running by being sent a signal that kills it.
988 for (xpp
= &rdy_head
[q
]; *xpp
!= NIL_PROC
; xpp
= &(*xpp
)->p_nextready
) {
990 if (*xpp
== rp
) { /* found process to remove */
991 *xpp
= (*xpp
)->p_nextready
; /* replace with next chain */
992 if (rp
== rdy_tail
[q
]) /* queue tail removed */
993 rdy_tail
[q
] = prev_xp
; /* set new tail */
994 if (rp
== proc_ptr
|| rp
== next_ptr
) /* active process removed */
995 pick_proc(); /* pick new process to run */
998 prev_xp
= *xpp
; /* save previous in chain */
1001 #if DEBUG_SCHED_CHECK
1003 check_runqueues("dequeue2");
1007 /*===========================================================================*
1009 *===========================================================================*/
1010 PRIVATE
void sched(rp
, queue
, front
)
1011 register struct proc
*rp
; /* process to be scheduled */
1012 int *queue
; /* return: queue to use */
1013 int *front
; /* return: front or back */
1015 /* This function determines the scheduling policy. It is called whenever a
1016 * process must be added to one of the scheduling queues to decide where to
1017 * insert it. As a side-effect the process' priority may be updated.
1019 int time_left
= (rp
->p_ticks_left
> 0); /* quantum fully consumed */
1021 /* Check whether the process has time left. Otherwise give a new quantum
1022 * and lower the process' priority, unless the process already is in the
1025 if (! time_left
) { /* quantum consumed ? */
1026 rp
->p_ticks_left
= rp
->p_quantum_size
; /* give new quantum */
1027 if (rp
->p_priority
< (IDLE_Q
-1)) {
1028 rp
->p_priority
+= 1; /* lower priority */
1032 /* If there is time left, the process is added to the front of its queue,
1033 * so that it can immediately run. The queue to use simply is always the
1034 * process' current priority.
1036 *queue
= rp
->p_priority
;
1040 /*===========================================================================*
1042 *===========================================================================*/
1043 PRIVATE
void pick_proc()
1045 /* Decide who to run now. A new process is selected by setting 'next_ptr'.
1046 * When a billable process is selected, record it in 'bill_ptr', so that the
1047 * clock task can tell who to bill for system time.
1049 register struct proc
*rp
; /* process to run */
1050 int q
; /* iterate over queues */
1052 /* Check each of the scheduling queues for ready processes. The number of
1053 * queues is defined in proc.h, and priorities are set in the task table.
1054 * The lowest queue contains IDLE, which is always ready.
1056 for (q
=0; q
< NR_SCHED_QUEUES
; q
++) {
1057 if ( (rp
= rdy_head
[q
]) != NIL_PROC
) {
1058 next_ptr
= rp
; /* run process 'rp' next */
1059 if (priv(rp
)->s_flags
& BILLABLE
)
1060 bill_ptr
= rp
; /* bill for system time */
1064 panic("no ready process", NO_NUM
);
1067 /*===========================================================================*
1069 *===========================================================================*/
1070 #define Q_BALANCE_TICKS 100
1071 PUBLIC
void balance_queues(tp
)
1072 timer_t
*tp
; /* watchdog timer pointer */
1074 /* Check entire process table and give all process a higher priority. This
1075 * effectively means giving a new quantum. If a process already is at its
1076 * maximum priority, its quantum will be renewed.
1078 static timer_t queue_timer
; /* timer structure to use */
1079 register struct proc
* rp
; /* process table pointer */
1080 clock_t next_period
; /* time of next period */
1081 int ticks_added
= 0; /* total time added */
1083 for (rp
=BEG_PROC_ADDR
; rp
<END_PROC_ADDR
; rp
++) {
1084 if (! isemptyp(rp
)) { /* check slot use */
1085 lock(5,"balance_queues");
1086 if (rp
->p_priority
> rp
->p_max_priority
) { /* update priority? */
1087 if (rp
->p_rts_flags
== 0) dequeue(rp
); /* take off queue */
1088 ticks_added
+= rp
->p_quantum_size
; /* do accounting */
1089 rp
->p_priority
-= 1; /* raise priority */
1090 if (rp
->p_rts_flags
== 0) enqueue(rp
); /* put on queue */
1093 ticks_added
+= rp
->p_quantum_size
- rp
->p_ticks_left
;
1094 rp
->p_ticks_left
= rp
->p_quantum_size
; /* give new quantum */
1100 kprintf("ticks_added: %d\n", ticks_added
);
1103 /* Now schedule a new watchdog timer to balance the queues again. The
1104 * period depends on the total amount of quantum ticks added.
1106 next_period
= MAX(Q_BALANCE_TICKS
, ticks_added
); /* calculate next */
1107 set_timer(&queue_timer
, get_uptime() + next_period
, balance_queues
);
1110 /*===========================================================================*
1112 *===========================================================================*/
1113 PUBLIC
int lock_send(dst_e
, m_ptr
)
1114 int dst_e
; /* to whom is message being sent? */
1115 message
*m_ptr
; /* pointer to message buffer */
1117 /* Safe gateway to mini_send() for tasks. */
1120 result
= mini_send(proc_ptr
, dst_e
, m_ptr
, NON_BLOCKING
);
1125 /*===========================================================================*
1127 *===========================================================================*/
1128 PUBLIC
void lock_enqueue(rp
)
1129 struct proc
*rp
; /* this process is now runnable */
1131 /* Safe gateway to enqueue() for tasks. */
1137 /*===========================================================================*
1139 *===========================================================================*/
1140 PUBLIC
void lock_dequeue(rp
)
1141 struct proc
*rp
; /* this process is no longer runnable */
1143 /* Safe gateway to dequeue() for tasks. */
1144 if (k_reenter
>= 0) {
1145 /* We're in an exception or interrupt, so don't lock (and ...
1156 /*===========================================================================*
1158 *===========================================================================*/
1159 #if DEBUG_ENABLE_IPC_WARNINGS
1160 PUBLIC
int isokendpt_f(file
, line
, e
, p
, fatalflag
)
1164 PUBLIC
int isokendpt_f(e
, p
, fatalflag
)
1170 /* Convert an endpoint number into a process number.
1171 * Return nonzero if the process is alive with the corresponding
1172 * generation number, zero otherwise.
1174 * This function is called with file and line number by the
1175 * isokendpt_d macro if DEBUG_ENABLE_IPC_WARNINGS is defined,
1176 * otherwise without. This allows us to print the where the
1177 * conversion was attempted, making the errors verbose without
1178 * adding code for that at every call.
1180 * If fatalflag is nonzero, we must panic if the conversion doesn't
1183 *p
= _ENDPOINT_P(e
);
1184 if(!isokprocn(*p
)) {
1185 #if DEBUG_ENABLE_IPC_WARNINGS
1186 kprintf("kernel:%s:%d: bad endpoint %d: proc %d out of range\n",
1189 } else if(isemptyn(*p
)) {
1190 #if DEBUG_ENABLE_IPC_WARNINGS
1191 kprintf("kernel:%s:%d: bad endpoint %d: proc %d empty\n", file
, line
, e
, *p
);
1193 } else if(proc_addr(*p
)->p_endpoint
!= e
) {
1194 #if DEBUG_ENABLE_IPC_WARNINGS
1195 kprintf("kernel:%s:%d: bad endpoint %d: proc %d has ept %d (generation %d vs. %d)\n", file
, line
,
1196 e
, *p
, proc_addr(*p
)->p_endpoint
,
1197 _ENDPOINT_G(e
), _ENDPOINT_G(proc_addr(*p
)->p_endpoint
));
1200 if(!ok
&& fatalflag
) {
1201 panic("invalid endpoint ", e
);