3.1.7 branch.
[minix.git] / kernel / main.c
blob1ec39736d4381f536b07e3d6cda7c361057790cb
1 /* This file contains the main program of MINIX as well as its shutdown code.
2 * The routine main() initializes the system and starts the ball rolling by
3 * setting up the process table, interrupt vectors, and scheduling each task
4 * to run to initialize itself.
5 * The routine shutdown() does the opposite and brings down MINIX.
7 * The entries into this file are:
8 * main: MINIX main program
9 * prepare_shutdown: prepare to take MINIX down
11 #include "kernel.h"
12 #include <string.h>
13 #include <unistd.h>
14 #include <assert.h>
15 #include <a.out.h>
16 #include <minix/com.h>
17 #include <minix/endpoint.h>
18 #include <minix/u64.h>
19 #include "proc.h"
20 #include "debug.h"
21 #include "clock.h"
23 /* Prototype declarations for PRIVATE functions. */
24 FORWARD _PROTOTYPE( void announce, (void));
26 /*===========================================================================*
27 * main *
28 *===========================================================================*/
29 PUBLIC void main(void)
31 /* Start the ball rolling. */
32 struct boot_image *ip; /* boot image pointer */
33 register struct proc *rp; /* process pointer */
34 register struct priv *sp; /* privilege structure pointer */
35 register int i, j;
36 int hdrindex; /* index to array of a.out headers */
37 phys_clicks text_base;
38 vir_clicks text_clicks, data_clicks, st_clicks;
39 reg_t ktsb; /* kernel task stack base */
40 struct exec e_hdr; /* for a copy of an a.out header */
42 /* Global value to test segment sanity. */
43 magictest = MAGICTEST;
45 DEBUGEXTRA(("main()\n"));
47 /* Clear the process table. Anounce each slot as empty and set up mappings
48 * for proc_addr() and proc_nr() macros. Do the same for the table with
49 * privilege structures for the system processes.
51 for (rp = BEG_PROC_ADDR, i = -NR_TASKS; rp < END_PROC_ADDR; ++rp, ++i) {
52 rp->p_rts_flags = RTS_SLOT_FREE; /* initialize free slot */
53 rp->p_magic = PMAGIC;
54 rp->p_nr = i; /* proc number from ptr */
55 rp->p_endpoint = _ENDPOINT(0, rp->p_nr); /* generation no. 0 */
57 for (sp = BEG_PRIV_ADDR, i = 0; sp < END_PRIV_ADDR; ++sp, ++i) {
58 sp->s_proc_nr = NONE; /* initialize as free */
59 sp->s_id = (sys_id_t) i; /* priv structure index */
60 ppriv_addr[i] = sp; /* priv ptr from number */
63 /* Set up proc table entries for processes in boot image. The stacks of the
64 * kernel tasks are initialized to an array in data space. The stacks
65 * of the servers have been added to the data segment by the monitor, so
66 * the stack pointer is set to the end of the data segment. All the
67 * processes are in low memory on the 8086. On the 386 only the kernel
68 * is in low memory, the rest is loaded in extended memory.
71 /* Task stacks. */
72 ktsb = (reg_t) t_stack;
74 for (i=0; i < NR_BOOT_PROCS; ++i) {
75 int schedulable_proc;
76 proc_nr_t proc_nr;
77 int ipc_to_m, kcalls;
79 ip = &image[i]; /* process' attributes */
80 DEBUGEXTRA(("initializing %s... ", ip->proc_name));
81 rp = proc_addr(ip->proc_nr); /* get process pointer */
82 ip->endpoint = rp->p_endpoint; /* ipc endpoint */
83 rp->p_scheduler = NULL; /* no user space scheduler */
84 rp->p_priority = ip->priority; /* current priority */
85 rp->p_quantum_size_ms = ip->quantum; /* quantum size */
86 make_zero64(rp->p_cpu_time_left);
87 strncpy(rp->p_name, ip->proc_name, P_NAME_LEN); /* set process name */
89 /* See if this process is immediately schedulable.
90 * In that case, set its privileges now and allow it to run.
91 * Only kernel tasks and the root system process get to run immediately.
92 * All the other system processes are inhibited from running by the
93 * RTS_NO_PRIV flag. They can only be scheduled once the root system
94 * process has set their privileges.
96 proc_nr = proc_nr(rp);
97 schedulable_proc = (iskerneln(proc_nr) || isrootsysn(proc_nr));
98 if(schedulable_proc) {
99 /* Assign privilege structure. Force a static privilege id. */
100 (void) get_priv(rp, static_priv_id(proc_nr));
102 /* Priviliges for kernel tasks. */
103 if(iskerneln(proc_nr)) {
104 /* Privilege flags. */
105 priv(rp)->s_flags = (proc_nr == IDLE ? IDL_F : TSK_F);
106 /* Allowed traps. */
107 priv(rp)->s_trap_mask = (proc_nr == CLOCK
108 || proc_nr == SYSTEM ? CSK_T : TSK_T);
109 ipc_to_m = TSK_M; /* allowed targets */
110 kcalls = TSK_KC; /* allowed kernel calls */
112 /* Priviliges for the root system process. */
113 else if(isrootsysn(proc_nr)) {
114 priv(rp)->s_flags= RSYS_F; /* privilege flags */
115 priv(rp)->s_trap_mask= RSYS_T; /* allowed traps */
116 ipc_to_m = RSYS_M; /* allowed targets */
117 kcalls = RSYS_KC; /* allowed kernel calls */
118 priv(rp)->s_sig_mgr = RSYS_SM; /* signal manager */
120 /* Priviliges for ordinary process. */
121 else {
122 NOT_REACHABLE;
125 /* Fill in target mask. */
126 for (j=0; j < NR_SYS_PROCS; j++) {
127 if (ipc_to_m & (1 << j))
128 set_sendto_bit(rp, j);
129 else
130 unset_sendto_bit(rp, j);
133 /* Fill in kernel call mask. */
134 for(j = 0; j < SYS_CALL_MASK_SIZE; j++) {
135 priv(rp)->s_k_call_mask[j] = (kcalls == NO_C ? 0 : (~0));
138 else {
139 /* Don't let the process run for now. */
140 RTS_SET(rp, RTS_NO_PRIV);
143 if (iskerneln(proc_nr)) { /* part of the kernel? */
144 if (ip->stksize > 0) { /* HARDWARE stack size is 0 */
145 rp->p_priv->s_stack_guard = (reg_t *) ktsb;
146 *rp->p_priv->s_stack_guard = STACK_GUARD;
148 ktsb += ip->stksize; /* point to high end of stack */
149 rp->p_reg.sp = ktsb; /* this task's initial stack ptr */
150 hdrindex = 0; /* all use the first a.out header */
151 } else {
152 hdrindex = 1 + i-NR_TASKS; /* system/user processes */
155 /* Architecture-specific way to find out aout header of this
156 * boot process.
158 arch_get_aout_headers(hdrindex, &e_hdr);
160 /* Convert addresses to clicks and build process memory map */
161 text_base = e_hdr.a_syms >> CLICK_SHIFT;
162 text_clicks = (vir_clicks) (CLICK_CEIL(e_hdr.a_text) >> CLICK_SHIFT);
163 data_clicks = (vir_clicks) (CLICK_CEIL(e_hdr.a_data
164 + e_hdr.a_bss) >> CLICK_SHIFT);
165 st_clicks = (vir_clicks) (CLICK_CEIL(e_hdr.a_total) >> CLICK_SHIFT);
166 if (!(e_hdr.a_flags & A_SEP))
168 data_clicks = (vir_clicks) (CLICK_CEIL(e_hdr.a_text +
169 e_hdr.a_data + e_hdr.a_bss) >> CLICK_SHIFT);
170 text_clicks = 0; /* common I&D */
172 rp->p_memmap[T].mem_phys = text_base;
173 rp->p_memmap[T].mem_len = text_clicks;
174 rp->p_memmap[D].mem_phys = text_base + text_clicks;
175 rp->p_memmap[D].mem_len = data_clicks;
176 rp->p_memmap[S].mem_phys = text_base + text_clicks + st_clicks;
177 rp->p_memmap[S].mem_vir = st_clicks;
178 rp->p_memmap[S].mem_len = 0;
180 /* Set initial register values. The processor status word for tasks
181 * is different from that of other processes because tasks can
182 * access I/O; this is not allowed to less-privileged processes
184 rp->p_reg.pc = 0; /* we cannot start anything else */
185 rp->p_reg.psw = (iskerneln(proc_nr)) ? INIT_TASK_PSW : INIT_PSW;
187 /* Initialize the server stack pointer. Take it down one word
188 * to give crtso.s something to use as "argc".
190 if (isusern(proc_nr)) { /* user-space process? */
191 rp->p_reg.sp = (rp->p_memmap[S].mem_vir +
192 rp->p_memmap[S].mem_len) << CLICK_SHIFT;
193 rp->p_reg.sp -= sizeof(reg_t);
196 /* scheduling functions depend on proc_ptr pointing somewhere. */
197 if(!proc_ptr) proc_ptr = rp;
199 /* If this process has its own page table, VM will set the
200 * PT up and manage it. VM will signal the kernel when it has
201 * done this; until then, don't let it run.
203 if(ip->flags & PROC_FULLVM)
204 RTS_SET(rp, RTS_VMINHIBIT);
206 /* None of the kernel tasks run */
207 if (rp->p_nr < 0) RTS_SET(rp, RTS_PROC_STOP);
208 RTS_UNSET(rp, RTS_SLOT_FREE); /* remove RTS_SLOT_FREE and schedule */
209 alloc_segments(rp);
210 DEBUGEXTRA(("done\n"));
213 /* Architecture-dependent initialization. */
214 DEBUGEXTRA(("arch_init()... "));
215 arch_init();
216 DEBUGEXTRA(("done\n"));
218 /* System and processes initialization */
219 DEBUGEXTRA(("system_init()... "));
220 system_init();
221 DEBUGEXTRA(("done\n"));
223 #if SPROFILE
224 sprofiling = 0; /* we're not profiling until instructed to */
225 #endif /* SPROFILE */
226 cprof_procs_no = 0; /* init nr of hash table slots used */
228 vm_running = 0;
229 krandom.random_sources = RANDOM_SOURCES;
230 krandom.random_elements = RANDOM_ELEMENTS;
232 /* MINIX is now ready. All boot image processes are on the ready queue.
233 * Return to the assembly code to start running the current process.
235 bill_ptr = proc_addr(IDLE); /* it has to point somewhere */
236 announce(); /* print MINIX startup banner */
239 * enable timer interrupts and clock task on the boot CPU
242 if (boot_cpu_init_timer(system_hz)) {
243 panic( "FATAL : failed to initialize timer interrupts; "
244 "cannot continue without any clock source!");
247 /* Warnings for sanity checks that take time. These warnings are printed
248 * so it's a clear warning no full release should be done with them
249 * enabled.
251 #if DEBUG_PROC_CHECK
252 FIXME("PROC check enabled");
253 #endif
255 DEBUGEXTRA(("cycles_accounting_init()... "));
256 cycles_accounting_init();
257 DEBUGEXTRA(("done\n"));
259 assert(runqueues_ok());
261 switch_to_user();
262 NOT_REACHABLE;
265 /*===========================================================================*
266 * announce *
267 *===========================================================================*/
268 PRIVATE void announce(void)
270 /* Display the MINIX startup banner. */
271 printf("\nMINIX %s.%s. "
272 #ifdef _SVN_REVISION
273 "(" _SVN_REVISION ")\n"
274 #endif
275 "Copyright 2010, Vrije Universiteit, Amsterdam, The Netherlands\n",
276 OS_RELEASE, OS_VERSION);
277 printf("MINIX is open source software, see http://www.minix3.org\n");
280 /*===========================================================================*
281 * prepare_shutdown *
282 *===========================================================================*/
283 PUBLIC void prepare_shutdown(const int how)
285 /* This function prepares to shutdown MINIX. */
286 static timer_t shutdown_timer;
288 /* Continue after 1 second, to give processes a chance to get scheduled to
289 * do shutdown work. Set a watchog timer to call shutdown(). The timer
290 * argument passes the shutdown status.
292 printf("MINIX will now be shut down ...\n");
293 tmr_arg(&shutdown_timer)->ta_int = how;
294 set_timer(&shutdown_timer, get_uptime() + system_hz, minix_shutdown);
297 /*===========================================================================*
298 * shutdown *
299 *===========================================================================*/
300 PUBLIC void minix_shutdown(timer_t *tp)
302 /* This function is called from prepare_shutdown or stop_sequence to bring
303 * down MINIX. How to shutdown is in the argument: RBT_HALT (return to the
304 * monitor), RBT_MONITOR (execute given code), RBT_RESET (hard reset).
306 arch_stop_local_timer();
307 intr_init(INTS_ORIG, 0);
308 arch_shutdown(tp ? tmr_arg(tp)->ta_int : RBT_PANIC);