Page 1 of 1

Which registers to push on the stack during interrupt handli

Posted: Thu Aug 10, 2006 12:46 pm
by Candamir
In my ASM stub for handling interrupts, I currently use pusha to push all registers on the stack, but now I ask myself if I should also push the extended registers on the stack during these procedure with pushad... (I already tried it, inserting a pushad instruction after the pusha and a popad instruction before the popa instruction, and interrupts still worked, so what is the difference/which is better?)

BTW, if I'm using software-based task switching, I believe I still need a TSS in order to jump from Ring 3 to Ring 0. Is this right?

Candamir

Re:Which registers to push on the stack during interrupt han

Posted: Thu Aug 10, 2006 1:22 pm
by Assembler
You need to push general-purpose resgisters with pushad and flag register with pushf and push the segment registers DS,SS,ES,DS,GS,FS and stack pointer SP.
BTW, if I'm using software-based task switching, I believe I still need a TSS in order to jump from Ring 3 to Ring 0. Is this right?
Yes u'll need that too :D

Re:Which registers to push on the stack during interrupt han

Posted: Thu Aug 10, 2006 1:48 pm
by Candamir
And all these segment registers have to be set to 0x10?

Candamir

Edit: In my current ASM routine, I only deal with ds, es, fs and gs, but not with ss. Is there something wrong with it?

Re:Which registers to push on the stack during interrupt han

Posted: Thu Aug 10, 2006 1:51 pm
by bluecode
You don't need to push the flags explicitly, they are already pushed implicitly by the cpu, when an interrupt occured. If you're using a flat memory model, with only 2 global segment descriptors, then you also don't need to push the segment registers (cs & ss are nonetheless pushed implicitly by the cpu). You also don't need the user-level stackpointer, because this is also implicitly pushed by the cpu.

Re:Which registers to push on the stack during interrupt han

Posted: Thu Aug 10, 2006 2:56 pm
by Ryu
Candamir wrote:(I already tried it, inserting a pushad instruction after the pusha and a popad instruction before the popa instruction, and interrupts still worked, so what is the difference/which is better?)
Neither saves any segment registers (CS,DS,SS,ES,FS,GS). The pusha is just the 16bit version which pushes in the order of: AX, CX, DX, BX, SP (state before the pusha instruction), BP, SI, and DI. And so pushad is the equivalent but pushing EAX, ECX.. and so on. However, I'm afraid both mnemonics are same opcode (60h), which the assembler may treat pusha in 32bit mode as pushad or it might place an operand override size which correctly differs the two mnemonics. But ofcourse it depends on the assembler, and same goes with popa/popad.
Candamir wrote:Edit: In my current ASM routine, I only deal with ds, es, fs and gs, but not with ss. Is there something wrong with it?
I 'm thinking you thought the pusha instruction pushes segment registers which it doesn't. If that is the case, you need to manually save those segment registers, meaning push/pop them individually.

Re:Which registers to push on the stack during interrupt han

Posted: Thu Aug 10, 2006 3:32 pm
by Candamir
Ryu wrote: I 'm thinking you thought the pusha instruction pushes segment registers which it doesn't.
That's not the case. In order to avoid further misunderstandings, I'll put my code here:

Code: Select all

    pusha                   ; pushad instead? Or both? 
    push ds
    push es
    push fs
    push gs                 ; What about push ss?

    mov ax, 0x10
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov eax, esp

    push eax
    mov eax, c_routine
    call eax
    pop eax

    pop gs                    ; What about pop ss?
    pop fs
    pop es
    pop ds
    popa                     ; popad instead? Or both?
    add esp, 8
    iret
Thanks anyway for your help so far

Candamir

Edit: This code /does/ work for interrupts and everything, but I'm asking myself if it is already enough to support multitasking routines...

Re:Which registers to push on the stack during interrupt han

Posted: Thu Aug 10, 2006 4:04 pm
by Candamir
Let me see if I understood it: In order to set up multitasking, the interrupt handler (the timer) just has to save all values on the stack into the current process_info struct, ask the scheduler for the next process and push its values on the stack. Is this correct?

If I have paging enabled, during which step do I change cr3? After pushing the new process values on the stack?

Candamir

Re:Which registers to push on the stack during interrupt han

Posted: Fri Aug 11, 2006 3:10 pm
by DruG5t0r3
; What about pop ss?
combination of int/iret already saves the SS register (along with CS and EIP).

Re:Which registers to push on the stack during interrupt han

Posted: Fri Aug 11, 2006 3:11 pm
by DruG5t0r3
oh yeah...and eflags too ::)

Re:Which registers to push on the stack during interrupt han

Posted: Sat Aug 12, 2006 7:06 am
by Pype.Clicker
doing a "pop ss" would result in nothing good anyway ... you cannot safely switch to a new stack segment without switching to a new stack pointer atomically... and no, "popad" will not "restore" the pushed value of ESP...