Page 1 of 1

Virtio: zero sized buffers are not allowed,cdrom kernel diff

Posted: Sun Jan 31, 2021 9:00 pm
by fbkr
Hi all,

I'm trying to add virtio drivers (just block device at the moment) to my OS, however, I'm running into this issue. Once I set everything up and write 0xF (ACK | DRIVER | FEATURES_OK | DRIVERS_OK) to the status register, I get "qemu-system-x86_64: virtio: zero sized buffers are not allowed" in the terminal. If I don't set the virtual queue, I don't get this error, so I got it pinpointed there.

For the virtqueue setup, the device needs a 256 element queue, and I have the descriptors at 0x7000, available queue at 0x8000 (0x7000 + 256 * 16) and used queue at 0x9000 (aligned to 4096) and I write 0x7 to queue 0's address. All the memory here is zero initialized. I'm using the legacy interface since BAR0 for this device is an IO port.

Could someone assist me with this? Resources on virtio is much thinner than I expected. I checked out the examples at dumais.io and the managarm source, however I didn't notice anything.

Update: when I run the same kernel with `-kernel`, everything works. Running it with `-cdrom` causes this problem on linux and macos, but works on Windows.

Re: Virtio error: "zero sized buffers are not allowed"

Posted: Sun Jan 31, 2021 10:08 pm
by fbkr
I just tested this on qemu(v5.1.0-11824-g8699890d91-dirty) on Windows and it actually worked without a problem.

However, both with qemu v4.2.1 and v5.1.0 on linux have this problem.

Re: Virtio error: "zero sized buffers are not allowed"

Posted: Sun Jan 31, 2021 10:53 pm
by Octocontrabass
fbkr wrote:I have the descriptors at 0x7000
Virtual or physical? How do you set up those descriptors? Did you set the buffer length to a nonzero value? (That's what QEMU is checking when it throws the error you're seeing.)

Re: Virtio error: "zero sized buffers are not allowed"

Posted: Sun Jan 31, 2021 11:16 pm
by fbkr
Octocontrabass wrote:
fbkr wrote:I have the descriptors at 0x7000
Virtual or physical?


It's physical.
Octocontrabass wrote:How do you set up those descriptors? Did you set the buffer length to a nonzero value? (That's what QEMU is checking when it throws the error you're seeing.)
This is happening at initialization, the descriptors are empty, and the whole buffer is zero filled.

Re: Virtio error: "zero sized buffers are not allowed"

Posted: Sun Jan 31, 2021 11:31 pm
by Octocontrabass

Re: Virtio error: "zero sized buffers are not allowed"

Posted: Sun Jan 31, 2021 11:52 pm
by fbkr
I think I do finish setting up the descriptors. Neither the general initialization nor the block device initialization mention putting anything into the queues during initialization. So I'm just trying to initialize an empty queue. Am I missing something obvious?

Re: Virtio error: "zero sized buffers are not allowed"

Posted: Mon Feb 01, 2021 12:21 am
by fbkr
One thing I've noticed is that this works correctly even under linux if I boot with `-kernel ...`, but if I boot with `-cdrom ...`, it fails. But the same ELF/ISO works on windows either way.

Qemu 5.2.0 has the same behaviour on macos as well, works with -kernel, fails with -cdrom

The ISO is being generated with `grub-mkrescue` and the elf is a multiboot1 image.

Re: Virtio: zero sized buffers are not allowed,cdrom kernel

Posted: Mon Feb 01, 2021 12:53 pm
by fbkr
I seem to have figured this out.

Even though the spec clearly says a device must initialize the status register with 0 (https://docs.oasis-open.org/virtio/virt ... #x1-120002), setting it to 0 before doing any initialization fixed the problem.