| 1 | /* @title: Kernel Text Patching */ |
|---|---|
| 2 | #pragma once |
| 3 | #include <asm.h> |
| 4 | #include <stdbool.h> |
| 5 | #include <stdint.h> |
| 6 | |
| 7 | #define CR0_WP (1UL << 16) |
| 8 | |
| 9 | struct text_patch_window { |
| 10 | uint64_t cr0; |
| 11 | bool interrupts; |
| 12 | }; |
| 13 | |
| 14 | static inline struct text_patch_window text_patch_begin(void) { |
| 15 | struct text_patch_window w = { |
| 16 | .interrupts = are_interrupts_enabled(), |
| 17 | }; |
| 18 | |
| 19 | disable_interrupts(); |
| 20 | w.cr0 = read_cr0(); |
| 21 | write_cr0(cr0: w.cr0 & ~CR0_WP); |
| 22 | |
| 23 | return w; |
| 24 | } |
| 25 | |
| 26 | static inline void text_patch_end(struct text_patch_window w) { |
| 27 | write_cr0(cr0: w.cr0); |
| 28 | |
| 29 | if (w.interrupts) |
| 30 | enable_interrupts(); |
| 31 | } |
| 32 |