Skip to content

Commit

Permalink
Kernel: Add exception_code to RegisterDump.
Browse files Browse the repository at this point in the history
Added the exception_code field to RegisterDump, removing the need
for RegisterDumpWithExceptionCode. To accomplish this, I had to
push a dummy exception code during some interrupt entries to properly
pad out the RegisterDump. Note that we also needed to change some code
in sys$sigreturn to deal with the new RegisterDump layout.
  • Loading branch information
DrewStratford authored and awesomekling committed Oct 7, 2019
1 parent 334e039 commit 7fc903b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 38 deletions.
20 changes: 10 additions & 10 deletions Kernel/Arch/i386/CPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ extern "C" void asm_irq_entry();
asm(
".globl asm_irq_entry\n"
"asm_irq_entry: \n"
" pushl $0x0\n"
" pusha\n"
" pushw %ds\n"
" pushw %es\n"
Expand All @@ -57,10 +58,11 @@ asm(
" popw %es\n"
" popw %ds\n"
" popa\n"
" add $0x4, %esp\n"
" iret\n");

#define EH_ENTRY(ec) \
extern "C" void exception_##ec##_handler(RegisterDumpWithExceptionCode&); \
extern "C" void exception_##ec##_handler(RegisterDump&); \
extern "C" void exception_##ec##_entry(); \
asm( \
".globl exception_" #ec "_entry\n" \
Expand Down Expand Up @@ -96,6 +98,7 @@ asm(
asm( \
".globl exception_" #ec "_entry\n" \
"exception_" #ec "_entry: \n" \
" pushl $0x0\n" \
" pusha\n" \
" pushw %ds\n" \
" pushw %es\n" \
Expand All @@ -118,10 +121,10 @@ asm(
" popw %es\n" \
" popw %ds\n" \
" popa\n" \
" add $0x4, %esp\n" \
" iret\n");

template<typename DumpType>
static void dump(const DumpType& regs)
static void dump(const RegisterDump& regs)
{
u16 ss;
u32 esp;
Expand All @@ -133,9 +136,7 @@ static void dump(const DumpType& regs)
esp = regs.esp_if_crossRing;
}

if constexpr (IsSame<DumpType, RegisterDumpWithExceptionCode>::value) {
kprintf("exception code: %04x\n", regs.exception_code);
}
kprintf("exception code: %04x\n", regs.exception_code);
kprintf(" pc=%04x:%08x ds=%04x es=%04x fs=%04x gs=%04x\n", regs.cs, regs.eip, regs.ds, regs.es, regs.fs, regs.gs);
kprintf(" stk=%04x:%08x\n", ss, esp);
if (current)
Expand All @@ -157,8 +158,7 @@ static void dump(const DumpType& regs)
}
}

template<typename RegisterDumpType>
static void handle_crash(RegisterDumpType& regs, const char* description, int signal)
static void handle_crash(RegisterDump& regs, const char* description, int signal)
{
if (!current) {
kprintf("%s with !current\n", description);
Expand Down Expand Up @@ -195,7 +195,7 @@ void exception_0_handler(RegisterDump& regs)
}

EH_ENTRY(13);
void exception_13_handler(RegisterDumpWithExceptionCode& regs)
void exception_13_handler(RegisterDump& regs)
{
handle_crash(regs, "General protection fault", SIGSEGV);
}
Expand Down Expand Up @@ -232,7 +232,7 @@ void exception_7_handler(RegisterDump& regs)

// 14: Page Fault
EH_ENTRY(14);
void exception_14_handler(RegisterDumpWithExceptionCode& regs)
void exception_14_handler(RegisterDump& regs)
{
ASSERT(current);

Expand Down
24 changes: 1 addition & 23 deletions Kernel/Arch/i386/CPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,29 +333,6 @@ class PageFault {
};

struct [[gnu::packed]] RegisterDump
{
u16 ss;
u16 gs;
u16 fs;
u16 es;
u16 ds;
u32 edi;
u32 esi;
u32 ebp;
u32 esp;
u32 ebx;
u32 edx;
u32 ecx;
u32 eax;
u32 eip;
u16 cs;
u16 __csPadding;
u32 eflags;
u32 esp_if_crossRing;
u16 ss_if_crossRing;
};

struct [[gnu::packed]] RegisterDumpWithExceptionCode
{
u16 ss;
u16 gs;
Expand All @@ -380,6 +357,7 @@ struct [[gnu::packed]] RegisterDumpWithExceptionCode
u16 ss_if_crossRing;
};


struct [[gnu::aligned(16)]] FPUState
{
u8 buffer[512];
Expand Down
2 changes: 2 additions & 0 deletions Kernel/Arch/i386/PIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern "C" void timer_interrupt_handler(RegisterDump&);
asm(
".globl timer_interrupt_entry \n"
"timer_interrupt_entry: \n"
" pushl $0x0\n"
" pusha\n"
" pushw %ds\n"
" pushw %es\n"
Expand All @@ -34,6 +35,7 @@ asm(
" popw %es\n"
" popw %ds\n"
" popa\n"
" add $0x4, %esp\n"
" iret\n");

static u32 s_ticks_this_second;
Expand Down
9 changes: 6 additions & 3 deletions Kernel/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,9 +864,12 @@ int Process::sys$sigreturn(RegisterDump& registers)
current->m_signal_mask = *stack_ptr;
stack_ptr++;

//pop edi, esi, ebp, esp, ebx, edx, ecx, eax and eip
memcpy(&registers.edi, stack_ptr, 9 * sizeof(u32));
stack_ptr += 9;
//pop edi, esi, ebp, esp, ebx, edx, ecx and eax
memcpy(&registers.edi, stack_ptr, 8 * sizeof(u32));
stack_ptr += 8;

registers.eip = *stack_ptr;
stack_ptr++;

registers.eflags = *stack_ptr;
stack_ptr++;
Expand Down
4 changes: 2 additions & 2 deletions Kernel/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,8 @@ void Scheduler::timer_tick(RegisterDump& regs)
outgoing_tss.eflags = regs.eflags;

// Compute process stack pointer.
// Add 12 for CS, EIP, EFLAGS (interrupt mechanic)
outgoing_tss.esp = regs.esp + 12;
// Add 16 for CS, EIP, EFLAGS, exception code (interrupt mechanic)
outgoing_tss.esp = regs.esp + 16;
outgoing_tss.ss = regs.ss;

if ((outgoing_tss.cs & 3) != 0) {
Expand Down
2 changes: 2 additions & 0 deletions Kernel/Syscall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern volatile RegisterDump* syscallRegDump;
asm(
".globl syscall_trap_handler \n"
"syscall_trap_handler:\n"
" pushl $0x0\n"
" pusha\n"
" pushw %ds\n"
" pushw %es\n"
Expand All @@ -35,6 +36,7 @@ asm(
" popw %es\n"
" popw %ds\n"
" popa\n"
" add $0x4, %esp\n"
" iret\n");

namespace Syscall {
Expand Down

0 comments on commit 7fc903b

Please sign in to comment.