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#define ALTCODEPATCH_TAG_XSAVE 3 26#define ALTCODEPATCH_TAG_XRSTOR 4 27 28 29#ifdef _ASSEMBLER 30 31#define CODEPATCH_START 990: 32#define CODEPATCH_END(tag) 991: \ 33 .pushsection .altcodepatch, "a" ; \ 34 .long (990b - KERNEL_LOAD_BASE) ; \ 35 .short (991b - 990b) ; \ 36 .short tag ; \ 37 .popsection 38 39#define ASM_STAC CODEPATCH_START \ 40 ASM_NOP3 ; \ 41 CODEPATCH_END(ALTCODEPATCH_TAG_STAC) 42 43#define ASM_CLAC CODEPATCH_START \ 44 ASM_NOP3 ; \ 45 CODEPATCH_END(ALTCODEPATCH_TAG_CLAC) 46 47#else 48 49#define _STRINGIFY(x...) #x 50#define STRINGIFY(x...) _STRINGIFY(x) 51 52#define CODEPATCH_START "990: \n" 53#define CODEPATCH_END(tag) "991: \n" \ 54 ".pushsection .altcodepatch, \"a\" \n" \ 55 ".long (990b - " STRINGIFY(KERNEL_LOAD_BASE) ") \n" \ 56 ".short (991b - 990b) \n" \ 57 ".short " STRINGIFY(tag) " \n" \ 58 ".popsection" 59 60#define ASM_STAC CODEPATCH_START \ 61 STRINGIFY(ASM_NOP3) "\n" \ 62 CODEPATCH_END(ALTCODEPATCH_TAG_STAC) 63 64#define ASM_CLAC CODEPATCH_START \ 65 STRINGIFY(ASM_NOP3) "\n"\ 66 CODEPATCH_END(ALTCODEPATCH_TAG_CLAC) 67 68 69void arch_altcodepatch_replace(uint16 tag, void* newcodepatch, size_t length); 70 71 72#endif 73 74#endif /* _KERNEL_ARCH_x86_ALTCODEPATCH_H */ 75