summaryrefslogtreecommitdiff
path: root/kernel/syscalls.c
blob: 1de5d3abc20c6aff6094a31bc37078b1ffca4e07 (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
#define FOOLOS_MODULE_NAME "syscalls"
#include "lib/buffer/ringbuffer.h"
#include "lib/logger/log.h"
#include "lib/bool/bool.h"
#include "fs/fs.h"
#include "fs/ext2.h"
#include "kernel/console.h"
#include "kernel/config.h"
#include <sys/stat.h>


static int preread;
static int alloc=0x600000; // TODO: implement properly sbrk!!!

int syscall_write(int file, char *buf, int len)
{

    #ifdef LOG_SYSCALLS
    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"write(file=%d, buf=0x%08X, len=%d)", file,buf,len);
    #endif

    // ALL output to stdout
    for(int i=0;i<len;i++)
    {
	//PutConsoleChar(buf[i],0b1111111111000000);
	console_put_char(buf[i]);
    }
    return len;
}

int syscall_read(int file, char *buf, int len)
{

    #ifdef LOG_SYSCALLS
    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"read(file=%d, buf=0x%08X, len=%d)", file,buf,len);
    #endif

    // stdin
    if(file==1)
    {
    
	char c;

	while(1)
	{
	    asm("cli");
	    bool ret=ringbuffer_get(&c);
	    asm("sti");
	    
	    if(ret)
	    {
		*buf=c;
		if(c=='X')return 0;
		return 1;
	    }

	}
    }

}

int syscall_readdir(const char *name,fs_dirent *dirs,int max)
{
    #ifdef LOG_SYSCALLS
    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"readdir(name=0x%08X, dirs=0x%08X,  %d)", name,dirs,max);
    #endif

    return fs_readdir(name,dirs,max);
}

int syscall_execve(char *name, char **argv, char **env)
{
    #ifdef LOG_SYSCALLS
    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"execve (name=0x%08X, argvs=0x%08X,  env=0x%08X)", name,argv,env);
    #endif

    ext2_inode_content(EXT2_RAM_ADDRESS,name,0x800000,0x100000);

    // autorun "user-space" prog
    asm("push $0x800000");
    asm("ret");
}

int syscall_open(char *name, int flags, int len)
{
    #ifdef LOG_SYSCALLS
    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"open (name=0x%08X, flags=%d,  len=%d)", name,flags,len);
    #endif

    return 1;

}

//newcomers
//
int syscall_close(int file,int none1,int none2)  
{
    #ifdef LOG_SYSCALLS
    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"close (file=%d)", file);
    #endif

    return -1;
}

int syscall_fstat(int file, struct stat *st,int none)
{
    #ifdef LOG_SYSCALLS
    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"fstat (file=%d,stat=0x%08X)", file,st);
    #endif

    st->st_mode = S_IFCHR;
    return 0;
}

int syscall_isatty(int file,int none1,int none2)  
{
    #ifdef LOG_SYSCALLS
    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"isatty (file=%d)", file);
    #endif

    return 1;
}

int syscall_lseek(int file,int ptr,int dir) 
{

    #ifdef LOG_SYSCALLS
    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"lseek (file=%d, ptr=%d, dir=%d)", file,ptr,dir);
    #endif

    if(dir==0)preread=ptr;
    else{ while(1);}
    return preread;
}

caddr_t syscall_sbrk(int incr, int none1, int none2) 
{
    #ifdef LOG_SYSCALLS
    log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"sbrk (incr=%d)", incr);
    #endif

    int oldalloc=alloc;
    alloc+=incr;
    return oldalloc;

	    /*
	    
            extern char end;	    
//	    char _end;
            static char *heap_end;
            char *prev_heap_end;
          
            if (heap_end == 0) {
              heap_end = &end;
            }
            prev_heap_end = heap_end;

            if (heap_end + incr > stack_ptr) {
              write (1, "Heap and stack collision\n", 25);
              abort ();
            }
          
            heap_end += incr;
            return (caddr_t) prev_heap_end;
	    */
}