1 /*
2  * Copyright (c) 2016-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 #ifndef PT_BLOCK_DECODER_H
30 #define PT_BLOCK_DECODER_H
31 
32 #include "pt_query_decoder.h"
33 #include "pt_image.h"
34 #include "pt_retstack.h"
35 #include "pt_ild.h"
36 #include "pt_msec_cache.h"
37 
38 
39 /* A block decoder.
40  *
41  * It decodes Intel(R) Processor Trace into a sequence of instruction blocks
42  * such that the instructions in each block can be decoded without further need
43  * of trace.
44  */
45 struct pt_block_decoder {
46 	/* The Intel(R) Processor Trace query decoder. */
47 	struct pt_query_decoder query;
48 
49 	/* The configuration flags.
50 	 *
51 	 * Those are our flags set by the user.  In @query.config.flags, we set
52 	 * the flags we need for the query decoder.
53 	 */
54 	struct pt_conf_flags flags;
55 
56 	/* The default image. */
57 	struct pt_image default_image;
58 
59 	/* The image. */
60 	struct pt_image *image;
61 
62 	/* The current cached section. */
63 	struct pt_msec_cache scache;
64 
65 	/* The current address space. */
66 	struct pt_asid asid;
67 
68 	/* The current Intel(R) Processor Trace event. */
69 	struct pt_event event;
70 
71 	/* The call/return stack for ret compression. */
72 	struct pt_retstack retstack;
73 
74 	/* The current instruction.
75 	 *
76 	 * This is only valid if @process_insn is set.
77 	 */
78 	struct pt_insn insn;
79 	struct pt_insn_ext iext;
80 
81 	/* The start IP of the next block.
82 	 *
83 	 * If tracing is disabled, this is the IP at which we assume tracing to
84 	 * be resumed.
85 	 */
86 	uint64_t ip;
87 
88 	/* The current execution mode. */
89 	enum pt_exec_mode mode;
90 
91 	/* The status of the last successful decoder query.
92 	 *
93 	 * Errors are reported directly; the status is always a non-negative
94 	 * pt_status_flag bit-vector.
95 	 */
96 	int status;
97 
98 	/* A collection of flags defining how to proceed flow reconstruction:
99 	 *
100 	 * - tracing is enabled.
101 	 */
102 	uint32_t enabled:1;
103 
104 	/* - process @event. */
105 	uint32_t process_event:1;
106 
107 	/* - instructions are executed speculatively. */
108 	uint32_t speculative:1;
109 
110 	/* - process @insn/@iext.
111 	 *
112 	 *   We have started processing events binding to @insn/@iext.  The
113 	 *   instruction has been accounted for in the previous block, but we
114 	 *   have not yet proceeded past it.
115 	 *
116 	 *   We will do so in pt_blk_event() after processing all events that
117 	 *   bind to it.
118 	 */
119 	uint32_t process_insn:1;
120 
121 	/* - a paging event has already been bound to @insn/@iext. */
122 	uint32_t bound_paging:1;
123 
124 	/* - a vmcs event has already been bound to @insn/@iext. */
125 	uint32_t bound_vmcs:1;
126 
127 	/* - a ptwrite event has already been bound to @insn/@iext. */
128 	uint32_t bound_ptwrite:1;
129 };
130 
131 
132 /* Initialize a block decoder.
133  *
134  * Returns zero on success; a negative error code otherwise.
135  * Returns -pte_internal, if @decoder or @config is NULL.
136  */
137 extern int pt_blk_decoder_init(struct pt_block_decoder *decoder,
138 			       const struct pt_config *config);
139 
140 /* Finalize a block decoder. */
141 extern void pt_blk_decoder_fini(struct pt_block_decoder *decoder);
142 
143 #endif /* PT_BLOCK_DECODER_H */
144