AcessNative - Bugfixes 'r' us, GUI can start and render (partially)
[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 // === GLOBALS ===
39 int     acess__errno;
40
41 // === CODE ===
42 // --- VFS Calls
43 int acess_chdir(const char *Path)
44 {
45         return _Syscall(SYS_CHDIR, ">s", Path);
46 }
47
48 int acess_open(const char *Path, int Flags)
49 {
50         if( strncmp(Path, "$$$$", 4) == 0 )
51         {
52                 return native_open(Path, Flags) | NATIVE_FILE_MASK;
53         }
54         DEBUG("open(\"%s\", 0x%x)", Path, Flags);
55         return _Syscall(SYS_OPEN, ">s >i", Path, Flags);
56 }
57
58 void acess_close(int FD) {
59         if(FD & NATIVE_FILE_MASK) {
60                 return native_close(FD & (NATIVE_FILE_MASK-1));
61         }
62         DEBUG("close(%i)", FD);
63         _Syscall(SYS_CLOSE, ">i", FD);
64 }
65
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);
69 }
70
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);
74 //      if( FD > 2 )
75                 DEBUG("read(0x%x, 0x%x, *%p)", FD, Bytes, Dest);
76         return _Syscall(SYS_READ, ">i >i <d", FD, Bytes, Bytes, Dest);
77 }
78
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);
82 //      if( FD > 2 )
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);
85 }
86
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);
90         }
91         DEBUG("seek(0x%x, 0x%llx, %i)", FD, Ofs, Dir);
92         return _Syscall(SYS_SEEK, ">i >I >i", FD, Ofs, Dir);
93 }
94
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);
100 }
101
102 int acess_ioctl(int fd, int id, void *data) {
103          int    len;
104         DEBUG("ioctl(%i, %i, %p)", fd, id, data);
105         // NOTE: The length here is hacky and could break
106         if( data == NULL )
107                 len = 0;
108         else
109                 len = PAGE_SIZE - ((uintptr_t)data % PAGE_SIZE);
110         return _Syscall(SYS_IOCTL, ">i >i ?d", fd, id, len, data);
111 }
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",
116                 fd,
117                 sizeof(t_sysFInfo)+maxacls*sizeof(t_sysACL), info,
118                 maxacls
119                 );
120 }
121
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);
125 }
126
127 int acess__SysSelect(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout, uint32_t events)
128 {
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,
135                 events
136                 );
137 }
138
139 int acess_select(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout)
140 {
141         return acess__SysSelect(nfds, read, write, error, timeout, 0);
142 }
143
144
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);
148 }
149
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);
153 }
154
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);
158 }
159
160
161 // --- Error Handler
162 int acess__SysSetFaultHandler(int (*Handler)(int)) {
163         printf("TODO: Set fault handler (asked to set to %p)\n", Handler);
164         return 0;
165 }
166
167 // --- Memory Management ---
168 uint64_t acess__SysAllocate(uint vaddr)
169 {
170         if( AllocateMemory(vaddr, 0x1000) == -1 )       // Allocate a page
171                 return 0;
172                 
173         return vaddr;   // Just ignore the need for paddrs :)
174 }
175
176 // --- Process Management ---
177 int acess_clone(int flags, void *stack)
178 {
179         extern int fork(void);
180         if(flags & CLONE_VM) {
181                  int    ret, newID, kernel_tid=0;
182                 Debug("USERSIDE fork()");
183                 
184                 newID = _Syscall(SYS_AN_FORK, "<d", sizeof(int), &kernel_tid);
185                 ret = fork();
186                 if(ret < 0) {
187                         return ret;
188                 }
189                 
190                 if(ret == 0)
191                 {
192                         _CloseSyscalls();
193                         giSyscall_ClientID = newID;
194                         _InitSyscalls();
195                         return 0;
196                 }
197                 
198                 // Return the acess TID instead
199                 return kernel_tid;
200         }
201         else
202         {
203                 fprintf(stderr, "ERROR: Threads currently unsupported\n");
204                 exit(-1);
205         }
206 }
207
208 int acess_execve(char *path, char **argv, char **envp)
209 {
210          int    i, argc;
211         
212         DEBUG("acess_execve: (path='%s', argv=%p, envp=%p)", path, argv, envp);
213         
214         // Get argument count
215         for( argc = 0; argv[argc]; argc ++ ) ;
216         DEBUG(" acess_execve: argc = %i", argc);
217
218         char    *new_argv[7+argc+1];
219         char    client_id_str[11];
220         char    socket_fd_str[11];
221         sprintf(client_id_str, "%i", giSyscall_ClientID);
222         sprintf(socket_fd_str, "%i", gSocket);
223         new_argv[0] = "ld-acess";       // TODO: Get path to ld-acess executable
224         new_argv[1] = "--key";          // Set client ID for Request.c
225         new_argv[2] = client_id_str;
226         new_argv[3] = "--socket";       // Socket
227         new_argv[4] = socket_fd_str;
228         new_argv[5] = "--binary";       // Set the binary path (instead of using argv[0])
229         new_argv[6] = path;
230         for( i = 0; i < argc; i ++ )    new_argv[7+i] = argv[i];
231         new_argv[7+i] = NULL;
232         
233         #if 1
234         argc += 7;
235         for( i = 0; i < argc; i ++ )
236                 printf("\"%s\" ", new_argv[i]);
237         printf("\n");
238         if(envp)
239         {
240                 printf("envp = %p\n", envp);
241                 for( i = 0; envp[i]; i ++ )
242                         printf("%i: \"%s\"\n", i, envp[i]);
243                 printf("envc = %i\n", i);
244         }
245         #endif
246         
247         // Call actual execve
248         return native_execve("./ld-acess", new_argv, envp);
249 }
250
251 void acess_sleep(void)
252 {
253         DEBUG("%s()", __func__);
254         _Syscall(SYS_SLEEP, "");
255 }
256
257 int acess_waittid(int TID, int *ExitStatus)
258 {
259         DEBUG("%s(%i, %p)", __func__, TID, ExitStatus);
260         return _Syscall(SYS_WAITTID, ">i <d", TID, sizeof(int), &ExitStatus);
261 }
262
263 int acess_setuid(int ID) { return _Syscall(SYS_SETUID, ">i", ID); }
264 int acess_setgid(int ID) { return _Syscall(SYS_SETGID, ">i", ID); }
265 int acess_gettid(void) { return _Syscall(SYS_GETTID, ""); }
266 int acess_getpid(void) { return _Syscall(SYS_GETPID, ""); }
267 int acess_getuid(void) { return _Syscall(SYS_GETUID, ""); }
268 int acess_getgid(void) { return _Syscall(SYS_GETGID, ""); }
269
270 int acess_SysSendMessage(int DestTID, int Length, void *Data)
271 {
272         DEBUG("%s(%i, 0x%x, %p)", __func__, DestTID, Length, Data);
273         return _Syscall(SYS_SENDMSG, ">i >d", DestTID, Length, Data);
274 }
275
276 int acess_SysGetMessage(int *SourceTID, int BufLen, void *Data)
277 {
278         DEBUG("%s(%p, %p)", __func__, SourceTID, Data);
279         return _Syscall(SYS_GETMSG, "<d <d",
280                 SourceTID ? sizeof(uint32_t) : 0, SourceTID,
281                 BufLen, Data
282                 );
283 }
284
285 int acess__SysWaitEvent(int Mask)
286 {
287         DEBUG("%s(%x)", __func__, Mask);
288         return _Syscall(SYS_WAITEVENT, ">i", Mask);
289 }
290
291 // --- Logging
292 void acess__SysDebug(const char *Format, ...)
293 {
294         va_list args;
295         
296         va_start(args, Format);
297         
298         printf("[_SysDebug %i] ", giSyscall_ClientID);
299         vprintf(Format, args);
300         printf("\n");
301         
302         va_end(args);
303 }
304
305 void acess__exit(int Status)
306 {
307         DEBUG("_exit(%i)", Status);
308         _Syscall(SYS_EXIT, ">i", Status);
309         exit(Status);
310 }
311
312
313 // === Symbol List ===
314 #define DEFSYM(name)    {#name, &acess_##name}
315 const tSym      caBuiltinSymbols[] = {
316         DEFSYM(_exit),
317         
318         DEFSYM(chdir),
319         DEFSYM(open),
320         DEFSYM(close),
321         DEFSYM(reopen),
322         DEFSYM(read),
323         DEFSYM(write),
324         DEFSYM(seek),
325         DEFSYM(tell),
326         DEFSYM(ioctl),
327         DEFSYM(finfo),
328         DEFSYM(SysReadDir),
329         DEFSYM(select),
330         DEFSYM(_SysOpenChild),
331         DEFSYM(_SysGetACL),
332         DEFSYM(_SysMount),
333         DEFSYM(_SysSelect),
334         
335         DEFSYM(clone),
336         DEFSYM(execve),
337         DEFSYM(sleep),
338         
339         DEFSYM(waittid),
340         DEFSYM(setuid),
341         DEFSYM(setgid),
342         DEFSYM(gettid),
343
344         DEFSYM(SysSendMessage),
345         DEFSYM(SysGetMessage),
346         
347         DEFSYM(_SysAllocate),
348         DEFSYM(_SysDebug),
349         DEFSYM(_SysSetFaultHandler),
350         DEFSYM(_SysWaitEvent),
351         
352         DEFSYM(_errno)
353 };
354
355 const int       ciNumBuiltinSymbols = sizeof(caBuiltinSymbols)/sizeof(caBuiltinSymbols[0]);
356

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