How mmap() (system call) works

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
clementttttttttt
Member
Member
Posts: 70
Joined: Tue Jul 14, 2020 4:01 am
Libera.chat IRC: clementttttttttt

How mmap() (system call) works

Post by clementttttttttt »

I can't really find information on how this works, and the Linux source is way too complicated.
sj95126
Member
Member
Posts: 151
Joined: Tue Aug 11, 2020 12:14 pm

Re: How mmap() (system call) works

Post by sj95126 »

The current linux source is extremely complicated, but it might be worthwhile perusing one of the early sources. It looks like mmap first appeared in the 0.96 kernels.
ggodw000
Member
Member
Posts: 396
Joined: Wed Nov 18, 2015 3:04 pm
Location: San Jose San Francisco Bay Area
Contact:

Re: How mmap() (system call) works

Post by ggodw000 »

I am studying kernel 2.6.x device driver book, mmap essentially maps device addresses to user address space in PAGE_SIZE granularity. Of course there are lot to it thou.
key takeaway after spending yrs on sw industry: big issue small because everyone jumps on it and fixes it. small issue is big since everyone ignores and it causes catastrophy later. #devilisinthedetails
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How mmap() (system call) works

Post by iansjack »

Not just devices but, more generally, files.

But that's what it does, not how it does it.
thewrongchristian
Member
Member
Posts: 426
Joined: Tue Apr 03, 2018 2:44 am

Re: How mmap() (system call) works

Post by thewrongchristian »

clementttttttttt wrote:I can't really find information on how this works, and the Linux source is way too complicated.
Have a look for BSD VM documentation. This is a nice overview: http://blog.pr4tt.com/2016/02/02/BSD-virtual-memory/

FreeBSD is based on the Mach VM subsystem used in 4.4 BSD.

NetBSD/OpenBSD replaced the Mach VM based subsystem with a simpler system called UVM but retains the Mach VM pmap interface to handle the CPU specific page mapping. It is described here: https://www.netbsd.org/docs/kernel/uvm.html

I find areas such as describing VM are better served by traditional text books. The "Design and Implementation of the FreeBSD Operating System" gives a good detailed overview of the FreeBSD VM, and should help in understanding how mmap is implemented.

I also find the BSDs VM subsystems are structured better than the Linux VM subsystem, which looks quite messy in comparison, and they tend to be better described in text books. I prefer the separation of hardware independent and hardware dependent layers via an API rather than the abstract page table data structure used by Linux.

TL;DR

Study the BSD VM subsystems. If you understand how they work, the mechanism of mmap should be simple. But you really need to know how VM works as a whole in order to understand mmap.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How mmap() (system call) works

Post by iansjack »

I have an earlier version of the book that you mention (The Design and Implementation of the 4.4 BSD Operating System ) and I can heartily recommend it as an excellent explanation of the workings of an operating system. It was very expensive at the time I bought it (probably the most expensive book I had ever bought), but I note that the new version is much cheaper.

There are also a number of books explaining the Linux kernel, and the massive Mac OS X Internals, that are worth reading if you can find a copy. Much better than trying to understand any of these OSs by just reading the source.

I'm afraid that far too many people seem to plunge into OS development without doing any background reading - not even the essential programming manuals produced by the chip manufacturers. This is a huge mistake and just leads to reinvention of the wheel (or in most cases the invention of the square wheel). We can profitably build upon the experience of those who went before us. And a little research will hopefully do away with the situation where newbies come here with every single little problem that they hit along the way (the same old problems time and time again).
thewrongchristian
Member
Member
Posts: 426
Joined: Tue Apr 03, 2018 2:44 am

Re: How mmap() (system call) works

Post by thewrongchristian »

iansjack wrote:I have an earlier version of the book that you mention (The Design and Implementation of the 4.4 BSD Operating System )
Damn you! This prompted me to seek out and buy a copy of the original 4.3 BSD edition. This is the edition I read at University, and describes the VAX based virtual memory system.

:D
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: How mmap() (system call) works

Post by Schol-R-LEA »

As a rather belated aside to @clementttttttttt, is it reasonable to assume that you are building a Unix style kernel? While any other design will likely have something equivalent to mmap(), the specific system call and its operation will only really apply to a Unixoid system - a different type of OS might have a completely different memory model and implementation.

Given that you are using the Linux kernel for your research, I am guessing that a Unixoid kernel is your goal, but I wanted to be sure.

I ask this mainly because there's a possibility of an XY issue coming up here; it does you no good if we correctly answer the wrong question, after all.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
Post Reply