Re: Using virtio-gpu instead of virtwl

Thomas Leonard
2022-05-18T08:55:44Z

[-- Attachment #1: Type: text/plain, Size: 3576 bytes --]

On Mon, 16 May 2022 at 11:55, Alyssa Ross <hi@alyssa.is> wrote:
>
> Thanks for the update!
>
> On Sun, May 15, 2022 at 03:20:24PM +0000, Thomas Leonard wrote:
> > On Thu, 14 Apr 2022 at 13:57, Alyssa Ross <hi@alyssa.is> wrote:
> > >
> > > On Wed, Apr 13, 2022 at 05:12:13PM +0000, Thomas Leonard wrote:
> > > > On Wed, 6 Apr 2022 at 12:19, Thomas Leonard <talex5@gmail.com> wrote:
> > > > [ converting from virtwl to virtio-gpu ]
> > > > > I tried, but failed, to figure out the protocol. I did manage to get a
> > > > > test application showing a little animation, but it crashes after a
> > > > > few seconds.
> > > >
> > > > OK, I found a solution to this: you can just open the device file
> > > > twice and use one instance for Wayland messages and the other for
> > > > allocating images. This avoids the first race. With that, I got the
> > > > proxy converted:
> > > >
> > > > https://github.com/talex5/wayland-proxy-virtwl/pull/28
> > > >
> > > > Though I'm not sure it's an improvement: +1,819 −577 lines!
> > > >
> > > > Instructions for configuring crosvm to use it:
> > > >
> > > > https://github.com/talex5/wayland-proxy-virtwl#virtio-gpu-support
> > > >
> > > > And I wrote up my guesses about the protocol here:
> > > >
> > > > https://github.com/talex5/wayland-proxy-virtwl/blob/master/virtio-spec.md
> > >
> > > That's extremely helpful, thanks for writing it up!
> >
> > A small update on this:
> >
> > First, I got virtio-gpu working with the jail. It just needs a couple
> > of extra paths for NixOS:
> >
> > diff --git a/src/linux.rs b/src/linux.rs
> > index ad031749..52d3142f 100644
> > --- a/src/linux.rs
> > +++ b/src/linux.rs
> > @@ -790,6 +790,8 @@ fn gpu_jail(cfg: &Config, policy: &str) ->
> > Result<Option<Minijail>> {
> > jail_mount_bind_if_exists(
> > &mut jail,
> > &[
> > + "/run/opengl-driver",
> > + "/nix/store",
> > "/usr/lib",
> > "/usr/lib64",
> > "/lib",
> >
> > Secondly, I realised that the "video" memory returned by virtio-gpu
> > was just regular host memory (from stracing crosvm). This is because
> > crosvm is compiled without minigbm support and falls back to this.
> >
> > After enabling minigbm and its amdgpu support (which also required
> > adding a dependency on mesa) it started allocating vram. However, this
> > broke the proxy because vram can't be used with the Wl_shm protocol.
>
> Do you have a Nix expression somewhere for crosvm with all this stuff
> fixed? I'd like to integrate it into my draft Nixpkgs PR. (If you
> didn't do it with Nix, I can make the changes myself.)

No, sorry. I've just been hacking on it in nix-shell. The only change
I made to nixpkgs was adding mesa as a dependency. The changes to get
minigbm to compile are just a hack for my system. But I've attached
the patches just for reference. However, since it stops the proxy from
working, these patches currently aren't an improvement from my point
of view!

BTW, I did get a little further: using __builtin_ia32_clflush fixed
the cache-coherency problem and the scrolling pattern now looks
correct. However, it still crashes the VM if you resize the window a
bit. I modified crosvm to clear the vram before handing it to the
guest (it wasn't doing that before), and that doesn't crash, so the
memory is mapped correctly on the host side.

--
talex5 (GitHub/Twitter)
http://roscidus.com/blog/

[-- Attachment #2: mesa.patch --]
[-- Type: application/octet-stream, Size: 577 bytes --]

index 26e89e0cf20..06baf0bb352 100644
--- a/pkgs/applications/virtualization/crosvm/default.nix
+++ b/pkgs/applications/virtualization/crosvm/default.nix
@@ -1,7 +1,7 @@
{ stdenv, lib, rust, rustPlatform, fetchgit
, meson, ninja, pkg-config, python3, wayland-scanner
, libcap, libdrm, libepoxy, libglvnd, minijail, wayland, wayland-protocols, xorg
-, linux
+, linux, mesa
}:

let
@@ -40,7 +40,7 @@ in

buildInputs = [
libcap libdrm libepoxy libglvnd minijail wayland wayland-protocols
- xorg.libX11
+ xorg.libX11 mesa
];

postPatch = ''

[-- Attachment #3: minigbm.patch --]
[-- Type: application/octet-stream, Size: 1180 bytes --]

diff --git a/Makefile b/Makefile
index 987708f..9442911 100644
--- a/Makefile
+++ b/Makefile
@@ -10,10 +10,10 @@ PC_LIBS := $(shell $(PKG_CONFIG) --libs $(PC_DEPS))

CPPFLAGS += -D_GNU_SOURCE=1
CFLAGS += -std=c99 -Wall -Wsign-compare -Wpointer-arith -Wcast-qual \
- -Wcast-align -D_GNU_SOURCE=1 -D_FILE_OFFSET_BITS=64
+ -Wcast-align -D_GNU_SOURCE=1 -D_FILE_OFFSET_BITS=64 -DDRV_AMDGPU -DDRI_DRIVER_DIR=/run/opengl-driver/lib/dri

ifdef DRV_AMDGPU
- CFLAGS += $(shell $(PKG_CONFIG) --cflags libdrm_amdgpu)
+ CFLAGS += $(shell $(PKG_CONFIG) --cflags libdrm_amdgpu osmesa)
LDLIBS += -ldrm_amdgpu -ldl
endif
ifdef DRV_I915
diff --git a/dri.c b/dri.c
index 8b55c32..ac8794b 100644
--- a/dri.c
+++ b/dri.c
@@ -452,7 +452,7 @@ size_t dri_num_planes_from_modifier(struct driver *drv, uint32_t format, uint64_
}

uint64_t planes;
- GLboolean ret = dri->image_extension->queryDmaBufFormatModifierAttribs(
+ int ret = dri->image_extension->queryDmaBufFormatModifierAttribs(
dri->device, format, modifier, __DRI_IMAGE_FORMAT_MODIFIER_ATTRIB_PLANE_COUNT, &planes);
if (!ret)
return 0;

[-- Attachment #4: crosvm.patch --]
[-- Type: application/octet-stream, Size: 1003 bytes --]

diff --git a/Cargo.toml b/Cargo.toml
index e98c7daa..8c1dfca0 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -134,7 +134,7 @@ disk = { path = "disk" }
enumn = "0.1.0"
gdbstub = { version = "0.5.0", optional = true }
gdbstub_arch = { version = "0.1.0", optional = true }
-rutabaga_gfx = { path = "rutabaga_gfx"}
+rutabaga_gfx = { path = "rutabaga_gfx", features = ["minigbm"]}
hypervisor = { path = "hypervisor" }
kernel_cmdline = { path = "kernel_cmdline" }
kernel_loader = { path = "kernel_loader" }
diff --git a/rutabaga_gfx/build.rs b/rutabaga_gfx/build.rs
index eb109a29..366d8953 100644
--- a/rutabaga_gfx/build.rs
+++ b/rutabaga_gfx/build.rs
@@ -72,6 +72,7 @@ fn build_minigbm(out_dir: &Path) -> Result<()> {

let make_flags = env::var("CARGO_MAKEFLAGS").unwrap();
let status = Command::new("make")
+ .env("DRV_AMDGPU", "1")
.env("MAKEFLAGS", make_flags)
.env("CROSS_COMPILE", get_cross_compile_prefix())
.arg(format!("OUT={}", out_dir.display()))