Some IDT Code...in C

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.
Slasher

Re:Some IDT Code...in C

Post by Slasher »

okay, the best thing to do would be to post your setup code and you IDT code for us to find the problem.
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:Some IDT Code...in C

Post by df »

this is my old IDT code, i think its changed and evolved through about 5 different 'OS' rewrites I've done.

Code: Select all

#include <osdefs.h>
#include <string.h>
#include <ia32_hal.h>

#include "screen.h"
#include "memory.h"
#include "idt.h"
#include "defines.h"
#include "irq.h"

#define TRAP_STUB(name, exc) \
   void _exception_##name() { guru_meditation(exc); while(1); }
#define TRAP_NAME(name) (UINT32)(void*)&_exception_##name

extern void hi_irq(void);
extern void low_irq(void);
extern void dead_isr(void);
extern void fpu_isr(void);

void add_gate(int sel, UINT16 gdt, UINT16 type);
void setup_gate(int sel, UINT32 phys_off, UINT16 type);
void init_idt(void);
void load_idt(int num_idt, GATE* idt_offs);
void guru_meditation(int num);

char *guru[]=
   {
        "Divide by Zero",
        "Debug",
        "Non Maskable Interrupt",
        "Breakpoint",
        "Interrupt on Overflow",
        "Array Bounds Error",
        "Illegal Opcode",
        "Math not available",
        "Double Fault",
        "Math segment overflow",
        "Invalid TSS",
        "Segment not present",
        "Stack Fault",
        "General Protection Error",
        "Page Fault",
        "na",
        "Math Error",
        "Alignment Error",
        "Machine Check"
   };

GATE   *idt;
int      num_idts;

TRAP_STUB(divide, 0);
TRAP_STUB(debug, 1);
TRAP_STUB(nmi, 2);
TRAP_STUB(breakpoint, 3);
TRAP_STUB(overflow, 4);
TRAP_STUB(bounds, 5);
TRAP_STUB(opcode, 6);
TRAP_STUB(mathgone, 7);
TRAP_STUB(double, 8);
TRAP_STUB(mathover, 9);
TRAP_STUB(tss, 10);
TRAP_STUB(segment, 11);
TRAP_STUB(stack, 12);
TRAP_STUB(general, 13);
TRAP_STUB(page, 14);
TRAP_STUB(matherr, 16);
TRAP_STUB(align, 17);
TRAP_STUB(mce, 18);

void setup_gate(int sel, UINT32 phys_off, UINT16 type)
{
   idt[sel].offset_0=(phys_off&0xFFFF);
    idt[sel].offset_16=(phys_off>>16);
    idt[sel].selector=0x08;            // code32
    idt[sel].type=type;
}

void add_gate(int sel, UINT16 gdt, UINT16 type)
{
   idt[sel].offset_0=0;
   idt[sel].offset_16=0;
   idt[sel].selector=gdt;
   idt[sel].type=type;
}

void init_idt(void)
{
    int c;

    num_idts=256;
    idt=(GATE*)__flat_alloc(sizeof(GATE)*256);
    memset(idt, 0x0, sizeof(GATE)*256);

   /* flush all IDT entries to a null ISR */
    for(c=0; c<256; c++)
        setup_gate(c, (UINT32)(void*)&dead_isr, CPL0_TRAP_GATE);

    /* setup the first 8 IRQ's */
    for(c=IRQ0; c<=IRQ7; c++)
        setup_gate(c, (UINT32)(void*)&low_irq, CPL0_IRQ_GATE);

    /* setup the second 8 IRQ's */
    for(c=IRQ8; c<=IRQ15; c++)
        setup_gate(c, (UINT32)(void*)&hi_irq, CPL0_IRQ_GATE);

    /* mask the IDT entries for the TRAPS */
   setup_gate(0,  TRAP_NAME(divide),   CPL0_TRAP_GATE);
   setup_gate(1,  TRAP_NAME(debug),    CPL0_TRAP_GATE);
   setup_gate(2,  TRAP_NAME(nmi),       CPL0_TRAP_GATE);
   setup_gate(3,  TRAP_NAME(breakpoint), CPL0_TRAP_GATE);
   setup_gate(4,  TRAP_NAME(overflow), CPL0_TRAP_GATE);
   setup_gate(5,  TRAP_NAME(bounds),    CPL0_TRAP_GATE);
   setup_gate(6,  TRAP_NAME(opcode),    CPL0_TRAP_GATE);
   setup_gate(7,  TRAP_NAME(mathgone), CPL0_TRAP_GATE);
   setup_gate(8,  TRAP_NAME(double),    CPL0_TRAP_GATE);
   setup_gate(9,  TRAP_NAME(mathover), CPL0_TRAP_GATE);
   setup_gate(10, TRAP_NAME(tss),       CPL0_TRAP_GATE);
   setup_gate(11, TRAP_NAME(segment),    CPL0_TRAP_GATE);
   setup_gate(12, TRAP_NAME(stack),    CPL0_TRAP_GATE);
   setup_gate(13, TRAP_NAME(general),    CPL0_TRAP_GATE);
   setup_gate(14, TRAP_NAME(page),    CPL0_TRAP_GATE);
   setup_gate(16, TRAP_NAME(matherr),    CPL0_TRAP_GATE);
   setup_gate(17, TRAP_NAME(align),    CPL0_TRAP_GATE);
   setup_gate(18, TRAP_NAME(mce),       CPL0_TRAP_GATE);

   /* init the IDT into the CPU */
   load_idt(num_idts, idt);
}


void load_idt(int num_idt, GATE* idt_offs)
{
   UINT32   x[2];

   x[0]=((num_idt*sizeof(GATE))-1)<<16;
   x[1]=(UINT32)idt_offs;

   __asm__ __volatile__ ("lidt (%0)"::"d"((int)((unsigned char*)x)+2):"memory");
   io_wait();
}

void guru_meditation(int num)
{
   disable_ints();
   //irqs=0xFFFF;
   //refresh_irqs();
   kprintf("\n\nGuru Meditation :: %i %s\n\n", num, guru[num]);

   while(1);
}
-- Stu --
Slasher

Re:Some IDT Code...in C

Post by Slasher »

hi,
looked at your code above Tom, remove the call to getch() in setup() ! try that! also where did you define err? :-\
jrfritz

Re:Some IDT Code...in C

Post by jrfritz »

DF, where can I download your OSes source to test the code?
jrfritz

Re:Some IDT Code...in C

Post by jrfritz »

Code Slasher, it still triple faults...

I still would like DF's code.
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:Some IDT Code...in C

Post by df »

the code is quite old, should/may/mightnot compile with gcc ;) last know gcc 2.something (2.7.2?/2.6.3?)...

there is a disk image that works in vmware, should work in bochs too...

ftp://ftp.mega-tokyo.com/pub/operating-systems

its rv1.zip
-- Stu --
Slasher

Re:Some IDT Code...in C

Post by Slasher »

Could you post your GDT, IDT setup TOM?
Can see why your code is TRIPLE FAULTING! :'(
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:Some IDT Code...in C

Post by df »

you know my idt code works, as my clock spins at 100 ticks in the top corner of the screen :D
-- Stu --
jrfritz

Re:Some IDT Code...in C

Post by jrfritz »

i'll post my code after I test DF's.
jrfritz

Re:Some IDT Code...in C

Post by jrfritz »

[attachment deleted by admin]
Berserk

Re:Some IDT Code...in C

Post by Berserk »

Hey,

Could somebody please explain to me what an IDT is and what it does (Read my Previous Post)

Ciao ;D
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:Some IDT Code...in C

Post by df »

Tom wrote: Ok, DF's won't work either...i'm going back to Code Slashers...
hmm well I know my code works. right now i'm at work (start of my day, 8am), so i cant look over your gdt stuff here... but my guess is (obviously) something else is wrong ;)
-- Stu --
Slasher

Re:Some IDT Code...in C

Post by Slasher »

hello,
Tom the problem is with err() it is a C function!!!!
C funtions end with
mov esp,ebp
pop ebp
ret
you do not want that in your Interrupt routines!! ;D
you have a number of choices
1. write the Inerrupt routined in ASM
2. add ASM code to the end of err()
option 1 is what i use though ;D

void err(void)
{
panic( "Fault",1 );
asm("popl %eax"); /* remove saved ebp by C*/
asm("popl %eax"); /*remove return address saved by C*/
asm("cli"); /* disable ints so that hlt stops pc*/
asm("hlt");
}

or (NASM)

_err:
push dword 1
push dword fault_msg
call _panic
jmp $
iret ;this is NEVER executed because of jmp $ above
Berserk

Re:Some IDT Code...in C

Post by Berserk »

Hi,

Does jmp $ do the same thing as hlt?

And can i use hlt in protected mode?
Slasher

Re:Some IDT Code...in C

Post by Slasher »

jmp $ - continue loop to the current address
hlt - stops CPU until next interrupt occurs. if interrupts are off then the CPU is stopped indefinately
yes all instructions are available in Protected mode as long as you are at level 0 or your are the OS ;D
Post Reply