2005-04-21 16:53:53 +02:00
|
|
|
/* This file contains the main program of MINIX as well as its shutdown code.
|
|
|
|
* The routine main() initializes the system and starts the ball rolling by
|
|
|
|
* setting up the process table, interrupt vectors, and scheduling each task
|
|
|
|
* to run to initialize itself.
|
2005-06-24 18:24:40 +02:00
|
|
|
* The routine shutdown() does the opposite and brings down MINIX.
|
2005-04-21 16:53:53 +02:00
|
|
|
*
|
|
|
|
* The entries into this file are:
|
|
|
|
* main: MINIX main program
|
|
|
|
* prepare_shutdown: prepare to take MINIX down
|
|
|
|
*
|
|
|
|
* Changes:
|
|
|
|
* Nov 24, 2004 simplified main() with system image (Jorrit N. Herder)
|
2005-06-24 18:24:40 +02:00
|
|
|
* Aug 20, 2004 new prepare_shutdown() and shutdown() (Jorrit N. Herder)
|
2005-04-21 16:53:53 +02:00
|
|
|
*/
|
|
|
|
#include "kernel.h"
|
|
|
|
#include <signal.h>
|
2005-07-20 17:25:38 +02:00
|
|
|
#include <string.h>
|
2005-04-21 16:53:53 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <a.out.h>
|
|
|
|
#include <minix/callnr.h>
|
|
|
|
#include <minix/com.h>
|
|
|
|
#include "proc.h"
|
|
|
|
|
2005-05-02 16:30:04 +02:00
|
|
|
/* Prototype declarations for PRIVATE functions. */
|
|
|
|
FORWARD _PROTOTYPE( void announce, (void));
|
2005-05-27 14:44:14 +02:00
|
|
|
FORWARD _PROTOTYPE( void shutdown, (timer_t *tp));
|
2005-05-02 16:30:04 +02:00
|
|
|
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* main *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void main()
|
|
|
|
{
|
|
|
|
/* Start the ball rolling. */
|
2005-07-29 17:26:23 +02:00
|
|
|
struct boot_image *ip; /* boot image pointer */
|
2005-07-20 17:25:38 +02:00
|
|
|
register struct proc *rp; /* process pointer */
|
|
|
|
register struct priv *sp; /* privilege structure pointer */
|
|
|
|
register int i, s;
|
2005-04-21 16:53:53 +02:00
|
|
|
int hdrindex; /* index to array of a.out headers */
|
2005-06-20 13:26:48 +02:00
|
|
|
phys_clicks text_base;
|
2005-07-20 17:25:38 +02:00
|
|
|
vir_clicks text_clicks, data_clicks;
|
2005-04-21 16:53:53 +02:00
|
|
|
reg_t ktsb; /* kernel task stack base */
|
|
|
|
struct exec e_hdr; /* for a copy of an a.out header */
|
|
|
|
|
|
|
|
/* Initialize the interrupt controller. */
|
|
|
|
intr_init(1);
|
|
|
|
|
2005-06-24 18:24:40 +02:00
|
|
|
/* Clear the process table. Anounce each slot as empty and set up mappings
|
2005-07-14 17:12:12 +02:00
|
|
|
* for proc_addr() and proc_nr() macros. Do the same for the table with
|
2005-07-26 14:48:34 +02:00
|
|
|
* privilege structures for the system processes.
|
2005-04-21 16:53:53 +02:00
|
|
|
*/
|
|
|
|
for (rp = BEG_PROC_ADDR, i = -NR_TASKS; rp < END_PROC_ADDR; ++rp, ++i) {
|
2005-06-30 17:55:19 +02:00
|
|
|
rp->p_rts_flags = SLOT_FREE; /* initialize free slot */
|
2005-04-21 16:53:53 +02:00
|
|
|
rp->p_nr = i; /* proc number from ptr */
|
|
|
|
(pproc_addr + NR_TASKS)[i] = rp; /* proc ptr from number */
|
|
|
|
}
|
2005-07-14 17:12:12 +02:00
|
|
|
for (sp = BEG_PRIV_ADDR, i = 0; sp < END_PRIV_ADDR; ++sp, ++i) {
|
|
|
|
sp->s_proc_nr = NONE; /* initialize as free */
|
|
|
|
sp->s_id = i; /* priv structure index */
|
|
|
|
ppriv_addr[i] = sp; /* priv ptr from number */
|
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
/* Set up proc table entries for tasks and servers. The stacks of the
|
|
|
|
* kernel tasks are initialized to an array in data space. The stacks
|
|
|
|
* of the servers have been added to the data segment by the monitor, so
|
|
|
|
* the stack pointer is set to the end of the data segment. All the
|
|
|
|
* processes are in low memory on the 8086. On the 386 only the kernel
|
|
|
|
* is in low memory, the rest is loaded in extended memory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Task stacks. */
|
|
|
|
ktsb = (reg_t) t_stack;
|
|
|
|
|
2005-07-14 17:12:12 +02:00
|
|
|
for (i=0; i < NR_BOOT_PROCS; ++i) {
|
|
|
|
ip = &image[i]; /* process' attributes */
|
|
|
|
rp = proc_addr(ip->proc_nr); /* get process pointer */
|
2005-06-30 17:55:19 +02:00
|
|
|
rp->p_max_priority = ip->priority; /* max scheduling priority */
|
|
|
|
rp->p_priority = ip->priority; /* current priority */
|
|
|
|
rp->p_quantum_size = ip->quantum; /* quantum size in ticks */
|
2005-08-22 17:14:11 +02:00
|
|
|
rp->p_ticks_left = ip->quantum; /* current credit */
|
2005-07-21 20:36:40 +02:00
|
|
|
strncpy(rp->p_name, ip->proc_name, P_NAME_LEN); /* set process name */
|
2005-07-26 14:48:34 +02:00
|
|
|
(void) get_priv(rp, (ip->flags & SYS_PROC)); /* assign structure */
|
|
|
|
priv(rp)->s_flags = ip->flags; /* process flags */
|
2005-08-04 21:23:03 +02:00
|
|
|
priv(rp)->s_trap_mask = ip->trap_mask; /* allowed traps */
|
|
|
|
priv(rp)->s_call_mask = ip->call_mask; /* kernel call mask */
|
|
|
|
priv(rp)->s_ipc_to.chunk[0] = ip->ipc_to; /* restrict targets */
|
2005-07-26 14:48:34 +02:00
|
|
|
if (iskerneln(proc_nr(rp))) { /* part of the kernel? */
|
2005-06-30 17:55:19 +02:00
|
|
|
if (ip->stksize > 0) { /* HARDWARE stack size is 0 */
|
2005-07-14 17:12:12 +02:00
|
|
|
rp->p_priv->s_stack_guard = (reg_t *) ktsb;
|
|
|
|
*rp->p_priv->s_stack_guard = STACK_GUARD;
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
2005-06-30 17:55:19 +02:00
|
|
|
ktsb += ip->stksize; /* point to high end of stack */
|
2005-04-21 16:53:53 +02:00
|
|
|
rp->p_reg.sp = ktsb; /* this task's initial stack ptr */
|
2005-04-29 17:36:43 +02:00
|
|
|
text_base = kinfo.code_base >> CLICK_SHIFT;
|
2005-04-21 16:53:53 +02:00
|
|
|
/* processes that are in the kernel */
|
|
|
|
hdrindex = 0; /* all use the first a.out header */
|
|
|
|
} else {
|
2005-07-26 14:48:34 +02:00
|
|
|
hdrindex = 1 + i-NR_TASKS; /* servers, drivers, INIT */
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The bootstrap loader created an array of the a.out headers at
|
|
|
|
* absolute address 'aout'. Get one element to e_hdr.
|
|
|
|
*/
|
|
|
|
phys_copy(aout + hdrindex * A_MINHDR, vir2phys(&e_hdr),
|
2005-07-26 14:48:34 +02:00
|
|
|
(phys_bytes) A_MINHDR);
|
2005-04-21 16:53:53 +02:00
|
|
|
/* Convert addresses to clicks and build process memory map */
|
|
|
|
text_base = e_hdr.a_syms >> CLICK_SHIFT;
|
|
|
|
text_clicks = (e_hdr.a_text + CLICK_SIZE-1) >> CLICK_SHIFT;
|
2005-07-20 17:25:38 +02:00
|
|
|
if (!(e_hdr.a_flags & A_SEP)) text_clicks = 0; /* common I&D */
|
2005-04-21 16:53:53 +02:00
|
|
|
data_clicks = (e_hdr.a_total + CLICK_SIZE-1) >> CLICK_SHIFT;
|
|
|
|
rp->p_memmap[T].mem_phys = text_base;
|
|
|
|
rp->p_memmap[T].mem_len = text_clicks;
|
|
|
|
rp->p_memmap[D].mem_phys = text_base + text_clicks;
|
|
|
|
rp->p_memmap[D].mem_len = data_clicks;
|
|
|
|
rp->p_memmap[S].mem_phys = text_base + text_clicks + data_clicks;
|
|
|
|
rp->p_memmap[S].mem_vir = data_clicks; /* empty - stack is in data */
|
|
|
|
|
|
|
|
/* Set initial register values. The processor status word for tasks
|
|
|
|
* is different from that of other processes because tasks can
|
|
|
|
* access I/O; this is not allowed to less-privileged processes
|
|
|
|
*/
|
2005-06-30 17:55:19 +02:00
|
|
|
rp->p_reg.pc = (reg_t) ip->initial_pc;
|
2005-06-24 18:24:40 +02:00
|
|
|
rp->p_reg.psw = (iskernelp(rp)) ? INIT_TASK_PSW : INIT_PSW;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
/* Initialize the server stack pointer. Take it down one word
|
|
|
|
* to give crtso.s something to use as "argc".
|
|
|
|
*/
|
2005-07-26 14:48:34 +02:00
|
|
|
if (isusern(proc_nr(rp))) { /* user-space process? */
|
2005-04-21 16:53:53 +02:00
|
|
|
rp->p_reg.sp = (rp->p_memmap[S].mem_vir +
|
|
|
|
rp->p_memmap[S].mem_len) << CLICK_SHIFT;
|
|
|
|
rp->p_reg.sp -= sizeof(reg_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set ready. The HARDWARE task is never ready. */
|
2005-08-19 18:43:28 +02:00
|
|
|
if (rp->p_nr != HARDWARE) {
|
|
|
|
rp->p_rts_flags = 0; /* runnable if no flags */
|
|
|
|
lock_enqueue(rp); /* add to scheduling queues */
|
|
|
|
} else {
|
|
|
|
rp->p_rts_flags = NO_MAP; /* prevent from running */
|
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
/* Code and data segments must be allocated in protected mode. */
|
|
|
|
alloc_segments(rp);
|
|
|
|
}
|
|
|
|
|
2005-08-03 13:53:36 +02:00
|
|
|
#if ENABLE_BOOTDEV
|
2005-04-29 17:36:43 +02:00
|
|
|
/* Expect an image of the boot device to be loaded into memory as well.
|
|
|
|
* The boot device is the last module that is loaded into memory, and,
|
|
|
|
* for example, can contain the root FS (useful for embedded systems).
|
2005-04-21 16:53:53 +02:00
|
|
|
*/
|
|
|
|
hdrindex ++;
|
2005-04-29 17:36:43 +02:00
|
|
|
phys_copy(aout + hdrindex * A_MINHDR,vir2phys(&e_hdr),(phys_bytes) A_MINHDR);
|
2005-04-21 16:53:53 +02:00
|
|
|
if (e_hdr.a_flags & A_IMG) {
|
2005-04-29 17:36:43 +02:00
|
|
|
kinfo.bootdev_base = e_hdr.a_syms;
|
|
|
|
kinfo.bootdev_size = e_hdr.a_data;
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
2005-04-29 17:36:43 +02:00
|
|
|
#endif
|
2005-04-21 16:53:53 +02:00
|
|
|
|
2005-06-17 11:09:54 +02:00
|
|
|
/* MINIX is now ready. All boot image processes are on the ready queue.
|
|
|
|
* Return to the assembly code to start running the current process.
|
2005-04-21 16:53:53 +02:00
|
|
|
*/
|
2005-06-17 11:09:54 +02:00
|
|
|
bill_ptr = proc_addr(IDLE); /* it has to point somewhere */
|
|
|
|
announce(); /* print MINIX startup banner */
|
2005-04-21 16:53:53 +02:00
|
|
|
restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*==========================================================================*
|
|
|
|
* announce *
|
|
|
|
*==========================================================================*/
|
|
|
|
PRIVATE void announce(void)
|
|
|
|
{
|
|
|
|
/* Display the MINIX startup banner. */
|
2005-08-08 16:34:10 +02:00
|
|
|
kprintf("MINIX %s.%s. Copyright 1987-2006 Prentice-Hall, Inc.\n",
|
2005-07-20 17:25:38 +02:00
|
|
|
OS_RELEASE, OS_VERSION);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
#if (CHIP == INTEL)
|
|
|
|
/* Real mode, or 16/32-bit protected mode? */
|
2005-08-03 18:06:35 +02:00
|
|
|
kprintf("Executing in %s mode.\n\n",
|
2005-07-20 17:25:38 +02:00
|
|
|
machine.protected ? "32-bit protected" : "real");
|
2005-04-21 16:53:53 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*==========================================================================*
|
|
|
|
* prepare_shutdown *
|
|
|
|
*==========================================================================*/
|
2005-07-27 16:32:16 +02:00
|
|
|
PUBLIC void prepare_shutdown(how)
|
|
|
|
int how;
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
2005-07-27 16:32:16 +02:00
|
|
|
/* This function prepares to shutdown MINIX. */
|
2005-07-21 20:36:40 +02:00
|
|
|
static timer_t shutdown_timer;
|
2005-07-29 17:26:23 +02:00
|
|
|
register struct proc *rp;
|
2005-05-24 12:06:17 +02:00
|
|
|
message m;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
/* Show debugging dumps on panics. Make sure that the TTY task is still
|
|
|
|
* available to handle them. This is done with help of a non-blocking send.
|
|
|
|
* We rely on TTY to call sys_abort() when it is done with the dumps.
|
|
|
|
*/
|
|
|
|
if (how == RBT_PANIC) {
|
|
|
|
m.m_type = PANIC_DUMPS;
|
2005-08-03 10:14:08 +02:00
|
|
|
if (nb_send(TTY_PROC_NR,&m)==OK) /* don't block if TTY isn't ready */
|
2005-04-21 16:53:53 +02:00
|
|
|
return; /* await sys_abort() from TTY */
|
|
|
|
}
|
|
|
|
|
2005-07-20 17:25:38 +02:00
|
|
|
/* Send a signal to all system processes that are still alive to inform
|
|
|
|
* them that the MINIX kernel is shutting down. A proper shutdown sequence
|
|
|
|
* should be implemented by a user-space server. This mechanism is useful
|
|
|
|
* as a backup in case of system panics, so that system processes can still
|
|
|
|
* run their shutdown code, e.g, to synchronize the FS or to let the TTY
|
|
|
|
* switch to the first console.
|
|
|
|
*/
|
2005-07-21 20:36:40 +02:00
|
|
|
kprintf("Sending SIGKSTOP to system processes ...\n");
|
2005-07-20 17:25:38 +02:00
|
|
|
for (rp=BEG_PROC_ADDR; rp<END_PROC_ADDR; rp++) {
|
2005-07-21 20:36:40 +02:00
|
|
|
if (!isemptyp(rp) && (priv(rp)->s_flags & SYS_PROC) && !iskernelp(rp))
|
2005-07-20 17:25:38 +02:00
|
|
|
send_sig(proc_nr(rp), SIGKSTOP);
|
|
|
|
}
|
2005-07-19 14:21:36 +02:00
|
|
|
|
2005-07-20 17:25:38 +02:00
|
|
|
/* Notify system processes of the upcoming shutdown and allow them to be
|
|
|
|
* scheduled by setting a watchog timer that calls shutdown(). The timer
|
|
|
|
* argument passes the shutdown status.
|
2005-04-21 16:53:53 +02:00
|
|
|
*/
|
2005-07-21 20:36:40 +02:00
|
|
|
kprintf("MINIX will now be shut down ...\n");
|
|
|
|
tmr_arg(&shutdown_timer)->ta_int = how;
|
2005-08-05 20:57:20 +02:00
|
|
|
#if DEAD_CODE /* timer hangs the boot monitor ... to be fixed! */
|
2005-07-29 17:26:23 +02:00
|
|
|
set_timer(&shutdown_timer, get_uptime() + HZ, shutdown);
|
2005-08-05 20:57:20 +02:00
|
|
|
#else
|
|
|
|
shutdown(&shutdown_timer);
|
|
|
|
#endif
|
2005-04-21 16:53:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*==========================================================================*
|
|
|
|
* shutdown *
|
|
|
|
*==========================================================================*/
|
2005-05-27 14:44:14 +02:00
|
|
|
PRIVATE void shutdown(tp)
|
|
|
|
timer_t *tp;
|
2005-04-21 16:53:53 +02:00
|
|
|
{
|
|
|
|
/* This function is called from prepare_shutdown or stop_sequence to bring
|
2005-06-24 18:24:40 +02:00
|
|
|
* down MINIX. How to shutdown is in the argument: RBT_HALT (return to the
|
|
|
|
* monitor), RBT_MONITOR (execute given code), RBT_RESET (hard reset).
|
2005-04-21 16:53:53 +02:00
|
|
|
*/
|
2005-05-27 14:44:14 +02:00
|
|
|
int how = tmr_arg(tp)->ta_int;
|
2005-06-24 18:24:40 +02:00
|
|
|
u16_t magic;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
/* Now mask all interrupts, including the clock, and stop the clock. */
|
2005-08-05 20:57:20 +02:00
|
|
|
outb(INT_CTLMASK, ~0);
|
2005-04-21 16:53:53 +02:00
|
|
|
clock_stop();
|
|
|
|
|
|
|
|
if (mon_return && how != RBT_RESET) {
|
|
|
|
/* Reinitialize the interrupt controllers to the BIOS defaults. */
|
|
|
|
intr_init(0);
|
|
|
|
outb(INT_CTLMASK, 0);
|
|
|
|
outb(INT2_CTLMASK, 0);
|
|
|
|
|
2005-06-24 18:24:40 +02:00
|
|
|
/* Return to the boot monitor. Set the program if not already done. */
|
|
|
|
if (how != RBT_MONITOR) phys_copy(vir2phys(""), kinfo.params_base, 1);
|
2005-04-21 16:53:53 +02:00
|
|
|
level0(monitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the system by jumping to the reset address (real mode), or by
|
2005-06-24 18:24:40 +02:00
|
|
|
* forcing a processor shutdown (protected mode). First stop the BIOS
|
|
|
|
* memory test by setting a soft reset flag.
|
2005-04-21 16:53:53 +02:00
|
|
|
*/
|
2005-06-24 18:24:40 +02:00
|
|
|
magic = STOP_MEM_CHECK;
|
|
|
|
phys_copy(vir2phys(&magic), SOFT_RESET_FLAG_ADDR, SOFT_RESET_FLAG_SIZE);
|
2005-04-21 16:53:53 +02:00
|
|
|
level0(reset);
|
|
|
|
}
|
|
|
|
|