2 * Copyright (c) 2014-2018, Intel Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
38 static struct ptunit_result from_user_null(void)
45 errcode = pt_asid_from_user(NULL, NULL);
46 ptu_int_eq(errcode, -pte_internal);
48 errcode = pt_asid_from_user(NULL, &user);
49 ptu_int_eq(errcode, -pte_internal);
54 static struct ptunit_result from_user_default(void)
59 errcode = pt_asid_from_user(&asid, NULL);
60 ptu_int_eq(errcode, 0);
61 ptu_uint_eq(asid.size, sizeof(asid));
62 ptu_uint_eq(asid.cr3, pt_asid_no_cr3);
63 ptu_uint_eq(asid.vmcs, pt_asid_no_vmcs);
68 static struct ptunit_result from_user_small(void)
70 struct pt_asid asid, user;
73 user.size = sizeof(user.size);
75 errcode = pt_asid_from_user(&asid, &user);
76 ptu_int_eq(errcode, 0);
77 ptu_uint_eq(asid.size, sizeof(asid));
78 ptu_uint_eq(asid.cr3, pt_asid_no_cr3);
79 ptu_uint_eq(asid.vmcs, pt_asid_no_vmcs);
84 static struct ptunit_result from_user_big(void)
86 struct pt_asid asid, user;
89 user.size = sizeof(user) + 4;
91 user.vmcs = 0x23000ull;
93 errcode = pt_asid_from_user(&asid, &user);
94 ptu_int_eq(errcode, 0);
95 ptu_uint_eq(asid.size, sizeof(asid));
96 ptu_uint_eq(asid.cr3, 0x4200ull);
97 ptu_uint_eq(asid.vmcs, 0x23000ull);
102 static struct ptunit_result from_user(void)
104 struct pt_asid asid, user;
107 user.size = sizeof(user);
108 user.cr3 = 0x4200ull;
109 user.vmcs = 0x23000ull;
111 errcode = pt_asid_from_user(&asid, &user);
112 ptu_int_eq(errcode, 0);
113 ptu_uint_eq(asid.size, sizeof(asid));
114 ptu_uint_eq(asid.cr3, 0x4200ull);
115 ptu_uint_eq(asid.vmcs, 0x23000ull);
120 static struct ptunit_result from_user_cr3(void)
122 struct pt_asid asid, user;
125 user.size = offsetof(struct pt_asid, vmcs);
126 user.cr3 = 0x4200ull;
127 user.vmcs = 0x23000ull;
129 errcode = pt_asid_from_user(&asid, &user);
130 ptu_int_eq(errcode, 0);
131 ptu_uint_eq(asid.size, sizeof(asid));
132 ptu_uint_eq(asid.cr3, 0x4200ull);
133 ptu_uint_eq(asid.vmcs, pt_asid_no_vmcs);
138 static struct ptunit_result to_user_null(void)
145 errcode = pt_asid_to_user(NULL, NULL, sizeof(asid));
146 ptu_int_eq(errcode, -pte_internal);
148 errcode = pt_asid_to_user(NULL, &asid, sizeof(asid));
149 ptu_int_eq(errcode, -pte_internal);
154 static struct ptunit_result to_user_too_small(void)
156 struct pt_asid asid, user;
161 errcode = pt_asid_to_user(&user, &asid, 0);
162 ptu_int_eq(errcode, -pte_invalid);
164 errcode = pt_asid_to_user(&user, &asid, sizeof(user.size) - 1);
165 ptu_int_eq(errcode, -pte_invalid);
170 static struct ptunit_result to_user_small(void)
172 struct pt_asid asid, user;
175 memset(&user, 0xcc, sizeof(user));
178 errcode = pt_asid_to_user(&user, &asid, sizeof(user.size));
179 ptu_int_eq(errcode, 0);
180 ptu_uint_eq(user.size, sizeof(user.size));
181 ptu_uint_eq(user.cr3, 0xccccccccccccccccull);
182 ptu_uint_eq(user.vmcs, 0xccccccccccccccccull);
187 static struct ptunit_result to_user_big(void)
189 struct pt_asid asid, user;
192 memset(&user, 0xcc, sizeof(user));
194 asid.cr3 = 0x4200ull;
195 asid.vmcs = 0x23000ull;
197 errcode = pt_asid_to_user(&user, &asid, sizeof(user) + 8);
198 ptu_int_eq(errcode, 0);
199 ptu_uint_eq(user.size, sizeof(asid));
200 ptu_uint_eq(user.cr3, 0x4200ull);
201 ptu_uint_eq(user.vmcs, 0x23000ull);
206 static struct ptunit_result to_user(void)
208 struct pt_asid asid, user;
211 memset(&user, 0xcc, sizeof(user));
213 asid.cr3 = 0x4200ull;
214 asid.vmcs = 0x23000ull;
216 errcode = pt_asid_to_user(&user, &asid, sizeof(user));
217 ptu_int_eq(errcode, 0);
218 ptu_uint_eq(user.size, sizeof(asid));
219 ptu_uint_eq(user.cr3, 0x4200ull);
220 ptu_uint_eq(user.vmcs, 0x23000ull);
225 static struct ptunit_result to_user_cr3(void)
227 struct pt_asid asid, user;
230 memset(&user, 0xcc, sizeof(user));
232 asid.cr3 = 0x4200ull;
234 errcode = pt_asid_to_user(&user, &asid, offsetof(struct pt_asid, vmcs));
235 ptu_int_eq(errcode, 0);
236 ptu_uint_eq(user.size, offsetof(struct pt_asid, vmcs));
237 ptu_uint_eq(user.cr3, 0x4200ull);
238 ptu_uint_eq(user.vmcs, 0xccccccccccccccccull);
243 static struct ptunit_result match_null(void)
250 errcode = pt_asid_match(NULL, NULL);
251 ptu_int_eq(errcode, -pte_internal);
253 errcode = pt_asid_match(NULL, &asid);
254 ptu_int_eq(errcode, -pte_internal);
256 errcode = pt_asid_match(&asid, NULL);
257 ptu_int_eq(errcode, -pte_internal);
262 static struct ptunit_result match_default(void)
264 struct pt_asid lhs, rhs;
270 errcode = pt_asid_match(&lhs, &rhs);
271 ptu_int_eq(errcode, 1);
274 lhs.vmcs = 0x42000ull;
276 errcode = pt_asid_match(&lhs, &rhs);
277 ptu_int_eq(errcode, 1);
279 errcode = pt_asid_match(&rhs, &lhs);
280 ptu_int_eq(errcode, 1);
285 static struct ptunit_result match_default_mixed(void)
287 struct pt_asid lhs, rhs;
293 errcode = pt_asid_match(&lhs, &rhs);
294 ptu_int_eq(errcode, 1);
297 rhs.vmcs = 0x42000ull;
299 errcode = pt_asid_match(&lhs, &rhs);
300 ptu_int_eq(errcode, 1);
302 errcode = pt_asid_match(&rhs, &lhs);
303 ptu_int_eq(errcode, 1);
308 static struct ptunit_result match_cr3(void)
310 struct pt_asid lhs, rhs;
319 errcode = pt_asid_match(&lhs, &rhs);
320 ptu_int_eq(errcode, 1);
325 static struct ptunit_result match_vmcs(void)
327 struct pt_asid lhs, rhs;
333 lhs.vmcs = 0x23000ull;
334 rhs.vmcs = 0x23000ull;
336 errcode = pt_asid_match(&lhs, &rhs);
337 ptu_int_eq(errcode, 1);
342 static struct ptunit_result match(void)
344 struct pt_asid lhs, rhs;
352 lhs.vmcs = 0x23000ull;
353 rhs.vmcs = 0x23000ull;
355 errcode = pt_asid_match(&lhs, &rhs);
356 ptu_int_eq(errcode, 1);
361 static struct ptunit_result match_cr3_false(void)
363 struct pt_asid lhs, rhs;
372 errcode = pt_asid_match(&lhs, &rhs);
373 ptu_int_eq(errcode, 0);
378 static struct ptunit_result match_vmcs_false(void)
380 struct pt_asid lhs, rhs;
386 lhs.vmcs = 0x42000ull;
387 rhs.vmcs = 0x23000ull;
389 errcode = pt_asid_match(&lhs, &rhs);
390 ptu_int_eq(errcode, 0);
395 int main(int argc, char **argv)
397 struct ptunit_suite suite;
399 suite = ptunit_mk_suite(argc, argv);
401 ptu_run(suite, from_user_null);
402 ptu_run(suite, from_user_default);
403 ptu_run(suite, from_user_small);
404 ptu_run(suite, from_user_big);
405 ptu_run(suite, from_user);
406 ptu_run(suite, from_user_cr3);
408 ptu_run(suite, to_user_null);
409 ptu_run(suite, to_user_too_small);
410 ptu_run(suite, to_user_small);
411 ptu_run(suite, to_user_big);
412 ptu_run(suite, to_user);
413 ptu_run(suite, to_user_cr3);
415 ptu_run(suite, match_null);
416 ptu_run(suite, match_default);
417 ptu_run(suite, match_default_mixed);
418 ptu_run(suite, match_cr3);
419 ptu_run(suite, match_vmcs);
420 ptu_run(suite, match);
421 ptu_run(suite, match_cr3_false);
422 ptu_run(suite, match_vmcs_false);
424 return ptunit_report(&suite);