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...) do{if(gbSyscallDebugEnabled)Debug(v);}while(0)
15 #define PAGE_SIZE 4096
17 #define TODO() Warning("TODO: %s", __func__)
19 typedef struct sFILE FILE;
21 extern void exit(int) __attribute__ ((noreturn));
22 extern int printf(const char *, ...);
23 extern int fprintf(FILE *,const char *, ...);
24 extern int sprintf(char *,const char *, ...);
25 extern int vprintf(const char *, va_list);
26 extern int strncmp(const char *, const char *, size_t);
29 extern int giSyscall_ClientID; // Needed for execve
30 extern void _InitSyscalls(void);
31 extern void _CloseSyscalls(void);
33 extern void Warning(const char *Format, ...);
34 extern void Debug(const char *Format, ...);
35 extern int AllocateMemory(uintptr_t VirtAddr, size_t ByteCount);
38 #define NATIVE_FILE_MASK 0x40000000
42 int gbSyscallDebugEnabled = 0;
43 char *gsExecutablePath = "./ld-acess";
47 int acess__SysChdir(const char *Path)
49 return _Syscall(SYS_CHDIR, ">s", Path);
52 int acess__SysOpen(const char *Path, unsigned int Flags)
54 if( strncmp(Path, "$$$$", 4) == 0 )
56 return native_open(Path, Flags) | NATIVE_FILE_MASK;
58 SYSTRACE("open(\"%s\", 0x%x)", Path, Flags);
59 return _Syscall(SYS_OPEN, ">s >i", Path, Flags);
62 void acess__SysClose(int FD)
64 if(FD & NATIVE_FILE_MASK) {
65 return native_close(FD & (NATIVE_FILE_MASK-1));
67 SYSTRACE("close(%i)", FD);
68 _Syscall(SYS_CLOSE, ">i", FD);
71 int acess__SysReopen(int FD, const char *Path, int Flags) {
72 SYSTRACE("reopen(0x%x, \"%s\", 0x%x)", FD, Path, Flags);
73 return _Syscall(SYS_REOPEN, ">i >s >i", FD, Path, Flags);
76 int acess__SysCopyFD(int srcfd, int dstfd) {
77 SYSTRACE("_SysCopyFD(%i, %i)", srcfd, dstfd);
78 return _Syscall(SYS_COPYFD, ">i >i", srcfd, dstfd);
81 int acess__SysFDFlags(int fd, int mask, int newflags) {
82 return _Syscall(SYS_FDFLAGS, ">i >i >i", fd, mask, newflags);
85 size_t acess__SysRead(int FD, void *Dest, size_t Bytes) {
86 if(FD & NATIVE_FILE_MASK)
87 return native_read(FD & (NATIVE_FILE_MASK-1), Dest, Bytes);
88 SYSTRACE("_SysRead(0x%x, 0x%x, *%p)", FD, Bytes, Dest);
89 return _Syscall(SYS_READ, ">i >i <d", FD, Bytes, Bytes, Dest);
92 size_t acess__SysWrite(int FD, const void *Src, size_t Bytes) {
93 if(FD & NATIVE_FILE_MASK)
94 return native_write(FD & (NATIVE_FILE_MASK-1), Src, Bytes);
95 SYSTRACE("_SysWrite(0x%x, 0x%x, %p\"%.*s\")", FD, Bytes, Src, Bytes, (char*)Src);
96 return _Syscall(SYS_WRITE, ">i >i >d", FD, Bytes, Bytes, Src);
98 uint64_t acess__SysTruncate(int fd, uint64_t size) {
103 int acess__SysSeek(int FD, int64_t Ofs, int Dir)
105 if(FD & NATIVE_FILE_MASK) {
106 return native_seek(FD & (NATIVE_FILE_MASK-1), Ofs, Dir);
108 SYSTRACE("_SysSeek(0x%x, 0x%llx, %i)", FD, Ofs, Dir);
109 return _Syscall(SYS_SEEK, ">i >I >i", FD, Ofs, Dir);
112 uint64_t acess__SysTell(int FD)
114 if(FD & NATIVE_FILE_MASK)
115 return native_tell( FD & (NATIVE_FILE_MASK-1) );
116 SYSTRACE("_SysTell(0x%x)", FD);
117 return _Syscall(SYS_TELL, ">i", FD);
120 int acess__SysIOCtl(int fd, int id, void *data) {
122 SYSTRACE("_SysIOCtl(%i, %i, %p)", fd, id, data);
123 // NOTE: The length here is hacky and could break
124 len = (data == NULL ? 0 : PAGE_SIZE - ((uintptr_t)data % PAGE_SIZE));
125 return _Syscall(SYS_IOCTL, ">i >i ?d", fd, id, len, data);
127 int acess__SysFInfo(int fd, t_sysFInfo *info, int maxacls) {
128 SYSTRACE("_SysFInfo(%i, %p, %i)", fd, info, maxacls);
129 return _Syscall(SYS_FINFO, ">i <d >i",
131 sizeof(t_sysFInfo)+maxacls*sizeof(t_sysACL), info,
136 int acess__SysReadDir(int fd, char *dest) {
137 SYSTRACE("_SysReadDir(%i, %p)", fd, dest);
138 return _Syscall(SYS_READDIR, ">i <d", fd, 256, dest);
141 int acess__SysSelect(int nfds, fd_set *read, fd_set *write, fd_set *error, int64_t *timeout, uint32_t events)
143 SYSTRACE("_SysSelect(%i, %p, %p, %p, %p, 0x%x)", nfds, read, write, error, timeout, events);
144 return _Syscall(SYS_SELECT, ">i ?d ?d ?d >d >i", nfds,
145 read ? (nfds+7)/8 : 0, read,
146 write ? (nfds+7)/8 : 0, write,
147 error ? (nfds+7)/8 : 0, error,
148 sizeof(*timeout), timeout,
152 int acess__SysMkDir(const char *pathname)
157 int acess__SysUnlink(const char *pathname)
163 void* acess__SysMMap(void *addr, size_t length, unsigned int _flags, int fd, uint64_t offset)
168 int acess__SysMUnMap(void *addr, size_t length)
173 uint64_t acess__SysMarshalFD(int FD)
178 int acess__SysUnMarshalFD(uint64_t Handle)
184 int acess__SysOpenChild(int fd, char *name, int flags) {
185 SYSTRACE("_SysOpenChild(0x%x, '%s', 0x%x)", fd, name, flags);
186 return _Syscall(SYS_OPENCHILD, ">i >s >i", fd, name, flags);
189 int acess__SysGetACL(int fd, t_sysACL *dest) {
190 SYSTRACE("%s(0x%x, %p)", __func__, fd, dest);
191 return _Syscall(SYS_GETACL, ">i <d", fd, sizeof(t_sysACL), dest);
194 int acess__SysMount(const char *Device, const char *Directory, const char *Type, const char *Options) {
195 SYSTRACE("_SysMount('%s', '%s', '%s', '%s')", Device, Directory, Type, Options);
196 return _Syscall(SYS_MOUNT, ">s >s >s >s", Device, Directory, Type, Options);
201 int acess__SysSetFaultHandler(int (*Handler)(int)) {
202 printf("TODO: Set fault handler (asked to set to %p)\n", Handler);
206 void acess__SysSetName(const char *Name)
212 int acess__SysGetName(char *NameDest)
219 int acess__SysSetPri(int Priority)
227 void *acess_SysLoadBin(const char *path, void **entry)
234 int acess__SysUnloadBin(void *base)
242 int acess__SysLoadModule(const char *Path)
248 // --- Timekeeping ---
249 int64_t acess__SysTimestamp(void)
253 // return now()*1000;
257 // --- Memory Management ---
258 uint64_t acess__SysGetPhys(uintptr_t vaddr)
265 uint64_t acess__SysAllocate(uintptr_t vaddr)
267 if( AllocateMemory(vaddr, 0x1000) == -1 ) // Allocate a page
270 return vaddr; // Just ignore the need for paddrs :)
273 // --- Process Management ---
274 int acess__SysClone(int flags, void *stack)
277 Warning("Win32 does not support anything like fork(2), cannot emulate");
280 extern int fork(void);
281 if(flags & CLONE_VM) {
282 int ret, newID, kernel_tid=0;
283 Debug("USERSIDE fork()");
285 newID = _Syscall(SYS_AN_FORK, "<d", sizeof(int), &kernel_tid);
294 giSyscall_ClientID = newID;
299 // Return the acess TID instead
304 Warning("ERROR: Threads currently unsupported\n");
310 int acess__SysKill(int pid, int sig)
312 // TODO: Impliment SysKill
316 int acess__SysExecVE(char *path, char **argv, const char **envp)
320 DEBUG("acess_execve: (path='%s', argv=%p, envp=%p)", path, argv, envp);
322 // Get argument count
323 for( argc = 0; argv[argc]; argc ++ ) ;
324 DEBUG(" acess_execve: argc = %i", argc);
326 const char *new_argv[7+argc+1];
327 char client_id_str[11];
328 char socket_fd_str[11];
329 sprintf(client_id_str, "%i", giSyscall_ClientID);
330 sprintf(socket_fd_str, "%i", gSocket);
331 new_argv[0] = "ld-acess"; // TODO: Get path to ld-acess executable
332 new_argv[1] = "--key"; // Set client ID for Request.c
333 new_argv[2] = client_id_str;
334 new_argv[3] = "--socket"; // Socket
335 new_argv[4] = socket_fd_str;
336 new_argv[5] = "--binary"; // Set the binary path (instead of using argv[0])
338 for( i = 0; i < argc; i ++ ) new_argv[7+i] = argv[i];
339 new_argv[7+i] = NULL;
343 for( i = 0; i < argc; i ++ )
344 printf("\"%s\" ", new_argv[i]);
348 printf("envp = %p\n", envp);
349 for( i = 0; envp[i]; i ++ )
350 printf("%i: \"%s\"\n", i, envp[i]);
351 printf("envc = %i\n", i);
355 // Call actual execve
356 return native_execve("./ld-acess", new_argv, envp);
359 int acess__SysSpawn(const char *binary, const char **argv, const char **envp, int nfd, int fds[], struct s_sys_spawninfo *info)
362 while( argv[argc++] );
364 Debug("_SysSpawn('%s', %p (%i), %p, %i, %p, %p)",
365 binary, argv, argc, envp, nfd, fds, info);
369 newID = _Syscall(SYS_AN_SPAWN, "<d >d >d",
370 sizeof(int), &kernel_tid,
371 nfd*sizeof(int), fds,
372 info ? sizeof(*info) : 0, info);
374 const char *new_argv[5+argc+1];
376 char client_id_str[11];
377 sprintf(client_id_str, "%i", newID);
378 new_argv[new_argc++] = gsExecutablePath; // TODO: Get path to ld-acess executable
379 new_argv[new_argc++] = "--key";
380 new_argv[new_argc++] = client_id_str;
381 new_argv[new_argc++] = "--binary";
382 new_argv[new_argc++] = binary;
383 for( i = 0; argv[i]; i ++)
384 new_argv[new_argc++] = argv[i];
385 new_argv[new_argc++] = NULL;
387 // TODO: Debug output?
389 native_spawn(gsExecutablePath, new_argv, envp);
394 //void acess_sleep(void)
396 // DEBUG("%s()", __func__);
397 // _Syscall(SYS_SLEEP, "");
400 void acess__SysTimedSleep(int64_t Delay)
402 DEBUG("%s(%lli)", __func__, Delay);
403 // Not accurate, but fuck it
404 //if( Delay > 1000 ) sleep(Delay / 1000);
405 //if( Delay % 1000 ) usleep( (Delay % 1000) * 1000 );
406 //_Syscall(SYS_TIMEDSLEEP, ">I", Delay);
409 int acess__SysWaitTID(int TID, int *ExitStatus)
411 DEBUG("%s(%i, %p)", __func__, TID, ExitStatus);
412 return _Syscall(SYS_WAITTID, ">i <d", TID, sizeof(int), &ExitStatus);
415 int acess_setuid(int ID) { return _Syscall(SYS_SETUID, ">i", ID); }
416 int acess_setgid(int ID) { return _Syscall(SYS_SETGID, ">i", ID); }
417 int acess_gettid(void) { return _Syscall(SYS_GETTID, ""); }
418 int acess__SysGetPID(void) { return _Syscall(SYS_GETPID, ""); }
419 int acess__SysGetUID(void) { return _Syscall(SYS_GETUID, ""); }
420 int acess__SysGetGID(void) { return _Syscall(SYS_GETGID, ""); }
421 int acess_getgid(void) { return _Syscall(SYS_GETGID, ""); }
423 int acess__SysSendMessage(int DestTID, int Length, void *Data)
425 DEBUG("%s(%i, 0x%x, %p)", __func__, DestTID, Length, Data);
426 return _Syscall(SYS_SENDMSG, ">i >d", DestTID, Length, Data);
429 int acess__SysGetMessage(int *SourceTID, int BufLen, void *Data)
431 DEBUG("%s(%p, %p)", __func__, SourceTID, Data);
432 return _Syscall(SYS_GETMSG, "<d <d",
433 SourceTID ? sizeof(uint32_t) : 0, SourceTID,
438 int acess__SysWaitEvent(int Mask)
440 DEBUG("%s(%x)", __func__, Mask);
441 return _Syscall(SYS_WAITEVENT, ">i", Mask);
445 void acess__SysDebug(const char *Format, ...)
449 va_start(args, Format);
451 printf("[_SysDebug %i] ", giSyscall_ClientID);
452 vprintf(Format, args);
458 void acess__exit(int Status)
460 DEBUG("_exit(%i)", Status);
461 _Syscall(SYS_EXIT, ">i", Status);
465 uint32_t acess__SysSetMemFlags(uintptr_t vaddr, uint32_t flags, uint32_t mask)
467 // TODO: Impliment acess__SysSetMemFlags?
472 // === Symbol List ===
474 # define DEFSYM(name) {#name, &acess_##name}
476 const tSym caBuiltinSymbols[] = {
481 DEFSYM(_SysOpenChild),
510 DEFSYM(_SysSendMessage),
511 DEFSYM(_SysGetMessage),
513 DEFSYM(_SysAllocate),
514 DEFSYM(_SysSetMemFlags),
516 {"_ZN4_sys5debugEPKcz", &acess__SysDebug},
517 DEFSYM(_SysSetFaultHandler),
518 DEFSYM(_SysWaitEvent),
523 const int ciNumBuiltinSymbols = sizeof(caBuiltinSymbols)/sizeof(caBuiltinSymbols[0]);