Home | History | Annotate | Line # | Download | only in include
asan.h revision 1.2
      1  1.2  skrll /*	$NetBSD: asan.h,v 1.2 2018/11/02 08:18:18 skrll Exp $	*/
      2  1.1   maxv 
      3  1.1   maxv /*
      4  1.1   maxv  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  1.1   maxv  * All rights reserved.
      6  1.1   maxv  *
      7  1.1   maxv  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1   maxv  * by Maxime Villard.
      9  1.1   maxv  *
     10  1.1   maxv  * Redistribution and use in source and binary forms, with or without
     11  1.1   maxv  * modification, are permitted provided that the following conditions
     12  1.1   maxv  * are met:
     13  1.1   maxv  * 1. Redistributions of source code must retain the above copyright
     14  1.1   maxv  *    notice, this list of conditions and the following disclaimer.
     15  1.1   maxv  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1   maxv  *    notice, this list of conditions and the following disclaimer in the
     17  1.1   maxv  *    documentation and/or other materials provided with the distribution.
     18  1.1   maxv  *
     19  1.1   maxv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1   maxv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1   maxv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1   maxv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1   maxv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1   maxv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1   maxv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1   maxv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1   maxv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1   maxv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1   maxv  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1   maxv  */
     31  1.1   maxv 
     32  1.1   maxv #include <sys/atomic.h>
     33  1.2  skrll #include <sys/ksyms.h>
     34  1.2  skrll 
     35  1.1   maxv #include <aarch64/pmap.h>
     36  1.1   maxv #include <aarch64/vmparam.h>
     37  1.1   maxv #include <aarch64/cpufunc.h>
     38  1.1   maxv #include <aarch64/armreg.h>
     39  1.1   maxv 
     40  1.1   maxv #define __MD_VIRTUAL_SHIFT	48	/* 49bit address space, cut half */
     41  1.1   maxv #define __MD_CANONICAL_BASE	0xFFFF000000000000
     42  1.1   maxv 
     43  1.1   maxv #define __MD_SHADOW_SIZE	(1ULL << (__MD_VIRTUAL_SHIFT - KASAN_SHADOW_SCALE_SHIFT))
     44  1.1   maxv #define KASAN_MD_SHADOW_START	(AARCH64_KSEG_END)
     45  1.1   maxv #define KASAN_MD_SHADOW_END	(KASAN_MD_SHADOW_START + __MD_SHADOW_SIZE)
     46  1.1   maxv 
     47  1.1   maxv static inline int8_t *
     48  1.1   maxv kasan_md_addr_to_shad(const void *addr)
     49  1.1   maxv {
     50  1.1   maxv 	vaddr_t va = (vaddr_t)addr;
     51  1.1   maxv 	return (int8_t *)(KASAN_MD_SHADOW_START +
     52  1.1   maxv 	    ((va - __MD_CANONICAL_BASE) >> KASAN_SHADOW_SCALE_SHIFT));
     53  1.1   maxv }
     54  1.1   maxv 
     55  1.1   maxv static inline bool
     56  1.1   maxv kasan_md_unsupported(vaddr_t addr)
     57  1.1   maxv {
     58  1.1   maxv 	return (addr < VM_MIN_KERNEL_ADDRESS) ||
     59  1.1   maxv 	    (addr >= VM_KERNEL_IO_ADDRESS);
     60  1.1   maxv }
     61  1.1   maxv 
     62  1.1   maxv static paddr_t
     63  1.1   maxv __md_palloc(void)
     64  1.1   maxv {
     65  1.1   maxv 	paddr_t pa;
     66  1.1   maxv 
     67  1.1   maxv 	pmap_alloc_pdp(pmap_kernel(), &pa);
     68  1.1   maxv 
     69  1.1   maxv 	return pa;
     70  1.1   maxv }
     71  1.1   maxv 
     72  1.1   maxv static void
     73  1.1   maxv kasan_md_shadow_map_page(vaddr_t va)
     74  1.1   maxv {
     75  1.1   maxv 	pd_entry_t *l0, *l1, *l2, *l3;
     76  1.1   maxv 	paddr_t l0pa, pa;
     77  1.1   maxv 	pd_entry_t pde;
     78  1.1   maxv 	size_t idx;
     79  1.1   maxv 
     80  1.1   maxv 	l0pa = reg_ttbr1_el1_read();
     81  1.1   maxv 	l0 = (void *)AARCH64_PA_TO_KVA(l0pa);
     82  1.1   maxv 
     83  1.1   maxv 	idx = l0pde_index(va);
     84  1.1   maxv 	pde = l0[idx];
     85  1.1   maxv 	if (!l0pde_valid(pde)) {
     86  1.1   maxv 		pa = __md_palloc();
     87  1.1   maxv 		atomic_swap_64(&l0[idx], pa | L0_TABLE);
     88  1.1   maxv 	} else {
     89  1.1   maxv 		pa = l0pde_pa(pde);
     90  1.1   maxv 	}
     91  1.1   maxv 	l1 = (void *)AARCH64_PA_TO_KVA(pa);
     92  1.1   maxv 
     93  1.1   maxv 	idx = l1pde_index(va);
     94  1.1   maxv 	pde = l1[idx];
     95  1.1   maxv 	if (!l1pde_valid(pde)) {
     96  1.1   maxv 		pa = __md_palloc();
     97  1.1   maxv 		atomic_swap_64(&l1[idx], pa | L1_TABLE);
     98  1.1   maxv 	} else {
     99  1.1   maxv 		pa = l1pde_pa(pde);
    100  1.1   maxv 	}
    101  1.1   maxv 	l2 = (void *)AARCH64_PA_TO_KVA(pa);
    102  1.1   maxv 
    103  1.1   maxv 	idx = l2pde_index(va);
    104  1.1   maxv 	pde = l2[idx];
    105  1.1   maxv 	if (!l2pde_valid(pde)) {
    106  1.1   maxv 		pa = __md_palloc();
    107  1.1   maxv 		atomic_swap_64(&l2[idx], pa | L2_TABLE);
    108  1.1   maxv 	} else {
    109  1.1   maxv 		pa = l2pde_pa(pde);
    110  1.1   maxv 	}
    111  1.1   maxv 	l3 = (void *)AARCH64_PA_TO_KVA(pa);
    112  1.1   maxv 
    113  1.1   maxv 	idx = l3pte_index(va);
    114  1.1   maxv 	pde = l3[idx];
    115  1.1   maxv 	if (!l3pte_valid(pde)) {
    116  1.1   maxv 		pa = __md_palloc();
    117  1.1   maxv 		atomic_swap_64(&l3[idx], pa | L3_PAGE | LX_BLKPAG_UXN |
    118  1.1   maxv 		    LX_BLKPAG_PXN | LX_BLKPAG_AF | LX_BLKPAG_AP_RW);
    119  1.1   maxv 		aarch64_tlbi_by_va(va);
    120  1.1   maxv 	}
    121  1.1   maxv }
    122  1.1   maxv 
    123  1.1   maxv #define kasan_md_early_init(a)	__nothing
    124  1.1   maxv 
    125  1.1   maxv static void
    126  1.1   maxv kasan_md_init(void)
    127  1.1   maxv {
    128  1.1   maxv 	vaddr_t eva, dummy;
    129  1.1   maxv 
    130  1.1   maxv 	CTASSERT((__MD_SHADOW_SIZE / L0_SIZE) == 64);
    131  1.1   maxv 
    132  1.1   maxv 	/* The VAs we've created until now. */
    133  1.1   maxv 	pmap_virtual_space(&eva, &dummy);
    134  1.1   maxv 	kasan_shadow_map((void *)VM_MIN_KERNEL_ADDRESS,
    135  1.1   maxv 	    eva - VM_MIN_KERNEL_ADDRESS);
    136  1.1   maxv }
    137  1.1   maxv 
    138  1.2  skrll static inline bool
    139  1.2  skrll __md_unwind_end(const char *name)
    140  1.2  skrll {
    141  1.2  skrll 	if (!strncmp(name, "el0_trap", 8) ||
    142  1.2  skrll 	    !strncmp(name, "el1_trap", 8)) {
    143  1.2  skrll 		return true;
    144  1.2  skrll 	}
    145  1.2  skrll 
    146  1.2  skrll 	return false;
    147  1.2  skrll }
    148  1.2  skrll 
    149  1.2  skrll static void
    150  1.2  skrll kasan_md_unwind(void)
    151  1.2  skrll {
    152  1.2  skrll 	uint64_t lr, *fp;
    153  1.2  skrll 	const char *mod;
    154  1.2  skrll 	const char *sym;
    155  1.2  skrll 	size_t nsym;
    156  1.2  skrll 	int error;
    157  1.2  skrll 
    158  1.2  skrll 	fp = (uint64_t *)__builtin_frame_address(0);
    159  1.2  skrll 	nsym = 0;
    160  1.2  skrll 
    161  1.2  skrll 	while (1) {
    162  1.2  skrll 		/*
    163  1.2  skrll 		 * normal stack frame
    164  1.2  skrll 		 *  fp[0]  saved fp(x29) value
    165  1.2  skrll 		 *  fp[1]  saved lr(x30) value
    166  1.2  skrll 		 */
    167  1.2  skrll 		lr = fp[1];
    168  1.2  skrll 
    169  1.2  skrll 		if (lr < VM_MIN_KERNEL_ADDRESS) {
    170  1.2  skrll 			break;
    171  1.2  skrll 		}
    172  1.2  skrll 		error = ksyms_getname(&mod, &sym, (vaddr_t)lr, KSYMS_PROC);
    173  1.2  skrll 		if (error) {
    174  1.2  skrll 			break;
    175  1.2  skrll 		}
    176  1.2  skrll 		printf("#%zu %p in %s <%s>\n", nsym, (void *)lr, sym, mod);
    177  1.2  skrll 		if (__md_unwind_end(sym)) {
    178  1.2  skrll 			break;
    179  1.2  skrll 		}
    180  1.2  skrll 
    181  1.2  skrll 		fp = (uint64_t *)fp[0];
    182  1.2  skrll 		if (fp == NULL) {
    183  1.2  skrll 			break;
    184  1.2  skrll 		}
    185  1.2  skrll 		nsym++;
    186  1.2  skrll 
    187  1.2  skrll 		if (nsym >= 15) {
    188  1.2  skrll 			break;
    189  1.2  skrll 		}
    190  1.2  skrll 	}
    191  1.2  skrll }
    192