Home | History | Annotate | Line # | Download | only in include
csan.h revision 1.2
      1 /*	$NetBSD: csan.h,v 1.2 2019/11/06 06:57:22 maxv Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Maxime Villard.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/ksyms.h>
     33 #include <x86/cpufunc.h>
     34 
     35 static inline bool
     36 kcsan_md_is_avail(void)
     37 {
     38 	return true;
     39 }
     40 
     41 static inline void
     42 kcsan_md_disable_intrs(uint64_t *state)
     43 {
     44 	*state = x86_read_psl();
     45 	x86_disable_intr();
     46 }
     47 
     48 static inline void
     49 kcsan_md_enable_intrs(uint64_t *state)
     50 {
     51 	x86_write_psl(*state);
     52 }
     53 
     54 static inline void
     55 kcsan_md_delay(uint64_t us)
     56 {
     57 	DELAY(us);
     58 }
     59 
     60 static inline bool
     61 __md_unwind_end(const char *name)
     62 {
     63 	if (!strcmp(name, "syscall") ||
     64 	    !strcmp(name, "alltraps") ||
     65 	    !strcmp(name, "handle_syscall") ||
     66 	    !strncmp(name, "Xtrap", 5) ||
     67 	    !strncmp(name, "Xintr", 5) ||
     68 	    !strncmp(name, "Xhandle", 7) ||
     69 	    !strncmp(name, "Xresume", 7) ||
     70 	    !strncmp(name, "Xstray", 6) ||
     71 	    !strncmp(name, "Xhold", 5) ||
     72 	    !strncmp(name, "Xrecurse", 8) ||
     73 	    !strcmp(name, "Xdoreti") ||
     74 	    !strncmp(name, "Xsoft", 5)) {
     75 		return true;
     76 	}
     77 
     78 	return false;
     79 }
     80 
     81 static void
     82 kcsan_md_unwind(void)
     83 {
     84 	uint64_t *rbp, rip;
     85 	const char *mod;
     86 	const char *sym;
     87 	size_t nsym;
     88 	int error;
     89 
     90 	rbp = (uint64_t *)__builtin_frame_address(0);
     91 	nsym = 0;
     92 
     93 	while (1) {
     94 		/* 8(%rbp) contains the saved %rip. */
     95 		rip = *(rbp + 1);
     96 
     97 		if (rip < KERNBASE) {
     98 			break;
     99 		}
    100 		error = ksyms_getname(&mod, &sym, (vaddr_t)rip, KSYMS_PROC);
    101 		if (error) {
    102 			break;
    103 		}
    104 		printf("#%zu %p in %s <%s>\n", nsym, (void *)rip, sym, mod);
    105 		if (__md_unwind_end(sym)) {
    106 			break;
    107 		}
    108 
    109 		rbp = (uint64_t *)*(rbp);
    110 		if (rbp == 0) {
    111 			break;
    112 		}
    113 		nsym++;
    114 
    115 		if (nsym >= 15) {
    116 			break;
    117 		}
    118 	}
    119 }
    120