Networking - Reworked route table management
[tpg/acess2.git] / Kernel / include / acess.h
index 5708ad2..1c645c2 100644 (file)
@@ -1,26 +1,48 @@
 /*
  * AcessOS Microkernel Version
- * common.h
+ * acess.h
+ */
+#ifndef _ACESS_H
+#define _ACESS_H
+/**
+ * \file acess.h
+ * \brief Acess2 Kernel API Core
  */
-#ifndef _COMMON_H
-#define _COMMON_H
 
+//! NULL Pointer
 #define NULL   ((void*)0)
+//! Pack a structure
 #define PACKED __attribute__((packed))
+//! Mark a function as not returning
+#define NORETURN       __attribute__((noreturn))
+//! Mark a parameter as unused
 #define UNUSED(x)      UNUSED_##x __attribute__((unused))
+//! Get the offset of a member in a structure
 #define offsetof(st, m) ((Uint)((char *)&((st *)(0))->m - (char *)0 ))
 
-//#include <stdint.h>
+/**
+ * \name Boolean constants
+ * \{
+ */
+#define TRUE   1
+#define FALSE  0
+/**
+ * \}
+ */
+
 #include <arch.h>
 #include <stdarg.h>
 #include "errno.h"
 
 // --- Types ---
-typedef  int   tPID;
-typedef  int   tTID;
-typedef Uint   tUID;
-typedef Uint   tGID;
-typedef Sint64 tTimestamp;
+typedef Uint32 tPID;   //!< Process ID type
+typedef Uint32 tTID;   //!< Thread ID Type
+typedef Uint32 tUID;   //!< User ID Type
+typedef Uint32 tGID;   //!< Group ID Type
+typedef Sint64 tTimestamp;     //!< Timestamp (miliseconds since 00:00 1 Jan 1970)
+typedef Sint64 tTime;  //!< Same again
+typedef struct sShortSpinlock  tShortSpinlock; //!< Opaque (kinda) spinlock
+typedef int    bool;   //!< Boolean type
 
 // --- Helper Macros ---
 /**
@@ -32,32 +54,17 @@ typedef Sint64      tTimestamp;
 #define STR(x) #x
 #define EXPAND_STR(x) STR(x)
 
+extern char    __buildnum[];
+#define BUILD_NUM      ((int)(Uint)&__buildnum)
+extern const char gsGitHash[];
+
 #define VER2(major,minor)      ((((major)&0xFF)<<8)|((minor)&0xFF))
 /**
  * \}
  */
 
-/**
- * \name Per-Process Configuration Settings
- * \{
- */
-enum eConfigTypes {
-       CFGT_NULL,
-       CFGT_INT,
-       CFGT_HEAPSTR,
-       CFGT_PTR
-};
-enum eConfigs {
-       CFG_VFS_CWD,
-       CFG_VFS_MAXFILES,
-       CFG_VFS_CHROOT,
-       NUM_CFG_ENTRIES
-};
-#define CFGINT(id)     (*Threads_GetCfgPtr(id))
-#define CFGPTR(id)     (*(void**)Threads_GetCfgPtr(id))
-/**
- * \}
- */
+//! \brief Error number
+#define errno  (*Threads_GetErrno())
 
 // === CONSTANTS ===
 // --- Memory Flags --
@@ -75,39 +82,74 @@ enum eConfigs {
  * \}
  */
 // --- Interface Flags & Macros
+/**
+ * \name Flags for Proc_Clone
+ * \{
+ */
+//! Clone the entire process
 #define CLONE_VM       0x10
+//! Don't copy user pages
+#define CLONE_NOUSER   0x20
+/**
+ * \}
+ */
 
 // === Types ===
+/**
+ * \brief Thread root function
+ * 
+ * Function pointer prototype of a thread entrypoint. When it
+ * returns, Threads_Exit is called
+ */
 typedef void (*tThreadFunction)(void*);
 
 // === Kernel Export Macros ===
 /**
- * \name Kernel Function 
+ * \name Kernel exports
  * \{
  */
+//! Kernel symbol definition
 typedef struct sKernelSymbol {
-       char    *Name;
-       Uint    Value;
+       const char      *Name;  //!< Symbolic name
+       tVAddr  Value;  //!< Value of the symbol
 } tKernelSymbol;
-#define        EXPORT(_name)   tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)_name}
-#define        EXPORTV(_name)  tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)&_name}
-#define        EXPORTAS(_sym,_name)    tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)_sym}
+//! Export a pointer symbol (function/array)
+#define        EXPORT(_name)   tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)_name}
+//! Export a variable
+#define        EXPORTV(_name)  tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)&_name}
+//! Export a symbol under another name
+#define        EXPORTAS(_sym,_name)    tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)_sym}
 /**
  * \}
  */
 
 // === FUNCTIONS ===
 // --- IRQs ---
-extern int     IRQ_AddHandler(int Num, void (*Callback)(int));
+/**
+ * \name IRQ hander registration
+ * \{
+ */
+extern int     IRQ_AddHandler(int Num, void (*Callback)(int, void*), void *Ptr);
+extern void    IRQ_RemHandler(int Handle);
+/**
+ * \}
+ */
 
 // --- Logging ---
-extern void    Log_KernelPanic(char *Ident, char *Message, ...);
-extern void    Log_Panic(char *Ident, char *Message, ...);
-extern void    Log_Error(char *Ident, char *Message, ...);
-extern void    Log_Warning(char *Ident, char *Message, ...);
-extern void    Log_Notice(char *Ident, char *Message, ...);
-extern void    Log_Log(char *Ident, char *Message, ...);
-extern void    Log_Debug(char *Ident, char *Message, ...);
+/**
+ * \name Logging to kernel ring buffer
+ * \{
+ */
+extern void    Log_KernelPanic(const char *Ident, const char *Message, ...);
+extern void    Log_Panic(const char *Ident, const char *Message, ...);
+extern void    Log_Error(const char *Ident, const char *Message, ...);
+extern void    Log_Warning(const char *Ident, const char *Message, ...);
+extern void    Log_Notice(const char *Ident, const char *Message, ...);
+extern void    Log_Log(const char *Ident, const char *Message, ...);
+extern void    Log_Debug(const char *Ident, const char *Message, ...);
+/**
+ * \}
+ */
 
 // --- Debug ---
 /**
@@ -115,16 +157,16 @@ extern void       Log_Debug(char *Ident, char *Message, ...);
  * \{
  */
 extern void    Debug_KernelPanic(void);        //!< Initiate a kernel panic
-extern void    Panic(char *Msg, ...);  //!< Print a panic message (initiates a kernel panic)
-extern void    Warning(char *Msg, ...);        //!< Print a warning message
-extern void    LogF(char *Fmt, ...);   //!< Print a log message without a trailing newline
-extern void    Log(char *Fmt, ...);    //!< Print a log message
-extern void    Debug(char *Fmt, ...);  //!< Print a debug message (doesn't go to KTerm)
-extern void    LogV(char *Fmt, va_list Args);  //!< va_list Log message
-extern void    Debug_Enter(char *FuncName, char *ArgTypes, ...);
-extern void    Debug_Log(char *FuncName, char *Fmt, ...);
-extern void    Debug_Leave(char *FuncName, char RetType, ...);
-extern void    Debug_HexDump(char *Header, void *Data, Uint Length);
+extern void    Panic(const char *Msg, ...);    //!< Print a panic message (initiates a kernel panic)
+extern void    Warning(const char *Msg, ...);  //!< Print a warning message
+extern void    LogF(const char *Fmt, ...);     //!< Print a log message without a trailing newline
+extern void    Log(const char *Fmt, ...);      //!< Print a log message
+extern void    Debug(const char *Fmt, ...);    //!< Print a debug message (doesn't go to KTerm)
+extern void    LogV(const char *Fmt, va_list Args);    //!< va_list Log message
+extern void    Debug_Enter(const char *FuncName, const char *ArgTypes, ...);
+extern void    Debug_Log(const char *FuncName, const char *Fmt, ...);
+extern void    Debug_Leave(const char *FuncName, char RetType, ...);
+extern void    Debug_HexDump(const char *Header, const void *Data, Uint Length);
 #define UNIMPLEMENTED()        Warning("'%s' unimplemented", __func__)
 #if DEBUG
 # define ENTER(_types...)      Debug_Enter((char*)__func__, _types)
@@ -149,6 +191,16 @@ extern void        Debug_HexDump(char *Header, void *Data, Uint Length);
  */
 
 // --- IO ---
+#if NO_IO_BUS
+#define inb(a) (Log_Panic("Arch", STR(ARCHDIR)" does not support in*/out* (%s:%i)", __FILE__, __LINE__),0)
+#define inw(a) inb(a)
+#define ind(a) inb(a)
+#define inq(a) inb(a)
+#define outb(a,b)      inb(a)
+#define outw(a,b)      inb(a)
+#define outd(a,b)      inb(a)
+#define outq(a,b)      inb(a)
+#else
 /**
  * \name I/O Memory Access
  * \{
@@ -164,7 +216,7 @@ extern Uint64       inq(Uint16 Port);
 /**
  * \}
  */
-
+#endif
 // --- Memory Management ---
 /**
  * \name Memory Management
@@ -176,7 +228,7 @@ extern Uint64       inq(Uint16 Port);
  * \param VAddr        Virtual Address to allocate at
  * \return Physical address allocated
  */
-extern tPAddr  MM_Allocate(tVAddr VAddr);
+extern tPAddr  MM_Allocate(tVAddr VAddr) __attribute__ ((warn_unused_result));
 /**
  * \brief Deallocate a page
  * \param VAddr        Virtual address to unmap
@@ -242,7 +294,7 @@ extern tVAddr       MM_MapHWPages(tPAddr PAddr, Uint Number);
 extern tVAddr  MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
 /**
  * \brief Unmaps an allocated hardware range
- * \param VAddr        Virtual address allocate by ::MM_MapHWPage or ::MM_AllocDMA
+ * \param VAddr        Virtual address allocate by ::MM_MapHWPages or ::MM_AllocDMA
  * \param Number       Number of pages to free
  */
 extern void    MM_UnmapHWPages(tVAddr VAddr, Uint Number);
@@ -254,6 +306,7 @@ extern tPAddr       MM_AllocPhys(void);
 /**
  * \brief Allocate a contiguous range of physical pages
  * \param Pages        Number of pages to allocate
+ * \param MaxBits      Maximum number of address bits allowed
  * \return First physical address allocated
  */
 extern tPAddr  MM_AllocPhysRange(int Pages, int MaxBits);
@@ -267,6 +320,30 @@ extern void        MM_RefPhys(tPAddr PAddr);
  * \param PAddr        Page to dereference
  */
 extern void    MM_DerefPhys(tPAddr PAddr);
+/**
+ * \brief Get the number of times a page has been referenced
+ * \param PAddr        Address to check
+ * \return Reference count for the page
+ */
+extern int     MM_GetRefCount(tPAddr PAddr);
+/**
+ * \brief Set the node associated with a page
+ * \param PAddr        Physical address of page
+ * \param Node Node pointer (tVFS_Node)
+ * \return Boolean failure
+ * \retval 0   Success
+ * \retval 1   Page not allocated
+ */
+extern int     MM_SetPageNode(tPAddr PAddr, void *Node);
+/**
+ * \brief Get the node associated with a page
+ * \param PAddr        Physical address of page
+ * \param Node Node pointer (tVFS_Node) destination
+ * \return Boolean failure
+ * \retval 0   Success
+ * \retval 1   Page not allocated
+ */
+extern int     MM_GetPageNode(tPAddr PAddr, void **Node);
 /**
  * \}
  */
@@ -277,10 +354,11 @@ extern void       MM_DerefPhys(tPAddr PAddr);
  * \{
  */
 extern int     memcmp(const void *m1, const void *m2, size_t count);
-extern void *memcpy(void *dest, const void *src, size_t count);
-extern void *memcpyd(void *dest, const void *src, size_t count);
-extern void *memset(void *dest, int val, size_t count);
-extern void *memsetd(void *dest, Uint32 val, size_t count);
+extern void    *memcpy(void *dest, const void *src, size_t count);
+extern void    *memcpyd(void *dest, const void *src, size_t count);
+extern void    *memmove(void *dest, const void *src, size_t len);
+extern void    *memset(void *dest, int val, size_t count);
+extern void    *memsetd(void *dest, Uint32 val, size_t count);
 /**
  * \}
  */
@@ -288,8 +366,8 @@ extern void *memsetd(void *dest, Uint32 val, size_t count);
  * \name Memory Validation
  * \{
  */
-extern int     CheckString(char *String);
-extern int     CheckMem(void *Mem, int Num);
+extern int     CheckString(const char *String);
+extern int     CheckMem(const void *Mem, int Num);
 /**
  * \}
  */
@@ -299,10 +377,19 @@ extern int        CheckMem(void *Mem, int Num);
  * \name Endianness Swapping
  * \{
  */
-extern Uint16  LittleEndian16(Uint16 Val);
-extern Uint16  BigEndian16(Uint16 Val);
-extern Uint32  LittleEndian32(Uint32 Val);
-extern Uint32  BigEndian32(Uint32 Val);
+#ifdef __BIG_ENDIAN__
+#define        LittleEndian16(_val)    SwapEndian16(_val)
+#define        LittleEndian32(_val)    SwapEndian32(_val)
+#define        BigEndian16(_val)       (_val)
+#define        BigEndian32(_val)       (_val)
+#else
+#define        LittleEndian16(_val)    (_val)
+#define        LittleEndian32(_val)    (_val)
+#define        BigEndian16(_val)       SwapEndian16(_val)
+#define        BigEndian32(_val)       SwapEndian32(_val)
+#endif
+extern Uint16  SwapEndian16(Uint16 Val);
+extern Uint32  SwapEndian32(Uint32 Val);
 /**
  * \}
  */
@@ -314,51 +401,64 @@ extern Uint32     BigEndian32(Uint32 Val);
  */
 extern int     vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args);
 extern int     sprintf(char *__s, const char *__format, ...);
-extern Uint    strlen(const char *Str);
+extern size_t  strlen(const char *Str);
 extern char    *strcpy(char *__dest, const char *__src);
 extern char    *strncpy(char *__dest, const char *__src, size_t max);
+extern char    *strcat(char *__dest, const char *__src);
+extern char    *strncat(char *__dest, const char *__src, size_t n);
 extern int     strcmp(const char *__str1, const char *__str2);
 extern int     strncmp(const char *Str1, const char *Str2, size_t num);
 extern int     strucmp(const char *Str1, const char *Str2);
-extern char    *strdup(const char *Str);
+// strdup macro is defined in heap.h
+extern char    *_strdup(const char *File, int Line, const char *Str);
 extern char    **str_split(const char *__str, char __ch);
+extern char    *strchr(const char *__s, int __c);
 extern int     strpos(const char *Str, char Ch);
 extern int     strpos8(const char *str, Uint32 search);
-extern void    itoa(char *buf, Uint num, int base, int minLength, char pad);
+extern void    itoa(char *buf, Uint64 num, int base, int minLength, char pad);
 extern int     atoi(const char *string);
-extern int     ReadUTF8(Uint8 *str, Uint32 *Val);
+extern int     ParseInt(const char *string, int *Val);
+extern int     ReadUTF8(const Uint8 *str, Uint32 *Val);
 extern int     WriteUTF8(Uint8 *str, Uint32 Val);
-extern int     ModUtil_SetIdent(char *Dest, char *Value);
-extern int     ModUtil_LookupString(char **Array, char *Needle);
-extern Uint8   ByteSum(void *Ptr, int Size);
+extern int     ModUtil_SetIdent(char *Dest, const char *Value);
+extern int     ModUtil_LookupString(const char **Array, const char *Needle);
+
+extern Uint8   ByteSum(const void *Ptr, int Size);
+extern int     Hex(char *Dest, size_t Size, const Uint8 *SourceData);
+extern int     UnHex(Uint8 *Dest, size_t DestSize, const char *SourceString);
 /**
  * \}
  */
 
-extern Uint    rand(void);
+/**
+ * \brief Get a random number
+ * \return Random number
+ * \note Current implementation is a linear congruency
+ */
+extern int     rand(void);
+/**
+ * \brief Call a function with a variable number of arguments
+ * \param Function     Function address
+ * \param NArgs        Number of entries in \a Args
+ * \param Args Array of arguments
+ * \return Integer from called Function
+ */
 extern int     CallWithArgArray(void *Function, int NArgs, Uint *Args);
 
 // --- Heap ---
+#include <heap.h>
 /**
- * \name Heap
- * \{
- */
-extern void *malloc(size_t size);
-extern void *calloc(size_t num, size_t size);
-extern void    *realloc(void *ptr, size_t size);
-extern void free(void *Ptr);
-extern int     IsHeap(void *Ptr);
-/**
- * \}
+ * \brief Magic heap allocation function
  */
+extern void    *alloca(size_t Size);
 
 // --- Modules ---
 /**
  * \name Modules
  * \{
  */
-extern int     Module_LoadMem(void *Buffer, Uint Length, char *ArgStr);
-extern int     Module_LoadFile(char *Path, char *ArgStr);
+extern int     Module_LoadMem(void *Buffer, Uint Length, const char *ArgStr);
+extern int     Module_LoadFile(const char *Path, const char *ArgStr);
 /**
  * \}
  */
@@ -391,6 +491,9 @@ extern int  Time_CreateTimer(int Delta, tTimerCallback *Callback, void *Argument)
  * \brief Removed an active timer
  */
 extern void    Time_RemoveTimer(int ID);
+/**
+ * \brief Wait for a period of milliseconds
+ */
 extern void    Time_Delay(int Delay);
 /**
  * \}
@@ -401,8 +504,10 @@ extern void        Time_Delay(int Delay);
  * \name Threads and Processes
  * \{
  */
-extern int     Proc_SpawnWorker(void);
-extern int     Proc_Spawn(char *Path);
+extern int     Proc_SpawnWorker(void (*Fcn)(void*), void *Data);
+extern int     Proc_Spawn(const char *Path);
+extern int     Proc_SysSpawn(const char *Binary, const char **ArgV, const char **EnvP, int nFD, int *FDs);
+extern int     Proc_Execve(const char *File, const char **ArgV, const char **EnvP, int DataSize);
 extern void    Threads_Exit(int TID, int Status);
 extern void    Threads_Yield(void);
 extern void    Threads_Sleep(void);
@@ -412,17 +517,23 @@ extern tTID       Threads_GetTID(void);
 extern tUID    Threads_GetUID(void);
 extern tGID    Threads_GetGID(void);
 extern int     SpawnTask(tThreadFunction Function, void *Arg);
-extern Uint    *Threads_GetCfgPtr(int Id);
-extern int     Threads_SetName(char *NewName);
+extern int     *Threads_GetErrno(void);
+extern int     Threads_SetName(const char *NewName);
 /**
  * \}
  */
 
 // --- Simple Math ---
+//! Divide and round up
 extern int     DivUp(int num, int dem);
+//! Divide and Modulo 64-bit unsigned integer
+extern Uint64  DivMod64U(Uint64 Num, Uint64 Den, Uint64 *Rem);
+
+static inline int MIN(int a, int b) { return a < b ? a : b; }
+static inline int MAX(int a, int b) { return a > b ? a : b; }
 
 #include <binary_ext.h>
 #include <vfs_ext.h>
-#include <adt.h>
+#include <mutex.h>
 
 #endif

UCC git Repository :: git.ucc.asn.au