Page 1 of 1
Multitasking issues
Posted: Sat Mar 15, 2008 10:15 am
by Jeko
I have some questions about software multitasking.
When I create a task, must I create a stack for user and a stack for kernel or only one of these? How must I use they?
Which values must I save in kernel stack and which in user stack?
(Using paging) Must I have static addresses? (for example 0x10000000 for code, 0x40000000 for heap)
While creating a task, how can I access to virtual memory of this task?
Re: Multitasking issues
Posted: Sat Mar 15, 2008 11:27 am
by cyr1x
MarkOS wrote:
When I create a task, must I create a stack for user and a stack for kernel or only one of these? How must I use they?
You need one for the kernel, which is used for interrupts and for task-switching(that is you push the current state on the kernel-stack).
Then the user stack is used by the program itself. When calling a function, etc. the values get pushed onto the user stack.
MarkOS wrote:Which values must I save in kernel stack and which in user stack?
See above.
MarkOS wrote:(Using paging) Must I have static addresses? (for example 0x10000000 for code, 0x40000000 for heap)
Of course not, but it's alot easier to handle. Supporting randomized code sections is very hard(ASLR) (OpenBSD and Vista are doing it for example).
MarkOS wrote:
While creating a task, how can I access to virtual memory of this task?
You create a new page directory put the code in it, load the CR3 with the page directories address.
Re: Multitasking issues
Posted: Sun Mar 16, 2008 8:31 am
by Jeko
cyr1x wrote:
MarkOS wrote:
While creating a task, how can I access to virtual memory of this task?
You create a new page directory put the code in it, load the CR3 with the page directories address.
I know this, but it isn't a waste of resources to switch PDBRs while creating tasks?
Another thing:
In the scheduler code, when I switch tasks, must I change any value in the TSS?
EDIT: Have you any source code of creating tasks in software multitasking with paging?
Re: Multitasking issues
Posted: Sun Mar 16, 2008 12:19 pm
by cyr1x
MarkOS wrote:
I know this, but it isn't a waste of resources to switch PDBRs while creating tasks?
You don't switch the PD while creating the task. The task/process resides in the PD. This is the point of paging that each process has it's own address space(virtual memory)
MarkOS wrote:
Another thing:
In the scheduler code, when I switch tasks, must I change any value in the TSS?
No, unless you make use of the hardware task-switching feature, but you must keep track of CR3 and ESP of each task.
MarkOS wrote:
EDIT: Have you any source code of creating tasks in software multitasking with paging?
No as I'm currently redesigning and rewriting my kernel. But you can check out JamesM's tutorial, although I heard there're some problems with it.
Re: Multitasking issues
Posted: Sun Mar 16, 2008 1:19 pm
by Jeko
cyr1x wrote:You don't switch the PD while creating the task. The task/process resides in the PD. This is the point of paging that each process has it's own address space(virtual memory)
How can I use memory mapped into a PD of a user task with the kernel, if I haven't mapped it also in kernel's address space?
For example if I want to insert something in the user stack at the beginning I must allocate a physical page, map it into the address space of the user, map it into the address space of the kernel, change values, and unmap from kernel's address space. Or not?
Posted: Sun Mar 16, 2008 1:24 pm
by JamesM
But you can check out JamesM's tutorial, although I heard there're some problems with it.
you make the baby JamesM cry
Posted: Sun Mar 16, 2008 1:25 pm
by piranha
The problems are fixable.
-JL
Posted: Sun Mar 16, 2008 1:35 pm
by lukem95
could you post a fix? iv been looking at my stack which is heavily based on that code, and it just doesn't seem to expand properly when more space is required.
i may be wrong however
Posted: Sun Mar 16, 2008 1:45 pm
by piranha
Ha, thats my problem too!
That I haven't gotten, I just put an expand(<large number>); right after init_paging();
-JL
Posted: Sun Mar 16, 2008 1:50 pm
by lukem95
mmm, thats just a hack-fix though really.
im trying to go through it as we speak. PM me if you find something please.
Posted: Sun Mar 16, 2008 2:41 pm
by piranha
I tried to get it working, and accidentally stumbled across getting usermode working.
Will do.
-JL
Posted: Sun Mar 16, 2008 2:50 pm
by JamesM
If you find the fault, please let me know. I'll add it to my tutorial with your name on it!
Posted: Thu Mar 20, 2008 4:31 pm
by Jeko
In a privilege level switch (user-kernel-user) must I change any value in the TSS? Or must I leave the TSS as it is when I load it the first time?
So the scheduler will do:
push all regs
push gs, fs, ds, es
push the stack
change the task
pop the new stack
pop gs, fs, ds, es
pop all regs
iret
right?
or:
push all regs
push gs, fs, ds, es
push the stack
change the task
change some values in TSS
pop the new stack
pop gs, fs, ds, es
pop all regs
iret
In the second case, which values must I change?
Posted: Thu Mar 20, 2008 5:37 pm
by t0xic
I use software multitasking, and I don't have a TSS because I'm staying in ring 0 for now. If you are intending to switch between rings, you will need a TSS, and will have to change some values (esp0 at a minimum). Here is a link to a tutorial:
http://www.osdever.net/tutorials/multitasking.php
Re: Multitasking issues
Posted: Fri Mar 21, 2008 3:43 am
by cyr1x
MarkOS wrote:For example if I want to insert something in the user stack at the beginning I must allocate a physical page, map it into the address space of the user, map it into the address space of the kernel, change values, and unmap from kernel's address space. Or not?
Why the hell the kernel needs to know that you are accessing the user stack?
And even then, in general the kernel is mapped in every process' address space.
MarkOS wrote:In a privilege level switch (user-kernel-user) must I change any value in the TSS? Or must I leave the TSS as it is when I load it the first time?
You need to adjust the ESP0-field on a task-switch.