Page 1 of 2
Software task switching . How?
Posted: Sun Jan 12, 2003 11:40 am
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!!
Re:Software task switching . How?
Posted: Sun Jan 12, 2003 1:48 pm
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
Re:Software task switching . How?
Posted: Sun Jan 12, 2003 3:13 pm
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.
Re:Software task switching . How?
Posted: Sun Jan 12, 2003 3:40 pm
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)
Re:Software task switching . How?
Posted: Sun Jan 12, 2003 10:29 pm
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?
Re:Software task switching . How?
Posted: Mon Jan 13, 2003 12:58 am
by OSpot
thank you for your advise.
I try software task switching
but I don't know how to switch task.
use TSS?
Re:Software task switching . How?
Posted: Mon Jan 13, 2003 1:01 am
by OSpot
sorry
>but I don't know how to switch task.
s/task/stack
Re:Software task switching . How?
Posted: Mon Jan 13, 2003 2:39 am
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.
Re:Software task switching . How?
Posted: Mon Jan 13, 2003 4:42 am
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
Re:Software task switching . How?
Posted: Mon Jan 13, 2003 5:55 am
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....
Re:Software task switching . How?
Posted: Mon Jan 13, 2003 6:23 am
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 ...
Re:Software task switching . How?
Posted: Mon Jan 13, 2003 7:08 am
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 ?
Re:Software task switching . How?
Posted: Mon Jan 13, 2003 7:23 am
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.
Re:Software task switching . How?
Posted: Mon Jan 13, 2003 7:24 am
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 ...
Re:Software task switching . How?
Posted: Mon Jan 13, 2003 7:41 am
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.