OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Apr 23, 2024 5:30 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: No interrupts get called
PostPosted: Fri May 05, 2017 2:02 pm 
Offline
Member
Member
User avatar

Joined: Fri Apr 08, 2016 5:03 am
Posts: 132
Location: atapio.cpp - why won't you work :(
Hi :) I'm trying to make my own interrupt handler (first time I ever do that) but I'm struggling to get it working :(

To test if the problem was the C part, I added a 'call panic' command right at the beggining of every isr, but I still don't get anything :(

The code I got if made of some parts from the wiki and from a french website about osdeving.

Here is the code (please don't kill me if it's sh*t xD):

idt.c
Code:
#include <idt.h>
#include <stddef.h>
#include <stdint.h>

void idt_entry(int num, uint32_t offset, uint16_t selector, uint8_t type_attr){
   IDT[num].offset_1 = (offset & 0xffff);;
   IDT[num].selector = selector;
   IDT[num].zero = 0;
   IDT[num].type_attr = type_attr;
   IDT[num].offset_2 = (offset & 0xffff0000) >> 16;
}

void loadIDT(){
   IDT_Pointer.limit = (sizeof(struct idt_entry) * IDT_SIZE) - 1;
   IDT_Pointer.base = &IDT;
   for (int i = 0; i < IDT_SIZE; i++){
      idt_entry(i, (uint32_t)&_asm_int_default_hdlr, 0x08,0x0E);
   }
   idt_entry(33, (uint32_t)&_asm_int_2_hdlr, 0x08, 0x0E);
   _loadIDT();
}

void _int_0_hdlr(){

}

void _int_1_hdlr(){

}

void _int_2_hdlr(){

}


idt.h
Code:
#include <stddef.h>
#include <stdint.h>

#define IDT_BASE  0x800
#define IDT_SIZE  0xFF

#define INTGATE  0x8E00

struct idt_entry {
   uint16_t offset_1; // offset bits 0..15
   uint16_t selector; // a code segment selector in GDT or LDT
   uint8_t zero;      // unused, set to 0
   uint8_t type_attr; // type and attributes, see below
   uint16_t offset_2; // offset bits 16..31
} __attribute__((packed));

struct idt_ptr {
   unsigned short limit;
   unsigned int base;
} __attribute__((packed));

struct idt_entry IDT[IDT_SIZE];
struct idt_ptr IDT_Pointer;

extern void _loadIDT();
extern void _asm_int_default_hdlr();
extern void _asm_int_0_hdlr();
extern void _asm_int_1_hdlr();
extern void _asm_int_2_hdlr();

void idt_entry(int num, uint32_t offset, uint16_t selector, uint8_t type_attr);
void loadIDT();


idt_loader.asm
Code:
.file "idt.c"
.global _loadIDT
.type _loadIDT, @function

.extern IDT_Pointer

_loadIDT:
   lidt IDT_Pointer
   mov 0x10, %eax
   mov %ds, %eax
   mov %es, %eax
   mov %fs, %eax
   mov %gs, %eax
   mov %ss, %eax
   jmp flush

flush:
   ret


interrupt_handler.asm
Code:
.file "idt.c"
.global _asm_int_default_hdlr
.global _asm_int_0_hdlr
.global _asm_int_1_hdlr
.global _asm_int_2_hdlr
.extern _int_0_hdlr
.extern _int_1_hdlr
.extern _int_2_hdlr

_asm_int_default_hdlr:
   call panic
   iret

_asm_int_0_hdlr:
   call panic
   #pushad
    cld
    call _int_0_hdlr
    #popad
    iret

_asm_int_1_hdlr:
   call panic
   #pushad
    cld
    call _int_1_hdlr
    #popad
    iret

_asm_int_2_hdlr:
   call panic
   #pushad
    cld
    call _int_2_hdlr
    #popad
    iret


Hope someone cal help me :)

_________________
My github page: https://github.com/AlexandreRouma
Meme-deving since 420 Bc !
YouTube: https://www.youtube.com/channel/UCyJnOD ... C8Y7pccc6A
Twitter: https://twitter.com/WhatsTheGeekYT


Top
 Profile  
 
 Post subject: Re: No interrupts get called
PostPosted: Fri May 05, 2017 3:01 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 21, 2011 9:47 pm
Posts: 286
Location: Tustin, CA USA
Mask interrupts? http://wiki.osdev.org/8259_PIC#Masking

Enable interrupts (sti)?

_________________
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber


Top
 Profile  
 
 Post subject: Re: No interrupts get called
PostPosted: Fri May 05, 2017 3:08 pm 
Offline
Member
Member
User avatar

Joined: Fri Apr 08, 2016 5:03 am
Posts: 132
Location: atapio.cpp - why won't you work :(
Thanks for answering :)

What a stupid mistake xD

I called sti after loading idt, and, It seems to work ! BUT when any interrupt is called, the vm reboots :(

_________________
My github page: https://github.com/AlexandreRouma
Meme-deving since 420 Bc !
YouTube: https://www.youtube.com/channel/UCyJnOD ... C8Y7pccc6A
Twitter: https://twitter.com/WhatsTheGeekYT


Top
 Profile  
 
 Post subject: Re: No interrupts get called
PostPosted: Fri May 05, 2017 3:22 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 21, 2011 9:47 pm
Posts: 286
Location: Tustin, CA USA
DeezRamChips wrote:
BUT when any interrupt is called, the vm reboots


probably something like...

IRQ0 (or similar) -- no handler
Double Fault -- no handler
Triple fault -- reboot

Fill in all the exceptions and faults in the IDT so you have something to work with.

_________________
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber


Top
 Profile  
 
 Post subject: Re: No interrupts get called
PostPosted: Fri May 05, 2017 3:35 pm 
Offline
Member
Member
User avatar

Joined: Fri Apr 08, 2016 5:03 am
Posts: 132
Location: atapio.cpp - why won't you work :(
I already assign an isr to every interrupt :/ It's done in the loadIDT() function:

Code:
for (int i = 0; i < IDT_SIZE; i++){
   idt_entry(i, (uint32_t)&_asm_int_default_hdlr, 0x08,0x0E);
}

_________________
My github page: https://github.com/AlexandreRouma
Meme-deving since 420 Bc !
YouTube: https://www.youtube.com/channel/UCyJnOD ... C8Y7pccc6A
Twitter: https://twitter.com/WhatsTheGeekYT


Top
 Profile  
 
 Post subject: Re: No interrupts get called
PostPosted: Fri May 05, 2017 3:53 pm 
Offline
Member
Member
User avatar

Joined: Fri Aug 07, 2015 6:13 am
Posts: 1134
DeezRamChips wrote:
I already assign an isr to every interrupt :/ It's done in the loadIDT() function:

Code:
for (int i = 0; i < IDT_SIZE; i++){
   idt_entry(i, (uint32_t)&_asm_int_default_hdlr, 0x08,0x0E);
}


You don't need an ISR for every interrupt. You need 32 ISRs for 32 different exceptions and other 224 entries for 244 interrupt request handlers. You should also remap your Programmable Interrupt Controller (PIC) so your exceptions don't interfere with your interrupt request handlers. When an exception occurs you should probably display something similar to a BSOD, halting the CPU if the kernel couldn't be recovered. I would suggest you taking a look at assembly macros for easier code management, you don't need to prototype all those entries by hand. Also don't forget about call gates if you are planning on using and IDT and interrupts for system calls. Those will probably have different flags and selectors. Every interrupt request should/could have a different handler (common assembly handler + different C handler scheme).

_________________
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader


Top
 Profile  
 
 Post subject: Re: No interrupts get called
PostPosted: Fri May 05, 2017 4:49 pm 
Offline
Member
Member
User avatar

Joined: Fri Apr 08, 2016 5:03 am
Posts: 132
Location: atapio.cpp - why won't you work :(
Before doing all of this, I think it would be better to have working interrupts :)

One thing I know, is that it's not related to the loading of the IDT, it only happens when I run 'sti' :/

_________________
My github page: https://github.com/AlexandreRouma
Meme-deving since 420 Bc !
YouTube: https://www.youtube.com/channel/UCyJnOD ... C8Y7pccc6A
Twitter: https://twitter.com/WhatsTheGeekYT


Top
 Profile  
 
 Post subject: Re: No interrupts get called
PostPosted: Fri May 05, 2017 6:54 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 21, 2011 9:47 pm
Posts: 286
Location: Tustin, CA USA
DeezRamChips wrote:
it only happens when I run 'sti'


Ok, let's walk through this a bit more... Maybe we can get there.

When you execute the 'sti' opcode, you are enabling interrupts. If you are experiencing a reboot right after that statement (I'm sure you have a debug statement in your code so you know this rather than presume), then you are getting an interrupt immediately. 'sti' alone will not cause a reboot.

Now. We know we are having a problem related to an interrupt. The causes are that the IDT is not set up (which it is as you indicate) or the IDT is set up incorrectly (a possibility, and generating a GPF) or it might be an alignment problem (but I cannot confirm it will manifest itself right after sti). Continuing down this line, if the interrupt or GPF is not handled properly, then you will get a Double Fault and if that is still unhanded a Triple Fault which leads to a reboot.

Now, if you have a common handler set up for all interrupts and that common handler is never called (a panic function call first thing as you mentioned), then you have to assume that the interrupt is never getting to the interrupt handler code. I would look into idt_entry() in particular and bit-map all the values you are setting -- like Present...?

_________________
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber


Top
 Profile  
 
 Post subject: Re: No interrupts get called
PostPosted: Sat May 06, 2017 1:35 pm 
Offline
Member
Member
User avatar

Joined: Fri Apr 08, 2016 5:03 am
Posts: 132
Location: atapio.cpp - why won't you work :(
Ok, thank you for answering :) I'll definitly check the structures tomorow !

I was wondering if the problem could be related to the GDT since I also created the functions to load it myself :/

_________________
My github page: https://github.com/AlexandreRouma
Meme-deving since 420 Bc !
YouTube: https://www.youtube.com/channel/UCyJnOD ... C8Y7pccc6A
Twitter: https://twitter.com/WhatsTheGeekYT


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], MichaelPetch, SemrushBot [Bot] and 116 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group