Set interrupt descriptor table in protected mode with C

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Lovmy
Posts: 17
Joined: Fri Oct 05, 2007 5:58 am

Set interrupt descriptor table in protected mode with C

Post 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 !
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Set interrupt descriptor table in protected mode with C

Post 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
Lovmy
Posts: 17
Joined: Fri Oct 05, 2007 5:58 am

Disassemble of handlerIRQ

Post 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 !
cyr1x
Member
Member
Posts: 207
Joined: Tue Aug 21, 2007 1:41 am
Location: Germany

Post by cyr1x »

You could trying to add somthing like this "__declspec(naked) void MyFunction();"
I'm not sure if bcc32 supports this.
Lovmy
Posts: 17
Joined: Fri Oct 05, 2007 5:58 am

Assembler

Post 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 !
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post 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
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Ready4Dis
Member
Member
Posts: 571
Joined: Sat Nov 18, 2006 9:11 am

Re: Assembler

Post 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
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Assembler

Post 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
Post Reply