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
16 #include <minix/com.h>
17 #include <minix/endpoint.h>
22 /* Prototype declarations for PRIVATE functions. */
23 FORWARD
_PROTOTYPE( void announce
, (void));
25 /*===========================================================================*
27 *===========================================================================*/
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 */
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 /* Global value to test segment sanity. */
42 magictest
= MAGICTEST
;
44 DEBUGMAX(("main()\n"));
46 /* Clear the process table. Anounce each slot as empty and set up mappings
47 * for proc_addr() and proc_nr() macros. Do the same for the table with
48 * privilege structures for the system processes.
50 for (rp
= BEG_PROC_ADDR
, i
= -NR_TASKS
; rp
< END_PROC_ADDR
; ++rp
, ++i
) {
51 rp
->p_rts_flags
= RTS_SLOT_FREE
; /* initialize free slot */
53 rp
->p_nr
= i
; /* proc number from ptr */
54 rp
->p_endpoint
= _ENDPOINT(0, rp
->p_nr
); /* generation no. 0 */
56 for (sp
= BEG_PRIV_ADDR
, i
= 0; sp
< END_PRIV_ADDR
; ++sp
, ++i
) {
57 sp
->s_proc_nr
= NONE
; /* initialize as free */
58 sp
->s_id
= (sys_id_t
) i
; /* priv structure index */
59 ppriv_addr
[i
] = sp
; /* priv ptr from number */
62 /* Set up proc table entries for processes in boot image. The stacks of the
63 * kernel tasks are initialized to an array in data space. The stacks
64 * of the servers have been added to the data segment by the monitor, so
65 * the stack pointer is set to the end of the data segment. All the
66 * processes are in low memory on the 8086. On the 386 only the kernel
67 * is in low memory, the rest is loaded in extended memory.
71 ktsb
= (reg_t
) t_stack
;
73 for (i
=0; i
< NR_BOOT_PROCS
; ++i
) {
78 ip
= &image
[i
]; /* process' attributes */
79 DEBUGMAX(("initializing %s... ", ip
->proc_name
));
80 rp
= proc_addr(ip
->proc_nr
); /* get process pointer */
81 ip
->endpoint
= rp
->p_endpoint
; /* ipc endpoint */
82 rp
->p_scheduler
= NULL
; /* no user space scheduler */
83 rp
->p_priority
= ip
->priority
; /* current priority */
84 rp
->p_quantum_size
= ip
->quantum
; /* quantum size in ticks */
85 rp
->p_ticks_left
= ip
->quantum
; /* current credit */
86 strncpy(rp
->p_name
, ip
->proc_name
, P_NAME_LEN
); /* set process name */
88 /* See if this process is immediately schedulable.
89 * In that case, set its privileges now and allow it to run.
90 * Only kernel tasks and the root system process get to run immediately.
91 * All the other system processes are inhibited from running by the
92 * RTS_NO_PRIV flag. They can only be scheduled once the root system
93 * process has set their privileges.
95 proc_nr
= proc_nr(rp
);
96 schedulable_proc
= (iskerneln(proc_nr
) || isrootsysn(proc_nr
));
97 if(schedulable_proc
) {
98 /* Assign privilege structure. Force a static privilege id. */
99 (void) get_priv(rp
, static_priv_id(proc_nr
));
101 /* Priviliges for kernel tasks. */
102 if(iskerneln(proc_nr
)) {
103 /* Privilege flags. */
104 priv(rp
)->s_flags
= (proc_nr
== IDLE
? IDL_F
: TSK_F
);
106 priv(rp
)->s_trap_mask
= (proc_nr
== CLOCK
107 || proc_nr
== SYSTEM
? CSK_T
: TSK_T
);
108 ipc_to_m
= TSK_M
; /* allowed targets */
109 kcalls
= TSK_KC
; /* allowed kernel calls */
111 /* Priviliges for the root system process. */
112 else if(isrootsysn(proc_nr
)) {
113 priv(rp
)->s_flags
= RSYS_F
; /* privilege flags */
114 priv(rp
)->s_trap_mask
= RSYS_T
; /* allowed traps */
115 ipc_to_m
= RSYS_M
; /* allowed targets */
116 kcalls
= RSYS_KC
; /* allowed kernel calls */
117 priv(rp
)->s_sig_mgr
= RSYS_SM
; /* signal manager */
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"));
223 sprofiling
= 0; /* we're not profiling until instructed to */
224 #endif /* SPROFILE */
225 cprof_procs_no
= 0; /* init nr of hash table slots used */
228 krandom
.random_sources
= RANDOM_SOURCES
;
229 krandom
.random_elements
= RANDOM_ELEMENTS
;
231 /* MINIX is now ready. All boot image processes are on the ready queue.
232 * Return to the assembly code to start running the current process.
234 bill_ptr
= proc_addr(IDLE
); /* it has to point somewhere */
235 announce(); /* print MINIX startup banner */
238 * enable timer interrupts and clock task on the boot CPU
241 if (boot_cpu_init_timer(system_hz
)) {
242 panic( "FATAL : failed to initialize timer interrupts; "
243 "cannot continue without any clock source!");
246 /* Warnings for sanity checks that take time. These warnings are printed
247 * so it's a clear warning no full release should be done with them
251 FIXME("PROC check enabled");
254 DEBUGMAX(("cycles_accounting_init()... "));
255 cycles_accounting_init();
256 DEBUGMAX(("done\n"));
258 assert(runqueues_ok());
264 /*===========================================================================*
266 *===========================================================================*/
267 PRIVATE
void announce(void)
269 /* Display the MINIX startup banner. */
270 printf("\nMINIX %s.%s. "
272 "(" _SVN_REVISION
")\n"
274 "Copyright 2010, Vrije Universiteit, Amsterdam, The Netherlands\n",
275 OS_RELEASE
, OS_VERSION
);
276 printf("MINIX is open source software, see http://www.minix3.org\n");
279 /*===========================================================================*
281 *===========================================================================*/
282 PUBLIC
void prepare_shutdown(const int how
)
284 /* This function prepares to shutdown MINIX. */
285 static timer_t shutdown_timer
;
287 /* Continue after 1 second, to give processes a chance to get scheduled to
288 * do shutdown work. Set a watchog timer to call shutdown(). The timer
289 * argument passes the shutdown status.
291 printf("MINIX will now be shut down ...\n");
292 tmr_arg(&shutdown_timer
)->ta_int
= how
;
293 set_timer(&shutdown_timer
, get_uptime() + system_hz
, minix_shutdown
);
296 /*===========================================================================*
298 *===========================================================================*/
299 PUBLIC
void minix_shutdown(timer_t
*tp
)
301 /* This function is called from prepare_shutdown or stop_sequence to bring
302 * down MINIX. How to shutdown is in the argument: RBT_HALT (return to the
303 * monitor), RBT_MONITOR (execute given code), RBT_RESET (hard reset).
305 arch_stop_local_timer();
306 intr_init(INTS_ORIG
, 0);
307 arch_shutdown(tp
? tmr_arg(tp
)->ta_int
: RBT_PANIC
);