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

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