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

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