7 #define DONT_INCLUDE_SYSCALL_NAMES 1
8 #include "../../Usermode/include/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);
71 DEBUG("read(0x%x, 0x%x, *%p)", FD, Bytes, Dest);
72 return _Syscall(SYS_READ, ">i >i <d", FD, Bytes, Bytes, Dest);
75 size_t acess_write(int FD, const void *Src, size_t Bytes) {
76 if(FD & NATIVE_FILE_MASK)
77 return native_write(FD & (NATIVE_FILE_MASK-1), Src, Bytes);
78 DEBUG("write(0x%x, 0x%x, %p\"%.*s\")", FD, Bytes, Src, Bytes, (char*)Src);
79 return _Syscall(SYS_WRITE, ">i >i >d", FD, Bytes, Bytes, Src);
82 int acess_seek(int FD, int64_t Ofs, int Dir) {
83 if(FD & NATIVE_FILE_MASK) {
84 return native_seek(FD & (NATIVE_FILE_MASK-1), Ofs, Dir);
86 DEBUG("seek(0x%x, 0x%llx, %i)", FD, Ofs, Dir);
87 return _Syscall(SYS_SEEK, ">i >I >i", FD, Ofs, Dir);
90 uint64_t acess_tell(int FD) {
91 if(FD & NATIVE_FILE_MASK)
92 return native_tell( FD & (NATIVE_FILE_MASK-1) );
93 return _Syscall(SYS_TELL, ">i", FD);
96 int acess_ioctl(int fd, int id, void *data) {
98 // NOTE: 1024 byte size is a hack
99 DEBUG("ioctl(%i, %i, %p)", fd, id, data);
103 len = PAGE_SIZE - ((uintptr_t)data % PAGE_SIZE);
104 return _Syscall(SYS_IOCTL, ">i >i ?d", fd, id, len, data);
106 int acess_finfo(int fd, t_sysFInfo *info, int maxacls) {
107 // DEBUG("offsetof(size, t_sysFInfo) = %i", offsetof(t_sysFInfo, size));
108 DEBUG("finfo(%i, %p, %i)", fd, info, maxacls);
109 return _Syscall(SYS_FINFO, ">i <d >i",
111 sizeof(t_sysFInfo)+maxacls*sizeof(t_sysACL), info,
116 int acess_readdir(int fd, char *dest) {
117 DEBUG("readdir(%i, %p)", fd, dest);
118 return _Syscall(SYS_READDIR, ">i <d", fd, 256, dest);
121 int acess__SysSelect(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout, uint32_t events)
123 DEBUG("_SysSelect(%i, %p, %p, %p, %p, 0x%x)", nfds, read, write, error, timeout, events);
124 return _Syscall(SYS_SELECT, ">i ?d ?d ?d >d >i", nfds,
125 read ? (nfds+7)/8 : 0, read,
126 write ? (nfds+7)/8 : 0, write,
127 error ? (nfds+7)/8 : 0, error,
128 sizeof(*timeout), timeout,
133 int acess_select(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout)
135 return acess__SysSelect(nfds, read, write, error, timeout, 0);
139 int acess__SysOpenChild(int fd, char *name, int flags) {
140 return _Syscall(SYS_OPENCHILD, ">i >s >i", fd, name, flags);
143 int acess__SysGetACL(int fd, t_sysACL *dest) {
144 return _Syscall(SYS_GETACL, ">i <d", fd, sizeof(t_sysACL), dest);
147 int acess__SysMount(const char *Device, const char *Directory, const char *Type, const char *Options) {
148 return _Syscall(SYS_MOUNT, ">s >s >s >s", Device, Directory, Type, Options);
153 int acess__SysSetFaultHandler(int (*Handler)(int)) {
154 printf("TODO: Set fault handler (asked to set to %p)\n", Handler);
158 // --- Memory Management ---
159 uint64_t acess__SysAllocate(uint vaddr)
161 if( AllocateMemory(vaddr, 0x1000) == -1 ) // Allocate a page
164 return vaddr; // Just ignore the need for paddrs :)
167 // --- Process Management ---
168 int acess_clone(int flags, void *stack)
170 extern int fork(void);
171 if(flags & CLONE_VM) {
172 int ret, newID, kernel_tid=0;
173 printf("USERSIDE fork()\n");
175 newID = _Syscall(SYS_AN_FORK, "<d", sizeof(int), &kernel_tid);
185 giSyscall_ClientID = newID;
189 // Return the acess TID instead
194 fprintf(stderr, "ERROR: Threads currently unsupported\n");
199 int acess_execve(char *path, char **argv, char **envp)
203 DEBUG("acess_execve: (path='%s', argv=%p, envp=%p)", path, argv, envp);
205 // Get argument count
206 for( argc = 0; argv[argc]; argc ++ ) ;
207 DEBUG(" acess_execve: argc = %i", argc);
209 char *new_argv[7+argc+1];
210 char client_id_str[11];
211 char socket_fd_str[11];
212 sprintf(client_id_str, "%i", giSyscall_ClientID);
213 sprintf(socket_fd_str, "%i", gSocket);
214 new_argv[0] = "ld-acess"; // TODO: Get path to ld-acess executable
215 new_argv[1] = "--key"; // Set client ID for Request.c
216 new_argv[2] = client_id_str;
217 new_argv[3] = "--socket"; // Socket
218 new_argv[4] = socket_fd_str;
219 new_argv[5] = "--binary"; // Set the binary path (instead of using argv[0])
221 for( i = 0; i < argc; i ++ ) new_argv[7+i] = argv[i];
222 new_argv[7+i] = NULL;
226 for( i = 0; i < argc; i ++ )
227 printf("\"%s\" ", new_argv[i]);
231 printf("envp = %p\n", envp);
232 for( i = 0; envp[i]; i ++ )
233 printf("%i: \"%s\"\n", i, envp[i]);
234 printf("envc = %i\n", i);
238 // Call actual execve
239 return native_execve("./ld-acess", new_argv, envp);
242 void acess_sleep(void)
244 _Syscall(SYS_SLEEP, "");
247 int acess_waittid(int TID, int *ExitStatus)
249 return _Syscall(SYS_WAITTID, ">i <d", TID, sizeof(int), &ExitStatus);
252 int acess_setuid(int ID) { return _Syscall(SYS_SETUID, ">i", ID); }
253 int acess_setgid(int ID) { return _Syscall(SYS_SETGID, ">i", ID); }
254 int acess_gettid(void) { return _Syscall(SYS_GETTID, ""); }
255 int acess_getpid(void) { return _Syscall(SYS_GETPID, ""); }
256 int acess_getuid(void) { return _Syscall(SYS_GETUID, ""); }
257 int acess_getgid(void) { return _Syscall(SYS_GETGID, ""); }
259 int acess_SysSendMessage(int DestTID, int Length, void *Data)
261 return _Syscall(SYS_SENDMSG, ">i >d", DestTID, Length, Data);
264 int acess_SysGetMessage(int *SourceTID, void *Data)
266 // static __thread int lastlen = 1024;
269 lastlen = _Syscall(SYS_GETMSG, "<d <d",
270 SourceTID ? sizeof(int) : 0, SourceTID,
271 Data ? 1024 : 0, Data
276 int acess__SysWaitEvent(int Mask)
278 return _Syscall(SYS_WAITEVENT, ">i", Mask);
282 void acess__SysDebug(const char *Format, ...)
286 va_start(args, Format);
288 printf("[_SysDebug %i]", giSyscall_ClientID);
289 vprintf(Format, args);
295 void acess__exit(int Status)
297 DEBUG("_exit(%i)", Status);
298 _Syscall(SYS_EXIT, ">i", Status);
303 // === Symbol List ===
304 #define DEFSYM(name) {#name, acess_##name}
305 const tSym caBuiltinSymbols[] = {
320 DEFSYM(_SysOpenChild),
334 DEFSYM(SysSendMessage),
335 DEFSYM(SysGetMessage),
337 DEFSYM(_SysAllocate),
339 DEFSYM(_SysSetFaultHandler),
340 DEFSYM(_SysWaitEvent)
343 const int ciNumBuiltinSymbols = sizeof(caBuiltinSymbols)/sizeof(caBuiltinSymbols[0]);