5

I was wondering how a system call is treated when used in a virtual machine. Does it send some kind of signal through the virtualizing kernel and then to the "real" kernel (of the physical machine)?

I googled the subject but I can't seem to find anything. Thank you in advance.

Kara
  • 6,115
  • 16
  • 50
  • 57
n00b1990
  • 1,189
  • 5
  • 17
  • 25
  • There's a nice description in the [VirtualBox documentation](http://www.virtualbox.org/manual/ch10.html#hwvirt) of the various ways it handles this sort of thing depending on the capabilities of the underlying hardware. – Ian Roberts Jan 19 '13 at 14:03

2 Answers2

8

There are 3 common strategies to handle this:
1. Hypervisor traps system calls from guest: The hypervisor checks whether the privileged instruction(effectively system call) came from the guest OS itself, or from a user-space program within the guest OS. If it's the former case, then the hypervisor will actually forward the call to the hardware, although through the virtualization instructions. If it's the latter, the hypervisor will redirect the call to the guest OS, and then proceed.
2. Binary translation: Here the hypervisor checks the code from the guest OS in what are called as "basic blocks", scanning for privileged instructions. Wherever it finds them, it replaces them with calls to it's own procedures to system calls. It then proceeds to cache these blocks & eventually builds a whole set of such blocks.
3. Paravirtualization: Here the guest OS itself is modified so that instead of making calls to the hardware, it has APIs to invoke the hypervisor to get its hardware I/O done.

Source: Modern Operating Systems by Andrew Tanenbaum

Stark07
  • 468
  • 8
  • 17
0

In a virtual machine, where all hardware is virtualized, it works just like it would on real hardware. It goes through whatever syscall instructions the architecture has, which are read by the hypervisor, which executes the instructions on it's virtual processor.

However, in a emulation layer like Wine, syscalls in the PE executables are mapped to syscalls on the Linux kernel APIs.

Linuxios
  • 34,849
  • 13
  • 91
  • 116
  • Thank you for the help, I understand now. Still have a bit of reading to do :P – n00b1990 Jan 20 '13 at 17:05
  • 2
    Virtual processor? Correct me if i'm wrong but a processor is not virtualized. This happens only on an emulator. The syscalls of a guest os are forwarded through the hypervisor to the real hardware, ie. the actual processor. That is no virtual processor is involved, it's just the guest os sees a "virtual processor". – KeyC0de Jan 01 '18 at 12:38
  • @Nik-Lz That's absolutely true; I'll update the answer when I have a moment, or you're welcome to if you'd like to – Linuxios Jan 01 '18 at 18:03
  • 3
    This answer could be improved by describing the life of a system call from start to finish, say writing some data to a network socket, starting from when the syscall is made in the application running in the host OS and finishing with the call returning to the application. – Jonathan Ellithorpe Jul 29 '19 at 18:11