7 #define DONT_INCLUDE_SYSCALL_NAMES 1
8 #include "../../Usermode/Libraries/ld-acess.so_src/include_exp/acess/sys.h"
9 #include "../syscalls.h"
14 #define DEBUG(v...) Debug(v)
15 //#define DEBUG(v...) do{}while(0)//Debug(v)
16 #define PAGE_SIZE 4096
18 typedef struct sFILE FILE;
20 extern void exit(int) __attribute__ ((noreturn));
21 extern int printf(const char *, ...);
22 extern int fprintf(FILE *,const char *, ...);
23 extern int sprintf(char *,const char *, ...);
24 extern int vprintf(const char *, va_list);
25 extern int strncmp(const char *, const char *, size_t);
28 extern int giSyscall_ClientID; // Needed for execve
29 extern void _InitSyscalls(void);
30 extern void _CloseSyscalls(void);
32 extern void Warning(const char *Format, ...);
33 extern void Debug(const char *Format, ...);
34 extern int AllocateMemory(uintptr_t VirtAddr, size_t ByteCount);
37 #define NATIVE_FILE_MASK 0x40000000
41 char *gsExecutablePath = "./ld-acess";
45 int acess__SysChdir(const char *Path)
47 return _Syscall(SYS_CHDIR, ">s", Path);
50 int acess__SysOpen(const char *Path, int Flags)
52 if( strncmp(Path, "$$$$", 4) == 0 )
54 return native_open(Path, Flags) | NATIVE_FILE_MASK;
56 DEBUG("open(\"%s\", 0x%x)", Path, Flags);
57 return _Syscall(SYS_OPEN, ">s >i", Path, Flags);
60 void acess__SysClose(int FD)
62 if(FD & NATIVE_FILE_MASK) {
63 return native_close(FD & (NATIVE_FILE_MASK-1));
65 DEBUG("close(%i)", FD);
66 _Syscall(SYS_CLOSE, ">i", FD);
69 int acess__SysReopen(int FD, const char *Path, int Flags) {
70 DEBUG("reopen(0x%x, \"%s\", 0x%x)", FD, Path, Flags);
71 return _Syscall(SYS_REOPEN, ">i >s >i", FD, Path, Flags);
74 int acess__SysCopyFD(int srcfd, int dstfd) {
75 return _Syscall(SYS_COPYFD, ">i >i", srcfd, dstfd);
78 int acess__SysFDFlags(int fd, int mask, int newflags) {
79 return _Syscall(SYS_FDFLAGS, ">i >i >i", fd, mask, newflags);
82 size_t acess__SysRead(int FD, void *Dest, size_t Bytes) {
83 if(FD & NATIVE_FILE_MASK)
84 return native_read(FD & (NATIVE_FILE_MASK-1), Dest, Bytes);
86 DEBUG("read(0x%x, 0x%x, *%p)", FD, Bytes, Dest);
87 return _Syscall(SYS_READ, ">i >i <d", FD, Bytes, Bytes, Dest);
90 size_t acess__SysWrite(int FD, const void *Src, size_t Bytes) {
91 if(FD & NATIVE_FILE_MASK)
92 return native_write(FD & (NATIVE_FILE_MASK-1), Src, Bytes);
94 DEBUG("write(0x%x, 0x%x, %p\"%.*s\")", FD, Bytes, Src, Bytes, (char*)Src);
95 return _Syscall(SYS_WRITE, ">i >i >d", FD, Bytes, Bytes, Src);
98 int acess__SysSeek(int FD, int64_t Ofs, int Dir)
100 if(FD & NATIVE_FILE_MASK) {
101 return native_seek(FD & (NATIVE_FILE_MASK-1), Ofs, Dir);
103 DEBUG("seek(0x%x, 0x%llx, %i)", FD, Ofs, Dir);
104 return _Syscall(SYS_SEEK, ">i >I >i", FD, Ofs, Dir);
107 uint64_t acess__SysTell(int FD)
109 if(FD & NATIVE_FILE_MASK)
110 return native_tell( FD & (NATIVE_FILE_MASK-1) );
111 DEBUG("tell(0x%x)", FD);
112 return _Syscall(SYS_TELL, ">i", FD);
115 int acess__SysIOCtl(int fd, int id, void *data) {
117 DEBUG("ioctl(%i, %i, %p)", fd, id, data);
118 // NOTE: The length here is hacky and could break
122 len = PAGE_SIZE - ((uintptr_t)data % PAGE_SIZE);
123 return _Syscall(SYS_IOCTL, ">i >i ?d", fd, id, len, data);
125 int acess__SysFInfo(int fd, t_sysFInfo *info, int maxacls) {
126 // DEBUG("offsetof(size, t_sysFInfo) = %i", offsetof(t_sysFInfo, size));
127 DEBUG("finfo(%i, %p, %i)", fd, info, maxacls);
128 return _Syscall(SYS_FINFO, ">i <d >i",
130 sizeof(t_sysFInfo)+maxacls*sizeof(t_sysACL), info,
135 int acess__SysReadDir(int fd, char *dest) {
136 DEBUG("SysReadDir(%i, %p)", fd, dest);
137 return _Syscall(SYS_READDIR, ">i <d", fd, 256, dest);
140 int acess__SysSelect(int nfds, fd_set *read, fd_set *write, fd_set *error, int64_t *timeout, uint32_t events)
142 DEBUG("_SysSelect(%i, %p, %p, %p, %p, 0x%x)", nfds, read, write, error, timeout, events);
143 return _Syscall(SYS_SELECT, ">i ?d ?d ?d >d >i", nfds,
144 read ? (nfds+7)/8 : 0, read,
145 write ? (nfds+7)/8 : 0, write,
146 error ? (nfds+7)/8 : 0, error,
147 sizeof(*timeout), timeout,
151 int acess__SysUnlink(const char *pathname)
157 int acess__SysOpenChild(int fd, char *name, int flags) {
158 DEBUG("_SysOpenChild(0x%x, '%s', 0x%x)", fd, name, flags);
159 return _Syscall(SYS_OPENCHILD, ">i >s >i", fd, name, flags);
162 int acess__SysGetACL(int fd, t_sysACL *dest) {
163 DEBUG("%s(0x%x, %p)", __func__, fd, dest);
164 return _Syscall(SYS_GETACL, ">i <d", fd, sizeof(t_sysACL), dest);
167 int acess__SysMount(const char *Device, const char *Directory, const char *Type, const char *Options) {
168 DEBUG("%s('%s', '%s', '%s', '%s')", __func__, Device, Directory, Type, Options);
169 return _Syscall(SYS_MOUNT, ">s >s >s >s", Device, Directory, Type, Options);
174 int acess__SysSetFaultHandler(int (*Handler)(int)) {
175 printf("TODO: Set fault handler (asked to set to %p)\n", Handler);
179 void acess__SysSetName(const char *Name)
184 int acess__SysGetName(char *NameDest)
190 int acess__SysSetPri(int Priority)
197 void *acess_SysLoadBin(const char *path, void **entry)
203 int acess__SysUnloadBin(void *base)
209 // --- Timekeeping ---
210 int64_t acess__SysTimestamp(void)
213 // return now()*1000;
217 // --- Memory Management ---
218 uint64_t acess__SysGetPhys(uintptr_t vaddr)
224 uint64_t acess__SysAllocate(uintptr_t vaddr)
226 if( AllocateMemory(vaddr, 0x1000) == -1 ) // Allocate a page
229 return vaddr; // Just ignore the need for paddrs :)
232 // --- Process Management ---
233 int acess__SysClone(int flags, void *stack)
236 Warning("Win32 does not support anything like fork(2), cannot emulate");
239 extern int fork(void);
240 if(flags & CLONE_VM) {
241 int ret, newID, kernel_tid=0;
242 Debug("USERSIDE fork()");
244 newID = _Syscall(SYS_AN_FORK, "<d", sizeof(int), &kernel_tid);
253 giSyscall_ClientID = newID;
258 // Return the acess TID instead
263 Warning("ERROR: Threads currently unsupported\n");
269 int acess__SysKill(int pid, int sig)
271 // TODO: Impliment SysKill
275 int acess__SysExecVE(char *path, char **argv, const char **envp)
279 DEBUG("acess_execve: (path='%s', argv=%p, envp=%p)", path, argv, envp);
281 // Get argument count
282 for( argc = 0; argv[argc]; argc ++ ) ;
283 DEBUG(" acess_execve: argc = %i", argc);
285 const char *new_argv[7+argc+1];
286 char client_id_str[11];
287 char socket_fd_str[11];
288 sprintf(client_id_str, "%i", giSyscall_ClientID);
289 sprintf(socket_fd_str, "%i", gSocket);
290 new_argv[0] = "ld-acess"; // TODO: Get path to ld-acess executable
291 new_argv[1] = "--key"; // Set client ID for Request.c
292 new_argv[2] = client_id_str;
293 new_argv[3] = "--socket"; // Socket
294 new_argv[4] = socket_fd_str;
295 new_argv[5] = "--binary"; // Set the binary path (instead of using argv[0])
297 for( i = 0; i < argc; i ++ ) new_argv[7+i] = argv[i];
298 new_argv[7+i] = NULL;
302 for( i = 0; i < argc; i ++ )
303 printf("\"%s\" ", new_argv[i]);
307 printf("envp = %p\n", envp);
308 for( i = 0; envp[i]; i ++ )
309 printf("%i: \"%s\"\n", i, envp[i]);
310 printf("envc = %i\n", i);
314 // Call actual execve
315 return native_execve("./ld-acess", new_argv, envp);
318 int acess__SysSpawn(const char *binary, const char **argv, const char **envp, int nfd, int fds[], struct s_sys_spawninfo *info)
321 while( argv[argc++] );
323 Debug("_SysSpawn('%s', %p (%i), %p, %i, %p, %p)",
324 binary, argv, argc, envp, nfd, fds, info);
328 newID = _Syscall(SYS_AN_SPAWN, "<d >d >d", sizeof(int), &kernel_tid,
329 nfd*sizeof(int), fds,
330 info ? sizeof(*info) : 0, info);
332 const char *new_argv[5+argc+1];
334 char client_id_str[11];
335 sprintf(client_id_str, "%i", newID);
336 new_argv[new_argc++] = gsExecutablePath; // TODO: Get path to ld-acess executable
337 new_argv[new_argc++] = "--key";
338 new_argv[new_argc++] = client_id_str;
339 new_argv[new_argc++] = "--binary";
340 new_argv[new_argc++] = binary;
341 for( i = 0; argv[i]; i ++)
342 new_argv[new_argc++] = argv[i];
343 new_argv[new_argc++] = NULL;
345 // TODO: Debug output?
347 native_spawn(gsExecutablePath, new_argv, envp);
352 //void acess_sleep(void)
354 // DEBUG("%s()", __func__);
355 // _Syscall(SYS_SLEEP, "");
358 int acess__SysWaitTID(int TID, int *ExitStatus)
360 DEBUG("%s(%i, %p)", __func__, TID, ExitStatus);
361 return _Syscall(SYS_WAITTID, ">i <d", TID, sizeof(int), &ExitStatus);
364 int acess_setuid(int ID) { return _Syscall(SYS_SETUID, ">i", ID); }
365 int acess_setgid(int ID) { return _Syscall(SYS_SETGID, ">i", ID); }
366 int acess_gettid(void) { return _Syscall(SYS_GETTID, ""); }
367 int acess__SysGetPID(void) { return _Syscall(SYS_GETPID, ""); }
368 int acess__SysGetUID(void) { return _Syscall(SYS_GETUID, ""); }
369 int acess_getgid(void) { return _Syscall(SYS_GETGID, ""); }
371 int acess__SysSendMessage(int DestTID, int Length, void *Data)
373 DEBUG("%s(%i, 0x%x, %p)", __func__, DestTID, Length, Data);
374 return _Syscall(SYS_SENDMSG, ">i >d", DestTID, Length, Data);
377 int acess__SysGetMessage(int *SourceTID, int BufLen, void *Data)
379 DEBUG("%s(%p, %p)", __func__, SourceTID, Data);
380 return _Syscall(SYS_GETMSG, "<d <d",
381 SourceTID ? sizeof(uint32_t) : 0, SourceTID,
386 int acess__SysWaitEvent(int Mask)
388 DEBUG("%s(%x)", __func__, Mask);
389 return _Syscall(SYS_WAITEVENT, ">i", Mask);
393 void acess__SysDebug(const char *Format, ...)
397 va_start(args, Format);
399 printf("[_SysDebug %i] ", giSyscall_ClientID);
400 vprintf(Format, args);
406 void acess__exit(int Status)
408 DEBUG("_exit(%i)", Status);
409 _Syscall(SYS_EXIT, ">i", Status);
413 uint32_t acess__SysSetMemFlags(uintptr_t vaddr, uint32_t flags, uint32_t mask)
415 // TODO: Impliment acess__SysSetMemFlags?
420 // === Symbol List ===
422 # define DEFSYM(name) {#name, &acess_##name}
424 const tSym caBuiltinSymbols[] = {
429 DEFSYM(_SysOpenChild),
456 DEFSYM(_SysSendMessage),
457 DEFSYM(_SysGetMessage),
459 DEFSYM(_SysAllocate),
460 DEFSYM(_SysSetMemFlags),
462 DEFSYM(_SysSetFaultHandler),
463 DEFSYM(_SysWaitEvent),
468 const int ciNumBuiltinSymbols = sizeof(caBuiltinSymbols)/sizeof(caBuiltinSymbols[0]);