xref: /minix/minix/servers/pm/main.c (revision 0a6a1f1d)
1 /* This file contains the main program of the process manager and some related
2  * procedures.  When MINIX starts up, the kernel runs for a little while,
3  * initializing itself and its tasks, and then it runs PM and VFS.  Both PM
4  * and VFS initialize themselves as far as they can. PM asks the kernel for
5  * all free memory and starts serving requests.
6  *
7  * The entry points into this file are:
8  *   main:	starts PM running
9  *   reply:	send a reply to a process making a PM system call
10  */
11 
12 #include "pm.h"
13 #include <minix/callnr.h>
14 #include <minix/com.h>
15 #include <minix/ds.h>
16 #include <minix/type.h>
17 #include <minix/endpoint.h>
18 #include <minix/minlib.h>
19 #include <minix/type.h>
20 #include <minix/vm.h>
21 #include <signal.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <sys/resource.h>
25 #include <sys/utsname.h>
26 #include <sys/wait.h>
27 #include <machine/archtypes.h>
28 #include <env.h>
29 #include <assert.h>
30 #include "mproc.h"
31 
32 #include "kernel/const.h"
33 #include "kernel/config.h"
34 #include "kernel/proc.h"
35 
36 #if ENABLE_SYSCALL_STATS
37 EXTERN unsigned long calls_stats[NR_PM_CALLS];
38 #endif
39 
40 static int get_nice_value(int queue);
41 static void handle_vfs_reply(void);
42 
43 /* SEF functions and variables. */
44 static void sef_local_startup(void);
45 static int sef_cb_init_fresh(int type, sef_init_info_t *info);
46 
47 /*===========================================================================*
48  *				main					     *
49  *===========================================================================*/
50 int main()
51 {
52 /* Main routine of the process manager. */
53   unsigned int call_index;
54   int ipc_status, result;
55 
56   /* SEF local startup. */
57   sef_local_startup();
58 
59   /* This is PM's main loop-  get work and do it, forever and forever. */
60   while (TRUE) {
61 	/* Wait for the next message. */
62 	if (sef_receive_status(ANY, &m_in, &ipc_status) != OK)
63 		panic("PM sef_receive_status error");
64 
65 	/* Check for system notifications first. Special cases. */
66 	if (is_ipc_notify(ipc_status)) {
67 		if (_ENDPOINT_P(m_in.m_source) == CLOCK)
68 			expire_timers(m_in.m_notify.timestamp);
69 
70 		/* done, continue */
71 		continue;
72 	}
73 
74 	/* Extract useful information from the message. */
75 	who_e = m_in.m_source;	/* who sent the message */
76 	if (pm_isokendpt(who_e, &who_p) != OK)
77 		panic("PM got message from invalid endpoint: %d", who_e);
78 	mp = &mproc[who_p];	/* process slot of caller */
79 	call_nr = m_in.m_type;	/* system call number */
80 
81 	/* Drop delayed calls from exiting processes. */
82 	if (mp->mp_flags & EXITING)
83 		continue;
84 
85 	if (IS_VFS_PM_RS(call_nr) && who_e == VFS_PROC_NR) {
86 		handle_vfs_reply();
87 
88 		result = SUSPEND;		/* don't reply */
89 	} else if (IS_PM_CALL(call_nr)) {
90 		/* If the system call number is valid, perform the call. */
91 		call_index = (unsigned int) (call_nr - PM_BASE);
92 
93 		if (call_index < NR_PM_CALLS && call_vec[call_index] != NULL) {
94 #if ENABLE_SYSCALL_STATS
95 			calls_stats[call_index]++;
96 #endif
97 
98 			result = (*call_vec[call_index])();
99 		} else
100 			result = ENOSYS;
101 	} else
102 		result = ENOSYS;
103 
104 	/* Send reply. */
105 	if (result != SUSPEND) reply(who_p, result);
106   }
107   return(OK);
108 }
109 
110 /*===========================================================================*
111  *			       sef_local_startup			     *
112  *===========================================================================*/
113 static void sef_local_startup()
114 {
115   /* Register init callbacks. */
116   sef_setcb_init_fresh(sef_cb_init_fresh);
117   sef_setcb_init_restart(SEF_CB_INIT_RESTART_STATEFUL);
118 
119   /* Register signal callbacks. */
120   sef_setcb_signal_manager(process_ksig);
121 
122   /* Let SEF perform startup. */
123   sef_startup();
124 }
125 
126 /*===========================================================================*
127  *		            sef_cb_init_fresh                                *
128  *===========================================================================*/
129 static int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *UNUSED(info))
130 {
131 /* Initialize the process manager. */
132   int s;
133   static struct boot_image image[NR_BOOT_PROCS];
134   register struct boot_image *ip;
135   static char core_sigs[] = { SIGQUIT, SIGILL, SIGTRAP, SIGABRT,
136 				SIGEMT, SIGFPE, SIGBUS, SIGSEGV };
137   static char ign_sigs[] = { SIGCHLD, SIGWINCH, SIGCONT, SIGINFO };
138   static char noign_sigs[] = { SIGILL, SIGTRAP, SIGEMT, SIGFPE,
139 				SIGBUS, SIGSEGV };
140   register struct mproc *rmp;
141   register char *sig_ptr;
142   message mess;
143 
144   /* Initialize process table, including timers. */
145   for (rmp=&mproc[0]; rmp<&mproc[NR_PROCS]; rmp++) {
146 	init_timer(&rmp->mp_timer);
147 	rmp->mp_magic = MP_MAGIC;
148   }
149 
150   /* Build the set of signals which cause core dumps, and the set of signals
151    * that are by default ignored.
152    */
153   sigemptyset(&core_sset);
154   for (sig_ptr = core_sigs; sig_ptr < core_sigs+sizeof(core_sigs); sig_ptr++)
155 	sigaddset(&core_sset, *sig_ptr);
156   sigemptyset(&ign_sset);
157   for (sig_ptr = ign_sigs; sig_ptr < ign_sigs+sizeof(ign_sigs); sig_ptr++)
158 	sigaddset(&ign_sset, *sig_ptr);
159   sigemptyset(&noign_sset);
160   for (sig_ptr = noign_sigs; sig_ptr < noign_sigs+sizeof(noign_sigs); sig_ptr++)
161 	sigaddset(&noign_sset, *sig_ptr);
162 
163   /* Obtain a copy of the boot monitor parameters.
164    */
165   if ((s=sys_getmonparams(monitor_params, sizeof(monitor_params))) != OK)
166       panic("get monitor params failed: %d", s);
167 
168   /* Initialize PM's process table. Request a copy of the system image table
169    * that is defined at the kernel level to see which slots to fill in.
170    */
171   if (OK != (s=sys_getimage(image)))
172   	panic("couldn't get image table: %d", s);
173   procs_in_use = 0;				/* start populating table */
174   for (ip = &image[0]; ip < &image[NR_BOOT_PROCS]; ip++) {
175   	if (ip->proc_nr >= 0) {			/* task have negative nrs */
176   		procs_in_use += 1;		/* found user process */
177 
178 		/* Set process details found in the image table. */
179 		rmp = &mproc[ip->proc_nr];
180   		strlcpy(rmp->mp_name, ip->proc_name, PROC_NAME_LEN);
181   		(void) sigemptyset(&rmp->mp_ignore);
182   		(void) sigemptyset(&rmp->mp_sigmask);
183   		(void) sigemptyset(&rmp->mp_catch);
184 		if (ip->proc_nr == INIT_PROC_NR) {	/* user process */
185   			/* INIT is root, we make it father of itself. This is
186   			 * not really OK, INIT should have no father, i.e.
187   			 * a father with pid NO_PID. But PM currently assumes
188   			 * that mp_parent always points to a valid slot number.
189   			 */
190   			rmp->mp_parent = INIT_PROC_NR;
191   			rmp->mp_procgrp = rmp->mp_pid = INIT_PID;
192 			rmp->mp_flags |= IN_USE;
193 
194 			/* Set scheduling info */
195 			rmp->mp_scheduler = KERNEL;
196 			rmp->mp_nice = get_nice_value(USR_Q);
197 		}
198 		else {					/* system process */
199   			if(ip->proc_nr == RS_PROC_NR) {
200   				rmp->mp_parent = INIT_PROC_NR;
201   			}
202   			else {
203   				rmp->mp_parent = RS_PROC_NR;
204   			}
205   			rmp->mp_pid = get_free_pid();
206 			rmp->mp_flags |= IN_USE | PRIV_PROC;
207 
208 			/* RS schedules this process */
209 			rmp->mp_scheduler = NONE;
210 			rmp->mp_nice = get_nice_value(SRV_Q);
211 		}
212 
213 		/* Get kernel endpoint identifier. */
214 		rmp->mp_endpoint = ip->endpoint;
215 
216 		/* Tell VFS about this system process. */
217 		memset(&mess, 0, sizeof(mess));
218 		mess.m_type = VFS_PM_INIT;
219 		mess.VFS_PM_SLOT = ip->proc_nr;
220 		mess.VFS_PM_PID = rmp->mp_pid;
221 		mess.VFS_PM_ENDPT = rmp->mp_endpoint;
222   		if (OK != (s=ipc_send(VFS_PROC_NR, &mess)))
223 			panic("can't sync up with VFS: %d", s);
224   	}
225   }
226 
227   /* Tell VFS that no more system processes follow and synchronize. */
228   memset(&mess, 0, sizeof(mess));
229   mess.m_type = VFS_PM_INIT;
230   mess.VFS_PM_ENDPT = NONE;
231   if (ipc_sendrec(VFS_PROC_NR, &mess) != OK || mess.m_type != OK)
232 	panic("can't sync up with VFS");
233 
234  system_hz = sys_hz();
235 
236   /* Initialize user-space scheduling. */
237   sched_init();
238 
239   return(OK);
240 }
241 
242 /*===========================================================================*
243  *				reply					     *
244  *===========================================================================*/
245 void reply(proc_nr, result)
246 int proc_nr;			/* process to reply to */
247 int result;			/* result of call (usually OK or error #) */
248 {
249 /* Send a reply to a user process.  System calls may occasionally fill in other
250  * fields, this is only for the main return value and for sending the reply.
251  */
252   struct mproc *rmp;
253   int r;
254 
255   if(proc_nr < 0 || proc_nr >= NR_PROCS)
256       panic("reply arg out of range: %d", proc_nr);
257 
258   rmp = &mproc[proc_nr];
259   rmp->mp_reply.m_type = result;
260 
261   if ((r = ipc_sendnb(rmp->mp_endpoint, &rmp->mp_reply)) != OK)
262 	printf("PM can't reply to %d (%s): %d\n", rmp->mp_endpoint,
263 		rmp->mp_name, r);
264 }
265 
266 /*===========================================================================*
267  *				get_nice_value				     *
268  *===========================================================================*/
269 static int get_nice_value(queue)
270 int queue;				/* store mem chunks here */
271 {
272 /* Processes in the boot image have a priority assigned. The PM doesn't know
273  * about priorities, but uses 'nice' values instead. The priority is between
274  * MIN_USER_Q and MAX_USER_Q. We have to scale between PRIO_MIN and PRIO_MAX.
275  */
276   int nice_val = (queue - USER_Q) * (PRIO_MAX-PRIO_MIN+1) /
277       (MIN_USER_Q-MAX_USER_Q+1);
278   if (nice_val > PRIO_MAX) nice_val = PRIO_MAX;	/* shouldn't happen */
279   if (nice_val < PRIO_MIN) nice_val = PRIO_MIN;	/* shouldn't happen */
280   return nice_val;
281 }
282 
283 /*===========================================================================*
284  *				handle_vfs_reply       			     *
285  *===========================================================================*/
286 static void handle_vfs_reply()
287 {
288   struct mproc *rmp;
289   endpoint_t proc_e;
290   int r, proc_n, new_parent;
291 
292   /* VFS_PM_REBOOT is the only request not associated with a process.
293    * Handle its reply first.
294    */
295   if (call_nr == VFS_PM_REBOOT_REPLY) {
296 	/* Ask the kernel to abort. All system services, including
297 	 * the PM, will get a HARD_STOP notification. Await the
298 	 * notification in the main loop.
299 	 */
300 	sys_abort(abort_flag);
301 
302 	return;
303   }
304 
305   /* Get the process associated with this call */
306   proc_e = m_in.VFS_PM_ENDPT;
307 
308   if (pm_isokendpt(proc_e, &proc_n) != OK) {
309 	panic("handle_vfs_reply: got bad endpoint from VFS: %d", proc_e);
310   }
311 
312   rmp = &mproc[proc_n];
313 
314   /* Now that VFS replied, mark the process as VFS-idle again */
315   if (!(rmp->mp_flags & VFS_CALL))
316 	panic("handle_vfs_reply: reply without request: %d", call_nr);
317 
318   new_parent = rmp->mp_flags & NEW_PARENT;
319   rmp->mp_flags &= ~(VFS_CALL | NEW_PARENT);
320 
321   if (rmp->mp_flags & UNPAUSED)
322   	panic("handle_vfs_reply: UNPAUSED set on entry: %d", call_nr);
323 
324   /* Call-specific handler code */
325   switch (call_nr) {
326   case VFS_PM_SETUID_REPLY:
327   case VFS_PM_SETGID_REPLY:
328   case VFS_PM_SETGROUPS_REPLY:
329 	/* Wake up the original caller */
330 	reply(rmp-mproc, OK);
331 
332 	break;
333 
334   case VFS_PM_SETSID_REPLY:
335 	/* Wake up the original caller */
336 	reply(rmp-mproc, rmp->mp_procgrp);
337 
338 	break;
339 
340   case VFS_PM_EXEC_REPLY:
341 	exec_restart(rmp, m_in.VFS_PM_STATUS, (vir_bytes)m_in.VFS_PM_PC,
342 		(vir_bytes)m_in.VFS_PM_NEWSP,
343 		(vir_bytes)m_in.VFS_PM_NEWPS_STR);
344 
345 	break;
346 
347   case VFS_PM_EXIT_REPLY:
348 	exit_restart(rmp, FALSE /*dump_core*/);
349 
350 	break;
351 
352   case VFS_PM_CORE_REPLY:
353 	if (m_in.VFS_PM_STATUS == OK)
354 		rmp->mp_sigstatus |= WCOREFLAG;
355 
356 	exit_restart(rmp, TRUE /*dump_core*/);
357 
358 	break;
359 
360   case VFS_PM_FORK_REPLY:
361 	/* Schedule the newly created process ... */
362 	r = OK;
363 	if (rmp->mp_scheduler != KERNEL && rmp->mp_scheduler != NONE) {
364 		r = sched_start_user(rmp->mp_scheduler, rmp);
365 	}
366 
367 	/* If scheduling the process failed, we want to tear down the process
368 	 * and fail the fork */
369 	if (r != OK) {
370 		/* Tear down the newly created process */
371 		rmp->mp_scheduler = NONE; /* don't try to stop scheduling */
372 		exit_proc(rmp, -1, FALSE /*dump_core*/);
373 
374 		/* Wake up the parent with a failed fork (unless dead) */
375 		if (!new_parent)
376 			reply(rmp->mp_parent, -1);
377 	}
378 	else {
379 		/* Wake up the child */
380 		reply(proc_n, OK);
381 
382 		/* Wake up the parent, unless the parent is already dead */
383 		if (!new_parent)
384 			reply(rmp->mp_parent, rmp->mp_pid);
385 	}
386 
387 	break;
388 
389   case VFS_PM_SRV_FORK_REPLY:
390 	/* Nothing to do */
391 
392 	break;
393 
394   case VFS_PM_UNPAUSE_REPLY:
395 	/* The target process must always be stopped while unpausing; otherwise
396 	 * it could just end up pausing itself on a new call afterwards.
397 	 */
398 	assert(rmp->mp_flags & PROC_STOPPED);
399 
400 	/* Process is now unpaused */
401 	rmp->mp_flags |= UNPAUSED;
402 
403 	break;
404 
405   default:
406 	panic("handle_vfs_reply: unknown reply code: %d", call_nr);
407   }
408 
409   /* Now that the process is idle again, look at pending signals */
410   if ((rmp->mp_flags & (IN_USE | EXITING)) == IN_USE)
411 	  restart_sigs(rmp);
412 }
413