Multitasking problem

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
FlashBurn

Multitasking problem

Post by FlashBurn »

I?ve written a scheduler with stack based switching. But there is a TSS for switching between the rings. But at the moment I only use 1 task (kernel). My problem now is that it seems to work if I go in an endless loop (NASM : "jmp $"), but if I go in an endless loop where I print out a character, my PC will reboot when the end of a line is reached! In Bochs everthing is working.

This is my scheduler:

Code: Select all

;----------------------------
pit_hndl:
???pushad
???push gs
???push fs
???push es
???push ds

???mov eax,1000h
???mov cr3,eax

;----------------------------
???mov eax,esp
???mov edi,[act_task]
???mov esi,edi
???add edi,12
???stosd

???lodsd
???mov [act_task],eax
???mov esi,eax
???add esi,4
???lodsd
???mov cr3,eax

;???lodsd
;???mov ss,eax
???add esi,4?????????;because no ring3 task
???lodsd
???mov esp,eax
;----------------------------

.end
???mov al,20h
???out 20h,al

???pop ds
???pop es
???pop fs
???pop gs
???popad
???iret
;----------------------------
;???vars
act_task???dd 0
;----------------------------
Maybe you know what the problem is. If you need further details or code, you only have to say it!
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Multitasking problem

Post by Pype.Clicker »

i think you should rewrite your code without all those stosd/lodsd... it is very obscure this way ...
FlashBurn

Re:Multitasking problem

Post by FlashBurn »

Maybe my task desc will help you. What is faster the lods/stos or a mix of [esi]/[edi] and inc esi/edi?

Code: Select all

;----------------------------
;   task descriptor:
;
;   dd pointer to next task descriptor
;   dd page directory
;   dd ss
;   dd esp
;   dd pointer to message structur
;   dd PID / task ID
;----------------------------
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Multitasking problem

Post by Pype.Clicker »

1. why do you reset CR3 to 0x1000 ? is this your kernel space ?

Code: Select all

   mov eax,esp
   mov edi,[act_task]
   mov esi,edi
   add edi,12
   stosd
   lodsd
   mov [act_task],eax
A very unclear code, imho.

Code: Select all

   mov esi,[act_task]
   mov edi,[esi+task.next]
   mov [esi+task._esp],esp
   mov [act_task],edi
seems both clearer and faster (stosd/lodsd are complex instructions which usually don't pair, etc. etc.)

i could try to go on and rewrite your code with this kind of "moves", but i fear i would make it even buggy as i don't know what operations you planned to do. So i'll have to leave it for you ...


mov esi,eax
add esi,4
lodsd
mov cr3,eax

; lodsd
; mov ss,eax
add esi,4 ;because no ring3 task
lodsd
mov esp,eax
FlashBurn

Re:Multitasking problem

Post by FlashBurn »

Yes at 0x1000 is the page dir for the kernel. This is my new rewritten code.

Code: Select all

;----------------------------
???mov edi,[act_task]
???mov [edi + 12],esp
???mov esi,[edi]
???
???mov [act_task],esi
???mov esp,[esi + 12]
   mov eax,[esi + 4]
   mov cr3,eax
;----------------------------
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Multitasking problem

Post by Pype.Clicker »

Are your task structures visible from any address space ? if yes, why bothering with mov CR3,0x1000 at start ? if not, you should not do

Code: Select all

  mov cr3,eax
  mov esp,[esi + 12]   
but rather

Code: Select all

  mov esp,[esi + 12]   
  mov cr3,eax
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Multitasking problem

Post by Pype.Clicker »

your problem is most likely due either to a bad segment register when the context is restored (in this case, having mov [0xb8000],'A' might lead to a GPF), or to a bad stack pointer restored (in which case, the calls/ret in a printf function might produce disgraceful results :-/ )

I think i could solve most of my assumptions if you provided the code for task structure initialization ...
FlashBurn

Re:Multitasking problem

Post by FlashBurn »

Ok, here it is.

Code: Select all

;create dummy task desc
   mov eax,task_dummy
   mov [task_dummy],eax
   mov [act_task],eax
   mov [start_pid],eax
   mov dword[task_dummy + 4],1000h
   mov dword[task_dummy + 8],10h
   mov eax,esp
   sub eax,8
   mov [task_dummy + 12],eax
FlashBurn

Re:Multitasking problem

Post by FlashBurn »

OK, I know now that my task switching code isn?t the problem. Because if I write a loop which prints 10 chars and then makes a newline and then writes 10 chars and so. My pc wont reboot ??? What could it be that my pc reboots when I write chars in an endless loop? Maybe I should start a new thread with my print function?!
Post Reply