2 * AcessOS Microkernel Version
9 * \brief Acess2 Kernel API Core
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))
27 * \name Boolean constants
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
51 // --- Helper Macros ---
56 #define CONCAT(x,y) x ## y
57 #define EXPAND_CONCAT(x,y) CONCAT(x,y)
59 #define EXPAND_STR(x) STR(x)
61 extern char __buildnum[];
62 #define BUILD_NUM ((int)(Uint)&__buildnum)
63 extern const char gsGitHash[];
64 extern const char gsBuildInfo[];
66 #define VER2(major,minor) ((((major)&0xFF)<<8)|((minor)&0xFF))
71 //! \brief Error number
72 #define errno (*Threads_GetErrno())
75 // --- Memory Flags --
79 * \todo Move to mm_virt.h
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)
89 // --- Interface Flags & Macros
91 * \name Flags for Proc_Clone
94 //! Clone the entire process
96 //! Don't copy user pages
97 #define CLONE_NOUSER 0x20
98 //! Inherit the parent's PGID
99 #define CLONE_PGID 0x40
106 * \brief Thread root function
108 * Function pointer prototype of a thread entrypoint. When it
109 * returns, Threads_Exit is called
111 typedef void (*tThreadFunction)(void*);
113 // === Kernel Export Macros ===
115 * \name Kernel exports
118 //! Kernel symbol definition
119 typedef struct sKernelSymbol {
120 const char *Name; //!< Symbolic name
121 tVAddr Value; //!< Value of the symbol
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}
136 * \name IRQ hander registration
139 extern int IRQ_AddHandler(int Num, void (*Callback)(int, void*), void *Ptr);
140 extern void IRQ_RemHandler(int Handle);
145 #include "logdebug.h"
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)
159 * \name I/O Memory Access
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);
174 // --- Memory Management ---
176 * \name Memory Management
178 * \todo Move to mm_virt.h
181 * \brief Allocate a physical page at \a VAddr
182 * \param VAddr Virtual Address to allocate at
183 * \return Physical address allocated
185 extern tPAddr MM_Allocate(tVAddr VAddr) __attribute__ ((warn_unused_result));
187 * \brief Deallocate a page
188 * \param VAddr Virtual address to unmap
190 extern void MM_Deallocate(tVAddr VAddr);
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
197 extern int MM_Map(tVAddr VAddr, tPAddr PAddr);
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
203 extern tPAddr MM_GetPhysAddr(volatile const void *Addr);
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
210 extern void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
212 * \brief Get the flags on a flag
213 * \param VAddr Virtual address of page
214 * \return Flags value of the page
216 extern Uint MM_GetFlags(tVAddr VAddr);
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
222 #define MM_IsUser(VAddr) (!(MM_GetFlags((tVAddr)(VAddr))&MM_PFLAG_KERNEL))
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
229 extern void *MM_MapTemp(tPAddr PAddr);
231 * \brief Free a temporarily mapped page
232 * \param Ptr Pointer to page base
234 extern void MM_FreeTemp(void *Ptr);
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
240 extern void *MM_MapHWPages(tPAddr PAddr, Uint Number);
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
248 extern void *MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
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
254 extern void MM_UnmapHWPages(tVAddr VAddr, Uint Number);
256 * \brief Allocate a single physical page
257 * \return Physical address allocated
259 extern tPAddr MM_AllocPhys(void);
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
266 extern tPAddr MM_AllocPhysRange(int Pages, int MaxBits);
268 * \brief Reference a physical page
269 * \param PAddr Page to mark as referenced
271 extern void MM_RefPhys(tPAddr PAddr);
273 * \brief Dereference a physical page
274 * \param PAddr Page to dereference
276 extern void MM_DerefPhys(tPAddr PAddr);
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
282 extern int MM_GetRefCount(tPAddr PAddr);
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
289 * \retval 1 Page not allocated
291 extern int MM_SetPageNode(tPAddr PAddr, void *Node);
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
298 * \retval 1 Page not allocated
300 extern int MM_GetPageNode(tPAddr PAddr, void **Node);
305 // --- Memory Manipulation ---
307 * \name Memory Manipulation
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);
320 * \name Memory Validation
323 extern int CheckString(const char *String);
324 extern int CheckMem(const void *Mem, int Num);
329 // --- Endianness ---
331 * \name Endianness Swapping
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)
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)
349 extern Uint16 SwapEndian16(Uint16 Val);
350 extern Uint32 SwapEndian32(Uint32 Val);
351 extern Uint64 SwapEndian64(Uint64 Val);
357 #include <acess_string.h>
362 * \brief Get a random number
363 * \return Random number
364 * \note Current implementation is a linear congruency
366 extern int rand(void);
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
374 extern int CallWithArgArray(void *Function, int NArgs, Uint *Args);
379 * \brief Magic heap allocation function
381 extern void *alloca(size_t Size);
388 extern int Module_LoadMem(void *Buffer, Uint Length, const char *ArgStr);
389 extern int Module_LoadFile(const char *Path, const char *ArgStr);
396 * \name Time and Timing
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)
403 extern tTime timestamp(int sec, int mins, int hrs, int day, int month, int year);
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)
408 extern void format_date(tTime TS, int *year, int *month, int *day, int *hrs, int *mins, int *sec, int *ms);
410 * \brief Gets the current timestamp (miliseconds since Midnight 1st January 1970)
412 extern Sint64 now(void);
419 * \name Threads and Processes
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);
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);
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; }
451 #include <binary_ext.h>