AcessNative - Fixing little bugs
[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/include/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         DEBUG("read(0x%x, 0x%x, *%p)", FD, Bytes, Dest);
72         return _Syscall(SYS_READ, ">i >i <d", FD, Bytes, Bytes, Dest);
73 }
74
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);
80 }
81
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);
85         }
86         DEBUG("seek(0x%x, 0x%llx, %i)", FD, Ofs, Dir);
87         return _Syscall(SYS_SEEK, ">i >I >i", FD, Ofs, Dir);
88 }
89
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);
94 }
95
96 int acess_ioctl(int fd, int id, void *data) {
97          int    len;
98         // NOTE: 1024 byte size is a hack
99         DEBUG("ioctl(%i, %i, %p)", fd, id, data);
100         if( data == NULL )
101                 len = 0;
102         else
103                 len = PAGE_SIZE - ((uintptr_t)data % PAGE_SIZE);
104         return _Syscall(SYS_IOCTL, ">i >i ?d", fd, id, len, data);
105 }
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",
110                 fd,
111                 sizeof(t_sysFInfo)+maxacls*sizeof(t_sysACL), info,
112                 maxacls
113                 );
114 }
115
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);
119 }
120
121 int acess__SysSelect(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout, uint32_t events)
122 {
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,
129                 events
130                 );
131 }
132
133 int acess_select(int nfds, fd_set *read, fd_set *write, fd_set *error, time_t *timeout)
134 {
135         return acess__SysSelect(nfds, read, write, error, timeout, 0);
136 }
137
138
139 int acess__SysOpenChild(int fd, char *name, int flags) {
140         return _Syscall(SYS_OPENCHILD, ">i >s >i", fd, name, flags);
141 }
142
143 int acess__SysGetACL(int fd, t_sysACL *dest) {
144         return _Syscall(SYS_GETACL, ">i <d", fd, sizeof(t_sysACL), dest);
145 }
146
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);
149 }
150
151
152 // --- Error Handler
153 int acess__SysSetFaultHandler(int (*Handler)(int)) {
154         printf("TODO: Set fault handler (asked to set to %p)\n", Handler);
155         return 0;
156 }
157
158 // --- Memory Management ---
159 uint64_t acess__SysAllocate(uint vaddr)
160 {
161         if( AllocateMemory(vaddr, 0x1000) == -1 )       // Allocate a page
162                 return 0;
163                 
164         return vaddr;   // Just ignore the need for paddrs :)
165 }
166
167 // --- Process Management ---
168 int acess_clone(int flags, void *stack)
169 {
170         extern int fork(void);
171         if(flags & CLONE_VM) {
172                  int    ret, newID, kernel_tid=0;
173                 printf("USERSIDE fork()\n");
174                 
175                 newID = _Syscall(SYS_AN_FORK, "<d", sizeof(int), &kernel_tid);
176                 ret = fork();
177                 if(ret < 0) {
178                         return ret;
179                 }
180                 
181                 if(ret == 0)
182                 {
183                         _CloseSyscalls();
184                         _InitSyscalls();
185                         giSyscall_ClientID = newID;
186                         return 0;
187                 }
188                 
189                 // Return the acess TID instead
190                 return kernel_tid;
191         }
192         else
193         {
194                 fprintf(stderr, "ERROR: Threads currently unsupported\n");
195                 exit(-1);
196         }
197 }
198
199 int acess_execve(char *path, char **argv, char **envp)
200 {
201          int    i, argc;
202         
203         DEBUG("acess_execve: (path='%s', argv=%p, envp=%p)", path, argv, envp);
204         
205         // Get argument count
206         for( argc = 0; argv[argc]; argc ++ ) ;
207         DEBUG(" acess_execve: argc = %i", argc);
208
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])
220         new_argv[6] = path;
221         for( i = 0; i < argc; i ++ )    new_argv[7+i] = argv[i];
222         new_argv[7+i] = NULL;
223         
224         #if 1
225         argc += 7;
226         for( i = 0; i < argc; i ++ )
227                 printf("\"%s\" ", new_argv[i]);
228         printf("\n");
229         if(envp)
230         {
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);
235         }
236         #endif
237         
238         // Call actual execve
239         return native_execve("./ld-acess", new_argv, envp);
240 }
241
242 void acess_sleep(void)
243 {
244         _Syscall(SYS_SLEEP, "");
245 }
246
247 int acess_waittid(int TID, int *ExitStatus)
248 {
249         return _Syscall(SYS_WAITTID, ">i <d", TID, sizeof(int), &ExitStatus);
250 }
251
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, ""); }
258
259 int acess_SysSendMessage(int DestTID, int Length, void *Data)
260 {
261         return _Syscall(SYS_SENDMSG, ">i >d", DestTID, Length, Data);
262 }
263
264 int acess_SysGetMessage(int *SourceTID, void *Data)
265 {
266 //      static __thread int lastlen = 1024;
267         int lastlen;
268
269         lastlen = _Syscall(SYS_GETMSG, "<d <d",
270                 SourceTID ? sizeof(int) : 0, SourceTID,
271                 Data ? 1024 : 0, Data
272                 );
273         return lastlen;
274 }
275
276 int acess__SysWaitEvent(int Mask)
277 {
278         return _Syscall(SYS_WAITEVENT, ">i", Mask);
279 }
280
281 // --- Logging
282 void acess__SysDebug(const char *Format, ...)
283 {
284         va_list args;
285         
286         va_start(args, Format);
287         
288         printf("[_SysDebug %i]", giSyscall_ClientID);
289         vprintf(Format, args);
290         printf("\n");
291         
292         va_end(args);
293 }
294
295 void acess__exit(int Status)
296 {
297         DEBUG("_exit(%i)", Status);
298         _Syscall(SYS_EXIT, ">i", Status);
299         exit(Status);
300 }
301
302
303 // === Symbol List ===
304 #define DEFSYM(name)    {#name, acess_##name}
305 const tSym      caBuiltinSymbols[] = {
306         DEFSYM(_exit),
307         
308         DEFSYM(chdir),
309         DEFSYM(open),
310         DEFSYM(close),
311         DEFSYM(reopen),
312         DEFSYM(read),
313         DEFSYM(write),
314         DEFSYM(seek),
315         DEFSYM(tell),
316         DEFSYM(ioctl),
317         DEFSYM(finfo),
318         DEFSYM(readdir),
319         DEFSYM(select),
320         DEFSYM(_SysOpenChild),
321         DEFSYM(_SysGetACL),
322         DEFSYM(_SysMount),
323         DEFSYM(_SysSelect),
324         
325         DEFSYM(clone),
326         DEFSYM(execve),
327         DEFSYM(sleep),
328         
329         DEFSYM(waittid),
330         DEFSYM(setuid),
331         DEFSYM(setgid),
332         DEFSYM(gettid),
333
334         DEFSYM(SysSendMessage),
335         DEFSYM(SysGetMessage),
336         
337         DEFSYM(_SysAllocate),
338         DEFSYM(_SysDebug),
339         DEFSYM(_SysSetFaultHandler),
340         DEFSYM(_SysWaitEvent)
341 };
342
343 const int       ciNumBuiltinSymbols = sizeof(caBuiltinSymbols)/sizeof(caBuiltinSymbols[0]);
344

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