xref: /netbsd-src/sys/arch/x86/x86/patch.c (revision 404ee5b9334f618040b6cdef96a0ff35a6fc4636)
1 /*	$NetBSD: patch.c,v 1.37 2019/09/18 15:07:08 kamil Exp $	*/
2 
3 /*-
4  * Copyright (c) 2007, 2008, 2009 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
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 /*
33  * Patch kernel code at boot time, depending on available CPU features.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: patch.c,v 1.37 2019/09/18 15:07:08 kamil Exp $");
38 
39 #include "opt_lockdebug.h"
40 #ifdef i386
41 #include "opt_spldebug.h"
42 #endif
43 
44 #include <sys/types.h>
45 #include <sys/systm.h>
46 
47 #include <machine/cpu.h>
48 #include <machine/cpufunc.h>
49 #include <machine/specialreg.h>
50 #include <machine/frameasm.h>
51 
52 #include <x86/cpuvar.h>
53 #include <x86/cputypes.h>
54 
55 struct hotpatch {
56 	uint8_t name;
57 	uint8_t size;
58 	void *addr;
59 } __packed;
60 
61 void	spllower(int);
62 void	spllower_end(void);
63 void	cx8_spllower(int);
64 void	cx8_spllower_end(void);
65 void	cx8_spllower_patch(void);
66 
67 void	mutex_spin_exit_end(void);
68 void	i686_mutex_spin_exit(int);
69 void	i686_mutex_spin_exit_end(void);
70 void	i686_mutex_spin_exit_patch(void);
71 
72 void	membar_consumer(void);
73 void	membar_consumer_end(void);
74 void	membar_sync(void);
75 void	membar_sync_end(void);
76 void	sse2_lfence(void);
77 void	sse2_lfence_end(void);
78 void	sse2_mfence(void);
79 void	sse2_mfence_end(void);
80 
81 void	_atomic_cas_64(void);
82 void	_atomic_cas_64_end(void);
83 void	_atomic_cas_cx8(void);
84 void	_atomic_cas_cx8_end(void);
85 
86 extern void	*atomic_lockpatch[];
87 
88 #define	X86_NOP		0x90
89 #define	X86_REP		0xf3
90 #define	X86_RET		0xc3
91 #define	X86_CS		0x2e
92 #define	X86_DS		0x3e
93 #define	X86_GROUP_0F	0x0f
94 
95 static void
96 adjust_jumpoff(uint8_t *ptr, void *from_s, void *to_s)
97 {
98 
99 	/* Branch hints */
100 	if (ptr[0] == X86_CS || ptr[0] == X86_DS)
101 		ptr++;
102 	/* Conditional jumps */
103 	if (ptr[0] == X86_GROUP_0F)
104 		ptr++;
105 	/* 4-byte relative jump or call */
106 	*(uint32_t *)(ptr + 1 - (uintptr_t)from_s + (uintptr_t)to_s) +=
107 	    ((uint32_t)(uintptr_t)from_s - (uint32_t)(uintptr_t)to_s);
108 }
109 
110 static void __unused
111 patchfunc(void *from_s, void *from_e, void *to_s, void *to_e,
112 	  void *pcrel)
113 {
114 
115 	if ((uintptr_t)from_e - (uintptr_t)from_s !=
116 	    (uintptr_t)to_e - (uintptr_t)to_s)
117 		panic("patchfunc: sizes do not match (from=%p)", from_s);
118 
119 	memcpy(to_s, from_s, (uintptr_t)to_e - (uintptr_t)to_s);
120 	if (pcrel != NULL)
121 		adjust_jumpoff(pcrel, from_s, to_s);
122 }
123 
124 static inline void __unused
125 patchbytes(void *addr, const uint8_t *bytes, size_t size)
126 {
127 	uint8_t *ptr = (uint8_t *)addr;
128 	size_t i;
129 
130 	for (i = 0; i < size; i++) {
131 		ptr[i] = bytes[i];
132 	}
133 }
134 
135 /* The local variables have unknown alignment to UBSan */
136 __noubsan
137 void
138 x86_hotpatch(uint32_t name, const uint8_t *bytes, size_t size)
139 {
140 	extern char __rodata_hotpatch_start;
141 	extern char __rodata_hotpatch_end;
142 	struct hotpatch *hps, *hpe, *hp;
143 
144 	hps = (struct hotpatch *)&__rodata_hotpatch_start;
145 	hpe = (struct hotpatch *)&__rodata_hotpatch_end;
146 
147 	for (hp = hps; hp < hpe; hp++) {
148 		if (hp->name != name) {
149 			continue;
150 		}
151 		if (hp->size != size) {
152 			panic("x86_hotpatch: incorrect size");
153 		}
154 		patchbytes(hp->addr, bytes, size);
155 	}
156 }
157 
158 void
159 x86_patch_window_open(u_long *psl, u_long *cr0)
160 {
161 	/* Disable interrupts. */
162 	*psl = x86_read_psl();
163 	x86_disable_intr();
164 
165 	/* Disable write protection in supervisor mode. */
166 	*cr0 = rcr0();
167 	lcr0(*cr0 & ~CR0_WP);
168 }
169 
170 void
171 x86_patch_window_close(u_long psl, u_long cr0)
172 {
173 	/* Write back and invalidate cache, flush pipelines. */
174 	wbinvd();
175 	x86_flush();
176 
177 	/* Re-enable write protection. */
178 	lcr0(cr0);
179 
180 	/* Restore the PSL, potentially re-enabling interrupts. */
181 	x86_write_psl(psl);
182 }
183 
184 void
185 x86_patch(bool early)
186 {
187 	static bool first, second;
188 	u_long psl;
189 	u_long cr0;
190 
191 	if (early) {
192 		if (first)
193 			return;
194 		first = true;
195 	} else {
196 		if (second)
197 			return;
198 		second = true;
199 	}
200 
201 	x86_patch_window_open(&psl, &cr0);
202 
203 	if (!early && ncpu == 1) {
204 #ifndef LOCKDEBUG
205 		/*
206 		 * Uniprocessor: kill LOCK prefixes.
207 		 */
208 		const uint8_t bytes[] = {
209 			X86_NOP
210 		};
211 
212 		/* lock -> nop */
213 		x86_hotpatch(HP_NAME_NOLOCK, bytes, sizeof(bytes));
214 		for (int i = 0; atomic_lockpatch[i] != 0; i++)
215 			patchbytes(atomic_lockpatch[i], bytes, sizeof(bytes));
216 #endif
217 	}
218 
219 	if (!early && (cpu_feature[0] & CPUID_SSE2) != 0) {
220 		/*
221 		 * Faster memory barriers.  We do not need to patch
222 		 * membar_producer to use SFENCE because on x86
223 		 * ordinary non-temporal stores are always issued in
224 		 * program order to main memory and to other CPUs.
225 		 */
226 		patchfunc(
227 		    sse2_lfence, sse2_lfence_end,
228 		    membar_consumer, membar_consumer_end,
229 		    NULL
230 		);
231 		patchfunc(
232 		    sse2_mfence, sse2_mfence_end,
233 		    membar_sync, membar_sync_end,
234 		    NULL
235 		);
236 	}
237 
238 #ifdef i386
239 	/*
240 	 * Patch early and late.  Second time around the 'lock' prefix
241 	 * may be gone.
242 	 */
243 	if ((cpu_feature[0] & CPUID_CX8) != 0) {
244 		patchfunc(
245 		    _atomic_cas_cx8, _atomic_cas_cx8_end,
246 		    _atomic_cas_64, _atomic_cas_64_end,
247 		    NULL
248 		);
249 	}
250 #endif	/* i386 */
251 
252 #if !defined(SPLDEBUG)
253 	if (!early && (cpu_feature[0] & CPUID_CX8) != 0) {
254 		/* Faster splx(), mutex_spin_exit(). */
255 		patchfunc(
256 		    cx8_spllower, cx8_spllower_end,
257 		    spllower, spllower_end,
258 		    cx8_spllower_patch
259 		);
260 #if defined(i386) && !defined(LOCKDEBUG)
261 		patchfunc(
262 		    i686_mutex_spin_exit, i686_mutex_spin_exit_end,
263 		    mutex_spin_exit, mutex_spin_exit_end,
264 		    i686_mutex_spin_exit_patch
265 		);
266 #endif	/* i386 && !LOCKDEBUG */
267 	}
268 #endif /* !SPLDEBUG */
269 
270 	/*
271 	 * On some Opteron revisions, locked operations erroneously
272 	 * allow memory references to be `bled' outside of critical
273 	 * sections.  Apply workaround.
274 	 */
275 	if (cpu_vendor == CPUVENDOR_AMD &&
276 	    (CPUID_TO_FAMILY(cpu_info_primary.ci_signature) == 0xe ||
277 	    (CPUID_TO_FAMILY(cpu_info_primary.ci_signature) == 0xf &&
278 	    CPUID_TO_EXTMODEL(cpu_info_primary.ci_signature) < 0x4))) {
279 		const uint8_t bytes[] = {
280 			0x0F, 0xAE, 0xE8 /* lfence */
281 		};
282 
283 		/* ret,nop,nop -> lfence */
284 		x86_hotpatch(HP_NAME_RETFENCE, bytes, sizeof(bytes));
285 	}
286 
287 	/*
288 	 * If SMAP is present then patch the prepared holes with clac/stac
289 	 * instructions.
290 	 *
291 	 * clac = 0x0f, 0x01, 0xca
292 	 * stac = 0x0f, 0x01, 0xcb
293 	 */
294 	if (!early && cpu_feature[5] & CPUID_SEF_SMAP) {
295 		KASSERT(rcr4() & CR4_SMAP);
296 		const uint8_t clac_bytes[] = {
297 			0x0F, 0x01, 0xCA /* clac */
298 		};
299 		const uint8_t stac_bytes[] = {
300 			0x0F, 0x01, 0xCB /* stac */
301 		};
302 
303 		/* nop,nop,nop -> clac */
304 		x86_hotpatch(HP_NAME_CLAC, clac_bytes, sizeof(clac_bytes));
305 
306 		/* nop,nop,nop -> stac */
307 		x86_hotpatch(HP_NAME_STAC, stac_bytes, sizeof(stac_bytes));
308 	}
309 
310 	x86_patch_window_close(psl, cr0);
311 }
312