Skip to contentSkip to conversation

Firmware-enabled split-lock detection causes later crashes in HVM domU

DISCUSSION Marek Marczykowski-Górecki 2 participants 2 messages Nov 27

lore
Share & tools
lore
#0 lore raw

Hi,

I'm forwarding analysis I've got in qubes issues. Looks like something that either Xen should turn off, or handle better (as KVM and VirtualBox supposedly do).

----- Forwarded message -----

hippalectryon-0 left a comment (QubesOS/qubes-issues#11300)

@loftlifter31 Thanks for the logs. This is probably not memory corruption. The three crashes with parameters (dump2, dump3 and the one in hypervisor.log) all fail at the same instruction with the same second parameter across separate boots, and dump2 and dump3 show nt!KiAlignmentFault (the alignment fault handler) on the stack. dump1 has the same fault code, in KiInsertQueueDpc (its stack is cut short). On x86, normal alignment checks only apply to user mode, so an alignment fault in kernel mode points to split-lock detection: the CPU traps a locked access that crosses a cache line. Xen passes that trap into the guest, and Windows crashes. KVM (in its default mode) handles it on the host instead, and VirtualBox seems to as well, which would explain why Windows runs fine in VirtualBox on Debian.

Xen never turns split-lock detection off, so whatever turned it on before Xen started stays in effect. My guess is Heads: it boots Linux first, then kexecs into Xen. Linux turns split-lock detection on by default on Tiger Lake, Alder Lake and Raptor Lake, and doesn't turn it off before kexec. The split_lock_detect=off you added goes to the dom0 kernel, which can't change this setting under Xen. This would fit the forum reports (Alder/Raptor Lake on Dasharo+Heads), though one of them says Dasharo UEFI crashed too, so Heads may not be the only way it gets turned on.

Could you try, in order:

1. In any Linux qube (no reboot needed), run:

```
timeout 10 python3 -c 'import mmap,ctypes as c
m=mmap.mmap(-1,4096,prot=7);m.write(bytes.fromhex("f0480fba2f00c3"))
f=c.CFUNCTYPE(None,c.c_void_p)(c.addressof(c.c_char.from_buffer(m)))
b=c.create_string_buffer(256);f((c.addressof(b)+63)//64*64+60);print("no split-lock #AC")'; echo "exit: $?"
```

It does one locked write across a cache line. If it prints no split-lock #AC, split-lock detection is likely off and my theory is likely wrong. If it hangs until the timeout (exit 124) or dies with a bus error, it is on; sudo dmesg | grep -i split_lock in that qube should then show a took a split_lock trap line.
2. In the Heads recovery shell, dmesg | grep -i split and paste whatever it prints. This shows whether Heads' Linux turned it on.
3. If you can rebuild Heads, add split_lock_detect=off to the Heads kernel command line (CONFIG_LINUX_COMMAND_LINE in your board's coreboot config), reflash, and see if the BSODs stop.

If this is confirmed, the imo fix probably belongs in Xen (turn split-lock detection off at boot), not in Heads. The microcode difference (0xbe under Xen, 0xb8 under Debian) is probably unrelated. xl dmesg confirms Xen is started via kexec from Heads.

Signature · 12 lines
Reply to this email directly or view it on GitHub:
https://github.com/QubesOS/qubes-issues/issues/11300#issuecomment-7207667716
You are receiving this because you are subscribed to this thread.

Message ID: <QubesOS/qubes-issues/issues/11300/[email protected]>

----- End forwarded message -----

-- 
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#1 lore raw

Re: Firmware-enabled split-lock detection causes later crashes in HVM domU [QubesOS/qubes-issues#11300]

Quoted 23 lines · Marek Marczykowski-Górecki

Hi,

I'm forwarding analysis I've got in qubes issues. Looks like something that either Xen should turn off, or handle better (as KVM and VirtualBox supposedly do).

----- Forwarded message -----

hippalectryon-0 left a comment (QubesOS/qubes-issues#11300)

@loftlifter31 Thanks for the logs. This is probably not memory corruption. The three crashes with parameters (dump2, dump3 and the one in hypervisor.log) all fail at the same instruction with the same second parameter across separate boots, and dump2 and dump3 show nt!KiAlignmentFault (the alignment fault handler) on the stack. dump1 has the same fault code, in KiInsertQueueDpc (its stack is cut short). On x86, normal alignment checks only apply to user mode, so an alignment fault in kernel mode points to split-lock detection: the CPU traps a locked access that crosses a cache line. Xen passes that trap into the guest, and Windows crashes. KVM (in its default mode) handles it on the host instead, and VirtualBox seems to as well, which would explain why Windows runs fine in VirtualBox on Debian.

Xen never turns split-lock detection off, so whatever turned it on before Xen started stays in effect. My guess is Heads: it boots Linux first, then kexecs into Xen. Linux turns split-lock detection on by default on Tiger Lake, Alder Lake and Raptor Lake, and doesn't turn it off before kexec. The split_lock_detect=off you added goes to the dom0 kernel, which can't change this setting under Xen. This would fit the forum reports (Alder/Raptor Lake on Dasharo+Heads), though one of them says Dasharo UEFI crashed too, so Heads may not be the only way it gets turned on.

Could you try, in order:

1. In any Linux qube (no reboot needed), run:

```
timeout 10 python3 -c 'import mmap,ctypes as c
m=mmap.mmap(-1,4096,prot=7);m.write(bytes.fromhex("f0480fba2f00c3"))
f=c.CFUNCTYPE(None,c.c_void_p)(c.addressof(c.c_char.from_buffer(m)))
b=c.create_string_buffer(256);f((c.addressof(b)+63)//64*64+60);print("no split-lock #AC")'; echo "exit: $?"
```
Quoted 6 lines

It does one locked write across a cache line. If it prints no split-lock #AC, split-lock detection is likely off and my theory is likely wrong. If it hangs until the timeout (exit 124) or dies with a bus error, it is on; sudo dmesg | grep -i split_lock in that qube should then show a took a split_lock trap line.
2. In the Heads recovery shell, dmesg | grep -i split and paste whatever it prints. This shows whether Heads' Linux turned it on.
3. If you can rebuild Heads, add split_lock_detect=off to the Heads kernel command line (CONFIG_LINUX_COMMAND_LINE in your board's coreboot config), reflash, and see if the BSODs stop.

If this is confirmed, the imo fix probably belongs in Xen (turn split-lock detection off at boot), not in Heads. The microcode difference (0xbe under Xen, 0xb8 under Debian) is probably unrelated. xl dmesg confirms Xen is started via kexec from Heads.

This area is far more complicated than it seems.  Some history is necessary, and various peoples lack of precision in the past has lead to misinformation on the subject.  (i.e. Linux's command line option, which confuses the capabilities.)

First, some terminology:

* Bus Lock.  When a processor has to exert exclusive access across the entire system fabric for correctness reasons.
* Split Lock.  A LOCKed RMW instruction, with the memory operand crossing a cacheline boundary.
* UC Lock.  A LOCKed RMW instruction, with the memory operand containing non-WriteBack memory.

For both Split lock and UC Lock, the CPU tends to handle these cases by exerting the Bus Lock.

Second, the capabilities:

Intel have had MSR_MEMORY_CONTROL for ages and ages.  It is entirely model specific.

One well known bit (31) in it that isn't publicly documented is "split lock disable".  AIUI, this bit simply splitlock conditions to not exert Bus Lock, and result in non-atomic behaviour.

Starting in around 2019, Intel introduced MSR_CORE_CAPABILITIES (alongside MSR_ARCH_CAPABILITIES) in order to provide model specific enumerations.  The MSR itself is architectural, but the bits in it are not.  Model checks are required to parse MSR_CORE_CAPS because some CPUs use the same bits to mean different things.

MSR_CORE_CAPS contains a SPLIT_LOCK_DISABLE bit (in a model specific position), which enumerates that bit 29 in MSR_MEMORY_CONTROL exists. This is also called SPLIT_LOCK_DETECT, and is documented in the SDM.  It causes a splitlock condition to yield an #AC exception instead of exerting a Bus Lock.

A related enumeration is UC_LOCK_DISABLE (again in a model specific position), which enumerates that bit 28 in MSR_MEMORY_CONTROL exists. This turns a UC Lock condition into a #GP fault rather than exerting Bus Lock.

Both are faults, so they break software which encounter them.  It's fine for debugging your own code, but it isn't fine for running anyone else's code.

Intel were told this wasn't good enough, so they went and made the Bus Lock Detect feature.  Bus Lock Detect is fully architectural, and AMD has picked up the feature too.

For the non-virt case, it causes userspace bus lock conditions to trigger a #DB Trap (i.e. after the Bus Lock occurred).  Importantly, this means that the kernel can ratelimit problem userspace[1] without breaking it.  Linux prints out a message and deschedules for a while.

For the virt case, both Intel and AMD offer a way of getting a VMExit on Bus Lock.  This applies irrespective of CPL, and lets the hypervisor ratelimit problem VMs, whether it's a problem kernel or problem userspace.

On systems without Bus Lock Detect, it is possible to use performance counters for the same purpose.  Xen doesn't, but other hypervisors do.

Third, the interactions:

One problem is that MSR_MEMORY_CONTROL is defined as a Core-scope MSR, not Thread-scope.  i.e. we cannot context switch these settings because latest-takes-precedent and clobbers the sibling hyperthread.  On Hybrid system, this clobbering only occurs on P-cores, because the E-cores aren't threaded.  So in practice, the choice to use the DISABLE controls must be system-wide.

The next problem is that Xen doesn't intercept #AC and #GP, so the faults end up getting serviced by a kernel which isn't aware of their cause, and doesn't have the controls available even if it would like them.  We could intercept and emulate, but that's opening several security cans of worms we successfully closed previously, so isn't a viable option.

If the DISABLE controls are used at the same time the DETECT controls, the DISABLE win.  They're faults and block the memory access, meaning there's nothing to trigger the DETECT for ratelimiting purposes.

Anyway, to the bug as reported.

IMO is a Heads bug.

Thierry, you said "but Heads tries to override unsafe defaults from firmware", and while I appreciate that point of view, splitlocks are purely a performance concern, not a correctness concern.

It's not reasonable to ask OS or VMM to turn this stuff off.  It's very model specific to even locate, meaning that we'd have to keep on backporting new model information to existing releases for them to know how to turn it off on new hardware.

It is great that Heads has an option, and I even recommend people trying it out.  I even have a patch to Xen to let an admin try this stuff out too, but people need to be aware that it's invasive into all software including VMs.

i.e. Use of the DISABLE options needs to be opt-in, by people aware of the consequences.  Xen does use of the DETECT options when they're available.

~Andrew

[1] It being userspace-only is my "fault".  I had to remind Intel that there were security vulnerabilities to be had to do with re-entrant #DB's because of how SYSCALL works.   This has now been fixed in FRED, but Bus Lock Detect didn't change behaviour.