Manual process loading in C?

Programming, for all ages and all languages.
Post Reply
User avatar
Roman
Member
Member
Posts: 568
Joined: Thu Mar 27, 2014 3:57 am
Location: Moscow, Russia
Contact:

Manual process loading in C?

Post by Roman »

Hello, OSDev.org.

Recently I started working on a kernel that would be able to run in both freestanding and hosted environments. I want to manually load ELF files and build host processes from them. How can I do it? Is there any way to create a "blank" process and control its memory and machine state under UNIX/POSIX?

Google told me that I can use ptrace on a "dummy" process started by fork/execv/etc. Is it a good way? Is there something better? What about forking the kernel and replacing the forked process from inside?

Ideally, I would like a way that is supported by many platforms, but I'm also interested in system-specific paths too.
Last edited by Roman on Thu Jan 07, 2016 4:40 pm, edited 1 time in total.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: Manual process loading in C?

Post by onlyonemac »

I would recommend forking and replacing the child. Should be fairly easy to do actually. You can either cleanly discard the inherited data (open file descriptors and whatever else is passed from parent to child) or you can just ignore it and initialise your own data structures. Then you can either implement fork in your own operating system for standalone use or replace the process loader.

Something makes me think that there should be an exec that operates on memory rather than files, but obviously (for security reasons, I assume) most kernel developers decided that it would be better to avoid relying on processes correctly loading and executing an executable image (which, if it went wrong, could have drastic consequences) and rather try to enforce that the kernel does it itself.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
Post Reply