Backports commit d2e6eedf5078d0f2ac17fc1a0d24f6be79c071d7 from qemu
Backports commit bb2e0039dc07177f928f9fe24758967da02d60a2 from qemu