Some IDT Code...in C
Re:Some IDT Code...in C
hi,
writing GDT and IDT code in C is so simple!!! why bother trying to fix some bad one when you can write a fresh one that you understand what is going on from the start? >:(
all you have to so is
1. create a descriptor structure that matches the format for GDT or IDT descriptors in the INTEL manuals
2. in GCC make sure you use SHORT for 16 bits and put __attribute__(PACKED) (something like that, not at my pc at home right now to check the right form) at the end of the struct
ie
typedef struct
{
short .....;
char *.....;
}idt_table __attribute__(PACKED);
typedef struct
{
short ...
unsigned char ...
...
}idt_entry __attribute__(PACKED);
3. write a function in asm (NASM) that will receive a pointer to an idt table struct
_set_idt:
push ebp
mov ebp,esp
lidt [ebp+8]
mov esp,ebp
pop ebp
ret
(something like that)
call this from the c file as set_idt(address of idt table)
4.write function to place idt entries at the address of the idt table
can share code if interested!
writing GDT and IDT code in C is so simple!!! why bother trying to fix some bad one when you can write a fresh one that you understand what is going on from the start? >:(
all you have to so is
1. create a descriptor structure that matches the format for GDT or IDT descriptors in the INTEL manuals
2. in GCC make sure you use SHORT for 16 bits and put __attribute__(PACKED) (something like that, not at my pc at home right now to check the right form) at the end of the struct
ie
typedef struct
{
short .....;
char *.....;
}idt_table __attribute__(PACKED);
typedef struct
{
short ...
unsigned char ...
...
}idt_entry __attribute__(PACKED);
3. write a function in asm (NASM) that will receive a pointer to an idt table struct
_set_idt:
push ebp
mov ebp,esp
lidt [ebp+8]
mov esp,ebp
pop ebp
ret
(something like that)
call this from the c file as set_idt(address of idt table)
4.write function to place idt entries at the address of the idt table
can share code if interested!

Re:Some IDT Code...in C
yes...i'd like to see the code...and the function to place idt entries at the address of the idt table.
Re:Some IDT Code...in C
I like C++ more than ASM. So...that's why I'll jut make a GDT and IDT in C/C++.
Re:Some IDT Code...in C
hi,
also like c/c++ but somethings are best described in asm
i.e accessing VESA is easier in ASM than C/C++, but this is another story ;D
Will post code next time i log on, i'm not at home right now and source for my os is not with me
also like c/c++ but somethings are best described in asm
i.e accessing VESA is easier in ASM than C/C++, but this is another story ;D
Will post code next time i log on, i'm not at home right now and source for my os is not with me
Re:Some IDT Code...in C
Hey...VESA...im interested in that too...but first IDT and GDT...please...
Re:Some IDT Code...in C
Does the code you are about to give me have the GDT code for the IDT also? Since the IDT needs the GDT CodeSel.
Re:Some IDT Code...in C
IDT is simple, but how do you mix ASM and C?
How do you call C functions from ASM, when ASM is in a flat binary format?
How do you call C functions from ASM, when ASM is in a flat binary format?
Re:Some IDT Code...in C
Well, Unexpected, you can't.
Look at this! ( hope this is not spam, probly is not since it is OS Dev ):
http://fritzos.sourceforge.net/yabbse/index.php
Look at this! ( hope this is not spam, probly is not since it is OS Dev ):
http://fritzos.sourceforge.net/yabbse/index.php
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Some IDT Code...in C
a trick i've used :Unexpected wrote: IDT is simple, but how do you mix ASM and C?
How do you call C functions from ASM, when ASM is in a flat binary format?
build up a table of functions pointers and give it to your flat ASM (assuming there is a well-known setup function in your flat ASM -- for instance starting at offset 0), then refere to that table (assuming the offset for funtion xyz() is known at assembly time)
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Some IDT Code...in C
i do not recommand at all the writing of interrupt handlers right in C
- the C compiler will add a "push ebp ; mov ebp, esp" that you must take in account in your exit sequence. The problem is that if the compiler thinks it can optimize that out ...
- you should make sure that optimizations will not ruin your plans. For instance, make sure that you have __volatile__ assembly commands (cannot be further optimized or removed)
- you definitively *must* save / restore registers around your handler. returning from an interrupt without restoring registers value will result of randomly registers modifications in the main code ... which will for sure lead to global chaos ...
- you must make sure you found back the stack state you had when entering ... it's common for GCC to post-pone the "add esp,x" that should be done right after a function call ... this means you're not even sure that your "IRET" command will act on the right stack datas ...
Therefore, i strongly suggest you opt for a small ASM-based stub that will call a C function ... that stub can be made generic if wished (i.e. push the interrupt number, then jump to a common stub that will lookup the kernel handlers table with that number as an offset), or be hand-written, or duplicated from a template on demand ... whatever.
If you still wish to code your interrupt in-and-out in plain C, i suggest you give a look at disassembled code (-s option in GCC) to make sure the code generated is the one you'd like to see executed by the CPU ...
- the C compiler will add a "push ebp ; mov ebp, esp" that you must take in account in your exit sequence. The problem is that if the compiler thinks it can optimize that out ...
- you should make sure that optimizations will not ruin your plans. For instance, make sure that you have __volatile__ assembly commands (cannot be further optimized or removed)
- you definitively *must* save / restore registers around your handler. returning from an interrupt without restoring registers value will result of randomly registers modifications in the main code ... which will for sure lead to global chaos ...
- you must make sure you found back the stack state you had when entering ... it's common for GCC to post-pone the "add esp,x" that should be done right after a function call ... this means you're not even sure that your "IRET" command will act on the right stack datas ...
Therefore, i strongly suggest you opt for a small ASM-based stub that will call a C function ... that stub can be made generic if wished (i.e. push the interrupt number, then jump to a common stub that will lookup the kernel handlers table with that number as an offset), or be hand-written, or duplicated from a template on demand ... whatever.
If you still wish to code your interrupt in-and-out in plain C, i suggest you give a look at disassembled code (-s option in GCC) to make sure the code generated is the one you'd like to see executed by the CPU ...