Jiri Olsa | c5e6319 | 2012-08-07 15:20:36 +0200 | [diff] [blame] | 1 | #include <linux/errno.h> |
| 2 | #include <linux/kernel.h> |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 3 | #include <linux/sched.h> |
| 4 | #include <linux/perf_event.h> |
Jiri Olsa | c5e6319 | 2012-08-07 15:20:36 +0200 | [diff] [blame] | 5 | #include <linux/bug.h> |
| 6 | #include <linux/stddef.h> |
| 7 | #include <asm/perf_regs.h> |
| 8 | #include <asm/ptrace.h> |
| 9 | |
| 10 | #ifdef CONFIG_X86_32 |
| 11 | #define PERF_REG_X86_MAX PERF_REG_X86_32_MAX |
| 12 | #else |
| 13 | #define PERF_REG_X86_MAX PERF_REG_X86_64_MAX |
| 14 | #endif |
| 15 | |
| 16 | #define PT_REGS_OFFSET(id, r) [id] = offsetof(struct pt_regs, r) |
| 17 | |
| 18 | static unsigned int pt_regs_offset[PERF_REG_X86_MAX] = { |
| 19 | PT_REGS_OFFSET(PERF_REG_X86_AX, ax), |
| 20 | PT_REGS_OFFSET(PERF_REG_X86_BX, bx), |
| 21 | PT_REGS_OFFSET(PERF_REG_X86_CX, cx), |
| 22 | PT_REGS_OFFSET(PERF_REG_X86_DX, dx), |
| 23 | PT_REGS_OFFSET(PERF_REG_X86_SI, si), |
| 24 | PT_REGS_OFFSET(PERF_REG_X86_DI, di), |
| 25 | PT_REGS_OFFSET(PERF_REG_X86_BP, bp), |
| 26 | PT_REGS_OFFSET(PERF_REG_X86_SP, sp), |
| 27 | PT_REGS_OFFSET(PERF_REG_X86_IP, ip), |
| 28 | PT_REGS_OFFSET(PERF_REG_X86_FLAGS, flags), |
| 29 | PT_REGS_OFFSET(PERF_REG_X86_CS, cs), |
| 30 | PT_REGS_OFFSET(PERF_REG_X86_SS, ss), |
| 31 | #ifdef CONFIG_X86_32 |
| 32 | PT_REGS_OFFSET(PERF_REG_X86_DS, ds), |
| 33 | PT_REGS_OFFSET(PERF_REG_X86_ES, es), |
| 34 | PT_REGS_OFFSET(PERF_REG_X86_FS, fs), |
| 35 | PT_REGS_OFFSET(PERF_REG_X86_GS, gs), |
| 36 | #else |
| 37 | /* |
| 38 | * The pt_regs struct does not store |
| 39 | * ds, es, fs, gs in 64 bit mode. |
| 40 | */ |
| 41 | (unsigned int) -1, |
| 42 | (unsigned int) -1, |
| 43 | (unsigned int) -1, |
| 44 | (unsigned int) -1, |
| 45 | #endif |
| 46 | #ifdef CONFIG_X86_64 |
| 47 | PT_REGS_OFFSET(PERF_REG_X86_R8, r8), |
| 48 | PT_REGS_OFFSET(PERF_REG_X86_R9, r9), |
| 49 | PT_REGS_OFFSET(PERF_REG_X86_R10, r10), |
| 50 | PT_REGS_OFFSET(PERF_REG_X86_R11, r11), |
| 51 | PT_REGS_OFFSET(PERF_REG_X86_R12, r12), |
| 52 | PT_REGS_OFFSET(PERF_REG_X86_R13, r13), |
| 53 | PT_REGS_OFFSET(PERF_REG_X86_R14, r14), |
| 54 | PT_REGS_OFFSET(PERF_REG_X86_R15, r15), |
| 55 | #endif |
| 56 | }; |
| 57 | |
| 58 | u64 perf_reg_value(struct pt_regs *regs, int idx) |
| 59 | { |
Dan Carpenter | 1e6dd8a | 2012-09-05 15:31:26 +0300 | [diff] [blame] | 60 | if (WARN_ON_ONCE(idx >= ARRAY_SIZE(pt_regs_offset))) |
Jiri Olsa | c5e6319 | 2012-08-07 15:20:36 +0200 | [diff] [blame] | 61 | return 0; |
| 62 | |
| 63 | return regs_get_register(regs, pt_regs_offset[idx]); |
| 64 | } |
| 65 | |
| 66 | #define REG_RESERVED (~((1ULL << PERF_REG_X86_MAX) - 1ULL)) |
| 67 | |
| 68 | #ifdef CONFIG_X86_32 |
| 69 | int perf_reg_validate(u64 mask) |
| 70 | { |
| 71 | if (!mask || mask & REG_RESERVED) |
| 72 | return -EINVAL; |
| 73 | |
| 74 | return 0; |
| 75 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 76 | |
| 77 | u64 perf_reg_abi(struct task_struct *task) |
| 78 | { |
| 79 | return PERF_SAMPLE_REGS_ABI_32; |
| 80 | } |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 81 | |
| 82 | void perf_get_regs_user(struct perf_regs *regs_user, |
| 83 | struct pt_regs *regs, |
| 84 | struct pt_regs *regs_user_copy) |
| 85 | { |
| 86 | regs_user->regs = task_pt_regs(current); |
| 87 | regs_user->abi = perf_reg_abi(current); |
| 88 | } |
Jiri Olsa | c5e6319 | 2012-08-07 15:20:36 +0200 | [diff] [blame] | 89 | #else /* CONFIG_X86_64 */ |
| 90 | #define REG_NOSUPPORT ((1ULL << PERF_REG_X86_DS) | \ |
| 91 | (1ULL << PERF_REG_X86_ES) | \ |
| 92 | (1ULL << PERF_REG_X86_FS) | \ |
| 93 | (1ULL << PERF_REG_X86_GS)) |
| 94 | |
| 95 | int perf_reg_validate(u64 mask) |
| 96 | { |
| 97 | if (!mask || mask & REG_RESERVED) |
| 98 | return -EINVAL; |
| 99 | |
| 100 | if (mask & REG_NOSUPPORT) |
| 101 | return -EINVAL; |
| 102 | |
| 103 | return 0; |
| 104 | } |
Jiri Olsa | 4018994 | 2012-08-07 15:20:37 +0200 | [diff] [blame] | 105 | |
| 106 | u64 perf_reg_abi(struct task_struct *task) |
| 107 | { |
| 108 | if (test_tsk_thread_flag(task, TIF_IA32)) |
| 109 | return PERF_SAMPLE_REGS_ABI_32; |
| 110 | else |
| 111 | return PERF_SAMPLE_REGS_ABI_64; |
| 112 | } |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 113 | |
| 114 | void perf_get_regs_user(struct perf_regs *regs_user, |
| 115 | struct pt_regs *regs, |
| 116 | struct pt_regs *regs_user_copy) |
| 117 | { |
Andy Lutomirski | 86c269f | 2015-01-04 10:36:20 -0800 | [diff] [blame] | 118 | struct pt_regs *user_regs = task_pt_regs(current); |
| 119 | |
| 120 | /* |
| 121 | * If we're in an NMI that interrupted task_pt_regs setup, then |
| 122 | * we can't sample user regs at all. This check isn't really |
| 123 | * sufficient, though, as we could be in an NMI inside an interrupt |
| 124 | * that happened during task_pt_regs setup. |
| 125 | */ |
| 126 | if (regs->sp > (unsigned long)&user_regs->r11 && |
| 127 | regs->sp <= (unsigned long)(user_regs + 1)) { |
| 128 | regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; |
| 129 | regs_user->regs = NULL; |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | /* |
Denys Vlasenko | aa21df0 | 2015-04-10 15:06:56 +0200 | [diff] [blame^] | 134 | * These registers are always saved on 64-bit syscall entry. |
| 135 | * On 32-bit entry points, they are saved too except r8..r11. |
Andy Lutomirski | 86c269f | 2015-01-04 10:36:20 -0800 | [diff] [blame] | 136 | */ |
| 137 | regs_user_copy->ip = user_regs->ip; |
| 138 | regs_user_copy->cx = user_regs->cx; |
| 139 | regs_user_copy->dx = user_regs->dx; |
| 140 | regs_user_copy->si = user_regs->si; |
| 141 | regs_user_copy->di = user_regs->di; |
| 142 | regs_user_copy->r8 = user_regs->r8; |
| 143 | regs_user_copy->r9 = user_regs->r9; |
| 144 | regs_user_copy->r10 = user_regs->r10; |
| 145 | regs_user_copy->r11 = user_regs->r11; |
| 146 | regs_user_copy->orig_ax = user_regs->orig_ax; |
| 147 | regs_user_copy->flags = user_regs->flags; |
Denys Vlasenko | aa21df0 | 2015-04-10 15:06:56 +0200 | [diff] [blame^] | 148 | regs_user_copy->sp = user_regs->sp; |
| 149 | regs_user_copy->cs = user_regs->cs; |
| 150 | regs_user_copy->ss = user_regs->ss; |
Andy Lutomirski | 86c269f | 2015-01-04 10:36:20 -0800 | [diff] [blame] | 151 | |
| 152 | /* |
Denys Vlasenko | aa21df0 | 2015-04-10 15:06:56 +0200 | [diff] [blame^] | 153 | * Most system calls don't save these registers, don't report them. |
Andy Lutomirski | 86c269f | 2015-01-04 10:36:20 -0800 | [diff] [blame] | 154 | */ |
| 155 | regs_user_copy->bx = -1; |
| 156 | regs_user_copy->bp = -1; |
| 157 | regs_user_copy->r12 = -1; |
| 158 | regs_user_copy->r13 = -1; |
| 159 | regs_user_copy->r14 = -1; |
| 160 | regs_user_copy->r15 = -1; |
| 161 | |
| 162 | /* |
| 163 | * For this to be at all useful, we need a reasonable guess for |
Denys Vlasenko | aa21df0 | 2015-04-10 15:06:56 +0200 | [diff] [blame^] | 164 | * the ABI. Be careful: we're in NMI context, and we're |
Andy Lutomirski | 86c269f | 2015-01-04 10:36:20 -0800 | [diff] [blame] | 165 | * considering current to be the current task, so we should |
| 166 | * be careful not to look at any other percpu variables that might |
| 167 | * change during context switches. |
| 168 | */ |
| 169 | if (IS_ENABLED(CONFIG_IA32_EMULATION) && |
| 170 | task_thread_info(current)->status & TS_COMPAT) { |
| 171 | /* Easy case: we're in a compat syscall. */ |
| 172 | regs_user->abi = PERF_SAMPLE_REGS_ABI_32; |
Andy Lutomirski | 86c269f | 2015-01-04 10:36:20 -0800 | [diff] [blame] | 173 | } else if (user_regs->orig_ax != -1) { |
| 174 | /* |
| 175 | * We're probably in a 64-bit syscall. |
| 176 | * Warning: this code is severely racy. At least it's better |
| 177 | * than just blindly copying user_regs. |
| 178 | */ |
| 179 | regs_user->abi = PERF_SAMPLE_REGS_ABI_64; |
Denys Vlasenko | aa21df0 | 2015-04-10 15:06:56 +0200 | [diff] [blame^] | 180 | /* usually contains return address (same as ->ip) */ |
| 181 | regs_user_copy->cx = -1; |
Andy Lutomirski | 86c269f | 2015-01-04 10:36:20 -0800 | [diff] [blame] | 182 | } else { |
| 183 | /* We're probably in an interrupt or exception. */ |
| 184 | regs_user->abi = user_64bit_mode(user_regs) ? |
| 185 | PERF_SAMPLE_REGS_ABI_64 : PERF_SAMPLE_REGS_ABI_32; |
Andy Lutomirski | 86c269f | 2015-01-04 10:36:20 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | regs_user->regs = regs_user_copy; |
Andy Lutomirski | 88a7c26 | 2015-01-04 10:36:19 -0800 | [diff] [blame] | 189 | } |
Jiri Olsa | c5e6319 | 2012-08-07 15:20:36 +0200 | [diff] [blame] | 190 | #endif /* CONFIG_X86_32 */ |