xref: /src/contrib/processor-trace/libipt/internal/include/pt_retstack.h (revision 85f87cf491bec6f90948a85b10f5523ea24db9e3)
1766f5c51SRuslan Bukin /*
285f87cf4SRuslan Bukin  * Copyright (c) 2013-2019, Intel Corporation
3766f5c51SRuslan Bukin  *
4766f5c51SRuslan Bukin  * Redistribution and use in source and binary forms, with or without
5766f5c51SRuslan Bukin  * modification, are permitted provided that the following conditions are met:
6766f5c51SRuslan Bukin  *
7766f5c51SRuslan Bukin  *  * Redistributions of source code must retain the above copyright notice,
8766f5c51SRuslan Bukin  *    this list of conditions and the following disclaimer.
9766f5c51SRuslan Bukin  *  * Redistributions in binary form must reproduce the above copyright notice,
10766f5c51SRuslan Bukin  *    this list of conditions and the following disclaimer in the documentation
11766f5c51SRuslan Bukin  *    and/or other materials provided with the distribution.
12766f5c51SRuslan Bukin  *  * Neither the name of Intel Corporation nor the names of its contributors
13766f5c51SRuslan Bukin  *    may be used to endorse or promote products derived from this software
14766f5c51SRuslan Bukin  *    without specific prior written permission.
15766f5c51SRuslan Bukin  *
16766f5c51SRuslan Bukin  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17766f5c51SRuslan Bukin  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18766f5c51SRuslan Bukin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19766f5c51SRuslan Bukin  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20766f5c51SRuslan Bukin  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21766f5c51SRuslan Bukin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22766f5c51SRuslan Bukin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23766f5c51SRuslan Bukin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24766f5c51SRuslan Bukin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25766f5c51SRuslan Bukin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26766f5c51SRuslan Bukin  * POSSIBILITY OF SUCH DAMAGE.
27766f5c51SRuslan Bukin  */
28766f5c51SRuslan Bukin 
29766f5c51SRuslan Bukin #ifndef PT_RETSTACK_H
30766f5c51SRuslan Bukin #define PT_RETSTACK_H
31766f5c51SRuslan Bukin 
32766f5c51SRuslan Bukin #include <stdint.h>
33766f5c51SRuslan Bukin 
34766f5c51SRuslan Bukin 
35766f5c51SRuslan Bukin /* The size of the call/return stack in number of entries. */
36766f5c51SRuslan Bukin enum {
37766f5c51SRuslan Bukin 	pt_retstack_size	= 64
38766f5c51SRuslan Bukin };
39766f5c51SRuslan Bukin 
40766f5c51SRuslan Bukin /* A stack of return addresses used for return compression. */
41766f5c51SRuslan Bukin struct pt_retstack {
42766f5c51SRuslan Bukin 	/* The stack of return addresses.
43766f5c51SRuslan Bukin 	 *
44766f5c51SRuslan Bukin 	 * We use one additional entry in order to distinguish a full from
45766f5c51SRuslan Bukin 	 * an empty stack.
46766f5c51SRuslan Bukin 	 */
47766f5c51SRuslan Bukin 	uint64_t stack[pt_retstack_size + 1];
48766f5c51SRuslan Bukin 
49766f5c51SRuslan Bukin 	/* The top of the stack. */
50766f5c51SRuslan Bukin 	uint8_t top;
51766f5c51SRuslan Bukin 
52766f5c51SRuslan Bukin 	/* The bottom of the stack. */
53766f5c51SRuslan Bukin 	uint8_t bottom;
54766f5c51SRuslan Bukin };
55766f5c51SRuslan Bukin 
56766f5c51SRuslan Bukin /* Initialize (or reset) a call/return stack. */
57766f5c51SRuslan Bukin extern void pt_retstack_init(struct pt_retstack *);
58766f5c51SRuslan Bukin 
59766f5c51SRuslan Bukin /* Test a call/return stack for emptiness.
60766f5c51SRuslan Bukin  *
61766f5c51SRuslan Bukin  * Returns zero if @retstack contains at least one element.
62766f5c51SRuslan Bukin  * Returns a positive integer if @retstack is empty.
63766f5c51SRuslan Bukin  * Returns -pte_invalid if @retstack is NULL.
64766f5c51SRuslan Bukin  */
65766f5c51SRuslan Bukin extern int pt_retstack_is_empty(const struct pt_retstack *retstack);
66766f5c51SRuslan Bukin 
67766f5c51SRuslan Bukin /* Pop and return the topmost IP.
68766f5c51SRuslan Bukin  *
69766f5c51SRuslan Bukin  * If @ip is not NULL, provides the topmost return address on success.
70766f5c51SRuslan Bukin  * If @retstack is not empty, pops the topmost return address on success.
71766f5c51SRuslan Bukin  *
72766f5c51SRuslan Bukin  * Returns zero on success.
73766f5c51SRuslan Bukin  * Returns -pte_invalid if @retstack is NULL.
74766f5c51SRuslan Bukin  * Returns -pte_noip if @retstack is empty.
75766f5c51SRuslan Bukin  */
76766f5c51SRuslan Bukin extern int pt_retstack_pop(struct pt_retstack *retstack, uint64_t *ip);
77766f5c51SRuslan Bukin 
78766f5c51SRuslan Bukin /* Push a return address onto the stack.
79766f5c51SRuslan Bukin  *
80766f5c51SRuslan Bukin  * Pushes @ip onto @retstack.
81766f5c51SRuslan Bukin  * If @retstack is full, drops the oldest return address.
82766f5c51SRuslan Bukin  *
83766f5c51SRuslan Bukin  * Returns zero on success.
84766f5c51SRuslan Bukin  */
85766f5c51SRuslan Bukin extern int pt_retstack_push(struct pt_retstack *retstack, uint64_t ip);
86766f5c51SRuslan Bukin 
87766f5c51SRuslan Bukin #endif /* PT_RETSTACK_H */
88