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 PAGE_SIZE 4096
17 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 Debug(const char *Format, ...);
33 extern int AllocateMemory(uintptr_t VirtAddr, size_t ByteCount);
36 #define NATIVE_FILE_MASK 0x40000000
40 int acess_chdir(const char *Path)
42 return _Syscall(SYS_CHDIR, ">s", Path);
45 int acess_open(const char *Path, int Flags)
47 if( strncmp(Path, "$$$$", 4) == 0 )
49 return native_open(Path, Flags) | NATIVE_FILE_MASK;
51 DEBUG("open(\"%s\", 0x%x)", Path, Flags);
52 return _Syscall(SYS_OPEN, ">s >i", Path, Flags);
55 void acess_close(int FD) {
56 if(FD & NATIVE_FILE_MASK) {
57 return native_close(FD & (NATIVE_FILE_MASK-1));
59 DEBUG("close(%i)", FD);
60 _Syscall(SYS_CLOSE, ">i", FD);
63 int acess_reopen(int FD, const char *Path, int Flags) {
64 DEBUG("reopen(0x%x, \"%s\", 0x%x)", FD, Path, Flags);
65 return _Syscall(SYS_REOPEN, ">i >s >i", FD, Path, Flags);
68 size_t acess_read(int FD, void *Dest, size_t Bytes) {
69 if(FD & NATIVE_FILE_MASK)
70 return native_read(FD & (NATIVE_FILE_MASK-1), Dest, Bytes);
72 DEBUG("read(0x%x, 0x%x, *%p)", FD, Bytes, Dest);
73 return _Syscall(SYS_READ, ">i >i <d", FD, Bytes, Bytes, Dest);
76 size_t acess_write(int FD, const void *Src, size_t Bytes) {
77 if(FD & NATIVE_FILE_MASK)
78 return native_write(FD & (NATIVE_FILE_MASK-1), Src, Bytes);
80 DEBUG("write(0x%x, 0x%x, %p\"%.*s\")", FD, Bytes, Src, Bytes, (char*)Src);
81 return _Syscall(SYS_WRITE, ">i >i >d", FD, Bytes, Bytes, Src);
84 int acess_seek(int FD, int64_t Ofs, int Dir) {
85 if(FD & NATIVE_FILE_MASK) {
86 return native_seek(FD & (NATIVE_FILE_MASK-1), Ofs, Dir);
88 DEBUG("seek(0x%x, 0x%llx, %i)", FD, Ofs, Dir);
89 return _Syscall(SYS_SEEK, ">i >I >i", FD, Ofs, Dir);
92 uint64_t acess_tell(int FD) {
93 if(FD & NATIVE_FILE_MASK)
94 return native_tell( FD & (NATIVE_FILE_MASK-1) );
95 return _Syscall(SYS_TELL, ">i", FD);
98 int acess_ioctl(int fd, int id, void *data) {
100 DEBUG("ioctl(%i, %i, %p)", fd, id, data);
101 // NOTE: The length here is hacky and could break
105 len = PAGE_SIZE - ((uintptr_t)data % PAGE_SIZE);
106 return _Syscall(SYS_IOCTL, ">i >i ?d", fd, id, len, data);
108 int acess_finfo(int fd, t_sysFInfo *info, int maxacls) {
109 // DEBUG("offsetof(size, t_sysFInfo) = %i", offsetof(t_sysFInfo, size));
110 DEBUG("finfo(%i, %p, %i)", fd, info, maxacls);
111 return _Syscall(SYS_FINFO, ">i <d >i",
113 sizeof(t_sysFInfo)+maxacls*sizeof(t_sysACL), info,
118 int acess_readdir(int fd, char *dest) {
119 DEBUG("readdir(%i, %p)", fd, dest);
120 return _Syscall(SYS_READDIR, ">i <d", fd, 256, dest);
123 int acess__SysSelect(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout, uint32_t events)
125 DEBUG("_SysSelect(%i, %p, %p, %p, %p, 0x%x)", nfds, read, write, error, timeout, events);
126 return _Syscall(SYS_SELECT, ">i ?d ?d ?d >d >i", nfds,
127 read ? (nfds+7)/8 : 0, read,
128 write ? (nfds+7)/8 : 0, write,
129 error ? (nfds+7)/8 : 0, error,
130 sizeof(*timeout), timeout,
135 int acess_select(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout)
137 return acess__SysSelect(nfds, read, write, error, timeout, 0);
141 int acess__SysOpenChild(int fd, char *name, int flags) {
142 return _Syscall(SYS_OPENCHILD, ">i >s >i", fd, name, flags);
145 int acess__SysGetACL(int fd, t_sysACL *dest) {
146 return _Syscall(SYS_GETACL, ">i <d", fd, sizeof(t_sysACL), dest);
149 int acess__SysMount(const char *Device, const char *Directory, const char *Type, const char *Options) {
150 return _Syscall(SYS_MOUNT, ">s >s >s >s", Device, Directory, Type, Options);
155 int acess__SysSetFaultHandler(int (*Handler)(int)) {
156 printf("TODO: Set fault handler (asked to set to %p)\n", Handler);
160 // --- Memory Management ---
161 uint64_t acess__SysAllocate(uint vaddr)
163 if( AllocateMemory(vaddr, 0x1000) == -1 ) // Allocate a page
166 return vaddr; // Just ignore the need for paddrs :)
169 // --- Process Management ---
170 int acess_clone(int flags, void *stack)
172 extern int fork(void);
173 if(flags & CLONE_VM) {
174 int ret, newID, kernel_tid=0;
175 printf("USERSIDE fork()\n");
177 newID = _Syscall(SYS_AN_FORK, "<d", sizeof(int), &kernel_tid);
187 giSyscall_ClientID = newID;
191 // Return the acess TID instead
196 fprintf(stderr, "ERROR: Threads currently unsupported\n");
201 int acess_execve(char *path, char **argv, char **envp)
205 DEBUG("acess_execve: (path='%s', argv=%p, envp=%p)", path, argv, envp);
207 // Get argument count
208 for( argc = 0; argv[argc]; argc ++ ) ;
209 DEBUG(" acess_execve: argc = %i", argc);
211 char *new_argv[7+argc+1];
212 char client_id_str[11];
213 char socket_fd_str[11];
214 sprintf(client_id_str, "%i", giSyscall_ClientID);
215 sprintf(socket_fd_str, "%i", gSocket);
216 new_argv[0] = "ld-acess"; // TODO: Get path to ld-acess executable
217 new_argv[1] = "--key"; // Set client ID for Request.c
218 new_argv[2] = client_id_str;
219 new_argv[3] = "--socket"; // Socket
220 new_argv[4] = socket_fd_str;
221 new_argv[5] = "--binary"; // Set the binary path (instead of using argv[0])
223 for( i = 0; i < argc; i ++ ) new_argv[7+i] = argv[i];
224 new_argv[7+i] = NULL;
228 for( i = 0; i < argc; i ++ )
229 printf("\"%s\" ", new_argv[i]);
233 printf("envp = %p\n", envp);
234 for( i = 0; envp[i]; i ++ )
235 printf("%i: \"%s\"\n", i, envp[i]);
236 printf("envc = %i\n", i);
240 // Call actual execve
241 return native_execve("./ld-acess", new_argv, envp);
244 void acess_sleep(void)
246 _Syscall(SYS_SLEEP, "");
249 int acess_waittid(int TID, int *ExitStatus)
251 return _Syscall(SYS_WAITTID, ">i <d", TID, sizeof(int), &ExitStatus);
254 int acess_setuid(int ID) { return _Syscall(SYS_SETUID, ">i", ID); }
255 int acess_setgid(int ID) { return _Syscall(SYS_SETGID, ">i", ID); }
256 int acess_gettid(void) { return _Syscall(SYS_GETTID, ""); }
257 int acess_getpid(void) { return _Syscall(SYS_GETPID, ""); }
258 int acess_getuid(void) { return _Syscall(SYS_GETUID, ""); }
259 int acess_getgid(void) { return _Syscall(SYS_GETGID, ""); }
261 int acess_SysSendMessage(int DestTID, int Length, void *Data)
263 return _Syscall(SYS_SENDMSG, ">i >d", DestTID, Length, Data);
266 int acess_SysGetMessage(int *SourceTID, void *Data)
268 // static __thread int lastlen = 1024;
271 lastlen = _Syscall(SYS_GETMSG, "<d <d",
272 SourceTID ? sizeof(uint32_t) : 0, SourceTID,
273 Data ? 1024 : 0, Data
278 int acess__SysWaitEvent(int Mask)
280 return _Syscall(SYS_WAITEVENT, ">i", Mask);
284 void acess__SysDebug(const char *Format, ...)
288 va_start(args, Format);
290 printf("[_SysDebug %i] ", giSyscall_ClientID);
291 vprintf(Format, args);
297 void acess__exit(int Status)
299 DEBUG("_exit(%i)", Status);
300 _Syscall(SYS_EXIT, ">i", Status);
305 // === Symbol List ===
306 #define DEFSYM(name) {#name, acess_##name}
307 const tSym caBuiltinSymbols[] = {
322 DEFSYM(_SysOpenChild),
336 DEFSYM(SysSendMessage),
337 DEFSYM(SysGetMessage),
339 DEFSYM(_SysAllocate),
341 DEFSYM(_SysSetFaultHandler),
342 DEFSYM(_SysWaitEvent)
345 const int ciNumBuiltinSymbols = sizeof(caBuiltinSymbols)/sizeof(caBuiltinSymbols[0]);