Page 1 of 1
creating process in user space
Posted: Mon Jun 04, 2012 4:24 am
by vjain20
Hi,
I am writing my own kernel and currently I am able to jump from kernel mode to user mode.
However to keep running in user mode I had to set all the page directory and page table permissions to user mode.
How can I create a process in user mode without loading from external file ? In other words the code of
the process is part of the kernel code but the process should run in user mode.
Re: creating process in user space
Posted: Mon Jun 04, 2012 4:33 am
by Combuster
That sounds like you're still copying tutorials?
How about doing the same steps except for using RAM as the disk?
Re: creating process in user space
Posted: Mon Jun 04, 2012 4:38 am
by vjain20
How about doing the same steps except for using RAM as the disk?
Could you please explain ?
Re: creating process in user space
Posted: Mon Jun 04, 2012 4:56 am
by Combuster
No, because you lack problem solving skills of your own.
A process consists of code, data, and control structures. The binary in storage provides the code and data, the rest is setup by the kernel to get it actually running. In your case, the kernel image provides the code and data, as well as the task setup.
What you need to do is determine what the situation is, both for virtual and physical memory, before loading a process, and then what the situation should be after loading a process. Then you write code to make those changes. Use of pencil and paper is mandatory.
Re: creating process in user space
Posted: Tue Jun 05, 2012 6:11 pm
by vjain20
I tried some things and got it working. But it is not elegant (or it is dirty I should say). Please comment.
Here is what I did :
- Added a section in the linker script for the user mode code.
- Wrote a function in C - initprocess() declared with the attribute 'section' (__attribute__((section("name"))) such that the code goes into the new section
and is linked to the address 0x10000000. This function just makes a system call to print something on the screen.
- Then I created a function - createinit() which
> creates page directory
> copies the code for initprocess() into a new page and maps it into page directory at the address 0x10000000.
> It allocates a page for a stack and maps it
> maps the kernel code and data by copying entries from kernel page directory
> jumps to user mode using iretd with eip = 0x10000000 and appropriate esp.
-Thanks!
Re: creating process in user space
Posted: Tue Jun 05, 2012 10:53 pm
by amd64pager
Have you got a memory manager?
If you do have one up and running,you could do this:
- compile time:
- compile an assembly program using your syscall interface(in pure binary),put it's binary code into your os
runtime:
- allocate a certain place in memory
set up data,code and stack location(set the virtual address of these to what the program expects)
copy the assembly programs code into the code area
set up pointers to the data area
put up any info you want to pass to it
do a user mode jump to it
This might be a bit complicated,but very near to how real OSes do it.
Expand it to your OSes needs.
Re: creating process in user space
Posted: Wed Jun 06, 2012 2:02 am
by vjain20
Thanks for the suggestions! I have a few queries though -
Have you got a memory manager?
If you do have one up and running,you could do this:
compile time:
compile an assembly program using your syscall interface(in pure binary),put it's binary code into your os
runtime:
I have a memory manager but I don't have a file system yet. As I understand you are suggesting to read the binary from disk which would
require a file manager. Am I right ?
set up data,code and stack location(set the virtual address of these to what the program expects)
copy the assembly programs code into the code area
set up pointers to the data area
What pointers do I need to set to data area ?
Re: creating process in user space
Posted: Wed Jun 06, 2012 2:37 am
by amd64pager
vjain20 wrote:Thanks for the suggestions! I have a few queries though -
Have you got a memory manager?
If you do have one up and running,you could do this:
compile time:
compile an assembly program using your syscall interface(in pure binary),put it's binary code into your os
runtime:
I have a memory manager but I don't have a file system yet. As I understand you are suggesting to read the binary from disk which would
require a file manager. Am I right ?
I mean put it's binary code in an array in your os - hardcode it.
vjain20 wrote:set up data,code and stack location(set the virtual address of these to what the program expects)
copy the assembly programs code into the code area
set up pointers to the data area
What pointers do I need to set to data area ?
If you are not going to put the data area at a specific place every time(which can't be done since programs have different sizes),you will have to pass a pointer in the stack or a register to the program to tell it where it is.I think I made a mistake while typing.
Re: creating process in user space
Posted: Wed Jun 06, 2012 3:06 am
by bluemoon
IMO it's better to have a working VFS before supporting userland applications, and before that, you need a working scheduler that at least can switch between kmain and itself.
Then to step forward, the first version VFS and file system support do not require to access physical storage, you can start with designing a ramdisk like many of us, and once you can load files and print the content, you can start with executable format parser/program loader and enhance process scheduling.
amd64pager wrote:I mean put it's binary code in an array in your os - hardcode it.
I don't think it's good. If you need to test for scheduler, you can create process spaces or threads using regular kernel function, you don't need byte array.
Re: creating process in user space
Posted: Wed Jun 06, 2012 5:57 am
by amd64pager
bluemoon wrote:IMO it's better to have a working VFS before supporting userland applications, and before that, you need a working scheduler that at least can switch between kmain and itself.
Then to step forward, the first version VFS and file system support do not require to access physical storage, you can start with designing a ramdisk like many of us, and once you can load files and print the content, you can start with executable format parser/program loader and enhance process scheduling.
amd64pager wrote:I mean put it's binary code in an array in your os - hardcode it.
I don't think it's good. If you need to test for scheduler, you can create process spaces or threads using regular kernel function, you don't need byte array.
I know,but it teaches you how real flat binary executable files are loaded.Replace the array and array size methods with filesystem access methods,and there - you can load flat binary files.
Re: creating process in user space
Posted: Wed Jun 13, 2012 5:43 pm
by vjain20
I mean put it's binary code in an array in your os - hardcode it.
@amd64pager - Thanks a lot for the suggestion. It is really helped!
Re: creating process in user space
Posted: Wed Jun 13, 2012 7:19 pm
by gravaera
Yo:
vjain20 wrote:Hi,
I am writing my own kernel and currently I am able to jump from kernel mode to user mode.
However to keep running in user mode I had to set all the page directory and page table permissions to user mode.
How can I create a process in user mode without loading from external file ? In other words the code of
the process is part of the kernel code but the process should run in user mode.
Hadn't noticed your question before: consider a "process" a grouping of threads which all share the same process ID and address space. Every new "process" must have all of its threads (if multiple) in the same address space (unless you intend to support another threading model). The address space in which the threads are run is of little concern -- the only thing that matters is that they are all in the same address space.
In that scenario, each "thread" has an address space (page directory or PML4) to which it is "bound" on creation, and it need not be unique. Things like file descriptors, etc are unique to the "process", still, though. Two processes may share the same address space in such a model. You may think of the address space as a canvas of sorts onto which any number of processes (groups of threads) can be painted.
Therefore the only work required to have two separate "processes" in the same address space is to determine whether or not to switch CR3 when scheduling a thread to a CPU. It's also implied that the conventional model of making Virtual Memory Management per-process would need to be deviated from, and you would need to have "per-address space" VMM, where a single address space may be shared by multiple processes and thus have the same virtual memory manager and allocator.
Apart from providing you with a clean way to support separate logical kernel processes running from the kernel address space, it has the interesting effect of allowing you to say, construct kernel services as separate processes and map multiple relevant driver processes into a single service's address space to share its address space and TLB. Finally, you need not implement anything as complex as the whole of the above -- it was really a presentation of a set of insights I think should be useful in helping you design.
--Good luck
gravaera
Re: creating process in user space
Posted: Thu Jun 14, 2012 1:58 am
by vjain20
@gravaera - Thanks a lot! That provides a good insight.