Sumit Trehan
Sumit Trehan

Reputation: 4035

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?

Upvotes: 3

Views: 2324

Answers (2)

anbu
anbu

Reputation: 1

If the kernel space ( that will have the copied user space data) is common between the two process, we should provide some locking mechanism for the kernel space to protect it from data crash or race condition.

Upvotes: 0

Jason
Jason

Reputation: 3917

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.

Upvotes: 5

Related Questions