getting a tripple fault when a key is pressed

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
0Scoder
Member
Member
Posts: 53
Joined: Sat Nov 11, 2006 8:02 am

getting a tripple fault when a key is pressed

Post by 0Scoder »

I have been working for weeks trying to fix a bug in my OS but to no avail. The bug is that when I press a key it page faults. I have traced this to be nothing in the key handler, and unnaffected by what is in the idt pointer structure. However, the faulting address (in CR2) changes with the value I put into the lidt instruction, so I pressume this is either a problem with my inline assembly (I definetly need more practise), or in some way with my paging code. Both I have included here in part, and also an extract from the bochs debug file...

IDT code:

Code: Select all

struct gate_descriptor idt[255];                 
struct descriptor_table_pointer idt_pointer;

void init_idt()
{
 int count;
     idt_pointer.limit=(256*sizeof(struct gate_descriptor))-1;   /*the size of the idt*/
    idt_pointer.base=(unsigned int)&idt;                  /*actuall pointer to the idt*/
    for(count=0; count<255; count++)                     /*Clear the idt*/
     free_int(count);
    asm volatile("lidt %0\n\t" :: "m" (idt_pointer));         /*load the idt*/
 return;
}

void alloc_int(unsigned char int_no, unsigned long offset, unsigned char dpl)
{
   idt[int_no].offset_high=((offset>>16) & 0xFFFF);
   idt[int_no].attributes=0x8E;
   idt[int_no].unused=0;
   idt[int_no].selector=0x08;
   idt[int_no].offset_low=(offset & 0xFFFF);
}

void free_int(unsigned char int_no)
{
   idt[int_no].offset_high=0;
   idt[int_no].attributes=0;
   idt[int_no].unused=0;
   idt[int_no].selector=0;
   idt[int_no].offset_low=0;
}
This is the part of the keyboard handler that sets the int up:

Code: Select all

/*Init function:*/
void keyboard_init()
{
   alloc_int(_IRQ_0+1,(unsigned long int)&keyboard_handler,IDT_RING0);
   enable_irq(1);
}
My paging code:

Code: Select all

void init_paging()
{
   unsigned long *page_directory = (unsigned long*) alloc_page(1);
   unsigned long *page_table = (unsigned long*) alloc_page(1);
   unsigned long *page_table_2 = (unsigned long*) alloc_page(1);

   unsigned long address=0;               //holds the physical address of a page
    unsigned int counter;
   unsigned long temp;
   
/*Mark all page directories as empty:*/
   for(counter=0; counter<1024; counter++)
   {
      page_directory[counter] = 0 | 2;      //supervisor, writeable, not present
   }
   
/*Fill up our first page table (lower 4mb):*/
   for(counter=0; counter<1024; counter++)
   {
      page_table[counter] = address | 3;      //supervisor, writeable, present
      address = address + 4096;            //4096 bytes - one page
   };

/*Fill up our second page table (lower 4mb):*/
   for(counter=0; counter<1024; counter++)
   {
      page_table_2[counter] = 0 | 2;         //supervisor, writeable, not present
   };

   page_table_2[0]=(int)page_table;         /*Map in lower page table*/
   page_table_2[0]=page_table_2[0] | 3;

   page_table_2[1022]=(int)page_directory;      /*Map in page directory*/
   page_table_2[1022]=page_table_2[1022] | 3;

   page_table_2[1023]=(int)page_table_2;      /*Map in self*/
   page_table_2[1023]=page_table_2[1023] | 3;
   
/*Add the pages to the page directory:*/
   page_directory[0]=(int)page_table;         /*attribute set to: supervisor level, read/write, present(011 in binary)*/
   page_directory[0]=page_directory[0] | 3;
   page_directory[1023]=(int)page_table_2;      /*attribute set to: supervisor level, read/write, present(011 in binary)*/
   page_directory[1023]=page_directory[0] | 3;

/*Enable paging:*/
   temp=(unsigned long)page_directory;
   write_cr3(temp);
   get_cr0(temp);
   write_cr0((temp | 0x80000000));
}
Here is the part from the Bochs debug file:

Code: Select all

00006983000d[KBD  ] keyboard: writing translated 20
00006983000d[KBD  ] service_keyboard: key in internal buffer waiting
00006983250d[PIC  ] IRQ line 1 now high
00006983250d[PIC  ] signalling IRQ(1)
00006983250d[CPU  ] interrupt(): vector = 129, INT = 0, EXT = 1
00006983250d[CPU  ] exception(0e h)
00006983250d[CPU  ] interrupt(): vector = 14, INT = 0, EXT = 1
00006983250d[CPU  ] exception(0e h)
00006983250d[CPU  ] interrupt(): vector = 8, INT = 0, EXT = 1
00006983250d[CPU  ] exception(0e h)
00011669250p[CPU  ] >>PANIC<< exception(): 3rd (14) exception with no resolution
Thanks in advance for helping out,
OScoder
AR

Re:getting a page fault when a key is pressed

Post by AR »

Your inline ASM says "lidt %0", it should be "lidt (%0)" IIRC.
0Scoder
Member
Member
Posts: 53
Joined: Sat Nov 11, 2006 8:02 am

Re:getting a page fault when a key is pressed

Post by 0Scoder »

Thanks, but unfortunately the problem is still there.

I am a bit suspisious of this part though:

Code: Select all

/*Enable paging:*/
   temp=(unsigned long)page_directory;
   write_cr3(temp);
   get_cr0(temp);
   write_cr0((temp | 0x80000000));
Mainly due to what the registers contain:

Code: Select all

00008696250i[CPU  ] | EAX=00000000  EBX=00004610  ECX=0010282c  EDX=00000000
00008696250i[CPU  ] | ESP=0000ffe4  EBP=0000ffe8  ESI=00007c06  EDI=00000005
00008696250i[CPU  ] | IOPL=0 NV UP EI PL NZ NA PE NC
00008696250i[CPU  ] | SEG selector     base    limit G D
00008696250i[CPU  ] | SEG sltr(index|ti|rpl)     base    limit G D
00008696250i[CPU  ] |  DS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00008696250i[CPU  ] |  ES:0010( 0002| 0|  0) 00000000 000fffff 1 1
00008696250i[CPU  ] |  FS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00008696250i[CPU  ] |  GS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00008696250i[CPU  ] |  SS:0010( 0002| 0|  0) 00000000 000fffff 1 1
00008696250i[CPU  ] |  CS:0008( 0001| 0|  0) 00000000 000fffff 1 1
00008696250i[CPU  ] | EIP=0010024c (0010024c)
00008696250i[CPU  ] | CR0=0xe0000011 CR1=0x00000000 CR2=0x00ff2040
00008696250i[CPU  ] | CR3=0x003fe000 CR4=0x00000000
Also I really should have included the asm things before:

Code: Select all

 #define sti()            asm("sti")
 #define cli()            asm("cli")
 #define write_cr0(data)   asm volatile("movl %0,%%cr0 \n\t" :: "r" (data))
 #define write_cr3(data)   asm volatile("movl %0,%%cr3 \n\t" :: "r"(data))
 #define get_cr0(data)      asm volatile("movl %%cr0,%0 \n\t" : "=r"(data))

 #define get_cs(data)      asm volatile("movw %%cs,%%ax \n\t":"=a"(data))
I'll attach all of the code to this message so people can have a proper look at it. Also I have included my bochssrc.txt file in the bin directory. Thanks to anyone who takes their time out to help me...

Oh yeah to make the whole thing use make alll, to make the image I use in bochs type make image, also you need to use make clean after every make as one file conflicts sometimes. I'll be fixing this later.
Dex4u

Re:getting a page fault when a key is pressed

Post by Dex4u »

I had a similar problem in my OS, when i first started OS Dev, it turn out to be using "sti" and "cli" in my keyboard function.
Lazy Neuromancer

Re:getting a tripple fault when a key is pressed

Post by Lazy Neuromancer »

Eheheh, i had this problem some time ago. ;D

One damn week of debugging, and then I understood that the IRQ handler was running out of stack (causing a unnoticed stack overflow) overwriting the system process list :P

Check your ESP.
0Scoder
Member
Member
Posts: 53
Joined: Sat Nov 11, 2006 8:02 am

Re:getting a tripple fault when a key is pressed

Post by 0Scoder »

Ok, I've fixed the bug(s) now. For one thing my lidt inline asm was wrong, it should have used (r) instead of (m). The other was that in my very bad asm coding I made it so that the get_cr0 macro actually got cr3; because I wrote part of this back to cr0, I had inadvertently switched back to real mode! The third and final problem I had was that I had got some of the sizes wrong for the idt_pointer struct, so it wasn't pointing to my idt correctly. Thankyou very much those people who helped me especially candy who I talked to on msn.

Here is the fixed code incase anyone is interested:

Code: Select all

struct descriptor_table_pointer
{
   unsigned short int limit;
   unsigned long int base;
} __attribute__((__packed__));

Code: Select all

asm volatile("lidt (%0)" :: "m" (idt_pointer));            

Code: Select all

#define get_cr0(data)   asm volatile("movl %%cr0,%0 \n\t" : "=r"(data))
Also one last thing was that it only worked in bochs as I had forgotten to clear the scancode table, as bochs starts with all memory 0'ed out so it didn't matter, but it was not so on my test machine!
Post Reply