xref: /freebsd/sys/amd64/include/msan.h (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1a422084aSMark Johnston /*-
2*4d846d26SWarner 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 /*
44a422084aSMark Johnston  * Our 32-bit origin cells encode a 2-bit type and 30-bit pointer.  The pointer
45a422084aSMark Johnston  * is compressed by making it a positive offset relative to KERNBASE.
46a422084aSMark Johnston  */
47a422084aSMark Johnston #define	KMSAN_ORIG_TYPE_SHIFT	30u
48a422084aSMark Johnston #define	KMSAN_ORIG_PTR_MASK	((1u << KMSAN_ORIG_TYPE_SHIFT) - 1)
49a422084aSMark Johnston 
50a422084aSMark Johnston static inline msan_orig_t
51a422084aSMark Johnston kmsan_md_orig_encode(int type, uintptr_t ptr)
52a422084aSMark Johnston {
53a422084aSMark Johnston 	return ((type << KMSAN_ORIG_TYPE_SHIFT) |
54a422084aSMark Johnston 	    ((ptr & KMSAN_ORIG_PTR_MASK)));
55a422084aSMark Johnston }
56a422084aSMark Johnston 
57a422084aSMark Johnston static inline void
58a422084aSMark Johnston kmsan_md_orig_decode(msan_orig_t orig, int *type, uintptr_t *ptr)
59a422084aSMark Johnston {
60a422084aSMark Johnston 	*type = orig >> KMSAN_ORIG_TYPE_SHIFT;
61a422084aSMark Johnston 	*ptr = (orig & KMSAN_ORIG_PTR_MASK) | KERNBASE;
62a422084aSMark Johnston }
63a422084aSMark Johnston 
64a422084aSMark Johnston static inline vm_offset_t
65a422084aSMark Johnston kmsan_md_addr_to_shad(vm_offset_t addr)
66a422084aSMark Johnston {
67a422084aSMark Johnston 	return (addr - VM_MIN_KERNEL_ADDRESS + KMSAN_SHAD_MIN_ADDRESS);
68a422084aSMark Johnston }
69a422084aSMark Johnston 
70a422084aSMark Johnston static inline vm_offset_t
71a422084aSMark Johnston kmsan_md_addr_to_orig(vm_offset_t addr)
72a422084aSMark Johnston {
73a422084aSMark Johnston 	return (addr - VM_MIN_KERNEL_ADDRESS + KMSAN_ORIG_MIN_ADDRESS);
74a422084aSMark Johnston }
75a422084aSMark Johnston 
76a422084aSMark Johnston static inline bool
77a422084aSMark Johnston kmsan_md_unsupported(vm_offset_t addr)
78a422084aSMark Johnston {
79a422084aSMark Johnston 	return (addr < VM_MIN_KERNEL_ADDRESS || addr >= KERNBASE);
80a422084aSMark Johnston }
81a422084aSMark Johnston 
82a422084aSMark Johnston #endif /* KMSAN */
83a422084aSMark Johnston 
84a422084aSMark Johnston #endif /* !_MACHINE_MSAN_H_ */
85