source: trunk/minix/servers/pm/main.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 17.3 KB
Line 
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 FS. Both PM
4 * and FS 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 * setreply: set the reply to be sent to process making an PM system call
10 */
11
12#include "pm.h"
13#include <minix/keymap.h>
14#include <minix/callnr.h>
15#include <minix/com.h>
16#include <minix/endpoint.h>
17#include <signal.h>
18#include <stdlib.h>
19#include <fcntl.h>
20#include <sys/resource.h>
21#include <string.h>
22#include "mproc.h"
23#include "param.h"
24
25#include "../../kernel/const.h"
26#include "../../kernel/config.h"
27#include "../../kernel/type.h"
28#include "../../kernel/proc.h"
29
30FORWARD _PROTOTYPE( void get_work, (void) );
31FORWARD _PROTOTYPE( void pm_init, (void) );
32FORWARD _PROTOTYPE( int get_nice_value, (int queue) );
33FORWARD _PROTOTYPE( void get_mem_chunks, (struct memory *mem_chunks) );
34FORWARD _PROTOTYPE( void patch_mem_chunks, (struct memory *mem_chunks,
35 struct mem_map *map_ptr) );
36FORWARD _PROTOTYPE( void do_x86_vm, (struct memory mem_chunks[NR_MEMS]) );
37
38#define click_to_round_k(n) \
39 ((unsigned) ((((unsigned long) (n) << CLICK_SHIFT) + 512) / 1024))
40
41/*===========================================================================*
42 * main *
43 *===========================================================================*/
44PUBLIC int main()
45{
46/* Main routine of the process manager. */
47 int result, s, proc_nr;
48 struct mproc *rmp;
49 sigset_t sigset;
50
51 pm_init(); /* initialize process manager tables */
52
53 /* This is PM's main loop- get work and do it, forever and forever. */
54 while (TRUE) {
55 get_work(); /* wait for an PM system call */
56
57 /* Check for system notifications first. Special cases. */
58 if (call_nr == SYN_ALARM) {
59 pm_expire_timers(m_in.NOTIFY_TIMESTAMP);
60 result = SUSPEND; /* don't reply */
61 } else if (call_nr == SYS_SIG) { /* signals pending */
62 sigset = m_in.NOTIFY_ARG;
63 if (sigismember(&sigset, SIGKSIG)) {
64 (void) ksig_pending();
65 }
66 result = SUSPEND; /* don't reply */
67 }
68 /* Else, if the system call number is valid, perform the call. */
69 else if ((unsigned) call_nr >= NCALLS) {
70 result = ENOSYS;
71 } else {
72 result = (*call_vec[call_nr])();
73 }
74
75 /* Send the results back to the user to indicate completion. */
76 if (result != SUSPEND) setreply(who_p, result);
77
78 swap_in(); /* maybe a process can be swapped in? */
79
80 /* Send out all pending reply messages, including the answer to
81 * the call just made above. The processes must not be swapped out.
82 */
83 for (proc_nr=0, rmp=mproc; proc_nr < NR_PROCS; proc_nr++, rmp++) {
84 /* In the meantime, the process may have been killed by a
85 * signal (e.g. if a lethal pending signal was unblocked)
86 * without the PM realizing it. If the slot is no longer in
87 * use or just a zombie, don't try to reply.
88 */
89 if ((rmp->mp_flags & (REPLY | ONSWAP | IN_USE | ZOMBIE)) ==
90 (REPLY | IN_USE)) {
91 if ((s=send(rmp->mp_endpoint, &rmp->mp_reply)) != OK) {
92 printf("PM can't reply to %d (%s)\n",
93 rmp->mp_endpoint, rmp->mp_name);
94 panic(__FILE__, "PM can't reply", NO_NUM);
95 }
96 rmp->mp_flags &= ~REPLY;
97 }
98 }
99 }
100 return(OK);
101}
102
103/*===========================================================================*
104 * get_work *
105 *===========================================================================*/
106PRIVATE void get_work()
107{
108/* Wait for the next message and extract useful information from it. */
109 if (receive(ANY, &m_in) != OK)
110 panic(__FILE__,"PM receive error", NO_NUM);
111 who_e = m_in.m_source; /* who sent the message */
112 if(pm_isokendpt(who_e, &who_p) != OK)
113 panic(__FILE__, "PM got message from invalid endpoint", who_e);
114 call_nr = m_in.m_type; /* system call number */
115
116 /* Process slot of caller. Misuse PM's own process slot if the kernel is
117 * calling. This can happen in case of synchronous alarms (CLOCK) or or
118 * event like pending kernel signals (SYSTEM).
119 */
120 mp = &mproc[who_p < 0 ? PM_PROC_NR : who_p];
121 if(who_p >= 0 && mp->mp_endpoint != who_e) {
122 panic(__FILE__, "PM endpoint number out of sync with source",
123 mp->mp_endpoint);
124 }
125}
126
127/*===========================================================================*
128 * setreply *
129 *===========================================================================*/
130PUBLIC void setreply(proc_nr, result)
131int proc_nr; /* process to reply to */
132int result; /* result of call (usually OK or error #) */
133{
134/* Fill in a reply message to be sent later to a user process. System calls
135 * may occasionally fill in other fields, this is only for the main return
136 * value, and for setting the "must send reply" flag.
137 */
138 register struct mproc *rmp = &mproc[proc_nr];
139
140 if(proc_nr < 0 || proc_nr >= NR_PROCS)
141 panic(__FILE__,"setreply arg out of range", proc_nr);
142
143 rmp->mp_reply.reply_res = result;
144 rmp->mp_flags |= REPLY; /* reply pending */
145
146 if (rmp->mp_flags & ONSWAP)
147 swap_inqueue(rmp); /* must swap this process back in */
148}
149
150/*===========================================================================*
151 * pm_init *
152 *===========================================================================*/
153PRIVATE void pm_init()
154{
155/* Initialize the process manager.
156 * Memory use info is collected from the boot monitor, the kernel, and
157 * all processes compiled into the system image. Initially this information
158 * is put into an array mem_chunks. Elements of mem_chunks are struct memory,
159 * and hold base, size pairs in units of clicks. This array is small, there
160 * should be no more than 8 chunks. After the array of chunks has been built
161 * the contents are used to initialize the hole list. Space for the hole list
162 * is reserved as an array with twice as many elements as the maximum number
163 * of processes allowed. It is managed as a linked list, and elements of the
164 * array are struct hole, which, in addition to storage for a base and size in
165 * click units also contain space for a link, a pointer to another element.
166*/
167 int s;
168 static struct boot_image image[NR_BOOT_PROCS];
169 register struct boot_image *ip;
170 static char core_sigs[] = { SIGQUIT, SIGILL, SIGTRAP, SIGABRT,
171 SIGEMT, SIGFPE, SIGUSR1, SIGSEGV, SIGUSR2 };
172 static char ign_sigs[] = { SIGCHLD, SIGWINCH, SIGCONT };
173 static char mess_sigs[] = { SIGTERM, SIGHUP, SIGABRT, SIGQUIT };
174 register struct mproc *rmp;
175 register int i;
176 register char *sig_ptr;
177 phys_clicks total_clicks, minix_clicks, free_clicks;
178 message mess;
179 struct mem_map mem_map[NR_LOCAL_SEGS];
180 struct memory mem_chunks[NR_MEMS];
181
182 /* Initialize process table, including timers. */
183 for (rmp=&mproc[0]; rmp<&mproc[NR_PROCS]; rmp++) {
184 tmr_inittimer(&rmp->mp_timer);
185 }
186
187 /* Build the set of signals which cause core dumps, and the set of signals
188 * that are by default ignored.
189 */
190 sigemptyset(&core_sset);
191 for (sig_ptr = core_sigs; sig_ptr < core_sigs+sizeof(core_sigs); sig_ptr++)
192 sigaddset(&core_sset, *sig_ptr);
193 sigemptyset(&ign_sset);
194 for (sig_ptr = ign_sigs; sig_ptr < ign_sigs+sizeof(ign_sigs); sig_ptr++)
195 sigaddset(&ign_sset, *sig_ptr);
196
197 /* Obtain a copy of the boot monitor parameters and the kernel info struct.
198 * Parse the list of free memory chunks. This list is what the boot monitor
199 * reported, but it must be corrected for the kernel and system processes.
200 */
201 if ((s=sys_getmonparams(monitor_params, sizeof(monitor_params))) != OK)
202 panic(__FILE__,"get monitor params failed",s);
203 get_mem_chunks(mem_chunks);
204 if ((s=sys_getkinfo(&kinfo)) != OK)
205 panic(__FILE__,"get kernel info failed",s);
206
207 /* Get the memory map of the kernel to see how much memory it uses. */
208 if ((s=get_mem_map(SYSTASK, mem_map)) != OK)
209 panic(__FILE__,"couldn't get memory map of SYSTASK",s);
210 minix_clicks = (mem_map[S].mem_phys+mem_map[S].mem_len)-mem_map[T].mem_phys;
211 patch_mem_chunks(mem_chunks, mem_map);
212
213 /* Initialize PM's process table. Request a copy of the system image table
214 * that is defined at the kernel level to see which slots to fill in.
215 */
216 if (OK != (s=sys_getimage(image)))
217 panic(__FILE__,"couldn't get image table: %d\n", s);
218 procs_in_use = 0; /* start populating table */
219 printf("Building process table:"); /* show what's happening */
220 for (ip = &image[0]; ip < &image[NR_BOOT_PROCS]; ip++) {
221 if (ip->proc_nr >= 0) { /* task have negative nrs */
222 procs_in_use += 1; /* found user process */
223
224 /* Set process details found in the image table. */
225 rmp = &mproc[ip->proc_nr];
226 strncpy(rmp->mp_name, ip->proc_name, PROC_NAME_LEN);
227 rmp->mp_parent = RS_PROC_NR;
228 rmp->mp_nice = get_nice_value(ip->priority);
229 sigemptyset(&rmp->mp_sig2mess);
230 sigemptyset(&rmp->mp_ignore);
231 sigemptyset(&rmp->mp_sigmask);
232 sigemptyset(&rmp->mp_catch);
233 if (ip->proc_nr == INIT_PROC_NR) { /* user process */
234 rmp->mp_procgrp = rmp->mp_pid = INIT_PID;
235 rmp->mp_flags |= IN_USE;
236 }
237 else { /* system process */
238 rmp->mp_pid = get_free_pid();
239 rmp->mp_flags |= IN_USE | DONT_SWAP | PRIV_PROC;
240 for (sig_ptr = mess_sigs;
241 sig_ptr < mess_sigs+sizeof(mess_sigs);
242 sig_ptr++)
243 sigaddset(&rmp->mp_sig2mess, *sig_ptr);
244 }
245
246 /* Get kernel endpoint identifier. */
247 rmp->mp_endpoint = ip->endpoint;
248
249 /* Get memory map for this process from the kernel. */
250 if ((s=get_mem_map(ip->proc_nr, rmp->mp_seg)) != OK)
251 panic(__FILE__,"couldn't get process entry",s);
252 if (rmp->mp_seg[T].mem_len != 0) rmp->mp_flags |= SEPARATE;
253 minix_clicks += rmp->mp_seg[S].mem_phys +
254 rmp->mp_seg[S].mem_len - rmp->mp_seg[T].mem_phys;
255 patch_mem_chunks(mem_chunks, rmp->mp_seg);
256
257 /* Tell FS about this system process. */
258 mess.PR_SLOT = ip->proc_nr;
259 mess.PR_PID = rmp->mp_pid;
260 mess.PR_ENDPT = rmp->mp_endpoint;
261 if (OK != (s=send(FS_PROC_NR, &mess)))
262 panic(__FILE__,"can't sync up with FS", s);
263 printf(" %s", ip->proc_name); /* display process name */
264 }
265 }
266 printf(".\n"); /* last process done */
267
268 /* Override some details. INIT, PM, FS and RS are somewhat special. */
269 mproc[PM_PROC_NR].mp_pid = PM_PID; /* PM has magic pid */
270 mproc[RS_PROC_NR].mp_parent = INIT_PROC_NR; /* INIT is root */
271 sigfillset(&mproc[PM_PROC_NR].mp_ignore); /* guard against signals */
272
273 /* Tell FS that no more system processes follow and synchronize. */
274 mess.PR_ENDPT = NONE;
275 if (sendrec(FS_PROC_NR, &mess) != OK || mess.m_type != OK)
276 panic(__FILE__,"can't sync up with FS", NO_NUM);
277
278#if ENABLE_BOOTDEV
279 /* Possibly we must correct the memory chunks for the boot device. */
280 if (kinfo.bootdev_size > 0) {
281 mem_map[T].mem_phys = kinfo.bootdev_base >> CLICK_SHIFT;
282 mem_map[T].mem_len = 0;
283 mem_map[D].mem_len = (kinfo.bootdev_size+CLICK_SIZE-1) >> CLICK_SHIFT;
284 patch_mem_chunks(mem_chunks, mem_map);
285 }
286#endif /* ENABLE_BOOTDEV */
287
288 /* Withhold some memory from x86 VM */
289 do_x86_vm(mem_chunks);
290
291 /* Initialize tables to all physical memory and print memory information. */
292 printf("Physical memory:");
293 mem_init(mem_chunks, &free_clicks);
294 total_clicks = minix_clicks + free_clicks;
295 printf(" total %u KB,", click_to_round_k(total_clicks));
296 printf(" system %u KB,", click_to_round_k(minix_clicks));
297 printf(" free %u KB.\n", click_to_round_k(free_clicks));
298}
299
300/*===========================================================================*
301 * get_nice_value *
302 *===========================================================================*/
303PRIVATE int get_nice_value(queue)
304int queue; /* store mem chunks here */
305{
306/* Processes in the boot image have a priority assigned. The PM doesn't know
307 * about priorities, but uses 'nice' values instead. The priority is between
308 * MIN_USER_Q and MAX_USER_Q. We have to scale between PRIO_MIN and PRIO_MAX.
309 */
310 int nice_val = (queue - USER_Q) * (PRIO_MAX-PRIO_MIN+1) /
311 (MIN_USER_Q-MAX_USER_Q+1);
312 if (nice_val > PRIO_MAX) nice_val = PRIO_MAX; /* shouldn't happen */
313 if (nice_val < PRIO_MIN) nice_val = PRIO_MIN; /* shouldn't happen */
314 return nice_val;
315}
316
317#if _WORD_SIZE == 2
318/* In real mode only 1M can be addressed, and in 16-bit protected we can go
319 * no further than we can count in clicks. (The 286 is further limited by
320 * its 24 bit address bus, but we can assume in that case that no more than
321 * 16M memory is reported by the BIOS.)
322 */
323#define MAX_REAL 0x00100000L
324#define MAX_16BIT (0xFFF0L << CLICK_SHIFT)
325#endif
326
327/*===========================================================================*
328 * get_mem_chunks *
329 *===========================================================================*/
330PRIVATE void get_mem_chunks(mem_chunks)
331struct memory *mem_chunks; /* store mem chunks here */
332{
333/* Initialize the free memory list from the 'memory' boot variable. Translate
334 * the byte offsets and sizes in this list to clicks, properly truncated. Also
335 * make sure that we don't exceed the maximum address space of the 286 or the
336 * 8086, i.e. when running in 16-bit protected mode or real mode.
337 */
338 long base, size, limit;
339 char *s, *end; /* use to parse boot variable */
340 int i, done = 0;
341 struct memory *memp;
342#if _WORD_SIZE == 2
343 unsigned long max_address;
344 struct machine machine;
345 if (OK != (i=sys_getmachine(&machine)))
346 panic(__FILE__, "sys_getmachine failed", i);
347#endif
348
349 /* Initialize everything to zero. */
350 for (i = 0; i < NR_MEMS; i++) {
351 memp = &mem_chunks[i]; /* next mem chunk is stored here */
352 memp->base = memp->size = 0;
353 }
354
355 /* The available memory is determined by MINIX' boot loader as a list of
356 * (base:size)-pairs in boothead.s. The 'memory' boot variable is set in
357 * in boot.s. The format is "b0:s0,b1:s1,b2:s2", where b0:s0 is low mem,
358 * b1:s1 is mem between 1M and 16M, b2:s2 is mem above 16M. Pairs b1:s1
359 * and b2:s2 are combined if the memory is adjacent.
360 */
361 s = find_param("memory"); /* get memory boot variable */
362 for (i = 0; i < NR_MEMS && !done; i++) {
363 memp = &mem_chunks[i]; /* next mem chunk is stored here */
364 base = size = 0; /* initialize next base:size pair */
365 if (*s != 0) { /* get fresh data, unless at end */
366
367 /* Read fresh base and expect colon as next char. */
368 base = strtoul(s, &end, 0x10); /* get number */
369 if (end != s && *end == ':') s = ++end; /* skip ':' */
370 else *s=0; /* terminate, should not happen */
371
372 /* Read fresh size and expect comma or assume end. */
373 size = strtoul(s, &end, 0x10); /* get number */
374 if (end != s && *end == ',') s = ++end; /* skip ',' */
375 else done = 1;
376 }
377 limit = base + size;
378#if _WORD_SIZE == 2
379 max_address = machine.protected ? MAX_16BIT : MAX_REAL;
380 if (limit > max_address) limit = max_address;
381#endif
382 base = (base + CLICK_SIZE-1) & ~(long)(CLICK_SIZE-1);
383 limit &= ~(long)(CLICK_SIZE-1);
384 if (limit <= base) continue;
385 memp->base = base >> CLICK_SHIFT;
386 memp->size = (limit - base) >> CLICK_SHIFT;
387 }
388}
389
390/*===========================================================================*
391 * patch_mem_chunks *
392 *===========================================================================*/
393PRIVATE void patch_mem_chunks(mem_chunks, map_ptr)
394struct memory *mem_chunks; /* store mem chunks here */
395struct mem_map *map_ptr; /* memory to remove */
396{
397/* Remove server memory from the free memory list. The boot monitor
398 * promises to put processes at the start of memory chunks. The
399 * tasks all use same base address, so only the first task changes
400 * the memory lists. The servers and init have their own memory
401 * spaces and their memory will be removed from the list.
402 */
403 struct memory *memp;
404 for (memp = mem_chunks; memp < &mem_chunks[NR_MEMS]; memp++) {
405 if (memp->base == map_ptr[T].mem_phys) {
406 memp->base += map_ptr[T].mem_len + map_ptr[D].mem_len;
407 memp->size -= map_ptr[T].mem_len + map_ptr[D].mem_len;
408 }
409 }
410}
411
412#define PAGE_SIZE 4096
413#define PAGE_TABLE_COVER (1024*PAGE_SIZE)
414/*=========================================================================*
415 * do_x86_vm *
416 *=========================================================================*/
417PRIVATE void do_x86_vm(mem_chunks)
418struct memory mem_chunks[NR_MEMS];
419{
420 phys_bytes high, bytes;
421 phys_clicks clicks, base_click;
422 unsigned pages;
423 int i, r;
424
425 /* Compute the highest memory location */
426 high= 0;
427 for (i= 0; i<NR_MEMS; i++)
428 {
429 if (mem_chunks[i].size == 0)
430 continue;
431 if (mem_chunks[i].base + mem_chunks[i].size > high)
432 high= mem_chunks[i].base + mem_chunks[i].size;
433 }
434
435 high <<= CLICK_SHIFT;
436#if VERBOSE_VM
437 printf("do_x86_vm: found high 0x%x\n", high);
438#endif
439
440 /* The number of pages we need is one for the page directory, enough
441 * page tables to cover the memory, and one page for alignement.
442 */
443 pages= 1 + (high + PAGE_TABLE_COVER-1)/PAGE_TABLE_COVER + 1;
444 bytes= pages*PAGE_SIZE;
445 clicks= (bytes + CLICK_SIZE-1) >> CLICK_SHIFT;
446
447#if VERBOSE_VM
448 printf("do_x86_vm: need %d pages\n", pages);
449 printf("do_x86_vm: need %d bytes\n", bytes);
450 printf("do_x86_vm: need %d clicks\n", clicks);
451#endif
452
453 for (i= 0; i<NR_MEMS; i++)
454 {
455 if (mem_chunks[i].size <= clicks)
456 continue;
457 break;
458 }
459 if (i >= NR_MEMS)
460 panic("PM", "not enough memory for VM page tables?", NO_NUM);
461 base_click= mem_chunks[i].base;
462 mem_chunks[i].base += clicks;
463 mem_chunks[i].size -= clicks;
464
465#if VERBOSE_VM
466 printf("do_x86_vm: using 0x%x clicks @ 0x%x\n", clicks, base_click);
467#endif
468 r= sys_vm_setbuf(base_click << CLICK_SHIFT, clicks << CLICK_SHIFT,
469 high);
470 if (r != 0)
471 printf("do_x86_vm: sys_vm_setbuf failed: %d\n", r);
472}
Note: See TracBrowser for help on using the repository browser.