1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _ASM_X86_KVM_PAGE_TRACK_H 3 #define _ASM_X86_KVM_PAGE_TRACK_H 4 5 enum kvm_page_track_mode { 6 KVM_PAGE_TRACK_WRITE, 7 KVM_PAGE_TRACK_MAX, 8 }; 9 10 /* 11 * The notifier represented by @kvm_page_track_notifier_node is linked into 12 * the head which will be notified when guest is triggering the track event. 13 * 14 * Write access on the head is protected by kvm->mmu_lock, read access 15 * is protected by track_srcu. 16 */ 17 struct kvm_page_track_notifier_head { 18 struct srcu_struct track_srcu; 19 struct hlist_head track_notifier_list; 20 }; 21 22 struct kvm_page_track_notifier_node { 23 struct hlist_node node; 24 25 /* 26 * It is called when guest is writing the write-tracked page 27 * and write emulation is finished at that time. 28 * 29 * @gpa: the physical address written by guest. 30 * @new: the data was written to the address. 31 * @bytes: the written length. 32 * @node: this node 33 */ 34 void (*track_write)(gpa_t gpa, const u8 *new, int bytes, 35 struct kvm_page_track_notifier_node *node); 36 /* 37 * It is called when memory slot is being moved or removed 38 * users can drop write-protection for the pages in that memory slot 39 * 40 * @kvm: the kvm where memory slot being moved or removed 41 * @slot: the memory slot being moved or removed 42 * @node: this node 43 */ 44 void (*track_flush_slot)(struct kvm *kvm, struct kvm_memory_slot *slot, 45 struct kvm_page_track_notifier_node *node); 46 47 /* 48 * Invoked when a memory region is removed from the guest. Or in KVM 49 * terms, when a memslot is deleted. 50 * 51 * @gfn: base gfn of the region being removed 52 * @nr_pages: number of pages in the to-be-removed region 53 * @node: this node 54 */ 55 void (*track_remove_region)(gfn_t gfn, unsigned long nr_pages, 56 struct kvm_page_track_notifier_node *node); 57 }; 58 59 int kvm_page_track_init(struct kvm *kvm); 60 void kvm_page_track_cleanup(struct kvm *kvm); 61 62 bool kvm_page_track_write_tracking_enabled(struct kvm *kvm); 63 int kvm_page_track_write_tracking_alloc(struct kvm_memory_slot *slot); 64 65 void kvm_page_track_free_memslot(struct kvm_memory_slot *slot); 66 int kvm_page_track_create_memslot(struct kvm *kvm, 67 struct kvm_memory_slot *slot, 68 unsigned long npages); 69 70 void kvm_slot_page_track_add_page(struct kvm *kvm, 71 struct kvm_memory_slot *slot, gfn_t gfn, 72 enum kvm_page_track_mode mode); 73 void kvm_slot_page_track_remove_page(struct kvm *kvm, 74 struct kvm_memory_slot *slot, gfn_t gfn, 75 enum kvm_page_track_mode mode); 76 bool kvm_slot_page_track_is_active(struct kvm *kvm, 77 const struct kvm_memory_slot *slot, 78 gfn_t gfn, enum kvm_page_track_mode mode); 79 80 void 81 kvm_page_track_register_notifier(struct kvm *kvm, 82 struct kvm_page_track_notifier_node *n); 83 void 84 kvm_page_track_unregister_notifier(struct kvm *kvm, 85 struct kvm_page_track_notifier_node *n); 86 void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new, 87 int bytes); 88 void kvm_page_track_flush_slot(struct kvm *kvm, struct kvm_memory_slot *slot); 89 void kvm_page_track_delete_slot(struct kvm *kvm, struct kvm_memory_slot *slot); 90 91 bool kvm_page_track_has_external_user(struct kvm *kvm); 92 93 #endif 94