creating process in user space
creating process in user space
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.
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.
- Thanks
Vaibhav jain
Vaibhav jain
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: creating process in user space
That sounds like you're still copying tutorials?
How about doing the same steps except for using RAM as the disk?
How about doing the same steps except for using RAM as the disk?
Re: creating process in user space
Could you please explain ?How about doing the same steps except for using RAM as the disk?
- Thanks
Vaibhav jain
Vaibhav jain
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: creating process in user space
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.
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
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!
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!
- amd64pager
- Member
- Posts: 73
- Joined: Fri Nov 25, 2011 8:27 am
- Location: In the 266 squadron of the RFC,near Maranique in the Southern Front in the WW1
Re: creating process in user space
Have you got a memory manager?
If you do have one up and running,you could do this:
Expand it to your OSes needs.
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
- 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
Expand it to your OSes needs.
It's surprising what the semiconductor industry's definition of macro is and what the CS description is.
Re: creating process in user space
Thanks for the suggestions! I have a few queries though -
require a file manager. Am I right ?
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 wouldHave 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:
require a file manager. Am I right ?
What pointers do I need to set to data area ?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
- Thanks
Vaibhav jain
Vaibhav jain
- amd64pager
- Member
- Posts: 73
- Joined: Fri Nov 25, 2011 8:27 am
- Location: In the 266 squadron of the RFC,near Maranique in the Southern Front in the WW1
Re: creating process in user space
I mean put it's binary code in an array in your os - hardcode it.vjain20 wrote:Thanks for the suggestions! I have a few queries though -
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 wouldHave 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:
require a file manager. Am I right ?
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.vjain20 wrote:What pointers do I need to set to data area ?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
It's surprising what the semiconductor industry's definition of macro is and what the CS description is.
Re: creating process in user space
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.
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.
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.amd64pager wrote:I mean put it's binary code in an array in your os - hardcode it.
- amd64pager
- Member
- Posts: 73
- Joined: Fri Nov 25, 2011 8:27 am
- Location: In the 266 squadron of the RFC,near Maranique in the Southern Front in the WW1
Re: creating process in user space
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.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.
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.amd64pager wrote:I mean put it's binary code in an array in your os - hardcode it.
It's surprising what the semiconductor industry's definition of macro is and what the CS description is.
Re: creating process in user space
@amd64pager - Thanks a lot for the suggestion. It is really helped!I mean put it's binary code in an array in your os - hardcode it.
- Thanks
Vaibhav jain
Vaibhav jain
- gravaera
- Member
- Posts: 737
- Joined: Tue Jun 02, 2009 4:35 pm
- Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.
Re: creating process in user space
Yo:
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
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.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.
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
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
Re: creating process in user space
@gravaera - Thanks a lot! That provides a good insight.
- Thanks
Vaibhav jain
Vaibhav jain