Added a separate keymap for escaped scancodes. This makes the code
[minix.git] / kernel / main.c
blob05bf762fddd9bf061284ad47e83a62c7a9a293e0
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"
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, s;
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 /* Clear the process table. Anounce each slot as empty and set up mappings
41 * for proc_addr() and proc_nr() macros. Do the same for the table with
42 * privilege structures for the system processes.
44 for (rp = BEG_PROC_ADDR, i = -NR_TASKS; rp < END_PROC_ADDR; ++rp, ++i) {
45 rp->p_rts_flags = SLOT_FREE; /* initialize free slot */
46 rp->p_nr = i; /* proc number from ptr */
47 rp->p_endpoint = _ENDPOINT(0, rp->p_nr); /* generation no. 0 */
48 (pproc_addr + NR_TASKS)[i] = rp; /* proc ptr from number */
50 for (sp = BEG_PRIV_ADDR, i = 0; sp < END_PRIV_ADDR; ++sp, ++i) {
51 sp->s_proc_nr = NONE; /* initialize as free */
52 sp->s_id = i; /* priv structure index */
53 ppriv_addr[i] = sp; /* priv ptr from number */
56 /* Set up proc table entries for processes in boot image. The stacks of the
57 * kernel tasks are initialized to an array in data space. The stacks
58 * of the servers have been added to the data segment by the monitor, so
59 * the stack pointer is set to the end of the data segment. All the
60 * processes are in low memory on the 8086. On the 386 only the kernel
61 * is in low memory, the rest is loaded in extended memory.
64 /* Task stacks. */
65 ktsb = (reg_t) t_stack;
67 for (i=0; i < NR_BOOT_PROCS; ++i) {
68 int ci;
69 bitchunk_t fv;
70 ip = &image[i]; /* process' attributes */
71 rp = proc_addr(ip->proc_nr); /* get process pointer */
72 ip->endpoint = rp->p_endpoint; /* ipc endpoint */
73 rp->p_max_priority = ip->priority; /* max scheduling priority */
74 rp->p_priority = ip->priority; /* current priority */
75 rp->p_quantum_size = ip->quantum; /* quantum size in ticks */
76 rp->p_ticks_left = ip->quantum; /* current credit */
77 strncpy(rp->p_name, ip->proc_name, P_NAME_LEN); /* set process name */
78 (void) get_priv(rp, (ip->flags & SYS_PROC)); /* assign structure */
79 priv(rp)->s_flags = ip->flags; /* process flags */
80 priv(rp)->s_trap_mask = ip->trap_mask; /* allowed traps */
82 /* Initialize call mask bitmap from unordered set.
83 * A single SYS_ALL_CALLS is a special case - it
84 * means all calls are allowed.
86 if(ip->nr_k_calls == 1 && ip->k_calls[0] == SYS_ALL_CALLS)
87 fv = ~0; /* fill call mask */
88 else
89 fv = 0; /* clear call mask */
91 for(ci = 0; ci < CALL_MASK_SIZE; ci++) /* fill or clear call mask */
92 priv(rp)->s_k_call_mask[ci] = fv;
93 if(!fv) /* not all full? enter calls bit by bit */
94 for(ci = 0; ci < ip->nr_k_calls; ci++)
95 SET_BIT(priv(rp)->s_k_call_mask,
96 ip->k_calls[ci]-KERNEL_CALL);
98 priv(rp)->s_ipc_to.chunk[0] = ip->ipc_to; /* restrict targets */
100 for (j=0; j<BITMAP_CHUNKS(NR_SYS_PROCS); j++) {
101 rp->p_priv->s_ipc_sendrec.chunk[j] = ~0L;
103 unset_sys_bit(rp->p_priv->s_ipc_sendrec, USER_PRIV_ID);
105 if (iskerneln(proc_nr(rp))) { /* part of the kernel? */
106 if (ip->stksize > 0) { /* HARDWARE stack size is 0 */
107 rp->p_priv->s_stack_guard = (reg_t *) ktsb;
108 *rp->p_priv->s_stack_guard = STACK_GUARD;
110 ktsb += ip->stksize; /* point to high end of stack */
111 rp->p_reg.sp = ktsb; /* this task's initial stack ptr */
112 hdrindex = 0; /* all use the first a.out header */
113 } else {
114 hdrindex = 1 + i-NR_TASKS; /* servers, drivers, INIT */
117 /* Architecture-specific way to find out aout header of this
118 * boot process.
120 arch_get_aout_headers(hdrindex, &e_hdr);
122 /* Convert addresses to clicks and build process memory map */
123 text_base = e_hdr.a_syms >> CLICK_SHIFT;
124 text_clicks = (e_hdr.a_text + CLICK_SIZE-1) >> CLICK_SHIFT;
125 data_clicks = (e_hdr.a_data+e_hdr.a_bss + CLICK_SIZE-1) >> CLICK_SHIFT;
126 st_clicks= (e_hdr.a_total + CLICK_SIZE-1) >> CLICK_SHIFT;
127 if (!(e_hdr.a_flags & A_SEP))
129 data_clicks= (e_hdr.a_text+e_hdr.a_data+e_hdr.a_bss +
130 CLICK_SIZE-1) >> CLICK_SHIFT;
131 text_clicks = 0; /* common I&D */
133 rp->p_memmap[T].mem_phys = text_base;
134 rp->p_memmap[T].mem_len = text_clicks;
135 rp->p_memmap[D].mem_phys = text_base + text_clicks;
136 rp->p_memmap[D].mem_len = data_clicks;
137 rp->p_memmap[S].mem_phys = text_base + text_clicks + st_clicks;
138 rp->p_memmap[S].mem_vir = st_clicks;
139 rp->p_memmap[S].mem_len = 0;
141 /* Set initial register values. The processor status word for tasks
142 * is different from that of other processes because tasks can
143 * access I/O; this is not allowed to less-privileged processes
145 rp->p_reg.pc = (reg_t) ip->initial_pc;
146 rp->p_reg.psw = (iskernelp(rp)) ? INIT_TASK_PSW : INIT_PSW;
148 /* Initialize the server stack pointer. Take it down one word
149 * to give crtso.s something to use as "argc".
151 if (isusern(proc_nr(rp))) { /* user-space process? */
152 rp->p_reg.sp = (rp->p_memmap[S].mem_vir +
153 rp->p_memmap[S].mem_len) << CLICK_SHIFT;
154 rp->p_reg.sp -= sizeof(reg_t);
157 /* If this process has its own page table, VM will set the
158 * PT up and manage it. VM will signal the kernel when it has
159 * done this; until then, don't let it run.
161 if(priv(rp)->s_flags & PROC_FULLVM)
162 RTS_SET(rp, VMINHIBIT);
164 /* Set ready. The HARDWARE task is never ready. */
165 if (rp->p_nr == HARDWARE) RTS_SET(rp, NO_PRIORITY);
166 RTS_UNSET(rp, SLOT_FREE); /* remove SLOT_FREE and schedule */
168 /* Code and data segments must be allocated in protected mode. */
169 alloc_segments(rp);
172 #if SPROFILE
173 sprofiling = 0; /* we're not profiling until instructed to */
174 #endif /* SPROFILE */
175 cprof_procs_no = 0; /* init nr of hash table slots used */
177 vm_running = 0;
179 /* MINIX is now ready. All boot image processes are on the ready queue.
180 * Return to the assembly code to start running the current process.
182 bill_ptr = proc_addr(IDLE); /* it has to point somewhere */
183 announce(); /* print MINIX startup banner */
184 restart();
187 /*===========================================================================*
188 * announce *
189 *===========================================================================*/
190 PRIVATE void announce(void)
192 /* Display the MINIX startup banner. */
193 kprintf("\nMINIX %s.%s. "
194 #ifdef _SVN_REVISION
195 "(" _SVN_REVISION ")\n"
196 #endif
197 "Copyright 2006, Vrije Universiteit, Amsterdam, The Netherlands\n",
198 OS_RELEASE, OS_VERSION);
201 /*===========================================================================*
202 * prepare_shutdown *
203 *===========================================================================*/
204 PUBLIC void prepare_shutdown(how)
205 int how;
207 /* This function prepares to shutdown MINIX. */
208 static timer_t shutdown_timer;
209 register struct proc *rp;
210 message m;
212 /* Continue after 1 second, to give processes a chance to get scheduled to
213 * do shutdown work. Set a watchog timer to call shutdown(). The timer
214 * argument passes the shutdown status.
216 kprintf("MINIX will now be shut down ...\n");
217 tmr_arg(&shutdown_timer)->ta_int = how;
218 set_timer(&shutdown_timer, get_uptime() + system_hz, minix_shutdown);
221 /*===========================================================================*
222 * shutdown *
223 *===========================================================================*/
224 PUBLIC void minix_shutdown(tp)
225 timer_t *tp;
227 /* This function is called from prepare_shutdown or stop_sequence to bring
228 * down MINIX. How to shutdown is in the argument: RBT_HALT (return to the
229 * monitor), RBT_MONITOR (execute given code), RBT_RESET (hard reset).
231 intr_init(INTS_ORIG);
232 clock_stop();
233 arch_shutdown(tp ? tmr_arg(tp)->ta_int : RBT_PANIC);