os-k/kaleid/include/kernel/kernbase.h

206 lines
5.5 KiB
C

//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Kaleid Kernel base types and functionalities //
//----------------------------------------------------------------------------//
//------------------------------------------//
// Dependencies //
//------------------------------------------//
#ifndef _KALEID_H
#include <kaleid.h>
#endif
//------------------------------------------//
// Start of header //
//------------------------------------------//
#ifndef _KALKERN_BASE_H
#define _KALKERN_BASE_H
//------------------------------------------//
// Elementary types //
//------------------------------------------//
typedef struct sLock_t volatile Lock_t;
typedef struct sThread_t Thread_t;
typedef struct sProcess_t Process_t;
typedef struct sTerminal_t Terminal_t;
typedef struct sListHead_t ListHead_t;
typedef struct sListNode_t ListNode_t;
//------------------------------------------//
// Values for __kstate //
//------------------------------------------//
//
// Current state of the kernel
//
typedef enum {
// the kernel is booting
KSTATE_INIT,
// the kernel is not running a process
KSTATE_KERNEL,
// a process is running in kernel mode
KSTATE_PROCESS,
// the kernel is panicking
KSTATE_PANIC,
} KernelState_t;
//------------------------------------------//
// Multiprocessor misc. //
//------------------------------------------//
#ifndef NCPU
#define NCPU 4
#endif
#define GetCurCPU() 0
//
// Declare an (extern) CPU-local variable
//
#define __DECLARE_PER_CPU(_X, _Tp, _Qual) \
_Qual _Tp __ ## _X [NCPU]; \
static inline _Tp Get ## _X (void) \
{ return __ ## _X [GetCurCPU()]; } \
static inline void _Set ## _X (_Tp _Y) \
{ (__ ## _X [GetCurCPU()] = _Y); }
#define DECLARE_PER_CPU(_X, _Tp) \
__DECLARE_PER_CPU(_X, _Tp, extern)
#define LOCAL_DEC_PER_CPU(_X, _Tp) \
__DECLARE_PER_CPU(_X, _Tp, static)
//
// Actually creates a CPU-local variable
//
#define CREATE_PER_CPU(_X, _Tp) \
_Tp __ ## _X [NCPU] = { (_Tp) 0 }
//------------------------------------------//
// Global constants //
//------------------------------------------//
// XXX
DECLARE_PER_CPU(PanicStr, const char *);
DECLARE_PER_CPU(KernState, KernelState_t);
DECLARE_PER_CPU(_StdOut, Terminal_t *);
DECLARE_PER_CPU(_StdDbg, Terminal_t *);
DECLARE_PER_CPU(CurProc, Process_t *);
DECLARE_PER_CPU(CurThread, Thread_t *);
//------------------------------------------//
// Macros for manipulating said //
// global constants //
//------------------------------------------//
#define SetPanicStr(str) \
do { \
SetKernState(KSTATE_PANIC); \
_SetPanicStr(str); \
} while (0)
#define SetKernState(x) \
do { \
_SetKernState(x); \
} while (0)
#define GetStdOut() (GetCurProc() == NULL ? Get_StdOut() : NULL)
#define SetStdOut(tm) \
do { \
if (GetCurProc() == NULL) \
_Set_StdOut(tm); \
} while (0);
#define GetStdDbg() (GetCurProc() == NULL ? Get_StdDbg() : NULL)
#define SetStdDbg(tm) \
do { \
if (GetCurProc() == NULL) \
_Set_StdDbg(tm); \
} while (0)
//------------------------------------------//
// Other Macros //
//------------------------------------------//
//
// Size of a tabulation in spaces
// Default: 4 spaces/tab
//
#define KTABSIZE 4
//
// Disable IRQs
//
#define DisableIRQs() asm volatile ("cli")
//
// Enable IRQs
//
#define EnableIRQs() asm volatile ("sti")
//
// Pause CPU until next interuption
// !!! Enables IRQs !!!
//
#define PauseCPU() asm volatile("sti\n\thlt")
//
// Halt the CPU indefinitely
//
#define HaltCPU() do { asm volatile ("hlt"); } while (1)
//------------------------------------------//
// Some base functions //
//------------------------------------------//
noreturn void StartPanic(const char *);
noreturn void CrashSystem(void);
//------------------------------------------//
// Useful I/O inlines //
//------------------------------------------//
static inline
void WriteByteOnPort(port_t port, port_t val)
{
asm volatile ("out %0, %1" : : "dN" (port), "a" (val));
}
static inline
uchar ReadByteFromPort(port_t port)
{
errno = ENOSYS;
(void)port;
return 0;
}
static inline
ushort ReadWordFromPort(port_t port)
{
errno = ENOSYS;
(void)port;
return 0;
}
//------------------------------------------//
// End of header //
//------------------------------------------//
#endif