3 #include "../../Usermode/include/acess/sys.h"
11 #include "../syscalls.h"
18 const char *ReadEntry(tOutValue **OutDest, tInValue **InDest,
19 int *Direction, const char *ArgTypes, va_list Args)
21 uint64_t val64, *ptr64;
22 uint32_t val32, *ptr32;
23 int direction = 0; // 0: Invalid, 1: Out, 2: In, 3: Out
28 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
29 if( *ArgTypes == '\0' ) return ArgTypes;
34 case '>': direction = 1; break;
35 case '<': direction = 2; break;
36 case '?': direction = 3; break;
43 while(*ArgTypes && *ArgTypes == ' ') ArgTypes ++;
44 if( *ArgTypes == '\0' ) return ArgTypes;
46 // Internal helper macro
47 #define MAKE_OUT(_dest,_typeChar,_typeName,_value) do{if((_dest)){\
48 *(_dest) = (tOutValue*)malloc(sizeof(tOutValue)+sizeof(_typeName));\
49 (*(_dest))->Type=(_typeChar);(*(_dest))->Length=sizeof(_typeName);\
50 *(_typeName*)((*(_dest))->Data) = (_value);\
52 #define MAKE_IN(_dest,_typeChar,_typeName,_value) do{if((_dest)){\
53 *(_dest) = (tInValue*)malloc(sizeof(tInValue));\
54 (*(_dest))->Type=(_typeChar);(*(_dest))->Length=sizeof(_typeName);\
55 (*(_dest))->Data = (_value);\
61 case 'i': // 32-bit integer
65 ptr32 = va_arg(Args, uint32_t*);
66 MAKE_IN(InDest, 'i', uint32_t*, ptr32);
68 MAKE_OUT(OutDest, 'i', uint32_t, *ptr32);
72 val32 = va_arg(Args, uint32_t);
73 MAKE_OUT(OutDest, 'i', uint32_t, val32);
76 case 'I': // 64-bit integer
80 ptr64 = va_arg(Args, uint64_t*);
81 MAKE_IN(InDest, 'I', uint64_t*, ptr64);
83 MAKE_OUT(OutDest, 'I', uint64_t, *ptr64);
87 val64 = va_arg(Args, uint64_t);
88 MAKE_OUT(OutDest, 'I', uint64_t, val64);
92 // Input string makes no sense!
94 fprintf(stderr, "ReadEntry: Incoming string is not defined\n");
98 str = va_arg(Args, char*);
101 int len = strlen(str) + 1;
102 *OutDest = malloc( sizeof(tOutValue) + len );
103 (*OutDest)->Type = 's';
104 (*OutDest)->Length = len;
105 memcpy((*OutDest)->Data, str, len);
110 len = va_arg(Args, int);
111 str = va_arg(Args, char*);
114 if( (direction & 2) && InDest )
116 *InDest = (tInValue*)malloc( sizeof(tInValue) );
117 (*InDest)->Type = 'd';
118 (*InDest)->Length = len;
119 (*InDest)->Data = str;
123 if( (direction & 1) && InDest )
125 *OutDest = (tOutValue*)malloc( sizeof(tOutValue) + len );
126 (*OutDest)->Type = 'd';
127 (*OutDest)->Length = len;
128 memcpy((*OutDest)->Data, str, len);
138 *Direction = direction;
146 * Whitespace is ignored
147 * >i: Input Integer (32-bits)
148 * >I: Input Long Integer (64-bits)
150 * >d: Input Buffer (Preceded by valid size)
151 * <I: Output long integer
152 * <d: Output Buffer (Preceded by valid size)
153 * ?d: Bi-directional buffer (Preceded by valid size), buffer contents
156 void _Syscall(int SyscallID, const char *ArgTypes, ...)
166 va_start(args, ArgTypes);
174 str = ReadEntry(NULL, NULL, &dir, str, args);
176 fprintf(stderr, "syscalls.c: ReadEntry failed (SyscallID = %i)\n", SyscallID);
181 if( dir & 1 ) outCount ++;
184 if( dir & 2 ) inCount ++;
189 output = malloc( outCount*sizeof(tOutValue*) );
190 input = malloc( inCount*sizeof(tInValue*) );
192 // Fill `output` and `input`
193 va_start(args, ArgTypes);
195 // - re-zero so they can be used as indicies
204 str = ReadEntry(&outParam, &inParam, &dir, str, args);
208 output[outCount++] = outParam;
210 input[inCount++] = inParam;
214 // Send syscall request
215 if( SendRequest(SyscallID, outCount, output, inCount, input) ) {
216 fprintf(stderr, "syscalls.c: SendRequest failed (SyscallID = %i)\n", SyscallID);
221 while(outCount--) free(output[outCount]);
223 while(inCount--) free(input[inCount]);
228 int open(const char *Path, int Flags) {
230 _Syscall(SYS_OPEN, "<i >s >i", &ret, Path, Flags);
235 _Syscall(SYS_CLOSE, ">i", FD);
238 size_t read(int FD, size_t Bytes, void *Dest) {
240 _Syscall(SYS_READ, "<i >i >i <d", &ret, FD, Bytes, Bytes, Dest);
244 size_t write(int FD, size_t Bytes, void *Src) {
246 _Syscall(SYS_WRITE, "<i >i >i >d", &ret, FD, Bytes, Bytes, Src);
250 int seek(int FD, int64_t Ofs, int Dir) {
252 _Syscall(SYS_SEEK, "<i >i >I >i", &ret, FD, Ofs, Dir);
256 uint64_t tell(int FD) {
258 _Syscall(SYS_TELL, "<I >i", &ret, FD);
262 int ioctl(int fd, int id, void *data) {
264 // NOTE: 1024 byte size is a hack
265 _Syscall(SYS_IOCTL, "<i >i >i ?d", &ret, fd, id, 1024, data);
268 int finfo(int fd, t_sysFInfo *info, int maxacls) {
270 _Syscall(SYS_FINFO, "<i >i <d >i",
272 sizeof(t_sysFInfo)+maxacls*sizeof(t_sysACL), info,
277 int readdir(int fd, char *dest) {
279 _Syscall(SYS_READDIR, "<i >i <d", &ret, fd, 256, dest);
283 int _SysOpenChild(int fd, char *name, int flags) {
285 _Syscall(SYS_OPENCHILD, "<i >i >s >i", &ret, fd, name, flags);
289 int _SysGetACL(int fd, t_sysACL *dest) {
291 _Syscall(SYS_GETACL, "<i >i <d", &ret, fd, sizeof(t_sysACL), dest);
295 int _SysMount(const char *Device, const char *Directory, const char *Type, const char *Options) {
297 _Syscall(SYS_MOUNT, "<i >s >s >s >s", &ret, Device, Directory, Type, Options);
303 int _SysSetFaultHandler(int (*Handler)(int)) {
308 // === Symbol List ===
309 #define DEFSYM(name) {#name, name}
310 const tSym caBuiltinSymbols[] = {
322 DEFSYM(_SysOpenChild),
326 {"_SysSetFaultHandler", _SysSetFaultHandler}
329 const int ciNumBuiltinSymbols = sizeof(caBuiltinSymbols)/sizeof(caBuiltinSymbols[0]);