2 * AcessOS Microkernel Version
13 extern int Proc_Clone(Uint *Err, Uint Flags);
14 extern int Threads_WaitTID(int TID, int *status);
15 extern Uint Proc_SendMessage(Uint *Err, Uint Dest, Uint Length, void *Data);
16 extern int Proc_GetMessage(Uint *Err, Uint *Source, void *Buffer);
17 extern int Proc_Execve(char *File, char **ArgV, char **EnvP);
18 extern Uint Binary_Load(char *file, Uint *entryPoint);
19 extern int VFS_FInfo(int FD, void *Dest, int MaxACLs);
20 extern int VFS_GetACL(int FD, void *Dest);
21 extern int VFS_ChDir(char *Dest);
22 extern int Threads_SetName(char *NewName);
23 extern int Threads_GetPID();
24 extern int Threads_GetTID();
25 extern int Threads_GetUID();
26 extern int Threads_GetGID();
29 int Syscall_ValidString(Uint Addr);
30 int Syscall_Valid(int Size, Uint Addr);
33 // TODO: Do sanity checking on arguments, ATM the user can really fuck with the kernel
34 void SyscallHandler(tSyscallRegs *Regs)
39 ENTER("iThread iNum", Threads_GetTID(), Regs->Num);
40 if(Regs->Num < NUM_SYSCALLS)
41 LOG("Syscall %s", cSYSCALL_NAMES[Regs->Num]);
42 LOG("Arg1: 0x%x, Arg2: 0x%x, Arg3: 0x%x", Regs->Arg1, Regs->Arg2, Regs->Arg3);
47 // -- Exit the current thread
48 case SYS_EXIT: Threads_Exit(0, Regs->Arg1); break;
50 // -- Put the current thread to sleep
51 case SYS_SLEEP: Threads_Sleep(); break;
53 // -- Yield current timeslice
54 case SYS_YIELD: Threads_Yield(); break;
56 // -- Clone the current thread
58 // Call clone system call
59 ret = Proc_Clone(&err, Regs->Arg1);
60 // Change user stack if a new stack address is passed
61 if(ret == 0 && Regs->Arg2)
62 Regs->StackPointer = Regs->Arg2;
71 // -- Wait for a thread
73 ret = Threads_WaitTID(Regs->Arg1, (void*)Regs->Arg2);
76 // -- Get the physical address of a page
78 ret = MM_GetPhysAddr(Regs->Arg1);
82 case SYS_MAP: MM_Map(Regs->Arg1, Regs->Arg2); break;
84 // -- Allocate an address
85 case SYS_ALLOCATE: ret = MM_Allocate(Regs->Arg1); break;
87 // -- Unmap an address
88 case SYS_UNMAP: MM_Deallocate(Regs->Arg1); break;
90 // -- Get Thread/Process IDs
91 case SYS_GETTID: ret = Threads_GetTID(); break;
92 case SYS_GETPID: ret = Threads_GetPID(); break;
94 // -- Get User/Group IDs
95 case SYS_GETUID: ret = Threads_GetUID(); break;
96 case SYS_GETGID: ret = Threads_GetGID(); break;
98 // -- Set User/Group IDs
99 case SYS_SETUID: Log("Setting User ID to %i", Regs->Arg1); break;
100 case SYS_SETGID: Log("Setting Group ID to %i", Regs->Arg1); break;
104 ret = Proc_SendMessage(&err, Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3);
106 // -- Check for messages
108 ret = Proc_GetMessage(&err, (Uint*)Regs->Arg1, (void*)Regs->Arg2);
111 // -- Set the thread's name
114 if(!Regs->Arg1) { ret = -1; err = -EINVAL; break; }
115 Threads_SetName( (void*)Regs->Arg1 );
122 if( !Syscall_ValidString(Regs->Arg1) ) {
127 ret = Proc_Execve((char*)Regs->Arg1, (char**)Regs->Arg2, (char**)Regs->Arg3);
130 if( !Syscall_ValidString(Regs->Arg1)
131 || !Syscall_Valid(sizeof(Uint), Regs->Arg2) ) {
136 ret = Binary_Load((char*)Regs->Arg1, (Uint*)Regs->Arg2);
140 // Virtual Filesystem
143 if( !Syscall_ValidString(Regs->Arg1) ) {
148 ret = VFS_Open((char*)Regs->Arg1, Regs->Arg2 | VFS_OPENFLAG_USER);
152 VFS_Close( Regs->Arg1 );
156 ret = VFS_Seek( Regs->Arg1, Regs->Arg2, Regs->Arg3);
160 ret = VFS_Tell( Regs->Arg1 );
165 ret = VFS_Write( Regs->Arg1, Regs->Arg2|((Uint64)Regs->Arg3<<32), (void*)Regs->Arg4 );
167 ret = VFS_Write( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
173 ret = VFS_Read( Regs->Arg1, Regs->Arg2|((Uint64)Regs->Arg3<<32), (void*)Regs->Arg4 );
175 ret = VFS_Read( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
180 ret = VFS_FInfo( Regs->Arg1, (void*)Regs->Arg2, Regs->Arg3 );
185 if( !Syscall_Valid(8, Regs->Arg1) ) {
190 ret = VFS_GetACL( Regs->Arg1, (void*)Regs->Arg2 );
195 if( !Syscall_ValidString(Regs->Arg2) ) {
200 ret = VFS_ReadDir( Regs->Arg1, (void*)Regs->Arg2 );
205 if( !Syscall_ValidString(Regs->Arg1) ) {
210 ret = VFS_ChDir( (void*)Regs->Arg1 );
215 ret = VFS_IOCtl( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
218 // Mount a filesystem
220 // Only root can mount filesystems
221 if(Threads_GetUID() != 0) {
226 // Sanity check the paths
227 if(!Syscall_ValidString(Regs->Arg1)
228 || !Syscall_ValidString(Regs->Arg2)
229 || !Syscall_ValidString(Regs->Arg3)
230 || !Syscall_ValidString(Regs->Arg4) ) {
236 (char*)Regs->Arg1, // Device
237 (char*)Regs->Arg2, // Mount point
238 (char*)Regs->Arg3, // Filesystem
239 (char*)Regs->Arg4 // Options
245 Log((char*)Regs->Arg1,
246 Regs->Arg2, Regs->Arg3, Regs->Arg4, Regs->Arg5, Regs->Arg6);
249 // -- Default (Return Error)
251 Warning("SyscallHandler: Unknown System Call %i", Regs->Num);
252 if(Regs->Num < NUM_SYSCALLS)
253 Warning(" Syscall '%s'", cSYSCALL_NAMES[Regs->Num]);
259 Regs->Return = ret&0xFFFFFFFF;
260 Regs->RetHi = ret >> 32;
266 LOG("SyscallHandler: err = %i", err);
272 * \fn int Syscall_ValidString(Uint Addr)
273 * \brief Checks if a memory address contains a valid string
275 int Syscall_ValidString(Uint Addr)
278 if(!MM_GetPhysAddr(Addr)) return 0;
283 if(!MM_GetPhysAddr(Addr)) return 0;
284 // Increment string pointer
292 * \fn int Syscall_Valid(int Size, Uint Addr)
293 * \brief Checks if a memory address is valid
295 int Syscall_Valid(int Size, Uint Addr)
299 if(!MM_GetPhysAddr(Addr)) return 0;