source: branches/minix3-book/servers/pm/main.c@ 26

Last change on this file since 26 was 4, checked in by Mattia Monga, 14 years ago

Importazione sorgenti libro

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