7638b481bc5f99b6ecef722e2668706f31021e8a
[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  * \return 1 if the memory is all user-accessable, 0 otherwise
165  */
166 extern int      MM_IsUser(tVAddr VAddr);
167 /**
168  * \brief Set the access flags on a page
169  * \param VAddr Virtual address of the page
170  * \param Flags New flags value
171  * \param Mask  Flags to set
172  */
173 extern void     MM_SetFlags(tVAddr VAddr, Uint Flags, Uint Mask);
174 /**
175  * \brief Temporarily map a page into the address space
176  * \param PAddr Physical addres to map
177  * \return Virtual address of page in memory
178  * \note There is only a limited ammount of slots avaliable
179  */
180 extern tVAddr   MM_MapTemp(tPAddr PAddr);
181 /**
182  * \brief Free a temporarily mapped page
183  * \param VAddr Allocate virtual addres of page
184  */
185 extern void     MM_FreeTemp(tVAddr VAddr);
186 /**
187  * \brief Map a physcal address range into the virtual address space
188  * \param PAddr Physical address to map in
189  * \param Number        Number of pages to map
190  */
191 extern tVAddr   MM_MapHWPage(tPAddr PAddr, Uint Number);
192 /**
193  * \brief Allocates DMA physical memory
194  * \param Pages Number of pages required
195  * \param MaxBits       Maximum number of bits the physical address can have
196  * \param PhysAddr      Pointer to the location to place the physical address allocated
197  * \return Virtual address allocate
198  */
199 extern tVAddr   MM_AllocDMA(int Pages, int MaxBits, tPAddr *PhysAddr);
200 /**
201  * \brief Unmaps an allocated hardware range
202  * \param VAddr Virtual address allocate by ::MM_MapHWPage or ::MM_AllocDMA
203  * \param Number        Number of pages to free
204  */
205 extern void     MM_UnmapHWPage(tVAddr VAddr, Uint Number);
206 /**
207  * \brief Allocate a single physical page
208  * \return Physical address allocated
209  */
210 extern tPAddr   MM_AllocPhys();
211 /**
212  * \brief Allocate a contiguous range of physical pages
213  * \param Pages Number of pages to allocate
214  * \return First physical address allocated
215  */
216 extern tPAddr   MM_AllocPhysRange(int Pages);
217 /**
218  * \brief Reference a physical page
219  * \param PAddr Page to mark as referenced
220  */
221 extern void     MM_RefPhys(tPAddr PAddr);
222 /**
223  * \brief Dereference a physical page
224  * \param PAddr Page to dereference
225  */
226 extern void     MM_DerefPhys(tPAddr PAddr);
227 /**
228  * \}
229  */
230
231 // --- Memory Manipulation ---
232 /**
233  * \name Memory Manipulation
234  * \{
235  */
236 extern int      memcmp(const void *m1, const void *m2, Uint count);
237 extern void *memcpy(void *dest, const void *src, Uint count);
238 extern void *memcpyd(void *dest, const void *src, Uint count);
239 extern void *memset(void *dest, int val, Uint count);
240 extern void *memsetd(void *dest, Uint val, Uint count);
241 /**
242  * \}
243  */
244 /**
245  * \name Memory Validation
246  * \{
247  */
248 extern int      CheckString(char *String);
249 extern int      CheckMem(void *Mem, int Num);
250 /**
251  * \}
252  */
253
254 // --- Endianness ---
255 /**
256  * \name Endianness Swapping
257  * \{
258  */
259 extern Uint16   LittleEndian16(Uint16 Val);
260 extern Uint16   BigEndian16(Uint16 Val);
261 extern Uint32   LittleEndian32(Uint32 Val);
262 extern Uint32   BigEndian32(Uint32 Val);
263 /**
264  * \}
265  */
266
267 // --- Strings ---
268 /**
269  * \name Strings
270  * \{
271  */
272 extern Uint     strlen(const char *Str);
273 extern char     *strcpy(char *__dest, const char *__src);
274 extern int      strcmp(const char *__str1, const char *__str2);
275 extern int      strncmp(const char *Str1, const char *Str2, size_t num);
276 extern int      strucmp(const char *Str1, const char *Str2);
277 extern char     *strdup(const char *Str);
278 extern int      strpos(const char *Str, char Ch);
279 extern int      strpos8(const char *str, Uint32 search);
280 extern void     itoa(char *buf, Uint num, int base, int minLength, char pad);
281 extern int      ReadUTF8(Uint8 *str, Uint32 *Val);
282 extern int      WriteUTF8(Uint8 *str, Uint32 Val);
283 extern int      LookupString(char **Array, char *Needle);
284 /**
285  * \}
286  */
287
288 extern Uint     rand();
289
290 // --- Heap ---
291 /**
292  * \name Heap
293  * \{
294  */
295 extern void *malloc(size_t size);
296 extern void *calloc(size_t num, size_t size);
297 extern void     *realloc(void *ptr, size_t size);
298 extern void free(void *Ptr);
299 extern int      IsHeap(void *Ptr);
300 /**
301  * \}
302  */
303
304 // --- Modules ---
305 /**
306  * \name Modules
307  * \{
308  */
309 extern int      Module_LoadMem(void *Buffer, Uint Length, char *ArgStr);
310 extern int      Module_LoadFile(char *Path, char *ArgStr);
311 /**
312  * \}
313  */
314
315 // --- Timing ---
316 /**
317  * \name Time and Timing
318  * \{
319  */
320 extern Sint64   timestamp(int sec, int mins, int hrs, int day, int month, int year);
321 extern Sint64   now();
322 extern int      Time_CreateTimer(int Delta, void *Callback, void *Argument);
323 extern void     Time_RemoveTimer(int ID);
324 extern void     Time_Delay(int Delay);
325 /**
326  * \}
327  */
328
329 // --- Threads ---
330 /**
331  * \name Threads and Processes
332  * \{
333  */
334 extern  int     Proc_SpawnWorker();
335 extern  int     Proc_Spawn(char *Path);
336 extern void     Threads_Exit();
337 extern void     Threads_Yield();
338 extern void     Threads_Sleep();
339 extern int      Threads_GetUID();
340 extern int      Threads_GetGID();
341 extern int      SpawnTask(tThreadFunction Function, void *Arg);
342 extern Uint     *Threads_GetCfgPtr(int Id);
343 /**
344  * \}
345  */
346
347 // --- Simple Math ---
348 extern int      DivUp(int num, int dem);
349
350 #include <binary_ext.h>
351 #include <vfs_ext.h>
352
353 #endif

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