Software task switching . How?

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.
OSpot

Software task switching . How?

Post by OSpot »

I want to know how to switch task not using far jump or far call to TSS.(not Intel way)

I heard that OS needs only one TSS.

So will you show me the whole code of software task switching.

ex)
void timer_handler() {

.
.
asm volatile("iret");
}

Sorry for my poor English!!
hanoi

Re:Software task switching . How?

Post by hanoi »

This is how i would do it:
#define MAX_TASKS 256

unsigned int Task=0;
long long SysCounter=0;

struct Task{
char *pagedir;
char *stack;
unsigned int pid;
} TaskList[MAX_TASKS];

asm(
".globl _timerISR \n"
"_timerISR: \n"
"pushad \n"
"pushfd \n"
"call _timer \n"
"popfd \n"
"popad \n"
"iret \n"
);

void timer()
{
SysCounter++;
Task++;
if(Task == MAX_TASKS-1) Task = 0;
stack = TaskList[Task].stack;
SetStack(TaskList[Task].stack);
setCR3(TaskList[Task].PageDir);
return;
}
Be warned, i haven't tested this or something...
and also it wouldn't be a good i dea to copy & paste that...
greetings, hanoi
eL JeDi

Re:Software task switching . How?

Post by eL JeDi »

At least there is one fail easy to see

Code: Select all

Task++;
if(Task == MAX_TASKS-1) Task = 0;
if you enter here when task is 254, then task++ = 255, and then the if line will sel task=0 so the task number 255 will never switched.
nullify

Re:Software task switching . How?

Post by nullify »

In hanoi's example,

Don't forget to end-of-interrupt the 8259A (i.e., outb(0x20, 0x20); ) and you might want to also switch segment registers (code, data, stack, extra segments)
OSpot

Re:Software task switching . How?

Post by OSpot »

Hi. Thank you all.

> and you might want to also switch segment registers ?>(code, data, stack, extra segments)

when and how do I switch segments?
Are they necesary ?

if I don't switch code segment ,dpl is the same prev task?

What is essential for software task switching?
OSpot

Re:Software task switching . How?

Post by OSpot »

thank you for your advise.

I try software task switching
but I don't know how to switch task.
use TSS?
OSpot

Re:Software task switching . How?

Post by OSpot »

sorry
>but I don't know how to switch task.
s/task/stack
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:Software task switching . How?

Post by Pype.Clicker »

Look for stack_switch in control.asm
It's pretty straightforward once each task have a place to store back its stack segment and stack pointer.
hanoi

Re:Software task switching . How?

Post by hanoi »

eL Jedi: just wrote it as an example..
i know it's incomplete

nullify: same for you
;D
it would also run all 256 tasks even if there are no tasks
OSpot

Re:Software task switching . How?

Post by OSpot »

to: Pype.Clicker

thank you. I read control.asm.
I think the essential code is
   mov [eax],esp.

Is it right?

I have problem on task initilize.
What should I do when the first task start.

Stack and EIP etc....
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:Software task switching . How?

Post by Pype.Clicker »

you should prepare the stack frame of each active task with what stack_switch expects to find which is:
  • values for every registers (popad)
  • values for segment registers (optionnal, popfd)
  • a valid value for ebp (pointing to a frame higher in the stack)
  • a valid return pointer (for eip, used by RET).
Iirc, what i did in Clicker was to redirect starting threads to _processIrqEnd and leave a "fake" interrupt frame on the kernel stack.
This is performed by _threadSetupEntry in kernel/mtask/thread, but this may be less easy to port ...
OSpot

Re:Software task switching . How?

Post by OSpot »

task1() :first task
task2() :second task

simply, there are two tasks.
I want to switch task1() to task2() and task2() to task1().

the way I think

(1)prepare stack frame for two tasks.(I don't know how)
(2) run task1()
task1(); -- like this ?

(3) on timer interrupt
swtich to task2();

(4) on timer interrupt
swtich to task1();


would you show me the simple sample code ?
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:Software task switching . How?

Post by Pype.Clicker »

Well, what you usually do is having a queue of "ready" tasks (waiting for CPU time) and one "running" task.

Code: Select all

on timer do:
    old_task=current;
    current.status=READY ; queue.put(current,@END);
    current=queue.head(); current.status=RUNNING;
    stack_switch(current, old_task);
    done.

on wakeup(Task X) do:
    X.status=READY;
    queue.put(X,@END);
    done.

on sleep (Task X) do:
    X.status=ASLEEP;
    current=queue.head(); current.status=RUNNING;
    stack_switch(current, X);
    done.

main: {
    Task task1 = new task(code_for_task1, stack_for_task1);
    Task task2 = new task(code_for_task2, stack_for_task2);
    Task initializor = new task();
    current = initializor;
    task1.wakeup();
    task2.wakeup();
    
    clock.install(timer);
   
    // do the rest of init job, all three tasks will be running.

    initializor.sleep();
    // and now, only task1 and task2 are running. 
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:Software task switching . How?

Post by Pype.Clicker »

well, this is a mix of Java and some event-driven pseudocode. Writing it in pure C would confuse everyone... At least, with that ugly pseudocode, you won't try to compile this template ...
OSpot

Re:Software task switching . How?

Post by OSpot »

[sup] Task task1 = new task(code_for_task1, stack_for_task1);
Task task2 = new task(code_for_task2, stack_for_task2);
[/sup]

I want to know what happens in new task()

in C or C++ or Assembler please.
Post Reply