xref: /minix3/minix/servers/pm/schedule.c (revision 637f688f0d4ccff0fa8a6ddafcd1d89fdfbc462d)
1*433d6423SLionel Sambuc #include "pm.h"
2*433d6423SLionel Sambuc #include <assert.h>
3*433d6423SLionel Sambuc #include <minix/callnr.h>
4*433d6423SLionel Sambuc #include <minix/com.h>
5*433d6423SLionel Sambuc #include <minix/config.h>
6*433d6423SLionel Sambuc #include <minix/sched.h>
7*433d6423SLionel Sambuc #include <minix/sysinfo.h>
8*433d6423SLionel Sambuc #include <minix/type.h>
9*433d6423SLionel Sambuc #include <machine/archtypes.h>
10*433d6423SLionel Sambuc #include <lib.h>
11*433d6423SLionel Sambuc #include "mproc.h"
12*433d6423SLionel Sambuc 
13*433d6423SLionel Sambuc #include <machine/archtypes.h>
14*433d6423SLionel Sambuc #include <minix/timers.h>
15*433d6423SLionel Sambuc #include "kernel/proc.h"
16*433d6423SLionel Sambuc 
17*433d6423SLionel Sambuc /*===========================================================================*
18*433d6423SLionel Sambuc  *				init_scheduling				     *
19*433d6423SLionel Sambuc  *===========================================================================*/
sched_init(void)20*433d6423SLionel Sambuc void sched_init(void)
21*433d6423SLionel Sambuc {
22*433d6423SLionel Sambuc 	struct mproc *trmp;
23*433d6423SLionel Sambuc 	endpoint_t parent_e;
24*433d6423SLionel Sambuc 	int proc_nr, s;
25*433d6423SLionel Sambuc 
26*433d6423SLionel Sambuc 	for (proc_nr=0, trmp=mproc; proc_nr < NR_PROCS; proc_nr++, trmp++) {
27*433d6423SLionel Sambuc 		/* Don't take over system processes. When the system starts,
28*433d6423SLionel Sambuc 		 * init is blocked on RTS_NO_QUANTUM until PM assigns a
29*433d6423SLionel Sambuc 		 * scheduler, from which other. Given that all other user
30*433d6423SLionel Sambuc 		 * processes are forked from init and system processes are
31*433d6423SLionel Sambuc 		 * managed by RS, there should be no other process that needs
32*433d6423SLionel Sambuc 		 * to be assigned a scheduler here */
33*433d6423SLionel Sambuc 		if (trmp->mp_flags & IN_USE && !(trmp->mp_flags & PRIV_PROC)) {
34*433d6423SLionel Sambuc 			assert(_ENDPOINT_P(trmp->mp_endpoint) == INIT_PROC_NR);
35*433d6423SLionel Sambuc 			parent_e = mproc[trmp->mp_parent].mp_endpoint;
36*433d6423SLionel Sambuc 			assert(parent_e == trmp->mp_endpoint);
37*433d6423SLionel Sambuc 			s = sched_start(SCHED_PROC_NR,	/* scheduler_e */
38*433d6423SLionel Sambuc 				trmp->mp_endpoint,	/* schedulee_e */
39*433d6423SLionel Sambuc 				parent_e,		/* parent_e */
40*433d6423SLionel Sambuc 				USER_Q, 		/* maxprio */
41*433d6423SLionel Sambuc 				USER_QUANTUM, 		/* quantum */
42*433d6423SLionel Sambuc 				-1,			/* don't change cpu */
43*433d6423SLionel Sambuc 				&trmp->mp_scheduler);	/* *newsched_e */
44*433d6423SLionel Sambuc 			if (s != OK) {
45*433d6423SLionel Sambuc 				printf("PM: SCHED denied taking over scheduling of %s: %d\n",
46*433d6423SLionel Sambuc 					trmp->mp_name, s);
47*433d6423SLionel Sambuc 			}
48*433d6423SLionel Sambuc 		}
49*433d6423SLionel Sambuc  	}
50*433d6423SLionel Sambuc }
51*433d6423SLionel Sambuc 
52*433d6423SLionel Sambuc /*===========================================================================*
53*433d6423SLionel Sambuc  *				sched_start_user			     *
54*433d6423SLionel Sambuc  *===========================================================================*/
sched_start_user(endpoint_t ep,struct mproc * rmp)55*433d6423SLionel Sambuc int sched_start_user(endpoint_t ep, struct mproc *rmp)
56*433d6423SLionel Sambuc {
57*433d6423SLionel Sambuc 	unsigned maxprio;
58*433d6423SLionel Sambuc 	endpoint_t inherit_from;
59*433d6423SLionel Sambuc 	int rv;
60*433d6423SLionel Sambuc 
61*433d6423SLionel Sambuc 	/* convert nice to priority */
62*433d6423SLionel Sambuc 	if ((rv = nice_to_priority(rmp->mp_nice, &maxprio)) != OK) {
63*433d6423SLionel Sambuc 		return rv;
64*433d6423SLionel Sambuc 	}
65*433d6423SLionel Sambuc 
66*433d6423SLionel Sambuc 	/* scheduler must know the parent, which is not the case for a child
67*433d6423SLionel Sambuc 	 * of a system process created by a regular fork; in this case the
68*433d6423SLionel Sambuc 	 * scheduler should inherit settings from init rather than the real
69*433d6423SLionel Sambuc 	 * parent
70*433d6423SLionel Sambuc 	 */
71*433d6423SLionel Sambuc 	if (mproc[rmp->mp_parent].mp_flags & PRIV_PROC) {
72*433d6423SLionel Sambuc 		assert(mproc[rmp->mp_parent].mp_scheduler == NONE);
73*433d6423SLionel Sambuc 		inherit_from = INIT_PROC_NR;
74*433d6423SLionel Sambuc 	} else {
75*433d6423SLionel Sambuc 		inherit_from = mproc[rmp->mp_parent].mp_endpoint;
76*433d6423SLionel Sambuc 	}
77*433d6423SLionel Sambuc 
78*433d6423SLionel Sambuc 	/* inherit quantum */
79*433d6423SLionel Sambuc 	return sched_inherit(ep, 			/* scheduler_e */
80*433d6423SLionel Sambuc 		rmp->mp_endpoint, 			/* schedulee_e */
81*433d6423SLionel Sambuc 		inherit_from, 				/* parent_e */
82*433d6423SLionel Sambuc 		maxprio, 				/* maxprio */
83*433d6423SLionel Sambuc 		&rmp->mp_scheduler);			/* *newsched_e */
84*433d6423SLionel Sambuc }
85*433d6423SLionel Sambuc 
86*433d6423SLionel Sambuc /*===========================================================================*
87*433d6423SLionel Sambuc  *				sched_nice				     *
88*433d6423SLionel Sambuc  *===========================================================================*/
sched_nice(struct mproc * rmp,int nice)89*433d6423SLionel Sambuc int sched_nice(struct mproc *rmp, int nice)
90*433d6423SLionel Sambuc {
91*433d6423SLionel Sambuc 	int rv;
92*433d6423SLionel Sambuc 	message m;
93*433d6423SLionel Sambuc 	unsigned maxprio;
94*433d6423SLionel Sambuc 
95*433d6423SLionel Sambuc 	/* If the kernel is the scheduler, we don't allow messing with the
96*433d6423SLionel Sambuc 	 * priority. If you want to control process priority, assign the process
97*433d6423SLionel Sambuc 	 * to a user-space scheduler */
98*433d6423SLionel Sambuc 	if (rmp->mp_scheduler == KERNEL || rmp->mp_scheduler == NONE)
99*433d6423SLionel Sambuc 		return (EINVAL);
100*433d6423SLionel Sambuc 
101*433d6423SLionel Sambuc 	if ((rv = nice_to_priority(nice, &maxprio)) != OK) {
102*433d6423SLionel Sambuc 		return rv;
103*433d6423SLionel Sambuc 	}
104*433d6423SLionel Sambuc 
105*433d6423SLionel Sambuc 	m.m_pm_sched_scheduling_set_nice.endpoint	= rmp->mp_endpoint;
106*433d6423SLionel Sambuc 	m.m_pm_sched_scheduling_set_nice.maxprio	= maxprio;
107*433d6423SLionel Sambuc 	if ((rv = _taskcall(rmp->mp_scheduler, SCHEDULING_SET_NICE, &m))) {
108*433d6423SLionel Sambuc 		return rv;
109*433d6423SLionel Sambuc 	}
110*433d6423SLionel Sambuc 
111*433d6423SLionel Sambuc 	return (OK);
112*433d6423SLionel Sambuc }
113