Big Changes - See commit details
[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 // --- Interface Flags & Macros
64 #define CLONE_VM        0x10
65
66 // === Types ===
67 typedef void (*tThreadFunction)(void*);
68
69 // === Kernel Export Macros ===
70 /**
71  * \name Kernel Function 
72  * \{
73  */
74 typedef struct sKernelSymbol {
75         char    *Name;
76         unsigned int    Value;
77 } tKernelSymbol;
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}
81 /**
82  * \}
83  */
84
85 // === FUNCTIONS ===
86 // --- Core ---
87 extern void     System_Init(char *ArgString);
88
89 // --- IRQs ---
90 extern int      IRQ_AddHandler(int Num, void (*Callback)(int));
91
92 // --- Debug ---
93 /**
94  * \name Debugging and Errors
95  * \{
96  */
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);
106 #if DEBUG
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)
112 #else
113 # define ENTER(...)
114 # define LOG(...)
115 # define LEAVE(...)
116 # define LEAVE_RET(_t,_v...)    return (_v)
117 # define LEAVE_RET0()   return
118 #endif
119 /**
120  * \}
121  */
122
123 // --- IO ---
124 /**
125  * \name I/O Memory Access
126  * \{
127  */
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);
136 /**
137  * \}
138  */
139
140 // --- Memory Management ---
141 /**
142  * \name Memory Management
143  * \{
144  * \todo Move to mm_virt.h
145  */
146 /**
147  * \brief Allocate a physical page at \a VAddr
148  * \param VAddr Virtual Address to allocate at
149  * \return Physical address allocated
150  */
151 extern tPAddr   MM_Allocate(tVAddr VAddr);
152 /**
153  * \brief Deallocate a page
154  * \param VAddr Virtual address to unmap
155  */
156 extern void     MM_Deallocate(tVAddr VAddr);
157 /**
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
162  */
163 extern int      MM_Map(tVAddr VAddr, tPAddr PAddr);
164 /**
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
168  */
169 extern tPAddr   MM_GetPhysAddr(tVAddr VAddr);
170 /**
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
174  */
175 extern int      MM_IsUser(tVAddr VAddr);
176 /**
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
181  */
182 extern void     MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
183 /**
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
188  */
189 extern tVAddr   MM_MapTemp(tPAddr PAddr);
190 /**
191  * \brief Free a temporarily mapped page
192  * \param VAddr Allocate virtual addres of page
193  */
194 extern void     MM_FreeTemp(tVAddr VAddr);
195 /**
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
199  */
200 extern tVAddr   MM_MapHWPage(tPAddr PAddr, Uint Number);
201 /**
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
207  */
208 extern tVAddr   MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
209 /**
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
213  */
214 extern void     MM_UnmapHWPage(tVAddr VAddr, Uint Number);
215 /**
216  * \brief Allocate a single physical page
217  * \return Physical address allocated
218  */
219 extern tPAddr   MM_AllocPhys();
220 /**
221  * \brief Allocate a contiguous range of physical pages
222  * \param Pages Number of pages to allocate
223  * \return First physical address allocated
224  */
225 extern tPAddr   MM_AllocPhysRange(int Pages);
226 /**
227  * \brief Reference a physical page
228  * \param PAddr Page to mark as referenced
229  */
230 extern void     MM_RefPhys(tPAddr PAddr);
231 /**
232  * \brief Dereference a physical page
233  * \param PAddr Page to dereference
234  */
235 extern void     MM_DerefPhys(tPAddr PAddr);
236 /**
237  * \}
238  */
239
240 // --- Memory Manipulation ---
241 /**
242  * \name Memory Manipulation
243  * \{
244  */
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);
250 /**
251  * \}
252  */
253 /**
254  * \name Memory Validation
255  * \{
256  */
257 extern int      CheckString(char *String);
258 extern int      CheckMem(void *Mem, int Num);
259 /**
260  * \}
261  */
262
263 // --- Endianness ---
264 /**
265  * \name Endianness Swapping
266  * \{
267  */
268 extern Uint16   LittleEndian16(Uint16 Val);
269 extern Uint16   BigEndian16(Uint16 Val);
270 extern Uint32   LittleEndian32(Uint32 Val);
271 extern Uint32   BigEndian32(Uint32 Val);
272 /**
273  * \}
274  */
275
276 // --- Strings ---
277 /**
278  * \name Strings
279  * \{
280  */
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);
293 /**
294  * \}
295  */
296
297 extern Uint     rand();
298
299 // --- Heap ---
300 /**
301  * \name Heap
302  * \{
303  */
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);
309 /**
310  * \}
311  */
312
313 // --- Modules ---
314 /**
315  * \name Modules
316  * \{
317  */
318 extern int      Module_LoadMem(void *Buffer, Uint Length, char *ArgStr);
319 extern int      Module_LoadFile(char *Path, char *ArgStr);
320 /**
321  * \}
322  */
323
324 // --- Timing ---
325 /**
326  * \name Time and Timing
327  * \{
328  */
329 extern Sint64   timestamp(int sec, int mins, int hrs, int day, int month, int year);
330 extern Sint64   now();
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);
334 /**
335  * \}
336  */
337
338 // --- Threads ---
339 /**
340  * \name Threads and Processes
341  * \{
342  */
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);
352 /**
353  * \}
354  */
355
356 // --- Simple Math ---
357 extern int      DivUp(int num, int dem);
358
359 #include <binary_ext.h>
360 #include <vfs_ext.h>
361
362 #endif

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