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

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