multi tasking 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
asdfgh
Member
Member
Posts: 42
Joined: Fri Apr 18, 2008 9:14 pm

multi tasking problem

Post 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
ChristianF
Member
Member
Posts: 79
Joined: Sun Jun 10, 2007 11:36 am

Re: multi tasking problem

Post 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
42
asdfgh
Member
Member
Posts: 42
Joined: Fri Apr 18, 2008 9:14 pm

Re: multi tasking problem

Post by asdfgh »

still hav the problem//
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: multi tasking problem

Post by Combuster »

Where did the EOI go?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
asdfgh
Member
Member
Posts: 42
Joined: Fri Apr 18, 2008 9:14 pm

Re: multi tasking problem

Post by asdfgh »

Eoi is in the function timer_irq
ChristianF
Member
Member
Posts: 79
Joined: Sun Jun 10, 2007 11:36 am

Re: multi tasking problem

Post 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
asdfgh
Member
Member
Posts: 42
Joined: Fri Apr 18, 2008 9:14 pm

Re: multi tasking problem

Post by asdfgh »

no still i have the same prob..
the irq is functioning correctly.
but i dont know wats the prob..
ChristianF
Member
Member
Posts: 79
Joined: Sun Jun 10, 2007 11:36 am

Re: multi tasking problem

Post 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
42
asdfgh
Member
Member
Posts: 42
Joined: Fri Apr 18, 2008 9:14 pm

Re: multi tasking problem

Post by asdfgh »

the problem was with my scheduling code...
now it is solved //
thankx OdinPG
Post Reply