1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2021 The FreeBSD Foundation
5 * Copyright (c) 2023 Juniper Networks, Inc.
6 *
7 * This software was developed by Mark Johnston under sponsorship from the
8 * FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are
12 * 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
17 * the documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #ifndef _MACHINE_MSAN_H_
33 #define _MACHINE_MSAN_H_
34
35 #ifdef KMSAN
36
37 #include <vm/vm.h>
38 #include <vm/pmap.h>
39 #include <vm/vm_page.h>
40 #include <machine/vmparam.h>
41
42 typedef uint32_t msan_orig_t;
43
44 /*
45 * Our 32-bit origin cells encode a 2-bit type and 30-bit pointer to a kernel
46 * instruction. The pointer is compressed by making it a positive offset
47 * relative to KERNBASE.
48 */
49 #define KMSAN_ORIG_TYPE_SHIFT 30u
50 #define KMSAN_ORIG_PTR_MASK ((1ul << KMSAN_ORIG_TYPE_SHIFT) - 1)
51
52 static inline msan_orig_t
kmsan_md_orig_encode(int type,uintptr_t ptr)53 kmsan_md_orig_encode(int type, uintptr_t ptr)
54 {
55 return ((type << KMSAN_ORIG_TYPE_SHIFT) |
56 ((ptr & KMSAN_ORIG_PTR_MASK)));
57 }
58
59 static inline void
kmsan_md_orig_decode(msan_orig_t orig,int * type,uintptr_t * ptr)60 kmsan_md_orig_decode(msan_orig_t orig, int *type, uintptr_t *ptr)
61 {
62 *type = orig >> KMSAN_ORIG_TYPE_SHIFT;
63 *ptr = (orig & KMSAN_ORIG_PTR_MASK) | KERNBASE;
64 }
65
66 static inline vm_offset_t
kmsan_md_addr_to_shad(vm_offset_t addr)67 kmsan_md_addr_to_shad(vm_offset_t addr)
68 {
69 return (addr - VM_MIN_KERNEL_ADDRESS + KMSAN_SHAD_MIN_ADDRESS);
70 }
71
72 static inline vm_offset_t
kmsan_md_addr_to_orig(vm_offset_t addr)73 kmsan_md_addr_to_orig(vm_offset_t addr)
74 {
75 return (addr - VM_MIN_KERNEL_ADDRESS + KMSAN_ORIG_MIN_ADDRESS);
76 }
77
78 static inline bool
kmsan_md_unsupported(vm_offset_t addr)79 kmsan_md_unsupported(vm_offset_t addr)
80 {
81 /*
82 * It would be cheaper to use VM_MAX_KERNEL_ADDRESS as the upper bound,
83 * but we need to exclude device mappings above kernel_vm_end but within
84 * the kernel map.
85 */
86 return (addr < VM_MIN_KERNEL_ADDRESS || addr >= kernel_vm_end);
87 }
88
89 #endif /* KMSAN */
90
91 #endif /* !_MACHINE_MSAN_H_ */
92