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
57 // --- Helper Macros ---
62 #define CONCAT(x,y) x ## y
63 #define EXPAND_CONCAT(x,y) CONCAT(x,y)
65 #define EXPAND_STR(x) STR(x)
67 extern char __buildnum[];
68 #define BUILD_NUM ((int)(Uint)&__buildnum)
69 extern const char gsGitHash[];
70 extern const char gsBuildInfo[];
72 #define VER2(major,minor) ((((major)&0xFF)<<8)|((minor)&0xFF))
77 //! \brief Error number
78 #define errno (*Threads_GetErrno())
81 // --- Memory Flags --
85 * \todo Move to mm_virt.h
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)
95 // --- Interface Flags & Macros
97 * \name Flags for Proc_Clone
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
112 * \brief Thread root function
114 * Function pointer prototype of a thread entrypoint. When it
115 * returns, Threads_Exit is called
117 typedef void (*tThreadFunction)(void*);
119 // === Kernel Export Macros ===
121 * \name Kernel exports
124 //! Kernel symbol definition
125 typedef struct sKernelSymbol {
126 const char *Name; //!< Symbolic name
127 tVAddr Value; //!< Value of the symbol
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}
142 * \name IRQ hander registration
145 extern int IRQ_AddHandler(int Num, void (*Callback)(int, void*), void *Ptr);
146 extern void IRQ_RemHandler(int Handle);
151 #include "logdebug.h"
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)
165 * \name I/O Memory Access
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);
180 // --- Memory Management ---
182 * \name Memory Management
184 * \todo Move to mm_virt.h
187 * \brief Allocate a physical page at \a VAddr
188 * \param VAddr Virtual Address to allocate at
189 * \return Physical address allocated
191 extern tPAddr MM_Allocate(tVAddr VAddr) __attribute__ ((warn_unused_result));
193 * \brief Deallocate a page
194 * \param VAddr Virtual address to unmap
196 extern void MM_Deallocate(tVAddr VAddr);
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
203 extern int MM_Map(tVAddr VAddr, tPAddr PAddr);
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
209 extern tPAddr MM_GetPhysAddr(volatile const void *Addr);
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
216 extern void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
218 * \brief Get the flags on a flag
219 * \param VAddr Virtual address of page
220 * \return Flags value of the page
222 extern Uint MM_GetFlags(tVAddr VAddr);
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
228 #define MM_IsUser(VAddr) (!(MM_GetFlags((tVAddr)(VAddr))&MM_PFLAG_KERNEL))
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
235 extern void *MM_MapTemp(tPAddr PAddr);
237 * \brief Free a temporarily mapped page
238 * \param Ptr Pointer to page base
240 extern void MM_FreeTemp(void *Ptr);
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
246 extern void *MM_MapHWPages(tPAddr PAddr, Uint Number);
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
254 extern void *MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
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
260 extern void MM_UnmapHWPages(tVAddr VAddr, Uint Number);
262 * \brief Allocate a single physical page
263 * \return Physical address allocated
265 extern tPAddr MM_AllocPhys(void);
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
272 extern tPAddr MM_AllocPhysRange(int Pages, int MaxBits);
274 * \brief Reference a physical page
275 * \param PAddr Page to mark as referenced
277 extern void MM_RefPhys(tPAddr PAddr);
279 * \brief Dereference a physical page
280 * \param PAddr Page to dereference
282 extern void MM_DerefPhys(tPAddr PAddr);
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
288 extern int MM_GetRefCount(tPAddr PAddr);
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
295 * \retval 1 Page not allocated
297 extern int MM_SetPageNode(tPAddr PAddr, void *Node);
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
304 * \retval 1 Page not allocated
306 extern int MM_GetPageNode(tPAddr PAddr, void **Node);
311 // --- Memory Manipulation ---
313 * \name Memory Manipulation
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);
326 * \name Memory Validation
329 extern int CheckString(const char *String);
330 extern int CheckMem(const void *Mem, int Num);
335 // --- Endianness ---
337 * \name Endianness Swapping
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)
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)
355 extern Uint16 SwapEndian16(Uint16 Val);
356 extern Uint32 SwapEndian32(Uint32 Val);
357 extern Uint64 SwapEndian64(Uint64 Val);
363 #include <acess_string.h>
368 * \brief Get a random number
369 * \return Random number
370 * \note Current implementation is a linear congruency
372 extern int rand(void);
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
380 extern int CallWithArgArray(void *Function, int NArgs, Uint *Args);
385 * \brief Magic heap allocation function
387 extern void *alloca(size_t Size);
394 extern int Module_LoadMem(void *Buffer, Uint Length, const char *ArgStr);
395 extern int Module_LoadFile(const char *Path, const char *ArgStr);
402 * \name Time and Timing
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)
409 extern tTime timestamp(int sec, int mins, int hrs, int day, int month, int year);
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)
414 extern void format_date(tTime TS, int *year, int *month, int *day, int *hrs, int *mins, int *sec, int *ms);
416 * \brief Gets the current timestamp (miliseconds since Midnight 1st January 1970)
418 extern Sint64 now(void);
425 * \name Threads and Processes
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);
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);
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; }
457 #include <binary_ext.h>