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

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