Multitasking - again

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Mikae
Member
Member
Posts: 94
Joined: Sun Jul 30, 2006 1:08 pm

Post by Mikae »

Check if bit TI of any selector, used in switching, is set to '1'.
cg123
Member
Member
Posts: 41
Joined: Wed Sep 27, 2006 2:34 pm

Post by cg123 »

No, none of them have it set.
Jules
Member
Member
Posts: 30
Joined: Mon Jan 08, 2007 3:19 am
Location: UK

Post by Jules »

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... :?
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?
Mikae
Member
Member
Posts: 94
Joined: Sun Jul 30, 2006 1:08 pm

Post by Mikae »

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?
Yes, this is what I want to see too :).

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.
cg123
Member
Member
Posts: 41
Joined: Wed Sep 27, 2006 2:34 pm

Post by cg123 »

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;
}
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

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 ...>
Author of COBOS
cg123
Member
Member
Posts: 41
Joined: Wed Sep 27, 2006 2:34 pm

Post by cg123 »

Well, I guess I spoke a few minutes too soon. :lol: 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:

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