Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is Kernel space used when Kernel is executing on the behalf of the user program i.e. System Call? Or is it the address space for all the Kernel threads (for example scheduler)?

If it is the first one, than does it mean that normal user program cannot have more than 3GB of memory (if the division is 3GB + 1GB)? Also, in that case how can kernel use High Memory, because to what virtual memory address will the pages from high memory be mapped to, as 1GB of kernel space will be logically mapped?

share|improve this question
up vote 37 down vote accepted

Is Kernel space used when Kernel is executing on the behalf of the user program i.e. System Call? Or is it the address space for all the Kernel threads (for example scheduler)?

Yes and yes.

Before we go any further, we should state this about memory.

Memory get's divided into two distinct areas:

  • The user space, which is a set of locations where normal user processes run (i.e everything other than the kernel). The role of the kernel is to manage applications running in this space from messing with each other, and the machine, and
  • The kernel space, which is the location where the code of the kernel is stored, and executes under.

Processes running under the user space have access only to a limited part of memory, whereas the kernel has access to all of the memory. Processes running in user space also don't have access to the kernel space. User space processes can only access a small part of the kernel via an interface exposed by the kernel - the system calls. If an process performs a system call, a software interrupt is sent to the kernel, which then dispatches the appropriate interrupt handler and continues its work after the handler has finished.

Kernel space code has the property to run in "kernel mode", which (in your typical desktop -x86- computer) is what you call code that executes under ring 0. Typically in x86 architecture, there are 4 rings of protection. Ring 0 (kernel mode), Ring 1 (may be used by virtual machine hypervisors or drivers), Ring 2 (may be used by drivers, I am not so sure about that though) and Ring 3. Ring 3 is what typical applications run under. It is the least privileged ring, and applications running on it have access to a subset of the processor's instructions. Ring 0 (kernel space) is the most privileged ring, and has access to all of the machine's instructions. For example to this, a "plain" application (like a browser) can not use x86 assembly instructions lgdt to load the global descriptor table or hlt to halt a processor.

If it is the first one, than does it mean that normal user program cannot have more than 3GB of memory (if the division is 3GB + 1GB)? Also, in that case how can kernel use High Memory, because to what virtual memory address will the pages from high memory be mapped to, as 1GB of kernel space will be logically mapped?

For an answer to this, please refer to the excellent answer by wag here

share|improve this answer
    
Don't hesitate to tell me if I have made a mistake somewhere. I am new to kernel programming, and I dumped here what I have learned so far, along with some other information I found around the web. Which means that there may be deficiencies in my understanding of concepts that may be demonstrated in the text. – NlightNFotis Aug 21 '13 at 14:08
    
Thanks! I think now I understand it better. Just to make sure that I get it correctly, I have one more question. Again considering that the first 3GB are used for userspace and 128MB of kernel space is used for High memory, is the remaining 896MB (Low Memory) statically mapped at boot-time? – Poojan Aug 21 '13 at 19:19
1  
@NlightNFotis I say that almost 15 people believe that whatever have you said, is correct (or so you makes us think ;)) – Braiam Aug 27 '13 at 22:01

If it is the first one, than does it mean that normal user program cannot have more than 3GB of memory (if the division is 3GB + 1GB)?

Yes this is the case on a normal linux system. There were a set of "4G/4G" patches floating around at one point that made the user and kernel address spaces completely independent (at a performance cost because it made it harder for the kernel to access user memory) but I don't think they were ever merged upstream and interest waned with the rise of x86-64

Also, in that case how can kernel use High Memory, because to what virtual memory address will the pages from high memory be mapped to, as 1GB of kernel space will be logically mapped?

The way linux used to work (and still does on systems where the memory is small compared to the address space) was that the whole of physical memory was permanently mapped into the kernel part of the address space. This allowed the kernel to access all of physical memory with no remapping but clearly it doesn't scale to machines with lots of physical memory.

So the concept of low and high memory was born. "low" memory is permanently mapped into the kernels address space. "high" memory is not.

When the processor is running a system call it is running in kernel mode but still in the context of the current process. So it can directly access both kernel address space and the user address space of the current process (assuming you aren't using the aforementioned 4G/4G patches). This means it is no problem for "high" memory to be allocated to a userland process.

Using "high" memory for kernel purposes is more of a problem. To access high memory that is not mapped to the current process it must be temporally mapped into the kernel's address space. That means extra code and a performance penalty.

share|improve this answer

protected by Community Feb 5 at 12:56

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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