OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 9:41 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: How mmap() (system call) works
PostPosted: Wed Oct 14, 2020 6:29 am 
Offline
Member
Member

Joined: Tue Jul 14, 2020 4:01 am
Posts: 70
I can't really find information on how this works, and the Linux source is way too complicated.


Top
 Profile  
 
 Post subject: Re: How mmap() (system call) works
PostPosted: Wed Oct 14, 2020 8:32 am 
Offline
Member
Member

Joined: Tue Aug 11, 2020 12:14 pm
Posts: 151
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.


Top
 Profile  
 
 Post subject: Re: How mmap() (system call) works
PostPosted: Wed Oct 14, 2020 9:23 am 
Offline
Member
Member

Joined: Wed Nov 18, 2015 3:04 pm
Posts: 396
Location: San Jose San Francisco Bay Area
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


Top
 Profile  
 
 Post subject: Re: How mmap() (system call) works
PostPosted: Wed Oct 14, 2020 10:14 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
Not just devices but, more generally, files.

But that's what it does, not how it does it.


Top
 Profile  
 
 Post subject: Re: How mmap() (system call) works
PostPosted: Wed Oct 14, 2020 3:21 pm 
Offline
Member
Member

Joined: Tue Apr 03, 2018 2:44 am
Posts: 401
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.


Top
 Profile  
 
 Post subject: Re: How mmap() (system call) works
PostPosted: Thu Oct 15, 2020 2:52 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
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).


Top
 Profile  
 
 Post subject: Re: How mmap() (system call) works
PostPosted: Mon Nov 30, 2020 4:04 am 
Offline
Member
Member

Joined: Tue Apr 03, 2018 2:44 am
Posts: 401
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


Top
 Profile  
 
 Post subject: Re: How mmap() (system call) works
PostPosted: Mon Nov 30, 2020 12:54 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1925
Location: Athens, GA, USA
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot], SemrushBot [Bot] and 65 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group