Page 1 of 1

multitasking (asm)

Posted: Tue Jan 08, 2008 7:52 am
by Jef
Hi,

i am writing my own OS and i have a question about multitasking.

I made a simple routine that gives the execution to the same task, just to see if the mechanism is written correct. Unfortunately (at VMWare) a take a kernel stack fault.
I use the same TSS for all tasks, overwriting the address in GDT.

What flags i must put in the TSS selector (in GDT byte 6) ?
Same as code selector (9Ah) or 50h ?

gdt4 is the TSS_Selector (in GDT)

init:

Code: Select all

mov	eax, MAX_PROCESSES
	shl	eax, 8				;*256
	push	eax
	call	MemAlloc
	mov	dword [ProcessArray], eax
	mov	edi, eax
	
	mov	al, 0
	mov	[edi+tss.cr3], eax
	mov	[edi+tss.ss], ss
	mov 	dword [edi+tss.esp], esp
	mov	[edi+tss.ds], ds
	mov	[edi+tss.es], ds
	mov	[edi+tss.cs], cs
	
	mov 	dword [edi+tss.eip], OS_Main_Loop	    ;Code to execute
	mov  	byte [edi+tss.eflags+1], 2		    ;Enable its interrupts
	mov	ax, TSS_SEL
	ltr	ax
	mov	eax, edi
	shr	eax, 8			;Just the interesting bits
	mov	[gdt4+3], ax		;Change tss descriptor in gdt
	mov 	byte [gdt4+5], 8900h>>8  ;Pretend it isn't busy
at timer interrupt

Code: Select all

mov	eax, dword [ProcessArray]
	shr	eax, 8			;Just the interesting bits
	mov	[gdt4+3], ax		;Change tss descriptor in gdt
	mov 	byte [gdt4+5], 8900h>>8  ;Pretend it isn't busy
	jmp	TSS_SEL:0		;Switch tasks
is it something obvious wrong ?
Any help acceptable :)

Posted: Tue Jan 08, 2008 8:55 am
by ucosty
Whats the exact error that bochs gives?

Posted: Tue Jan 08, 2008 9:42 am
by Craze Frog
He doesn't use bochs.

Posted: Tue Jan 08, 2008 1:22 pm
by ucosty
Something of an automatic assumption. Usually works, too.

edit: and as it turns out I didn't pay attention to where he said vmware. :oops:

That being said VMWare won't tell you anything useful.

Run it in bochs and then tell us what error it spits out.

Posted: Tue Jan 08, 2008 8:32 pm
by Jef
another strange thing (LOL) is that my OS cannot run in Bochs or VirtualPC.
Its runs ok in VMWare and at real pc.
In Bochs and VirtualPC (probable) something goes wrong with GUI and shows me some green vertical lines (at half screen).
I am using 800x600x16 (if matters).

anyway, i change the code,
i fix the bug with the stack (mov dword [edi+tss.esp], esp )
i have now two processes, with there own stack
but again i take a "reboot". probable triple-fault.

bochs log says:
00005826966p[CPU0 ] >>PANIC<< get_SS_ESP_from_TSS: TR.cache invalid

maybe its better to make a different TSS selector for every process.
Note: I am not using memory paging
If there is a sample code for multitasking, is welcome :)

one more question,
when you have dual/quad core cpu (or two cpus) how you select the cpu to load a process ?

Posted: Tue Jan 08, 2008 10:15 pm
by Dex
Try these:

Posted: Tue Jan 08, 2008 10:17 pm
by Jef
i forgot to post new code
here we are....

init

Code: Select all

mov	eax, MAX_PROCESSES
	shl	eax, 8				;*256
	push	eax
	call	MemAlloc
	mov	dword [ProcessArray], eax
	mov	edi, eax	
	
	mov	eax, 4*1024
	push	eax
	call	MemAlloc
	add	eax, (4*1024)-4
	mov 	dword [edi+tss.esp], eax	;stack
	mov	byte [edi+tss.cr3], 0
	mov 	dword [edi+tss.eip], OS_Main_Loop	    ;Code to execute
	mov  	byte [edi+tss.eflags+1], 2		    ;Enable its interrupts
	mov	[edi+tss.ss], ss
	mov	[edi+tss.fs], fs	
	mov	[edi+tss.ds], ds
	mov	[edi+tss.es], es
	mov	[edi+tss.cs], cs
	
	mov	edi, dword [ProcessArray]
	add	edi, 256			;second process
	
	mov	eax, 4*1024
	push	eax
	call	MemAlloc
	add	eax, (4*1024)-4
	mov 	dword [edi+tss.esp], eax	;stack
	mov	byte [edi+tss.cr3], 0
	mov 	dword [edi+tss.eip], ClockLoop		    ;Code to execute
	mov  	byte [edi+tss.eflags+1], 2		    ;Enable its interrupts
	mov	[edi+tss.ss], ss
	mov	[edi+tss.fs], fs	
	mov	[edi+tss.ds], ds
	mov	[edi+tss.es], es
	mov	[edi+tss.cs], cs
	
	mov	ax, TSS_SEL
	ltr	ax
	mov	eax, dword [ProcessArray]	;point to first process
	shr	eax, 8			;Just the interesting bits
	mov	[gdt4+3], ax		;Change tss descriptor in gdt
	mov 	byte [gdt4+5], 8900h>>8  ;Pretend it isn't busy
	
.wait1:
	hlt
	jmp	.wait1
and irq 0

Code: Select all

IRQ_Timer:
	movzx	eax, byte [CurrentProcess]
	shl	eax, 8	;*256
	add	eax, dword [ProcessArray]
	
	
	pop	dword [eax+tss.eip]
	pop	dword [eax+tss.cs]
	pop	dword [eax+tss.eflags]	
	mov	[eax+tss.esp], esp
	mov	[eax+tss.esi], esi
	mov	[eax+tss.edi], edi
	mov	[eax+tss.ebp], ebp
	mov	[eax+tss.ebx], ebx
	mov	[eax+tss.ecx], ecx
	mov	[eax+tss.edx], edx	
	
	mov	al,20h
	out	20h,al
	
	movzx	eax, byte [CurrentProcess]
	
	inc	eax
	cmp	eax, 2			;max processes
	jne	.ok
	
	mov	eax, 0
.ok
	mov	byte [CurrentProcess], al	
	
	shl	eax, 8	;*256
	add	eax, dword [ProcessArray]
	
	mov	esi, [eax+tss.esi]
	mov	edi, [eax+tss.edi]
	mov	ebp, [eax+tss.ebp]
	mov	ebx, [eax+tss.ebx]
	mov	ecx, [eax+tss.ecx]
	mov	edx, [eax+tss.edx]
	mov	esp, [eax+tss.esp]
	push	dword [eax+tss.eflags]
	push	dword [eax+tss.cs]
	push	dword [eax+tss.eip]
	
	;i try with,or with out the 3 lines that follows
	shr	eax, 8			;Just the interesting bits
	mov	[gdt4+3], ax		;Change tss descriptor in gdt
	mov 	byte [gdt4+5], 8900h>>8  ;Pretend it isn't busy	
	

	iretd
any idea ?

Posted: Wed Jan 09, 2008 10:02 pm
by Jef
Dex wrote:Try these:
PM10.asm helps me a lot.
now multitasking works (with little problems).

THANX