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
44 * @handler: page track handler
45 * @priv: tracker private
48 * zero on success, negative error code if failed.
50 int intel_vgpu_register_page_track(struct intel_vgpu
*vgpu
, unsigned long gfn
,
51 gvt_page_track_handler_t handler
, void *priv
)
53 struct intel_vgpu_page_track
*track
;
56 track
= intel_vgpu_find_page_track(vgpu
, gfn
);
60 track
= kzalloc(sizeof(*track
), GFP_KERNEL
);
64 track
->handler
= handler
;
65 track
->priv_data
= priv
;
67 ret
= radix_tree_insert(&vgpu
->page_track_tree
, gfn
, track
);
77 * intel_vgpu_unregister_page_track - unregister the tracked guest page
79 * @gfn: the gfn of guest page
82 void intel_vgpu_unregister_page_track(struct intel_vgpu
*vgpu
,
85 struct intel_vgpu_page_track
*track
;
87 track
= radix_tree_delete(&vgpu
->page_track_tree
, gfn
);
90 intel_gvt_hypervisor_disable_page_track(vgpu
, gfn
);
96 * intel_vgpu_enable_page_track - set write-protection on guest page
98 * @gfn: the gfn of guest page
101 * zero on success, negative error code if failed.
103 int intel_vgpu_enable_page_track(struct intel_vgpu
*vgpu
, unsigned long gfn
)
105 struct intel_vgpu_page_track
*track
;
108 track
= intel_vgpu_find_page_track(vgpu
, gfn
);
115 ret
= intel_gvt_hypervisor_enable_page_track(vgpu
, gfn
);
118 track
->tracked
= true;
123 * intel_vgpu_enable_page_track - cancel write-protection on guest page
125 * @gfn: the gfn of guest page
128 * zero on success, negative error code if failed.
130 int intel_vgpu_disable_page_track(struct intel_vgpu
*vgpu
, unsigned long gfn
)
132 struct intel_vgpu_page_track
*track
;
135 track
= intel_vgpu_find_page_track(vgpu
, gfn
);
142 ret
= intel_gvt_hypervisor_disable_page_track(vgpu
, gfn
);
145 track
->tracked
= false;
150 * intel_vgpu_page_track_handler - called when write to write-protected page
152 * @gpa: the gpa of this write
153 * @data: the writed data
154 * @bytes: the length of this write
157 * zero on success, negative error code if failed.
159 int intel_vgpu_page_track_handler(struct intel_vgpu
*vgpu
, u64 gpa
,
160 void *data
, unsigned int bytes
)
162 struct intel_vgpu_page_track
*page_track
;
165 mutex_lock(&vgpu
->vgpu_lock
);
167 page_track
= intel_vgpu_find_page_track(vgpu
, gpa
>> PAGE_SHIFT
);
173 if (unlikely(vgpu
->failsafe
)) {
174 /* Remove write protection to prevent furture traps. */
175 intel_vgpu_disable_page_track(vgpu
, gpa
>> PAGE_SHIFT
);
177 ret
= page_track
->handler(page_track
, gpa
, data
, bytes
);
179 gvt_err("guest page write error, gpa %llx\n", gpa
);
183 mutex_unlock(&vgpu
->vgpu_lock
);