1/*
2 * Copyright (c) 2014-2018, Intel Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
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.
15 *
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.
27 */
28
29#include "ptunit.h"
30
31#include "pt_mapped_section.h"
32
33#include "intel-pt.h"
34
35
36static struct ptunit_result begin(void)
37{
38	struct pt_mapped_section msec;
39	struct pt_section sec;
40	uint64_t begin;
41
42	pt_msec_init(&msec, &sec, NULL, 0x2000ull, 0x100ull, 0x1000ull);
43
44	begin = pt_msec_begin(&msec);
45	ptu_uint_eq(begin, 0x2000);
46
47	return ptu_passed();
48}
49
50static struct ptunit_result end(void)
51{
52	struct pt_mapped_section msec;
53	struct pt_section sec;
54	uint64_t end;
55
56	pt_msec_init(&msec, &sec, NULL, 0x2000ull, 0x100ull, 0x1000ull);
57
58	end = pt_msec_end(&msec);
59	ptu_uint_eq(end, 0x3000);
60
61	return ptu_passed();
62}
63
64static struct ptunit_result offset(void)
65{
66	struct pt_mapped_section msec;
67	struct pt_section sec;
68	uint64_t offset;
69
70	pt_msec_init(&msec, &sec, NULL, 0x2000ull, 0x100ull, 0x1000ull);
71
72	offset = pt_msec_offset(&msec);
73	ptu_uint_eq(offset, 0x100ull);
74
75	return ptu_passed();
76}
77
78static struct ptunit_result size(void)
79{
80	struct pt_mapped_section msec;
81	struct pt_section sec;
82	uint64_t size;
83
84	pt_msec_init(&msec, &sec, NULL, 0x2000ull, 0x100ull, 0x1000ull);
85
86	size = pt_msec_size(&msec);
87	ptu_uint_eq(size, 0x1000ull);
88
89	return ptu_passed();
90}
91
92static struct ptunit_result asid(void)
93{
94	struct pt_mapped_section msec;
95	struct pt_asid asid;
96	const struct pt_asid *pasid;
97
98	pt_asid_init(&asid);
99	asid.cr3 = 0xa00000ull;
100	asid.vmcs = 0xb00000ull;
101
102	pt_msec_init(&msec, NULL, &asid, 0x2000ull, 0x100ull, 0x1000ull);
103
104	pasid = pt_msec_asid(&msec);
105	ptu_ptr(pasid);
106	ptu_uint_eq(pasid->cr3, asid.cr3);
107	ptu_uint_eq(pasid->vmcs, asid.vmcs);
108
109	return ptu_passed();
110}
111
112static struct ptunit_result asid_null(void)
113{
114	struct pt_mapped_section msec;
115	const struct pt_asid *pasid;
116
117	pt_msec_init(&msec, NULL, NULL, 0x2000ull, 0x100ull, 0x1000ull);
118
119	pasid = pt_msec_asid(&msec);
120	ptu_ptr(pasid);
121	ptu_uint_eq(pasid->cr3, pt_asid_no_cr3);
122	ptu_uint_eq(pasid->vmcs, pt_asid_no_vmcs);
123
124	return ptu_passed();
125}
126
127static struct ptunit_result map(void)
128{
129	struct pt_mapped_section msec;
130	uint64_t mapped;
131
132	pt_msec_init(&msec, NULL, NULL, 0x2000ull, 0x100ull, 0x1000ull);
133
134	mapped = pt_msec_map(&msec, 0x900);
135	ptu_uint_eq(mapped, 0x2800);
136
137	return ptu_passed();
138}
139
140static struct ptunit_result unmap(void)
141{
142	struct pt_mapped_section msec;
143	uint64_t offset;
144
145	pt_msec_init(&msec, NULL, NULL, 0x2000ull, 0x100ull, 0x1000ull);
146
147	offset = pt_msec_unmap(&msec, 0x3000);
148	ptu_uint_eq(offset, 0x1100);
149
150	return ptu_passed();
151}
152
153static struct ptunit_result section(void)
154{
155	static struct pt_section section;
156	struct pt_mapped_section msec;
157	struct pt_section *psection;
158
159	pt_msec_init(&msec, &section, NULL, 0x2000ull, 0x100ull, 0x1000ull);
160
161	psection = pt_msec_section(&msec);
162	ptu_ptr_eq(psection, &section);
163
164	return ptu_passed();
165}
166
167static struct ptunit_result section_null(void)
168{
169	struct pt_mapped_section msec;
170	struct pt_section *psection;
171
172	pt_msec_init(&msec, NULL, NULL, 0x2000ull, 0x100ull, 0x1000ull);
173
174	psection = pt_msec_section(&msec);
175	ptu_ptr_eq(psection, NULL);
176
177	return ptu_passed();
178}
179
180int main(int argc, char **argv)
181{
182	struct ptunit_suite suite;
183
184	suite = ptunit_mk_suite(argc, argv);
185
186	ptu_run(suite, begin);
187	ptu_run(suite, end);
188	ptu_run(suite, offset);
189	ptu_run(suite, size);
190	ptu_run(suite, asid);
191	ptu_run(suite, asid_null);
192	ptu_run(suite, map);
193	ptu_run(suite, unmap);
194	ptu_run(suite, section);
195	ptu_run(suite, section_null);
196
197	return ptunit_report(&suite);
198}
199