2 * AcessOS Microkernel Version
8 #define NULL ((void*)0)
9 #define PACKED __attribute__((packed))
10 #define UNUSED(x) UNUSED_##x __attribute__((unused))
11 #define offsetof(st, m) ((Uint)((char *)&((st *)(0))->m - (char *)0 ))
22 typedef Sint64 tTimestamp;
23 typedef struct sShortSpinlock tShortSpinlock;
24 typedef struct sMutex tMutex;
25 typedef struct sSemaphore tSemaphore;
28 tShortSpinlock Protector; //!< Protector for the lock strucure
29 const char *Name; //!< Human-readable name
30 struct sThread *volatile Owner; //!< Owner of the lock (set upon getting the lock)
31 struct sThread *Waiting; //!< Waiting threads
32 struct sThread *LastWaiting; //!< Waiting threads
36 tShortSpinlock Protector; //!< Protector for the lock strucure
37 const char *Name; //!< Human-readable name
38 volatile int Value; //!< Current mutex value
39 struct sThread *Waiting; //!< Waiting threads
40 struct sThread *LastWaiting; //!< Waiting threads
43 // --- Helper Macros ---
48 #define CONCAT(x,y) x ## y
49 #define EXPAND_CONCAT(x,y) CONCAT(x,y)
51 #define EXPAND_STR(x) STR(x)
53 #define VER2(major,minor) ((((major)&0xFF)<<8)|((minor)&0xFF))
59 * \name Per-Process Configuration Settings
74 #define CFGINT(id) (*Threads_GetCfgPtr(id))
75 #define CFGPTR(id) (*(void**)Threads_GetCfgPtr(id))
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
99 typedef void (*tThreadFunction)(void*);
101 // === Kernel Export Macros ===
103 * \name Kernel Function
106 typedef struct sKernelSymbol {
110 #define EXPORT(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)_name}
111 #define EXPORTV(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)&_name}
112 #define EXPORTAS(_sym,_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (tVAddr)_sym}
119 extern int IRQ_AddHandler(int Num, void (*Callback)(int));
122 extern void Log_KernelPanic(char *Ident, char *Message, ...);
123 extern void Log_Panic(char *Ident, char *Message, ...);
124 extern void Log_Error(char *Ident, char *Message, ...);
125 extern void Log_Warning(char *Ident, char *Message, ...);
126 extern void Log_Notice(char *Ident, char *Message, ...);
127 extern void Log_Log(char *Ident, char *Message, ...);
128 extern void Log_Debug(char *Ident, char *Message, ...);
132 * \name Debugging and Errors
135 extern void Debug_KernelPanic(void); //!< Initiate a kernel panic
136 extern void Panic(const char *Msg, ...); //!< Print a panic message (initiates a kernel panic)
137 extern void Warning(const char *Msg, ...); //!< Print a warning message
138 extern void LogF(const char *Fmt, ...); //!< Print a log message without a trailing newline
139 extern void Log(const char *Fmt, ...); //!< Print a log message
140 extern void Debug(const char *Fmt, ...); //!< Print a debug message (doesn't go to KTerm)
141 extern void LogV(const char *Fmt, va_list Args); //!< va_list Log message
142 extern void Debug_Enter(const char *FuncName, const char *ArgTypes, ...);
143 extern void Debug_Log(const char *FuncName, const char *Fmt, ...);
144 extern void Debug_Leave(const char *FuncName, char RetType, ...);
145 extern void Debug_HexDump(const char *Header, const void *Data, Uint Length);
146 #define UNIMPLEMENTED() Warning("'%s' unimplemented", __func__)
148 # define ENTER(_types...) Debug_Enter((char*)__func__, _types)
149 # define LOG(_fmt...) Debug_Log((char*)__func__, _fmt)
150 # define LEAVE(_t...) Debug_Leave((char*)__func__, _t)
151 # define LEAVE_RET(_t,_v...) do{LEAVE(_t,_v);return _v;}while(0)
152 # define LEAVE_RET0() do{LEAVE('-');return;}while(0)
157 # define LEAVE_RET(_t,_v...) return (_v)
158 # define LEAVE_RET0() return
161 # define ASSERT(expr) do{if(!(expr))Panic("%s: Assertion '"#expr"' failed",(char*)__func__);}while(0)
163 # define ASSERT(expr)
171 * \name I/O Memory Access
174 extern void outb(Uint16 Port, Uint8 Data);
175 extern void outw(Uint16 Port, Uint16 Data);
176 extern void outd(Uint16 Port, Uint32 Data);
177 extern void outq(Uint16 Port, Uint64 Data);
178 extern Uint8 inb(Uint16 Port);
179 extern Uint16 inw(Uint16 Port);
180 extern Uint32 ind(Uint16 Port);
181 extern Uint64 inq(Uint16 Port);
186 // --- Memory Management ---
188 * \name Memory Management
190 * \todo Move to mm_virt.h
193 * \brief Allocate a physical page at \a VAddr
194 * \param VAddr Virtual Address to allocate at
195 * \return Physical address allocated
197 extern tPAddr MM_Allocate(tVAddr VAddr);
199 * \brief Deallocate a page
200 * \param VAddr Virtual address to unmap
202 extern void MM_Deallocate(tVAddr VAddr);
204 * \brief Map a physical page at \a PAddr to \a VAddr
205 * \param VAddr Target virtual address
206 * \param PAddr Physical address to map
207 * \return Boolean Success
209 extern int MM_Map(tVAddr VAddr, tPAddr PAddr);
211 * \brief Get the physical address of \a Addr
212 * \param Addr Address of the page to get the physical address of
213 * \return Physical page mapped at \a Addr
215 extern tPAddr MM_GetPhysAddr(tVAddr Addr);
217 * \brief Set the access flags on a page
218 * \param VAddr Virtual address of the page
219 * \param Flags New flags value
220 * \param Mask Flags to set
222 extern void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
224 * \brief Get the flags on a flag
225 * \param VAddr Virtual address of page
226 * \return Flags value of the page
228 extern Uint MM_GetFlags(tVAddr VAddr);
230 * \brief Checks is a memory range is user accessable
231 * \param VAddr Base address to check
232 * \return 1 if the memory is all user-accessable, 0 otherwise
234 #define MM_IsUser(VAddr) (!(MM_GetFlags((VAddr))&MM_PFLAG_KERNEL))
236 * \brief Temporarily map a page into the address space
237 * \param PAddr Physical addres to map
238 * \return Virtual address of page in memory
239 * \note There is only a limited ammount of slots avaliable
241 extern tVAddr MM_MapTemp(tPAddr PAddr);
243 * \brief Free a temporarily mapped page
244 * \param VAddr Allocate virtual addres of page
246 extern void MM_FreeTemp(tVAddr VAddr);
248 * \brief Map a physcal address range into the virtual address space
249 * \param PAddr Physical address to map in
250 * \param Number Number of pages to map
252 extern tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number);
254 * \brief Allocates DMA physical memory
255 * \param Pages Number of pages required
256 * \param MaxBits Maximum number of bits the physical address can have
257 * \param PhysAddr Pointer to the location to place the physical address allocated
258 * \return Virtual address allocate
260 extern tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
262 * \brief Unmaps an allocated hardware range
263 * \param VAddr Virtual address allocate by ::MM_MapHWPage or ::MM_AllocDMA
264 * \param Number Number of pages to free
266 extern void MM_UnmapHWPages(tVAddr VAddr, Uint Number);
268 * \brief Allocate a single physical page
269 * \return Physical address allocated
271 extern tPAddr MM_AllocPhys(void);
273 * \brief Allocate a contiguous range of physical pages
274 * \param Pages Number of pages to allocate
275 * \param MaxBits Maximum number of address bits allowed
276 * \return First physical address allocated
278 extern tPAddr MM_AllocPhysRange(int Pages, int MaxBits);
280 * \brief Reference a physical page
281 * \param PAddr Page to mark as referenced
283 extern void MM_RefPhys(tPAddr PAddr);
285 * \brief Dereference a physical page
286 * \param PAddr Page to dereference
288 extern void MM_DerefPhys(tPAddr PAddr);
293 // --- Memory Manipulation ---
295 * \name Memory Manipulation
298 extern int memcmp(const void *m1, const void *m2, size_t count);
299 extern void *memcpy(void *dest, const void *src, size_t count);
300 extern void *memcpyd(void *dest, const void *src, size_t count);
301 extern void *memset(void *dest, int val, size_t count);
302 extern void *memsetd(void *dest, Uint32 val, size_t count);
307 * \name Memory Validation
310 extern int CheckString(char *String);
311 extern int CheckMem(void *Mem, int Num);
316 // --- Endianness ---
318 * \name Endianness Swapping
321 extern Uint16 LittleEndian16(Uint16 Val);
322 extern Uint16 BigEndian16(Uint16 Val);
323 extern Uint32 LittleEndian32(Uint32 Val);
324 extern Uint32 BigEndian32(Uint32 Val);
334 extern int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args);
335 extern int sprintf(char *__s, const char *__format, ...);
336 extern size_t strlen(const char *Str);
337 extern char *strcpy(char *__dest, const char *__src);
338 extern char *strncpy(char *__dest, const char *__src, size_t max);
339 extern int strcmp(const char *__str1, const char *__str2);
340 extern int strncmp(const char *Str1, const char *Str2, size_t num);
341 extern int strucmp(const char *Str1, const char *Str2);
342 //extern char *strdup(const char *Str);
343 #define strdup(Str) _strdup(_MODULE_NAME_"/"__FILE__, __LINE__, (Str))
344 extern char *_strdup(const char *File, int Line, const char *Str);
345 extern char **str_split(const char *__str, char __ch);
346 extern char *strchr(const char *__s, int __c);
347 extern int strpos(const char *Str, char Ch);
348 extern int strpos8(const char *str, Uint32 search);
349 extern void itoa(char *buf, Uint64 num, int base, int minLength, char pad);
350 extern int atoi(const char *string);
351 extern int ReadUTF8(Uint8 *str, Uint32 *Val);
352 extern int WriteUTF8(Uint8 *str, Uint32 Val);
353 extern int ModUtil_SetIdent(char *Dest, char *Value);
354 extern int ModUtil_LookupString(char **Array, char *Needle);
356 extern Uint8 ByteSum(void *Ptr, int Size);
357 extern int UnHex(Uint8 *Dest, size_t DestSize, const char *SourceString);
362 extern int rand(void);
363 extern int CallWithArgArray(void *Function, int NArgs, Uint *Args);
373 extern int Module_LoadMem(void *Buffer, Uint Length, char *ArgStr);
374 extern int Module_LoadFile(char *Path, char *ArgStr);
381 * \name Time and Timing
385 * \brief Create a timestamp from a time
387 extern Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year);
389 * \brief Gets the current timestamp (miliseconds since Midnight 1st January 1970)
391 extern Sint64 now(void);
393 * \brief Timer callback function
395 typedef void (tTimerCallback)(void *);
397 * \brief Creates a one-shot timer
398 * \param Delta Period of the timer
399 * \param Callback Function to call each time
400 * \param Argument Argument to pass to the callback
402 extern int Time_CreateTimer(int Delta, tTimerCallback *Callback, void *Argument);
404 * \brief Removed an active timer
406 extern void Time_RemoveTimer(int ID);
407 extern void Time_Delay(int Delay);
414 * \name Threads and Processes
417 extern int Proc_SpawnWorker(void);
418 extern int Proc_Spawn(char *Path);
419 extern void Threads_Exit(int TID, int Status);
420 extern void Threads_Yield(void);
421 extern void Threads_Sleep(void);
422 extern int Threads_WakeTID(tTID Thread);
423 extern tPID Threads_GetPID(void);
424 extern tTID Threads_GetTID(void);
425 extern tUID Threads_GetUID(void);
426 extern tGID Threads_GetGID(void);
427 extern int SpawnTask(tThreadFunction Function, void *Arg);
428 extern Uint *Threads_GetCfgPtr(int Id);
429 extern int Threads_SetName(const char *NewName);
430 extern void Mutex_Acquire(tMutex *Mutex);
431 extern void Mutex_Release(tMutex *Mutex);
432 extern int Mutex_IsLocked(tMutex *Mutex);
437 // --- Simple Math ---
438 extern int DivUp(int num, int dem);
440 #include <binary_ext.h>