PatchworkOS  69292a3
A non-POSIX operating system.
Loading...
Searching...
No Matches
cli.h
Go to the documentation of this file.
1#pragma once
2
3#include <assert.h>
4#include <kernel/cpu/cpu.h>
5#include <kernel/cpu/percpu.h>
6#include <kernel/cpu/regs.h>
7#include <stdbool.h>
8#include <stdint.h>
9
10/**
11 * @brief Clear Interrupt Flag (CLI) Handling
12 * @defgroup kernel_cpu_cli CLI
13 * @ingroup kernel_cpu
14 *
15 * Manages nested CLI (Clear Interrupt Flag) calls.
16 * @{
17 */
18
19/**
20 * @brief Increments the CLI depth, disabling interrupts if depth was zero.
21 *
22 * @warning Must have a matching `cli_pop()` call to re-enable interrupts when depth reaches zero.
23 */
24static inline void cli_push(void)
25{
26 uint64_t rflags = rflags_read();
27 ASM("cli" :: : "memory");
28
29 if (SELF->cli == 0)
30 {
31 SELF->oldRflags = rflags;
32 }
33 SELF->cli++;
34}
35
36/**
37 * @brief Decrements the CLI depth, re-enabling interrupts if depth reaches zero and interrupts were enabled prior to
38 * the first `cli_push()` call.
39 *
40 * @warning This function should only be called after a `cli_push()` call.
41 */
42static inline void cli_pop(void)
43{
45 assert(SELF->cli != 0);
46 SELF->cli--;
47 if (SELF->cli == 0 && (SELF->oldRflags & RFLAGS_INTERRUPT_ENABLE))
48 {
49 ASM("sti" :: : "memory");
50 }
51}
52
53/**
54 * @brief Macro to increment CLI depth for the duration of the current scope.
55 */
56#define CLI_SCOPE() \
57 cli_push(); \
58 __attribute__((cleanup(cli_scope_cleanup))) int CONCAT(i, __COUNTER__) = 1;
59
60static inline void cli_scope_cleanup(int* _)
61{
62 cli_pop();
63}
64
65/** @} */
#define assert(expression)
Definition assert.h:29
static void cli_pop(void)
Decrements the CLI depth, re-enabling interrupts if depth reaches zero and interrupts were enabled pr...
Definition cli.h:42
static void cli_push(void)
Increments the CLI depth, disabling interrupts if depth was zero.
Definition cli.h:24
static void cli_scope_cleanup(int *_)
Definition cli.h:60
#define SELF
Macro to access data in the current cpu.
Definition percpu.h:85
#define ASM(...)
Inline assembly macro.
Definition defs.h:164
#define RFLAGS_INTERRUPT_ENABLE
Definition regs.h:34
static uint64_t rflags_read(void)
Definition regs.h:80
__UINT64_TYPE__ uint64_t
Definition stdint.h:17