0

If we have a 32bit CPU, it can have 4GB of virtual address space. the first 3GB ( 0- 3GB) is for user space virtual addresses and rest 1GB ( 3GB - 4GB) is for kernel virtual addresses. But as I can read in many articles , and even in LDD book , it's said that the kernel virtual address are mapped directly with the physical memory with a fixed offset. i.e. 0xc0000001 kernel virtual address is mapped to 0x1 RAM. physical address. And also , the physical memory mapped for the kernel can not be swapped out.

My question is , how the user space access the physical RAM, if all the RAM is given to the Kernel.

Thanks in advance for your answers!

  • "if all the RAM is given to the Kernel." - As you said, the kernel uses only 1GB of memory. It is definitely not "all the RAM". – Tsyvarev Jul 31 '18 at 20:06
  • *"all the RAM is given to the Kernel"* so that the kernel can *manage* it. When the process is created, its code loaded, and it requests memory buffers, the kernel will allocate physical memory to that process using page table mappings. When the process terminates, the physical memory reverts back to the kernel. – sawdust Aug 01 '18 at 01:35
  • @Tsyvarev Thanks. But what will happen in the small memory systems? Lets say we have 512 MB of RAM. In that case , whole 512 MB of memory will be mapped with the kernel logical address right? And if that memory is non swappable, how it can be used by the userspace? I think I am missing to understand a concept. Can you please clarify on this? – Puneet Gupta Aug 01 '18 at 11:41
  • @sawdust Thanks for your answer. So, you mean to say that kernel allocates RAM memory for a userspace process to run .That's why whole RAM memory should be accessible by the kernel. So lets say our RAM is 1G and only 1 userspace process is running in the system. Assume , userspace process has taken the memory of 8KB ( 0 - 0x1FFF ), so as long as the process is there in the system , this range of memory will not be available and if kernel process tries to get the memory using kmalloc it can only get from the region: 0x00001FFF - 0x3FFFFFFF) ? Is my understanding correct? – Puneet Gupta Aug 01 '18 at 11:59
  • I guess you simply cannot run Linux kernel in "standard" configuration on PC with RAM 512M. – Tsyvarev Aug 01 '18 at 12:11

1 Answers1

2

How user virtual address access the physical memory , if all the memory is mapped directly with kernel virtual address

This question makes no sense because its premise is incorrect.
All physical memory is not mapped to the kernel (unless the physical RAM is truly that small and the kernel image that big).
The kernel only maps the physical memory pages actually needed to hold its code and data.
Unused memory pages go to the free memory pool.

My question is , how the user space access the physical RAM, if all the RAM is given to the Kernel.

What is true is that "all the RAM is given to the Kernel" so that the kernel can manage it.
Whenever a process is created, its code loaded, and it requests memory buffers, the kernel will "allocate" physical memory to that process using page table mappings.
When the process terminates (or its pages get swapped out), the physical memory "reverts" back to the kernel.

Userspace should only be aware of virtual memory. It can only access the physical memory that has been mapped to virtual memory in its virtual address space.
The translation from virtual address to physical address and accessing a RAM location is handled by the CPU and MMU during the execution of an instruction.

So lets say our RAM is 1G and only 1 userspace process is running in the system. Assume , userspace process has taken the memory of 8KB ( 0 - 0x1FFF ), so as long as the process is there in the system , this range of memory will not be available and if kernel process tries to get the memory using kmalloc it can only get from the region: 0x00001FFF - 0x3FFFFFFF) ? Is my understanding correct?

No, your understanding is incorrect.
The memory is allocated in units of pages, and contiguous virtual pages are not guaranteed to be mapped to continuous physical pages.
So you cannot assume anything about which physical memory pages will be mapped for a user process

Nor are virtual to physical memory mappings permanent.
The contents of virtual memory pages can be swapped out when not in use, and that physical memory can then be used by another process or the kernel.
The process does not own any physical memory, and uses a physical memory page only because it has been (temporarily) mapped to a virtual page of that process.

kmalloc() allocates a block of (kernel) virtual memory (that will be backed up with physical memory). There is no kernel function for allocating a block of physical memory.

Note that only virtual address space is divided into user space and kernel space. There is no such division for physical memory.

sawdust
  • 16,103
  • 3
  • 40
  • 50
  • Thanks for the explanation. One more small question. Is this statement true: "The virtual address space reserved for the kernel ( i.e. 1gb in the 1g/3g split ) is not swappable" . That means we would never get a page fault if we access a valid kernel virtual/logical address from the kernel space. – Puneet Gupta Aug 02 '18 at 06:15
  • That's awkwardly worded. See [Do kernel pages get swapped out?](https://stackoverflow.com/questions/4535379/do-kernel-pages-get-swapped-out). – sawdust Aug 02 '18 at 07:48