Kernel - Removed #include <adt.h> from acess.h, reduce global deps
[tpg/acess2.git] / Kernel / include / acess.h
1 /*
2  * AcessOS Microkernel Version
3  * acess.h
4  */
5 #ifndef _ACESS_H
6 #define _ACESS_H
7
8 #define NULL    ((void*)0)
9 #define PACKED  __attribute__((packed))
10 #define UNUSED(x)       UNUSED_##x __attribute__((unused))
11 #define offsetof(st, m) ((Uint)((char *)&((st *)(0))->m - (char *)0 ))
12
13 #include <arch.h>
14 #include <stdarg.h>
15 #include "errno.h"
16
17 // --- Types ---
18 typedef  int    tPID;
19 typedef  int    tTID;
20 typedef Uint    tUID;
21 typedef Uint    tGID;
22 typedef Sint64  tTimestamp;
23 typedef Sint64  tTime;
24 typedef struct sShortSpinlock   tShortSpinlock;
25
26 // --- Helper Macros ---
27 /**
28  * \name Helper Macros
29  * \{
30  */
31 #define CONCAT(x,y) x ## y
32 #define EXPAND_CONCAT(x,y) CONCAT(x,y)
33 #define STR(x) #x
34 #define EXPAND_STR(x) STR(x)
35
36 #define VER2(major,minor)       ((((major)&0xFF)<<8)|((minor)&0xFF))
37 /**
38  * \}
39  */
40
41 /**
42  * \name Per-Thread Configuration Settings
43  * \{
44  */
45 enum eConfigTypes {
46         CFGT_NULL,
47         CFGT_INT,
48         CFGT_HEAPSTR,
49         CFGT_PTR
50 };
51 enum eConfigs {
52         CFG_VFS_CWD,
53         CFG_VFS_MAXFILES,
54         CFG_VFS_CHROOT,
55         CFG_ERRNO,
56         NUM_CFG_ENTRIES
57 };
58 #define CFGINT(id)      (*Threads_GetCfgPtr(id))
59 #define CFGPTR(id)      (*(void**)Threads_GetCfgPtr(id))
60
61 #define errno   (CFGINT(CFG_ERRNO))
62 /**
63  * \}
64  */
65
66 // === CONSTANTS ===
67 // --- Memory Flags --
68 /**
69  * \name Memory Flags
70  * \{
71  * \todo Move to mm_virt.h
72  */
73 #define MM_PFLAG_RO             0x01    // Writes disallowed
74 #define MM_PFLAG_EXEC   0x02    // Allow execution
75 #define MM_PFLAG_NOPAGE 0x04    // Prevent from being paged out
76 #define MM_PFLAG_COW    0x08    // Copy-On-Write
77 #define MM_PFLAG_KERNEL 0x10    // Kernel-Only (Ring0)
78 /**
79  * \}
80  */
81 // --- Interface Flags & Macros
82 #define CLONE_VM        0x10
83
84 // === Types ===
85 typedef void (*tThreadFunction)(void*);
86
87 // === Kernel Export Macros ===
88 /**
89  * \name Kernel Function 
90  * \{
91  */
92 typedef struct sKernelSymbol {
93         const char      *Name;
94         tVAddr  Value;
95 } tKernelSymbol;
96 #define EXPORT(_name)   tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)_name}
97 #define EXPORTV(_name)  tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)&_name}
98 #define EXPORTAS(_sym,_name)    tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)_sym}
99 /**
100  * \}
101  */
102
103 // === FUNCTIONS ===
104 // --- IRQs ---
105 extern int      IRQ_AddHandler(int Num, void (*Callback)(int));
106 extern void     IRQ_RemHandler(int Handle);
107
108 // --- Logging ---
109 extern void     Log_KernelPanic(const char *Ident, const char *Message, ...);
110 extern void     Log_Panic(const char *Ident, const char *Message, ...);
111 extern void     Log_Error(const char *Ident, const char *Message, ...);
112 extern void     Log_Warning(const char *Ident, const char *Message, ...);
113 extern void     Log_Notice(const char *Ident, const char *Message, ...);
114 extern void     Log_Log(const char *Ident, const char *Message, ...);
115 extern void     Log_Debug(const char *Ident, const char *Message, ...);
116
117 // --- Debug ---
118 /**
119  * \name Debugging and Errors
120  * \{
121  */
122 extern void     Debug_KernelPanic(void);        //!< Initiate a kernel panic
123 extern void     Panic(const char *Msg, ...);    //!< Print a panic message (initiates a kernel panic)
124 extern void     Warning(const char *Msg, ...);  //!< Print a warning message
125 extern void     LogF(const char *Fmt, ...);     //!< Print a log message without a trailing newline
126 extern void     Log(const char *Fmt, ...);      //!< Print a log message
127 extern void     Debug(const char *Fmt, ...);    //!< Print a debug message (doesn't go to KTerm)
128 extern void     LogV(const char *Fmt, va_list Args);    //!< va_list Log message
129 extern void     Debug_Enter(const char *FuncName, const char *ArgTypes, ...);
130 extern void     Debug_Log(const char *FuncName, const char *Fmt, ...);
131 extern void     Debug_Leave(const char *FuncName, char RetType, ...);
132 extern void     Debug_HexDump(const char *Header, const void *Data, Uint Length);
133 #define UNIMPLEMENTED() Warning("'%s' unimplemented", __func__)
134 #if DEBUG
135 # define ENTER(_types...)       Debug_Enter((char*)__func__, _types)
136 # define LOG(_fmt...)   Debug_Log((char*)__func__, _fmt)
137 # define LEAVE(_t...)   Debug_Leave((char*)__func__, _t)
138 # define LEAVE_RET(_t,_v...)    do{LEAVE(_t,_v);return _v;}while(0)
139 # define LEAVE_RET0()   do{LEAVE('-');return;}while(0)
140 #else
141 # define ENTER(...)
142 # define LOG(...)
143 # define LEAVE(...)
144 # define LEAVE_RET(_t,_v...)    return (_v)
145 # define LEAVE_RET0()   return
146 #endif
147 #if SANITY
148 # define ASSERT(expr) do{if(!(expr))Panic("%s: Assertion '"#expr"' failed",(char*)__func__);}while(0)
149 #else
150 # define ASSERT(expr)
151 #endif
152 /**
153  * \}
154  */
155
156 // --- IO ---
157 /**
158  * \name I/O Memory Access
159  * \{
160  */
161 extern void     outb(Uint16 Port, Uint8 Data);
162 extern void     outw(Uint16 Port, Uint16 Data);
163 extern void     outd(Uint16 Port, Uint32 Data);
164 extern void     outq(Uint16 Port, Uint64 Data);
165 extern Uint8    inb(Uint16 Port);
166 extern Uint16   inw(Uint16 Port);
167 extern Uint32   ind(Uint16 Port);
168 extern Uint64   inq(Uint16 Port);
169 /**
170  * \}
171  */
172
173 // --- Memory Management ---
174 /**
175  * \name Memory Management
176  * \{
177  * \todo Move to mm_virt.h
178  */
179 /**
180  * \brief Allocate a physical page at \a VAddr
181  * \param VAddr Virtual Address to allocate at
182  * \return Physical address allocated
183  */
184 extern tPAddr   MM_Allocate(tVAddr VAddr) __attribute__ ((warn_unused_result));
185 /**
186  * \brief Deallocate a page
187  * \param VAddr Virtual address to unmap
188  */
189 extern void     MM_Deallocate(tVAddr VAddr);
190 /**
191  * \brief Map a physical page at \a PAddr to \a VAddr
192  * \param VAddr Target virtual address
193  * \param PAddr Physical address to map
194  * \return Boolean Success
195  */
196 extern int      MM_Map(tVAddr VAddr, tPAddr PAddr);
197 /**
198  * \brief Get the physical address of \a Addr
199  * \param Addr  Address of the page to get the physical address of
200  * \return Physical page mapped at \a Addr
201  */
202 extern tPAddr   MM_GetPhysAddr(tVAddr Addr);
203 /**
204  * \brief Set the access flags on a page
205  * \param VAddr Virtual address of the page
206  * \param Flags New flags value
207  * \param Mask  Flags to set
208  */
209 extern void     MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
210 /**
211  * \brief Get the flags on a flag
212  * \param VAddr Virtual address of page
213  * \return Flags value of the page
214  */
215 extern Uint     MM_GetFlags(tVAddr VAddr);
216 /**
217  * \brief Checks is a memory range is user accessable
218  * \param VAddr Base address to check
219  * \return 1 if the memory is all user-accessable, 0 otherwise
220  */
221 #define MM_IsUser(VAddr)        (!(MM_GetFlags((VAddr))&MM_PFLAG_KERNEL))
222 /**
223  * \brief Temporarily map a page into the address space
224  * \param PAddr Physical addres to map
225  * \return Virtual address of page in memory
226  * \note There is only a limited ammount of slots avaliable
227  */
228 extern tVAddr   MM_MapTemp(tPAddr PAddr);
229 /**
230  * \brief Free a temporarily mapped page
231  * \param VAddr Allocate virtual addres of page
232  */
233 extern void     MM_FreeTemp(tVAddr VAddr);
234 /**
235  * \brief Map a physcal address range into the virtual address space
236  * \param PAddr Physical address to map in
237  * \param Number        Number of pages to map
238  */
239 extern tVAddr   MM_MapHWPages(tPAddr PAddr, Uint Number);
240 /**
241  * \brief Allocates DMA physical memory
242  * \param Pages Number of pages required
243  * \param MaxBits       Maximum number of bits the physical address can have
244  * \param PhysAddr      Pointer to the location to place the physical address allocated
245  * \return Virtual address allocate
246  */
247 extern tVAddr   MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
248 /**
249  * \brief Unmaps an allocated hardware range
250  * \param VAddr Virtual address allocate by ::MM_MapHWPage or ::MM_AllocDMA
251  * \param Number        Number of pages to free
252  */
253 extern void     MM_UnmapHWPages(tVAddr VAddr, Uint Number);
254 /**
255  * \brief Allocate a single physical page
256  * \return Physical address allocated
257  */
258 extern tPAddr   MM_AllocPhys(void);
259 /**
260  * \brief Allocate a contiguous range of physical pages
261  * \param Pages Number of pages to allocate
262  * \param MaxBits       Maximum number of address bits allowed
263  * \return First physical address allocated
264  */
265 extern tPAddr   MM_AllocPhysRange(int Pages, int MaxBits);
266 /**
267  * \brief Reference a physical page
268  * \param PAddr Page to mark as referenced
269  */
270 extern void     MM_RefPhys(tPAddr PAddr);
271 /**
272  * \brief Dereference a physical page
273  * \param PAddr Page to dereference
274  */
275 extern void     MM_DerefPhys(tPAddr PAddr);
276 /**
277  * \brief Get the number of times a page has been referenced
278  * \param PAddr Address to check
279  * \return Reference count for the page
280  */
281 extern int      MM_GetRefCount(tPAddr PAddr);
282 /**
283  * \brief Set the node/offset associated with a page
284  * \param PAddr Physical address of page
285  * \param Node  Node pointer (tVFS_Node)
286  * \param Offset        File offset
287  * \return Boolean failure
288  * \retval 0    Success
289  * \retval 1    Page not allocated
290  */
291 extern int      MM_SetPageInfo(tPAddr PAddr, void *Node, Uint64 Offset);
292 /**
293  * \brief Get the node/offset associated with a page
294  * \param PAddr Physical address of page
295  * \param Node  Node pointer (tVFS_Node) destination
296  * \param Offset        File offset destination (pointer)
297  * \return Boolean failure
298  * \retval 0    Success
299  * \retval 1    Page not allocated
300  */
301 extern int      MM_GetPageInfo(tPAddr PAddr, void **Node, Uint64 *Offset);
302 /**
303  * \}
304  */
305
306 // --- Memory Manipulation ---
307 /**
308  * \name Memory Manipulation
309  * \{
310  */
311 extern int      memcmp(const void *m1, const void *m2, size_t count);
312 extern void *memcpy(void *dest, const void *src, size_t count);
313 extern void *memcpyd(void *dest, const void *src, size_t count);
314 extern void *memset(void *dest, int val, size_t count);
315 extern void *memsetd(void *dest, Uint32 val, size_t count);
316 /**
317  * \}
318  */
319 /**
320  * \name Memory Validation
321  * \{
322  */
323 extern int      CheckString(const char *String);
324 extern int      CheckMem(const void *Mem, int Num);
325 /**
326  * \}
327  */
328
329 // --- Endianness ---
330 /**
331  * \name Endianness Swapping
332  * \{
333  */
334 #ifdef __BIG_ENDIAN__
335 #define LittleEndian16(_val)    SwapEndian16(_val)
336 #define LittleEndian32(_val)    SwapEndian32(_val)
337 #define BigEndian16(_val)       (_val)
338 #define BigEndian32(_val)       (_val)
339 #else
340 #define LittleEndian16(_val)    (_val)
341 #define LittleEndian32(_val)    (_val)
342 #define BigEndian16(_val)       SwapEndian16(_val)
343 #define BigEndian32(_val)       SwapEndian32(_val)
344 #endif
345 extern Uint16   SwapEndian16(Uint16 Val);
346 extern Uint32   SwapEndian32(Uint32 Val);
347 /**
348  * \}
349  */
350
351 // --- Strings ---
352 /**
353  * \name Strings
354  * \{
355  */
356 extern int      vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args);
357 extern int      sprintf(char *__s, const char *__format, ...);
358 extern size_t   strlen(const char *Str);
359 extern char     *strcpy(char *__dest, const char *__src);
360 extern char     *strncpy(char *__dest, const char *__src, size_t max);
361 extern char     *strcat(char *__dest, const char *__src);
362 extern char     *strncat(char *__dest, const char *__src, size_t n);
363 extern int      strcmp(const char *__str1, const char *__str2);
364 extern int      strncmp(const char *Str1, const char *Str2, size_t num);
365 extern int      strucmp(const char *Str1, const char *Str2);
366 // strdup macro is defined in heap.h
367 extern char     *_strdup(const char *File, int Line, const char *Str);
368 extern char     **str_split(const char *__str, char __ch);
369 extern char     *strchr(const char *__s, int __c);
370 extern int      strpos(const char *Str, char Ch);
371 extern int      strpos8(const char *str, Uint32 search);
372 extern void     itoa(char *buf, Uint64 num, int base, int minLength, char pad);
373 extern int      atoi(const char *string);
374 extern int      ReadUTF8(const Uint8 *str, Uint32 *Val);
375 extern int      WriteUTF8(Uint8 *str, Uint32 Val);
376 extern int      ModUtil_SetIdent(char *Dest, const char *Value);
377 extern int      ModUtil_LookupString(const char **Array, const char *Needle);
378
379 extern Uint8    ByteSum(const void *Ptr, int Size);
380 extern int      UnHex(Uint8 *Dest, size_t DestSize, const char *SourceString);
381 /**
382  * \}
383  */
384
385 extern int      rand(void);
386 extern int      CallWithArgArray(void *Function, int NArgs, Uint *Args);
387
388 // --- Heap ---
389 #include <heap.h>
390
391 // --- Modules ---
392 /**
393  * \name Modules
394  * \{
395  */
396 extern int      Module_LoadMem(void *Buffer, Uint Length, const char *ArgStr);
397 extern int      Module_LoadFile(const char *Path, const char *ArgStr);
398 /**
399  * \}
400  */
401
402 // --- Timing ---
403 /**
404  * \name Time and Timing
405  * \{
406  */
407 /**
408  * \brief Create a timestamp from a time
409  */
410 extern Sint64   timestamp(int sec, int mins, int hrs, int day, int month, int year);
411 /**
412  * \brief Gets the current timestamp (miliseconds since Midnight 1st January 1970)
413  */
414 extern Sint64   now(void);
415 /**
416  * \brief Timer callback function
417  */
418 typedef void (tTimerCallback)(void *);
419 /**
420  * \brief Creates a one-shot timer
421  * \param Delta Period of the timer
422  * \param Callback      Function to call each time
423  * \param Argument      Argument to pass to the callback
424  */
425 extern int      Time_CreateTimer(int Delta, tTimerCallback *Callback, void *Argument);
426 /**
427  * \brief Removed an active timer
428  */
429 extern void     Time_RemoveTimer(int ID);
430 /**
431  * \brief Wait for a period of milliseconds
432  */
433 extern void     Time_Delay(int Delay);
434 /**
435  * \}
436  */
437
438 // --- Threads ---
439 /**
440  * \name Threads and Processes
441  * \{
442  */
443 extern int      Proc_SpawnWorker(void);
444 extern int      Proc_Spawn(char *Path);
445 extern void     Threads_Exit(int TID, int Status);
446 extern void     Threads_Yield(void);
447 extern void     Threads_Sleep(void);
448 extern int      Threads_WakeTID(tTID Thread);
449 extern tPID     Threads_GetPID(void);
450 extern tTID     Threads_GetTID(void);
451 extern tUID     Threads_GetUID(void);
452 extern tGID     Threads_GetGID(void);
453 extern int      SpawnTask(tThreadFunction Function, void *Arg);
454 extern Uint     *Threads_GetCfgPtr(int Id);
455 extern int      Threads_SetName(const char *NewName);
456 /**
457  * \}
458  */
459
460 // --- Simple Math ---
461 extern int      DivUp(int num, int dem);
462
463 #include <binary_ext.h>
464 #include <vfs_ext.h>
465 #include <mutex.h>
466
467 #endif

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