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

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