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