Page 1 of 1

multi tasking problem

Posted: Fri Jun 27, 2008 4:36 am
by asdfgh
this is te code for multitasking.. i get no error but the task is not executing

Code: Select all

typedef struct task
{
        u32int pid;       // Process ID
        u32int stack;     // Task's stack
        u32int state : 1; // Is task alive?
        u8int name[32];   // Task's name

        void (*thread)(); // Actual thread entry
}task_t;

task_t tasks[2];          // Our tasklist
int pid = 0;             // Current Process ID
int t=-1;


void CreateTask(int id, unsigned char name[32], void (*thread)())
{
        asm volatile("cli");
        unsigned int *stack;
        task_t *task = (task_t*) kmalloc(sizeof(task_t));
        task->stack = kmalloc_a(0x4000) + 0x4000;       // Allocate 4 kilobytes of space

        stack = (unsigned int*)task->stack;

        // Expand down stack
        // processor data
        *--stack = 0x202;       // EFLAGS
        *--stack = 0x08;        // CS
        *--stack = (unsigned int) thread;       // EIP

        // pusha
        *--stack = 0;           // EDI
        *--stack = 0;           // ESI
        *--stack = 0;           // EBP
        *--stack = 0;           // NULL
        *--stack = 0;           // EBX
        *--stack = 0;           // EDX
        *--stack = 0;           // ECX
        *--stack = 0;           // EAX

        // data segments
        *--stack = 0x10;        // DS
        *--stack = 0x10;        // ES
        *--stack = 0x10;        // FS
        *--stack = 0x10;        // GS

        task->state = 1;
        task->stack = (unsigned int) stack;
        task->thread = thread;
        strncpy(task->name, name, strlen(name));

        tasks[id] = *task;
        t++;
        asm volatile("sti");
        
}


unsigned int sched(unsigned int context)  // Our assembly code provides us with the context
{
     
		tasks[pid].stack = context;      // save the old context into current task
         switch(pid)
         {
         case 0:
            if(t>0)             
			 pid = 1;
              break;
         case 1:
              pid = 0;
              break;
         }
         return tasks[pid].stack;         // Return new task's context.
}


this is the assmbly file..

Code: Select all

.global _sched     
.global _irq0 
_irq0: #timer irq
 pusha
 push %ds         # push ds onto stack
 push %es         # push es
 push %fs         # push fs
 push %gs         # push gs
               
 mov  $0x10,%eax   # save selector
 mov  %eax,%ds             
 mov  %eax,%es 
 mov  %eax,%fs
 mov  %eax,%gs             
                
 mov  %esp,%eax    # stack => eax
 push %eax         # push eax (esp)
 call _sched      # sched(eax) returns stack of next task
  mov %eax,%esp     # return value => stack              
 call _timer_irq                

 pop %gs          # pop gs off task stack
 pop %fs          # pop fs
 pop %es          # pop es
 pop %ds          # pop ds
 popa            # pop general registers
 iret

what is the problem

Re: multi tasking problem

Posted: Fri Jun 27, 2008 4:52 am
by ChristianF
hmmmmmmmmm....

I think the Problem is in thar line:

Code: Select all

task->stack = kmalloc_a(0x4000) + 0x4000;       // Allocate 4 kilobytes of space
in this line you allocate 16KB of Memory. You should use 0x1000 (4 KB) instead of 0x4000 (16KB).
If you don't know, which Hex-Value is what, you could also write 4096.
for example:

Code: Select all

task->stack = kmalloc_a(4096) + 4096;       // Allocate 4 kilobytes of space
But that is up to you.

Cheers Christian

*EDIT*
0x1000 = 4096 Bytes = 4 KB
:D

Re: multi tasking problem

Posted: Fri Jun 27, 2008 5:15 am
by asdfgh
still hav the problem//

Re: multi tasking problem

Posted: Fri Jun 27, 2008 5:48 am
by Combuster
Where did the EOI go?

Re: multi tasking problem

Posted: Fri Jun 27, 2008 6:06 am
by asdfgh
Eoi is in the function timer_irq

Re: multi tasking problem

Posted: Fri Jun 27, 2008 6:24 am
by ChristianF
May be that in this line something is wrong:

Code: Select all

tasks[id] = *task;
I think that you have to use memcpy for copying the allocated space to task[id], so i have done this.
For Example, something like this:

Code: Select all

memcpy(&tasks[id], task, sizeof(task_t));

Hope it helps!
Cheers Christian

Re: multi tasking problem

Posted: Fri Jun 27, 2008 6:37 am
by asdfgh
no still i have the same prob..
the irq is functioning correctly.
but i dont know wats the prob..

Re: multi tasking problem

Posted: Fri Jun 27, 2008 6:45 am
by ChristianF
hummmm....
I can show you my old Function ffrom my Multitasking :S
It worked, but at the moment i am rewriting my Kernel, so I can not test it.

Here it is:

Code: Select all

extern schedule
_sched:
	pusha
	push ds
	push es
	push fs
	push gs

	mov eax, 0x10
	mov ds, eax
	mov es, eax
	mov fs, eax
	mov gs, eax

	push esp
	call schedule

	mov esp, eax

	mov al, 0x20
	out 0x20, al

	pop gs
	pop fs
	pop es
	pop ds
	popa
	iret

Cheers Christian

Re: multi tasking problem

Posted: Fri Jun 27, 2008 7:11 am
by asdfgh
the problem was with my scheduling code...
now it is solved //
thankx OdinPG