Page 1 of 1

(Solved) Page Fault Exception after iret

Posted: Thu Jun 23, 2016 6:50 pm
by Cyber
I've been following bran's kernel development tutorials and I've run across a little error. I have IRQs setup but when I try to iret at the end of a handler to return to the code, I get a page fault exception. I don't even have paging enabled yet (could that be the problem?). I assume something happened that made the kernel unable to return to the previous location but I can't figure out what is causing it and how to fix it. I appreciate any help given. Thanks!

ASM source:

Code: Select all

.global isr0
#...
.global isr31

.type isr0, @function
isr0:
	cli
	push $0
	push $0
	jmp isr_common_stub
#...

.type isr31, @function
isr31:
	cli
	push $0
	push $31
	jmp isr_common_stub

#extern fault_handler

isr_common_stub:
	pusha
	push %ds
	push %es
	push %fs
	push %gs
	movw $0x10, %ax
	movw %ax, %ds
	movw %ax, %es
	movw %ax, %fs
	movw %ax, %gs
	movl %esp, %eax
	push %eax
	call fault_handler
	pop %eax
	pop %gs
	pop %fs
	pop %es
	pop %ds
	popa
	add $8, %esp
	iret

.global irq0
#...
.global irq15

.type irq0, @function
irq0:
	cli
	push $0
	push $32
	jmp irq_common_stub

#...

.type irq15, @function
irq15:
	cli
	push $0
	push $47
	jmp irq_common_stub

irq_common_stub:
	pusha
	push %ds
	push %es
	push %fs
	push %gs
	movw $0x10, %ax
	movw %ax, %ds
	movw %ax, %es
	movw %ax, %fs
	movw %ax, %gs
	movl %esp, %eax
	push %eax
	call irq_handler
	pop %eax
	pop %gs
	pop %fs
	pop %es
	pop %ds
	popa
	add $8, %esp
	call debug_donehandling
	iret
C source

Code: Select all

void irq_handler(struct regs *r)
{
	if(r->int_no == 32)
	{
		printf("-h: timer!\n");
	}
    /* This is a blank function pointer */
    void (*handler)(struct regs *r);

    /* Find out if we have a custom handler to run for this
    *  IRQ, and then finally, run it */
    handler = irq_routines[r->int_no - 32];
    if (handler)
    {
    	printf("-h: handler!\n");
        handler(r);
    }

    /* If the IDT entry that was invoked was greater than 40
    *  (meaning IRQ8 - 15), then we need to send an EOI to
    *  the slave controller */
    if (r->int_no >= 40)
    {
    	printf("-h: EOI Slave!\n");
        outportb(0xA0, 0x20);
    }

    /* In either case, we need to send an EOI to the master
    *  interrupt controller too */
    printf("-h: EOI Master!\n");
    outportb(0x20, 0x20);
}

void fault_handler(struct regs *r)
{
	//printf("fault handler activated");
    /* Is this a fault whose number is from 0 to 31? */
    if (r->int_no < 32)
    {
        /* Display the description for the Exception that occurred.
        *  In this tutorial, we will simply halt the system using an
        *  infinite loop */
        printf(exception_messages[r->int_no]);
        printf(" Exception.  System halted.");
        for (;;);
    }
    else
    {
    	irq_handler(r);
    }
}
If you need to see any more just tell me. I really appreciate if someone takes the time out of their day to help me get my OS working.

Re: Page Fault Exception after iret

Posted: Fri Jun 24, 2016 3:34 am
by Combuster
Start with posting a bochs log.

Re: Page Fault Exception after iret

Posted: Fri Jun 24, 2016 3:38 pm
by Cyber
Here's the log

Code: Select all

========================================================================
                        Bochs x86 Emulator 2.6
            Built from SVN snapshot on September 2nd, 2012
========================================================================
00000000000i[     ] LTDL_LIBRARY_PATH not set. using compile time default '/usr/lib/bochs/plugins'
00000000000i[     ] BXSHARE not set. using compile time default '/usr/share/bochs'
00000000000i[     ] lt_dlhandle is 0x39333d0
00000000000i[PLGIN] loaded plugin libbx_unmapped.so
00000000000i[     ] lt_dlhandle is 0x3934030
00000000000i[PLGIN] loaded plugin libbx_biosdev.so
00000000000i[     ] lt_dlhandle is 0x39349b0
00000000000i[PLGIN] loaded plugin libbx_speaker.so
00000000000i[     ] lt_dlhandle is 0x3934d60
00000000000i[PLGIN] loaded plugin libbx_extfpuirq.so
00000000000i[     ] lt_dlhandle is 0x39368c0
00000000000i[PLGIN] loaded plugin libbx_parallel.so
00000000000i[     ] lt_dlhandle is 0x3938570
00000000000i[PLGIN] loaded plugin libbx_serial.so
00000000000i[     ] lt_dlhandle is 0x393c150
00000000000i[PLGIN] loaded plugin libbx_gameport.so
00000000000i[     ] lt_dlhandle is 0x393cbf0
00000000000i[PLGIN] loaded plugin libbx_iodebug.so
00000000000i[     ] reading configuration from bochsrc
00000000000i[     ] lt_dlhandle is 0x393e830
00000000000i[PLGIN] loaded plugin libbx_sb16.so
00000000000e[     ] bochsrc:30: 'vga_update_interval' will be replaced by new 'vga: update_freq' option.
00000000000e[     ] bochsrc:31: 'keyboard_serial_delay' will be replaced by new 'keyboard' option.
00000000000e[     ] bochsrc:32: 'keyboard_paste_delay' will be replaced by new 'keyboard' option.
00000000000i[     ] lt_dlhandle is 0x393d450
00000000000i[PLGIN] loaded plugin libbx_x.so
00000000000i[     ] installing x module as the Bochs GUI
00000000000i[     ] using log file /dev/stdout
00000000000i[     ] Bochs x86 Emulator 2.6
00000000000i[     ]   Built from SVN snapshot on September 2nd, 2012
00000000000i[     ] System configuration
00000000000i[     ]   processors: 1 (cores=1, HT threads=1)
00000000000i[     ]   A20 line support: yes
00000000000i[     ] IPS is set to 1000000
00000000000i[     ] CPU configuration
00000000000i[     ]   level: 6
00000000000i[     ]   SMP support: no
00000000000i[     ]   APIC support: xapic
00000000000i[     ]   FPU support: yes
00000000000i[     ]   MMX support: yes
00000000000i[     ]   3dnow! support: no
00000000000i[     ]   SEP support: yes
00000000000i[     ]   SSE support: sse2
00000000000i[     ]   XSAVE support: no 
00000000000i[     ]   AES support: no
00000000000i[     ]   MOVBE support: no
00000000000i[     ]   ADX support: no
00000000000i[     ]   x86-64 support: yes
00000000000i[     ]   1G paging support: no
00000000000i[     ]   MWAIT support: yes
00000000000i[     ]   AVX support: no
00000000000i[     ]   VMX support: 1
00000000000i[     ] Optimization configuration
00000000000i[     ]   RepeatSpeedups support: yes
00000000000i[     ]   Fast function calls: yes
00000000000i[     ]   Handlers Chaining speedups: no
00000000000i[     ] Devices configuration
00000000000i[     ]   NE2000 support: yes
00000000000i[     ]   PCI support: yes, enabled=yes
00000000000i[     ]   SB16 support: yes
00000000000i[     ]   USB support: yes
00000000000i[     ]   VGA extension support: vbe cirrus
00000000000i[MEM0 ] allocated memory at 0x7fa6c65bb010. after alignment, vector=0x7fa6c65bc000
00000000000i[MEM0 ] 32.00MB
00000000000i[MEM0 ] mem block size = 0x00100000, blocks=32
00000000000i[MEM0 ] rom at 0xfffe0000/131072 ('/usr/share/bochs/BIOS-bochs-latest')
00000000000i[     ] lt_dlhandle is 0x3b08040
00000000000i[PLGIN] loaded plugin libbx_hdimage.so
00000000000i[     ] lt_dlhandle is 0x3b089d0
00000000000i[PLGIN] loaded plugin libbx_soundmod.so
00000000000i[     ] lt_dlhandle is 0x3b132d0
00000000000i[PLGIN] loaded plugin libbx_pci.so
00000000000i[     ] lt_dlhandle is 0x3b13f40
00000000000i[PLGIN] loaded plugin libbx_pci2isa.so
00000000000i[     ] lt_dlhandle is 0x3b14870
00000000000i[PLGIN] loaded plugin libbx_acpi.so
00000000000i[     ] lt_dlhandle is 0x3b152f0
00000000000i[PLGIN] loaded plugin libbx_cmos.so
00000000000i[     ] lt_dlhandle is 0x3b15bd0
00000000000i[PLGIN] loaded plugin libbx_dma.so
00000000000i[     ] lt_dlhandle is 0x3b16660
00000000000i[PLGIN] loaded plugin libbx_pic.so
00000000000i[     ] lt_dlhandle is 0x3b16f00
00000000000i[PLGIN] loaded plugin libbx_pit.so
00000000000i[     ] lt_dlhandle is 0x3b17930
00000000000i[PLGIN] loaded plugin libbx_floppy.so
00000000000i[     ] lt_dlhandle is 0x3b18520
00000000000i[PLGIN] loaded plugin libbx_vga.so
00000000000i[     ] lt_dlhandle is 0x3b18cb0
00000000000i[PLGIN] loaded plugin libbx_ioapic.so
00000000000i[     ] lt_dlhandle is 0x3b196f0
00000000000i[PLGIN] loaded plugin libbx_keyboard.so
00000000000i[     ] lt_dlhandle is 0x3b19f90
00000000000i[PLGIN] loaded plugin libbx_harddrv.so
00000000000i[     ] lt_dlhandle is 0x3b2c080
00000000000i[PLGIN] loaded plugin libbx_pci_ide.so
00000000000i[PLGIN] init_dev of 'pci' plugin device by virtual method
00000000000i[PCI  ] 440FX Host bridge present at device 0, function 0
00000000000i[PLGIN] init_dev of 'pci2isa' plugin device by virtual method
00000000000i[PCI  ] PIIX3 PCI-to-ISA bridge present at device 1, function 0
00000000000i[PLGIN] init_dev of 'cmos' plugin device by virtual method
00000000000i[CMOS ] Using local time for initial clock
00000000000i[CMOS ] Setting initial clock to: Fri Jun 24 17:34:40 2016 (time0=1466804080)
00000000000i[PLGIN] init_dev of 'dma' plugin device by virtual method
00000000000i[DMA  ] channel 4 used by cascade
00000000000i[PLGIN] init_dev of 'pic' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'pit' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'floppy' plugin device by virtual method
00000000000i[DMA  ] channel 2 used by Floppy Drive
00000000000i[PLGIN] init_dev of 'vga' plugin device by virtual method
00000000000i[MEM0 ] Register memory access handlers: 0x00000000000a0000 - 0x00000000000bffff
00000000000i[VGA  ] interval=333333
00000000000i[MEM0 ] Register memory access handlers: 0x00000000e0000000 - 0x00000000e0ffffff
00000000000i[BXVGA] VBE Bochs Display Extension Enabled
00000000000i[XGUI ] test_alloc_colors: 16 colors available out of 16 colors tried
00000000000i[XGUI ] font 8 wide x 16 high, display depth = 24
00000000000i[MEM0 ] rom at 0xc0000/41472 ('/usr/share/vgabios/vgabios.bin')
00000000000i[PLGIN] init_dev of 'acpi' plugin device by virtual method
00000000000i[PCI  ] ACPI Controller present at device 1, function 3
00000000000i[PLGIN] init_dev of 'ioapic' plugin device by virtual method
00000000000i[IOAP ] initializing I/O APIC
00000000000i[MEM0 ] Register memory access handlers: 0x00000000fec00000 - 0x00000000fec00fff
00000000000i[PLGIN] init_dev of 'keyboard' plugin device by virtual method
00000000000i[KBD  ] will paste characters every 1000 keyboard ticks
00000000000i[PLGIN] init_dev of 'harddrv' plugin device by virtual method
00000000000i[HD   ] CD on ata0-0: '/home/cyber/Documents/meaty-skeleton/digitos.iso'
00000000000i[CD1  ] load cdrom with path=/home/cyber/Documents/meaty-skeleton/digitos.iso
00000000000i[CD1  ] Opening image file as a cd.
00000000000i[HD   ] Media present in CD-ROM drive
00000000000i[HD   ] Capacity is 2505 sectors (4.89 MB)
00000000000i[HD   ] Using boot sequence cdrom, none, none
00000000000i[HD   ] Floppy boot signature check is enabled
00000000000i[PLGIN] init_dev of 'pci_ide' plugin device by virtual method
00000000000i[PCI  ] PIIX3 PCI IDE controller present at device 1, function 1
00000000000i[PLGIN] init_dev of 'unmapped' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'biosdev' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'speaker' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'extfpuirq' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'parallel' plugin device by virtual method
00000000000i[PAR  ] parallel port 1 at 0x0378 irq 7
00000000000i[PLGIN] init_dev of 'serial' plugin device by virtual method
00000000000i[SER  ] com1 at 0x03f8 irq 4
00000000000i[PLGIN] init_dev of 'gameport' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'iodebug' plugin device by virtual method
00000000000i[PLGIN] init_dev of 'sb16' plugin device by virtual method
00000000000i[SB16 ] Sound lowlevel module 'linux' initialized
00000000000i[SB16 ] midi=1,/dev/midi00  wave=1,/dev/dsp  log=2,/dev/stdout  dmatimer=600000
00000000000i[DMA  ] channel 1 used by SB16
00000000000 (1) SB16 emulation initialised, IRQ 5, IO 220/330/388, DMA 1/0
00000000000i[PLGIN] register state of 'pci' plugin device by virtual method
00000000000i[PLGIN] register state of 'pci2isa' plugin device by virtual method
00000000000i[PLGIN] register state of 'cmos' plugin device by virtual method
00000000000i[PLGIN] register state of 'dma' plugin device by virtual method
00000000000i[PLGIN] register state of 'pic' plugin device by virtual method
00000000000i[PLGIN] register state of 'pit' plugin device by virtual method
00000000000i[PLGIN] register state of 'floppy' plugin device by virtual method
00000000000i[PLGIN] register state of 'vga' plugin device by virtual method
00000000000i[PLGIN] register state of 'unmapped' plugin device by virtual method
00000000000i[PLGIN] register state of 'biosdev' plugin device by virtual method
00000000000i[PLGIN] register state of 'speaker' plugin device by virtual method
00000000000i[PLGIN] register state of 'extfpuirq' plugin device by virtual method
00000000000i[PLGIN] register state of 'parallel' plugin device by virtual method
00000000000i[PLGIN] register state of 'serial' plugin device by virtual method
00000000000i[PLGIN] register state of 'gameport' plugin device by virtual method
00000000000i[PLGIN] register state of 'iodebug' plugin device by virtual method
00000000000i[PLGIN] register state of 'sb16' plugin device by virtual method
00000000000i[PLGIN] register state of 'acpi' plugin device by virtual method
00000000000i[PLGIN] register state of 'ioapic' plugin device by virtual method
00000000000i[PLGIN] register state of 'keyboard' plugin device by virtual method
00000000000i[PLGIN] register state of 'harddrv' plugin device by virtual method
00000000000i[PLGIN] register state of 'pci_ide' plugin device by virtual method
00000000000i[SYS  ] bx_pc_system_c::Reset(HARDWARE) called
00000000000i[CPU0 ] cpu hardware reset
00000000000i[APIC0] allocate APIC id=0 (MMIO enabled) to 0x00000000fee00000
00000000000i[CPU0 ] CPUID[0x00000000]: 00000002 756e6547 6c65746e 49656e69
00000000000i[CPU0 ] CPUID[0x00000001]: 00000633 00010800 00002028 1fcbfbff
00000000000i[CPU0 ] CPUID[0x00000002]: 00410601 00000000 00000000 00000000
00000000000i[CPU0 ] CPUID[0x80000000]: 80000008 00000000 00000000 00000000
00000000000i[CPU0 ] CPUID[0x80000001]: 00000000 00000000 00000101 2a100000
00000000000i[CPU0 ] CPUID[0x80000002]: 20202020 20202020 20202020 6e492020
00000000000i[CPU0 ] CPUID[0x80000003]: 286c6574 50202952 69746e65 52286d75
00000000000i[CPU0 ] CPUID[0x80000004]: 20342029 20555043 20202020 00202020
00000000000i[CPU0 ] CPUID[0x80000005]: 01ff01ff 01ff01ff 40020140 40020140
00000000000i[CPU0 ] CPUID[0x80000006]: 00000000 42004200 02008140 00000000
00000000000i[CPU0 ] CPUID[0x80000007]: 00000000 00000000 00000000 00000000
00000000000i[CPU0 ] CPUID[0x80000008]: 00003028 00000000 00000000 00000000
00000000000i[PLGIN] reset of 'pci' plugin device by virtual method
00000000000i[PLGIN] reset of 'pci2isa' plugin device by virtual method
00000000000i[PLGIN] reset of 'cmos' plugin device by virtual method
00000000000i[PLGIN] reset of 'dma' plugin device by virtual method
00000000000i[PLGIN] reset of 'pic' plugin device by virtual method
00000000000i[PLGIN] reset of 'pit' plugin device by virtual method
00000000000i[PLGIN] reset of 'floppy' plugin device by virtual method
00000000000i[PLGIN] reset of 'vga' plugin device by virtual method
00000000000i[PLGIN] reset of 'acpi' plugin device by virtual method
00000000000i[PLGIN] reset of 'ioapic' plugin device by virtual method
00000000000i[PLGIN] reset of 'keyboard' plugin device by virtual method
00000000000i[PLGIN] reset of 'harddrv' plugin device by virtual method
00000000000i[PLGIN] reset of 'pci_ide' plugin device by virtual method
00000000000i[PLGIN] reset of 'unmapped' plugin device by virtual method
00000000000i[PLGIN] reset of 'biosdev' plugin device by virtual method
00000000000i[PLGIN] reset of 'speaker' plugin device by virtual method
00000000000i[SPEAK] Using lowlevel sound support for output
00000000000i[PLGIN] reset of 'extfpuirq' plugin device by virtual method
00000000000i[PLGIN] reset of 'parallel' plugin device by virtual method
00000000000i[PLGIN] reset of 'serial' plugin device by virtual method
00000000000i[PLGIN] reset of 'gameport' plugin device by virtual method
00000000000i[PLGIN] reset of 'iodebug' plugin device by virtual method
00000000000i[PLGIN] reset of 'sb16' plugin device by virtual method
00000000000i[XGUI ] Mouse capture on
00000000000i[     ] set SIGINT handler to bx_debug_ctrlc_handler
Next at t=0
(0) [0x00000000fffffff0] f000:fff0 (unk. ctxt): jmp far f000:e05b         ; ea5be000f0
00000000000i[XGUI ] Mouse capture off
<bochs:1> c
00000000025i[MEM0 ] allocate_block: block=0x0 used 0x1 of 0x20
00000004661i[BIOS ] $Revision: 11318 $ $Date: 2012-08-06 19:59:54 +0200 (Mo, 06. Aug 2012) $
00000318040i[KBD  ] reset-disable command received
00000319320i[BIOS ] Starting rombios32
00000319762i[BIOS ] Shutdown flag 0
00000320359i[BIOS ] ram_size=0x02000000
00000320787i[BIOS ] ram_end=32MB
00000331778i[BIOS ] Found 1 cpu(s)
00000345959i[BIOS ] bios_table_addr: 0x000fa438 end=0x000fcc00
00000673754i[PCI  ] 440FX PMC write to PAM register 59 (TLB Flush)
00001001686i[P2I  ] PCI IRQ routing: PIRQA# set to 0x0b
00001001710i[P2I  ] PCI IRQ routing: PIRQB# set to 0x09
00001001734i[P2I  ] PCI IRQ routing: PIRQC# set to 0x0b
00001001758i[P2I  ] PCI IRQ routing: PIRQD# set to 0x09
00001001768i[P2I  ] write: ELCR2 = 0x0a
00001002547i[BIOS ] PIIX3/PIIX4 init: elcr=00 0a
00001010227i[BIOS ] PCI: bus=0 devfn=0x00: vendor_id=0x8086 device_id=0x1237 class=0x0600
00001012506i[BIOS ] PCI: bus=0 devfn=0x08: vendor_id=0x8086 device_id=0x7000 class=0x0601
00001014624i[BIOS ] PCI: bus=0 devfn=0x09: vendor_id=0x8086 device_id=0x7010 class=0x0101
00001014853i[PIDE ] new BM-DMA address: 0xc000
00001015473i[BIOS ] region 4: 0x0000c000
00001017507i[BIOS ] PCI: bus=0 devfn=0x0b: vendor_id=0x8086 device_id=0x7113 class=0x0680
00001017737i[ACPI ] new irq line = 11
00001017751i[ACPI ] new irq line = 9
00001017778i[ACPI ] new PM base address: 0xb000
00001017792i[ACPI ] new SM base address: 0xb100
00001017820i[PCI  ] setting SMRAM control register to 0x4a
00001181914i[CPU0 ] Enter to System Management Mode
00001181914i[CPU0 ] enter_system_management_mode: temporary disable VMX while in SMM mode
00001181924i[CPU0 ] RSM: Resuming from System Management Mode
00001345945i[PCI  ] setting SMRAM control register to 0x0a
00001360847i[BIOS ] MP table addr=0x000fa510 MPC table addr=0x000fa440 size=0xc8
00001362662i[BIOS ] SMBIOS table addr=0x000fa520
00001362713i[MEM0 ] allocate_block: block=0x1f used 0x2 of 0x20
00001365641i[BIOS ] Firmware waking vector 0x1ff00cc
00001370500i[BIOS ] ACPI tables: RSDP addr=0x000fa640 ACPI DATA addr=0x01ff0000 size=0x1f22
00001370537i[PCI  ] 440FX PMC write to PAM register 59 (TLB Flush)
00001371268i[BIOS ] bios_table_cur_addr: 0x000fa664
00001498886i[VBIOS] VGABios $Id: vgabios.c,v 1.75 2011/10/15 14:07:21 vruppert Exp $
00001498957i[BXVGA] VBE known Display Interface b0c0
00001498989i[BXVGA] VBE known Display Interface b0c5
00001501914i[VBIOS] VBE Bios $Id: vbe.c,v 1.64 2011/07/19 18:25:05 vruppert Exp $
00001666665i[XGUI ] charmap update. Font Height is 16
00005715028i[BIOS ] IDE time out
00008781828i[BIOS ] Booting from 07c0:0000
00008790679i[MEM0 ] allocate_block: block=0x1 used 0x3 of 0x20
00043445258i[MEM0 ] allocate_block: block=0x1e used 0x4 of 0x20

00097450020e[CPU0 ] check_cs(0x0010): not a valid code segment !

Re: Page Fault Exception after iret

Posted: Sat Jun 25, 2016 4:56 am
by Combuster
So, did you spot your real error yet? I know exactly at which two places I'd like to have a breakpoint to see what register and stack values I should have and what register and stack values I actually have. Guess where that would be...



The very first and last instruction of the interrupt handler in question. The state has to be exactly the same for the return to work properly.

Re: Page Fault Exception after iret

Posted: Sat Jun 25, 2016 12:09 pm
by Cyber
I'm a bit confused here though, because I thought it would be the same after as before?

First, the processor pushes eip, cs, eflags, useresp, and ss.
Then, two values are pushed onto the stack manually: the err code and interrupt number.
Then the pushal pushes edi, esi, ebp, esp, ebx, edx, ecx, and eax.
Then gs, fs, es, ds are pushed manually.
Then the fault handler is called.
Then gs, fs, es, ds are popped manually.
Then the popal pops edi, esi, ebp, esp, ebx, edx, ecx, and eax.
Then the esp is manually incremented by 8 skipping over the interrupt number and err code.
Then iret pops eip, cs, eflags, useresp, ss.

So where's the problem here? I'm really confused as to what is going wrong in this process because it seems very solid to me. Aren't all of the registers returning to their previous state? I've experimented with different values for the esp incrementing and none of them have any effect on the outcome.

Re: Page Fault Exception after iret

Posted: Sat Jun 25, 2016 12:24 pm
by BASICFreak
Cyber wrote:Then, two values are pushed onto the stack manually: the err code and interrupt number.
This statement is both true and false ;)

The Error Code will be pushed by the CPU if there is an error code, otherwise you could(should) push 0 manually.

Exception (all Hex) 8, A, B, C, D, E, 11, and 1E push error codes for you.

Also, I have no clue what "debug_donehandling" is or does, but if it is in C - you have (likely) scratched registers that you just restored!



Best regards,

B!

Re: Page Fault Exception after iret

Posted: Sat Jun 25, 2016 3:33 pm
by Cyber
BASICFreak wrote:
Cyber wrote:Then, two values are pushed onto the stack manually: the err code and interrupt number.
This statement is both true and false ;)

The Error Code will be pushed by the CPU if there is an error code, otherwise you could(should) push 0 manually.

Exception (all Hex) 8, A, B, C, D, E, 11, and 1E push error codes for you.

Also, I have no clue what "debug_donehandling" is or does, but if it is in C - you have (likely) scratched registers that you just restored!



Best regards,

B!
Yes I do have it set up that way so it doesn't push the 0 on some of them. the debug_donehandling thing was something I had that just sent a debugger message to the terminal, but I have been doing all tests with it commented out and it doesn't seem to make a difference at all.

Re: Page Fault Exception after iret

Posted: Sun Jun 26, 2016 4:20 am
by Combuster
I've experimented with different values for the esp incrementing
So, you're trying the approach every newbie does and which never works: guesswork. Trying to permute code until it holds out. If it doesn't even change one thing, maybe that tells you something else?

Also, where are those register dumps you should have made earlier?

Re: Page Fault Exception after iret

Posted: Wed Jun 29, 2016 4:54 pm
by Cyber
EDIT: Used lea instead of add to preserve eflags
EDIT2: I just checked and the stacks are exactly the same. I can post that if necessary.
Combuster wrote:So, did you spot your real error yet? I know exactly at which two places I'd like to have a breakpoint to see what register and stack values I should have and what register and stack values I actually have. Guess where that would be...



The very first and last instruction of the interrupt handler in question. The state has to be exactly the same for the return to work properly.
I did the breakpoints and got the following:


BEFORE (directly after irq0 is called):

Code: Select all

rax: 0x00000000_00000001 rcx: 0x00000000_000003d5
rdx: 0x00000000_00000000 rbx: 0x00000000_00010000
rsp: 0x00000000_001138e7 rbp: 0x00000000_00000000
rsi: 0x00000000_00000000 rdi: 0x00000000_00000000
r8 : 0x00000000_00000000 r9 : 0x00000000_00000000
r10: 0x00000000_00000000 r11: 0x00000000_00000000
r12: 0x00000000_00000000 r13: 0x00000000_00000000
r14: 0x00000000_00000000 r15: 0x00000000_00000000
rip: 0x00000000_001002b3
eflags 0x00200006: ID vip vif ac vm rf nt IOPL=0 of df if tf sf zf af PF cf
AFTER (directly before iret):

Code: Select all

rax: 0x00000000_00000001 rcx: 0x00000000_000003d5
rdx: 0x00000000_00000000 rbx: 0x00000000_00010000
rsp: 0x00000000_001138e7 rbp: 0x00000000_00000000
rsi: 0x00000000_00000000 rdi: 0x00000000_00000000
r8 : 0x00000000_00000000 r9 : 0x00000000_00000000
r10: 0x00000000_00000000 r11: 0x00000000_00000000
r12: 0x00000000_00000000 r13: 0x00000000_00000000
r14: 0x00000000_00000000 r15: 0x00000000_00000000
rip: 0x00000000_00100357
eflags 0x00200006: ID vip vif ac vm rf nt IOPL=0 of df if tf sf zf af PF cf
The only change is in the instruction pointer (001002b3 to 00100357). It should be popped by iretl, so what's the problem here?

Re: Page Fault Exception after iret

Posted: Wed Jun 29, 2016 8:04 pm
by Cyber
Finally fixed the problem. All along it was an issue with the gdt and forgetting to do a few things after installing it. Thanks for all the help everyone gave.

Re: (Solved) Page Fault Exception after iret

Posted: Thu Jun 30, 2016 2:02 am
by Combuster
rsp: 0x00000000_001138e7
I see x86_64 ABI breakage. Unaligned stacks are bad for your health ;)

Re: Page Fault Exception after iret

Posted: Wed Aug 24, 2016 12:46 pm
by FlatChicken
Cyber wrote:Finally fixed the problem. All along it was an issue with the gdt and forgetting to do a few things after installing it. Thanks for all the help everyone gave.
Hi. Could I ask what the "few things" were, please? I seem to be inexactly the same boat - my stack is back as it was, View | Stack shows my code selector "8" staring me in the face in second place, yet "check_cs(0x0010): not a valid code segment !" on the iret.

I get the same thing even if I iret immediately, it's not something I am messing up. It is something already messed up.

Cheers.


ETA- Found it. I was missing the gdt_flush(). I assume that's what you meant.