3

I have been bothering with similar questions indirectly on my other posts. Now, my understanding is better. Thus, my questions are better. So, I want to summarize the facts here. This example is based on X86-32-bit system.

Please say yes/no to my points. If no, then please explain.

  1. MMU will look into the CR3 register to find the Process - Page Directory base address.

  2. The CR3 register is set by the kernel.

  3. Now MMU after reading the Page directory base address, will offset to the Page Table index (calculated from VA), from here it will read the Page frame number, now it will find the offset on the page frame number based on the VA given. It gets the physical memory address. All this is done in MMU right? Don't know when MMU is disabled, who will do all this circus? If software then it will be slow right?

  4. I know then page fault occurs when the MMU cannot resolve the address. The kernel is informed. The kernel will update the page table based on the reading from kernel virtual memory area struct. Am I correct?

  5. Keeping in mind, the point 4. Does it mean that before executing any process. Perhaps during loading process. Does Kernel first fills the kernel virtual memory area struct. For example, where the section of memory will be BSS, Code, DS,etc. It could be that some sections are in RAM, and some are in Storage device. When the sections of the program is moved from storage to main memory, I am assuming that kernel would be updating the Kernel virtual memory area struct. Am I correct here? So, it is the kernel who keeps a close track on the program location - whether in storage device or RAM - inode number of device and file offset.

  6. Sequence wise -> During Process loading ( may be a loader program)-> Kernel will populate the data in the kernel virtual memory area struct. It will also set the CR3 register. Now Process starts executing, it will initially get some frequent page faults.Now the VM area struct will be updated (if required) and then the page table. Now, MMU will succeed in translating the address. So, when I say process accessing a memory, it is the MMU which is accessing the memory on behalf of the process. This is all about user-space. Kernel space is entirely different. The kernel space doesn't need the MMU, it can directly map to the physical address - low mem. For high mem ( to access user space from kernel space), it will do the temporary page table updation - internally. This is a separate page table for kernel, a temporary one. The kernel space doesn't need MMU. Am I correct?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
dexterous
  • 6,422
  • 12
  • 51
  • 99
  • 1
    If MMU is disabled, everyone uses physical addressing and there is no page protection. There are no page faults, no swapping, no virtual memory support of any kind. So modern OSes run with MMU disabled just long enough to set up the page tables, etc and then enable it. – Ben Voigt Nov 20 '13 at 04:29
  • An easy way to get the answer is to **look inside** the [kernel](http://kernel.org/) source code. **It is [free software](http://en.wikipedia.org/wiki/Free_software)** so you have freedom #1 "The freedom to study how the program works" ! – Basile Starynkevitch Nov 20 '13 at 06:20

1 Answers1

2

Don't know when MMU is disabled, who will do all this circus?

Nobody. All this circus is intended to do two things: translate the virtual address you gave it into a real address, and if it can't do that then to abort the instruction entirely and start executing a routine addressed from an architecturally pre-defined address, see "page fault" there for the basic one.

When the MMU is shut off, no translation is done and the address you gave it is fed directly down the CPU's address-processing pipe just as any address the MMU might have translated it to would have been.

So, when I say process accessing a memory, it is the MMU which is accessing the memory on behalf of the process.

You're on the right track here, the MMU is mediating the access, but it isn't doing the access. It's doing only what you described before, translating it. What's generally called the Load/Store unit, gets it next, and it's the one that handles talking to whatever holds the closest good copy of the data at that address, "does the access".

The kernel space doesn't need the MMU, it can directly map to the physical address

That depends on how you define "need". It can certainly shut it off, but it almost never does. First, it has to talk to user space, and the MMU has to be running to translate what user space has to addresses the Load-Store unit can use. Second, the flexibility and protection provided by the MMU are very valuable, they're not discarded without a really compelling reason. I know at least one OS will (or would, it's been a while) run some bulk copies MMU-off, but that's about it.

jthill
  • 55,082
  • 5
  • 77
  • 137
  • Thanks! My last query to you - Is the Kernel Virtual Memory struct first formed when the process is about to execute? Kindly, please update on my 6th point. What you said about user space access from kernel space conflicts with High MEM concept. Would you please elaborate it, I might have misunderstood. – dexterous Nov 20 '13 at 11:50
  • It has to be built before the process starts, yes. I'm not sure I understand your question, because I don't see any other possible answers. Can you help me on that? – jthill Nov 20 '13 at 13:26
  • So, you say that Loader will built the Kernel virtual memory struct for that particular process? I was saying that can you please acknowledge/query my 6th point in the question. Next, HIGH MEM is the part of the memory where kernel cannot access the region directly. For this it temporarily makes a pagetable entry to access the High Memory area. In this I don't see the role of MMU. Because Pagetable is in kernel space. Thus, it knows what is the mapping. It can directly look into the page table and can access it. So, In my opinion no need of MMU in kernel space is required. Am I correct? – dexterous Nov 20 '13 at 14:16
  • kernel memory management serves anyone, the loader included. It's responsible for setting up page tables so the fault handlers can decide whether to provide real storage to back an untranslatable address or pass the trouble along to the faulting process, but it's not responsible for deciding what to allocate or what to put in it. for the so-called "high memory", the mmu is certainly required: since the code is using virtual addresses, something has to translate those to the currently-valid real addresses that back them. That's the MMU's job. Code using physical addresses directly is a rarity. – jthill Nov 20 '13 at 16:27
  • I have a prog1.c, it's binary is prog1.out. Now, after executing ./prog1.out. Who sets up the kernel virtual memory area struct? Is it the loader ? Who instructs kernel to setup the kernl virtual memory area struct? The kernel drivers generally access the low mem - it is just an offset to the physical address. They uses kmalloc generally. When kernel wants to access the user-space address, are u saying that the kernel will request MMU to get it translated to a right address. Check this accepted answer here - http://stackoverflow.com/questions/1658757/linux-3-1-virtual-address-split – dexterous Nov 20 '13 at 16:51