2 * AcessOS Microkernel Version
8 #define NULL ((void*)0)
9 #define PACKED __attribute__ ((packed))
18 typedef Sint64 tTimestamp;
20 // --- Helper Macros ---
25 #define CONCAT(x,y) x ## y
26 #define EXPAND_CONCAT(x,y) CONCAT(x,y)
28 #define EXPAND_STR(x) STR(x)
30 #define VER2(major,minor) ((((major)&0xFF)<<8)|((minor)&0xFF))
36 * \name Per-Process Configuration Settings
51 #define CFGINT(id) (*Threads_GetCfgPtr(id))
52 #define CFGPTR(id) (*(void**)Threads_GetCfgPtr(id))
58 // --- Memory Flags --
62 * \todo Move to mm_virt.h
64 #define MM_PFLAG_RO 0x01 // Writes disallowed
65 #define MM_PFLAG_EXEC 0x02 // Allow execution
66 #define MM_PFLAG_NOPAGE 0x04 // Prevent from being paged out
67 #define MM_PFLAG_COW 0x08 // Copy-On-Write
68 #define MM_PFLAG_KERNEL 0x10 // Kernel-Only (Ring0)
72 // --- Interface Flags & Macros
76 typedef void (*tThreadFunction)(void*);
78 // === Kernel Export Macros ===
80 * \name Kernel Function
83 typedef struct sKernelSymbol {
87 #define EXPORT(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)_name}
88 #define EXPORTV(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)&_name}
89 #define EXPORTAS(_sym,_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)_sym}
96 extern void System_Init(char *ArgString);
99 extern int IRQ_AddHandler(int Num, void (*Callback)(int));
103 * \name Debugging and Errors
106 extern void Panic(char *Msg, ...);
107 extern void Warning(char *Msg, ...);
108 extern void Log(char *Fmt, ...);
109 extern void LogV(char *Fmt, va_list Args);
110 extern void LogF(char *Fmt, ...);
111 extern void Debug_Enter(char *FuncName, char *ArgTypes, ...);
112 extern void Debug_Log(char *FuncName, char *Fmt, ...);
113 extern void Debug_Leave(char *FuncName, char RetType, ...);
114 extern void Debug_HexDump(char *Header, void *Data, Uint Length);
115 #define UNIMPLEMENTED() Warning("'%s' unimplemented", __func__)
117 # define ENTER(_types...) Debug_Enter((char*)__func__, _types)
118 # define LOG(_fmt...) Debug_Log((char*)__func__, _fmt)
119 # define LEAVE(_t...) Debug_Leave((char*)__func__, _t)
120 # define LEAVE_RET(_t,_v...) do{LEAVE(_t,_v);return _v;}while(0)
121 # define LEAVE_RET0() do{LEAVE('-');return;}while(0)
126 # define LEAVE_RET(_t,_v...) return (_v)
127 # define LEAVE_RET0() return
130 # define ASSERT(expr) do{if(!(expr))Panic("%s: Assertion '"#expr"' failed",(char*)__func__);}while(0)
132 # define ASSERT(expr)
140 * \name I/O Memory Access
143 extern void outb(Uint16 Port, Uint8 Data);
144 extern void outw(Uint16 Port, Uint16 Data);
145 extern void outd(Uint16 Port, Uint32 Data);
146 extern void outq(Uint16 Port, Uint64 Data);
147 extern Uint8 inb(Uint16 Port);
148 extern Uint16 inw(Uint16 Port);
149 extern Uint32 ind(Uint16 Port);
150 extern Uint64 inq(Uint16 Port);
155 // --- Memory Management ---
157 * \name Memory Management
159 * \todo Move to mm_virt.h
162 * \brief Allocate a physical page at \a VAddr
163 * \param VAddr Virtual Address to allocate at
164 * \return Physical address allocated
166 extern tPAddr MM_Allocate(tVAddr VAddr);
168 * \brief Deallocate a page
169 * \param VAddr Virtual address to unmap
171 extern void MM_Deallocate(tVAddr VAddr);
173 * \brief Map a physical page at \a PAddr to \a VAddr
174 * \param VAddr Target virtual address
175 * \param PAddr Physical address to map
176 * \return Boolean Success
178 extern int MM_Map(tVAddr VAddr, tPAddr PAddr);
180 * \brief Get the physical address of \a VAddr
181 * \param VAddr Address of the page to get the physical address of
182 * \return Physical page mapped at \a VAddr
184 extern tPAddr MM_GetPhysAddr(tVAddr VAddr);
186 * \brief Checks is a memory range is user accessable
187 * \param VAddr Base address to check
188 * \return 1 if the memory is all user-accessable, 0 otherwise
190 extern int MM_IsUser(tVAddr VAddr);
192 * \brief Set the access flags on a page
193 * \param VAddr Virtual address of the page
194 * \param Flags New flags value
195 * \param Mask Flags to set
197 extern void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
199 * \brief Temporarily map a page into the address space
200 * \param PAddr Physical addres to map
201 * \return Virtual address of page in memory
202 * \note There is only a limited ammount of slots avaliable
204 extern tVAddr MM_MapTemp(tPAddr PAddr);
206 * \brief Free a temporarily mapped page
207 * \param VAddr Allocate virtual addres of page
209 extern void MM_FreeTemp(tVAddr VAddr);
211 * \brief Map a physcal address range into the virtual address space
212 * \param PAddr Physical address to map in
213 * \param Number Number of pages to map
215 extern tVAddr MM_MapHWPage(tPAddr PAddr, Uint Number);
217 * \brief Allocates DMA physical memory
218 * \param Pages Number of pages required
219 * \param MaxBits Maximum number of bits the physical address can have
220 * \param PhysAddr Pointer to the location to place the physical address allocated
221 * \return Virtual address allocate
223 extern tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
225 * \brief Unmaps an allocated hardware range
226 * \param VAddr Virtual address allocate by ::MM_MapHWPage or ::MM_AllocDMA
227 * \param Number Number of pages to free
229 extern void MM_UnmapHWPage(tVAddr VAddr, Uint Number);
231 * \brief Allocate a single physical page
232 * \return Physical address allocated
234 extern tPAddr MM_AllocPhys();
236 * \brief Allocate a contiguous range of physical pages
237 * \param Pages Number of pages to allocate
238 * \return First physical address allocated
240 extern tPAddr MM_AllocPhysRange(int Pages, int MaxBits);
242 * \brief Reference a physical page
243 * \param PAddr Page to mark as referenced
245 extern void MM_RefPhys(tPAddr PAddr);
247 * \brief Dereference a physical page
248 * \param PAddr Page to dereference
250 extern void MM_DerefPhys(tPAddr PAddr);
255 // --- Memory Manipulation ---
257 * \name Memory Manipulation
260 extern int memcmp(const void *m1, const void *m2, Uint count);
261 extern void *memcpy(void *dest, const void *src, Uint count);
262 extern void *memcpyd(void *dest, const void *src, Uint count);
263 extern void *memset(void *dest, int val, Uint count);
264 extern void *memsetd(void *dest, Uint val, Uint count);
269 * \name Memory Validation
272 extern int CheckString(char *String);
273 extern int CheckMem(void *Mem, int Num);
278 // --- Endianness ---
280 * \name Endianness Swapping
283 extern Uint16 LittleEndian16(Uint16 Val);
284 extern Uint16 BigEndian16(Uint16 Val);
285 extern Uint32 LittleEndian32(Uint32 Val);
286 extern Uint32 BigEndian32(Uint32 Val);
296 extern Uint strlen(const char *Str);
297 extern char *strcpy(char *__dest, const char *__src);
298 extern int strcmp(const char *__str1, const char *__str2);
299 extern int strncmp(const char *Str1, const char *Str2, size_t num);
300 extern int strucmp(const char *Str1, const char *Str2);
301 extern char *strdup(const char *Str);
302 extern int strpos(const char *Str, char Ch);
303 extern int strpos8(const char *str, Uint32 search);
304 extern void itoa(char *buf, Uint num, int base, int minLength, char pad);
305 extern int ReadUTF8(Uint8 *str, Uint32 *Val);
306 extern int WriteUTF8(Uint8 *str, Uint32 Val);
307 extern int LookupString(char **Array, char *Needle);
308 extern Uint8 ByteSum(void *Ptr, int Size);
320 extern void *malloc(size_t size);
321 extern void *calloc(size_t num, size_t size);
322 extern void *realloc(void *ptr, size_t size);
323 extern void free(void *Ptr);
324 extern int IsHeap(void *Ptr);
334 extern int Module_LoadMem(void *Buffer, Uint Length, char *ArgStr);
335 extern int Module_LoadFile(char *Path, char *ArgStr);
342 * \name Time and Timing
346 * \brief Create a timestamp from a time
348 extern Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year);
350 extern int Time_CreateTimer(int Delta, void *Callback, void *Argument);
351 extern void Time_RemoveTimer(int ID);
352 extern void Time_Delay(int Delay);
359 * \name Threads and Processes
362 extern int Proc_SpawnWorker();
363 extern int Proc_Spawn(char *Path);
364 extern void Threads_Exit();
365 extern void Threads_Yield();
366 extern void Threads_Sleep();
367 extern tUID Threads_GetUID();
368 extern tGID Threads_GetGID();
369 extern int SpawnTask(tThreadFunction Function, void *Arg);
370 extern Uint *Threads_GetCfgPtr(int Id);
375 // --- Simple Math ---
376 extern int DivUp(int num, int dem);
378 #include <binary_ext.h>