Page 1 of 2
Scheduling & Task Switching
Posted: Sun Nov 27, 2005 1:27 pm
by 0xBADC0DE
Hi, I've been making an operating system over the past few days and I've gotten up to the point where the system's in Pmode, I can put text on the screen, and paging, interrupts, and the gdt are all initilized. However, I'm having a bit of difficulty with task-switching.
I would like to know how to jump to a new task, as in save current registers, load new task's registers, and jump, and how to save the current task's registers before jumping to a new task.
Thanks!
Re:Scheduling & Task Switching
Posted: Sun Nov 27, 2005 1:34 pm
by Cjmovie
I know beyond infinity has a tutorial, but considering I have no idea where it's located, for my post at least, you must be satisified with my tutorial
.
http://hosted.cjmovie.net/TutMultitask.htm
Of course, any questions following the reading of this I will be glad to answer....and add to the tutorial.....
Hope it's of some help.
Re:Scheduling & Task Switching
Posted: Sun Nov 27, 2005 1:48 pm
by 0xBADC0DE
thx. Reads better than infinity's
Re:Scheduling & Task Switching
Posted: Sun Nov 27, 2005 2:11 pm
by Dex4u
Here is a simple demo.
Re:Scheduling & Task Switching
Posted: Sun Nov 27, 2005 3:25 pm
by Humble
hi all
Cjmovie, i need to know what changes should make in order to use cpl3 user-space process, do i need to change the
to
and about segment registers, do the descriptor DPL should be higher or lower than RPL & CPL ??
to make it a bit clearer, should i make a descriptor in GDT with DPL 11 and the RPL 11 , or i can use the same descriptors with DPL 00 but use the RPL 11 ??
finally what changes should i make in order to make cpl3 works ?
Re:Scheduling & Task Switching
Posted: Sun Nov 27, 2005 3:37 pm
by Cjmovie
Hello Humble,
I read the part that says '11', I'm thinking....it only goes 0-3. But, you mean 11 in binary
.
First of all, I am _not_ an expert, and have yet to implement them myself.
So, all I can do is refer you to: Volume 3, section 5.17 of intel manual. Particulry (spelling?) Figure 5-4.
Re:Scheduling & Task Switching
Posted: Sun Nov 27, 2005 3:43 pm
by tiger
Hi, it's osdever registered.
Anyways, cjmovie, in your code it fake pushes EAX twice. Are you sure that's right?
Re:Scheduling & Task Switching
Posted: Sun Nov 27, 2005 3:47 pm
by Humble
Cjmovie : thanks, no problem
but can any one like pype, df or beyond infinity answer my question
Cjmovie i think there is a mistake in ur code
Code: Select all
push esp ;Push pointer to all the stuff we just pushed
call _TaskSwitch ;Call C code
mov al, 0x20 ;Port number AND command number to Acknowledge IRQ
out al, al ;Acknowledge IRQ, so we keep getting interrupts
mov esp, eax ;Replace the stack with what the C code gave us
should be
Code: Select all
push esp ;Push pointer to all the stuff we just pushed
call _TaskSwitch ;Call C code
mov esp, eax ;Replace the stack with what the C code gave us
mov al, 0x20 ;Port number AND command number to Acknowledge IRQ
out al, al ;Acknowledge IRQ, so we keep getting interrupts
since the mov al, 0x20 changed the returned value of eax
Re:Scheduling & Task Switching
Posted: Sun Nov 27, 2005 11:31 pm
by pradeep
It's true that peoples are always good at finding others mistake but not theirs.
Re:Scheduling & Task Switching
Posted: Sun Nov 27, 2005 11:41 pm
by Cjmovie
It's also true one should learn from their mistakes. That's why I've updated the tutorial fixing a total of 4 errors
.
Re:Scheduling & Task Switching
Posted: Mon Nov 28, 2005 7:38 pm
by tiger
Also, you've made a few more mistakes
Code: Select all
eax:0x00000022, ebx:0x00000000, ecx:0x00000000, edx:0x000003d5
ebp:0x00000000, esp:0x00a0558f, esi:0x00000000, edi:0x00102170
eip:0x00100061, eflags:0x00000002, inhibit_mask:0
cs:s=0x0008, dl=0x0000ffff, dh=0x00cf9a00, valid=1
ss:s=0x0010, dl=0x0000ffff, dh=0x00cf9300, valid=7
ds:s=0x0010, dl=0x0000ffff, dh=0x00cf9300, valid=7
es:s=0x0010, dl=0x0000ffff, dh=0x00cf9300, valid=1
fs:s=0x0010, dl=0x0000ffff, dh=0x00cf9300, valid=1
gs:s=0x0010, dl=0x0000ffff, dh=0x00cf9300, valid=1
ldtr:s=0x0000, dl=0x00000000, dh=0x00000000, valid=0
tr:s=0x0000, dl=0x00000000, dh=0x00000000, valid=0
gdtr:base=0x00106070, limit=0x17
idtr:base=0x001060a0, limit=0x7ff
dr0:0x00000000, dr1:0x00000000, dr2:0x00000000
dr3:0x00000000, dr6:0xffff0ff0, dr7:0x00000400
cr0:0x80000011, cr1:0x00000000, cr2:0x00000000
cr3:0x00001000, cr4:0x00000000
Your tutorial does not work - you push the registers in the wrong order. I found this out while I was extending your tutorial.I extended it so it could support threads returning. I also extended it to pass paramaters to a stub in registers eax and ebx. That stub calls eax with the paramaters in ebx. While I was debugging it, I found out that it jumped to the thread stub function, which printed on the screen which address it was jumping to and what was the value of the paramater. However, it was going to call address 0, which resulted in a ISR6 Exception. In the debugger log, I found out that the address of the function was instead stored in EDI.
CreateThreadCode:
Code: Select all
////////////////////////////////////////////
// Find Empty Thread Structure
unsigned long curr_thread;
for(curr_thread=0;curr_thread<TH_MAXTHREADS;curr_thread++)
if(threads[curr_thread].flags == TH_UNUSED)
break;
if(curr_thread == TH_MAXTHREADS)
return TH_MAXTHREADS; // Return TH_MAXTHREADS if error occured - will never be valid thread handle
///////////////////////////////////////////
// Fill That Structure
threads[curr_thread].keEspBase = (unsigned long)malloc(TH_STACKSIZE);
threads[curr_thread].keEsp = threads[curr_thread].keEspBase + TH_STACKSIZE; // Start at top of stack
unsigned long *esp = (unsigned long*)threads[curr_thread].keEsp;
//////////////////////////////////////////
// IRET Stack Frame
*--esp = 0x0202; // EFLAGS
*--esp = 0x08; // CS
*--esp = (unsigned long)KeAsmThreadStub; // EIP
/////////////////////////////////////////
// PUSHA, POPA
*--esp = 0; // EDI
*--esp = 0; // ESI
*--esp = 0; // EBP
*--esp = 0; // Offset
*--esp = param; // EBX - Paramater
*--esp = 0; // EDX
*--esp = 0; // ECX
*--esp = (unsigned long)en; // EAX - KeThreadStub calls this
/////////////////////////////////////////
// Segment Registers
*--esp = 0x10;// GS
*--esp = 0x10;// FS
*--esp = 0x10;// DS
*--esp = 0x10;// ES
threads[curr_thread].flags = flags;
threads[curr_thread].keEsp = (unsigned long)esp;
threads[curr_thread].priority = prio;
return curr_thread;
KeAsmThreadStubCode:
Code: Select all
push ebp
push ebx
push eax
call _KeThreadStub
pop eax
pop ebx
pop ebp
ret
KeThreadStub Code
Code: Select all
void KeThreadStub(KeThreadEntry entry,unsigned long param)
{
#ifdef DEBUG
printf("[DEBUG]: Calling function @ %x with param @ %x",entry,param); // ENTRY=EAX, PARAM=EBX
#endif
KeThreadExit(entry(param));
}
More suprising, your old code worked better than the new code.
What you thought was 'EDI' was actually 'EAX', because I never got a signle exception, due to my stub code actually jumped to the right address.
Re:Scheduling & Task Switching
Posted: Tue Nov 29, 2005 3:35 am
by Rob
Re:Scheduling & Task Switching
Posted: Tue Nov 29, 2005 4:38 am
by Pype.Clicker
Humble wrote:
Cjmovie : thanks, no problem
but can any one like pype, df or beyond infinity answer my question
Yeah Sure ....
erhm, someone, what was the question, again ?
Nah, seriously, man, how am i supposed to answer a question i don't see about some *stacksetup--=0x3020 i never heard of ?
All i can offer you is a pointer to my own code in
CVS ...
Re:Scheduling & Task Switching
Posted: Tue Nov 29, 2005 5:52 am
by distantvoices
www.distantvoices.org/html/multitasking.html
that's the loco of my tutorial.
If you consider it crap to read - well, proofread it, make corrections, if you feel competent for doing so, and send me a mail about so I can include your stuff, eh?
"reads better than infinity's" is definitely not very helpful from my point of view.
as for the question,if there's been any: I have to read up myself for that kinda cruciality, so why don't you check the intel manuals yourself, dear humble? I don't know by heart and currently lack the time.
Has not the OSFAQ some hints about this too?
stay safe
Re:Scheduling & Task Switching
Posted: Tue Nov 29, 2005 11:21 am
by Humble
Pype.Clicker wrote:
Humble wrote:
Cjmovie : thanks, no problem
but can any one like pype, df or beyond infinity answer my question
Yeah Sure ....
erhm, someone, what was the question, again ?
Nah, seriously, man, how am i supposed to answer a question i don't see about some *stacksetup--=0x3020 i never heard of ?
All i can offer you is a pointer to my own code in
CVS ...
Here is my question:
What changes should i make in order to use cpl3 user-space process, do i need to change the
Code: Select all
*stacksetup--=0x0202;
[code]
to
[code]
*stacksetup--=0x3202;
this code is taken from beyond infinity's tutorial.
What should i do with the segment register, should the descriptor's DPL should be higher or lower than RPL & CPL ??
to make it a bit clearer, should i make a descriptor in GDT with DPL 11 and the RPL 11 , or i can use the same descriptors with DPL 00 but use the RPL 11 ??
My Question:
Finally what changes in beyond infinity's tutorial should i make in order to make cpl3 works ?
Thanks Pype