2 * AcessOS Microkernel Version
9 * \brief Acess2 Kernel API Core
13 #define NULL ((void*)0)
15 #define PACKED __attribute__((packed))
16 //! Mark a function as not returning
17 #define NORETURN __attribute__((noreturn))
18 //! Mark a function (or variable) as deprecated
19 #define DEPRECATED __attribute__((deprecated))
20 //! Mark a parameter as unused
21 #define UNUSED(x) UNUSED_##x __attribute__((unused))
22 //! Get the offset of a member in a structure
23 #define offsetof(st, m) ((Uint)((char *)&((st *)(0))->m - (char *)0 ))
26 * \name Boolean constants
40 typedef Uint32 tPID; //!< Process ID type
41 typedef Uint32 tTID; //!< Thread ID Type
42 typedef Uint32 tUID; //!< User ID Type
43 typedef Uint32 tGID; //!< Group ID Type
44 typedef Sint64 tTimestamp; //!< Timestamp (miliseconds since 00:00 1 Jan 1970)
45 typedef Sint64 tTime; //!< Same again
46 typedef struct sShortSpinlock tShortSpinlock; //!< Opaque (kinda) spinlock
47 typedef int bool; //!< Boolean type
48 typedef Uint64 off_t; //!< VFS Offset
50 // --- Helper Macros ---
55 #define CONCAT(x,y) x ## y
56 #define EXPAND_CONCAT(x,y) CONCAT(x,y)
58 #define EXPAND_STR(x) STR(x)
60 extern char __buildnum[];
61 #define BUILD_NUM ((int)(Uint)&__buildnum)
62 extern const char gsGitHash[];
64 #define VER2(major,minor) ((((major)&0xFF)<<8)|((minor)&0xFF))
69 //! \brief Error number
70 #define errno (*Threads_GetErrno())
73 // --- Memory Flags --
77 * \todo Move to mm_virt.h
79 #define MM_PFLAG_RO 0x01 // Writes disallowed
80 #define MM_PFLAG_EXEC 0x02 // Allow execution
81 #define MM_PFLAG_NOPAGE 0x04 // Prevent from being paged out
82 #define MM_PFLAG_COW 0x08 // Copy-On-Write
83 #define MM_PFLAG_KERNEL 0x10 // Kernel-Only (Ring0)
87 // --- Interface Flags & Macros
89 * \name Flags for Proc_Clone
92 //! Clone the entire process
94 //! Don't copy user pages
95 #define CLONE_NOUSER 0x20
102 * \brief Thread root function
104 * Function pointer prototype of a thread entrypoint. When it
105 * returns, Threads_Exit is called
107 typedef void (*tThreadFunction)(void*);
109 // === Kernel Export Macros ===
111 * \name Kernel exports
114 //! Kernel symbol definition
115 typedef struct sKernelSymbol {
116 const char *Name; //!< Symbolic name
117 tVAddr Value; //!< Value of the symbol
119 //! Export a pointer symbol (function/array)
120 #define EXPORT(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)_name}
121 //! Export a variable
122 #define EXPORTV(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)&_name}
123 //! Export a symbol under another name
124 #define EXPORTAS(_sym,_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)_sym}
132 * \name IRQ hander registration
135 extern int IRQ_AddHandler(int Num, void (*Callback)(int, void*), void *Ptr);
136 extern void IRQ_RemHandler(int Handle);
143 * \name Logging to kernel ring buffer
146 extern void Log_KernelPanic(const char *Ident, const char *Message, ...);
147 extern void Log_Panic(const char *Ident, const char *Message, ...);
148 extern void Log_Error(const char *Ident, const char *Message, ...);
149 extern void Log_Warning(const char *Ident, const char *Message, ...);
150 extern void Log_Notice(const char *Ident, const char *Message, ...);
151 extern void Log_Log(const char *Ident, const char *Message, ...);
152 extern void Log_Debug(const char *Ident, const char *Message, ...);
159 * \name Debugging and Errors
162 extern void Debug_KernelPanic(void); //!< Initiate a kernel panic
163 extern void Panic(const char *Msg, ...); //!< Print a panic message (initiates a kernel panic)
164 extern void Warning(const char *Msg, ...); //!< Print a warning message
165 extern void LogF(const char *Fmt, ...); //!< Print a log message without a trailing newline
166 extern void Log(const char *Fmt, ...); //!< Print a log message
167 extern void Debug(const char *Fmt, ...); //!< Print a debug message (doesn't go to KTerm)
168 extern void LogV(const char *Fmt, va_list Args); //!< va_list Log message
169 extern void Debug_Enter(const char *FuncName, const char *ArgTypes, ...);
170 extern void Debug_Log(const char *FuncName, const char *Fmt, ...);
171 extern void Debug_Leave(const char *FuncName, char RetType, ...);
172 extern void Debug_HexDump(const char *Header, const void *Data, Uint Length);
173 #define UNIMPLEMENTED() Warning("'%s' unimplemented", __func__)
175 # define ENTER(_types...) Debug_Enter((char*)__func__, _types)
176 # define LOG(_fmt...) Debug_Log((char*)__func__, _fmt)
177 # define LEAVE(_t...) Debug_Leave((char*)__func__, _t)
178 # define LEAVE_RET(_t,_v...) do{LEAVE(_t,_v);return _v;}while(0)
179 # define LEAVE_RET0() do{LEAVE('-');return;}while(0)
184 # define LEAVE_RET(_t,_v...) return (_v)
185 # define LEAVE_RET0() return
188 # define ASSERT(expr) do{if(!(expr))Panic("%s:%i - %s: Assertion '"#expr"' failed",__FILE__,__LINE__,(char*)__func__);}while(0)
190 # define ASSERT(expr)
198 #define inb(a) (Log_Panic("Arch", STR(ARCHDIR)" does not support in*/out* (%s:%i)", __FILE__, __LINE__),0)
199 #define inw(a) inb(a)
200 #define ind(a) inb(a)
201 #define inq(a) inb(a)
202 #define outb(a,b) inb(a)
203 #define outw(a,b) inb(a)
204 #define outd(a,b) inb(a)
205 #define outq(a,b) inb(a)
208 * \name I/O Memory Access
211 extern void outb(Uint16 Port, Uint8 Data);
212 extern void outw(Uint16 Port, Uint16 Data);
213 extern void outd(Uint16 Port, Uint32 Data);
214 extern void outq(Uint16 Port, Uint64 Data);
215 extern Uint8 inb(Uint16 Port);
216 extern Uint16 inw(Uint16 Port);
217 extern Uint32 ind(Uint16 Port);
218 extern Uint64 inq(Uint16 Port);
223 // --- Memory Management ---
225 * \name Memory Management
227 * \todo Move to mm_virt.h
230 * \brief Allocate a physical page at \a VAddr
231 * \param VAddr Virtual Address to allocate at
232 * \return Physical address allocated
234 extern tPAddr MM_Allocate(tVAddr VAddr) __attribute__ ((warn_unused_result));
236 * \brief Deallocate a page
237 * \param VAddr Virtual address to unmap
239 extern void MM_Deallocate(tVAddr VAddr);
241 * \brief Map a physical page at \a PAddr to \a VAddr
242 * \param VAddr Target virtual address
243 * \param PAddr Physical address to map
244 * \return Boolean Success
246 extern int MM_Map(tVAddr VAddr, tPAddr PAddr);
248 * \brief Get the physical address of \a Addr
249 * \param Addr Address of the page to get the physical address of
250 * \return Physical page mapped at \a Addr
252 extern tPAddr MM_GetPhysAddr(tVAddr Addr);
254 * \brief Set the access flags on a page
255 * \param VAddr Virtual address of the page
256 * \param Flags New flags value
257 * \param Mask Flags to set
259 extern void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
261 * \brief Get the flags on a flag
262 * \param VAddr Virtual address of page
263 * \return Flags value of the page
265 extern Uint MM_GetFlags(tVAddr VAddr);
267 * \brief Checks is a memory range is user accessable
268 * \param VAddr Base address to check
269 * \return 1 if the memory is all user-accessable, 0 otherwise
271 #define MM_IsUser(VAddr) (!(MM_GetFlags((VAddr))&MM_PFLAG_KERNEL))
273 * \brief Temporarily map a page into the address space
274 * \param PAddr Physical addres to map
275 * \return Virtual address of page in memory
276 * \note There is only a limited ammount of slots avaliable
278 extern tVAddr MM_MapTemp(tPAddr PAddr);
280 * \brief Free a temporarily mapped page
281 * \param VAddr Allocate virtual addres of page
283 extern void MM_FreeTemp(tVAddr VAddr);
285 * \brief Map a physcal address range into the virtual address space
286 * \param PAddr Physical address to map in
287 * \param Number Number of pages to map
289 extern tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number);
291 * \brief Allocates DMA physical memory
292 * \param Pages Number of pages required
293 * \param MaxBits Maximum number of bits the physical address can have
294 * \param PhysAddr Pointer to the location to place the physical address allocated
295 * \return Virtual address allocate
297 extern tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
299 * \brief Unmaps an allocated hardware range
300 * \param VAddr Virtual address allocate by ::MM_MapHWPages or ::MM_AllocDMA
301 * \param Number Number of pages to free
303 extern void MM_UnmapHWPages(tVAddr VAddr, Uint Number);
305 * \brief Allocate a single physical page
306 * \return Physical address allocated
308 extern tPAddr MM_AllocPhys(void);
310 * \brief Allocate a contiguous range of physical pages
311 * \param Pages Number of pages to allocate
312 * \param MaxBits Maximum number of address bits allowed
313 * \return First physical address allocated
315 extern tPAddr MM_AllocPhysRange(int Pages, int MaxBits);
317 * \brief Reference a physical page
318 * \param PAddr Page to mark as referenced
320 extern void MM_RefPhys(tPAddr PAddr);
322 * \brief Dereference a physical page
323 * \param PAddr Page to dereference
325 extern void MM_DerefPhys(tPAddr PAddr);
327 * \brief Get the number of times a page has been referenced
328 * \param PAddr Address to check
329 * \return Reference count for the page
331 extern int MM_GetRefCount(tPAddr PAddr);
333 * \brief Set the node associated with a page
334 * \param PAddr Physical address of page
335 * \param Node Node pointer (tVFS_Node)
336 * \return Boolean failure
338 * \retval 1 Page not allocated
340 extern int MM_SetPageNode(tPAddr PAddr, void *Node);
342 * \brief Get the node associated with a page
343 * \param PAddr Physical address of page
344 * \param Node Node pointer (tVFS_Node) destination
345 * \return Boolean failure
347 * \retval 1 Page not allocated
349 extern int MM_GetPageNode(tPAddr PAddr, void **Node);
354 // --- Memory Manipulation ---
356 * \name Memory Manipulation
359 extern int memcmp(const void *m1, const void *m2, size_t count);
360 extern void *memcpy(void *dest, const void *src, size_t count);
361 extern void *memcpyd(void *dest, const void *src, size_t count);
362 extern void *memmove(void *dest, const void *src, size_t len);
363 extern void *memset(void *dest, int val, size_t count);
364 extern void *memsetd(void *dest, Uint32 val, size_t count);
369 * \name Memory Validation
372 extern int CheckString(const char *String);
373 extern int CheckMem(const void *Mem, int Num);
378 // --- Endianness ---
380 * \name Endianness Swapping
383 #ifdef __BIG_ENDIAN__
384 #define LittleEndian16(_val) SwapEndian16(_val)
385 #define LittleEndian32(_val) SwapEndian32(_val)
386 #define BigEndian16(_val) (_val)
387 #define BigEndian32(_val) (_val)
389 #define LittleEndian16(_val) (_val)
390 #define LittleEndian32(_val) (_val)
391 #define BigEndian16(_val) SwapEndian16(_val)
392 #define BigEndian32(_val) SwapEndian32(_val)
394 extern Uint16 SwapEndian16(Uint16 Val);
395 extern Uint32 SwapEndian32(Uint32 Val);
405 extern int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args);
406 extern int snprintf(char *__s, size_t __n, const char *__format, ...);
407 extern int sprintf(char *__s, const char *__format, ...);
408 extern size_t strlen(const char *Str);
409 extern char *strcpy(char *__dest, const char *__src);
410 extern char *strncpy(char *__dest, const char *__src, size_t max);
411 extern char *strcat(char *__dest, const char *__src);
412 extern char *strncat(char *__dest, const char *__src, size_t n);
413 extern int strcmp(const char *__str1, const char *__str2);
414 extern int strncmp(const char *Str1, const char *Str2, size_t num);
415 extern int strucmp(const char *Str1, const char *Str2);
416 // strdup macro is defined in heap.h
417 extern char *_strdup(const char *File, int Line, const char *Str);
418 extern char **str_split(const char *__str, char __ch);
419 extern char *strchr(const char *__s, int __c);
420 extern int strpos(const char *Str, char Ch);
421 extern int strpos8(const char *str, Uint32 search);
422 extern void itoa(char *buf, Uint64 num, int base, int minLength, char pad);
423 extern int atoi(const char *string);
424 extern int ParseInt(const char *string, int *Val);
425 extern int ReadUTF8(const Uint8 *str, Uint32 *Val);
426 extern int WriteUTF8(Uint8 *str, Uint32 Val);
427 extern int ModUtil_SetIdent(char *Dest, const char *Value);
428 extern int ModUtil_LookupString(const char **Array, const char *Needle);
430 extern Uint8 ByteSum(const void *Ptr, int Size);
431 extern int Hex(char *Dest, size_t Size, const Uint8 *SourceData);
432 extern int UnHex(Uint8 *Dest, size_t DestSize, const char *SourceString);
438 * \brief Get a random number
439 * \return Random number
440 * \note Current implementation is a linear congruency
442 extern int rand(void);
444 * \brief Call a function with a variable number of arguments
445 * \param Function Function address
446 * \param NArgs Number of entries in \a Args
447 * \param Args Array of arguments
448 * \return Integer from called Function
450 extern int CallWithArgArray(void *Function, int NArgs, Uint *Args);
455 * \brief Magic heap allocation function
457 extern void *alloca(size_t Size);
464 extern int Module_LoadMem(void *Buffer, Uint Length, const char *ArgStr);
465 extern int Module_LoadFile(const char *Path, const char *ArgStr);
472 * \name Time and Timing
476 * \brief Create a timestamp from a time
478 extern tTime timestamp(int sec, int mins, int hrs, int day, int month, int year);
480 * \brief Extract the date/time from a timestamp
482 extern void format_date(tTime TS, int *year, int *month, int *day, int *hrs, int *mins, int *sec, int *ms);
484 * \brief Gets the current timestamp (miliseconds since Midnight 1st January 1970)
486 extern Sint64 now(void);
493 * \name Threads and Processes
496 extern int Proc_SpawnWorker(void (*Fcn)(void*), void *Data);
497 extern int Proc_Spawn(const char *Path);
498 extern int Proc_SysSpawn(const char *Binary, const char **ArgV, const char **EnvP, int nFD, int *FDs);
499 extern int Proc_Execve(const char *File, const char **ArgV, const char **EnvP, int DataSize);
500 extern void Threads_Exit(int TID, int Status);
501 extern void Threads_Yield(void);
502 extern void Threads_Sleep(void);
503 extern int Threads_WakeTID(tTID Thread);
504 extern tPID Threads_GetPID(void);
505 extern tTID Threads_GetTID(void);
506 extern tUID Threads_GetUID(void);
507 extern tGID Threads_GetGID(void);
508 extern int SpawnTask(tThreadFunction Function, void *Arg);
509 extern int *Threads_GetErrno(void);
510 extern int Threads_SetName(const char *NewName);
515 // --- Simple Math ---
516 //! Divide and round up
517 extern int DivUp(int num, int dem);
518 //! Divide and Modulo 64-bit unsigned integer
519 extern Uint64 DivMod64U(Uint64 Num, Uint64 Den, Uint64 *Rem);
521 static inline int MIN(int a, int b) { return a < b ? a : b; }
522 static inline int MAX(int a, int b) { return a > b ? a : b; }
524 #include <binary_ext.h>