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.
31 #include "pt_decoder_function.h"
32 #include "pt_packet_decoder.h"
33 #include "pt_query_decoder.h"
34 #include "pt_encoder.h"
35 #include "pt_opcodes.h"
40 /* A test fixture for decoder function fetch tests. */
41 struct fetch_fixture {
42 /* The trace buffer. */
45 /* A trace configuration. */
46 struct pt_config config;
48 /* A trace encoder. */
49 struct pt_encoder encoder;
51 /* The test fixture initialization and finalization functions. */
52 struct ptunit_result (*init)(struct fetch_fixture *);
53 struct ptunit_result (*fini)(struct fetch_fixture *);
56 static struct ptunit_result ffix_init(struct fetch_fixture *ffix)
58 memset(ffix->buffer, pt_opc_bad, sizeof(ffix->buffer));
60 memset(&ffix->config, 0, sizeof(ffix->config));
61 ffix->config.size = sizeof(ffix->config);
62 ffix->config.begin = ffix->buffer;
63 ffix->config.end = ffix->buffer + sizeof(ffix->buffer);
65 pt_encoder_init(&ffix->encoder, &ffix->config);
70 static struct ptunit_result ffix_fini(struct fetch_fixture *ffix)
72 pt_encoder_fini(&ffix->encoder);
78 static struct ptunit_result fetch_null(struct fetch_fixture *ffix)
80 const struct pt_decoder_function *dfun;
83 errcode = pt_df_fetch(NULL, ffix->config.begin, &ffix->config);
84 ptu_int_eq(errcode, -pte_internal);
86 errcode = pt_df_fetch(&dfun, NULL, &ffix->config);
87 ptu_int_eq(errcode, -pte_nosync);
89 errcode = pt_df_fetch(&dfun, ffix->config.begin, NULL);
90 ptu_int_eq(errcode, -pte_internal);
95 static struct ptunit_result fetch_empty(struct fetch_fixture *ffix)
97 const struct pt_decoder_function *dfun;
100 errcode = pt_df_fetch(&dfun, ffix->config.end, &ffix->config);
101 ptu_int_eq(errcode, -pte_eos);
106 static struct ptunit_result fetch_unknown(struct fetch_fixture *ffix)
108 const struct pt_decoder_function *dfun;
111 ffix->config.begin[0] = pt_opc_bad;
113 errcode = pt_df_fetch(&dfun, ffix->config.begin, &ffix->config);
114 ptu_int_eq(errcode, 0);
115 ptu_ptr_eq(dfun, &pt_decode_unknown);
120 static struct ptunit_result fetch_unknown_ext(struct fetch_fixture *ffix)
122 const struct pt_decoder_function *dfun;
125 ffix->config.begin[0] = pt_opc_ext;
126 ffix->config.begin[1] = pt_ext_bad;
128 errcode = pt_df_fetch(&dfun, ffix->config.begin, &ffix->config);
129 ptu_int_eq(errcode, 0);
130 ptu_ptr_eq(dfun, &pt_decode_unknown);
135 static struct ptunit_result fetch_unknown_ext2(struct fetch_fixture *ffix)
137 const struct pt_decoder_function *dfun;
140 ffix->config.begin[0] = pt_opc_ext;
141 ffix->config.begin[1] = pt_ext_ext2;
142 ffix->config.begin[2] = pt_ext2_bad;
144 errcode = pt_df_fetch(&dfun, ffix->config.begin, &ffix->config);
145 ptu_int_eq(errcode, 0);
146 ptu_ptr_eq(dfun, &pt_decode_unknown);
151 static struct ptunit_result fetch_packet(struct fetch_fixture *ffix,
152 const struct pt_packet *packet,
153 const struct pt_decoder_function *df)
155 const struct pt_decoder_function *dfun;
158 errcode = pt_enc_next(&ffix->encoder, packet);
159 ptu_int_ge(errcode, 0);
161 errcode = pt_df_fetch(&dfun, ffix->config.begin, &ffix->config);
162 ptu_int_eq(errcode, 0);
163 ptu_ptr_eq(dfun, df);
168 static struct ptunit_result fetch_type(struct fetch_fixture *ffix,
169 enum pt_packet_type type,
170 const struct pt_decoder_function *dfun)
172 struct pt_packet packet;
174 memset(&packet, 0, sizeof(packet));
177 ptu_test(fetch_packet, ffix, &packet, dfun);
182 static struct ptunit_result fetch_tnt_8(struct fetch_fixture *ffix)
184 struct pt_packet packet;
186 memset(&packet, 0, sizeof(packet));
187 packet.type = ppt_tnt_8;
188 packet.payload.tnt.bit_size = 1;
190 ptu_test(fetch_packet, ffix, &packet, &pt_decode_tnt_8);
195 static struct ptunit_result fetch_mode_exec(struct fetch_fixture *ffix)
197 struct pt_packet packet;
199 memset(&packet, 0, sizeof(packet));
200 packet.type = ppt_mode;
201 packet.payload.mode.leaf = pt_mol_exec;
203 ptu_test(fetch_packet, ffix, &packet, &pt_decode_mode);
208 static struct ptunit_result fetch_mode_tsx(struct fetch_fixture *ffix)
210 struct pt_packet packet;
212 memset(&packet, 0, sizeof(packet));
213 packet.type = ppt_mode;
214 packet.payload.mode.leaf = pt_mol_tsx;
216 ptu_test(fetch_packet, ffix, &packet, &pt_decode_mode);
221 static struct ptunit_result fetch_exstop_ip(struct fetch_fixture *ffix)
223 struct pt_packet packet;
225 memset(&packet, 0, sizeof(packet));
226 packet.type = ppt_exstop;
227 packet.payload.exstop.ip = 1;
229 ptu_test(fetch_packet, ffix, &packet, &pt_decode_exstop);
234 int main(int argc, char **argv)
236 struct fetch_fixture ffix;
237 struct ptunit_suite suite;
239 ffix.init = ffix_init;
240 ffix.fini = ffix_fini;
242 suite = ptunit_mk_suite(argc, argv);
244 ptu_run_f(suite, fetch_null, ffix);
245 ptu_run_f(suite, fetch_empty, ffix);
247 ptu_run_f(suite, fetch_unknown, ffix);
248 ptu_run_f(suite, fetch_unknown_ext, ffix);
249 ptu_run_f(suite, fetch_unknown_ext2, ffix);
251 ptu_run_fp(suite, fetch_type, ffix, ppt_pad, &pt_decode_pad);
252 ptu_run_fp(suite, fetch_type, ffix, ppt_psb, &pt_decode_psb);
253 ptu_run_fp(suite, fetch_type, ffix, ppt_tip, &pt_decode_tip);
254 ptu_run_fp(suite, fetch_type, ffix, ppt_tnt_64, &pt_decode_tnt_64);
255 ptu_run_fp(suite, fetch_type, ffix, ppt_tip_pge, &pt_decode_tip_pge);
256 ptu_run_fp(suite, fetch_type, ffix, ppt_tip_pgd, &pt_decode_tip_pgd);
257 ptu_run_fp(suite, fetch_type, ffix, ppt_fup, &pt_decode_fup);
258 ptu_run_fp(suite, fetch_type, ffix, ppt_pip, &pt_decode_pip);
259 ptu_run_fp(suite, fetch_type, ffix, ppt_ovf, &pt_decode_ovf);
260 ptu_run_fp(suite, fetch_type, ffix, ppt_psbend, &pt_decode_psbend);
261 ptu_run_fp(suite, fetch_type, ffix, ppt_tsc, &pt_decode_tsc);
262 ptu_run_fp(suite, fetch_type, ffix, ppt_cbr, &pt_decode_cbr);
263 ptu_run_fp(suite, fetch_type, ffix, ppt_tma, &pt_decode_tma);
264 ptu_run_fp(suite, fetch_type, ffix, ppt_mtc, &pt_decode_mtc);
265 ptu_run_fp(suite, fetch_type, ffix, ppt_cyc, &pt_decode_cyc);
266 ptu_run_fp(suite, fetch_type, ffix, ppt_stop, &pt_decode_stop);
267 ptu_run_fp(suite, fetch_type, ffix, ppt_vmcs, &pt_decode_vmcs);
268 ptu_run_fp(suite, fetch_type, ffix, ppt_mnt, &pt_decode_mnt);
269 ptu_run_fp(suite, fetch_type, ffix, ppt_exstop, &pt_decode_exstop);
270 ptu_run_fp(suite, fetch_type, ffix, ppt_mwait, &pt_decode_mwait);
271 ptu_run_fp(suite, fetch_type, ffix, ppt_pwre, &pt_decode_pwre);
272 ptu_run_fp(suite, fetch_type, ffix, ppt_pwrx, &pt_decode_pwrx);
273 ptu_run_fp(suite, fetch_type, ffix, ppt_ptw, &pt_decode_ptw);
275 ptu_run_f(suite, fetch_tnt_8, ffix);
276 ptu_run_f(suite, fetch_mode_exec, ffix);
277 ptu_run_f(suite, fetch_mode_tsx, ffix);
278 ptu_run_f(suite, fetch_exstop_ip, ffix);
280 return ptunit_report(&suite);
284 /* Dummy decode functions to satisfy link dependencies.
286 * As a nice side-effect, we will know if we need to add more tests when
287 * adding new decoder functions.
289 int pt_pkt_decode_unknown(struct pt_packet_decoder *d, struct pt_packet *p)
294 return -pte_internal;
296 int pt_qry_decode_unknown(struct pt_query_decoder *d)
300 return -pte_internal;
303 int pt_pkt_decode_pad(struct pt_packet_decoder *d, struct pt_packet *p)
308 return -pte_internal;
310 int pt_qry_decode_pad(struct pt_query_decoder *d)
314 return -pte_internal;
317 int pt_pkt_decode_psb(struct pt_packet_decoder *d, struct pt_packet *p)
322 return -pte_internal;
324 int pt_qry_decode_psb(struct pt_query_decoder *d)
328 return -pte_internal;
331 int pt_pkt_decode_tip(struct pt_packet_decoder *d, struct pt_packet *p)
336 return -pte_internal;
338 int pt_qry_decode_tip(struct pt_query_decoder *d)
342 return -pte_internal;
345 int pt_pkt_decode_tnt_8(struct pt_packet_decoder *d, struct pt_packet *p)
350 return -pte_internal;
352 int pt_qry_decode_tnt_8(struct pt_query_decoder *d)
356 return -pte_internal;
359 int pt_pkt_decode_tnt_64(struct pt_packet_decoder *d, struct pt_packet *p)
364 return -pte_internal;
366 int pt_qry_decode_tnt_64(struct pt_query_decoder *d)
370 return -pte_internal;
373 int pt_pkt_decode_tip_pge(struct pt_packet_decoder *d, struct pt_packet *p)
378 return -pte_internal;
380 int pt_qry_decode_tip_pge(struct pt_query_decoder *d)
384 return -pte_internal;
387 int pt_pkt_decode_tip_pgd(struct pt_packet_decoder *d, struct pt_packet *p)
392 return -pte_internal;
394 int pt_qry_decode_tip_pgd(struct pt_query_decoder *d)
398 return -pte_internal;
401 int pt_pkt_decode_fup(struct pt_packet_decoder *d, struct pt_packet *p)
406 return -pte_internal;
408 int pt_qry_decode_fup(struct pt_query_decoder *d)
412 return -pte_internal;
414 int pt_qry_header_fup(struct pt_query_decoder *d)
418 return -pte_internal;
421 int pt_pkt_decode_pip(struct pt_packet_decoder *d, struct pt_packet *p)
426 return -pte_internal;
428 int pt_qry_decode_pip(struct pt_query_decoder *d)
432 return -pte_internal;
434 int pt_qry_header_pip(struct pt_query_decoder *d)
438 return -pte_internal;
441 int pt_pkt_decode_ovf(struct pt_packet_decoder *d, struct pt_packet *p)
446 return -pte_internal;
448 int pt_qry_decode_ovf(struct pt_query_decoder *d)
452 return -pte_internal;
455 int pt_pkt_decode_mode(struct pt_packet_decoder *d, struct pt_packet *p)
460 return -pte_internal;
462 int pt_qry_decode_mode(struct pt_query_decoder *d)
466 return -pte_internal;
468 int pt_qry_header_mode(struct pt_query_decoder *d)
472 return -pte_internal;
475 int pt_pkt_decode_psbend(struct pt_packet_decoder *d, struct pt_packet *p)
480 return -pte_internal;
482 int pt_qry_decode_psbend(struct pt_query_decoder *d)
486 return -pte_internal;
489 int pt_pkt_decode_tsc(struct pt_packet_decoder *d, struct pt_packet *p)
494 return -pte_internal;
496 int pt_qry_decode_tsc(struct pt_query_decoder *d)
500 return -pte_internal;
502 int pt_qry_header_tsc(struct pt_query_decoder *d)
506 return -pte_internal;
509 int pt_pkt_decode_cbr(struct pt_packet_decoder *d, struct pt_packet *p)
514 return -pte_internal;
516 int pt_qry_decode_cbr(struct pt_query_decoder *d)
520 return -pte_internal;
522 int pt_qry_header_cbr(struct pt_query_decoder *d)
526 return -pte_internal;
529 int pt_pkt_decode_tma(struct pt_packet_decoder *d, struct pt_packet *p)
534 return -pte_internal;
536 int pt_qry_decode_tma(struct pt_query_decoder *d)
540 return -pte_internal;
543 int pt_pkt_decode_mtc(struct pt_packet_decoder *d, struct pt_packet *p)
548 return -pte_internal;
550 int pt_qry_decode_mtc(struct pt_query_decoder *d)
554 return -pte_internal;
557 int pt_pkt_decode_cyc(struct pt_packet_decoder *d, struct pt_packet *p)
562 return -pte_internal;
564 int pt_qry_decode_cyc(struct pt_query_decoder *d)
568 return -pte_internal;
571 int pt_pkt_decode_stop(struct pt_packet_decoder *d, struct pt_packet *p)
576 return -pte_internal;
578 int pt_qry_decode_stop(struct pt_query_decoder *d)
582 return -pte_internal;
585 int pt_pkt_decode_vmcs(struct pt_packet_decoder *d, struct pt_packet *p)
590 return -pte_internal;
592 int pt_qry_decode_vmcs(struct pt_query_decoder *d)
596 return -pte_internal;
598 int pt_qry_header_vmcs(struct pt_query_decoder *d)
602 return -pte_internal;
605 int pt_pkt_decode_mnt(struct pt_packet_decoder *d, struct pt_packet *p)
610 return -pte_internal;
612 int pt_qry_decode_mnt(struct pt_query_decoder *d)
616 return -pte_internal;
618 int pt_qry_header_mnt(struct pt_query_decoder *d)
622 return -pte_internal;
625 int pt_pkt_decode_exstop(struct pt_packet_decoder *d, struct pt_packet *p)
630 return -pte_internal;
632 int pt_qry_decode_exstop(struct pt_query_decoder *d)
636 return -pte_internal;
639 int pt_pkt_decode_mwait(struct pt_packet_decoder *d, struct pt_packet *p)
644 return -pte_internal;
646 int pt_qry_decode_mwait(struct pt_query_decoder *d)
650 return -pte_internal;
653 int pt_pkt_decode_pwre(struct pt_packet_decoder *d, struct pt_packet *p)
658 return -pte_internal;
660 int pt_qry_decode_pwre(struct pt_query_decoder *d)
664 return -pte_internal;
667 int pt_pkt_decode_pwrx(struct pt_packet_decoder *d, struct pt_packet *p)
672 return -pte_internal;
674 int pt_qry_decode_pwrx(struct pt_query_decoder *d)
678 return -pte_internal;
681 int pt_pkt_decode_ptw(struct pt_packet_decoder *d, struct pt_packet *p)
686 return -pte_internal;
688 int pt_qry_decode_ptw(struct pt_query_decoder *d)
692 return -pte_internal;