[RFC PATCH 6/7] x86/kexec: Debugging support: Dump registers on exception

H. Peter Anvin hpa at zytor.com
Tue Nov 5 14:27:50 PST 2024


On 11/5/24 13:58, H. Peter Anvin wrote:
> 
> Then you can just do the obvious (have your assembly stub point %rdi to 
> the base of the register dump; set the frame order to whatever you'd 
> like, except rip/err/exc, or reverse the order if you prefer by changing 
> the loop):
> 
> static inline __noreturn void die(void)
> {
>      while (1)
>          asm volatile("hlt");
> }
> 
> void dump_register_frame(const unsigned long frame[])
> {
>      static const char regnames[][5] = {
>          "rax:", "rcx:", "rdx:", "rbx:",
>          "rsp:", "rbp:", "rsi:", "rdi:",
>          "r8: ", "r9: ", "r10:", "r11:",
>          "r12:", "r13:", "r14:", "r15:",
>          "cr2:", "Exc:", "Err:", "rip:"
>      };
> 
>      for (size_t i = 0; i < ARRAY_SIZE(regnames); i++) {
>          __putstr(regnames[i]);
>          __puthex(frame[i]);
>          __putstr("\n");
>      }
> 
>      /* Only return from int3 */
>      if (frame[17] != 3)
>          die();
> }
> 

I don't know if it is necessary, but you can do something like this to 
make absolutely sure you don't end up with non-relative symbol references:

static inline __constfunc void * sym_addr(const void *sym)
{
	void *addr;
	asm("lea %c1(%%rip),%0" : "=r" (addr) : "i" (sym));
	return addr;
}




More information about the kexec mailing list