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

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