Multitasking issues

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.
Post Reply
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Multitasking issues

Post 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?
cyr1x
Member
Member
Posts: 207
Joined: Tue Aug 21, 2007 1:41 am
Location: Germany

Re: Multitasking issues

Post 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.
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Re: Multitasking issues

Post 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?
cyr1x
Member
Member
Posts: 207
Joined: Tue Aug 21, 2007 1:41 am
Location: Germany

Re: Multitasking issues

Post 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.
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Re: Multitasking issues

Post 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?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

But you can check out JamesM's tutorial, although I heard there're some problems with it.
:shock: you make the baby JamesM cry :(
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Post by piranha »

The problems are fixable.

-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
User avatar
lukem95
Member
Member
Posts: 536
Joined: Fri Aug 03, 2007 6:03 am
Location: Cambridge, UK

Post 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
~ Lukem95 [ Cake ]
Release: 0.08b
Image
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Post by piranha »

Ha, thats my problem too!

That I haven't gotten, I just put an expand(<large number>); right after init_paging();

-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
User avatar
lukem95
Member
Member
Posts: 536
Joined: Fri Aug 03, 2007 6:03 am
Location: Cambridge, UK

Post 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.
~ Lukem95 [ Cake ]
Release: 0.08b
Image
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Post by piranha »

I tried to get it working, and accidentally stumbled across getting usermode working.
Will do.
-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

If you find the fault, please let me know. I'll add it to my tutorial with your name on it! :)
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post 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?
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post 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
cyr1x
Member
Member
Posts: 207
Joined: Tue Aug 21, 2007 1:41 am
Location: Germany

Re: Multitasking issues

Post 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.
Post Reply