From 1545a9a790c9882bb8e3e28e5fec858388149875 Mon Sep 17 00:00:00 2001 From: Lucia Ceionia Date: Sat, 17 Sep 2022 16:31:41 -0500 Subject: [PATCH] V86 returns to caller --- Makefile | 1 - handler.nasm | 5 ++--- interrupt.c | 3 ++- kernel.c | 4 +++- v86.nasm | 1 + 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index ca4eeb9..e8822d8 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ CFLAGS = -target "i686-elf" -m32 -ffreestanding -march=pentium-m -fno-stack-prot all: $(objects) nasm boot.nasm -o boot.bin - # not sure why but if interrupt.c has any optimization everything just breaks immediately gcc -Tlink.ld -m32 -ffreestanding -nostartfiles -nostdlib -o kernel.bin\ $(objects) dd bs=256 count=1 conv=notrunc if=boot.bin of=virtdisk.bin diff --git a/handler.nasm b/handler.nasm index 34e1fa0..6320680 100644 --- a/handler.nasm +++ b/handler.nasm @@ -20,9 +20,8 @@ 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' +mov dword [0xb8000], 0x0f000f00 | 'G' | 'P' << 16 +mov dword [0xb8004], 0x0f000f00 | 'F' | '!' << 16 .hlt: hlt jmp .hlt diff --git a/interrupt.c b/interrupt.c index b4fea36..394221f 100644 --- a/interrupt.c +++ b/interrupt.c @@ -128,6 +128,7 @@ void gpf_handler_v86(struct interrupt_frame *frame, unsigned long error_code) { // vga += (sizeof(uint8_t)*2)*2; //} vga = (char*)0xb8000 + (160*3); + uint32_t *tss_esp0 = (uint32_t*)0x20004; for(;;) { switch (ip[0]) { case 0x66: // O32 @@ -176,7 +177,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 ("mov %%eax, %%esp\nret"::"a"(*tss_esp0)); for(;;); case 0x3: kbd_wait(); diff --git a/kernel.c b/kernel.c index 01ba120..930ada7 100644 --- a/kernel.c +++ b/kernel.c @@ -52,8 +52,10 @@ void print_cr4() { printDword(reg, 0xB8000 + (160*5) + 50 + 8*4 + 4); } +__attribute((__no_caller_saved_registers__)) extern void enter_v86(uint32_t ss, uint32_t esp, uint32_t cs, uint32_t eip); extern void v86Code(); +__attribute((__no_caller_saved_registers__)) extern void jmp_usermode_test(); /* @@ -107,6 +109,6 @@ void start() { print_cr4(); FARPTR v86_entry = i386LinearToFp(v86Code); enter_v86(0x8000, 0xFF00, FP_SEG(v86_entry), FP_OFF(v86_entry)); - //jmp_usermode_test(); + jmp_usermode_test(); } diff --git a/v86.nasm b/v86.nasm index cea6508..1ce84fe 100644 --- a/v86.nasm +++ b/v86.nasm @@ -71,6 +71,7 @@ jmp $ global enter_v86 enter_v86: mov ebp, esp ; save stack pointer +mov dword [0x20004], ebp ; tss ESP0 push dword [ebp+4] ; ss push dword [ebp+8] ; esp pushfd ; eflags