I have a small question about referencing interrupt handlers written in assembly from C code, when writing them to interrupt gates for an IDT.
Suppose for instance I wish to reference isr1, my handler for interrupt line 1. In assembly I write it and declare it global. In C I tried declaring isr1 as an external unsigned 32 bit integer, e.g.
extern u32int isr1;
which didn't work, but declaring it as a function as in Bran's tutorial, Molloy's tutorial,
extern void isr1(void);
and then casting
...(u32int) isr1...
works when writing the IDT entries.
Obviously isr1 is a function of the latter type, but why does not simply referencing it as a 32bit code position work the same?
I flicked through my C book but couldn't find the answer, I just don't feel happy that I don't understand why this doesn't work, so any help is appreciated.
Best wishes,
Richard
Referencing interrupt handlers in C
-
- Member
- Posts: 38
- Joined: Mon Mar 24, 2008 4:17 pm
Re: Referencing interrupt handlers in C
Hi,
You're probably not taking the address of the variable.
When you cast a function pointer to an integer, the address of the function is implicitly taken as the function itself has no valid meaning as an integer.
However, casting an integer to an integer will just give you the value of that integer, not its address. To get its address, you use the "&" operator, so you are actually casting from a (u32int*) to an (u32int).
Hope this helps,
James
You're probably not taking the address of the variable.
Code: Select all
extern u32int isr1;
...(u32int)&isr1...;
However, casting an integer to an integer will just give you the value of that integer, not its address. To get its address, you use the "&" operator, so you are actually casting from a (u32int*) to an (u32int).
Hope this helps,
James
-
- Member
- Posts: 38
- Joined: Mon Mar 24, 2008 4:17 pm
Re: Referencing interrupt handlers in C
Ahh of course! So by writing isr1 to the IDT entries, I'm really writing the first 32 bits of the instructions as opposed to the address. That makes sense when I think about how to define strings in assembly. Sorry, I had intended to write the address, and thought the assembly label would be linked as an address, but with a little thought, it isn't.
Thanks for your help, I modified my code and it worked, I will rewrite it again to make sure I understand completely.
Best wishes,
Richard
Thanks for your help, I modified my code and it worked, I will rewrite it again to make sure I understand completely.
Best wishes,
Richard
-
- Member
- Posts: 38
- Joined: Mon Mar 24, 2008 4:17 pm
Re: Referencing interrupt handlers in C
Also, @JamesM, why do you choose 16-bit descriptors in your tutorial? In 32-bit mode, wouldn't it make more sense to have 32-bit descriptors? They seem to work in mine (lower flags of IDT entries).
Richard
Richard
-
- Member
- Posts: 38
- Joined: Mon Mar 24, 2008 4:17 pm
Re: Referencing interrupt handlers in C
Is there and error on your roll-your-own unix clone tutorial on your IDT flag format diagram? Looking at 5-15 in the current Intel manual (system programming, part 1), shouldn't the lower 5 bits of the flag fields be 01110 (which is 14) rather than 00110 which is 6?
Best wishes,
Richard
Best wishes,
Richard
Re: Referencing interrupt handlers in C
Where are you seeing this?
I wrote:See? Very similar to the GDT entry and ptr structs. The flags field format is shown on the right. The lower 5-bits should be constant at 0b0110 - 14 in decimal.
Re: Referencing interrupt handlers in C
Heh, right, yes, typo. There should be 3 1's. It does say "14 in decimal", however...
-
- Member
- Posts: 38
- Joined: Mon Mar 24, 2008 4:17 pm
Re: Referencing interrupt handlers in C
Haha yeah, it does; I had used 3 1s anyway since I was using the Intel manual, and just saw it when I was browsing your code. Just thought you would like to know so you could correct it (or wondered why you had chosen 16-bit descriptors haha).
Best wishes,
Richard
Best wishes,
Richard