summaryrefslogtreecommitdiff
path: root/syscalls.c
blob: c4bd549302ff658476541a73428f4d020f7c60aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>

typedef struct fs_dirent_struct
{
    uint32_t	inode;
    uint32_t	offset;
    uint16_t	length;
    uint8_t	type;
    char	name[256];
}fs_dirent;

//fool-os syscalls
#define SYSCALL_READDIR 63
#define SYSCALL_HAS_DATA 80
#define SYSCALL_TUNE 81

//syscalls required by newlib
#define SYSCALL_EXIT 60
#define SYSCALL_WRITE 61
#define SYSCALL_READ 62
#define SYSCALL_EXECVE 64
#define SYSCALL_OPEN 65
#define SYSCALL_CLOSE 66
#define SYSCALL_FSTAT 67
#define SYSCALL_ISATTY 68
#define SYSCALL_LSEEK 69
#define SYSCALL_SBRK 70
#define SYSCALL_STAT 74
#define SYSCALL_LSTAT 79

// stubs only so far!
#define SYSCALL_GETTIMEOFDAY 71
#define SYSCALL_FORK 72
#define SYSCALL_KILL 73
#define SYSCALL_LINK 73
#define SYSCALL_TIMES 75
#define SYSCALL_UNLINK 76
#define SYSCALL_WAIT 77
#define SYSCALL_GETPID 78

/*
int syscall_execve(char *name, char **argv, char **env);
int syscall_write(int file, char *buf, int len);

int syscall_readdir(const char *name,struct fs_dirent *dirs,int max);

int syscall_exit(int ret, int none1, int none2);
int syscall_open(char *name, int flags, int len);
int syscall_read(int file, char *buf, int len);
int syscall_close(int file,int none1,int none2);
int syscall_fstat(int file, struct stat *st,int none);
int syscall_isatty(int file,int none1,int none2);  
int syscall_lseek(int file,int ptr,int dir); 
int syscall_stat(char *file, struct stat *st);
int syscall_sbrk(int incr, int none1, int none2);
//
int syscall_gettimeofday(struct timeval *tv, struct timezone *tz);
int syscall_fork(void);
int syscall_getpid(void);
int syscall_kill(int pid, int sig);
int syscall_link(char *old, char *ne);
int syscall_times(struct tms *buf);
int syscall_unlink(char *name);
int syscall_wait(int *status);
//
int syscall_unhandled(int nr);
*/

extern char **environ;
//struct _reent *_impure_ptr;

// generic syscall interface!
/*int volatile  __attribute__ ((noinline))  syscall(int call, int p1, int p2, int p3)
{
	int ebx; // persist over func call( a,c,d are scratch)
	int ret; // will hold return val

        asm("mov %%ebx, %0": "=b" (ebx));

//	asm("pusha");

	// select syscall
	asm("mov %0, %%eax"::"m"(call));  

	// pass params
	asm("mov %0,%%edx"::"m"(p1));
	asm("mov %0,%%ecx"::"m"(p2));
	asm("mov %0,%%ebx"::"m"(p3));

	// interrrupt
	asm("int $0x80");

	// get return value
        asm("mov %%ebx, %0": "=b" (ret));

	// restore
	asm("mov %0,%%ebx"::"m"(ebx));

//	asm("popa");

	return ret;
}
*/

// fool os custom
int readdir(const char *name,fs_dirent  *dirs,int max)
{

    return syscall(SYSCALL_READDIR,name,dirs,max);
}

int has_data_waiting()
{

    return syscall(SYSCALL_HAS_DATA,0,0,0);
}

int fool_tune(int v1, int v2, int v3)
{

    return syscall(SYSCALL_TUNE,v1,v2,v3);
}

void _exit(int ret)
{
    _exit2(ret,environ);
}

void _exit2(int ret,char **environ)
{
    return syscall(SYSCALL_EXIT,ret,environ,0);
}

//required by newlibc
int _close(int file) 
{
    return syscall(SYSCALL_CLOSE,file,0,0);
}

int _isatty(int file) 
{
    return syscall(SYSCALL_ISATTY,file,0,0);
}

int _lseek(int file, int ptr, int dir) 
{
    return syscall(SYSCALL_LSEEK,file,ptr,dir);
}

int _read(int file, char *ptr, int len)
{

    return syscall(SYSCALL_READ,file,ptr,len);
}

int _open(const char *name, int flags, int mode)
{
    return syscall(SYSCALL_OPEN,name,flags,mode);
}

int _write(int file, char *ptr, int len) 
{
    return syscall(SYSCALL_WRITE,file,ptr,len);
}

int _execve(char *name, char **argv, char **env) 
{
    return syscall(SYSCALL_EXECVE,name,argv,env);
}

uint32_t _sbrk(int incr) 
{
    return syscall(SYSCALL_SBRK,incr,0,0);
}

int _gettimeofday(struct timeval *tv, void *tz)
{
    return syscall(SYSCALL_GETTIMEOFDAY,tv,tz,0);
}

int _fork(void)
{
    return syscall(SYSCALL_FORK,0,0,0);
}

int _getpid(void)
{
    return syscall(SYSCALL_GETPID,0,0,0);
}

int _kill(int pid, int sig)
{
    return syscall(SYSCALL_KILL,pid,sig,0);
}

int _link(char *old, char *ne)
{
    return syscall(SYSCALL_LINK,old,ne,0);
}

int _unlink(char *name)
{
    return syscall(SYSCALL_UNLINK,name,0,0);
}

int _times(struct tms *buf)
{
    return syscall(SYSCALL_TIMES,buf,0,0);
}

int _wait(int *status)
{
    return syscall(SYSCALL_WAIT,status,0,0);
}

int _stat(const char *file, struct stat *st)
{
    return syscall(SYSCALL_STAT,file,st,0);
}

int _lstat(const char *file, struct stat *st)
{
    return syscall(SYSCALL_LSTAT,file,st,0);
}

int _fstat(int file, struct stat *st)
{
    return syscall(SYSCALL_FSTAT,file,st,0);
}