1 /*
2  * Copyright (c) 2013-2019, 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_cpu.h"
32 #include "pt_cpuid.h"
33 
34 #include "intel-pt.h"
35 
36 #include <stdlib.h>
37 
38 
39 void pt_cpuid(uint32_t leaf, uint32_t *eax, uint32_t *ebx, uint32_t *ecx,
40 	      uint32_t *edx)
41 {
42 	(void) leaf;
43 	(void) eax;
44 	(void) ebx;
45 	(void) ecx;
46 	(void) edx;
47 }
48 
49 
50 static struct ptunit_result cpu_valid(void)
51 {
52 	struct pt_cpu cpu;
53 	int error;
54 
55 	error = pt_cpu_parse(&cpu, "6/44/2");
56 	ptu_int_eq(error, 0);
57 	ptu_int_eq(cpu.vendor, pcv_intel);
58 	ptu_uint_eq(cpu.family, 6);
59 	ptu_uint_eq(cpu.model, 44);
60 	ptu_uint_eq(cpu.stepping, 2);
61 
62 	error = pt_cpu_parse(&cpu, "0xf/0x2c/0xf");
63 	ptu_int_eq(error, 0);
64 	ptu_int_eq(cpu.vendor, pcv_intel);
65 	ptu_uint_eq(cpu.family, 0xf);
66 	ptu_uint_eq(cpu.model, 0x2c);
67 	ptu_uint_eq(cpu.stepping, 0xf);
68 
69 	error = pt_cpu_parse(&cpu, "022/054/017");
70 	ptu_int_eq(error, 0);
71 	ptu_int_eq(cpu.vendor, pcv_intel);
72 	ptu_uint_eq(cpu.family, 022);
73 	ptu_uint_eq(cpu.model, 054);
74 	ptu_uint_eq(cpu.stepping, 017);
75 
76 	error = pt_cpu_parse(&cpu, "6/44");
77 	ptu_int_eq(error, 0);
78 	ptu_int_eq(cpu.vendor, pcv_intel);
79 	ptu_uint_eq(cpu.family, 6);
80 	ptu_uint_eq(cpu.model, 44);
81 	ptu_uint_eq(cpu.stepping, 0);
82 
83 	return ptu_passed();
84 }
85 
86 static struct ptunit_result cpu_null(void)
87 {
88 	struct pt_cpu cpu;
89 	int error;
90 
91 	error = pt_cpu_parse(&cpu, NULL);
92 	ptu_int_eq(error, -pte_invalid);
93 
94 	error = pt_cpu_parse(NULL, "");
95 	ptu_int_eq(error, -pte_invalid);
96 
97 	error = pt_cpu_parse(NULL, NULL);
98 	ptu_int_eq(error, -pte_invalid);
99 
100 	return ptu_passed();
101 }
102 
103 static struct ptunit_result cpu_incomplete(void)
104 {
105 	struct pt_cpu cpu;
106 	int error;
107 
108 	error = pt_cpu_parse(&cpu, "");
109 	ptu_int_eq(error, -pte_invalid);
110 
111 	error = pt_cpu_parse(&cpu, "6");
112 	ptu_int_eq(error, -pte_invalid);
113 
114 	error = pt_cpu_parse(&cpu, "6/");
115 	ptu_int_eq(error, -pte_invalid);
116 
117 	error = pt_cpu_parse(&cpu, "6//2");
118 	ptu_int_eq(error, -pte_invalid);
119 
120 	error = pt_cpu_parse(&cpu, "//");
121 	ptu_int_eq(error, -pte_invalid);
122 
123 	return ptu_passed();
124 }
125 
126 static struct ptunit_result cpu_invalid(void)
127 {
128 	struct pt_cpu cpu;
129 	int error;
130 
131 	error = pt_cpu_parse(&cpu, "e/44/2");
132 	ptu_int_eq(error, -pte_invalid);
133 
134 	error = pt_cpu_parse(&cpu, "6/e/2");
135 	ptu_int_eq(error, -pte_invalid);
136 
137 	error = pt_cpu_parse(&cpu, "6/44/e");
138 	ptu_int_eq(error, -pte_invalid);
139 
140 	error = pt_cpu_parse(&cpu, "65536/44/2");
141 	ptu_int_eq(error, -pte_invalid);
142 
143 	error = pt_cpu_parse(&cpu, "6/256/2");
144 	ptu_int_eq(error, -pte_invalid);
145 
146 	error = pt_cpu_parse(&cpu, "6/44/256");
147 	ptu_int_eq(error, -pte_invalid);
148 
149 	error = pt_cpu_parse(&cpu, "-1/44/2");
150 	ptu_int_eq(error, -pte_invalid);
151 
152 	error = pt_cpu_parse(&cpu, "6/-1/2");
153 	ptu_int_eq(error, -pte_invalid);
154 
155 	error = pt_cpu_parse(&cpu, "6/44/-1");
156 	ptu_int_eq(error, -pte_invalid);
157 
158 	return ptu_passed();
159 }
160 
161 int main(int argc, char **argv)
162 {
163 	struct ptunit_suite suite;
164 
165 	suite = ptunit_mk_suite(argc, argv);
166 
167 	ptu_run(suite, cpu_valid);
168 	ptu_run(suite, cpu_null);
169 	ptu_run(suite, cpu_incomplete);
170 	ptu_run(suite, cpu_invalid);
171 
172 	return ptunit_report(&suite);
173 }
174