Re: [PATCH v2 4/4] ftrace: arm64: move from REGS to ARGS

From: Mark Rutland
Date: Thu Nov 17 2022 - 05:52:49 EST


On Tue, Nov 15, 2022 at 11:27:03AM +0000, Will Deacon wrote:
> On Thu, Nov 03, 2022 at 05:05:20PM +0000, Mark Rutland wrote:
> > This commit replaces arm64's support for FTRACE_WITH_REGS with support
> > for FTRACE_WITH_ARGS. This removes some overhead and complexity, and
> > removes some latent issues with inconsistent presentation of struct
> > pt_regs (which can only be reliably saved/restored at exception
> > boundaries).
>
> [...]
>
> > @@ -78,10 +77,71 @@ static inline unsigned long ftrace_call_adjust(unsigned long addr)
> > return addr;
> > }
> >
> > -#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
> > +#ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
> > struct dyn_ftrace;
> > struct ftrace_ops;
> > -struct ftrace_regs;
> > +
> > +#define arch_ftrace_get_regs(regs) NULL
> > +
> > +struct ftrace_regs {
> > + /* x0 - x8 */
> > + unsigned long regs[9];
> > + unsigned long __unused;
> > +
> > + unsigned long fp;
> > + unsigned long lr;
> > +
> > + unsigned long sp;
> > + unsigned long pc;
> > +};
> > +
> > +static __always_inline unsigned long
> > +ftrace_regs_get_instruction_pointer(const struct ftrace_regs *fregs)
> > +{
> > + return fregs->pc;
> > +}
> > +
> > +static __always_inline void
> > +ftrace_regs_set_instruction_pointer(struct ftrace_regs *fregs,
> > + unsigned long pc)
> > +{
> > + fregs->pc = pc;
> > +}
> > +
> > +static __always_inline unsigned long
> > +ftrace_regs_get_stack_pointer(const struct ftrace_regs *fregs)
> > +{
> > + return fregs->sp;
> > +}
> > +
> > +static __always_inline unsigned long
> > +ftrace_regs_get_argument(struct ftrace_regs *fregs, unsigned int n)
> > +{
> > + if (n < 8)
> > + return fregs->regs[n];
>
> Where does this '8' come from?

Because in AAPCS64 the arguments are in x0 to x7, as mentioned in the commit
message:

| Per AAPCS64, all function call argument and return values are held in
| the following GPRs:
|
| * X0 - X7 : parameter / result registers
| * X8 : indirect result location register
| * SP : stack pointer (AKA SP)

The 'indirect result location register' would be used when returning large
structures, and isn't a function argument as such.

The logic is the same as in regs_get_kernel_argument() for pt_regs.

I can add a comment here to explain that, if that would help?

The rest of the registers are as described in the commit message (and I now
spot a typo that I'll go fix):

| Additionally, ad function call boundaries, the following GPRs hold
| context/return information:
|
| * X29 : frame pointer (AKA FP)
| * X30 : link register (AKA LR)
|
| ... and for ftrace we need to capture the instrumented address:
|
| * PC : program counter
|
| No other GPRs are relevant, as none of the other arguments hold
| parameters or return values:
|
| * X9 - X17 : temporaries, may be clobbered
| * X18 : shadow call stack pointer (or temorary)
| * X19 - X28 : callee saved

Thanks,
Mark.