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>
46 #include <minix/portio.h>
48 /* Scheduling and message passing functions. The functions are available to
49 * other parts of the kernel through lock_...(). The lock temporarily disables
50 * interrupts to prevent race conditions.
52 FORWARD
_PROTOTYPE( int mini_send
, (struct proc
*caller_ptr
, int dst_e
,
53 message
*m_ptr
, unsigned flags
));
54 FORWARD
_PROTOTYPE( int mini_receive
, (struct proc
*caller_ptr
, int src
,
55 message
*m_ptr
, unsigned flags
));
56 FORWARD
_PROTOTYPE( int mini_notify
, (struct proc
*caller_ptr
, int dst
));
57 FORWARD
_PROTOTYPE( int deadlock
, (int function
,
58 register struct proc
*caller
, int src_dst
));
59 FORWARD
_PROTOTYPE( void enqueue
, (struct proc
*rp
));
60 FORWARD
_PROTOTYPE( void dequeue
, (struct proc
*rp
));
61 FORWARD
_PROTOTYPE( void sched
, (struct proc
*rp
, int *queue
, int *front
));
62 FORWARD
_PROTOTYPE( void pick_proc
, (void));
64 #define BuildMess(m_ptr, src, dst_ptr) \
65 (m_ptr)->m_source = proc_addr(src)->p_endpoint; \
66 (m_ptr)->m_type = NOTIFY_FROM(src); \
67 (m_ptr)->NOTIFY_TIMESTAMP = get_uptime(); \
70 (m_ptr)->NOTIFY_ARG = priv(dst_ptr)->s_int_pending; \
71 priv(dst_ptr)->s_int_pending = 0; \
74 (m_ptr)->NOTIFY_ARG = priv(dst_ptr)->s_sig_pending; \
75 priv(dst_ptr)->s_sig_pending = 0; \
79 #define CopyMess(s,sp,sm,dp,dm) \
80 cp_mess(proc_addr(s)->p_endpoint, \
81 (sp)->p_memmap[D].mem_phys, \
82 (vir_bytes)sm, (dp)->p_memmap[D].mem_phys, (vir_bytes)dm)
84 /*===========================================================================*
86 *===========================================================================*/
87 PUBLIC
int sys_call(call_nr
, src_dst_e
, m_ptr
, bit_map
)
88 int call_nr
; /* system call number and flags */
89 int src_dst_e
; /* src to receive from or dst to send to */
90 message
*m_ptr
; /* pointer to message in the caller's space */
91 long bit_map
; /* notification event set or flags */
93 /* System calls are done by trapping to the kernel with an INT instruction.
94 * The trap is caught and sys_call() is called to send or receive a message
95 * (or both). The caller is always given by 'proc_ptr'.
97 register struct proc
*caller_ptr
= proc_ptr
; /* get pointer to caller */
98 int function
= call_nr
& SYSCALL_FUNC
; /* get system call function */
99 unsigned flags
= call_nr
& SYSCALL_FLAGS
; /* get flags */
100 int mask_entry
; /* bit to check in send mask */
101 int group_size
; /* used for deadlock check */
102 int result
; /* the system call's result */
104 vir_clicks vlo
, vhi
; /* virtual clicks containing message to send */
107 if (RTS_ISSET(caller_ptr
, SLOT_FREE
))
109 kprintf("called by the dead?!?\n");
114 /* Require a valid source and/ or destination process, unless echoing. */
115 if (src_dst_e
!= ANY
&& function
!= ECHO
) {
116 if(!isokendpt(src_dst_e
, &src_dst
)) {
117 #if DEBUG_ENABLE_IPC_WARNINGS
118 kprintf("sys_call: trap %d by %d with bad endpoint %d\n",
119 function
, proc_nr(caller_ptr
), src_dst_e
);
123 } else src_dst
= src_dst_e
;
125 /* Check if the process has privileges for the requested call. Calls to the
126 * kernel may only be SENDREC, because tasks always reply and may not block
127 * if the caller doesn't do receive().
129 if (! (priv(caller_ptr
)->s_trap_mask
& (1 << function
)) ||
130 (iskerneln(src_dst
) && function
!= SENDREC
131 && function
!= RECEIVE
)) {
132 #if DEBUG_ENABLE_IPC_WARNINGS
133 kprintf("sys_call: trap %d not allowed, caller %d, src_dst %d\n",
134 function
, proc_nr(caller_ptr
), src_dst
);
136 return(ETRAPDENIED
); /* trap denied by mask or kernel */
139 /* If the call involves a message buffer, i.e., for SEND, RECEIVE, SENDREC,
140 * or ECHO, check the message pointer. This check allows a message to be
141 * anywhere in data or stack or gap. It will have to be made more elaborate
142 * for machines which don't have the gap mapped.
144 if (function
& CHECK_PTR
) {
145 vlo
= (vir_bytes
) m_ptr
>> CLICK_SHIFT
;
146 vhi
= ((vir_bytes
) m_ptr
+ MESS_SIZE
- 1) >> CLICK_SHIFT
;
147 if (vlo
< caller_ptr
->p_memmap
[D
].mem_vir
|| vlo
> vhi
||
148 vhi
>= caller_ptr
->p_memmap
[S
].mem_vir
+
149 caller_ptr
->p_memmap
[S
].mem_len
) {
150 #if DEBUG_ENABLE_IPC_WARNINGS
151 kprintf("sys_call: invalid message pointer, trap %d, caller %d\n",
152 function
, proc_nr(caller_ptr
));
154 return(EFAULT
); /* invalid message pointer */
158 /* If the call is to send to a process, i.e., for SEND, SENDREC or NOTIFY,
159 * verify that the caller is allowed to send to the given destination.
161 if (function
& CHECK_DST
) {
162 if (! get_sys_bit(priv(caller_ptr
)->s_ipc_to
, nr_to_id(src_dst
))) {
163 #if DEBUG_ENABLE_IPC_WARNINGS
164 kprintf("sys_call: ipc mask denied trap %d from %d to %d\n",
165 function
, proc_nr(caller_ptr
), src_dst
);
167 return(ECALLDENIED
); /* call denied by ipc mask */
171 /* Check for a possible deadlock for blocking SEND(REC) and RECEIVE. */
172 if (function
& CHECK_DEADLOCK
) {
173 if (group_size
= deadlock(function
, caller_ptr
, src_dst
)) {
174 #if DEBUG_ENABLE_IPC_WARNINGS
175 kprintf("sys_call: trap %d from %d to %d deadlocked, group size %d\n",
176 function
, proc_nr(caller_ptr
), src_dst
, group_size
);
182 /* Now check if the call is known and try to perform the request. The only
183 * system calls that exist in MINIX are sending and receiving messages.
184 * - SENDREC: combines SEND and RECEIVE in a single system call
185 * - SEND: sender blocks until its message has been delivered
186 * - RECEIVE: receiver blocks until an acceptable message has arrived
187 * - NOTIFY: nonblocking call; deliver notification or mark pending
188 * - ECHO: nonblocking call; directly echo back the message
192 /* A flag is set so that notifications cannot interrupt SENDREC. */
193 caller_ptr
->p_misc_flags
|= REPLY_PENDING
;
196 result
= mini_send(caller_ptr
, src_dst_e
, m_ptr
, flags
);
197 if (function
== SEND
|| result
!= OK
) {
198 break; /* done, or SEND failed */
199 } /* fall through for SENDREC */
201 if (function
== RECEIVE
)
202 caller_ptr
->p_misc_flags
&= ~REPLY_PENDING
;
203 result
= mini_receive(caller_ptr
, src_dst_e
, m_ptr
, flags
);
206 result
= mini_notify(caller_ptr
, src_dst
);
209 CopyMess(caller_ptr
->p_nr
, caller_ptr
, m_ptr
, caller_ptr
, m_ptr
);
213 result
= EBADCALL
; /* illegal system call */
216 /* Now, return the result of the system call to the caller. */
220 /*===========================================================================*
222 *===========================================================================*/
223 PRIVATE
int deadlock(function
, cp
, src_dst
)
224 int function
; /* trap number */
225 register struct proc
*cp
; /* pointer to caller */
226 int src_dst
; /* src or dst process */
228 /* Check for deadlock. This can happen if 'caller_ptr' and 'src_dst' have
229 * a cyclic dependency of blocking send and receive calls. The only cyclic
230 * depency that is not fatal is if the caller and target directly SEND(REC)
231 * and RECEIVE to each other. If a deadlock is found, the group size is
232 * returned. Otherwise zero is returned.
234 register struct proc
*xp
; /* process pointer */
235 int group_size
= 1; /* start with only caller */
238 while (src_dst
!= ANY
) { /* check while process nr */
240 xp
= proc_addr(src_dst
); /* follow chain of processes */
241 group_size
++; /* extra process in group */
243 /* Check whether the last process in the chain has a dependency. If it
244 * has not, the cycle cannot be closed and we are done.
246 if (RTS_ISSET(xp
, RECEIVING
)) { /* xp has dependency */
247 if(xp
->p_getfrom_e
== ANY
) src_dst
= ANY
;
248 else okendpt(xp
->p_getfrom_e
, &src_dst
);
249 } else if (RTS_ISSET(xp
, SENDING
)) { /* xp has dependency */
250 okendpt(xp
->p_sendto_e
, &src_dst
);
252 return(0); /* not a deadlock */
255 /* Now check if there is a cyclic dependency. For group sizes of two,
256 * a combination of SEND(REC) and RECEIVE is not fatal. Larger groups
257 * or other combinations indicate a deadlock.
259 if (src_dst
== proc_nr(cp
)) { /* possible deadlock */
260 if (group_size
== 2) { /* caller and src_dst */
261 /* The function number is magically converted to flags. */
262 if ((xp
->p_rts_flags
^ (function
<< 2)) & SENDING
) {
263 return(0); /* not a deadlock */
266 return(group_size
); /* deadlock found */
269 return(0); /* not a deadlock */
272 /*===========================================================================*
274 *===========================================================================*/
275 PRIVATE
int mini_send(caller_ptr
, dst_e
, m_ptr
, flags
)
276 register struct proc
*caller_ptr
; /* who is trying to send a message? */
277 int dst_e
; /* to whom is message being sent? */
278 message
*m_ptr
; /* pointer to message buffer */
279 unsigned flags
; /* system call flags */
281 /* Send a message from 'caller_ptr' to 'dst'. If 'dst' is blocked waiting
282 * for this message, copy the message to it and unblock 'dst'. If 'dst' is
283 * not waiting at all, or is waiting for another source, queue 'caller_ptr'.
285 register struct proc
*dst_ptr
;
286 register struct proc
**xpp
;
289 dst_p
= _ENDPOINT_P(dst_e
);
290 dst_ptr
= proc_addr(dst_p
);
292 if (RTS_ISSET(dst_ptr
, NO_ENDPOINT
)) return EDSTDIED
;
294 /* Check if 'dst' is blocked waiting for this message. The destination's
295 * SENDING flag may be set when its SENDREC call blocked while sending.
297 if ( (RTS_ISSET(dst_ptr
, RECEIVING
) && !RTS_ISSET(dst_ptr
, SENDING
)) &&
298 (dst_ptr
->p_getfrom_e
== ANY
299 || dst_ptr
->p_getfrom_e
== caller_ptr
->p_endpoint
)) {
300 /* Destination is indeed waiting for this message. */
301 CopyMess(caller_ptr
->p_nr
, caller_ptr
, m_ptr
, dst_ptr
,
303 RTS_UNSET(dst_ptr
, RECEIVING
);
304 } else if ( ! (flags
& NON_BLOCKING
)) {
305 /* Destination is not waiting. Block and dequeue caller. */
306 caller_ptr
->p_messbuf
= m_ptr
;
307 RTS_SET(caller_ptr
, SENDING
);
308 caller_ptr
->p_sendto_e
= dst_e
;
310 /* Process is now blocked. Put in on the destination's queue. */
311 xpp
= &dst_ptr
->p_caller_q
; /* find end of list */
312 while (*xpp
!= NIL_PROC
) xpp
= &(*xpp
)->p_q_link
;
313 *xpp
= caller_ptr
; /* add caller to end */
314 caller_ptr
->p_q_link
= NIL_PROC
; /* mark new end of list */
321 /*===========================================================================*
323 *===========================================================================*/
324 PRIVATE
int mini_receive(caller_ptr
, src_e
, m_ptr
, flags
)
325 register struct proc
*caller_ptr
; /* process trying to get message */
326 int src_e
; /* which message source is wanted */
327 message
*m_ptr
; /* pointer to message buffer */
328 unsigned flags
; /* system call flags */
330 /* A process or task wants to get a message. If a message is already queued,
331 * acquire it and deblock the sender. If no message from the desired source
332 * is available block the caller, unless the flags don't allow blocking.
334 register struct proc
**xpp
;
335 register struct notification
**ntf_q_pp
;
340 int i
, src_id
, src_proc_nr
, src_p
;
342 if(src_e
== ANY
) src_p
= ANY
;
345 okendpt(src_e
, &src_p
);
346 if (RTS_ISSET(proc_addr(src_p
), NO_ENDPOINT
)) return ESRCDIED
;
350 /* Check to see if a message from desired source is already available.
351 * The caller's SENDING flag may be set if SENDREC couldn't send. If it is
352 * set, the process should be blocked.
354 if (!RTS_ISSET(caller_ptr
, SENDING
)) {
356 /* Check if there are pending notifications, except for SENDREC. */
357 if (! (caller_ptr
->p_misc_flags
& REPLY_PENDING
)) {
359 map
= &priv(caller_ptr
)->s_notify_pending
;
360 for (chunk
=&map
->chunk
[0]; chunk
<&map
->chunk
[NR_SYS_CHUNKS
]; chunk
++) {
362 /* Find a pending notification from the requested source. */
363 if (! *chunk
) continue; /* no bits in chunk */
364 for (i
=0; ! (*chunk
& (1<<i
)); ++i
) {} /* look up the bit */
365 src_id
= (chunk
- &map
->chunk
[0]) * BITCHUNK_BITS
+ i
;
366 if (src_id
>= NR_SYS_PROCS
) break; /* out of range */
367 src_proc_nr
= id_to_nr(src_id
); /* get source proc */
368 #if DEBUG_ENABLE_IPC_WARNINGS
369 if(src_proc_nr
== NONE
) {
370 kprintf("mini_receive: sending notify from NONE\n");
373 if (src_e
!=ANY
&& src_p
!= src_proc_nr
) continue;/* source not ok */
374 *chunk
&= ~(1 << i
); /* no longer pending */
376 /* Found a suitable source, deliver the notification message. */
377 BuildMess(&m
, src_proc_nr
, caller_ptr
); /* assemble message */
378 CopyMess(src_proc_nr
, proc_addr(HARDWARE
), &m
, caller_ptr
, m_ptr
);
379 return(OK
); /* report success */
383 /* Check caller queue. Use pointer pointers to keep code simple. */
384 xpp
= &caller_ptr
->p_caller_q
;
385 while (*xpp
!= NIL_PROC
) {
386 if (src_e
== ANY
|| src_p
== proc_nr(*xpp
)) {
388 if (RTS_ISSET(*xpp
, SLOT_FREE
))
390 kprintf("listening to the dead?!?\n");
395 /* Found acceptable message. Copy it and update status. */
396 CopyMess((*xpp
)->p_nr
, *xpp
, (*xpp
)->p_messbuf
, caller_ptr
, m_ptr
);
397 RTS_UNSET(*xpp
, SENDING
);
398 *xpp
= (*xpp
)->p_q_link
; /* remove from queue */
399 return(OK
); /* report success */
401 xpp
= &(*xpp
)->p_q_link
; /* proceed to next */
405 /* No suitable message is available or the caller couldn't send in SENDREC.
406 * Block the process trying to receive, unless the flags tell otherwise.
408 if ( ! (flags
& NON_BLOCKING
)) {
409 caller_ptr
->p_getfrom_e
= src_e
;
410 caller_ptr
->p_messbuf
= m_ptr
;
411 RTS_SET(caller_ptr
, RECEIVING
);
418 /*===========================================================================*
420 *===========================================================================*/
421 PRIVATE
int mini_notify(caller_ptr
, dst
)
422 register struct proc
*caller_ptr
; /* sender of the notification */
423 int dst
; /* which process to notify */
425 register struct proc
*dst_ptr
= proc_addr(dst
);
426 int src_id
; /* source id for late delivery */
427 message m
; /* the notification message */
429 /* Check to see if target is blocked waiting for this message. A process
430 * can be both sending and receiving during a SENDREC system call.
432 if ( (RTS_ISSET(dst_ptr
, RECEIVING
) && !RTS_ISSET(dst_ptr
, SENDING
)) &&
433 ! (dst_ptr
->p_misc_flags
& REPLY_PENDING
) &&
434 (dst_ptr
->p_getfrom_e
== ANY
||
435 dst_ptr
->p_getfrom_e
== caller_ptr
->p_endpoint
)) {
437 /* Destination is indeed waiting for a message. Assemble a notification
438 * message and deliver it. Copy from pseudo-source HARDWARE, since the
439 * message is in the kernel's address space.
441 BuildMess(&m
, proc_nr(caller_ptr
), dst_ptr
);
442 CopyMess(proc_nr(caller_ptr
), proc_addr(HARDWARE
), &m
,
443 dst_ptr
, dst_ptr
->p_messbuf
);
444 RTS_UNSET(dst_ptr
, RECEIVING
);
448 /* Destination is not ready to receive the notification. Add it to the
449 * bit map with pending notifications. Note the indirectness: the system id
450 * instead of the process number is used in the pending bit map.
452 src_id
= priv(caller_ptr
)->s_id
;
453 set_sys_bit(priv(dst_ptr
)->s_notify_pending
, src_id
);
457 /*===========================================================================*
459 *===========================================================================*/
460 PUBLIC
int lock_notify(src_e
, dst_e
)
461 int src_e
; /* (endpoint) sender of the notification */
462 int dst_e
; /* (endpoint) who is to be notified */
464 /* Safe gateway to mini_notify() for tasks and interrupt handlers. The sender
465 * is explicitely given to prevent confusion where the call comes from. MINIX
466 * kernel is not reentrant, which means to interrupts are disabled after
467 * the first kernel entry (hardware interrupt, trap, or exception). Locking
468 * is done by temporarily disabling interrupts.
470 int result
, src
, dst
;
472 if(!isokendpt(src_e
, &src
) || !isokendpt(dst_e
, &dst
))
475 /* Exception or interrupt occurred, thus already locked. */
476 if (k_reenter
>= 0) {
477 result
= mini_notify(proc_addr(src
), dst
);
480 /* Call from task level, locking is required. */
483 result
= mini_notify(proc_addr(src
), dst
);
489 /*===========================================================================*
491 *===========================================================================*/
492 PRIVATE
void enqueue(rp
)
493 register struct proc
*rp
; /* this process is now runnable */
495 /* Add 'rp' to one of the queues of runnable processes. This function is
496 * responsible for inserting a process into one of the scheduling queues.
497 * The mechanism is implemented here. The actual scheduling policy is
498 * defined in sched() and pick_proc().
500 int q
; /* scheduling queue to use */
501 int front
; /* add to front or back */
503 #if DEBUG_SCHED_CHECK
504 check_runqueues("enqueue1");
505 if (rp
->p_ready
) kprintf("enqueue() already ready process\n");
508 /* Determine where to insert to process. */
509 sched(rp
, &q
, &front
);
511 /* Now add the process to the queue. */
512 if (rdy_head
[q
] == NIL_PROC
) { /* add to empty queue */
513 rdy_head
[q
] = rdy_tail
[q
] = rp
; /* create a new queue */
514 rp
->p_nextready
= NIL_PROC
; /* mark new end */
516 else if (front
) { /* add to head of queue */
517 rp
->p_nextready
= rdy_head
[q
]; /* chain head of queue */
518 rdy_head
[q
] = rp
; /* set new queue head */
520 else { /* add to tail of queue */
521 rdy_tail
[q
]->p_nextready
= rp
; /* chain tail of queue */
522 rdy_tail
[q
] = rp
; /* set new queue tail */
523 rp
->p_nextready
= NIL_PROC
; /* mark new end */
526 /* Now select the next process to run. */
529 #if DEBUG_SCHED_CHECK
531 check_runqueues("enqueue2");
535 /*===========================================================================*
537 *===========================================================================*/
538 PRIVATE
void dequeue(rp
)
539 register struct proc
*rp
; /* this process is no longer runnable */
541 /* A process must be removed from the scheduling queues, for example, because
542 * it has blocked. If the currently active process is removed, a new process
543 * is picked to run by calling pick_proc().
545 register int q
= rp
->p_priority
; /* queue to use */
546 register struct proc
**xpp
; /* iterate over queue */
547 register struct proc
*prev_xp
;
549 /* Side-effect for kernel: check if the task's stack still is ok? */
551 if (*priv(rp
)->s_stack_guard
!= STACK_GUARD
)
552 panic("stack overrun by task", proc_nr(rp
));
555 #if DEBUG_SCHED_CHECK
556 check_runqueues("dequeue1");
557 if (! rp
->p_ready
) kprintf("%s:%d: dequeue() already unready process\n",
561 /* Now make sure that the process is not in its ready queue. Remove the
562 * process if it is found. A process can be made unready even if it is not
563 * running by being sent a signal that kills it.
566 for (xpp
= &rdy_head
[q
]; *xpp
!= NIL_PROC
; xpp
= &(*xpp
)->p_nextready
) {
568 if (*xpp
== rp
) { /* found process to remove */
569 *xpp
= (*xpp
)->p_nextready
; /* replace with next chain */
570 if (rp
== rdy_tail
[q
]) /* queue tail removed */
571 rdy_tail
[q
] = prev_xp
; /* set new tail */
572 if (rp
== proc_ptr
|| rp
== next_ptr
) /* active process removed */
573 pick_proc(); /* pick new process to run */
576 prev_xp
= *xpp
; /* save previous in chain */
579 #if DEBUG_SCHED_CHECK
581 check_runqueues("dequeue2");
585 /*===========================================================================*
587 *===========================================================================*/
588 PRIVATE
void sched(rp
, queue
, front
)
589 register struct proc
*rp
; /* process to be scheduled */
590 int *queue
; /* return: queue to use */
591 int *front
; /* return: front or back */
593 /* This function determines the scheduling policy. It is called whenever a
594 * process must be added to one of the scheduling queues to decide where to
595 * insert it. As a side-effect the process' priority may be updated.
597 int time_left
= (rp
->p_ticks_left
> 0); /* quantum fully consumed */
599 /* Check whether the process has time left. Otherwise give a new quantum
600 * and lower the process' priority, unless the process already is in the
603 if (! time_left
) { /* quantum consumed ? */
604 rp
->p_ticks_left
= rp
->p_quantum_size
; /* give new quantum */
605 if (rp
->p_priority
< (IDLE_Q
-1)) {
606 rp
->p_priority
+= 1; /* lower priority */
610 /* If there is time left, the process is added to the front of its queue,
611 * so that it can immediately run. The queue to use simply is always the
612 * process' current priority.
614 *queue
= rp
->p_priority
;
618 /*===========================================================================*
620 *===========================================================================*/
621 PRIVATE
void pick_proc()
623 /* Decide who to run now. A new process is selected by setting 'next_ptr'.
624 * When a billable process is selected, record it in 'bill_ptr', so that the
625 * clock task can tell who to bill for system time.
627 register struct proc
*rp
; /* process to run */
628 int q
; /* iterate over queues */
630 /* Check each of the scheduling queues for ready processes. The number of
631 * queues is defined in proc.h, and priorities are set in the task table.
632 * The lowest queue contains IDLE, which is always ready.
634 for (q
=0; q
< NR_SCHED_QUEUES
; q
++) {
635 if ( (rp
= rdy_head
[q
]) != NIL_PROC
) {
636 next_ptr
= rp
; /* run process 'rp' next */
637 if (priv(rp
)->s_flags
& BILLABLE
)
638 bill_ptr
= rp
; /* bill for system time */
644 /*===========================================================================*
646 *===========================================================================*/
647 #define Q_BALANCE_TICKS 100
648 PUBLIC
void balance_queues(tp
)
649 timer_t
*tp
; /* watchdog timer pointer */
651 /* Check entire process table and give all process a higher priority. This
652 * effectively means giving a new quantum. If a process already is at its
653 * maximum priority, its quantum will be renewed.
655 static timer_t queue_timer
; /* timer structure to use */
656 register struct proc
* rp
; /* process table pointer */
657 clock_t next_period
; /* time of next period */
658 int ticks_added
= 0; /* total time added */
660 for (rp
=BEG_PROC_ADDR
; rp
<END_PROC_ADDR
; rp
++) {
661 if (! isemptyp(rp
)) { /* check slot use */
662 lock(5,"balance_queues");
663 if (rp
->p_priority
> rp
->p_max_priority
) { /* update priority? */
664 if (rp
->p_rts_flags
== 0) dequeue(rp
); /* take off queue */
665 ticks_added
+= rp
->p_quantum_size
; /* do accounting */
666 rp
->p_priority
-= 1; /* raise priority */
667 if (rp
->p_rts_flags
== 0) enqueue(rp
); /* put on queue */
670 ticks_added
+= rp
->p_quantum_size
- rp
->p_ticks_left
;
671 rp
->p_ticks_left
= rp
->p_quantum_size
; /* give new quantum */
677 kprintf("ticks_added: %d\n", ticks_added
);
680 /* Now schedule a new watchdog timer to balance the queues again. The
681 * period depends on the total amount of quantum ticks added.
683 next_period
= MAX(Q_BALANCE_TICKS
, ticks_added
); /* calculate next */
684 set_timer(&queue_timer
, get_uptime() + next_period
, balance_queues
);
687 /*===========================================================================*
689 *===========================================================================*/
690 PUBLIC
int lock_send(dst_e
, m_ptr
)
691 int dst_e
; /* to whom is message being sent? */
692 message
*m_ptr
; /* pointer to message buffer */
694 /* Safe gateway to mini_send() for tasks. */
697 result
= mini_send(proc_ptr
, dst_e
, m_ptr
, NON_BLOCKING
);
702 /*===========================================================================*
704 *===========================================================================*/
705 PUBLIC
void lock_enqueue(rp
)
706 struct proc
*rp
; /* this process is now runnable */
708 /* Safe gateway to enqueue() for tasks. */
714 /*===========================================================================*
716 *===========================================================================*/
717 PUBLIC
void lock_dequeue(rp
)
718 struct proc
*rp
; /* this process is no longer runnable */
720 /* Safe gateway to dequeue() for tasks. */
721 if (k_reenter
>= 0) {
722 /* We're in an exception or interrupt, so don't lock (and ...
733 /*===========================================================================*
735 *===========================================================================*/
736 #if DEBUG_ENABLE_IPC_WARNINGS
737 PUBLIC
int isokendpt_f(file
, line
, e
, p
, fatalflag
)
741 PUBLIC
int isokendpt_f(e
, p
, fatalflag
)
747 /* Convert an endpoint number into a process number.
748 * Return nonzero if the process is alive with the corresponding
749 * generation number, zero otherwise.
751 * This function is called with file and line number by the
752 * isokendpt_d macro if DEBUG_ENABLE_IPC_WARNINGS is defined,
753 * otherwise without. This allows us to print the where the
754 * conversion was attempted, making the errors verbose without
755 * adding code for that at every call.
757 * If fatalflag is nonzero, we must panic if the conversion doesn't
762 #if DEBUG_ENABLE_IPC_WARNINGS
763 kprintf("kernel:%s:%d: bad endpoint %d: proc %d out of range\n",
766 } else if(isemptyn(*p
)) {
767 #if DEBUG_ENABLE_IPC_WARNINGS
768 kprintf("kernel:%s:%d: bad endpoint %d: proc %d empty\n", file
, line
, e
, *p
);
770 } else if(proc_addr(*p
)->p_endpoint
!= e
) {
771 #if DEBUG_ENABLE_IPC_WARNINGS
772 kprintf("kernel:%s:%d: bad endpoint %d: proc %d has ept %d (generation %d vs. %d)\n", file
, line
,
773 e
, *p
, proc_addr(*p
)->p_endpoint
,
774 _ENDPOINT_G(e
), _ENDPOINT_G(proc_addr(*p
)->p_endpoint
));
777 if(!ok
&& fatalflag
) {
778 panic("invalid endpoint ", e
);