2 * Copyright (c) 2013-2019, 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.
30 #include "pt_packet.h"
31 #include "pt_opcodes.h"
36 /* A psb packet contains a unique 2-byte repeating pattern.
38 * There are only two ways to fill up a 64bit work with such a pattern.
40 static const uint64_t psb_pattern[] = {
41 ((uint64_t) pt_psb_lohi | (uint64_t) pt_psb_lohi << 16 |
42 (uint64_t) pt_psb_lohi << 32 | (uint64_t) pt_psb_lohi << 48),
43 ((uint64_t) pt_psb_hilo | (uint64_t) pt_psb_hilo << 16 |
44 (uint64_t) pt_psb_hilo << 32 | (uint64_t) pt_psb_hilo << 48)
47 static const uint8_t *truncate(const uint8_t *pointer, size_t alignment)
49 uintptr_t raw = (uintptr_t) pointer;
54 return (const uint8_t *) raw;
57 static const uint8_t *align(const uint8_t *pointer, size_t alignment)
59 return truncate(pointer + alignment - 1, alignment);
62 /* Find a psb packet given a position somewhere in the payload.
64 * Return the position of the psb packet.
65 * Return NULL, if this is not a psb packet.
67 static const uint8_t *pt_find_psb(const uint8_t *pos,
68 const struct pt_config *config)
70 const uint8_t *begin, *end;
76 begin = config->begin;
79 /* Navigate to the end of the psb payload pattern.
81 * Beware that PSB is an extended opcode. We must not confuse the extend
82 * opcode of the following packet as belonging to the PSB.
84 if (*pos != pt_psb_hi)
87 for (; (pos + 1) < end; pos += 2) {
100 * We're right after the psb payload and within the buffer.
101 * Navigate to the expected beginning of the psb packet.
105 /* Check if we're still inside the buffer. */
109 /* Check that this is indeed a psb packet we're at. */
110 if (pos[0] != pt_opc_psb || pos[1] != pt_ext_psb)
113 errcode = pt_pkt_read_psb(pos, config);
120 static int pt_sync_within_bounds(const uint8_t *pos, const uint8_t *begin,
123 /* We allow @pos == @end representing the very end of the trace.
125 * This will result in -pte_eos when we actually try to read from @pos.
127 return (begin <= pos) && (pos <= end);
130 int pt_sync_set(const uint8_t **sync, const uint8_t *pos,
131 const struct pt_config *config)
133 const uint8_t *begin, *end;
136 if (!sync || !pos || !config)
137 return -pte_internal;
139 begin = config->begin;
142 if (!pt_sync_within_bounds(pos, begin, end))
148 /* Check that this is indeed a psb packet we're at. */
149 if (pos[0] != pt_opc_psb || pos[1] != pt_ext_psb)
152 errcode = pt_pkt_read_psb(pos, config);
161 int pt_sync_forward(const uint8_t **sync, const uint8_t *pos,
162 const struct pt_config *config)
164 const uint8_t *begin, *end, *start;
166 if (!sync || !pos || !config)
167 return -pte_internal;
170 begin = config->begin;
173 if (!pt_sync_within_bounds(pos, begin, end))
174 return -pte_internal;
176 /* We search for a full 64bit word. It's OK to skip the current one. */
177 pos = align(pos, sizeof(*psb_pattern));
179 /* Search for the psb payload pattern in the buffer. */
181 const uint8_t *current = pos;
184 pos += sizeof(uint64_t);
188 val = * (const uint64_t *) current;
190 if ((val != psb_pattern[0]) && (val != psb_pattern[1]))
193 /* We found a 64bit word's worth of psb payload pattern. */
194 current = pt_find_psb(pos, config);
198 /* If @start points inside a PSB, we may find that one. Ignore
199 * it unless @start points to its beginning.
209 int pt_sync_backward(const uint8_t **sync, const uint8_t *pos,
210 const struct pt_config *config)
212 const uint8_t *begin, *end;
214 if (!sync || !pos || !config)
215 return -pte_internal;
217 begin = config->begin;
220 if (!pt_sync_within_bounds(pos, begin, end))
221 return -pte_internal;
223 /* We search for a full 64bit word. It's OK to skip the current one. */
224 pos = truncate(pos, sizeof(*psb_pattern));
226 /* Search for the psb payload pattern in the buffer. */
228 const uint8_t *next = pos;
231 pos -= sizeof(uint64_t);
235 val = * (const uint64_t *) pos;
237 if ((val != psb_pattern[0]) && (val != psb_pattern[1]))
240 /* We found a 64bit word's worth of psb payload pattern. */
241 next = pt_find_psb(next, config);