General Cleanup, Implemented DMA Allocation
[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         NUM_CFG_ENTRIES
40 };
41 #define CFGINT(id)      (*Threads_GetCfgPtr(id))
42 #define CFGPTR(id)      (*(void**)Threads_GetCfgPtr(id))
43 /**
44  * \}
45  */
46
47 // === CONSTANTS ===
48 // --- Memory Flags --
49 /**
50  * \name Memory Flags
51  * \{
52  * \todo Move to mm_virt.h
53  */
54 #define MM_PFLAG_RO             0x01    // Writes disallowed
55 #define MM_PFLAG_EXEC   0x02    // Allow execution
56 #define MM_PFLAG_NOPAGE 0x04    // Prevent from being paged out
57 #define MM_PFLAG_COW    0x08    // Copy-On-Write
58 #define MM_PFLAG_KERNEL 0x10    // Kernel-Only (Ring0)
59 /**
60  * \}
61  */
62
63 // === Kernel Export Macros ===
64 /**
65  * \name Kernel Function 
66  * \{
67  */
68 typedef struct sKernelSymbol {
69         char    *Name;
70         unsigned int    Value;
71 } tKernelSymbol;
72 #define EXPORT(_name)   tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)_name}
73 #define EXPORTV(_name)  tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)&_name}
74 #define EXPORTAS(_sym,_name)    tKernelSymbol _kexp_##_name __attribute__((section ("KEXPORT"),unused))={#_name, (Uint)_sym}
75 /**
76  * \}
77  */
78
79 // === FUNCTIONS ===
80 // --- Core ---
81 extern void     System_Init(char *ArgString);
82
83 // --- IRQs ---
84 extern int      IRQ_AddHandler(int Num, void (*Callback)(int));
85
86 // --- Debug ---
87 /**
88  * \name Debugging and Errors
89  * \{
90  */
91 extern void     Panic(char *Msg, ...);
92 extern void     Warning(char *Msg, ...);
93 extern void     Log(char *Fmt, ...);
94 extern void     LogV(char *Fmt, va_list Args);
95 extern void     LogF(char *Fmt, ...);
96 extern void     Debug_Enter(char *FuncName, char *ArgTypes, ...);
97 extern void     Debug_Log(char *FuncName, char *Fmt, ...);
98 extern void     Debug_Leave(char *FuncName, char RetType, ...);
99 extern void     Debug_HexDump(char *Header, void *Data, Uint Length);
100 #if DEBUG
101 # define ENTER(_types...)       Debug_Enter((char*)__func__, _types)
102 # define LOG(_fmt...)   Debug_Log((char*)__func__, _fmt)
103 # define LEAVE(_t...)   Debug_Leave((char*)__func__, _t)
104 #else
105 # define ENTER(...)
106 # define LOG(...)
107 # define LEAVE(...)
108 #endif
109 /**
110  * \}
111  */
112
113 // --- IO ---
114 /**
115  * \name I/O Memory Access
116  * \{
117  */
118 extern void     outb(Uint16 Port, Uint8 Data);
119 extern void     outw(Uint16 Port, Uint16 Data);
120 extern void     outd(Uint16 Port, Uint32 Data);
121 extern void     outq(Uint16 Port, Uint64 Data);
122 extern Uint8    inb(Uint16 Port);
123 extern Uint16   inw(Uint16 Port);
124 extern Uint32   ind(Uint16 Port);
125 extern Uint64   inq(Uint16 Port);
126 /**
127  * \}
128  */
129
130 // --- Memory Management ---
131 /**
132  * \name Memory Management
133  * \{
134  * \todo Move to mm_virt.h
135  */
136 /**
137  * \brief Allocate a physical page at \a VAddr
138  * \param VAddr Virtual Address to allocate at
139  * \return Physical address allocated
140  */
141 extern tPAddr   MM_Allocate(tVAddr VAddr);
142 /**
143  * \brief Deallocate a page
144  * \param VAddr Virtual address to unmap
145  */
146 extern void     MM_Deallocate(tVAddr VAddr);
147 /**
148  * \brief Map a physical page at \a PAddr to \a VAddr
149  * \param VAddr Target virtual address
150  * \param PAddr Physical address to map
151  * \return Boolean Success
152  */
153 extern int      MM_Map(tVAddr VAddr, tPAddr PAddr);
154 /**
155  * \brief Get the physical address of \a VAddr
156  * \param VAddr Address of the page to get the physical address of
157  * \return Physical page mapped at \A VAddr
158  */
159 extern tPAddr   MM_GetPhysAddr(tVAddr VAddr);
160 /**
161  * \brief Checks is a memory range is user accessable
162  * \param VAddr Base address to check
163  * \param Length        Number of bytes to check
164  * \return 1 if the memory is all user-accessable, 0 otherwise
165  */
166 extern int      MM_IsUser(tVAddr VAddr, int Length);
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 // --- Endianness ---
246 /**
247  * \name Endianness Swapping
248  * \{
249  */
250 extern Uint16   LittleEndian16(Uint16 Val);
251 extern Uint16   BigEndian16(Uint16 Val);
252 extern Uint32   LittleEndian32(Uint32 Val);
253 extern Uint32   BigEndian32(Uint32 Val);
254 /**
255  * \}
256  */
257
258 // --- Strings ---
259 /**
260  * \name Strings
261  * \{
262  */
263 extern Uint     strlen(const char *Str);
264 extern char     *strcpy(char *__dest, const char *__src);
265 extern int      strcmp(const char *__str1, const char *__str2);
266 extern int      strncmp(const char *Str1, const char *Str2, size_t num);
267 extern int      strucmp(const char *Str1, const char *Str2);
268 extern char     *strdup(const char *Str);
269 extern int      strpos(const char *Str, char Ch);
270 extern int      strpos8(const char *str, Uint32 search);
271 extern void     itoa(char *buf, Uint num, int base, int minLength, char pad);
272 extern int      ReadUTF8(Uint8 *str, Uint32 *Val);
273 extern int      WriteUTF8(Uint8 *str, Uint32 Val);
274 /**
275  * \}
276  */
277
278 extern Uint     rand();
279
280 // --- Heap ---
281 /**
282  * \name Heap
283  * \{
284  */
285 extern void *malloc(size_t size);
286 extern void *calloc(size_t num, size_t size);
287 extern void     *realloc(void *ptr, size_t size);
288 extern void free(void *Ptr);
289 extern int      IsHeap(void *Ptr);
290 /**
291  * \}
292  */
293
294 // --- Modules ---
295 /**
296  * \name Modules
297  * \{
298  */
299 extern int      Module_LoadMem(void *Buffer, Uint Length, char *ArgStr);
300 extern int      Module_LoadFile(char *Path, char *ArgStr);
301 /**
302  * \}
303  */
304
305 // --- Timing ---
306 /**
307  * \name Time and Timing
308  * \{
309  */
310 extern Sint64   timestamp(int sec, int mins, int hrs, int day, int month, int year);
311 extern Sint64   now();
312 extern int      Time_CreateTimer(int Delta, void *Callback, void *Argument);
313 extern void     Time_RemoveTimer(int ID);
314 extern void     Time_Delay(int Delay);
315 /**
316  * \}
317  */
318
319 // --- Threads ---
320 /**
321  * \name Threads and Processes
322  * \{
323  */
324 extern  int     Proc_SpawnWorker();
325 extern  int     Proc_Spawn(char *Path);
326 extern void     Threads_Exit();
327 extern void     Threads_Yield();
328 extern void     Threads_Sleep();
329 extern int      Threads_GetUID();
330 extern int      Threads_GetGID();
331 extern int      SpawnTask(tThreadFunction Function, void *Arg);
332 extern Uint     *Threads_GetCfgPtr(int Id);
333 /**
334  * \}
335  */
336
337 // --- Simple Math ---
338 extern int      DivUp(int num, int dem);
339
340 #include <binary_ext.h>
341 #include <vfs_ext.h>
342
343 #endif

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