2 * Copyright (c) 2017-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.
31 #include "pt_msec_cache.h"
36 int pt_section_get(struct pt_section *section)
43 ucount = section->ucount + 1;
47 section->ucount = ucount;
51 int pt_section_put(struct pt_section *section)
58 ucount = section->ucount;
62 section->ucount = ucount - 1;
66 int pt_section_map(struct pt_section *section)
68 uint16_t ucount, mcount;
73 ucount = section->ucount;
77 mcount = section->mcount + 1;
81 section->mcount = mcount;
85 int pt_section_unmap(struct pt_section *section)
87 uint16_t ucount, mcount;
92 ucount = section->ucount;
96 mcount = section->mcount;
100 section->mcount = mcount - 1;
106 /* The section stored in the image.
108 * This is either the fixture's section or NULL.
110 struct pt_section *section;
113 extern int pt_image_validate(struct pt_image *, struct pt_mapped_section *,
115 extern int pt_image_find(struct pt_image *, struct pt_mapped_section *,
116 const struct pt_asid *, uint64_t);
118 int pt_image_validate(struct pt_image *image, struct pt_mapped_section *msec,
119 uint64_t vaddr, int isid)
121 struct pt_section *section;
127 return -pte_internal;
129 section = image->section;
133 if (section != msec->section)
139 int pt_image_find(struct pt_image *image, struct pt_mapped_section *msec,
140 const struct pt_asid *asid, uint64_t vaddr)
142 struct pt_section *section;
146 if (!image || !msec || !asid)
147 return -pte_internal;
149 section = image->section;
154 return -pte_internal;
156 msec->section = section;
158 return pt_section_get(section);
161 /* A test fixture providing a section and checking the use and map count. */
162 struct test_fixture {
163 /* A test section. */
164 struct pt_section section;
167 struct pt_msec_cache mcache;
170 struct pt_image image;
172 /* The test fixture initialization and finalization functions. */
173 struct ptunit_result (*init)(struct test_fixture *);
174 struct ptunit_result (*fini)(struct test_fixture *);
177 static struct ptunit_result init_null(void)
181 status = pt_msec_cache_init(NULL);
182 ptu_int_eq(status, -pte_internal);
187 static struct ptunit_result fini_null(void)
189 pt_msec_cache_fini(NULL);
194 static struct ptunit_result invalidate_null(void)
198 status = pt_msec_cache_invalidate(NULL);
199 ptu_int_eq(status, -pte_internal);
204 static struct ptunit_result read_null(void)
206 const struct pt_mapped_section *msec;
207 struct pt_msec_cache mcache;
208 struct pt_image image;
211 status = pt_msec_cache_read(NULL, &msec, &image, 0ull);
212 ptu_int_eq(status, -pte_internal);
214 status = pt_msec_cache_read(&mcache, NULL, &image, 0ull);
215 ptu_int_eq(status, -pte_internal);
217 status = pt_msec_cache_read(&mcache, &msec, NULL, 0ull);
218 ptu_int_eq(status, -pte_internal);
223 static struct ptunit_result fill_null(void)
225 const struct pt_mapped_section *msec;
226 struct pt_msec_cache mcache;
227 struct pt_image image;
231 memset(&mcache, 0, sizeof(mcache));
233 status = pt_msec_cache_fill(NULL, &msec, &image, &asid, 0ull);
234 ptu_int_eq(status, -pte_internal);
236 status = pt_msec_cache_fill(&mcache, NULL, &image, &asid, 0ull);
237 ptu_int_eq(status, -pte_internal);
239 status = pt_msec_cache_fill(&mcache, &msec, NULL, &asid, 0ull);
240 ptu_int_eq(status, -pte_internal);
242 status = pt_msec_cache_fill(&mcache, &msec, &image, NULL, 0ull);
243 ptu_int_eq(status, -pte_internal);
248 static struct ptunit_result invalidate(struct test_fixture *tfix)
250 struct pt_section *section;
253 status = pt_msec_cache_invalidate(&tfix->mcache);
254 ptu_int_eq(status, 0);
256 section = pt_msec_section(&tfix->mcache.msec);
259 ptu_uint_eq(tfix->section.mcount, 0);
260 ptu_uint_eq(tfix->section.ucount, 0);
265 static struct ptunit_result read_nomap(struct test_fixture *tfix)
267 const struct pt_mapped_section *msec;
272 status = pt_msec_cache_read(&tfix->mcache, &msec, &tfix->image, 0ull);
273 ptu_int_eq(status, -pte_nomap);
279 static struct ptunit_result read(struct test_fixture *tfix)
281 const struct pt_mapped_section *msec;
282 struct pt_section *section;
285 status = pt_msec_cache_read(&tfix->mcache, &msec, &tfix->image, 0ull);
286 ptu_int_eq(status, 0);
288 ptu_ptr_eq(msec, &tfix->mcache.msec);
290 section = pt_msec_section(msec);
291 ptu_ptr_eq(section, &tfix->section);
296 static struct ptunit_result fill_nomap(struct test_fixture *tfix)
298 const struct pt_mapped_section *msec;
300 struct pt_section *section;
305 status = pt_msec_cache_fill(&tfix->mcache, &msec, &tfix->image, &asid,
307 ptu_int_eq(status, -pte_nomap);
309 section = pt_msec_section(&tfix->mcache.msec);
313 ptu_uint_eq(tfix->section.mcount, 0);
314 ptu_uint_eq(tfix->section.ucount, 0);
319 static struct ptunit_result fill(struct test_fixture *tfix)
321 const struct pt_mapped_section *msec;
322 struct pt_section *section;
326 status = pt_msec_cache_fill(&tfix->mcache, &msec, &tfix->image, &asid,
328 ptu_int_eq(status, 0);
330 ptu_ptr_eq(msec, &tfix->mcache.msec);
332 section = pt_msec_section(msec);
333 ptu_ptr_eq(section, &tfix->section);
335 ptu_uint_eq(section->mcount, 1);
336 ptu_uint_eq(section->ucount, 1);
341 static struct ptunit_result sfix_init(struct test_fixture *tfix)
343 memset(&tfix->section, 0, sizeof(tfix->section));
344 memset(&tfix->mcache, 0, sizeof(tfix->mcache));
345 memset(&tfix->image, 0, sizeof(tfix->image));
350 static struct ptunit_result ifix_init(struct test_fixture *tfix)
352 ptu_test(sfix_init, tfix);
354 tfix->image.section = &tfix->section;
359 static struct ptunit_result cfix_init(struct test_fixture *tfix)
361 ptu_test(sfix_init, tfix);
363 tfix->mcache.msec.section = &tfix->section;
365 tfix->section.ucount = 1;
366 tfix->section.mcount = 1;
371 static struct ptunit_result cifix_init(struct test_fixture *tfix)
373 ptu_test(cfix_init, tfix);
375 tfix->image.section = &tfix->section;
380 int main(int argc, char **argv)
382 struct ptunit_suite suite;
383 struct test_fixture sfix, ifix, cfix, cifix;
385 sfix.init = sfix_init;
388 ifix.init = ifix_init;
391 cfix.init = cfix_init;
394 cifix.init = cifix_init;
397 suite = ptunit_mk_suite(argc, argv);
399 ptu_run(suite, init_null);
400 ptu_run(suite, fini_null);
401 ptu_run(suite, invalidate_null);
402 ptu_run(suite, read_null);
403 ptu_run(suite, fill_null);
405 ptu_run_f(suite, invalidate, sfix);
406 ptu_run_f(suite, invalidate, cfix);
408 ptu_run_f(suite, read_nomap, sfix);
409 ptu_run_f(suite, read_nomap, ifix);
410 ptu_run_f(suite, read_nomap, cfix);
411 ptu_run_f(suite, read, cifix);
413 ptu_run_f(suite, fill_nomap, sfix);
414 ptu_run_f(suite, fill_nomap, cfix);
415 ptu_run_f(suite, fill, ifix);
416 ptu_run_f(suite, fill, cifix);
418 return ptunit_report(&suite);