'Implementing Multitasking' for a beginner

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

'Implementing Multitasking' for a beginner

Post by adeelmahmood1 »

hi
i have just started working on multitasking. i have searched on net and havent find alot of useful resources. if some one could help me with some basic knowledge of multitasking and how it works etc..
as i m also a beginner so if u guys could help me with multitasking this thread could than be helpful for many other beginners
ur help is greatly appreciated
Whatever5k

Re:'Implementing Multitasking' for a beginner

Post by Whatever5k »

Two approaches are widely used: either use TSS based task-switching or stack-based (software) task-switching. Both approaches have different advantages, a search on alt.os.development will quickly show you those...

Have a look at http://osdev.neopages.net/tutorials/soft_ts.php for software task-switching...
So first choose what you want to do - software or TSS..!?
adeelmahmood1

Re:'Implementing Multitasking' for a beginner

Post by adeelmahmood1 »

well i have been reading the material u recommended to me but i still not clear in my mind how to start off coding for this ... ???
it looks like i have to setup a gdt entry which will be the TSS descriptor and then i will have a TSS which will use this descriptor .. so every time i make a new task i have to go back to my boot loader to add a new entry in the GDT .. is it like that ??
i dont know i m terribly confused about this .. u know these books stuff makes u confuse . so if someone could help me out and tell me exactly how the process goes ... some example code will be greatly appreciated
thanx for ur help
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:'Implementing Multitasking' for a beginner

Post by Pype.Clicker »

all the code i can provide is in kernel/mtask of the Clicker Project.

You're not forced to go back to your bootsector at all ! just add some code in your kernel that will allocate a large enough table, copy your actual GDT into it and reload the GDTR with the new limit/base of the GDT ...
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:'Implementing Multitasking' for a beginner

Post by distantvoices »

This is, how I do the multitasking stuff at the moment:

I have an array of TSS'es. I have plenty of Space in my GDT to give them the proper descriptors. I also have an array of Process control blocks - in which I save things like User,ProcessId, Tss-Selector, cr3, cursorx,cursory, time to run (pure round robin) signals, console to use.

Upon startup of the process subsystem, I build the tss-descriptors and enter the selectors in the Process control blocks. These are fix.

Then, for experiment, I fill in some fields of the process control blocks, the proper fields of the tss'es - to ease this, you should define a tss-type or tss-structure some where, f.ex. esp0,ss0, esp3,ss3,cr3,eip(entry of the task),esp,ss - and the segment registers. And I define one spare tss for the kernel to drop things on at the very first task switch. It will be then the tss for a kind of system thread which handles tasklets.

After having the initialization stuff done, I leave the rest to the timer interrupt handler, which decrements the time to run field in the process control block of the currently running process (there is a global pointer for this.) and the scheduler which is called at the end of the timer isr and does the rest of irq handling- like marking the irq as done and freeing interrupts before jumping to the new task - or staying with the actual task.

adeelmahmood - You have to find the way to this yourself. I can just give you hintses and trickses, as said in the idt-kernel-thread - remember? And it is my very pleasure to give hintses and trickses, because i know how crappy this stuff can be. Looking at example code is only one small part of it: getting an overall image of how in damned hell does this thing work?

I am myself at the very beginning of implementing proper debugged functions for process creation and initialization. It will cause me surely some grey hair or some cruel headache.

stay safe gosh.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
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:'Implementing Multitasking' for a beginner

Post by Pype.Clicker »

beyond infinity wrote: This is, how I do the multitasking stuff at the moment:

I have an array of TSS'es. I have plenty of Space in my GDT to give them the proper descriptors. I also have an array of Process control blocks - in which I save things like User,ProcessId, Tss-Selector, cr3, cursorx,cursory, time to run (pure round robin) signals, console to use.
imho, you should not have cursor coordinates and console information in the thread itself. These information has nothing to do with the 'thread' concept : this is something you should re-design. Maybe you could turn it into some more generic concept such as thread-local storage, but just having thread->cursorx ... i'm not convinced it's a good idea ...
Whatever5k

Re:'Implementing Multitasking' for a beginner

Post by Whatever5k »

If you don't like those TSS/GDT problems, just go for software task-switching :)
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:'Implementing Multitasking' for a beginner

Post by distantvoices »

@pype:

My threads won't get this information, but a process who has the screen assigned to it, will definitely need this information: imagine a task switch, hmm? So I just put it at a very convenient location at the moment: in the process-control-block. There, the console driver finds the necessary information and continues its work.

You see, it is not only a question of design, it is a question of convenience, where to put information about a process.

Later on, when I have made some more efforts with that stuff, I of course will take this information away from the pcb and put it in a kind of sub structure. but I stay with following: A process which sits at the video console, has to store cursor information somewhere ... as well as other informations like time to run.

@abless: Just a question of design. The tss stuff is somewhat easier to understand for me, so I have implemented it first. Now I work on stack switching stuff. I am curious if I 'll get things sorted out right.

stay safe.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
adeelmahmood1

Re:'Implementing Multitasking' for a beginner

Post by adeelmahmood1 »

well i was cheking out this code and i have a few questions about this code i have also attached the complete file but the code which actually confuses me is posted here:

Code: Select all

dword tmp;
/* 0x28 -- TSS for main() */
  tmp = ((dword)_DS)<<4;
  tmp += (word)&tss[0];
  setup_GDT_entry (&gdt[5], tmp, sizeof(TSS), ACS_TSS, 0);

  /* 0x30 -- TSS for task() */
  tmp = ((dword)_DS)<<4;
  tmp += (word)&tss[1];
  setup_GDT_entry (&gdt[6], tmp, sizeof(TSS), ACS_TSS, 0);
now first of all how does the author knows that the first entry is at 0x28 and the second one at 0x30.. then is it A gooa idea to make ur main ( which is basically ur kernel) a task ??

Code: Select all

  tss[1].eip = (word)&task;                     /* cs:eip point to task() */
now the tss 1 points to task function .. this is ok but he never points the tss 0 to main function ???
also the code is using a "sizeOf()" function does any one has implemented this function
ok now these are the building blocks i have made for my multitasking system
[glow=red,2,300]
1.make an array of segment descriptors
2.make an array of task state segments
3.define total tasks
4.define stacks for tasks
5.define tasks
6.fill entries in the GDT (over-write the previous one which is in asm, this one will be in C)
7.fill the GDT with array of TSS's
8.give values to tss registers and stuff
9.load task register
10.scheduler switches b/w task called by timer interrupt
[/glow]
waiting for ur suggestions and advises
thanz for ur help

[attachment deleted by admin]
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:'Implementing Multitasking' for a beginner

Post by Pype.Clicker »

adeelmahmood1 wrote: also the code is using a "sizeOf()" function does any one has implemented this function
sizeof() is not a function but an operator (well, okay, it does look like a function, but in fact, it is closer to '&var' or '*ptr' than to fct(x) ...
Therefore it is handled by the compiler itself ... provided that you spell it correctly "sizeof()" and not "sizeOf()" ... check your favourite C book for more info ...
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:'Implementing Multitasking' for a beginner

Post by Pype.Clicker »

adeelmahmood1 wrote: now first of all how does the author knows that the first entry is at 0x28 and the second one at 0x30..
He (or She ... why not, after all ;) ) Knows ... because he decided so. Each entry in the GDT takes up 8 bytes and -- to make it simple, ignoring TI and RPL bits for now -- the processor expects the segments register to be loaded with that offset to access a segment ... so the Nth entry is descriptor (N-1)*8.
As described in the Intel System Programming Manual, GDT[0] is reserved and must not be used, descriptors 0x08, 0x10, .., 0x20 have been defined for code/data/stack and vram respectively, so he (she) just took the next one ...

then is it A gooa idea to make ur main ( which is basically ur kernel) a task ??

Code: Select all

  tss[1].eip = (word)&task;                     /* cs:eip point to task() */
now the tss 1 points to task function .. this is ok but he never points the tss 0 to main function ???
This is because (s)he does not jump to that task: (s)he declares it as the current task, just to have a place to store back the CPU state in case of a task switch.
So tss[0].eip will be filled by the CPU when JMP Far 0x30:0 is issued.

It may seem weird at the first sight, but the CPU needs TR to be loaded with a valid TSS selector when it has to store the actual state before restoring the state of another task.
It also need a valid TR when a interrupt occur in DPL level 3 so that it can find back SS0 and ESP0.

Considering those things, you *need* to have a task for your kernel ... not necessarily a task that will never enter user mode (that's what Linux does: a single task that moves accross address spaces and switches between user and supervisor mode), but a task nonetheless ...
adeelmahmood1

Re:'Implementing Multitasking' for a beginner

Post by adeelmahmood1 »

well right now i m just trying to change my GDT to C so that after that i could be able to add the TSS entries in my C kernel .. i m attaching the gdt.c file which is a very simple one to understand and i m calling this
setup_GDT() in my C kernel but on running i get the same old annoying bochs error
fetch-raw-descriptor LDTR.valid=0
and sometimes i get a
load-seg-reg GDT es.index (1fea) >es.limit (00019)
so wat do u think
ur help is greatly appreciated

[attachment deleted by admin]
FlashBurn

Re:'Implementing Multitasking' for a beginner

Post by FlashBurn »

Could someone explain the processID in more detail?! How can I give these ID?s? Because when I give a process an ID and then increase a var, I will get into trouble. Imagine that then there is a new process and I read this var and give the process the next number. Then my old process get killed. Then I have a free number, but I can?t decrease my var, because then I will have 2 processes with the same ID! Have can work with the ID?s, or better, how can I organize them?
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:'Implementing Multitasking' for a beginner

Post by distantvoices »

The processID is merely a book keeping feature of unix-like operating systems and mainframes: It is just a number stored in the process-structure.

I am not sure, but you might find it possible to hand out processID's at the base: next free number. You needn't scan for free numbers or so: You just hand out the next one to a new process and increase the counter. So I imagine this. I haven't implementet that kind of stuff yet since I am now about to build some functions to conveniently create processes/tasks.

Mark the following: You don't need to decrease this var nor care about free numbers. If you kill a process, its good. No need to care about those numbers, hm? there are about 65000 free ones if you take an int as storage for it.

As for organizing these ID'S: In the Kernel, you work with Process/Thread structures. these are passed thro and fro' queues related to running/waitng/sleeping/blocked states by the scheduler and other functions which take care of this moving - If you know about linked Lists, you will understand that only pointers are moved around.
Each of these PROCESS/Thread structures has - depending on your design - a field for the process ID.

You can organize processes in a kind of tree: each process saves the PID of its creator. so a child process knows about its parent process.

the OS-Gurus here may explain this to you in a more accurate way than I did.

stay safe
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
Post Reply