2019-02-16 23:36:33 +01:00
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
// GNU GPL OS/K //
|
|
|
|
// //
|
|
|
|
// Desc: Miscenalleous I/O functions and inlines //
|
|
|
|
// //
|
|
|
|
// //
|
|
|
|
// 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/>. //
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
|
|
|
|
#ifndef _KALKERN_BASE_H
|
|
|
|
#include <kernel/base.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _KALKERN_IOMISC_H
|
|
|
|
#define _KALKERN_IOMISC_H
|
|
|
|
|
|
|
|
//------------------------------------------//
|
2019-04-21 21:18:13 +02:00
|
|
|
// IRQ-related stuff //
|
|
|
|
//------------------------------------------//
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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-04-24 23:00:12 +02:00
|
|
|
extern void IoSendEOItoPIC(uchar isr);
|
|
|
|
extern void IoEnableNMI(void);
|
|
|
|
extern void IoDisableNMI(void);
|
2019-05-06 22:04:22 +02:00
|
|
|
error_t IdtRegisterIsr(void (*isr)(ISRFrame_t *regs), uchar isrNo);
|
2019-04-24 23:00:12 +02:00
|
|
|
|
2019-04-21 21:18:13 +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 //
|
|
|
|
//------------------------------------------//
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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 //
|
|
|
|
//------------------------------------------//
|
|
|
|
|
|
|
|
static inline void IoWriteByteOnPort(port_t port, uchar val)
|
|
|
|
{ asm volatile ("outb %1, %0" : : "dN" (port), "a" (val)); }
|
2019-02-16 23:36:33 +01:00
|
|
|
|
2019-05-07 23:16:56 +02:00
|
|
|
static inline void IoWriteWordOnPort(port_t port, ushort val)
|
|
|
|
{ asm volatile ("outw %1, %0" : : "dN" (port), "a" (val)); }
|
|
|
|
|
|
|
|
static inline void IoWriteDWordOnPort(port_t port, uint val)
|
|
|
|
{ asm volatile ("outl %1, %0" : : "dN" (port), "a" (val)); }
|
|
|
|
|
2019-04-21 21:18:13 +02:00
|
|
|
static inline uchar IoReadByteFromPort(port_t port) {
|
|
|
|
uchar ret;
|
|
|
|
asm volatile ("inb %1, %0" : "=a"(ret) : "Nd"(port));
|
|
|
|
return ret;
|
2019-02-16 23:36:33 +01:00
|
|
|
}
|
|
|
|
|
2019-04-21 21:18:13 +02:00
|
|
|
static inline ushort IoReadWordFromPort(port_t port) {
|
|
|
|
ushort ret;
|
|
|
|
asm volatile ("inw %1, %0" : "=a"(ret) : "Nd"(port));
|
|
|
|
return ret;
|
2019-02-16 23:36:33 +01:00
|
|
|
}
|
|
|
|
|
2019-04-21 21:18:13 +02:00
|
|
|
static inline ushort IoReadDWordFromPort(port_t port) {
|
|
|
|
uint ret;
|
|
|
|
asm volatile ("inl %1, %0" : "=a"(ret) : "Nd"(port));
|
|
|
|
return ret;
|
2019-02-16 23:36:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------//
|
2019-04-21 21:18:13 +02: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
|
|
|
|
|