xref: /freebsd/sys/amd64/include/msan.h (revision f6df79ab)
1a422084aSMark Johnston /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3a422084aSMark Johnston  *
4a422084aSMark Johnston  * Copyright (c) 2021 The FreeBSD Foundation
5a422084aSMark Johnston  *
6a422084aSMark Johnston  * This software was developed by Mark Johnston under sponsorship from the
7a422084aSMark Johnston  * FreeBSD Foundation.
8a422084aSMark Johnston  *
9a422084aSMark Johnston  * Redistribution and use in source and binary forms, with or without
10a422084aSMark Johnston  * modification, are permitted provided that the following conditions are
11a422084aSMark Johnston  * met:
12a422084aSMark Johnston  * 1. Redistributions of source code must retain the above copyright
13a422084aSMark Johnston  *    notice, this list of conditions and the following disclaimer.
14a422084aSMark Johnston  * 2. Redistributions in binary form must reproduce the above copyright
15a422084aSMark Johnston  *    notice, this list of conditions and the following disclaimer in
16a422084aSMark Johnston  *    the documentation and/or other materials provided with the distribution.
17a422084aSMark Johnston  *
18a422084aSMark Johnston  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19a422084aSMark Johnston  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20a422084aSMark Johnston  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21a422084aSMark Johnston  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22a422084aSMark Johnston  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23a422084aSMark Johnston  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24a422084aSMark Johnston  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25a422084aSMark Johnston  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26a422084aSMark Johnston  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27a422084aSMark Johnston  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28a422084aSMark Johnston  * SUCH DAMAGE.
29a422084aSMark Johnston  */
30a422084aSMark Johnston 
31a422084aSMark Johnston #ifndef _MACHINE_MSAN_H_
32a422084aSMark Johnston #define	_MACHINE_MSAN_H_
33a422084aSMark Johnston 
34a422084aSMark Johnston #ifdef KMSAN
35a422084aSMark Johnston 
36a422084aSMark Johnston #include <vm/vm.h>
37a422084aSMark Johnston #include <vm/pmap.h>
38a422084aSMark Johnston #include <vm/vm_page.h>
39a422084aSMark Johnston #include <machine/vmparam.h>
40a422084aSMark Johnston 
41a422084aSMark Johnston typedef uint32_t msan_orig_t;
42a422084aSMark Johnston 
43a422084aSMark Johnston /*
44640e5cb3SMark Johnston  * Our 32-bit origin cells encode a 2-bit type and 30-bit pointer to a kernel
45640e5cb3SMark Johnston  * instruction.  The pointer is compressed by making it a positive offset
46640e5cb3SMark Johnston  * relative to KERNBASE.
47a422084aSMark Johnston  */
48a422084aSMark Johnston #define	KMSAN_ORIG_TYPE_SHIFT	30u
49a422084aSMark Johnston #define	KMSAN_ORIG_PTR_MASK	((1u << KMSAN_ORIG_TYPE_SHIFT) - 1)
50a422084aSMark Johnston 
51a422084aSMark Johnston static inline msan_orig_t
kmsan_md_orig_encode(int type,uintptr_t ptr)52a422084aSMark Johnston kmsan_md_orig_encode(int type, uintptr_t ptr)
53a422084aSMark Johnston {
54a422084aSMark Johnston 	return ((type << KMSAN_ORIG_TYPE_SHIFT) |
55a422084aSMark Johnston 	    ((ptr & KMSAN_ORIG_PTR_MASK)));
56a422084aSMark Johnston }
57a422084aSMark Johnston 
58a422084aSMark Johnston static inline void
kmsan_md_orig_decode(msan_orig_t orig,int * type,uintptr_t * ptr)59a422084aSMark Johnston kmsan_md_orig_decode(msan_orig_t orig, int *type, uintptr_t *ptr)
60a422084aSMark Johnston {
61a422084aSMark Johnston 	*type = orig >> KMSAN_ORIG_TYPE_SHIFT;
62a422084aSMark Johnston 	*ptr = (orig & KMSAN_ORIG_PTR_MASK) | KERNBASE;
63a422084aSMark Johnston }
64a422084aSMark Johnston 
65a422084aSMark Johnston static inline vm_offset_t
kmsan_md_addr_to_shad(vm_offset_t addr)66a422084aSMark Johnston kmsan_md_addr_to_shad(vm_offset_t addr)
67a422084aSMark Johnston {
68a422084aSMark Johnston 	return (addr - VM_MIN_KERNEL_ADDRESS + KMSAN_SHAD_MIN_ADDRESS);
69a422084aSMark Johnston }
70a422084aSMark Johnston 
71a422084aSMark Johnston static inline vm_offset_t
kmsan_md_addr_to_orig(vm_offset_t addr)72a422084aSMark Johnston kmsan_md_addr_to_orig(vm_offset_t addr)
73a422084aSMark Johnston {
74a422084aSMark Johnston 	return (addr - VM_MIN_KERNEL_ADDRESS + KMSAN_ORIG_MIN_ADDRESS);
75a422084aSMark Johnston }
76a422084aSMark Johnston 
77a422084aSMark Johnston static inline bool
kmsan_md_unsupported(vm_offset_t addr)78a422084aSMark Johnston kmsan_md_unsupported(vm_offset_t addr)
79a422084aSMark Johnston {
80640e5cb3SMark Johnston 	/*
81640e5cb3SMark Johnston 	 * The kernel itself isn't shadowed: for most purposes global variables
82640e5cb3SMark Johnston 	 * are always initialized, and because KMSAN kernels are large
83640e5cb3SMark Johnston 	 * (GENERIC-KMSAN is ~80MB at the time of writing), shadowing would
84f6df79abSElyes Haouas 	 * incur significant memory usage.
85640e5cb3SMark Johnston 	 */
86a422084aSMark Johnston 	return (addr < VM_MIN_KERNEL_ADDRESS || addr >= KERNBASE);
87a422084aSMark Johnston }
88a422084aSMark Johnston 
89a422084aSMark Johnston #endif /* KMSAN */
90a422084aSMark Johnston 
91a422084aSMark Johnston #endif /* !_MACHINE_MSAN_H_ */
92