Page 1 of 1

POSIX mapping of executables/libs into memory

Posted: Wed May 04, 2016 1:37 pm
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.

Re: POSIX mapping of executables/libs into memory

Posted: Wed May 04, 2016 3:31 pm
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.

Re: POSIX mapping of executables/libs into memory

Posted: Wed May 04, 2016 3:40 pm
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.

Re: POSIX mapping of executables/libs into memory

Posted: Wed May 04, 2016 3:49 pm
by gerryg400
For me basically mmap does 2 separate things. Having 2 separate APIs would seem to make sense.

Re: POSIX mapping of executables/libs into memory

Posted: Wed May 04, 2016 4:08 pm
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?

Re: POSIX mapping of executables/libs into memory

Posted: Wed May 04, 2016 4:35 pm
by gerryg400
Mapping files and mapping anonymous memory may be quite different depending on your design.

Re: POSIX mapping of executables/libs into memory

Posted: Wed May 04, 2016 10:46 pm
by Hellbender
mariuszp wrote:.. a non-standard system call ..
There are standards for system calls?

Re: POSIX mapping of executables/libs into memory

Posted: Thu May 05, 2016 11:01 am
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".