2005-04-21 16:53:53 +02:00
|
|
|
/* This file contains essentially all of the process and message handling.
|
2005-05-30 13:05:42 +02:00
|
|
|
* Together with "mpx.s" it forms the lowest layer of the MINIX kernel.
|
|
|
|
* There is one entry point from the outside:
|
2005-04-21 16:53:53 +02:00
|
|
|
*
|
2005-05-24 12:06:17 +02:00
|
|
|
* sys_call: a system call, i.e., the kernel is trapped with an INT
|
2005-04-21 16:53:53 +02:00
|
|
|
*
|
2005-05-30 13:05:42 +02:00
|
|
|
* As well as several entry points used from the interrupt and task level:
|
2005-04-21 16:53:53 +02:00
|
|
|
*
|
2005-06-17 15:00:04 +02:00
|
|
|
* lock_notify: notify a process of a system event
|
2005-05-18 12:36:23 +02:00
|
|
|
* lock_send: send a message to a process
|
2005-08-19 18:43:28 +02:00
|
|
|
* lock_enqueue: put a process on one of the scheduling queues
|
|
|
|
* lock_dequeue: remove a process from the scheduling queues
|
2005-04-21 16:53:53 +02:00
|
|
|
*
|
|
|
|
* Changes:
|
2005-08-19 18:43:28 +02:00
|
|
|
* Aug 19, 2005 rewrote multilevel scheduling code (Jorrit N. Herder)
|
2005-08-04 11:26:36 +02:00
|
|
|
* Jul 25, 2005 protection and checks in sys_call() (Jorrit N. Herder)
|
2005-08-19 18:43:28 +02:00
|
|
|
* May 26, 2005 rewrote message passing functions (Jorrit N. Herder)
|
|
|
|
* May 24, 2005 new notification system call (Jorrit N. Herder)
|
|
|
|
* Oct 28, 2004 nonblocking send and receive calls (Jorrit N. Herder)
|
2005-05-30 13:05:42 +02:00
|
|
|
*
|
|
|
|
* The code here is critical to make everything work and is important for the
|
|
|
|
* overall performance of the system. A large fraction of the code deals with
|
|
|
|
* list manipulation. To make this both easy to understand and fast to execute
|
|
|
|
* pointer pointers are used throughout the code. Pointer pointers prevent
|
|
|
|
* exceptions for the head or tail of a linked list.
|
|
|
|
*
|
|
|
|
* node_t *queue, *new_node; // assume these as global variables
|
|
|
|
* node_t **xpp = &queue; // get pointer pointer to head of queue
|
|
|
|
* while (*xpp != NULL) // find last pointer of the linked list
|
|
|
|
* xpp = &(*xpp)->next; // get pointer to next pointer
|
|
|
|
* *xpp = new_node; // now replace the end (the NULL pointer)
|
|
|
|
* new_node->next = NULL; // and mark the new end of the list
|
|
|
|
*
|
|
|
|
* For example, when adding a new node to the end of the list, one normally
|
|
|
|
* makes an exception for an empty list and looks up the end of the list for
|
|
|
|
* nonempty lists. As shown above, this is not required with pointer pointers.
|
2005-04-21 16:53:53 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <minix/com.h>
|
2005-07-14 17:12:12 +02:00
|
|
|
#include <minix/callnr.h>
|
|
|
|
#include "kernel.h"
|
2005-04-21 16:53:53 +02:00
|
|
|
#include "proc.h"
|
|
|
|
|
|
|
|
|
2005-05-24 12:06:17 +02:00
|
|
|
/* Scheduling and message passing functions. The functions are available to
|
|
|
|
* other parts of the kernel through lock_...(). The lock temporarily disables
|
|
|
|
* interrupts to prevent race conditions.
|
|
|
|
*/
|
|
|
|
FORWARD _PROTOTYPE( int mini_send, (struct proc *caller_ptr, int dst,
|
2005-05-27 14:44:14 +02:00
|
|
|
message *m_ptr, unsigned flags) );
|
2005-06-07 14:34:25 +02:00
|
|
|
FORWARD _PROTOTYPE( int mini_receive, (struct proc *caller_ptr, int src,
|
2005-05-27 14:44:14 +02:00
|
|
|
message *m_ptr, unsigned flags) );
|
2005-07-27 16:32:16 +02:00
|
|
|
FORWARD _PROTOTYPE( int mini_notify, (struct proc *caller_ptr, int dst) );
|
2005-05-24 12:06:17 +02:00
|
|
|
|
2005-08-19 18:43:28 +02:00
|
|
|
FORWARD _PROTOTYPE( void enqueue, (struct proc *rp) );
|
|
|
|
FORWARD _PROTOTYPE( void dequeue, (struct proc *rp) );
|
|
|
|
FORWARD _PROTOTYPE( void sched, (struct proc *rp, int *queue, int *front) );
|
2005-04-21 16:53:53 +02:00
|
|
|
FORWARD _PROTOTYPE( void pick_proc, (void) );
|
2005-08-19 18:43:28 +02:00
|
|
|
FORWARD _PROTOTYPE( void balance_queues, (struct proc *rp) );
|
2005-07-14 17:12:12 +02:00
|
|
|
|
|
|
|
#define BuildMess(m_ptr, src, dst_ptr) \
|
|
|
|
(m_ptr)->m_source = (src); \
|
|
|
|
(m_ptr)->m_type = NOTIFY_FROM(src); \
|
|
|
|
(m_ptr)->NOTIFY_TIMESTAMP = get_uptime(); \
|
2005-07-19 14:21:36 +02:00
|
|
|
switch (src) { \
|
|
|
|
case HARDWARE: \
|
2005-07-14 17:12:12 +02:00
|
|
|
(m_ptr)->NOTIFY_ARG = priv(dst_ptr)->s_int_pending; \
|
|
|
|
priv(dst_ptr)->s_int_pending = 0; \
|
2005-07-19 14:21:36 +02:00
|
|
|
break; \
|
|
|
|
case SYSTEM: \
|
|
|
|
(m_ptr)->NOTIFY_ARG = priv(dst_ptr)->s_sig_pending; \
|
|
|
|
priv(dst_ptr)->s_sig_pending = 0; \
|
|
|
|
break; \
|
2005-07-14 17:12:12 +02:00
|
|
|
}
|
|
|
|
|
2005-04-21 16:53:53 +02:00
|
|
|
#if (CHIP == INTEL)
|
|
|
|
#define CopyMess(s,sp,sm,dp,dm) \
|
|
|
|
cp_mess(s, (sp)->p_memmap[D].mem_phys, (vir_bytes)sm, (dp)->p_memmap[D].mem_phys, (vir_bytes)dm)
|
|
|
|
#endif /* (CHIP == INTEL) */
|
|
|
|
|
|
|
|
#if (CHIP == M68000)
|
|
|
|
/* M68000 does not have cp_mess() in assembly like INTEL. Declare prototype
|
|
|
|
* for cp_mess() here and define the function below. Also define CopyMess.
|
|
|
|
*/
|
|
|
|
#endif /* (CHIP == M68000) */
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* sys_call *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int sys_call(call_nr, src_dst, m_ptr)
|
2005-05-30 13:05:42 +02:00
|
|
|
int call_nr; /* system call number and flags */
|
2005-05-24 12:06:17 +02:00
|
|
|
int src_dst; /* src to receive from or dst to send to */
|
2005-04-21 16:53:53 +02:00
|
|
|
message *m_ptr; /* pointer to message in the caller's space */
|
|
|
|
{
|
|
|
|
/* System calls are done by trapping to the kernel with an INT instruction.
|
|
|
|
* The trap is caught and sys_call() is called to send or receive a message
|
|
|
|
* (or both). The caller is always given by 'proc_ptr'.
|
|
|
|
*/
|
|
|
|
register struct proc *caller_ptr = proc_ptr; /* get pointer to caller */
|
|
|
|
int function = call_nr & SYSCALL_FUNC; /* get system call function */
|
2005-05-27 14:44:14 +02:00
|
|
|
unsigned flags = call_nr & SYSCALL_FLAGS; /* get flags */
|
2005-04-21 16:53:53 +02:00
|
|
|
int mask_entry; /* bit to check in send mask */
|
|
|
|
int result; /* the system call's result */
|
2005-05-26 15:17:57 +02:00
|
|
|
vir_clicks vlo, vhi; /* virtual clicks containing message to send */
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2005-05-31 11:50:51 +02:00
|
|
|
/* Check if the process has privileges for the requested call. Calls to the
|
|
|
|
* kernel may only be SENDREC, because tasks always reply and may not block
|
|
|
|
* if the caller doesn't do receive().
|
2005-04-21 16:53:53 +02:00
|
|
|
*/
|
2005-08-04 21:23:03 +02:00
|
|
|
if (! (priv(caller_ptr)->s_trap_mask & (1 << function)) ||
|
2005-08-08 14:18:45 +02:00
|
|
|
(iskerneln(src_dst) && function != SENDREC && function != RECEIVE)) {
|
2005-08-10 11:51:29 +02:00
|
|
|
kprintf("sys_call: trap %d not allowed, caller %d, src_dst %d\n",
|
|
|
|
function, proc_nr(caller_ptr), src_dst);
|
|
|
|
return(ECALLDENIED); /* trap denied by mask or kernel */
|
2005-08-05 20:57:20 +02:00
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2005-06-07 14:34:25 +02:00
|
|
|
/* Require a valid source and/ or destination process, unless echoing. */
|
2005-08-02 17:28:09 +02:00
|
|
|
if (! (isokprocn(src_dst) || src_dst == ANY || function == ECHO)) {
|
2005-08-05 20:57:20 +02:00
|
|
|
kprintf("sys_call: invalid src_dst, src_dst %d, caller %d\n",
|
|
|
|
src_dst, proc_nr(caller_ptr));
|
|
|
|
return(EBADSRCDST); /* invalid process number */
|
2005-08-02 17:28:09 +02:00
|
|
|
}
|
2005-05-26 15:17:57 +02:00
|
|
|
|
2005-07-26 14:48:34 +02:00
|
|
|
/* If the call involves a message buffer, i.e., for SEND, RECEIVE, SENDREC,
|
|
|
|
* or ECHO, check the message pointer. This check allows a message to be
|
|
|
|
* anywhere in data or stack or gap. It will have to be made more elaborate
|
|
|
|
* for machines which don't have the gap mapped.
|
2005-05-26 15:17:57 +02:00
|
|
|
*/
|
2005-07-26 15:51:21 +02:00
|
|
|
if (function & CHECK_PTR) {
|
2005-08-05 20:57:20 +02:00
|
|
|
vlo = (vir_bytes) m_ptr >> CLICK_SHIFT;
|
|
|
|
vhi = ((vir_bytes) m_ptr + MESS_SIZE - 1) >> CLICK_SHIFT;
|
2005-07-26 14:48:34 +02:00
|
|
|
if (vlo < caller_ptr->p_memmap[D].mem_vir || vlo > vhi ||
|
|
|
|
vhi >= caller_ptr->p_memmap[S].mem_vir +
|
2005-08-05 20:57:20 +02:00
|
|
|
caller_ptr->p_memmap[S].mem_len) {
|
2005-08-10 11:51:29 +02:00
|
|
|
kprintf("sys_call: invalid message pointer, trap %d, caller %d\n",
|
2005-08-05 20:57:20 +02:00
|
|
|
function, proc_nr(caller_ptr));
|
|
|
|
return(EFAULT); /* invalid message pointer */
|
|
|
|
}
|
2005-07-26 14:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the call is to send to a process, i.e., for SEND, SENDREC or NOTIFY,
|
|
|
|
* verify that the caller is allowed to send to the given destination and
|
|
|
|
* that the destination is still alive.
|
|
|
|
*/
|
2005-07-26 15:51:21 +02:00
|
|
|
if (function & CHECK_DST) {
|
2005-08-04 21:23:03 +02:00
|
|
|
if (! get_sys_bit(priv(caller_ptr)->s_ipc_to, nr_to_id(src_dst))) {
|
2005-08-05 20:57:20 +02:00
|
|
|
kprintf("sys_call: ipc mask denied %d sending to %d\n",
|
2005-07-26 14:48:34 +02:00
|
|
|
proc_nr(caller_ptr), src_dst);
|
2005-08-05 20:57:20 +02:00
|
|
|
return(ECALLDENIED); /* call denied by ipc mask */
|
2005-07-26 14:48:34 +02:00
|
|
|
}
|
|
|
|
|
2005-08-05 20:57:20 +02:00
|
|
|
if (isemptyn(src_dst)) {
|
|
|
|
kprintf("sys_call: dead destination, function %d, caller %d\n",
|
|
|
|
function, proc_nr(caller_ptr));
|
|
|
|
return(EDEADDST); /* cannot send to the dead */
|
|
|
|
}
|
2005-07-26 14:48:34 +02:00
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
/* Now check if the call is known and try to perform the request. The only
|
|
|
|
* system calls that exist in MINIX are sending and receiving messages.
|
2005-05-30 13:05:42 +02:00
|
|
|
* - SENDREC: combines SEND and RECEIVE in a single system call
|
|
|
|
* - SEND: sender blocks until its message has been delivered
|
|
|
|
* - RECEIVE: receiver blocks until an acceptable message has arrived
|
2005-07-26 14:48:34 +02:00
|
|
|
* - NOTIFY: nonblocking call; deliver notification or mark pending
|
|
|
|
* - ECHO: nonblocking call; directly echo back the message
|
2005-04-21 16:53:53 +02:00
|
|
|
*/
|
2005-05-26 15:17:57 +02:00
|
|
|
switch(function) {
|
2005-07-27 16:08:59 +02:00
|
|
|
case SENDREC:
|
2005-07-27 16:32:16 +02:00
|
|
|
/* A flag is set so that notifications cannot interrupt SENDREC. */
|
|
|
|
priv(caller_ptr)->s_flags |= SENDREC_BUSY;
|
2005-05-27 14:44:14 +02:00
|
|
|
/* fall through */
|
|
|
|
case SEND:
|
|
|
|
result = mini_send(caller_ptr, src_dst, m_ptr, flags);
|
2005-05-26 15:17:57 +02:00
|
|
|
if (function == SEND || result != OK) {
|
|
|
|
break; /* done, or SEND failed */
|
2005-05-27 14:44:14 +02:00
|
|
|
} /* fall through for SENDREC */
|
2005-05-26 15:17:57 +02:00
|
|
|
case RECEIVE:
|
2005-07-27 16:32:16 +02:00
|
|
|
if (function == RECEIVE)
|
|
|
|
priv(caller_ptr)->s_flags &= ~SENDREC_BUSY;
|
2005-06-07 14:34:25 +02:00
|
|
|
result = mini_receive(caller_ptr, src_dst, m_ptr, flags);
|
2005-05-26 15:17:57 +02:00
|
|
|
break;
|
|
|
|
case NOTIFY:
|
2005-07-27 16:32:16 +02:00
|
|
|
result = mini_notify(caller_ptr, src_dst);
|
2005-05-26 15:17:57 +02:00
|
|
|
break;
|
2005-05-27 15:57:00 +02:00
|
|
|
case ECHO:
|
|
|
|
CopyMess(caller_ptr->p_nr, caller_ptr, m_ptr, caller_ptr, m_ptr);
|
|
|
|
result = OK;
|
|
|
|
break;
|
2005-05-26 15:17:57 +02:00
|
|
|
default:
|
|
|
|
result = EBADCALL; /* illegal system call */
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, return the result of the system call to the caller. */
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mini_send *
|
|
|
|
*===========================================================================*/
|
2005-05-26 15:17:57 +02:00
|
|
|
PRIVATE int mini_send(caller_ptr, dst, m_ptr, flags)
|
2005-04-21 16:53:53 +02:00
|
|
|
register struct proc *caller_ptr; /* who is trying to send a message? */
|
2005-05-24 12:06:17 +02:00
|
|
|
int dst; /* to whom is message being sent? */
|
2005-04-21 16:53:53 +02:00
|
|
|
message *m_ptr; /* pointer to message buffer */
|
2005-05-27 14:44:14 +02:00
|
|
|
unsigned flags; /* system call flags */
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2005-05-24 12:06:17 +02:00
|
|
|
/* Send a message from 'caller_ptr' to 'dst'. If 'dst' is blocked waiting
|
|
|
|
* for this message, copy the message to it and unblock 'dst'. If 'dst' is
|
2005-04-21 16:53:53 +02:00
|
|
|
* not waiting at all, or is waiting for another source, queue 'caller_ptr'.
|
|
|
|
*/
|
2005-06-20 13:26:48 +02:00
|
|
|
register struct proc *dst_ptr = proc_addr(dst);
|
2005-05-26 15:17:57 +02:00
|
|
|
register struct proc **xpp;
|
2005-06-07 14:34:25 +02:00
|
|
|
register struct proc *xp;
|
2005-05-27 14:44:14 +02:00
|
|
|
|
2005-06-06 11:30:44 +02:00
|
|
|
/* Check for deadlock by 'caller_ptr' and 'dst' sending to each other. */
|
2005-06-07 14:34:25 +02:00
|
|
|
xp = dst_ptr;
|
2005-06-30 17:55:19 +02:00
|
|
|
while (xp->p_rts_flags & SENDING) { /* check while sending */
|
2005-06-07 14:34:25 +02:00
|
|
|
xp = proc_addr(xp->p_sendto); /* get xp's destination */
|
|
|
|
if (xp == caller_ptr) return(ELOCKED); /* deadlock if cyclic */
|
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2005-05-26 15:17:57 +02:00
|
|
|
/* Check if 'dst' is blocked waiting for this message. The destination's
|
2005-05-30 13:05:42 +02:00
|
|
|
* SENDING flag may be set when its SENDREC call blocked while sending.
|
2005-05-26 15:17:57 +02:00
|
|
|
*/
|
2005-06-30 17:55:19 +02:00
|
|
|
if ( (dst_ptr->p_rts_flags & (RECEIVING | SENDING)) == RECEIVING &&
|
2005-05-26 15:17:57 +02:00
|
|
|
(dst_ptr->p_getfrom == ANY || dst_ptr->p_getfrom == caller_ptr->p_nr)) {
|
2005-04-21 16:53:53 +02:00
|
|
|
/* Destination is indeed waiting for this message. */
|
2005-05-26 15:17:57 +02:00
|
|
|
CopyMess(caller_ptr->p_nr, caller_ptr, m_ptr, dst_ptr,
|
2005-05-24 12:06:17 +02:00
|
|
|
dst_ptr->p_messbuf);
|
2005-08-19 18:43:28 +02:00
|
|
|
if ((dst_ptr->p_rts_flags &= ~RECEIVING) == 0) enqueue(dst_ptr);
|
2005-05-27 14:44:14 +02:00
|
|
|
} else if ( ! (flags & NON_BLOCKING)) {
|
2005-04-21 16:53:53 +02:00
|
|
|
/* Destination is not waiting. Block and queue caller. */
|
|
|
|
caller_ptr->p_messbuf = m_ptr;
|
2005-08-19 18:43:28 +02:00
|
|
|
if (caller_ptr->p_rts_flags == 0) dequeue(caller_ptr);
|
2005-06-30 17:55:19 +02:00
|
|
|
caller_ptr->p_rts_flags |= SENDING;
|
2005-05-24 12:06:17 +02:00
|
|
|
caller_ptr->p_sendto = dst;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
/* Process is now blocked. Put in on the destination's queue. */
|
2005-05-30 13:05:42 +02:00
|
|
|
xpp = &dst_ptr->p_caller_q; /* find end of list */
|
2005-08-19 18:43:28 +02:00
|
|
|
while (*xpp != NIL_PROC) xpp = &(*xpp)->p_q_link;
|
2005-05-30 13:05:42 +02:00
|
|
|
*xpp = caller_ptr; /* add caller to end */
|
|
|
|
caller_ptr->p_q_link = NIL_PROC; /* mark new end of list */
|
2005-04-21 16:53:53 +02:00
|
|
|
} else {
|
|
|
|
return(ENOTREADY);
|
|
|
|
}
|
|
|
|
return(OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2005-06-07 14:34:25 +02:00
|
|
|
* mini_receive *
|
2005-04-21 16:53:53 +02:00
|
|
|
*===========================================================================*/
|
2005-06-07 14:34:25 +02:00
|
|
|
PRIVATE int mini_receive(caller_ptr, src, m_ptr, flags)
|
2005-04-21 16:53:53 +02:00
|
|
|
register struct proc *caller_ptr; /* process trying to get message */
|
|
|
|
int src; /* which message source is wanted */
|
|
|
|
message *m_ptr; /* pointer to message buffer */
|
2005-05-27 14:44:14 +02:00
|
|
|
unsigned flags; /* system call flags */
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2005-05-30 13:05:42 +02:00
|
|
|
/* A process or task wants to get a message. If a message is already queued,
|
2005-04-21 16:53:53 +02:00
|
|
|
* acquire it and deblock the sender. If no message from the desired source
|
2005-05-30 13:05:42 +02:00
|
|
|
* is available block the caller, unless the flags don't allow blocking.
|
2005-04-21 16:53:53 +02:00
|
|
|
*/
|
2005-05-26 15:17:57 +02:00
|
|
|
register struct proc **xpp;
|
2005-05-19 16:05:51 +02:00
|
|
|
register struct notification **ntf_q_pp;
|
2005-04-21 16:53:53 +02:00
|
|
|
message m;
|
2005-05-26 15:17:57 +02:00
|
|
|
int bit_nr;
|
2005-07-14 17:12:12 +02:00
|
|
|
sys_map_t *map;
|
|
|
|
bitchunk_t *chunk;
|
|
|
|
int i, src_id, src_proc_nr;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2005-05-26 15:17:57 +02:00
|
|
|
/* Check to see if a message from desired source is already available.
|
2005-05-27 14:44:14 +02:00
|
|
|
* The caller's SENDING flag may be set if SENDREC couldn't send. If it is
|
2005-05-26 15:17:57 +02:00
|
|
|
* set, the process should be blocked.
|
|
|
|
*/
|
2005-06-30 17:55:19 +02:00
|
|
|
if (!(caller_ptr->p_rts_flags & SENDING)) {
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2005-05-30 13:05:42 +02:00
|
|
|
/* Check if there are pending notifications, except for SENDREC. */
|
2005-07-27 16:32:16 +02:00
|
|
|
if (! (priv(caller_ptr)->s_flags & SENDREC_BUSY)) {
|
2005-05-27 14:44:14 +02:00
|
|
|
|
2005-07-14 17:12:12 +02:00
|
|
|
map = &priv(caller_ptr)->s_notify_pending;
|
|
|
|
for (chunk=&map->chunk[0]; chunk<&map->chunk[NR_SYS_CHUNKS]; chunk++) {
|
|
|
|
|
|
|
|
/* Find a pending notification from the requested source. */
|
|
|
|
if (! *chunk) continue; /* no bits in chunk */
|
|
|
|
for (i=0; ! (*chunk & (1<<i)); ++i) {} /* look up the bit */
|
|
|
|
src_id = (chunk - &map->chunk[0]) * BITCHUNK_BITS + i;
|
|
|
|
if (src_id >= NR_SYS_PROCS) break; /* out of range */
|
|
|
|
src_proc_nr = id_to_nr(src_id); /* get source proc */
|
|
|
|
if (src!=ANY && src!=src_proc_nr) continue; /* source not ok */
|
|
|
|
*chunk &= ~(1 << i); /* no longer pending */
|
|
|
|
|
|
|
|
/* Found a suitable source, deliver the notification message. */
|
|
|
|
BuildMess(&m, src_proc_nr, caller_ptr); /* assemble message */
|
|
|
|
CopyMess(src_proc_nr, proc_addr(HARDWARE), &m, caller_ptr, m_ptr);
|
|
|
|
return(OK); /* report success */
|
|
|
|
}
|
2005-05-19 16:05:51 +02:00
|
|
|
}
|
2005-07-14 17:12:12 +02:00
|
|
|
|
|
|
|
/* Check caller queue. Use pointer pointers to keep code simple. */
|
|
|
|
xpp = &caller_ptr->p_caller_q;
|
|
|
|
while (*xpp != NIL_PROC) {
|
2005-07-27 16:32:16 +02:00
|
|
|
if (src == ANY || src == proc_nr(*xpp)) {
|
2005-07-14 17:12:12 +02:00
|
|
|
/* Found acceptable message. Copy it and update status. */
|
|
|
|
CopyMess((*xpp)->p_nr, *xpp, (*xpp)->p_messbuf, caller_ptr, m_ptr);
|
2005-08-19 18:43:28 +02:00
|
|
|
if (((*xpp)->p_rts_flags &= ~SENDING) == 0) enqueue(*xpp);
|
2005-07-14 17:12:12 +02:00
|
|
|
*xpp = (*xpp)->p_q_link; /* remove from queue */
|
|
|
|
return(OK); /* report success */
|
|
|
|
}
|
|
|
|
xpp = &(*xpp)->p_q_link; /* proceed to next */
|
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
|
2005-05-27 14:44:14 +02:00
|
|
|
/* No suitable message is available or the caller couldn't send in SENDREC.
|
2005-05-26 15:17:57 +02:00
|
|
|
* Block the process trying to receive, unless the flags tell otherwise.
|
2005-04-21 16:53:53 +02:00
|
|
|
*/
|
2005-05-27 14:44:14 +02:00
|
|
|
if ( ! (flags & NON_BLOCKING)) {
|
2005-05-30 13:05:42 +02:00
|
|
|
caller_ptr->p_getfrom = src;
|
2005-04-21 16:53:53 +02:00
|
|
|
caller_ptr->p_messbuf = m_ptr;
|
2005-08-19 18:43:28 +02:00
|
|
|
if (caller_ptr->p_rts_flags == 0) dequeue(caller_ptr);
|
2005-06-30 17:55:19 +02:00
|
|
|
caller_ptr->p_rts_flags |= RECEIVING;
|
2005-04-21 16:53:53 +02:00
|
|
|
return(OK);
|
|
|
|
} else {
|
|
|
|
return(ENOTREADY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-24 12:06:17 +02:00
|
|
|
|
2005-07-14 17:12:12 +02:00
|
|
|
/*===========================================================================*
|
2005-07-27 16:32:16 +02:00
|
|
|
* mini_notify *
|
2005-07-14 17:12:12 +02:00
|
|
|
*===========================================================================*/
|
2005-07-27 16:32:16 +02:00
|
|
|
PRIVATE int mini_notify(caller_ptr, dst)
|
2005-07-14 17:12:12 +02:00
|
|
|
register struct proc *caller_ptr; /* sender of the notification */
|
|
|
|
int dst; /* which process to notify */
|
|
|
|
{
|
|
|
|
register struct proc *dst_ptr = proc_addr(dst);
|
|
|
|
int src_id; /* source id for late delivery */
|
|
|
|
message m; /* the notification message */
|
|
|
|
|
|
|
|
/* Check to see if target is blocked waiting for this message. A process
|
|
|
|
* can be both sending and receiving during a SENDREC system call.
|
|
|
|
*/
|
|
|
|
if ((dst_ptr->p_rts_flags & (RECEIVING|SENDING)) == RECEIVING &&
|
2005-07-27 16:32:16 +02:00
|
|
|
! (priv(dst_ptr)->s_flags & SENDREC_BUSY) &&
|
2005-07-14 17:12:12 +02:00
|
|
|
(dst_ptr->p_getfrom == ANY || dst_ptr->p_getfrom == caller_ptr->p_nr)) {
|
|
|
|
|
|
|
|
/* Destination is indeed waiting for a message. Assemble a notification
|
|
|
|
* message and deliver it. Copy from pseudo-source HARDWARE, since the
|
|
|
|
* message is in the kernel's address space.
|
|
|
|
*/
|
|
|
|
BuildMess(&m, proc_nr(caller_ptr), dst_ptr);
|
|
|
|
CopyMess(proc_nr(caller_ptr), proc_addr(HARDWARE), &m,
|
|
|
|
dst_ptr, dst_ptr->p_messbuf);
|
|
|
|
dst_ptr->p_rts_flags &= ~RECEIVING; /* deblock destination */
|
2005-08-19 18:43:28 +02:00
|
|
|
if (dst_ptr->p_rts_flags == 0) enqueue(dst_ptr);
|
2005-07-14 17:12:12 +02:00
|
|
|
return(OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Destination is not ready to receive the notification. Add it to the
|
|
|
|
* bit map with pending notifications. Note the indirectness: the system id
|
|
|
|
* instead of the process number is used in the pending bit map.
|
|
|
|
*/
|
|
|
|
src_id = priv(caller_ptr)->s_id;
|
|
|
|
set_sys_bit(priv(dst_ptr)->s_notify_pending, src_id);
|
|
|
|
return(OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-24 12:06:17 +02:00
|
|
|
/*==========================================================================*
|
|
|
|
* lock_notify *
|
|
|
|
*==========================================================================*/
|
2005-07-27 16:32:16 +02:00
|
|
|
PUBLIC int lock_notify(src, dst)
|
2005-07-14 17:12:12 +02:00
|
|
|
int src; /* sender of the notification */
|
|
|
|
int dst; /* who is to be notified */
|
2005-05-24 12:06:17 +02:00
|
|
|
{
|
2005-07-14 17:12:12 +02:00
|
|
|
/* Safe gateway to mini_notify() for tasks and interrupt handlers. The sender
|
|
|
|
* is explicitely given to prevent confusion where the call comes from. MINIX
|
2005-06-17 15:00:04 +02:00
|
|
|
* kernel is not reentrant, which means to interrupts are disabled after
|
|
|
|
* the first kernel entry (hardware interrupt, trap, or exception). Locking
|
2005-07-14 17:12:12 +02:00
|
|
|
* is done by temporarily disabling interrupts.
|
2005-05-24 16:35:58 +02:00
|
|
|
*/
|
2005-05-24 12:06:17 +02:00
|
|
|
int result;
|
2005-05-18 12:36:23 +02:00
|
|
|
|
2005-06-17 15:00:04 +02:00
|
|
|
/* Exception or interrupt occurred, thus already locked. */
|
|
|
|
if (k_reenter >= 0) {
|
2005-07-27 16:32:16 +02:00
|
|
|
result = mini_notify(proc_addr(src), dst);
|
2005-06-17 15:00:04 +02:00
|
|
|
}
|
2005-06-17 11:09:54 +02:00
|
|
|
|
2005-06-17 15:00:04 +02:00
|
|
|
/* Call from task level, locking is required. */
|
|
|
|
else {
|
2005-07-27 16:32:16 +02:00
|
|
|
lock(0, "notify");
|
|
|
|
result = mini_notify(proc_addr(src), dst);
|
2005-06-17 15:00:04 +02:00
|
|
|
unlock(0);
|
|
|
|
}
|
2005-06-17 11:09:54 +02:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-21 16:53:53 +02:00
|
|
|
/*===========================================================================*
|
2005-08-19 18:43:28 +02:00
|
|
|
* enqueue *
|
2005-04-21 16:53:53 +02:00
|
|
|
*===========================================================================*/
|
2005-08-19 18:43:28 +02:00
|
|
|
PRIVATE void enqueue(rp)
|
2005-04-21 16:53:53 +02:00
|
|
|
register struct proc *rp; /* this process is now runnable */
|
|
|
|
{
|
2005-08-19 18:43:28 +02:00
|
|
|
/* Add 'rp' to one of the queues of runnable processes. We need to decide
|
|
|
|
* where to put the process based on its quantum. If there is time left, it
|
|
|
|
* is added to the front of its queue, so that it can immediately run.
|
|
|
|
* Otherwise its is given a new quantum and added to the rear of the queue.
|
|
|
|
*/
|
|
|
|
register int q; /* scheduling queue to use */
|
|
|
|
int time_left; /* quantum fully used? */
|
|
|
|
|
|
|
|
/* Check if the process has time left and determine what queue to use. A
|
|
|
|
* process that consumed a full quantum is given a lower priority, so that
|
|
|
|
* the CPU-bound processes cannot starve I/O-bound processes. When the
|
|
|
|
* threshold is reached, the scheduling queues are balanced to prevent all
|
|
|
|
* processes from ending up in the lowest queue.
|
|
|
|
*/
|
|
|
|
time_left = (rp->p_sched_ticks > 0); /* check ticks left */
|
|
|
|
if ( ! time_left) { /* quantum consumed ? */
|
|
|
|
rp->p_sched_ticks = rp->p_quantum_size; /* give new quantum */
|
|
|
|
#if DEAD_CODE
|
|
|
|
if (proc_nr(rp) != IDLE) { /* already lowest priority */
|
|
|
|
rp->p_priority ++; /* lower the priority */
|
|
|
|
if (rp->p_priority >= IDLE_Q) /* threshold exceeded */
|
|
|
|
balance_queues(rp); /* rebalance queues */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
q = rp->p_priority; /* scheduling queue to use */
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2005-07-14 17:12:12 +02:00
|
|
|
#if DEBUG_SCHED_CHECK
|
2005-08-19 18:43:28 +02:00
|
|
|
check_runqueues("enqueue");
|
|
|
|
if(rp->p_ready) kprintf("enqueue() already ready process\n");
|
2005-05-24 14:32:34 +02:00
|
|
|
#endif
|
|
|
|
|
2005-08-19 18:43:28 +02:00
|
|
|
/* Now add the process to the queue. */
|
2005-06-20 13:26:48 +02:00
|
|
|
if (rdy_head[q] == NIL_PROC) { /* add to empty queue */
|
|
|
|
rdy_head[q] = rdy_tail[q] = rp; /* create a new queue */
|
|
|
|
rp->p_nextready = NIL_PROC; /* mark new end */
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
2005-08-19 18:43:28 +02:00
|
|
|
else if (time_left) { /* add to head of queue */
|
2005-06-20 13:26:48 +02:00
|
|
|
rp->p_nextready = rdy_head[q]; /* chain head of queue */
|
|
|
|
rdy_head[q] = rp; /* set new queue head */
|
|
|
|
}
|
|
|
|
else { /* add to tail of queue */
|
|
|
|
rdy_tail[q]->p_nextready = rp; /* chain tail of queue */
|
|
|
|
rdy_tail[q] = rp; /* set new queue tail */
|
|
|
|
rp->p_nextready = NIL_PROC; /* mark new end */
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
2005-06-30 17:55:19 +02:00
|
|
|
pick_proc(); /* select next to run */
|
2005-07-14 17:12:12 +02:00
|
|
|
|
|
|
|
#if DEBUG_SCHED_CHECK
|
|
|
|
rp->p_ready = 1;
|
2005-08-19 18:43:28 +02:00
|
|
|
check_runqueues("enqueue");
|
2005-07-14 17:12:12 +02:00
|
|
|
#endif
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2005-08-19 18:43:28 +02:00
|
|
|
* dequeue *
|
2005-04-21 16:53:53 +02:00
|
|
|
*===========================================================================*/
|
2005-08-19 18:43:28 +02:00
|
|
|
PRIVATE void dequeue(rp)
|
2005-04-21 16:53:53 +02:00
|
|
|
register struct proc *rp; /* this process is no longer runnable */
|
|
|
|
{
|
|
|
|
/* A process has blocked. See ready for a description of the queues. */
|
|
|
|
|
2005-05-30 13:05:42 +02:00
|
|
|
register int q = rp->p_priority; /* queue to use */
|
|
|
|
register struct proc **xpp; /* iterate over queue */
|
2005-06-20 13:26:48 +02:00
|
|
|
register struct proc *prev_xp;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2005-06-24 18:24:40 +02:00
|
|
|
/* Side-effect for kernel: check if the task's stack still is ok? */
|
|
|
|
if (iskernelp(rp)) {
|
2005-07-14 17:12:12 +02:00
|
|
|
if (*priv(rp)->s_stack_guard != STACK_GUARD)
|
2005-05-26 15:17:57 +02:00
|
|
|
panic("stack overrun by task", proc_nr(rp));
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
|
2005-07-14 17:12:12 +02:00
|
|
|
#if DEBUG_SCHED_CHECK
|
2005-08-19 18:43:28 +02:00
|
|
|
check_runqueues("dequeue");
|
|
|
|
if (! rp->p_ready) kprintf("dequeue() already unready process\n");
|
2005-07-14 17:12:12 +02:00
|
|
|
#endif
|
|
|
|
|
2005-04-21 16:53:53 +02:00
|
|
|
/* Now make sure that the process is not in its ready queue. Remove the
|
2005-05-20 11:37:43 +02:00
|
|
|
* process if it is found. A process can be made unready even if it is not
|
|
|
|
* running by being sent a signal that kills it.
|
2005-04-21 16:53:53 +02:00
|
|
|
*/
|
2005-06-20 13:26:48 +02:00
|
|
|
prev_xp = NIL_PROC;
|
|
|
|
for (xpp = &rdy_head[q]; *xpp != NIL_PROC; xpp = &(*xpp)->p_nextready) {
|
|
|
|
|
|
|
|
if (*xpp == rp) { /* found process to remove */
|
|
|
|
*xpp = (*xpp)->p_nextready; /* replace with next chain */
|
|
|
|
if (rp == rdy_tail[q]) /* queue tail removed */
|
|
|
|
rdy_tail[q] = prev_xp; /* set new tail */
|
|
|
|
if (rp == proc_ptr || rp == next_ptr) /* active process removed */
|
2005-05-26 15:17:57 +02:00
|
|
|
pick_proc(); /* pick new process to run */
|
|
|
|
break;
|
|
|
|
}
|
2005-06-20 13:26:48 +02:00
|
|
|
prev_xp = *xpp; /* save previous in chain */
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
2005-07-26 14:48:34 +02:00
|
|
|
|
2005-08-19 18:43:28 +02:00
|
|
|
#if DEAD_CODE
|
2005-07-26 14:48:34 +02:00
|
|
|
/* The caller blocked. Reset the scheduling priority and quantums allowed.
|
|
|
|
* The process' priority may have been lowered if a process consumed too
|
|
|
|
* many full quantums in a row to prevent damage from infinite loops
|
|
|
|
*/
|
|
|
|
rp->p_priority = rp->p_max_priority;
|
|
|
|
rp->p_full_quantums = QUANTUMS(rp->p_priority);
|
2005-08-19 18:43:28 +02:00
|
|
|
#endif
|
2005-07-14 17:12:12 +02:00
|
|
|
|
|
|
|
#if DEBUG_SCHED_CHECK
|
|
|
|
rp->p_ready = 0;
|
2005-08-19 18:43:28 +02:00
|
|
|
check_runqueues("dequeue");
|
2005-07-14 17:12:12 +02:00
|
|
|
#endif
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
|
2005-08-19 18:43:28 +02:00
|
|
|
|
2005-04-21 16:53:53 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* sched *
|
|
|
|
*===========================================================================*/
|
2005-08-19 18:43:28 +02:00
|
|
|
PRIVATE void sched(sched_ptr, queue, front)
|
|
|
|
struct proc *sched_ptr; /* process to be scheduled */
|
|
|
|
int *queue; /* return: queue to use */
|
|
|
|
int *front; /* return: front or back */
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
|
2005-08-19 18:43:28 +02:00
|
|
|
}
|
2005-05-26 15:17:57 +02:00
|
|
|
|
2005-08-19 18:43:28 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* balance_queues *
|
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE void balance_queues(pp)
|
|
|
|
struct proc *pp; /* process that caused this */
|
|
|
|
{
|
|
|
|
/* To balance the scheduling queues, they will be rebuild whenever a process
|
|
|
|
* is put in the lowest queues where IDLE resides. All processes get their
|
|
|
|
* priority raised up to their maximum priority.
|
|
|
|
*/
|
|
|
|
register struct proc *rp;
|
|
|
|
register int q;
|
|
|
|
int penalty = pp->p_priority - pp->p_max_priority;
|
|
|
|
|
|
|
|
/* First clean up the old scheduling queues. */
|
|
|
|
for (q=0; q<NR_SCHED_QUEUES; q++) {
|
|
|
|
rdy_head[q] = rdy_tail[q] = NIL_PROC;
|
2005-06-30 17:55:19 +02:00
|
|
|
}
|
|
|
|
|
2005-08-19 18:43:28 +02:00
|
|
|
/* Then rebuild the queues, while balancing priorities. Each process that is
|
|
|
|
* in use may get a higher priority and gets a new quantum. Processes that
|
|
|
|
* are runnable are added to the scheduling queues, unless it concerns the
|
|
|
|
* process that caused this function to be called (it will be added after
|
|
|
|
* returning from this function).
|
2005-06-30 17:55:19 +02:00
|
|
|
*/
|
2005-08-19 18:43:28 +02:00
|
|
|
for (rp=BEG_PROC_ADDR; rp<END_PROC_ADDR; rp++) {
|
|
|
|
if (! (rp->p_rts_flags & SLOT_FREE)) { /* update in-use slots */
|
|
|
|
rp->p_priority = MAX(rp->p_priority - penalty, rp->p_max_priority);
|
|
|
|
rp->p_sched_ticks = rp->p_quantum_size;
|
|
|
|
if (rp->p_rts_flags == 0) { /* process is runnable */
|
|
|
|
if (rp != pp) enqueue(rp); /* add it to a queue */
|
|
|
|
}
|
|
|
|
}
|
2005-06-30 17:55:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* pick_proc *
|
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE void pick_proc()
|
|
|
|
{
|
|
|
|
/* Decide who to run now. A new process is selected by setting 'next_ptr'.
|
|
|
|
* When a billable process is selected, record it in 'bill_ptr', so that the
|
|
|
|
* clock task can tell who to bill for system time.
|
|
|
|
*/
|
|
|
|
register struct proc *rp; /* process to run */
|
|
|
|
int q; /* iterate over queues */
|
|
|
|
|
|
|
|
/* Check each of the scheduling queues for ready processes. The number of
|
|
|
|
* queues is defined in proc.h, and priorities are set in the task table.
|
|
|
|
* The lowest queue contains IDLE, which is always ready.
|
|
|
|
*/
|
|
|
|
for (q=0; q < NR_SCHED_QUEUES; q++) {
|
|
|
|
if ( (rp = rdy_head[q]) != NIL_PROC) {
|
|
|
|
next_ptr = rp; /* run process 'rp' next */
|
2005-07-14 17:12:12 +02:00
|
|
|
if (priv(rp)->s_flags & BILLABLE)
|
2005-06-30 17:55:19 +02:00
|
|
|
bill_ptr = rp; /* bill for system time */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
|
2005-05-18 12:36:23 +02:00
|
|
|
|
|
|
|
/*==========================================================================*
|
|
|
|
* lock_send *
|
|
|
|
*==========================================================================*/
|
2005-05-24 16:35:58 +02:00
|
|
|
PUBLIC int lock_send(dst, m_ptr)
|
2005-05-24 12:06:17 +02:00
|
|
|
int dst; /* to whom is message being sent? */
|
|
|
|
message *m_ptr; /* pointer to message buffer */
|
2005-05-18 12:36:23 +02:00
|
|
|
{
|
|
|
|
/* Safe gateway to mini_send() for tasks. */
|
|
|
|
int result;
|
2005-06-01 11:37:52 +02:00
|
|
|
lock(2, "send");
|
2005-05-27 14:44:14 +02:00
|
|
|
result = mini_send(proc_ptr, dst, m_ptr, NON_BLOCKING);
|
2005-06-01 11:37:52 +02:00
|
|
|
unlock(2);
|
2005-05-18 12:36:23 +02:00
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-21 16:53:53 +02:00
|
|
|
/*==========================================================================*
|
2005-08-19 18:43:28 +02:00
|
|
|
* lock_enqueue *
|
2005-04-21 16:53:53 +02:00
|
|
|
*==========================================================================*/
|
2005-08-19 18:43:28 +02:00
|
|
|
PUBLIC void lock_enqueue(rp)
|
2005-04-21 16:53:53 +02:00
|
|
|
struct proc *rp; /* this process is now runnable */
|
|
|
|
{
|
2005-08-19 18:43:28 +02:00
|
|
|
/* Safe gateway to enqueue() for tasks. */
|
|
|
|
lock(3, "enqueue");
|
|
|
|
enqueue(rp);
|
2005-06-01 11:37:52 +02:00
|
|
|
unlock(3);
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*==========================================================================*
|
2005-08-19 18:43:28 +02:00
|
|
|
* lock_dequeue *
|
2005-04-21 16:53:53 +02:00
|
|
|
*==========================================================================*/
|
2005-08-19 18:43:28 +02:00
|
|
|
PUBLIC void lock_dequeue(rp)
|
2005-04-21 16:53:53 +02:00
|
|
|
struct proc *rp; /* this process is no longer runnable */
|
|
|
|
{
|
2005-08-19 18:43:28 +02:00
|
|
|
/* Safe gateway to dequeue() for tasks. */
|
|
|
|
lock(4, "dequeue");
|
|
|
|
dequeue(rp);
|
2005-06-01 11:37:52 +02:00
|
|
|
unlock(4);
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|