xref: /openbsd-src/sys/arch/amd64/include/codepatch.h (revision 897fc685943471cf985a0fe38ba076ea6fe74fa5)
1 /*      $OpenBSD: codepatch.h,v 1.4 2017/08/25 19:28:48 guenther Exp $    */
2 /*
3  * Copyright (c) 2014-2015 Stefan Fritsch <sf@sfritsch.de>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #ifndef _MACHINE_CODEPATCH_H_
19 #define _MACHINE_CODEPATCH_H_
20 
21 #include <machine/param.h>
22 
23 #ifndef _LOCORE
24 
25 void *codepatch_maprw(vaddr_t *nva, vaddr_t dest);
26 void codepatch_unmaprw(vaddr_t nva);
27 void codepatch_fill_nop(void *caddr, uint16_t len);
28 void codepatch_nop(uint16_t tag);
29 void codepatch_replace(uint16_t tag, void *code, size_t len);
30 void codepatch_call(uint16_t tag, void *func);
31 
32 #endif /* !_LOCORE */
33 
34 /*
35  * Mark the start of some code snippet to be patched.
36  */
37 #define	CODEPATCH_START	998:
38 /*
39  * Mark the end of some code to be patched, and assign the given tag.
40  */
41 #define	CODEPATCH_END(tag)			 \
42 	999:					 \
43 	.section .codepatch, "a"		;\
44 	.quad 998b				;\
45 	.short (999b - 998b)			;\
46 	.short tag				;\
47 	.int 0					;\
48 	.previous
49 
50 #define CPTAG_STAC		1
51 #define CPTAG_CLAC		2
52 #define CPTAG_EOI		3
53 
54 /*
55  * As stac/clac SMAP instructions are 3 bytes, we want the fastest
56  * 3 byte nop sequence possible here.  This will be replaced by
57  * stac/clac instructions if SMAP is detected after booting.
58  *
59  * This would be 'nop (%rax)' if binutils could cope.
60  * Intel documents multi-byte NOP sequences as being available
61  * on all family 0x6 and 0xf processors (ie 686+)
62  */
63 #define SMAP_NOP	.byte 0x0f, 0x1f, 0x00
64 #define SMAP_STAC	CODEPATCH_START			;\
65 			SMAP_NOP			;\
66 			CODEPATCH_END(CPTAG_STAC)
67 #define SMAP_CLAC	CODEPATCH_START			;\
68 			SMAP_NOP			;\
69 			CODEPATCH_END(CPTAG_CLAC)
70 
71 #endif /* _MACHINE_CODEPATCH_H_ */
72