xref: /freebsd/contrib/processor-trace/libipt/internal/include/pt_block_cache.h (revision 85f87cf491bec6f90948a85b10f5523ea24db9e3)
174fe6c29SRuslan Bukin /*
2*85f87cf4SRuslan Bukin  * Copyright (c) 2016-2019, Intel Corporation
374fe6c29SRuslan Bukin  *
474fe6c29SRuslan Bukin  * Redistribution and use in source and binary forms, with or without
574fe6c29SRuslan Bukin  * modification, are permitted provided that the following conditions are met:
674fe6c29SRuslan Bukin  *
774fe6c29SRuslan Bukin  *  * Redistributions of source code must retain the above copyright notice,
874fe6c29SRuslan Bukin  *    this list of conditions and the following disclaimer.
974fe6c29SRuslan Bukin  *  * Redistributions in binary form must reproduce the above copyright notice,
1074fe6c29SRuslan Bukin  *    this list of conditions and the following disclaimer in the documentation
1174fe6c29SRuslan Bukin  *    and/or other materials provided with the distribution.
1274fe6c29SRuslan Bukin  *  * Neither the name of Intel Corporation nor the names of its contributors
1374fe6c29SRuslan Bukin  *    may be used to endorse or promote products derived from this software
1474fe6c29SRuslan Bukin  *    without specific prior written permission.
1574fe6c29SRuslan Bukin  *
1674fe6c29SRuslan Bukin  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1774fe6c29SRuslan Bukin  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1874fe6c29SRuslan Bukin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1974fe6c29SRuslan Bukin  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2074fe6c29SRuslan Bukin  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2174fe6c29SRuslan Bukin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2274fe6c29SRuslan Bukin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2374fe6c29SRuslan Bukin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2474fe6c29SRuslan Bukin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2574fe6c29SRuslan Bukin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2674fe6c29SRuslan Bukin  * POSSIBILITY OF SUCH DAMAGE.
2774fe6c29SRuslan Bukin  */
2874fe6c29SRuslan Bukin 
2974fe6c29SRuslan Bukin #ifndef PT_BLOCK_CACHE_H
3074fe6c29SRuslan Bukin #define PT_BLOCK_CACHE_H
3174fe6c29SRuslan Bukin 
3274fe6c29SRuslan Bukin #include "intel-pt.h"
3374fe6c29SRuslan Bukin 
3474fe6c29SRuslan Bukin #include <stdint.h>
3574fe6c29SRuslan Bukin 
3674fe6c29SRuslan Bukin 
3774fe6c29SRuslan Bukin /* A block cache entry qualifier.
3874fe6c29SRuslan Bukin  *
3974fe6c29SRuslan Bukin  * This describes what to do at the decision point determined by a block cache
4074fe6c29SRuslan Bukin  * entry.
4174fe6c29SRuslan Bukin  */
4274fe6c29SRuslan Bukin enum pt_bcache_qualifier {
4374fe6c29SRuslan Bukin 	/* This is not a decision point.
4474fe6c29SRuslan Bukin 	 *
4574fe6c29SRuslan Bukin 	 * The next decision point is too far away and one or more fields
4674fe6c29SRuslan Bukin 	 * threatened to overflow so we had to stop somewhere on our way.
4774fe6c29SRuslan Bukin 	 *
4874fe6c29SRuslan Bukin 	 * Apply the displacement and number of instructions and continue from
4974fe6c29SRuslan Bukin 	 * the resulting IP.
5074fe6c29SRuslan Bukin 	 */
5174fe6c29SRuslan Bukin 	ptbq_again,
5274fe6c29SRuslan Bukin 
5374fe6c29SRuslan Bukin 	/* The decision point is a conditional branch.
5474fe6c29SRuslan Bukin 	 *
5574fe6c29SRuslan Bukin 	 * This requires a conditional branch query.
5674fe6c29SRuslan Bukin 	 *
5774fe6c29SRuslan Bukin 	 * The isize field should provide the size of the branch instruction so
5874fe6c29SRuslan Bukin 	 * only taken branches require the instruction to be decoded.
5974fe6c29SRuslan Bukin 	 */
6074fe6c29SRuslan Bukin 	ptbq_cond,
6174fe6c29SRuslan Bukin 
6274fe6c29SRuslan Bukin 	/* The decision point is a near indirect call.
6374fe6c29SRuslan Bukin 	 *
6474fe6c29SRuslan Bukin 	 * This requires a return-address stack update and an indirect branch
6574fe6c29SRuslan Bukin 	 * query.
6674fe6c29SRuslan Bukin 	 *
6774fe6c29SRuslan Bukin 	 * The isize field should provide the size of the call instruction so
6874fe6c29SRuslan Bukin 	 * the return address can be computed by adding it to the displacement
6974fe6c29SRuslan Bukin 	 * that brings us to the call instruction.
7074fe6c29SRuslan Bukin 	 *
7174fe6c29SRuslan Bukin 	 * No instruction decode is required.
7274fe6c29SRuslan Bukin 	 */
7374fe6c29SRuslan Bukin 	ptbq_ind_call,
7474fe6c29SRuslan Bukin 
7574fe6c29SRuslan Bukin 	/* The decision point is a near return.
7674fe6c29SRuslan Bukin 	 *
7774fe6c29SRuslan Bukin 	 * The return may be compressed so this requires a conditional branch
7874fe6c29SRuslan Bukin 	 * query to determine the compression state and either a return-address
7974fe6c29SRuslan Bukin 	 * stack lookup or an indirect branch query.
8074fe6c29SRuslan Bukin 	 *
8174fe6c29SRuslan Bukin 	 * No instruction decode is required.
8274fe6c29SRuslan Bukin 	 */
8374fe6c29SRuslan Bukin 	ptbq_return,
8474fe6c29SRuslan Bukin 
8574fe6c29SRuslan Bukin 	/* The decision point is an indirect jump or far branch.
8674fe6c29SRuslan Bukin 	 *
8774fe6c29SRuslan Bukin 	 * This requires an indirect branch query.
8874fe6c29SRuslan Bukin 	 *
8974fe6c29SRuslan Bukin 	 * No instruction decode is required.
9074fe6c29SRuslan Bukin 	 */
9174fe6c29SRuslan Bukin 	ptbq_indirect,
9274fe6c29SRuslan Bukin 
9374fe6c29SRuslan Bukin 	/* The decision point requires the instruction at the decision point IP
9474fe6c29SRuslan Bukin 	 * to be decoded to determine the next step.
9574fe6c29SRuslan Bukin 	 *
9674fe6c29SRuslan Bukin 	 * This is used for
9774fe6c29SRuslan Bukin 	 *
9874fe6c29SRuslan Bukin 	 *   - near direct calls that need to maintain the return-address stack.
9974fe6c29SRuslan Bukin 	 *
10074fe6c29SRuslan Bukin 	 *   - near direct jumps that are too far away to be handled with a
10174fe6c29SRuslan Bukin 	 *     block cache entry as they would overflow the displacement field.
10274fe6c29SRuslan Bukin 	 */
10374fe6c29SRuslan Bukin 	ptbq_decode
10474fe6c29SRuslan Bukin };
10574fe6c29SRuslan Bukin 
10674fe6c29SRuslan Bukin /* A block cache entry.
10774fe6c29SRuslan Bukin  *
10874fe6c29SRuslan Bukin  * There will be one such entry per byte of decoded memory image.  Each entry
10974fe6c29SRuslan Bukin  * corresponds to an IP in the traced memory image.  The cache is initialized
11074fe6c29SRuslan Bukin  * with invalid entries for all IPs.
11174fe6c29SRuslan Bukin  *
11274fe6c29SRuslan Bukin  * Only entries for the first byte of each instruction will be used; other
11374fe6c29SRuslan Bukin  * entries are ignored and will remain invalid.
11474fe6c29SRuslan Bukin  *
11574fe6c29SRuslan Bukin  * Each valid entry gives the distance from the entry's IP to the next decision
11674fe6c29SRuslan Bukin  * point both in bytes and in the number of instructions.
11774fe6c29SRuslan Bukin  */
11874fe6c29SRuslan Bukin struct pt_bcache_entry {
11974fe6c29SRuslan Bukin 	/* The displacement to the next decision point in bytes.
12074fe6c29SRuslan Bukin 	 *
12174fe6c29SRuslan Bukin 	 * This is zero if we are at a decision point except for ptbq_again
12274fe6c29SRuslan Bukin 	 * where it gives the displacement to the next block cache entry to be
12374fe6c29SRuslan Bukin 	 * used.
12474fe6c29SRuslan Bukin 	 */
12574fe6c29SRuslan Bukin 	int32_t displacement:16;
12674fe6c29SRuslan Bukin 
12774fe6c29SRuslan Bukin 	/* The number of instructions to the next decision point.
12874fe6c29SRuslan Bukin 	 *
12974fe6c29SRuslan Bukin 	 * This is typically one at a decision point since we are already
13074fe6c29SRuslan Bukin 	 * accounting for the instruction at the decision point.
13174fe6c29SRuslan Bukin 	 *
13274fe6c29SRuslan Bukin 	 * Note that this field must be smaller than the respective struct
13374fe6c29SRuslan Bukin 	 * pt_block field so we can fit one block cache entry into an empty
13474fe6c29SRuslan Bukin 	 * block.
13574fe6c29SRuslan Bukin 	 */
13674fe6c29SRuslan Bukin 	uint32_t ninsn:8;
13774fe6c29SRuslan Bukin 
13874fe6c29SRuslan Bukin 	/* The execution mode for all instruction between here and the next
13974fe6c29SRuslan Bukin 	 * decision point.
14074fe6c29SRuslan Bukin 	 *
14174fe6c29SRuslan Bukin 	 * This is enum pt_exec_mode.
14274fe6c29SRuslan Bukin 	 *
14374fe6c29SRuslan Bukin 	 * This is ptem_unknown if the entry is not valid.
14474fe6c29SRuslan Bukin 	 */
14574fe6c29SRuslan Bukin 	uint32_t mode:2;
14674fe6c29SRuslan Bukin 
14774fe6c29SRuslan Bukin 	/* The decision point qualifier.
14874fe6c29SRuslan Bukin 	 *
14974fe6c29SRuslan Bukin 	 * This is enum pt_bcache_qualifier.
15074fe6c29SRuslan Bukin 	 */
15174fe6c29SRuslan Bukin 	uint32_t qualifier:3;
15274fe6c29SRuslan Bukin 
15374fe6c29SRuslan Bukin 	/* The size of the instruction at the decision point.
15474fe6c29SRuslan Bukin 	 *
15574fe6c29SRuslan Bukin 	 * This is zero if the size is too big to fit into the field.  In this
15674fe6c29SRuslan Bukin 	 * case, the instruction needs to be decoded to determine its size.
15774fe6c29SRuslan Bukin 	 */
15874fe6c29SRuslan Bukin 	uint32_t isize:3;
15974fe6c29SRuslan Bukin };
16074fe6c29SRuslan Bukin 
16174fe6c29SRuslan Bukin /* Get the execution mode of a block cache entry. */
pt_bce_exec_mode(struct pt_bcache_entry bce)16274fe6c29SRuslan Bukin static inline enum pt_exec_mode pt_bce_exec_mode(struct pt_bcache_entry bce)
16374fe6c29SRuslan Bukin {
16474fe6c29SRuslan Bukin 	return (enum pt_exec_mode) bce.mode;
16574fe6c29SRuslan Bukin }
16674fe6c29SRuslan Bukin 
16774fe6c29SRuslan Bukin /* Get the block cache qualifier of a block cache entry. */
16874fe6c29SRuslan Bukin static inline enum pt_bcache_qualifier
pt_bce_qualifier(struct pt_bcache_entry bce)16974fe6c29SRuslan Bukin pt_bce_qualifier(struct pt_bcache_entry bce)
17074fe6c29SRuslan Bukin {
17174fe6c29SRuslan Bukin 	return (enum pt_bcache_qualifier) bce.qualifier;
17274fe6c29SRuslan Bukin }
17374fe6c29SRuslan Bukin 
17474fe6c29SRuslan Bukin /* Check if a block cache entry is valid. */
pt_bce_is_valid(struct pt_bcache_entry bce)17574fe6c29SRuslan Bukin static inline int pt_bce_is_valid(struct pt_bcache_entry bce)
17674fe6c29SRuslan Bukin {
17774fe6c29SRuslan Bukin 	return pt_bce_exec_mode(bce) != ptem_unknown;
17874fe6c29SRuslan Bukin }
17974fe6c29SRuslan Bukin 
18074fe6c29SRuslan Bukin 
18174fe6c29SRuslan Bukin 
18274fe6c29SRuslan Bukin /* A block cache. */
18374fe6c29SRuslan Bukin struct pt_block_cache {
18474fe6c29SRuslan Bukin 	/* The number of cache entries. */
18574fe6c29SRuslan Bukin 	uint32_t nentries;
18674fe6c29SRuslan Bukin 
18774fe6c29SRuslan Bukin 	/* A variable-length array of @nentries entries. */
18874fe6c29SRuslan Bukin 	struct pt_bcache_entry entry[];
18974fe6c29SRuslan Bukin };
19074fe6c29SRuslan Bukin 
19174fe6c29SRuslan Bukin /* Create a block cache.
19274fe6c29SRuslan Bukin  *
19374fe6c29SRuslan Bukin  * @nentries is the number of entries in the cache and should match the size of
19474fe6c29SRuslan Bukin  * the to-be-cached section in bytes.
19574fe6c29SRuslan Bukin  */
19674fe6c29SRuslan Bukin extern struct pt_block_cache *pt_bcache_alloc(uint64_t nentries);
19774fe6c29SRuslan Bukin 
19874fe6c29SRuslan Bukin /* Destroy a block cache. */
19974fe6c29SRuslan Bukin extern void pt_bcache_free(struct pt_block_cache *bcache);
20074fe6c29SRuslan Bukin 
20174fe6c29SRuslan Bukin /* Cache a block.
20274fe6c29SRuslan Bukin  *
20374fe6c29SRuslan Bukin  * It is expected that all calls for the same @index write the same @bce.
20474fe6c29SRuslan Bukin  *
20574fe6c29SRuslan Bukin  * Returns zero on success, a negative error code otherwise.
20674fe6c29SRuslan Bukin  * Returns -pte_internal if @bcache is NULL.
20774fe6c29SRuslan Bukin  * Returns -pte_internal if @index is outside of @bcache.
20874fe6c29SRuslan Bukin  */
20974fe6c29SRuslan Bukin extern int pt_bcache_add(struct pt_block_cache *bcache, uint64_t index,
21074fe6c29SRuslan Bukin 			 struct pt_bcache_entry bce);
21174fe6c29SRuslan Bukin 
21274fe6c29SRuslan Bukin /* Lookup a cached block.
21374fe6c29SRuslan Bukin  *
21474fe6c29SRuslan Bukin  * The returned cache entry need not be valid.  The caller is expected to check
21574fe6c29SRuslan Bukin  * for validity using pt_bce_is_valid(*@bce).
21674fe6c29SRuslan Bukin  *
21774fe6c29SRuslan Bukin  * Returns zero on success, a negative error code otherwise.
21874fe6c29SRuslan Bukin  * Returns -pte_internal if @bcache or @bce is NULL.
21974fe6c29SRuslan Bukin  * Returns -pte_internal if @index is outside of @bcache.
22074fe6c29SRuslan Bukin  */
22174fe6c29SRuslan Bukin extern int pt_bcache_lookup(struct pt_bcache_entry *bce,
22274fe6c29SRuslan Bukin 			    const struct pt_block_cache *bcache,
22374fe6c29SRuslan Bukin 			    uint64_t index);
22474fe6c29SRuslan Bukin 
22574fe6c29SRuslan Bukin #endif /* PT_BLOCK_CACHE_H */
226