panic() cleanup.
[minix.git] / kernel / proc.c
blobe159945e5250bef4fa46d62f3d4fc499e6363ff7
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 * Changes:
8 * Aug 19, 2005 rewrote scheduling code (Jorrit N. Herder)
9 * Jul 25, 2005 rewrote system call handling (Jorrit N. Herder)
10 * May 26, 2005 rewrote message passing functions (Jorrit N. Herder)
11 * May 24, 2005 new notification system call (Jorrit N. Herder)
12 * Oct 28, 2004 nonblocking send and receive calls (Jorrit N. Herder)
14 * The code here is critical to make everything work and is important for the
15 * overall performance of the system. A large fraction of the code deals with
16 * list manipulation. To make this both easy to understand and fast to execute
17 * pointer pointers are used throughout the code. Pointer pointers prevent
18 * exceptions for the head or tail of a linked list.
20 * node_t *queue, *new_node; // assume these as global variables
21 * node_t **xpp = &queue; // get pointer pointer to head of queue
22 * while (*xpp != NULL) // find last pointer of the linked list
23 * xpp = &(*xpp)->next; // get pointer to next pointer
24 * *xpp = new_node; // now replace the end (the NULL pointer)
25 * new_node->next = NULL; // and mark the new end of the list
27 * For example, when adding a new node to the end of the list, one normally
28 * makes an exception for an empty list and looks up the end of the list for
29 * nonempty lists. As shown above, this is not required with pointer pointers.
32 #include <minix/com.h>
33 #include <minix/endpoint.h>
34 #include <stddef.h>
35 #include <signal.h>
36 #include <minix/syslib.h>
38 #include "debug.h"
39 #include "kernel.h"
40 #include "proc.h"
41 #include "vm.h"
43 /* Scheduling and message passing functions */
44 FORWARD _PROTOTYPE( void idle, (void));
45 FORWARD _PROTOTYPE( int mini_send, (struct proc *caller_ptr, int dst_e,
46 message *m_ptr, int flags));
47 FORWARD _PROTOTYPE( int mini_receive, (struct proc *caller_ptr, int src,
48 message *m_ptr, int flags));
49 FORWARD _PROTOTYPE( int mini_senda, (struct proc *caller_ptr,
50 asynmsg_t *table, size_t size));
51 FORWARD _PROTOTYPE( int deadlock, (int function,
52 register struct proc *caller, int src_dst));
53 FORWARD _PROTOTYPE( int try_async, (struct proc *caller_ptr));
54 FORWARD _PROTOTYPE( int try_one, (struct proc *src_ptr, struct proc *dst_ptr,
55 int *postponed));
56 FORWARD _PROTOTYPE( void sched, (struct proc *rp, int *queue, int *front));
57 FORWARD _PROTOTYPE( struct proc * pick_proc, (void));
58 FORWARD _PROTOTYPE( void enqueue_head, (struct proc *rp));
60 #define PICK_ANY 1
61 #define PICK_HIGHERONLY 2
63 #define BuildNotifyMessage(m_ptr, src, dst_ptr) \
64 (m_ptr)->m_type = NOTIFY_FROM(src); \
65 (m_ptr)->NOTIFY_TIMESTAMP = get_uptime(); \
66 switch (src) { \
67 case HARDWARE: \
68 (m_ptr)->NOTIFY_ARG = priv(dst_ptr)->s_int_pending; \
69 priv(dst_ptr)->s_int_pending = 0; \
70 break; \
71 case SYSTEM: \
72 (m_ptr)->NOTIFY_ARG = priv(dst_ptr)->s_sig_pending; \
73 priv(dst_ptr)->s_sig_pending = 0; \
74 break; \
77 /*===========================================================================*
78 * QueueMess *
79 *===========================================================================*/
80 PRIVATE int QueueMess(endpoint_t ep, vir_bytes msg_lin, struct proc *dst)
82 int k;
83 phys_bytes addr;
84 NOREC_ENTER(queuemess);
85 /* Queue a message from the src process (in memory) to the dst
86 * process (using dst process table entry). Do actual copy to
87 * kernel here; it's an error if the copy fails into kernel.
89 vmassert(!(dst->p_misc_flags & MF_DELIVERMSG));
90 vmassert(dst->p_delivermsg_lin);
91 vmassert(isokendpt(ep, &k));
93 #if 0
94 if(INMEMORY(dst)) {
95 PHYS_COPY_CATCH(msg_lin, dst->p_delivermsg_lin,
96 sizeof(message), addr);
97 if(!addr) {
98 PHYS_COPY_CATCH(vir2phys(&ep), dst->p_delivermsg_lin,
99 sizeof(ep), addr);
100 if(!addr) {
101 NOREC_RETURN(queuemess, OK);
105 #endif
107 PHYS_COPY_CATCH(msg_lin, vir2phys(&dst->p_delivermsg), sizeof(message), addr);
108 if(addr) {
109 NOREC_RETURN(queuemess, EFAULT);
112 dst->p_delivermsg.m_source = ep;
113 dst->p_misc_flags |= MF_DELIVERMSG;
115 NOREC_RETURN(queuemess, OK);
118 /*===========================================================================*
119 * idle *
120 *===========================================================================*/
121 PRIVATE void idle(void)
123 /* This function is called whenever there is no work to do.
124 * Halt the CPU, and measure how many timestamp counter ticks are
125 * spent not doing anything. This allows test setups to measure
126 * the CPU utiliziation of certain workloads with high precision.
129 /* start accounting for the idle time */
130 cycles_accounting_stop(proc_addr(KERNEL));
131 halt_cpu();
133 * end of accounting for the idle task does not happen here, the kernel
134 * is handling stuff for quite a while before it gets back here!
138 /*===========================================================================*
139 * schedcheck *
140 *===========================================================================*/
141 PUBLIC struct proc * schedcheck(void)
143 /* This function is called an instant before proc_ptr is
144 * to be scheduled again.
146 NOREC_ENTER(schedch);
149 * if the current process is still runnable check the misc flags and let
150 * it run unless it becomes not runnable in the meantime
152 if (proc_is_runnable(proc_ptr))
153 goto check_misc_flags;
156 * if a process becomes not runnable while handling the misc flags, we
157 * need to pick a new one here and start from scratch. Also if the
158 * current process wasn' runnable, we pick a new one here
160 not_runnable_pick_new:
161 if (proc_is_preempted(proc_ptr)) {
162 proc_ptr->p_rts_flags &= ~RTS_PREEMPTED;
163 if (proc_is_runnable(proc_ptr))
164 enqueue_head(proc_ptr);
166 /* this enqueues the process again */
167 if (proc_no_quantum(proc_ptr))
168 RTS_UNSET(proc_ptr, RTS_NO_QUANTUM);
171 * if we have no process to run, set IDLE as the current process for
172 * time accounting and put the cpu in and idle state. After the next
173 * timer interrupt the execution resumes here and we can pick another
174 * process. If there is still nothing runnable we "schedule" IDLE again
176 while (!(proc_ptr = pick_proc())) {
177 proc_ptr = proc_addr(IDLE);
178 if (priv(proc_ptr)->s_flags & BILLABLE)
179 bill_ptr = proc_ptr;
180 idle();
183 switch_address_space(proc_ptr);
185 check_misc_flags:
187 vmassert(proc_ptr);
188 vmassert(proc_is_runnable(proc_ptr));
189 while (proc_ptr->p_misc_flags &
190 (MF_KCALL_RESUME | MF_DELIVERMSG |
191 MF_SC_DEFER | MF_SC_TRACE | MF_SC_ACTIVE)) {
193 vmassert(proc_is_runnable(proc_ptr));
194 if (proc_ptr->p_misc_flags & MF_KCALL_RESUME) {
195 kernel_call_resume(proc_ptr);
197 else if (proc_ptr->p_misc_flags & MF_DELIVERMSG) {
198 TRACE(VF_SCHEDULING, printf("delivering to %s / %d\n",
199 proc_ptr->p_name, proc_ptr->p_endpoint););
200 if(delivermsg(proc_ptr) == VMSUSPEND) {
201 TRACE(VF_SCHEDULING,
202 printf("suspending %s / %d\n",
203 proc_ptr->p_name,
204 proc_ptr->p_endpoint););
205 vmassert(!proc_is_runnable(proc_ptr));
208 else if (proc_ptr->p_misc_flags & MF_SC_DEFER) {
209 /* Perform the system call that we deferred earlier. */
211 #if DEBUG_SCHED_CHECK
212 if (proc_ptr->p_misc_flags & MF_SC_ACTIVE)
213 panic("MF_SC_ACTIVE and MF_SC_DEFER set");
214 #endif
216 arch_do_syscall(proc_ptr);
218 /* If the process is stopped for signal delivery, and
219 * not blocked sending a message after the system call,
220 * inform PM.
222 if ((proc_ptr->p_misc_flags & MF_SIG_DELAY) &&
223 !RTS_ISSET(proc_ptr, RTS_SENDING))
224 sig_delay_done(proc_ptr);
226 else if (proc_ptr->p_misc_flags & MF_SC_TRACE) {
227 /* Trigger a system call leave event if this was a
228 * system call. We must do this after processing the
229 * other flags above, both for tracing correctness and
230 * to be able to use 'break'.
232 if (!(proc_ptr->p_misc_flags & MF_SC_ACTIVE))
233 break;
235 proc_ptr->p_misc_flags &=
236 ~(MF_SC_TRACE | MF_SC_ACTIVE);
238 /* Signal the "leave system call" event.
239 * Block the process.
241 cause_sig(proc_nr(proc_ptr), SIGTRAP);
243 else if (proc_ptr->p_misc_flags & MF_SC_ACTIVE) {
244 /* If MF_SC_ACTIVE was set, remove it now:
245 * we're leaving the system call.
247 proc_ptr->p_misc_flags &= ~MF_SC_ACTIVE;
249 break;
253 * the selected process might not be runnable anymore. We have
254 * to checkit and schedule another one
256 if (!proc_is_runnable(proc_ptr))
257 goto not_runnable_pick_new;
259 TRACE(VF_SCHEDULING, printf("starting %s / %d\n",
260 proc_ptr->p_name, proc_ptr->p_endpoint););
261 #if DEBUG_TRACE
262 proc_ptr->p_schedules++;
263 #endif
265 proc_ptr = arch_finish_schedcheck();
266 cycles_accounting_stop(proc_addr(KERNEL));
268 NOREC_RETURN(schedch, proc_ptr);
271 /*===========================================================================*
272 * sys_call *
273 *===========================================================================*/
274 PUBLIC int do_ipc(call_nr, src_dst_e, m_ptr, bit_map)
275 int call_nr; /* system call number and flags */
276 int src_dst_e; /* src to receive from or dst to send to */
277 message *m_ptr; /* pointer to message in the caller's space */
278 long bit_map; /* notification event set or flags */
280 /* System calls are done by trapping to the kernel with an INT instruction.
281 * The trap is caught and sys_call() is called to send or receive a message
282 * (or both). The caller is always given by 'proc_ptr'.
284 register struct proc *caller_ptr = proc_ptr; /* get pointer to caller */
285 int result; /* the system call's result */
286 int src_dst_p; /* Process slot number */
287 size_t msg_size;
289 /* If this process is subject to system call tracing, handle that first. */
290 if (caller_ptr->p_misc_flags & (MF_SC_TRACE | MF_SC_DEFER)) {
291 /* Are we tracing this process, and is it the first sys_call entry? */
292 if ((caller_ptr->p_misc_flags & (MF_SC_TRACE | MF_SC_DEFER)) ==
293 MF_SC_TRACE) {
294 /* We must notify the tracer before processing the actual
295 * system call. If we don't, the tracer could not obtain the
296 * input message. Postpone the entire system call.
298 caller_ptr->p_misc_flags &= ~MF_SC_TRACE;
299 caller_ptr->p_misc_flags |= MF_SC_DEFER;
301 /* Signal the "enter system call" event. Block the process. */
302 cause_sig(proc_nr(caller_ptr), SIGTRAP);
304 /* Preserve the return register's value. */
305 return caller_ptr->p_reg.retreg;
308 /* If the MF_SC_DEFER flag is set, the syscall is now being resumed. */
309 caller_ptr->p_misc_flags &= ~MF_SC_DEFER;
311 #if DEBUG_SCHED_CHECK
312 if (caller_ptr->p_misc_flags & MF_SC_ACTIVE)
313 panic("MF_SC_ACTIVE already set");
314 #endif
316 /* Set a flag to allow reliable tracing of leaving the system call. */
317 caller_ptr->p_misc_flags |= MF_SC_ACTIVE;
320 #if DEBUG_SCHED_CHECK
321 if(caller_ptr->p_misc_flags & MF_DELIVERMSG) {
322 printf("sys_call: MF_DELIVERMSG on for %s / %d\n",
323 caller_ptr->p_name, caller_ptr->p_endpoint);
324 panic("MF_DELIVERMSG on");
326 #endif
328 #if 0
329 if(src_dst_e != 4 && src_dst_e != 5 &&
330 caller_ptr->p_endpoint != 4 && caller_ptr->p_endpoint != 5) {
331 if(call_nr == SEND)
332 printf("(%d SEND to %d) ", caller_ptr->p_endpoint, src_dst_e);
333 else if(call_nr == RECEIVE)
334 printf("(%d RECEIVE from %d) ", caller_ptr->p_endpoint, src_dst_e);
335 else if(call_nr == SENDREC)
336 printf("(%d SENDREC to %d) ", caller_ptr->p_endpoint, src_dst_e);
337 else
338 printf("(%d %d to/from %d) ", caller_ptr->p_endpoint, call_nr, src_dst_e);
340 #endif
342 #if DEBUG_SCHED_CHECK
343 if (RTS_ISSET(caller_ptr, RTS_SLOT_FREE))
345 printf("called by the dead?!?\n");
346 return EINVAL;
348 #endif
350 /* Check destination. SENDA is special because its argument is a table and
351 * not a single destination. RECEIVE is the only call that accepts ANY (in
352 * addition to a real endpoint). The other calls (SEND, SENDREC,
353 * and NOTIFY) require an endpoint to corresponds to a process. In addition,
354 * it is necessary to check whether a process is allowed to send to a given
355 * destination.
357 if (call_nr == SENDA)
359 /* No destination argument */
361 else if (src_dst_e == ANY)
363 if (call_nr != RECEIVE)
365 #if 0
366 printf("sys_call: trap %d by %d with bad endpoint %d\n",
367 call_nr, proc_nr(caller_ptr), src_dst_e);
368 #endif
369 return EINVAL;
371 src_dst_p = src_dst_e;
373 else
375 /* Require a valid source and/or destination process. */
376 if(!isokendpt(src_dst_e, &src_dst_p)) {
377 #if 0
378 printf("sys_call: trap %d by %d with bad endpoint %d\n",
379 call_nr, proc_nr(caller_ptr), src_dst_e);
380 #endif
381 return EDEADSRCDST;
384 /* If the call is to send to a process, i.e., for SEND, SENDNB,
385 * SENDREC or NOTIFY, verify that the caller is allowed to send to
386 * the given destination.
388 if (call_nr != RECEIVE)
390 if (!may_send_to(caller_ptr, src_dst_p)) {
391 #if DEBUG_ENABLE_IPC_WARNINGS
392 printf(
393 "sys_call: ipc mask denied trap %d from %d to %d\n",
394 call_nr, caller_ptr->p_endpoint, src_dst_e);
395 #endif
396 return(ECALLDENIED); /* call denied by ipc mask */
401 /* Only allow non-negative call_nr values less than 32 */
402 if (call_nr < 0 || call_nr >= 32)
404 #if DEBUG_ENABLE_IPC_WARNINGS
405 printf("sys_call: trap %d not allowed, caller %d, src_dst %d\n",
406 call_nr, proc_nr(caller_ptr), src_dst_p);
407 #endif
408 return(ETRAPDENIED); /* trap denied by mask or kernel */
411 /* Check if the process has privileges for the requested call. Calls to the
412 * kernel may only be SENDREC, because tasks always reply and may not block
413 * if the caller doesn't do receive().
415 if (!(priv(caller_ptr)->s_trap_mask & (1 << call_nr))) {
416 #if DEBUG_ENABLE_IPC_WARNINGS
417 printf("sys_call: trap %d not allowed, caller %d, src_dst %d\n",
418 call_nr, proc_nr(caller_ptr), src_dst_p);
419 #endif
420 return(ETRAPDENIED); /* trap denied by mask or kernel */
423 /* SENDA has no src_dst value here, so this check is in mini_senda() as well.
425 if (call_nr != SENDREC && call_nr != RECEIVE && call_nr != SENDA &&
426 iskerneln(src_dst_p)) {
427 #if DEBUG_ENABLE_IPC_WARNINGS
428 printf("sys_call: trap %d not allowed, caller %d, src_dst %d\n",
429 call_nr, proc_nr(caller_ptr), src_dst_e);
430 #endif
431 return(ETRAPDENIED); /* trap denied by mask or kernel */
434 /* Get and check the size of the argument in bytes.
435 * Normally this is just the size of a regular message, but in the
436 * case of SENDA the argument is a table.
438 if(call_nr == SENDA) {
439 msg_size = (size_t) src_dst_e;
441 /* Limit size to something reasonable. An arbitrary choice is 16
442 * times the number of process table entries.
444 if (msg_size > 16*(NR_TASKS + NR_PROCS))
445 return EDOM;
446 msg_size *= sizeof(asynmsg_t); /* convert to bytes */
447 } else {
448 msg_size = sizeof(*m_ptr);
451 /* Now check if the call is known and try to perform the request. The only
452 * system calls that exist in MINIX are sending and receiving messages.
453 * - SENDREC: combines SEND and RECEIVE in a single system call
454 * - SEND: sender blocks until its message has been delivered
455 * - RECEIVE: receiver blocks until an acceptable message has arrived
456 * - NOTIFY: asynchronous call; deliver notification or mark pending
457 * - SENDA: list of asynchronous send requests
459 switch(call_nr) {
460 case SENDREC:
461 /* A flag is set so that notifications cannot interrupt SENDREC. */
462 caller_ptr->p_misc_flags |= MF_REPLY_PEND;
463 /* fall through */
464 case SEND:
465 result = mini_send(caller_ptr, src_dst_e, m_ptr, 0);
466 if (call_nr == SEND || result != OK)
467 break; /* done, or SEND failed */
468 /* fall through for SENDREC */
469 case RECEIVE:
470 if (call_nr == RECEIVE)
471 caller_ptr->p_misc_flags &= ~MF_REPLY_PEND;
472 result = mini_receive(caller_ptr, src_dst_e, m_ptr, 0);
473 break;
474 case NOTIFY:
475 result = mini_notify(caller_ptr, src_dst_e);
476 break;
477 case SENDNB:
478 result = mini_send(caller_ptr, src_dst_e, m_ptr, NON_BLOCKING);
479 break;
480 case SENDA:
481 result = mini_senda(caller_ptr, (asynmsg_t *)m_ptr, (size_t)src_dst_e);
482 break;
483 default:
484 result = EBADCALL; /* illegal system call */
487 /* Now, return the result of the system call to the caller. */
488 return(result);
491 /*===========================================================================*
492 * deadlock *
493 *===========================================================================*/
494 PRIVATE int deadlock(function, cp, src_dst)
495 int function; /* trap number */
496 register struct proc *cp; /* pointer to caller */
497 proc_nr_t src_dst; /* src or dst process */
499 /* Check for deadlock. This can happen if 'caller_ptr' and 'src_dst' have
500 * a cyclic dependency of blocking send and receive calls. The only cyclic
501 * depency that is not fatal is if the caller and target directly SEND(REC)
502 * and RECEIVE to each other. If a deadlock is found, the group size is
503 * returned. Otherwise zero is returned.
505 register struct proc *xp; /* process pointer */
506 int group_size = 1; /* start with only caller */
507 #if DEBUG_ENABLE_IPC_WARNINGS
508 static struct proc *processes[NR_PROCS + NR_TASKS];
509 processes[0] = cp;
510 #endif
512 while (src_dst != ANY) { /* check while process nr */
513 endpoint_t dep;
514 xp = proc_addr(src_dst); /* follow chain of processes */
515 #if DEBUG_ENABLE_IPC_WARNINGS
516 processes[group_size] = xp;
517 #endif
518 group_size ++; /* extra process in group */
520 /* Check whether the last process in the chain has a dependency. If it
521 * has not, the cycle cannot be closed and we are done.
523 if((dep = P_BLOCKEDON(xp)) == NONE)
524 return 0;
526 if(dep == ANY)
527 src_dst = ANY;
528 else
529 okendpt(dep, &src_dst);
531 /* Now check if there is a cyclic dependency. For group sizes of two,
532 * a combination of SEND(REC) and RECEIVE is not fatal. Larger groups
533 * or other combinations indicate a deadlock.
535 if (src_dst == proc_nr(cp)) { /* possible deadlock */
536 if (group_size == 2) { /* caller and src_dst */
537 /* The function number is magically converted to flags. */
538 if ((xp->p_rts_flags ^ (function << 2)) & RTS_SENDING) {
539 return(0); /* not a deadlock */
542 #if DEBUG_ENABLE_IPC_WARNINGS
544 int i;
545 printf("deadlock between these processes:\n");
546 for(i = 0; i < group_size; i++) {
547 printf(" %10s ", processes[i]->p_name);
548 proc_stacktrace(processes[i]);
551 #endif
552 return(group_size); /* deadlock found */
555 return(0); /* not a deadlock */
558 /*===========================================================================*
559 * mini_send *
560 *===========================================================================*/
561 PRIVATE int mini_send(caller_ptr, dst_e, m_ptr, flags)
562 register struct proc *caller_ptr; /* who is trying to send a message? */
563 int dst_e; /* to whom is message being sent? */
564 message *m_ptr; /* pointer to message buffer */
565 int flags;
567 /* Send a message from 'caller_ptr' to 'dst'. If 'dst' is blocked waiting
568 * for this message, copy the message to it and unblock 'dst'. If 'dst' is
569 * not waiting at all, or is waiting for another source, queue 'caller_ptr'.
571 register struct proc *dst_ptr;
572 register struct proc **xpp;
573 int dst_p;
574 phys_bytes linaddr;
575 vir_bytes addr;
576 int r;
578 if(!(linaddr = umap_local(caller_ptr, D, (vir_bytes) m_ptr,
579 sizeof(message)))) {
580 return EFAULT;
582 dst_p = _ENDPOINT_P(dst_e);
583 dst_ptr = proc_addr(dst_p);
585 if (RTS_ISSET(dst_ptr, RTS_NO_ENDPOINT))
587 return EDEADSRCDST;
590 /* Check if 'dst' is blocked waiting for this message. The destination's
591 * RTS_SENDING flag may be set when its SENDREC call blocked while sending.
593 if (WILLRECEIVE(dst_ptr, caller_ptr->p_endpoint)) {
594 /* Destination is indeed waiting for this message. */
595 vmassert(!(dst_ptr->p_misc_flags & MF_DELIVERMSG));
596 if((r=QueueMess(caller_ptr->p_endpoint, linaddr, dst_ptr)) != OK)
597 return r;
598 RTS_UNSET(dst_ptr, RTS_RECEIVING);
599 } else {
600 if(flags & NON_BLOCKING) {
601 return(ENOTREADY);
604 /* Check for a possible deadlock before actually blocking. */
605 if (deadlock(SEND, caller_ptr, dst_p)) {
606 return(ELOCKED);
609 /* Destination is not waiting. Block and dequeue caller. */
610 PHYS_COPY_CATCH(linaddr, vir2phys(&caller_ptr->p_sendmsg),
611 sizeof(message), addr);
613 if(addr) { return EFAULT; }
614 RTS_SET(caller_ptr, RTS_SENDING);
615 caller_ptr->p_sendto_e = dst_e;
617 /* Process is now blocked. Put in on the destination's queue. */
618 xpp = &dst_ptr->p_caller_q; /* find end of list */
619 while (*xpp != NIL_PROC) xpp = &(*xpp)->p_q_link;
620 *xpp = caller_ptr; /* add caller to end */
621 caller_ptr->p_q_link = NIL_PROC; /* mark new end of list */
623 return(OK);
626 /*===========================================================================*
627 * mini_receive *
628 *===========================================================================*/
629 PRIVATE int mini_receive(caller_ptr, src_e, m_ptr, flags)
630 register struct proc *caller_ptr; /* process trying to get message */
631 int src_e; /* which message source is wanted */
632 message *m_ptr; /* pointer to message buffer */
633 int flags;
635 /* A process or task wants to get a message. If a message is already queued,
636 * acquire it and deblock the sender. If no message from the desired source
637 * is available block the caller.
639 register struct proc **xpp;
640 message m;
641 sys_map_t *map;
642 bitchunk_t *chunk;
643 int i, r, src_id, src_proc_nr, src_p;
644 phys_bytes linaddr;
646 vmassert(!(caller_ptr->p_misc_flags & MF_DELIVERMSG));
648 if(!(linaddr = umap_local(caller_ptr, D, (vir_bytes) m_ptr,
649 sizeof(message)))) {
650 return EFAULT;
653 /* This is where we want our message. */
654 caller_ptr->p_delivermsg_lin = linaddr;
655 caller_ptr->p_delivermsg_vir = (vir_bytes) m_ptr;
657 if(src_e == ANY) src_p = ANY;
658 else
660 okendpt(src_e, &src_p);
661 if (RTS_ISSET(proc_addr(src_p), RTS_NO_ENDPOINT))
663 return EDEADSRCDST;
668 /* Check to see if a message from desired source is already available. The
669 * caller's RTS_SENDING flag may be set if SENDREC couldn't send. If it is
670 * set, the process should be blocked.
672 if (!RTS_ISSET(caller_ptr, RTS_SENDING)) {
674 /* Check if there are pending notifications, except for SENDREC. */
675 if (! (caller_ptr->p_misc_flags & MF_REPLY_PEND)) {
677 map = &priv(caller_ptr)->s_notify_pending;
678 for (chunk=&map->chunk[0]; chunk<&map->chunk[NR_SYS_CHUNKS]; chunk++) {
679 endpoint_t hisep;
681 /* Find a pending notification from the requested source. */
682 if (! *chunk) continue; /* no bits in chunk */
683 for (i=0; ! (*chunk & (1<<i)); ++i) {} /* look up the bit */
684 src_id = (chunk - &map->chunk[0]) * BITCHUNK_BITS + i;
685 if (src_id >= NR_SYS_PROCS) break; /* out of range */
686 src_proc_nr = id_to_nr(src_id); /* get source proc */
687 #if DEBUG_ENABLE_IPC_WARNINGS
688 if(src_proc_nr == NONE) {
689 printf("mini_receive: sending notify from NONE\n");
691 #endif
692 if (src_e!=ANY && src_p != src_proc_nr) continue;/* source not ok */
693 *chunk &= ~(1 << i); /* no longer pending */
695 /* Found a suitable source, deliver the notification message. */
696 BuildNotifyMessage(&m, src_proc_nr, caller_ptr); /* assemble message */
697 hisep = proc_addr(src_proc_nr)->p_endpoint;
698 vmassert(!(caller_ptr->p_misc_flags & MF_DELIVERMSG));
699 vmassert(src_e == ANY || hisep == src_e);
700 if((r=QueueMess(hisep, vir2phys(&m), caller_ptr)) != OK) {
701 panic("mini_receive: local QueueMess failed");
703 return(OK); /* report success */
707 /* Check caller queue. Use pointer pointers to keep code simple. */
708 xpp = &caller_ptr->p_caller_q;
709 while (*xpp != NIL_PROC) {
710 if (src_e == ANY || src_p == proc_nr(*xpp)) {
711 #if DEBUG_SCHED_CHECK
712 if (RTS_ISSET(*xpp, RTS_SLOT_FREE) || RTS_ISSET(*xpp, RTS_NO_ENDPOINT))
714 printf("%d: receive from %d; found dead %d (%s)?\n",
715 caller_ptr->p_endpoint, src_e, (*xpp)->p_endpoint,
716 (*xpp)->p_name);
717 return EINVAL;
719 #endif
721 /* Found acceptable message. Copy it and update status. */
722 vmassert(!(caller_ptr->p_misc_flags & MF_DELIVERMSG));
723 QueueMess((*xpp)->p_endpoint,
724 vir2phys(&(*xpp)->p_sendmsg), caller_ptr);
725 if ((*xpp)->p_misc_flags & MF_SIG_DELAY)
726 sig_delay_done(*xpp);
727 RTS_UNSET(*xpp, RTS_SENDING);
728 *xpp = (*xpp)->p_q_link; /* remove from queue */
729 return(OK); /* report success */
731 xpp = &(*xpp)->p_q_link; /* proceed to next */
734 if (caller_ptr->p_misc_flags & MF_ASYNMSG)
736 if (src_e != ANY)
737 r= try_one(proc_addr(src_p), caller_ptr, NULL);
738 else
739 r= try_async(caller_ptr);
741 if (r == OK)
742 return OK; /* Got a message */
746 /* No suitable message is available or the caller couldn't send in SENDREC.
747 * Block the process trying to receive, unless the flags tell otherwise.
749 if ( ! (flags & NON_BLOCKING)) {
750 /* Check for a possible deadlock before actually blocking. */
751 if (deadlock(RECEIVE, caller_ptr, src_p)) {
752 return(ELOCKED);
755 caller_ptr->p_getfrom_e = src_e;
756 RTS_SET(caller_ptr, RTS_RECEIVING);
757 return(OK);
758 } else {
759 return(ENOTREADY);
763 /*===========================================================================*
764 * mini_notify *
765 *===========================================================================*/
766 PUBLIC int mini_notify(caller_ptr, dst_e)
767 register struct proc *caller_ptr; /* sender of the notification */
768 endpoint_t dst_e; /* which process to notify */
770 register struct proc *dst_ptr;
771 int src_id; /* source id for late delivery */
772 message m; /* the notification message */
773 int r;
774 int dst_p;
776 if (!isokendpt(dst_e, &dst_p)) {
777 util_stacktrace();
778 printf("mini_notify: bogus endpoint %d\n", dst_e);
779 return EDEADSRCDST;
782 dst_ptr = proc_addr(dst_p);
784 /* Check to see if target is blocked waiting for this message. A process
785 * can be both sending and receiving during a SENDREC system call.
787 if (WILLRECEIVE(dst_ptr, caller_ptr->p_endpoint) &&
788 ! (dst_ptr->p_misc_flags & MF_REPLY_PEND)) {
789 /* Destination is indeed waiting for a message. Assemble a notification
790 * message and deliver it. Copy from pseudo-source HARDWARE, since the
791 * message is in the kernel's address space.
793 BuildNotifyMessage(&m, proc_nr(caller_ptr), dst_ptr);
794 vmassert(!(dst_ptr->p_misc_flags & MF_DELIVERMSG));
795 if((r=QueueMess(caller_ptr->p_endpoint, vir2phys(&m), dst_ptr)) != OK) {
796 panic("mini_notify: local QueueMess failed");
798 RTS_UNSET(dst_ptr, RTS_RECEIVING);
799 return(OK);
802 /* Destination is not ready to receive the notification. Add it to the
803 * bit map with pending notifications. Note the indirectness: the privilege id
804 * instead of the process number is used in the pending bit map.
806 src_id = priv(caller_ptr)->s_id;
807 set_sys_bit(priv(dst_ptr)->s_notify_pending, src_id);
808 return(OK);
811 #define ASCOMPLAIN(caller, entry, field) \
812 printf("kernel:%s:%d: asyn failed for %s in %s " \
813 "(%d/%d, tab 0x%lx)\n",__FILE__,__LINE__, \
814 field, caller->p_name, entry, priv(caller)->s_asynsize, priv(caller)->s_asyntab)
816 #define A_RETRIEVE(entry, field) \
817 if(data_copy(caller_ptr->p_endpoint, \
818 table_v + (entry)*sizeof(asynmsg_t) + offsetof(struct asynmsg,field),\
819 KERNEL, (vir_bytes) &tabent.field, \
820 sizeof(tabent.field)) != OK) {\
821 ASCOMPLAIN(caller_ptr, entry, #field); \
822 return EFAULT; \
825 #define A_INSERT(entry, field) \
826 if(data_copy(KERNEL, (vir_bytes) &tabent.field, \
827 caller_ptr->p_endpoint, \
828 table_v + (entry)*sizeof(asynmsg_t) + offsetof(struct asynmsg,field),\
829 sizeof(tabent.field)) != OK) {\
830 ASCOMPLAIN(caller_ptr, entry, #field); \
831 return EFAULT; \
834 /*===========================================================================*
835 * mini_senda *
836 *===========================================================================*/
837 PRIVATE int mini_senda(struct proc *caller_ptr, asynmsg_t *table, size_t size)
839 int i, dst_p, done, do_notify;
840 unsigned flags;
841 struct proc *dst_ptr;
842 struct priv *privp;
843 asynmsg_t tabent;
844 vir_bytes table_v = (vir_bytes) table;
845 vir_bytes linaddr;
847 privp= priv(caller_ptr);
848 if (!(privp->s_flags & SYS_PROC))
850 printf(
851 "mini_senda: warning caller has no privilege structure\n");
852 return EPERM;
855 /* Clear table */
856 privp->s_asyntab= -1;
857 privp->s_asynsize= 0;
859 if (size == 0)
861 /* Nothing to do, just return */
862 return OK;
865 if(!(linaddr = umap_local(caller_ptr, D, (vir_bytes) table,
866 size * sizeof(*table)))) {
867 printf("mini_senda: umap_local failed; 0x%lx len 0x%lx\n",
868 table, size * sizeof(*table));
869 return EFAULT;
872 /* Limit size to something reasonable. An arbitrary choice is 16
873 * times the number of process table entries.
875 * (this check has been duplicated in sys_call but is left here
876 * as a sanity check)
878 if (size > 16*(NR_TASKS + NR_PROCS))
880 return EDOM;
883 /* Scan the table */
884 do_notify= FALSE;
885 done= TRUE;
886 for (i= 0; i<size; i++)
889 /* Read status word */
890 A_RETRIEVE(i, flags);
891 flags= tabent.flags;
893 /* Skip empty entries */
894 if (flags == 0)
895 continue;
897 /* Check for reserved bits in the flags field */
898 if (flags & ~(AMF_VALID|AMF_DONE|AMF_NOTIFY|AMF_NOREPLY) ||
899 !(flags & AMF_VALID))
901 return EINVAL;
904 /* Skip entry if AMF_DONE is already set */
905 if (flags & AMF_DONE)
906 continue;
908 /* Get destination */
909 A_RETRIEVE(i, dst);
911 if (!isokendpt(tabent.dst, &dst_p))
913 /* Bad destination, report the error */
914 tabent.result= EDEADSRCDST;
915 A_INSERT(i, result);
916 tabent.flags= flags | AMF_DONE;
917 A_INSERT(i, flags);
919 if (flags & AMF_NOTIFY)
920 do_notify= 1;
921 continue;
924 if (iskerneln(dst_p))
926 /* Asynchronous sends to the kernel are not allowed */
927 tabent.result= ECALLDENIED;
928 A_INSERT(i, result);
929 tabent.flags= flags | AMF_DONE;
930 A_INSERT(i, flags);
932 if (flags & AMF_NOTIFY)
933 do_notify= 1;
934 continue;
937 if (!may_send_to(caller_ptr, dst_p))
939 /* Send denied by IPC mask */
940 tabent.result= ECALLDENIED;
941 A_INSERT(i, result);
942 tabent.flags= flags | AMF_DONE;
943 A_INSERT(i, flags);
945 if (flags & AMF_NOTIFY)
946 do_notify= 1;
947 continue;
950 #if 0
951 printf("mini_senda: entry[%d]: flags 0x%x dst %d/%d\n",
952 i, tabent.flags, tabent.dst, dst_p);
953 #endif
955 dst_ptr = proc_addr(dst_p);
957 /* RTS_NO_ENDPOINT should be removed */
958 if (dst_ptr->p_rts_flags & RTS_NO_ENDPOINT)
960 tabent.result= EDEADSRCDST;
961 A_INSERT(i, result);
962 tabent.flags= flags | AMF_DONE;
963 A_INSERT(i, flags);
965 if (flags & AMF_NOTIFY)
966 do_notify= TRUE;
967 continue;
970 /* Check if 'dst' is blocked waiting for this message.
971 * If AMF_NOREPLY is set, do not satisfy the receiving part of
972 * a SENDREC.
974 if (WILLRECEIVE(dst_ptr, caller_ptr->p_endpoint) &&
975 (!(flags & AMF_NOREPLY) ||
976 !(dst_ptr->p_misc_flags & MF_REPLY_PEND)))
978 /* Destination is indeed waiting for this message. */
979 /* Copy message from sender. */
980 tabent.result= QueueMess(caller_ptr->p_endpoint,
981 linaddr + (vir_bytes) &table[i].msg -
982 (vir_bytes) table, dst_ptr);
983 if(tabent.result == OK)
984 RTS_UNSET(dst_ptr, RTS_RECEIVING);
986 A_INSERT(i, result);
987 tabent.flags= flags | AMF_DONE;
988 A_INSERT(i, flags);
990 if (flags & AMF_NOTIFY)
991 do_notify= 1;
992 continue;
994 else
996 /* Should inform receiver that something is pending */
997 dst_ptr->p_misc_flags |= MF_ASYNMSG;
998 done= FALSE;
999 continue;
1002 if (do_notify)
1003 printf("mini_senda: should notify caller\n");
1004 if (!done)
1006 privp->s_asyntab= (vir_bytes)table;
1007 privp->s_asynsize= size;
1009 return OK;
1013 /*===========================================================================*
1014 * try_async *
1015 *===========================================================================*/
1016 PRIVATE int try_async(caller_ptr)
1017 struct proc *caller_ptr;
1019 int r;
1020 struct priv *privp;
1021 struct proc *src_ptr;
1022 int postponed = FALSE;
1024 /* Try all privilege structures */
1025 for (privp = BEG_PRIV_ADDR; privp < END_PRIV_ADDR; ++privp)
1027 if (privp->s_proc_nr == NONE)
1028 continue;
1030 src_ptr= proc_addr(privp->s_proc_nr);
1032 vmassert(!(caller_ptr->p_misc_flags & MF_DELIVERMSG));
1033 r= try_one(src_ptr, caller_ptr, &postponed);
1034 if (r == OK)
1035 return r;
1038 /* Nothing found, clear MF_ASYNMSG unless messages were postponed */
1039 if (postponed == FALSE)
1040 caller_ptr->p_misc_flags &= ~MF_ASYNMSG;
1042 return ESRCH;
1046 /*===========================================================================*
1047 * try_one *
1048 *===========================================================================*/
1049 PRIVATE int try_one(struct proc *src_ptr, struct proc *dst_ptr, int *postponed)
1051 int i, done;
1052 unsigned flags;
1053 size_t size;
1054 endpoint_t dst_e;
1055 struct priv *privp;
1056 asynmsg_t tabent;
1057 vir_bytes table_v;
1058 struct proc *caller_ptr;
1059 int r;
1061 privp= priv(src_ptr);
1063 /* Basic validity checks */
1064 if (privp->s_id == USER_PRIV_ID) return EAGAIN;
1065 if (privp->s_asynsize == 0) return EAGAIN;
1066 if (!may_send_to(src_ptr, proc_nr(dst_ptr))) return EAGAIN;
1068 size= privp->s_asynsize;
1069 table_v = privp->s_asyntab;
1070 caller_ptr = src_ptr;
1072 dst_e= dst_ptr->p_endpoint;
1074 /* Scan the table */
1075 done= TRUE;
1076 for (i= 0; i<size; i++)
1078 /* Read status word */
1079 A_RETRIEVE(i, flags);
1080 flags= tabent.flags;
1082 /* Skip empty entries */
1083 if (flags == 0)
1085 continue;
1088 /* Check for reserved bits in the flags field */
1089 if (flags & ~(AMF_VALID|AMF_DONE|AMF_NOTIFY|AMF_NOREPLY) ||
1090 !(flags & AMF_VALID))
1092 printf("try_one: bad bits in table\n");
1093 privp->s_asynsize= 0;
1094 return EINVAL;
1097 /* Skip entry is AMF_DONE is already set */
1098 if (flags & AMF_DONE)
1100 continue;
1103 /* Clear done. We are done when all entries are either empty
1104 * or done at the start of the call.
1106 done= FALSE;
1108 /* Get destination */
1109 A_RETRIEVE(i, dst);
1111 if (tabent.dst != dst_e)
1113 continue;
1116 /* If AMF_NOREPLY is set, do not satisfy the receiving part of
1117 * a SENDREC. Do not unset MF_ASYNMSG later because of this,
1118 * though: this message is still to be delivered later.
1120 if ((flags & AMF_NOREPLY) &&
1121 (dst_ptr->p_misc_flags & MF_REPLY_PEND))
1123 if (postponed != NULL)
1124 *postponed = TRUE;
1126 continue;
1129 /* Deliver message */
1130 A_RETRIEVE(i, msg);
1131 r = QueueMess(src_ptr->p_endpoint, vir2phys(&tabent.msg),
1132 dst_ptr);
1134 tabent.result= r;
1135 A_INSERT(i, result);
1136 tabent.flags= flags | AMF_DONE;
1137 A_INSERT(i, flags);
1139 if (flags & AMF_NOTIFY)
1141 printf("try_one: should notify caller\n");
1143 return OK;
1145 if (done)
1146 privp->s_asynsize= 0;
1147 return EAGAIN;
1150 /*===========================================================================*
1151 * enqueue *
1152 *===========================================================================*/
1153 PUBLIC void enqueue(rp)
1154 register struct proc *rp; /* this process is now runnable */
1156 /* Add 'rp' to one of the queues of runnable processes. This function is
1157 * responsible for inserting a process into one of the scheduling queues.
1158 * The mechanism is implemented here. The actual scheduling policy is
1159 * defined in sched() and pick_proc().
1161 int q; /* scheduling queue to use */
1162 int front; /* add to front or back */
1164 NOREC_ENTER(enqueuefunc);
1166 #if DEBUG_SCHED_CHECK
1167 if (rp->p_ready) panic("enqueue already ready process");
1168 #endif
1170 /* Determine where to insert to process. */
1171 sched(rp, &q, &front);
1173 vmassert(q >= 0);
1175 /* Now add the process to the queue. */
1176 if (rdy_head[q] == NIL_PROC) { /* add to empty queue */
1177 rdy_head[q] = rdy_tail[q] = rp; /* create a new queue */
1178 rp->p_nextready = NIL_PROC; /* mark new end */
1180 else if (front) { /* add to head of queue */
1181 rp->p_nextready = rdy_head[q]; /* chain head of queue */
1182 rdy_head[q] = rp; /* set new queue head */
1184 else { /* add to tail of queue */
1185 rdy_tail[q]->p_nextready = rp; /* chain tail of queue */
1186 rdy_tail[q] = rp; /* set new queue tail */
1187 rp->p_nextready = NIL_PROC; /* mark new end */
1190 #if DEBUG_SCHED_CHECK
1191 rp->p_ready = 1;
1192 CHECK_RUNQUEUES;
1193 #endif
1196 * enqueueing a process with a higher priority than the current one, it gets
1197 * preempted. The current process must be preemptible. Testing the priority
1198 * also makes sure that a process does not preempt itself
1200 vmassert(proc_ptr);
1201 if ((proc_ptr->p_priority > rp->p_priority) &&
1202 (priv(proc_ptr)->s_flags & PREEMPTIBLE))
1203 RTS_SET(proc_ptr, RTS_PREEMPTED); /* calls dequeue() */
1205 #if DEBUG_SCHED_CHECK
1206 CHECK_RUNQUEUES;
1207 #endif
1209 NOREC_RETURN(enqueuefunc, );
1212 /*===========================================================================*
1213 * enqueue_head *
1214 *===========================================================================*/
1216 * put a process at the front of its run queue. It comes handy when a process is
1217 * preempted and removed from run queue to not to have a currently not-runnable
1218 * process on a run queue. We have to put this process back at the fron to be
1219 * fair
1221 PRIVATE void enqueue_head(struct proc *rp)
1223 int q = rp->p_priority; /* scheduling queue to use */
1225 #if DEBUG_SCHED_CHECK
1226 if (rp->p_ready) panic("enqueue already ready process");
1227 #endif
1230 * the process was runnable without its quantum expired when dequeued. A
1231 * process with no time left should vahe been handled else and differently
1233 vmassert(rp->p_ticks_left);
1235 vmassert(q >= 0);
1238 /* Now add the process to the queue. */
1239 if (rdy_head[q] == NIL_PROC) { /* add to empty queue */
1240 rdy_head[q] = rdy_tail[q] = rp; /* create a new queue */
1241 rp->p_nextready = NIL_PROC; /* mark new end */
1243 else /* add to head of queue */
1244 rp->p_nextready = rdy_head[q]; /* chain head of queue */
1245 rdy_head[q] = rp; /* set new queue head */
1247 #if DEBUG_SCHED_CHECK
1248 rp->p_ready = 1;
1249 CHECK_RUNQUEUES;
1250 #endif
1253 /*===========================================================================*
1254 * dequeue *
1255 *===========================================================================*/
1256 PUBLIC void dequeue(rp)
1257 register struct proc *rp; /* this process is no longer runnable */
1259 /* A process must be removed from the scheduling queues, for example, because
1260 * it has blocked. If the currently active process is removed, a new process
1261 * is picked to run by calling pick_proc().
1263 register int q = rp->p_priority; /* queue to use */
1264 register struct proc **xpp; /* iterate over queue */
1265 register struct proc *prev_xp;
1267 NOREC_ENTER(dequeuefunc);
1269 #if DEBUG_STACK_CHECK
1270 /* Side-effect for kernel: check if the task's stack still is ok? */
1271 if (iskernelp(rp)) {
1272 if (*priv(rp)->s_stack_guard != STACK_GUARD)
1273 panic("stack overrun by task: %d", proc_nr(rp));
1275 #endif
1277 #if DEBUG_SCHED_CHECK
1278 if (! rp->p_ready) panic("dequeue() already unready process");
1279 #endif
1281 /* Now make sure that the process is not in its ready queue. Remove the
1282 * process if it is found. A process can be made unready even if it is not
1283 * running by being sent a signal that kills it.
1285 prev_xp = NIL_PROC;
1286 for (xpp = &rdy_head[q]; *xpp != NIL_PROC; xpp = &(*xpp)->p_nextready) {
1288 if (*xpp == rp) { /* found process to remove */
1289 *xpp = (*xpp)->p_nextready; /* replace with next chain */
1290 if (rp == rdy_tail[q]) /* queue tail removed */
1291 rdy_tail[q] = prev_xp; /* set new tail */
1293 #if DEBUG_SCHED_CHECK
1294 rp->p_ready = 0;
1295 CHECK_RUNQUEUES;
1296 #endif
1297 break;
1299 prev_xp = *xpp; /* save previous in chain */
1302 #if DEBUG_SCHED_CHECK
1303 CHECK_RUNQUEUES;
1304 #endif
1306 NOREC_RETURN(dequeuefunc, );
1309 /*===========================================================================*
1310 * sched *
1311 *===========================================================================*/
1312 PRIVATE void sched(rp, queue, front)
1313 register struct proc *rp; /* process to be scheduled */
1314 int *queue; /* return: queue to use */
1315 int *front; /* return: front or back */
1317 /* This function determines the scheduling policy. It is called whenever a
1318 * process must be added to one of the scheduling queues to decide where to
1319 * insert it. As a side-effect the process' priority may be updated.
1321 int time_left = (rp->p_ticks_left > 0); /* quantum fully consumed */
1323 /* Check whether the process has time left. Otherwise give a new quantum
1324 * and lower the process' priority, unless the process already is in the
1325 * lowest queue.
1327 if (! time_left) { /* quantum consumed ? */
1328 rp->p_ticks_left = rp->p_quantum_size; /* give new quantum */
1329 if (rp->p_priority < (NR_SCHED_QUEUES-1)) {
1330 rp->p_priority += 1; /* lower priority */
1334 /* If there is time left, the process is added to the front of its queue,
1335 * so that it can immediately run. The queue to use simply is always the
1336 * process' current priority.
1338 *queue = rp->p_priority;
1339 *front = time_left;
1342 /*===========================================================================*
1343 * pick_proc *
1344 *===========================================================================*/
1345 PRIVATE struct proc * pick_proc(void)
1347 /* Decide who to run now. A new process is selected an returned.
1348 * When a billable process is selected, record it in 'bill_ptr', so that the
1349 * clock task can tell who to bill for system time.
1351 register struct proc *rp; /* process to run */
1352 int q; /* iterate over queues */
1354 /* Check each of the scheduling queues for ready processes. The number of
1355 * queues is defined in proc.h, and priorities are set in the task table.
1356 * The lowest queue contains IDLE, which is always ready.
1358 for (q=0; q < NR_SCHED_QUEUES; q++) {
1359 if(!(rp = rdy_head[q])) {
1360 TRACE(VF_PICKPROC, printf("queue %d empty\n", q););
1361 continue;
1363 TRACE(VF_PICKPROC, printf("found %s / %d on queue %d\n",
1364 rp->p_name, rp->p_endpoint, q););
1365 vmassert(!proc_is_runnable(rp));
1366 if (priv(rp)->s_flags & BILLABLE)
1367 bill_ptr = rp; /* bill for system time */
1368 return rp;
1370 return NULL;
1373 /*===========================================================================*
1374 * balance_queues *
1375 *===========================================================================*/
1376 #define Q_BALANCE_TICKS 100
1377 PUBLIC void balance_queues(tp)
1378 timer_t *tp; /* watchdog timer pointer */
1380 /* Check entire process table and give all process a higher priority. This
1381 * effectively means giving a new quantum. If a process already is at its
1382 * maximum priority, its quantum will be renewed.
1384 static timer_t queue_timer; /* timer structure to use */
1385 register struct proc* rp; /* process table pointer */
1386 clock_t next_period; /* time of next period */
1387 int ticks_added = 0; /* total time added */
1389 for (rp=BEG_PROC_ADDR; rp<END_PROC_ADDR; rp++) {
1390 if (! isemptyp(rp)) { /* check slot use */
1391 if (rp->p_priority > rp->p_max_priority) { /* update priority? */
1392 if (proc_is_runnable(rp)) dequeue(rp); /* take off queue */
1393 ticks_added += rp->p_quantum_size; /* do accounting */
1394 rp->p_priority -= 1; /* raise priority */
1395 if (proc_is_runnable(rp)) enqueue(rp); /* put on queue */
1397 else {
1398 ticks_added += rp->p_quantum_size - rp->p_ticks_left;
1399 rp->p_ticks_left = rp->p_quantum_size; /* give new quantum */
1404 /* Now schedule a new watchdog timer to balance the queues again. The
1405 * period depends on the total amount of quantum ticks added.
1407 next_period = MAX(Q_BALANCE_TICKS, ticks_added); /* calculate next */
1408 set_timer(&queue_timer, get_uptime() + next_period, balance_queues);
1411 /*===========================================================================*
1412 * endpoint_lookup *
1413 *===========================================================================*/
1414 PUBLIC struct proc *endpoint_lookup(endpoint_t e)
1416 int n;
1418 if(!isokendpt(e, &n)) return NULL;
1420 return proc_addr(n);
1423 /*===========================================================================*
1424 * isokendpt_f *
1425 *===========================================================================*/
1426 #if DEBUG_ENABLE_IPC_WARNINGS
1427 PUBLIC int isokendpt_f(file, line, e, p, fatalflag)
1428 char *file;
1429 int line;
1430 #else
1431 PUBLIC int isokendpt_f(e, p, fatalflag)
1432 #endif
1433 endpoint_t e;
1434 int *p, fatalflag;
1436 int ok = 0;
1437 /* Convert an endpoint number into a process number.
1438 * Return nonzero if the process is alive with the corresponding
1439 * generation number, zero otherwise.
1441 * This function is called with file and line number by the
1442 * isokendpt_d macro if DEBUG_ENABLE_IPC_WARNINGS is defined,
1443 * otherwise without. This allows us to print the where the
1444 * conversion was attempted, making the errors verbose without
1445 * adding code for that at every call.
1447 * If fatalflag is nonzero, we must panic if the conversion doesn't
1448 * succeed.
1450 *p = _ENDPOINT_P(e);
1451 if(!isokprocn(*p)) {
1452 #if DEBUG_ENABLE_IPC_WARNINGS
1453 printf("kernel:%s:%d: bad endpoint %d: proc %d out of range\n",
1454 file, line, e, *p);
1455 #endif
1456 } else if(isemptyn(*p)) {
1457 #if 0
1458 printf("kernel:%s:%d: bad endpoint %d: proc %d empty\n", file, line, e, *p);
1459 #endif
1460 } else if(proc_addr(*p)->p_endpoint != e) {
1461 #if DEBUG_ENABLE_IPC_WARNINGS
1462 printf("kernel:%s:%d: bad endpoint %d: proc %d has ept %d (generation %d vs. %d)\n", file, line,
1463 e, *p, proc_addr(*p)->p_endpoint,
1464 _ENDPOINT_G(e), _ENDPOINT_G(proc_addr(*p)->p_endpoint));
1465 #endif
1466 } else ok = 1;
1467 if(!ok && fatalflag) {
1468 panic("invalid endpoint: %d", e);
1470 return ok;