-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys.c
More file actions
472 lines (373 loc) · 11.5 KB
/
sys.c
File metadata and controls
472 lines (373 loc) · 11.5 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
/*
* sys.c - Syscalls implementation
*/
#include <block.h>
#include <devices.h>
#include <errno.h>
#include <io.h>
#include <list.h>
#include <mm.h>
#include <mm_address.h>
#include <random.h>
#include <sched.h>
#include <semaphore.h>
#include <types.h>
#include <utils.h>
#define LECTURA 0
#define ESCRIPTURA 1
int check_fd(int fd, int permissions) {
if (fd != 1)
return -EBADF;
if (permissions != ESCRIPTURA)
return -EACCES;
return 0;
}
int sys_ni_syscall() { return -ENOSYS; }
int sys_getpid() { return current()->PID; }
int sys_fork() {
union task_union *new = NULL;
int clone_ret;
if ((clone_ret = clone_current_task(&new)) != 0) {
return -clone_ret;
}
allocate_DIR(&new->task);
page_table_entry *parent_PT = get_PT(current());
int user_page_nr = 0;
int temp_page = -1;
for (int i = NUM_PAG_KERNEL; i < TOTAL_PAGES; ++i) {
if (parent_PT[i].bits.present)
++user_page_nr;
else
temp_page = i;
}
int phys_frames[user_page_nr];
if (temp_page < 0 || alloc_frames(user_page_nr, phys_frames) == -1) {
new->task.PID = -1;
new->task.TID = -1;
list_add(&new->task.queue_anchor, &free_queue);
return -ENOMEM;
}
page_table_entry *new_PT = get_PT(&new->task);
copy_data(parent_PT, new_PT, sizeof(page_table_entry) * NUM_PAG_KERNEL);
int *next_phys_frame = phys_frames;
for (int i = NUM_PAG_KERNEL; i < TOTAL_PAGES; ++i) {
if (parent_PT[i].bits.present) {
int current_frame = *(next_phys_frame++);
set_ss_pag(new_PT, i, current_frame);
new_PT[i].bits.avail = parent_PT[i].bits.avail;
set_ss_pag(parent_PT, temp_page, current_frame);
set_cr3(current()->dir_pages_baseAddr);
copy_data(
(void *)(i * PAGE_SIZE), (void *)(temp_page * PAGE_SIZE), PAGE_SIZE);
}
}
del_ss_pag(parent_PT, temp_page);
set_cr3(current()->dir_pages_baseAddr);
new->task.PID = allocate_new_pid();
new->task.TID = allocate_new_tid();
new->task.sem_group = 0;
new->stack[KERNEL_STACK_SIZE - 19] = (unsigned long)ret_from_fork;
new->stack[KERNEL_STACK_SIZE - 20] =
(unsigned long)&new->stack[KERNEL_STACK_SIZE - 18];
new->task.esp = (unsigned long)&new->stack[KERNEL_STACK_SIZE - 20];
update_process_state_rr(&new->task, &ready_queue);
return new->task.PID;
}
int sys_waitkey(char *buffer, int timeout) {
if (!access_ok(VERIFY_WRITE, (void *)buffer, 1)) {
return -14; // EFAULT
}
if (timeout <= 0)
return -22; // EINVAL
int error = sys_read_key(buffer, timeout);
if (error < 0)
return error;
else
return 0;
}
int sys_write(int fd, char *buffer, int size) {
int check_fd_result = check_fd(fd, ESCRIPTURA);
if (check_fd_result != 0) {
return check_fd_result;
}
if (size == 0)
return 0;
if (size < 0)
return -EINVAL;
if (!access_ok(VERIFY_READ, (void *)buffer, size)) {
return -EFAULT;
}
int (*write_function)(char *, int);
switch (fd) {
case 1:
write_function = sys_write_console;
break;
default:
return -EIO;
}
int remaining = size;
while (remaining > 0) {
const int MAX_BUFF_SIZE = KERNEL_STACK_SIZE / 4;
int buff_size;
if (remaining > MAX_BUFF_SIZE)
buff_size = MAX_BUFF_SIZE;
else
buff_size = remaining;
char sys_buffer[buff_size];
if (copy_from_user(&buffer[size - remaining], sys_buffer, buff_size) != 0) {
return -EFAULT;
}
int written = write_function(sys_buffer, buff_size);
if (written <= 0)
return written;
remaining -= written;
if (written < buff_size)
return size - remaining;
}
return size;
}
int sys_gettime() { return zeos_ticks; }
int sys_get_stats(int pid, struct stats *st) {
struct task_struct *task = get_task_with_pid(pid);
if (!access_ok(VERIFY_WRITE, st, sizeof(struct stats))) {
return -EFAULT;
}
if (task == NULL)
return -ESRCH;
if (copy_to_user(&task->st, st, sizeof(struct stats)) != 0) {
return -EFAULT;
} else {
return 0;
}
}
int sys_gotoXY(int new_x, int new_y) {
if (new_x < 0 || new_x > NUM_COLUMNS)
return -EINVAL;
if (new_y < 0 || new_y > NUM_ROWS)
return -EINVAL;
x = new_x;
y = new_y;
return 0;
}
int sys_changeColor(int fg, int bg) {
// Background colors bigger than 8 activate blinking instead of a bright color
if (fg < 0 || fg > 0xF)
return -EINVAL;
if (bg < 0 || bg > 0xF)
return -EINVAL;
foreground = fg;
background = bg;
return 0;
}
int sys_clrscr(char *b) {
Word *screen = (Word *)0xb8000;
if (b == ((void *)0)) {
char print_char = ' ';
int color = (background << 12 | foreground << 8) & 0xFF00;
Word ch = (Word)(print_char & 0x00FF) | color;
for (Byte i = 0; i < NUM_COLUMNS; ++i) {
for (Byte j = 0; j < NUM_ROWS; ++j) {
screen[(j * NUM_COLUMNS + i)] = ch;
}
}
} else {
int size = sizeof(char) * NUM_COLUMNS * NUM_ROWS * 2;
if (!access_ok(VERIFY_READ, b, size) || copy_from_user(b, screen, size) < 0)
return -EFAULT;
}
return 0;
}
void (*pthread_wrapper)(void *(void *), void *) = NULL;
int sys_set_thread_wrapper(void (*wrapper)(void *(void *), void *)) {
if (pthread_wrapper != NULL)
return -EINVAL;
if (!access_ok(VERIFY_READ, wrapper, 1))
return -EFAULT;
pthread_wrapper = wrapper;
return 0;
}
int sys_create_thread_stack(
void (*function)(void *arg),
int N,
void *parameter) {
if (!access_ok(VERIFY_READ, function, sizeof(void *)))
return -EFAULT;
if (N < 1)
return -EINVAL;
if (!access_ok(VERIFY_READ, parameter, sizeof(void *)))
return -EFAULT;
union task_union *new = NULL;
int clone_ret;
if ((clone_ret = clone_current_task(&new)) != 0) {
return clone_ret;
}
int phys_frames[N + 1]; //+1 for the TLS even thought we don't use it :(
if (alloc_frames(N + 1, phys_frames) == -1) {
new->task.TID = -1;
new->task.PID = -1;
list_add(&new->task.queue_anchor, &free_queue);
return -ENOMEM;
}
page_table_entry *PT = get_PT(current());
int block_sizes[2] = {N, 1};
int block_starts[2] = {};
if (allocate_user_pages(block_sizes, block_starts, 2, PT, phys_frames) != 0) {
new->task.TID = -1;
new->task.PID = -1;
list_add(&new->task.queue_anchor, &free_queue);
free_frames(N + 1, phys_frames);
return -ENOMEM;
}
int first_stack_page = block_starts[0];
set_cr3(get_DIR(current()));
list_add(&new->task.thread_anchor, ¤t()->thread_anchor);
new->task.TID = allocate_new_tid();
unsigned long *stack_bottom =
(unsigned long *)((N + first_stack_page) * PAGE_SIZE - 4);
new->stack[KERNEL_STACK_SIZE - 3] = (unsigned long)(stack_bottom - 2); // ESP
new->stack[KERNEL_STACK_SIZE - 6] = (unsigned long)pthread_wrapper; // EIP
unsigned long ret_to_pagefault = NULL;
if (copy_to_user(¶meter, stack_bottom, sizeof(unsigned long)) ||
copy_to_user(&function, stack_bottom - 1, sizeof(unsigned long))) {
new->task.TID = -1;
new->task.PID = -1;
list_add(&new->task.queue_anchor, &free_queue);
deallocate_user_pages(block_sizes, block_starts, 2, PT, NULL);
free_frames(N + 1, phys_frames);
}
copy_to_user(
&ret_to_pagefault,
stack_bottom - 2,
sizeof(unsigned long)); // @RET
update_process_state_rr(&new->task, &ready_queue);
new->task.esp = (unsigned long)&new->stack[KERNEL_STACK_SIZE - 19];
return 0;
}
char *sys_memRegGet(int num_pages) {
if (num_pages <= 0)
return (char *)-EINVAL;
int frames[num_pages];
if (alloc_frames(num_pages, frames) < 0)
return (char *)-ENOMEM;
page_table_entry *PT = get_PT(current());
int first_page;
if (allocate_user_pages(&num_pages, &first_page, 1, PT, frames) < 0) {
free_frames(num_pages, frames);
return (char *)-ENOMEM;
}
for (int page = first_page; page < first_page + num_pages; ++page) {
if (page == first_page)
PT[page].bits.avail = PT_AVAIL_USER_ALLOCATED_HEAD;
else
PT[page].bits.avail = PT_AVAIL_USER_ALLOCATED;
}
set_cr3(get_DIR(current()));
return (char *)(PAGE_SIZE * first_page);
}
int sys_memRegDel(char *m) {
if ((unsigned long)m % PAGE_SIZE != 0)
return -EINVAL;
if (!access_ok(VERIFY_READ, m, sizeof(char *)))
return -EFAULT;
page_table_entry *PT = get_PT(current());
int first_page = (unsigned long)m / PAGE_SIZE;
if (!((PT[first_page].bits.avail & PT_AVAIL_USER_ALLOCATED_HEAD) ==
PT_AVAIL_USER_ALLOCATED_HEAD))
return -EINVAL;
int num_pages = 1;
while (first_page + num_pages < TOTAL_PAGES &&
(PT[first_page + num_pages].bits.avail &
PT_AVAIL_USER_ALLOCATED_HEAD) == PT_AVAIL_USER_ALLOCATED)
++num_pages;
int frames[num_pages];
deallocate_user_pages(&num_pages, &first_page, 1, PT, frames);
free_frames(num_pages, frames);
set_cr3(get_DIR(current()));
return 0;
}
sem_t *sys_semCreate(int initial_value) {
struct task_struct *process = current();
// Get the process' semaphore group or assign one if it doesn't have any
struct sem_group *semaphore_group;
if (process->sem_group == 0) {
semaphore_group = assign_semaphore_group(process);
if (semaphore_group == 0)
return (sem_t *)-EAGAIN;
} else {
semaphore_group = process->sem_group;
}
semaphore_group->in_use_sems++;
// Search for available semaphore and when found initialize it
for (int i = 0; i < NR_TASKS; ++i) {
struct sem *current_semaphore = &semaphore_group->semaphores[i];
if (current_semaphore->owner_TID != -1)
continue;
current_semaphore->owner_TID = process->TID;
current_semaphore->counter = initial_value;
return (sem_t *)i;
}
return (sem_t *)-ENOMEM;
}
int sys_semWait(sem_t *s) {
struct sem *current_semaphore = get_semaphore(s);
if (current_semaphore == 0)
return -EINVAL; // Again error, i'll look it up later
if (current_semaphore->owner_TID == -1)
return -EINVAL;
current()->blocked.reason = BR_SEMAPHORE;
current()->blocked.blocked.semaphore.s = current_semaphore;
if (--(current_semaphore->counter) < 0)
block();
return 0;
}
int sys_semSignal(sem_t *s) {
struct sem *current_semaphore = get_semaphore(s);
if (current_semaphore == 0)
return -EINVAL;
if (current_semaphore->owner_TID == -1)
return -EINVAL;
if (++(current_semaphore->counter) <= 0 &&
!list_empty(¤t_semaphore->blocked_anchor)) {
if (unblock(list_first(¤t_semaphore->blocked_anchor)) < 0)
return -EINVAL;
}
return 0;
}
int sys_semDestroy(sem_t *s) {
struct sem *current_semaphore = get_semaphore(s);
struct task_struct *process = current();
int unblocked_threads = 0;
if (current_semaphore == 0)
return -EINVAL;
if (current_semaphore->owner_TID == -1)
return -EINVAL;
if (current_semaphore->owner_TID != process->TID)
return -EINVAL;
unblocked_threads = unblock_blocked_semaphore_threads(current_semaphore);
if (unblocked_threads == -1)
return -EINVAL;
initialize_semaphore(current_semaphore);
if (--current()->sem_group->in_use_sems == 0) {
int unassign_sem_group_res = unassign_semaphore_group(process);
if (unassign_sem_group_res == 0)
return -EINVAL;
}
return unblocked_threads;
}
void sys_exit() {
struct task_struct *process = current();
// We destroy any semaphores whose creator is the thread that's exiting
if (process->sem_group != 0) {
for (int i = 0; i < NR_SEMS; ++i) {
if (process->sem_group->semaphores[i].owner_TID == process->TID)
sys_semDestroy((sem_t *)i);
}
}
if (list_empty(&process->thread_anchor))
clear_user_space(process);
list_del(&process->thread_anchor);
process->PID = -1;
process->TID = -1;
list_add(&(process->queue_anchor), &free_queue);
sched_next_rr();
}