Page 3 of 9

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 4:19 pm
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.

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 4:21 pm
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);
}

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 4:28 pm
by Slasher
hi,
looked at your code above Tom, remove the call to getch() in setup() ! try that! also where did you define err? :-\

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 4:38 pm
by jrfritz
DF, where can I download your OSes source to test the code?

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 4:51 pm
by jrfritz
Code Slasher, it still triple faults...

I still would like DF's code.

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 4:52 pm
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

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 4:53 pm
by Slasher
Could you post your GDT, IDT setup TOM?
Can see why your code is TRIPLE FAULTING! :'(

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 4:57 pm
by df
you know my idt code works, as my clock spins at 100 ticks in the top corner of the screen :D

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 4:59 pm
by jrfritz
i'll post my code after I test DF's.

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 5:35 pm
by jrfritz
[attachment deleted by admin]

Re:Some IDT Code...in C

Posted: Mon Dec 16, 2002 1:47 am
by Berserk
Hey,

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

Ciao ;D

Re:Some IDT Code...in C

Posted: Mon Dec 16, 2002 2:15 am
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 ;)

Re:Some IDT Code...in C

Posted: Mon Dec 16, 2002 6:23 am
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

Re:Some IDT Code...in C

Posted: Mon Dec 16, 2002 6:47 am
by Berserk
Hi,

Does jmp $ do the same thing as hlt?

And can i use hlt in protected mode?

Re:Some IDT Code...in C

Posted: Mon Dec 16, 2002 8:37 am
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