Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Copying to kernel space during system call

Suppose we have a system call write, which takes in a buffer as an argument. This buffer memory is a part of the user address space.

How does the write call succeed further?

Suppose that if I assume that the entire buffer is copied to the kernel space and the now the process is preempted and some other process is given the CPU and the new process now issues a different system call, which might overwrite the buffer of the previous write call.

How such a case is handled? Or there is an entirely different mechanism where no copy of data is done from user space to kernel space?

like image 293
Sumit Trehan Avatar asked May 19 '26 19:05

Sumit Trehan


1 Answers

Generally you don't need to copy from userspace into kernel (monolithic kernels). For virtual memory systems, The pages allocated to the process are readable and writeable by the kernel. Going the other way, data does need to be copied though since processes can not access pages allocated to the kernel.

If you take the write system call for linux with x86-64 for example, the process calls write with the file descriptor, address of the buffer, and size. The write method places the system call number into rax (1), arguments into registers (rdi, rsi, rdx [, r10, r8]), and executes the syscall instruction (which enters the kernel). The call is dispatched to the handler which pushes the registers onto the kernel stack, and executes the call number. There's no explicit copying of data within pointers into the kernel's memory.

Microkernels (Mach, L4, etc...) are different though.

  • Anatomy of a System Call (this might help)
like image 119
Jason Avatar answered May 22 '26 17:05

Jason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!