Multitasking

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
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Multitasking

Post by vjain20 »

I am following JamesM tutorial (not completely though) to write my kernel. I want to implement multitasking in kernel mode as given in the tutorial
http://www.jamesmolloy.co.uk/tutorial_h ... sking.html. But my code differs from the tutorial in that
I defined a stack for the kernel separately instead of using the one provided by grub so that I don't have to move the stack as done in the tutorial.
As per the tutorial, to create a child process I have to create a copy of parent process page directory such that all the entries mapping the kernel
are copied whereas for all other entries the data is copied and new page tables are created and mapped to the child page directory. This is done by comparing the parent process page directory to the kernel directory. Since the parent process is a kernel mode process its page directory is the kernel directory itself

Code: Select all

 if (kernel_directory->tables[i] == src->tables[i])
        {
           // It's in the kernel, so just use the same pointer.
           dir->tables[i] = src->tables[i];
           dir->tablesPhysical[i] = src->tablesPhysical[i];
        }
        else
        {
           // Copy the table.
           u32int phys;
           dir->tables[i] = clone_table(src->tables[i], &phys);
           dir->tablesPhysical[i] = phys | 0x07;
        }

Since in my implementation the kernel stack is also mapped into the kernel directory this code will not work as the entries for stack
will also be copied from the parent process page directory instead of creating a new stack for the child.
This is how I defined the stack -

Code: Select all

section .multiboot
MultibootHeader:
	dd MAGIC
	dd FLAGS
	dd CHECKSUM

section .text

STACKSIZE equ 0x4000 		;16KB

loader:
	cmp eax, 0x2BADB002	; verify booted with grub
	jne .bad
	
	mov esp, STACKSIZE + stack
	mov ax, 0x10
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax         

.test:
	push ebx
	call kmain

.bad:
       jmp $ 

section .data
align 4                             ; stack should be 4-byte aligned
stack:
	TIMES STACKSIZE db 0

Please suggest how to get the stack addresses so that the data can be copied while the rest of the kernel area is just mapped and not copied.
-Thanks
Vaibhav Jain
- Thanks
Vaibhav jain
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: Multitasking

Post by Nessphoro »

Create a new stack for the child yourself?

Like, in my OS stacks are not shared, so every time I create a thread it gets its own stack.
And when you create a thread you give an address where the execution should start so the previous thread's stack doesn't affect anything.
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: Multitasking

Post by vjain20 »

But since it is a child process and not another thread I need to copy the stack of the
parent process. That's what the code in tutorial does. It copies the stack but not the rest of the kernel.
- Thanks
Vaibhav jain
User avatar
Nessphoro
Member
Member
Posts: 308
Joined: Sat Apr 30, 2011 12:50 am

Re: Multitasking

Post by Nessphoro »

James' tutorial is broken in many ways, I wouldn't suggest following it word-in-word.
A child process is still a thread.
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Re: Multitasking

Post by piranha »

Each task must have its own stack, otherwise they are going to trash each other. In fork, you must copy the stack over. If you put the stack somewhere in the address space that is copied and not linked, and use the clone_directory code thats in the tutorial, you'll get a nice new stack auto-magically. I'd suggest that you follow the tutorial more closely if you're going by that, or just start over with your own code (as its true - that tutorial will not be a good base to build off of).

Basically, either don't put the stack there, or move it later. Or add code to skip over the pages of the stack in the clone_directory function and force them to be copied.

-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: Multitasking

Post by vjain20 »

Add code to skip over the pages of the stack in the clone_directory function and force them to be copied.
How to do this is my question exactly.

-Thanks
Vaibhav Jain
- Thanks
Vaibhav jain
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: Multitasking

Post by vjain20 »

You have the address of the stack in the parent process, so you know to which PDEs and PTEs in the page directory it corresponds.
Are you referring to the stack label in assembly file - loader.asm ?



-Thanks
Vaibhav Jain
- Thanks
Vaibhav jain
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: Multitasking

Post by vjain20 »

I'm referring to the stack you're running on.

You have added it to the mapping somehow, so you should be aware of its whereabouts.
The kernel stack is mapped just like the rest of kernel by identity mapping the first 4 MB of memory.
I haven't mapped it separately. So the only thing I can think of is the stack label in loader.asm at which
stack is defined.

Code: Select all

 mov esp, STACKSIZE + stack
   mov ax, 0x10
   mov ds, ax
   mov es, ax
   mov fs, ax
   mov gs, ax         

.test:
   push ebx
   call kmain
.bad:
       jmp $ 
section .data
align 4                             ; stack should be 4-byte aligned

stack:          ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   TIMES STACKSIZE db 0
[/size]
- Thanks
Vaibhav jain
User avatar
serviper
Member
Member
Posts: 31
Joined: Sat Jul 16, 2011 6:05 am
Location: China
Contact:

Re: Multitasking

Post by serviper »

vjain20 wrote:
I'm referring to the stack you're running on.

You have added it to the mapping somehow, so you should be aware of its whereabouts.
The kernel stack is mapped just like the rest of kernel by identity mapping the first 4 MB of memory.
I haven't mapped it separately. So the only thing I can think of is the stack label in loader.asm at which
stack is defined.
Why not allocate another page for the first kernel stack after paging is enabled?
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: Multitasking

Post by vjain20 »

Why not allocate another page for the first kernel stack after paging is enabled?
Does that mean that once paging is enable another stack should be created by allocating a page(s) and then esp should be set to
point to the bottom of this stack? In that case wouldn't it require copying contents of the old stack (defined in loader.asm) to the
new stack ?


-Thanks
Vaibhav Jain
- Thanks
Vaibhav jain
User avatar
serviper
Member
Member
Posts: 31
Joined: Sat Jul 16, 2011 6:05 am
Location: China
Contact:

Re: Multitasking

Post by serviper »

vjain20 wrote:
Why not allocate another page for the first kernel stack after paging is enabled?
Does that mean that once paging is enable another stack should be created by allocating a page(s) and then esp should be set to
point to the bottom of this stack? In that case wouldn't it require copying contents of the old stack (defined in loader.asm) to the
new stack ?


-Thanks
Vaibhav Jain
If you set up such a kernel stack in function A and then call B and never return, it's ok to begin with an empty stack.
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: Multitasking

Post by vjain20 »

If you set up such a kernel stack in function A and then call B and never return, it's ok to begin with an empty stack.
I just thought of that and put an infinite loop at the end of main(). Thanks!

-Thanks
Vaibhav Jain
- Thanks
Vaibhav jain
Post Reply