Forgot to build test :S Added defintion of CFG_VFS_CHROOT
[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 #else
106 # define ENTER(...)
107 # define LOG(...)
108 # define LEAVE(...)
109 #endif
110 /**
111  * \}
112  */
113
114 // --- IO ---
115 /**
116  * \name I/O Memory Access
117  * \{
118  */
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);
127 /**
128  * \}
129  */
130
131 // --- Memory Management ---
132 /**
133  * \name Memory Management
134  * \{
135  * \todo Move to mm_virt.h
136  */
137 /**
138  * \brief Allocate a physical page at \a VAddr
139  * \param VAddr Virtual Address to allocate at
140  * \return Physical address allocated
141  */
142 extern tPAddr   MM_Allocate(tVAddr VAddr);
143 /**
144  * \brief Deallocate a page
145  * \param VAddr Virtual address to unmap
146  */
147 extern void     MM_Deallocate(tVAddr VAddr);
148 /**
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
153  */
154 extern int      MM_Map(tVAddr VAddr, tPAddr PAddr);
155 /**
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
159  */
160 extern tPAddr   MM_GetPhysAddr(tVAddr VAddr);
161 /**
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
166  */
167 extern int      MM_IsUser(tVAddr VAddr, int Length);
168 /**
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
173  */
174 extern void     MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
175 /**
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
180  */
181 extern tVAddr   MM_MapTemp(tPAddr PAddr);
182 /**
183  * \brief Free a temporarily mapped page
184  * \param VAddr Allocate virtual addres of page
185  */
186 extern void     MM_FreeTemp(tVAddr VAddr);
187 /**
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
191  */
192 extern tVAddr   MM_MapHWPage(tPAddr PAddr, Uint Number);
193 /**
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
199  */
200 extern tVAddr   MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
201 /**
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
205  */
206 extern void     MM_UnmapHWPage(tVAddr VAddr, Uint Number);
207 /**
208  * \brief Allocate a single physical page
209  * \return Physical address allocated
210  */
211 extern tPAddr   MM_AllocPhys();
212 /**
213  * \brief Allocate a contiguous range of physical pages
214  * \param Pages Number of pages to allocate
215  * \return First physical address allocated
216  */
217 extern tPAddr   MM_AllocPhysRange(int Pages);
218 /**
219  * \brief Reference a physical page
220  * \param PAddr Page to mark as referenced
221  */
222 extern void     MM_RefPhys(tPAddr PAddr);
223 /**
224  * \brief Dereference a physical page
225  * \param PAddr Page to dereference
226  */
227 extern void     MM_DerefPhys(tPAddr PAddr);
228 /**
229  * \}
230  */
231
232 // --- Memory Manipulation ---
233 /**
234  * \name Memory Manipulation
235  * \{
236  */
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);
242 /**
243  * \}
244  */
245
246 // --- Endianness ---
247 /**
248  * \name Endianness Swapping
249  * \{
250  */
251 extern Uint16   LittleEndian16(Uint16 Val);
252 extern Uint16   BigEndian16(Uint16 Val);
253 extern Uint32   LittleEndian32(Uint32 Val);
254 extern Uint32   BigEndian32(Uint32 Val);
255 /**
256  * \}
257  */
258
259 // --- Strings ---
260 /**
261  * \name Strings
262  * \{
263  */
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);
275 /**
276  * \}
277  */
278
279 extern Uint     rand();
280
281 // --- Heap ---
282 /**
283  * \name Heap
284  * \{
285  */
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);
291 /**
292  * \}
293  */
294
295 // --- Modules ---
296 /**
297  * \name Modules
298  * \{
299  */
300 extern int      Module_LoadMem(void *Buffer, Uint Length, char *ArgStr);
301 extern int      Module_LoadFile(char *Path, char *ArgStr);
302 /**
303  * \}
304  */
305
306 // --- Timing ---
307 /**
308  * \name Time and Timing
309  * \{
310  */
311 extern Sint64   timestamp(int sec, int mins, int hrs, int day, int month, int year);
312 extern Sint64   now();
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);
316 /**
317  * \}
318  */
319
320 // --- Threads ---
321 /**
322  * \name Threads and Processes
323  * \{
324  */
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);
334 /**
335  * \}
336  */
337
338 // --- Simple Math ---
339 extern int      DivUp(int num, int dem);
340
341 #include <binary_ext.h>
342 #include <vfs_ext.h>
343
344 #endif

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