0

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?

3
  • If you are writing an OS you'll use special instructions to control the MMU Sep 30, 2018 at 16:27
  • The line of code you posted is Undefined Behavior. Sep 30, 2018 at 18:13
  • @RobertHarvey Is it UB even if the CPU doesn't have an MMU? We used to write code like that for the 6502 to access memory-mapped IO (of course the pointer was less than 0xFFFF so that it fit into memory). Sep 14, 2023 at 1:11

3 Answers 3

1

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.

1

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.

1
  • 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. Sep 30, 2018 at 18:19
0

Yes

In Linux distros, the kernel is responsible for managing the memory resources and ensuring efficient allocation and deallocation of memory for various processes running on the system.

In Linux, the kernel uses a technique called virtual memory to provide each process with its own virtual address space. This virtual address space is then mapped to the physical memory by the kernel. The kernel maintains a page table that keeps track of the mapping between virtual addresses and physical addresses.

When a process needs to access a particular memory location, it uses the virtual address. The kernel translates this virtual address to the corresponding physical address and performs the necessary read or write operation on the physical memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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