OK - sorry
I have made the change to 4-byte align the stack as suggested. The switching code is (adapted) from a tutorial found on bonafide, I believe. 'Stacks' simply refers to a structure containing esp and ss for all the task stacks (just 2 records long at the moment - one for the kernel at index 0 and one for the task at index 1). Here's the switching code:
Code: Select all
void schedule()
{
//very simple no-priority round-robin 'scheduler'
next_task = current_task+1;
if(next_task>ltasks) next_task=0;
}
and the asm part (handles int 0x30):
Code: Select all
[extern _current_task]
[extern _next_task]
[extern _stacks]
[global _tswitch]
_tswitch: ;change system state (task switch) int 0x30 (48)
PUSH DS
PUSH ES
PUSH FS
PUSH GS
PUSH EAX
PUSH EBX
PUSH ECX
PUSH EDX
PUSH ESI
PUSH EDI
PUSH EBP ; SAVE TASK REGISTERS ON THE STACK
MOV EAX, 10h ; SYSTEM DATA SEGMENT SELECTOR
MOV DS, EAX
MOV ES, EAX
MOV EBX, [_current_task]
SHL EBX, 3 ;*8
LEA ESI, [_stacks]
MOV [DS:EBX+ESI], ESP
MOV [DS:EBX+ESI+4], SS
MOV EBX, [_next_task]
MOV [_current_task], EBX
SHL EBX, 3 ;*8
MOV ESP, [DS:EBX+ESI]
MOV SS, [DS:EBX+ESI+4]
POP EBP
POP EDI
POP ESI
POP EDX
POP ECX
POP EBX
POP EAX
POP GS
POP FS
POP ES
POP DS ; RESTORE TASK REGISTERS FROM THE STACK
iretd
So, the idea is that the ss and esp offset in stacks is calculated from _current_task and _next_task.
If I call int 0x30 prior to adding a task to the stack list, it's fine - stack[0] holds the correct values for the kernel.
If I attempt to call int 0x30 having added my own task to the stack list (via the previously posted code), I get a fault as CS, IP and all registers and selectors are set to zero - it's like I set up a blank stack for the new task.
I have checked in bochs, and the value of ESP for the new task is exactly what I would expect - the top of the memory allocated for the stack minus the stack size.
Sorry I didn't include all this detail in my first post - I obviously over-did the brevity! I was also quite embarrased about posting my code, as being a self-taught amateur no-one has ever critiqued it before!
Adam
[EDIT: Amended error in source code and removed db 66h's before pushing selectors. It was pointed out that all pushed selectors are 32 bit anyway in pmode...]