I know that there is a hardware in CPU, the MMU which takes care of mapping a given address to physical address. For example:

int *ptr = 0x12345

Will make a pointer to my process address of 0x12345 which gets translated by CPU to a real address. Now my question is if I write a small kernel myself and run the above code, is 0x12345 is still considered a virtual address or I can access the physical address directly at OS level?

  • If you are writing an OS you'll use special instructions to control the MMU – whatsisname Sep 30 '18 at 16:27
  • The line of code you posted is Undefined Behavior. – Robert Harvey Sep 30 '18 at 18:13

That depends on your OS, whose design-choices are constrained by the CPU. And naturally on whether you are running on the bare metal or under a hypervisor / in a vm.

If you have the ability to use paging in kernel-mode and enough address-space, there is good reason to map everything relevant: It makes things simpler and more performant.

Using the identity-mapping actually makes code more difficult when interacting with user-mode.

  1. User-space, which means the current process.
  2. The kernel.
  3. All the physical memory.
  4. All of device-space.

You might have to compromise in mapping the physical memory due to space-constraints. In that case, you would only map the page-table, and dedicate some space for each CPU's private ephemeral work-mappings.


I would like to answer my own question. Yes at OS level you have access to the physical memory. a boot loader (like GRUB) usually boots you into a protected mode environment where paging is off, which means there is no virtual memory setup yet which means you are using the physical memory directly.

  • Well, something obviously has access to physical memory directly, otherwise you wouldn't be able to malloc at all. The boot loader would obviously have direct access to at least some memory. I don't believe you have come to the correct conclusion about "virtual memory," however. malloc is implementation-specific; it doesn't require virtual memory to work. Typically it would make a request to the OS for memory. In short, I think you've over-simplified here, though the nice thing about malloc is that you don't have to worry about the implementation details anyway. – Robert Harvey Sep 30 '18 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.