/*===========================================================================* * privileges_dmp * *===========================================================================*/ PUBLIC void privileges_dmp() { register struct proc *rp; static struct proc *oldrp = BEG_PROC_ADDR; register struct priv *sp; static char ipc_to[NR_SYS_PROCS + 1 + NR_SYS_PROCS/8]; int r, i,j, n = 0; /* First obtain a fresh copy of the current process and system table. */ if ((r = sys_getprivtab(priv)) != OK) { report("IS","warning: couldn't get copy of system privileges table", r); return; } if ((r = sys_getproctab(proc)) != OK) { report("IS","warning: couldn't get copy of process table", r); return; } printf("\n--nr-id-name---- -flags- -traps- -ipc_to mask------------------------ \n"); for (rp = oldrp; rp < END_PROC_ADDR; rp++) { if (isemptyp(rp)) continue; if (++n > 23) break; if (proc_nr(rp) == IDLE) printf("(%2d) ", proc_nr(rp)); else if (proc_nr(rp) < 0) printf("[%2d] ", proc_nr(rp)); else printf(" %2d ", proc_nr(rp)); r = -1; for (sp = &priv[0]; sp < &priv[NR_SYS_PROCS]; sp++) if (sp->s_proc_nr == rp->p_nr) { r ++; break; } if (r == -1 && ! (rp->p_rts_flags & SLOT_FREE)) { sp = &priv[USER_PRIV_ID]; } printf("(%02u) %-7.7s %s %s ", sp->s_id, rp->p_name, s_flags_str(sp->s_flags), s_traps_str(sp->s_trap_mask) ); for (i=j=0; i < NR_SYS_PROCS; i++, j++) { ipc_to[j] = get_sys_bit(sp->s_ipc_to, i) ? '1' : '0'; if (i % 8 == 7) ipc_to[++j] = ' '; } ipc_to[j] = '\0'; printf(" %s \n", ipc_to); } if (rp == END_PROC_ADDR) rp = BEG_PROC_ADDR; else printf("--more--\r"); oldrp = rp; }
void print_proc(struct proc *pp) { endpoint_t dep; printf("%d: %s %d prio %d time %d/%d cycles 0x%x%08x cpu %2d " "pdbr 0x%lx rts %s misc %s sched %s ", proc_nr(pp), pp->p_name, pp->p_endpoint, pp->p_priority, pp->p_user_time, pp->p_sys_time, ex64hi(pp->p_cycles), ex64lo(pp->p_cycles), pp->p_cpu, #if defined(__i386__) pp->p_seg.p_cr3, #elif defined(__arm__) pp->p_seg.p_ttbr, #endif rtsflagstr(pp->p_rts_flags), miscflagstr(pp->p_misc_flags), schedulerstr(pp->p_scheduler)); print_sigmgr(pp); dep = P_BLOCKEDON(pp); if(dep != NONE) { printf(" blocked on: "); print_endpoint(dep); } printf("\n"); }
static void kernel_call_finish(struct proc * caller, message *msg, int result) { if(result == VMSUSPEND) { /* Special case: message has to be saved for handling * until VM tells us it's allowed. VM has been notified * and we must wait for its reply to restart the call. */ assert(RTS_ISSET(caller, RTS_VMREQUEST)); assert(caller->p_vmrequest.type == VMSTYPE_KERNELCALL); caller->p_vmrequest.saved.reqmsg = *msg; caller->p_misc_flags |= MF_KCALL_RESUME; } else { /* * call is finished, we could have been suspended because of VM, * remove the request message */ caller->p_vmrequest.saved.reqmsg.m_source = NONE; if (result != EDONTREPLY) { /* copy the result as a message to the original user buffer */ msg->m_source = SYSTEM; msg->m_type = result; /* report status of call */ #if DEBUG_IPC_HOOK hook_ipc_msgkresult(msg, caller); #endif if (copy_msg_to_user(msg, (message *)caller->p_delivermsg_vir)) { printf("WARNING wrong user pointer 0x%08x from " "process %s / %d\n", caller->p_delivermsg_vir, caller->p_name, caller->p_endpoint); cause_sig(proc_nr(caller), SIGSEGV); } } } }
/* * this function checks the basic syscall parameters and if accepted it * dispatches its handling to the right handler */ void kernel_call(message *m_user, struct proc * caller) { int result = OK; message msg; caller->p_delivermsg_vir = (vir_bytes) m_user; /* * the ldt and cr3 of the caller process is loaded because it just've trapped * into the kernel or was already set in switch_to_user() before we resume * execution of an interrupted kernel call */ if (copy_msg_from_user(m_user, &msg) == 0) { msg.m_source = caller->p_endpoint; result = kernel_call_dispatch(caller, &msg); } else { printf("WARNING wrong user pointer 0x%08x from process %s / %d\n", m_user, caller->p_name, caller->p_endpoint); cause_sig(proc_nr(caller), SIGSEGV); return; } /* remember who invoked the kcall so we can bill it its time */ kbill_kcall = caller; kernel_call_finish(caller, &msg, result); }
/*===========================================================================* * memmap_dmp * *===========================================================================*/ PUBLIC void memmap_dmp() { register struct proc *rp; static struct proc *oldrp = proc; int r, n = 0; phys_clicks size; /* First obtain a fresh copy of the current process table. */ if ((r = sys_getproctab(proc)) != OK) { report("IS","warning: couldn't get copy of process table", r); return; } printf("\n-nr/name--- --pc-- --sp-- -----text----- -----data----- ----stack----- --size-\n"); for (rp = oldrp; rp < END_PROC_ADDR; rp++) { if (isemptyp(rp)) continue; if (++n > 23) break; size = rp->p_memmap[T].mem_len + ((rp->p_memmap[S].mem_phys + rp->p_memmap[S].mem_len) - rp->p_memmap[D].mem_phys); printf("%3d %-7.7s%7lx%7lx %4x %4x %4x %4x %4x %4x %4x %4x %4x %5uK\n", proc_nr(rp), rp->p_name, (unsigned long) rp->p_reg.pc, (unsigned long) rp->p_reg.sp, rp->p_memmap[T].mem_vir, rp->p_memmap[T].mem_phys, rp->p_memmap[T].mem_len, rp->p_memmap[D].mem_vir, rp->p_memmap[D].mem_phys, rp->p_memmap[D].mem_len, rp->p_memmap[S].mem_vir, rp->p_memmap[S].mem_phys, rp->p_memmap[S].mem_len, click_to_round_k(size)); } if (rp == END_PROC_ADDR) rp = proc; else printf("--more--\r"); oldrp = rp; }
PUBLIC void proctab_dmp() { /* Proc table dump */ register struct proc *rp; static struct proc *oldrp = BEG_PROC_ADDR; int r, n = 0; phys_clicks text, data, size; /* First obtain a fresh copy of the current process table. */ if ((r = sys_getproctab(proc)) != OK) { report("IS","warning: couldn't get copy of process table", r); return; } printf("\n-nr-----gen---endpoint--name--- -prior-quant- -user---sys----size-rts flags-\n"); for (rp = oldrp; rp < END_PROC_ADDR; rp++) { if (isemptyp(rp)) continue; if (++n > 23) break; text = rp->p_memmap[T].mem_phys; data = rp->p_memmap[D].mem_phys; size = rp->p_memmap[T].mem_len + ((rp->p_memmap[S].mem_phys + rp->p_memmap[S].mem_len) - data); if (proc_nr(rp) == IDLE) printf("(%2d) ", proc_nr(rp)); else if (proc_nr(rp) < 0) printf("[%2d] ", proc_nr(rp)); else printf(" %2d ", proc_nr(rp)); printf(" %5d %10d ", _ENDPOINT_G(rp->p_endpoint), rp->p_endpoint); printf(" %-8.8s %02u/%02u %02d/%02u %6lu%6lu %6uK %s", rp->p_name, rp->p_priority, rp->p_max_priority, rp->p_ticks_left, rp->p_quantum_size, rp->p_user_time, rp->p_sys_time, click_to_round_k(size), p_rts_flags_str(rp->p_rts_flags)); if (rp->p_rts_flags & (SENDING|RECEIVING)) { printf(" %-7.7s", proc_name(_ENDPOINT_P(rp->p_getfrom_e))); } printf("\n"); } if (rp == END_PROC_ADDR) rp = BEG_PROC_ADDR; else printf("--more--\r"); oldrp = rp; }
PRIVATE void ser_dump_segs(void) { struct proc *pp; for (pp= BEG_PROC_ADDR; pp < END_PROC_ADDR; pp++) { if (isemptyp(pp)) continue; printf("%d: %s ep %d\n", proc_nr(pp), pp->p_name, pp->p_endpoint); printseg("cs: ", 1, pp, pp->p_reg.cs); printseg("ds: ", 0, pp, pp->p_reg.ds); if(pp->p_reg.ss != pp->p_reg.ds) { printseg("ss: ", 0, pp, pp->p_reg.ss); } } }
/*===========================================================================* * sendmask_dmp * *===========================================================================*/ PUBLIC void sendmask_dmp() { register struct proc *rp; static struct proc *oldrp = BEG_PROC_ADDR; int r, i,j, n = 0; /* First obtain a fresh copy of the current process table. */ if ((r = sys_getproctab(proc)) != OK) { report("IS","warning: couldn't get copy of process table", r); return; } printf("\n\n"); printf("Sendmask dump for process table. User processes (*) don't have []."); printf("\n"); printf("The rows of bits indicate to which processes each process may send."); printf("\n\n"); #if DEAD_CODE printf(" "); for (j=proc_nr(BEG_PROC_ADDR); j< INIT_PROC_NR+1; j++) { printf("%3d", j); } printf(" *\n"); for (rp = oldrp; rp < END_PROC_ADDR; rp++) { if (isemptyp(rp)) continue; if (++n > 20) break; printf("%8s ", rp->p_name); if (proc_nr(rp) == IDLE) printf("(%2d) ", proc_nr(rp)); else if (proc_nr(rp) < 0) printf("[%2d] ", proc_nr(rp)); else printf(" %2d ", proc_nr(rp)); for (j=proc_nr(BEG_PROC_ADDR); j<INIT_PROC_NR+2; j++) { if (isallowed(rp->p_sendmask, j)) printf(" 1 "); else printf(" 0 "); } printf("\n"); } if (rp == END_PROC_ADDR) { printf("\n"); rp = BEG_PROC_ADDR; } else printf("--more--\r"); oldrp = rp; #endif }
PUBLIC void print_proc(struct proc *pp) { struct proc *depproc = NULL; endpoint_t dep; printf("%d: %s %d prio %d time %d/%d cycles 0x%x%08x cpu %2d " "cr3 0x%lx rts %s misc %s sched %s ", proc_nr(pp), pp->p_name, pp->p_endpoint, pp->p_priority, pp->p_user_time, pp->p_sys_time, ex64hi(pp->p_cycles), ex64lo(pp->p_cycles), pp->p_cpu, pp->p_seg.p_cr3, rtsflagstr(pp->p_rts_flags), miscflagstr(pp->p_misc_flags), schedulerstr(pp->p_scheduler)); print_sigmgr(pp); dep = P_BLOCKEDON(pp); if(dep != NONE) { printf(" blocked on: "); print_endpoint(dep); } printf("\n"); }
/*===========================================================================* * main * *===========================================================================*/ PUBLIC void main() { /* Start the ball rolling. */ struct boot_image *ip; /* boot image pointer */ register struct proc *rp; /* process pointer */ register struct priv *sp; /* privilege structure pointer */ register int i, s; int hdrindex; /* index to array of a.out headers */ phys_clicks text_base; vir_clicks text_clicks, data_clicks; 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); /* Clear the process table. Anounce each slot as empty and set up mappings * for proc_addr() and proc_nr() macros. Do the same for the table with * privilege structures for the system processes. */ for (rp = BEG_PROC_ADDR, i = -NR_TASKS; rp < END_PROC_ADDR; ++rp, ++i) { rp->p_rts_flags = SLOT_FREE; /* initialize free slot */ rp->p_nr = i; /* proc number from ptr */ (pproc_addr + NR_TASKS)[i] = rp; /* proc ptr from number */ } 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 */ } /* Set up proc table entries for processes in boot image. 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; for (i=0; i < NR_BOOT_PROCS; ++i) { ip = &image[i]; /* process' attributes */ rp = proc_addr(ip->proc_nr); /* get process pointer */ 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 */ rp->p_ticks_left = ip->quantum; /* current credit */ strncpy(rp->p_name, ip->proc_name, P_NAME_LEN); /* set process name */ (void) get_priv(rp, (ip->flags & SYS_PROC)); /* assign structure */ priv(rp)->s_flags = ip->flags; /* process flags */ 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 */ if (iskerneln(proc_nr(rp))) { /* part of the kernel? */ if (ip->stksize > 0) { /* HARDWARE stack size is 0 */ rp->p_priv->s_stack_guard = (reg_t *) ktsb; *rp->p_priv->s_stack_guard = STACK_GUARD; } ktsb += ip->stksize; /* point to high end of stack */ rp->p_reg.sp = ktsb; /* this task's initial stack ptr */ text_base = kinfo.code_base >> CLICK_SHIFT; /* processes that are in the kernel */ hdrindex = 0; /* all use the first a.out header */ } else { hdrindex = 1 + i-NR_TASKS; /* servers, drivers, INIT */ } /* 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), (phys_bytes) A_MINHDR); /* 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; if (!(e_hdr.a_flags & A_SEP)) text_clicks = 0; /* common I&D */ 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 */ rp->p_reg.pc = (reg_t) ip->initial_pc; rp->p_reg.psw = (iskernelp(rp)) ? INIT_TASK_PSW : INIT_PSW; /* Initialize the server stack pointer. Take it down one word * to give crtso.s something to use as "argc". */ if (isusern(proc_nr(rp))) { /* user-space process? */ 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. */ 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 */ } /* Code and data segments must be allocated in protected mode. */ alloc_segments(rp); }
/*===========================================================================* * main * *===========================================================================*/ PUBLIC int main(void) { /* Start the ball rolling. */ struct boot_image *ip; /* boot image pointer */ register struct proc *rp; /* process pointer */ register int i, j; size_t argsz; /* size of arguments passed to crtso on stack */ BKL_LOCK(); /* Global value to test segment sanity. */ magictest = MAGICTEST; DEBUGEXTRA(("main()\n")); proc_init(); /* Set up proc table entries for processes in boot image. 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. */ for (i=0; i < NR_BOOT_PROCS; ++i) { int schedulable_proc; proc_nr_t proc_nr; int ipc_to_m, kcalls; sys_map_t map; ip = &image[i]; /* process' attributes */ DEBUGEXTRA(("initializing %s... ", ip->proc_name)); rp = proc_addr(ip->proc_nr); /* get process pointer */ ip->endpoint = rp->p_endpoint; /* ipc endpoint */ make_zero64(rp->p_cpu_time_left); strncpy(rp->p_name, ip->proc_name, P_NAME_LEN); /* set process name */ reset_proc_accounting(rp); /* See if this process is immediately schedulable. * In that case, set its privileges now and allow it to run. * Only kernel tasks and the root system process get to run immediately. * All the other system processes are inhibited from running by the * RTS_NO_PRIV flag. They can only be scheduled once the root system * process has set their privileges. */ proc_nr = proc_nr(rp); schedulable_proc = (iskerneln(proc_nr) || isrootsysn(proc_nr)); if(schedulable_proc) { /* Assign privilege structure. Force a static privilege id. */ (void) get_priv(rp, static_priv_id(proc_nr)); /* Priviliges for kernel tasks. */ if(iskerneln(proc_nr)) { /* Privilege flags. */ priv(rp)->s_flags = (proc_nr == IDLE ? IDL_F : TSK_F); /* Allowed traps. */ priv(rp)->s_trap_mask = (proc_nr == CLOCK || proc_nr == SYSTEM ? CSK_T : TSK_T); ipc_to_m = TSK_M; /* allowed targets */ kcalls = TSK_KC; /* allowed kernel calls */ } /* Priviliges for the root system process. */ else if(isrootsysn(proc_nr)) { priv(rp)->s_flags= RSYS_F; /* privilege flags */ priv(rp)->s_trap_mask= SRV_T; /* allowed traps */ ipc_to_m = SRV_M; /* allowed targets */ kcalls = SRV_KC; /* allowed kernel calls */ priv(rp)->s_sig_mgr = SRV_SM; /* signal manager */ rp->p_priority = SRV_Q; /* priority queue */ rp->p_quantum_size_ms = SRV_QT; /* quantum size */ } /* Priviliges for ordinary process. */ else { NOT_REACHABLE; } /* Fill in target mask. */ memset(&map, 0, sizeof(map)); if (ipc_to_m == ALL_M) { for(j = 0; j < NR_SYS_PROCS; j++) set_sys_bit(map, j); } fill_sendto_mask(rp, &map); /* Fill in kernel call mask. */ for(j = 0; j < SYS_CALL_MASK_SIZE; j++) { priv(rp)->s_k_call_mask[j] = (kcalls == NO_C ? 0 : (~0)); } } else { /* Don't let the process run for now. */ RTS_SET(rp, RTS_NO_PRIV | RTS_NO_QUANTUM); } rp->p_memmap[T].mem_vir = ABS2CLICK(ip->memmap.text_vaddr); rp->p_memmap[T].mem_phys = ABS2CLICK(ip->memmap.text_paddr); rp->p_memmap[T].mem_len = ABS2CLICK(ip->memmap.text_bytes); rp->p_memmap[D].mem_vir = ABS2CLICK(ip->memmap.data_vaddr); rp->p_memmap[D].mem_phys = ABS2CLICK(ip->memmap.data_paddr); rp->p_memmap[D].mem_len = ABS2CLICK(ip->memmap.data_bytes); rp->p_memmap[S].mem_phys = ABS2CLICK(ip->memmap.data_paddr + ip->memmap.data_bytes + ip->memmap.stack_bytes); rp->p_memmap[S].mem_vir = ABS2CLICK(ip->memmap.data_vaddr + ip->memmap.data_bytes + ip->memmap.stack_bytes); rp->p_memmap[S].mem_len = 0; /* 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 */ rp->p_reg.pc = ip->memmap.entry; rp->p_reg.psw = (iskerneln(proc_nr)) ? INIT_TASK_PSW : INIT_PSW; /* Initialize the server stack pointer. Take it down three words * to give crtso.s something to use as "argc", "argv" and "envp". */ if (isusern(proc_nr)) { /* user-space process? */ rp->p_reg.sp = (rp->p_memmap[S].mem_vir + rp->p_memmap[S].mem_len) << CLICK_SHIFT; argsz = 3 * sizeof(reg_t); rp->p_reg.sp -= argsz; phys_memset(rp->p_reg.sp - (rp->p_memmap[S].mem_vir << CLICK_SHIFT) + (rp->p_memmap[S].mem_phys << CLICK_SHIFT), 0, argsz); } /* scheduling functions depend on proc_ptr pointing somewhere. */ if(!get_cpulocal_var(proc_ptr)) get_cpulocal_var(proc_ptr) = rp; /* If this process has its own page table, VM will set the * PT up and manage it. VM will signal the kernel when it has * done this; until then, don't let it run. */ if(ip->flags & PROC_FULLVM) rp->p_rts_flags |= RTS_VMINHIBIT; rp->p_rts_flags |= RTS_PROC_STOP; rp->p_rts_flags &= ~RTS_SLOT_FREE; alloc_segments(rp); DEBUGEXTRA(("done\n")); } #define IPCNAME(n) { \ assert((n) >= 0 && (n) <= IPCNO_HIGHEST); \ assert(!ipc_call_names[n]); \ ipc_call_names[n] = #n; \ } IPCNAME(SEND); IPCNAME(RECEIVE); IPCNAME(SENDREC); IPCNAME(NOTIFY); IPCNAME(SENDNB); IPCNAME(SENDA); /* Architecture-dependent initialization. */ DEBUGEXTRA(("arch_init()... ")); arch_init(); DEBUGEXTRA(("done\n")); /* System and processes initialization */ DEBUGEXTRA(("system_init()... ")); system_init(); DEBUGEXTRA(("done\n")); #ifdef CONFIG_SMP if (config_no_apic) { BOOT_VERBOSE(printf("APIC disabled, disables SMP, using legacy PIC\n")); smp_single_cpu_fallback(); } else if (config_no_smp) { BOOT_VERBOSE(printf("SMP disabled, using legacy PIC\n")); smp_single_cpu_fallback(); } else { smp_init(); /* * if smp_init() returns it means that it failed and we try to finish * single CPU booting */ bsp_finish_booting(); } #else /* * if configured for a single CPU, we are already on the kernel stack which we * are going to use everytime we execute kernel code. We finish booting and we * never return here */ bsp_finish_booting(); #endif NOT_REACHABLE; return 1; }
/*===========================================================================* * exception * *===========================================================================*/ PUBLIC void exception_handler(int is_nested, struct exception_frame * frame) { /* An exception or unexpected interrupt has occurred. */ register struct ex_s *ep; struct proc *saved_proc; /* Save proc_ptr, because it may be changed by debug statements. */ saved_proc = get_cpulocal_var(proc_ptr); ep = &ex_data[frame->vector]; if (frame->vector == 2) { /* spurious NMI on some machines */ printf("got spurious NMI\n"); return; } /* * handle special cases for nested problems as they might be tricky or filter * them out quickly if the traps are not nested */ if (is_nested) { /* * if a problem occured while copying a message from userspace because * of a wrong pointer supplied by userland, handle it the only way we * can handle it ... */ if (((void*)frame->eip >= (void*)copy_msg_to_user && (void*)frame->eip <= (void*)__copy_msg_to_user_end) || ((void*)frame->eip >= (void*)copy_msg_from_user && (void*)frame->eip <= (void*)__copy_msg_from_user_end)) { switch(frame->vector) { /* these error are expected */ case PAGE_FAULT_VECTOR: case PROTECTION_VECTOR: frame->eip = (reg_t) __user_copy_msg_pointer_failure; return; default: panic("Copy involving a user pointer failed unexpectedly!"); } } /* Pass any error resulting from restoring FPU state, as a FPU * exception to the process. */ if (((void*)frame->eip >= (void*)fxrstor && (void *)frame->eip <= (void*)__fxrstor_end) || ((void*)frame->eip >= (void*)frstor && (void *)frame->eip <= (void*)__frstor_end)) { frame->eip = (reg_t) __frstor_failure; return; } } if(frame->vector == PAGE_FAULT_VECTOR) { pagefault(saved_proc, frame, is_nested); return; } /* If an exception occurs while running a process, the is_nested variable * will be zero. Exceptions in interrupt handlers or system traps will make * is_nested non-zero. */ if (is_nested == 0 && ! iskernelp(saved_proc)) { #if 0 { printf( "vec_nr= %d, trap_errno= 0x%lx, eip= 0x%lx, cs= 0x%x, eflags= 0x%lx\n", frame->vector, (unsigned long)frame->errcode, (unsigned long)frame->eip, frame->cs, (unsigned long)frame->eflags); printseg("cs: ", 1, saved_proc, frame->cs); printseg("ds: ", 0, saved_proc, saved_proc->p_reg.ds); if(saved_proc->p_reg.ds != saved_proc->p_reg.ss) { printseg("ss: ", 0, saved_proc, saved_proc->p_reg.ss); } proc_stacktrace(saved_proc); } #endif cause_sig(proc_nr(saved_proc), ep->signum); return; } /* Exception in system code. This is not supposed to happen. */ inkernel_disaster(saved_proc, frame, ep, is_nested); panic("return from inkernel_disaster"); }
/*===========================================================================* * exception * *===========================================================================*/ PUBLIC void exception_handler(int is_nested, struct exception_frame * frame) { /* An exception or unexpected interrupt has occurred. */ struct ex_s { char *msg; int signum; int minprocessor; }; static struct ex_s ex_data[] = { { "Divide error", SIGFPE, 86 }, { "Debug exception", SIGTRAP, 86 }, { "Nonmaskable interrupt", SIGBUS, 86 }, { "Breakpoint", SIGEMT, 86 }, { "Overflow", SIGFPE, 86 }, { "Bounds check", SIGFPE, 186 }, { "Invalid opcode", SIGILL, 186 }, { "Coprocessor not available", SIGFPE, 186 }, { "Double fault", SIGBUS, 286 }, { "Coprocessor segment overrun", SIGSEGV, 286 }, { "Invalid TSS", SIGSEGV, 286 }, { "Segment not present", SIGSEGV, 286 }, { "Stack exception", SIGSEGV, 286 }, /* STACK_FAULT already used */ { "General protection", SIGSEGV, 286 }, { "Page fault", SIGSEGV, 386 }, /* not close */ { NULL, SIGILL, 0 }, /* probably software trap */ { "Coprocessor error", SIGFPE, 386 }, { "Alignment check", SIGBUS, 386 }, { "Machine check", SIGBUS, 386 }, { "SIMD exception", SIGFPE, 386 }, }; register struct ex_s *ep; struct proc *saved_proc; /* Save proc_ptr, because it may be changed by debug statements. */ saved_proc = proc_ptr; ep = &ex_data[frame->vector]; if (frame->vector == 2) { /* spurious NMI on some machines */ printf("got spurious NMI\n"); return; } /* * handle special cases for nested problems as they might be tricky or filter * them out quickly if the traps are not nested */ if (is_nested) { /* * if a problem occured while copying a message from userspace because * of a wrong pointer supplied by userland, handle it the only way we * can handle it ... */ if (((void*)frame->eip >= (void*)copy_msg_to_user && (void*)frame->eip <= (void*)__copy_msg_to_user_end) || ((void*)frame->eip >= (void*)copy_msg_from_user && (void*)frame->eip <= (void*)__copy_msg_from_user_end)) { switch(frame->vector) { /* these error are expected */ case PAGE_FAULT_VECTOR: case PROTECTION_VECTOR: frame->eip = (reg_t) __user_copy_msg_pointer_failure; return; default: panic("Copy involving a user pointer failed unexpectedly!"); } } } if(frame->vector == PAGE_FAULT_VECTOR) { pagefault(saved_proc, frame, is_nested); return; } /* If an exception occurs while running a process, the is_nested variable * will be zero. Exceptions in interrupt handlers or system traps will make * is_nested non-zero. */ if (is_nested == 0 && ! iskernelp(saved_proc)) { #if 0 { printf( "vec_nr= %d, trap_errno= 0x%lx, eip= 0x%lx, cs= 0x%x, eflags= 0x%lx\n", frame->vector, (unsigned long)frame->errcode, (unsigned long)frame->eip, frame->cs, (unsigned long)frame->eflags); printseg("cs: ", 1, saved_proc, frame->cs); printseg("ds: ", 0, saved_proc, saved_proc->p_reg.ds); if(saved_proc->p_reg.ds != saved_proc->p_reg.ss) { printseg("ss: ", 0, saved_proc, saved_proc->p_reg.ss); } proc_stacktrace(saved_proc); } #endif cause_sig(proc_nr(saved_proc), ep->signum); return; } /* Exception in system code. This is not supposed to happen. */ if (ep->msg == NULL || machine.processor < ep->minprocessor) printf("\nIntel-reserved exception %d\n", frame->vector); else printf("\n%s\n", ep->msg); printf("is_nested = %d ", is_nested); printf("vec_nr= %d, trap_errno= 0x%x, eip= 0x%x, " "cs= 0x%x, eflags= 0x%x trap_esp 0x%08x\n", frame->vector, frame->errcode, frame->eip, frame->cs, frame->eflags, frame); printf("KERNEL registers :\n"); printf( "\t%%eax 0x%08x %%ebx 0x%08x %%ecx 0x%08x %%edx 0x%08x\n" "\t%%esp 0x%08x %%ebp 0x%08x %%esi 0x%08x %%edi 0x%08x\n", ((u32_t *)frame)[-1], ((u32_t *)frame)[-2], ((u32_t *)frame)[-3], ((u32_t *)frame)[-4], ((u32_t *)frame)[-5], ((u32_t *)frame)[-6], ((u32_t *)frame)[-7], ((u32_t *)frame)[-8] ); printseg("ker cs: ", 1, NULL, frame->cs); printseg("ker ds: ", 0, NULL, DS_SELECTOR); /* TODO should we enable this only when compiled for some debug mode? */ if (saved_proc) { printf("scheduled was: process %d (%s), ", proc_nr(saved_proc), saved_proc->p_name); printf("pc = %u:0x%x\n", (unsigned) saved_proc->p_reg.cs, (unsigned) saved_proc->p_reg.pc); proc_stacktrace(saved_proc); panic("Unhandled kernel exception"); } else { /* in an early stage of boot process we don't have processes yet */ panic("exception in kernel while booting"); } }
PRIVATE void pagefault( struct proc *pr, struct exception_frame * frame, int is_nested) { int in_physcopy = 0; reg_t pagefaultcr2; message m_pagefault; int err; pagefaultcr2 = read_cr2(); #if 0 printf("kernel: pagefault in pr %d, addr 0x%lx, his cr3 0x%lx, actual cr3 0x%lx\n", pr->p_endpoint, pagefaultcr2, pr->p_seg.p_cr3, read_cr3()); #endif in_physcopy = (frame->eip > (vir_bytes) phys_copy) && (frame->eip < (vir_bytes) phys_copy_fault); if((is_nested || iskernelp(pr)) && catch_pagefaults && in_physcopy) { #if 0 printf("pf caught! addr 0x%lx\n", pagefaultcr2); #endif if (is_nested) { frame->eip = (reg_t) phys_copy_fault_in_kernel; } else { pr->p_reg.pc = (reg_t) phys_copy_fault; pr->p_reg.retreg = pagefaultcr2; } return; } if(is_nested) { printf("pagefault in kernel at pc 0x%lx address 0x%lx\n", frame->eip, pagefaultcr2); inkernel_disaster(pr, frame, NULL, is_nested); } /* System processes that don't have their own page table can't * have page faults. VM does have its own page table but also * can't have page faults (because VM has to handle them). */ if((pr->p_endpoint <= INIT_PROC_NR && !(pr->p_misc_flags & MF_FULLVM)) || pr->p_endpoint == VM_PROC_NR) { /* Page fault we can't / don't want to * handle. */ printf("pagefault for process %d ('%s') on CPU %d, " "pc = 0x%x, addr = 0x%x, flags = 0x%x, is_nested %d\n", pr->p_endpoint, pr->p_name, cpuid, pr->p_reg.pc, pagefaultcr2, frame->errcode, is_nested); if(!is_nested) { printf("process vir addr of pagefault is 0x%lx\n", pagefaultcr2 - (pr->p_memmap[D].mem_phys << CLICK_SHIFT)); } proc_stacktrace(pr); printf("pc of pagefault: 0x%lx\n", frame->eip); cause_sig(proc_nr(pr), SIGSEGV); return; } /* Don't schedule this process until pagefault is handled. */ RTS_SET(pr, RTS_PAGEFAULT); /* tell Vm about the pagefault */ m_pagefault.m_source = pr->p_endpoint; m_pagefault.m_type = VM_PAGEFAULT; m_pagefault.VPF_ADDR = pagefaultcr2; m_pagefault.VPF_FLAGS = frame->errcode; if ((err = mini_send(pr, VM_PROC_NR, &m_pagefault, FROM_KERNEL))) { panic("WARNING: pagefault: mini_send returned %d\n", err); } return; }
/*===========================================================================* * kmain * *===========================================================================*/ void kmain(kinfo_t *local_cbi) { /* Start the ball rolling. */ struct boot_image *ip; /* boot image pointer */ register struct proc *rp; /* process pointer */ register int i, j; /* save a global copy of the boot parameters */ memcpy(&kinfo, local_cbi, sizeof(kinfo)); memcpy(&kmess, kinfo.kmess, sizeof(kmess)); #ifdef __arm__ /* We want to initialize serial before we do any output */ omap3_ser_init(); #endif /* We can talk now */ printf("MINIX booting\n"); /* Kernel may use bits of main memory before VM is started */ kernel_may_alloc = 1; assert(sizeof(kinfo.boot_procs) == sizeof(image)); memcpy(kinfo.boot_procs, image, sizeof(kinfo.boot_procs)); cstart(); BKL_LOCK(); DEBUGEXTRA(("main()\n")); proc_init(); if(NR_BOOT_MODULES != kinfo.mbi.mods_count) panic("expecting %d boot processes/modules, found %d", NR_BOOT_MODULES, kinfo.mbi.mods_count); /* Set up proc table entries for processes in boot image. */ for (i=0; i < NR_BOOT_PROCS; ++i) { int schedulable_proc; proc_nr_t proc_nr; int ipc_to_m, kcalls; sys_map_t map; ip = &image[i]; /* process' attributes */ DEBUGEXTRA(("initializing %s... ", ip->proc_name)); rp = proc_addr(ip->proc_nr); /* get process pointer */ ip->endpoint = rp->p_endpoint; /* ipc endpoint */ make_zero64(rp->p_cpu_time_left); if(i < NR_TASKS) /* name (tasks only) */ strlcpy(rp->p_name, ip->proc_name, sizeof(rp->p_name)); if(i >= NR_TASKS) { /* Remember this so it can be passed to VM */ multiboot_module_t *mb_mod = &kinfo.module_list[i - NR_TASKS]; ip->start_addr = mb_mod->mod_start; ip->len = mb_mod->mod_end - mb_mod->mod_start; } reset_proc_accounting(rp); /* See if this process is immediately schedulable. * In that case, set its privileges now and allow it to run. * Only kernel tasks and the root system process get to run immediately. * All the other system processes are inhibited from running by the * RTS_NO_PRIV flag. They can only be scheduled once the root system * process has set their privileges. */ proc_nr = proc_nr(rp); schedulable_proc = (iskerneln(proc_nr) || isrootsysn(proc_nr) || proc_nr == VM_PROC_NR); if(schedulable_proc) { /* Assign privilege structure. Force a static privilege id. */ (void) get_priv(rp, static_priv_id(proc_nr)); /* Priviliges for kernel tasks. */ if(proc_nr == VM_PROC_NR) { priv(rp)->s_flags = VM_F; priv(rp)->s_trap_mask = SRV_T; ipc_to_m = SRV_M; kcalls = SRV_KC; priv(rp)->s_sig_mgr = SELF; rp->p_priority = SRV_Q; rp->p_quantum_size_ms = SRV_QT; } else if(iskerneln(proc_nr)) { /* Privilege flags. */ priv(rp)->s_flags = (proc_nr == IDLE ? IDL_F : TSK_F); /* Allowed traps. */ priv(rp)->s_trap_mask = (proc_nr == CLOCK || proc_nr == SYSTEM ? CSK_T : TSK_T); ipc_to_m = TSK_M; /* allowed targets */ kcalls = TSK_KC; /* allowed kernel calls */ } /* Priviliges for the root system process. */ else { assert(isrootsysn(proc_nr)); priv(rp)->s_flags= RSYS_F; /* privilege flags */ priv(rp)->s_trap_mask= SRV_T; /* allowed traps */ ipc_to_m = SRV_M; /* allowed targets */ kcalls = SRV_KC; /* allowed kernel calls */ priv(rp)->s_sig_mgr = SRV_SM; /* signal manager */ rp->p_priority = SRV_Q; /* priority queue */ rp->p_quantum_size_ms = SRV_QT; /* quantum size */ } /* Fill in target mask. */ memset(&map, 0, sizeof(map)); if (ipc_to_m == ALL_M) { for(j = 0; j < NR_SYS_PROCS; j++) set_sys_bit(map, j); } fill_sendto_mask(rp, &map); /* Fill in kernel call mask. */ for(j = 0; j < SYS_CALL_MASK_SIZE; j++) { priv(rp)->s_k_call_mask[j] = (kcalls == NO_C ? 0 : (~0)); } } else { /* Don't let the process run for now. */ RTS_SET(rp, RTS_NO_PRIV | RTS_NO_QUANTUM); } /* Arch-specific state initialization. */ arch_boot_proc(ip, rp); /* scheduling functions depend on proc_ptr pointing somewhere. */ if(!get_cpulocal_var(proc_ptr)) get_cpulocal_var(proc_ptr) = rp; /* Process isn't scheduled until VM has set up a pagetable for it. */ if(rp->p_nr != VM_PROC_NR && rp->p_nr >= 0) { rp->p_rts_flags |= RTS_VMINHIBIT; rp->p_rts_flags |= RTS_BOOTINHIBIT; } rp->p_rts_flags |= RTS_PROC_STOP; rp->p_rts_flags &= ~RTS_SLOT_FREE; DEBUGEXTRA(("done\n")); } /* update boot procs info for VM */ memcpy(kinfo.boot_procs, image, sizeof(kinfo.boot_procs)); #define IPCNAME(n) { \ assert((n) >= 0 && (n) <= IPCNO_HIGHEST); \ assert(!ipc_call_names[n]); \ ipc_call_names[n] = #n; \ } arch_post_init(); IPCNAME(SEND); IPCNAME(RECEIVE); IPCNAME(SENDREC); IPCNAME(NOTIFY); IPCNAME(SENDNB); IPCNAME(SENDA); /* System and processes initialization */ memory_init(); DEBUGEXTRA(("system_init()... ")); system_init(); DEBUGEXTRA(("done\n")); /* The bootstrap phase is over, so we can add the physical * memory used for it to the free list. */ add_memmap(&kinfo, kinfo.bootstrap_start, kinfo.bootstrap_len); #ifdef CONFIG_SMP if (config_no_apic) { BOOT_VERBOSE(printf("APIC disabled, disables SMP, using legacy PIC\n")); smp_single_cpu_fallback(); } else if (config_no_smp) { BOOT_VERBOSE(printf("SMP disabled, using legacy PIC\n")); smp_single_cpu_fallback(); } else { smp_init(); /* * if smp_init() returns it means that it failed and we try to finish * single CPU booting */ bsp_finish_booting(); } #else /* * if configured for a single CPU, we are already on the kernel stack which we * are going to use everytime we execute kernel code. We finish booting and we * never return here */ bsp_finish_booting(); #endif NOT_REACHABLE; }
/*===========================================================================* * main * *===========================================================================*/ PUBLIC void main() { /* Start the ball rolling. */ struct boot_image *ip; /* boot image pointer */ register struct proc *rp; /* process pointer */ register struct priv *sp; /* privilege structure pointer */ register int i, s; // a.out 头部数组的索引. int hdrindex; /* index to array of a.out headers */ phys_clicks text_base; vir_clicks text_clicks, data_clicks; // 内核任务栈的基地址(低端) reg_t ktsb; /* kernel task stack base */ // 用来放置 a.out 头部的一个副本. struct exec e_hdr; /* for a copy of an a.out header */ /* Initialize the interrupt controller. */ // 初始化 8259 中断控制器芯片. intr_init(1); /* Clear the process table. Anounce each slot as empty and set up mappings * for proc_addr() and proc_nr() macros. Do the same for the table with * privilege structures for the system processes. */ // 初如化进程表与进程指针表. // BEG_PROC_ADDR: 进程表地址; for (rp = BEG_PROC_ADDR, i = -NR_TASKS; rp < END_PROC_ADDR; ++rp, ++i) { // 将进程表中每一项都设置为空闲. rp->p_rts_flags = SLOT_FREE; /* initialize free slot */ // 进程号, i 的初值为 -NR_TASKS, 可见系统任务拥有负的进程号 rp->p_nr = i; /* proc number from ptr */ // 建立进程数组与进程指针数组之间的映射关系 (pproc_addr + NR_TASKS)[i] = rp; /* proc ptr from number */ } // 初始化优先级表 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 */ } /* 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. */ /* * 为任务和服务进程设置进程表项. 内核任务的栈被初始化成一个在数据空间中的 * 数组. 服务进程的栈已经由控制器添加到数据段中, 所有它们的栈指针开始时 * 指向数据段的末尾. 所有的进程都在 8086 的低内存. 对于 386, 只有内核在 * 低内存, 剩下的都在扩展内存中. */ /* Task stacks. */ /* 任务栈 */ ktsb = (reg_t) t_stack; // 为那些包含在系统引导映像文件中的程序分配进程表项. for (i=0; i < NR_BOOT_PROCS; ++i) { ip = &image[i]; /* process' attributes */ // 获取进程指针 rp = proc_addr(ip->proc_nr); /* get process pointer */ // 最大调度优先级 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 */ // 剩余时间片 rp->p_ticks_left = ip->quantum; /* current credit */ // 将程序名复制到进程表项中 strncpy(rp->p_name, ip->proc_name, P_NAME_LEN); /* set process name */ // 为进程分配一个特权级结构体, 即 从系统特权级表中分配一项 (void) get_priv(rp, (ip->flags & SYS_PROC)); /* assign structure */ // 初始化特权级结构体的标志. priv(rp)->s_flags = ip->flags; /* process flags */ // 初始化特权级结构体的 允许的系统调用陷井 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 */ // 如果进程是内核任务 if (iskerneln(proc_nr(rp))) { /* part of the kernel? */ // 如果进程的栈大小大于 0, 设置进程的栈警戒字, if (ip->stksize > 0) { /* HARDWARE stack size is 0 */ // 设置内核任务栈警戒字指针. rp->p_priv->s_stack_guard = (reg_t *) ktsb; // 指针运算符(->) 要比取值运行符 (*) 的优先级要高. // 等价于: // *(rp->p_priv->s_stack_guard) = STACK_GUARD // 效果是在栈的最顶端(在低地址)放置一个特殊值, // 这个值就是栈警戒字. *rp->p_priv->s_stack_guard = STACK_GUARD; } ktsb += ip->stksize; /* point to high end of stack */ // 初始进程的栈指针 rp->p_reg.sp = ktsb; /* this task's initial stack ptr */ // kinfo ??? // 内核代码的基地址右移 CLICK_SHIFT 位, 赋给 text_base. text_base = kinfo.code_base >> CLICK_SHIFT; /* processes that are in the kernel */ // 内核任务使用同一个 a.out 头部信息 hdrindex = 0; /* all use the first a.out header */ } else { // 非内核任务, 计算它的 a.out 头部数组索引, 因为 0 号项 // 留给了内核任务, 所以需 加 1. hdrindex = 1 + i-NR_TASKS; /* servers, drivers, INIT */ } /* The bootstrap loader created an array of the a.out headers at * absolute address 'aout'. Get one element to e_hdr. */ /* * 引导加载程序会在绝对地址 'aout' 处放置一个 a.out 头部数组. * 从中取一项复制到 e_hdr. */ phys_copy(aout + hdrindex * A_MINHDR, vir2phys(&e_hdr), (phys_bytes) A_MINHDR); /* Convert addresses to clicks and build process memory map */ /* 将地址转换为以 click 为单位, 并建立进程内存映射 */ // 既然这里要设置 text_base, 那 146 行附近的 // text_base = kinfo.code_base >> CLICK_SHIFT; // 岂不是多余的?? // 将 a.out 头部的符号表大小右移 CLICK_SHIFT 位,赋给 text_base. text_base = e_hdr.a_syms >> CLICK_SHIFT; // 计算程序文本段大小, 以 click 为单位, 上取整. text_clicks = (e_hdr.a_text + CLICK_SIZE-1) >> CLICK_SHIFT; // 如果 a.out 头部指明它的 I/D 是合并的 ??? if (!(e_hdr.a_flags & A_SEP)) text_clicks = 0; /* common I&D */ // 计算程序占用的内存量, 以 click 为单位, 上取整. 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 */ /* * 设置寄存器的初始值. 与其他进程相比, 内核任务的处理器状态字 * 稍有不同, 因为内核任务可以访问 I/O; 而对于非特权进来来说, 这是不 * 允许的. */ // 初始化进程的 PC 和 processor status word. rp->p_reg.pc = (reg_t) ip->initial_pc; rp->p_reg.psw = (iskernelp(rp)) ? INIT_TASK_PSW : INIT_PSW; /* Initialize the server stack pointer. Take it down one word * to give crtso.s something to use as "argc". */ /* * 初始化服务器进程的栈指针. 下移一个字的空间, 使得 crtso.s 有 * 空间放置 "argc". */ if (isusern(proc_nr(rp))) { /* user-space process? */ 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. */ if (rp->p_nr != HARDWARE) { // 如果进程不是 HARDWARE, 清空进程标志, 并加入调度队列. rp->p_rts_flags = 0; /* runnable if no flags */ lock_enqueue(rp); /* add to scheduling queues */ } else { // 对于 HARDWARE 任务, 则阻止其运行. ??? rp->p_rts_flags = NO_MAP; /* prevent from running */ } /* Code and data segments must be allocated in protected mode. */ /* 数据与代码段必须在保护模式下分配 */ alloc_segments(rp); }
/*===========================================================================* * main * *===========================================================================*/ PUBLIC void main() { /* Start the ball rolling. */ struct boot_image *ip; /* boot image pointer */ register struct proc *rp; /* process pointer */ register struct priv *sp; /* privilege structure pointer */ register int i, j, s; int hdrindex; /* index to array of a.out headers */ phys_clicks text_base; vir_clicks text_clicks, data_clicks, st_clicks; reg_t ktsb; /* kernel task stack base */ struct exec e_hdr; /* for a copy of an a.out header */ /* Architecture-dependent initialization. */ arch_init(); /* Global value to test segment sanity. */ magictest = MAGICTEST; /* Clear the process table. Anounce each slot as empty and set up mappings * for proc_addr() and proc_nr() macros. Do the same for the table with * privilege structures for the system processes. */ for (rp = BEG_PROC_ADDR, i = -NR_TASKS; rp < END_PROC_ADDR; ++rp, ++i) { rp->p_rts_flags = SLOT_FREE; /* initialize free slot */ #if DEBUG_SCHED_CHECK rp->p_magic = PMAGIC; #endif rp->p_nr = i; /* proc number from ptr */ rp->p_endpoint = _ENDPOINT(0, rp->p_nr); /* generation no. 0 */ } 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 */ } /* Set up proc table entries for processes in boot image. 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; for (i=0; i < NR_BOOT_PROCS; ++i) { int ci; bitchunk_t fv; ip = &image[i]; /* process' attributes */ rp = proc_addr(ip->proc_nr); /* get process pointer */ ip->endpoint = rp->p_endpoint; /* ipc endpoint */ 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 */ rp->p_ticks_left = ip->quantum; /* current credit */ strncpy(rp->p_name, ip->proc_name, P_NAME_LEN); /* set process name */ (void) get_priv(rp, (ip->flags & SYS_PROC)); /* assign structure */ priv(rp)->s_flags = ip->flags; /* process flags */ priv(rp)->s_trap_mask = ip->trap_mask; /* allowed traps */ /* Warn about violations of the boot image table order consistency. */ if (priv_id(rp) != s_nr_to_id(ip->proc_nr) && (ip->flags & SYS_PROC)) kprintf("Warning: boot image table has wrong process order\n"); /* Initialize call mask bitmap from unordered set. * A single SYS_ALL_CALLS is a special case - it * means all calls are allowed. */ if(ip->nr_k_calls == 1 && ip->k_calls[0] == SYS_ALL_CALLS) fv = ~0; /* fill call mask */ else fv = 0; /* clear call mask */ for(ci = 0; ci < CALL_MASK_SIZE; ci++) /* fill or clear call mask */ priv(rp)->s_k_call_mask[ci] = fv; if(!fv) /* not all full? enter calls bit by bit */ for(ci = 0; ci < ip->nr_k_calls; ci++) SET_BIT(priv(rp)->s_k_call_mask, ip->k_calls[ci]-KERNEL_CALL); for (j = 0; j < NR_SYS_PROCS && j < BITCHUNK_BITS; j++) if (ip->ipc_to & (1 << j)) set_sendto_bit(rp, j); /* restrict targets */ if (iskerneln(proc_nr(rp))) { /* part of the kernel? */ if (ip->stksize > 0) { /* HARDWARE stack size is 0 */ rp->p_priv->s_stack_guard = (reg_t *) ktsb; *rp->p_priv->s_stack_guard = STACK_GUARD; } ktsb += ip->stksize; /* point to high end of stack */ rp->p_reg.sp = ktsb; /* this task's initial stack ptr */ hdrindex = 0; /* all use the first a.out header */ } else { hdrindex = 1 + i-NR_TASKS; /* servers, drivers, INIT */ } /* Architecture-specific way to find out aout header of this * boot process. */ arch_get_aout_headers(hdrindex, &e_hdr); /* 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; data_clicks = (e_hdr.a_data+e_hdr.a_bss + CLICK_SIZE-1) >> CLICK_SHIFT; st_clicks= (e_hdr.a_total + CLICK_SIZE-1) >> CLICK_SHIFT; if (!(e_hdr.a_flags & A_SEP)) { data_clicks= (e_hdr.a_text+e_hdr.a_data+e_hdr.a_bss + CLICK_SIZE-1) >> CLICK_SHIFT; text_clicks = 0; /* common I&D */ } 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 + st_clicks; rp->p_memmap[S].mem_vir = st_clicks; rp->p_memmap[S].mem_len = 0; /* 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 */ rp->p_reg.pc = (reg_t) ip->initial_pc; rp->p_reg.psw = (iskernelp(rp)) ? INIT_TASK_PSW : INIT_PSW; /* Initialize the server stack pointer. Take it down one word * to give crtso.s something to use as "argc". */ if (isusern(proc_nr(rp))) { /* user-space process? */ 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); } /* scheduling functions depend on proc_ptr pointing somewhere. */ if(!proc_ptr) proc_ptr = rp; /* If this process has its own page table, VM will set the * PT up and manage it. VM will signal the kernel when it has * done this; until then, don't let it run. */ if(priv(rp)->s_flags & PROC_FULLVM) RTS_SET(rp, VMINHIBIT); /* Set ready. The HARDWARE task is never ready. */ if (rp->p_nr == HARDWARE) RTS_SET(rp, PROC_STOP); RTS_UNSET(rp, SLOT_FREE); /* remove SLOT_FREE and schedule */ alloc_segments(rp); }