• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2020 Google, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #ifndef _ISASPEC_H_
25 #define _ISASPEC_H_
26 
27 #include <stdbool.h>
28 #include <stdint.h>
29 
30 #include <stdio.h>
31 #include "util/bitset.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 struct isa_decode_value {
38 	/** for {NAME} */
39 	const char *str;
40 	/** for all other fields */
41 	uint64_t num;
42 };
43 
44 struct isa_decode_hook {
45 	const char *fieldname;
46 	void (*cb)(void *data, struct isa_decode_value *val);
47 };
48 
49 struct isa_entrypoint {
50 	const char *name;
51 	uint32_t offset;
52 };
53 
54 struct isa_print_state {
55 	FILE *out;
56 
57 	/**
58 	 * Column number of current line
59 	 */
60 	unsigned line_column;
61 };
62 
63 void isa_print(struct isa_print_state *state, const char *fmt, ...) PRINTFLIKE(2, 3);
64 
65 struct isa_decode_options {
66 	uint32_t gpu_id;
67 
68 	/** show errors detected in decoding, like unexpected dontcare bits */
69 	bool show_errors;
70 
71 	/**
72 	 * If non-zero, maximum # of instructions that are unmatched before
73 	 * bailing, ie. to trigger stopping if we start trying to decode
74 	 * random garbage.
75 	 */
76 	unsigned max_errors;
77 
78 	/** Generate branch target labels */
79 	bool branch_labels;
80 
81 	/**
82 	 * Flag which can be set, for ex, but decode hook to trigger end of
83 	 * decoding
84 	 */
85 	bool stop;
86 
87 	/**
88 	 * Data passed back to decode hooks
89 	 */
90 	void *cbdata;
91 
92 	/**
93 	 * Callback for field decode
94 	 */
95 	void (*field_cb)(void *data, const char *field_name, struct isa_decode_value *val);
96 
97 	/**
98 	 * Callback for fields that need custom code to print their value.
99 	 */
100 	void (*field_print_cb)(struct isa_print_state *print, const char *field_name, uint64_t val);
101 
102 	/**
103 	 * Callback prior to instruction decode
104 	 */
105 	void (*pre_instr_cb)(void *data, unsigned n, void *instr);
106 
107 	/**
108 	 * Callback after instruction decode
109 	 */
110 	void (*post_instr_cb)(void *data, unsigned n, void *instr);
111 
112 	/**
113 	 * callback for undefined instructions
114 	 */
115 	void (*no_match_cb)(FILE *out, const BITSET_WORD *bitset, size_t size);
116 
117 	/**
118 	 * List of known entrypoints to treat like call targets
119 	 */
120 	unsigned entrypoint_count;
121 	const struct isa_entrypoint *entrypoints;
122 };
123 
124 void isa_disasm(void *bin, int sz, FILE *out, const struct isa_decode_options *options);
125 
126 bool isa_decode(void *out, void *bin, const struct isa_decode_options *options);
127 
128 #ifdef __cplusplus
129 }
130 #endif
131 
132 #endif /* _ISASPEC_H_ */
133