1use core::{hint::unreachable_unchecked, num::NonZero};
2
3use derive_more::Display;
4
5use crate::{
6 DecoderContext, HandlePacket, PacketBlockInformation, PacketBlockSize,
7 error::{DecoderError, DecoderResult},
8};
9
10#[inline]
11fn handle_cbr_packet<H: HandlePacket>(
12 buf: &[u8],
13 _byte: u8,
14 context: &mut DecoderContext,
15 packet_handler: &mut H,
16) -> DecoderResult<(), H> {
17 let packet_length = 4;
18
19 let Some(core_bus_ratio) = buf.get(context.pos + 2) else {
20 return Err(DecoderError::UnexpectedEOF);
21 };
22 packet_handler
23 .on_cbr_packet(context, *core_bus_ratio)
24 .map_err(DecoderError::PacketHandler)?;
25
26 context.pos += packet_length;
27
28 Ok(())
29}
30
31#[inline]
32fn handle_pip_packet<H: HandlePacket>(
33 buf: &[u8],
34 _byte: u8,
35 context: &mut DecoderContext,
36 packet_handler: &mut H,
37) -> DecoderResult<(), H> {
38 let packet_length = 8;
39
40 let Some([byte2, byte3, byte4, byte5, byte6, byte7]) = buf
41 .get((context.pos + 2)..)
42 .and_then(|buf| buf.first_chunk::<6>())
43 else {
44 return Err(DecoderError::UnexpectedEOF);
45 };
46 let rsvd_nr = (*byte2 % 2) != 0;
47 let byte2 = *byte2 & 0b1111_1110; let cr3 = u64::from_le_bytes([byte2, *byte3, *byte4, *byte5, *byte6, *byte7, 0, 0]) << 5;
49
50 packet_handler
51 .on_pip_packet(context, cr3, rsvd_nr)
52 .map_err(DecoderError::PacketHandler)?;
53
54 context.pos += packet_length;
55
56 Ok(())
57}
58
59#[expect(clippy::unreadable_literal)]
60const PSB: u128 = 0x82028202820282028202820282028202;
61
62#[inline]
63fn handle_psb_packet<H: HandlePacket>(
64 buf: &[u8],
65 _byte: u8,
66 context: &mut DecoderContext,
67 packet_handler: &mut H,
68) -> DecoderResult<(), H> {
69 let packet_length = 16;
70
71 let Some(bytes) = buf
72 .get(context.pos..)
73 .and_then(|buf| buf.first_chunk::<16>())
74 else {
75 return Err(DecoderError::UnexpectedEOF);
76 };
77 let psb = u128::from_le_bytes(*bytes);
78 if psb != PSB {
79 return Err(DecoderError::InvalidPacket);
80 }
81
82 packet_handler
83 .on_psb_packet(context)
84 .map_err(DecoderError::PacketHandler)?;
85
86 context.pos += packet_length;
87
88 Ok(())
89}
90
91#[inline]
92fn handle_psbend_packet<H: HandlePacket>(
93 _buf: &[u8],
94 _byte: u8,
95 context: &mut DecoderContext,
96 packet_handler: &mut H,
97) -> DecoderResult<(), H> {
98 let packet_length = 2;
99
100 packet_handler
101 .on_psbend_packet(context)
102 .map_err(DecoderError::PacketHandler)?;
103
104 context.pos += packet_length;
105
106 Ok(())
107}
108
109#[inline]
110fn handle_trace_stop_packet<H: HandlePacket>(
111 _buf: &[u8],
112 _byte: u8,
113 context: &mut DecoderContext,
114 packet_handler: &mut H,
115) -> DecoderResult<(), H> {
116 let packet_length = 2;
117
118 packet_handler
119 .on_trace_stop_packet(context)
120 .map_err(DecoderError::PacketHandler)?;
121
122 context.pos += packet_length;
123
124 Ok(())
125}
126
127#[inline]
128fn handle_long_tnt_packet<H: HandlePacket>(
129 buf: &[u8],
130 _byte: u8,
131 context: &mut DecoderContext,
132 packet_handler: &mut H,
133) -> DecoderResult<(), H> {
134 let packet_length = 8;
135
136 let Some(bytes) = buf
137 .get(context.pos..)
138 .and_then(|buf| buf.first_chunk::<8>())
139 else {
140 return Err(DecoderError::UnexpectedEOF);
141 };
142 let packet = u64::from_le_bytes(*bytes);
143 let leading_zeros = packet.leading_zeros();
144 if leading_zeros == 64 - 16 {
145 return Err(DecoderError::InvalidPacket);
147 }
148 let packet_bytes = packet >> 16;
149 let packet_bytes = unsafe { NonZero::new_unchecked(packet_bytes) };
151
152 debug_assert!(leading_zeros <= 64 - 16 - 1, "Unexpected");
155 let highest_bit = 46u32.wrapping_sub(leading_zeros); debug_assert!(highest_bit <= 46 || highest_bit == u32::MAX, "Unexpected");
157
158 packet_handler
159 .on_long_tnt_packet(context, packet_bytes, highest_bit)
160 .map_err(DecoderError::PacketHandler)?;
161
162 context.pos += packet_length;
163
164 Ok(())
165}
166
167#[inline]
168fn handle_vmcs_packet<H: HandlePacket>(
169 buf: &[u8],
170 _byte: u8,
171 context: &mut DecoderContext,
172 packet_handler: &mut H,
173) -> DecoderResult<(), H> {
174 let packet_length = 7;
175
176 let Some([byte2, byte3, byte4, byte5, byte6]) = buf
177 .get((context.pos + 2)..)
178 .and_then(|buf| buf.first_chunk::<5>())
179 else {
180 return Err(DecoderError::UnexpectedEOF);
181 };
182 let vmcs_pointer = u64::from_le_bytes([*byte2, *byte3, *byte4, *byte5, *byte6, 0, 0, 0]) << 12;
183
184 packet_handler
185 .on_vmcs_packet(context, vmcs_pointer)
186 .map_err(DecoderError::PacketHandler)?;
187
188 context.pos += packet_length;
189
190 Ok(())
191}
192
193#[inline]
194fn handle_ovf_packet<H: HandlePacket>(
195 _buf: &[u8],
196 _byte: u8,
197 context: &mut DecoderContext,
198 packet_handler: &mut H,
199) -> DecoderResult<(), H> {
200 let packet_length = 2;
201
202 packet_handler
203 .on_ovf_packet(context)
204 .map_err(DecoderError::PacketHandler)?;
205
206 context.packet_block = None;
207 context.pos += packet_length;
208
209 Ok(())
210}
211
212#[inline]
213fn handle_mnt_packet<H: HandlePacket>(
214 buf: &[u8],
215 _byte: u8,
216 context: &mut DecoderContext,
217 packet_handler: &mut H,
218) -> DecoderResult<(), H> {
219 let packet_length = 11;
220
221 let Some(
222 [
223 byte2,
224 byte3,
225 byte4,
226 byte5,
227 byte6,
228 byte7,
229 byte8,
230 byte9,
231 byte10,
232 ],
233 ) = buf
234 .get((context.pos + 2)..)
235 .and_then(|buf| buf.first_chunk::<9>())
236 else {
237 return Err(DecoderError::UnexpectedEOF);
238 };
239 if *byte2 != 0b1000_1000 {
240 return Err(DecoderError::UnexpectedEOF);
241 }
242 let payload = u64::from_le_bytes([
243 *byte3, *byte4, *byte5, *byte6, *byte7, *byte8, *byte9, *byte10,
244 ]);
245
246 packet_handler
247 .on_mnt_packet(context, payload)
248 .map_err(DecoderError::PacketHandler)?;
249
250 context.pos += packet_length;
251
252 Ok(())
253}
254
255#[inline]
256fn handle_tma_packet<H: HandlePacket>(
257 buf: &[u8],
258 _byte: u8,
259 context: &mut DecoderContext,
260 packet_handler: &mut H,
261) -> DecoderResult<(), H> {
262 let packet_length = 7;
263
264 let Some([byte2, byte3, _byte4, byte5, byte6]) = buf.get((context.pos + 2)..(context.pos + 7))
265 else {
266 return Err(DecoderError::UnexpectedEOF);
267 };
268
269 let ctc = u16::from_le_bytes([*byte2, *byte3]);
270 let fast_counter = *byte5;
271 let fc8 = *byte6 % 2 != 0;
272
273 packet_handler
274 .on_tma_packet(context, ctc, fast_counter, fc8)
275 .map_err(DecoderError::PacketHandler)?;
276
277 context.pos += packet_length;
278
279 Ok(())
280}
281
282#[derive(Debug, Display, Clone, Copy)]
284pub enum PtwPayload {
285 #[display("FourBytes({_0:#x})")]
287 FourBytes(u32),
288 #[display("EightBytes({_0:#x})")]
290 EightBytes(u64),
291}
292
293#[inline]
294fn handle_ptw_packet<H: HandlePacket>(
295 buf: &[u8],
296 byte: u8,
297 context: &mut DecoderContext,
298 packet_handler: &mut H,
299) -> DecoderResult<(), H> {
300 let packet_length;
301
302 let ip_bit = (byte & 0b1000_0000) != 0;
303 let payload_bytes = (byte & 0b0110_0000) >> 5;
304 debug_assert!(payload_bytes <= 0b11, "Unexpected");
305 let payload = match payload_bytes {
306 0b00 => {
307 packet_length = 6;
308
309 let Some(bytes) = buf
310 .get((context.pos + 2)..)
311 .and_then(|buf| buf.first_chunk::<4>())
312 else {
313 return Err(DecoderError::UnexpectedEOF);
314 };
315 let payload = u32::from_le_bytes(*bytes);
316 PtwPayload::FourBytes(payload)
317 }
318 0b01 => {
319 packet_length = 10;
320
321 let Some(bytes) = buf
322 .get((context.pos + 2)..)
323 .and_then(|buf| buf.first_chunk::<8>())
324 else {
325 return Err(DecoderError::UnexpectedEOF);
326 };
327 let payload = u64::from_le_bytes(*bytes);
328 PtwPayload::EightBytes(payload)
329 }
330 0b10 | 0b11 => {
331 return Err(DecoderError::InvalidPacket);
332 }
333 _ => {
334 unsafe {
336 unreachable_unchecked();
337 }
338 }
339 };
340
341 packet_handler
342 .on_ptw_packet(context, ip_bit, payload)
343 .map_err(DecoderError::PacketHandler)?;
344
345 context.pos += packet_length;
346
347 Ok(())
348}
349
350#[inline]
351fn handle_exstop_packet<H: HandlePacket>(
352 _buf: &[u8],
353 byte: u8,
354 context: &mut DecoderContext,
355 packet_handler: &mut H,
356) -> DecoderResult<(), H> {
357 let packet_length = 2;
358
359 let ip_bit = (byte & 0b1000_0000) != 0;
360
361 packet_handler
362 .on_exstop_packet(context, ip_bit)
363 .map_err(DecoderError::PacketHandler)?;
364
365 context.pos += packet_length;
366
367 Ok(())
368}
369
370#[inline]
371fn handle_mwait_packet<H: HandlePacket>(
372 buf: &[u8],
373 _byte: u8,
374 context: &mut DecoderContext,
375 packet_handler: &mut H,
376) -> DecoderResult<(), H> {
377 let packet_length = 10;
378
379 let Some(mwait_hints) = buf.get(context.pos + 2) else {
380 return Err(DecoderError::UnexpectedEOF);
381 };
382 let Some(ext) = buf.get(context.pos + 2) else {
383 return Err(DecoderError::UnexpectedEOF);
384 };
385 let ext = *ext & 0b0000_0011;
386
387 packet_handler
388 .on_mwait_packet(context, *mwait_hints, ext)
389 .map_err(DecoderError::PacketHandler)?;
390
391 context.pos += packet_length;
392
393 Ok(())
394}
395
396#[inline]
397fn handle_pwre_packet<H: HandlePacket>(
398 buf: &[u8],
399 _byte: u8,
400 context: &mut DecoderContext,
401 packet_handler: &mut H,
402) -> DecoderResult<(), H> {
403 let packet_length = 4;
404
405 let Some([byte2, byte3]) = buf.get((context.pos + 2)..(context.pos + 4)) else {
406 return Err(DecoderError::UnexpectedEOF);
407 };
408 let hw = (*byte2 & 0b1000_0000) != 0;
409 let resolved_thread_c_state = (*byte3 & 0b1111_0000) >> 4;
410 let resolved_thread_sub_c_state = *byte3 & 0b0000_1111;
411
412 packet_handler
413 .on_pwre_packet(
414 context,
415 hw,
416 resolved_thread_c_state,
417 resolved_thread_sub_c_state,
418 )
419 .map_err(DecoderError::PacketHandler)?;
420
421 context.pos += packet_length;
422
423 Ok(())
424}
425
426#[inline]
427fn handle_pwrx_packet<H: HandlePacket>(
428 buf: &[u8],
429 _byte: u8,
430 context: &mut DecoderContext,
431 packet_handler: &mut H,
432) -> DecoderResult<(), H> {
433 let packet_length = 7;
434
435 let Some([byte2, byte3]) = buf.get((context.pos + 2)..(context.pos + 4)) else {
436 return Err(DecoderError::UnexpectedEOF);
437 };
438 let last_core_c_state = (*byte2 & 0b1111_0000) >> 4;
439 let deepest_core_c_state = *byte2 & 0b0000_1111;
440 let wake_reason = *byte3 & 0b0000_1111;
441
442 packet_handler
443 .on_pwrx_packet(
444 context,
445 last_core_c_state,
446 deepest_core_c_state,
447 wake_reason,
448 )
449 .map_err(DecoderError::PacketHandler)?;
450
451 context.pos += packet_length;
452
453 Ok(())
454}
455
456#[inline]
457fn handle_cfe_packet<H: HandlePacket>(
458 buf: &[u8],
459 byte: u8,
460 context: &mut DecoderContext,
461 packet_handler: &mut H,
462) -> DecoderResult<(), H> {
463 let packet_length = 2;
464
465 let ip_bit = (byte & 0b1000_0000) != 0;
466 let r#type = byte & 0b0001_1111;
467 let Some(vector) = buf.get(context.pos + 3) else {
468 return Err(DecoderError::UnexpectedEOF);
469 };
470
471 packet_handler
472 .on_cfe_packet(context, ip_bit, r#type, *vector)
473 .map_err(DecoderError::PacketHandler)?;
474
475 context.pos += packet_length;
476
477 Ok(())
478}
479
480#[inline]
481fn handle_evd_packet<H: HandlePacket>(
482 buf: &[u8],
483 _byte: u8,
484 context: &mut DecoderContext,
485 packet_handler: &mut H,
486) -> DecoderResult<(), H> {
487 let packet_length = 11;
488
489 let Some(
490 [
491 byte2,
492 byte3,
493 byte4,
494 byte5,
495 byte6,
496 byte7,
497 byte8,
498 byte9,
499 byte10,
500 ],
501 ) = buf.get((context.pos + 2)..(context.pos + 11))
502 else {
503 return Err(DecoderError::UnexpectedEOF);
504 };
505 let r#type = byte2 & 0b001_1111;
506 let payload = u64::from_le_bytes([
507 *byte3, *byte4, *byte5, *byte6, *byte7, *byte8, *byte9, *byte10,
508 ]);
509
510 packet_handler
511 .on_evd_packet(context, r#type, payload)
512 .map_err(DecoderError::PacketHandler)?;
513
514 context.pos += packet_length;
515
516 Ok(())
517}
518
519#[inline]
520fn handle_bbp_packet<H: HandlePacket>(
521 buf: &[u8],
522 _byte: u8,
523 context: &mut DecoderContext,
524 packet_handler: &mut H,
525) -> DecoderResult<(), H> {
526 let packet_length = 3;
527
528 let Some(byte) = buf.get(context.pos + 2) else {
529 return Err(DecoderError::UnexpectedEOF);
530 };
531 let sz_bit = (*byte & 0b1000_0000) != 0;
532 let size = PacketBlockSize::from_sz_bit(sz_bit);
533 let r#type = *byte & 0b0001_1111;
534 packet_handler
535 .on_bbp_packet(context, sz_bit, r#type)
536 .map_err(DecoderError::PacketHandler)?;
537
538 context.packet_block = Some(PacketBlockInformation { size, r#type });
539 context.pos += packet_length;
540
541 Ok(())
542}
543
544#[inline]
545fn handle_bep_packet<H: HandlePacket>(
546 _buf: &[u8],
547 byte: u8,
548 context: &mut DecoderContext,
549 packet_handler: &mut H,
550) -> DecoderResult<(), H> {
551 let packet_length = 2;
552
553 let ip_bit = (byte & 0b1000_0000) != 0;
554 packet_handler
555 .on_bep_packet(context, ip_bit)
556 .map_err(DecoderError::PacketHandler)?;
557
558 context.packet_block = None;
559 context.pos += packet_length;
560
561 Ok(())
562}
563
564#[inline]
565pub fn decode<H: HandlePacket>(
566 buf: &[u8],
567 context: &mut DecoderContext,
568 packet_handler: &mut H,
569) -> DecoderResult<(), H> {
570 let Some(byte) = buf.get(context.pos + 1) else {
572 return Err(DecoderError::UnexpectedEOF);
573 };
574 let byte = *byte;
575
576 match byte {
577 0b0000_0011 => {
578 handle_cbr_packet(buf, byte, context, packet_handler)?;
579 }
580 0b0001_0010 | 0b0011_0010 | 0b0101_0010 | 0b0111_0010 | 0b1001_0010 | 0b1011_0010
581 | 0b1101_0010 | 0b1111_0010 => {
582 handle_ptw_packet(buf, byte, context, packet_handler)?;
584 }
585 0b0001_0011 => {
586 handle_cfe_packet(buf, byte, context, packet_handler)?;
587 }
588 0b0010_0010 => {
589 handle_pwre_packet(buf, byte, context, packet_handler)?;
590 }
591 0b0010_0011 => {
592 handle_psbend_packet(buf, byte, context, packet_handler)?;
593 }
594 0b0011_0011 | 0b1011_0011 => {
595 handle_bep_packet(buf, byte, context, packet_handler)?;
597 }
598 0b0100_0011 => {
599 handle_pip_packet(buf, byte, context, packet_handler)?;
600 }
601 0b0101_0011 => {
602 handle_evd_packet(buf, byte, context, packet_handler)?;
603 }
604 0b0110_0010 | 0b1110_0010 => {
605 handle_exstop_packet(buf, byte, context, packet_handler)?;
607 }
608 0b0110_0011 => {
609 handle_bbp_packet(buf, byte, context, packet_handler)?;
610 }
611 0b0111_0011 => {
612 handle_tma_packet(buf, byte, context, packet_handler)?;
613 }
614 0b1000_0010 => {
615 handle_psb_packet(buf, byte, context, packet_handler)?;
616 }
617 0b1000_0011 => {
618 handle_trace_stop_packet(buf, byte, context, packet_handler)?;
619 }
620 0b1010_0010 => {
621 handle_pwrx_packet(buf, byte, context, packet_handler)?;
622 }
623 0b1010_0011 => {
624 handle_long_tnt_packet(buf, byte, context, packet_handler)?;
625 }
626 0b1100_0010 => {
627 handle_mwait_packet(buf, byte, context, packet_handler)?;
628 }
629 0b1100_1000 => {
630 handle_vmcs_packet(buf, byte, context, packet_handler)?;
631 }
632 0b1111_0011 => {
633 handle_ovf_packet(buf, byte, context, packet_handler)?;
634 }
635 0b1100_0011 => {
636 handle_mnt_packet(buf, byte, context, packet_handler)?;
637 }
638 _ => {
639 return Err(DecoderError::InvalidPacket);
640 }
641 }
642
643 Ok(())
644}