os-k/include/kernel.h

226 lines
7.2 KiB
C

//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Desc: Kaleid kernel base include file //
// //
// //
// 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 _LIBC_H
#include <libc.h>
#endif
#ifndef _KERNEL_H
#define _KERNEL_H
//----------------------------------------------------------------------------//
typedef volatile ulong Spinlock_t;
typedef Spinlock_t Lock_t;
//typedef struct Lock_t Lock_t;
typedef struct Time_t Time_t;
typedef struct Buffer_t Buffer_t;
typedef struct ListHead_t ListHead_t;
typedef struct ListNode_t ListNode_t;
typedef struct Thread_t Thread_t;
typedef struct Process_t Process_t;
typedef struct BootInfo_t BootInfo_t;
typedef struct CpuCore_t CpuCore_t;
typedef struct CpuInfo_t CpuInfo_t;
typedef struct ISRFrame_t ISRFrame_t;
typedef enum ProcState_t ProcState_t;
typedef struct IdtDescriptor_t IdtDescriptor_t;
typedef struct IdtEntry_t IdtEntry_t;
typedef struct IdtPtr_t IdtPtr_t;
typedef struct ISRList_t ISRList_t;
typedef struct ISRFrame_t ISRFrame_t;
typedef struct MemoryMap_t MemoryMap_t;
typedef struct MapEntry_t MapEntry_t;
typedef struct GdtEntry_t GdtEntry_t;
typedef struct GdtPtr_t GdtPtr_t;
typedef struct Tss_t Tss_t;
typedef struct TssDescriptor_t TssDescriptor_t;
//----------------------------------------------------------------------------//
//
// Holds all CPU-local variables
//
struct CpuCore_t
{
// CPU number, index in CPU list
int index;
// CPU APIC id
int apicId;
CpuInfo_t *infos;
// Number of ticks since boot time
ulong ticks; // XXX I/O APIC
// Current process & thread
Process_t *process;
Thread_t *thread;
// Need re-schedule?
bool needReSched;
// Is preemption ON? ("PREEMPT_ON" in sched.h)
ulong preemptCount;
// List heads for the four priority classes
ListHead_t *idlePrioProcs;
ListHead_t *reglPrioProcs;
ListHead_t *servPrioProcs;
ListHead_t *timeCritProcs;
};
struct CpuInfo_t
{
// CPU Vendor String (always 12 characters)
char vendorStr[12];
// CPU Model code (enum in cpu.h)
int modelCode;
// CPU Features flag
uint featureFlag;
};
struct ISRFrame_t {
/* The registers */
ulong efer;
ulong cr0;
ulong cr2;
ulong cr3;
ulong cr4;
ulong cr8;
ulong r15;
ulong r14;
ulong r13;
ulong r12;
ulong r11;
ulong r10;
ulong r9;
ulong r8;
ulong rbp;
ulong rdi;
ulong rsi;
ulong rdx;
ulong rcx;
ulong rbx;
ulong rax;
/* The error code and interrupt id */
ulong intNo;
ulong ErrorCode;
/* These are pushed automatically by the CPU */
ulong rip;
ulong cs;
ulong rflags;
ulong rsp;
ulong ss;
} __attribute__((__packed__));
//----------------------------------------------------------------------------//
#ifndef NCPUS
#define NCPUS 1
#endif
// Current CPU number
#define _KeCurCPU 0
extern int KeCPUCount;
extern volatile CpuCore_t *KeCurCPU;
extern CpuCore_t _KeCPUTable[NCPUS];
//----------------------------------------------------------------------------//
#define KeCurProc (KeCurCPU->process)
#define KeCurThread (KeCurCPU->thread)
//----------------------------------------------------------------------------//
#include <asm.h>
//----------------------------------------------------------------------------//
#define DEC_PER_CPU(pref, name, field, type) \
static inline type pref##Get##name() { return KeGetCurCPU()->field; } \
static inline void _##pref##Set##name(type __val) \
{ KeGetCurCPU()->field = __val; }
//----------------------------------------------------------------------------//
#ifdef NDEBUG
#define DEBUG if(0)
#else
#define DEBUG if(1)
#endif
//----------------------------------------------------------------------------//
error_t KernLog(const char *, ...);
#ifndef NDEBUG
void DebugLog(const char *, ...);
#else
#define DebugLog(fmt, ...) ((void)0)
#endif
//----------------------------------------------------------------------------//
extern volatile bool KeIsPanicking;
noreturn void KeStartPanic(const char *, ...);
//----------------------------------------------------------------------------//
#define XBUG(c) do { \
volatile ushort *_vga \
=(volatile ushort*)0xB8000; \
while(_vga<(volatile ushort*)(0xB8000+(80*25*2))) \
*_vga++=(c|(0xf<<8)); \
asm volatile("cli\nhlt"); \
}while(0)
#define BUG() XBUG('B')
#define BUGON(x) if(x)BUG()
//----------------------------------------------------------------------------//
#define __GET_GETRIP() \
__attribute__((__noinline__)) \
static ulong __getrip() \
{ return (size_t)__builtin_return_address(0); }
#define __PRINT_RIP() \
DebugLog("%s.RIP=%p\n",__func__,__getrip())
//----------------------------------------------------------------------------//
#endif