mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-10-27 12:48:22 +00:00
kernel: fix taking smep/smap path on unsupported systems
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
|
||||
static bool has_smap;
|
||||
static bool has_smap_detected;
|
||||
static bool has_smep;
|
||||
|
||||
void arch_syscall_entry();
|
||||
static void illisr(isr_t *self, context_t *ctx) {
|
||||
@@ -79,6 +80,7 @@ static void enable_smep_if_available(void) {
|
||||
asm volatile("mov %%cr4, %0" : "=r"(cr4));
|
||||
cr4 |= CR4_SMEP;
|
||||
asm volatile("mov %0, %%cr4" : : "r"(cr4) : "memory");
|
||||
has_smep = true;
|
||||
}
|
||||
|
||||
static bool detect_smap(void) {
|
||||
@@ -115,6 +117,14 @@ void arch_cpu_user_access_end(void) {
|
||||
asm volatile("clac" : : : "memory");
|
||||
}
|
||||
|
||||
bool arch_cpu_smep_enabled(void) {
|
||||
return has_smep;
|
||||
}
|
||||
|
||||
bool arch_cpu_smap_enabled(void) {
|
||||
return has_smap;
|
||||
}
|
||||
|
||||
void arch_nmi_isr(context_t *ctx) {
|
||||
// we are in an NMI context, running on IST0
|
||||
// first load the proper gsbase to get access to cpu-local variables.
|
||||
|
||||
@@ -324,8 +324,14 @@ static volatile struct limine_executable_address_request kaddrreq = {
|
||||
#define ERROR_FETCH 16
|
||||
|
||||
static void pfisr(isr_t *self, context_t *ctx) {
|
||||
if ((ctx->rflags & ARCH_CONTEXT_RFLAGS_AC) == 0 && IS_USER_ADDRESS((void *)ctx->cr2) && !IS_USER_ADDRESS((void *)ctx->rip)) {
|
||||
_panic("SMAP/SMEP violation", ctx);
|
||||
if (!ARCH_CONTEXT_ISUSER(ctx) && IS_USER_ADDRESS((void *)ctx->cr2)) {
|
||||
if ((ctx->error & ERROR_FETCH) && arch_cpu_smep_enabled()) {
|
||||
_panic("SMEP violation", ctx);
|
||||
}
|
||||
|
||||
if ((ctx->error & ERROR_FETCH) == 0 && (ctx->rflags & ARCH_CONTEXT_RFLAGS_AC) == 0 && arch_cpu_smap_enabled()) {
|
||||
_panic("SMAP violation", ctx);
|
||||
}
|
||||
}
|
||||
|
||||
thread_t *thread = current_thread();
|
||||
|
||||
Reference in New Issue
Block a user