2 * AcessOS Microkernel Version
9 * \brief Acess2 Kernel API Core
16 # define HALT_CPU() for(;;);
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))
30 #define ALIGN(x) __attribute__((aligned(x)))
33 * \name Boolean constants
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 typedef struct { char _[PAGE_SIZE];} tPage; // Representation of a page for pointer arithmatic
58 // --- Helper Macros ---
63 #define CONCAT(x,y) x ## y
64 #define EXPAND_CONCAT(x,y) CONCAT(x,y)
66 #define EXPAND_STR(x) STR(x)
68 extern char __buildnum[];
69 #define BUILD_NUM ((int)(Uint)&__buildnum)
70 extern const char gsGitHash[];
71 extern const char gsBuildInfo[];
73 #define VER2(major,minor) ((((major)&0xFF)<<8)|((minor)&0xFF))
78 //! \brief Error number
79 #define errno (*Threads_GetErrno())
82 // --- Memory Flags --
86 * \todo Move to mm_virt.h
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)
96 // --- Interface Flags & Macros
98 * \name Flags for Proc_Clone
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
113 * \brief Thread root function
115 * Function pointer prototype of a thread entrypoint. When it
116 * returns, Threads_Exit is called
118 typedef void (*tThreadFunction)(void*);
120 // === Kernel Export Macros ===
122 * \name Kernel exports
125 //! Kernel symbol definition
126 typedef struct sKernelSymbol {
127 const char *Name; //!< Symbolic name
128 tVAddr Value; //!< Value of the symbol
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}
143 * \name IRQ hander registration
146 extern int IRQ_AddHandler(int Num, void (*Callback)(int, void*), void *Ptr);
147 extern void IRQ_RemHandler(int Handle);
152 #include "logdebug.h"
156 #define inb(a) (Log_Panic("Arch", STR(ARCHDIR)" does not support in* (%s:%i)", __FILE__, __LINE__),0)
157 #define inw(a) inb(a)
158 #define ind(a) inb(a)
159 #define inq(a) inb(a)
160 #define outb(a,b) (Log_Panic("Arch", STR(ARCHDIR)" does not support out* (%s:%i)", __FILE__, __LINE__),(void)(b))
161 #define outw(a,b) outb(a,b)
162 #define outd(a,b) outb(a,b)
163 #define outq(a,b) outb(a,b)
166 * \name I/O Memory Access
169 extern void outb(Uint16 Port, Uint8 Data);
170 extern void outw(Uint16 Port, Uint16 Data);
171 extern void outd(Uint16 Port, Uint32 Data);
172 extern void outq(Uint16 Port, Uint64 Data);
173 extern Uint8 inb(Uint16 Port);
174 extern Uint16 inw(Uint16 Port);
175 extern Uint32 ind(Uint16 Port);
176 extern Uint64 inq(Uint16 Port);
181 // --- Memory Management ---
183 * \name Memory Management
185 * \todo Move to mm_virt.h
188 * \brief Allocate a physical page at \a VAddr
189 * \param VAddr Virtual Address to allocate at
190 * \return Physical address allocated
192 extern tPAddr MM_Allocate(volatile void *VAddr) __attribute__ ((warn_unused_result));
194 * \breif Allocate a zeroed COW page to \a VAddr
195 * \param VAddr Virtual address to allocate at
196 * \return Physical address allocated (don't cache)
198 extern void MM_AllocateZero(volatile void *VAddr);
200 * \brief Deallocate a page
201 * \param VAddr Virtual address to unmap
203 extern void MM_Deallocate(volatile void *VAddr);
205 * \brief Map a physical page at \a PAddr to \a VAddr
206 * \param VAddr Target virtual address
207 * \param PAddr Physical address to map
208 * \return Boolean Success
210 extern int MM_Map(volatile void * VAddr, tPAddr PAddr);
212 * \brief Get the physical address of \a Addr
213 * \param Addr Address of the page to get the physical address of
214 * \return Physical page mapped at \a Addr
216 extern tPAddr MM_GetPhysAddr(volatile const void *Addr);
218 * \brief Set the access flags on a page
219 * \param VAddr Virtual address of the page
220 * \param Flags New flags value
221 * \param Mask Flags to set
223 extern void MM_SetFlags(volatile void *VAddr, Uint Flags, Uint Mask);
225 * \brief Get the flags on a flag
226 * \param VAddr Virtual address of page
227 * \return Flags value of the page
229 extern Uint MM_GetFlags(volatile const void *VAddr);
231 * \brief Checks is a memory range is user accessable
232 * \param VAddr Base address to check
233 * \return 1 if the memory is all user-accessable, 0 otherwise
235 #define MM_IsUser(VAddr) (!(MM_GetFlags((const void*)(VAddr))&MM_PFLAG_KERNEL))
237 * \brief Temporarily map a page into the address space
238 * \param PAddr Physical addres to map
239 * \return Virtual address of page in memory
240 * \note There is only a limited ammount of slots avaliable
242 extern void *MM_MapTemp(tPAddr PAddr);
244 * \brief Free a temporarily mapped page
245 * \param Ptr Pointer to page base
247 extern void MM_FreeTemp(void *Ptr);
249 * \brief Map a physcal address range into the virtual address space
250 * \param PAddr Physical address to map in
251 * \param Number Number of pages to map
253 extern void *MM_MapHWPages(tPAddr PAddr, Uint Number);
255 * \brief Allocates DMA physical memory
256 * \param Pages Number of pages required
257 * \param MaxBits Maximum number of bits the physical address can have
258 * \param PhysAddr Pointer to the location to place the physical address allocated
259 * \return Virtual address allocate
261 extern void *MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
263 * \brief Unmaps an allocated hardware range
264 * \param VAddr Virtual address allocate by ::MM_MapHWPages or ::MM_AllocDMA
265 * \param Number Number of pages to free
267 extern void MM_UnmapHWPages(volatile void *VAddr, Uint Number);
269 * \brief Allocate a single physical page
270 * \return Physical address allocated
272 extern tPAddr MM_AllocPhys(void);
274 * \brief Allocate a contiguous range of physical pages
275 * \param Pages Number of pages to allocate
276 * \param MaxBits Maximum number of address bits allowed
277 * \return First physical address allocated
279 extern tPAddr MM_AllocPhysRange(int Pages, int MaxBits);
281 * \brief Reference a physical page
282 * \param PAddr Page to mark as referenced
284 extern void MM_RefPhys(tPAddr PAddr);
286 * \brief Dereference a physical page
287 * \param PAddr Page to dereference
289 extern void MM_DerefPhys(tPAddr PAddr);
291 * \brief Get the number of times a page has been referenced
292 * \param PAddr Address to check
293 * \return Reference count for the page
295 extern int MM_GetRefCount(tPAddr PAddr);
297 * \brief Set the node associated with a page
298 * \param PAddr Physical address of page
299 * \param Node Node pointer (tVFS_Node)
300 * \return Boolean failure
302 * \retval 1 Page not allocated
304 extern int MM_SetPageNode(tPAddr PAddr, void *Node);
306 * \brief Get the node associated with a page
307 * \param PAddr Physical address of page
308 * \param Node Node pointer (tVFS_Node) destination
309 * \return Boolean failure
311 * \retval 1 Page not allocated
313 extern int MM_GetPageNode(tPAddr PAddr, void **Node);
318 // --- Memory Manipulation ---
320 * \name Memory Manipulation
323 extern int memcmp(const void *m1, const void *m2, size_t count);
324 extern void *memcpy(void *dest, const void *src, size_t count);
325 extern void *memcpyd(void *dest, const void *src, size_t count);
326 extern void *memmove(void *dest, const void *src, size_t len);
327 extern void *memset(void *dest, int val, size_t count);
328 extern void *memsetd(void *dest, Uint32 val, size_t count);
333 * \name Memory Validation
336 extern int CheckString(const char *String);
337 extern int CheckMem(const void *Mem, int Num);
342 // --- Endianness ---
344 * \name Endianness Swapping
347 #ifdef __BIG_ENDIAN__
348 #define LittleEndian16(_val) SwapEndian16(_val)
349 #define LittleEndian32(_val) SwapEndian32(_val)
350 #define LittleEndian64(_val) SwapEndian32(_val)
351 #define BigEndian16(_val) (_val)
352 #define BigEndian32(_val) (_val)
353 #define BigEndian64(_val) (_val)
355 #define LittleEndian16(_val) (_val)
356 #define LittleEndian32(_val) (_val)
357 #define LittleEndian64(_val) (_val)
358 #define BigEndian16(_val) SwapEndian16(_val)
359 #define BigEndian32(_val) SwapEndian32(_val)
360 #define BigEndian64(_val) SwapEndian64(_val)
362 extern Uint16 SwapEndian16(Uint16 Val);
363 extern Uint32 SwapEndian32(Uint32 Val);
364 extern Uint64 SwapEndian64(Uint64 Val);
370 #include <acess_string.h>
375 * \brief Get a random number
376 * \return Random number
377 * \note Current implementation is a linear congruency
379 extern int rand(void);
381 * \brief Call a function with a variable number of arguments
382 * \param Function Function address
383 * \param NArgs Number of entries in \a Args
384 * \param Args Array of arguments
385 * \return Integer from called Function
387 extern int CallWithArgArray(void *Function, int NArgs, Uint *Args);
392 * \brief Magic heap allocation function
394 extern void *alloca(size_t Size);
401 extern int Module_LoadMem(void *Buffer, Uint Length, const char *ArgStr);
402 extern int Module_LoadFile(const char *Path, const char *ArgStr);
409 * \name Time and Timing
413 * \brief Create a timestamp from a time
414 * \note Days/Months are zero-based (e.g. December is 11, and has a day range of 0-30)
416 extern tTime timestamp(int sec, int mins, int hrs, int day, int month, int year);
418 * \brief Extract the date/time from a timestamp
419 * \note Days/Months are zero-based (e.g. December is 11, and has a day range of 0-30)
421 extern void format_date(tTime TS, int *year, int *month, int *day, int *hrs, int *mins, int *sec, int *ms);
423 * \brief Gets the current timestamp (miliseconds since Midnight 1st January 1970)
425 extern Sint64 now(void);
432 * \name Threads and Processes
435 extern struct sThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data);
436 extern int Proc_Spawn(const char *Path);
437 extern int Proc_SysSpawn(const char *Binary, const char **ArgV, const char **EnvP, int nFD, int *FDs);
438 extern int Proc_Execve(const char *File, const char **ArgV, const char **EnvP, int DataSize);
439 extern void Threads_Exit(int TID, int Status);
440 extern void Threads_Yield(void);
441 extern void Threads_Sleep(void);
442 extern int Threads_WakeTID(tTID Thread);
443 extern tPGID Threads_GetPGID(void);
444 extern tPID Threads_GetPID(void);
445 extern tTID Threads_GetTID(void);
446 extern tUID Threads_GetUID(void);
447 extern tGID Threads_GetGID(void);
448 extern int SpawnTask(tThreadFunction Function, void *Arg);
449 extern int *Threads_GetErrno(void);
450 extern int Threads_SetName(const char *NewName);
455 // --- Simple Math ---
456 //! Divide and round up
457 extern int DivUp(int num, int dem);
458 //! Divide and Modulo 64-bit unsigned integer
459 extern Uint64 DivMod64U(Uint64 Num, Uint64 Den, Uint64 *Rem);
461 static inline int MIN(int a, int b) { return a < b ? a : b; }
462 static inline int MAX(int a, int b) { return a > b ? a : b; }
464 #include <binary_ext.h>