Backports commit 1d854963ea340855efe3f8a5b99c95a75bd717ae from qemu
Copied from tag v4.2, 64291f7db5bd8150a74ad2036f1037e6a0428df2. Backports commit 68a03b8c8853c66724c6f200af3f821ae0d7e934 from qemu