fix compiler warning
[minix.git] / kernel / main.c
blobc867544ecf2597ef9c80f123f5c25bf2f0109cb7
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 <signal.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <a.out.h>
16 #include <minix/callnr.h>
17 #include <minix/com.h>
18 #include <minix/endpoint.h>
19 #include "proc.h"
20 #include "debug.h"
22 /* Prototype declarations for PRIVATE functions. */
23 FORWARD _PROTOTYPE( void announce, (void));
25 /*===========================================================================*
26 * main *
27 *===========================================================================*/
28 PUBLIC void main()
30 /* Start the ball rolling. */
31 struct boot_image *ip; /* boot image pointer */
32 register struct proc *rp; /* process pointer */
33 register struct priv *sp; /* privilege structure pointer */
34 register int i, j, s;
35 int hdrindex; /* index to array of a.out headers */
36 phys_clicks text_base;
37 vir_clicks text_clicks, data_clicks, st_clicks;
38 reg_t ktsb; /* kernel task stack base */
39 struct exec e_hdr; /* for a copy of an a.out header */
41 /* Architecture-dependent initialization. */
42 arch_init();
44 /* Global value to test segment sanity. */
45 magictest = MAGICTEST;
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 = SLOT_FREE; /* initialize free slot */
53 #if DEBUG_SCHED_CHECK
54 rp->p_magic = PMAGIC;
55 #endif
56 rp->p_nr = i; /* proc number from ptr */
57 rp->p_endpoint = _ENDPOINT(0, rp->p_nr); /* generation no. 0 */
59 for (sp = BEG_PRIV_ADDR, i = 0; sp < END_PRIV_ADDR; ++sp, ++i) {
60 sp->s_proc_nr = NONE; /* initialize as free */
61 sp->s_id = i; /* priv structure index */
62 ppriv_addr[i] = sp; /* priv ptr from number */
65 /* Set up proc table entries for processes in boot image. The stacks of the
66 * kernel tasks are initialized to an array in data space. The stacks
67 * of the servers have been added to the data segment by the monitor, so
68 * the stack pointer is set to the end of the data segment. All the
69 * processes are in low memory on the 8086. On the 386 only the kernel
70 * is in low memory, the rest is loaded in extended memory.
73 /* Task stacks. */
74 ktsb = (reg_t) t_stack;
76 for (i=0; i < NR_BOOT_PROCS; ++i) {
77 int ci;
78 bitchunk_t fv;
80 ip = &image[i]; /* process' attributes */
81 rp = proc_addr(ip->proc_nr); /* get process pointer */
82 ip->endpoint = rp->p_endpoint; /* ipc endpoint */
83 rp->p_max_priority = ip->priority; /* max scheduling priority */
84 rp->p_priority = ip->priority; /* current priority */
85 rp->p_quantum_size = ip->quantum; /* quantum size in ticks */
86 rp->p_ticks_left = ip->quantum; /* current credit */
87 strncpy(rp->p_name, ip->proc_name, P_NAME_LEN); /* set process name */
88 (void) get_priv(rp, (ip->flags & SYS_PROC)); /* assign structure */
89 priv(rp)->s_flags = ip->flags; /* process flags */
90 priv(rp)->s_trap_mask = ip->trap_mask; /* allowed traps */
92 /* Warn about violations of the boot image table order consistency. */
93 if (priv_id(rp) != s_nr_to_id(ip->proc_nr) && (ip->flags & SYS_PROC))
94 kprintf("Warning: boot image table has wrong process order\n");
96 /* Initialize call mask bitmap from unordered set.
97 * A single SYS_ALL_CALLS is a special case - it
98 * means all calls are allowed.
100 if(ip->nr_k_calls == 1 && ip->k_calls[0] == SYS_ALL_CALLS)
101 fv = ~0; /* fill call mask */
102 else
103 fv = 0; /* clear call mask */
105 for(ci = 0; ci < CALL_MASK_SIZE; ci++) /* fill or clear call mask */
106 priv(rp)->s_k_call_mask[ci] = fv;
107 if(!fv) /* not all full? enter calls bit by bit */
108 for(ci = 0; ci < ip->nr_k_calls; ci++)
109 SET_BIT(priv(rp)->s_k_call_mask,
110 ip->k_calls[ci]-KERNEL_CALL);
112 for (j = 0; j < NR_SYS_PROCS && j < BITCHUNK_BITS; j++)
113 if (ip->ipc_to & (1 << j))
114 set_sendto_bit(rp, j); /* restrict targets */
116 if (iskerneln(proc_nr(rp))) { /* part of the kernel? */
117 if (ip->stksize > 0) { /* HARDWARE stack size is 0 */
118 rp->p_priv->s_stack_guard = (reg_t *) ktsb;
119 *rp->p_priv->s_stack_guard = STACK_GUARD;
121 ktsb += ip->stksize; /* point to high end of stack */
122 rp->p_reg.sp = ktsb; /* this task's initial stack ptr */
123 hdrindex = 0; /* all use the first a.out header */
124 } else {
125 hdrindex = 1 + i-NR_TASKS; /* servers, drivers, INIT */
128 /* Architecture-specific way to find out aout header of this
129 * boot process.
131 arch_get_aout_headers(hdrindex, &e_hdr);
133 /* Convert addresses to clicks and build process memory map */
134 text_base = e_hdr.a_syms >> CLICK_SHIFT;
135 text_clicks = (e_hdr.a_text + CLICK_SIZE-1) >> CLICK_SHIFT;
136 data_clicks = (e_hdr.a_data+e_hdr.a_bss + CLICK_SIZE-1) >> CLICK_SHIFT;
137 st_clicks= (e_hdr.a_total + CLICK_SIZE-1) >> CLICK_SHIFT;
138 if (!(e_hdr.a_flags & A_SEP))
140 data_clicks= (e_hdr.a_text+e_hdr.a_data+e_hdr.a_bss +
141 CLICK_SIZE-1) >> CLICK_SHIFT;
142 text_clicks = 0; /* common I&D */
144 rp->p_memmap[T].mem_phys = text_base;
145 rp->p_memmap[T].mem_len = text_clicks;
146 rp->p_memmap[D].mem_phys = text_base + text_clicks;
147 rp->p_memmap[D].mem_len = data_clicks;
148 rp->p_memmap[S].mem_phys = text_base + text_clicks + st_clicks;
149 rp->p_memmap[S].mem_vir = st_clicks;
150 rp->p_memmap[S].mem_len = 0;
152 /* Set initial register values. The processor status word for tasks
153 * is different from that of other processes because tasks can
154 * access I/O; this is not allowed to less-privileged processes
156 rp->p_reg.pc = (reg_t) ip->initial_pc;
157 rp->p_reg.psw = (iskernelp(rp)) ? INIT_TASK_PSW : INIT_PSW;
159 /* Initialize the server stack pointer. Take it down one word
160 * to give crtso.s something to use as "argc".
162 if (isusern(proc_nr(rp))) { /* user-space process? */
163 rp->p_reg.sp = (rp->p_memmap[S].mem_vir +
164 rp->p_memmap[S].mem_len) << CLICK_SHIFT;
165 rp->p_reg.sp -= sizeof(reg_t);
168 /* scheduling functions depend on proc_ptr pointing somewhere. */
169 if(!proc_ptr) proc_ptr = rp;
171 /* If this process has its own page table, VM will set the
172 * PT up and manage it. VM will signal the kernel when it has
173 * done this; until then, don't let it run.
175 if(priv(rp)->s_flags & PROC_FULLVM)
176 RTS_SET(rp, VMINHIBIT);
178 /* Set ready. The HARDWARE task is never ready. */
179 if (rp->p_nr == HARDWARE) RTS_SET(rp, PROC_STOP);
180 RTS_UNSET(rp, SLOT_FREE); /* remove SLOT_FREE and schedule */
181 alloc_segments(rp);
184 #if SPROFILE
185 sprofiling = 0; /* we're not profiling until instructed to */
186 #endif /* SPROFILE */
187 cprof_procs_no = 0; /* init nr of hash table slots used */
189 vm_running = 0;
190 krandom.random_sources = RANDOM_SOURCES;
191 krandom.random_elements = RANDOM_ELEMENTS;
193 /* MINIX is now ready. All boot image processes are on the ready queue.
194 * Return to the assembly code to start running the current process.
196 bill_ptr = proc_addr(IDLE); /* it has to point somewhere */
197 announce(); /* print MINIX startup banner */
198 /* Warnings for sanity checks that take time. These warnings are printed
199 * so it's a clear warning no full release should be done with them
200 * enabled.
202 #if DEBUG_SCHED_CHECK
203 FIXME("DEBUG_SCHED_CHECK enabled");
204 #endif
205 #if DEBUG_VMASSERT
206 FIXME("DEBUG_VMASSERT enabled");
207 #endif
208 #if DEBUG_PROC_CHECK
209 FIXME("PROC check enabled");
210 #endif
211 restart();
214 /*===========================================================================*
215 * announce *
216 *===========================================================================*/
217 PRIVATE void announce(void)
219 /* Display the MINIX startup banner. */
220 kprintf("\nMINIX %s.%s. "
221 #ifdef _SVN_REVISION
222 "(" _SVN_REVISION ")\n"
223 #endif
224 "Copyright 2009, Vrije Universiteit, Amsterdam, The Netherlands\n",
225 OS_RELEASE, OS_VERSION);
226 kprintf("MINIX is open source software, see http://www.minix3.org\n");
229 /*===========================================================================*
230 * prepare_shutdown *
231 *===========================================================================*/
232 PUBLIC void prepare_shutdown(how)
233 int how;
235 /* This function prepares to shutdown MINIX. */
236 static timer_t shutdown_timer;
237 register struct proc *rp;
238 message m;
240 /* Continue after 1 second, to give processes a chance to get scheduled to
241 * do shutdown work. Set a watchog timer to call shutdown(). The timer
242 * argument passes the shutdown status.
244 kprintf("MINIX will now be shut down ...\n");
245 tmr_arg(&shutdown_timer)->ta_int = how;
246 set_timer(&shutdown_timer, get_uptime() + system_hz, minix_shutdown);
249 /*===========================================================================*
250 * shutdown *
251 *===========================================================================*/
252 PUBLIC void minix_shutdown(tp)
253 timer_t *tp;
255 /* This function is called from prepare_shutdown or stop_sequence to bring
256 * down MINIX. How to shutdown is in the argument: RBT_HALT (return to the
257 * monitor), RBT_MONITOR (execute given code), RBT_RESET (hard reset).
259 intr_init(INTS_ORIG);
260 clock_stop();
261 arch_shutdown(tp ? tmr_arg(tp)->ta_int : RBT_PANIC);