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

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