1 /* 2 * Copyright 2016 Anju T, IBM Corporation. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 7 * 2 of the License, or (at your option) any later version. 8 */ 9 10 #include <linux/errno.h> 11 #include <linux/kernel.h> 12 #include <linux/sched.h> 13 #include <linux/sched/task_stack.h> 14 #include <linux/perf_event.h> 15 #include <linux/bug.h> 16 #include <linux/stddef.h> 17 #include <asm/ptrace.h> 18 #include <asm/perf_regs.h> 19 20 #define PT_REGS_OFFSET(id, r) [id] = offsetof(struct pt_regs, r) 21 22 #define REG_RESERVED (~((1ULL << PERF_REG_POWERPC_MAX) - 1)) 23 24 static unsigned int pt_regs_offset[PERF_REG_POWERPC_MAX] = { 25 PT_REGS_OFFSET(PERF_REG_POWERPC_R0, gpr[0]), 26 PT_REGS_OFFSET(PERF_REG_POWERPC_R1, gpr[1]), 27 PT_REGS_OFFSET(PERF_REG_POWERPC_R2, gpr[2]), 28 PT_REGS_OFFSET(PERF_REG_POWERPC_R3, gpr[3]), 29 PT_REGS_OFFSET(PERF_REG_POWERPC_R4, gpr[4]), 30 PT_REGS_OFFSET(PERF_REG_POWERPC_R5, gpr[5]), 31 PT_REGS_OFFSET(PERF_REG_POWERPC_R6, gpr[6]), 32 PT_REGS_OFFSET(PERF_REG_POWERPC_R7, gpr[7]), 33 PT_REGS_OFFSET(PERF_REG_POWERPC_R8, gpr[8]), 34 PT_REGS_OFFSET(PERF_REG_POWERPC_R9, gpr[9]), 35 PT_REGS_OFFSET(PERF_REG_POWERPC_R10, gpr[10]), 36 PT_REGS_OFFSET(PERF_REG_POWERPC_R11, gpr[11]), 37 PT_REGS_OFFSET(PERF_REG_POWERPC_R12, gpr[12]), 38 PT_REGS_OFFSET(PERF_REG_POWERPC_R13, gpr[13]), 39 PT_REGS_OFFSET(PERF_REG_POWERPC_R14, gpr[14]), 40 PT_REGS_OFFSET(PERF_REG_POWERPC_R15, gpr[15]), 41 PT_REGS_OFFSET(PERF_REG_POWERPC_R16, gpr[16]), 42 PT_REGS_OFFSET(PERF_REG_POWERPC_R17, gpr[17]), 43 PT_REGS_OFFSET(PERF_REG_POWERPC_R18, gpr[18]), 44 PT_REGS_OFFSET(PERF_REG_POWERPC_R19, gpr[19]), 45 PT_REGS_OFFSET(PERF_REG_POWERPC_R20, gpr[20]), 46 PT_REGS_OFFSET(PERF_REG_POWERPC_R21, gpr[21]), 47 PT_REGS_OFFSET(PERF_REG_POWERPC_R22, gpr[22]), 48 PT_REGS_OFFSET(PERF_REG_POWERPC_R23, gpr[23]), 49 PT_REGS_OFFSET(PERF_REG_POWERPC_R24, gpr[24]), 50 PT_REGS_OFFSET(PERF_REG_POWERPC_R25, gpr[25]), 51 PT_REGS_OFFSET(PERF_REG_POWERPC_R26, gpr[26]), 52 PT_REGS_OFFSET(PERF_REG_POWERPC_R27, gpr[27]), 53 PT_REGS_OFFSET(PERF_REG_POWERPC_R28, gpr[28]), 54 PT_REGS_OFFSET(PERF_REG_POWERPC_R29, gpr[29]), 55 PT_REGS_OFFSET(PERF_REG_POWERPC_R30, gpr[30]), 56 PT_REGS_OFFSET(PERF_REG_POWERPC_R31, gpr[31]), 57 PT_REGS_OFFSET(PERF_REG_POWERPC_NIP, nip), 58 PT_REGS_OFFSET(PERF_REG_POWERPC_MSR, msr), 59 PT_REGS_OFFSET(PERF_REG_POWERPC_ORIG_R3, orig_gpr3), 60 PT_REGS_OFFSET(PERF_REG_POWERPC_CTR, ctr), 61 PT_REGS_OFFSET(PERF_REG_POWERPC_LINK, link), 62 PT_REGS_OFFSET(PERF_REG_POWERPC_XER, xer), 63 PT_REGS_OFFSET(PERF_REG_POWERPC_CCR, ccr), 64 #ifdef CONFIG_PPC64 65 PT_REGS_OFFSET(PERF_REG_POWERPC_SOFTE, softe), 66 #else 67 PT_REGS_OFFSET(PERF_REG_POWERPC_SOFTE, mq), 68 #endif 69 PT_REGS_OFFSET(PERF_REG_POWERPC_TRAP, trap), 70 PT_REGS_OFFSET(PERF_REG_POWERPC_DAR, dar), 71 PT_REGS_OFFSET(PERF_REG_POWERPC_DSISR, dsisr), 72 PT_REGS_OFFSET(PERF_REG_POWERPC_SIER, dar), 73 PT_REGS_OFFSET(PERF_REG_POWERPC_MMCRA, dsisr), 74 }; 75 76 u64 perf_reg_value(struct pt_regs *regs, int idx) 77 { 78 if (WARN_ON_ONCE(idx >= PERF_REG_POWERPC_MAX)) 79 return 0; 80 81 if (idx == PERF_REG_POWERPC_SIER && 82 (IS_ENABLED(CONFIG_FSL_EMB_PERF_EVENT) || 83 IS_ENABLED(CONFIG_PPC32) || 84 !is_sier_available())) 85 return 0; 86 87 if (idx == PERF_REG_POWERPC_MMCRA && 88 (IS_ENABLED(CONFIG_FSL_EMB_PERF_EVENT) || 89 IS_ENABLED(CONFIG_PPC32))) 90 return 0; 91 92 return regs_get_register(regs, pt_regs_offset[idx]); 93 } 94 95 int perf_reg_validate(u64 mask) 96 { 97 if (!mask || mask & REG_RESERVED) 98 return -EINVAL; 99 return 0; 100 } 101 102 u64 perf_reg_abi(struct task_struct *task) 103 { 104 #ifdef CONFIG_PPC64 105 if (!test_tsk_thread_flag(task, TIF_32BIT)) 106 return PERF_SAMPLE_REGS_ABI_64; 107 else 108 #endif 109 return PERF_SAMPLE_REGS_ABI_32; 110 } 111 112 void perf_get_regs_user(struct perf_regs *regs_user, 113 struct pt_regs *regs, 114 struct pt_regs *regs_user_copy) 115 { 116 regs_user->regs = task_pt_regs(current); 117 regs_user->abi = (regs_user->regs) ? perf_reg_abi(current) : 118 PERF_SAMPLE_REGS_ABI_NONE; 119 } 120