-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys.c
313 lines (259 loc) · 8.42 KB
/
sys.c
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* sys.c - Syscalls implementation
*/
#include <devices.h>
#include <utils.h>
#include <io.h>
#include <mm.h>
#include <mm_address.h>
#include <sched.h>
#include <cbuffer.h>
#include <errno.h>
#define LECTURA 0
#define ESCRIPTURA 1
int zeos_ticks;
extern struct list_head freequeue, readyqueue, readqueue;
extern int dir_pages_refs[NR_TASKS];
extern int quantum_left;
extern struct cbuffer read_buffer;
extern struct sem semaphores[20];
unsigned long getEbp();
extern int MAX_PID;
int check_fd(int fd, int permissions)
{
if (fd == 1 && permissions!=ESCRIPTURA) return -EACCES;
if (fd == 0 && permissions!=LECTURA) return -EACCES;
if (fd != LECTURA && fd != ESCRIPTURA) return -EBADF;
return 0;
}
int sys_ni_syscall()
{
return -38; /*ENOSYS*/
}
int sys_getpid()
{
return current()->PID;
}
int ret_from_fork(){
return 0;
}
int sys_fork()
{
int PID=-1;
// creates the child process
if (list_empty(&freequeue)) return -EAGAIN; // NO PROCESS LEFT
struct list_head *new_task_ptr = list_first(&freequeue);
list_del(new_task_ptr);
struct task_struct *new_task = list_head_to_task_struct(new_task_ptr);
// Copy data from parent to child
copy_data(current(), new_task,(int) sizeof(union task_union));
if(allocate_DIR(new_task) < 0) return -ENOMEM;
page_table_entry * n_pt = get_PT(new_task);
page_table_entry * c_pt = get_PT(current());
int page;
for (page = 0; page < NUM_PAG_KERNEL; page++){
n_pt[1+page].entry = c_pt[1+page].entry;
}
for (page = 0; page < NUM_PAG_CODE; page++){
n_pt[PAG_LOG_INIT_CODE+page].entry = c_pt[PAG_LOG_INIT_CODE+page].entry;
}
int new_frames[NUM_PAG_DATA];
for(page = 0; page< NUM_PAG_DATA; page++){
if((new_frames[page] = alloc_frame()) < 0){
for(page=page-1;page >=0;page--)
free_frame(new_frames[page]);
return -ENOMEM; // no free space left
}
set_ss_pag(n_pt, PAG_LOG_INIT_DATA+page, new_frames[page]);
set_ss_pag(c_pt, PAG_LOG_INIT_DATA+NUM_PAG_DATA+page, new_frames[page]);
copy_data( (int*)((PAG_LOG_INIT_DATA+page)<<12),
(int*)((PAG_LOG_INIT_DATA+NUM_PAG_DATA+page)<<12),
PAGE_SIZE
);
del_ss_pag(c_pt, PAG_LOG_INIT_DATA+NUM_PAG_DATA+page);
}
set_cr3(get_DIR(current()));
PID = MAX_PID++;
new_task->PID = PID;
new_task->state = ST_READY;
memset(&(new_task->stats), 0, sizeof(struct stats));
/*
for (unsigned long * p = (unsigned long *) &(new_task->stats);
p != (unsigned long *)(&new_task->stats + sizeof(struct stats)); *(p++) = 0);
*/
int index = (getEbp() - (int) current())/sizeof(int);
((union task_union*)new_task)->stack[index] =(int) ret_from_fork;
((union task_union*)new_task)->stack[index-1] = 0;
new_task->kernel_esp= &((union task_union*)new_task)->stack[index-1];
list_add_tail(new_task_ptr, &readyqueue);
return PID;
}
void sys_exit()
{
struct task_struct * a = current();
int i;
for ( i = 0; i<NR_SEM; i++ ){
if(semaphores[i].owner == a->PID){
sys_sem_destroy(i);
}
}
a->PID = -1;
int pos = ((int) a->dir_pages_baseAddr - (int) dir_pages) /(sizeof(page_table_entry)*1024);
if(!(--dir_pages_refs[pos])) {
page_table_entry * pt = get_PT(a);
int page;
for(page = 0; page< NUM_PAG_DATA; page++){
free_frame(pt[PAG_LOG_INIT_DATA+page].bits.pbase_addr);
del_ss_pag(pt, PAG_LOG_INIT_DATA+page);
}
}
update_process_state_rr(a,&freequeue);
sched_next_rr();
}
#define CHUNK_SIZE 64
int sys_write(int fd, char* buffer, int size){
int err;
if((err=check_fd(fd, ESCRIPTURA)) < 0) return err;
if(buffer == NULL) return -EFAULT;
if(size < 0) return -EINVAL;
char dest[CHUNK_SIZE];
int i = 0;
int writen = 0;
while (i < size-CHUNK_SIZE){
copy_from_user(buffer+i,dest, CHUNK_SIZE);
writen += sys_write_console(dest, CHUNK_SIZE);
i+=CHUNK_SIZE;
}
copy_from_user(buffer+i,dest, size%CHUNK_SIZE);
writen += sys_write_console(dest, size%CHUNK_SIZE);
return writen;
}
int sys_read_keyboard(char *buffer, int size) {
char src[CHUNK_SIZE];
char *dest = buffer;
int i = 0;
while( i < size ){
if(cbuffer_empty(&read_buffer)){
enqueue_current(&readqueue);
sched_next_rr();
continue;
}
src[i%CHUNK_SIZE]=cbuffer_pop(&read_buffer);
i++;
if(!i%CHUNK_SIZE) {
copy_to_user(src, dest, CHUNK_SIZE);
dest += CHUNK_SIZE;
}
}
if (size%CHUNK_SIZE)
copy_to_user(src, dest, size%CHUNK_SIZE);
return size;
}
int sys_read(int fd, char* buffer, int size){
int err;
if((err=check_fd(fd, LECTURA)) < 0) return err;
if(buffer == NULL) return -EFAULT;
if(size < 0) return -EINVAL;
return sys_read_keyboard(buffer, size);
}
int sys_gettime(){
return zeos_ticks;
}
int sys_get_stats(int pid, struct stats *st){
struct task_struct *act;
int i;
if(!access_ok(VERIFY_WRITE, st, sizeof(struct stats))) return -EFAULT;
if (pid < 0) return -EINVAL;
for (act = &(task[i=0].task); i < NR_TASKS; act = &(task[++i].task)){
if (act -> PID == pid){
//*st = (act -> stats);
act->stats.remaining_ticks = quantum_left;
copy_to_user(&(act->stats), st, sizeof(struct stats));
return 0;
}
}
return -ESRCH;
}
int sys_clone(void (* function)(void), void *stack){
int PID=-1;
if (!access_ok(VERIFY_WRITE, stack, sizeof(void*))) return -EFAULT;
if (!access_ok(VERIFY_READ, function, sizeof(void*))) return -EFAULT;
// creates the child process
if (list_empty(&freequeue)) return -EAGAIN; // NO PROCESS LEFT
struct list_head *new_task_ptr = list_first(&freequeue);
list_del(new_task_ptr);
struct task_struct *new_task = list_head_to_task_struct(new_task_ptr);
// Copy data from parent to child
copy_data(current(), new_task,(int) sizeof(union task_union));
int pos = ((int) new_task->dir_pages_baseAddr - (int) dir_pages) /(sizeof(page_table_entry)*1024);
dir_pages_refs[pos]++;
PID = MAX_PID++;
new_task->PID = PID;
new_task->state = ST_READY;
memset(&(new_task->stats), 0, sizeof(struct stats));
/*
for (unsigned long * p = (unsigned long *) &(new_task->stats);
p != (unsigned long *)((&new_task->stats) + 1); *(p++) = 0);
*/ int index = (getEbp() - (int) current())/sizeof(int);
new_task->kernel_esp= &((union task_union*)new_task)->stack[index];
((union task_union*)new_task)->stack[KERNEL_STACK_SIZE - 2]=(int)stack;
((union task_union*)new_task)->stack[KERNEL_STACK_SIZE - 5]=(int)function;
list_add_tail(new_task_ptr, &readyqueue);
return PID;
}
int sys_sem_destroy(int n_sem){
if ( n_sem < 0 || n_sem >= 20) return -EINVAL;
struct sem *s = &semaphores[n_sem];
//uninitzializated
if ( s->owner <= 0 ) return -EINVAL;
if ( current()->PID != s->owner) return -EPERM;
while(!list_empty(&s->queue)){
struct task_struct *act = list_head_to_task_struct(list_first(&s->queue));
act->sem_wait_ret = -1;
update_process_state_rr(act, &readyqueue);
}
s->owner = 0;
return 1;
}
int sys_sem_init(int n_sem, unsigned int value){
//invalid n_sem
if(n_sem < 0 || n_sem >=20) return -EINVAL;
struct sem *s = &semaphores[n_sem];
//sem used
if(s->owner > 0 ) return -EBUSY;
//gather sem
s->owner = current()->PID;
s->value = value;
INIT_LIST_HEAD(&s->queue);
return 0;
}
int sys_sem_wait(int n_sem){
//invalid n_sem
if(n_sem < 0 || n_sem >=20) return -EINVAL;
struct sem *s = &semaphores[n_sem];
if(s->owner <= 0) return -EINVAL;
current()->sem_wait_ret = 0;
//#pragma omp critical
if( s->value <= 0 ){
//block the process
enqueue_current(&s->queue);
sched_next_rr();
return current()->sem_wait_ret;
}else{
s->value--;
}
return 0;
}
int sys_sem_signal(int n_sem){
if(n_sem < 0 || n_sem >=20) return -EINVAL;
struct sem *s = &semaphores[n_sem];
//uninitzializated
if( s->owner <= 0 ) return -EINVAL;
//#pragma omp critical
if( list_empty(&s->queue) ){
s->value++;
}else{
update_process_state_rr(list_head_to_task_struct(list_first(&s->queue)), &readyqueue);
}
return 1;
}