2 * AcessOS Microkernel Version
8 #define NULL ((void*)0)
13 // --- Helper Macros ---
18 #define CONCAT(x,y) x ## y
19 #define EXPAND_CONCAT(x,y) CONCAT(x,y)
21 #define EXPAND_STR(x) STR(x)
27 * \name Per-Process Configuration Settings
42 #define CFGINT(id) (*Threads_GetCfgPtr(id))
43 #define CFGPTR(id) (*(void**)Threads_GetCfgPtr(id))
49 // --- Memory Flags --
53 * \todo Move to mm_virt.h
55 #define MM_PFLAG_RO 0x01 // Writes disallowed
56 #define MM_PFLAG_EXEC 0x02 // Allow execution
57 #define MM_PFLAG_NOPAGE 0x04 // Prevent from being paged out
58 #define MM_PFLAG_COW 0x08 // Copy-On-Write
59 #define MM_PFLAG_KERNEL 0x10 // Kernel-Only (Ring0)
63 // --- Interface Flags & Macros
67 typedef void (*tThreadFunction)(void*);
69 // === Kernel Export Macros ===
71 * \name Kernel Function
74 typedef struct sKernelSymbol {
78 #define EXPORT(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)_name}
79 #define EXPORTV(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)&_name}
80 #define EXPORTAS(_sym,_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)_sym}
87 extern void System_Init(char *ArgString);
90 extern int IRQ_AddHandler(int Num, void (*Callback)(int));
94 * \name Debugging and Errors
97 extern void Panic(char *Msg, ...);
98 extern void Warning(char *Msg, ...);
99 extern void Log(char *Fmt, ...);
100 extern void LogV(char *Fmt, va_list Args);
101 extern void LogF(char *Fmt, ...);
102 extern void Debug_Enter(char *FuncName, char *ArgTypes, ...);
103 extern void Debug_Log(char *FuncName, char *Fmt, ...);
104 extern void Debug_Leave(char *FuncName, char RetType, ...);
105 extern void Debug_HexDump(char *Header, void *Data, Uint Length);
107 # define ENTER(_types...) Debug_Enter((char*)__func__, _types)
108 # define LOG(_fmt...) Debug_Log((char*)__func__, _fmt)
109 # define LEAVE(_t...) Debug_Leave((char*)__func__, _t)
110 # define LEAVE_RET(_t,_v...) do{LEAVE(_t,_v);return _v;}while(0)
111 # define LEAVE_RET0() do{LEAVE('-');return;}while(0)
116 # define LEAVE_RET(_t,_v...) return (_v)
117 # define LEAVE_RET0() return
125 * \name I/O Memory Access
128 extern void outb(Uint16 Port, Uint8 Data);
129 extern void outw(Uint16 Port, Uint16 Data);
130 extern void outd(Uint16 Port, Uint32 Data);
131 extern void outq(Uint16 Port, Uint64 Data);
132 extern Uint8 inb(Uint16 Port);
133 extern Uint16 inw(Uint16 Port);
134 extern Uint32 ind(Uint16 Port);
135 extern Uint64 inq(Uint16 Port);
140 // --- Memory Management ---
142 * \name Memory Management
144 * \todo Move to mm_virt.h
147 * \brief Allocate a physical page at \a VAddr
148 * \param VAddr Virtual Address to allocate at
149 * \return Physical address allocated
151 extern tPAddr MM_Allocate(tVAddr VAddr);
153 * \brief Deallocate a page
154 * \param VAddr Virtual address to unmap
156 extern void MM_Deallocate(tVAddr VAddr);
158 * \brief Map a physical page at \a PAddr to \a VAddr
159 * \param VAddr Target virtual address
160 * \param PAddr Physical address to map
161 * \return Boolean Success
163 extern int MM_Map(tVAddr VAddr, tPAddr PAddr);
165 * \brief Get the physical address of \a VAddr
166 * \param VAddr Address of the page to get the physical address of
167 * \return Physical page mapped at \a VAddr
169 extern tPAddr MM_GetPhysAddr(tVAddr VAddr);
171 * \brief Checks is a memory range is user accessable
172 * \param VAddr Base address to check
173 * \return 1 if the memory is all user-accessable, 0 otherwise
175 extern int MM_IsUser(tVAddr VAddr);
177 * \brief Set the access flags on a page
178 * \param VAddr Virtual address of the page
179 * \param Flags New flags value
180 * \param Mask Flags to set
182 extern void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
184 * \brief Temporarily map a page into the address space
185 * \param PAddr Physical addres to map
186 * \return Virtual address of page in memory
187 * \note There is only a limited ammount of slots avaliable
189 extern tVAddr MM_MapTemp(tPAddr PAddr);
191 * \brief Free a temporarily mapped page
192 * \param VAddr Allocate virtual addres of page
194 extern void MM_FreeTemp(tVAddr VAddr);
196 * \brief Map a physcal address range into the virtual address space
197 * \param PAddr Physical address to map in
198 * \param Number Number of pages to map
200 extern tVAddr MM_MapHWPage(tPAddr PAddr, Uint Number);
202 * \brief Allocates DMA physical memory
203 * \param Pages Number of pages required
204 * \param MaxBits Maximum number of bits the physical address can have
205 * \param PhysAddr Pointer to the location to place the physical address allocated
206 * \return Virtual address allocate
208 extern tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
210 * \brief Unmaps an allocated hardware range
211 * \param VAddr Virtual address allocate by ::MM_MapHWPage or ::MM_AllocDMA
212 * \param Number Number of pages to free
214 extern void MM_UnmapHWPage(tVAddr VAddr, Uint Number);
216 * \brief Allocate a single physical page
217 * \return Physical address allocated
219 extern tPAddr MM_AllocPhys();
221 * \brief Allocate a contiguous range of physical pages
222 * \param Pages Number of pages to allocate
223 * \return First physical address allocated
225 extern tPAddr MM_AllocPhysRange(int Pages);
227 * \brief Reference a physical page
228 * \param PAddr Page to mark as referenced
230 extern void MM_RefPhys(tPAddr PAddr);
232 * \brief Dereference a physical page
233 * \param PAddr Page to dereference
235 extern void MM_DerefPhys(tPAddr PAddr);
240 // --- Memory Manipulation ---
242 * \name Memory Manipulation
245 extern int memcmp(const void *m1, const void *m2, Uint count);
246 extern void *memcpy(void *dest, const void *src, Uint count);
247 extern void *memcpyd(void *dest, const void *src, Uint count);
248 extern void *memset(void *dest, int val, Uint count);
249 extern void *memsetd(void *dest, Uint val, Uint count);
254 * \name Memory Validation
257 extern int CheckString(char *String);
258 extern int CheckMem(void *Mem, int Num);
263 // --- Endianness ---
265 * \name Endianness Swapping
268 extern Uint16 LittleEndian16(Uint16 Val);
269 extern Uint16 BigEndian16(Uint16 Val);
270 extern Uint32 LittleEndian32(Uint32 Val);
271 extern Uint32 BigEndian32(Uint32 Val);
281 extern Uint strlen(const char *Str);
282 extern char *strcpy(char *__dest, const char *__src);
283 extern int strcmp(const char *__str1, const char *__str2);
284 extern int strncmp(const char *Str1, const char *Str2, size_t num);
285 extern int strucmp(const char *Str1, const char *Str2);
286 extern char *strdup(const char *Str);
287 extern int strpos(const char *Str, char Ch);
288 extern int strpos8(const char *str, Uint32 search);
289 extern void itoa(char *buf, Uint num, int base, int minLength, char pad);
290 extern int ReadUTF8(Uint8 *str, Uint32 *Val);
291 extern int WriteUTF8(Uint8 *str, Uint32 Val);
292 extern int LookupString(char **Array, char *Needle);
304 extern void *malloc(size_t size);
305 extern void *calloc(size_t num, size_t size);
306 extern void *realloc(void *ptr, size_t size);
307 extern void free(void *Ptr);
308 extern int IsHeap(void *Ptr);
318 extern int Module_LoadMem(void *Buffer, Uint Length, char *ArgStr);
319 extern int Module_LoadFile(char *Path, char *ArgStr);
326 * \name Time and Timing
329 extern Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year);
331 extern int Time_CreateTimer(int Delta, void *Callback, void *Argument);
332 extern void Time_RemoveTimer(int ID);
333 extern void Time_Delay(int Delay);
340 * \name Threads and Processes
343 extern int Proc_SpawnWorker();
344 extern int Proc_Spawn(char *Path);
345 extern void Threads_Exit();
346 extern void Threads_Yield();
347 extern void Threads_Sleep();
348 extern int Threads_GetUID();
349 extern int Threads_GetGID();
350 extern int SpawnTask(tThreadFunction Function, void *Arg);
351 extern Uint *Threads_GetCfgPtr(int Id);
356 // --- Simple Math ---
357 extern int DivUp(int num, int dem);
359 #include <binary_ext.h>