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{}while(0)//Debug(v)
15 #define PAGE_SIZE 4096
17 typedef struct sFILE FILE;
19 extern void exit(int) __attribute__ ((noreturn));
20 extern int printf(const char *, ...);
21 extern int fprintf(FILE *,const char *, ...);
22 extern int sprintf(char *,const char *, ...);
23 extern int vprintf(const char *, va_list);
24 extern int strncmp(const char *, const char *, size_t);
27 extern int giSyscall_ClientID; // Needed for execve
28 extern void _InitSyscalls(void);
29 extern void _CloseSyscalls(void);
31 extern void Warning(const char *Format, ...);
32 extern void Debug(const char *Format, ...);
33 extern int AllocateMemory(uintptr_t VirtAddr, size_t ByteCount);
36 #define NATIVE_FILE_MASK 0x40000000
43 int acess_chdir(const char *Path)
45 return _Syscall(SYS_CHDIR, ">s", Path);
48 int acess_open(const char *Path, int Flags)
50 if( strncmp(Path, "$$$$", 4) == 0 )
52 return native_open(Path, Flags) | NATIVE_FILE_MASK;
54 DEBUG("open(\"%s\", 0x%x)", Path, Flags);
55 return _Syscall(SYS_OPEN, ">s >i", Path, Flags);
58 void acess_close(int FD) {
59 if(FD & NATIVE_FILE_MASK) {
60 return native_close(FD & (NATIVE_FILE_MASK-1));
62 DEBUG("close(%i)", FD);
63 _Syscall(SYS_CLOSE, ">i", FD);
66 int acess_reopen(int FD, const char *Path, int Flags) {
67 DEBUG("reopen(0x%x, \"%s\", 0x%x)", FD, Path, Flags);
68 return _Syscall(SYS_REOPEN, ">i >s >i", FD, Path, Flags);
71 size_t acess_read(int FD, void *Dest, size_t Bytes) {
72 if(FD & NATIVE_FILE_MASK)
73 return native_read(FD & (NATIVE_FILE_MASK-1), Dest, Bytes);
75 DEBUG("read(0x%x, 0x%x, *%p)", FD, Bytes, Dest);
76 return _Syscall(SYS_READ, ">i >i <d", FD, Bytes, Bytes, Dest);
79 size_t acess_write(int FD, const void *Src, size_t Bytes) {
80 if(FD & NATIVE_FILE_MASK)
81 return native_write(FD & (NATIVE_FILE_MASK-1), Src, Bytes);
83 DEBUG("write(0x%x, 0x%x, %p\"%.*s\")", FD, Bytes, Src, Bytes, (char*)Src);
84 return _Syscall(SYS_WRITE, ">i >i >d", FD, Bytes, Bytes, Src);
87 int acess_seek(int FD, int64_t Ofs, int Dir) {
88 if(FD & NATIVE_FILE_MASK) {
89 return native_seek(FD & (NATIVE_FILE_MASK-1), Ofs, Dir);
91 DEBUG("seek(0x%x, 0x%llx, %i)", FD, Ofs, Dir);
92 return _Syscall(SYS_SEEK, ">i >I >i", FD, Ofs, Dir);
95 uint64_t acess_tell(int FD) {
96 if(FD & NATIVE_FILE_MASK)
97 return native_tell( FD & (NATIVE_FILE_MASK-1) );
98 DEBUG("tell(0x%x)", FD);
99 return _Syscall(SYS_TELL, ">i", FD);
102 int acess_ioctl(int fd, int id, void *data) {
104 DEBUG("ioctl(%i, %i, %p)", fd, id, data);
105 // NOTE: The length here is hacky and could break
109 len = PAGE_SIZE - ((uintptr_t)data % PAGE_SIZE);
110 return _Syscall(SYS_IOCTL, ">i >i ?d", fd, id, len, data);
112 int acess_finfo(int fd, t_sysFInfo *info, int maxacls) {
113 // DEBUG("offsetof(size, t_sysFInfo) = %i", offsetof(t_sysFInfo, size));
114 DEBUG("finfo(%i, %p, %i)", fd, info, maxacls);
115 return _Syscall(SYS_FINFO, ">i <d >i",
117 sizeof(t_sysFInfo)+maxacls*sizeof(t_sysACL), info,
122 int acess_SysReadDir(int fd, char *dest) {
123 DEBUG("SysReadDir(%i, %p)", fd, dest);
124 return _Syscall(SYS_READDIR, ">i <d", fd, 256, dest);
127 int acess__SysSelect(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout, uint32_t events)
129 DEBUG("_SysSelect(%i, %p, %p, %p, %p, 0x%x)", nfds, read, write, error, timeout, events);
130 return _Syscall(SYS_SELECT, ">i ?d ?d ?d >d >i", nfds,
131 read ? (nfds+7)/8 : 0, read,
132 write ? (nfds+7)/8 : 0, write,
133 error ? (nfds+7)/8 : 0, error,
134 sizeof(*timeout), timeout,
139 int acess_select(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout)
141 return acess__SysSelect(nfds, read, write, error, timeout, 0);
145 int acess__SysOpenChild(int fd, char *name, int flags) {
146 DEBUG("_SysOpenChild(0x%x, '%s', 0x%x)", fd, name, flags);
147 return _Syscall(SYS_OPENCHILD, ">i >s >i", fd, name, flags);
150 int acess__SysGetACL(int fd, t_sysACL *dest) {
151 DEBUG("%s(0x%x, %p)", __func__, fd, dest);
152 return _Syscall(SYS_GETACL, ">i <d", fd, sizeof(t_sysACL), dest);
155 int acess__SysMount(const char *Device, const char *Directory, const char *Type, const char *Options) {
156 DEBUG("%s('%s', '%s', '%s', '%s')", __func__, Device, Directory, Type, Options);
157 return _Syscall(SYS_MOUNT, ">s >s >s >s", Device, Directory, Type, Options);
162 int acess__SysSetFaultHandler(int (*Handler)(int)) {
163 printf("TODO: Set fault handler (asked to set to %p)\n", Handler);
167 // --- Memory Management ---
168 uint64_t acess__SysAllocate(uint vaddr)
170 if( AllocateMemory(vaddr, 0x1000) == -1 ) // Allocate a page
173 return vaddr; // Just ignore the need for paddrs :)
176 // --- Process Management ---
177 int acess_clone(int flags, void *stack)
180 Warning("Win32 does not support anything like fork(2), cannot emulate");
183 extern int fork(void);
184 if(flags & CLONE_VM) {
185 int ret, newID, kernel_tid=0;
186 Debug("USERSIDE fork()");
188 newID = _Syscall(SYS_AN_FORK, "<d", sizeof(int), &kernel_tid);
197 giSyscall_ClientID = newID;
202 // Return the acess TID instead
207 Warning("ERROR: Threads currently unsupported\n");
213 int acess_execve(char *path, char **argv, const char **envp)
217 DEBUG("acess_execve: (path='%s', argv=%p, envp=%p)", path, argv, envp);
219 // Get argument count
220 for( argc = 0; argv[argc]; argc ++ ) ;
221 DEBUG(" acess_execve: argc = %i", argc);
223 const char *new_argv[7+argc+1];
224 char client_id_str[11];
225 char socket_fd_str[11];
226 sprintf(client_id_str, "%i", giSyscall_ClientID);
227 sprintf(socket_fd_str, "%i", gSocket);
228 new_argv[0] = "ld-acess"; // TODO: Get path to ld-acess executable
229 new_argv[1] = "--key"; // Set client ID for Request.c
230 new_argv[2] = client_id_str;
231 new_argv[3] = "--socket"; // Socket
232 new_argv[4] = socket_fd_str;
233 new_argv[5] = "--binary"; // Set the binary path (instead of using argv[0])
235 for( i = 0; i < argc; i ++ ) new_argv[7+i] = argv[i];
236 new_argv[7+i] = NULL;
240 for( i = 0; i < argc; i ++ )
241 printf("\"%s\" ", new_argv[i]);
245 printf("envp = %p\n", envp);
246 for( i = 0; envp[i]; i ++ )
247 printf("%i: \"%s\"\n", i, envp[i]);
248 printf("envc = %i\n", i);
252 // Call actual execve
253 return native_execve("./ld-acess", new_argv, envp);
256 void acess_sleep(void)
258 DEBUG("%s()", __func__);
259 _Syscall(SYS_SLEEP, "");
262 int acess_waittid(int TID, int *ExitStatus)
264 DEBUG("%s(%i, %p)", __func__, TID, ExitStatus);
265 return _Syscall(SYS_WAITTID, ">i <d", TID, sizeof(int), &ExitStatus);
268 int acess_setuid(int ID) { return _Syscall(SYS_SETUID, ">i", ID); }
269 int acess_setgid(int ID) { return _Syscall(SYS_SETGID, ">i", ID); }
270 int acess_gettid(void) { return _Syscall(SYS_GETTID, ""); }
271 int acess_getpid(void) { return _Syscall(SYS_GETPID, ""); }
272 int acess_getuid(void) { return _Syscall(SYS_GETUID, ""); }
273 int acess_getgid(void) { return _Syscall(SYS_GETGID, ""); }
275 int acess_SysSendMessage(int DestTID, int Length, void *Data)
277 DEBUG("%s(%i, 0x%x, %p)", __func__, DestTID, Length, Data);
278 return _Syscall(SYS_SENDMSG, ">i >d", DestTID, Length, Data);
281 int acess_SysGetMessage(int *SourceTID, int BufLen, void *Data)
283 DEBUG("%s(%p, %p)", __func__, SourceTID, Data);
284 return _Syscall(SYS_GETMSG, "<d <d",
285 SourceTID ? sizeof(uint32_t) : 0, SourceTID,
290 int acess__SysWaitEvent(int Mask)
292 DEBUG("%s(%x)", __func__, Mask);
293 return _Syscall(SYS_WAITEVENT, ">i", Mask);
297 void acess__SysDebug(const char *Format, ...)
301 va_start(args, Format);
303 printf("[_SysDebug %i] ", giSyscall_ClientID);
304 vprintf(Format, args);
310 void acess__exit(int Status)
312 DEBUG("_exit(%i)", Status);
313 _Syscall(SYS_EXIT, ">i", Status);
318 // === Symbol List ===
319 #define DEFSYM(name) {#name, &acess_##name}
320 const tSym caBuiltinSymbols[] = {
335 DEFSYM(_SysOpenChild),
349 DEFSYM(SysSendMessage),
350 DEFSYM(SysGetMessage),
352 DEFSYM(_SysAllocate),
354 DEFSYM(_SysSetFaultHandler),
355 DEFSYM(_SysWaitEvent),
360 const int ciNumBuiltinSymbols = sizeof(caBuiltinSymbols)/sizeof(caBuiltinSymbols[0]);