You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
2.2 KiB
C
98 lines
2.2 KiB
C
/* See the end of this file for copyright and license terms. */
|
|
|
|
#pragma once
|
|
|
|
#include <gay/cdefs.h>
|
|
#include <gay/types.h>
|
|
|
|
static __always_inline void enable_intr(void)
|
|
{
|
|
__asm__ volatile("sti");
|
|
}
|
|
|
|
static __always_inline void disable_intr(void)
|
|
{
|
|
__asm__ volatile("cli" ::: "memory");
|
|
}
|
|
|
|
static inline register_t read_flags(void)
|
|
{
|
|
register_t flags;
|
|
|
|
#ifdef __x86_64__
|
|
__asm__(
|
|
" pushfq \n"
|
|
" popq %0 \n"
|
|
: "=r"(flags)
|
|
);
|
|
#else
|
|
__asm__(
|
|
" pushfl \n"
|
|
" popl %0 \n"
|
|
: "=r"(flags)
|
|
);
|
|
#endif
|
|
|
|
return flags;
|
|
}
|
|
|
|
/**
|
|
* @brief Temporarily disable interrupts.
|
|
*
|
|
* It should be noted that yes, there is also `disable_intr()`, which does the
|
|
* exact same thing except it has no return value. This is yet another instance
|
|
* of me finding it funny to cause unnecessary confusion, but this time it's
|
|
* also because FreeBSD uses the same unfortunate naming scheme.
|
|
*
|
|
* @return The contents of the status register before disabling interrupts.
|
|
* This should be passed to `intr_restore()` after the atomic operation
|
|
* is complete so that the interrupt enable flag can be restored to what
|
|
* it was before.
|
|
*/
|
|
static inline register_t intr_disable(void)
|
|
{
|
|
register_t eflags = read_flags();
|
|
disable_intr();
|
|
return eflags;
|
|
}
|
|
|
|
static inline void intr_restore(register_t flags)
|
|
{
|
|
/* bit 9 is IF (Interrupt Enable Flag) */
|
|
if (flags & (1 << 9))
|
|
enable_intr();
|
|
}
|
|
|
|
static inline bool intr_enabled(void)
|
|
{
|
|
register_t eflags = read_flags();
|
|
return (eflags & (1 << 9)) != 0;
|
|
}
|
|
|
|
static __always_inline void halt(void)
|
|
{
|
|
__asm__ volatile("hlt");
|
|
}
|
|
|
|
static __always_inline void x86_pause(void)
|
|
{
|
|
__asm__ volatile("pause");
|
|
}
|
|
|
|
/** @brief Use this macro to build spin-wait loops. */
|
|
#define spin_loop for (;; x86_pause())
|
|
|
|
/*
|
|
* This file is part of GayBSD.
|
|
* Copyright (c) 2021 fef <owo@fef.moe>.
|
|
*
|
|
* GayBSD is nonviolent software: you may only use, redistribute, and/or
|
|
* modify it under the terms of the Cooperative Nonviolent Public License
|
|
* (CNPL) as found in the LICENSE file in the source code root directory
|
|
* or at <https://git.pixie.town/thufie/npl-builder>; either version 7
|
|
* of the license, or (at your option) any later version.
|
|
*
|
|
* GayBSD comes with ABSOLUTELY NO WARRANTY, to the extent
|
|
* permitted by applicable law. See the CNPL for details.
|
|
*/
|