Kernel hangs after isr creation [SOLVED]

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
leledumbo
Member
Member
Posts: 103
Joined: Wed Apr 23, 2008 8:46 pm

Kernel hangs after isr creation [SOLVED]

Post by leledumbo »

This is the next episode of my previous post.
After following GDT and IDT tutorial, everything's still fine and there's no change as both GDT and IDT changes happened internally. But after following ISR, my kernel hangs. Possible errors are here:

Code: Select all

isr_common_stub:
    pusha

    mov eax, esp
    push eax
    push gs
    push fs
    push es
    push ds

    mov ax, 0x10
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax

    mov eax,fault_handler
    call eax

    pop ds
    pop es
    pop fs
    pop gs
    pop eax

    popa
    add esp, 8
    iret
I try reversing from the original code, as Pascal uses left-to-right convention. The fault_handler and registers record are defined as below:

Code: Select all

type
  PRegisters = ^TRegisters;
  TRegisters = record
    gs,fs,es,ds: LongWord;
    edi,esi,ebp,esp,ebx,edx,ecx,eax: LongWord;
    InterruptNumber,ErrorCode: LongWord;
    eip,cs,eflags,useresp,ss: LongWord;
  end;
...
procedure FaultHandler(r: PRegisters); [public, alias: 'fault_handler'];
begin
  if r^.InterruptNumber<32 then begin
    WriteStr(ExceptionMessages[r^.InterruptNumber]);
    WriteStrLn('Exception. System Halted!');
    while true do ;
  end;
end;
After this, the kernel always hangs regardless the GDT, IDT, and ISR are installed. Can anyone help me?
Attachments
fpcos.zip
(61.21 KiB) Downloaded 49 times
Last edited by leledumbo on Sun Jun 08, 2008 11:18 pm, edited 1 time in total.
leledumbo
Member
Member
Posts: 103
Joined: Wed Apr 23, 2008 8:46 pm

Post by leledumbo »

Forgot. Original code:

Code: Select all

isr_common_stub:
    pusha
    push ds
    push es
    push fs
    push gs
    mov ax, 0x10   ; Load the Kernel Data Segment descriptor!
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov eax, esp   ; Push us the stack
    push eax
    mov eax, _fault_handler
    call eax       ; A special call, preserves the 'eip' register
    pop eax
    pop gs
    pop fs
    pop es
    pop ds
    popa
    add esp, 8     ; Cleans up the pushed error code and pushed ISR number
    iret           ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP!
defined in C:

Code: Select all

struct regs
{
    unsigned int gs, fs, es, ds;      /* pushed the segs last */
    unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax;  /* pushed by 'pusha' */
    unsigned int int_no, err_code;    /* our 'push byte #' and ecodes do this */
    unsigned int eip, cs, eflags, useresp, ss;   /* pushed by the processor automatically */ 
};
...
void fault_handler(struct regs *r)
{
    /* 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 */
        puts(exception_messages[r->int_no]);
        puts(" Exception. System Halted!\n");
        for (;;);
    }
}
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Post by piranha »

Have you tried placing puts() or printf() statements in there to see where the hang occurs?

Edit: Also, your asm code doesn't match the original. Have you tried using the original code?
-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
leledumbo
Member
Member
Posts: 103
Joined: Wed Apr 23, 2008 8:46 pm

Post by leledumbo »

Solved. It's a calling convention problem. I thought it was stdcall, but then I realized that it's cdecl.
Post Reply