<div dir="ltr">Hi Frank,<div>thanks for your precise answer.</div><div>At this point, there is only two things I don't get:</div><div><br></div><div>1) callback functions for E1000 (e.g. e1kRegWriteTDT()), are executed</div><div>in R0 context (or at least this is what Log says), but I can find the relative</div><div>symbols only in VBoxDD.so (which is related to R3), and not in VMMR0.r0.</div><div>How is that possible?</div><div><br></div><div>2) Since I'm writing some code for a new networking driver (for my University),</div><div>how can I tell the compiler "compile and run this code in R0, compile and run that code in R3"?</div><div><br></div><div>I hope I was clear enough.</div><div><br></div><div>Kind regards,</div><div>Luca<br><div class="gmail_extra"><br><div class="gmail_quote">2016-02-02 10:18 GMT+01:00 Frank Mehnert <span dir="ltr"><<a href="mailto:frank.mehnert@oracle.com" target="_blank">frank.mehnert@oracle.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Luca,<br>
<br>
device emulation code in VirtualBox can run within three contexts:<br>
<br>
* R3 (part of VBoxDD): Normal userland code executed in the VM<br>
  process context. This code is executed each time we leave the<br>
  guest and go back to userland. This code is not that performance-<br>
  critical, e.g. device initialization, memory allocation etc.<br>
<br>
* R0 (part of VMMR0): Code which is executed in kernel context.<br>
  This happens if the VM runs in VT-x/AMD-V mode and we left the<br>
  VM and entered the root mode where the VirtualBox VMM runs<br>
  (next to the host OS kernel). For performance reasons we don't<br>
  switch to userland (R3). The amount of R0 code is much smaller<br>
  than the amount of R3 code. Such code can also call host OS<br>
  kernel functions directly (e.g. submit a network IP packet to<br>
  the host OS network layer). Calling the host OS code from VMMR0<br>
  is usually done using SUPR0* functions which are implemented in<br>
  src/VBox/HostDrivers/Support and runtime functions which are<br>
  implemented in src/VBox/Runtime/r0drv<br>
<br>
* RC/GC (part of VMMRC.rc): This code is executed if the VM runs<br>
  in non VT-x/AMD-V mode (legacy). Only 32-bit code. This code is<br>
  part of the hypervisor which runs in R0 in the context of the<br>
  guest process. The guest itself runs at R1 (guest userland as<br>
  R3 as usual). Google should explain you x86 ring compression.<br>
<br>
Of course R3 code cannot directly call R0 code. The code in our<br>
device driver has sections which are unique to two or all three<br>
contexts. That means that this code is compiled three times and<br>
exists in all three contexts. Other code is exclusively used in<br>
one or two contexts.<br>
<br>
Kind regards,<br>
<br>
Frank<br>
<div><div class="h5"><br>
On Tuesday 02 February 2016 10:00:55 Luca Carotenuto wrote:<br>
> Gregory, thanks for the reply;<br>
> anyway, my question was about R3 context of the host.<br>
> Since the host must emulate the E1000 behaviour, it doesn't need to access<br>
> the I/O space on the host. So, why does it need to be in R0 context?<br>
> Also, as far as I know, the file VBoxDD.so is a library for R3 context, and<br>
> it has the references<br>
> for functions running in R0 context (e.g. e1kRegWriteTDT).<br>
> How can it be possible?<br>
><br>
> Kind Regards,<br>
> Luca Carotenuto<br>
><br>
> 2016-02-01 16:11 GMT+01:00 Gregory Woodbury <<a href="mailto:redwolfe@gmail.com">redwolfe@gmail.com</a>>:<br>
> > Depending on the CPU architecture, Ring 3 processes cannot access the<br>
> > I/O space without causing a General Protection Exception.<br>
> ><br>
> > Callback routines from IO requests are to let the application be told<br>
> > that an operation is completed.<br>
> > And thus have to be in the application's memory space, but they are<br>
> > actually called from the<br>
> > kernel in R0 space. There are special provisions in the x86 type<br>
> > architecture for this, and<br>
> > the callback routine has only a limited amount of freedom to access IO<br>
> > space.<br>
> ><br>
> > In a fully emulated environment, this might not be necessary, but<br>
> > using the hardware virtualization of KVM/QEMU, to access<br>
> > the IO space still requires Ring0 privleges.  I is a sort of<br>
> > mind-bending set of restrictions and interactioins until one<br>
> > gets used to thinking like the system developers did.<br>
> ><br>
> ><br>
> > On Mon, Feb 1, 2016 at 6:59 AM, Luca Carotenuto<br>
> ><br>
> > <<a href="mailto:luca.carotenuto.91@gmail.com">luca.carotenuto.91@gmail.com</a>> wrote:<br>
> > > 1) As I understand, when we talk about Ring-0 context, we refer to<br>
> > > kernel<br>
> > > space, while Ring-3 context refers to user space. Am I right?<br>
> > ><br>
> > > 2) I'm looking inside the E1000 emulation code<br>
> > > (/src/VBox/Devices/Network/DevE1000.cpp).<br>
> > > Assuming that the above is right, for what concerns the registers<br>
> ><br>
> > callbacks,<br>
> ><br>
> > >  I do not understand why those callbacks are compiled to be executed in<br>
> > ><br>
> > > Ring-0 context<br>
> > > (When I use logging inside those functions, the thread name i R0).<br>
> > > Infact, looking inside the VboxDD.so file in the "out/bin" directory<br>
> ><br>
> > (using<br>
> ><br>
> > > objdump tool), it seems that this code "belongs" to Ring-3 context,<br>
> > > so why is it running in Ring-0 context?<br>
> ><br>
> > --<br>
> > G.Wolfe Woodbury<br>
> > <a href="mailto:redwolfe@gmail.com">redwolfe@gmail.com</a><br>
<br>
--<br>
</div></div>Dr.-Ing. Frank Mehnert | Software Development Director, VirtualBox<br>
ORACLE Deutschland B.V. & Co. KG | Werkstr. 24 | 71384 Weinstadt, Germany<br>
<br>
ORACLE Deutschland B.V. & Co. KG<br>
Hauptverwaltung: Riesstraße 25, D-80992 München<br>
Registergericht: Amtsgericht München, HRA 95603<br>
<br>
Komplementärin: ORACLE Deutschland Verwaltung B.V.<br>
Hertogswetering 163/167, 3543 AS Utrecht, Niederlande<br>
Handelsregister der Handelskammer Midden-Niederlande, Nr. 30143697<br>
Geschäftsführer: Alexander van der Ven, Jan Schultheiss, Val Maher<br>
<div class="HOEnZb"><div class="h5"><br>
_______________________________________________<br>
vbox-dev mailing list<br>
<a href="mailto:vbox-dev@virtualbox.org">vbox-dev@virtualbox.org</a><br>
<a href="https://www.virtualbox.org/mailman/listinfo/vbox-dev" rel="noreferrer" target="_blank">https://www.virtualbox.org/mailman/listinfo/vbox-dev</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Luca Carotenuto</div></div>
</div></div></div>