POSIX mapping of executables/libs into memory

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

POSIX mapping of executables/libs into memory

Post by mariuszp »

I am redesigning my memory manager to properly handle mapping files into memory. In fact, I want everything to technically be a memory-mapped "file", created using mmap() - including anynous memory, which will simply be a "file" filled with zeroes. Other POSIX systems take a similar approach. So basically when I map a read-only file I want it to be read into memory when a page fault occurs etc, hence preserving lots of physical memory.

I have a simple question about how ELF files should be handled. When loading from program headers, I will call mmap() on various parts of the ELF file to map it into memory as a private copy. But, as you know, the segment might be shorter in the file than it is in memory, and the rest is to be filled with zeroes.

I want to try to remain standard-conformant, so I ask, how do other POSIX-based systems do it? Do they just map as much from the file as possible, and then do another mmap() call to map anonymous memory to fill the rest of the segment with zeroes? Or is there some standard function that lets us map a smaller segment in a file to a larger segment of memory?

I hope the question is clear.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: POSIX mapping of executables/libs into memory

Post by gerryg400 »

In this case my loader will call mmap twice for the segment. Once to mmap part of the ELF file and a second time to mmap additional anonymous pages. So these segments result in 2 mappings in the mm. It probably doesn't matter but my ELF loader is in userspace.

I have not implemented mmaped files yet but have thought a lot about how I will do it.
If a trainstation is where trains stop, what is a workstation ?
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: POSIX mapping of executables/libs into memory

Post by mariuszp »

In this case i'll probably add a non-standard system call for this purpose and have an mmap() function which wraps around it, in the C library. This will let me use the full potential of my kernel and minimize the number of mappings that must be kept track of.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: POSIX mapping of executables/libs into memory

Post by gerryg400 »

For me basically mmap does 2 separate things. Having 2 separate APIs would seem to make sense.
If a trainstation is where trains stop, what is a workstation ?
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: POSIX mapping of executables/libs into memory

Post by mariuszp »

gerryg400 wrote:For me basically mmap does 2 separate things. Having 2 separate APIs would seem to make sense.
In what way does it do 2 separate things?
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: POSIX mapping of executables/libs into memory

Post by gerryg400 »

Mapping files and mapping anonymous memory may be quite different depending on your design.
If a trainstation is where trains stop, what is a workstation ?
Hellbender
Member
Member
Posts: 63
Joined: Fri May 01, 2015 2:23 am
Libera.chat IRC: Hellbender

Re: POSIX mapping of executables/libs into memory

Post by Hellbender »

mariuszp wrote:.. a non-standard system call ..
There are standards for system calls?
Hellbender OS at github.
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: POSIX mapping of executables/libs into memory

Post by mariuszp »

Hellbender wrote:
mariuszp wrote:.. a non-standard system call ..
There are standards for system calls?
I consider to be a "system call" a function which does nothing other than:

Code: Select all

function:
  mov %rcx, %r10
  mov $5, %rax
  syscall
  ret
So by "non-standard system call" I meant such a function which is not part of POSIX, and my mmap() will be a function which then calls this "non-standard system call".
Post Reply