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

235 lines
5.6 KiB
C
Raw Normal View History

2019-01-21 15:00:04 +01:00
//----------------------------------------------------------------------------//
// 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
//------------------------------------------//
2019-02-06 14:07:38 +01:00
typedef struct sLock_t Lock_t;
2019-01-21 15:00:04 +01:00
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;
//------------------------------------------//
2019-02-06 14:07:38 +01:00
/* XXX */
2019-01-21 15:00:04 +01:00
//
// Current state of the kernel
//
typedef enum {
2019-02-06 14:07:38 +01:00
// The kernel is booting
2019-01-21 15:00:04 +01:00
KSTATE_INIT,
2019-02-06 14:07:38 +01:00
// The kernel is not running a process
2019-01-21 15:00:04 +01:00
KSTATE_KERNEL,
2019-02-06 14:07:38 +01:00
// A process is running in kernel mode
2019-01-21 15:00:04 +01:00
KSTATE_PROCESS,
2019-02-06 14:07:38 +01:00
// The kernel is panicking
2019-01-21 15:00:04 +01:00
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
2019-02-06 14:07:38 +01:00
DECLARE_PER_CPU(PanicStr, const char *);
2019-01-21 15:00:04 +01:00
2019-02-06 14:07:38 +01:00
DECLARE_PER_CPU(KernState, KernelState_t);
2019-01-21 15:00:04 +01:00
2019-02-06 14:07:38 +01:00
DECLARE_PER_CPU(_StdOut, Terminal_t *);
DECLARE_PER_CPU(_StdDbg, Terminal_t *);
2019-01-21 15:00:04 +01:00
2019-02-06 14:07:38 +01:00
DECLARE_PER_CPU(CurProc, Process_t *);
DECLARE_PER_CPU(CurThread, Thread_t *);
DECLARE_PER_CPU(ReSchedFlag, bool);
DECLARE_PER_CPU(PreemptCount, ulong);
2019-01-21 15:00:04 +01:00
//------------------------------------------//
2019-02-06 14:07:38 +01:00
#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)
2019-01-21 15:00:04 +01:00
//------------------------------------------//
2019-02-06 14:07:38 +01:00
//
// Value of the preemption count indicating that preemption is activated
//
#define PREEMPT_ON 0
2019-01-21 15:00:04 +01:00
//
// 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)
//------------------------------------------//
2019-02-06 14:07:38 +01:00
//
// 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;
2019-01-21 15:00:04 +01:00
//------------------------------------------//
noreturn void StartPanic(const char *);
noreturn void CrashSystem(void);
//------------------------------------------//
// Useful I/O inlines //
//------------------------------------------//
2019-02-06 14:07:38 +01:00
2019-01-21 15:00:04 +01:00
static inline
void WriteByteOnPort(port_t port, port_t val)
{
2019-02-06 14:07:38 +01:00
KalAssert(FALSE && ENOSYS);
(void)port;
(void)val;
2019-01-21 15:00:04 +01:00
}
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