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

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