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 parameter as unused
19 #define UNUSED(x) UNUSED_##x __attribute__((unused))
20 //! Get the offset of a member in a structure
21 #define offsetof(st, m) ((Uint)((char *)&((st *)(0))->m - (char *)0 ))
24 * \name Boolean constants
38 typedef Uint32 tPID; //!< Process ID type
39 typedef Uint32 tTID; //!< Thread ID Type
40 typedef Uint32 tUID; //!< User ID Type
41 typedef Uint32 tGID; //!< Group ID Type
42 typedef Sint64 tTimestamp; //!< Timestamp (miliseconds since 00:00 1 Jan 1970)
43 typedef Sint64 tTime; //!< Same again
44 typedef struct sShortSpinlock tShortSpinlock; //!< Opaque (kinda) spinlock
45 typedef int bool; //!< Boolean type
47 // --- Helper Macros ---
52 #define CONCAT(x,y) x ## y
53 #define EXPAND_CONCAT(x,y) CONCAT(x,y)
55 #define EXPAND_STR(x) STR(x)
57 extern char __buildnum[];
58 #define BUILD_NUM ((int)(Uint)&__buildnum)
59 extern const char gsGitHash[];
61 #define VER2(major,minor) ((((major)&0xFF)<<8)|((minor)&0xFF))
66 //! \brief Error number
67 #define errno (*Threads_GetErrno())
70 // --- Memory Flags --
74 * \todo Move to mm_virt.h
76 #define MM_PFLAG_RO 0x01 // Writes disallowed
77 #define MM_PFLAG_EXEC 0x02 // Allow execution
78 #define MM_PFLAG_NOPAGE 0x04 // Prevent from being paged out
79 #define MM_PFLAG_COW 0x08 // Copy-On-Write
80 #define MM_PFLAG_KERNEL 0x10 // Kernel-Only (Ring0)
84 // --- Interface Flags & Macros
86 * \name Flags for Proc_Clone
89 //! Clone the entire process
97 * \brief Thread root function
99 * Function pointer prototype of a thread entrypoint. When it
100 * returns, Threads_Exit is called
102 typedef void (*tThreadFunction)(void*);
104 // === Kernel Export Macros ===
106 * \name Kernel exports
109 //! Kernel symbol definition
110 typedef struct sKernelSymbol {
111 const char *Name; //!< Symbolic name
112 tVAddr Value; //!< Value of the symbol
114 //! Export a pointer symbol (function/array)
115 #define EXPORT(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)_name}
116 //! Export a variable
117 #define EXPORTV(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)&_name}
118 //! Export a symbol under another name
119 #define EXPORTAS(_sym,_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)_sym}
127 * \name IRQ hander registration
130 extern int IRQ_AddHandler(int Num, void (*Callback)(int, void*), void *Ptr);
131 extern void IRQ_RemHandler(int Handle);
138 * \name Logging to kernel ring buffer
141 extern void Log_KernelPanic(const char *Ident, const char *Message, ...);
142 extern void Log_Panic(const char *Ident, const char *Message, ...);
143 extern void Log_Error(const char *Ident, const char *Message, ...);
144 extern void Log_Warning(const char *Ident, const char *Message, ...);
145 extern void Log_Notice(const char *Ident, const char *Message, ...);
146 extern void Log_Log(const char *Ident, const char *Message, ...);
147 extern void Log_Debug(const char *Ident, const char *Message, ...);
154 * \name Debugging and Errors
157 extern void Debug_KernelPanic(void); //!< Initiate a kernel panic
158 extern void Panic(const char *Msg, ...); //!< Print a panic message (initiates a kernel panic)
159 extern void Warning(const char *Msg, ...); //!< Print a warning message
160 extern void LogF(const char *Fmt, ...); //!< Print a log message without a trailing newline
161 extern void Log(const char *Fmt, ...); //!< Print a log message
162 extern void Debug(const char *Fmt, ...); //!< Print a debug message (doesn't go to KTerm)
163 extern void LogV(const char *Fmt, va_list Args); //!< va_list Log message
164 extern void Debug_Enter(const char *FuncName, const char *ArgTypes, ...);
165 extern void Debug_Log(const char *FuncName, const char *Fmt, ...);
166 extern void Debug_Leave(const char *FuncName, char RetType, ...);
167 extern void Debug_HexDump(const char *Header, const void *Data, Uint Length);
168 #define UNIMPLEMENTED() Warning("'%s' unimplemented", __func__)
170 # define ENTER(_types...) Debug_Enter((char*)__func__, _types)
171 # define LOG(_fmt...) Debug_Log((char*)__func__, _fmt)
172 # define LEAVE(_t...) Debug_Leave((char*)__func__, _t)
173 # define LEAVE_RET(_t,_v...) do{LEAVE(_t,_v);return _v;}while(0)
174 # define LEAVE_RET0() do{LEAVE('-');return;}while(0)
179 # define LEAVE_RET(_t,_v...) return (_v)
180 # define LEAVE_RET0() return
183 # define ASSERT(expr) do{if(!(expr))Panic("%s: Assertion '"#expr"' failed",(char*)__func__);}while(0)
185 # define ASSERT(expr)
193 #define inb(a) (Log_Panic("Arch", STR(ARCHDIR)" does not support in*/out* (%s:%i)", __FILE__, __LINE__),0)
194 #define inw(a) inb(a)
195 #define ind(a) inb(a)
196 #define inq(a) inb(a)
197 #define outb(a,b) inb(a)
198 #define outw(a,b) inb(a)
199 #define outd(a,b) inb(a)
200 #define outq(a,b) inb(a)
203 * \name I/O Memory Access
206 extern void outb(Uint16 Port, Uint8 Data);
207 extern void outw(Uint16 Port, Uint16 Data);
208 extern void outd(Uint16 Port, Uint32 Data);
209 extern void outq(Uint16 Port, Uint64 Data);
210 extern Uint8 inb(Uint16 Port);
211 extern Uint16 inw(Uint16 Port);
212 extern Uint32 ind(Uint16 Port);
213 extern Uint64 inq(Uint16 Port);
218 // --- Memory Management ---
220 * \name Memory Management
222 * \todo Move to mm_virt.h
225 * \brief Allocate a physical page at \a VAddr
226 * \param VAddr Virtual Address to allocate at
227 * \return Physical address allocated
229 extern tPAddr MM_Allocate(tVAddr VAddr) __attribute__ ((warn_unused_result));
231 * \brief Deallocate a page
232 * \param VAddr Virtual address to unmap
234 extern void MM_Deallocate(tVAddr VAddr);
236 * \brief Map a physical page at \a PAddr to \a VAddr
237 * \param VAddr Target virtual address
238 * \param PAddr Physical address to map
239 * \return Boolean Success
241 extern int MM_Map(tVAddr VAddr, tPAddr PAddr);
243 * \brief Get the physical address of \a Addr
244 * \param Addr Address of the page to get the physical address of
245 * \return Physical page mapped at \a Addr
247 extern tPAddr MM_GetPhysAddr(tVAddr Addr);
249 * \brief Set the access flags on a page
250 * \param VAddr Virtual address of the page
251 * \param Flags New flags value
252 * \param Mask Flags to set
254 extern void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
256 * \brief Get the flags on a flag
257 * \param VAddr Virtual address of page
258 * \return Flags value of the page
260 extern Uint MM_GetFlags(tVAddr VAddr);
262 * \brief Checks is a memory range is user accessable
263 * \param VAddr Base address to check
264 * \return 1 if the memory is all user-accessable, 0 otherwise
266 #define MM_IsUser(VAddr) (!(MM_GetFlags((VAddr))&MM_PFLAG_KERNEL))
268 * \brief Temporarily map a page into the address space
269 * \param PAddr Physical addres to map
270 * \return Virtual address of page in memory
271 * \note There is only a limited ammount of slots avaliable
273 extern tVAddr MM_MapTemp(tPAddr PAddr);
275 * \brief Free a temporarily mapped page
276 * \param VAddr Allocate virtual addres of page
278 extern void MM_FreeTemp(tVAddr VAddr);
280 * \brief Map a physcal address range into the virtual address space
281 * \param PAddr Physical address to map in
282 * \param Number Number of pages to map
284 extern tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number);
286 * \brief Allocates DMA physical memory
287 * \param Pages Number of pages required
288 * \param MaxBits Maximum number of bits the physical address can have
289 * \param PhysAddr Pointer to the location to place the physical address allocated
290 * \return Virtual address allocate
292 extern tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
294 * \brief Unmaps an allocated hardware range
295 * \param VAddr Virtual address allocate by ::MM_MapHWPages or ::MM_AllocDMA
296 * \param Number Number of pages to free
298 extern void MM_UnmapHWPages(tVAddr VAddr, Uint Number);
300 * \brief Allocate a single physical page
301 * \return Physical address allocated
303 extern tPAddr MM_AllocPhys(void);
305 * \brief Allocate a contiguous range of physical pages
306 * \param Pages Number of pages to allocate
307 * \param MaxBits Maximum number of address bits allowed
308 * \return First physical address allocated
310 extern tPAddr MM_AllocPhysRange(int Pages, int MaxBits);
312 * \brief Reference a physical page
313 * \param PAddr Page to mark as referenced
315 extern void MM_RefPhys(tPAddr PAddr);
317 * \brief Dereference a physical page
318 * \param PAddr Page to dereference
320 extern void MM_DerefPhys(tPAddr PAddr);
322 * \brief Get the number of times a page has been referenced
323 * \param PAddr Address to check
324 * \return Reference count for the page
326 extern int MM_GetRefCount(tPAddr PAddr);
328 * \brief Set the node associated with a page
329 * \param PAddr Physical address of page
330 * \param Node Node pointer (tVFS_Node)
331 * \return Boolean failure
333 * \retval 1 Page not allocated
335 extern int MM_SetPageNode(tPAddr PAddr, void *Node);
337 * \brief Get the node associated with a page
338 * \param PAddr Physical address of page
339 * \param Node Node pointer (tVFS_Node) destination
340 * \return Boolean failure
342 * \retval 1 Page not allocated
344 extern int MM_GetPageNode(tPAddr PAddr, void **Node);
349 // --- Memory Manipulation ---
351 * \name Memory Manipulation
354 extern int memcmp(const void *m1, const void *m2, size_t count);
355 extern void *memcpy(void *dest, const void *src, size_t count);
356 extern void *memcpyd(void *dest, const void *src, size_t count);
357 extern void *memmove(void *dest, const void *src, size_t len);
358 extern void *memset(void *dest, int val, size_t count);
359 extern void *memsetd(void *dest, Uint32 val, size_t count);
364 * \name Memory Validation
367 extern int CheckString(const char *String);
368 extern int CheckMem(const void *Mem, int Num);
373 // --- Endianness ---
375 * \name Endianness Swapping
378 #ifdef __BIG_ENDIAN__
379 #define LittleEndian16(_val) SwapEndian16(_val)
380 #define LittleEndian32(_val) SwapEndian32(_val)
381 #define BigEndian16(_val) (_val)
382 #define BigEndian32(_val) (_val)
384 #define LittleEndian16(_val) (_val)
385 #define LittleEndian32(_val) (_val)
386 #define BigEndian16(_val) SwapEndian16(_val)
387 #define BigEndian32(_val) SwapEndian32(_val)
389 extern Uint16 SwapEndian16(Uint16 Val);
390 extern Uint32 SwapEndian32(Uint32 Val);
400 extern int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args);
401 extern int sprintf(char *__s, const char *__format, ...);
402 extern size_t strlen(const char *Str);
403 extern char *strcpy(char *__dest, const char *__src);
404 extern char *strncpy(char *__dest, const char *__src, size_t max);
405 extern char *strcat(char *__dest, const char *__src);
406 extern char *strncat(char *__dest, const char *__src, size_t n);
407 extern int strcmp(const char *__str1, const char *__str2);
408 extern int strncmp(const char *Str1, const char *Str2, size_t num);
409 extern int strucmp(const char *Str1, const char *Str2);
410 // strdup macro is defined in heap.h
411 extern char *_strdup(const char *File, int Line, const char *Str);
412 extern char **str_split(const char *__str, char __ch);
413 extern char *strchr(const char *__s, int __c);
414 extern int strpos(const char *Str, char Ch);
415 extern int strpos8(const char *str, Uint32 search);
416 extern void itoa(char *buf, Uint64 num, int base, int minLength, char pad);
417 extern int atoi(const char *string);
418 extern int ReadUTF8(const Uint8 *str, Uint32 *Val);
419 extern int WriteUTF8(Uint8 *str, Uint32 Val);
420 extern int ModUtil_SetIdent(char *Dest, const char *Value);
421 extern int ModUtil_LookupString(const char **Array, const char *Needle);
423 extern Uint8 ByteSum(const void *Ptr, int Size);
424 extern int UnHex(Uint8 *Dest, size_t DestSize, const char *SourceString);
430 * \brief Get a random number
431 * \return Random number
432 * \note Current implementation is a linear congruency
434 extern int rand(void);
436 * \brief Call a function with a variable number of arguments
437 * \param Function Function address
438 * \param NArgs Number of entries in \a Args
439 * \param Args Array of arguments
440 * \return Integer from called Function
442 extern int CallWithArgArray(void *Function, int NArgs, Uint *Args);
452 extern int Module_LoadMem(void *Buffer, Uint Length, const char *ArgStr);
453 extern int Module_LoadFile(const char *Path, const char *ArgStr);
460 * \name Time and Timing
464 * \brief Create a timestamp from a time
466 extern Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year);
468 * \brief Gets the current timestamp (miliseconds since Midnight 1st January 1970)
470 extern Sint64 now(void);
472 * \brief Timer callback function
474 typedef void (tTimerCallback)(void *);
476 * \brief Creates a one-shot timer
477 * \param Delta Period of the timer
478 * \param Callback Function to call each time
479 * \param Argument Argument to pass to the callback
481 extern int Time_CreateTimer(int Delta, tTimerCallback *Callback, void *Argument);
483 * \brief Removed an active timer
485 extern void Time_RemoveTimer(int ID);
487 * \brief Wait for a period of milliseconds
489 extern void Time_Delay(int Delay);
496 * \name Threads and Processes
499 extern int Proc_SpawnWorker(void (*Fcn)(void*), void *Data);
500 extern int Proc_Spawn(const char *Path);
501 extern void Threads_Exit(int TID, int Status);
502 extern void Threads_Yield(void);
503 extern void Threads_Sleep(void);
504 extern int Threads_WakeTID(tTID Thread);
505 extern tPID Threads_GetPID(void);
506 extern tTID Threads_GetTID(void);
507 extern tUID Threads_GetUID(void);
508 extern tGID Threads_GetGID(void);
509 extern int SpawnTask(tThreadFunction Function, void *Arg);
510 extern int *Threads_GetErrno(void);
511 extern int Threads_SetName(const char *NewName);
516 // --- Simple Math ---
517 //! Divide and round up
518 extern int DivUp(int num, int dem);
519 //! Divide and Modulo 64-bit unsigned integer
520 extern Uint64 DivMod64U(Uint64 Num, Uint64 Den, Uint64 *Rem);
522 static inline int MIN(int a, int b) { return a < b ? a : b; }
523 static inline int MAX(int a, int b) { return a > b ? a : b; }
525 #include <binary_ext.h>