Skip to content

Commit

Permalink
lib,irq: implement interrupts enable/disable with restore
Browse files Browse the repository at this point in the history
Signed-off-by: Pawel Wieczorkiewicz <[email protected]>
  • Loading branch information
wipawel committed Nov 13, 2023
1 parent f44f421 commit a31cc7e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
4 changes: 2 additions & 2 deletions arch/x86/apic.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ void init_apic_timer(void) {
printk("Initializing local APIC timer\n");

/* Enable interrupts for calibration */
sti();
unsigned long flags = interrupts_enable_save();

/* Spend 200ms calibrating the timer, 10 iterations of 20ms each */
for (i = 0; i < CAL_ITERATIONS; ++i) {
Expand All @@ -199,7 +199,7 @@ void init_apic_timer(void) {
min_ticks = min(min_ticks, elapsed_ticks);
}

cli();
interrupts_restore(flags);

/* Interrupt every min_ticks ticks */
apic_write(APIC_TMR_DCR, APIC_TIMER_DIVIDE_BY_16);
Expand Down
23 changes: 23 additions & 0 deletions include/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,33 @@ static inline void interrupts_enable(void) {
sti();
}

static inline unsigned long interrupts_enable_save(void) {
unsigned long flags = read_eflags();
interrupts_enable();
return flags;
}

static inline void interrupts_disable(void) {
cli();
}

static inline unsigned long interrupts_disable_save(void) {
unsigned long flags = read_eflags();
interrupts_disable();
return flags;
}

static inline void interrupts_restore(unsigned long flags) {
if (flags & X86_EFLAGS_IF)
sti();
else
cli();
}

static inline bool interrupts_enabled(void) {
return read_eflags() & X86_EFLAGS_IF;
}

static inline unsigned long read_cs(void) {
unsigned long cs;

Expand Down
4 changes: 2 additions & 2 deletions tests/test_cond_branch_mispredictions.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,13 @@ int __aligned(PAGE_SIZE) test_cond_branch_mispredictions(void *unused) {
printk("Testing conditional branch %s BTB flushing\n",
WITH_BTB_FLUSH ? "with" : "without");

cli();
unsigned long flags = interrupts_disable_save();
test_cond_forward_branch_cl0(LOOP_ITERATIONS);
test_cond_forward_branch_cl1(LOOP_ITERATIONS);

test_cond_backward_branch_cl0(LOOP_ITERATIONS);
test_cond_backward_branch_cl1(LOOP_ITERATIONS);
sti();
interrupts_restore(flags);

return 0;
}
4 changes: 2 additions & 2 deletions tests/test_uncond_branch_mispredictions.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,13 @@ int __aligned(PAGE_SIZE) test_uncond_branch_mispredictions(void *unused) {
printk("Testing direct unconditional " STR(BRANCH) " %s BTB flushing\n",
WITH_BTB_FLUSH ? "with" : "without");

cli();
unsigned long flags = interrupts_disable_save();
test_uncond_forward_branch_cl0(LOOP_ITERATIONS);
test_uncond_forward_branch_cl1(LOOP_ITERATIONS);

test_uncond_backward_branch_cl0(LOOP_ITERATIONS);
test_uncond_backward_branch_cl1(LOOP_ITERATIONS);
sti();
interrupts_restore(flags);

return 0;
}

0 comments on commit a31cc7e

Please sign in to comment.