#include <oskit/x86/base_trap.h>
struct trap_state { /* Saved segment registers */ unsigned int gs; unsigned int fs; unsigned int es; unsigned int ds; /* PUSHA register state frame */ unsigned int edi; unsigned int esi; unsigned int ebp; unsigned int cr2; /* we save cr2 over esp for page faults */ unsigned int ebx; unsigned int edx; unsigned int ecx; unsigned int eax; /* Processor trap number, 0-31. */ unsigned int trapno; /* Error code pushed by the processor, 0 if none. */ unsigned int err; /* Processor state frame */ unsigned int eip; unsigned int cs; unsigned int eflags; unsigned int esp; unsigned int ss; /* Virtual 8086 segment registers */ unsigned int v86_es; unsigned int v86_ds; unsigned int v86_fs; unsigned int v86_gs; };
This structure defines the saved state frame pushed on the stack by the default trap entrypoints provided by the base environment (see Section 10.8.3). It is also used by the trap_dump routine, which is used in the default environment to dump the saved register state and panic if an unexpected trap occurs; and by gdb_trap, the default trap handler for remote GDB debugging.This client OS is not obligated to use this structure as the saved state frame for traps it handles; if this structure is not used, then the OS must also override (or not use) the dependent routines mentioned above.
The structure elements from err down corresponds to the basic trap frames pushed on the stack by the x86 processor. (For traps in which the processor does not push an error code, the default trap entrypoint code sets err to zero.) The structure elements from esp down are only pushed by traps from lower privilege (rings 1-3), and the structure elements from v86_es down are only pushed by traps from v86 mode.
The rest of the state frame is pushed manually by the default trap entrypoint code. The saved integer register state is organized in a format compatible with the processor's PUSHA instruction. However, in the slot that would otherwise hold the pushed ESP (which is useless since it is the trap handler's stack pointer rather than the trapping code's stack pointer), the default trap handler saves the CR2 register (page fault linear address) during page faults.
This trap state structure is borrowed from Mach.