Software task switching . How?
-
OSpot
Software task switching . How?
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!!
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?
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
#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?
At least there is one fail easy to see
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.
Code: Select all
Task++;
if(Task == MAX_TASKS-1) Task = 0;
-
nullify
Re:Software task switching . How?
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)
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?
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?
> 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?
thank you for your advise.
I try software task switching
but I don't know how to switch task.
use TSS?
I try software task switching
but I don't know how to switch task.
use TSS?
-
OSpot
Re:Software task switching . How?
sorry
>but I don't know how to switch task.
s/task/stack
>but I don't know how to switch task.
s/task/stack
- Pype.Clicker
- Member

- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Software task switching . How?
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.
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?
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
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?
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....
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....
- Pype.Clicker
- Member

- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Software task switching . How?
you should prepare the stack frame of each active task with what stack_switch expects to find which is:
This is performed by _threadSetupEntry in kernel/mtask/thread, but this may be less easy to port ...
- 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).
This is performed by _threadSetupEntry in kernel/mtask/thread, but this may be less easy to port ...
-
OSpot
Re:Software task switching . How?
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 ?
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 ?
- Pype.Clicker
- Member

- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Software task switching . How?
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.
- Pype.Clicker
- Member

- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Software task switching . How?
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?
[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.
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.