From 2081b1521963e0d521b3c467d467a2a18940baba Mon Sep 17 00:00:00 2001 From: "Chloe M." Date: Mon, 13 Jul 2026 03:54:22 +0000 Subject: [PATCH] ptos/amd64+hal: intr: Add IRQL managmenet Signed-off-by: Chloe M. --- service/ptos/arch/amd64/cpu/intr.c | 57 ++++++++++++++++++++++++++++++ service/ptos/head/hal/intr.h | 23 ++++++++++++ service/ptos/head/ke/bugcheck.h | 6 +++- service/ptos/ke/bugcheck.c | 4 ++- 4 files changed, 88 insertions(+), 2 deletions(-) diff --git a/service/ptos/arch/amd64/cpu/intr.c b/service/ptos/arch/amd64/cpu/intr.c index c76ab45..1589017 100644 --- a/service/ptos/arch/amd64/cpu/intr.c +++ b/service/ptos/arch/amd64/cpu/intr.c @@ -7,6 +7,7 @@ */ #include +#include #include #include #include @@ -14,6 +15,20 @@ /* Globals */ static INTR_HANDLER HandlerTable[256]; +/* + * Set a new IRQL + */ +static inline VOID +SetIrql(IRQL Irql) +{ + ASMV( + "mov %0, %%cr8" + : + : "r" ((UQUAD)Irql) + : "memory" + ); +} + UCHAR HalIntrRegister(INTR_HANDLER *Handler, BOOLEAN IsUser) { @@ -56,3 +71,45 @@ HalIntrRegister(INTR_HANDLER *Handler, BOOLEAN IsUser) /* No more vectors */ return 0; } + +IRQL +HalGetIrql(VOID) +{ + UQUAD CurrentIrql; + + ASMV( + "mov %%cr8, %0" + : "=r" (CurrentIrql) + : + : "memory" + ); + + return (IRQL)CurrentIrql; +} + +IRQL +HalRaiseIrql(IRQL Irql) +{ + IRQL CurrentIrql; + + CurrentIrql = HalGetIrql(); + if (Irql < CurrentIrql) { + KeBugCheck(BUGCHECK_IRQL_NOT_GTE, "got bad irql\n"); + } + + SetIrql(Irql); + return CurrentIrql; +} + +IRQL +HalLowerIrql(IRQL Irql) +{ + IRQL CurrentIrql; + + CurrentIrql = HalGetIrql(); + if (Irql > CurrentIrql) { + KeBugCheck(BUGCHECK_IRQL_NOT_LTE, "got bad irql\n"); + } + + return CurrentIrql; +} diff --git a/service/ptos/head/hal/intr.h b/service/ptos/head/hal/intr.h index 6548cba..a5dbd07 100644 --- a/service/ptos/head/hal/intr.h +++ b/service/ptos/head/hal/intr.h @@ -52,4 +52,27 @@ typedef struct _INTR_HANDLER { */ UCHAR HalIntrRegister(INTR_HANDLER *Handler, BOOLEAN IsUser); +/* + * Returns the current IRQL + */ +IRQL HalGetIrql(VOID); + +/* + * Raise the IRQL level to a higher level + * + * @Irql: IRQL to raise to + * + * Returns the previous IRQL + */ +IRQL HalRaiseIrql(IRQL Irql); + +/* + * Lower the IRQL level to a lower level + * + * @Irql: IRQL to lower to + * + * Returns the previous IRQL + */ +IRQL HalLowerIrql(IRQL Irql); + #endif /* !_HAL_INTR_H_ */ diff --git a/service/ptos/head/ke/bugcheck.h b/service/ptos/head/ke/bugcheck.h index 7cad9ff..82dd702 100644 --- a/service/ptos/head/ke/bugcheck.h +++ b/service/ptos/head/ke/bugcheck.h @@ -19,13 +19,17 @@ * @BUGCHECK_IO_ERROR: Fatal I/O error * @BUGCHECK_UNBOUND_RESRC: Fatal unbound resource * @BUGCHECK_EXCEPTION: Fatal exception + * @BUGCHECK_IRQL_NOT_GTE: IRQL not greater than or equal + * @BUGCHECK_IRQL_NOT_LTE: IRQL not less than or equal */ typedef enum { BUGCHECK_MISC, BUGCHECK_OOM, BUGCHECK_IO_ERROR, BUGCHECK_UNBOUND_RESRC, - BUGCHECK_EXCEPTION + BUGCHECK_EXCEPTION, + BUGCHECK_IRQL_NOT_GTE, + BUGCHECK_IRQL_NOT_LTE } BUGCHECK_REASON; /* diff --git a/service/ptos/ke/bugcheck.c b/service/ptos/ke/bugcheck.c index e3db2d2..8d33b42 100644 --- a/service/ptos/ke/bugcheck.c +++ b/service/ptos/ke/bugcheck.c @@ -36,7 +36,9 @@ static CHAR *ReasonTable[] = { [BUGCHECK_OOM] = "out of memory", [BUGCHECK_IO_ERROR] = "i/o error", [BUGCHECK_UNBOUND_RESRC] = "unbounded resource", - [BUGCHECK_EXCEPTION] = "fatal exception" + [BUGCHECK_EXCEPTION] = "fatal exception", + [BUGCHECK_IRQL_NOT_GTE] = "irql not greater than or equal", + [BUGCHECK_IRQL_NOT_LTE] = "irql not less than or equal" }; VOID