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
18 #include <minix/com.h>
19 #include <minix/endpoint.h>
20 #include <machine/vmparam.h>
21 #include <minix/u64.h>
22 #include <minix/type.h>
25 #include "arch_proto.h"
35 /* dummy for linking */
38 /* Prototype declarations for PRIVATE functions. */
39 static void announce(void);
41 void bsp_finish_booting(void)
45 sprofiling
= 0; /* we're not profiling until instructed to */
47 cprof_procs_no
= 0; /* init nr of hash table slots used */
52 krandom
.random_sources
= RANDOM_SOURCES
;
53 krandom
.random_elements
= RANDOM_ELEMENTS
;
55 /* MINIX is now ready. All boot image processes are on the ready queue.
56 * Return to the assembly code to start running the current process.
59 /* it should point somewhere */
60 get_cpulocal_var(bill_ptr
) = get_cpulocal_var_ptr(idle_proc
);
61 get_cpulocal_var(proc_ptr
) = get_cpulocal_var_ptr(idle_proc
);
62 announce(); /* print MINIX startup banner */
65 * we have access to the cpu local run queue, only now schedule the processes.
66 * We ignore the slots for the former kernel tasks
68 for (i
=0; i
< NR_BOOT_PROCS
- NR_TASKS
; i
++) {
69 RTS_UNSET(proc_addr(i
), RTS_PROC_STOP
);
72 * enable timer interrupts and clock task on the boot CPU
74 if (boot_cpu_init_timer(system_hz
)) {
75 panic("FATAL : failed to initialize timer interrupts, "
76 "cannot continue without any clock source!");
81 /* Warnings for sanity checks that take time. These warnings are printed
82 * so it's a clear warning no full release should be done with them
86 FIXME("DEBUG_SCHED_CHECK enabled");
89 FIXME("DEBUG_VMASSERT enabled");
92 FIXME("PROC check enabled");
95 DEBUGEXTRA(("cycles_accounting_init()... "));
96 cycles_accounting_init();
97 DEBUGEXTRA(("done\n"));
100 cpu_set_flag(bsp_cpu_id
, CPU_IS_READY
);
101 machine
.processors_count
= ncpus
;
102 machine
.bsp_id
= bsp_cpu_id
;
104 machine
.processors_count
= 1;
108 /* Kernel may no longer use bits of memory as VM will be running soon */
109 kernel_may_alloc
= 0;
115 /*===========================================================================*
117 *===========================================================================*/
118 void kmain(kinfo_t
*local_cbi
)
120 /* Start the ball rolling. */
121 struct boot_image
*ip
; /* boot image pointer */
122 register struct proc
*rp
; /* process pointer */
125 /* save a global copy of the boot parameters */
126 memcpy(&kinfo
, local_cbi
, sizeof(kinfo
));
127 memcpy(&kmess
, kinfo
.kmess
, sizeof(kmess
));
129 /* We can talk now */
130 printf("MINIX booting\n");
132 /* Kernel may use bits of main memory before VM is started */
133 kernel_may_alloc
= 1;
135 assert(sizeof(kinfo
.boot_procs
) == sizeof(image
));
136 memcpy(kinfo
.boot_procs
, image
, sizeof(kinfo
.boot_procs
));
142 DEBUGEXTRA(("main()\n"));
146 /* Set up proc table entries for processes in boot image. */
147 for (i
=0; i
< NR_BOOT_PROCS
; ++i
) {
148 int schedulable_proc
;
150 int ipc_to_m
, kcalls
;
153 ip
= &image
[i
]; /* process' attributes */
154 DEBUGEXTRA(("initializing %s... ", ip
->proc_name
));
155 rp
= proc_addr(ip
->proc_nr
); /* get process pointer */
156 ip
->endpoint
= rp
->p_endpoint
; /* ipc endpoint */
157 make_zero64(rp
->p_cpu_time_left
);
158 if(i
< NR_TASKS
) /* name (tasks only) */
159 strlcpy(rp
->p_name
, ip
->proc_name
, sizeof(rp
->p_name
));
162 /* Remember this so it can be passed to VM */
163 multiboot_module_t
*mb_mod
= &kinfo
.module_list
[i
- NR_TASKS
];
164 ip
->start_addr
= mb_mod
->mod_start
;
165 ip
->len
= mb_mod
->mod_end
- mb_mod
->mod_start
;
168 reset_proc_accounting(rp
);
170 /* See if this process is immediately schedulable.
171 * In that case, set its privileges now and allow it to run.
172 * Only kernel tasks and the root system process get to run immediately.
173 * All the other system processes are inhibited from running by the
174 * RTS_NO_PRIV flag. They can only be scheduled once the root system
175 * process has set their privileges.
177 proc_nr
= proc_nr(rp
);
178 schedulable_proc
= (iskerneln(proc_nr
) || isrootsysn(proc_nr
) ||
179 proc_nr
== VM_PROC_NR
);
180 if(schedulable_proc
) {
181 /* Assign privilege structure. Force a static privilege id. */
182 (void) get_priv(rp
, static_priv_id(proc_nr
));
184 /* Priviliges for kernel tasks. */
185 if(proc_nr
== VM_PROC_NR
) {
186 priv(rp
)->s_flags
= VM_F
;
187 priv(rp
)->s_trap_mask
= SRV_T
;
190 priv(rp
)->s_sig_mgr
= SELF
;
191 rp
->p_priority
= SRV_Q
;
192 rp
->p_quantum_size_ms
= SRV_QT
;
194 else if(iskerneln(proc_nr
)) {
195 /* Privilege flags. */
196 priv(rp
)->s_flags
= (proc_nr
== IDLE
? IDL_F
: TSK_F
);
198 priv(rp
)->s_trap_mask
= (proc_nr
== CLOCK
199 || proc_nr
== SYSTEM
? CSK_T
: TSK_T
);
200 ipc_to_m
= TSK_M
; /* allowed targets */
201 kcalls
= TSK_KC
; /* allowed kernel calls */
203 /* Priviliges for the root system process. */
205 assert(isrootsysn(proc_nr
));
206 priv(rp
)->s_flags
= RSYS_F
; /* privilege flags */
207 priv(rp
)->s_trap_mask
= SRV_T
; /* allowed traps */
208 ipc_to_m
= SRV_M
; /* allowed targets */
209 kcalls
= SRV_KC
; /* allowed kernel calls */
210 priv(rp
)->s_sig_mgr
= SRV_SM
; /* signal manager */
211 rp
->p_priority
= SRV_Q
; /* priority queue */
212 rp
->p_quantum_size_ms
= SRV_QT
; /* quantum size */
215 /* Fill in target mask. */
216 memset(&map
, 0, sizeof(map
));
218 if (ipc_to_m
== ALL_M
) {
219 for(j
= 0; j
< NR_SYS_PROCS
; j
++)
223 fill_sendto_mask(rp
, &map
);
225 /* Fill in kernel call mask. */
226 for(j
= 0; j
< SYS_CALL_MASK_SIZE
; j
++) {
227 priv(rp
)->s_k_call_mask
[j
] = (kcalls
== NO_C
? 0 : (~0));
231 /* Don't let the process run for now. */
232 RTS_SET(rp
, RTS_NO_PRIV
| RTS_NO_QUANTUM
);
235 /* Arch-specific state initialization. */
236 arch_boot_proc(ip
, rp
);
238 /* scheduling functions depend on proc_ptr pointing somewhere. */
239 if(!get_cpulocal_var(proc_ptr
))
240 get_cpulocal_var(proc_ptr
) = rp
;
242 /* Process isn't scheduled until VM has set up a pagetable for it. */
243 if(rp
->p_nr
!= VM_PROC_NR
&& rp
->p_nr
>= 0) {
244 rp
->p_rts_flags
|= RTS_VMINHIBIT
;
245 rp
->p_rts_flags
|= RTS_BOOTINHIBIT
;
248 rp
->p_rts_flags
|= RTS_PROC_STOP
;
249 rp
->p_rts_flags
&= ~RTS_SLOT_FREE
;
250 DEBUGEXTRA(("done\n"));
253 /* update boot procs info for VM */
254 memcpy(kinfo
.boot_procs
, image
, sizeof(kinfo
.boot_procs
));
256 #define IPCNAME(n) { \
257 assert((n) >= 0 && (n) <= IPCNO_HIGHEST); \
258 assert(!ipc_call_names[n]); \
259 ipc_call_names[n] = #n; \
271 /* System and processes initialization */
273 DEBUGEXTRA(("system_init()... "));
275 DEBUGEXTRA(("done\n"));
277 /* The bootstrap phase is over, so we can add the physical
278 * memory used for it to the free list.
280 add_memmap(&kinfo
, kinfo
.bootstrap_start
, kinfo
.bootstrap_len
);
283 if (config_no_apic
) {
284 BOOT_VERBOSE(printf("APIC disabled, disables SMP, using legacy PIC\n"));
285 smp_single_cpu_fallback();
286 } else if (config_no_smp
) {
287 BOOT_VERBOSE(printf("SMP disabled, using legacy PIC\n"));
288 smp_single_cpu_fallback();
292 * if smp_init() returns it means that it failed and we try to finish
295 bsp_finish_booting();
299 * if configured for a single CPU, we are already on the kernel stack which we
300 * are going to use everytime we execute kernel code. We finish booting and we
303 bsp_finish_booting();
309 /*===========================================================================*
311 *===========================================================================*/
312 static void announce(void)
314 /* Display the MINIX startup banner. */
315 printf("\nMINIX %s.%s. "
317 "(" _VCS_REVISION
")\n"
319 "Copyright 2012, Vrije Universiteit, Amsterdam, The Netherlands\n",
320 OS_RELEASE
, OS_VERSION
);
321 printf("MINIX is open source software, see http://www.minix3.org\n");
324 /*===========================================================================*
326 *===========================================================================*/
327 void prepare_shutdown(const int how
)
329 /* This function prepares to shutdown MINIX. */
330 static timer_t shutdown_timer
;
332 /* Continue after 1 second, to give processes a chance to get scheduled to
333 * do shutdown work. Set a watchog timer to call shutdown(). The timer
334 * argument passes the shutdown status.
336 printf("MINIX will now be shut down ...\n");
337 tmr_arg(&shutdown_timer
)->ta_int
= how
;
338 set_timer(&shutdown_timer
, get_uptime() + system_hz
, minix_shutdown
);
341 /*===========================================================================*
343 *===========================================================================*/
344 void minix_shutdown(timer_t
*tp
)
346 /* This function is called from prepare_shutdown or stop_sequence to bring
347 * down MINIX. How to shutdown is in the argument: RBT_HALT (return to the
348 * monitor), RBT_MONITOR (execute given code), RBT_RESET (hard reset).
354 * we will need to stop timers on all cpus if SMP is enabled and put them in
355 * such a state that we can perform the whole boot process once restarted from
361 hw_intr_disable_all();
363 arch_shutdown(tp
? tmr_arg(tp
)->ta_int
: RBT_PANIC
);
366 /*===========================================================================*
368 *===========================================================================*/
371 /* Perform system initializations prior to calling main(). Most settings are
372 * determined with help of the environment strings passed by MINIX' loader.
374 register char *value
; /* value in key=value pair */
377 /* low-level initialization */
380 /* determine verbosity */
381 if ((value
= env_get(VERBOSEBOOTVARNAME
)))
382 verboseboot
= atoi(value
);
384 /* Get clock tick frequency. */
385 value
= env_get("hz");
387 system_hz
= atoi(value
);
388 if(!value
|| system_hz
< 2 || system_hz
> 50000) /* sanity check */
389 system_hz
= DEFAULT_HZ
;
391 DEBUGEXTRA(("cstart\n"));
393 /* Record miscellaneous information for user-space servers. */
394 kinfo
.nr_procs
= NR_PROCS
;
395 kinfo
.nr_tasks
= NR_TASKS
;
396 strlcpy(kinfo
.release
, OS_RELEASE
, sizeof(kinfo
.release
));
397 strlcpy(kinfo
.version
, OS_VERSION
, sizeof(kinfo
.version
));
399 /* Load average data initialization. */
400 kloadinfo
.proc_last_slot
= 0;
401 for(h
= 0; h
< _LOAD_HISTORY
; h
++)
402 kloadinfo
.proc_load_history
[h
] = 0;
405 value
= env_get("no_apic");
407 config_no_apic
= atoi(value
);
410 value
= env_get("apic_timer_x");
412 config_apic_timer_x
= atoi(value
);
414 config_apic_timer_x
= 1;
418 value
= env_get("watchdog");
420 watchdog_enabled
= atoi(value
);
426 value
= env_get("no_smp");
428 config_no_smp
= atoi(value
);
432 DEBUGEXTRA(("intr_init(0)\n"));
439 /*===========================================================================*
441 *===========================================================================*/
444 const char *params
, /* boot monitor parameters */
445 const char *name
/* key to look up */
448 /* Get environment value - kernel version of getenv to avoid setting up the
449 * usual environment array.
451 register const char *namep
;
454 for (envp
= (char *) params
; *envp
!= 0;) {
455 for (namep
= name
; *namep
!= 0 && *namep
== *envp
; namep
++, envp
++)
457 if (*namep
== '\0' && *envp
== '=') return(envp
+ 1);
464 /*===========================================================================*
466 *===========================================================================*/
467 char *env_get(const char *name
)
469 return get_value(kinfo
.param_buf
, name
);
472 void cpu_print_freq(unsigned cpu
)
476 freq
= cpu_get_freq(cpu
);
477 printf("CPU %d freq %lu MHz\n", cpu
, div64u(freq
, 1000000));
482 return get_cpulocal_var(fpu_presence
);