NeKernel dev
Loading...
Searching...
No Matches
System.h
Go to the documentation of this file.
1/* ========================================
2
3Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
4
5File: System.h
6Purpose: System Call Interface.
7
8======================================== */
9
10#ifndef LIBSYS_SYSTEM_CALLS_H
11#define LIBSYS_SYSTEM_CALLS_H
12
14
16#define kPrintDevicePath "/devices/tty{}"
17#define kCDDevicePath "/devices/dvd{}"
18
19// ------------------------------------------------------------------------------------------ //
21// ------------------------------------------------------------------------------------------ //
22
23struct REF_TYPE {
24 UInt64 __hash;
26};
27
28typedef REF_TYPE* Ref;
29
30typedef Ref IORef;
31typedef Ref FSRef;
32typedef Ref DylibRef;
33typedef Ref ThreadRef;
34typedef Ref SocketRef;
36typedef Ref MutexRef;
37typedef Ref EventRef;
39
40// ------------------------------------------------------------------------------------------ //
42// ------------------------------------------------------------------------------------------ //
43
48IMPORT_C Ref LdrGetDylibSymbolFromHandle(_Input const Char* symbol, _Input Ref dll_handle);
49
54IMPORT_C Ref LdrOpenDylibHandle(_Input const Char* path, _Input const Char* drive_letter);
55
60
61// ------------------------------------------------------------------------------------------ //
62// File API.
63// ------------------------------------------------------------------------------------------ //
64
69IMPORT_C Ref IoOpenFile(const Char* fs_path, const Char* drive_letter);
70
74IMPORT_C Void IoCloseFile(_Input Ref file_desc);
75
83IMPORT_C SInt32 IoCtrlFile(_Input Ref file_desc, _Input UInt32 ioctl_code, _Input VoidPtr in_data,
84 _Output VoidPtr out_data);
85
88IMPORT_C const Char* IoMimeFile(_Input Ref file_desc);
89
93IMPORT_C const Char* IoDimFile(_Input Ref dir_desc);
94
100IMPORT_C UInt32 IoWriteFile(_Input Ref file_desc, _Output VoidPtr out_data, UInt64 sz_data);
101
106IMPORT_C UInt32 IoReadFile(_Input Ref file_desc, _Output VoidPtr* out_data, UInt64 sz_data);
107
111IMPORT_C UInt64 IoRewindFile(_Input Ref file_desc);
112
116IMPORT_C UInt64 IoTellFile(_Input Ref file_desc);
117
119IMPORT_C UInt64 IoSeekFile(_Input Ref file_desc, UInt64 file_offset);
120
121// ------------------------------------------------------------------------
122// Process API.
123// ------------------------------------------------------------------------
124
129IMPORT_C UInt32 RtlSpawnIB(UIntPtr process_id);
130
134IMPORT_C UIntPtr RtlSpawnProcess(const Char* process_path, UInt32 argc, Char** argv, Char** envp,
135 UInt32 envp_len);
136
139IMPORT_C Bool RtlExitProcess(UIntPtr handle, UIntPtr exit_code);
140
141// ------------------------------------------------------------------------
142// Memory Manager API.
143// ------------------------------------------------------------------------
144
149IMPORT_C VoidPtr MmCreateHeap(_Input UInt64 len, _Input UInt32 flags);
150
155
158
161
164
167
170
172IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input UInt64 len, _Input UInt8 value);
173
175IMPORT_C SInt64 MmStrCmp(_Input const Char* dest, _Input const Char* src);
176
178IMPORT_C SInt64 MmStrLen(const Char* str);
179
180// ------------------------------------------------------------------------
181// @brief Error API.
182// ------------------------------------------------------------------------
183
185
186// ------------------------------------------------------------------------
187// @brief Threading API.
188// ------------------------------------------------------------------------
189
193
197
202
204typedef SInt32 (*ThrProcKind)(SInt32 argc, Char** argv);
205
211IMPORT_C ThreadRef ThrCreateThread(const Char* thread_name, ThrProcKind procedure,
212 SInt32 argument_count, SInt32 flags);
213
217
221
225
226// ------------------------------------------------------------------------
227// @brief Drive Management API.
228// ------------------------------------------------------------------------
229
230// ------------------------------------------------------------------------------------------ //
234// ------------------------------------------------------------------------------------------ //
236
237// ------------------------------------------------------------------------------------------ //
241// ------------------------------------------------------------------------------------------ //
243
244// ------------------------------------------------------------------------------------------ //
248// ------------------------------------------------------------------------------------------ //
250
251// ------------------------------------------------------------------------------------------ //
255// ------------------------------------------------------------------------------------------ //
256IMPORT_C Void DrvMountDrive(_Input const Char* path, _Input const Char* letter);
257
258// ------------------------------------------------------------------------------------------ //
261// ------------------------------------------------------------------------------------------ //
263
264// ------------------------------------------------------------------------
265// Event handling API, use to listen to OS specific events.
266// ------------------------------------------------------------------------
267
268// ------------------------------------------------------------------------------------------ //
273// ------------------------------------------------------------------------------------------ //
274
275IMPORT_C Void EvtAddListener(_Input const Char* event_name, _Input Ref listener);
276
277// ------------------------------------------------------------------------------------------ //
282// ------------------------------------------------------------------------------------------ //
283
284IMPORT_C Void EvtRemoveListener(_Input const Char* event_name, _Input Ref listener);
285
286// ------------------------------------------------------------------------------------------ //
291// ------------------------------------------------------------------------------------------ //
292
293IMPORT_C VoidPtr EvtDispatchEvent(_Input const Char* event_name, _Input VoidPtr event_data);
294
295// ------------------------------------------------------------------------------------------ //
296// Power API.
297// ------------------------------------------------------------------------------------------ //
298
299enum {
306};
307
309
311
312// ------------------------------------------------------------------------------------------ //
313// CD-ROM API.
314// ------------------------------------------------------------------------------------------ //
315
317
319
321
322// ------------------------------------------------------------------------------------------ //
323// TTY API.
324// ------------------------------------------------------------------------------------------ //
325
326IMPORT_C SInt32 PrintOut(IORef file /* nullptr to direct to stdout */, const Char* fmt, ...);
327
328IMPORT_C SInt32 PrintIn(IORef file /* nullptr to direct to stdout */, const Char* fmt, ...);
329
331
333
334IMPORT_C IORef PrintGet(const Char* path);
335
336// ------------------------------------------------------------------------------------------ //
337// @brief Scheduler/Debug API.
338// ------------------------------------------------------------------------------------------ //
339
341typedef UInt64 ProcessRef;
342
344
346
348
350
352
354
355// ------------------------------------------------------------------------------------------ //
356// @brief Filesystem API.
357// ------------------------------------------------------------------------------------------ //
358
359IMPORT_C BOOL FsCopy(const Char* path, const Char* dst);
360
361IMPORT_C BOOL FsMove(const Char* path, const Char* dst);
362
363IMPORT_C BOOL FsExists(const Char* path);
364
365IMPORT_C BOOL FsCreateDir(const Char* path);
366
367IMPORT_C BOOL FsCreateFile(const Char* path);
368
369IMPORT_C BOOL FsCreateAlias(const Char* path, const Char* from);
370
371// ------------------------------------------------------------------------------------------ //
372// @brief Installable Filesystem API.
373// ------------------------------------------------------------------------------------------ //
374
375IMPORT_C BOOL IfsMount(const Char* path, const Char* drive_letter);
376
377IMPORT_C BOOL IfsUnmount(const Char* drive_letter);
378
379IMPORT_C BOOL IfsIsMounted(const Char* drive_letter);
380
381// ------------------------------------------------------------------------------------------ //
382// @brief String Manip API.
383// ------------------------------------------------------------------------------------------ //
384
385IMPORT_C Char* StrFmt(const Char* fmt, ...);
386
387IMPORT_C UInt64 StrMathToNumber(const Char* in, const Char** endp, const SInt16 base);
388
389#endif // ifndef LIBSYS_SYSTEM_CALLS_H
#define _Output
Definition CompilerHint.h:15
#define _Input
Definition CompilerHint.h:14
#define _InOut
Definition CompilerHint.h:22
#define BOOL
IMPORT_C const Char * IoDimFile(_Input Ref dir_desc)
Gets the dir DIM.
IMPORT_C Char * StrFmt(const Char *fmt,...)
Definition SystemCalls.cc:14
IMPORT_C IORef PrintCreate(Void)
IMPORT_C const Char * IoMimeFile(_Input Ref file_desc)
Gets the file mime (if any).
IMPORT_C SInt32 SchedWriteMemory(_Input ProcessRef, SInt32, SInt32)
IMPORT_C IORef CdOpenTray(Void)
IMPORT_C Ref IoOpenFile(const Char *fs_path, const Char *drive_letter)
Opens a file from a drive.
IMPORT_C SInt32 MmSetHeapFlags(_Input VoidPtr heap, _Input UInt32 flags)
Change protection flags of a memory region.
IMPORT_C Void DrvUnmountDrive(_Input Char letter)
Unmount a drive.
IMPORT_C Bool RtlExitProcess(UIntPtr handle, UIntPtr exit_code)
Exits a process with an exit_code.
IMPORT_C BOOL FsMove(const Char *path, const Char *dst)
Ref MutexRef
Definition System.h:36
Ref SocketRef
Definition System.h:34
IMPORT_C SInt32 PwrReadCode(_Output SInt32 &code)
IMPORT_C SInt32 CdEjectDrive(_Input IORef cdrom)
SInt32 AffinityRef
Definition System.h:340
Ref NetworkRef
Definition System.h:35
IMPORT_C Ref LdrOpenDylibHandle(_Input const Char *path, _Input const Char *drive_letter)
Open Dylib handle.
IMPORT_C UInt64 StrMathToNumber(const Char *in, const Char **endp, const SInt16 base)
IMPORT_C BOOL IfsMount(const Char *path, const Char *drive_letter)
IMPORT_C Void IoCloseFile(_Input Ref file_desc)
Closes a file and flushes its content.
Definition SystemCalls.cc:129
IMPORT_C UInt64 IoRewindFile(_Input Ref file_desc)
Rewind the file pointer to the beginning of the file.
IMPORT_C SInt32 PwrSendCode(_Output SInt32 &code)
IMPORT_C BOOL FsCreateFile(const Char *path)
IMPORT_C SInt32 SchedGetAffinity(_Input ProcessRef, _InOut AffinityRef *)
IMPORT_C UInt32 MmFillCRC32Heap(_Input VoidPtr heap)
Fill memory region with CRC32.
IMPORT_C VoidPtr EvtDispatchEvent(_Input const Char *event_name, _Input VoidPtr event_data)
Dispatch an event.
IMPORT_C SInt32 ThrExitThread(_Input ThreadRef thread, _Input SInt32 exit_code)
Exit a thread.
IMPORT_C UInt32 IoWriteFile(_Input Ref file_desc, _Output VoidPtr out_data, UInt64 sz_data)
Write data to a file ref.
IMPORT_C SInt32 ThrJoinThread(ThreadRef thrd)
Joins a thread.
IMPORT_C BOOL IfsIsMounted(const Char *drive_letter)
IMPORT_C SInt32 ThrYieldThread(ThreadRef thrd)
Yields the current thread.
IMPORT_C SInt32 SchedReadMemory(_Input ProcessRef, SInt32, SInt32)
IMPORT_C SInt64 MmStrLen(const Char *str)
Get length of string.
Definition SystemCalls.cc:84
IMPORT_C ThreadRef ThrCreateThread(const Char *thread_name, ThrProcKind procedure, SInt32 argument_count, SInt32 flags)
Creates a thread.
IMPORT_C SInt32 SchedSetAffinity(_Input ProcessRef, SInt32 req, _Input AffinityRef *)
IMPORT_C UInt32 RtlSpawnIB(UIntPtr process_id)
Spawns a Thread Information Block and Global Information Block inside the current process.
IMPORT_C UIntPtr RtlSpawnProcess(const Char *process_path, UInt32 argc, Char **argv, Char **envp, UInt32 envp_len)
Spawns a process with a unique pid (stored as UIntPtr).
IMPORT_C UInt32 LdrCloseDylibHandle(_Input Ref *dll_handle)
Close Dylib handle.
REF_TYPE * Ref
Definition System.h:28
IMPORT_C SInt32 MmDestroyHeap(_Input VoidPtr heap)
Destroys the pointer.
IMPORT_C VoidPtr MmCreateHeap(_Input UInt64 len, _Input UInt32 flags)
Creates a new heap from the process's address space.
IMPORT_C SInt32 IoCtrlFile(_Input Ref file_desc, _Input UInt32 ioctl_code, _Input VoidPtr in_data, _Output VoidPtr out_data)
I/O control (ioctl) on a file.
SInt32(* ThrProcKind)(SInt32 argc, Char **argv)
Thread procedure function type.
Definition System.h:204
IMPORT_C SInt64 MmStrCmp(_Input const Char *dest, _Input const Char *src)
Compare string regions.
IMPORT_C SInt32 ThrExitMainThread(_Input SInt32 exit_code)
Exit the main thread.
IMPORT_C SInt64 MmCmpMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input UInt64 len)
Compare memory regions.
UInt64 ProcessRef
Definition System.h:341
IMPORT_C BOOL IfsUnmount(const Char *drive_letter)
IMPORT_C Void EvtAddListener(_Input const Char *event_name, _Input Ref listener)
Add an event listener.
IMPORT_C Ref DrvGetMountedDrive(_Input Char letter)
Get a mounted drive from a letter.
IMPORT_C Void EvtRemoveListener(_Input const Char *event_name, _Input Ref listener)
Remove an event listener.
Ref FSRef
Definition System.h:31
Ref DylibRef
Definition System.h:32
IMPORT_C BOOL FsCopy(const Char *path, const Char *dst)
IMPORT_C SInt32 CdCloseTray(Void)
IMPORT_C Ref LdrGetDylibSymbolFromHandle(_Input const Char *symbol, _Input Ref dll_handle)
Dynamic Loader API.
IMPORT_C SInt32 PrintIn(IORef file, const Char *fmt,...)
Ref IORef
Definition System.h:30
IMPORT_C SInt32 ThrDetachThread(ThreadRef thrd)
Detach a thread.
IMPORT_C BOOL FsCreateAlias(const Char *path, const Char *from)
IMPORT_C UIntPtr SchedGetCurrentProcessID(Void)
IMPORT_C UInt32 MmGetHeapFlags(_Input VoidPtr heap)
Change protection flags of a memory region.
IMPORT_C Char * DrvGetDefaultDriveLetter(Void)
Get the default drive letter.
IMPORT_C UInt32 IoReadFile(_Input Ref file_desc, _Output VoidPtr *out_data, UInt64 sz_data)
Read data from a file.
IMPORT_C IORef PrintGet(const Char *path)
@ kPowerCodeShutdown
Definition System.h:301
@ kPowerCodeInvalid
Definition System.h:300
@ kPowerCodeWake
Definition System.h:304
@ kPowerCodeCount
Definition System.h:305
@ kPowerCodeReboot
Definition System.h:302
@ kPowerCodeSleep
Definition System.h:303
IMPORT_C SInt32 ThrExitCurrentThread(_Input SInt32 exit_code)
Exit the current thread.
IMPORT_C SInt32 SchedFireSignal(_Input ProcessRef, SInt32)
IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input UInt64 len)
Copy memory region.
Definition SystemCalls.cc:21
IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input UInt64 len, _Input UInt8 value)
Fill memory region.
Definition SystemCalls.cc:94
IMPORT_C SInt32 PrintOut(IORef file, const Char *fmt,...)
IMPORT_C Char * DrvGetDriveLetterFromPath(_Input const Char *path)
Get the drive letter from a path.
Ref SemaphoreRef
Definition System.h:38
IMPORT_C BOOL FsCreateDir(const Char *path)
IMPORT_C UInt64 IoTellFile(_Input Ref file_desc)
Tell the current position of the file pointer.
Definition SystemCalls.cc:145
IMPORT_C BOOL FsExists(const Char *path)
IMPORT_C UInt64 IoSeekFile(_Input Ref file_desc, UInt64 file_offset)
Seek file offset from file descriptor.
Ref ThreadRef
Definition System.h:33
IMPORT_C Void DrvMountDrive(_Input const Char *path, _Input const Char *letter)
Mount a drive.
IMPORT_C SInt32 PrintRelease(IORef)
Ref EventRef
Definition System.h:37
IMPORT_C SInt32 ErrGetLastError(Void)
void * VoidPtr
Definition Macros.h:59
__INT64_TYPE__ SInt64
Definition Macros.h:54
__INT16_TYPE__ SInt16
Definition Macros.h:56
#define IMPORT_C
Definition Macros.h:22
__INT32_TYPE__ SInt32
Definition Macros.h:55
Types API.
Definition System.h:23
UInt64 __hash
Definition System.h:24
VoidPtr __self
Hash of the syscall.
Definition System.h:25