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

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