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

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