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__SysMkDir(const char *pathname)
153 DEBUG("TODO: _SysMkDir");
156 int acess__SysUnlink(const char *pathname)
159 DEBUG("TODO: _Unlink");
163 int acess__SysOpenChild(int fd, char *name, int flags) {
164 DEBUG("_SysOpenChild(0x%x, '%s', 0x%x)", fd, name, flags);
165 return _Syscall(SYS_OPENCHILD, ">i >s >i", fd, name, flags);
168 int acess__SysGetACL(int fd, t_sysACL *dest) {
169 DEBUG("%s(0x%x, %p)", __func__, fd, dest);
170 return _Syscall(SYS_GETACL, ">i <d", fd, sizeof(t_sysACL), dest);
173 int acess__SysMount(const char *Device, const char *Directory, const char *Type, const char *Options) {
174 DEBUG("%s('%s', '%s', '%s', '%s')", __func__, Device, Directory, Type, Options);
175 return _Syscall(SYS_MOUNT, ">s >s >s >s", Device, Directory, Type, Options);
180 int acess__SysSetFaultHandler(int (*Handler)(int)) {
181 printf("TODO: Set fault handler (asked to set to %p)\n", Handler);
185 void acess__SysSetName(const char *Name)
190 int acess__SysGetName(char *NameDest)
196 int acess__SysSetPri(int Priority)
203 void *acess_SysLoadBin(const char *path, void **entry)
209 int acess__SysUnloadBin(void *base)
215 // --- Timekeeping ---
216 int64_t acess__SysTimestamp(void)
219 // return now()*1000;
223 // --- Memory Management ---
224 uint64_t acess__SysGetPhys(uintptr_t vaddr)
230 uint64_t acess__SysAllocate(uintptr_t vaddr)
232 if( AllocateMemory(vaddr, 0x1000) == -1 ) // Allocate a page
235 return vaddr; // Just ignore the need for paddrs :)
238 // --- Process Management ---
239 int acess__SysClone(int flags, void *stack)
242 Warning("Win32 does not support anything like fork(2), cannot emulate");
245 extern int fork(void);
246 if(flags & CLONE_VM) {
247 int ret, newID, kernel_tid=0;
248 Debug("USERSIDE fork()");
250 newID = _Syscall(SYS_AN_FORK, "<d", sizeof(int), &kernel_tid);
259 giSyscall_ClientID = newID;
264 // Return the acess TID instead
269 Warning("ERROR: Threads currently unsupported\n");
275 int acess__SysKill(int pid, int sig)
277 // TODO: Impliment SysKill
281 int acess__SysExecVE(char *path, char **argv, const char **envp)
285 DEBUG("acess_execve: (path='%s', argv=%p, envp=%p)", path, argv, envp);
287 // Get argument count
288 for( argc = 0; argv[argc]; argc ++ ) ;
289 DEBUG(" acess_execve: argc = %i", argc);
291 const char *new_argv[7+argc+1];
292 char client_id_str[11];
293 char socket_fd_str[11];
294 sprintf(client_id_str, "%i", giSyscall_ClientID);
295 sprintf(socket_fd_str, "%i", gSocket);
296 new_argv[0] = "ld-acess"; // TODO: Get path to ld-acess executable
297 new_argv[1] = "--key"; // Set client ID for Request.c
298 new_argv[2] = client_id_str;
299 new_argv[3] = "--socket"; // Socket
300 new_argv[4] = socket_fd_str;
301 new_argv[5] = "--binary"; // Set the binary path (instead of using argv[0])
303 for( i = 0; i < argc; i ++ ) new_argv[7+i] = argv[i];
304 new_argv[7+i] = NULL;
308 for( i = 0; i < argc; i ++ )
309 printf("\"%s\" ", new_argv[i]);
313 printf("envp = %p\n", envp);
314 for( i = 0; envp[i]; i ++ )
315 printf("%i: \"%s\"\n", i, envp[i]);
316 printf("envc = %i\n", i);
320 // Call actual execve
321 return native_execve("./ld-acess", new_argv, envp);
324 int acess__SysSpawn(const char *binary, const char **argv, const char **envp, int nfd, int fds[], struct s_sys_spawninfo *info)
327 while( argv[argc++] );
329 Debug("_SysSpawn('%s', %p (%i), %p, %i, %p, %p)",
330 binary, argv, argc, envp, nfd, fds, info);
334 newID = _Syscall(SYS_AN_SPAWN, "<d >d >d", sizeof(int), &kernel_tid,
335 nfd*sizeof(int), fds,
336 info ? sizeof(*info) : 0, info);
338 const char *new_argv[5+argc+1];
340 char client_id_str[11];
341 sprintf(client_id_str, "%i", newID);
342 new_argv[new_argc++] = gsExecutablePath; // TODO: Get path to ld-acess executable
343 new_argv[new_argc++] = "--key";
344 new_argv[new_argc++] = client_id_str;
345 new_argv[new_argc++] = "--binary";
346 new_argv[new_argc++] = binary;
347 for( i = 0; argv[i]; i ++)
348 new_argv[new_argc++] = argv[i];
349 new_argv[new_argc++] = NULL;
351 // TODO: Debug output?
353 native_spawn(gsExecutablePath, new_argv, envp);
358 //void acess_sleep(void)
360 // DEBUG("%s()", __func__);
361 // _Syscall(SYS_SLEEP, "");
364 int acess__SysWaitTID(int TID, int *ExitStatus)
366 DEBUG("%s(%i, %p)", __func__, TID, ExitStatus);
367 return _Syscall(SYS_WAITTID, ">i <d", TID, sizeof(int), &ExitStatus);
370 int acess_setuid(int ID) { return _Syscall(SYS_SETUID, ">i", ID); }
371 int acess_setgid(int ID) { return _Syscall(SYS_SETGID, ">i", ID); }
372 int acess_gettid(void) { return _Syscall(SYS_GETTID, ""); }
373 int acess__SysGetPID(void) { return _Syscall(SYS_GETPID, ""); }
374 int acess__SysGetUID(void) { return _Syscall(SYS_GETUID, ""); }
375 int acess_getgid(void) { return _Syscall(SYS_GETGID, ""); }
377 int acess__SysSendMessage(int DestTID, int Length, void *Data)
379 DEBUG("%s(%i, 0x%x, %p)", __func__, DestTID, Length, Data);
380 return _Syscall(SYS_SENDMSG, ">i >d", DestTID, Length, Data);
383 int acess__SysGetMessage(int *SourceTID, int BufLen, void *Data)
385 DEBUG("%s(%p, %p)", __func__, SourceTID, Data);
386 return _Syscall(SYS_GETMSG, "<d <d",
387 SourceTID ? sizeof(uint32_t) : 0, SourceTID,
392 int acess__SysWaitEvent(int Mask)
394 DEBUG("%s(%x)", __func__, Mask);
395 return _Syscall(SYS_WAITEVENT, ">i", Mask);
399 void acess__SysDebug(const char *Format, ...)
403 va_start(args, Format);
405 printf("[_SysDebug %i] ", giSyscall_ClientID);
406 vprintf(Format, args);
412 void acess__exit(int Status)
414 DEBUG("_exit(%i)", Status);
415 _Syscall(SYS_EXIT, ">i", Status);
419 uint32_t acess__SysSetMemFlags(uintptr_t vaddr, uint32_t flags, uint32_t mask)
421 // TODO: Impliment acess__SysSetMemFlags?
426 // === Symbol List ===
428 # define DEFSYM(name) {#name, &acess_##name}
430 const tSym caBuiltinSymbols[] = {
435 DEFSYM(_SysOpenChild),
464 DEFSYM(_SysSendMessage),
465 DEFSYM(_SysGetMessage),
467 DEFSYM(_SysAllocate),
468 DEFSYM(_SysSetMemFlags),
470 DEFSYM(_SysSetFaultHandler),
471 DEFSYM(_SysWaitEvent),
476 const int ciNumBuiltinSymbols = sizeof(caBuiltinSymbols)/sizeof(caBuiltinSymbols[0]);