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

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