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

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