Page 1 of 1

Set interrupt descriptor table in protected mode with C

Posted: Mon Jan 07, 2008 9:54 am
by Lovmy
Hello,

I have little problem, il have make boot disk, and i swap to protected mode.
But i have problem to set timer interrupt.

This is a part of my code C:

Code: Select all

unsigned short irq0_timeout;

struct pIDT
{
	unsigned short limite;
	unsigned long base;
};

struct IDT 
{
	unsigned short offset_0_15;
	unsigned short selecteurSegment;
	unsigned char zero;
	unsigned char drapeau;
	unsigned short offset_16_31;
};

void enable(void)
{
	asm { sti }	
}

void disable(void)
{
	asm { cli }	
}

void activerInterruption( void )
{
	unsigned long adresseINT;
	unsigned char index;
	pointeurIDT.limite = (sizeof (struct IDT) * 256) - 1;
	pointeurIDT.base = (unsigned long)&listeIDT;

	adresseINT = (unsigned long)handlerIRQ;
	for ( index = 0; index < 255; index++ )
	{
		listeIDT[index].offset_0_15 = adresseINT & 0xFFFF;
		listeIDT[index].offset_16_31 = (adresseINT >> 16) & 0xFFFF;
		listeIDT[index].selecteurSegment = 0x08;
	// Numero du selecteur pour le code
		listeIDT[index].zero = 0;					// P DLP
		listeIDT[index].drapeau = 0x8E;				// 1 00 01110
	}
	
	// remapping des interruptions 	
    outp( 0x20, 0x11 );
    outp( 0xA0, 0x11 );
    outp( 0x21, 0x20 );	
// Remaps IRQ0-IRQ7 to 0x20-0x27 in interrupt vector table
    outp( 0xA1, 0x28 );	
// Remaps IRQ8-IRQ15 to 0x28-0x2F in interrupt vector table
    
    outp( 0x21, 0x04 );
    outp( 0xA1, 0x02 );	// PIC2 is connected to PIC1 via IRQ2
    
    outp( 0x21, 0x01 );
    outp( 0xA1, 0x01 );
    outp( 0x21, 0xFE );
    outp( 0xA1, 0xFF );
	
    adresseINT = (unsigned long)&pointeurIDT;
    asm {
		LIDT FWORD PTR adresseINT
	}
	enable();
}

void handlerIRQ(void)
{
	// AX,BX,CX,DX,SP,BP,SI et DI
	asm {
		PUSHAD
	}
	disable();
	if ( irq0_timeout < 65535 )
		irq0_timeout++;
	eoi();
	enable();
	asm {
		POPAD
		POP EBP
		IRET
	}
}

void desactiverInterruption( void )
{
	disable();
	outp( 0x21, 0xFF );
	outp( 0xA1, 0xFF );
}

void eoi(void)
{
	outp( 0x20, 0x20 );
	outp( 0xA0, 0x20 );
}
When i call activerInterruption, i have reboot.

I have add:

Code: Select all

POP EBP
IRET
to handlerIRQ because BCC32 don't support interrupt declaration of function.

Can you help me ?
I use BCC32 and TLINK32 to compile.

Best regards !

Re: Set interrupt descriptor table in protected mode with C

Posted: Mon Jan 07, 2008 10:09 am
by jal
Lovmy wrote:Can you help me ?
Look at disassembler output of the function to see what BCC generates 'under water' like stack frame etc. It's very likely that before your 'pushad' line is executed, some registers are messed with (like bp set to sp). It's better to first make an interrupt routine in assembler, imho.


JAL

Disassemble of handlerIRQ

Posted: Mon Jan 07, 2008 10:20 am
by Lovmy
This is assembler code for handlerIRQ:

Code: Select all

55			push ebp
8BEC			mov ebp,esp

60			pusha
E856FEFFFF		call 0x1cfa 			-> disable()
66833D1C3C4000FF	cmp word [0x403c1c],byte -0x1	-> if ( irq0_timeout < 65535 )
7307			jnc 0x1eb5
66FF051C3C4000		inc word [0x403c1c]		-> irq0_timeout++;
E835000000		call 0x1eef			-> eoi();
E835FEFFFF		call 0x1cf4			-> enable();
61			popa
5D			pop ebp
CF			iret

5D			pop ebp
C3			ret
I have had popa, pop ebp and iret because BCC32 generate onyl pop ebp and ret.

Thank you for help !

Posted: Mon Jan 07, 2008 1:22 pm
by cyr1x
You could trying to add somthing like this "__declspec(naked) void MyFunction();"
I'm not sure if bcc32 supports this.

Assembler

Posted: Mon Jan 07, 2008 1:41 pm
by Lovmy
Hello,

OK __declspec(naked) delete line:

Code: Select all

push ebp
mov ebp,esp
before code of procedure and

Code: Select all

pop ebp
ret
after code of procedure.
Can you write example of interrupt function in assembleur 32 bits for i386 ?

Thank you !

Posted: Mon Jan 07, 2008 2:16 pm
by Combuster
if you are looking for code, you should read bran's kernel tutorial. It has everything including interrupt handlers.

basically, what an interrupt handler looks like is this:

-save the segment registers you are going to use
-save the general purpose registers you are going to use
-load the segment registers as needed
-execute whatever you need
-restore the GPRs and segment registers

Re: Assembler

Posted: Mon Jan 07, 2008 6:21 pm
by Ready4Dis
Lovmy wrote:Hello,

OK __declspec(naked) delete line:

Code: Select all

push ebp
mov ebp,esp
before code of procedure and

Code: Select all

pop ebp
ret
after code of procedure.
Can you write example of interrupt function in assembleur 32 bits for i386 ?

Thank you !
Here is an example of my timer interrupt:

Code: Select all

timer_irq:
	push	eax								;Store eax incase we interrupted in the middle of a process!
	add		dword	[TimerTicksLow],	1
	adc		dword	[TimerTicksHigh],	0
	cmp		dword	[TimerWait],		0	;Check if it's zero already
	je		.TimerDone
	dec		dword	[TimerWait]				;Delay countdown
.TimerDone
	mov		al, 0x20
	out		0x20, al
	pop		eax
	iret

Re: Assembler

Posted: Tue Jan 08, 2008 2:43 am
by jal
Lovmy wrote:Hello,

OK __declspec(naked) delete line:

Code: Select all

push ebp
mov ebp,esp
before code of procedure and

Code: Select all

pop ebp
ret
after code of procedure.
Can you write example of interrupt function in assembleur 32 bits for i386 ?
Well, at first glance everything seems fine. The crash must occur somewhere else. You didn't write I think which virtual machine you use. If you use Bochs or Qemu, isn't there some output indicating where it goes wrong?


JAL