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))
26 #define ALIGN(x) __attribute__((aligned(x)))
29 * \name Boolean constants
42 typedef Uint32 tPID; //!< Process ID type
43 typedef Uint32 tPGID; //!< Process Group ID type
44 typedef Uint32 tTID; //!< Thread ID Type
45 typedef Uint32 tUID; //!< User ID Type
46 typedef Uint32 tGID; //!< Group ID Type
47 typedef Sint64 tTimestamp; //!< Timestamp (miliseconds since 00:00 1 Jan 1970)
48 typedef Sint64 tTime; //!< Same again
49 typedef struct sShortSpinlock tShortSpinlock; //!< Opaque (kinda) spinlock
50 typedef int bool; //!< Boolean type
51 typedef Uint64 off_t; //!< VFS Offset
53 // --- Helper Macros ---
58 #define CONCAT(x,y) x ## y
59 #define EXPAND_CONCAT(x,y) CONCAT(x,y)
61 #define EXPAND_STR(x) STR(x)
63 extern char __buildnum[];
64 #define BUILD_NUM ((int)(Uint)&__buildnum)
65 extern const char gsGitHash[];
66 extern const char gsBuildInfo[];
68 #define VER2(major,minor) ((((major)&0xFF)<<8)|((minor)&0xFF))
73 //! \brief Error number
74 #define errno (*Threads_GetErrno())
77 // --- Memory Flags --
81 * \todo Move to mm_virt.h
83 #define MM_PFLAG_RO 0x01 // Writes disallowed
84 #define MM_PFLAG_EXEC 0x02 // Allow execution
85 #define MM_PFLAG_NOPAGE 0x04 // Prevent from being paged out
86 #define MM_PFLAG_COW 0x08 // Copy-On-Write
87 #define MM_PFLAG_KERNEL 0x10 // Kernel-Only (Ring0)
91 // --- Interface Flags & Macros
93 * \name Flags for Proc_Clone
96 //! Clone the entire process
98 //! Don't copy user pages
99 #define CLONE_NOUSER 0x20
100 //! Inherit the parent's PGID
101 #define CLONE_PGID 0x40
108 * \brief Thread root function
110 * Function pointer prototype of a thread entrypoint. When it
111 * returns, Threads_Exit is called
113 typedef void (*tThreadFunction)(void*);
115 // === Kernel Export Macros ===
117 * \name Kernel exports
120 //! Kernel symbol definition
121 typedef struct sKernelSymbol {
122 const char *Name; //!< Symbolic name
123 tVAddr Value; //!< Value of the symbol
125 //! Export a pointer symbol (function/array)
126 #define EXPORT(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)_name}
127 //! Export a variable
128 #define EXPORTV(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)&_name}
129 //! Export a symbol under another name
130 #define EXPORTAS(_sym,_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)_sym}
138 * \name IRQ hander registration
141 extern int IRQ_AddHandler(int Num, void (*Callback)(int, void*), void *Ptr);
142 extern void IRQ_RemHandler(int Handle);
147 #include "logdebug.h"
151 #define inb(a) (Log_Panic("Arch", STR(ARCHDIR)" does not support in* (%s:%i)", __FILE__, __LINE__),0)
152 #define inw(a) inb(a)
153 #define ind(a) inb(a)
154 #define inq(a) inb(a)
155 #define outb(a,b) (Log_Panic("Arch", STR(ARCHDIR)" does not support out* (%s:%i)", __FILE__, __LINE__),(void)(b))
156 #define outw(a,b) outb(a,b)
157 #define outd(a,b) outb(a,b)
158 #define outq(a,b) outb(a,b)
161 * \name I/O Memory Access
164 extern void outb(Uint16 Port, Uint8 Data);
165 extern void outw(Uint16 Port, Uint16 Data);
166 extern void outd(Uint16 Port, Uint32 Data);
167 extern void outq(Uint16 Port, Uint64 Data);
168 extern Uint8 inb(Uint16 Port);
169 extern Uint16 inw(Uint16 Port);
170 extern Uint32 ind(Uint16 Port);
171 extern Uint64 inq(Uint16 Port);
176 // --- Memory Management ---
178 * \name Memory Management
180 * \todo Move to mm_virt.h
183 * \brief Allocate a physical page at \a VAddr
184 * \param VAddr Virtual Address to allocate at
185 * \return Physical address allocated
187 extern tPAddr MM_Allocate(tVAddr VAddr) __attribute__ ((warn_unused_result));
189 * \brief Deallocate a page
190 * \param VAddr Virtual address to unmap
192 extern void MM_Deallocate(tVAddr VAddr);
194 * \brief Map a physical page at \a PAddr to \a VAddr
195 * \param VAddr Target virtual address
196 * \param PAddr Physical address to map
197 * \return Boolean Success
199 extern int MM_Map(tVAddr VAddr, tPAddr PAddr);
201 * \brief Get the physical address of \a Addr
202 * \param Addr Address of the page to get the physical address of
203 * \return Physical page mapped at \a Addr
205 extern tPAddr MM_GetPhysAddr(volatile const void *Addr);
207 * \brief Set the access flags on a page
208 * \param VAddr Virtual address of the page
209 * \param Flags New flags value
210 * \param Mask Flags to set
212 extern void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
214 * \brief Get the flags on a flag
215 * \param VAddr Virtual address of page
216 * \return Flags value of the page
218 extern Uint MM_GetFlags(tVAddr VAddr);
220 * \brief Checks is a memory range is user accessable
221 * \param VAddr Base address to check
222 * \return 1 if the memory is all user-accessable, 0 otherwise
224 #define MM_IsUser(VAddr) (!(MM_GetFlags((tVAddr)(VAddr))&MM_PFLAG_KERNEL))
226 * \brief Temporarily map a page into the address space
227 * \param PAddr Physical addres to map
228 * \return Virtual address of page in memory
229 * \note There is only a limited ammount of slots avaliable
231 extern void *MM_MapTemp(tPAddr PAddr);
233 * \brief Free a temporarily mapped page
234 * \param Ptr Pointer to page base
236 extern void MM_FreeTemp(void *Ptr);
238 * \brief Map a physcal address range into the virtual address space
239 * \param PAddr Physical address to map in
240 * \param Number Number of pages to map
242 extern void *MM_MapHWPages(tPAddr PAddr, Uint Number);
244 * \brief Allocates DMA physical memory
245 * \param Pages Number of pages required
246 * \param MaxBits Maximum number of bits the physical address can have
247 * \param PhysAddr Pointer to the location to place the physical address allocated
248 * \return Virtual address allocate
250 extern void *MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
252 * \brief Unmaps an allocated hardware range
253 * \param VAddr Virtual address allocate by ::MM_MapHWPages or ::MM_AllocDMA
254 * \param Number Number of pages to free
256 extern void MM_UnmapHWPages(tVAddr VAddr, Uint Number);
258 * \brief Allocate a single physical page
259 * \return Physical address allocated
261 extern tPAddr MM_AllocPhys(void);
263 * \brief Allocate a contiguous range of physical pages
264 * \param Pages Number of pages to allocate
265 * \param MaxBits Maximum number of address bits allowed
266 * \return First physical address allocated
268 extern tPAddr MM_AllocPhysRange(int Pages, int MaxBits);
270 * \brief Reference a physical page
271 * \param PAddr Page to mark as referenced
273 extern void MM_RefPhys(tPAddr PAddr);
275 * \brief Dereference a physical page
276 * \param PAddr Page to dereference
278 extern void MM_DerefPhys(tPAddr PAddr);
280 * \brief Get the number of times a page has been referenced
281 * \param PAddr Address to check
282 * \return Reference count for the page
284 extern int MM_GetRefCount(tPAddr PAddr);
286 * \brief Set the node associated with a page
287 * \param PAddr Physical address of page
288 * \param Node Node pointer (tVFS_Node)
289 * \return Boolean failure
291 * \retval 1 Page not allocated
293 extern int MM_SetPageNode(tPAddr PAddr, void *Node);
295 * \brief Get the node associated with a page
296 * \param PAddr Physical address of page
297 * \param Node Node pointer (tVFS_Node) destination
298 * \return Boolean failure
300 * \retval 1 Page not allocated
302 extern int MM_GetPageNode(tPAddr PAddr, void **Node);
307 // --- Memory Manipulation ---
309 * \name Memory Manipulation
312 extern int memcmp(const void *m1, const void *m2, size_t count);
313 extern void *memcpy(void *dest, const void *src, size_t count);
314 extern void *memcpyd(void *dest, const void *src, size_t count);
315 extern void *memmove(void *dest, const void *src, size_t len);
316 extern void *memset(void *dest, int val, size_t count);
317 extern void *memsetd(void *dest, Uint32 val, size_t count);
322 * \name Memory Validation
325 extern int CheckString(const char *String);
326 extern int CheckMem(const void *Mem, int Num);
331 // --- Endianness ---
333 * \name Endianness Swapping
336 #ifdef __BIG_ENDIAN__
337 #define LittleEndian16(_val) SwapEndian16(_val)
338 #define LittleEndian32(_val) SwapEndian32(_val)
339 #define LittleEndian64(_val) SwapEndian32(_val)
340 #define BigEndian16(_val) (_val)
341 #define BigEndian32(_val) (_val)
342 #define BigEndian64(_val) (_val)
344 #define LittleEndian16(_val) (_val)
345 #define LittleEndian32(_val) (_val)
346 #define LittleEndian64(_val) (_val)
347 #define BigEndian16(_val) SwapEndian16(_val)
348 #define BigEndian32(_val) SwapEndian32(_val)
349 #define BigEndian64(_val) SwapEndian64(_val)
351 extern Uint16 SwapEndian16(Uint16 Val);
352 extern Uint32 SwapEndian32(Uint32 Val);
353 extern Uint64 SwapEndian64(Uint64 Val);
359 #include <acess_string.h>
364 * \brief Get a random number
365 * \return Random number
366 * \note Current implementation is a linear congruency
368 extern int rand(void);
370 * \brief Call a function with a variable number of arguments
371 * \param Function Function address
372 * \param NArgs Number of entries in \a Args
373 * \param Args Array of arguments
374 * \return Integer from called Function
376 extern int CallWithArgArray(void *Function, int NArgs, Uint *Args);
381 * \brief Magic heap allocation function
383 extern void *alloca(size_t Size);
390 extern int Module_LoadMem(void *Buffer, Uint Length, const char *ArgStr);
391 extern int Module_LoadFile(const char *Path, const char *ArgStr);
398 * \name Time and Timing
402 * \brief Create a timestamp from a time
403 * \note Days/Months are zero-based (e.g. December is 11, and has a day range of 0-30)
405 extern tTime timestamp(int sec, int mins, int hrs, int day, int month, int year);
407 * \brief Extract the date/time from a timestamp
408 * \note Days/Months are zero-based (e.g. December is 11, and has a day range of 0-30)
410 extern void format_date(tTime TS, int *year, int *month, int *day, int *hrs, int *mins, int *sec, int *ms);
412 * \brief Gets the current timestamp (miliseconds since Midnight 1st January 1970)
414 extern Sint64 now(void);
421 * \name Threads and Processes
424 extern struct sThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data);
425 extern int Proc_Spawn(const char *Path);
426 extern int Proc_SysSpawn(const char *Binary, const char **ArgV, const char **EnvP, int nFD, int *FDs);
427 extern int Proc_Execve(const char *File, const char **ArgV, const char **EnvP, int DataSize);
428 extern void Threads_Exit(int TID, int Status);
429 extern void Threads_Yield(void);
430 extern void Threads_Sleep(void);
431 extern int Threads_WakeTID(tTID Thread);
432 extern tPGID Threads_GetPGID(void);
433 extern tPID Threads_GetPID(void);
434 extern tTID Threads_GetTID(void);
435 extern tUID Threads_GetUID(void);
436 extern tGID Threads_GetGID(void);
437 extern int SpawnTask(tThreadFunction Function, void *Arg);
438 extern int *Threads_GetErrno(void);
439 extern int Threads_SetName(const char *NewName);
444 // --- Simple Math ---
445 //! Divide and round up
446 extern int DivUp(int num, int dem);
447 //! Divide and Modulo 64-bit unsigned integer
448 extern Uint64 DivMod64U(Uint64 Num, Uint64 Den, Uint64 *Rem);
450 static inline int MIN(int a, int b) { return a < b ? a : b; }
451 static inline int MAX(int a, int b) { return a > b ? a : b; }
453 #include <binary_ext.h>