| .. | .. | 
|---|
| 29 | 29 |   | 
|---|
| 30 | 30 |  static struct pt_regs exec_summary_regs; | 
|---|
| 31 | 31 |   | 
|---|
| 32 |  | -bool in_task_stack(unsigned long *stack, struct task_struct *task,  | 
|---|
| 33 |  | -		   struct stack_info *info)  | 
|---|
 | 32 | +bool noinstr in_task_stack(unsigned long *stack, struct task_struct *task,  | 
|---|
 | 33 | +			   struct stack_info *info)  | 
|---|
| 34 | 34 |  { | 
|---|
| 35 | 35 |  	unsigned long *begin = task_stack_page(task); | 
|---|
| 36 | 36 |  	unsigned long *end   = task_stack_page(task) + THREAD_SIZE; | 
|---|
| .. | .. | 
|---|
| 46 | 46 |  	return true; | 
|---|
| 47 | 47 |  } | 
|---|
| 48 | 48 |   | 
|---|
| 49 |  | -bool in_entry_stack(unsigned long *stack, struct stack_info *info)  | 
|---|
 | 49 | +/* Called from get_stack_info_noinstr - so must be noinstr too */  | 
|---|
 | 50 | +bool noinstr in_entry_stack(unsigned long *stack, struct stack_info *info)  | 
|---|
| 50 | 51 |  { | 
|---|
| 51 | 52 |  	struct entry_stack *ss = cpu_entry_stack(smp_processor_id()); | 
|---|
| 52 | 53 |   | 
|---|
| .. | .. | 
|---|
| 65 | 66 |  } | 
|---|
| 66 | 67 |   | 
|---|
| 67 | 68 |  static void printk_stack_address(unsigned long address, int reliable, | 
|---|
| 68 |  | -				 char *log_lvl)  | 
|---|
 | 69 | +				 const char *log_lvl)  | 
|---|
| 69 | 70 |  { | 
|---|
| 70 | 71 |  	touch_nmi_watchdog(); | 
|---|
| 71 | 72 |  	printk("%s %s%pB\n", log_lvl, reliable ? "" : "? ", (void *)address); | 
|---|
 | 73 | +}  | 
|---|
 | 74 | +  | 
|---|
 | 75 | +static int copy_code(struct pt_regs *regs, u8 *buf, unsigned long src,  | 
|---|
 | 76 | +		     unsigned int nbytes)  | 
|---|
 | 77 | +{  | 
|---|
 | 78 | +	if (!user_mode(regs))  | 
|---|
 | 79 | +		return copy_from_kernel_nofault(buf, (u8 *)src, nbytes);  | 
|---|
 | 80 | +  | 
|---|
 | 81 | +	/* The user space code from other tasks cannot be accessed. */  | 
|---|
 | 82 | +	if (regs != task_pt_regs(current))  | 
|---|
 | 83 | +		return -EPERM;  | 
|---|
 | 84 | +	/*  | 
|---|
 | 85 | +	 * Make sure userspace isn't trying to trick us into dumping kernel  | 
|---|
 | 86 | +	 * memory by pointing the userspace instruction pointer at it.  | 
|---|
 | 87 | +	 */  | 
|---|
 | 88 | +	if (__chk_range_not_ok(src, nbytes, TASK_SIZE_MAX))  | 
|---|
 | 89 | +		return -EINVAL;  | 
|---|
 | 90 | +  | 
|---|
 | 91 | +	/*  | 
|---|
 | 92 | +	 * Even if named copy_from_user_nmi() this can be invoked from  | 
|---|
 | 93 | +	 * other contexts and will not try to resolve a pagefault, which is  | 
|---|
 | 94 | +	 * the correct thing to do here as this code can be called from any  | 
|---|
 | 95 | +	 * context.  | 
|---|
 | 96 | +	 */  | 
|---|
 | 97 | +	return copy_from_user_nmi(buf, (void __user *)src, nbytes);  | 
|---|
| 72 | 98 |  } | 
|---|
| 73 | 99 |   | 
|---|
| 74 | 100 |  /* | 
|---|
| .. | .. | 
|---|
| 97 | 123 |  #define OPCODE_BUFSIZE (PROLOGUE_SIZE + 1 + EPILOGUE_SIZE) | 
|---|
| 98 | 124 |  	u8 opcodes[OPCODE_BUFSIZE]; | 
|---|
| 99 | 125 |  	unsigned long prologue = regs->ip - PROLOGUE_SIZE; | 
|---|
| 100 |  | -	bool bad_ip;  | 
|---|
| 101 | 126 |   | 
|---|
| 102 |  | -	/*  | 
|---|
| 103 |  | -	 * Make sure userspace isn't trying to trick us into dumping kernel  | 
|---|
| 104 |  | -	 * memory by pointing the userspace instruction pointer at it.  | 
|---|
| 105 |  | -	 */  | 
|---|
| 106 |  | -	bad_ip = user_mode(regs) &&  | 
|---|
| 107 |  | -		__chk_range_not_ok(prologue, OPCODE_BUFSIZE, TASK_SIZE_MAX);  | 
|---|
| 108 |  | -  | 
|---|
| 109 |  | -	if (bad_ip || probe_kernel_read(opcodes, (u8 *)prologue,  | 
|---|
| 110 |  | -					OPCODE_BUFSIZE)) {  | 
|---|
| 111 |  | -		printk("%sCode: Bad RIP value.\n", loglvl);  | 
|---|
| 112 |  | -	} else {  | 
|---|
 | 127 | +	switch (copy_code(regs, opcodes, prologue, sizeof(opcodes))) {  | 
|---|
 | 128 | +	case 0:  | 
|---|
| 113 | 129 |  		printk("%sCode: %" __stringify(PROLOGUE_SIZE) "ph <%02x> %" | 
|---|
| 114 | 130 |  		       __stringify(EPILOGUE_SIZE) "ph\n", loglvl, opcodes, | 
|---|
| 115 | 131 |  		       opcodes[PROLOGUE_SIZE], opcodes + PROLOGUE_SIZE + 1); | 
|---|
 | 132 | +		break;  | 
|---|
 | 133 | +	case -EPERM:  | 
|---|
 | 134 | +		/* No access to the user space stack of other tasks. Ignore. */  | 
|---|
 | 135 | +		break;  | 
|---|
 | 136 | +	default:  | 
|---|
 | 137 | +		printk("%sCode: Unable to access opcode bytes at RIP 0x%lx.\n",  | 
|---|
 | 138 | +		       loglvl, prologue);  | 
|---|
 | 139 | +		break;  | 
|---|
| 116 | 140 |  	} | 
|---|
| 117 | 141 |  } | 
|---|
| 118 | 142 |   | 
|---|
| .. | .. | 
|---|
| 126 | 150 |  	show_opcodes(regs, loglvl); | 
|---|
| 127 | 151 |  } | 
|---|
| 128 | 152 |   | 
|---|
| 129 |  | -void show_iret_regs(struct pt_regs *regs)  | 
|---|
 | 153 | +void show_iret_regs(struct pt_regs *regs, const char *log_lvl)  | 
|---|
| 130 | 154 |  { | 
|---|
| 131 |  | -	show_ip(regs, KERN_DEFAULT);  | 
|---|
| 132 |  | -	printk(KERN_DEFAULT "RSP: %04x:%016lx EFLAGS: %08lx", (int)regs->ss,  | 
|---|
 | 155 | +	show_ip(regs, log_lvl);  | 
|---|
 | 156 | +	printk("%sRSP: %04x:%016lx EFLAGS: %08lx", log_lvl, (int)regs->ss,  | 
|---|
| 133 | 157 |  		regs->sp, regs->flags); | 
|---|
| 134 | 158 |  } | 
|---|
| 135 | 159 |   | 
|---|
| 136 | 160 |  static void show_regs_if_on_stack(struct stack_info *info, struct pt_regs *regs, | 
|---|
| 137 |  | -				  bool partial)  | 
|---|
 | 161 | +				  bool partial, const char *log_lvl)  | 
|---|
| 138 | 162 |  { | 
|---|
| 139 | 163 |  	/* | 
|---|
| 140 | 164 |  	 * These on_stack() checks aren't strictly necessary: the unwind code | 
|---|
| .. | .. | 
|---|
| 146 | 170 |  	 * they can be printed in the right context. | 
|---|
| 147 | 171 |  	 */ | 
|---|
| 148 | 172 |  	if (!partial && on_stack(info, regs, sizeof(*regs))) { | 
|---|
| 149 |  | -		__show_regs(regs, SHOW_REGS_SHORT);  | 
|---|
 | 173 | +		__show_regs(regs, SHOW_REGS_SHORT, log_lvl);  | 
|---|
| 150 | 174 |   | 
|---|
| 151 | 175 |  	} else if (partial && on_stack(info, (void *)regs + IRET_FRAME_OFFSET, | 
|---|
| 152 | 176 |  				       IRET_FRAME_SIZE)) { | 
|---|
| .. | .. | 
|---|
| 155 | 179 |  		 * full pt_regs might not have been saved yet.  In that case | 
|---|
| 156 | 180 |  		 * just print the iret frame. | 
|---|
| 157 | 181 |  		 */ | 
|---|
| 158 |  | -		show_iret_regs(regs);  | 
|---|
 | 182 | +		show_iret_regs(regs, log_lvl);  | 
|---|
| 159 | 183 |  	} | 
|---|
| 160 | 184 |  } | 
|---|
| 161 | 185 |   | 
|---|
| 162 | 186 |  void show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs, | 
|---|
| 163 |  | -			unsigned long *stack, char *log_lvl)  | 
|---|
 | 187 | +			unsigned long *stack, const char *log_lvl)  | 
|---|
| 164 | 188 |  { | 
|---|
| 165 | 189 |  	struct unwind_state state; | 
|---|
| 166 | 190 |  	struct stack_info stack_info = {0}; | 
|---|
| .. | .. | 
|---|
| 210 | 234 |  			printk("%s <%s>\n", log_lvl, stack_name); | 
|---|
| 211 | 235 |   | 
|---|
| 212 | 236 |  		if (regs) | 
|---|
| 213 |  | -			show_regs_if_on_stack(&stack_info, regs, partial);  | 
|---|
 | 237 | +			show_regs_if_on_stack(&stack_info, regs, partial, log_lvl);  | 
|---|
| 214 | 238 |   | 
|---|
| 215 | 239 |  		/* | 
|---|
| 216 | 240 |  		 * Scan the stack, printing any text addresses we find.  At the | 
|---|
| .. | .. | 
|---|
| 271 | 295 |  			/* if the frame has entry regs, print them */ | 
|---|
| 272 | 296 |  			regs = unwind_get_entry_regs(&state, &partial); | 
|---|
| 273 | 297 |  			if (regs) | 
|---|
| 274 |  | -				show_regs_if_on_stack(&stack_info, regs, partial);  | 
|---|
 | 298 | +				show_regs_if_on_stack(&stack_info, regs, partial, log_lvl);  | 
|---|
| 275 | 299 |  		} | 
|---|
| 276 | 300 |   | 
|---|
| 277 | 301 |  		if (stack_name) | 
|---|
| .. | .. | 
|---|
| 279 | 303 |  	} | 
|---|
| 280 | 304 |  } | 
|---|
| 281 | 305 |   | 
|---|
| 282 |  | -void show_stack(struct task_struct *task, unsigned long *sp)  | 
|---|
 | 306 | +void show_stack(struct task_struct *task, unsigned long *sp,  | 
|---|
 | 307 | +		       const char *loglvl)  | 
|---|
| 283 | 308 |  { | 
|---|
| 284 | 309 |  	task = task ? : current; | 
|---|
| 285 | 310 |   | 
|---|
| .. | .. | 
|---|
| 290 | 315 |  	if (!sp && task == current) | 
|---|
| 291 | 316 |  		sp = get_stack_pointer(current, NULL); | 
|---|
| 292 | 317 |   | 
|---|
| 293 |  | -	show_trace_log_lvl(task, NULL, sp, KERN_DEFAULT);  | 
|---|
 | 318 | +	show_trace_log_lvl(task, NULL, sp, loglvl);  | 
|---|
| 294 | 319 |  } | 
|---|
| 295 | 320 |   | 
|---|
| 296 | 321 |  void show_stack_regs(struct pt_regs *regs) | 
|---|
| .. | .. | 
|---|
| 344 | 369 |  	oops_exit(); | 
|---|
| 345 | 370 |   | 
|---|
| 346 | 371 |  	/* Executive summary in case the oops scrolled away */ | 
|---|
| 347 |  | -	__show_regs(&exec_summary_regs, SHOW_REGS_ALL);  | 
|---|
 | 372 | +	__show_regs(&exec_summary_regs, SHOW_REGS_ALL, KERN_DEFAULT);  | 
|---|
| 348 | 373 |   | 
|---|
| 349 | 374 |  	if (!signr) | 
|---|
| 350 | 375 |  		return; | 
|---|
| .. | .. | 
|---|
| 365 | 390 |  } | 
|---|
| 366 | 391 |  NOKPROBE_SYMBOL(oops_end); | 
|---|
| 367 | 392 |   | 
|---|
| 368 |  | -int __die(const char *str, struct pt_regs *regs, long err)  | 
|---|
 | 393 | +static void __die_header(const char *str, struct pt_regs *regs, long err)  | 
|---|
| 369 | 394 |  { | 
|---|
 | 395 | +	const char *pr = "";  | 
|---|
 | 396 | +  | 
|---|
| 370 | 397 |  	/* Save the regs of the first oops for the executive summary later. */ | 
|---|
| 371 | 398 |  	if (!die_counter) | 
|---|
| 372 | 399 |  		exec_summary_regs = *regs; | 
|---|
| 373 | 400 |   | 
|---|
 | 401 | +	if (IS_ENABLED(CONFIG_PREEMPTION))  | 
|---|
 | 402 | +		pr = IS_ENABLED(CONFIG_PREEMPT_RT) ? " PREEMPT_RT" : " PREEMPT";  | 
|---|
 | 403 | +  | 
|---|
| 374 | 404 |  	printk(KERN_DEFAULT | 
|---|
| 375 | 405 |  	       "%s: %04lx [#%d]%s%s%s%s%s\n", str, err & 0xffff, ++die_counter, | 
|---|
| 376 |  | -	       IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT"         : "",  | 
|---|
 | 406 | +	       pr,  | 
|---|
| 377 | 407 |  	       IS_ENABLED(CONFIG_SMP)     ? " SMP"             : "", | 
|---|
| 378 | 408 |  	       debug_pagealloc_enabled()  ? " DEBUG_PAGEALLOC" : "", | 
|---|
| 379 | 409 |  	       IS_ENABLED(CONFIG_KASAN)   ? " KASAN"           : "", | 
|---|
| 380 | 410 |  	       IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION) ? | 
|---|
| 381 | 411 |  	       (boot_cpu_has(X86_FEATURE_PTI) ? " PTI" : " NOPTI") : ""); | 
|---|
 | 412 | +}  | 
|---|
 | 413 | +NOKPROBE_SYMBOL(__die_header);  | 
|---|
| 382 | 414 |   | 
|---|
 | 415 | +static int __die_body(const char *str, struct pt_regs *regs, long err)  | 
|---|
 | 416 | +{  | 
|---|
| 383 | 417 |  	show_regs(regs); | 
|---|
| 384 | 418 |  	print_modules(); | 
|---|
| 385 | 419 |   | 
|---|
| .. | .. | 
|---|
| 388 | 422 |  		return 1; | 
|---|
| 389 | 423 |   | 
|---|
| 390 | 424 |  	return 0; | 
|---|
 | 425 | +}  | 
|---|
 | 426 | +NOKPROBE_SYMBOL(__die_body);  | 
|---|
 | 427 | +  | 
|---|
 | 428 | +int __die(const char *str, struct pt_regs *regs, long err)  | 
|---|
 | 429 | +{  | 
|---|
 | 430 | +	__die_header(str, regs, err);  | 
|---|
 | 431 | +	return __die_body(str, regs, err);  | 
|---|
| 391 | 432 |  } | 
|---|
| 392 | 433 |  NOKPROBE_SYMBOL(__die); | 
|---|
| 393 | 434 |   | 
|---|
| .. | .. | 
|---|
| 405 | 446 |  	oops_end(flags, regs, sig); | 
|---|
| 406 | 447 |  } | 
|---|
| 407 | 448 |   | 
|---|
 | 449 | +void die_addr(const char *str, struct pt_regs *regs, long err, long gp_addr)  | 
|---|
 | 450 | +{  | 
|---|
 | 451 | +	unsigned long flags = oops_begin();  | 
|---|
 | 452 | +	int sig = SIGSEGV;  | 
|---|
 | 453 | +  | 
|---|
 | 454 | +	__die_header(str, regs, err);  | 
|---|
 | 455 | +	if (gp_addr)  | 
|---|
 | 456 | +		kasan_non_canonical_hook(gp_addr);  | 
|---|
 | 457 | +	if (__die_body(str, regs, err))  | 
|---|
 | 458 | +		sig = 0;  | 
|---|
 | 459 | +	oops_end(flags, regs, sig);  | 
|---|
 | 460 | +}  | 
|---|
 | 461 | +  | 
|---|
| 408 | 462 |  void show_regs(struct pt_regs *regs) | 
|---|
| 409 | 463 |  { | 
|---|
 | 464 | +	enum show_regs_mode print_kernel_regs;  | 
|---|
 | 465 | +  | 
|---|
| 410 | 466 |  	show_regs_print_info(KERN_DEFAULT); | 
|---|
| 411 | 467 |   | 
|---|
| 412 |  | -	__show_regs(regs, user_mode(regs) ? SHOW_REGS_USER : SHOW_REGS_ALL);  | 
|---|
 | 468 | +	print_kernel_regs = user_mode(regs) ? SHOW_REGS_USER : SHOW_REGS_ALL;  | 
|---|
 | 469 | +	__show_regs(regs, print_kernel_regs, KERN_DEFAULT);  | 
|---|
| 413 | 470 |   | 
|---|
| 414 | 471 |  	/* | 
|---|
| 415 | 472 |  	 * When in-kernel, we also print out the stack at the time of the fault.. | 
|---|