Dual core APIC Timer issue

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
sawdust
Member
Member
Posts: 51
Joined: Thu Dec 20, 2007 4:04 pm

Dual core APIC Timer issue

Post by sawdust »

Hello OS Gurus,

I'm facing some problems with the local-APIC timer.
My config is tyan s2881/dual-core opteron. The second core is masked from Linux OS along
with a piece of memory.
As soon as this AP core is initialized, I'm launching a tiny OS to run on it. This OS has its
own gdt and idt. I've hooked an ISR to be called during local APIC timer interrupts.
The AP core gets the timer interrupt and its handler gets called. But the BSP core also gets
an interrupt and reboots.
I'd assume that the local-APIC's interrupt should not be broadcast.

Same story when I set the local-apic timer to "no interrupts and periodic mode". The BSP core
gets a reset.

However, when I make this as a standalone kernel like PyrOS and
boot with grub in a floppy, it gets executed in the BSP core and runs fine.

Any help will be highly appreciated.

serial out
Welcome to xxxx_OS
.......
apic_id= 1
Apic Ver= 16
maxLVT =4
currentCount = 0xFFFF8C57

### recvd SW interrupt: 80 (APIC int handler)



Here is the APIC code:

Code: Select all

/*Credits: taken from spoon microkernel and pyrOS*/
void load_apic( int cpu_id )
{
    unsigned long decrease,currentCount=0;
    unsigned int m;

    // Software enable the local APIC
    APIC_WRITE( APIC_spurious, ((1<<8 ) | (APIC_INTERRUPT + 15)) );

#if 1 // no intr
	/* Set the divider to 1, no divider */
	APIC_WRITE(APIC_TIMERdivider, APIC_TIMERdivideBy1);

	/* Set the initial counter to 0xffffffff */
	APIC_WRITE(APIC_TIMERinitialCount, 0xffffffff);
	/* Set the apic timer to no interrupts and periodic mode */
	APIC_WRITE(APIC_LVTtimer, (1 << 17)|(1<< 16)|(0 << 12)|(0 << 0)); //no interrupt

#else // intr mode

    APIC_WRITE( APIC_LVTtimer, APIC_INTERRUPT );
    APIC_WRITE( APIC_TIMERdivider, APIC_TIMERdivideBy128 );
    // Starts it counting down..
    APIC_WRITE( APIC_TIMERinitialCount, 0xFFFFFFFF );
    for(m=0;m<699999;m++)  // approx 50 ms
    {
		;
    }
	currentCount = APIC_READ( APIC_TIMERcurrentCount );
    decrease = 0xFFFFFFFF - currentCount;
    APIC_WRITE( APIC_TIMERdivider, APIC_TIMERdivideBy16 );
    APIC_WRITE( APIC_TIMERinitialCount, (unsigned int)decrease );

	/* Set the apic timer to interrupt and periodic mode */
	APIC_WRITE( APIC_LVTtimer, (1 << 17)|(0<< 16)|(0 << 12)|APIC_INTERRUPT);
#endif // if 0
}


void microdelay(unsigned microsecs)
{
	unsigned long start, value, ticks;
	/* Calculate the number of ticks to run, our FSB runs a 200Mhz */
	ticks = microsecs * 200;
	start = APIC_READ(APIC_TIMERcurrentCount);
	do {
		value = APIC_READ(APIC_TIMERcurrentCount);
	} while((start - value) < ticks);
	
}
The IDT hook:

Code: Select all

....
	idt_set_gate(80, (unsigned)apic_isr1, 0x08, 0x8E); // test apic isr hook 0x50
	idt_set_gate(95, (unsigned)apic_isr15, 0x08, 0x8E); // test apic isr
...


int handler:

Code: Select all

void apic_isr_handler(struct REGS *r)
{
		serialOut("### recvd APIC interrupt: %d",r->int_no);
		if(r->int_no >= 80)
		{
			  APIC_SET( APIC_endOfInterrupt, 0 );
		}
}
isr entry:

Code: Select all

.type apic_isr1,@function
apic_isr1:
push $0
push $80
jmp sw_isr_common

.................
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Dual core APIC Timer issue

Post by Brendan »

Hi,
sawdust wrote:I've hooked an ISR to be called during local APIC timer interrupts.
The AP core gets the timer interrupt and its handler gets called. But the BSP core also gets
an interrupt and reboots.
That is impossible - the local APIC timer hardware is not capable of interrupting a different CPU. Even if it was possible I'd assume Linux does have exception handlers (including a double fault exception handler) that would prevent the problem from causing a triple fault/reset.

AFAIK none of the bugs in the code you posted would cause any of the CPUs to reset.
sawdust wrote:However, when I make this as a standalone kernel like PyrOS and
boot with grub in a floppy, it gets executed in the BSP core and runs fine.
That's like saying an eye from a fish works well in a fish, but an eye from a fish doesn't work so well in a goat... ;)


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
sawdust
Member
Member
Posts: 51
Joined: Thu Dec 20, 2007 4:04 pm

Post by sawdust »

Hi Brendan,
Thanks for the clarification. I got this triple fault even by starting the timer in no-interrupt mode. What advice can you give me to debug this one?
Thanks
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Post by Brendan »

Hi,
sawdust wrote:What advice can you give me to debug this one?
Normally I'd suggest debugging it on Bochs, so you can stop everything and examine memory and CPU contents, single-step the code, etc.

I'm not too sure how hard this would be for your "LinuxBIOS + OS + Linux arrangement" though - I don't think LinuxBIOS supports Bochs, so you might need to try Qemu's "half a debugger" as an alternative. Unfortunately, for Qemu you'd be running on a generic SMP Pentium II or something and couldn't configure the emulator to emulate more modern CPUs, which might cause some trouble with your "hardwire rather than auto-detect" code.

Porting LinuxBIOS to Bochs might be the best alternative in the long run - the hardware emulated by Bochs isn't very different to the hardware emulated by Qemu, so it shouldn't be too hard.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
sawdust
Member
Member
Posts: 51
Joined: Thu Dec 20, 2007 4:04 pm

Post by sawdust »

I restarted my expt after the holidays.
As Brendan pointed out, the local APIC interrupts did not interrupt the BSP
core. It was that I did not setup the basic ISRs(0~32) for the AP.

The timer is working in the 2nd core in non-interrupt-periodic mode.

It is only that any kind of interrupt, delivered to the AP core, is resulting
in a GPF with error code 24 (decimal). I tried self-IPI and it's the same case.
I googled for this code, but found nothing.

:?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Post by Brendan »

Hi,
sawdust wrote:It is only that any kind of interrupt, delivered to the AP core, is resulting
in a GPF with error code 24 (decimal). I tried self-IPI and it's the same case.
That means something tried to load the value 0x18 into one of the segment registers, and the CPU rejected the idea. If you can get EIP from the exception handler (and a disassembly) you could see which instruction tried to load the value 0x18 into which segment register.

In any case, either the code shouldn't be loading the value 0x18 into a segment register, or the fourth GDT entry is wrong, or the GDT limit is wrong.
sawdust wrote:I googled for this code, but found nothing.
Googling for code only works if you're writing the same code as someone else. If you are writing the same code as someone else you should probably throw your code away and help the people who didn't need googled code to "write" theirs.... ;)


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
sawdust
Member
Member
Posts: 51
Joined: Thu Dec 20, 2007 4:04 pm

Post by sawdust »

In any case, either the code shouldn't be loading the value 0x18 into a segment register, or the fourth GDT entry is wrong, or the GDT limit is wrong.
Thanks Brendan, I'm a bit slow learner and will ask all stupid questions.
I'm not clear about the fourth GDT entry you mentioned. I've declared only 3 entries, null, CS and DS.

/*null*/
gdt_set_gate(0, 0, 0, 0, 0);

/* Code Segment. */
gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);

/* Data Segment*/
gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);

Googling for code only works if you're writing the same code
Haha.... I tried to say I was googling for the error code. My bad English :D
Your presence in this forum gives me the much needed confidence.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Post by Brendan »

Hi,
Brendan wrote:In any case, either the code shouldn't be loading the value 0x18 into a segment register, or the fourth GDT entry is wrong, or the GDT limit is wrong.
sawdust wrote:I've declared only 3 entries, null, CS and DS.
In that case, the code probably shouldn't be loading the value 0x18 into a segment register:
  • 0x0000 = NULL
    0x0008 = 4GB code segment
    0x0010 = 4GB data segment
    0x0018 = undefined (GPF if you try to use it, and you tried to use it)
    0x0020 = undefined (GPF if you try to use it)
    0x0028 = undefined (GPF if you try to use it)
    0x0030 = undefined (GPF if you try to use it)
    ...
    0xFFF8 = undefined (GPF if you try to use it)
So, did you find out which instruction tried to load 0x18 into a segment register? You should be able to find this from the return value for EIP on the GPF handler's stack (it's just like getting the error code from the GPF handler's stack, which you've already done)...

I could make a silly guess based on no information at all, but instead I'll make 2 silly guesses!

My first guess is your interrupt handler stub is wrong and the GPF occurs when an interrupt handler tries to return from an interrupt, causing the CPU to attempt to load trash into CS or SS (or DS, ES, FS or GS).

My second guess is that (earlier during boot) you use a temporary GDT that does have a valid fourth entry, then you load 0x0018 into a segment register (and it works), but later you switch to a different GDT without reloading segment registers, then push the old segment register on the stack and try to reload it.

To be honest, I like "silly guess #2" the most - it's possible that LinuxBIOS used the first GDT and loaded a segment register with 0x18, and then you started using your own GDT without reloading segment registers.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
sawdust
Member
Member
Posts: 51
Joined: Thu Dec 20, 2007 4:04 pm

Post by sawdust »

To be honest, I like "silly guess #2" the most - it's possible that LinuxBIOS used the first GDT and loaded a segment register with 0x18, and then you started using your own GDT without reloading segment registers.
Brendan, You da man ! It was your "silly guess #2". Thanks a lot for
educating me and fixing the problem :D
Post Reply