3

If not, how does the kernel build a page table when creating a new process, and modify a page table entry when swapping in/out a page of a process?

I think that instructions, running in either user mode or kernel mode, can only use a virtual address (which is then converted to a physical address by the CPU internal hardware) to access anywhere in the RAM (and memory-mapping peripherals), unless MMU is disabled.

I know that the kernel is responsible for creating and maintaining page tables while the CPU's internal hardware logic looks up in the page table(CR3 in x86 stores the base address of the page table of the current process, which is a physical address) to convert a virtual address to a physical address.

My question is: when the instructions in the kernel code that try to modify the content of a page table entry are executing, how is the virtual address (if any) of the page table itself converted to a physical address? Must MMU be disabled during that time so that no address-translation is needed?

1
  • I don't have first-hand experience with MMU code, but it would be reasonable for the page tables to have virtual addresses, since that is the only kind of memory address the CPU should be using while the MMU is enabled. No, the MMU should not be disabled just to access a page table. This really should not be that complicated. It's not like physical addresses are haphazardly assigned to virtual addresses. The kernel is in full control of these assignments, and virtual-to-physical address translation is performed automatically by the MMU for every memory access by the CPU..
    – sawdust
    Commented Jul 13, 2016 at 9:29

1 Answer 1

3

These should answer your question:
JamesM's kernel development tutorials (read the explanation under "6.4.2. Required definitions")
Paging - OSDev Wiki (read section 6 Manipulation)


Original half-baked answer:

The physical address of the current page directory1 is stored in the CPU register CR3.

Virtual address translation is dictated by the kernel, there is no cryptic internal hardware in the CPU.


Direct quote from Virtual and Physical Addresses - University of Waterloo

Dynamic Relocation

  • The memory management unit (MMU) of the CPU contains a relocation register.
  • Whenever a thread tries to access a memory location (through a virtual address), the value of the relocation register is added to the virtual memory address – dynamic binding.
  • The kernel maintains a separate relocation value1 for each process (as part of the virtual address space); changes the relocation register at every context switch.
2
  • Thank you very much. That's just what I wanted to know. Sorry for my poor English, which led you to misunderstand my question at first.
    – xiaokaoy
    Commented Jul 14, 2016 at 0:51
  • No, it was my fault, as I answered without throughout research.
    – guest
    Commented Jul 14, 2016 at 1:51

You must log in to answer this question.

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