Backports commit 79525dfd08262d8de10d271f17e5a4096ef96d16 from qemu
Backports commit 14e4c1e2355473ccb2939afc69ac8f25de103b92 from qemu