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
15 #include <minix/com.h>
16 #include <minix/endpoint.h>
21 /* Prototype declarations for PRIVATE functions. */
22 FORWARD
_PROTOTYPE( void announce
, (void));
24 /*===========================================================================*
26 *===========================================================================*/
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 */
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 DEBUGMAX(("main()\n"));
45 /* Clear the process table. Anounce each slot as empty and set up mappings
46 * for proc_addr() and proc_nr() macros. Do the same for the table with
47 * privilege structures for the system processes.
49 for (rp
= BEG_PROC_ADDR
, i
= -NR_TASKS
; rp
< END_PROC_ADDR
; ++rp
, ++i
) {
50 rp
->p_rts_flags
= RTS_SLOT_FREE
; /* initialize free slot */
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
= (proc_nr_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.
72 ktsb
= (reg_t
) t_stack
;
74 for (i
=0; i
< NR_BOOT_PROCS
; ++i
) {
79 ip
= &image
[i
]; /* process' attributes */
80 DEBUGMAX(("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_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 */
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
);
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 */
119 /* Priviliges for ordinary process. */
124 /* Fill in target mask. */
125 for (j
=0; j
< NR_SYS_PROCS
; j
++) {
126 if (ipc_to_m
& (1 << j
))
127 set_sendto_bit(rp
, j
);
129 unset_sendto_bit(rp
, j
);
132 /* Fill in kernel call mask. */
133 for(j
= 0; j
< SYS_CALL_MASK_SIZE
; j
++) {
134 priv(rp
)->s_k_call_mask
[j
] = (kcalls
== NO_C
? 0 : (~0));
138 /* Don't let the process run for now. */
139 RTS_SET(rp
, RTS_NO_PRIV
);
142 if (iskerneln(proc_nr
)) { /* part of the kernel? */
143 if (ip
->stksize
> 0) { /* HARDWARE stack size is 0 */
144 rp
->p_priv
->s_stack_guard
= (reg_t
*) ktsb
;
145 *rp
->p_priv
->s_stack_guard
= STACK_GUARD
;
147 ktsb
+= ip
->stksize
; /* point to high end of stack */
148 rp
->p_reg
.sp
= ktsb
; /* this task's initial stack ptr */
149 hdrindex
= 0; /* all use the first a.out header */
151 hdrindex
= 1 + i
-NR_TASKS
; /* system/user processes */
154 /* Architecture-specific way to find out aout header of this
157 arch_get_aout_headers(hdrindex
, &e_hdr
);
159 /* Convert addresses to clicks and build process memory map */
160 text_base
= e_hdr
.a_syms
>> CLICK_SHIFT
;
161 text_clicks
= (vir_clicks
) (CLICK_CEIL(e_hdr
.a_text
) >> CLICK_SHIFT
);
162 data_clicks
= (vir_clicks
) (CLICK_CEIL(e_hdr
.a_data
163 + e_hdr
.a_bss
) >> CLICK_SHIFT
);
164 st_clicks
= (vir_clicks
) (CLICK_CEIL(e_hdr
.a_total
) >> CLICK_SHIFT
);
165 if (!(e_hdr
.a_flags
& A_SEP
))
167 data_clicks
= (vir_clicks
) (CLICK_CEIL(e_hdr
.a_text
+
168 e_hdr
.a_data
+ e_hdr
.a_bss
) >> CLICK_SHIFT
);
169 text_clicks
= 0; /* common I&D */
171 rp
->p_memmap
[T
].mem_phys
= text_base
;
172 rp
->p_memmap
[T
].mem_len
= text_clicks
;
173 rp
->p_memmap
[D
].mem_phys
= text_base
+ text_clicks
;
174 rp
->p_memmap
[D
].mem_len
= data_clicks
;
175 rp
->p_memmap
[S
].mem_phys
= text_base
+ text_clicks
+ st_clicks
;
176 rp
->p_memmap
[S
].mem_vir
= st_clicks
;
177 rp
->p_memmap
[S
].mem_len
= 0;
179 /* Set initial register values. The processor status word for tasks
180 * is different from that of other processes because tasks can
181 * access I/O; this is not allowed to less-privileged processes
183 rp
->p_reg
.pc
= (reg_t
) ip
->initial_pc
;
184 rp
->p_reg
.psw
= (iskerneln(proc_nr
)) ? INIT_TASK_PSW
: INIT_PSW
;
186 /* Initialize the server stack pointer. Take it down one word
187 * to give crtso.s something to use as "argc".
189 if (isusern(proc_nr
)) { /* user-space process? */
190 rp
->p_reg
.sp
= (rp
->p_memmap
[S
].mem_vir
+
191 rp
->p_memmap
[S
].mem_len
) << CLICK_SHIFT
;
192 rp
->p_reg
.sp
-= sizeof(reg_t
);
195 /* scheduling functions depend on proc_ptr pointing somewhere. */
196 if(!proc_ptr
) proc_ptr
= rp
;
198 /* If this process has its own page table, VM will set the
199 * PT up and manage it. VM will signal the kernel when it has
200 * done this; until then, don't let it run.
202 if(ip
->flags
& PROC_FULLVM
)
203 RTS_SET(rp
, RTS_VMINHIBIT
);
205 /* None of the kernel tasks run */
206 if (rp
->p_nr
< 0) RTS_SET(rp
, RTS_PROC_STOP
);
207 RTS_UNSET(rp
, RTS_SLOT_FREE
); /* remove RTS_SLOT_FREE and schedule */
209 DEBUGMAX(("done\n"));
212 /* Architecture-dependent initialization. */
213 DEBUGMAX(("arch_init()... "));
215 DEBUGMAX(("done\n"));
217 /* System and processes initialization */
218 DEBUGMAX(("system_init()... "));
220 DEBUGMAX(("done\n"));
221 /* Initialize timers handling */
222 DEBUGMAX(("clock_init()... "));
224 DEBUGMAX(("done\n"));
227 sprofiling
= 0; /* we're not profiling until instructed to */
228 #endif /* SPROFILE */
229 cprof_procs_no
= 0; /* init nr of hash table slots used */
232 krandom
.random_sources
= RANDOM_SOURCES
;
233 krandom
.random_elements
= RANDOM_ELEMENTS
;
235 /* MINIX is now ready. All boot image processes are on the ready queue.
236 * Return to the assembly code to start running the current process.
238 bill_ptr
= proc_addr(IDLE
); /* it has to point somewhere */
239 announce(); /* print MINIX startup banner */
242 * enable timer interrupts and clock task on the boot CPU
245 if (boot_cpu_init_timer(system_hz
)) {
246 panic( "FATAL : failed to initialize timer interrupts; "
247 "cannot continue without any clock source!");
250 /* Warnings for sanity checks that take time. These warnings are printed
251 * so it's a clear warning no full release should be done with them
254 #if DEBUG_SCHED_CHECK
255 FIXME("DEBUG_SCHED_CHECK enabled");
258 FIXME("DEBUG_VMASSERT enabled");
261 FIXME("PROC check enabled");
264 DEBUGMAX(("cycles_accounting_init()... "));
265 cycles_accounting_init();
266 DEBUGMAX(("done\n"));
272 /*===========================================================================*
274 *===========================================================================*/
275 PRIVATE
void announce(void)
277 /* Display the MINIX startup banner. */
278 printf("\nMINIX %s.%s. "
280 "(" _SVN_REVISION
")\n"
282 "Copyright 2010, Vrije Universiteit, Amsterdam, The Netherlands\n",
283 OS_RELEASE
, OS_VERSION
);
284 printf("MINIX is open source software, see http://www.minix3.org\n");
287 /*===========================================================================*
289 *===========================================================================*/
290 PUBLIC
void prepare_shutdown(how
)
293 /* This function prepares to shutdown MINIX. */
294 static timer_t shutdown_timer
;
296 /* Continue after 1 second, to give processes a chance to get scheduled to
297 * do shutdown work. Set a watchog timer to call shutdown(). The timer
298 * argument passes the shutdown status.
300 printf("MINIX will now be shut down ...\n");
301 tmr_arg(&shutdown_timer
)->ta_int
= how
;
302 set_timer(&shutdown_timer
, get_uptime() + system_hz
, minix_shutdown
);
305 /*===========================================================================*
307 *===========================================================================*/
308 PUBLIC
void minix_shutdown(tp
)
311 /* This function is called from prepare_shutdown or stop_sequence to bring
312 * down MINIX. How to shutdown is in the argument: RBT_HALT (return to the
313 * monitor), RBT_MONITOR (execute given code), RBT_RESET (hard reset).
315 arch_stop_local_timer();
316 intr_init(INTS_ORIG
, 0);
317 arch_shutdown(tp
? tmr_arg(tp
)->ta_int
: RBT_PANIC
);