17

So i always thought MMU is part of the unix kernel that translates addresses to physical addresses but in the MMU wiki page it says its a computer hardware that usually have its own memory, but that page doesn't talk much about Unix/Linux operating systems

So I'm confused, does the all the translation happen in hardware and kernel doesn't do any translation? and basically the operating system doesn't know anything about the real physical address?

I'm asking about Unix based operating systems, but if you know about other operating systems as well like windows or if its a general thing in modern computers let me know, thanks.

3 Answers 3

21

The MMU (memory management unit) is a physical component of the computer system, typically part of the CPU (but not necessarily). It translates virtual addresses (also known as linear addresses in the x86 world) to physical addresses; it can also enforce memory access control, cache control, and bus arbitration. It doesn’t usually have its own memory, it relies on data in the system’s main memory to operate.

The MMU performs this translation by using information stored in data structures such as page tables; these specify which physical address ranges correspond to linear address ranges (if any — a page can be “not present”). The page tables are set up by the kernel, and the kernel determines what the mappings should be — so the ultimate authority on physical addresses is the kernel, however it always operates with the help of the MMU. Put another way, the CPU always operates on linear addresses, which are translated to physical addresses by the MMU, but the kernel is aware of the translations and programs the MMU to perform them.

User-space processes are oblivious to all this and aren’t (normally) aware of the physical addresses corresponding to the linear addresses they use, and typically can’t access the mappings either. There are some cases where physical mappings leak, but those are usually considered to be security vulnerabilities and quickly addressed. However, in Linux, processes with sufficient privileges can see their physical map in /proc/<pid>/pagemap.

For Linux specifically, see the memory management documentation, and in particular the section on examining page tables.

5
  • So which functions in Unix/Linux are used to access page table of a specific process? and can other processes access this or part of this page table with system calls? side question: Unix/Linux uses multi level page tables right?
    – John P
    Commented Oct 4, 2018 at 15:50
  • @John “which functions”, at what level? In the kernel? Commented Oct 4, 2018 at 15:59
  • Yes, Linux uses multi-level page tables; see How does the CPU knows which physical address is mapped to which virtual address? (which I need to update to take into account four- and five-level page tables). Commented Oct 4, 2018 at 16:00
  • Yes at the kernel level, Doesn't the kernel have some sort of function where it gives the PID of that process as input and returns the starting physical address of the page table? if so, can a process access his own page table using a system call?
    – John P
    Commented Oct 4, 2018 at 16:48
  • @StephenKitt "but the kernel is aware of the translations and programs the MMU to perform them.", could you point out where are the most important pieces of linux kernel code where this logic is implemented? Where the both addresses virtual and physical both converted to each other? exact methods, it is under mm folder I assume, thank you. Commented Jul 24, 2023 at 17:45
4

The Memory Management Unit (MMU) is a piece of hardware that translates virtual addresses to physical addresses. For example, when the CPU emits an address to read, the MMU translates that virtual address to a physical address and then emits the physical address to the memory bus.

The MMU is programmed by the operating system. The operating system manages memory (both physical and virtual) and manages the virtual-to-physical mapping.

1
  • 1
    so operating systems have access to MMU and page tables to see physical address of pages right? if so, which kernel function can map the virtual addresses to physical or give us the page table? and can normal processes somehow use this by using syscalls? thanks
    – John P
    Commented Oct 4, 2018 at 15:46
0

Decided to write an answer, because I was searching exactly where this conversion from physical to virtual is done:

Let me leave a link to Linus'es original post: Translating address in Kernel Space So you can find this file

#include <asm/io.h>

not in mm folder, that I was originally expected, the reason is because difference between architecture are too wider thus each architecture has way more differences to embed everything in mm folder.

So look at arch/x86/include/asm/io.h file particularly virt_to_phys https://codebrowser.dev/linux/linux/arch/x86/include/asm/io.h.html#virt_to_phys

And according to Linus comment in x86 everything simpler because bus address are the same virtual address, the difference between bus address and virtual is that bus address is how non-CPU devices see memory layout.

And again because in x86 it is simpler and both terms(bus address and virtual address) are used interchaningly

Following define can prove my words

#define isa_bus_to_virt     phys_to_virt

Assuming that isa/pci are the external (non-CPU) devices.

If we elaborate a little bit more we can see that in the file linux/arch/powerpc/mm/ioremap_32.c which is related to other architecture (PowerPC) where memory layout for ISA devices are different

https://codebrowser.dev/linux/linux/arch/powerpc/mm/ioremap_32.c.html

and can see in the function

__ioremap_caller 

such a comment:

/*
* If the address lies within the first 16 MB, assume it's in ISA
* memory space
*/
if (p < 16 * 1024 * 1024)
    p += _ISA_MEM_BASE;

which actually confirms Linus'es word that in PowerPC it has a little bit different layout, considering that bus address and virtual addresses are different.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .