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, "Invalid long TNT packet"); let highest_bit = 46u32.wrapping_sub(leading_zeros); debug_assert!(highest_bit <= 46 || highest_bit == u32::MAX, "Unexpected");
155
156 packet_handler
157 .on_long_tnt_packet(context, packet_bytes, highest_bit)
158 .map_err(DecoderError::PacketHandler)?;
159
160 context.pos += packet_length;
161
162 Ok(())
163}
164
165#[inline]
166fn handle_vmcs_packet<H: HandlePacket>(
167 buf: &[u8],
168 _byte: u8,
169 context: &mut DecoderContext,
170 packet_handler: &mut H,
171) -> DecoderResult<(), H> {
172 let packet_length = 7;
173
174 let Some([byte2, byte3, byte4, byte5, byte6]) = buf
175 .get((context.pos + 2)..)
176 .and_then(|buf| buf.first_chunk::<5>())
177 else {
178 return Err(DecoderError::UnexpectedEOF);
179 };
180 let vmcs_pointer = u64::from_le_bytes([*byte2, *byte3, *byte4, *byte5, *byte6, 0, 0, 0]) << 12;
181
182 packet_handler
183 .on_vmcs_packet(context, vmcs_pointer)
184 .map_err(DecoderError::PacketHandler)?;
185
186 context.pos += packet_length;
187
188 Ok(())
189}
190
191#[inline]
192fn handle_ovf_packet<H: HandlePacket>(
193 _buf: &[u8],
194 _byte: u8,
195 context: &mut DecoderContext,
196 packet_handler: &mut H,
197) -> DecoderResult<(), H> {
198 let packet_length = 2;
199
200 packet_handler
201 .on_ovf_packet(context)
202 .map_err(DecoderError::PacketHandler)?;
203
204 context.packet_block = None;
205 context.pos += packet_length;
206
207 Ok(())
208}
209
210#[inline]
211fn handle_mnt_packet<H: HandlePacket>(
212 buf: &[u8],
213 _byte: u8,
214 context: &mut DecoderContext,
215 packet_handler: &mut H,
216) -> DecoderResult<(), H> {
217 let packet_length = 11;
218
219 let Some(
220 [
221 byte2,
222 byte3,
223 byte4,
224 byte5,
225 byte6,
226 byte7,
227 byte8,
228 byte9,
229 byte10,
230 ],
231 ) = buf
232 .get((context.pos + 2)..)
233 .and_then(|buf| buf.first_chunk::<9>())
234 else {
235 return Err(DecoderError::UnexpectedEOF);
236 };
237 if *byte2 != 0b1000_1000 {
238 return Err(DecoderError::UnexpectedEOF);
239 }
240 let payload = u64::from_le_bytes([
241 *byte3, *byte4, *byte5, *byte6, *byte7, *byte8, *byte9, *byte10,
242 ]);
243
244 packet_handler
245 .on_mnt_packet(context, payload)
246 .map_err(DecoderError::PacketHandler)?;
247
248 context.pos += packet_length;
249
250 Ok(())
251}
252
253#[inline]
254fn handle_tma_packet<H: HandlePacket>(
255 buf: &[u8],
256 _byte: u8,
257 context: &mut DecoderContext,
258 packet_handler: &mut H,
259) -> DecoderResult<(), H> {
260 let packet_length = 7;
261
262 let Some([byte2, byte3, _byte4, byte5, byte6]) = buf.get((context.pos + 2)..(context.pos + 7))
263 else {
264 return Err(DecoderError::UnexpectedEOF);
265 };
266
267 let ctc = u16::from_le_bytes([*byte2, *byte3]);
268 let fast_counter = *byte5;
269 let fc8 = *byte6 % 2 != 0;
270
271 packet_handler
272 .on_tma_packet(context, ctc, fast_counter, fc8)
273 .map_err(DecoderError::PacketHandler)?;
274
275 context.pos += packet_length;
276
277 Ok(())
278}
279
280#[derive(Debug, Display, Clone, Copy)]
282pub enum PtwPayload {
283 #[display("FourBytes({_0:#x})")]
285 FourBytes(u32),
286 #[display("EightBytes({_0:#x})")]
288 EightBytes(u64),
289}
290
291#[inline]
292fn handle_ptw_packet<H: HandlePacket>(
293 buf: &[u8],
294 byte: u8,
295 context: &mut DecoderContext,
296 packet_handler: &mut H,
297) -> DecoderResult<(), H> {
298 let packet_length;
299
300 let ip_bit = (byte & 0b1000_0000) != 0;
301 let payload_bytes = (byte & 0b0110_0000) >> 5;
302 debug_assert!(payload_bytes <= 0b11, "Unexpected");
303 let payload = match payload_bytes {
304 0b00 => {
305 packet_length = 6;
306
307 let Some(bytes) = buf
308 .get((context.pos + 2)..)
309 .and_then(|buf| buf.first_chunk::<4>())
310 else {
311 return Err(DecoderError::UnexpectedEOF);
312 };
313 let payload = u32::from_le_bytes(*bytes);
314 PtwPayload::FourBytes(payload)
315 }
316 0b01 => {
317 packet_length = 10;
318
319 let Some(bytes) = buf
320 .get((context.pos + 2)..)
321 .and_then(|buf| buf.first_chunk::<8>())
322 else {
323 return Err(DecoderError::UnexpectedEOF);
324 };
325 let payload = u64::from_le_bytes(*bytes);
326 PtwPayload::EightBytes(payload)
327 }
328 0b10 | 0b11 => {
329 return Err(DecoderError::InvalidPacket);
330 }
331 _ => {
332 unsafe {
334 unreachable_unchecked();
335 }
336 }
337 };
338
339 packet_handler
340 .on_ptw_packet(context, ip_bit, payload)
341 .map_err(DecoderError::PacketHandler)?;
342
343 context.pos += packet_length;
344
345 Ok(())
346}
347
348#[inline]
349fn handle_exstop_packet<H: HandlePacket>(
350 _buf: &[u8],
351 byte: u8,
352 context: &mut DecoderContext,
353 packet_handler: &mut H,
354) -> DecoderResult<(), H> {
355 let packet_length = 2;
356
357 let ip_bit = (byte & 0b1000_0000) != 0;
358
359 packet_handler
360 .on_exstop_packet(context, ip_bit)
361 .map_err(DecoderError::PacketHandler)?;
362
363 context.pos += packet_length;
364
365 Ok(())
366}
367
368#[inline]
369fn handle_mwait_packet<H: HandlePacket>(
370 buf: &[u8],
371 _byte: u8,
372 context: &mut DecoderContext,
373 packet_handler: &mut H,
374) -> DecoderResult<(), H> {
375 let packet_length = 10;
376
377 let Some(mwait_hints) = buf.get(context.pos + 2) else {
378 return Err(DecoderError::UnexpectedEOF);
379 };
380 let Some(ext) = buf.get(context.pos + 2) else {
381 return Err(DecoderError::UnexpectedEOF);
382 };
383 let ext = *ext & 0b0000_0011;
384
385 packet_handler
386 .on_mwait_packet(context, *mwait_hints, ext)
387 .map_err(DecoderError::PacketHandler)?;
388
389 context.pos += packet_length;
390
391 Ok(())
392}
393
394#[inline]
395fn handle_pwre_packet<H: HandlePacket>(
396 buf: &[u8],
397 _byte: u8,
398 context: &mut DecoderContext,
399 packet_handler: &mut H,
400) -> DecoderResult<(), H> {
401 let packet_length = 4;
402
403 let Some([byte2, byte3]) = buf.get((context.pos + 2)..(context.pos + 4)) else {
404 return Err(DecoderError::UnexpectedEOF);
405 };
406 let hw = (*byte2 & 0b1000_0000) != 0;
407 let resolved_thread_c_state = (*byte3 & 0b1111_0000) >> 4;
408 let resolved_thread_sub_c_state = *byte3 & 0b0000_1111;
409
410 packet_handler
411 .on_pwre_packet(
412 context,
413 hw,
414 resolved_thread_c_state,
415 resolved_thread_sub_c_state,
416 )
417 .map_err(DecoderError::PacketHandler)?;
418
419 context.pos += packet_length;
420
421 Ok(())
422}
423
424#[inline]
425fn handle_pwrx_packet<H: HandlePacket>(
426 buf: &[u8],
427 _byte: u8,
428 context: &mut DecoderContext,
429 packet_handler: &mut H,
430) -> DecoderResult<(), H> {
431 let packet_length = 7;
432
433 let Some([byte2, byte3]) = buf.get((context.pos + 2)..(context.pos + 4)) else {
434 return Err(DecoderError::UnexpectedEOF);
435 };
436 let last_core_c_state = (*byte2 & 0b1111_0000) >> 4;
437 let deepest_core_c_state = *byte2 & 0b0000_1111;
438 let wake_reason = *byte3 & 0b0000_1111;
439
440 packet_handler
441 .on_pwrx_packet(
442 context,
443 last_core_c_state,
444 deepest_core_c_state,
445 wake_reason,
446 )
447 .map_err(DecoderError::PacketHandler)?;
448
449 context.pos += packet_length;
450
451 Ok(())
452}
453
454#[inline]
455fn handle_cfe_packet<H: HandlePacket>(
456 buf: &[u8],
457 byte: u8,
458 context: &mut DecoderContext,
459 packet_handler: &mut H,
460) -> DecoderResult<(), H> {
461 let packet_length = 2;
462
463 let ip_bit = (byte & 0b1000_0000) != 0;
464 let r#type = byte & 0b0001_1111;
465 let Some(vector) = buf.get(context.pos + 3) else {
466 return Err(DecoderError::UnexpectedEOF);
467 };
468
469 packet_handler
470 .on_cfe_packet(context, ip_bit, r#type, *vector)
471 .map_err(DecoderError::PacketHandler)?;
472
473 context.pos += packet_length;
474
475 Ok(())
476}
477
478#[inline]
479fn handle_evd_packet<H: HandlePacket>(
480 buf: &[u8],
481 _byte: u8,
482 context: &mut DecoderContext,
483 packet_handler: &mut H,
484) -> DecoderResult<(), H> {
485 let packet_length = 11;
486
487 let Some(
488 [
489 byte2,
490 byte3,
491 byte4,
492 byte5,
493 byte6,
494 byte7,
495 byte8,
496 byte9,
497 byte10,
498 ],
499 ) = buf.get((context.pos + 2)..(context.pos + 11))
500 else {
501 return Err(DecoderError::UnexpectedEOF);
502 };
503 let r#type = byte2 & 0b001_1111;
504 let payload = u64::from_le_bytes([
505 *byte3, *byte4, *byte5, *byte6, *byte7, *byte8, *byte9, *byte10,
506 ]);
507
508 packet_handler
509 .on_evd_packet(context, r#type, payload)
510 .map_err(DecoderError::PacketHandler)?;
511
512 context.pos += packet_length;
513
514 Ok(())
515}
516
517#[inline]
518fn handle_bbp_packet<H: HandlePacket>(
519 buf: &[u8],
520 _byte: u8,
521 context: &mut DecoderContext,
522 packet_handler: &mut H,
523) -> DecoderResult<(), H> {
524 let packet_length = 3;
525
526 let Some(byte) = buf.get(context.pos + 2) else {
527 return Err(DecoderError::UnexpectedEOF);
528 };
529 let sz_bit = (*byte & 0b1000_0000) != 0;
530 let size = PacketBlockSize::from_sz_bit(sz_bit);
531 let r#type = *byte & 0b0001_1111;
532 packet_handler
533 .on_bbp_packet(context, sz_bit, r#type)
534 .map_err(DecoderError::PacketHandler)?;
535
536 context.packet_block = Some(PacketBlockInformation { size, r#type });
537 context.pos += packet_length;
538
539 Ok(())
540}
541
542#[inline]
543fn handle_bep_packet<H: HandlePacket>(
544 _buf: &[u8],
545 byte: u8,
546 context: &mut DecoderContext,
547 packet_handler: &mut H,
548) -> DecoderResult<(), H> {
549 let packet_length = 2;
550
551 let ip_bit = (byte & 0b1000_0000) != 0;
552 packet_handler
553 .on_bep_packet(context, ip_bit)
554 .map_err(DecoderError::PacketHandler)?;
555
556 context.packet_block = None;
557 context.pos += packet_length;
558
559 Ok(())
560}
561
562#[inline]
563pub fn decode<H: HandlePacket>(
564 buf: &[u8],
565 context: &mut DecoderContext,
566 packet_handler: &mut H,
567) -> DecoderResult<(), H> {
568 let Some(byte) = buf.get(context.pos + 1) else {
570 return Err(DecoderError::UnexpectedEOF);
571 };
572 let byte = *byte;
573
574 match byte {
575 0b0000_0011 => {
576 handle_cbr_packet(buf, byte, context, packet_handler)?;
577 }
578 0b0001_0010 | 0b0011_0010 | 0b0101_0010 | 0b0111_0010 | 0b1001_0010 | 0b1011_0010
579 | 0b1101_0010 | 0b1111_0010 => {
580 handle_ptw_packet(buf, byte, context, packet_handler)?;
582 }
583 0b0001_0011 => {
584 handle_cfe_packet(buf, byte, context, packet_handler)?;
585 }
586 0b0010_0010 => {
587 handle_pwre_packet(buf, byte, context, packet_handler)?;
588 }
589 0b0010_0011 => {
590 handle_psbend_packet(buf, byte, context, packet_handler)?;
591 }
592 0b0011_0011 | 0b1011_0011 => {
593 handle_bep_packet(buf, byte, context, packet_handler)?;
595 }
596 0b0100_0011 => {
597 handle_pip_packet(buf, byte, context, packet_handler)?;
598 }
599 0b0101_0011 => {
600 handle_evd_packet(buf, byte, context, packet_handler)?;
601 }
602 0b0110_0010 | 0b1110_0010 => {
603 handle_exstop_packet(buf, byte, context, packet_handler)?;
605 }
606 0b0110_0011 => {
607 handle_bbp_packet(buf, byte, context, packet_handler)?;
608 }
609 0b0111_0011 => {
610 handle_tma_packet(buf, byte, context, packet_handler)?;
611 }
612 0b1000_0010 => {
613 handle_psb_packet(buf, byte, context, packet_handler)?;
614 }
615 0b1000_0011 => {
616 handle_trace_stop_packet(buf, byte, context, packet_handler)?;
617 }
618 0b1010_0010 => {
619 handle_pwrx_packet(buf, byte, context, packet_handler)?;
620 }
621 0b1010_0011 => {
622 handle_long_tnt_packet(buf, byte, context, packet_handler)?;
623 }
624 0b1100_0010 => {
625 handle_mwait_packet(buf, byte, context, packet_handler)?;
626 }
627 0b1100_1000 => {
628 handle_vmcs_packet(buf, byte, context, packet_handler)?;
629 }
630 0b1111_0011 => {
631 handle_ovf_packet(buf, byte, context, packet_handler)?;
632 }
633 0b1100_0011 => {
634 handle_mnt_packet(buf, byte, context, packet_handler)?;
635 }
636 _ => {
637 return Err(DecoderError::InvalidPacket);
638 }
639 }
640
641 Ok(())
642}