xref: /linux/arch/x86/include/asm/kvm_page_track.h (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
1 #ifndef _ASM_X86_KVM_PAGE_TRACK_H
2 #define _ASM_X86_KVM_PAGE_TRACK_H
3 
4 enum kvm_page_track_mode {
5 	KVM_PAGE_TRACK_WRITE,
6 	KVM_PAGE_TRACK_MAX,
7 };
8 
9 /*
10  * The notifier represented by @kvm_page_track_notifier_node is linked into
11  * the head which will be notified when guest is triggering the track event.
12  *
13  * Write access on the head is protected by kvm->mmu_lock, read access
14  * is protected by track_srcu.
15  */
16 struct kvm_page_track_notifier_head {
17 	struct srcu_struct track_srcu;
18 	struct hlist_head track_notifier_list;
19 };
20 
21 struct kvm_page_track_notifier_node {
22 	struct hlist_node node;
23 
24 	/*
25 	 * It is called when guest is writing the write-tracked page
26 	 * and write emulation is finished at that time.
27 	 *
28 	 * @vcpu: the vcpu where the write access happened.
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)(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
35 			    int bytes, 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 void kvm_page_track_init(struct kvm *kvm);
49 void kvm_page_track_cleanup(struct kvm *kvm);
50 
51 void kvm_page_track_free_memslot(struct kvm_memory_slot *free,
52 				 struct kvm_memory_slot *dont);
53 int kvm_page_track_create_memslot(struct kvm_memory_slot *slot,
54 				  unsigned long npages);
55 
56 void kvm_slot_page_track_add_page(struct kvm *kvm,
57 				  struct kvm_memory_slot *slot, gfn_t gfn,
58 				  enum kvm_page_track_mode mode);
59 void kvm_slot_page_track_remove_page(struct kvm *kvm,
60 				     struct kvm_memory_slot *slot, gfn_t gfn,
61 				     enum kvm_page_track_mode mode);
62 bool kvm_page_track_is_active(struct kvm_vcpu *vcpu, gfn_t gfn,
63 			      enum kvm_page_track_mode mode);
64 
65 void
66 kvm_page_track_register_notifier(struct kvm *kvm,
67 				 struct kvm_page_track_notifier_node *n);
68 void
69 kvm_page_track_unregister_notifier(struct kvm *kvm,
70 				   struct kvm_page_track_notifier_node *n);
71 void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
72 			  int bytes);
73 void kvm_page_track_flush_slot(struct kvm *kvm, struct kvm_memory_slot *slot);
74 #endif
75