2 * Copyright(c) 2011-2017 Intel Corporation. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * intel_vgpu_find_page_track - find page track rcord of guest page
29 * @gfn: the gfn of guest page
32 * A pointer to struct intel_vgpu_page_track if found, else NULL returned.
34 struct intel_vgpu_page_track *intel_vgpu_find_page_track(
35 struct intel_vgpu *vgpu, unsigned long gfn)
37 return radix_tree_lookup(&vgpu->page_track_tree, gfn);
41 * intel_vgpu_register_page_track - register a guest page to be tacked
43 * @gfn: the gfn of guest page
46 * zero on success, negative error code if failed.
48 int intel_vgpu_register_page_track(struct intel_vgpu *vgpu, unsigned long gfn,
49 gvt_page_track_handler_t handler, void *priv)
51 struct intel_vgpu_page_track *track;
54 track = intel_vgpu_find_page_track(vgpu, gfn);
58 track = kzalloc(sizeof(*track), GFP_KERNEL);
62 track->handler = handler;
63 track->priv_data = priv;
65 ret = radix_tree_insert(&vgpu->page_track_tree, gfn, track);
75 * intel_vgpu_unregister_page_track - unregister the tracked guest page
77 * @gfn: the gfn of guest page
80 void intel_vgpu_unregister_page_track(struct intel_vgpu *vgpu,
83 struct intel_vgpu_page_track *track;
85 track = radix_tree_delete(&vgpu->page_track_tree, gfn);
88 intel_gvt_hypervisor_disable_page_track(vgpu, gfn);
94 * intel_vgpu_enable_page_track - set write-protection on guest page
96 * @gfn: the gfn of guest page
99 * zero on success, negative error code if failed.
101 int intel_vgpu_enable_page_track(struct intel_vgpu *vgpu, unsigned long gfn)
103 struct intel_vgpu_page_track *track;
106 track = intel_vgpu_find_page_track(vgpu, gfn);
113 ret = intel_gvt_hypervisor_enable_page_track(vgpu, gfn);
116 track->tracked = true;
121 * intel_vgpu_enable_page_track - cancel write-protection on guest page
123 * @gfn: the gfn of guest page
126 * zero on success, negative error code if failed.
128 int intel_vgpu_disable_page_track(struct intel_vgpu *vgpu, unsigned long gfn)
130 struct intel_vgpu_page_track *track;
133 track = intel_vgpu_find_page_track(vgpu, gfn);
140 ret = intel_gvt_hypervisor_disable_page_track(vgpu, gfn);
143 track->tracked = false;
148 * intel_vgpu_page_track_handler - called when write to write-protected page
150 * @gpa: the gpa of this write
151 * @data: the writed data
152 * @bytes: the length of this write
155 * zero on success, negative error code if failed.
157 int intel_vgpu_page_track_handler(struct intel_vgpu *vgpu, u64 gpa,
158 void *data, unsigned int bytes)
160 struct intel_gvt *gvt = vgpu->gvt;
161 struct intel_vgpu_page_track *page_track;
164 mutex_lock(&gvt->lock);
166 page_track = intel_vgpu_find_page_track(vgpu, gpa >> PAGE_SHIFT);
172 if (unlikely(vgpu->failsafe)) {
173 /* Remove write protection to prevent furture traps. */
174 intel_vgpu_disable_page_track(vgpu, gpa >> PAGE_SHIFT);
176 ret = page_track->handler(page_track, gpa, data, bytes);
178 gvt_err("guest page write error, gpa %llx\n", gpa);
182 mutex_unlock(&gvt->lock);