Reputation: 19
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!
Upvotes: 0
Views: 1497
Reputation: 17067
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.
Upvotes: 2