microsoft / OHCL-Linux-Kernel

Underhill Linux Kernel
Other
10 stars 4 forks source link

[RFC] hv, vtl: Fix the frame pointer build warning #23

Closed yjiang5 closed 1 month ago

yjiang5 commented 2 months ago

Currently there are stack frame pointer warning as following.

drivers/hv/mshv_vtl_main.o: warning: objtool: mshv_vtl_return_tdx+0x22d: call without frame pointer save/setup

Such warning comes from the "pushq %%rbp" and "popq %%rbp" instructions around the tdcall.

When CONFIG_FRAME_POINTER is enabled, the objtools expects the stack frame pointer is setup as shown in has_valid_stack_frame() function:

static bool has_valid_stack_frame(struct insn_state state) { struct cfi_state cfi = &state->cfi;

if (cfi->cfa.base == CFI_BP &&
    check_reg_frame_pos(&cfi->regs[CFI_BP], -cfi->cfa.offset) &&
    check_reg_frame_pos(&cfi->regs[CFI_RA], -cfi->cfa.offset + 8))
    return true;

if (cfi->drap && cfi->regs[CFI_BP].base == CFI_BP)
    return true;

return false;

}

However, the "popq %%rbp" instruction confuses the objtools. As shown in update_cfi_state(), the CFA's base is changed to SP after the "pop %rbp" instuction. case OP_SRC_POP: case OP_SRC_POPF: if (!cfi->drap && op->dest.reg == cfa->base) {

        /* pop %rbp */
        cfa->base = CFI_SP;
    }

This cause the has_valid_stack_frame() fail in the next function call kernel_fpu_end().

Add UNWIND_HINT_SAVE/UNWIND_HINT_RESTORE before and after the inline assembly code, so that objtools will save the CFI information before the "pushq %rbp" and restore it after "popq %rbp".

chris-oo commented 2 months ago

This looks good to me, but I'm not an expert on how unwinding works in Linux... Is there anyone else who has expertise in this area?

romank-msft commented 2 months ago

This looks short and sweet!

Another way of fixing this would be where the benefits include the compiler deciding when to save/restore rbp (potentially more room for optimization) and less assembly:

yjiang5 commented 2 months ago

Sure, I can have a try on this. I have some question/concern on the assembly code, but I can try to see if it works and then back to you.