KVM: the code in the Linux kernel which provides a friendly interface to userspace for using the CPU virtualization. This includes functions that userspace can call for "create CPU", "run CPU", etc. For a full virtual machine, you need to have some userspace code which can use this. This is usually either QEMU, or the simpler "kvmtool"; some large cloud providers have their own custom userspace code instead.
QEMU: emulates a virtual piece of hardware, including disks, memory, CPUs, serial port, graphics, and other devices. Also provides mechanisms (a UI, and some programmable interfaces) for doing things like starting, stopping, and migrating. QEMU supports several different 'accelerator' modes for how it handles the CPU emulation:
- TCG: pure emulation -- works anywhere, but very slow
- KVM: uses the Linux kernel's KVM APIs to allow running guest code using host CPU hardware virtualization support
- hax: similar to KVM, but using the Intel HAXM code, which will work on Windows hosts
From an implementation point of view the boundary between KVM and QEMU is very clear -- KVM is a part of the host kernel, whereas QEMU is a separate userspace program. For a user, you typically don't have to care where the boundary is, because that's an implementation detail.
To answer your question about what happens for MMIO:
- the guest makes an MMIO access
- this is trapped to the host kernel by the hardware
- the host kernel (KVM) might then emulate this MMIO access itself, because a few devices are implemented in the kernel for performance reasons. This usually applies only to the interrupt controller and perhaps the iommu.
- otherwise, KVM reports the MMIO access back to userspace (ie to QEMU, kvmtool, etc)
- userspace then can handle the access, using its device emulation code
- userspace then returns the result (eg the data to return for a read) to the kernel
- the kernel updates the vcpu's register state as required to complete emulation of the instruction
- the kernel then resumes execution of the VM at the following instruction