only check local benchmark dir if it exists
[minix.git] / kernel / main.c
blobfe28258c5ab300012eed4dccef3ba2a818143108
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 <a.out.h>
15 #include <minix/com.h>
16 #include <minix/endpoint.h>
17 #include "proc.h"
18 #include "debug.h"
19 #include "clock.h"
21 /* Prototype declarations for PRIVATE functions. */
22 FORWARD _PROTOTYPE( void announce, (void));
24 /*===========================================================================*
25 * main *
26 *===========================================================================*/
27 PUBLIC void main()
29 /* Start the ball rolling. */
30 struct boot_image *ip; /* boot image pointer */
31 register struct proc *rp; /* process pointer */
32 register struct priv *sp; /* privilege structure pointer */
33 register int i, j;
34 int hdrindex; /* index to array of a.out headers */
35 phys_clicks text_base;
36 vir_clicks text_clicks, data_clicks, st_clicks;
37 reg_t ktsb; /* kernel task stack base */
38 struct exec e_hdr; /* for a copy of an a.out header */
40 /* Global value to test segment sanity. */
41 magictest = MAGICTEST;
43 /* Clear the process table. Anounce each slot as empty and set up mappings
44 * for proc_addr() and proc_nr() macros. Do the same for the table with
45 * privilege structures for the system processes.
47 for (rp = BEG_PROC_ADDR, i = -NR_TASKS; rp < END_PROC_ADDR; ++rp, ++i) {
48 rp->p_rts_flags = RTS_SLOT_FREE; /* initialize free slot */
49 #if DEBUG_SCHED_CHECK
50 rp->p_magic = PMAGIC;
51 #endif
52 rp->p_nr = i; /* proc number from ptr */
53 rp->p_endpoint = _ENDPOINT(0, rp->p_nr); /* generation no. 0 */
55 for (sp = BEG_PRIV_ADDR, i = 0; sp < END_PRIV_ADDR; ++sp, ++i) {
56 sp->s_proc_nr = NONE; /* initialize as free */
57 sp->s_id = (proc_nr_t) i; /* priv structure index */
58 ppriv_addr[i] = sp; /* priv ptr from number */
61 /* Set up proc table entries for processes in boot image. The stacks of the
62 * kernel tasks are initialized to an array in data space. The stacks
63 * of the servers have been added to the data segment by the monitor, so
64 * the stack pointer is set to the end of the data segment. All the
65 * processes are in low memory on the 8086. On the 386 only the kernel
66 * is in low memory, the rest is loaded in extended memory.
69 /* Task stacks. */
70 ktsb = (reg_t) t_stack;
72 for (i=0; i < NR_BOOT_PROCS; ++i) {
73 int schedulable_proc;
74 proc_nr_t proc_nr;
75 int ipc_to_m, kcalls;
77 ip = &image[i]; /* process' attributes */
78 rp = proc_addr(ip->proc_nr); /* get process pointer */
79 ip->endpoint = rp->p_endpoint; /* ipc endpoint */
80 rp->p_max_priority = ip->priority; /* max scheduling priority */
81 rp->p_priority = ip->priority; /* current priority */
82 rp->p_quantum_size = ip->quantum; /* quantum size in ticks */
83 rp->p_ticks_left = ip->quantum; /* current credit */
84 strncpy(rp->p_name, ip->proc_name, P_NAME_LEN); /* set process name */
86 /* See if this process is immediately schedulable.
87 * In that case, set its privileges now and allow it to run.
88 * Only kernel tasks and the root system process get to run immediately.
89 * All the other system processes are inhibited from running by the
90 * RTS_NO_PRIV flag. They can only be scheduled once the root system
91 * process has set their privileges.
93 proc_nr = proc_nr(rp);
94 schedulable_proc = (iskerneln(proc_nr) || isrootsysn(proc_nr));
95 if(schedulable_proc) {
96 /* Assign privilege structure. Force a static privilege id. */
97 (void) get_priv(rp, static_priv_id(proc_nr));
99 /* Priviliges for kernel tasks. */
100 if(iskerneln(proc_nr)) {
101 /* Privilege flags. */
102 priv(rp)->s_flags = (proc_nr == IDLE ? IDL_F : TSK_F);
103 /* Allowed traps. */
104 priv(rp)->s_trap_mask = (proc_nr == CLOCK
105 || proc_nr == SYSTEM ? CSK_T : TSK_T);
106 ipc_to_m = TSK_M; /* allowed targets */
107 kcalls = TSK_KC; /* allowed kernel calls */
109 /* Priviliges for the root system process. */
110 else if(isrootsysn(proc_nr)) {
111 priv(rp)->s_flags= RSYS_F; /* privilege flags */
112 priv(rp)->s_trap_mask= RSYS_T; /* allowed traps */
113 ipc_to_m = RSYS_M; /* allowed targets */
114 kcalls = RSYS_KC; /* allowed kernel calls */
116 /* Priviliges for ordinary process. */
117 else {
118 NOT_REACHABLE;
121 /* Fill in target mask. */
122 for (j=0; j < NR_SYS_PROCS; j++) {
123 if (ipc_to_m & (1 << j))
124 set_sendto_bit(rp, j);
125 else
126 unset_sendto_bit(rp, j);
129 /* Fill in kernel call mask. */
130 for(j = 0; j < SYS_CALL_MASK_SIZE; j++) {
131 priv(rp)->s_k_call_mask[j] = (kcalls == NO_C ? 0 : (~0));
134 else {
135 /* Don't let the process run for now. */
136 RTS_SET(rp, RTS_NO_PRIV);
139 if (iskerneln(proc_nr)) { /* part of the kernel? */
140 if (ip->stksize > 0) { /* HARDWARE stack size is 0 */
141 rp->p_priv->s_stack_guard = (reg_t *) ktsb;
142 *rp->p_priv->s_stack_guard = STACK_GUARD;
144 ktsb += ip->stksize; /* point to high end of stack */
145 rp->p_reg.sp = ktsb; /* this task's initial stack ptr */
146 hdrindex = 0; /* all use the first a.out header */
147 } else {
148 hdrindex = 1 + i-NR_TASKS; /* system/user processes */
151 /* Architecture-specific way to find out aout header of this
152 * boot process.
154 arch_get_aout_headers(hdrindex, &e_hdr);
156 /* Convert addresses to clicks and build process memory map */
157 text_base = e_hdr.a_syms >> CLICK_SHIFT;
158 text_clicks = (vir_clicks) (CLICK_CEIL(e_hdr.a_text) >> CLICK_SHIFT);
159 data_clicks = (vir_clicks) (CLICK_CEIL(e_hdr.a_data
160 + e_hdr.a_bss) >> CLICK_SHIFT);
161 st_clicks = (vir_clicks) (CLICK_CEIL(e_hdr.a_total) >> CLICK_SHIFT);
162 if (!(e_hdr.a_flags & A_SEP))
164 data_clicks = (vir_clicks) (CLICK_CEIL(e_hdr.a_text +
165 e_hdr.a_data + e_hdr.a_bss) >> CLICK_SHIFT);
166 text_clicks = 0; /* common I&D */
168 rp->p_memmap[T].mem_phys = text_base;
169 rp->p_memmap[T].mem_len = text_clicks;
170 rp->p_memmap[D].mem_phys = text_base + text_clicks;
171 rp->p_memmap[D].mem_len = data_clicks;
172 rp->p_memmap[S].mem_phys = text_base + text_clicks + st_clicks;
173 rp->p_memmap[S].mem_vir = st_clicks;
174 rp->p_memmap[S].mem_len = 0;
176 /* Set initial register values. The processor status word for tasks
177 * is different from that of other processes because tasks can
178 * access I/O; this is not allowed to less-privileged processes
180 rp->p_reg.pc = (reg_t) ip->initial_pc;
181 rp->p_reg.psw = (iskerneln(proc_nr)) ? INIT_TASK_PSW : INIT_PSW;
183 /* Initialize the server stack pointer. Take it down one word
184 * to give crtso.s something to use as "argc".
186 if (isusern(proc_nr)) { /* user-space process? */
187 rp->p_reg.sp = (rp->p_memmap[S].mem_vir +
188 rp->p_memmap[S].mem_len) << CLICK_SHIFT;
189 rp->p_reg.sp -= sizeof(reg_t);
192 /* scheduling functions depend on proc_ptr pointing somewhere. */
193 if(!proc_ptr) proc_ptr = rp;
195 /* If this process has its own page table, VM will set the
196 * PT up and manage it. VM will signal the kernel when it has
197 * done this; until then, don't let it run.
199 if(ip->flags & PROC_FULLVM)
200 RTS_SET(rp, RTS_VMINHIBIT);
202 /* Set ready. The HARDWARE task is never ready. */
203 if (rp->p_nr == HARDWARE) RTS_SET(rp, RTS_PROC_STOP);
204 /* IDLE task is never put on a run queue as it is never ready to run */
205 if (rp->p_nr == IDLE) RTS_SET(rp, RTS_PROC_STOP);
206 RTS_UNSET(rp, RTS_SLOT_FREE); /* remove RTS_SLOT_FREE and schedule */
207 alloc_segments(rp);
210 /* Architecture-dependent initialization. */
211 arch_init();
213 #if SPROFILE
214 sprofiling = 0; /* we're not profiling until instructed to */
215 #endif /* SPROFILE */
216 cprof_procs_no = 0; /* init nr of hash table slots used */
218 #ifdef CONFIG_IDLE_TSC
219 idle_tsc = cvu64(0);
220 #endif
222 vm_running = 0;
223 krandom.random_sources = RANDOM_SOURCES;
224 krandom.random_elements = RANDOM_ELEMENTS;
226 /* MINIX is now ready. All boot image processes are on the ready queue.
227 * Return to the assembly code to start running the current process.
229 bill_ptr = proc_addr(IDLE); /* it has to point somewhere */
230 announce(); /* print MINIX startup banner */
233 * enable timer interrupts and clock task on the boot CPU
235 if (boot_cpu_init_timer(system_hz)) {
236 minix_panic("FATAL : failed to initialize timer interrupts, "
237 "cannot continue without any clock source!",
238 NO_NUM);
241 /* Warnings for sanity checks that take time. These warnings are printed
242 * so it's a clear warning no full release should be done with them
243 * enabled.
245 #if DEBUG_SCHED_CHECK
246 FIXME("DEBUG_SCHED_CHECK enabled");
247 #endif
248 #if DEBUG_VMASSERT
249 FIXME("DEBUG_VMASSERT enabled");
250 #endif
251 #if DEBUG_PROC_CHECK
252 FIXME("PROC check enabled");
253 #endif
255 restart();
256 NOT_REACHABLE;
259 /*===========================================================================*
260 * announce *
261 *===========================================================================*/
262 PRIVATE void announce(void)
264 /* Display the MINIX startup banner. */
265 kprintf("\nMINIX %s.%s. "
266 #ifdef _SVN_REVISION
267 "(" _SVN_REVISION ")\n"
268 #endif
269 "Copyright 2010, Vrije Universiteit, Amsterdam, The Netherlands\n",
270 OS_RELEASE, OS_VERSION);
271 kprintf("MINIX is open source software, see http://www.minix3.org\n");
274 /*===========================================================================*
275 * prepare_shutdown *
276 *===========================================================================*/
277 PUBLIC void prepare_shutdown(how)
278 int how;
280 /* This function prepares to shutdown MINIX. */
281 static timer_t shutdown_timer;
283 /* Continue after 1 second, to give processes a chance to get scheduled to
284 * do shutdown work. Set a watchog timer to call shutdown(). The timer
285 * argument passes the shutdown status.
287 kprintf("MINIX will now be shut down ...\n");
288 tmr_arg(&shutdown_timer)->ta_int = how;
289 set_timer(&shutdown_timer, get_uptime() + system_hz, minix_shutdown);
292 /*===========================================================================*
293 * shutdown *
294 *===========================================================================*/
295 PUBLIC void minix_shutdown(tp)
296 timer_t *tp;
298 /* This function is called from prepare_shutdown or stop_sequence to bring
299 * down MINIX. How to shutdown is in the argument: RBT_HALT (return to the
300 * monitor), RBT_MONITOR (execute given code), RBT_RESET (hard reset).
302 arch_stop_local_timer();
303 intr_init(INTS_ORIG, 0);
304 arch_shutdown(tp ? tmr_arg(tp)->ta_int : RBT_PANIC);