summaryrefslogtreecommitdiff
path: root/userspace/syscalls.c
blob: 747141814fd8f47b6d3ffacc4294911f4ccc8898 (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
#include <sys/stat.h>
#include <string.h>
#include <stdint.h>
#include "../kernel/syscalls.h"

/*
char *__env1[]={0};
char *__env2[]={"dupa","test"};
char **environ={__env1,__env2};
*/

void _exit(int ret)
{
while(1);
}

// generic syscall interface!
int syscall(int call, int p1, int p2, int p3)
{
	int ebx; // will hold return value;

	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" (ebx));

	asm("popa");

	return ebx;
}

int close(int file) 
{
    return syscall(SYSCALL_CLOSE,file,0,0);
}

int fstat(int file, struct stat *st)
{
    return syscall(SYSCALL_FSTAT,file,st,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 readdir(const char *name,fs_dirent  *dirs,int max)
{

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

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);
}


caddr_t sbrk(int incr) 
{
    static int alloc=0x300000; // TODO: implement properly sbrk!!!
    int oldalloc=alloc;
    alloc+=incr;
    return oldalloc;

    return syscall(SYSCALL_SBRK,incr,0,0);
}