Page 1 of 1

Task switching - Interruption 1Ch

Posted: Sat Apr 28, 2018 11:49 am
by Cpcdos
My work is to switch stack between 2 or more functions (Preemptible multi-task) between main, _mytask1 and _mytask_2 functions

For begin, this is my code FreeBasic/GAS (AT&T)

INIT_INTERRUPT()

Code: Select all

jmp saut_1%f
DS_SEL: .short 0
INT1C_OFF: .int 0
INT1C_SEL: .short 0

saut_1:
push ds
pop DS_SEL
push cs
pop WORD PTR [cs_]
push ds
pop WORD PTR [ds_]
push es
pop WORD PTR [es_]
push ss
pop WORD PTR [ss_]

mov ax, 0x204
mov bl, 0x1c
int 0x31
jc saut_2%f
mov INT1C_SEL, cx
mov INT1C_OFF, edx
saut_2:
START_INTERRUPT()

Code: Select all

mov ax, 0x205
mov bl, 0x1c
push cs
pop cx
mov edx, OFFSET _MY_INTERRUPT_FUNCTION
int 0x31
jc saut_3%f
saut_3:
This code work perfectly, MY_INTERRUPT_FUNCTION() is executed during "1Ch tic"
And this is my problem :
MY_INTERRUPT_FUNCTION()

Code: Select all

sti
' Save registers
push ds
push es
push fs
push gs

' Entry point of DS
mov dx, ds

' Load data section in DS
mov ax, cs:DS_SEL
mov ds, ax

' Restaure seg values
mov ax, cs
mov cs_, ax
mov ds_, dx
mov ax, es
mov es_, ax
mov ax, ss
mov ss_, ax

mov fs_, fs
mov gs_, gs

' Push all registers on stack
pushad

' ===== SWITCH STACK =====
push esp
CALL SWITCH_TASK
mov esp, eax
pop esp
' ===== SWITCH STACK =====

' Restaure registers
popad

pop gs
pop fs
pop es
pop ds

sti
iret
This not work, iret return to the original EIP position, so i search how interrupt stack work, according this stack representation (Without error code) :
Image
About iret instruction, if i want modify the "Return EIP register" on my current interrupt i must write my new EIP on it ? Yes ? So after my CALL, i push like this _mytask1 for test :

Code: Select all

push eax
mov eax, _mytask1
mov ss:[esp]+0, eax
pop eax
But this not work..

I've seeing the content of "ss:[ESP]+0", the famous "Return EIP" before modifications, and i've every "0x16F".. what?? :-|

I've already executed on ring0, with cwsdpr0.exe on FreeDOS..
I'm lost, if someone can help me?

Thank you a lot,

Re: Task switching - Interruption 1Ch

Posted: Sat Apr 28, 2018 1:34 pm
by simeonz
I haven't looked through the rest, but this seems unintentional:

Code: Select all

push eax  ;Old ss:[esp] is now ss:[esp+4], eax is stored in ss:[esp]
mov eax, _mytask1
mov ss:[esp], eax ; Changes stored eax on stack to _mytask1
pop eax
It should be:

Code: Select all

push eax
mov eax, _mytask1
mov ss:[esp+4], eax
pop eax
Edit: some assembly syntax