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