From 8bfcd4fd19da32073c054376a1b946f4e7e8d605 Mon Sep 17 00:00:00 2001 From: Lucia Ceionia Date: Fri, 16 Sep 2022 17:07:27 -0500 Subject: [PATCH] GPT handler detects V86 mode; Interrupt printing fixes --- entry.nasm | 2 +- handler.nasm | 11 +++++--- interrupt.c | 74 ++++++++++++++++++++++++++++++++-------------------- print.c | 8 ------ print.h | 3 --- 5 files changed, 53 insertions(+), 45 deletions(-) diff --git a/entry.nasm b/entry.nasm index 48868bc..a6b2fe9 100644 --- a/entry.nasm +++ b/entry.nasm @@ -68,7 +68,7 @@ inc al cmp eax, 200 jl .loop xor ebx, ebx -div bl +div bl ; Unhandled DIV0 exception global jmp_usermode_test jmp_usermode_test: diff --git a/handler.nasm b/handler.nasm index 81aede4..34e1fa0 100644 --- a/handler.nasm +++ b/handler.nasm @@ -12,10 +12,14 @@ jmp .hlt extern gpf_handler_v86 global gpfHandler gpfHandler: -iret +push eax mov ax, 0x10 mov ds, ax -;jmp gpf_handler_v86 +mov eax, dword [esp+16] ; EFLAGS +and eax, 1 << 17 ; VM flag +test eax, eax +pop eax +jnz gpf_handler_v86 mov word [0xb8000], 0x0f00 | 'G' mov word [0xb8002], 0x0f00 | 'P' mov word [0xb8004], 0x0f00 | 'F' @@ -71,8 +75,7 @@ kbd_wait: mov byte [KBDWAIT], 0 .loop: hlt -xor eax, eax -mov al, [KBDWAIT] +movzx eax, byte [KBDWAIT] test eax, eax jz .loop ret diff --git a/interrupt.c b/interrupt.c index cb6612c..b4fea36 100644 --- a/interrupt.c +++ b/interrupt.c @@ -1,10 +1,26 @@ #include #include -#include "print.h" - #include "interrupt.h" +char int_nibbleToHex(uint8_t n) { + return n > 9 ? (n - 10) + 'A' : n + '0'; +} +void int_printByte(uint8_t v, uint16_t *buff) { + *(char *)&buff[0] = int_nibbleToHex((v >> 4) & 0xF); + *(char *)&buff[1] = int_nibbleToHex(v & 0xF); +} +__attribute((__no_caller_saved_registers__)) +void int_printWord(uint16_t v, uint16_t *buff) { + int_printByte(v >> 8, buff); + int_printByte(v, &buff[2]); +} +__attribute((__no_caller_saved_registers__)) +void int_printDword(uint32_t v, uint16_t *buff) { + int_printWord(v >> 16, buff); + int_printWord(v, &buff[4]); +} + struct __attribute__((__packed__)) IDTR_t { uint16_t size; uint32_t offset; @@ -74,7 +90,7 @@ extern void jmp_usermode_test(); #define VALID_FLAGS 0xDFF __attribute__ ((interrupt)) void gpf_handler_v86(struct interrupt_frame *frame, unsigned long error_code) { - asm volatile("mov %%ax,%%ds"::"a"(0x10)); + //asm volatile("mov %%ax,%%ds"::"a"(0x10)); uint8_t *ip; uint16_t *stack; uint32_t *stack32; @@ -84,17 +100,17 @@ void gpf_handler_v86(struct interrupt_frame *frame, unsigned long error_code) { stack32 = (uint32_t*)stack; char *vga = (char*)0xb8000 + (160 * 10); - vga[0] = 'I'; vga[2] = 'P'; printWord(frame->eip, &vga[4]); vga += 14; - vga[0] = 'C'; vga[2] = 'S'; printWord(frame->cs, &vga[4]); vga += 14; - vga[0] = 'F'; vga[2] = 'L'; printDword(frame->eflags, &vga[4]); vga += 14; + vga[0] = 'I'; vga[2] = 'P'; int_printWord(frame->eip, (uint16_t*)&vga[4]); vga += 14; + vga[0] = 'C'; vga[2] = 'S'; int_printWord(frame->cs, (uint16_t*)&vga[4]); vga += 14; + vga[0] = 'F'; vga[2] = 'L'; int_printDword(frame->eflags, (uint16_t*)&vga[4]); vga += 14; vga = (char*)0xb8000 + (160 * 11); - vga[0] = 'S'; vga[2] = 'P'; printWord(frame->esp, &vga[4]); vga += 14; - vga[0] = 'S'; vga[2] = 'S'; printWord(frame->ss, &vga[4]); vga += 14; + vga[0] = 'S'; vga[2] = 'P'; int_printWord(frame->esp, (uint16_t*)&vga[4]); vga += 14; + vga[0] = 'S'; vga[2] = 'S'; int_printWord(frame->ss, (uint16_t*)&vga[4]); vga += 14; vga = (char*)0xb8000 + (160 * 12); - vga[0] = 'E'; vga[2] = 'S'; printWord(frame->es, &vga[4]); vga += 14; - vga[0] = 'D'; vga[2] = 'S'; printWord(frame->ds, &vga[4]); vga += 14; - vga[0] = 'F'; vga[2] = 'S'; printWord(frame->fs, &vga[4]); vga += 14; - vga[0] = 'G'; vga[2] = 'S'; printWord(frame->gs, &vga[4]); vga += 14; + vga[0] = 'E'; vga[2] = 'S'; int_printWord(frame->es, (uint16_t*)&vga[4]); vga += 14; + vga[0] = 'D'; vga[2] = 'S'; int_printWord(frame->ds, (uint16_t*)&vga[4]); vga += 14; + vga[0] = 'F'; vga[2] = 'S'; int_printWord(frame->fs, (uint16_t*)&vga[4]); vga += 14; + vga[0] = 'G'; vga[2] = 'S'; int_printWord(frame->gs, (uint16_t*)&vga[4]); vga += 14; //vga[2]++; //printDword(frame, &vga[20]); @@ -160,7 +176,7 @@ void gpf_handler_v86(struct interrupt_frame *frame, unsigned long error_code) { vga[0] = 'I'; vga[2]++; if (vga[2] < '0') vga[2] = '0'; switch (ip[1]) { case 0x30: - asm("jmp jmp_usermode_test"); + asm ("jmp jmp_usermode_test"); for(;;); case 0x3: kbd_wait(); @@ -172,10 +188,10 @@ void gpf_handler_v86(struct interrupt_frame *frame, unsigned long error_code) { stack[1] = frame->cs; stack[2] = (uint16_t) frame->eflags; - //if (v86_if) - // stack[2] |= EFLAG_IF; - //else - // stack[2] &= ~EFLAG_IF; + if (v86_if) + stack[2] |= EFLAG_IF; + else + stack[2] &= ~EFLAG_IF; frame->cs = ivt[ip[1] * 2 + 1]; frame->eip = ivt[ip[1] * 2]; @@ -201,19 +217,19 @@ void gpf_handler_v86(struct interrupt_frame *frame, unsigned long error_code) { for(;;); } } - done: + done:; vga = (char*)0xb8000 + (160 * 13); - vga[0] = 'I'; vga[2] = 'P'; printWord(frame->eip, &vga[4]); vga += 14; - vga[0] = 'C'; vga[2] = 'S'; printWord(frame->cs, &vga[4]); vga += 14; - vga[0] = 'F'; vga[2] = 'L'; printDword(frame->eflags, &vga[4]); vga += 14; + vga[0] = 'I'; vga[2] = 'P'; int_printWord(frame->eip, (uint16_t*)&vga[4]); vga += 14; + vga[0] = 'C'; vga[2] = 'S'; int_printWord(frame->cs, (uint16_t*)&vga[4]); vga += 14; + vga[0] = 'F'; vga[2] = 'L'; int_printDword(frame->eflags, (uint16_t*)&vga[4]); vga += 14; vga = (char*)0xb8000 + (160 * 14); - vga[0] = 'S'; vga[2] = 'P'; printWord(frame->esp, &vga[4]); vga += 14; - vga[0] = 'S'; vga[2] = 'S'; printWord(frame->ss, &vga[4]); vga += 14; + vga[0] = 'S'; vga[2] = 'P'; int_printWord(frame->esp, (uint16_t*)&vga[4]); vga += 14; + vga[0] = 'S'; vga[2] = 'S'; int_printWord(frame->ss, (uint16_t*)&vga[4]); vga += 14; vga = (char*)0xb8000 + (160 * 15); - vga[0] = 'E'; vga[2] = 'S'; printWord(frame->es, &vga[4]); vga += 14; - vga[0] = 'D'; vga[2] = 'S'; printWord(frame->ds, &vga[4]); vga += 14; - vga[0] = 'F'; vga[2] = 'S'; printWord(frame->fs, &vga[4]); vga += 14; - vga[0] = 'G'; vga[2] = 'S'; printWord(frame->gs, &vga[4]); vga += 14; + vga[0] = 'E'; vga[2] = 'S'; int_printWord(frame->es, (uint16_t*)&vga[4]); vga += 14; + vga[0] = 'D'; vga[2] = 'S'; int_printWord(frame->ds, (uint16_t*)&vga[4]); vga += 14; + vga[0] = 'F'; vga[2] = 'S'; int_printWord(frame->fs, (uint16_t*)&vga[4]); vga += 14; + vga[0] = 'G'; vga[2] = 'S'; int_printWord(frame->gs, (uint16_t*)&vga[4]); vga += 14; } extern void timerHandler(); @@ -252,8 +268,8 @@ void setup_interrupts() { set_system_gate(0x20, timerHandler); set_system_gate(0x21, keyboardHandler); - set_trap_gate(13, gpf_handler_v86); - //set_trap_gate(13, gpfHandler); + //set_trap_gate(13, gpf_handler_v86); + set_trap_gate(13, gpfHandler); asm volatile("lidt %0": : "m"(IDTR)); picInit(); diff --git a/print.c b/print.c index 8a49bc2..080c77a 100644 --- a/print.c +++ b/print.c @@ -1,24 +1,16 @@ #include "print.h" -__attribute((__always_inline__)) -__attribute((__no_caller_saved_registers__)) char nibbleToHex(uint8_t n) { return n > 9 ? (n - 10) + 'A' : n + '0'; } -__attribute((__always_inline__)) -__attribute((__no_caller_saved_registers__)) void printByte(uint8_t v, uint16_t *buff) { *(char *)&buff[0] = nibbleToHex((v >> 4) & 0xF); *(char *)&buff[1] = nibbleToHex(v & 0xF); } -__attribute((__always_inline__)) -__attribute((__no_caller_saved_registers__)) void printWord(uint16_t v, uint16_t *buff) { printByte(v >> 8, buff); printByte(v, &buff[2]); } -__attribute((__always_inline__)) -__attribute((__no_caller_saved_registers__)) void printDword(uint32_t v, uint16_t *buff) { printWord(v >> 16, buff); printWord(v, &buff[4]); diff --git a/print.h b/print.h index 22f018c..ade22fe 100644 --- a/print.h +++ b/print.h @@ -1,9 +1,6 @@ #pragma once #include -__attribute((__no_caller_saved_registers__)) void printByte(uint8_t v, uint16_t *buff); -__attribute((__no_caller_saved_registers__)) void printWord(uint16_t v, uint16_t *buff); -__attribute((__no_caller_saved_registers__)) void printDword(uint32_t v, uint16_t *buff);