1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Performance counter callchain support - powerpc architecture code 4 * 5 * Copyright © 2009 Paul Mackerras, IBM Corporation. 6 */ 7 #include <linux/kernel.h> 8 #include <linux/sched.h> 9 #include <linux/perf_event.h> 10 #include <linux/percpu.h> 11 #include <linux/uaccess.h> 12 #include <linux/mm.h> 13 #include <asm/ptrace.h> 14 #include <asm/pgtable.h> 15 #include <asm/sigcontext.h> 16 #include <asm/ucontext.h> 17 #include <asm/vdso.h> 18 #ifdef CONFIG_PPC64 19 #include "../kernel/ppc32.h" 20 #endif 21 #include <asm/pte-walk.h> 22 23 24 /* 25 * Is sp valid as the address of the next kernel stack frame after prev_sp? 26 * The next frame may be in a different stack area but should not go 27 * back down in the same stack area. 28 */ 29 static int valid_next_sp(unsigned long sp, unsigned long prev_sp) 30 { 31 if (sp & 0xf) 32 return 0; /* must be 16-byte aligned */ 33 if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD)) 34 return 0; 35 if (sp >= prev_sp + STACK_FRAME_MIN_SIZE) 36 return 1; 37 /* 38 * sp could decrease when we jump off an interrupt stack 39 * back to the regular process stack. 40 */ 41 if ((sp & ~(THREAD_SIZE - 1)) != (prev_sp & ~(THREAD_SIZE - 1))) 42 return 1; 43 return 0; 44 } 45 46 void 47 perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) 48 { 49 unsigned long sp, next_sp; 50 unsigned long next_ip; 51 unsigned long lr; 52 long level = 0; 53 unsigned long *fp; 54 55 lr = regs->link; 56 sp = regs->gpr[1]; 57 perf_callchain_store(entry, perf_instruction_pointer(regs)); 58 59 if (!validate_sp(sp, current, STACK_FRAME_OVERHEAD)) 60 return; 61 62 for (;;) { 63 fp = (unsigned long *) sp; 64 next_sp = fp[0]; 65 66 if (next_sp == sp + STACK_INT_FRAME_SIZE && 67 fp[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) { 68 /* 69 * This looks like an interrupt frame for an 70 * interrupt that occurred in the kernel 71 */ 72 regs = (struct pt_regs *)(sp + STACK_FRAME_OVERHEAD); 73 next_ip = regs->nip; 74 lr = regs->link; 75 level = 0; 76 perf_callchain_store_context(entry, PERF_CONTEXT_KERNEL); 77 78 } else { 79 if (level == 0) 80 next_ip = lr; 81 else 82 next_ip = fp[STACK_FRAME_LR_SAVE]; 83 84 /* 85 * We can't tell which of the first two addresses 86 * we get are valid, but we can filter out the 87 * obviously bogus ones here. We replace them 88 * with 0 rather than removing them entirely so 89 * that userspace can tell which is which. 90 */ 91 if ((level == 1 && next_ip == lr) || 92 (level <= 1 && !kernel_text_address(next_ip))) 93 next_ip = 0; 94 95 ++level; 96 } 97 98 perf_callchain_store(entry, next_ip); 99 if (!valid_next_sp(next_sp, sp)) 100 return; 101 sp = next_sp; 102 } 103 } 104 105 #ifdef CONFIG_PPC64 106 /* 107 * On 64-bit we don't want to invoke hash_page on user addresses from 108 * interrupt context, so if the access faults, we read the page tables 109 * to find which page (if any) is mapped and access it directly. 110 */ 111 static int read_user_stack_slow(void __user *ptr, void *buf, int nb) 112 { 113 int ret = -EFAULT; 114 pgd_t *pgdir; 115 pte_t *ptep, pte; 116 unsigned shift; 117 unsigned long addr = (unsigned long) ptr; 118 unsigned long offset; 119 unsigned long pfn, flags; 120 void *kaddr; 121 122 pgdir = current->mm->pgd; 123 if (!pgdir) 124 return -EFAULT; 125 126 local_irq_save(flags); 127 ptep = find_current_mm_pte(pgdir, addr, NULL, &shift); 128 if (!ptep) 129 goto err_out; 130 if (!shift) 131 shift = PAGE_SHIFT; 132 133 /* align address to page boundary */ 134 offset = addr & ((1UL << shift) - 1); 135 136 pte = READ_ONCE(*ptep); 137 if (!pte_present(pte) || !pte_user(pte)) 138 goto err_out; 139 pfn = pte_pfn(pte); 140 if (!page_is_ram(pfn)) 141 goto err_out; 142 143 /* no highmem to worry about here */ 144 kaddr = pfn_to_kaddr(pfn); 145 memcpy(buf, kaddr + offset, nb); 146 ret = 0; 147 err_out: 148 local_irq_restore(flags); 149 return ret; 150 } 151 152 static int read_user_stack_64(unsigned long __user *ptr, unsigned long *ret) 153 { 154 if ((unsigned long)ptr > TASK_SIZE - sizeof(unsigned long) || 155 ((unsigned long)ptr & 7)) 156 return -EFAULT; 157 158 pagefault_disable(); 159 if (!__get_user_inatomic(*ret, ptr)) { 160 pagefault_enable(); 161 return 0; 162 } 163 pagefault_enable(); 164 165 return read_user_stack_slow(ptr, ret, 8); 166 } 167 168 static int read_user_stack_32(unsigned int __user *ptr, unsigned int *ret) 169 { 170 if ((unsigned long)ptr > TASK_SIZE - sizeof(unsigned int) || 171 ((unsigned long)ptr & 3)) 172 return -EFAULT; 173 174 pagefault_disable(); 175 if (!__get_user_inatomic(*ret, ptr)) { 176 pagefault_enable(); 177 return 0; 178 } 179 pagefault_enable(); 180 181 return read_user_stack_slow(ptr, ret, 4); 182 } 183 184 static inline int valid_user_sp(unsigned long sp, int is_64) 185 { 186 if (!sp || (sp & 7) || sp > (is_64 ? TASK_SIZE : 0x100000000UL) - 32) 187 return 0; 188 return 1; 189 } 190 191 /* 192 * 64-bit user processes use the same stack frame for RT and non-RT signals. 193 */ 194 struct signal_frame_64 { 195 char dummy[__SIGNAL_FRAMESIZE]; 196 struct ucontext uc; 197 unsigned long unused[2]; 198 unsigned int tramp[6]; 199 struct siginfo *pinfo; 200 void *puc; 201 struct siginfo info; 202 char abigap[288]; 203 }; 204 205 static int is_sigreturn_64_address(unsigned long nip, unsigned long fp) 206 { 207 if (nip == fp + offsetof(struct signal_frame_64, tramp)) 208 return 1; 209 if (vdso64_rt_sigtramp && current->mm->context.vdso_base && 210 nip == current->mm->context.vdso_base + vdso64_rt_sigtramp) 211 return 1; 212 return 0; 213 } 214 215 /* 216 * Do some sanity checking on the signal frame pointed to by sp. 217 * We check the pinfo and puc pointers in the frame. 218 */ 219 static int sane_signal_64_frame(unsigned long sp) 220 { 221 struct signal_frame_64 __user *sf; 222 unsigned long pinfo, puc; 223 224 sf = (struct signal_frame_64 __user *) sp; 225 if (read_user_stack_64((unsigned long __user *) &sf->pinfo, &pinfo) || 226 read_user_stack_64((unsigned long __user *) &sf->puc, &puc)) 227 return 0; 228 return pinfo == (unsigned long) &sf->info && 229 puc == (unsigned long) &sf->uc; 230 } 231 232 static void perf_callchain_user_64(struct perf_callchain_entry_ctx *entry, 233 struct pt_regs *regs) 234 { 235 unsigned long sp, next_sp; 236 unsigned long next_ip; 237 unsigned long lr; 238 long level = 0; 239 struct signal_frame_64 __user *sigframe; 240 unsigned long __user *fp, *uregs; 241 242 next_ip = perf_instruction_pointer(regs); 243 lr = regs->link; 244 sp = regs->gpr[1]; 245 perf_callchain_store(entry, next_ip); 246 247 while (entry->nr < entry->max_stack) { 248 fp = (unsigned long __user *) sp; 249 if (!valid_user_sp(sp, 1) || read_user_stack_64(fp, &next_sp)) 250 return; 251 if (level > 0 && read_user_stack_64(&fp[2], &next_ip)) 252 return; 253 254 /* 255 * Note: the next_sp - sp >= signal frame size check 256 * is true when next_sp < sp, which can happen when 257 * transitioning from an alternate signal stack to the 258 * normal stack. 259 */ 260 if (next_sp - sp >= sizeof(struct signal_frame_64) && 261 (is_sigreturn_64_address(next_ip, sp) || 262 (level <= 1 && is_sigreturn_64_address(lr, sp))) && 263 sane_signal_64_frame(sp)) { 264 /* 265 * This looks like an signal frame 266 */ 267 sigframe = (struct signal_frame_64 __user *) sp; 268 uregs = sigframe->uc.uc_mcontext.gp_regs; 269 if (read_user_stack_64(&uregs[PT_NIP], &next_ip) || 270 read_user_stack_64(&uregs[PT_LNK], &lr) || 271 read_user_stack_64(&uregs[PT_R1], &sp)) 272 return; 273 level = 0; 274 perf_callchain_store_context(entry, PERF_CONTEXT_USER); 275 perf_callchain_store(entry, next_ip); 276 continue; 277 } 278 279 if (level == 0) 280 next_ip = lr; 281 perf_callchain_store(entry, next_ip); 282 ++level; 283 sp = next_sp; 284 } 285 } 286 287 #else /* CONFIG_PPC64 */ 288 /* 289 * On 32-bit we just access the address and let hash_page create a 290 * HPTE if necessary, so there is no need to fall back to reading 291 * the page tables. Since this is called at interrupt level, 292 * do_page_fault() won't treat a DSI as a page fault. 293 */ 294 static int read_user_stack_32(unsigned int __user *ptr, unsigned int *ret) 295 { 296 int rc; 297 298 if ((unsigned long)ptr > TASK_SIZE - sizeof(unsigned int) || 299 ((unsigned long)ptr & 3)) 300 return -EFAULT; 301 302 pagefault_disable(); 303 rc = __get_user_inatomic(*ret, ptr); 304 pagefault_enable(); 305 306 return rc; 307 } 308 309 static inline void perf_callchain_user_64(struct perf_callchain_entry_ctx *entry, 310 struct pt_regs *regs) 311 { 312 } 313 314 static inline int valid_user_sp(unsigned long sp, int is_64) 315 { 316 if (!sp || (sp & 7) || sp > TASK_SIZE - 32) 317 return 0; 318 return 1; 319 } 320 321 #define __SIGNAL_FRAMESIZE32 __SIGNAL_FRAMESIZE 322 #define sigcontext32 sigcontext 323 #define mcontext32 mcontext 324 #define ucontext32 ucontext 325 #define compat_siginfo_t struct siginfo 326 327 #endif /* CONFIG_PPC64 */ 328 329 /* 330 * Layout for non-RT signal frames 331 */ 332 struct signal_frame_32 { 333 char dummy[__SIGNAL_FRAMESIZE32]; 334 struct sigcontext32 sctx; 335 struct mcontext32 mctx; 336 int abigap[56]; 337 }; 338 339 /* 340 * Layout for RT signal frames 341 */ 342 struct rt_signal_frame_32 { 343 char dummy[__SIGNAL_FRAMESIZE32 + 16]; 344 compat_siginfo_t info; 345 struct ucontext32 uc; 346 int abigap[56]; 347 }; 348 349 static int is_sigreturn_32_address(unsigned int nip, unsigned int fp) 350 { 351 if (nip == fp + offsetof(struct signal_frame_32, mctx.mc_pad)) 352 return 1; 353 if (vdso32_sigtramp && current->mm->context.vdso_base && 354 nip == current->mm->context.vdso_base + vdso32_sigtramp) 355 return 1; 356 return 0; 357 } 358 359 static int is_rt_sigreturn_32_address(unsigned int nip, unsigned int fp) 360 { 361 if (nip == fp + offsetof(struct rt_signal_frame_32, 362 uc.uc_mcontext.mc_pad)) 363 return 1; 364 if (vdso32_rt_sigtramp && current->mm->context.vdso_base && 365 nip == current->mm->context.vdso_base + vdso32_rt_sigtramp) 366 return 1; 367 return 0; 368 } 369 370 static int sane_signal_32_frame(unsigned int sp) 371 { 372 struct signal_frame_32 __user *sf; 373 unsigned int regs; 374 375 sf = (struct signal_frame_32 __user *) (unsigned long) sp; 376 if (read_user_stack_32((unsigned int __user *) &sf->sctx.regs, ®s)) 377 return 0; 378 return regs == (unsigned long) &sf->mctx; 379 } 380 381 static int sane_rt_signal_32_frame(unsigned int sp) 382 { 383 struct rt_signal_frame_32 __user *sf; 384 unsigned int regs; 385 386 sf = (struct rt_signal_frame_32 __user *) (unsigned long) sp; 387 if (read_user_stack_32((unsigned int __user *) &sf->uc.uc_regs, ®s)) 388 return 0; 389 return regs == (unsigned long) &sf->uc.uc_mcontext; 390 } 391 392 static unsigned int __user *signal_frame_32_regs(unsigned int sp, 393 unsigned int next_sp, unsigned int next_ip) 394 { 395 struct mcontext32 __user *mctx = NULL; 396 struct signal_frame_32 __user *sf; 397 struct rt_signal_frame_32 __user *rt_sf; 398 399 /* 400 * Note: the next_sp - sp >= signal frame size check 401 * is true when next_sp < sp, for example, when 402 * transitioning from an alternate signal stack to the 403 * normal stack. 404 */ 405 if (next_sp - sp >= sizeof(struct signal_frame_32) && 406 is_sigreturn_32_address(next_ip, sp) && 407 sane_signal_32_frame(sp)) { 408 sf = (struct signal_frame_32 __user *) (unsigned long) sp; 409 mctx = &sf->mctx; 410 } 411 412 if (!mctx && next_sp - sp >= sizeof(struct rt_signal_frame_32) && 413 is_rt_sigreturn_32_address(next_ip, sp) && 414 sane_rt_signal_32_frame(sp)) { 415 rt_sf = (struct rt_signal_frame_32 __user *) (unsigned long) sp; 416 mctx = &rt_sf->uc.uc_mcontext; 417 } 418 419 if (!mctx) 420 return NULL; 421 return mctx->mc_gregs; 422 } 423 424 static void perf_callchain_user_32(struct perf_callchain_entry_ctx *entry, 425 struct pt_regs *regs) 426 { 427 unsigned int sp, next_sp; 428 unsigned int next_ip; 429 unsigned int lr; 430 long level = 0; 431 unsigned int __user *fp, *uregs; 432 433 next_ip = perf_instruction_pointer(regs); 434 lr = regs->link; 435 sp = regs->gpr[1]; 436 perf_callchain_store(entry, next_ip); 437 438 while (entry->nr < entry->max_stack) { 439 fp = (unsigned int __user *) (unsigned long) sp; 440 if (!valid_user_sp(sp, 0) || read_user_stack_32(fp, &next_sp)) 441 return; 442 if (level > 0 && read_user_stack_32(&fp[1], &next_ip)) 443 return; 444 445 uregs = signal_frame_32_regs(sp, next_sp, next_ip); 446 if (!uregs && level <= 1) 447 uregs = signal_frame_32_regs(sp, next_sp, lr); 448 if (uregs) { 449 /* 450 * This looks like an signal frame, so restart 451 * the stack trace with the values in it. 452 */ 453 if (read_user_stack_32(&uregs[PT_NIP], &next_ip) || 454 read_user_stack_32(&uregs[PT_LNK], &lr) || 455 read_user_stack_32(&uregs[PT_R1], &sp)) 456 return; 457 level = 0; 458 perf_callchain_store_context(entry, PERF_CONTEXT_USER); 459 perf_callchain_store(entry, next_ip); 460 continue; 461 } 462 463 if (level == 0) 464 next_ip = lr; 465 perf_callchain_store(entry, next_ip); 466 ++level; 467 sp = next_sp; 468 } 469 } 470 471 void 472 perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) 473 { 474 if (!is_32bit_task()) 475 perf_callchain_user_64(entry, regs); 476 else 477 perf_callchain_user_32(entry, regs); 478 } 479