-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys.c
More file actions
533 lines (416 loc) · 12.9 KB
/
Copy pathsys.c
File metadata and controls
533 lines (416 loc) · 12.9 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
* sys.c - Syscalls implementation
*/
#include <devices.h>
#include <segment.h>
#include <utils.h>
#include <errno.h>
#include <sched.h>
#include <mm_address.h>
#include <mm.h>
#include <interrupt.h>
#include <io.h>
#include <sf.h>
int
sys_ni_syscall ()
{
return -ENOSYS;
}
int
sys_write (int fd, char *buffer, int size)
{
struct task_struct *current_task;
struct fitxers_oberts *fitxer_obert;
int return_write;
current_task = current ();
fitxer_obert = (current_task->taula_canals[fd]);
if (fd < 0 || fd >= NUM_CANALS)
return -ECHRNG;
if (current_task->taula_canals[fd] == NULL)
return -EBADF;
if (fitxer_obert->mode_acces == O_RDONLY)
return -EROFS;
if (size < 0)
return -EINVAL;
if (size > ((NUM_PAG_DATA * PAGE_SIZE) - KERNEL_STACK_SIZE))
return -EFBIG;
return_write = check_address(buffer,size);
if (return_write != 0) return return_write;
if (fitxer_obert->opened_file->operations->sys_write_dev != NULL)
return_write =
(*((fitxer_obert->opened_file)->operations->sys_write_dev)) (fd,
buffer,
size);
else return -EINVAL;
return return_write;
}
int
sys_read (int fd, char *buffer, int size)
{
struct task_struct *current_task;
struct fitxers_oberts *fitxer_obert;
int return_read;
current_task = current ();
current_task->size = size; //KBD
if (fd < 0 || fd >= NUM_CANALS)
return -ECHRNG;
if (current_task->taula_canals[fd] == NULL)
return -EBADF;
if (size < 0)
return -EINVAL;
if (size > (NUM_PAG_DATA * PAGE_SIZE) - KERNEL_STACK_SIZE)
return -EFBIG;
fitxer_obert = (current_task->taula_canals[fd]);
if (fitxer_obert->mode_acces == O_WRONLY)
return -EPERM;
current_task->buffer = buffer;
if (fitxer_obert->opened_file->operations->sys_read_dev == NULL) return -ENOSYS;
return_read = (*(fitxer_obert->opened_file->operations->sys_read_dev)) (fd,buffer,size);
//Farem que si hi ha error de i/o acabi la transaccio
if (return_read == -1)
return -EIO;
return return_read;
}
/*
* Note that the flag value (low two bits) for sys_open means:
* 00 - read-only
* 01 - write-only
* 10 - read-write
*/
int
sys_open (const char *path, int flags)
{
int fd, file_entry, tfo_entry;
struct task_struct *proces = current ();
struct file *file;
if (strlen (path) > MAX_NAME_LENGTH)
return -ENAMETOOLONG;
//Comprovem que podem obrir mes fitxers
for (tfo_entry = 0; tfo_entry < MAX_OPEN_FILES &&
taula_fitxers_oberts[tfo_entry].refs > 0; tfo_entry++);
if (tfo_entry == MAX_OPEN_FILES)
return -ENFILE;
//Comprovem que ens queden canals
for (fd = 0; fd < NUM_CANALS && proces->taula_canals[fd] != NULL; fd++);
if (fd == NUM_CANALS)
return -EMFILE;
//Obtenim el fitxer del directori
for (file_entry = 0;
file_entry < MAX_FILES && (strcmp (directori[file_entry].nom, path));
file_entry++);
if (file_entry == MAX_FILES)
if (flags < O_CREAT)
return -ENOENT;
else
{
//Crear nou file:
file = create_file (path);
if (file < 0)
return (int) file;
flags -= O_CREAT;
}
else
file = &directori[file_entry];
//Nomes open depenent al qui ho tingui implementat
if (file->operations->sys_open_dev != NULL)
(*(file->operations->sys_open_dev)) (path, flags);
//Obrim el dispositiu/fitxer!
//Comprovem que tenim permis d'acces
if (file->mode_acces_valid != O_RDWR && flags != file->mode_acces_valid)
return -EPERM;
//Nova entrada a la TFO i punter al canal
taula_fitxers_oberts[tfo_entry].refs = 1;
taula_fitxers_oberts[tfo_entry].mode_acces = flags;
taula_fitxers_oberts[tfo_entry].lseek = 0;
taula_fitxers_oberts[tfo_entry].opened_file = file;
file->n_refs++;
proces->taula_canals[fd] = &taula_fitxers_oberts[tfo_entry];
return fd;
}
int
sys_close (int fd)
{
struct task_struct *current_task;
current_task = current ();
if (fd < 0 || fd >= NUM_CANALS)
return -ECHRNG;
if (current_task->taula_canals[fd] == NULL)
return -EBADF;
//Close dependent
if (current_task->taula_canals[fd]->opened_file->operations->
sys_close_dev != NULL)
(current_task->taula_canals[fd]->opened_file->operations->
sys_close_dev) (fd);
//Close generic
current_task->taula_canals[fd]->refs--;
current_task->taula_canals[fd]->opened_file->n_refs--;
current_task->taula_canals[fd] = NULL;
return 0;
}
int
sys_dup (int fd)
{
int new_fd;
struct task_struct *proces = current ();
if (fd < 0 || fd >= NUM_CANALS)
return -ECHRNG;
if (proces->taula_canals[fd] == NULL)
return -EBADF;
for (new_fd = 0;
new_fd < NUM_CANALS && proces->taula_canals[new_fd] != NULL; new_fd++);
if (new_fd == NUM_CANALS)
return -EMFILE;
proces->taula_canals[new_fd] = proces->taula_canals[fd];
proces->taula_canals[new_fd]->refs++;
proces->taula_canals[new_fd]->opened_file->n_refs++;
return new_fd;
}
int
sys_unlink (const char *path)
{
struct file *fitxer;
int file_entry;
if (strlen (path) > MAX_NAME_LENGTH)
return -ENAMETOOLONG;
for (file_entry = 0;
file_entry < MAX_FILES && (strcmp (directori[file_entry].nom, path));
file_entry++);
if (file_entry == MAX_FILES)
return -ENOENT;
fitxer = &directori[file_entry];
if (fitxer->n_refs == 0)
{
fitxer->n_refs = -1;
if (fitxer->operations->sys_release_dev != NULL)
(fitxer->operations->sys_release_dev) (file_entry);
fitxer->operations->sys_open_dev = NULL;
fitxer->operations->sys_close_dev = NULL;
fitxer->operations->sys_read_dev = NULL;
fitxer->operations->sys_write_dev = NULL;
fitxer->operations->sys_release_dev = NULL;
fitxer->n_refs = -1;
fitxer->nom[0] = 0;
}
return -EBUSY;
}
int
sys_readdir (struct dir_ent *buffer, int offset)
{
int size = 0;
char *s1 = directori[offset].nom;
if (offset < 0 || offset >= MAX_FILES)
return -ENOENT;
if (directori[offset].n_refs == -1)
return -ENOENT;
for (; *s1 != 0; ++s1, ++size);
copy_to_user (directori[offset].nom, buffer, size);
buffer->size = directori[offset].size;
return 0;
}
int
sys_nice (int quantum)
{
int old = -EINVAL; /* Invalid argument */
struct task_struct *current_task = current ();
if (quantum > 0)
{
old = current_task->quantum;
current_task->quantum = quantum;
}
return old;
}
int
sys_getpid ()
{
struct task_struct *p = current ();
return p->pid;
}
int
sys_fork ()
{
int i, j;
int frames[NUM_PAG_DATA];
int fill = search_free_task ();
if (fill == -1)
return -EAGAIN;
/* Copiar el task_union del pare al fill */
copy_data (current (), &task[fill], 4096);
/* Herencia dades usuari, mirem que hi hagi prou frames lliures */
for (j = 0; j < NUM_PAG_DATA && current ()->pagines_fisiques[j] != -1; j++)
{
frames[j] = alloc_frames (1);
if (frames[j] == -1)
{
for (i = j - 1; i >= 0; i--)
{
free_frames (frames[i], 1);
}
task[fill].task.pid = -1;
return -EAGAIN;
}
}
for (i = 0; i < NUM_PAG_DATA && i <= j; i++)
{
/* Associa sa pagina logica del pare amb sa pagina fisica o 'frame'
del fill(nomes de forma temporal) */
set_ss_pag (PAG_LOG_INIT_DATA_P0 + NUM_PAG_DATA + i, frames[i]);
/* Copiam el frame del pare al frame del fill, el qual acabam
d'associar amb la pagina logica del nou proces */
copy_data ((void *) (PAGE_SIZE * (PAG_LOG_INIT_DATA_P0 + i)),
(void *) (PAGE_SIZE *
(PAG_LOG_INIT_DATA_P0 + NUM_PAG_DATA + i)),
PAGE_SIZE);
/* Guardar la informacio sobre els nous marcs de pagines al task_struct del fill */
task[fill].task.pagines_fisiques[i] = frames[i];
/*Alliberem les pagines fisiques temporals */
del_ss_pag (PAG_LOG_INIT_DATA_P0 + NUM_PAG_DATA + i);
}
set_cr3 ();
/* Modifiquem el 'PID' del fill mitjancant l'eax, que sera el valor que retornara
quan el proces restauri el seu context. -10 perque quan entrem al sistema
s'apila ss,esp,eflags,cs,eip i llavors tots els registres de la macro SAVE_ALL
(entry.S).
*/
task[fill].stack[KERNEL_STACK_SIZE - 10] = 0;
/* Inicialitzar els camps del task_struct no comuns al fill */
task[fill].task.pid = pid++;
task[fill].task.quantum = current ()->quantum; /* Tots els processos tindran el mateix quantum */
task[fill].task.tics_cpu = 0;
/* Incrementem referencies de la TFO i dels Files */
for (i = 0; i < NUM_CANALS; i++)
{
if (task[fill].task.taula_canals[i] != NULL)
{
task[fill].task.taula_canals[i]->refs++;
task[fill].task.taula_canals[i]->opened_file->n_refs++;
}
}
/* Inserir el nou proces a la llista de preparats: runqueue */
list_add_tail (&(task[fill].task.run_list), &runqueue);
return pid - 1;
}
void
sys_exit ()
{
int i;
struct task_struct *proces_actual = current ();
/* Seguent es el punter al task_union seguent de la runqueue */
union task_union *seguent =
(union task_union *) list_head_to_task_struct (proces_actual->run_list.
next);
if (proces_actual->pid != 0)
{
/* Mai podem matar el proces 0 */
/* Alliberar les estructures del proces */
proces_actual->pid = -1; /* Marcam la posicio del vector task com a lliure */
/* Alliberem taula de canals */
for (i = 0; i < NUM_CANALS; i++)
sys_close (i);
list_del (&proces_actual->run_list); /* Eliminem el proces de la runqueue */
for (i = 0; i < NUM_PAG_DATA; i++) /* Alliberam les pagines fisiques */
free_frames (proces_actual->pagines_fisiques[i], 1);
/* Posar el seguent element de la runqueue */
task_switch (seguent);
}
}
int
sys_get_stats (int spid, int *tics)
{
int i = 0;
if (spid < 0)
return -EINVAL; /* Invalid argument */
if (spid > NR_TASKS)
return -ESRCH; /* No such process */
/* Cercam el proces amb PID=spid */
for (i = 0; i < NR_TASKS && task[i].task.pid != spid; i++);
if (task[i].task.pid != spid)
return -ESRCH; /* No such process */
/* Si hi ha el proces amb PID=spid */
return copy_to_user (&(task[i].task.tics_cpu), tics, 4);
}
int
sys_sem_init (int n_sem, unsigned int value)
{
/* inicialitzam el comptador del semafor n_sem a value */
if (n_sem < 0)
return -EINVAL; /* Error si l'identificador n_sem es invalid */
else if (n_sem >= SEM_VALUE_MAX)
return -EINVAL;
if (sem[n_sem].init == 1)
return -EBUSY; /* Error si el semafor n_sem ja esta inicialitzat -> Device or resource busy */
/* init=0 => NO inicialitzat, init=1 => SI inicialitzat */
sem[n_sem].count = value;
sem[n_sem].init = 1;
INIT_LIST_HEAD (&(sem[n_sem].queue));
return 0;
}
int
sys_sem_wait (int n_sem)
{
/* Si el comptador del semafor n_sem es mes petit o igual que zero,
aquesta crida bloqueja en aquest semafor el proces que la ha invocat.
En cas que el comptador sigui mes gran que zero, aquesta crida decrementa
el valor del semafor. El proces 0 no es pot bloquejar en cap cas, per tant
s'ha de considerar un error la utilitzacio d'aquesta crida pel proces 0. */
struct task_struct *old_task;
union task_union *new_task;
old_task = current ();
if (n_sem < 0 || n_sem >= SEM_VALUE_MAX || sem[n_sem].init == 0)
return -EINVAL;
if (old_task->pid == 0)
return -EPERM;
if (sem[n_sem].count <= 0)
{
/*Eliminam de la runqueue i el posem a la cua de bloqued del semafor */
list_del (&(old_task->run_list));
list_add_tail (&(old_task->run_list), &(sem[n_sem].queue));
/*Fem un task_switch a una nova tasca */
new_task =
(union task_union *) (list_head_to_task_struct (runqueue.next));
call_from_int = 0;
task_switch (new_task);
}
else
sem[n_sem].count--;
return 0;
}
int
sys_sem_signal (int n_sem)
{
/* Si no hi ha cap proces bloquejat en el semafor n_sem aleshores
aquesta crida incrementa el comptador del semafor n_sem. En el cas
que hi hagi un o mes processos bloquejats sobre n_sem, aquesta crida
desbloqueja el primer proces. */
struct list_head *blocked_task;
union task_union *blocked_task_union;
if (n_sem < 0 || n_sem >= SEM_VALUE_MAX || sem[n_sem].init == 0)
return -EINVAL;
if (!list_empty (&(sem[n_sem].queue)))
{
blocked_task = sem[n_sem].queue.next;
list_del (blocked_task);
//Hem de fer que l'eax tingui el valor del return del sem_wait
blocked_task_union =
get_task_union (list_head_to_task_struct (blocked_task));
if ((int) blocked_task_union == NULL)
return -ESRCH;
blocked_task_union->stack[KERNEL_STACK_SIZE - 10] = 0;
list_add (blocked_task, &runqueue);
}
else
sem[n_sem].count++;
return 0;
}
int
sys_sem_destroy (int n_sem)
{
/* destrueim el semafor n_sem si aquest esta inicialitzat */
if (n_sem < 0 || n_sem >= SEM_VALUE_MAX)
return -EINVAL; /* Error si l'identificador n_sem es invalid */
if (sem[n_sem].init == 0)
return -EINVAL; /* Error si el semafor no esta inicialitzat */
else if (!list_empty (&sem[n_sem].queue))
return -EBUSY; /* Error si encara hi ha processos bloquejats a la cua */
sem[n_sem].init = 0;
return 0;
}