3

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?

Sumit Trehan
  • 3,985
  • 3
  • 27
  • 42

2 Answers2

5

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.

Jason
  • 3,777
  • 14
  • 27
  • Exactly. With synhronous calls, no copying is required. With asynchronous calls, copying can be avoided by passing buffer pointers/lengths that are allocated with an extended lifetime by the user code and are later returned in the callbacks for consumption and release. – Martin James Sep 30 '14 at 21:59
  • @Jason What would happen if while executing the system call, the context switch happens. The new process issues a system call which would then overwrite the the registers pointed by you "di, rsi, rdx [, r10, r8]". How this is handled? – Sumit Trehan Oct 02 '14 at 04:38
  • 1
    @SumitTrehan That's why register state can be saved in a context switch. Register state can be restored before the process is re-scheduled on the CPU. This excludes registers that return values from system calls though (`rax`). – Jason Oct 02 '14 at 15:54
0

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.

anbu
  • 1
  • 2