creating process in user space

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

creating process in user space

Post 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.
- Thanks
Vaibhav jain
User avatar
Combuster
Member
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

Post by Combuster »

That sounds like you're still copying tutorials?

How about doing the same steps except for using RAM as the disk?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: creating process in user space

Post by vjain20 »

How about doing the same steps except for using RAM as the disk?
Could you please explain ?
- Thanks
Vaibhav jain
User avatar
Combuster
Member
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

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: creating process in user space

Post 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!
User avatar
amd64pager
Member
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

Post 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.
It's surprising what the semiconductor industry's definition of macro is and what the CS description is.
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: creating process in user space

Post 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 ?
- Thanks
Vaibhav jain
User avatar
amd64pager
Member
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

Post 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. :wink:
It's surprising what the semiconductor industry's definition of macro is and what the CS description is.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: creating process in user space

Post 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.
User avatar
amd64pager
Member
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

Post 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.
It's surprising what the semiconductor industry's definition of macro is and what the CS description is.
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: creating process in user space

Post 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!
- Thanks
Vaibhav jain
User avatar
gravaera
Member
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

Post 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
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: creating process in user space

Post by vjain20 »

@gravaera - Thanks a lot! That provides a good insight.
- Thanks
Vaibhav jain
Post Reply