Various Changes
[tpg/acess2.git] / Kernel / syscalls.c
1 /*
2  * AcessOS Microkernel Version
3  * syscalls.c
4  */
5 #define DEBUG   0
6
7 #include <common.h>
8 #include <syscalls.h>
9 #include <proc.h>
10 #include <errno.h>
11
12 // === IMPORTS ===
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();
27
28 // === PROTOTYPES ===
29  int    Syscall_ValidString(Uint Addr);
30  int    Syscall_Valid(int Size, Uint Addr);
31
32 // === CODE ===
33 // TODO: Do sanity checking on arguments, ATM the user can really fuck with the kernel
34 void SyscallHandler(tSyscallRegs *Regs)
35 {
36         Uint64  ret = 0;
37         Uint    err = 0;
38         #if DEBUG
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);
43         #endif
44         
45         switch(Regs->Num)
46         {
47         // -- Exit the current thread
48         case SYS_EXIT:  Threads_Exit(0, Regs->Arg1);    break;
49         
50         // -- Put the current thread to sleep
51         case SYS_SLEEP: Threads_Sleep();        break;
52         
53         // -- Yield current timeslice
54         case SYS_YIELD: Threads_Yield();        break;
55         
56         // -- Clone the current thread
57         case SYS_CLONE:
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;
63                 break;
64         
65         // -- Send a signal
66         case SYS_KILL:
67                 err = -ENOSYS;
68                 ret = -1;
69                 break;
70         
71         // -- Wait for a thread
72         case SYS_WAITTID:
73                 ret = Threads_WaitTID(Regs->Arg1, (void*)Regs->Arg2);
74                 break;
75         
76         // -- Get the physical address of a page
77         case SYS_GETPHYS:
78                 ret = MM_GetPhysAddr(Regs->Arg1);
79                 break;
80         
81         // -- Map an address
82         case SYS_MAP:   MM_Map(Regs->Arg1, Regs->Arg2); break;
83         
84         // -- Allocate an address
85         case SYS_ALLOCATE:      ret = MM_Allocate(Regs->Arg1);  break;
86         
87         // -- Unmap an address
88         case SYS_UNMAP:         MM_Deallocate(Regs->Arg1);      break;
89         
90         // -- Get Thread/Process IDs
91         case SYS_GETTID:        ret = Threads_GetTID(); break;
92         case SYS_GETPID:        ret = Threads_GetPID(); break;
93         
94         // -- Get User/Group IDs
95         case SYS_GETUID:        ret = Threads_GetUID(); break;
96         case SYS_GETGID:        ret = Threads_GetGID(); break;
97         
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;
101         
102         // -- Send Message
103         case SYS_SENDMSG:
104                 ret = Proc_SendMessage(&err, Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3);
105                 break;
106         // -- Check for messages
107         case SYS_GETMSG:
108                 ret = Proc_GetMessage(&err, (Uint*)Regs->Arg1, (void*)Regs->Arg2);
109                 break;
110         
111         // -- Set the thread's name
112         case SYS_SETNAME:
113                 // Sanity Check
114                 if(!Regs->Arg1) {       ret = -1;       err = -EINVAL;  break;  }
115                 Threads_SetName( (void*)Regs->Arg1 );
116                 break;
117         
118         // ---
119         // Binary Control
120         // ---
121         case SYS_EXECVE:
122                 if( !Syscall_ValidString(Regs->Arg1) ) {
123                         err = -EINVAL;
124                         ret = -1;
125                         break;
126                 }
127                 ret = Proc_Execve((char*)Regs->Arg1, (char**)Regs->Arg2, (char**)Regs->Arg3);
128                 break;
129         case SYS_LOADBIN:
130                 if( !Syscall_ValidString(Regs->Arg1)
131                 ||  !Syscall_Valid(sizeof(Uint), Regs->Arg2) ) {
132                         err = -EINVAL;
133                         ret = -1;
134                         break;
135                 }
136                 ret = Binary_Load((char*)Regs->Arg1, (Uint*)Regs->Arg2);
137                 break;
138         
139         // ---
140         // Virtual Filesystem
141         // ---
142         case SYS_OPEN:
143                 if( !Syscall_ValidString(Regs->Arg1) ) {
144                         err = -EINVAL;
145                         ret = -1;
146                         break;
147                 }
148                 ret = VFS_Open((char*)Regs->Arg1, Regs->Arg2 | VFS_OPENFLAG_USER);
149                 break;
150         
151         case SYS_CLOSE:
152                 VFS_Close( Regs->Arg1 );
153                 break;
154         
155         case SYS_SEEK:
156                 ret = VFS_Seek( Regs->Arg1, Regs->Arg2, Regs->Arg3);
157                 break;
158                 
159         case SYS_TELL:
160                 ret = VFS_Tell( Regs->Arg1 );
161                 break;
162         
163         case SYS_WRITE:
164                 #if BITS < 64
165                 ret = VFS_Write( Regs->Arg1, Regs->Arg2|((Uint64)Regs->Arg3<<32), (void*)Regs->Arg4 );
166                 #else
167                 ret = VFS_Write( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
168                 #endif
169                 break;
170         
171         case SYS_READ:
172                 #if BITS < 64
173                 ret = VFS_Read( Regs->Arg1, Regs->Arg2|((Uint64)Regs->Arg3<<32), (void*)Regs->Arg4 );
174                 #else
175                 ret = VFS_Read( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
176                 #endif
177                 break;
178         
179         case SYS_FINFO:
180                 ret = VFS_FInfo( Regs->Arg1, (void*)Regs->Arg2, Regs->Arg3 );
181                 break;
182         
183         // Get ACL Value
184         case SYS_GETACL:
185                 if( !Syscall_Valid(8, Regs->Arg1) ) {
186                         err = -EINVAL;
187                         ret = -1;
188                         break;
189                 }
190                 ret = VFS_GetACL( Regs->Arg1, (void*)Regs->Arg2 );
191                 break;
192         
193         // Read Directory
194         case SYS_READDIR:
195                 if( !Syscall_ValidString(Regs->Arg2) ) {
196                         err = -EINVAL;
197                         ret = -1;
198                         break;
199                 }
200                 ret = VFS_ReadDir( Regs->Arg1, (void*)Regs->Arg2 );
201                 break;
202         
203         // Change Directory
204         case SYS_CHDIR:
205                 if( !Syscall_ValidString(Regs->Arg1) ) {
206                         err = -EINVAL;
207                         ret = -1;
208                         break;
209                 }
210                 ret = VFS_ChDir( (void*)Regs->Arg1 );
211                 break;
212         
213         // IO Control
214         case SYS_IOCTL:
215                 ret = VFS_IOCtl( Regs->Arg1, Regs->Arg2, (void*)Regs->Arg3 );
216                 break;
217         
218         // Mount a filesystem
219         case SYS_MOUNT:
220                 // Only root can mount filesystems
221                 if(Threads_GetUID() != 0) {
222                         err = -EACCES;
223                         ret = -1;
224                         break;
225                 }
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) ) {
231                         err = -EINVAL;
232                         ret = -1;
233                         break;
234                 }
235                 ret = VFS_Mount(
236                         (char*)Regs->Arg1,      // Device
237                         (char*)Regs->Arg2,      // Mount point
238                         (char*)Regs->Arg3,      // Filesystem
239                         (char*)Regs->Arg4       // Options
240                         );
241                 break;
242         
243         // -- Debug
244         case SYS_DEBUG:
245                 Log((char*)Regs->Arg1,
246                         Regs->Arg2, Regs->Arg3, Regs->Arg4, Regs->Arg5, Regs->Arg6);
247                 break;
248         
249         // -- Default (Return Error)
250         default:
251                 Warning("SyscallHandler: Unknown System Call %i", Regs->Num);
252                 if(Regs->Num < NUM_SYSCALLS)
253                         Warning(" Syscall '%s'", cSYSCALL_NAMES[Regs->Num]);
254                 err = -ENOSYS;
255                 ret = -1;
256                 break;
257         }
258         #if BITS < 64
259         Regs->Return = ret&0xFFFFFFFF;
260         Regs->RetHi = ret >> 32;
261         #else
262         Regs->Return = ret;
263         #endif
264         Regs->Error = err;
265         #if DEBUG
266         LOG("SyscallHandler: err = %i", err);
267         LEAVE('x', ret);
268         #endif
269 }
270
271 /**
272  * \fn int Syscall_ValidString(Uint Addr)
273  * \brief Checks if a memory address contains a valid string
274  */
275 int Syscall_ValidString(Uint Addr)
276 {
277         // Check 1st page
278         if(!MM_GetPhysAddr(Addr))       return 0;
279         
280         // Traverse String
281         while(*(char*)Addr)
282         {
283                 if(!MM_GetPhysAddr(Addr))       return 0;
284                 // Increment string pointer
285                 Addr ++;
286         }
287         
288         return 1;
289 }
290
291 /**
292  * \fn int Syscall_Valid(int Size, Uint Addr)
293  * \brief Checks if a memory address is valid
294  */
295 int Syscall_Valid(int Size, Uint Addr)
296 {
297         while(Size--)
298         {
299                 if(!MM_GetPhysAddr(Addr))       return 0;
300                 Addr ++;
301         }
302         return 1;
303 }

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