2 * AcessOS Microkernel Version
8 #define NULL ((void*)0)
9 #define PACKED __attribute__((packed))
10 #define NORETURN __attribute__((noreturn))
11 #define UNUSED(x) UNUSED_##x __attribute__((unused))
12 #define offsetof(st, m) ((Uint)((char *)&((st *)(0))->m - (char *)0 ))
26 typedef Sint64 tTimestamp;
28 typedef struct sShortSpinlock tShortSpinlock;
31 // --- Helper Macros ---
36 #define CONCAT(x,y) x ## y
37 #define EXPAND_CONCAT(x,y) CONCAT(x,y)
39 #define EXPAND_STR(x) STR(x)
41 extern char __buildnum[];
42 #define BUILD_NUM ((int)(Uint)&__buildnum)
43 extern const char gsGitHash[];
45 #define VER2(major,minor) ((((major)&0xFF)<<8)|((minor)&0xFF))
51 * \name Per-Thread Configuration Settings
67 #define CFGINT(id) (*Threads_GetCfgPtr(id))
68 #define CFGPTR(id) (*(void**)Threads_GetCfgPtr(id))
70 #define errno (CFGINT(CFG_ERRNO))
76 // --- Memory Flags --
80 * \todo Move to mm_virt.h
82 #define MM_PFLAG_RO 0x01 // Writes disallowed
83 #define MM_PFLAG_EXEC 0x02 // Allow execution
84 #define MM_PFLAG_NOPAGE 0x04 // Prevent from being paged out
85 #define MM_PFLAG_COW 0x08 // Copy-On-Write
86 #define MM_PFLAG_KERNEL 0x10 // Kernel-Only (Ring0)
90 // --- Interface Flags & Macros
94 typedef void (*tThreadFunction)(void*);
96 // === Kernel Export Macros ===
98 * \name Kernel Function
101 typedef struct sKernelSymbol {
105 #define EXPORT(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)_name}
106 #define EXPORTV(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)&_name}
107 #define EXPORTAS(_sym,_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)_sym}
114 extern int IRQ_AddHandler(int Num, void (*Callback)(int, void*), void *Ptr);
115 extern void IRQ_RemHandler(int Handle);
118 extern void Log_KernelPanic(const char *Ident, const char *Message, ...);
119 extern void Log_Panic(const char *Ident, const char *Message, ...);
120 extern void Log_Error(const char *Ident, const char *Message, ...);
121 extern void Log_Warning(const char *Ident, const char *Message, ...);
122 extern void Log_Notice(const char *Ident, const char *Message, ...);
123 extern void Log_Log(const char *Ident, const char *Message, ...);
124 extern void Log_Debug(const char *Ident, const char *Message, ...);
128 * \name Debugging and Errors
131 extern void Debug_KernelPanic(void); //!< Initiate a kernel panic
132 extern void Panic(const char *Msg, ...); //!< Print a panic message (initiates a kernel panic)
133 extern void Warning(const char *Msg, ...); //!< Print a warning message
134 extern void LogF(const char *Fmt, ...); //!< Print a log message without a trailing newline
135 extern void Log(const char *Fmt, ...); //!< Print a log message
136 extern void Debug(const char *Fmt, ...); //!< Print a debug message (doesn't go to KTerm)
137 extern void LogV(const char *Fmt, va_list Args); //!< va_list Log message
138 extern void Debug_Enter(const char *FuncName, const char *ArgTypes, ...);
139 extern void Debug_Log(const char *FuncName, const char *Fmt, ...);
140 extern void Debug_Leave(const char *FuncName, char RetType, ...);
141 extern void Debug_HexDump(const char *Header, const void *Data, Uint Length);
142 #define UNIMPLEMENTED() Warning("'%s' unimplemented", __func__)
144 # define ENTER(_types...) Debug_Enter((char*)__func__, _types)
145 # define LOG(_fmt...) Debug_Log((char*)__func__, _fmt)
146 # define LEAVE(_t...) Debug_Leave((char*)__func__, _t)
147 # define LEAVE_RET(_t,_v...) do{LEAVE(_t,_v);return _v;}while(0)
148 # define LEAVE_RET0() do{LEAVE('-');return;}while(0)
153 # define LEAVE_RET(_t,_v...) return (_v)
154 # define LEAVE_RET0() return
157 # define ASSERT(expr) do{if(!(expr))Panic("%s: Assertion '"#expr"' failed",(char*)__func__);}while(0)
159 # define ASSERT(expr)
167 #define inb(a) (Log_Panic("Arch", "ARMv7 does not support in*/out* (%s:%i)", __FILE__, __LINE__),0)
168 #define inw(a) inb(a)
169 #define ind(a) inb(a)
170 #define inq(a) inb(a)
171 #define outb(a,b) inb(a)
172 #define outw(a,b) inb(a)
173 #define outd(a,b) inb(a)
174 #define outq(a,b) inb(a)
177 * \name I/O Memory Access
180 extern void outb(Uint16 Port, Uint8 Data);
181 extern void outw(Uint16 Port, Uint16 Data);
182 extern void outd(Uint16 Port, Uint32 Data);
183 extern void outq(Uint16 Port, Uint64 Data);
184 extern Uint8 inb(Uint16 Port);
185 extern Uint16 inw(Uint16 Port);
186 extern Uint32 ind(Uint16 Port);
187 extern Uint64 inq(Uint16 Port);
192 // --- Memory Management ---
194 * \name Memory Management
196 * \todo Move to mm_virt.h
199 * \brief Allocate a physical page at \a VAddr
200 * \param VAddr Virtual Address to allocate at
201 * \return Physical address allocated
203 extern tPAddr MM_Allocate(tVAddr VAddr) __attribute__ ((warn_unused_result));
205 * \brief Deallocate a page
206 * \param VAddr Virtual address to unmap
208 extern void MM_Deallocate(tVAddr VAddr);
210 * \brief Map a physical page at \a PAddr to \a VAddr
211 * \param VAddr Target virtual address
212 * \param PAddr Physical address to map
213 * \return Boolean Success
215 extern int MM_Map(tVAddr VAddr, tPAddr PAddr);
217 * \brief Get the physical address of \a Addr
218 * \param Addr Address of the page to get the physical address of
219 * \return Physical page mapped at \a Addr
221 extern tPAddr MM_GetPhysAddr(tVAddr Addr);
223 * \brief Set the access flags on a page
224 * \param VAddr Virtual address of the page
225 * \param Flags New flags value
226 * \param Mask Flags to set
228 extern void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
230 * \brief Get the flags on a flag
231 * \param VAddr Virtual address of page
232 * \return Flags value of the page
234 extern Uint MM_GetFlags(tVAddr VAddr);
236 * \brief Checks is a memory range is user accessable
237 * \param VAddr Base address to check
238 * \return 1 if the memory is all user-accessable, 0 otherwise
240 #define MM_IsUser(VAddr) (!(MM_GetFlags((VAddr))&MM_PFLAG_KERNEL))
242 * \brief Temporarily map a page into the address space
243 * \param PAddr Physical addres to map
244 * \return Virtual address of page in memory
245 * \note There is only a limited ammount of slots avaliable
247 extern tVAddr MM_MapTemp(tPAddr PAddr);
249 * \brief Free a temporarily mapped page
250 * \param VAddr Allocate virtual addres of page
252 extern void MM_FreeTemp(tVAddr VAddr);
254 * \brief Map a physcal address range into the virtual address space
255 * \param PAddr Physical address to map in
256 * \param Number Number of pages to map
258 extern tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number);
260 * \brief Allocates DMA physical memory
261 * \param Pages Number of pages required
262 * \param MaxBits Maximum number of bits the physical address can have
263 * \param PhysAddr Pointer to the location to place the physical address allocated
264 * \return Virtual address allocate
266 extern tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
268 * \brief Unmaps an allocated hardware range
269 * \param VAddr Virtual address allocate by ::MM_MapHWPage or ::MM_AllocDMA
270 * \param Number Number of pages to free
272 extern void MM_UnmapHWPages(tVAddr VAddr, Uint Number);
274 * \brief Allocate a single physical page
275 * \return Physical address allocated
277 extern tPAddr MM_AllocPhys(void);
279 * \brief Allocate a contiguous range of physical pages
280 * \param Pages Number of pages to allocate
281 * \param MaxBits Maximum number of address bits allowed
282 * \return First physical address allocated
284 extern tPAddr MM_AllocPhysRange(int Pages, int MaxBits);
286 * \brief Reference a physical page
287 * \param PAddr Page to mark as referenced
289 extern void MM_RefPhys(tPAddr PAddr);
291 * \brief Dereference a physical page
292 * \param PAddr Page to dereference
294 extern void MM_DerefPhys(tPAddr PAddr);
296 * \brief Get the number of times a page has been referenced
297 * \param PAddr Address to check
298 * \return Reference count for the page
300 extern int MM_GetRefCount(tPAddr PAddr);
302 * \brief Set the node associated with a page
303 * \param PAddr Physical address of page
304 * \param Node Node pointer (tVFS_Node)
305 * \return Boolean failure
307 * \retval 1 Page not allocated
309 extern int MM_SetPageNode(tPAddr PAddr, void *Node);
311 * \brief Get the node associated with a page
312 * \param PAddr Physical address of page
313 * \param Node Node pointer (tVFS_Node) destination
314 * \return Boolean failure
316 * \retval 1 Page not allocated
318 extern int MM_GetPageNode(tPAddr PAddr, void **Node);
323 // --- Memory Manipulation ---
325 * \name Memory Manipulation
328 extern int memcmp(const void *m1, const void *m2, size_t count);
329 extern void *memcpy(void *dest, const void *src, size_t count);
330 extern void *memcpyd(void *dest, const void *src, size_t count);
331 extern void *memset(void *dest, int val, size_t count);
332 extern void *memsetd(void *dest, Uint32 val, size_t count);
337 * \name Memory Validation
340 extern int CheckString(const char *String);
341 extern int CheckMem(const void *Mem, int Num);
346 // --- Endianness ---
348 * \name Endianness Swapping
351 #ifdef __BIG_ENDIAN__
352 #define LittleEndian16(_val) SwapEndian16(_val)
353 #define LittleEndian32(_val) SwapEndian32(_val)
354 #define BigEndian16(_val) (_val)
355 #define BigEndian32(_val) (_val)
357 #define LittleEndian16(_val) (_val)
358 #define LittleEndian32(_val) (_val)
359 #define BigEndian16(_val) SwapEndian16(_val)
360 #define BigEndian32(_val) SwapEndian32(_val)
362 extern Uint16 SwapEndian16(Uint16 Val);
363 extern Uint32 SwapEndian32(Uint32 Val);
373 extern int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args);
374 extern int sprintf(char *__s, const char *__format, ...);
375 extern size_t strlen(const char *Str);
376 extern char *strcpy(char *__dest, const char *__src);
377 extern char *strncpy(char *__dest, const char *__src, size_t max);
378 extern char *strcat(char *__dest, const char *__src);
379 extern char *strncat(char *__dest, const char *__src, size_t n);
380 extern int strcmp(const char *__str1, const char *__str2);
381 extern int strncmp(const char *Str1, const char *Str2, size_t num);
382 extern int strucmp(const char *Str1, const char *Str2);
383 // strdup macro is defined in heap.h
384 extern char *_strdup(const char *File, int Line, const char *Str);
385 extern char **str_split(const char *__str, char __ch);
386 extern char *strchr(const char *__s, int __c);
387 extern int strpos(const char *Str, char Ch);
388 extern int strpos8(const char *str, Uint32 search);
389 extern void itoa(char *buf, Uint64 num, int base, int minLength, char pad);
390 extern int atoi(const char *string);
391 extern int ReadUTF8(const Uint8 *str, Uint32 *Val);
392 extern int WriteUTF8(Uint8 *str, Uint32 Val);
393 extern int ModUtil_SetIdent(char *Dest, const char *Value);
394 extern int ModUtil_LookupString(const char **Array, const char *Needle);
396 extern Uint8 ByteSum(const void *Ptr, int Size);
397 extern int UnHex(Uint8 *Dest, size_t DestSize, const char *SourceString);
402 extern int rand(void);
403 extern int CallWithArgArray(void *Function, int NArgs, Uint *Args);
413 extern int Module_LoadMem(void *Buffer, Uint Length, const char *ArgStr);
414 extern int Module_LoadFile(const char *Path, const char *ArgStr);
421 * \name Time and Timing
425 * \brief Create a timestamp from a time
427 extern Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year);
429 * \brief Gets the current timestamp (miliseconds since Midnight 1st January 1970)
431 extern Sint64 now(void);
433 * \brief Timer callback function
435 typedef void (tTimerCallback)(void *);
437 * \brief Creates a one-shot timer
438 * \param Delta Period of the timer
439 * \param Callback Function to call each time
440 * \param Argument Argument to pass to the callback
442 extern int Time_CreateTimer(int Delta, tTimerCallback *Callback, void *Argument);
444 * \brief Removed an active timer
446 extern void Time_RemoveTimer(int ID);
448 * \brief Wait for a period of milliseconds
450 extern void Time_Delay(int Delay);
457 * \name Threads and Processes
460 extern int Proc_SpawnWorker(void (*Fcn)(void*), void *Data);
461 extern int Proc_Spawn(const char *Path);
462 extern void Threads_Exit(int TID, int Status);
463 extern void Threads_Yield(void);
464 extern void Threads_Sleep(void);
465 extern int Threads_WakeTID(tTID Thread);
466 extern tPID Threads_GetPID(void);
467 extern tTID Threads_GetTID(void);
468 extern tUID Threads_GetUID(void);
469 extern tGID Threads_GetGID(void);
470 extern int SpawnTask(tThreadFunction Function, void *Arg);
471 extern Uint *Threads_GetCfgPtr(int Id);
472 extern int Threads_SetName(const char *NewName);
477 // --- Simple Math ---
478 extern int DivUp(int num, int dem);
479 extern Uint64 DivMod64U(Uint64 Num, Uint64 Den, Uint64 *Rem);
481 #include <binary_ext.h>