1 /* 2 * Copyright 2018, Jérôme Duval, jerome.duval@gmail.com. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _KERNEL_ARCH_x86_ALTCODEPATCH_H 6 #define _KERNEL_ARCH_x86_ALTCODEPATCH_H 7 8 9 #include <arch/x86/arch_kernel.h> 10 11 12 #define ASM_NOP1 .byte 0x90 13 #define ASM_NOP2 .byte 0x66, 0x90 14 #define ASM_NOP3 .byte 0x0f, 0x1f, 0x00 15 #define ASM_NOP4 .byte 0x0f, 0x1f, 0x40, 0x00 16 #define ASM_NOP5 .byte 0x0f, 0x1f, 0x44, 0x00, 0x00 17 #define ASM_NOP6 .byte 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 18 #define ASM_NOP7 .byte 0x0f, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00 19 #define ASM_NOP8 .byte 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 20 #define ASM_NOP9 .byte 0x66, 0x0f, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 21 22 23 #define ALTCODEPATCH_TAG_STAC 1 24 #define ALTCODEPATCH_TAG_CLAC 2 25 26 27 #ifdef _ASSEMBLER 28 29 #define CODEPATCH_START 990: 30 #define CODEPATCH_END(tag) 991: \ 31 .pushsection .altcodepatch, "a" ; \ 32 .long (990b - KERNEL_LOAD_BASE) ; \ 33 .short (991b - 990b) ; \ 34 .short tag ; \ 35 .popsection 36 37 #define ASM_STAC CODEPATCH_START \ 38 ASM_NOP3 ; \ 39 CODEPATCH_END(ALTCODEPATCH_TAG_STAC) 40 41 #define ASM_CLAC CODEPATCH_START \ 42 ASM_NOP3 ; \ 43 CODEPATCH_END(ALTCODEPATCH_TAG_CLAC) 44 45 #else 46 47 #define _STRINGIFY(x...) #x 48 #define STRINGIFY(x...) _STRINGIFY(x) 49 50 #define CODEPATCH_START "990: \n" 51 #define CODEPATCH_END(tag) "991: \n" \ 52 ".pushsection .altcodepatch, \"a\" \n" \ 53 ".long (990b - " STRINGIFY(KERNEL_LOAD_BASE) ") \n" \ 54 ".short (991b - 990b) \n" \ 55 ".short " STRINGIFY(tag) " \n" \ 56 ".popsection" 57 58 #define ASM_STAC CODEPATCH_START \ 59 STRINGIFY(ASM_NOP3) "\n" \ 60 CODEPATCH_END(ALTCODEPATCH_TAG_STAC) 61 62 #define ASM_CLAC CODEPATCH_START \ 63 STRINGIFY(ASM_NOP3) "\n"\ 64 CODEPATCH_END(ALTCODEPATCH_TAG_CLAC) 65 66 67 void arch_altcodepatch_replace(uint16 tag, void* newcodepatch, size_t length); 68 69 70 #endif 71 72 #endif /* _KERNEL_ARCH_x86_ALTCODEPATCH_H */ 73