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 #include <linux/kvm_types.h>
6 
7 enum kvm_page_track_mode {
8 	KVM_PAGE_TRACK_WRITE,
9 	KVM_PAGE_TRACK_MAX,
10 };
11 
12 /*
13  * The notifier represented by @kvm_page_track_notifier_node is linked into
14  * the head which will be notified when guest is triggering the track event.
15  *
16  * Write access on the head is protected by kvm->mmu_lock, read access
17  * is protected by track_srcu.
18  */
19 struct kvm_page_track_notifier_head {
20 	struct srcu_struct track_srcu;
21 	struct hlist_head track_notifier_list;
22 };
23 
24 struct kvm_page_track_notifier_node {
25 	struct hlist_node node;
26 
27 	/*
28 	 * It is called when guest is writing the write-tracked page
29 	 * and write emulation is finished at that time.
30 	 *
31 	 * @gpa: the physical address written by guest.
32 	 * @new: the data was written to the address.
33 	 * @bytes: the written length.
34 	 * @node: this node
35 	 */
36 	void (*track_write)(gpa_t gpa, const u8 *new, int bytes,
37 			    struct kvm_page_track_notifier_node *node);
38 
39 	/*
40 	 * Invoked when a memory region is removed from the guest.  Or in KVM
41 	 * terms, when a memslot is deleted.
42 	 *
43 	 * @gfn:       base gfn of the region being removed
44 	 * @nr_pages:  number of pages in the to-be-removed region
45 	 * @node:      this node
46 	 */
47 	void (*track_remove_region)(gfn_t gfn, unsigned long nr_pages,
48 				    struct kvm_page_track_notifier_node *node);
49 };
50 
51 void kvm_slot_page_track_add_page(struct kvm *kvm,
52 				  struct kvm_memory_slot *slot, gfn_t gfn,
53 				  enum kvm_page_track_mode mode);
54 void kvm_slot_page_track_remove_page(struct kvm *kvm,
55 				     struct kvm_memory_slot *slot, gfn_t gfn,
56 				     enum kvm_page_track_mode mode);
57 
58 void
59 kvm_page_track_register_notifier(struct kvm *kvm,
60 				 struct kvm_page_track_notifier_node *n);
61 void
62 kvm_page_track_unregister_notifier(struct kvm *kvm,
63 				   struct kvm_page_track_notifier_node *n);
64 
65 #endif
66