Task switching - Interruption 1Ch

Programming, for all ages and all languages.
Post Reply
User avatar
Cpcdos
Member
Member
Posts: 35
Joined: Sat Feb 02, 2013 4:16 pm
Location: France [Lyon]

Task switching - Interruption 1Ch

Post 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,
FAVIER Sébastien
Sorry for my bad bad English level, I'm young French studient .. :)
simeonz
Member
Member
Posts: 360
Joined: Fri Aug 19, 2016 10:28 pm

Re: Task switching - Interruption 1Ch

Post 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
Post Reply