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;
27 tShortSpinlock Protector; //!< Protector for the lock strucure
28 struct sThread *volatile Owner; //!< Owner of the lock (set upon getting the lock)
29 struct sThread *Waiting; //!< Waiting threads
30 struct sThread *LastWaiting; //!< Waiting threads
33 // --- Helper Macros ---
38 #define CONCAT(x,y) x ## y
39 #define EXPAND_CONCAT(x,y) CONCAT(x,y)
41 #define EXPAND_STR(x) STR(x)
43 #define VER2(major,minor) ((((major)&0xFF)<<8)|((minor)&0xFF))
49 * \name Per-Process Configuration Settings
64 #define CFGINT(id) (*Threads_GetCfgPtr(id))
65 #define CFGPTR(id) (*(void**)Threads_GetCfgPtr(id))
71 // --- Memory Flags --
75 * \todo Move to mm_virt.h
77 #define MM_PFLAG_RO 0x01 // Writes disallowed
78 #define MM_PFLAG_EXEC 0x02 // Allow execution
79 #define MM_PFLAG_NOPAGE 0x04 // Prevent from being paged out
80 #define MM_PFLAG_COW 0x08 // Copy-On-Write
81 #define MM_PFLAG_KERNEL 0x10 // Kernel-Only (Ring0)
85 // --- Interface Flags & Macros
89 typedef void (*tThreadFunction)(void*);
91 // === Kernel Export Macros ===
93 * \name Kernel Function
96 typedef struct sKernelSymbol {
100 #define EXPORT(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)_name}
101 #define EXPORTV(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)&_name}
102 #define EXPORTAS(_sym,_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)_sym}
109 extern int IRQ_AddHandler(int Num, void (*Callback)(int));
112 extern void Log_KernelPanic(char *Ident, char *Message, ...);
113 extern void Log_Panic(char *Ident, char *Message, ...);
114 extern void Log_Error(char *Ident, char *Message, ...);
115 extern void Log_Warning(char *Ident, char *Message, ...);
116 extern void Log_Notice(char *Ident, char *Message, ...);
117 extern void Log_Log(char *Ident, char *Message, ...);
118 extern void Log_Debug(char *Ident, char *Message, ...);
122 * \name Debugging and Errors
125 extern void Debug_KernelPanic(void); //!< Initiate a kernel panic
126 extern void Panic(char *Msg, ...); //!< Print a panic message (initiates a kernel panic)
127 extern void Warning(char *Msg, ...); //!< Print a warning message
128 extern void LogF(char *Fmt, ...); //!< Print a log message without a trailing newline
129 extern void Log(char *Fmt, ...); //!< Print a log message
130 extern void Debug(char *Fmt, ...); //!< Print a debug message (doesn't go to KTerm)
131 extern void LogV(char *Fmt, va_list Args); //!< va_list Log message
132 extern void Debug_Enter(char *FuncName, char *ArgTypes, ...);
133 extern void Debug_Log(char *FuncName, char *Fmt, ...);
134 extern void Debug_Leave(char *FuncName, char RetType, ...);
135 extern void Debug_HexDump(char *Header, void *Data, Uint Length);
136 #define UNIMPLEMENTED() Warning("'%s' unimplemented", __func__)
138 # define ENTER(_types...) Debug_Enter((char*)__func__, _types)
139 # define LOG(_fmt...) Debug_Log((char*)__func__, _fmt)
140 # define LEAVE(_t...) Debug_Leave((char*)__func__, _t)
141 # define LEAVE_RET(_t,_v...) do{LEAVE(_t,_v);return _v;}while(0)
142 # define LEAVE_RET0() do{LEAVE('-');return;}while(0)
147 # define LEAVE_RET(_t,_v...) return (_v)
148 # define LEAVE_RET0() return
151 # define ASSERT(expr) do{if(!(expr))Panic("%s: Assertion '"#expr"' failed",(char*)__func__);}while(0)
153 # define ASSERT(expr)
161 * \name I/O Memory Access
164 extern void outb(Uint16 Port, Uint8 Data);
165 extern void outw(Uint16 Port, Uint16 Data);
166 extern void outd(Uint16 Port, Uint32 Data);
167 extern void outq(Uint16 Port, Uint64 Data);
168 extern Uint8 inb(Uint16 Port);
169 extern Uint16 inw(Uint16 Port);
170 extern Uint32 ind(Uint16 Port);
171 extern Uint64 inq(Uint16 Port);
176 // --- Memory Management ---
178 * \name Memory Management
180 * \todo Move to mm_virt.h
183 * \brief Allocate a physical page at \a VAddr
184 * \param VAddr Virtual Address to allocate at
185 * \return Physical address allocated
187 extern tPAddr MM_Allocate(tVAddr VAddr);
189 * \brief Deallocate a page
190 * \param VAddr Virtual address to unmap
192 extern void MM_Deallocate(tVAddr VAddr);
194 * \brief Map a physical page at \a PAddr to \a VAddr
195 * \param VAddr Target virtual address
196 * \param PAddr Physical address to map
197 * \return Boolean Success
199 extern int MM_Map(tVAddr VAddr, tPAddr PAddr);
201 * \brief Get the physical address of \a Addr
202 * \param Addr Address of the page to get the physical address of
203 * \return Physical page mapped at \a Addr
205 extern tPAddr MM_GetPhysAddr(tVAddr Addr);
207 * \brief Set the access flags on a page
208 * \param VAddr Virtual address of the page
209 * \param Flags New flags value
210 * \param Mask Flags to set
212 extern void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
214 * \brief Get the flags on a flag
215 * \param VAddr Virtual address of page
216 * \return Flags value of the page
218 extern Uint MM_GetFlags(tVAddr VAddr);
220 * \brief Checks is a memory range is user accessable
221 * \param VAddr Base address to check
222 * \return 1 if the memory is all user-accessable, 0 otherwise
224 #define MM_IsUser(VAddr) (!(MM_GetFlags((VAddr))&MM_PFLAG_KERNEL))
226 * \brief Temporarily map a page into the address space
227 * \param PAddr Physical addres to map
228 * \return Virtual address of page in memory
229 * \note There is only a limited ammount of slots avaliable
231 extern tVAddr MM_MapTemp(tPAddr PAddr);
233 * \brief Free a temporarily mapped page
234 * \param VAddr Allocate virtual addres of page
236 extern void MM_FreeTemp(tVAddr VAddr);
238 * \brief Map a physcal address range into the virtual address space
239 * \param PAddr Physical address to map in
240 * \param Number Number of pages to map
242 extern tVAddr MM_MapHWPages(tPAddr PAddr, Uint Number);
244 * \brief Allocates DMA physical memory
245 * \param Pages Number of pages required
246 * \param MaxBits Maximum number of bits the physical address can have
247 * \param PhysAddr Pointer to the location to place the physical address allocated
248 * \return Virtual address allocate
250 extern tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
252 * \brief Unmaps an allocated hardware range
253 * \param VAddr Virtual address allocate by ::MM_MapHWPage or ::MM_AllocDMA
254 * \param Number Number of pages to free
256 extern void MM_UnmapHWPages(tVAddr VAddr, Uint Number);
258 * \brief Allocate a single physical page
259 * \return Physical address allocated
261 extern tPAddr MM_AllocPhys(void);
263 * \brief Allocate a contiguous range of physical pages
264 * \param Pages Number of pages to allocate
265 * \param MaxBits Maximum number of address bits allowed
266 * \return First physical address allocated
268 extern tPAddr MM_AllocPhysRange(int Pages, int MaxBits);
270 * \brief Reference a physical page
271 * \param PAddr Page to mark as referenced
273 extern void MM_RefPhys(tPAddr PAddr);
275 * \brief Dereference a physical page
276 * \param PAddr Page to dereference
278 extern void MM_DerefPhys(tPAddr PAddr);
283 // --- Memory Manipulation ---
285 * \name Memory Manipulation
288 extern int memcmp(const void *m1, const void *m2, size_t count);
289 extern void *memcpy(void *dest, const void *src, size_t count);
290 extern void *memcpyd(void *dest, const void *src, size_t count);
291 extern void *memset(void *dest, int val, size_t count);
292 extern void *memsetd(void *dest, Uint32 val, size_t count);
297 * \name Memory Validation
300 extern int CheckString(char *String);
301 extern int CheckMem(void *Mem, int Num);
306 // --- Endianness ---
308 * \name Endianness Swapping
311 extern Uint16 LittleEndian16(Uint16 Val);
312 extern Uint16 BigEndian16(Uint16 Val);
313 extern Uint32 LittleEndian32(Uint32 Val);
314 extern Uint32 BigEndian32(Uint32 Val);
324 extern int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args);
325 extern int sprintf(char *__s, const char *__format, ...);
326 extern Uint strlen(const char *Str);
327 extern char *strcpy(char *__dest, const char *__src);
328 extern char *strncpy(char *__dest, const char *__src, size_t max);
329 extern int strcmp(const char *__str1, const char *__str2);
330 extern int strncmp(const char *Str1, const char *Str2, size_t num);
331 extern int strucmp(const char *Str1, const char *Str2);
332 //extern char *strdup(const char *Str);
333 #define strdup(Str) _strdup(_MODULE_NAME_"/"__FILE__, __LINE__, (Str))
334 extern char *_strdup(const char *File, int Line, const char *Str);
335 extern char **str_split(const char *__str, char __ch);
336 extern int strpos(const char *Str, char Ch);
337 extern int strpos8(const char *str, Uint32 search);
338 extern void itoa(char *buf, Uint num, int base, int minLength, char pad);
339 extern int atoi(const char *string);
340 extern int ReadUTF8(Uint8 *str, Uint32 *Val);
341 extern int WriteUTF8(Uint8 *str, Uint32 Val);
342 extern int ModUtil_SetIdent(char *Dest, char *Value);
343 extern int ModUtil_LookupString(char **Array, char *Needle);
344 extern Uint8 ByteSum(void *Ptr, int Size);
349 extern Uint rand(void);
350 extern int CallWithArgArray(void *Function, int NArgs, Uint *Args);
360 extern int Module_LoadMem(void *Buffer, Uint Length, char *ArgStr);
361 extern int Module_LoadFile(char *Path, char *ArgStr);
368 * \name Time and Timing
372 * \brief Create a timestamp from a time
374 extern Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year);
376 * \brief Gets the current timestamp (miliseconds since Midnight 1st January 1970)
378 extern Sint64 now(void);
380 * \brief Timer callback function
382 typedef void (tTimerCallback)(void *);
384 * \brief Creates a one-shot timer
385 * \param Delta Period of the timer
386 * \param Callback Function to call each time
387 * \param Argument Argument to pass to the callback
389 extern int Time_CreateTimer(int Delta, tTimerCallback *Callback, void *Argument);
391 * \brief Removed an active timer
393 extern void Time_RemoveTimer(int ID);
394 extern void Time_Delay(int Delay);
401 * \name Threads and Processes
404 extern int Proc_SpawnWorker(void);
405 extern int Proc_Spawn(char *Path);
406 extern void Threads_Exit(int TID, int Status);
407 extern void Threads_Yield(void);
408 extern void Threads_Sleep(void);
409 extern int Threads_WakeTID(tTID Thread);
410 extern tPID Threads_GetPID(void);
411 extern tTID Threads_GetTID(void);
412 extern tUID Threads_GetUID(void);
413 extern tGID Threads_GetGID(void);
414 extern int SpawnTask(tThreadFunction Function, void *Arg);
415 extern Uint *Threads_GetCfgPtr(int Id);
416 extern int Threads_SetName(char *NewName);
417 extern void Mutex_Acquire(tMutex *Mutex);
418 extern void Mutex_Release(tMutex *Mutex);
419 extern int Mutex_IsLocked(tMutex *Mutex);
424 // --- Simple Math ---
425 extern int DivUp(int num, int dem);
427 #include <binary_ext.h>