how to create the first user process?

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
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

how to create the first user process?

Post by ckzippo »

Hello,every one.I'm going to create the first process in my kernel.
I have create several kernel processes in my OS,that means they run in ring 0.Because there are no privilege changes when interrupt occurs,this seems easy,and i have done that.
I want to create user process that run in ring 3, and i have nothing in mind what should i do.
Can anyone tell me what should i do to create such process or any code to read or books?

Thank you very much.
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: how to create the first user process?

Post by Combuster »

If you can make a kernel thread, the only thing it needs to do to land in userspace is to build a custom stackframe containing eip/cs/eflags/esp/ss and IRET to it. The intel manuals should help you out from there.
"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 ]
Nable
Member
Member
Posts: 453
Joined: Tue Nov 08, 2011 11:35 am

Re: how to create the first user process?

Post by Nable »

I hope that helps.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: how to create the first user process?

Post by bluemoon »

When you work on implementing user-space, I suggest:
1. If you have not handle exception yet, do it now (including #TS, #SS and #GP).
2. Tweak the syscall interface, if using interrupt, set user bits so that accessible from ring3
3. plan the address layout
4. you only need a few instructions to enter ring3, I'll leave you figure out the detail from the manual.

The next thing would be create user-space from executable file, you will need:
1. File system support (operate on physical device or ram drive), a minimum VFS layer would do fine.
2. Program loader, which extract the program into address space, do relocation, etc. Consult the ELF document(or executable format of your choice) for detail.
3. You're done here.
cheekygirl
Posts: 1
Joined: Wed Jul 25, 2012 5:32 pm
Contact:

Re: how to create the first user process?

Post by cheekygirl »

I’m still learning about Ubuntu. Far away to patch the kernel. Lol
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

Re: how to create the first user process?

Post by ckzippo »

bluemoon wrote:When you work on implementing user-space, I suggest:
1. If you have not handle exception yet, do it now (including #TS, #SS and #GP).
2. Tweak the syscall interface, if using interrupt, set user bits so that accessible from ring3
3. plan the address layout
4. you only need a few instructions to enter ring3, I'll leave you figure out the detail from the manual.

The next thing would be create user-space from executable file, you will need:
1. File system support (operate on physical device or ram drive), a minimum VFS layer would do fine.
2. Program loader, which extract the program into address space, do relocation, etc. Consult the ELF document(or executable format of your choice) for detail.
3. You're done here.
Thank you.
1.I have implement interrupt mechanism, but just print which interrupt has happend.
2.i use 0x80 as the syscall vector, i just set the privilege as 3 and type trap gate, i wonder whether this can work.
3.i can not understand exactly.
4.i think the instructions are push several registers and iret.But several problems confuse me.
a.i use page,i wonder whether should i set LDT? i see some codes do that.but i think there is no need.
b.i want to use the kernel code as the first process's code.how to set the eip?
c.how to set a TSS.which part of TSS should i set? i just set ss0,sp0,can this work?

Thanks again.
ckzippo
Member
Member
Posts: 27
Joined: Wed Jun 06, 2012 4:10 am

Re: how to create the first user process?

Post by ckzippo »

Combuster wrote:If you can make a kernel thread, the only thing it needs to do to land in userspace is to build a custom stackframe containing eip/cs/eflags/esp/ss and IRET to it. The intel manuals should help you out from there.
thanks. :)
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: how to create the first user process?

Post by bluemoon »

ckzippo wrote: > 3. plan the address layout
3.i can not understand exactly.
It's better to plan the address space layout in early stage, basically you just slice the whole address space into zones, for example:
3-4G Kernel
2-3G Global Resources, Drivers
0-2G Applications:
* 2G-32M ~ 2G: Process specific information, handle mappings
* 2G-64M ~ 2G-32M: Default stack
* 4M and Up: Application
ckzippo wrote:4.i think the instructions are push several registers and iret.But several problems confuse me.
a.i use page,i wonder whether should i set LDT? i see some codes do that.but i think there is no need.
LDT is optional, and it's generally not use for flat model
ckzippo wrote:b.i want to use the kernel code as the first process's code.how to set the eip?
1. Set DS/ES/FS/GS to data selector +3
2. push value of new SS(data selctor+3)
3. push application stack (new value of esp)
4. push new value of flags (e.g. 0x202 to enable interrupts)
5. push new value of CS (code selector +3)
6. push new value of EIP
7. IRETD
ckzippo wrote:c.how to set a TSS.which part of TSS should i set? i just set ss0,sp0,can this work?
For 32-bit TSS, you only need to set SS0 and ESP0 (which refer to kernel stack, it can be different per each user thread, search kernel-stack per thread vs per core for more), and optionally IOPL
Post Reply