Kernel - Expose thread timer for EVENT_TIMER, multiple sleepers for workqueue, cleanu...
[tpg/acess2.git] / KernelLand / Kernel / include / acess.h
index 2cd2cff..fe1a0f9 100644 (file)
@@ -9,16 +9,26 @@
  * \brief Acess2 Kernel API Core
  */
 
-//! NULL Pointer
-#define NULL   ((void*)0)
+#include <stddef.h>
+#include <stdbool.h>
+#include <arch.h>
+
+#ifndef HALT_CPU
+# define HALT_CPU()    for(;;);
+#endif
+
 //! Pack a structure
 #define PACKED __attribute__((packed))
 //! Mark a function as not returning
 #define NORETURN       __attribute__((noreturn))
+//! Mark a function that its return value should be used
+#define WARN_UNUSED_RET        __attribute__((warn_unused_result))
+//! Mark a function (or variable) as deprecated
+#define DEPRECATED     __attribute__((deprecated))
 //! 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 ))
+//! 
+#define ALIGN(x)       __attribute__((aligned(x)))
 
 /**
  * \name Boolean constants
  * \}
  */
 
-#include <arch.h>
 #include <stdarg.h>
 #include "errno.h"
 
 // --- Types ---
 typedef Uint32 tPID;   //!< Process ID type
+typedef Uint32 tPGID;  //!< Process Group 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
+typedef Uint64 off_t;  //!< VFS Offset
+typedef struct { char _[PAGE_SIZE];}   tPage;  // Representation of a page for pointer arithmatic
 
 // --- Helper Macros ---
 /**
@@ -56,7 +67,8 @@ typedef int   bool;   //!< Boolean type
 
 extern char    __buildnum[];
 #define BUILD_NUM      ((int)(Uint)&__buildnum)
-extern const char gsGitHash[];
+extern const char      gsGitHash[];
+extern const char      gsBuildInfo[];
 
 #define VER2(major,minor)      ((((major)&0xFF)<<8)|((minor)&0xFF))
 /**
@@ -90,6 +102,8 @@ extern const char gsGitHash[];
 #define CLONE_VM       0x10
 //! Don't copy user pages
 #define CLONE_NOUSER   0x20
+//! Inherit the parent's PGID
+#define CLONE_PGID     0x40
 /**
  * \}
  */
@@ -135,71 +149,18 @@ extern void       IRQ_RemHandler(int Handle);
  * \}
  */
 
-// --- Logging ---
-/**
- * \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 ---
-/**
- * \name Debugging and Errors
- * \{
- */
-extern void    Debug_KernelPanic(void);        //!< Initiate a kernel panic
-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)
-# define LOG(_fmt...)  Debug_Log((char*)__func__, _fmt)
-# define LEAVE(_t...)  Debug_Leave((char*)__func__, _t)
-# define LEAVE_RET(_t,_v...)   do{LEAVE(_t,_v);return _v;}while(0)
-# define LEAVE_RET0()  do{LEAVE('-');return;}while(0)
-#else
-# define ENTER(...)
-# define LOG(...)
-# define LEAVE(...)
-# define LEAVE_RET(_t,_v...)   return (_v)
-# define LEAVE_RET0()  return
-#endif
-#if SANITY
-# define ASSERT(expr) do{if(!(expr))Panic("%s: Assertion '"#expr"' failed",(char*)__func__);}while(0)
-#else
-# define ASSERT(expr)
-#endif
-/**
- * \}
- */
+#include "logdebug.h"
 
 // --- IO ---
 #if NO_IO_BUS
-#define inb(a) (Log_Panic("Arch", STR(ARCHDIR)" does not support in*/out* (%s:%i)", __FILE__, __LINE__),0)
+#define inb(a) (Log_Panic("Arch", STR(ARCHDIR)" does not support in* (%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)
+#define outb(a,b)      (Log_Panic("Arch", STR(ARCHDIR)" does not support out* (%s:%i)", __FILE__, __LINE__),(void)(b))
+#define outw(a,b)      outb(a,b)
+#define outd(a,b)      outb(a,b)
+#define outq(a,b)      outb(a,b)
 #else
 /**
  * \name I/O Memory Access
@@ -228,62 +189,68 @@ extern Uint64     inq(Uint16 Port);
  * \param VAddr        Virtual Address to allocate at
  * \return Physical address allocated
  */
-extern tPAddr  MM_Allocate(tVAddr VAddr) __attribute__ ((warn_unused_result));
+extern tPAddr  MM_Allocate(volatile void *VAddr) __attribute__ ((warn_unused_result));
+/**
+ * \breif Allocate a zeroed COW page to \a VAddr
+ * \param VAddr        Virtual address to allocate at
+ * \return Physical address allocated (don't cache)
+ */
+extern void    MM_AllocateZero(volatile void *VAddr);
 /**
  * \brief Deallocate a page
  * \param VAddr        Virtual address to unmap
  */
-extern void    MM_Deallocate(tVAddr VAddr);
+extern void    MM_Deallocate(volatile void *VAddr);
 /**
  * \brief Map a physical page at \a PAddr to \a VAddr
  * \param VAddr        Target virtual address
  * \param PAddr        Physical address to map
  * \return Boolean Success
  */
-extern int     MM_Map(tVAddr VAddr, tPAddr PAddr);
+extern int     MM_Map(volatile void * VAddr, tPAddr PAddr);
 /**
  * \brief Get the physical address of \a Addr
  * \param Addr Address of the page to get the physical address of
  * \return Physical page mapped at \a Addr
  */
-extern tPAddr  MM_GetPhysAddr(tVAddr Addr);
+extern tPAddr  MM_GetPhysAddr(volatile const void *Addr);
 /**
  * \brief Set the access flags on a page
  * \param VAddr        Virtual address of the page
  * \param Flags New flags value
  * \param Mask Flags to set
  */
-extern void    MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
+extern void    MM_SetFlags(volatile void *VAddr, Uint Flags, Uint Mask);
 /**
  * \brief Get the flags on a flag
  * \param VAddr        Virtual address of page
  * \return Flags value of the page
  */
-extern Uint    MM_GetFlags(tVAddr VAddr);
+extern Uint    MM_GetFlags(volatile const void *VAddr);
 /**
  * \brief Checks is a memory range is user accessable
  * \param VAddr        Base address to check
  * \return 1 if the memory is all user-accessable, 0 otherwise
  */
-#define MM_IsUser(VAddr)       (!(MM_GetFlags((VAddr))&MM_PFLAG_KERNEL))
+#define MM_IsUser(VAddr)       (!(MM_GetFlags((const void*)(VAddr))&MM_PFLAG_KERNEL))
 /**
  * \brief Temporarily map a page into the address space
  * \param PAddr        Physical addres to map
  * \return Virtual address of page in memory
  * \note There is only a limited ammount of slots avaliable
  */
-extern tVAddr  MM_MapTemp(tPAddr PAddr);
+extern void    *MM_MapTemp(tPAddr PAddr);
 /**
  * \brief Free a temporarily mapped page
- * \param VAddr        Allocate virtual addres of page
+ * \param Ptr  Pointer to page base
  */
-extern void    MM_FreeTemp(tVAddr VAddr);
+extern void    MM_FreeTemp(void *Ptr);
 /**
  * \brief Map a physcal address range into the virtual address space
  * \param PAddr        Physical address to map in
  * \param Number       Number of pages to map
  */
-extern tVAddr  MM_MapHWPages(tPAddr PAddr, Uint Number);
+extern void    *MM_MapHWPages(tPAddr PAddr, Uint Number);
 /**
  * \brief Allocates DMA physical memory
  * \param Pages        Number of pages required
@@ -291,13 +258,13 @@ extern tVAddr     MM_MapHWPages(tPAddr PAddr, Uint Number);
  * \param PhysAddr     Pointer to the location to place the physical address allocated
  * \return Virtual address allocate
  */
-extern tVAddr  MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
+extern void    *MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
 /**
  * \brief Unmaps an allocated hardware range
  * \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);
+extern void    MM_UnmapHWPages(volatile void *VAddr, Uint Number);
 /**
  * \brief Allocate a single physical page
  * \return Physical address allocated
@@ -380,55 +347,29 @@ extern int        CheckMem(const void *Mem, int Num);
 #ifdef __BIG_ENDIAN__
 #define        LittleEndian16(_val)    SwapEndian16(_val)
 #define        LittleEndian32(_val)    SwapEndian32(_val)
+#define        LittleEndian64(_val)    SwapEndian32(_val)
 #define        BigEndian16(_val)       (_val)
 #define        BigEndian32(_val)       (_val)
+#define        BigEndian64(_val)       (_val)
 #else
 #define        LittleEndian16(_val)    (_val)
 #define        LittleEndian32(_val)    (_val)
+#define        LittleEndian64(_val)    (_val)
 #define        BigEndian16(_val)       SwapEndian16(_val)
 #define        BigEndian32(_val)       SwapEndian32(_val)
+#define        BigEndian64(_val)       SwapEndian64(_val)
 #endif
 extern Uint16  SwapEndian16(Uint16 Val);
 extern Uint32  SwapEndian32(Uint32 Val);
+extern Uint64  SwapEndian64(Uint64 Val);
 /**
  * \}
  */
 
 // --- Strings ---
-/**
- * \name Strings
- * \{
- */
-extern int     vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args);
-extern int     sprintf(char *__s, const char *__format, ...);
-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);
-// 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, Uint64 num, int base, int minLength, char pad);
-extern int     atoi(const char *string);
-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, const char *Value);
-extern int     ModUtil_LookupString(const char **Array, const char *Needle);
+#include <acess_string.h>
 
-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);
-/**
- * \}
- */
+#include <ctype.h>
 
 /**
  * \brief Get a random number
@@ -470,10 +411,12 @@ extern int        Module_LoadFile(const char *Path, const char *ArgStr);
  */
 /**
  * \brief Create a timestamp from a time
+ * \note Days/Months are zero-based (e.g. December is 11, and has a day range of 0-30)
  */
 extern tTime   timestamp(int sec, int mins, int hrs, int day, int month, int year);
 /**
  * \brief Extract the date/time from a timestamp
+ * \note Days/Months are zero-based (e.g. December is 11, and has a day range of 0-30)
  */
 extern void    format_date(tTime TS, int *year, int *month, int *day, int *hrs, int *mins, int *sec, int *ms);
 /**
@@ -489,7 +432,7 @@ extern Sint64       now(void);
  * \name Threads and Processes
  * \{
  */
-extern int     Proc_SpawnWorker(void (*Fcn)(void*), void *Data);
+extern struct sThread  *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);
@@ -497,6 +440,7 @@ extern void Threads_Exit(int TID, int Status);
 extern void    Threads_Yield(void);
 extern void    Threads_Sleep(void);
 extern int     Threads_WakeTID(tTID Thread);
+extern tPGID   Threads_GetPGID(void);
 extern tPID    Threads_GetPID(void);
 extern tTID    Threads_GetTID(void);
 extern tUID    Threads_GetUID(void);

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