1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 221ebbedaSXiao Guangrong #ifndef _ASM_X86_KVM_PAGE_TRACK_H 321ebbedaSXiao Guangrong #define _ASM_X86_KVM_PAGE_TRACK_H 421ebbedaSXiao Guangrong 521ebbedaSXiao Guangrong enum kvm_page_track_mode { 621ebbedaSXiao Guangrong KVM_PAGE_TRACK_WRITE, 721ebbedaSXiao Guangrong KVM_PAGE_TRACK_MAX, 821ebbedaSXiao Guangrong }; 921ebbedaSXiao Guangrong 100eb05bf2SXiao Guangrong /* 110eb05bf2SXiao Guangrong * The notifier represented by @kvm_page_track_notifier_node is linked into 120eb05bf2SXiao Guangrong * the head which will be notified when guest is triggering the track event. 130eb05bf2SXiao Guangrong * 140eb05bf2SXiao Guangrong * Write access on the head is protected by kvm->mmu_lock, read access 150eb05bf2SXiao Guangrong * is protected by track_srcu. 160eb05bf2SXiao Guangrong */ 170eb05bf2SXiao Guangrong struct kvm_page_track_notifier_head { 180eb05bf2SXiao Guangrong struct srcu_struct track_srcu; 190eb05bf2SXiao Guangrong struct hlist_head track_notifier_list; 200eb05bf2SXiao Guangrong }; 210eb05bf2SXiao Guangrong 220eb05bf2SXiao Guangrong struct kvm_page_track_notifier_node { 230eb05bf2SXiao Guangrong struct hlist_node node; 240eb05bf2SXiao Guangrong 250eb05bf2SXiao Guangrong /* 260eb05bf2SXiao Guangrong * It is called when guest is writing the write-tracked page 270eb05bf2SXiao Guangrong * and write emulation is finished at that time. 280eb05bf2SXiao Guangrong * 290eb05bf2SXiao Guangrong * @gpa: the physical address written by guest. 300eb05bf2SXiao Guangrong * @new: the data was written to the address. 310eb05bf2SXiao Guangrong * @bytes: the written length. 32d126363dSJike Song * @node: this node 330eb05bf2SXiao Guangrong */ 34b271e17dSSean Christopherson void (*track_write)(gpa_t gpa, const u8 *new, int bytes, 35b271e17dSSean Christopherson struct kvm_page_track_notifier_node *node); 36ae7cd873SXiaoguang Chen /* 37ae7cd873SXiaoguang Chen * It is called when memory slot is being moved or removed 38ae7cd873SXiaoguang Chen * users can drop write-protection for the pages in that memory slot 39ae7cd873SXiaoguang Chen * 40ae7cd873SXiaoguang Chen * @kvm: the kvm where memory slot being moved or removed 41ae7cd873SXiaoguang Chen * @slot: the memory slot being moved or removed 42d126363dSJike Song * @node: this node 43ae7cd873SXiaoguang Chen */ 44d126363dSJike Song void (*track_flush_slot)(struct kvm *kvm, struct kvm_memory_slot *slot, 45d126363dSJike Song struct kvm_page_track_notifier_node *node); 46*b83ab124SYan Zhao 47*b83ab124SYan Zhao /* 48*b83ab124SYan Zhao * Invoked when a memory region is removed from the guest. Or in KVM 49*b83ab124SYan Zhao * terms, when a memslot is deleted. 50*b83ab124SYan Zhao * 51*b83ab124SYan Zhao * @gfn: base gfn of the region being removed 52*b83ab124SYan Zhao * @nr_pages: number of pages in the to-be-removed region 53*b83ab124SYan Zhao * @node: this node 54*b83ab124SYan Zhao */ 55*b83ab124SYan Zhao void (*track_remove_region)(gfn_t gfn, unsigned long nr_pages, 56*b83ab124SYan Zhao struct kvm_page_track_notifier_node *node); 570eb05bf2SXiao Guangrong }; 580eb05bf2SXiao Guangrong 59eb7511bfSHaimin Zhang int kvm_page_track_init(struct kvm *kvm); 602beb6dadSPaolo Bonzini void kvm_page_track_cleanup(struct kvm *kvm); 610eb05bf2SXiao Guangrong 621e76a3ceSDavid Stevens bool kvm_page_track_write_tracking_enabled(struct kvm *kvm); 631e76a3ceSDavid Stevens int kvm_page_track_write_tracking_alloc(struct kvm_memory_slot *slot); 64deae4a10SDavid Stevens 65e96c81eeSSean Christopherson void kvm_page_track_free_memslot(struct kvm_memory_slot *slot); 66deae4a10SDavid Stevens int kvm_page_track_create_memslot(struct kvm *kvm, 67deae4a10SDavid Stevens struct kvm_memory_slot *slot, 6821ebbedaSXiao Guangrong unsigned long npages); 69f29d4d78SXiao Guangrong 70f29d4d78SXiao Guangrong void kvm_slot_page_track_add_page(struct kvm *kvm, 71f29d4d78SXiao Guangrong struct kvm_memory_slot *slot, gfn_t gfn, 72f29d4d78SXiao Guangrong enum kvm_page_track_mode mode); 73f29d4d78SXiao Guangrong void kvm_slot_page_track_remove_page(struct kvm *kvm, 74f29d4d78SXiao Guangrong struct kvm_memory_slot *slot, gfn_t gfn, 75f29d4d78SXiao Guangrong enum kvm_page_track_mode mode); 769d395a0aSBen Gardon bool kvm_slot_page_track_is_active(struct kvm *kvm, 778283e36aSBen Gardon const struct kvm_memory_slot *slot, 788283e36aSBen Gardon gfn_t gfn, enum kvm_page_track_mode mode); 790eb05bf2SXiao Guangrong 800eb05bf2SXiao Guangrong void 810eb05bf2SXiao Guangrong kvm_page_track_register_notifier(struct kvm *kvm, 820eb05bf2SXiao Guangrong struct kvm_page_track_notifier_node *n); 830eb05bf2SXiao Guangrong void 840eb05bf2SXiao Guangrong kvm_page_track_unregister_notifier(struct kvm *kvm, 850eb05bf2SXiao Guangrong struct kvm_page_track_notifier_node *n); 860eb05bf2SXiao Guangrong void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new, 870eb05bf2SXiao Guangrong int bytes); 88ae7cd873SXiaoguang Chen void kvm_page_track_flush_slot(struct kvm *kvm, struct kvm_memory_slot *slot); 89*b83ab124SYan Zhao void kvm_page_track_delete_slot(struct kvm *kvm, struct kvm_memory_slot *slot); 90c70934e0SSean Christopherson 91c70934e0SSean Christopherson bool kvm_page_track_has_external_user(struct kvm *kvm); 92c70934e0SSean Christopherson 9321ebbedaSXiao Guangrong #endif 94