Add a new KVM exit type to allow userspace to handle memory faults that KVM cannot resolve, but that userspace *may* be able to handle (without terminating the guest). KVM will initially use KVM_EXIT_MEMORY_FAULT to report implicit conversions between private and shared memory. With guest private memory, there will be two kind of memory conversions: - explicit conversion: happens when the guest explicitly calls into KVM to map a range (as private or shared) - implicit conversion: happens when the guest attempts to access a gfn that is configured in the "wrong" state (private vs. shared) On x86 (first architecture to support guest private memory), explicit conversions will be reported via KVM_EXIT_HYPERCALL+KVM_HC_MAP_GPA_RANGE, but reporting KVM_EXIT_HYPERCALL for implicit conversions is undesriable as there is (obviously) no hypercall, and there is no guarantee that the guest actually intends to convert between private and shared, i.e. what KVM thinks is an implicit conversion "request" could actually be the result of a guest code bug. KVM_EXIT_MEMORY_FAULT will be used to report memory faults that appear to be implicit conversions. Note! To allow for future possibilities where KVM reports KVM_EXIT_MEMORY_FAULT and fills run->memory_fault on _any_ unresolved fault, KVM returns "-EFAULT" (-1 with errno == EFAULT from userspace's perspective), not '0'! Due to historical baggage within KVM, exiting to userspace with '0' from deep callstacks, e.g. in emulation paths, is infeasible as doing so would require a near-complete overhaul of KVM, whereas KVM already propagates -errno return codes to userspace even when the -errno originated in a low level helper. Report the gpa+size instead of a single gfn even though the initial usage is expected to always report single pages. It's entirely possible, likely even, that KVM will someday support sub-page granularity faults, e.g. Intel's sub-page protection feature allows for additional protections at 128-byte granularity.
Showing the changes to include/uapi (1 of 4 files). Show all
Unified · Split
| @@ -275,6 +275,7 @@ struct kvm_xen_exit { · 274 unmodified lines | |||
| 275 | #define KVM_EXIT_RISCV_CSR 36 | 275 | #define KVM_EXIT_RISCV_CSR 36 |
| 276 | #define KVM_EXIT_NOTIFY 37 | 276 | #define KVM_EXIT_NOTIFY 37 |
| 277 | #define KVM_EXIT_LOONGARCH_IOCSR 38 | 277 | #define KVM_EXIT_LOONGARCH_IOCSR 38 |
| 278 | #define KVM_EXIT_MEMORY_FAULT 39 | ||
| 278 | 279 | ||
| 279 | /* For KVM_EXIT_INTERNAL_ERROR */ | 280 | /* For KVM_EXIT_INTERNAL_ERROR */ |
| 280 | /* Emulate instruction failed. */ | 281 | /* Emulate instruction failed. */ |
| @@ -528,6 +529,12 @@ struct kvm_run { · 247 unmodified lines | |||
| 528 | #define KVM_NOTIFY_CONTEXT_INVALID (1 << 0) | 529 | #define KVM_NOTIFY_CONTEXT_INVALID (1 << 0) |
| 529 | __u32 flags; | 530 | __u32 flags; |
| 530 | } notify; | 531 | } notify; |
| 532 | /* KVM_EXIT_MEMORY_FAULT */ | ||
| 533 | struct { | ||
| 534 | __u64 flags; | ||
| 535 | __u64 gpa; | ||
| 536 | __u64 size; | ||
| 537 | } memory_fault; | ||
| 531 | /* Fix the size of the union. */ | 538 | /* Fix the size of the union. */ |
| 532 | char padding[256]; | 539 | char padding[256]; |
| 533 | }; | 540 | }; |
| @@ -1212,6 +1219,7 @@ struct kvm_ppc_resize_hpt { · 678 unmodified lines | |||
| 1212 | #define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229 | 1219 | #define KVM_CAP_ARM_SUPPORTED_BLOCK_SIZES 229 |
| 1213 | #define KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES 230 | 1220 | #define KVM_CAP_ARM_SUPPORTED_REG_MASK_RANGES 230 |
| 1214 | #define KVM_CAP_USER_MEMORY2 231 | 1221 | #define KVM_CAP_USER_MEMORY2 231 |
| 1222 | #define KVM_CAP_MEMORY_FAULT_INFO 232 | ||
| 1215 | 1223 | ||
| 1216 | #ifdef KVM_CAP_IRQ_ROUTING | 1224 | #ifdef KVM_CAP_IRQ_ROUTING |
| 1217 | 1225 | ||