Multitasking - again
When it does this, what are the values of the selectors loaded into your segment registers? I don't know whether bochs would load the selectors before or after testing them, so what are the top few words of your stack?cg123 wrote:..well, I've got another problem.
Now it triple faults on the iret with this message:
fetch_raw_descriptor: LDT: index (105f)20b > limit (0)
...but I don't have an LDT. Is that in itself the problem, or...
Yes, this is what I want to see too .I don't know whether bochs would load the selectors before or after testing them, so what are the top few words of your stack?
As I understand cg123 doesn't use hardware multi tasking, so it is not a problem of data segment's selectors. But if there is privilege level switching during 'iret', the problem may be in TSS, SS:ESP[X] fields.
Well, I've finally fixed it. For anyone interested, here's my final code:
Code: Select all
_irq0:
cli
jmp _swap
_tnum:
dd 0
_swap:
;push
pushad
push ds
push es
push fs
push gs
mov eax,[_tnum]
inc eax
mov ebx,eax
mov ecx,8
mul ecx
add eax,_procdata
mov esp,[eax]
cmp esp,0
jne .go2
; go to proc 1
mov [_tnum],DWORD 0
jmp _swap
.go2:
mov [_tnum],ebx
sti
mov al,0x20
out 0x20,al
;pop
pop gs
pop fs
pop es
pop ds
popad
iret
section .bss
global _procdata
_procdata:
%rep 256
resd 2 ; esp, sleep
%endrep
Code: Select all
typedef struct {
unsigned int esp;
unsigned int sleep;
} proc_t;
extern proc_t procdata[256];
char kstacks[256][1024];
unsigned int c_stack(unsigned int num,unsigned eip)
{
unsigned int* ptr = (unsigned int*)&kstacks[num][1024];
ptr--;
*ptr--=0x202;
*ptr--=0x08;
*ptr--=eip;
*ptr--=0;
*ptr--=0;
*ptr--=0;
*ptr--=0;
*ptr--=0;
*ptr--=0;
*ptr--=0;
*ptr--=0;
*ptr--=0x10;
*ptr--=0x10;
*ptr--=0x10;
*ptr=0x10;
proc_t p;
p.esp = (unsigned int)ptr;
p.sleep = 0;
procdata[num] = p;
return (unsigned int)ptr;
}
based on the bochs output you don't have the ldt selected in your segment selectors which problably point to a wrong stack when you do the iret. start checking there 32-bit has a different stack behaviour for exceptions and privelage changes, the reason why i went to long mode, so you'll have to look into that i guess.
<guess to late ...>
<guess to late ...>
Author of COBOS
Well, I guess I spoke a few minutes too soon. When I do any sort of stack manipulation in the functions, it eventually triple faults on the 'iret' in _switch. How would I go about fixing this?
Edit:
..well, I feel rather stupid now. I forgot to save the value of esp. ACTUAL final _swap:
Edit:
..well, I feel rather stupid now. I forgot to save the value of esp. ACTUAL final _swap:
Code: Select all
_swap:
;push
pushad
push ds
push es
push fs
push gs
; save esp
mov eax,[_tnum]
mov ecx,8
mul ecx
add eax,_procdata
mov [eax],esp
; increment task
mov eax,[_tnum]
inc eax
mov ebx,eax
; load esp
mov ecx,8
mul ecx
add eax,_procdata
mov esp,[eax]
cmp esp,0
jne .go2
; go to proc 1
mov [_tnum],DWORD 0
jmp _swap
.go2:
add eax,4
cmp DWORD [eax],0
je .go
inc ebx
mov [_tnum],ebx
mov ebx,[eax]
dec ebx
mov [eax],ebx
jmp _swap
.go:
mov [_tnum],ebx
sti
mov al,0x20
out 0x20,al
;pop
pop gs
pop fs
pop es
pop ds
popad
iret