Page 1 of 1

Problem with unmasking irq's

Posted: Fri Dec 19, 2003 6:36 am
by petrusss
Hi!
I'm having problems with my OS, I'm trying to fix hardware interrupts but I can't make it work... :|
Software interrupts work perfectly.
When I try to unmask the keyboard, I get a General Protection Fault.

This is my unmasking code:

Code: Select all

void unmaskIRQ(unsigned char irq) {
   if(irq==ALL) {
      outb(MASTERDATA,0x00);
      outb(SLAVEDATA,0x00);
   }
   else {
      irq = irq & (1<<irq);

      if(irq < 8)
         outb(MASTERDATA, irq&0xFF);
      else
         outb(SLAVEDATA, irq>>8);
   }
}

Re:Problem with unmasking irq's

Posted: Fri Dec 19, 2003 6:38 am
by petrusss
And here's the main code:

Code: Select all

extern void kbdHandler_stub();
extern void print_version_stub();

void kbdHandler();
void print_version();

void kstart() {
   INTS(false);
   remapPIC(0x20,0x28);
   maskIRQ(ALL);

   LoadExceptions();

   AddInt(KEYBOARD, kbdHandler_stub, 0);
   AddInt(21, print_version_stub, 0);


   loadIDTR();


   unmaskIRQ(KEYBOARD);
   INTS(true);


   kclearScreen();
   kprintf("Welcome!");

   asm("int $1");

   return;
}

void print_version() {
   kprintf("\n======================\n");
   kprintf("PeOS version 0.0000001\n");
   kprintf("======================\n");

}


void kbdHandler() {
   kprintf("Key pressed!");
   // char scancode = inb(0x60);


    //sendEOI();
   //outb(0x21, EOI);
}
I've tried to put the call to unmaskIRQ elsewhere in the code, but I still get GPF.

Re:Problem with unmasking irq's

Posted: Fri Dec 19, 2003 8:43 am
by Pype.Clicker
petrusss wrote: This is my unmasking code:

Code: Select all

void unmaskIRQ(unsigned char irq) {
   if(irq==ALL) {
      outb(MASTERDATA,0x00);
      outb(SLAVEDATA,0x00);
   }
   else {
      irq = irq & (1<<irq);

      if(irq < 8)
         outb(MASTERDATA, irq&0xFF);
      else
         outb(SLAVEDATA, irq>>8);
   }
}
There are things that looks weird in the "irq = irq & (1<<irq)" command. I would rather have expected something like

Code: Select all

   irqmask = inb(MASTERDATA); 
   irqmask &= (1<<irq);
   outb(MASTERDATA,irqmask);
so that only the bit from "irq" number is toggled, and other are left unchanged.
There are chances that your code also enable IRQ0, which has no handler so far and thus will trigger GPF every 1/18.2 sec.

Re:Problem with unmasking irq's

Posted: Fri Dec 19, 2003 10:15 am
by petrusss
Thanks, I don't get GPF now :)
But I still don't get keypresses :|

Re:Problem with unmasking irq's

Posted: Fri Dec 19, 2003 10:25 am
by Pype.Clicker
where do "kstart()" returns to ? if it's a 'cli; hlt" loop, you're unlikely to get any keypress at all ...

Did you sent an end-of-interrupt signal to the PIC in your handler?
Did you read the byte from the keyboard controller ?

both these stuff are mandatory to get more IRQ1 ?

And what's that "int $1" stuff ??

Re:Problem with unmasking irq's

Posted: Fri Dec 19, 2003 10:36 am
by petrusss
Hahah!
Of course... :]
When I press a key, I start getting alot of GPF's =|
I'll experiment a bit.

Re:Problem with unmasking irq's

Posted: Fri Dec 19, 2003 11:43 am
by petrusss
OK, I can't make it not crash. :|
I get some GPF's (~100), and then it crashes.
This is what I get from bochs:

00083325742i[CPU ] read_virtual_checks(): write beyond limit
00083325790p[CPU ] >>PANIC<< fetch_raw_descriptor: LDTR.valid=0

A keypress starts that GPF loop =|

Re:Problem with unmasking irq's

Posted: Mon Dec 22, 2003 12:52 pm
by Pype.Clicker
it is likely that you have something wrong in your ISR stub, which makes a bad segment selector being popped, which triggers a GPF, etc.