os-k/include/asm.h

156 lines
4.9 KiB
C
Raw Normal View History

2019-02-16 23:36:33 +01:00
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
2019-05-13 23:22:27 +02:00
// Desc: Inline assembly functions //
2019-02-16 23:36:33 +01:00
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// any later version. //
// //
// OS/K is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
2019-05-13 23:22:27 +02:00
#ifndef _LIBC_H
#include <libc.h>
2019-02-16 23:36:33 +01:00
#endif
2019-05-13 23:22:27 +02:00
#ifndef _ASM_H
#define _ASM_H
2019-02-16 23:36:33 +01:00
//------------------------------------------//
// IRQ-related stuff //
//------------------------------------------//
2019-05-19 00:07:01 +02:00
//
// Launch interrupt
//
#define interrupt(n) asm volatile ("int %0" : : "N" (n) : "cc", "memory")
//
// Returns whether IRQs are enabled
//
static inline bool KeCheckIRQStatus(void) {
ulong flags;
asm volatile ("pushf\n\tpop %0" : "=g"(flags) );
return flags & (1 << 9);
}
//
// Disables IRQs
//
#define KeDisableIRQs() asm volatile ("cli")
//
// Enables IRQs
//
#define KeEnableIRQs() asm volatile ("sti")
//
// Disables IRQs, but saves its previous state
//
static inline ulong KePauseIRQs(void) {
ulong flags;
asm volatile ("pushf\n\tcli\n\tpop %0" : "=r"(flags) : : "memory");
return flags;
}
2019-05-14 11:48:07 +02:00
extern void KeSendEOItoPIC(uchar isr);
extern void KeEnableNMI(void);
extern void KeDisableNMI(void);
2019-04-24 23:00:12 +02:00
//
// Restore IRQ flag to its state before KePauseIRQs
//
#define KeRestoreIRQs(flags) asm("push %0\n\tpopf" : : "rm"(flags) : "memory","cc")
//------------------------------------------//
// 101 ways to hang the system //
//------------------------------------------//
2019-05-13 23:22:27 +02:00
//
// Just relaxes the CPU a few cycles
//
#define KeRelaxCPU() asm volatile("pause\n": : : "memory")
//
// Pauses CPU until next interuption
//
#define KePauseCPU() asm volatile ("sti\n\thlt")
//
// Halts the CPU indefinitely
//
#define KeHaltCPU() do { asm volatile ("hlt"); } while (1)
//
// Halts the system, making sure it won't recover
//
#define KeCrashSystem() do { KeDisableIRQs(); KeHaltCPU(); } while (1)
//------------------------------------------//
// Ports I/O //
//------------------------------------------//
2019-05-13 23:22:27 +02:00
static inline void IoWriteByteOnPort(ushort port, uchar val)
{ asm volatile ("outb %1, %0" : : "dN" (port), "a" (val)); }
2019-02-16 23:36:33 +01:00
2019-05-13 23:22:27 +02:00
static inline void IoWriteWordOnPort(ushort port, ushort val)
2019-05-07 23:16:56 +02:00
{ asm volatile ("outw %1, %0" : : "dN" (port), "a" (val)); }
2019-05-13 23:22:27 +02:00
static inline void IoWriteDWordOnPort(ushort port, uint val)
2019-05-07 23:16:56 +02:00
{ asm volatile ("outl %1, %0" : : "dN" (port), "a" (val)); }
2019-05-13 23:22:27 +02:00
static inline uchar IoReadByteFromPort(ushort port) {
uchar ret;
asm volatile ("inb %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
2019-02-16 23:36:33 +01:00
}
2019-05-13 23:22:27 +02:00
static inline ushort IoReadWordFromPort(ushort port) {
ushort ret;
asm volatile ("inw %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
2019-02-16 23:36:33 +01:00
}
2019-05-13 23:22:27 +02:00
static inline ushort IoReadDWordFromPort(ushort port) {
uint ret;
asm volatile ("inl %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
2019-02-16 23:36:33 +01:00
}
2019-05-22 23:32:44 +02:00
static inline ulong KeReadStsc(void) {
int eax, edx;
asm volatile ("rdtsc":"=a"(eax),"=d"(edx));
return ((ulong)edx << 32) + eax;
}
2020-01-09 20:58:57 +01:00
static inline void KeFlushTlbSingle(ulong addr)
2020-01-08 00:28:10 +01:00
{
asm volatile("invlpg (%0)" ::"r" (addr) : "memory");
}
2019-02-16 23:36:33 +01:00
//------------------------------------------//
// Misc. I/O //
//------------------------------------------//
//
// Wait for completion of some I/O operation
// Port 0x80 is used for 'checkpoints' during POST
//
#define IoWaitCompletion() IoWriteByteOnPort(0x80, 0)
2019-02-16 23:36:33 +01:00
#endif