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)
64 // === Kernel Export Macros ===
66 * \name Kernel Function
69 typedef struct sKernelSymbol {
73 #define EXPORT(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)_name}
74 #define EXPORTV(_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)&_name}
75 #define EXPORTAS(_sym,_name) tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)_sym}
82 extern void System_Init(char *ArgString);
85 extern int IRQ_AddHandler(int Num, void (*Callback)(int));
89 * \name Debugging and Errors
92 extern void Panic(char *Msg, ...);
93 extern void Warning(char *Msg, ...);
94 extern void Log(char *Fmt, ...);
95 extern void LogV(char *Fmt, va_list Args);
96 extern void LogF(char *Fmt, ...);
97 extern void Debug_Enter(char *FuncName, char *ArgTypes, ...);
98 extern void Debug_Log(char *FuncName, char *Fmt, ...);
99 extern void Debug_Leave(char *FuncName, char RetType, ...);
100 extern void Debug_HexDump(char *Header, void *Data, Uint Length);
102 # define ENTER(_types...) Debug_Enter((char*)__func__, _types)
103 # define LOG(_fmt...) Debug_Log((char*)__func__, _fmt)
104 # define LEAVE(_t...) Debug_Leave((char*)__func__, _t)
116 * \name I/O Memory Access
119 extern void outb(Uint16 Port, Uint8 Data);
120 extern void outw(Uint16 Port, Uint16 Data);
121 extern void outd(Uint16 Port, Uint32 Data);
122 extern void outq(Uint16 Port, Uint64 Data);
123 extern Uint8 inb(Uint16 Port);
124 extern Uint16 inw(Uint16 Port);
125 extern Uint32 ind(Uint16 Port);
126 extern Uint64 inq(Uint16 Port);
131 // --- Memory Management ---
133 * \name Memory Management
135 * \todo Move to mm_virt.h
138 * \brief Allocate a physical page at \a VAddr
139 * \param VAddr Virtual Address to allocate at
140 * \return Physical address allocated
142 extern tPAddr MM_Allocate(tVAddr VAddr);
144 * \brief Deallocate a page
145 * \param VAddr Virtual address to unmap
147 extern void MM_Deallocate(tVAddr VAddr);
149 * \brief Map a physical page at \a PAddr to \a VAddr
150 * \param VAddr Target virtual address
151 * \param PAddr Physical address to map
152 * \return Boolean Success
154 extern int MM_Map(tVAddr VAddr, tPAddr PAddr);
156 * \brief Get the physical address of \a VAddr
157 * \param VAddr Address of the page to get the physical address of
158 * \return Physical page mapped at \A VAddr
160 extern tPAddr MM_GetPhysAddr(tVAddr VAddr);
162 * \brief Checks is a memory range is user accessable
163 * \param VAddr Base address to check
164 * \param Length Number of bytes to check
165 * \return 1 if the memory is all user-accessable, 0 otherwise
167 extern int MM_IsUser(tVAddr VAddr, int Length);
169 * \brief Set the access flags on a page
170 * \param VAddr Virtual address of the page
171 * \param Flags New flags value
172 * \param Mask Flags to set
174 extern void MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
176 * \brief Temporarily map a page into the address space
177 * \param PAddr Physical addres to map
178 * \return Virtual address of page in memory
179 * \note There is only a limited ammount of slots avaliable
181 extern tVAddr MM_MapTemp(tPAddr PAddr);
183 * \brief Free a temporarily mapped page
184 * \param VAddr Allocate virtual addres of page
186 extern void MM_FreeTemp(tVAddr VAddr);
188 * \brief Map a physcal address range into the virtual address space
189 * \param PAddr Physical address to map in
190 * \param Number Number of pages to map
192 extern tVAddr MM_MapHWPage(tPAddr PAddr, Uint Number);
194 * \brief Allocates DMA physical memory
195 * \param Pages Number of pages required
196 * \param MaxBits Maximum number of bits the physical address can have
197 * \param PhysAddr Pointer to the location to place the physical address allocated
198 * \return Virtual address allocate
200 extern tVAddr MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
202 * \brief Unmaps an allocated hardware range
203 * \param VAddr Virtual address allocate by ::MM_MapHWPage or ::MM_AllocDMA
204 * \param Number Number of pages to free
206 extern void MM_UnmapHWPage(tVAddr VAddr, Uint Number);
208 * \brief Allocate a single physical page
209 * \return Physical address allocated
211 extern tPAddr MM_AllocPhys();
213 * \brief Allocate a contiguous range of physical pages
214 * \param Pages Number of pages to allocate
215 * \return First physical address allocated
217 extern tPAddr MM_AllocPhysRange(int Pages);
219 * \brief Reference a physical page
220 * \param PAddr Page to mark as referenced
222 extern void MM_RefPhys(tPAddr PAddr);
224 * \brief Dereference a physical page
225 * \param PAddr Page to dereference
227 extern void MM_DerefPhys(tPAddr PAddr);
232 // --- Memory Manipulation ---
234 * \name Memory Manipulation
237 extern int memcmp(const void *m1, const void *m2, Uint count);
238 extern void *memcpy(void *dest, const void *src, Uint count);
239 extern void *memcpyd(void *dest, const void *src, Uint count);
240 extern void *memset(void *dest, int val, Uint count);
241 extern void *memsetd(void *dest, Uint val, Uint count);
246 // --- Endianness ---
248 * \name Endianness Swapping
251 extern Uint16 LittleEndian16(Uint16 Val);
252 extern Uint16 BigEndian16(Uint16 Val);
253 extern Uint32 LittleEndian32(Uint32 Val);
254 extern Uint32 BigEndian32(Uint32 Val);
264 extern Uint strlen(const char *Str);
265 extern char *strcpy(char *__dest, const char *__src);
266 extern int strcmp(const char *__str1, const char *__str2);
267 extern int strncmp(const char *Str1, const char *Str2, size_t num);
268 extern int strucmp(const char *Str1, const char *Str2);
269 extern char *strdup(const char *Str);
270 extern int strpos(const char *Str, char Ch);
271 extern int strpos8(const char *str, Uint32 search);
272 extern void itoa(char *buf, Uint num, int base, int minLength, char pad);
273 extern int ReadUTF8(Uint8 *str, Uint32 *Val);
274 extern int WriteUTF8(Uint8 *str, Uint32 Val);
286 extern void *malloc(size_t size);
287 extern void *calloc(size_t num, size_t size);
288 extern void *realloc(void *ptr, size_t size);
289 extern void free(void *Ptr);
290 extern int IsHeap(void *Ptr);
300 extern int Module_LoadMem(void *Buffer, Uint Length, char *ArgStr);
301 extern int Module_LoadFile(char *Path, char *ArgStr);
308 * \name Time and Timing
311 extern Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year);
313 extern int Time_CreateTimer(int Delta, void *Callback, void *Argument);
314 extern void Time_RemoveTimer(int ID);
315 extern void Time_Delay(int Delay);
322 * \name Threads and Processes
325 extern int Proc_SpawnWorker();
326 extern int Proc_Spawn(char *Path);
327 extern void Threads_Exit();
328 extern void Threads_Yield();
329 extern void Threads_Sleep();
330 extern int Threads_GetUID();
331 extern int Threads_GetGID();
332 extern int SpawnTask(tThreadFunction Function, void *Arg);
333 extern Uint *Threads_GetCfgPtr(int Id);
338 // --- Simple Math ---
339 extern int DivUp(int num, int dem);
341 #include <binary_ext.h>