50e2064049
This commit removes all traces of Minix segments (the text/data/stack memory map abstraction in the kernel) and significance of Intel segments (hardware segments like CS, DS that add offsets to all addressing before page table translation). This ultimately simplifies the memory layout and addressing and makes the same layout possible on non-Intel architectures. There are only two types of addresses in the world now: virtual and physical; even the kernel and processes have the same virtual address space. Kernel and user processes can be distinguished at a glance as processes won't use 0xF0000000 and above. No static pre-allocated memory sizes exist any more. Changes to booting: . The pre_init.c leaves the kernel and modules exactly as they were left by the bootloader in physical memory . The kernel starts running using physical addressing, loaded at a fixed location given in its linker script by the bootloader. All code and data in this phase are linked to this fixed low location. . It makes a bootstrap pagetable to map itself to a fixed high location (also in linker script) and jumps to the high address. All code and data then use this high addressing. . All code/data symbols linked at the low addresses is prefixed by an objcopy step with __k_unpaged_*, so that that code cannot reference highly-linked symbols (which aren't valid yet) or vice versa (symbols that aren't valid any more). . The two addressing modes are separated in the linker script by collecting the unpaged_*.o objects and linking them with low addresses, and linking the rest high. Some objects are linked twice, once low and once high. . The bootstrap phase passes a lot of information (e.g. free memory list, physical location of the modules, etc.) using the kinfo struct. . After this bootstrap the low-linked part is freed. . The kernel maps in VM into the bootstrap page table so that VM can begin executing. Its first job is to make page tables for all other boot processes. So VM runs before RS, and RS gets a fully dynamic, VM-managed address space. VM gets its privilege info from RS as usual but that happens after RS starts running. . Both the kernel loading VM and VM organizing boot processes happen using the libexec logic. This removes the last reason for VM to still know much about exec() and vm/exec.c is gone. Further Implementation: . All segments are based at 0 and have a 4 GB limit. . The kernel is mapped in at the top of the virtual address space so as not to constrain the user processes. . Processes do not use segments from the LDT at all; there are no segments in the LDT any more, so no LLDT is needed. . The Minix segments T/D/S are gone and so none of the user-space or in-kernel copy functions use them. The copy functions use a process endpoint of NONE to realize it's a physical address, virtual otherwise. . The umap call only makes sense to translate a virtual address to a physical address now. . Segments-related calls like newmap and alloc_segments are gone. . All segments-related translation in VM is gone (vir2map etc). . Initialization in VM is simpler as no moving around is necessary. . VM and all other boot processes can be linked wherever they wish and will be mapped in at the right location by the kernel and VM respectively. Other changes: . The multiboot code is less special: it does not use mb_print for its diagnostics any more but uses printf() as normal, saving the output into the diagnostics buffer, only printing to the screen using the direct print functions if a panic() occurs. . The multiboot code uses the flexible 'free memory map list' style to receive the list of free memory if available. . The kernel determines the memory layout of the processes to a degree: it tells VM where the kernel starts and ends and where the kernel wants the top of the process to be. VM then uses this entire range, i.e. the stack is right at the top, and mmap()ped bits of memory are placed below that downwards, and the break grows upwards. Other Consequences: . Every process gets its own page table as address spaces can't be separated any more by segments. . As all segments are 0-based, there is no distinction between virtual and linear addresses, nor between userspace and kernel addresses. . Less work is done when context switching, leading to a net performance increase. (8% faster on my machine for 'make servers'.) . The layout and configuration of the GDT makes sysenter and syscall possible.
338 lines
9.2 KiB
C
338 lines
9.2 KiB
C
/* This file contains a simple exception handler. Exceptions in user
|
|
* processes are converted to signals. Exceptions in a kernel task cause
|
|
* a panic.
|
|
*/
|
|
|
|
#include "kernel/kernel.h"
|
|
#include "arch_proto.h"
|
|
#include <signal.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#include "kernel/proc.h"
|
|
#include "kernel/proto.h"
|
|
#include <machine/vm.h>
|
|
|
|
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 },
|
|
};
|
|
|
|
static void inkernel_disaster(struct proc *saved_proc,
|
|
struct exception_frame *frame, struct ex_s *ep, int is_nested);
|
|
|
|
extern int catch_pagefaults;
|
|
|
|
static void proc_stacktrace_execute(struct proc *whichproc, reg_t v_bp, reg_t pc);
|
|
|
|
static void pagefault( struct proc *pr,
|
|
struct exception_frame * frame,
|
|
int is_nested)
|
|
{
|
|
int in_physcopy = 0, in_memset = 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);
|
|
|
|
in_memset = (frame->eip > (vir_bytes) phys_memset) &&
|
|
(frame->eip < (vir_bytes) memset_fault);
|
|
|
|
if((is_nested || iskernelp(pr)) &&
|
|
catch_pagefaults && (in_physcopy || in_memset)) {
|
|
#if 0
|
|
printf("pf caught! addr 0x%lx\n", pagefaultcr2);
|
|
#endif
|
|
if (is_nested) {
|
|
if(in_physcopy) {
|
|
assert(!in_memset);
|
|
frame->eip = (reg_t) phys_copy_fault_in_kernel;
|
|
} else {
|
|
frame->eip = (reg_t) memset_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);
|
|
}
|
|
|
|
/* VM can't handle page faults. */
|
|
if(pr->p_endpoint == VM_PROC_NR) {
|
|
/* Page fault we can't / don't want to
|
|
* handle.
|
|
*/
|
|
printf("pagefault for VM on CPU %d, "
|
|
"pc = 0x%x, addr = 0x%x, flags = 0x%x, is_nested %d\n",
|
|
cpuid, pr->p_reg.pc, pagefaultcr2, frame->errcode,
|
|
is_nested);
|
|
proc_stacktrace(pr);
|
|
printf("pc of pagefault: 0x%lx\n", frame->eip);
|
|
panic("pagefault in VM");
|
|
|
|
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;
|
|
}
|
|
|
|
static void inkernel_disaster(struct proc *saved_proc,
|
|
struct exception_frame * frame, struct ex_s *ep,
|
|
int is_nested)
|
|
{
|
|
#if USE_SYSDEBUG
|
|
if(ep) {
|
|
if (ep->msg == NULL)
|
|
printf("\nIntel-reserved exception %d\n", frame->vector);
|
|
else
|
|
printf("\n%s\n", ep->msg);
|
|
}
|
|
|
|
printf("cpu %d is_nested = %d ", cpuid, 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");
|
|
#define REG(n) (((u32_t *)frame)[-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",
|
|
REG(1), REG(2), REG(3), REG(4),
|
|
REG(5), REG(6), REG(7), REG(8));
|
|
|
|
{
|
|
reg_t k_ebp = REG(6);
|
|
printf("KERNEL stacktrace, starting with ebp = 0x%lx:\n", k_ebp);
|
|
proc_stacktrace_execute(proc_addr(SYSTEM), k_ebp, frame->eip);
|
|
}
|
|
|
|
if (saved_proc) {
|
|
printf("scheduled was: process %d (%s), ", saved_proc->p_endpoint, saved_proc->p_name);
|
|
printf("pc = 0x%x\n", (unsigned) saved_proc->p_reg.pc);
|
|
proc_stacktrace(saved_proc);
|
|
|
|
panic("Unhandled kernel exception");
|
|
}
|
|
|
|
/* in an early stage of boot process we don't have processes yet */
|
|
panic("exception in kernel while booting, no saved_proc yet");
|
|
#endif /* USE_SYSDEBUG */
|
|
}
|
|
|
|
/*===========================================================================*
|
|
* exception *
|
|
*===========================================================================*/
|
|
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");
|
|
}
|
|
|
|
#if USE_SYSDEBUG
|
|
/*===========================================================================*
|
|
* proc_stacktrace_execute *
|
|
*===========================================================================*/
|
|
static void proc_stacktrace_execute(struct proc *whichproc, reg_t v_bp, reg_t pc)
|
|
{
|
|
reg_t v_hbp;
|
|
int iskernel;
|
|
int n = 0;
|
|
|
|
iskernel = iskernelp(whichproc);
|
|
|
|
printf("%-8.8s %6d 0x%lx ",
|
|
whichproc->p_name, whichproc->p_endpoint, pc);
|
|
|
|
while(v_bp) {
|
|
reg_t v_pc;
|
|
|
|
#define PRCOPY(pr, pv, v, n) \
|
|
(iskernel ? (memcpy((char *) v, (char *) pv, n), OK) : \
|
|
data_copy(pr->p_endpoint, pv, KERNEL, (vir_bytes) (v), n))
|
|
|
|
if(PRCOPY(whichproc, v_bp, &v_hbp, sizeof(v_hbp)) != OK) {
|
|
printf("(v_bp 0x%lx ?)", v_bp);
|
|
break;
|
|
}
|
|
if(PRCOPY(whichproc, v_bp + sizeof(v_pc), &v_pc, sizeof(v_pc)) != OK) {
|
|
printf("(v_pc 0x%lx ?)", v_bp + sizeof(v_pc));
|
|
break;
|
|
}
|
|
printf("0x%lx ", (unsigned long) v_pc);
|
|
if(v_hbp != 0 && v_hbp <= v_bp) {
|
|
printf("(hbp %lx ?)", v_hbp);
|
|
break;
|
|
}
|
|
v_bp = v_hbp;
|
|
if(n++ > 50) {
|
|
printf("(truncated after %d steps) ", n);
|
|
break;
|
|
}
|
|
}
|
|
printf("\n");
|
|
}
|
|
#endif /* USE_SYSDEBUG */
|
|
|
|
/*===========================================================================*
|
|
* proc_stacktrace *
|
|
*===========================================================================*/
|
|
void proc_stacktrace(struct proc *whichproc)
|
|
{
|
|
#if USE_SYSDEBUG
|
|
proc_stacktrace_execute(whichproc, whichproc->p_reg.fp, whichproc->p_reg.pc);
|
|
#endif /* USE_SYSDEBUG */
|
|
}
|
|
|
|
void enable_fpu_exception(void)
|
|
{
|
|
u32_t cr0 = read_cr0();
|
|
if(!(cr0 & I386_CR0_TS))
|
|
write_cr0(cr0 | I386_CR0_TS);
|
|
}
|
|
|
|
void disable_fpu_exception(void)
|
|
{
|
|
clts();
|
|
}
|
|
|