Kernel - Standardised kernel build tag (placed in gsBuildInfo)
[tpg/acess2.git] / KernelLand / Kernel / include / acess.h
1 /*
2  * AcessOS Microkernel Version
3  * acess.h
4  */
5 #ifndef _ACESS_H
6 #define _ACESS_H
7 /**
8  * \file acess.h
9  * \brief Acess2 Kernel API Core
10  */
11
12 //! NULL Pointer
13 #define NULL    ((void*)0)
14 //! Pack a structure
15 #define PACKED  __attribute__((packed))
16 //! Mark a function as not returning
17 #define NORETURN        __attribute__((noreturn))
18 //! Mark a function (or variable) as deprecated
19 #define DEPRECATED      __attribute__((deprecated))
20 //! Mark a parameter as unused
21 #define UNUSED(x)       UNUSED_##x __attribute__((unused))
22 //! Get the offset of a member in a structure
23 #define offsetof(st, m) ((Uint)((char *)&((st *)(0))->m - (char *)0 ))
24
25 /**
26  * \name Boolean constants
27  * \{
28  */
29 #define TRUE    1
30 #define FALSE   0
31 /**
32  * \}
33  */
34
35 #include <arch.h>
36 #include <stdarg.h>
37 #include "errno.h"
38
39 // --- Types ---
40 typedef Uint32  tPID;   //!< Process ID type
41 typedef Uint32  tPGID;  //!< Process Group ID type
42 typedef Uint32  tTID;   //!< Thread ID Type
43 typedef Uint32  tUID;   //!< User ID Type
44 typedef Uint32  tGID;   //!< Group ID Type
45 typedef Sint64  tTimestamp;     //!< Timestamp (miliseconds since 00:00 1 Jan 1970)
46 typedef Sint64  tTime;  //!< Same again
47 typedef struct sShortSpinlock   tShortSpinlock; //!< Opaque (kinda) spinlock
48 typedef int     bool;   //!< Boolean type
49 typedef Uint64  off_t;  //!< VFS Offset
50
51 // --- Helper Macros ---
52 /**
53  * \name Helper Macros
54  * \{
55  */
56 #define CONCAT(x,y) x ## y
57 #define EXPAND_CONCAT(x,y) CONCAT(x,y)
58 #define STR(x) #x
59 #define EXPAND_STR(x) STR(x)
60
61 extern char     __buildnum[];
62 #define BUILD_NUM       ((int)(Uint)&__buildnum)
63 extern const char       gsGitHash[];
64 extern const char       gsBuildInfo[];
65
66 #define VER2(major,minor)       ((((major)&0xFF)<<8)|((minor)&0xFF))
67 /**
68  * \}
69  */
70
71 //! \brief Error number
72 #define errno   (*Threads_GetErrno())
73
74 // === CONSTANTS ===
75 // --- Memory Flags --
76 /**
77  * \name Memory Flags
78  * \{
79  * \todo Move to mm_virt.h
80  */
81 #define MM_PFLAG_RO             0x01    // Writes disallowed
82 #define MM_PFLAG_EXEC   0x02    // Allow execution
83 #define MM_PFLAG_NOPAGE 0x04    // Prevent from being paged out
84 #define MM_PFLAG_COW    0x08    // Copy-On-Write
85 #define MM_PFLAG_KERNEL 0x10    // Kernel-Only (Ring0)
86 /**
87  * \}
88  */
89 // --- Interface Flags & Macros
90 /**
91  * \name Flags for Proc_Clone
92  * \{
93  */
94 //! Clone the entire process
95 #define CLONE_VM        0x10
96 //! Don't copy user pages
97 #define CLONE_NOUSER    0x20
98 //! Inherit the parent's PGID
99 #define CLONE_PGID      0x40
100 /**
101  * \}
102  */
103
104 // === Types ===
105 /**
106  * \brief Thread root function
107  * 
108  * Function pointer prototype of a thread entrypoint. When it
109  * returns, Threads_Exit is called
110  */
111 typedef void (*tThreadFunction)(void*);
112
113 // === Kernel Export Macros ===
114 /**
115  * \name Kernel exports
116  * \{
117  */
118 //! Kernel symbol definition
119 typedef struct sKernelSymbol {
120         const char      *Name;  //!< Symbolic name
121         tVAddr  Value;  //!< Value of the symbol
122 } tKernelSymbol;
123 //! Export a pointer symbol (function/array)
124 #define EXPORT(_name)   tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)_name}
125 //! Export a variable
126 #define EXPORTV(_name)  tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)&_name}
127 //! Export a symbol under another name
128 #define EXPORTAS(_sym,_name)    tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)_sym}
129 /**
130  * \}
131  */
132
133 // === FUNCTIONS ===
134 // --- IRQs ---
135 /**
136  * \name IRQ hander registration
137  * \{
138  */
139 extern int      IRQ_AddHandler(int Num, void (*Callback)(int, void*), void *Ptr);
140 extern void     IRQ_RemHandler(int Handle);
141 /**
142  * \}
143  */
144
145 // --- Logging ---
146 /**
147  * \name Logging to kernel ring buffer
148  * \{
149  */
150 extern void     Log_KernelPanic(const char *Ident, const char *Message, ...);
151 extern void     Log_Panic(const char *Ident, const char *Message, ...);
152 extern void     Log_Error(const char *Ident, const char *Message, ...);
153 extern void     Log_Warning(const char *Ident, const char *Message, ...);
154 extern void     Log_Notice(const char *Ident, const char *Message, ...);
155 extern void     Log_Log(const char *Ident, const char *Message, ...);
156 extern void     Log_Debug(const char *Ident, const char *Message, ...);
157 /**
158  * \}
159  */
160
161 // --- Debug ---
162 /**
163  * \name Debugging and Errors
164  * \{
165  */
166 extern void     Debug_KernelPanic(void);        //!< Initiate a kernel panic
167 extern void     Panic(const char *Msg, ...);    //!< Print a panic message (initiates a kernel panic)
168 extern void     Warning(const char *Msg, ...);  //!< Print a warning message
169 extern void     LogF(const char *Fmt, ...);     //!< Print a log message without a trailing newline
170 extern void     Log(const char *Fmt, ...);      //!< Print a log message
171 extern void     Debug(const char *Fmt, ...);    //!< Print a debug message (doesn't go to KTerm)
172 extern void     LogV(const char *Fmt, va_list Args);    //!< va_list Log message
173 extern void     Debug_Enter(const char *FuncName, const char *ArgTypes, ...);
174 extern void     Debug_Log(const char *FuncName, const char *Fmt, ...);
175 extern void     Debug_Leave(const char *FuncName, char RetType, ...);
176 extern void     Debug_HexDump(const char *Header, const void *Data, Uint Length);
177 #define UNIMPLEMENTED() Warning("'%s' unimplemented", __func__)
178 #if DEBUG
179 # define ENTER(_types...)       Debug_Enter((char*)__func__, _types)
180 # define LOG(_fmt...)   Debug_Log((char*)__func__, _fmt)
181 # define LEAVE(_t...)   Debug_Leave((char*)__func__, _t)
182 # define LEAVE_RET(_t,_v...)    do{LEAVE(_t,_v);return _v;}while(0)
183 # define LEAVE_RET0()   do{LEAVE('-');return;}while(0)
184 #else
185 # define ENTER(...)
186 # define LOG(...)
187 # define LEAVE(...)
188 # define LEAVE_RET(_t,_v...)    return (_v)
189 # define LEAVE_RET0()   return
190 #endif
191 #if SANITY
192 # define ASSERT(expr) do{if(!(expr))Panic("%s:%i - %s: Assertion '"#expr"' failed",__FILE__,__LINE__,(char*)__func__);}while(0)
193 #else
194 # define ASSERT(expr)
195 #endif
196 /**
197  * \}
198  */
199
200 // --- IO ---
201 #if NO_IO_BUS
202 #define inb(a)  (Log_Panic("Arch", STR(ARCHDIR)" does not support in*/out* (%s:%i)", __FILE__, __LINE__),0)
203 #define inw(a)  inb(a)
204 #define ind(a)  inb(a)
205 #define inq(a)  inb(a)
206 #define outb(a,b)       inb(a)
207 #define outw(a,b)       inb(a)
208 #define outd(a,b)       inb(a)
209 #define outq(a,b)       inb(a)
210 #else
211 /**
212  * \name I/O Memory Access
213  * \{
214  */
215 extern void     outb(Uint16 Port, Uint8 Data);
216 extern void     outw(Uint16 Port, Uint16 Data);
217 extern void     outd(Uint16 Port, Uint32 Data);
218 extern void     outq(Uint16 Port, Uint64 Data);
219 extern Uint8    inb(Uint16 Port);
220 extern Uint16   inw(Uint16 Port);
221 extern Uint32   ind(Uint16 Port);
222 extern Uint64   inq(Uint16 Port);
223 /**
224  * \}
225  */
226 #endif
227 // --- Memory Management ---
228 /**
229  * \name Memory Management
230  * \{
231  * \todo Move to mm_virt.h
232  */
233 /**
234  * \brief Allocate a physical page at \a VAddr
235  * \param VAddr Virtual Address to allocate at
236  * \return Physical address allocated
237  */
238 extern tPAddr   MM_Allocate(tVAddr VAddr) __attribute__ ((warn_unused_result));
239 /**
240  * \brief Deallocate a page
241  * \param VAddr Virtual address to unmap
242  */
243 extern void     MM_Deallocate(tVAddr VAddr);
244 /**
245  * \brief Map a physical page at \a PAddr to \a VAddr
246  * \param VAddr Target virtual address
247  * \param PAddr Physical address to map
248  * \return Boolean Success
249  */
250 extern int      MM_Map(tVAddr VAddr, tPAddr PAddr);
251 /**
252  * \brief Get the physical address of \a Addr
253  * \param Addr  Address of the page to get the physical address of
254  * \return Physical page mapped at \a Addr
255  */
256 extern tPAddr   MM_GetPhysAddr(const void *Addr);
257 /**
258  * \brief Set the access flags on a page
259  * \param VAddr Virtual address of the page
260  * \param Flags New flags value
261  * \param Mask  Flags to set
262  */
263 extern void     MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
264 /**
265  * \brief Get the flags on a flag
266  * \param VAddr Virtual address of page
267  * \return Flags value of the page
268  */
269 extern Uint     MM_GetFlags(tVAddr VAddr);
270 /**
271  * \brief Checks is a memory range is user accessable
272  * \param VAddr Base address to check
273  * \return 1 if the memory is all user-accessable, 0 otherwise
274  */
275 #define MM_IsUser(VAddr)        (!(MM_GetFlags((VAddr))&MM_PFLAG_KERNEL))
276 /**
277  * \brief Temporarily map a page into the address space
278  * \param PAddr Physical addres to map
279  * \return Virtual address of page in memory
280  * \note There is only a limited ammount of slots avaliable
281  */
282 extern void     *MM_MapTemp(tPAddr PAddr);
283 /**
284  * \brief Free a temporarily mapped page
285  * \param VAddr Allocate virtual addres of page
286  */
287 extern void     MM_FreeTemp(void *Ptr);
288 /**
289  * \brief Map a physcal address range into the virtual address space
290  * \param PAddr Physical address to map in
291  * \param Number        Number of pages to map
292  */
293 extern tVAddr   MM_MapHWPages(tPAddr PAddr, Uint Number);
294 /**
295  * \brief Allocates DMA physical memory
296  * \param Pages Number of pages required
297  * \param MaxBits       Maximum number of bits the physical address can have
298  * \param PhysAddr      Pointer to the location to place the physical address allocated
299  * \return Virtual address allocate
300  */
301 extern tVAddr   MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
302 /**
303  * \brief Unmaps an allocated hardware range
304  * \param VAddr Virtual address allocate by ::MM_MapHWPages or ::MM_AllocDMA
305  * \param Number        Number of pages to free
306  */
307 extern void     MM_UnmapHWPages(tVAddr VAddr, Uint Number);
308 /**
309  * \brief Allocate a single physical page
310  * \return Physical address allocated
311  */
312 extern tPAddr   MM_AllocPhys(void);
313 /**
314  * \brief Allocate a contiguous range of physical pages
315  * \param Pages Number of pages to allocate
316  * \param MaxBits       Maximum number of address bits allowed
317  * \return First physical address allocated
318  */
319 extern tPAddr   MM_AllocPhysRange(int Pages, int MaxBits);
320 /**
321  * \brief Reference a physical page
322  * \param PAddr Page to mark as referenced
323  */
324 extern void     MM_RefPhys(tPAddr PAddr);
325 /**
326  * \brief Dereference a physical page
327  * \param PAddr Page to dereference
328  */
329 extern void     MM_DerefPhys(tPAddr PAddr);
330 /**
331  * \brief Get the number of times a page has been referenced
332  * \param PAddr Address to check
333  * \return Reference count for the page
334  */
335 extern int      MM_GetRefCount(tPAddr PAddr);
336 /**
337  * \brief Set the node associated with a page
338  * \param PAddr Physical address of page
339  * \param Node  Node pointer (tVFS_Node)
340  * \return Boolean failure
341  * \retval 0    Success
342  * \retval 1    Page not allocated
343  */
344 extern int      MM_SetPageNode(tPAddr PAddr, void *Node);
345 /**
346  * \brief Get the node associated with a page
347  * \param PAddr Physical address of page
348  * \param Node  Node pointer (tVFS_Node) destination
349  * \return Boolean failure
350  * \retval 0    Success
351  * \retval 1    Page not allocated
352  */
353 extern int      MM_GetPageNode(tPAddr PAddr, void **Node);
354 /**
355  * \}
356  */
357
358 // --- Memory Manipulation ---
359 /**
360  * \name Memory Manipulation
361  * \{
362  */
363 extern int      memcmp(const void *m1, const void *m2, size_t count);
364 extern void     *memcpy(void *dest, const void *src, size_t count);
365 extern void     *memcpyd(void *dest, const void *src, size_t count);
366 extern void     *memmove(void *dest, const void *src, size_t len);
367 extern void     *memset(void *dest, int val, size_t count);
368 extern void     *memsetd(void *dest, Uint32 val, size_t count);
369 /**
370  * \}
371  */
372 /**
373  * \name Memory Validation
374  * \{
375  */
376 extern int      CheckString(const char *String);
377 extern int      CheckMem(const void *Mem, int Num);
378 /**
379  * \}
380  */
381
382 // --- Endianness ---
383 /**
384  * \name Endianness Swapping
385  * \{
386  */
387 #ifdef __BIG_ENDIAN__
388 #define LittleEndian16(_val)    SwapEndian16(_val)
389 #define LittleEndian32(_val)    SwapEndian32(_val)
390 #define LittleEndian64(_val)    SwapEndian32(_val)
391 #define BigEndian16(_val)       (_val)
392 #define BigEndian32(_val)       (_val)
393 #define BigEndian64(_val)       (_val)
394 #else
395 #define LittleEndian16(_val)    (_val)
396 #define LittleEndian32(_val)    (_val)
397 #define LittleEndian64(_val)    (_val)
398 #define BigEndian16(_val)       SwapEndian16(_val)
399 #define BigEndian32(_val)       SwapEndian32(_val)
400 #define BigEndian64(_val)       SwapEndian64(_val)
401 #endif
402 extern Uint16   SwapEndian16(Uint16 Val);
403 extern Uint32   SwapEndian32(Uint32 Val);
404 extern Uint64   SwapEndian64(Uint64 Val);
405 /**
406  * \}
407  */
408
409 // --- Strings ---
410 /**
411  * \name Strings
412  * \{
413  */
414 extern int      vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args);
415 extern int      snprintf(char *__s, size_t __n, const char *__format, ...);
416 extern int      sprintf(char *__s, const char *__format, ...);
417 extern size_t   strlen(const char *Str);
418 extern char     *strcpy(char *__dest, const char *__src);
419 extern char     *strncpy(char *__dest, const char *__src, size_t max);
420 extern char     *strcat(char *__dest, const char *__src);
421 extern char     *strncat(char *__dest, const char *__src, size_t n);
422 extern int      strcmp(const char *__str1, const char *__str2);
423 extern int      strncmp(const char *Str1, const char *Str2, size_t num);
424 extern int      strucmp(const char *Str1, const char *Str2);
425 // strdup macro is defined in heap.h
426 extern char     *_strdup(const char *File, int Line, const char *Str);
427 extern char     **str_split(const char *__str, char __ch);
428 extern char     *strchr(const char *__s, int __c);
429 extern int      strpos(const char *Str, char Ch);
430 extern int      strpos8(const char *str, Uint32 search);
431 extern void     itoa(char *buf, Uint64 num, int base, int minLength, char pad);
432 extern int      atoi(const char *string);
433 extern int      ParseInt(const char *string, int *Val);
434 extern int      ReadUTF8(const Uint8 *str, Uint32 *Val);
435 extern int      WriteUTF8(Uint8 *str, Uint32 Val);
436 extern int      ModUtil_SetIdent(char *Dest, const char *Value);
437 extern int      ModUtil_LookupString(const char **Array, const char *Needle);
438
439 extern Uint8    ByteSum(const void *Ptr, int Size);
440 extern int      Hex(char *Dest, size_t Size, const Uint8 *SourceData);
441 extern int      UnHex(Uint8 *Dest, size_t DestSize, const char *SourceString);
442 /**
443  * \}
444  */
445
446 #include <ctype.h>
447
448 /**
449  * \brief Get a random number
450  * \return Random number
451  * \note Current implementation is a linear congruency
452  */
453 extern int      rand(void);
454 /**
455  * \brief Call a function with a variable number of arguments
456  * \param Function      Function address
457  * \param NArgs Number of entries in \a Args
458  * \param Args  Array of arguments
459  * \return Integer from called Function
460  */
461 extern int      CallWithArgArray(void *Function, int NArgs, Uint *Args);
462
463 // --- Heap ---
464 #include <heap.h>
465 /**
466  * \brief Magic heap allocation function
467  */
468 extern void     *alloca(size_t Size);
469
470 // --- Modules ---
471 /**
472  * \name Modules
473  * \{
474  */
475 extern int      Module_LoadMem(void *Buffer, Uint Length, const char *ArgStr);
476 extern int      Module_LoadFile(const char *Path, const char *ArgStr);
477 /**
478  * \}
479  */
480
481 // --- Timing ---
482 /**
483  * \name Time and Timing
484  * \{
485  */
486 /**
487  * \brief Create a timestamp from a time
488  * \note Days/Months are zero-based (e.g. December is 11, and has a day range of 0-30)
489  */
490 extern tTime    timestamp(int sec, int mins, int hrs, int day, int month, int year);
491 /**
492  * \brief Extract the date/time from a timestamp
493  * \note Days/Months are zero-based (e.g. December is 11, and has a day range of 0-30)
494  */
495 extern void     format_date(tTime TS, int *year, int *month, int *day, int *hrs, int *mins, int *sec, int *ms);
496 /**
497  * \brief Gets the current timestamp (miliseconds since Midnight 1st January 1970)
498  */
499 extern Sint64   now(void);
500 /**
501  * \}
502  */
503
504 // --- Threads ---
505 /**
506  * \name Threads and Processes
507  * \{
508  */
509 extern int      Proc_SpawnWorker(void (*Fcn)(void*), void *Data);
510 extern int      Proc_Spawn(const char *Path);
511 extern int      Proc_SysSpawn(const char *Binary, const char **ArgV, const char **EnvP, int nFD, int *FDs);
512 extern int      Proc_Execve(const char *File, const char **ArgV, const char **EnvP, int DataSize);
513 extern void     Threads_Exit(int TID, int Status);
514 extern void     Threads_Yield(void);
515 extern void     Threads_Sleep(void);
516 extern int      Threads_WakeTID(tTID Thread);
517 extern tPGID    Threads_GetPGID(void);
518 extern tPID     Threads_GetPID(void);
519 extern tTID     Threads_GetTID(void);
520 extern tUID     Threads_GetUID(void);
521 extern tGID     Threads_GetGID(void);
522 extern int      SpawnTask(tThreadFunction Function, void *Arg);
523 extern int      *Threads_GetErrno(void);
524 extern int      Threads_SetName(const char *NewName);
525 /**
526  * \}
527  */
528
529 // --- Simple Math ---
530 //! Divide and round up
531 extern int      DivUp(int num, int dem);
532 //! Divide and Modulo 64-bit unsigned integer
533 extern Uint64   DivMod64U(Uint64 Num, Uint64 Den, Uint64 *Rem);
534
535 static inline int MIN(int a, int b) { return a < b ? a : b; }
536 static inline int MAX(int a, int b) { return a > b ? a : b; }
537
538 #include <binary_ext.h>
539 #include <vfs_ext.h>
540 #include <mutex.h>
541
542 #endif

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