Page 1 of 1

SMP, trampoline.asm freezing on enable pmode. [Solved]

Posted: Sat Dec 12, 2015 5:00 pm
by digo_rp
Hi all,

could anyone help me with my trampoline.asm, please...

when I do

mov eax, cr0
or eax, 1
mov cr0, eax

all my pc freeze.!

my trampoline code is at http://pastebin.com/xFYK0A8c

thanks so much...

Re: SMP, trampoline.asm freezing on enable pmode.

Posted: Sat Dec 12, 2015 5:32 pm
by TightCoderEx
There is no actual helping with the code as it doesn't make any sense in any context, so the only thing that could be done is doing it for you. What you need to do is read this so you have a clear understanding of what the descriptor tables is all about. Here is an example of a flat memory model mapping 4 gigabytes.

Code: Select all

  GDT
      dq      0  
      dw     -1, 0, 0x9A00, 0xcf
      dw     -1, 0, 0x9200, 0xcf
This is how it appears in memory;
FF FF 00 00 00 9A CF 00 <-> FF FF 00 00 00 92 CF 00

Remember, what you are looking at here are little endian values.

Re: SMP, trampoline.asm freezing on enable pmode.

Posted: Sat Dec 12, 2015 6:49 pm
by Brendan
Hi,
digo_rp wrote:could anyone help me with my trampoline.asm, please...
I think you want something more like this:

Code: Select all

        mov     EAX, [GDTPointer] ; Loaded from smp.c
        lgdt    [EAX]             ; load GDT into GDTR
       
        mov     EAX, [IDTPointer] ; Loaded from smp.c
        lidt    [EAX]             ; load IDT into IDTR
Of course you'd have to make sure that the pointers are 16-bit offsets from "CS/DS base" (and not normal 32-bit pointers); and because these things are small it's probably easier/faster to have the actual data instead of a pointer to the actual data.

Also, because you aren't using the stack or most segment registers in real mode, it's probably easier to not bother loading segment registers in real mode at all. For example, maybe more like this:

Code: Select all

               align 4

               dw 0               ;Padding for alignment
GDTR:          dw 0x1234          ;GDT limit
               dd 0xA1A2A3A4      ;GDT base
IDTR:          dw 0x1234          ;IDT limit
               dd 0xA1A2A3A4      ;IDT base
               dw 0               ;Padding for alignment

StackPointer:  dd 0x03030303
APCPUBooted:   dd 0xBABBBCBD

ap_boot:
        cli
        mov esp,[cs:StackPointer]
        mov dword [cs:APCPUBooted], 0xA1B2C3D4
        lgdt [cs:GDTPointer]
        lidt [cs:IDTPointer]

        mov eax, cr0 
        or eax, 1
        mov cr0, eax

        jmp 0x08:.Stage3

Cheers,

Brendan

Re: SMP, trampoline.asm freezing on enable pmode.

Posted: Sun Dec 13, 2015 3:53 pm
by digo_rp
Hi Brendan, thanks for you reply, but, the weirdest is that when I enable pmode, all my O.S. freeze!, I have been doing a lot of tests, the cpu is at real mode,
the segment registers are ok with segment 0x7000, in real mode the AP cpu Works fine, but when I change to pmode... freeze...

the gdt is ok, I try to put at the trampoline.asm... the gdt in the same code segment, .BSS too, .data too, with 0x00 null, 0x08 descriptor code, 0x10 data, as all pmode kernel...

but I don´t really know what is happening... I ´m using Intel smp_int.c example..., do I have to do some bus locking before send a SIPI to AP CPU?

I don´t really know...

thanks so much....

Re: SMP, trampoline.asm freezing on enable pmode.

Posted: Sun Dec 13, 2015 3:55 pm
by digo_rp
TightCoderEx, forgive me, I forgot to say thanks to you too...

thanks so much for you reply too..

Re: SMP, trampoline.asm freezing on enable pmode.

Posted: Sun Dec 13, 2015 5:09 pm
by Brendan
Hi,
digo_rp wrote:but I don´t really know what is happening... I ´m using Intel smp_int.c example..., do I have to do some bus locking before send a SIPI to AP CPU?
Normally you send the "INIT, SIPI, SIPI" sequence and don't have to do anything else (and even the second SIPI is often unnecessary too). There's no bus locking or anything needed. Note that I'm not too sure what Intel's "smp_int.c" is - it's possible that it's intended for firmware (and does things that are unnecessary or inappropriate for an OS).

My suggestion would be to single-step the AP CPU's startup code (and examine things it loads from memory, etc) using (e.g) Bochs. It's possible there's something going wrong (e.g. "lgdt" using a 16-bit address size and expecting a 24-bit "GDT base address" when your GDT needs a 32-bit base address).


Cheers,

Brendan

Re: SMP, trampoline.asm freezing on enable pmode.

Posted: Mon Dec 14, 2015 6:05 am
by digo_rp
Nice, i will check that out...
thanks so much

Re: SMP, trampoline.asm freezing on enable pmode.

Posted: Mon Dec 14, 2015 9:16 am
by digo_rp
Brendan, I found when the triple fault hapens..

just after set cr0.pe to 1
I have to do a jmp far...

is after that jmp when I got a triple fault

this is the contents of vbox.log

00:00:12.280059 cs={0008 base=0000000000000000 limit=ffffffff flags=0000c09b} dr0=00000000 dr1=00000000
00:00:12.280064 ds={0010 base=0000000000000000 limit=ffffffff flags=0000c093} dr2=00000000 dr3=00000000
00:00:12.280067 es={0010 base=0000000000000000 limit=ffffffff flags=0000c093} dr4=00000000 dr5=00000000
00:00:12.280070 fs={0010 base=0000000000000000 limit=ffffffff flags=0000c093} dr6=ffff0ff0 dr7=00000400
00:00:12.280073 gs={0010 base=0000000000000000 limit=ffffffff flags=0000c093} cr0=00000013 cr2=00000000
00:00:12.280076 ss={0010 base=0000000000000000 limit=ffffffff flags=0000c093} cr3=00000000 cr4=00000600

do you know how can I solve that problem?

thanks so much

Re: SMP, trampoline.asm freezing on enable pmode.

Posted: Mon Dec 14, 2015 12:12 pm
by Brendan
Hi,
digo_rp wrote:00:00:12.280059 cs={0008 base=0000000000000000 limit=ffffffff flags=0000c09b} dr0=00000000 dr1=00000000
00:00:12.280064 ds={0010 base=0000000000000000 limit=ffffffff flags=0000c093} dr2=00000000 dr3=00000000
00:00:12.280067 es={0010 base=0000000000000000 limit=ffffffff flags=0000c093} dr4=00000000 dr5=00000000
00:00:12.280070 fs={0010 base=0000000000000000 limit=ffffffff flags=0000c093} dr6=ffff0ff0 dr7=00000400
00:00:12.280073 gs={0010 base=0000000000000000 limit=ffffffff flags=0000c093} cr0=00000013 cr2=00000000
00:00:12.280076 ss={0010 base=0000000000000000 limit=ffffffff flags=0000c093} cr3=00000000 cr4=00000600
Everything in that looks perfectly correct. Either it's from the wrong CPU (e.g. from the BSP), or the AP successfully switched to protected mode and successfully loaded all segment registers with correct descriptors.
digo_rp wrote:do you know how can I solve that problem?
I think you need more information about the symptoms before you can even begin to solve the problem. For example, what was CS:EIP when the triple fault happened and which instruction that's supposed to be at that address? The more you know about the symptoms the easier it is to find the cause.


Cheers,

Brendan

Re: SMP, trampoline.asm freezing on enable pmode.

Posted: Mon Dec 14, 2015 6:41 pm
by digo_rp
Hi, I copy IDT from main kernel to address below 1MB and load it!
now I got invalid opcode!

and with all my segment registers I got a real mode segment, I´m using nasm to create my trampoline..asm

jmp 0x08:Stage3 <- the problem should be that line, right?

[BITS 32]
Stage3:
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
mov esp, [cs:StackPointer]

thanks so much :D

Re: SMP, trampoline.asm freezing on enable pmode.

Posted: Tue Dec 15, 2015 8:18 am
by digo_rp
Brendan, I got news for you, thanks so much for you help...

now I got my trampoline working...

and I´ll go to develop mutex and spinlock

thanks so much...

thanks to everyone here ..

:-D

Re: SMP, trampoline.asm freezing on enable pmode.

Posted: Mon Dec 28, 2015 7:42 am
by digo_rp
Brendan, now I´m having a problem...

my OS is freezing when you start it and use it for a couple of seconds...

I´m using spinlock at timer on each cpu... and I´m using a phisical way of ioapic... all hardware irqs are just sent to BSP...

do you know what can I do?

thanks so much

Re: SMP, trampoline.asm freezing on enable pmode.

Posted: Mon Dec 28, 2015 4:46 pm
by digo_rp
I found the problem, thanks so much all