multitasking (asm)

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.
Post Reply
User avatar
Jef
Member
Member
Posts: 112
Joined: Tue Jan 08, 2008 7:25 am
Location: Greece
Contact:

multitasking (asm)

Post 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 :)
Keep coding...
...the sky is the limit

AsteriOS project: http://www.mindfields.gr/main/index.php ... &Itemid=27
User avatar
ucosty
Member
Member
Posts: 271
Joined: Tue Aug 08, 2006 7:43 am
Location: Sydney, Australia

Post by ucosty »

Whats the exact error that bochs gives?
The cake is a lie | rackbits.com
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Post by Craze Frog »

He doesn't use bochs.
User avatar
ucosty
Member
Member
Posts: 271
Joined: Tue Aug 08, 2006 7:43 am
Location: Sydney, Australia

Post 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.
The cake is a lie | rackbits.com
User avatar
Jef
Member
Member
Posts: 112
Joined: Tue Jan 08, 2008 7:25 am
Location: Greece
Contact:

Post 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 ?
Keep coding...
...the sky is the limit

AsteriOS project: http://www.mindfields.gr/main/index.php ... &Itemid=27
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

Try these:
Attachments
PM7.ASM
(11.62 KiB) Downloaded 28 times
PM10.ASM
(14.63 KiB) Downloaded 29 times
User avatar
Jef
Member
Member
Posts: 112
Joined: Tue Jan 08, 2008 7:25 am
Location: Greece
Contact:

Post 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 ?
Keep coding...
...the sky is the limit

AsteriOS project: http://www.mindfields.gr/main/index.php ... &Itemid=27
User avatar
Jef
Member
Member
Posts: 112
Joined: Tue Jan 08, 2008 7:25 am
Location: Greece
Contact:

Post by Jef »

Dex wrote:Try these:
PM10.asm helps me a lot.
now multitasking works (with little problems).

THANX
Keep coding...
...the sky is the limit

AsteriOS project: http://www.mindfields.gr/main/index.php ... &Itemid=27
Post Reply