Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
/*
 * Copyright (c) 2014-2019, Intel Corporation
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  * Neither the name of Intel Corporation nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "pt_encoder.h"
#include "pt_config.h"
#include "pt_opcodes.h"

#include <string.h>
#include <stdlib.h>


int pt_encoder_init(struct pt_encoder *encoder, const struct pt_config *config)
{
	int errcode;

	if (!encoder)
		return -pte_invalid;

	memset(encoder, 0, sizeof(*encoder));

	errcode = pt_config_from_user(&encoder->config, config);
	if (errcode < 0)
		return errcode;

	encoder->pos = encoder->config.begin;

	return 0;
}

void pt_encoder_fini(struct pt_encoder *encoder)
{
	(void) encoder;

	/* Nothing to do. */
}

struct pt_encoder *pt_alloc_encoder(const struct pt_config *config)
{
	struct pt_encoder *encoder;
	int errcode;

	encoder = malloc(sizeof(*encoder));
	if (!encoder)
		return NULL;

	errcode = pt_encoder_init(encoder, config);
	if (errcode < 0) {
		free(encoder);
		return NULL;
	}

	return encoder;
}

void pt_free_encoder(struct pt_encoder *encoder)
{
	pt_encoder_fini(encoder);
	free(encoder);
}

int pt_enc_sync_set(struct pt_encoder *encoder, uint64_t offset)
{
	uint8_t *begin, *end, *pos;

	if (!encoder)
		return -pte_invalid;

	begin = encoder->config.begin;
	end = encoder->config.end;
	pos = begin + offset;

	if (end < pos || pos < begin)
		return -pte_eos;

	encoder->pos = pos;
	return 0;
}

int pt_enc_get_offset(const struct pt_encoder *encoder, uint64_t *offset)
{
	const uint8_t *raw, *begin;

	if (!encoder || !offset)
		return -pte_invalid;

	/* The encoder is synchronized at all times. */
	raw = encoder->pos;
	if (!raw)
		return -pte_internal;

	begin = encoder->config.begin;
	if (!begin)
		return -pte_internal;

	*offset = (uint64_t) (int64_t) (raw - begin);
	return 0;
}

const struct pt_config *pt_enc_get_config(const struct pt_encoder *encoder)
{
	if (!encoder)
		return NULL;

	return &encoder->config;
}

/* Check the remaining space.
 *
 * Returns zero if there are at least \@size bytes of free space available in
 * \@encoder's Intel PT buffer.
 *
 * Returns -pte_eos if not enough space is available.
 * Returns -pte_internal if \@encoder is NULL.
 * Returns -pte_internal if \@encoder is not synchronized.
 */
static int pt_reserve(const struct pt_encoder *encoder, unsigned int size)
{
	const uint8_t *begin, *end, *pos;

	if (!encoder)
		return -pte_internal;

	/* The encoder is synchronized at all times. */
	pos = encoder->pos;
	if (!pos)
		return -pte_internal;

	begin = encoder->config.begin;
	end = encoder->config.end;

	pos += size;
	if (pos < begin || end < pos)
		return -pte_eos;

	return 0;
}

/* Return the size of an IP payload based on its IP compression.
 *
 * Returns -pte_bad_packet if \@ipc is not a valid IP compression.
 */
static int pt_ipc_size(enum pt_ip_compression ipc)
{
	switch (ipc) {
	case pt_ipc_suppressed:
		return 0;

	case pt_ipc_update_16:
		return pt_pl_ip_upd16_size;

	case pt_ipc_update_32:
		return pt_pl_ip_upd32_size;

	case pt_ipc_update_48:
		return pt_pl_ip_upd48_size;

	case pt_ipc_sext_48:
		return pt_pl_ip_sext48_size;

	case pt_ipc_full:
		return pt_pl_ip_full_size;
	}

	return -pte_invalid;
}

/* Encode an integer value.
 *
 * Writes the \@size least signifficant bytes of \@value starting from \@pos.
 *
 * The caller needs to ensure that there is enough space available.
 *
 * Returns the updated position.
 */
static uint8_t *pt_encode_int(uint8_t *pos, uint64_t val, int size)
{
	for (; size; --size, val >>= 8)
		*pos++ = (uint8_t) val;

	return pos;
}

/* Encode an IP packet.
 *
 * Write an IP packet with opcode \@opc and payload from \@packet if there is
 * enough space in \@encoder's Intel PT buffer.
 *
 * Returns the number of bytes written on success.
 *
 * Returns -pte_eos if there is not enough space.
 * Returns -pte_internal if \@encoder or \@packet is NULL.
 * Returns -pte_invalid if \@packet.ipc is not valid.
 */
static int pt_encode_ip(struct pt_encoder *encoder, enum pt_opcode op,
			const struct pt_packet_ip *packet)
{
	uint8_t *pos;
	uint8_t opc, ipc;
	int size, errcode;

	if (!encoder || !packet)
		return pte_internal;

	size = pt_ipc_size(packet->ipc);
	if (size < 0)
		return size;

	errcode = pt_reserve(encoder,
			     /* opc size = */ 1u + (unsigned int) size);
	if (errcode < 0)
		return errcode;

	/* We already checked the ipc in pt_ipc_size(). */
	ipc = (uint8_t) (packet->ipc << pt_opm_ipc_shr);
	opc = (uint8_t) op;

	pos = encoder->pos;
	*pos++ = opc | ipc;

	encoder->pos = pt_encode_int(pos, packet->ip, size);
	return /* opc size = */ 1 + size;
}

int pt_enc_next(struct pt_encoder *encoder, const struct pt_packet *packet)
{
	uint8_t *pos, *begin;
	int errcode;

	if (!encoder || !packet)
		return -pte_invalid;

	pos = begin = encoder->pos;
	switch (packet->type) {
	case ppt_pad:
		errcode = pt_reserve(encoder, ptps_pad);
		if (errcode < 0)
			return errcode;

		*pos++ = pt_opc_pad;

		encoder->pos = pos;
		return (int) (pos - begin);

	case ppt_psb: {
		uint64_t psb;

		errcode = pt_reserve(encoder, ptps_psb);
		if (errcode < 0)
			return errcode;

		psb = ((uint64_t) pt_psb_hilo << 48 |
		       (uint64_t) pt_psb_hilo << 32 |
		       (uint64_t) pt_psb_hilo << 16 |
		       (uint64_t) pt_psb_hilo);

		pos = pt_encode_int(pos, psb, 8);
		pos = pt_encode_int(pos, psb, 8);

		encoder->pos = pos;
		return (int) (pos - begin);
	}

	case ppt_psbend:
		errcode = pt_reserve(encoder, ptps_psbend);
		if (errcode < 0)
			return errcode;

		*pos++ = pt_opc_ext;
		*pos++ = pt_ext_psbend;

		encoder->pos = pos;
		return (int) (pos - begin);

	case ppt_ovf:
		errcode = pt_reserve(encoder, ptps_ovf);
		if (errcode < 0)
			return errcode;

		*pos++ = pt_opc_ext;
		*pos++ = pt_ext_ovf;

		encoder->pos = pos;
		return (int) (pos - begin);

	case ppt_fup:
		return pt_encode_ip(encoder, pt_opc_fup, &packet->payload.ip);

	case ppt_tip:
		return pt_encode_ip(encoder, pt_opc_tip, &packet->payload.ip);

	case ppt_tip_pge:
		return pt_encode_ip(encoder, pt_opc_tip_pge,
				    &packet->payload.ip);

	case ppt_tip_pgd:
		return pt_encode_ip(encoder, pt_opc_tip_pgd,
				    &packet->payload.ip);

	case ppt_tnt_8: {
		uint8_t opc, stop;

		if (packet->payload.tnt.bit_size >= 7)
			return -pte_bad_packet;

		errcode = pt_reserve(encoder, ptps_tnt_8);
		if (errcode < 0)
			return errcode;

		stop = packet->payload.tnt.bit_size + pt_opm_tnt_8_shr;
		opc = (uint8_t)
			(packet->payload.tnt.payload << pt_opm_tnt_8_shr);

		*pos++ = (uint8_t) (opc | (1u << stop));

		encoder->pos = pos;
		return (int) (pos - begin);
	}

	case ppt_tnt_64: {
		uint64_t tnt, stop;

		errcode = pt_reserve(encoder, ptps_tnt_64);
		if (errcode < 0)
			return errcode;

		if (packet->payload.tnt.bit_size >= pt_pl_tnt_64_bits)
			return -pte_invalid;

		stop = 1ull << packet->payload.tnt.bit_size;
		tnt = packet->payload.tnt.payload;

		if (tnt & ~(stop - 1))
			return -pte_invalid;

		*pos++ = pt_opc_ext;
		*pos++ = pt_ext_tnt_64;
		pos = pt_encode_int(pos, tnt | stop, pt_pl_tnt_64_size);

		encoder->pos = pos;
		return (int) (pos - begin);
	}

	case ppt_mode: {
		uint8_t mode;

		errcode = pt_reserve(encoder, ptps_mode);
		if (errcode < 0)
			return errcode;

		switch (packet->payload.mode.leaf) {
		default:
			return -pte_bad_packet;

		case pt_mol_exec:
			mode = pt_mol_exec;

			if (packet->payload.mode.bits.exec.csl)
				mode |= pt_mob_exec_csl;

			if (packet->payload.mode.bits.exec.csd)
				mode |= pt_mob_exec_csd;
			break;

		case pt_mol_tsx:
			mode = pt_mol_tsx;

			if (packet->payload.mode.bits.tsx.intx)
				mode |= pt_mob_tsx_intx;

			if (packet->payload.mode.bits.tsx.abrt)
				mode |= pt_mob_tsx_abrt;
			break;
		}

		*pos++ = pt_opc_mode;
		*pos++ = mode;

		encoder->pos = pos;
		return (int) (pos - begin);
	}

	case ppt_pip: {
		uint64_t cr3;

		errcode = pt_reserve(encoder, ptps_pip);
		if (errcode < 0)
			return errcode;

		cr3 = packet->payload.pip.cr3;
		cr3 >>= pt_pl_pip_shl;
		cr3 <<= pt_pl_pip_shr;

		if (packet->payload.pip.nr)
			cr3 |= (uint64_t) pt_pl_pip_nr;

		*pos++ = pt_opc_ext;
		*pos++ = pt_ext_pip;
		pos = pt_encode_int(pos, cr3, pt_pl_pip_size);

		encoder->pos = pos;
		return (int) (pos - begin);
	}

	case ppt_tsc:
		errcode = pt_reserve(encoder, ptps_tsc);
		if (errcode < 0)
			return errcode;

		*pos++ = pt_opc_tsc;
		pos = pt_encode_int(pos, packet->payload.tsc.tsc,
				    pt_pl_tsc_size);

		encoder->pos = pos;
		return (int) (pos - begin);

	case ppt_cbr:
		errcode = pt_reserve(encoder, ptps_cbr);
		if (errcode < 0)
			return errcode;

		*pos++ = pt_opc_ext;
		*pos++ = pt_ext_cbr;
		*pos++ = packet->payload.cbr.ratio;
		*pos++ = 0;

		encoder->pos = pos;
		return (int) (pos - begin);

	case ppt_tma: {
		uint16_t ctc, fc;

		errcode = pt_reserve(encoder, ptps_tma);
		if (errcode < 0)
			return errcode;

		ctc = packet->payload.tma.ctc;
		fc = packet->payload.tma.fc;

		if (fc & ~pt_pl_tma_fc_mask)
			return -pte_bad_packet;

		*pos++ = pt_opc_ext;
		*pos++ = pt_ext_tma;
		pos = pt_encode_int(pos, ctc, pt_pl_tma_ctc_size);
		*pos++ = 0;
		pos = pt_encode_int(pos, fc, pt_pl_tma_fc_size);

		encoder->pos = pos;
		return (int) (pos - begin);
	}

	case ppt_mtc:
		errcode = pt_reserve(encoder, ptps_mtc);
		if (errcode < 0)
			return errcode;

		*pos++ = pt_opc_mtc;
		*pos++ = packet->payload.mtc.ctc;

		encoder->pos = pos;
		return (int) (pos - begin);

	case ppt_cyc: {
		uint8_t byte[pt_pl_cyc_max_size], index, end;
		uint64_t ctc;

		ctc = (uint8_t) packet->payload.cyc.value;
		ctc <<= pt_opm_cyc_shr;

		byte[0] = pt_opc_cyc;
		byte[0] |= (uint8_t) ctc;

		ctc = packet->payload.cyc.value;
		ctc >>= (8 - pt_opm_cyc_shr);
		if (ctc)
			byte[0] |= pt_opm_cyc_ext;

		for (end = 1; ctc; ++end) {
			/* Check if the CYC payload is too big. */
			if (pt_pl_cyc_max_size <= end)
				return -pte_bad_packet;

			ctc <<= pt_opm_cycx_shr;

			byte[end] = (uint8_t) ctc;

			ctc >>= 8;
			if (ctc)
				byte[end] |= pt_opm_cycx_ext;
		}

		errcode = pt_reserve(encoder, end);
		if (errcode < 0)
			return errcode;

		for (index = 0; index < end; ++index)
			*pos++ = byte[index];

		encoder->pos = pos;
		return (int) (pos - begin);
	}

	case ppt_stop:
		errcode = pt_reserve(encoder, ptps_stop);
		if (errcode < 0)
			return errcode;

		*pos++ = pt_opc_ext;
		*pos++ = pt_ext_stop;

		encoder->pos = pos;
		return (int) (pos - begin);

	case ppt_vmcs:
		errcode = pt_reserve(encoder, ptps_vmcs);
		if (errcode < 0)
			return errcode;

		*pos++ = pt_opc_ext;
		*pos++ = pt_ext_vmcs;
		pos = pt_encode_int(pos,
				    packet->payload.vmcs.base >> pt_pl_vmcs_shl,
				    pt_pl_vmcs_size);

		encoder->pos = pos;
		return (int) (pos - begin);

	case ppt_mnt:
		errcode = pt_reserve(encoder, ptps_mnt);
		if (errcode < 0)
			return errcode;

		*pos++ = pt_opc_ext;
		*pos++ = pt_ext_ext2;
		*pos++ = pt_ext2_mnt;
		pos = pt_encode_int(pos, packet->payload.mnt.payload,
				    pt_pl_mnt_size);

		encoder->pos = pos;
		return (int) (pos - begin);

	case ppt_exstop: {
		uint8_t ext;

		errcode = pt_reserve(encoder, ptps_exstop);
		if (errcode < 0)
			return errcode;

		ext = packet->payload.exstop.ip ?
			pt_ext_exstop_ip : pt_ext_exstop;

		*pos++ = pt_opc_ext;
		*pos++ = ext;

		encoder->pos = pos;
		return (int) (pos - begin);
	}

	case ppt_mwait:
		errcode = pt_reserve(encoder, ptps_mwait);
		if (errcode < 0)
			return errcode;

		*pos++ = pt_opc_ext;
		*pos++ = pt_ext_mwait;
		pos = pt_encode_int(pos, packet->payload.mwait.hints,
				    pt_pl_mwait_hints_size);
		pos = pt_encode_int(pos, packet->payload.mwait.ext,
				    pt_pl_mwait_ext_size);

		encoder->pos = pos;
		return (int) (pos - begin);

	case ppt_pwre: {
		uint64_t payload;

		errcode = pt_reserve(encoder, ptps_pwre);
		if (errcode < 0)
			return errcode;

		payload = 0ull;
		payload |= ((uint64_t) packet->payload.pwre.state <<
			    pt_pl_pwre_state_shr) &
			(uint64_t) pt_pl_pwre_state_mask;
		payload |= ((uint64_t) packet->payload.pwre.sub_state <<
			    pt_pl_pwre_sub_state_shr) &
			(uint64_t) pt_pl_pwre_sub_state_mask;

		if (packet->payload.pwre.hw)
			payload |= (uint64_t) pt_pl_pwre_hw_mask;

		*pos++ = pt_opc_ext;
		*pos++ = pt_ext_pwre;
		pos = pt_encode_int(pos, payload, pt_pl_pwre_size);

		encoder->pos = pos;
		return (int) (pos - begin);
	}

	case ppt_pwrx: {
		uint64_t payload;

		errcode = pt_reserve(encoder, ptps_pwrx);
		if (errcode < 0)
			return errcode;

		payload = 0ull;
		payload |= ((uint64_t) packet->payload.pwrx.last <<
			    pt_pl_pwrx_last_shr) &
			(uint64_t) pt_pl_pwrx_last_mask;
		payload |= ((uint64_t) packet->payload.pwrx.deepest <<
			    pt_pl_pwrx_deepest_shr) &
			(uint64_t) pt_pl_pwrx_deepest_mask;

		if (packet->payload.pwrx.interrupt)
			payload |= (uint64_t) pt_pl_pwrx_wr_int;
		if (packet->payload.pwrx.store)
			payload |= (uint64_t) pt_pl_pwrx_wr_store;
		if (packet->payload.pwrx.autonomous)
			payload |= (uint64_t) pt_pl_pwrx_wr_hw;

		*pos++ = pt_opc_ext;
		*pos++ = pt_ext_pwrx;
		pos = pt_encode_int(pos, payload, pt_pl_pwrx_size);

		encoder->pos = pos;
		return (int) (pos - begin);
	}

	case ppt_ptw: {
		uint8_t plc, ext;
		int size;

		plc = packet->payload.ptw.plc;

		size = pt_ptw_size(plc);
		if (size < 0)
			return size;

		errcode = pt_reserve(encoder,
				     (unsigned int) (pt_opcs_ptw + size));
		if (errcode < 0)
			return errcode;

		ext = pt_ext_ptw;
		ext |= plc << pt_opm_ptw_pb_shr;

		if (packet->payload.ptw.ip)
			ext |= (uint8_t) pt_opm_ptw_ip;

		*pos++ = pt_opc_ext;
		*pos++ = ext;
		pos = pt_encode_int(pos, packet->payload.ptw.payload, size);

		encoder->pos = pos;
		return (int) (pos - begin);
	}

	case ppt_unknown:
	case ppt_invalid:
		return -pte_bad_opc;
	}

	return -pte_bad_opc;
}

int pt_encode_pad(struct pt_encoder *encoder)
{
	struct pt_packet packet;

	packet.type = ppt_pad;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_psb(struct pt_encoder *encoder)
{
	struct pt_packet packet;

	packet.type = ppt_psb;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_psbend(struct pt_encoder *encoder)
{
	struct pt_packet packet;

	packet.type = ppt_psbend;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_tip(struct pt_encoder *encoder, uint64_t ip,
		  enum pt_ip_compression ipc)
{
	struct pt_packet packet;

	packet.type = ppt_tip;
	packet.payload.ip.ip = ip;
	packet.payload.ip.ipc = ipc;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_tnt_8(struct pt_encoder *encoder, uint8_t tnt, int size)
{
	struct pt_packet packet;

	packet.type = ppt_tnt_8;
	packet.payload.tnt.bit_size = (uint8_t) size;
	packet.payload.tnt.payload = tnt;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_tnt_64(struct pt_encoder *encoder, uint64_t tnt, int size)
{
	struct pt_packet packet;

	packet.type = ppt_tnt_64;
	packet.payload.tnt.bit_size = (uint8_t) size;
	packet.payload.tnt.payload = tnt;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_tip_pge(struct pt_encoder *encoder, uint64_t ip,
		      enum pt_ip_compression ipc)
{
	struct pt_packet packet;

	packet.type = ppt_tip_pge;
	packet.payload.ip.ip = ip;
	packet.payload.ip.ipc = ipc;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_tip_pgd(struct pt_encoder *encoder, uint64_t ip,
		      enum pt_ip_compression ipc)
{
	struct pt_packet packet;

	packet.type = ppt_tip_pgd;
	packet.payload.ip.ip = ip;
	packet.payload.ip.ipc = ipc;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_fup(struct pt_encoder *encoder, uint64_t ip,
		  enum pt_ip_compression ipc)
{
	struct pt_packet packet;

	packet.type = ppt_fup;
	packet.payload.ip.ip = ip;
	packet.payload.ip.ipc = ipc;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_pip(struct pt_encoder *encoder, uint64_t cr3, uint8_t flags)
{
	struct pt_packet packet;

	packet.type = ppt_pip;
	packet.payload.pip.cr3 = cr3;
	packet.payload.pip.nr = (flags & pt_pl_pip_nr) != 0;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_ovf(struct pt_encoder *encoder)
{
	struct pt_packet packet;

	packet.type = ppt_ovf;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_mode_exec(struct pt_encoder *encoder, enum pt_exec_mode mode)
{
	struct pt_packet packet;

	packet.type = ppt_mode;
	packet.payload.mode.leaf = pt_mol_exec;
	packet.payload.mode.bits.exec = pt_set_exec_mode(mode);

	return pt_enc_next(encoder, &packet);
}


int pt_encode_mode_tsx(struct pt_encoder *encoder, uint8_t bits)
{
	struct pt_packet packet;

	packet.type = ppt_mode;
	packet.payload.mode.leaf = pt_mol_tsx;

	if (bits & pt_mob_tsx_intx)
		packet.payload.mode.bits.tsx.intx = 1;
	else
		packet.payload.mode.bits.tsx.intx = 0;

	if (bits & pt_mob_tsx_abrt)
		packet.payload.mode.bits.tsx.abrt = 1;
	else
		packet.payload.mode.bits.tsx.abrt = 0;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_tsc(struct pt_encoder *encoder, uint64_t tsc)
{
	struct pt_packet packet;

	packet.type = ppt_tsc;
	packet.payload.tsc.tsc = tsc;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_cbr(struct pt_encoder *encoder, uint8_t cbr)
{
	struct pt_packet packet;

	packet.type = ppt_cbr;
	packet.payload.cbr.ratio = cbr;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_tma(struct pt_encoder *encoder, uint16_t ctc, uint16_t fc)
{
	struct pt_packet packet;

	packet.type = ppt_tma;
	packet.payload.tma.ctc = ctc;
	packet.payload.tma.fc = fc;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_mtc(struct pt_encoder *encoder, uint8_t ctc)
{
	struct pt_packet packet;

	packet.type = ppt_mtc;
	packet.payload.mtc.ctc = ctc;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_cyc(struct pt_encoder *encoder, uint32_t ctc)
{
	struct pt_packet packet;

	packet.type = ppt_cyc;
	packet.payload.cyc.value = ctc;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_stop(struct pt_encoder *encoder)
{
	struct pt_packet packet;

	packet.type = ppt_stop;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_vmcs(struct pt_encoder *encoder, uint64_t payload)
{
	struct pt_packet packet;

	packet.type = ppt_vmcs;
	packet.payload.vmcs.base = payload;

	return pt_enc_next(encoder, &packet);
}

int pt_encode_mnt(struct pt_encoder *encoder, uint64_t payload)
{
	struct pt_packet packet;

	packet.type = ppt_mnt;
	packet.payload.mnt.payload = payload;

	return pt_enc_next(encoder, &packet);
}