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.
29 #include "pt_insn_decoder.h"
31 #include "pt_config.h"
33 #include "pt_compiler.h"
41 static int pt_insn_check_ip_event(struct pt_insn_decoder *,
42 const struct pt_insn *,
43 const struct pt_insn_ext *);
46 static void pt_insn_reset(struct pt_insn_decoder *decoder)
51 decoder->mode = ptem_unknown;
55 decoder->process_event = 0;
56 decoder->speculative = 0;
57 decoder->process_insn = 0;
58 decoder->bound_paging = 0;
59 decoder->bound_vmcs = 0;
60 decoder->bound_ptwrite = 0;
62 pt_retstack_init(&decoder->retstack);
63 pt_asid_init(&decoder->asid);
66 static int pt_insn_status(const struct pt_insn_decoder *decoder, int flags)
73 status = decoder->status;
75 /* Indicate whether tracing is disabled or enabled.
77 * This duplicates the indication in struct pt_insn and covers the case
78 * where we indicate the status after synchronizing.
80 if (!decoder->enabled)
81 flags |= pts_ip_suppressed;
83 /* Forward end-of-trace indications.
85 * Postpone it as long as we're still processing events, though.
87 if ((status & pts_eos) && !decoder->process_event)
93 /* Initialize the query decoder flags based on our flags. */
95 static int pt_insn_init_qry_flags(struct pt_conf_flags *qflags,
96 const struct pt_conf_flags *flags)
98 if (!qflags || !flags)
101 memset(qflags, 0, sizeof(*qflags));
102 qflags->variant.query.keep_tcal_on_ovf =
103 flags->variant.insn.keep_tcal_on_ovf;
108 int pt_insn_decoder_init(struct pt_insn_decoder *decoder,
109 const struct pt_config *uconfig)
111 struct pt_config config;
115 return -pte_internal;
117 errcode = pt_config_from_user(&config, uconfig);
121 /* The user supplied decoder flags. */
122 decoder->flags = config.flags;
124 /* Set the flags we need for the query decoder we use. */
125 errcode = pt_insn_init_qry_flags(&config.flags, &decoder->flags);
129 errcode = pt_qry_decoder_init(&decoder->query, &config);
133 pt_image_init(&decoder->default_image, NULL);
134 decoder->image = &decoder->default_image;
136 errcode = pt_msec_cache_init(&decoder->scache);
140 pt_insn_reset(decoder);
145 void pt_insn_decoder_fini(struct pt_insn_decoder *decoder)
150 pt_msec_cache_fini(&decoder->scache);
151 pt_image_fini(&decoder->default_image);
152 pt_qry_decoder_fini(&decoder->query);
155 struct pt_insn_decoder *pt_insn_alloc_decoder(const struct pt_config *config)
157 struct pt_insn_decoder *decoder;
160 decoder = malloc(sizeof(*decoder));
164 errcode = pt_insn_decoder_init(decoder, config);
173 void pt_insn_free_decoder(struct pt_insn_decoder *decoder)
178 pt_insn_decoder_fini(decoder);
182 /* Maybe synthesize a tick event.
184 * If we're not already processing events, check the current time against the
185 * last event's time. If it changed, synthesize a tick event with the new time.
187 * Returns zero if no tick event has been created.
188 * Returns a positive integer if a tick event has been created.
189 * Returns a negative error code otherwise.
191 static int pt_insn_tick(struct pt_insn_decoder *decoder, uint64_t ip)
195 uint32_t lost_mtc, lost_cyc;
199 return -pte_internal;
201 /* We're not generating tick events if tracing is disabled. */
202 if (!decoder->enabled)
203 return -pte_internal;
205 /* Events already provide a timestamp so there is no need to synthesize
206 * an artificial tick event. There's no room, either, since this would
207 * overwrite the in-progress event.
209 * In rare cases where we need to proceed to an event location using
210 * trace this may cause us to miss a timing update if the event is not
211 * forwarded to the user.
213 * The only case I can come up with at the moment is a MODE.EXEC binding
214 * to the TIP IP of a far branch.
216 if (decoder->process_event)
219 errcode = pt_qry_time(&decoder->query, &tsc, &lost_mtc, &lost_cyc);
221 /* If we don't have wall-clock time, we use relative time. */
222 if (errcode != -pte_no_time)
226 ev = &decoder->event;
228 /* We're done if time has not changed since the last event. */
232 /* Time has changed so we create a new tick event. */
233 memset(ev, 0, sizeof(*ev));
234 ev->type = ptev_tick;
235 ev->variant.tick.ip = ip;
237 /* Indicate if we have wall-clock time or only relative time. */
238 if (errcode != -pte_no_time)
241 ev->lost_mtc = lost_mtc;
242 ev->lost_cyc = lost_cyc;
244 /* We now have an event to process. */
245 decoder->process_event = 1;
250 /* Query an indirect branch.
252 * Returns zero on success, a negative error code otherwise.
254 static int pt_insn_indirect_branch(struct pt_insn_decoder *decoder,
261 return -pte_internal;
265 status = pt_qry_indirect_branch(&decoder->query, ip);
269 if (decoder->flags.variant.insn.enable_tick_events) {
270 errcode = pt_insn_tick(decoder, evip);
278 /* Query a conditional branch.
280 * Returns zero on success, a negative error code otherwise.
282 static int pt_insn_cond_branch(struct pt_insn_decoder *decoder, int *taken)
287 return -pte_internal;
289 status = pt_qry_cond_branch(&decoder->query, taken);
293 if (decoder->flags.variant.insn.enable_tick_events) {
294 errcode = pt_insn_tick(decoder, decoder->ip);
302 static int pt_insn_start(struct pt_insn_decoder *decoder, int status)
305 return -pte_internal;
310 decoder->status = status;
312 if (!(status & pts_ip_suppressed))
313 decoder->enabled = 1;
315 /* Process any initial events.
317 * Some events are processed after proceeding to the next IP in order to
318 * indicate things like tracing disable or trace stop in the preceding
319 * instruction. Those events will be processed without such an
320 * indication before decoding the current instruction.
322 * We do this already here so we can indicate user-events that precede
323 * the first instruction.
325 return pt_insn_check_ip_event(decoder, NULL, NULL);
328 int pt_insn_sync_forward(struct pt_insn_decoder *decoder)
335 pt_insn_reset(decoder);
337 status = pt_qry_sync_forward(&decoder->query, &decoder->ip);
339 return pt_insn_start(decoder, status);
342 int pt_insn_sync_backward(struct pt_insn_decoder *decoder)
349 pt_insn_reset(decoder);
351 status = pt_qry_sync_backward(&decoder->query, &decoder->ip);
353 return pt_insn_start(decoder, status);
356 int pt_insn_sync_set(struct pt_insn_decoder *decoder, uint64_t offset)
363 pt_insn_reset(decoder);
365 status = pt_qry_sync_set(&decoder->query, &decoder->ip, offset);
367 return pt_insn_start(decoder, status);
370 int pt_insn_get_offset(const struct pt_insn_decoder *decoder, uint64_t *offset)
375 return pt_qry_get_offset(&decoder->query, offset);
378 int pt_insn_get_sync_offset(const struct pt_insn_decoder *decoder,
384 return pt_qry_get_sync_offset(&decoder->query, offset);
387 struct pt_image *pt_insn_get_image(struct pt_insn_decoder *decoder)
392 return decoder->image;
395 int pt_insn_set_image(struct pt_insn_decoder *decoder,
396 struct pt_image *image)
402 image = &decoder->default_image;
404 decoder->image = image;
408 const struct pt_config *
409 pt_insn_get_config(const struct pt_insn_decoder *decoder)
414 return pt_qry_get_config(&decoder->query);
417 int pt_insn_time(struct pt_insn_decoder *decoder, uint64_t *time,
418 uint32_t *lost_mtc, uint32_t *lost_cyc)
420 if (!decoder || !time)
423 return pt_qry_time(&decoder->query, time, lost_mtc, lost_cyc);
426 int pt_insn_core_bus_ratio(struct pt_insn_decoder *decoder, uint32_t *cbr)
428 if (!decoder || !cbr)
431 return pt_qry_core_bus_ratio(&decoder->query, cbr);
434 int pt_insn_asid(const struct pt_insn_decoder *decoder, struct pt_asid *asid,
437 if (!decoder || !asid)
440 return pt_asid_to_user(asid, &decoder->asid, size);
443 static inline int event_pending(struct pt_insn_decoder *decoder)
450 if (decoder->process_event)
453 status = decoder->status;
454 if (!(status & pts_event_pending))
457 status = pt_qry_event(&decoder->query, &decoder->event,
458 sizeof(decoder->event));
462 decoder->process_event = 1;
463 decoder->status = status;
467 static int check_erratum_skd022(struct pt_insn_decoder *decoder)
469 struct pt_insn_ext iext;
474 return -pte_internal;
476 insn.mode = decoder->mode;
477 insn.ip = decoder->ip;
479 errcode = pt_insn_decode(&insn, &iext, decoder->image, &decoder->asid);
483 switch (iext.iclass) {
487 case PTI_INST_VMLAUNCH:
488 case PTI_INST_VMRESUME:
493 static inline int handle_erratum_skd022(struct pt_insn_decoder *decoder)
500 return -pte_internal;
502 errcode = check_erratum_skd022(decoder);
506 /* We turn the async disable into a sync disable. It will be processed
507 * after decoding the instruction.
509 ev = &decoder->event;
511 ip = ev->variant.async_disabled.ip;
513 ev->type = ptev_disabled;
514 ev->variant.disabled.ip = ip;
519 static int pt_insn_proceed(struct pt_insn_decoder *decoder,
520 const struct pt_insn *insn,
521 const struct pt_insn_ext *iext)
523 if (!decoder || !insn || !iext)
524 return -pte_internal;
526 /* Branch displacements apply to the next instruction. */
527 decoder->ip += insn->size;
529 /* We handle non-branches, non-taken conditional branches, and
530 * compressed returns directly in the switch and do some pre-work for
533 * All kinds of branches are handled below the switch.
535 switch (insn->iclass) {
540 case ptic_cond_jump: {
543 status = pt_insn_cond_branch(decoder, &taken);
547 decoder->status = status;
555 /* Log the call for return compression.
557 * Unless this is a call to the next instruction as is used
558 * for position independent code.
560 if (iext->variant.branch.displacement ||
561 !iext->variant.branch.is_direct)
562 pt_retstack_push(&decoder->retstack, decoder->ip);
569 /* Check for a compressed return. */
570 status = pt_insn_cond_branch(decoder, &taken);
572 decoder->status = status;
574 /* A compressed return is indicated by a taken
575 * conditional branch.
578 return -pte_bad_retcomp;
580 return pt_retstack_pop(&decoder->retstack,
589 case ptic_far_return:
594 return -pte_bad_insn;
597 /* Process a direct or indirect branch.
599 * This combines calls, uncompressed returns, taken conditional jumps,
600 * and all flavors of far transfers.
602 if (iext->variant.branch.is_direct)
603 decoder->ip += (uint64_t) (int64_t)
604 iext->variant.branch.displacement;
608 status = pt_insn_indirect_branch(decoder, &decoder->ip);
613 decoder->status = status;
615 /* We do need an IP to proceed. */
616 if (status & pts_ip_suppressed)
623 static int pt_insn_at_skl014(const struct pt_event *ev,
624 const struct pt_insn *insn,
625 const struct pt_insn_ext *iext,
626 const struct pt_config *config)
631 if (!ev || !insn || !iext || !config)
632 return -pte_internal;
634 if (!ev->ip_suppressed)
637 switch (insn->iclass) {
640 /* The erratum only applies to unconditional direct branches. */
641 if (!iext->variant.branch.is_direct)
644 /* Check the filter against the branch target. */
647 ip += (uint64_t) (int64_t) iext->variant.branch.displacement;
649 status = pt_filter_addr_check(&config->addr_filter, ip);
665 static int pt_insn_at_disabled_event(const struct pt_event *ev,
666 const struct pt_insn *insn,
667 const struct pt_insn_ext *iext,
668 const struct pt_config *config)
670 if (!ev || !insn || !iext || !config)
671 return -pte_internal;
673 if (ev->ip_suppressed) {
674 if (pt_insn_is_far_branch(insn, iext) ||
675 pt_insn_changes_cpl(insn, iext) ||
676 pt_insn_changes_cr3(insn, iext))
679 /* If we don't have a filter configuration we assume that no
680 * address filters were used and the erratum does not apply.
682 * We might otherwise disable tracing too early.
684 if (config->addr_filter.config.addr_cfg &&
685 config->errata.skl014 &&
686 pt_insn_at_skl014(ev, insn, iext, config))
689 switch (insn->iclass) {
696 /* If we got an IP with the disabled event, we may
697 * ignore direct branches that go to a different IP.
699 if (iext->variant.branch.is_direct) {
704 ip += (uint64_t) (int64_t)
705 iext->variant.branch.displacement;
707 if (ip != ev->variant.disabled.ip)
714 case ptic_far_return:
720 return -pte_bad_insn;
727 /* Postpone proceeding past @insn/@iext and indicate a pending event.
729 * There may be further events pending on @insn/@iext. Postpone proceeding past
730 * @insn/@iext until we processed all events that bind to it.
732 * Returns a non-negative pt_status_flag bit-vector indicating a pending event
733 * on success, a negative pt_error_code otherwise.
735 static int pt_insn_postpone(struct pt_insn_decoder *decoder,
736 const struct pt_insn *insn,
737 const struct pt_insn_ext *iext)
739 if (!decoder || !insn || !iext)
740 return -pte_internal;
742 if (!decoder->process_insn) {
743 decoder->process_insn = 1;
744 decoder->insn = *insn;
745 decoder->iext = *iext;
748 return pt_insn_status(decoder, pts_event_pending);
751 /* Remove any postponed instruction from @decoder.
753 * Returns zero on success, a negative pt_error_code otherwise.
755 static int pt_insn_clear_postponed(struct pt_insn_decoder *decoder)
758 return -pte_internal;
760 decoder->process_insn = 0;
761 decoder->bound_paging = 0;
762 decoder->bound_vmcs = 0;
763 decoder->bound_ptwrite = 0;
768 /* Proceed past a postponed instruction.
770 * Returns zero on success, a negative pt_error_code otherwise.
772 static int pt_insn_proceed_postponed(struct pt_insn_decoder *decoder)
777 return -pte_internal;
779 if (!decoder->process_insn)
780 return -pte_internal;
782 /* There's nothing to do if tracing got disabled. */
783 if (!decoder->enabled)
784 return pt_insn_clear_postponed(decoder);
786 status = pt_insn_proceed(decoder, &decoder->insn, &decoder->iext);
790 return pt_insn_clear_postponed(decoder);
793 /* Check for events that bind to instruction.
795 * Check whether an event is pending that binds to @insn/@iext, and, if that is
796 * the case, proceed past @insn/@iext and indicate the event by setting
799 * If that is not the case, we return zero. This is what pt_insn_status() would
802 * - we suppress pts_eos as long as we're processing events
803 * - we do not set pts_ip_suppressed since tracing must be enabled
805 * Returns a non-negative pt_status_flag bit-vector on success, a negative error
808 static int pt_insn_check_insn_event(struct pt_insn_decoder *decoder,
809 const struct pt_insn *insn,
810 const struct pt_insn_ext *iext)
816 return -pte_internal;
818 status = event_pending(decoder);
822 ev = &decoder->event;
826 case ptev_async_paging:
827 case ptev_async_vmcs:
828 case ptev_async_disabled:
829 case ptev_async_branch:
840 /* We're only interested in events that bind to instructions. */
844 status = pt_insn_at_disabled_event(ev, insn, iext,
845 &decoder->query.config);
849 /* We're at a synchronous disable event location.
851 * Let's determine the IP at which we expect tracing to resume.
853 status = pt_insn_next_ip(&decoder->ip, insn, iext);
855 /* We don't know the IP on error. */
858 /* For indirect calls, assume that we return to the next
861 * We only check the instruction class, not the
862 * is_direct property, since direct calls would have
863 * been handled by pt_insn_nex_ip() or would have
864 * provoked a different error.
866 if (status != -pte_bad_query)
869 switch (insn->iclass) {
872 decoder->ip = insn->ip + insn->size;
883 /* We bind at most one paging event to an instruction. */
884 if (decoder->bound_paging)
887 if (!pt_insn_binds_to_pip(insn, iext))
890 /* We bound a paging event. Make sure we do not bind further
891 * paging events to this instruction.
893 decoder->bound_paging = 1;
895 return pt_insn_postpone(decoder, insn, iext);
898 /* We bind at most one vmcs event to an instruction. */
899 if (decoder->bound_vmcs)
902 if (!pt_insn_binds_to_vmcs(insn, iext))
905 /* We bound a vmcs event. Make sure we do not bind further vmcs
906 * events to this instruction.
908 decoder->bound_vmcs = 1;
910 return pt_insn_postpone(decoder, insn, iext);
913 /* We bind at most one ptwrite event to an instruction. */
914 if (decoder->bound_ptwrite)
917 if (ev->ip_suppressed) {
918 if (!pt_insn_is_ptwrite(insn, iext))
921 /* Fill in the event IP. Our users will need them to
922 * make sense of the PTWRITE payload.
924 ev->variant.ptwrite.ip = decoder->ip;
925 ev->ip_suppressed = 0;
927 /* The ptwrite event contains the IP of the ptwrite
928 * instruction (CLIP) unlike most events that contain
929 * the IP of the first instruction that did not complete
932 * It's easier to handle this case here, as well.
934 if (decoder->ip != ev->variant.ptwrite.ip)
938 /* We bound a ptwrite event. Make sure we do not bind further
939 * ptwrite events to this instruction.
941 decoder->bound_ptwrite = 1;
943 return pt_insn_postpone(decoder, insn, iext);
946 return pt_insn_status(decoder, pts_event_pending);
950 /* The maximum number of steps to take when determining whether the
951 * event location can be reached.
953 bdm64_max_steps = 0x100
956 /* Try to work around erratum BDM64.
958 * If we got a transaction abort immediately following a branch that produced
959 * trace, the trace for that branch might have been corrupted.
961 * Returns a positive integer if the erratum was handled.
962 * Returns zero if the erratum does not seem to apply.
963 * Returns a negative error code otherwise.
965 static int handle_erratum_bdm64(struct pt_insn_decoder *decoder,
966 const struct pt_event *ev,
967 const struct pt_insn *insn,
968 const struct pt_insn_ext *iext)
972 if (!decoder || !ev || !insn || !iext)
973 return -pte_internal;
975 /* This only affects aborts. */
976 if (!ev->variant.tsx.aborted)
979 /* This only affects branches. */
980 if (!pt_insn_is_branch(insn, iext))
983 /* Let's check if we can reach the event location from here.
985 * If we can, let's assume the erratum did not hit. We might still be
986 * wrong but we're not able to tell.
988 status = pt_insn_range_is_contiguous(decoder->ip, ev->variant.tsx.ip,
989 decoder->mode, decoder->image,
990 &decoder->asid, bdm64_max_steps);
994 /* We can't reach the event location. This could either mean that we
995 * stopped too early (and status is zero) or that the erratum hit.
997 * We assume the latter and pretend that the previous branch brought us
998 * to the event location, instead.
1000 decoder->ip = ev->variant.tsx.ip;
1005 /* Check whether a peek TSX event should be postponed.
1007 * This involves handling erratum BDM64.
1009 * Returns a positive integer if the event is to be postponed.
1010 * Returns zero if the event should be processed.
1011 * Returns a negative error code otherwise.
1013 static inline int pt_insn_postpone_tsx(struct pt_insn_decoder *decoder,
1014 const struct pt_insn *insn,
1015 const struct pt_insn_ext *iext,
1016 const struct pt_event *ev)
1020 if (!decoder || !ev)
1021 return -pte_internal;
1023 if (ev->ip_suppressed)
1026 if (insn && iext && decoder->query.config.errata.bdm64) {
1027 status = handle_erratum_bdm64(decoder, ev, insn, iext);
1032 if (decoder->ip != ev->variant.tsx.ip)
1038 /* Check for events that bind to an IP.
1040 * Check whether an event is pending that binds to @decoder->ip, and, if that is
1041 * the case, indicate the event by setting pt_pts_event_pending.
1043 * Returns a non-negative pt_status_flag bit-vector on success, a negative error
1046 static int pt_insn_check_ip_event(struct pt_insn_decoder *decoder,
1047 const struct pt_insn *insn,
1048 const struct pt_insn_ext *iext)
1050 struct pt_event *ev;
1054 return -pte_internal;
1056 status = event_pending(decoder);
1061 return pt_insn_status(decoder, 0);
1064 ev = &decoder->event;
1070 return pt_insn_status(decoder, pts_event_pending);
1072 case ptev_async_disabled:
1073 if (ev->variant.async_disabled.at != decoder->ip)
1076 if (decoder->query.config.errata.skd022) {
1079 errcode = handle_erratum_skd022(decoder);
1084 /* If the erratum applies, we postpone the
1085 * modified event to the next call to
1092 return pt_insn_status(decoder, pts_event_pending);
1095 status = pt_insn_postpone_tsx(decoder, insn, iext, ev);
1103 return pt_insn_status(decoder, pts_event_pending);
1105 case ptev_async_branch:
1106 if (ev->variant.async_branch.from != decoder->ip)
1109 return pt_insn_status(decoder, pts_event_pending);
1112 return pt_insn_status(decoder, pts_event_pending);
1114 case ptev_exec_mode:
1115 if (!ev->ip_suppressed &&
1116 ev->variant.exec_mode.ip != decoder->ip)
1119 return pt_insn_status(decoder, pts_event_pending);
1122 if (decoder->enabled)
1125 return pt_insn_status(decoder, pts_event_pending);
1127 case ptev_async_paging:
1128 if (!ev->ip_suppressed &&
1129 ev->variant.async_paging.ip != decoder->ip)
1132 return pt_insn_status(decoder, pts_event_pending);
1135 if (decoder->enabled)
1138 return pt_insn_status(decoder, pts_event_pending);
1140 case ptev_async_vmcs:
1141 if (!ev->ip_suppressed &&
1142 ev->variant.async_vmcs.ip != decoder->ip)
1145 return pt_insn_status(decoder, pts_event_pending);
1148 return pt_insn_status(decoder, pts_event_pending);
1151 if (!ev->ip_suppressed && decoder->enabled &&
1152 decoder->ip != ev->variant.exstop.ip)
1155 return pt_insn_status(decoder, pts_event_pending);
1158 if (!ev->ip_suppressed && decoder->enabled &&
1159 decoder->ip != ev->variant.mwait.ip)
1162 return pt_insn_status(decoder, pts_event_pending);
1166 return pt_insn_status(decoder, pts_event_pending);
1169 /* Any event binding to the current PTWRITE instruction is
1170 * handled in pt_insn_check_insn_event().
1172 * Any subsequent ptwrite event binds to a different instruction
1173 * and must wait until the next iteration - as long as tracing
1176 * When tracing is disabled, we forward all ptwrite events
1177 * immediately to the user.
1179 if (decoder->enabled)
1182 return pt_insn_status(decoder, pts_event_pending);
1187 return pt_insn_status(decoder, pts_event_pending);
1190 return pt_insn_status(decoder, 0);
1193 static inline int insn_to_user(struct pt_insn *uinsn, size_t size,
1194 const struct pt_insn *insn)
1196 if (!uinsn || !insn)
1197 return -pte_internal;
1202 /* Zero out any unknown bytes. */
1203 if (sizeof(*insn) < size) {
1204 memset(uinsn + sizeof(*insn), 0, size - sizeof(*insn));
1206 size = sizeof(*insn);
1209 memcpy(uinsn, insn, size);
1214 static int pt_insn_decode_cached(struct pt_insn_decoder *decoder,
1215 const struct pt_mapped_section *msec,
1216 struct pt_insn *insn, struct pt_insn_ext *iext)
1220 if (!decoder || !insn || !iext)
1221 return -pte_internal;
1223 /* Try reading the memory containing @insn from the cached section. If
1224 * that fails, if we don't have a cached section, or if decode fails
1225 * later on, fall back to decoding @insn from @decoder->image.
1227 * The latter will also handle truncated instructions that cross section
1232 return pt_insn_decode(insn, iext, decoder->image,
1235 status = pt_msec_read(msec, insn->raw, sizeof(insn->raw), insn->ip);
1237 if (status != -pte_nomap)
1240 return pt_insn_decode(insn, iext, decoder->image,
1244 /* We initialize @insn->size to the maximal possible size. It will be
1245 * set to the actual size during instruction decode.
1247 insn->size = (uint8_t) status;
1249 status = pt_ild_decode(insn, iext);
1251 if (status != -pte_bad_insn)
1254 return pt_insn_decode(insn, iext, decoder->image,
1261 static int pt_insn_msec_lookup(struct pt_insn_decoder *decoder,
1262 const struct pt_mapped_section **pmsec)
1264 struct pt_msec_cache *scache;
1265 struct pt_image *image;
1269 if (!decoder || !pmsec)
1270 return -pte_internal;
1272 scache = &decoder->scache;
1273 image = decoder->image;
1276 isid = pt_msec_cache_read(scache, pmsec, image, ip);
1278 if (isid != -pte_nomap)
1281 return pt_msec_cache_fill(scache, pmsec, image,
1282 &decoder->asid, ip);
1288 int pt_insn_next(struct pt_insn_decoder *decoder, struct pt_insn *uinsn,
1291 const struct pt_mapped_section *msec;
1292 struct pt_insn_ext iext;
1293 struct pt_insn insn, *pinsn;
1296 if (!uinsn || !decoder)
1297 return -pte_invalid;
1299 /* Tracing must be enabled.
1301 * If it isn't we should be processing events until we either run out of
1302 * trace or process a tracing enabled event.
1304 if (!decoder->enabled) {
1305 if (decoder->status & pts_eos)
1308 return -pte_no_enable;
1311 pinsn = size == sizeof(insn) ? uinsn : &insn;
1313 /* Zero-initialize the instruction in case of error returns. */
1314 memset(pinsn, 0, sizeof(*pinsn));
1316 /* Fill in a few things from the current decode state.
1318 * This reflects the state of the last pt_insn_next(), pt_insn_event()
1319 * or pt_insn_start() call.
1321 if (decoder->speculative)
1322 pinsn->speculative = 1;
1323 pinsn->ip = decoder->ip;
1324 pinsn->mode = decoder->mode;
1326 isid = pt_insn_msec_lookup(decoder, &msec);
1328 if (isid != -pte_nomap)
1334 /* We set an incorrect isid if @msec is NULL. This will be corrected
1335 * when we read the memory from the image later on.
1339 status = pt_insn_decode_cached(decoder, msec, pinsn, &iext);
1341 /* Provide the incomplete instruction - the IP and mode fields
1342 * are valid and may help diagnose the error.
1344 (void) insn_to_user(uinsn, size, pinsn);
1348 /* Provide the decoded instruction to the user. It won't change during
1351 status = insn_to_user(uinsn, size, pinsn);
1355 /* Check for events that bind to the current instruction.
1357 * If an event is indicated, we're done.
1359 status = pt_insn_check_insn_event(decoder, pinsn, &iext);
1364 if (status & pts_event_pending)
1368 /* Determine the next instruction's IP. */
1369 status = pt_insn_proceed(decoder, pinsn, &iext);
1373 /* Indicate events that bind to the new IP.
1375 * Although we only look at the IP for binding events, we pass the
1376 * decoded instruction in order to handle errata.
1378 return pt_insn_check_ip_event(decoder, pinsn, &iext);
1381 static int pt_insn_process_enabled(struct pt_insn_decoder *decoder)
1383 struct pt_event *ev;
1386 return -pte_internal;
1388 ev = &decoder->event;
1390 /* This event can't be a status update. */
1391 if (ev->status_update)
1392 return -pte_bad_context;
1394 /* We must have an IP in order to start decoding. */
1395 if (ev->ip_suppressed)
1398 /* We must currently be disabled. */
1399 if (decoder->enabled)
1400 return -pte_bad_context;
1402 decoder->ip = ev->variant.enabled.ip;
1403 decoder->enabled = 1;
1408 static int pt_insn_process_disabled(struct pt_insn_decoder *decoder)
1410 struct pt_event *ev;
1413 return -pte_internal;
1415 ev = &decoder->event;
1417 /* This event can't be a status update. */
1418 if (ev->status_update)
1419 return -pte_bad_context;
1421 /* We must currently be enabled. */
1422 if (!decoder->enabled)
1423 return -pte_bad_context;
1425 /* We preserve @decoder->ip. This is where we expect tracing to resume
1426 * and we'll indicate that on the subsequent enabled event if tracing
1427 * actually does resume from there.
1429 decoder->enabled = 0;
1434 static int pt_insn_process_async_branch(struct pt_insn_decoder *decoder)
1436 struct pt_event *ev;
1439 return -pte_internal;
1441 ev = &decoder->event;
1443 /* This event can't be a status update. */
1444 if (ev->status_update)
1445 return -pte_bad_context;
1447 /* Tracing must be enabled in order to make sense of the event. */
1448 if (!decoder->enabled)
1449 return -pte_bad_context;
1451 decoder->ip = ev->variant.async_branch.to;
1456 static int pt_insn_process_paging(struct pt_insn_decoder *decoder)
1462 return -pte_internal;
1464 cr3 = decoder->event.variant.paging.cr3;
1465 if (decoder->asid.cr3 != cr3) {
1466 errcode = pt_msec_cache_invalidate(&decoder->scache);
1470 decoder->asid.cr3 = cr3;
1476 static int pt_insn_process_overflow(struct pt_insn_decoder *decoder)
1478 struct pt_event *ev;
1481 return -pte_internal;
1483 ev = &decoder->event;
1485 /* This event can't be a status update. */
1486 if (ev->status_update)
1487 return -pte_bad_context;
1489 /* If the IP is suppressed, the overflow resolved while tracing was
1490 * disabled. Otherwise it resolved while tracing was enabled.
1492 if (ev->ip_suppressed) {
1493 /* Tracing is disabled.
1495 * It doesn't make sense to preserve the previous IP. This will
1496 * just be misleading. Even if tracing had been disabled
1497 * before, as well, we might have missed the re-enable in the
1500 decoder->enabled = 0;
1503 /* Tracing is enabled and we're at the IP at which the overflow
1506 decoder->ip = ev->variant.overflow.ip;
1507 decoder->enabled = 1;
1510 /* We don't know the TSX state. Let's assume we execute normally.
1512 * We also don't know the execution mode. Let's keep what we have
1513 * in case we don't get an update before we have to decode the next
1516 decoder->speculative = 0;
1521 static int pt_insn_process_exec_mode(struct pt_insn_decoder *decoder)
1523 enum pt_exec_mode mode;
1524 struct pt_event *ev;
1527 return -pte_internal;
1529 ev = &decoder->event;
1530 mode = ev->variant.exec_mode.mode;
1532 /* Use status update events to diagnose inconsistencies. */
1533 if (ev->status_update && decoder->enabled &&
1534 decoder->mode != ptem_unknown && decoder->mode != mode)
1535 return -pte_bad_status_update;
1537 decoder->mode = mode;
1542 static int pt_insn_process_tsx(struct pt_insn_decoder *decoder)
1545 return -pte_internal;
1547 decoder->speculative = decoder->event.variant.tsx.speculative;
1552 static int pt_insn_process_stop(struct pt_insn_decoder *decoder)
1554 struct pt_event *ev;
1557 return -pte_internal;
1559 ev = &decoder->event;
1561 /* This event can't be a status update. */
1562 if (ev->status_update)
1563 return -pte_bad_context;
1565 /* Tracing is always disabled before it is stopped. */
1566 if (decoder->enabled)
1567 return -pte_bad_context;
1572 static int pt_insn_process_vmcs(struct pt_insn_decoder *decoder)
1578 return -pte_internal;
1580 vmcs = decoder->event.variant.vmcs.base;
1581 if (decoder->asid.vmcs != vmcs) {
1582 errcode = pt_msec_cache_invalidate(&decoder->scache);
1586 decoder->asid.vmcs = vmcs;
1592 int pt_insn_event(struct pt_insn_decoder *decoder, struct pt_event *uevent,
1595 struct pt_event *ev;
1598 if (!decoder || !uevent)
1599 return -pte_invalid;
1601 /* We must currently process an event. */
1602 if (!decoder->process_event)
1603 return -pte_bad_query;
1605 ev = &decoder->event;
1608 /* This is not a user event.
1610 * We either indicated it wrongly or the user called
1611 * pt_insn_event() without a pts_event_pending indication.
1613 return -pte_bad_query;
1616 /* Indicate that tracing resumes from the IP at which tracing
1617 * had been disabled before (with some special treatment for
1620 if (decoder->ip == ev->variant.enabled.ip)
1621 ev->variant.enabled.resumed = 1;
1623 status = pt_insn_process_enabled(decoder);
1629 case ptev_async_disabled:
1630 if (!ev->ip_suppressed &&
1631 decoder->ip != ev->variant.async_disabled.at)
1632 return -pte_bad_query;
1636 status = pt_insn_process_disabled(decoder);
1642 case ptev_async_branch:
1643 if (decoder->ip != ev->variant.async_branch.from)
1644 return -pte_bad_query;
1646 status = pt_insn_process_async_branch(decoder);
1652 case ptev_async_paging:
1653 if (!ev->ip_suppressed &&
1654 decoder->ip != ev->variant.async_paging.ip)
1655 return -pte_bad_query;
1659 status = pt_insn_process_paging(decoder);
1665 case ptev_async_vmcs:
1666 if (!ev->ip_suppressed &&
1667 decoder->ip != ev->variant.async_vmcs.ip)
1668 return -pte_bad_query;
1672 status = pt_insn_process_vmcs(decoder);
1679 status = pt_insn_process_overflow(decoder);
1685 case ptev_exec_mode:
1686 status = pt_insn_process_exec_mode(decoder);
1693 status = pt_insn_process_tsx(decoder);
1700 status = pt_insn_process_stop(decoder);
1707 if (!ev->ip_suppressed && decoder->enabled &&
1708 decoder->ip != ev->variant.exstop.ip)
1709 return -pte_bad_query;
1714 if (!ev->ip_suppressed && decoder->enabled &&
1715 decoder->ip != ev->variant.mwait.ip)
1716 return -pte_bad_query;
1729 /* Copy the event to the user. Make sure we're not writing beyond the
1730 * memory provided by the user.
1732 * We might truncate details of an event but only for those events the
1733 * user can't know about, anyway.
1735 if (sizeof(*ev) < size)
1738 memcpy(uevent, ev, size);
1740 /* This completes processing of the current event. */
1741 decoder->process_event = 0;
1743 /* If we just handled an instruction event, check for further events
1744 * that bind to this instruction.
1746 * If we don't have further events, proceed beyond the instruction so we
1747 * can check for IP events, as well.
1749 if (decoder->process_insn) {
1750 status = pt_insn_check_insn_event(decoder, &decoder->insn,
1757 if (status & pts_event_pending)
1761 /* Proceed to the next instruction. */
1762 status = pt_insn_proceed_postponed(decoder);
1767 /* Indicate further events that bind to the same IP. */
1768 return pt_insn_check_ip_event(decoder, NULL, NULL);