xref: /haiku/headers/private/kernel/arch/x86/arch_altcodepatch.h (revision 9495126984d664dfa8afc1382b5614ee69ba2a1e)
1a2021292SJérôme Duval /*
2a2021292SJérôme Duval  * Copyright 2018, Jérôme Duval, jerome.duval@gmail.com.
3a2021292SJérôme Duval  * Distributed under the terms of the MIT License.
4a2021292SJérôme Duval  */
5a2021292SJérôme Duval #ifndef _KERNEL_ARCH_x86_ALTCODEPATCH_H
6a2021292SJérôme Duval #define _KERNEL_ARCH_x86_ALTCODEPATCH_H
7a2021292SJérôme Duval 
8a2021292SJérôme Duval 
9a2021292SJérôme Duval #include <arch/x86/arch_kernel.h>
10a2021292SJérôme Duval 
11a2021292SJérôme Duval 
12a2021292SJérôme Duval #define ASM_NOP1	.byte 0x90
13a2021292SJérôme Duval #define ASM_NOP2	.byte 0x66, 0x90
14a2021292SJérôme Duval #define ASM_NOP3	.byte 0x0f, 0x1f, 0x00
15a2021292SJérôme Duval #define ASM_NOP4	.byte 0x0f, 0x1f, 0x40, 0x00
16a2021292SJérôme Duval #define ASM_NOP5	.byte 0x0f, 0x1f, 0x44, 0x00, 0x00
17a2021292SJérôme Duval #define ASM_NOP6	.byte 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00
18a2021292SJérôme Duval #define ASM_NOP7	.byte 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00
19a2021292SJérôme Duval #define ASM_NOP8	.byte 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
20a2021292SJérôme Duval #define ASM_NOP9	.byte 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
21a2021292SJérôme Duval 
22a2021292SJérôme Duval 
239dd4d2ddSJérôme Duval #define ALTCODEPATCH_TAG_STAC		1
249dd4d2ddSJérôme Duval #define ALTCODEPATCH_TAG_CLAC		2
25*94951269SJérôme Duval #define ALTCODEPATCH_TAG_XSAVE		3
26*94951269SJérôme Duval #define ALTCODEPATCH_TAG_XRSTOR		4
279dd4d2ddSJérôme Duval 
289dd4d2ddSJérôme Duval 
29a2021292SJérôme Duval #ifdef _ASSEMBLER
30a2021292SJérôme Duval 
31a2021292SJérôme Duval #define CODEPATCH_START	990:
32a2021292SJérôme Duval #define CODEPATCH_END(tag)	991:	\
33a2021292SJérôme Duval 	.pushsection	.altcodepatch, "a"	;	\
34a2021292SJérôme Duval 	.long			(990b - KERNEL_LOAD_BASE)	;	\
35a2021292SJérôme Duval 	.short			(991b - 990b)		;	\
36a2021292SJérôme Duval 	.short			tag				;	\
37a2021292SJérôme Duval 	.popsection
38a2021292SJérôme Duval 
399dd4d2ddSJérôme Duval #define ASM_STAC	CODEPATCH_START \
409dd4d2ddSJérôme Duval 					ASM_NOP3	;	 \
419dd4d2ddSJérôme Duval 					CODEPATCH_END(ALTCODEPATCH_TAG_STAC)
429dd4d2ddSJérôme Duval 
439dd4d2ddSJérôme Duval #define ASM_CLAC	CODEPATCH_START \
449dd4d2ddSJérôme Duval 					ASM_NOP3	;	\
459dd4d2ddSJérôme Duval 					CODEPATCH_END(ALTCODEPATCH_TAG_CLAC)
469dd4d2ddSJérôme Duval 
47a2021292SJérôme Duval #else
48a2021292SJérôme Duval 
49a2021292SJérôme Duval #define _STRINGIFY(x...)	#x
50a2021292SJérôme Duval #define STRINGIFY(x...)		_STRINGIFY(x)
51a2021292SJérôme Duval 
52a2021292SJérôme Duval #define CODEPATCH_START	"990:	\n"
53a2021292SJérôme Duval #define CODEPATCH_END(tag)	"991:	\n"	\
54a2021292SJérôme Duval 	".pushsection	.altcodepatch, \"a\"	\n" \
55a2021292SJérôme Duval 	".long			(990b - " STRINGIFY(KERNEL_LOAD_BASE) ")	\n" \
56a2021292SJérôme Duval 	".short			(991b - 990b)		\n" \
57a2021292SJérôme Duval 	".short			" STRINGIFY(tag)	"	\n" \
58a2021292SJérôme Duval 	".popsection"
59a2021292SJérôme Duval 
609dd4d2ddSJérôme Duval #define ASM_STAC	CODEPATCH_START \
619dd4d2ddSJérôme Duval 					STRINGIFY(ASM_NOP3)	"\n" \
629dd4d2ddSJérôme Duval 					CODEPATCH_END(ALTCODEPATCH_TAG_STAC)
639dd4d2ddSJérôme Duval 
649dd4d2ddSJérôme Duval #define ASM_CLAC	CODEPATCH_START \
659dd4d2ddSJérôme Duval 					STRINGIFY(ASM_NOP3)	"\n"\
669dd4d2ddSJérôme Duval 					CODEPATCH_END(ALTCODEPATCH_TAG_CLAC)
679dd4d2ddSJérôme Duval 
689dd4d2ddSJérôme Duval 
69a2021292SJérôme Duval void arch_altcodepatch_replace(uint16 tag, void* newcodepatch, size_t length);
70a2021292SJérôme Duval 
71a2021292SJérôme Duval 
72a2021292SJérôme Duval #endif
73a2021292SJérôme Duval 
74a2021292SJérôme Duval #endif	/* _KERNEL_ARCH_x86_ALTCODEPATCH_H */
75