Page 2 of 9

Re:Some IDT Code...in C

Posted: Thu Dec 12, 2002 12:07 pm
by Unexpected
Ok I make handlers in ASM... But how can I call C functions?

_IRQ1:
...??...
IRET

For exeample how to call Print(message) function?

Re:Some IDT Code...in C

Posted: Thu Dec 12, 2002 12:37 pm
by jrfritz
Code slasher, could you show me a function that loads the IDT in the right place?

Re:Some IDT Code...in C

Posted: Thu Dec 12, 2002 12:41 pm
by whyme_t
Just to show what Pype was explaining, the asm code produced:

Code: Select all

000000ae <_Exp_Reserved>:
  ae:   55                      push   %ebp
  af:   89 e5                   mov    %esp,%ebp
  b1:   cf                      iret   
  b2:   5d                      pop    %ebp
  b3:   c3                      ret  
This clearly shows your Exp_Reserved function returning from interrupt before ebp is popped back off.

You can call your C functions, call _Print. But better not pass in parameters unless you know what your doing. Follow Pype's advice, use a stub which will take care of the nitty gritty stuff, before and after calling your C isr.

Re:Some IDT Code...in C

Posted: Thu Dec 12, 2002 12:58 pm
by Slasher
hi,
Tom, the funtion is just a call to lidt i.e

(Nasm)
_set_idt:
push ebp
mov ebp,esp
lidt [ebp+8]
mov esp,ebp
pop ebp
ret
this function in C, is

void set_idt(descriptor_reg *idt_ptr);

so from definitions in my post above,
if you define

descriptor_reg idt;

and then somewhere in your setup

idt.base=0x00000600;(notice base address is written out completely and not 0x0600)
idt.limit=256*8 - 1;

To put the idt at absolute address 0x0600 with 256 possible entries
just call

set_idt(&idt);

done ;D

Re:Some IDT Code...in C

Posted: Thu Dec 12, 2002 1:02 pm
by jrfritz
Ok....one last thing...

Do you have the al 256 entries do to a test with?

Re:Some IDT Code...in C

Posted: Thu Dec 12, 2002 1:12 pm
by jrfritz
Oh wait...i'll just use some source that I already had and use a for ( i < 255 )

Re:Some IDT Code...in C

Posted: Thu Dec 12, 2002 1:16 pm
by Slasher
dont understand you?? :(
do you mean, do you have 256 entries to use?
well, that is up to you and how you set the limit field in the idt. if you set idt.limit to 32*8 - 1,
you will only be able to handle interrupts 0 to 31 (first 32 ints) :)
in my previous post i sent a file, help.txt, that contained

void set_idt_entry(unsigned long num,void (*funct)(void));

to set int 0 to call a funtion TIMER, use

set_idt_entry(0,TIMER);

to set int 40 (0x28) to call function SYSTEM_CODE

set_idt_entry(40,SYSTEM_CODE);
that is all you need.

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 5:32 am
by Berserk
Hey,

First of all, What is an IDT, i know that it is an Interrupt Descriptor Table, but what does it do? Does it allow you to access BIOS interrupts from Protected Mode?

Well, I am not going to make one yet, First i go to actually get INTO Protected Mode ;D I was just wondering what it is ;)

So, Could somebody Please help me in the Where do i Start thread ;D

Ciao ;D

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 2:12 pm
by jrfritz
This code triple faults bochs when I sti:


extern "C" long SYS_CODE_SEL;

void err()
{
   panic( "Fault",1 );
}

typedef struct
{
unsigned short limit; /*16 bits*/
unsigned short base_low;
unsigned char base_mid; /*8 bits*/
unsigned char access;
unsigned char attribute;
unsigned short base_hi;
} __attribute__ ((packed)) gdt_entry;

typedef struct
{
unsigned short offset_low;
unsigned short selector;
unsigned char param_count;
unsigned char access;
unsigned short offset_high;
} __attribute__ ((packed)) idt_entry;

typedef struct
{
unsigned short limit; /*16 bits*/
unsigned long base; /*32 bits*/
} __attribute__ ((packed)) descriptor_reg;

descriptor_reg idt,gdt;

/* in my kernel initialization funtion idt is set up as follows
same idea applies when you want to change the initial boot to Pmode gdt

idt.limit=8*256-1;
idt.base=0x00000600;
*/


int set_idt_entry(unsigned long num,void (*funct)(void))
{
/*KERNEL_CODE_SEL is my kernel code selector in the GDT */
/*read INTEL MANUAL for explaination of each value and its meaning*/

   idt_entry* idt_temp;

idt_temp=(idt_entry *)(idt.base+(num * 8));

idt_temp->offset_low=(unsigned short) (((unsigned long)funct & 0xffff));
idt_temp->selector=(unsigned short)SYS_CODE_SEL;
idt_temp->access=(unsigned char)0x8e;
idt_temp->param_count=(unsigned char)0;
idt_temp->offset_high=(unsigned short)(((unsigned long)funct >> 16));

return 0;
}

extern "C" void loadidt(descriptor_reg *idt_ptr);

void init_idt(void) {

idt.limit=32*8-1;
idt.base=0x00000600;

set_idt_entry( 0x0, err );
set_idt_entry( 0x1, err );
set_idt_entry( 0x2, err );
set_idt_entry( 0x3, err );
set_idt_entry( 0x4, err );
set_idt_entry( 0x5, err );
set_idt_entry( 0x6, err );
set_idt_entry( 0x7, err );
set_idt_entry( 0x8, err );
set_idt_entry( 0x9, err );
set_idt_entry( 0xA, err );
set_idt_entry( 0xB, err );
set_idt_entry( 0xC, err );
set_idt_entry( 0xD, err );
set_idt_entry( 0xE, err );
set_idt_entry( 0xF, err );
set_idt_entry( 0x10, err );
set_idt_entry( 0x11, err );
set_idt_entry( 0x12, err );
set_idt_entry( 0x13, err );
set_idt_entry( 0x14, err );
set_idt_entry( 0x15, err );
set_idt_entry( 0x16, err );
set_idt_entry( 0x17, err );
set_idt_entry( 0x18, err );
set_idt_entry( 0x19, err );
set_idt_entry( 0x1A, err );
set_idt_entry( 0x1B, err );
set_idt_entry( 0x1C, err );
set_idt_entry( 0x1D, err );
set_idt_entry( 0x1E, err );
set_idt_entry( 0x1F, err );
}

start(){    init_idt();

   loadidt( &idt );

   getch();

   sti();}

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 2:40 pm
by Slasher
hi,
SYS_CODE_SEL is just a number that matchs the CODE_SEL constant in the ASM file
its not available when you are linking cause in the the asm file it is an EQU definition
ie SYS_CODE_SEL equ 0x18
so if in the ASM file SYS_CODE_SEL is 0x18
just
#define SYS_CODE_SEL 0X18
in a header file for the c code

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 4:08 pm
by jrfritz
I have a problem...here is my SYS_CODE_SEL:

; ring 0 kernel code segment descriptor
SYS_CODE_SEL   equ   $-gdt
gdt2:
   dw 0FFFFh
   dw 0      ; (base gets set above)
   db 0
   db 9Ah      ; present,ring 0,code,non-conforming,readable
   db 0CFh
   db 0

How to I put this in C++ code?

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 4:12 pm
by Unexpected
My IDT is still not working...
Hey, can anyone put FULL IDT source in C?
With all exeptions.
Please :(

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 4:13 pm
by Slasher
okay,
each selector is 8 bytes long.
so if your SYS_CODE_SEL is the 2nd in the GDT then it has value 8*2= 16 or 0x10
just
#define SYS_CODE_SEL 0x10 in your C++ file.
done. ;D

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 4:14 pm
by jrfritz
That's what me and code slasher are working on ;)

I'll help you when I get mine working...i'll give you the full working and tested code. :)

Re:Some IDT Code...in C

Posted: Sun Dec 15, 2002 4:15 pm
by jrfritz
Still still triple fault...