On Tue, Sep 16, 2025, Paolo Bonzini wrote: > On 8/27/25 01:04, Roman Kisel wrote: > > On 8/26/2025 5:07 AM, Peter Zijlstra wrote: > > > I do not know what OpenHCL is. Nor is it clear from the code what NMIs > > > can't happen. Anyway, same can be achieved with breakpoints / kprobes. > > > You can get a trap after setting CR2 and scribble it. > > > > > > You simply cannot use CR2 this way. > > > > The code in question runs with interrupts disabled, and the kernel runs > > without the memory swapping when using that module - the kernel is > > a firmware to host a vTPM for virtual machines. Somewhat similar to SMM. > > That should've been reflected somewhere in the comments and in Kconfig, > > we could do better. All in all, the page fault cannot happen in that > > path thus CR2 won't be trashed. > > > > Nor this kind of code can be stepped through in a self-hosted > > kernel debugger like kgdb. There are other examples of such code iiuc: > > As Sean mentioned, you do have to make sure that this is annotated as > noinstr (not instrumentable). And also just use assembly - KVM started with > a similar asm block, though without the sketchy "register asm",
Ooh, yeah, don't use "register asm". I missed that when I peeked at the code. Using "register asm" will most definitely cause problems, because the compiler doesn't track usage in C code, i.e. will happily use the GPR and clobber your asm value in the process. That inevitably leads to very confusing and somewhat transient errors. E.g. if someone inserts a printk for debugging, the call to printk can clobber the very state it's trying to print. > and I was initially skeptical but using a dedicated .S file was absolutely > the right thing to do. +1000 to putting the assembly in a .S file. I too was a bit skeptical about moving the entire sequence into proper assembly; thankfully, some non-KVM folks talked us into it :-)