Cleanup and Bugfixing
[tpg/acess2.git] / Kernel / include / common.h
1 /*
2  * AcessOS Microkernel Version
3  * common.h
4  */
5 #ifndef _COMMON_H
6 #define _COMMON_H
7
8 #define NULL    ((void*)0)
9
10 #include <arch.h>
11 #include <stdarg.h>
12
13 // --- Helper Macros ---
14 /**
15  * \name Helper Macros
16  * \{
17  */
18 #define CONCAT(x,y) x ## y
19 #define EXPAND_CONCAT(x,y) CONCAT(x,y)
20 #define STR(x) #x
21 #define EXPAND_STR(x) STR(x)
22 /**
23  * \}
24  */
25
26 /**
27  * \name Per-Process Configuration Settings
28  * \{
29  */
30 enum eConfigTypes {
31         CFGT_NULL,
32         CFGT_INT,
33         CFGT_HEAPSTR,
34         CFGT_PTR
35 };
36 enum eConfigs {
37         CFG_VFS_CWD,
38         CFG_VFS_MAXFILES,
39         CFG_VFS_CHROOT,
40         NUM_CFG_ENTRIES
41 };
42 #define CFGINT(id)      (*Threads_GetCfgPtr(id))
43 #define CFGPTR(id)      (*(void**)Threads_GetCfgPtr(id))
44 /**
45  * \}
46  */
47
48 // === CONSTANTS ===
49 // --- Memory Flags --
50 /**
51  * \name Memory Flags
52  * \{
53  * \todo Move to mm_virt.h
54  */
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)
60 /**
61  * \}
62  */
63
64 // === Kernel Export Macros ===
65 /**
66  * \name Kernel Function 
67  * \{
68  */
69 typedef struct sKernelSymbol {
70         char    *Name;
71         unsigned int    Value;
72 } tKernelSymbol;
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}
76 /**
77  * \}
78  */
79
80 // === FUNCTIONS ===
81 // --- Core ---
82 extern void     System_Init(char *ArgString);
83
84 // --- IRQs ---
85 extern int      IRQ_AddHandler(int Num, void (*Callback)(int));
86
87 // --- Debug ---
88 /**
89  * \name Debugging and Errors
90  * \{
91  */
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);
101 #if DEBUG
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)
105 # define LEAVE_RET(_t,_v...)    do{LEAVE(_t,_v);return _v;}while(0)
106 //# define LEAVE_RET(_t)        do{LEAVE(_t);return;}
107 #else
108 # define ENTER(...)
109 # define LOG(...)
110 # define LEAVE(...)
111 # define LEAVE_RET(_t,_v...)    return (_v)
112 #endif
113 /**
114  * \}
115  */
116
117 // --- IO ---
118 /**
119  * \name I/O Memory Access
120  * \{
121  */
122 extern void     outb(Uint16 Port, Uint8 Data);
123 extern void     outw(Uint16 Port, Uint16 Data);
124 extern void     outd(Uint16 Port, Uint32 Data);
125 extern void     outq(Uint16 Port, Uint64 Data);
126 extern Uint8    inb(Uint16 Port);
127 extern Uint16   inw(Uint16 Port);
128 extern Uint32   ind(Uint16 Port);
129 extern Uint64   inq(Uint16 Port);
130 /**
131  * \}
132  */
133
134 // --- Memory Management ---
135 /**
136  * \name Memory Management
137  * \{
138  * \todo Move to mm_virt.h
139  */
140 /**
141  * \brief Allocate a physical page at \a VAddr
142  * \param VAddr Virtual Address to allocate at
143  * \return Physical address allocated
144  */
145 extern tPAddr   MM_Allocate(tVAddr VAddr);
146 /**
147  * \brief Deallocate a page
148  * \param VAddr Virtual address to unmap
149  */
150 extern void     MM_Deallocate(tVAddr VAddr);
151 /**
152  * \brief Map a physical page at \a PAddr to \a VAddr
153  * \param VAddr Target virtual address
154  * \param PAddr Physical address to map
155  * \return Boolean Success
156  */
157 extern int      MM_Map(tVAddr VAddr, tPAddr PAddr);
158 /**
159  * \brief Get the physical address of \a VAddr
160  * \param VAddr Address of the page to get the physical address of
161  * \return Physical page mapped at \A VAddr
162  */
163 extern tPAddr   MM_GetPhysAddr(tVAddr VAddr);
164 /**
165  * \brief Checks is a memory range is user accessable
166  * \param VAddr Base address to check
167  * \return 1 if the memory is all user-accessable, 0 otherwise
168  */
169 extern int      MM_IsUser(tVAddr VAddr);
170 /**
171  * \brief Set the access flags on a page
172  * \param VAddr Virtual address of the page
173  * \param Flags New flags value
174  * \param Mask  Flags to set
175  */
176 extern void     MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
177 /**
178  * \brief Temporarily map a page into the address space
179  * \param PAddr Physical addres to map
180  * \return Virtual address of page in memory
181  * \note There is only a limited ammount of slots avaliable
182  */
183 extern tVAddr   MM_MapTemp(tPAddr PAddr);
184 /**
185  * \brief Free a temporarily mapped page
186  * \param VAddr Allocate virtual addres of page
187  */
188 extern void     MM_FreeTemp(tVAddr VAddr);
189 /**
190  * \brief Map a physcal address range into the virtual address space
191  * \param PAddr Physical address to map in
192  * \param Number        Number of pages to map
193  */
194 extern tVAddr   MM_MapHWPage(tPAddr PAddr, Uint Number);
195 /**
196  * \brief Allocates DMA physical memory
197  * \param Pages Number of pages required
198  * \param MaxBits       Maximum number of bits the physical address can have
199  * \param PhysAddr      Pointer to the location to place the physical address allocated
200  * \return Virtual address allocate
201  */
202 extern tVAddr   MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
203 /**
204  * \brief Unmaps an allocated hardware range
205  * \param VAddr Virtual address allocate by ::MM_MapHWPage or ::MM_AllocDMA
206  * \param Number        Number of pages to free
207  */
208 extern void     MM_UnmapHWPage(tVAddr VAddr, Uint Number);
209 /**
210  * \brief Allocate a single physical page
211  * \return Physical address allocated
212  */
213 extern tPAddr   MM_AllocPhys();
214 /**
215  * \brief Allocate a contiguous range of physical pages
216  * \param Pages Number of pages to allocate
217  * \return First physical address allocated
218  */
219 extern tPAddr   MM_AllocPhysRange(int Pages);
220 /**
221  * \brief Reference a physical page
222  * \param PAddr Page to mark as referenced
223  */
224 extern void     MM_RefPhys(tPAddr PAddr);
225 /**
226  * \brief Dereference a physical page
227  * \param PAddr Page to dereference
228  */
229 extern void     MM_DerefPhys(tPAddr PAddr);
230 /**
231  * \}
232  */
233
234 // --- Memory Manipulation ---
235 /**
236  * \name Memory Manipulation
237  * \{
238  */
239 extern int      memcmp(const void *m1, const void *m2, Uint count);
240 extern void *memcpy(void *dest, const void *src, Uint count);
241 extern void *memcpyd(void *dest, const void *src, Uint count);
242 extern void *memset(void *dest, int val, Uint count);
243 extern void *memsetd(void *dest, Uint val, Uint count);
244 /**
245  * \}
246  */
247 /**
248  * \name Memory Validation
249  * \{
250  */
251 extern int      CheckString(char *String);
252 extern int      CheckMem(void *Mem, int Num);
253 /**
254  * \}
255  */
256
257 // --- Endianness ---
258 /**
259  * \name Endianness Swapping
260  * \{
261  */
262 extern Uint16   LittleEndian16(Uint16 Val);
263 extern Uint16   BigEndian16(Uint16 Val);
264 extern Uint32   LittleEndian32(Uint32 Val);
265 extern Uint32   BigEndian32(Uint32 Val);
266 /**
267  * \}
268  */
269
270 // --- Strings ---
271 /**
272  * \name Strings
273  * \{
274  */
275 extern Uint     strlen(const char *Str);
276 extern char     *strcpy(char *__dest, const char *__src);
277 extern int      strcmp(const char *__str1, const char *__str2);
278 extern int      strncmp(const char *Str1, const char *Str2, size_t num);
279 extern int      strucmp(const char *Str1, const char *Str2);
280 extern char     *strdup(const char *Str);
281 extern int      strpos(const char *Str, char Ch);
282 extern int      strpos8(const char *str, Uint32 search);
283 extern void     itoa(char *buf, Uint num, int base, int minLength, char pad);
284 extern int      ReadUTF8(Uint8 *str, Uint32 *Val);
285 extern int      WriteUTF8(Uint8 *str, Uint32 Val);
286 extern int      LookupString(char **Array, char *Needle);
287 /**
288  * \}
289  */
290
291 extern Uint     rand();
292
293 // --- Heap ---
294 /**
295  * \name Heap
296  * \{
297  */
298 extern void *malloc(size_t size);
299 extern void *calloc(size_t num, size_t size);
300 extern void     *realloc(void *ptr, size_t size);
301 extern void free(void *Ptr);
302 extern int      IsHeap(void *Ptr);
303 /**
304  * \}
305  */
306
307 // --- Modules ---
308 /**
309  * \name Modules
310  * \{
311  */
312 extern int      Module_LoadMem(void *Buffer, Uint Length, char *ArgStr);
313 extern int      Module_LoadFile(char *Path, char *ArgStr);
314 /**
315  * \}
316  */
317
318 // --- Timing ---
319 /**
320  * \name Time and Timing
321  * \{
322  */
323 extern Sint64   timestamp(int sec, int mins, int hrs, int day, int month, int year);
324 extern Sint64   now();
325 extern int      Time_CreateTimer(int Delta, void *Callback, void *Argument);
326 extern void     Time_RemoveTimer(int ID);
327 extern void     Time_Delay(int Delay);
328 /**
329  * \}
330  */
331
332 // --- Threads ---
333 /**
334  * \name Threads and Processes
335  * \{
336  */
337 extern  int     Proc_SpawnWorker();
338 extern  int     Proc_Spawn(char *Path);
339 extern void     Threads_Exit();
340 extern void     Threads_Yield();
341 extern void     Threads_Sleep();
342 extern int      Threads_GetUID();
343 extern int      Threads_GetGID();
344 extern int      SpawnTask(tThreadFunction Function, void *Arg);
345 extern Uint     *Threads_GetCfgPtr(int Id);
346 /**
347  * \}
348  */
349
350 // --- Simple Math ---
351 extern int      DivUp(int num, int dem);
352
353 #include <binary_ext.h>
354 #include <vfs_ext.h>
355
356 #endif

UCC git Repository :: git.ucc.asn.au