<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    Hi Luca,<br>
    On 02.02.2016 10:50, Luca Carotenuto wrote:<br>
    <blockquote
cite="mid:CAB5gLBzQ=_3UiAhJq9xJ8khwY1F2kYUewhjti6eTkhtBC4Z4Ow@mail.gmail.com"
      type="cite">
      <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>
    </blockquote>
    Guess the symbols have been stripped, as they're local and therefore
    not needed for dynamic linking. There are no magic jumps between R0
    and R3, it's all done through the registration of handlers, in this
    particular case when mapping PCI regions (see e1kMap()), which can
    be controlled through the flags fR0Enabled and fRCEnabled which are
    taken from the device config.<br>
    <br>
    If there's a R0 handler for some IO or MMIO region, it is used. R0
    handlers can return e.g. VINF_IOM_R3_MMIO_WRITE, which conveys the
    information that handling this particular request isn't possible in
    R0 and needs to be retried in R3.<br>
    <br>
    So this isn't as fine grained as you probably imagined from looking
    at the code.<br>
    <blockquote
cite="mid:CAB5gLBzQ=_3UiAhJq9xJ8khwY1F2kYUewhjti6eTkhtBC4Z4Ow@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <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>
    </blockquote>
    There's no magic. Everything is handled by the build process. Ever
    wondered why the DevE1000.cpp file shows up so many times in
    Makefile.kmk?<br>
    <br>
    To compile the necessary parts for each purpose there are some
    macros which can be checked. The main one is IN_RING3 which is used
    quite a bit in DevE1000.cpp, and that should hopefully make the
    situation clear.<br>
    <br>
    Klaus<br>
    <blockquote
cite="mid:CAB5gLBzQ=_3UiAhJq9xJ8khwY1F2kYUewhjti6eTkhtBC4Z4Ow@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <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 moz-do-not-send="true"
                  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 moz-do-not-send="true"
                      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 moz-do-not-send="true"
                      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 moz-do-not-send="true"
                      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 moz-do-not-send="true"
                      href="mailto:vbox-dev@virtualbox.org">vbox-dev@virtualbox.org</a><br>
                    <a moz-do-not-send="true"
                      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 class="moz-signature">Luca Carotenuto<!-- This signature was generated by the MyDesktop Oracle Business Signature utility version 5.0 -->
              </div>
            </div>
          </div>
        </div>
      </div>
    </blockquote>
  </body>
</html>