1

I am working on a 64 bits x86 Linux computer.

On this architecture, each process has its own 64bits address memory space.

Memory blocks are mapped to physical memory (or swap) by MMU component.

Now, i am wondering how works Kernel memory. If i write a linux kernel module with this code:

char * address;
address = 0x.....;
*address = 0x42;

Where this 0x42 will be written ? Directly in physical memory ?

Or does linux kernel as a 64 bits memory space, like any user and process ?

Thanks

Bob5421
  • 7,757
  • 14
  • 81
  • 175
  • A code of a linux kernel module is executed by some process. A process accesses the memory according to its address space. This is applied both for accesses in the user space and in the kernel space. It is address space which differentiates user space and kernel space accesses. See more here: https://stackoverflow.com/questions/13013491/why-is-kernel-mapped-to-the-same-address-space-as-processes – Tsyvarev Mar 31 '19 at 19:18

1 Answers1

0

Each process is assigned 64 bit virtual address space. Out of which userspace can map maximum of 128TB, and other half is for kernel. Basically kernel resides in RAM and same is mapped to each process virtual address space. Virtual Memory layout for x86_64 bit

In kernel, all kernel threads share same address space. So if you access a memory (considering mapping is present, no page fault) it will write to that location.

Anand
  • 119
  • 1
  • 11
  • Okay so if i have 2 process, kernel space is exactly the same between the 2 process ? – Bob5421 Apr 05 '19 at 13:15
  • yes.Mapping is same for all the process. Read Robert love linux kernel development, you will get more idea. – Anand Apr 05 '19 at 14:00
  • Thanks. But There is something i do not understand: current_task is a pointer to task_struct. But this value is different on each process. So we cannot say the mapping is the same if one value can differ from a process to another... – Bob5421 Apr 05 '19 at 15:03
  • From https://elixir.bootlin.com/linux/v4.0/source/arch/x86/kernel/cpu/common.c#L1138 current_task is percpu variable which hold current_task executing on that cpu. So it changes on what is executing on the core. – Anand Apr 05 '19 at 15:21
  • Thanks. Do you know a way to find the address (i just need the address of task_struct for current process ?) – Bob5421 Apr 05 '19 at 15:53
  • Have a look at link https://stackoverflow.com/questions/12193281/how-does-the-kernel-know-what-is-the-current-thread – Anand Apr 17 '19 at 04:18