Page 1 of 1
AP startup issue
Posted: Sun Feb 15, 2009 11:37 am
by worldsapart
Hi guys,
I've been trying to start up my APs. what i did initially ws jus make them increment a counter in memory. And that worked and i ws getting them al started up. But after that I tried setting up gdt and tryin to jump them into protected mode. It jus doesnt seem to work. it breaks... Triple fault!
I'd really appreciate it if u guys could take a look at my code and tel me wat's wrong. What i do, is i copy the AP startup code to a low memory location and the APs jump to it on startup. I know it's something to do wit me referencing symbols not accesible to the APs.. but i jus cant seem to find a way around it.. Thanks for ur time guys..
Code: Select all
bits 16 ; We are still in 16 bit Real Mode
global AP_startup
extern AP_main
extern AP_stage3
extern Stage3
;*************************************************;
; Bootloader Entry Point
;*************************************************;
AP_startup:
; Incrementing processor count - THIS WORKS FINE. SO APs ARE STARTING UP
mov bx, 500h
lock
inc byte [bx]
mov ax, cs
mov ds, ax
;EFLAGS
push 2
popf
mov bx, 0x600
;Loading GDT and IDT
lgdt [bx]
; Enabling Protected mode
mov eax, cr0
or al, 1
mov cr0, eax
; Far jump... THIS IS WAT IS SCREWED.. I GET A RELOCATION ERROR
jmp 0x8:Pmode
bits 32
Pmode:
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
call AP_main
cli ; Clear all Interrupts
hlt ; halt the system
Hope it's readable. Thanks again.
Re: AP startup issue
Posted: Sun Feb 15, 2009 11:49 am
by 01000101
you should setup *all* of your segment registers and get a stack setup before you start using any memory offsets and the PUSH/POP instructions.
Also, I don't see where you unlock your bus lock... you do an "inc [lockspace]", but you never alter that memory location again.
Re: AP startup issue
Posted: Sun Feb 15, 2009 12:01 pm
by worldsapart
well... I am not sure if this is rite.. but the manual says that the bus is locked only for the duration of the instruction accompanying the LOCK prefix... please correct me if, i'm wrong..
Re: AP startup issue
Posted: Sun Feb 15, 2009 4:27 pm
by Combuster
True, the lock prefix forces ordering and atomicity around one read-modify-write instruction. In between locked instructions, the processor can do other things (not doing so once resulted in Cyrix' infamous coma bug).
Re: AP startup issue
Posted: Sun Feb 15, 2009 6:08 pm
by worldsapart
Thanks combustor for clearing that up.. did u find anythin wrong wit my code? I really need some help... thanks.
Re: AP startup issue
Posted: Sun Feb 15, 2009 7:46 pm
by Brendan
Hi,
Code: Select all
bits 16 ; We are still in 16 bit Real Mode
global AP_startup
extern AP_main
extern AP_stage3
extern Stage3
;*************************************************;
; Bootloader Entry Point
;*************************************************;
AP_startup:
; Incrementing processor count - THIS WORKS FINE. SO APs ARE STARTING UP
mov bx, 500h
lock
inc byte [bx]
You can't assume DS is set to anything here, so "lock inc byte [bx]" might increment the byte at the address 0x0000:0x0500 or it might try to increment the byte at the address 0xFFFF:0x0500 (or any other segment). Also, it's probably better to write "lock inc byte [0x0500]" rather than spreading it over 3 separate lines. Incrementing a word or dword might be even better (e.g. not much point limiting it to 256 CPUs when computers that exceed this limit are on the way).
Note: because you only use DS a few times, maybe "lock inc byte [cs:0x0500]" would be better than setting up DS.
Code: Select all
mov ax, cs
mov ds, ax
;EFLAGS
push 2
popf
You can't assume SS or ESP is set to anything here either - the PUSH could trash your code or data, or the POP could get a value from ROM, etc.
Code: Select all
mov bx, 0x600
;Loading GDT and IDT
lgdt [bx]
Could maybe change this to "lgdt [cs:0x0600]" too (and get rid of the "mov ds,ax" above).
You should consider replacing hard-coded constants with defines (for example, use "%define AP_TEMP_GDT_ADDRESS 0x600" somewhere and then "lgdt [cs:AP_TEMP_GDT_ADDRESS]" here, and then do the same for the "lock inc [cs:0x500]").
Code: Select all
; Enabling Protected mode
mov eax, cr0
or al, 1
mov cr0, eax
; Far jump... THIS IS WAT IS SCREWED.. I GET A RELOCATION ERROR
jmp 0x8:Pmode
A relocation error sounds like a problem with the linking. I've got no idea how you're trying to link this, so I've got no idea what's going wrong.
Cheers,
Brendan
Re: AP startup issue
Posted: Mon Feb 16, 2009 12:39 pm
by worldsapart
[Removed post]
Issue explained below..
Re: AP startup issue
Posted: Mon Feb 16, 2009 8:06 pm
by worldsapart
k.. This is wat i have so far.. thanks for those tips Brendan.. I got rid of the triple faults n the relocation errors.. But it stil doesnt work...
Code: Select all
bits 16 ; We are still in 16 bit Real Mode
global AP_startup
extern AP_main
%define COUNTER 0x0500
%define GDTR 0x0600
%define CODE_DESC 0x08
;*************************************************;
; Bootloader Entry Point
;*************************************************;
AP_startup:
cli
; TRIED PUTTING THIS LINE AFTER SETTING UP THE SEGMENTS. BUT DIDNT WORK!!
; HERE IT WORKS JUST FINE! WHY IS THAT??
; Incrementing processor count
lock inc byte [COUNTER]
xor ax, ax
mov cs, ax
mov ds, ax
mov ss, ax
mov sp, 0x100
;SETTING UP THE STACK GOT RID OF THE TRIPLE FAULT THE PUSH OR POP WAS CREATING
;THANKS FOR THAT
;EFLAGS
push 2
popf
;Setting up GDT
lgdt [GDTR]
; IN THE KERNEL CODE RUNNING ON THE BSP I COPIED THE GDTR ENTRY AND THE GDTABLE TO LOW MEMORY
; "GDTR" POINTS TO THE GDTR ENTRY STRUCTURE.
; Enabling Protected mode
mov eax, cr0
or al, 1
mov cr0, eax
bits 32
; CODE_DESC - 0x8
jmp CODE_DESC:Pmode
Pmode:
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
mov esp, 0x1000
call AP_main ; C fUNCTION .... BUT NEVER GETS HERE!
hlt ; halt the system
I have added comments, in between so that you can understand wat's goin on.. here's some more info abt my implementation..
the COUNTER value is initialized in my C code as:
Code: Select all
unsigned char* counter = (unsigned char*)0x500;
The GDTR entry is initialized as follows:
Code: Select all
typedef struct DTR
{
unsigned short int limit;
unsigned short int alignment;
unsigned int offset;
}DTR_t;
DTR_t *GDT = (DTR_t*)0x600;
So that's where the 0x600 and 0x500 come from... Am sorry for the trouble guys.. help will be appreciated.. I know I'm doing something terribly wrong!...
Re: AP startup issue
Posted: Tue Feb 17, 2009 4:10 am
by djmauretto
Code: Select all
cli
; TRIED PUTTING THIS LINE AFTER SETTING UP THE SEGMENTS. BUT DIDNT WORK!!
; HERE IT WORKS JUST FINE! WHY IS THAT??
; Incrementing processor count
lock inc byte [COUNTER]
xor ax, ax
mov cs, ax ???????????
mov ds, ax
mov ss, ax
mov sp, 0x100
mov cs,ax
is invalid instruction
Re: AP startup issue
Posted: Tue Feb 17, 2009 7:39 am
by Combuster
CS affects execution - you can't set it without setting IP as well. In other words you are trying to do a jump without using a jump instruction.
Re: AP startup issue
Posted: Tue Feb 17, 2009 12:53 pm
by worldsapart
Damn.. that ws stupid! .. corrected that! ... but when i remove that it triple faults somewher.. isolating it now. thanks guys.