1use super::SegmentRegister;
7use super::TableRegister;
8use super::X86PartitionCapabilities;
9use crate::state::HvRegisterState;
10use crate::state::StateElement;
11use crate::state::state_trait;
12use hvdef::HV_MESSAGE_SIZE;
13use hvdef::HvInternalActivityRegister;
14use hvdef::HvMessage;
15use hvdef::HvRegisterValue;
16use hvdef::HvX64InterruptStateRegister;
17use hvdef::HvX64PendingEventReg0;
18use hvdef::HvX64PendingExceptionEvent;
19use hvdef::HvX64PendingExtIntEvent;
20use hvdef::HvX64PendingInterruptionRegister;
21use hvdef::HvX64PendingInterruptionType;
22use hvdef::HvX64RegisterName;
23use hvdef::HvX64SegmentRegister;
24use hvdef::HvX64TableRegister;
25use inspect::Inspect;
26use mesh_protobuf::Protobuf;
27use std::fmt::Debug;
28use vm_topology::processor::x86::X86VpInfo;
29use x86defs::RFlags;
30use x86defs::X64_CR0_CD;
31use x86defs::X64_CR0_ET;
32use x86defs::X64_CR0_NW;
33use x86defs::X64_EFER_NXE;
34use x86defs::X86X_MSR_DEFAULT_PAT;
35use x86defs::apic::APIC_BASE_PAGE;
36use x86defs::apic::ApicBase;
37use x86defs::apic::ApicVersion;
38use x86defs::xsave::DEFAULT_MXCSR;
39use x86defs::xsave::Fxsave;
40use x86defs::xsave::INIT_FCW;
41use x86defs::xsave::XCOMP_COMPRESSED;
42use x86defs::xsave::XFEATURE_SSE;
43use x86defs::xsave::XFEATURE_X87;
44use x86defs::xsave::XFEATURE_YMM;
45use x86defs::xsave::XSAVE_LEGACY_LEN;
46use x86defs::xsave::XSAVE_VARIABLE_OFFSET;
47use x86defs::xsave::XsaveHeader;
48use zerocopy::FromBytes;
49use zerocopy::FromZeros;
50use zerocopy::Immutable;
51use zerocopy::IntoBytes;
52use zerocopy::KnownLayout;
53use zerocopy::Ref;
54
55#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Protobuf, Inspect)]
56#[mesh(package = "virt.x86")]
57pub struct Registers {
58 #[inspect(hex)]
59 #[mesh(1)]
60 pub rax: u64,
61 #[inspect(hex)]
62 #[mesh(2)]
63 pub rcx: u64,
64 #[inspect(hex)]
65 #[mesh(3)]
66 pub rdx: u64,
67 #[inspect(hex)]
68 #[mesh(4)]
69 pub rbx: u64,
70 #[inspect(hex)]
71 #[mesh(5)]
72 pub rsp: u64,
73 #[inspect(hex)]
74 #[mesh(6)]
75 pub rbp: u64,
76 #[inspect(hex)]
77 #[mesh(7)]
78 pub rsi: u64,
79 #[inspect(hex)]
80 #[mesh(8)]
81 pub rdi: u64,
82 #[inspect(hex)]
83 #[mesh(9)]
84 pub r8: u64,
85 #[inspect(hex)]
86 #[mesh(10)]
87 pub r9: u64,
88 #[inspect(hex)]
89 #[mesh(11)]
90 pub r10: u64,
91 #[inspect(hex)]
92 #[mesh(12)]
93 pub r11: u64,
94 #[inspect(hex)]
95 #[mesh(13)]
96 pub r12: u64,
97 #[inspect(hex)]
98 #[mesh(14)]
99 pub r13: u64,
100 #[inspect(hex)]
101 #[mesh(15)]
102 pub r14: u64,
103 #[inspect(hex)]
104 #[mesh(16)]
105 pub r15: u64,
106 #[inspect(hex)]
107 #[mesh(17)]
108 pub rip: u64,
109 #[inspect(hex)]
110 #[mesh(18)]
111 pub rflags: u64,
112 #[mesh(19)]
113 pub cs: SegmentRegister,
114 #[mesh(20)]
115 pub ds: SegmentRegister,
116 #[mesh(21)]
117 pub es: SegmentRegister,
118 #[mesh(22)]
119 pub fs: SegmentRegister,
120 #[mesh(23)]
121 pub gs: SegmentRegister,
122 #[mesh(24)]
123 pub ss: SegmentRegister,
124 #[mesh(25)]
125 pub tr: SegmentRegister,
126 #[mesh(26)]
127 pub ldtr: SegmentRegister,
128 #[mesh(27)]
129 pub gdtr: TableRegister,
130 #[mesh(28)]
131 pub idtr: TableRegister,
132 #[inspect(hex)]
133 #[mesh(29)]
134 pub cr0: u64,
135 #[inspect(hex)]
136 #[mesh(30)]
137 pub cr2: u64,
138 #[inspect(hex)]
139 #[mesh(31)]
140 pub cr3: u64,
141 #[inspect(hex)]
142 #[mesh(32)]
143 pub cr4: u64,
144 #[inspect(hex)]
145 #[mesh(33)]
146 pub cr8: u64,
147 #[inspect(hex)]
148 #[mesh(34)]
149 pub efer: u64,
150}
151
152impl HvRegisterState<HvX64RegisterName, 34> for Registers {
153 fn names(&self) -> &'static [HvX64RegisterName; 34] {
154 &[
155 HvX64RegisterName::Rax,
156 HvX64RegisterName::Rcx,
157 HvX64RegisterName::Rdx,
158 HvX64RegisterName::Rbx,
159 HvX64RegisterName::Rsp,
160 HvX64RegisterName::Rbp,
161 HvX64RegisterName::Rsi,
162 HvX64RegisterName::Rdi,
163 HvX64RegisterName::R8,
164 HvX64RegisterName::R9,
165 HvX64RegisterName::R10,
166 HvX64RegisterName::R11,
167 HvX64RegisterName::R12,
168 HvX64RegisterName::R13,
169 HvX64RegisterName::R14,
170 HvX64RegisterName::R15,
171 HvX64RegisterName::Rip,
172 HvX64RegisterName::Rflags,
173 HvX64RegisterName::Cr0,
174 HvX64RegisterName::Cr2,
175 HvX64RegisterName::Cr3,
176 HvX64RegisterName::Cr4,
177 HvX64RegisterName::Cr8,
178 HvX64RegisterName::Efer,
179 HvX64RegisterName::Cs,
180 HvX64RegisterName::Ds,
181 HvX64RegisterName::Es,
182 HvX64RegisterName::Fs,
183 HvX64RegisterName::Gs,
184 HvX64RegisterName::Ss,
185 HvX64RegisterName::Tr,
186 HvX64RegisterName::Ldtr,
187 HvX64RegisterName::Gdtr,
188 HvX64RegisterName::Idtr,
189 ]
190 }
191
192 fn get_values<'a>(&self, it: impl Iterator<Item = &'a mut HvRegisterValue>) {
193 for (dest, src) in it.zip([
194 self.rax.into(),
195 self.rcx.into(),
196 self.rdx.into(),
197 self.rbx.into(),
198 self.rsp.into(),
199 self.rbp.into(),
200 self.rsi.into(),
201 self.rdi.into(),
202 self.r8.into(),
203 self.r9.into(),
204 self.r10.into(),
205 self.r11.into(),
206 self.r12.into(),
207 self.r13.into(),
208 self.r14.into(),
209 self.r15.into(),
210 self.rip.into(),
211 self.rflags.into(),
212 self.cr0.into(),
213 self.cr2.into(),
214 self.cr3.into(),
215 self.cr4.into(),
216 self.cr8.into(),
217 self.efer.into(),
218 HvX64SegmentRegister::from(self.cs).into(),
219 HvX64SegmentRegister::from(self.ds).into(),
220 HvX64SegmentRegister::from(self.es).into(),
221 HvX64SegmentRegister::from(self.fs).into(),
222 HvX64SegmentRegister::from(self.gs).into(),
223 HvX64SegmentRegister::from(self.ss).into(),
224 HvX64SegmentRegister::from(self.tr).into(),
225 HvX64SegmentRegister::from(self.ldtr).into(),
226 HvX64TableRegister::from(self.gdtr).into(),
227 HvX64TableRegister::from(self.idtr).into(),
228 ]) {
229 *dest = src;
230 }
231 }
232
233 fn set_values(&mut self, mut it: impl Iterator<Item = HvRegisterValue>) {
234 for (dest, src) in [
235 &mut self.rax,
236 &mut self.rcx,
237 &mut self.rdx,
238 &mut self.rbx,
239 &mut self.rsp,
240 &mut self.rbp,
241 &mut self.rsi,
242 &mut self.rdi,
243 &mut self.r8,
244 &mut self.r9,
245 &mut self.r10,
246 &mut self.r11,
247 &mut self.r12,
248 &mut self.r13,
249 &mut self.r14,
250 &mut self.r15,
251 &mut self.rip,
252 &mut self.rflags,
253 &mut self.cr0,
254 &mut self.cr2,
255 &mut self.cr3,
256 &mut self.cr4,
257 &mut self.cr8,
258 &mut self.efer,
259 ]
260 .into_iter()
261 .zip(&mut it)
262 {
263 *dest = src.as_u64();
264 }
265
266 for (dest, src) in [
267 &mut self.cs,
268 &mut self.ds,
269 &mut self.es,
270 &mut self.fs,
271 &mut self.gs,
272 &mut self.ss,
273 &mut self.tr,
274 &mut self.ldtr,
275 ]
276 .into_iter()
277 .zip(&mut it)
278 {
279 *dest = src.as_segment().into();
280 }
281
282 for (dest, src) in [&mut self.gdtr, &mut self.idtr].into_iter().zip(it) {
283 *dest = src.as_table().into();
284 }
285 }
286}
287
288impl StateElement<X86PartitionCapabilities, X86VpInfo> for Registers {
289 fn is_present(_caps: &X86PartitionCapabilities) -> bool {
290 true
291 }
292
293 fn at_reset(caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
294 let cs = SegmentRegister {
295 base: 0xffff0000,
296 limit: 0xffff,
297 selector: 0xf000,
298 attributes: 0x9b,
299 };
300 let ds = SegmentRegister {
301 base: 0,
302 limit: 0xffff,
303 selector: 0,
304 attributes: 0x93,
305 };
306 let tr = SegmentRegister {
307 base: 0,
308 limit: 0xffff,
309 selector: 0,
310 attributes: 0x8b,
311 };
312 let ldtr = SegmentRegister {
313 base: 0,
314 limit: 0xffff,
315 selector: 0,
316 attributes: 0x82,
317 };
318 let gdtr = TableRegister {
319 base: 0,
320 limit: 0xffff,
321 };
322 let efer = if caps.nxe_forced_on { X64_EFER_NXE } else { 0 };
323 Self {
324 rax: 0,
325 rcx: 0,
326 rdx: caps.reset_rdx,
327 rbx: 0,
328 rbp: 0,
329 rsp: 0,
330 rsi: 0,
331 rdi: 0,
332 r8: 0,
333 r9: 0,
334 r10: 0,
335 r11: 0,
336 r12: 0,
337 r13: 0,
338 r14: 0,
339 r15: 0,
340 rip: 0xfff0,
341 rflags: RFlags::at_reset().into(),
342 cs,
343 ds,
344 es: ds,
345 fs: ds,
346 gs: ds,
347 ss: ds,
348 tr,
349 ldtr,
350 gdtr,
351 idtr: gdtr,
352 cr0: X64_CR0_ET | X64_CR0_CD | X64_CR0_NW,
353 cr2: 0,
354 cr3: 0,
355 cr4: 0,
356 cr8: 0,
357 efer,
358 }
359 }
360}
361
362#[derive(Default, Debug, PartialEq, Eq, Protobuf, Inspect)]
363#[mesh(package = "virt.x86")]
364pub struct Activity {
365 #[mesh(1)]
366 pub mp_state: MpState,
367 #[mesh(2)]
368 pub nmi_pending: bool,
369 #[mesh(3)]
370 pub nmi_masked: bool,
371 #[mesh(4)]
372 pub interrupt_shadow: bool,
373 #[mesh(5)]
374 pub pending_event: Option<PendingEvent>,
375 #[mesh(6)]
376 pub pending_interruption: Option<PendingInterruption>,
377}
378
379#[derive(Copy, Clone, Debug, PartialEq, Eq, Protobuf, Inspect, Default)]
380#[mesh(package = "virt.x86")]
381pub enum MpState {
382 #[mesh(1)]
383 #[default]
384 Running,
385 #[mesh(2)]
386 WaitForSipi,
387 #[mesh(3)]
388 Halted,
389 #[mesh(4)]
390 Idle,
391}
392
393impl HvRegisterState<HvX64RegisterName, 4> for Activity {
396 fn names(&self) -> &'static [HvX64RegisterName; 4] {
397 &[
398 HvX64RegisterName::InternalActivityState,
399 HvX64RegisterName::PendingInterruption,
400 HvX64RegisterName::InterruptState,
401 HvX64RegisterName::PendingEvent0,
402 ]
403 }
404
405 fn get_values<'a>(&self, it: impl Iterator<Item = &'a mut HvRegisterValue>) {
406 let mut activity = HvInternalActivityRegister::from(0);
407 match self.mp_state {
408 MpState::Running => {}
409 MpState::WaitForSipi => {
410 activity.set_startup_suspend(true);
411 }
412 MpState::Halted => {
413 activity.set_halt_suspend(true);
414 }
415 MpState::Idle => {
416 activity.set_idle_suspend(true);
417 }
418 };
419
420 let pending_event = if let Some(event) = self.pending_event {
421 match event {
422 PendingEvent::Exception {
423 vector,
424 error_code,
425 parameter,
426 } => HvX64PendingExceptionEvent::new()
427 .with_event_pending(true)
428 .with_event_type(hvdef::HV_X64_PENDING_EVENT_EXCEPTION)
429 .with_vector(vector.into())
430 .with_deliver_error_code(error_code.is_some())
431 .with_error_code(error_code.unwrap_or(0))
432 .with_exception_parameter(parameter)
433 .into(),
434
435 PendingEvent::ExtInt { vector } => HvX64PendingExtIntEvent::new()
436 .with_event_pending(true)
437 .with_event_type(hvdef::HV_X64_PENDING_EVENT_EXT_INT)
438 .with_vector(vector)
439 .into(),
440 }
441 } else {
442 0
443 };
444
445 let mut pending_interruption = HvX64PendingInterruptionRegister::new();
446 if let Some(interruption) = self.pending_interruption {
447 pending_interruption.set_interruption_pending(true);
448 let ty = match interruption {
449 PendingInterruption::Exception { vector, error_code } => {
450 pending_interruption.set_interruption_vector(vector.into());
451 pending_interruption.set_deliver_error_code(error_code.is_some());
452 pending_interruption.set_error_code(error_code.unwrap_or(0));
453 HvX64PendingInterruptionType::HV_X64_PENDING_EXCEPTION
454 }
455 PendingInterruption::Interrupt { vector } => {
456 pending_interruption.set_interruption_vector(vector.into());
457 HvX64PendingInterruptionType::HV_X64_PENDING_INTERRUPT
458 }
459 PendingInterruption::Nmi => HvX64PendingInterruptionType::HV_X64_PENDING_NMI,
460 };
461 pending_interruption.set_interruption_type(ty.0);
462 }
463
464 let interrupt_state = HvX64InterruptStateRegister::new()
465 .with_nmi_masked(self.nmi_masked)
466 .with_interrupt_shadow(self.interrupt_shadow);
467
468 for (dest, src) in it.zip([
469 HvRegisterValue::from(u64::from(activity)),
470 u64::from(pending_interruption).into(),
471 u64::from(interrupt_state).into(),
472 pending_event.into(),
473 ]) {
474 *dest = src;
475 }
476 }
477
478 fn set_values(&mut self, mut it: impl Iterator<Item = HvRegisterValue>) {
479 let activity = HvInternalActivityRegister::from(it.next().unwrap().as_u64());
480 let interruption = HvX64PendingInterruptionRegister::from(it.next().unwrap().as_u64());
481 let interrupt_state = HvX64InterruptStateRegister::from(it.next().unwrap().as_u64());
482 let event = HvX64PendingEventReg0::from(it.next().unwrap().as_u128());
483
484 let mp_state = if activity.startup_suspend() {
485 MpState::WaitForSipi
486 } else if activity.halt_suspend() {
487 MpState::Halted
488 } else if activity.idle_suspend() {
489 MpState::Idle
490 } else {
491 MpState::Running
492 };
493
494 let pending_event = event.event_pending().then(|| match event.event_type() {
495 hvdef::HV_X64_PENDING_EVENT_EXCEPTION => {
496 let event = HvX64PendingExceptionEvent::from(u128::from(event));
497 PendingEvent::Exception {
498 vector: event.vector().try_into().expect("exception code is 8 bits"),
499 error_code: event.deliver_error_code().then(|| event.error_code()),
500 parameter: event.exception_parameter(),
501 }
502 }
503 hvdef::HV_X64_PENDING_EVENT_EXT_INT => {
504 let event = HvX64PendingExtIntEvent::from(u128::from(event));
505 PendingEvent::ExtInt {
506 vector: event.vector(),
507 }
508 }
509 ty => panic!("unhandled event type: {}", ty),
510 });
511
512 let pending_interruption = interruption.interruption_pending().then(|| {
513 match HvX64PendingInterruptionType(interruption.interruption_type()) {
514 HvX64PendingInterruptionType::HV_X64_PENDING_INTERRUPT => {
515 PendingInterruption::Interrupt {
516 vector: interruption
517 .interruption_vector()
518 .try_into()
519 .expect("x86 vector is 8 bits"),
520 }
521 }
522 HvX64PendingInterruptionType::HV_X64_PENDING_NMI => PendingInterruption::Nmi,
523 HvX64PendingInterruptionType::HV_X64_PENDING_EXCEPTION => {
524 PendingInterruption::Exception {
525 vector: interruption
526 .interruption_vector()
527 .try_into()
528 .expect("exception code is 8 bits"),
529 error_code: interruption
530 .deliver_error_code()
531 .then(|| interruption.error_code()),
532 }
533 }
534 ty => panic!("unhandled interruption type: {ty:?}"),
535 }
536 });
537
538 *self = Self {
539 mp_state,
540 nmi_pending: false,
541 nmi_masked: interrupt_state.nmi_masked(),
542 interrupt_shadow: interrupt_state.interrupt_shadow(),
543 pending_event,
544 pending_interruption,
545 };
546 }
547}
548
549impl StateElement<X86PartitionCapabilities, X86VpInfo> for Activity {
550 fn is_present(_caps: &X86PartitionCapabilities) -> bool {
551 true
552 }
553
554 fn at_reset(_caps: &X86PartitionCapabilities, vp_info: &X86VpInfo) -> Self {
555 let mp_state = if vp_info.base.is_bsp() {
556 MpState::Running
557 } else {
558 MpState::WaitForSipi
561 };
562 Self {
563 mp_state,
564 nmi_pending: false,
565 nmi_masked: false,
566 interrupt_shadow: false,
567 pending_event: None,
568 pending_interruption: None,
569 }
570 }
571}
572
573#[derive(Debug, PartialEq, Eq, Copy, Clone, Protobuf, Inspect)]
574#[mesh(package = "virt.x86")]
575#[inspect(external_tag)]
576pub enum PendingEvent {
577 #[mesh(1)]
578 Exception {
579 #[mesh(1)]
580 vector: u8,
581 #[mesh(2)]
582 error_code: Option<u32>,
583 #[mesh(3)]
584 parameter: u64,
585 },
586 #[mesh(2)]
587 ExtInt {
588 #[mesh(1)]
589 vector: u8,
590 },
591}
592
593#[derive(Debug, PartialEq, Eq, Copy, Clone, Protobuf, Inspect)]
594#[mesh(package = "virt.x86")]
595#[inspect(external_tag)]
596pub enum PendingInterruption {
597 #[mesh(1)]
598 Exception {
599 #[mesh(1)]
600 vector: u8,
601 #[mesh(2)]
602 error_code: Option<u32>,
603 },
604 #[mesh(2)]
605 Interrupt {
606 #[mesh(1)]
607 vector: u8,
608 },
609 #[mesh(3)]
610 Nmi,
611}
612
613#[derive(Debug, Default, PartialEq, Eq, Copy, Clone, Protobuf, Inspect)]
614#[mesh(package = "virt.x86")]
615pub struct DebugRegisters {
616 #[mesh(1)]
617 #[inspect(hex)]
618 pub dr0: u64,
619 #[mesh(2)]
620 #[inspect(hex)]
621 pub dr1: u64,
622 #[mesh(3)]
623 #[inspect(hex)]
624 pub dr2: u64,
625 #[mesh(4)]
626 #[inspect(hex)]
627 pub dr3: u64,
628 #[mesh(5)]
629 #[inspect(hex)]
630 pub dr6: u64,
631 #[mesh(6)]
632 #[inspect(hex)]
633 pub dr7: u64,
634}
635
636impl HvRegisterState<HvX64RegisterName, 6> for DebugRegisters {
637 fn names(&self) -> &'static [HvX64RegisterName; 6] {
638 &[
639 HvX64RegisterName::Dr0,
640 HvX64RegisterName::Dr1,
641 HvX64RegisterName::Dr2,
642 HvX64RegisterName::Dr3,
643 HvX64RegisterName::Dr6,
644 HvX64RegisterName::Dr7,
645 ]
646 }
647
648 fn get_values<'a>(&self, it: impl Iterator<Item = &'a mut HvRegisterValue>) {
649 for (dest, src) in it.zip([
650 self.dr0.into(),
651 self.dr1.into(),
652 self.dr2.into(),
653 self.dr3.into(),
654 self.dr6.into(),
655 self.dr7.into(),
656 ]) {
657 *dest = src;
658 }
659 }
660
661 fn set_values(&mut self, it: impl Iterator<Item = HvRegisterValue>) {
662 for (src, dest) in it.zip([
663 &mut self.dr0,
664 &mut self.dr1,
665 &mut self.dr2,
666 &mut self.dr3,
667 &mut self.dr6,
668 &mut self.dr7,
669 ]) {
670 *dest = src.as_u64();
671 }
672 }
673}
674
675impl StateElement<X86PartitionCapabilities, X86VpInfo> for DebugRegisters {
676 fn is_present(_caps: &X86PartitionCapabilities) -> bool {
677 true
678 }
679
680 fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
681 Self {
682 dr0: 0,
683 dr1: 0,
684 dr2: 0,
685 dr3: 0,
686 dr6: 0xffff0ff0,
687 dr7: 0x400,
688 }
689 }
690
691 fn can_compare(caps: &X86PartitionCapabilities) -> bool {
692 !caps.dr6_tsx_broken
696 }
697}
698
699#[derive(PartialEq, Eq, Protobuf)]
700#[mesh(package = "virt.x86")]
701pub struct Xsave {
702 #[mesh(1)]
703 pub data: Vec<u64>,
704}
705
706impl Xsave {
707 fn normalize(&mut self) {
708 let (mut fxsave, data) = Ref::<_, Fxsave>::from_prefix(self.data.as_mut_bytes()).unwrap();
709 let header = XsaveHeader::mut_from_prefix(data).unwrap().0; fxsave.mxcsr_mask = 0;
714
715 if header.xstate_bv & XFEATURE_SSE != 0 {
719 if fxsave.xmm.iter().eq(std::iter::repeat_n(&[0; 16], 16))
720 && fxsave.mxcsr == DEFAULT_MXCSR
721 {
722 header.xstate_bv &= !XFEATURE_SSE;
723 }
724 } else {
725 fxsave.xmm.fill(Default::default());
726 }
727
728 if header.xstate_bv & (XFEATURE_SSE | XFEATURE_YMM) == 0 {
729 fxsave.mxcsr = 0;
730 }
731
732 if header.xstate_bv & XFEATURE_X87 != 0 {
734 if fxsave.fcw == INIT_FCW
735 && fxsave.fsw == 0
736 && fxsave.ftw == 0
737 && fxsave.fop == 0
738 && fxsave.fip == 0
739 && fxsave.fdp == 0
740 && fxsave.st == [[0; 16]; 8]
741 {
742 fxsave.fcw = 0;
743 header.xstate_bv &= !XFEATURE_X87;
744 }
745 } else {
746 fxsave.fcw = 0;
747 fxsave.fsw = 0;
748 fxsave.ftw = 0;
749 fxsave.fop = 0;
750 fxsave.fip = 0;
751 fxsave.fdp = 0;
752 fxsave.st.fill(Default::default());
753 }
754
755 fxsave.unused.fill(0);
759 }
760
761 pub fn from_compact(data: &[u8], caps: &X86PartitionCapabilities) -> Self {
763 assert_eq!(data.len() % 8, 0);
764 let mut aligned = vec![0; data.len() / 8];
765 aligned.as_mut_bytes().copy_from_slice(data);
766 let mut this = Self { data: aligned };
767
768 this.normalize();
769
770 if caps.xsaves_state_bv_broken {
775 let header =
776 XsaveHeader::mut_from_prefix(&mut this.data.as_mut_bytes()[XSAVE_LEGACY_LEN..])
777 .unwrap()
778 .0; header.xstate_bv |= header.xcomp_bv & 0x1c00;
784 }
785
786 this
787 }
788
789 pub fn from_standard(src: &[u8], caps: &X86PartitionCapabilities) -> Self {
791 let mut this = Self {
792 data: vec![0; caps.xsave.compact_len as usize / 8],
793 };
794 this.data.as_mut_bytes()[..XSAVE_VARIABLE_OFFSET]
795 .copy_from_slice(&src[..XSAVE_VARIABLE_OFFSET]);
796
797 let (mut header, data) =
798 Ref::<_, XsaveHeader>::from_prefix(&mut this.data.as_mut_bytes()[XSAVE_LEGACY_LEN..])
799 .unwrap();
800
801 header.xcomp_bv = caps.xsave.features | caps.xsave.supervisor_features | XCOMP_COMPRESSED;
802 let mut cur = 0;
803 for (i, feature) in caps.xsave.variable_features(header.xcomp_bv) {
804 let offset = feature.offset as usize;
805 let len = feature.len as usize;
806 if feature.align {
807 cur = (cur + 63) & !63;
808 }
809 if header.xstate_bv & (1 << i) != 0 {
810 data[cur..cur + len].copy_from_slice(&src[offset..offset + len]);
811 }
812 cur += len;
813 }
814 this.normalize();
815 this
816 }
817
818 pub fn write_standard(&self, data: &mut [u8], caps: &X86PartitionCapabilities) {
820 data[..XSAVE_LEGACY_LEN].copy_from_slice(self.fxsave().as_bytes());
822
823 let header = self.xsave_header();
825 data[XSAVE_LEGACY_LEN..XSAVE_VARIABLE_OFFSET].copy_from_slice(
826 XsaveHeader {
827 xcomp_bv: 0,
828 ..*header
829 }
830 .as_bytes(),
831 );
832
833 let mut cur = XSAVE_VARIABLE_OFFSET;
835 for (i, feature) in caps.xsave.variable_features(header.xcomp_bv) {
836 let offset = feature.offset as usize;
837 let len = feature.len as usize;
838 if feature.align {
839 cur = (cur + 63) & !63;
840 }
841 if header.xstate_bv & (1 << i) != 0 {
842 data[offset..offset + len].copy_from_slice(&self.data.as_bytes()[cur..cur + len]);
843 }
844 cur += len;
845 }
846 }
847
848 pub fn compact(&self) -> &[u8] {
850 self.data.as_bytes()
851 }
852
853 pub fn fxsave(&self) -> Fxsave {
858 let mut fxsave = Fxsave::read_from_prefix(self.data.as_bytes()).unwrap().0; let header = self.xsave_header();
860 if header.xstate_bv & XFEATURE_X87 == 0 {
861 fxsave.fcw = INIT_FCW;
862 }
863 if header.xstate_bv & (XFEATURE_SSE | XFEATURE_YMM) == 0 {
864 fxsave.mxcsr = DEFAULT_MXCSR;
865 }
866 fxsave
867 }
868
869 fn xsave_header(&self) -> &XsaveHeader {
870 XsaveHeader::ref_from_prefix(&self.data.as_bytes()[XSAVE_LEGACY_LEN..])
871 .unwrap()
872 .0 }
874}
875
876impl Debug for Xsave {
877 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
878 f.debug_struct("Xsave")
879 .field("legacy", &format_args!("{:x?}", self.fxsave()))
880 .field("header", &format_args!("{:x?}", self.xsave_header()))
881 .field("data", &&self.data[XSAVE_VARIABLE_OFFSET / 8..])
882 .finish()
883 }
884}
885
886impl Inspect for Xsave {
887 fn inspect(&self, req: inspect::Request<'_>) {
888 let Fxsave {
889 fcw,
890 fsw,
891 ftw,
892 reserved: _,
893 fop,
894 fip,
895 fdp,
896 mxcsr,
897 mxcsr_mask,
898 st,
899 xmm,
900 reserved2: _,
901 unused: _,
902 } = self.fxsave();
903
904 let &XsaveHeader {
905 xstate_bv,
906 xcomp_bv,
907 reserved: _,
908 } = self.xsave_header();
909
910 let mut resp = req.respond();
911 resp.hex("fcw", fcw)
912 .hex("fsw", fsw)
913 .hex("ftw", ftw)
914 .hex("fop", fop)
915 .hex("fip", fip)
916 .hex("fdp", fdp)
917 .hex("mxcsr", mxcsr)
918 .hex("mxcsr_mask", mxcsr_mask)
919 .hex("xstate_bv", xstate_bv)
920 .hex("xcomp_bv", xcomp_bv);
921
922 for (st, name) in st
923 .iter()
924 .zip(["st0", "st1", "st2", "st3", "st4", "st5", "st6", "st7"])
925 {
926 resp.field(name, st);
927 }
928
929 for (xmm, name) in xmm.iter().zip([
930 "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7", "xmm8", "xmm9",
931 "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
932 ]) {
933 resp.field(name, xmm);
934 }
935 }
936}
937
938impl StateElement<X86PartitionCapabilities, X86VpInfo> for Xsave {
939 fn is_present(_caps: &X86PartitionCapabilities) -> bool {
940 true
941 }
942
943 fn at_reset(caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
944 let mut data = vec![0; caps.xsave.compact_len as usize];
945 *XsaveHeader::mut_from_prefix(&mut data[XSAVE_LEGACY_LEN..])
946 .unwrap()
947 .0 = XsaveHeader {
948 xstate_bv: 0,
950 xcomp_bv: XCOMP_COMPRESSED | caps.xsave.features | caps.xsave.supervisor_features,
951 reserved: [0; 6],
952 };
953 Self::from_compact(&data, caps)
954 }
955}
956
957#[derive(PartialEq, Eq, Clone, Protobuf, Inspect)]
958#[mesh(package = "virt.x86")]
959#[inspect(hex)]
960pub struct Apic {
961 #[mesh(1)]
962 pub apic_base: u64,
963 #[inspect(with = "ApicRegisters::from_array_ref")]
964 #[mesh(2)]
965 pub registers: [u32; 64],
966 #[inspect(iter_by_index)]
967 #[mesh(3)]
968 pub auto_eoi: [u32; 8],
969}
970
971impl Debug for Apic {
972 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
973 let Self {
974 apic_base,
975 registers,
976 auto_eoi,
977 } = self;
978 f.debug_struct("Apic")
979 .field("apic_base", &format_args!("{:#x}", apic_base))
980 .field("registers", &format_args!("{:#x?}", registers))
981 .field("registers", &format_args!("{:#x?}", auto_eoi))
982 .finish()
983 }
984}
985
986impl Apic {
987 pub fn new(apic_base: ApicBase, registers: ApicRegisters, auto_eoi: [u32; 8]) -> Self {
988 Self {
989 apic_base: apic_base.into(),
990 registers: *registers.as_array(),
991 auto_eoi,
992 }
993 }
994
995 pub fn registers(&self) -> &ApicRegisters {
996 ApicRegisters::from_array_ref(&self.registers)
997 }
998}
999
1000#[repr(C)]
1001#[derive(Copy, Clone, Debug, IntoBytes, Immutable, KnownLayout, FromBytes, Inspect)]
1002#[inspect(hex)]
1003pub struct ApicRegisters {
1004 #[inspect(skip)]
1005 pub reserved_0: [u32; 2],
1006 pub id: u32,
1007 pub version: u32,
1008 #[inspect(skip)]
1009 pub reserved_4: [u32; 4],
1010 pub tpr: u32, pub apr: u32, pub ppr: u32, pub eoi: u32, pub rrd: u32, pub ldr: u32, pub dfr: u32, pub svr: u32, #[inspect(iter_by_index)]
1019 pub isr: [u32; 8], #[inspect(iter_by_index)]
1021 pub tmr: [u32; 8], #[inspect(iter_by_index)]
1023 pub irr: [u32; 8], pub esr: u32, #[inspect(skip)]
1026 pub reserved_29: [u32; 6],
1027 pub lvt_cmci: u32,
1028 #[inspect(iter_by_index)]
1029 pub icr: [u32; 2], pub lvt_timer: u32,
1031 pub lvt_thermal: u32,
1032 pub lvt_pmc: u32,
1033 pub lvt_lint0: u32,
1034 pub lvt_lint1: u32,
1035 pub lvt_error: u32,
1036 pub timer_icr: u32, pub timer_ccr: u32, #[inspect(skip)]
1039 pub reserved_3a: [u32; 4],
1040 pub timer_dcr: u32, #[inspect(skip)]
1042 pub reserved_3f: u32,
1043}
1044
1045const _: () = assert!(size_of::<ApicRegisters>() == 0x100);
1046
1047impl From<ApicRegisters> for hvdef::HvX64InterruptControllerState {
1048 fn from(value: ApicRegisters) -> Self {
1049 Self {
1050 apic_id: value.id,
1051 apic_version: value.version,
1052 apic_ldr: value.ldr,
1053 apic_dfr: value.dfr,
1054 apic_spurious: value.svr,
1055 apic_isr: value.isr,
1056 apic_tmr: value.tmr,
1057 apic_irr: value.irr,
1058 apic_esr: value.esr,
1059 apic_icr_high: value.icr[1],
1060 apic_icr_low: value.icr[0],
1061 apic_lvt_timer: value.lvt_timer,
1062 apic_lvt_thermal: value.lvt_thermal,
1063 apic_lvt_perfmon: value.lvt_pmc,
1064 apic_lvt_lint0: value.lvt_lint0,
1065 apic_lvt_lint1: value.lvt_lint1,
1066 apic_lvt_error: value.lvt_error,
1067 apic_lvt_cmci: value.lvt_cmci,
1068 apic_error_status: value.esr,
1069 apic_initial_count: value.timer_icr,
1070 apic_counter_value: value.timer_ccr,
1071 apic_divide_configuration: value.timer_dcr,
1072 apic_remote_read: value.rrd,
1073 }
1074 }
1075}
1076
1077impl From<hvdef::HvX64InterruptControllerState> for ApicRegisters {
1078 fn from(value: hvdef::HvX64InterruptControllerState) -> Self {
1079 let hvdef::HvX64InterruptControllerState {
1080 apic_id,
1081 apic_version,
1082 apic_ldr,
1083 apic_dfr,
1084 apic_spurious,
1085 apic_isr,
1086 apic_tmr,
1087 apic_irr,
1088 apic_esr,
1089 apic_icr_high,
1090 apic_icr_low,
1091 apic_lvt_timer,
1092 apic_lvt_thermal,
1093 apic_lvt_perfmon,
1094 apic_lvt_lint0,
1095 apic_lvt_lint1,
1096 apic_lvt_error,
1097 apic_lvt_cmci,
1098 apic_error_status: _,
1100 apic_initial_count,
1101 apic_counter_value,
1102 apic_divide_configuration,
1103 apic_remote_read,
1104 } = value;
1105 Self {
1106 reserved_0: [0; 2],
1107 id: apic_id,
1108 version: apic_version,
1109 reserved_4: [0; 4],
1110 tpr: 0,
1111 apr: 0,
1112 ppr: 0,
1113 eoi: 0,
1114 rrd: apic_remote_read,
1115 ldr: apic_ldr,
1116 dfr: apic_dfr,
1117 svr: apic_spurious,
1118 isr: apic_isr,
1119 tmr: apic_tmr,
1120 irr: apic_irr,
1121 esr: apic_esr,
1122 reserved_29: [0; 6],
1123 lvt_cmci: apic_lvt_cmci,
1124 icr: [apic_icr_low, apic_icr_high],
1125 lvt_timer: apic_lvt_timer,
1126 lvt_thermal: apic_lvt_thermal,
1127 lvt_pmc: apic_lvt_perfmon,
1128 lvt_lint0: apic_lvt_lint0,
1129 lvt_lint1: apic_lvt_lint1,
1130 lvt_error: apic_lvt_error,
1131 timer_icr: apic_initial_count,
1132 timer_ccr: apic_counter_value,
1133 reserved_3a: [0; 4],
1134 timer_dcr: apic_divide_configuration,
1135 reserved_3f: 0,
1136 }
1137 }
1138}
1139
1140#[repr(C)]
1141#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
1142struct ApicRegister {
1143 value: u32,
1144 zero: [u32; 3],
1145}
1146
1147impl ApicRegisters {
1148 pub fn as_array(&self) -> &[u32; 64] {
1149 zerocopy::transmute_ref!(self)
1150 }
1151
1152 pub fn from_array(array: [u32; 64]) -> Self {
1153 zerocopy::transmute!(array)
1154 }
1155
1156 pub fn from_array_ref(array: &[u32; 64]) -> &Self {
1157 zerocopy::transmute_ref!(array)
1158 }
1159
1160 pub fn as_page(&self) -> [u8; 1024] {
1161 let mut bytes = [0; 1024];
1162 self.as_array()
1163 .map(|value| ApicRegister {
1164 value,
1165 zero: [0; 3],
1166 })
1167 .write_to(bytes.as_mut_slice())
1168 .unwrap();
1169 bytes
1170 }
1171
1172 pub fn from_page(page: &[u8; 1024]) -> Self {
1174 let registers = <[ApicRegister; 64]>::read_from_bytes(page.as_slice()).unwrap();
1175 Self::from_array(registers.map(|reg| reg.value))
1176 }
1177}
1178
1179impl StateElement<X86PartitionCapabilities, X86VpInfo> for Apic {
1180 fn is_present(_caps: &X86PartitionCapabilities) -> bool {
1181 true
1182 }
1183
1184 fn at_reset(caps: &X86PartitionCapabilities, vp_info: &X86VpInfo) -> Self {
1185 let x2apic = caps.x2apic_enabled;
1186
1187 let mut regs = ApicRegisters::new_zeroed();
1188 regs.id = if x2apic {
1189 vp_info.apic_id
1190 } else {
1191 vp_info.apic_id << 24
1192 };
1193 regs.version = ApicVersion::new()
1194 .with_version(0x14)
1195 .with_max_lvt_entry(5)
1196 .into();
1197 if x2apic {
1198 regs.ldr = ((vp_info.apic_id << 12) & 0xffff0000) | (1 << (vp_info.apic_id & 0xf));
1199 } else {
1200 regs.dfr = !0;
1201 }
1202 regs.svr = 0xff;
1203 regs.lvt_timer = 0x10000;
1204 regs.lvt_thermal = 0x10000;
1205 regs.lvt_pmc = 0x10000;
1206 regs.lvt_lint0 = 0x10000;
1207 regs.lvt_lint1 = 0x10000;
1208 regs.lvt_error = 0x10000;
1209
1210 let apic_base = ApicBase::new()
1211 .with_base_page(APIC_BASE_PAGE)
1212 .with_bsp(vp_info.base.is_bsp())
1213 .with_x2apic(x2apic)
1214 .with_enable(true);
1215
1216 Apic::new(apic_base, regs, [0; 8])
1217 }
1218
1219 fn can_compare(caps: &X86PartitionCapabilities) -> bool {
1220 caps.can_freeze_time
1223 }
1224}
1225
1226const NMI_VECTOR: u32 = 2;
1229
1230impl ApicRegisters {
1231 pub fn set_hv_apic_nmi_pending(&mut self, pending: bool) {
1233 self.irr[0] &= !(1 << NMI_VECTOR);
1234 self.irr[0] |= (pending as u32) << NMI_VECTOR;
1235 }
1236
1237 pub fn hv_apic_nmi_pending(&self) -> bool {
1239 self.irr[0] & (1 << NMI_VECTOR) != 0
1240 }
1241}
1242
1243#[derive(Debug, Default, PartialEq, Eq, Protobuf, Inspect)]
1244#[mesh(package = "virt.x86")]
1245pub struct Xcr0 {
1246 #[mesh(1)]
1247 #[inspect(hex)]
1248 pub value: u64,
1249}
1250
1251impl HvRegisterState<HvX64RegisterName, 1> for Xcr0 {
1252 fn names(&self) -> &'static [HvX64RegisterName; 1] {
1253 &[HvX64RegisterName::Xfem]
1254 }
1255
1256 fn get_values<'a>(&self, it: impl Iterator<Item = &'a mut HvRegisterValue>) {
1257 for (dest, src) in it.zip([self.value]) {
1258 *dest = src.into();
1259 }
1260 }
1261
1262 fn set_values(&mut self, it: impl Iterator<Item = HvRegisterValue>) {
1263 for (src, dest) in it.zip([&mut self.value]) {
1264 *dest = src.as_u64();
1265 }
1266 }
1267}
1268
1269impl StateElement<X86PartitionCapabilities, X86VpInfo> for Xcr0 {
1270 fn is_present(caps: &X86PartitionCapabilities) -> bool {
1271 caps.xsave.features != 0
1272 }
1273
1274 fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
1275 Self { value: 1 }
1276 }
1277}
1278
1279#[derive(Debug, Default, PartialEq, Eq, Protobuf, Inspect)]
1280#[mesh(package = "virt.x86")]
1281pub struct Xss {
1282 #[mesh(1)]
1283 #[inspect(hex)]
1284 pub value: u64,
1285}
1286
1287impl HvRegisterState<HvX64RegisterName, 1> for Xss {
1288 fn names(&self) -> &'static [HvX64RegisterName; 1] {
1289 &[HvX64RegisterName::Xss]
1290 }
1291
1292 fn get_values<'a>(&self, it: impl Iterator<Item = &'a mut HvRegisterValue>) {
1293 for (dest, src) in it.zip([self.value]) {
1294 *dest = src.into();
1295 }
1296 }
1297
1298 fn set_values(&mut self, it: impl Iterator<Item = HvRegisterValue>) {
1299 for (src, dest) in it.zip([&mut self.value]) {
1300 *dest = src.as_u64();
1301 }
1302 }
1303}
1304
1305impl StateElement<X86PartitionCapabilities, X86VpInfo> for Xss {
1306 fn is_present(caps: &X86PartitionCapabilities) -> bool {
1307 caps.xsave.supervisor_features != 0
1308 }
1309
1310 fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
1311 Self { value: 0 }
1312 }
1313}
1314
1315#[repr(C)]
1316#[derive(Default, Debug, PartialEq, Eq, Protobuf, Inspect)]
1317#[mesh(package = "virt.x86")]
1318pub struct Pat {
1319 #[mesh(1)]
1320 #[inspect(hex)]
1321 pub value: u64,
1322}
1323
1324impl HvRegisterState<HvX64RegisterName, 1> for Pat {
1325 fn names(&self) -> &'static [HvX64RegisterName; 1] {
1326 &[HvX64RegisterName::Pat]
1327 }
1328
1329 fn get_values<'a>(&self, it: impl Iterator<Item = &'a mut HvRegisterValue>) {
1330 for (dest, src) in it.zip([self.value]) {
1331 *dest = src.into();
1332 }
1333 }
1334
1335 fn set_values(&mut self, it: impl Iterator<Item = HvRegisterValue>) {
1336 for (src, dest) in it.zip([&mut self.value]) {
1337 *dest = src.as_u64();
1338 }
1339 }
1340}
1341
1342impl StateElement<X86PartitionCapabilities, X86VpInfo> for Pat {
1343 fn is_present(_caps: &X86PartitionCapabilities) -> bool {
1344 true
1345 }
1346
1347 fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
1348 Self {
1349 value: X86X_MSR_DEFAULT_PAT,
1350 }
1351 }
1352}
1353
1354#[repr(C)]
1355#[derive(Default, Debug, PartialEq, Eq, Protobuf, Inspect)]
1356#[mesh(package = "virt.x86")]
1357#[inspect(hex)]
1358pub struct Mtrrs {
1359 #[mesh(1)]
1360 #[inspect(hex)]
1361 pub msr_mtrr_def_type: u64,
1362 #[mesh(2)]
1363 #[inspect(iter_by_index)]
1364 pub fixed: [u64; 11],
1365 #[mesh(3)]
1366 #[inspect(iter_by_index)]
1367 pub variable: [u64; 16],
1368}
1369
1370impl HvRegisterState<HvX64RegisterName, 28> for Mtrrs {
1371 fn names(&self) -> &'static [HvX64RegisterName; 28] {
1372 &[
1373 HvX64RegisterName::MsrMtrrDefType,
1374 HvX64RegisterName::MsrMtrrFix64k00000,
1375 HvX64RegisterName::MsrMtrrFix16k80000,
1376 HvX64RegisterName::MsrMtrrFix16kA0000,
1377 HvX64RegisterName::MsrMtrrFix4kC0000,
1378 HvX64RegisterName::MsrMtrrFix4kC8000,
1379 HvX64RegisterName::MsrMtrrFix4kD0000,
1380 HvX64RegisterName::MsrMtrrFix4kD8000,
1381 HvX64RegisterName::MsrMtrrFix4kE0000,
1382 HvX64RegisterName::MsrMtrrFix4kE8000,
1383 HvX64RegisterName::MsrMtrrFix4kF0000,
1384 HvX64RegisterName::MsrMtrrFix4kF8000,
1385 HvX64RegisterName::MsrMtrrPhysBase0,
1386 HvX64RegisterName::MsrMtrrPhysMask0,
1387 HvX64RegisterName::MsrMtrrPhysBase1,
1388 HvX64RegisterName::MsrMtrrPhysMask1,
1389 HvX64RegisterName::MsrMtrrPhysBase2,
1390 HvX64RegisterName::MsrMtrrPhysMask2,
1391 HvX64RegisterName::MsrMtrrPhysBase3,
1392 HvX64RegisterName::MsrMtrrPhysMask3,
1393 HvX64RegisterName::MsrMtrrPhysBase4,
1394 HvX64RegisterName::MsrMtrrPhysMask4,
1395 HvX64RegisterName::MsrMtrrPhysBase5,
1396 HvX64RegisterName::MsrMtrrPhysMask5,
1397 HvX64RegisterName::MsrMtrrPhysBase6,
1398 HvX64RegisterName::MsrMtrrPhysMask6,
1399 HvX64RegisterName::MsrMtrrPhysBase7,
1400 HvX64RegisterName::MsrMtrrPhysMask7,
1401 ]
1402 }
1403
1404 fn get_values<'a>(&self, it: impl Iterator<Item = &'a mut HvRegisterValue>) {
1405 for (dest, src) in it.zip(
1406 [self.msr_mtrr_def_type]
1407 .into_iter()
1408 .chain(self.fixed)
1409 .chain(self.variable),
1410 ) {
1411 *dest = src.into();
1412 }
1413 }
1414
1415 fn set_values(&mut self, it: impl Iterator<Item = HvRegisterValue>) {
1416 for (src, dest) in it.zip(
1417 [&mut self.msr_mtrr_def_type]
1418 .into_iter()
1419 .chain(&mut self.fixed)
1420 .chain(&mut self.variable),
1421 ) {
1422 *dest = src.as_u64();
1423 }
1424 }
1425}
1426
1427impl StateElement<X86PartitionCapabilities, X86VpInfo> for Mtrrs {
1428 fn is_present(_caps: &X86PartitionCapabilities) -> bool {
1429 true
1430 }
1431
1432 fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
1433 Self {
1434 msr_mtrr_def_type: 0,
1435 fixed: [0; 11],
1436 variable: [0; 16],
1437 }
1438 }
1439}
1440
1441#[repr(C)]
1442#[derive(Default, Debug, PartialEq, Eq, Protobuf, Inspect)]
1443#[mesh(package = "virt.x86")]
1444#[inspect(hex)]
1445pub struct VirtualMsrs {
1446 #[mesh(1)]
1447 pub kernel_gs_base: u64,
1448 #[mesh(2)]
1449 pub sysenter_cs: u64,
1450 #[mesh(3)]
1451 pub sysenter_eip: u64,
1452 #[mesh(4)]
1453 pub sysenter_esp: u64,
1454 #[mesh(5)]
1455 pub star: u64,
1456 #[mesh(6)]
1457 pub lstar: u64,
1458 #[mesh(7)]
1459 pub cstar: u64,
1460 #[mesh(8)]
1461 pub sfmask: u64,
1462}
1463
1464impl HvRegisterState<HvX64RegisterName, 8> for VirtualMsrs {
1465 fn names(&self) -> &'static [HvX64RegisterName; 8] {
1466 &[
1467 HvX64RegisterName::KernelGsBase,
1468 HvX64RegisterName::SysenterCs,
1469 HvX64RegisterName::SysenterEsp,
1470 HvX64RegisterName::SysenterEip,
1471 HvX64RegisterName::Star,
1472 HvX64RegisterName::Lstar,
1473 HvX64RegisterName::Cstar,
1474 HvX64RegisterName::Sfmask,
1475 ]
1476 }
1477
1478 fn get_values<'a>(&self, it: impl Iterator<Item = &'a mut HvRegisterValue>) {
1479 for (dest, src) in it.zip([
1480 self.kernel_gs_base,
1481 self.sysenter_cs,
1482 self.sysenter_eip,
1483 self.sysenter_esp,
1484 self.star,
1485 self.lstar,
1486 self.cstar,
1487 self.sfmask,
1488 ]) {
1489 *dest = src.into();
1490 }
1491 }
1492
1493 fn set_values(&mut self, it: impl Iterator<Item = HvRegisterValue>) {
1494 for (src, dest) in it.zip([
1495 &mut self.kernel_gs_base,
1496 &mut self.sysenter_cs,
1497 &mut self.sysenter_eip,
1498 &mut self.sysenter_esp,
1499 &mut self.star,
1500 &mut self.lstar,
1501 &mut self.cstar,
1502 &mut self.sfmask,
1503 ]) {
1504 *dest = src.as_u64();
1505 }
1506 }
1507}
1508
1509impl StateElement<X86PartitionCapabilities, X86VpInfo> for VirtualMsrs {
1510 fn is_present(_caps: &X86PartitionCapabilities) -> bool {
1511 true
1512 }
1513
1514 fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
1515 Self {
1516 kernel_gs_base: 0,
1517 sysenter_cs: 0,
1518 sysenter_eip: 0,
1519 sysenter_esp: 0,
1520 star: 0,
1521 lstar: 0,
1522 cstar: 0,
1523 sfmask: 0,
1524 }
1525 }
1526}
1527
1528#[repr(C)]
1529#[derive(Default, Debug, PartialEq, Eq, Protobuf, Inspect)]
1530#[mesh(package = "virt.x86")]
1531pub struct TscAux {
1532 #[mesh(1)]
1533 #[inspect(hex)]
1534 pub value: u64,
1535}
1536
1537impl HvRegisterState<HvX64RegisterName, 1> for TscAux {
1538 fn names(&self) -> &'static [HvX64RegisterName; 1] {
1539 &[HvX64RegisterName::TscAux]
1540 }
1541
1542 fn get_values<'a>(&self, it: impl Iterator<Item = &'a mut HvRegisterValue>) {
1543 for (dest, src) in it.zip([self.value]) {
1544 *dest = src.into();
1545 }
1546 }
1547
1548 fn set_values(&mut self, it: impl Iterator<Item = HvRegisterValue>) {
1549 for (src, dest) in it.zip([&mut self.value]) {
1550 *dest = src.as_u64();
1551 }
1552 }
1553}
1554
1555impl StateElement<X86PartitionCapabilities, X86VpInfo> for TscAux {
1556 fn is_present(caps: &X86PartitionCapabilities) -> bool {
1557 caps.tsc_aux
1558 }
1559
1560 fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
1561 Default::default()
1562 }
1563}
1564
1565#[repr(C)]
1566#[derive(Default, Debug, PartialEq, Eq, Protobuf, Inspect)]
1567#[mesh(package = "virt.x86")]
1568pub struct Tsc {
1569 #[mesh(1)]
1570 #[inspect(hex)]
1571 pub value: u64,
1572}
1573
1574impl HvRegisterState<HvX64RegisterName, 1> for Tsc {
1575 fn names(&self) -> &'static [HvX64RegisterName; 1] {
1576 &[HvX64RegisterName::Tsc]
1577 }
1578
1579 fn get_values<'a>(&self, it: impl Iterator<Item = &'a mut HvRegisterValue>) {
1580 for (dest, src) in it.zip([self.value]) {
1581 *dest = src.into();
1582 }
1583 }
1584
1585 fn set_values(&mut self, it: impl Iterator<Item = HvRegisterValue>) {
1586 for (src, dest) in it.zip([&mut self.value]) {
1587 *dest = src.as_u64();
1588 }
1589 }
1590}
1591
1592impl StateElement<X86PartitionCapabilities, X86VpInfo> for Tsc {
1593 fn is_present(_caps: &X86PartitionCapabilities) -> bool {
1594 true
1595 }
1596
1597 fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
1598 Self { value: 0 }
1599 }
1600
1601 fn can_compare(caps: &X86PartitionCapabilities) -> bool {
1602 caps.can_freeze_time
1603 }
1604}
1605
1606#[repr(C)]
1611#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Protobuf, Inspect)]
1612#[mesh(package = "virt.x86")]
1613pub struct TscDeadline {
1614 #[mesh(1)]
1615 #[inspect(hex)]
1616 pub value: u64,
1617}
1618
1619impl HvRegisterState<HvX64RegisterName, 1> for TscDeadline {
1620 fn names(&self) -> &'static [HvX64RegisterName; 1] {
1621 &[HvX64RegisterName::TscDeadline]
1622 }
1623
1624 fn get_values<'a>(&self, it: impl Iterator<Item = &'a mut HvRegisterValue>) {
1625 for (dest, src) in it.zip([self.value]) {
1626 *dest = src.into();
1627 }
1628 }
1629
1630 fn set_values(&mut self, it: impl Iterator<Item = HvRegisterValue>) {
1631 for (src, dest) in it.zip([&mut self.value]) {
1632 *dest = src.as_u64();
1633 }
1634 }
1635}
1636
1637impl StateElement<X86PartitionCapabilities, X86VpInfo> for TscDeadline {
1638 fn is_present(caps: &X86PartitionCapabilities) -> bool {
1639 caps.tsc_deadline
1640 }
1641
1642 fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
1643 Self::default()
1644 }
1645
1646 fn can_compare(caps: &X86PartitionCapabilities) -> bool {
1647 caps.can_freeze_time
1648 }
1649}
1650
1651#[repr(C)]
1652#[derive(Default, Debug, PartialEq, Eq, Protobuf, Inspect)]
1653#[mesh(package = "virt.x86")]
1654pub struct Cet {
1655 #[mesh(1)]
1656 #[inspect(hex)]
1657 pub scet: u64,
1658 }
1660
1661impl HvRegisterState<HvX64RegisterName, 1> for Cet {
1662 fn names(&self) -> &'static [HvX64RegisterName; 1] {
1663 &[HvX64RegisterName::SCet]
1664 }
1665
1666 fn get_values<'a>(&self, it: impl Iterator<Item = &'a mut HvRegisterValue>) {
1667 for (dest, src) in it.zip([self.scet]) {
1668 *dest = src.into();
1669 }
1670 }
1671
1672 fn set_values(&mut self, it: impl Iterator<Item = HvRegisterValue>) {
1673 for (src, dest) in it.zip([&mut self.scet]) {
1674 *dest = src.as_u64();
1675 }
1676 }
1677}
1678
1679impl StateElement<X86PartitionCapabilities, X86VpInfo> for Cet {
1680 fn is_present(caps: &X86PartitionCapabilities) -> bool {
1681 caps.cet
1682 }
1683
1684 fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
1685 Self { scet: 0 }
1686 }
1687}
1688
1689#[repr(C)]
1690#[derive(Default, Debug, PartialEq, Eq, Protobuf, Inspect)]
1691#[mesh(package = "virt.x86")]
1692#[inspect(hex)]
1693pub struct CetSs {
1694 #[mesh(1)]
1695 pub ssp: u64,
1696 #[mesh(2)]
1697 pub interrupt_ssp_table_addr: u64,
1698 }
1700
1701impl HvRegisterState<HvX64RegisterName, 2> for CetSs {
1702 fn names(&self) -> &'static [HvX64RegisterName; 2] {
1703 &[
1704 HvX64RegisterName::Ssp,
1705 HvX64RegisterName::InterruptSspTableAddr,
1706 ]
1707 }
1708
1709 fn get_values<'a>(&self, it: impl Iterator<Item = &'a mut HvRegisterValue>) {
1710 for (dest, src) in it.zip([self.ssp, self.interrupt_ssp_table_addr]) {
1711 *dest = src.into();
1712 }
1713 }
1714
1715 fn set_values(&mut self, it: impl Iterator<Item = HvRegisterValue>) {
1716 for (src, dest) in it.zip([&mut self.ssp, &mut self.interrupt_ssp_table_addr]) {
1717 *dest = src.as_u64();
1718 }
1719 }
1720}
1721
1722impl StateElement<X86PartitionCapabilities, X86VpInfo> for CetSs {
1723 fn is_present(caps: &X86PartitionCapabilities) -> bool {
1724 caps.cet_ss
1725 }
1726
1727 fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
1728 Default::default()
1729 }
1730}
1731
1732#[repr(C)]
1733#[derive(Debug, Default, PartialEq, Eq, Protobuf, Inspect)]
1734#[mesh(package = "virt.x86")]
1735#[inspect(hex)]
1736pub struct SyntheticMsrs {
1737 #[mesh(1)]
1738 pub vp_assist_page: u64,
1739 #[mesh(2)]
1740 pub scontrol: u64,
1741 #[mesh(3)]
1742 pub siefp: u64,
1743 #[mesh(4)]
1744 pub simp: u64,
1745 #[mesh(5)]
1746 #[inspect(iter_by_index)]
1747 pub sint: [u64; 16],
1748}
1749
1750impl HvRegisterState<HvX64RegisterName, 20> for SyntheticMsrs {
1751 fn names(&self) -> &'static [HvX64RegisterName; 20] {
1752 &[
1753 HvX64RegisterName::VpAssistPage,
1754 HvX64RegisterName::Scontrol,
1755 HvX64RegisterName::Sifp,
1756 HvX64RegisterName::Sipp,
1757 HvX64RegisterName::Sint0,
1758 HvX64RegisterName::Sint1,
1759 HvX64RegisterName::Sint2,
1760 HvX64RegisterName::Sint3,
1761 HvX64RegisterName::Sint4,
1762 HvX64RegisterName::Sint5,
1763 HvX64RegisterName::Sint6,
1764 HvX64RegisterName::Sint7,
1765 HvX64RegisterName::Sint8,
1766 HvX64RegisterName::Sint9,
1767 HvX64RegisterName::Sint10,
1768 HvX64RegisterName::Sint11,
1769 HvX64RegisterName::Sint12,
1770 HvX64RegisterName::Sint13,
1771 HvX64RegisterName::Sint14,
1772 HvX64RegisterName::Sint15,
1773 ]
1774 }
1775 fn get_values<'a>(&self, it: impl Iterator<Item = &'a mut HvRegisterValue>) {
1776 for (dest, src) in it.zip(
1777 [self.vp_assist_page, self.scontrol, self.siefp, self.simp]
1778 .into_iter()
1779 .chain(self.sint),
1780 ) {
1781 *dest = src.into();
1782 }
1783 }
1784
1785 fn set_values(&mut self, it: impl Iterator<Item = HvRegisterValue>) {
1786 for (src, dest) in it.zip(
1787 [
1788 &mut self.vp_assist_page,
1789 &mut self.scontrol,
1790 &mut self.siefp,
1791 &mut self.simp,
1792 ]
1793 .into_iter()
1794 .chain(&mut self.sint),
1795 ) {
1796 *dest = src.as_u64();
1797 }
1798 }
1799}
1800
1801impl StateElement<X86PartitionCapabilities, X86VpInfo> for SyntheticMsrs {
1802 fn is_present(caps: &X86PartitionCapabilities) -> bool {
1803 caps.hv1
1804 }
1805
1806 fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
1807 Self {
1808 vp_assist_page: 0,
1809 scontrol: 1,
1810 siefp: 0,
1811 simp: 0,
1812 sint: [0x10000; 16],
1813 }
1814 }
1815}
1816
1817#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, Protobuf, Inspect)]
1818#[mesh(package = "virt.x86")]
1819#[inspect(hex)]
1820pub struct SynicTimer {
1821 #[mesh(1)]
1822 pub config: u64,
1823 #[mesh(2)]
1824 pub count: u64,
1825 #[mesh(3)]
1826 pub adjustment: u64,
1827 #[mesh(4)]
1828 pub undelivered_message_expiration_time: Option<u64>,
1829}
1830
1831#[derive(Debug, Copy, Clone, Eq, PartialEq, Protobuf, Inspect)]
1832#[mesh(package = "virt.x86")]
1833pub struct SynicTimers {
1834 #[mesh(1)]
1835 #[inspect(iter_by_index)]
1836 pub timers: [SynicTimer; 4],
1837}
1838
1839impl SynicTimers {
1840 pub fn as_hv(&self) -> hvdef::HvSyntheticTimersState {
1841 let timers = self.timers.map(|timer| hvdef::HvStimerState {
1842 undelivered_message_pending: timer.undelivered_message_expiration_time.is_some().into(),
1843 reserved: 0,
1844 config: timer.config,
1845 count: timer.count,
1846 adjustment: timer.adjustment,
1847 undelivered_expiration_time: timer.undelivered_message_expiration_time.unwrap_or(0),
1848 });
1849
1850 hvdef::HvSyntheticTimersState {
1851 timers,
1852 reserved: [0; 5],
1853 }
1854 }
1855
1856 pub fn from_hv(state: hvdef::HvSyntheticTimersState) -> Self {
1857 let timers = state.timers.map(|timer| SynicTimer {
1858 config: timer.config,
1859 count: timer.count,
1860 adjustment: timer.adjustment,
1861 undelivered_message_expiration_time: (timer.undelivered_message_pending & 1 != 0)
1862 .then_some(timer.undelivered_expiration_time),
1863 });
1864 Self { timers }
1865 }
1866}
1867
1868impl StateElement<X86PartitionCapabilities, X86VpInfo> for SynicTimers {
1869 fn is_present(caps: &X86PartitionCapabilities) -> bool {
1870 caps.hv1
1871 }
1872
1873 fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
1874 Self {
1875 timers: [SynicTimer::default(); 4],
1876 }
1877 }
1878
1879 fn can_compare(_caps: &X86PartitionCapabilities) -> bool {
1880 false
1883 }
1884}
1885
1886#[derive(Debug, Default, PartialEq, Eq, Protobuf, Inspect)]
1887#[mesh(package = "virt.x86")]
1888pub struct SynicMessageQueues {
1889 #[mesh(1)]
1890 #[inspect(with = "|x| inspect::iter_by_index(x.iter().map(Vec::len))")]
1891 pub queues: [Vec<[u8; HV_MESSAGE_SIZE]>; 16],
1892}
1893
1894impl StateElement<X86PartitionCapabilities, X86VpInfo> for SynicMessageQueues {
1895 fn is_present(caps: &X86PartitionCapabilities) -> bool {
1896 caps.hv1
1897 }
1898
1899 fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
1900 Default::default()
1901 }
1902}
1903
1904#[derive(Debug, Copy, Clone, PartialEq, Eq, Protobuf)]
1905#[mesh(package = "virt.x86")]
1906pub struct SynicMessagePage {
1907 #[mesh(1)]
1908 pub data: [u8; 4096],
1909}
1910
1911impl Inspect for SynicMessagePage {
1912 fn inspect(&self, req: inspect::Request<'_>) {
1913 let mut resp = req.respond();
1914 let mut occupied = 0u16;
1915 for (sint, slot) in self
1916 .data
1917 .as_chunks::<HV_MESSAGE_SIZE>()
1918 .0
1919 .iter()
1920 .enumerate()
1921 {
1922 let msg: HvMessage = zerocopy::transmute!(*slot);
1923 if msg.header.typ != hvdef::HvMessageType::HvMessageTypeNone {
1924 occupied |= 1 << sint;
1925 }
1926 }
1927 resp.binary("occupied_bitmap", occupied);
1928 }
1929}
1930
1931impl StateElement<X86PartitionCapabilities, X86VpInfo> for SynicMessagePage {
1932 fn is_present(caps: &X86PartitionCapabilities) -> bool {
1933 caps.hv1
1934 }
1935
1936 fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
1937 Self { data: [0; 4096] }
1938 }
1939}
1940
1941#[derive(Debug, Copy, Clone, PartialEq, Eq, Protobuf, Inspect)]
1942#[mesh(package = "virt.x86")]
1943#[inspect(skip)]
1944pub struct SynicEventFlagsPage {
1945 #[mesh(1)]
1946 pub data: [u8; 4096],
1947}
1948
1949impl StateElement<X86PartitionCapabilities, X86VpInfo> for SynicEventFlagsPage {
1950 fn is_present(caps: &X86PartitionCapabilities) -> bool {
1951 caps.hv1
1952 }
1953
1954 fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
1955 Self { data: [0; 4096] }
1956 }
1957}
1958
1959#[derive(Debug, Clone, Default, PartialEq, Eq, Protobuf, Inspect)]
1961#[mesh(package = "virt.x86")]
1962#[inspect(skip)]
1963pub struct NestedState {
1964 #[mesh(1)]
1965 pub data: Vec<u8>,
1966}
1967
1968impl StateElement<X86PartitionCapabilities, X86VpInfo> for NestedState {
1969 fn is_present(caps: &X86PartitionCapabilities) -> bool {
1970 caps.nested_virt
1971 }
1972
1973 fn at_reset(_caps: &X86PartitionCapabilities, _vp_info: &X86VpInfo) -> Self {
1974 Self::default()
1975 }
1976
1977 fn can_compare(_caps: &X86PartitionCapabilities) -> bool {
1978 false
1979 }
1980}
1981
1982state_trait! {
1983 "Per-VP state",
1984 AccessVpState,
1985 X86PartitionCapabilities,
1986 X86VpInfo,
1987 VpSavedState,
1988 "virt.x86",
1989 (1, "registers", registers, set_registers, Registers),
1990 (2, "activity", activity, set_activity, Activity),
1991 (3, "xsave", xsave, set_xsave, Xsave),
1992 (4, "apic", apic, set_apic, Apic),
1993 (5, "xcr", xcr, set_xcr, Xcr0),
1994 (6, "xss", xss, set_xss, Xss),
1995 (7, "mtrrs", mtrrs, set_mtrrs, Mtrrs),
1996 (8, "pat", pat, set_pat, Pat),
1997 (9, "msrs", virtual_msrs, set_virtual_msrs, VirtualMsrs),
1998 (10, "drs", debug_regs, set_debug_regs, DebugRegisters),
1999 (11, "tsc", tsc, set_tsc, Tsc),
2000 (12, "cet", cet, set_cet, Cet),
2001 (13, "cet_ss", cet_ss, set_cet_ss, CetSs),
2002 (14, "tsc_aux", tsc_aux, set_tsc_aux, TscAux),
2003 (15, "tsc_deadline", tsc_deadline, set_tsc_deadline, TscDeadline),
2005
2006 (100, "synic", synic_msrs, set_synic_msrs, SyntheticMsrs),
2008 (
2013 101,
2014 "simp",
2015 synic_message_page,
2016 set_synic_message_page,
2017 SynicMessagePage
2018 ),
2019 (
2020 102,
2021 "siefp",
2022 synic_event_flags_page,
2023 set_synic_event_flags_page,
2024 SynicEventFlagsPage
2025 ),
2026 (
2027 103,
2028 "synic_message_queues",
2029 synic_message_queues,
2030 set_synic_message_queues,
2031 SynicMessageQueues
2032 ),
2033 (104, "synic_timers", synic_timers, set_synic_timers, SynicTimers),
2034
2035 (200, "nested_state", nested_state, set_nested_state, NestedState),
2036}
2037
2038pub fn x86_init<T: AccessVpState>(access: &mut T, vp_info: &X86VpInfo) -> Result<(), T::Error> {
2040 let cr0 = access.registers()?.cr0;
2042 let mut regs = Registers::at_reset(access.caps(), vp_info);
2043 let cr0_mask = X64_CR0_NW | X64_CR0_CD;
2044 regs.cr0 = (cr0 & cr0_mask) | (regs.cr0 & !cr0_mask);
2045 access.set_registers(®s)?;
2046 access.set_debug_regs(&StateElement::at_reset(access.caps(), vp_info))?;
2047
2048 let current_apic = access.apic()?;
2053 let mut apic = Apic::at_reset(access.caps(), vp_info);
2054 apic.registers[x86defs::apic::ApicRegister::ID.0 as usize] =
2055 current_apic.registers[x86defs::apic::ApicRegister::ID.0 as usize];
2056 apic.apic_base = current_apic.apic_base;
2057 access.set_apic(&apic)?;
2058 if TscDeadline::is_present(access.caps()) {
2059 access.set_tsc_deadline(&TscDeadline::at_reset(access.caps(), vp_info))?;
2060 }
2061
2062 if !vp_info.base.is_bsp() {
2064 let mut activity = access.activity()?;
2065 activity.mp_state = MpState::WaitForSipi;
2066 access.set_activity(&activity)?;
2067 }
2068
2069 Ok(())
2070}