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

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