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

235 lines
5.6 KiB
C

//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Kaleid Kernel base types and functionalities //
//----------------------------------------------------------------------------//
#ifndef _KALBASE_H
#include <kalbase.h>
#endif
//------------------------------------------//
#ifndef _KALKERN_BASE_H
#define _KALKERN_BASE_H
//------------------------------------------//
typedef struct sLock_t 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;
//------------------------------------------//
/* XXX */
//
// 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 INITOK
#define INITOK ((unsigned int)0xCAFEBABE)
#endif
#ifndef NCPUS
#define NCPUS 4
#endif
#define GetCurCPU() 0
//
// Declare an (extern) CPU-local variable
//
#define __DECLARE_PER_CPU(_X, _Tp, _Qual) \
_Qual _Tp __ ## _X [NCPUS]; \
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 [NCPUS] = { (_Tp) 0 }
//------------------------------------------//
// 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 *);
DECLARE_PER_CPU(ReSchedFlag, bool);
DECLARE_PER_CPU(PreemptCount, ulong);
//------------------------------------------//
#define SetKernState(x) _SetKernState(x);
//
// StdOut/StdDbg manipulation
//
#define GetStdOut() (GetCurProc() == NULL ? Get_StdOut() : GetCurProc()->stdOut)
#define GetStdDbg() (GetCurProc() == NULL ? Get_StdDbg() : GetCurProc()->stdDbg)
#define SetStdOut(tm) do { if (GetCurProc() == NULL) _Set_StdOut(tm); \
else GetCurProc()->stdOut = (tm); } while (0)
#define SetStdDbg(tm) do { if (GetCurProc() == NULL) _Set_StdDbg(tm); \
else GetCurProc()->stdDbg = (tm); } while (0)
//
// Re-scheduling and preemption
// XXX XXX XXX atomic operations
//
#define SetReSchedFlag(x) _SetReSchedFlag(x)
#define DisablePreemption() _SetPreemptCount(GetPreemptCount()+1)
#define EnablePreemption() do { KalAssert(GetPreemptCount() > 0); \
_SetPreemptCount(GetPreemptCount()-1); } while(0)
//------------------------------------------//
//
// Value of the preemption count indicating that preemption is activated
//
#define PREEMPT_ON 0
//
// 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)
//------------------------------------------//
//
// A process
//
typedef struct sProcess_t {
// Identifier
int pid;
// Current priority class
int prioClass;
// Default priority class (without boosts)
int defPrioClass;
// Current priority level
int prioLevel;
// Default priority level
int defPrioLevel;
// Current state
int procState;
// Remaining time running
ulong timeSlice;
// Default time-slice
ulong defTimeSlice;
// Scheduler internals
ListNode_t *schedNode;
// Standard output/debug
Terminal_t *stdOut, *stdDbg;
} Process_t;
//------------------------------------------//
noreturn void StartPanic(const char *);
noreturn void CrashSystem(void);
//------------------------------------------//
// Useful I/O inlines //
//------------------------------------------//
static inline
void WriteByteOnPort(port_t port, port_t val)
{
KalAssert(FALSE && ENOSYS);
(void)port;
(void)val;
}
static inline
uchar ReadByteFromPort(port_t port)
{
KalAssert(FALSE && ENOSYS);
(void)port;
return 0;
}
static inline
ushort ReadWordFromPort(port_t port)
{
KalAssert(FALSE && ENOSYS);
(void)port;
return 0;
}
//------------------------------------------//
#endif