Merge branch 'master' of git://git.ucc.asn.au/tpg/acess2
[tpg/acess2.git] / AcessNative / ld-acess_src / exports.c
1 /*
2  * AcessNative
3  *
4  * exports.c
5  * - Exported functions
6  */
7 #define DONT_INCLUDE_SYSCALL_NAMES 1
8 #include "../../Usermode/Libraries/ld-acess.so_src/include_exp/acess/sys.h"
9 #include "../syscalls.h"
10 #include "exports.h"
11 #include <stdarg.h>
12 #include <stddef.h>
13
14 #define DEBUG(v...)     Debug(v)
15 #define PAGE_SIZE       4096
16
17 typedef struct sFILE    FILE;
18
19 extern FILE     *stderr;
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);
26
27 extern int      gSocket;
28 extern int      giSyscall_ClientID;     // Needed for execve
29 extern void     _InitSyscalls(void);
30 extern void     _CloseSyscalls(void);
31
32 extern void     Debug(const char *Format, ...);
33 extern int      AllocateMemory(uintptr_t VirtAddr, size_t ByteCount);
34
35 // === CONSTANTS ===
36 #define NATIVE_FILE_MASK        0x40000000
37
38 // === CODE ===
39 // --- VFS Calls
40 int acess_chdir(const char *Path)
41 {
42         return _Syscall(SYS_CHDIR, ">s", Path);
43 }
44
45 int acess_open(const char *Path, int Flags)
46 {
47         if( strncmp(Path, "$$$$", 4) == 0 )
48         {
49                 return native_open(Path, Flags) | NATIVE_FILE_MASK;
50         }
51         DEBUG("open(\"%s\", 0x%x)", Path, Flags);
52         return _Syscall(SYS_OPEN, ">s >i", Path, Flags);
53 }
54
55 void acess_close(int FD) {
56         if(FD & NATIVE_FILE_MASK) {
57                 return native_close(FD & (NATIVE_FILE_MASK-1));
58         }
59         DEBUG("close(%i)", FD);
60         _Syscall(SYS_CLOSE, ">i", FD);
61 }
62
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);
66 }
67
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 //      if( FD > 2 )
72                 DEBUG("read(0x%x, 0x%x, *%p)", FD, Bytes, Dest);
73         return _Syscall(SYS_READ, ">i >i <d", FD, Bytes, Bytes, Dest);
74 }
75
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);
79 //      if( FD > 2 )
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);
82 }
83
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);
87         }
88         DEBUG("seek(0x%x, 0x%llx, %i)", FD, Ofs, Dir);
89         return _Syscall(SYS_SEEK, ">i >I >i", FD, Ofs, Dir);
90 }
91
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);
96 }
97
98 int acess_ioctl(int fd, int id, void *data) {
99          int    len;
100         DEBUG("ioctl(%i, %i, %p)", fd, id, data);
101         // NOTE: The length here is hacky and could break
102         if( data == NULL )
103                 len = 0;
104         else
105                 len = PAGE_SIZE - ((uintptr_t)data % PAGE_SIZE);
106         return _Syscall(SYS_IOCTL, ">i >i ?d", fd, id, len, data);
107 }
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",
112                 fd,
113                 sizeof(t_sysFInfo)+maxacls*sizeof(t_sysACL), info,
114                 maxacls
115                 );
116 }
117
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);
121 }
122
123 int acess__SysSelect(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout, uint32_t events)
124 {
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,
131                 events
132                 );
133 }
134
135 int acess_select(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout)
136 {
137         return acess__SysSelect(nfds, read, write, error, timeout, 0);
138 }
139
140
141 int acess__SysOpenChild(int fd, char *name, int flags) {
142         return _Syscall(SYS_OPENCHILD, ">i >s >i", fd, name, flags);
143 }
144
145 int acess__SysGetACL(int fd, t_sysACL *dest) {
146         return _Syscall(SYS_GETACL, ">i <d", fd, sizeof(t_sysACL), dest);
147 }
148
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);
151 }
152
153
154 // --- Error Handler
155 int acess__SysSetFaultHandler(int (*Handler)(int)) {
156         printf("TODO: Set fault handler (asked to set to %p)\n", Handler);
157         return 0;
158 }
159
160 // --- Memory Management ---
161 uint64_t acess__SysAllocate(uint vaddr)
162 {
163         if( AllocateMemory(vaddr, 0x1000) == -1 )       // Allocate a page
164                 return 0;
165                 
166         return vaddr;   // Just ignore the need for paddrs :)
167 }
168
169 // --- Process Management ---
170 int acess_clone(int flags, void *stack)
171 {
172         extern int fork(void);
173         if(flags & CLONE_VM) {
174                  int    ret, newID, kernel_tid=0;
175                 printf("USERSIDE fork()\n");
176                 
177                 newID = _Syscall(SYS_AN_FORK, "<d", sizeof(int), &kernel_tid);
178                 ret = fork();
179                 if(ret < 0) {
180                         return ret;
181                 }
182                 
183                 if(ret == 0)
184                 {
185                         _CloseSyscalls();
186                         _InitSyscalls();
187                         giSyscall_ClientID = newID;
188                         return 0;
189                 }
190                 
191                 // Return the acess TID instead
192                 return kernel_tid;
193         }
194         else
195         {
196                 fprintf(stderr, "ERROR: Threads currently unsupported\n");
197                 exit(-1);
198         }
199 }
200
201 int acess_execve(char *path, char **argv, char **envp)
202 {
203          int    i, argc;
204         
205         DEBUG("acess_execve: (path='%s', argv=%p, envp=%p)", path, argv, envp);
206         
207         // Get argument count
208         for( argc = 0; argv[argc]; argc ++ ) ;
209         DEBUG(" acess_execve: argc = %i", argc);
210
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])
222         new_argv[6] = path;
223         for( i = 0; i < argc; i ++ )    new_argv[7+i] = argv[i];
224         new_argv[7+i] = NULL;
225         
226         #if 1
227         argc += 7;
228         for( i = 0; i < argc; i ++ )
229                 printf("\"%s\" ", new_argv[i]);
230         printf("\n");
231         if(envp)
232         {
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);
237         }
238         #endif
239         
240         // Call actual execve
241         return native_execve("./ld-acess", new_argv, envp);
242 }
243
244 void acess_sleep(void)
245 {
246         _Syscall(SYS_SLEEP, "");
247 }
248
249 int acess_waittid(int TID, int *ExitStatus)
250 {
251         return _Syscall(SYS_WAITTID, ">i <d", TID, sizeof(int), &ExitStatus);
252 }
253
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, ""); }
260
261 int acess_SysSendMessage(int DestTID, int Length, void *Data)
262 {
263         return _Syscall(SYS_SENDMSG, ">i >d", DestTID, Length, Data);
264 }
265
266 int acess_SysGetMessage(int *SourceTID, void *Data)
267 {
268 //      static __thread int lastlen = 1024;
269         int lastlen;
270
271         lastlen = _Syscall(SYS_GETMSG, "<d <d",
272                 SourceTID ? sizeof(uint32_t) : 0, SourceTID,
273                 Data ? 1024 : 0, Data
274                 );
275         return lastlen;
276 }
277
278 int acess__SysWaitEvent(int Mask)
279 {
280         return _Syscall(SYS_WAITEVENT, ">i", Mask);
281 }
282
283 // --- Logging
284 void acess__SysDebug(const char *Format, ...)
285 {
286         va_list args;
287         
288         va_start(args, Format);
289         
290         printf("[_SysDebug %i] ", giSyscall_ClientID);
291         vprintf(Format, args);
292         printf("\n");
293         
294         va_end(args);
295 }
296
297 void acess__exit(int Status)
298 {
299         DEBUG("_exit(%i)", Status);
300         _Syscall(SYS_EXIT, ">i", Status);
301         exit(Status);
302 }
303
304
305 // === Symbol List ===
306 #define DEFSYM(name)    {#name, acess_##name}
307 const tSym      caBuiltinSymbols[] = {
308         DEFSYM(_exit),
309         
310         DEFSYM(chdir),
311         DEFSYM(open),
312         DEFSYM(close),
313         DEFSYM(reopen),
314         DEFSYM(read),
315         DEFSYM(write),
316         DEFSYM(seek),
317         DEFSYM(tell),
318         DEFSYM(ioctl),
319         DEFSYM(finfo),
320         DEFSYM(readdir),
321         DEFSYM(select),
322         DEFSYM(_SysOpenChild),
323         DEFSYM(_SysGetACL),
324         DEFSYM(_SysMount),
325         DEFSYM(_SysSelect),
326         
327         DEFSYM(clone),
328         DEFSYM(execve),
329         DEFSYM(sleep),
330         
331         DEFSYM(waittid),
332         DEFSYM(setuid),
333         DEFSYM(setgid),
334         DEFSYM(gettid),
335
336         DEFSYM(SysSendMessage),
337         DEFSYM(SysGetMessage),
338         
339         DEFSYM(_SysAllocate),
340         DEFSYM(_SysDebug),
341         DEFSYM(_SysSetFaultHandler),
342         DEFSYM(_SysWaitEvent)
343 };
344
345 const int       ciNumBuiltinSymbols = sizeof(caBuiltinSymbols)/sizeof(caBuiltinSymbols[0]);
346

UCC git Repository :: git.ucc.asn.au