1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 20d55303cSDeepa Dinamani #include <linux/compat.h> 32ee0d7fdSJean Pihet #include <linux/errno.h> 42ee0d7fdSJean Pihet #include <linux/kernel.h> 52ee0d7fdSJean Pihet #include <linux/perf_event.h> 62ee0d7fdSJean Pihet #include <linux/bug.h> 768db0cf1SIngo Molnar #include <linux/sched/task_stack.h> 8ff268ff7SMark Salter 92ee0d7fdSJean Pihet #include <asm/perf_regs.h> 102ee0d7fdSJean Pihet #include <asm/ptrace.h> 112ee0d7fdSJean Pihet 122ee0d7fdSJean Pihet u64 perf_reg_value(struct pt_regs *regs, int idx) 132ee0d7fdSJean Pihet { 142ee0d7fdSJean Pihet if (WARN_ON_ONCE((u32)idx >= PERF_REG_ARM64_MAX)) 152ee0d7fdSJean Pihet return 0; 162ee0d7fdSJean Pihet 172ee0d7fdSJean Pihet /* 188dfe804aSJiping Ma * Our handling of compat tasks (PERF_SAMPLE_REGS_ABI_32) is weird, but 19*ad14c192SXiaoming Ni * we're stuck with it for ABI compatibility reasons. 208dfe804aSJiping Ma * 218dfe804aSJiping Ma * For a 32-bit consumer inspecting a 32-bit task, then it will look at 228dfe804aSJiping Ma * the first 16 registers (see arch/arm/include/uapi/asm/perf_regs.h). 238dfe804aSJiping Ma * These correspond directly to a prefix of the registers saved in our 248dfe804aSJiping Ma * 'struct pt_regs', with the exception of the PC, so we copy that down 258dfe804aSJiping Ma * (x15 corresponds to SP_hyp in the architecture). 268dfe804aSJiping Ma * 278dfe804aSJiping Ma * So far, so good. 288dfe804aSJiping Ma * 298dfe804aSJiping Ma * The oddity arises when a 64-bit consumer looks at a 32-bit task and 308dfe804aSJiping Ma * asks for registers beyond PERF_REG_ARM_MAX. In this case, we return 318dfe804aSJiping Ma * SP_usr, LR_usr and PC in the positions where the AArch64 SP, LR and 328dfe804aSJiping Ma * PC registers would normally live. The initial idea was to allow a 338dfe804aSJiping Ma * 64-bit unwinder to unwind a 32-bit task and, although it's not clear 348dfe804aSJiping Ma * how well that works in practice, somebody might be relying on it. 358dfe804aSJiping Ma * 368dfe804aSJiping Ma * At the time we make a sample, we don't know whether the consumer is 378dfe804aSJiping Ma * 32-bit or 64-bit, so we have to cater for both possibilities. 382ee0d7fdSJean Pihet */ 392ee0d7fdSJean Pihet if (compat_user_mode(regs)) { 402ee0d7fdSJean Pihet if ((u32)idx == PERF_REG_ARM64_SP) 412ee0d7fdSJean Pihet return regs->compat_sp; 422ee0d7fdSJean Pihet if ((u32)idx == PERF_REG_ARM64_LR) 432ee0d7fdSJean Pihet return regs->compat_lr; 448dfe804aSJiping Ma if (idx == 15) 458dfe804aSJiping Ma return regs->pc; 462ee0d7fdSJean Pihet } 472ee0d7fdSJean Pihet 485b75a6afSWill Deacon if ((u32)idx == PERF_REG_ARM64_SP) 495b75a6afSWill Deacon return regs->sp; 505b75a6afSWill Deacon 515b75a6afSWill Deacon if ((u32)idx == PERF_REG_ARM64_PC) 525b75a6afSWill Deacon return regs->pc; 535b75a6afSWill Deacon 542ee0d7fdSJean Pihet return regs->regs[idx]; 552ee0d7fdSJean Pihet } 562ee0d7fdSJean Pihet 572ee0d7fdSJean Pihet #define REG_RESERVED (~((1ULL << PERF_REG_ARM64_MAX) - 1)) 582ee0d7fdSJean Pihet 592ee0d7fdSJean Pihet int perf_reg_validate(u64 mask) 602ee0d7fdSJean Pihet { 612ee0d7fdSJean Pihet if (!mask || mask & REG_RESERVED) 622ee0d7fdSJean Pihet return -EINVAL; 632ee0d7fdSJean Pihet 642ee0d7fdSJean Pihet return 0; 652ee0d7fdSJean Pihet } 662ee0d7fdSJean Pihet 672ee0d7fdSJean Pihet u64 perf_reg_abi(struct task_struct *task) 682ee0d7fdSJean Pihet { 692ee0d7fdSJean Pihet if (is_compat_thread(task_thread_info(task))) 702ee0d7fdSJean Pihet return PERF_SAMPLE_REGS_ABI_32; 712ee0d7fdSJean Pihet else 722ee0d7fdSJean Pihet return PERF_SAMPLE_REGS_ABI_64; 732ee0d7fdSJean Pihet } 7488a7c26aSAndy Lutomirski 7588a7c26aSAndy Lutomirski void perf_get_regs_user(struct perf_regs *regs_user, 7688a7c26aSAndy Lutomirski struct pt_regs *regs, 7788a7c26aSAndy Lutomirski struct pt_regs *regs_user_copy) 7888a7c26aSAndy Lutomirski { 7988a7c26aSAndy Lutomirski regs_user->regs = task_pt_regs(current); 8088a7c26aSAndy Lutomirski regs_user->abi = perf_reg_abi(current); 8188a7c26aSAndy Lutomirski } 82