This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sys.c
241 lines (192 loc) · 6.65 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
/*
* sys.c - Syscalls implementation
*/
#include <devices.h>
#include <utils.h>
#include <io.h>
#include <sched.h>
#include <error_code.h>
#include "mm.h"
#define LECTURA 0
#define ESCRIPTURA 1
#define ERROR 2
unsigned int zeos_ticks = 0;
extern struct list_head freequeue;
extern struct list_head readyqueue;
void update_stats(int entering) // El pramatre actua com a boolea i inidica si estem entrant o sortint de sys
{
if (entering)
{
current()->stadistics.user_ticks += get_ticks()-current()->stadistics.elapsed_total_ticks;
current()->stadistics.elapsed_total_ticks = get_ticks();
}
else
{
current()->stadistics.system_ticks += get_ticks()-current()->stadistics.elapsed_total_ticks;
current()->stadistics.elapsed_total_ticks = get_ticks();
}
}
int check_fd(int fd, int permissions)
{
if (fd != ESCRIPTURA && fd != ERROR) return EBADF;
if (permissions != ESCRIPTURA) return EACCES;
return 0;
}
int sys_ni_syscall()
{
return ENOSYS;
}
int sys_write(int fd, char * buffer, int size)
{
update_stats(1);
int ret = check_fd(fd, ESCRIPTURA);
if (ret < 0)
return ret;
if (buffer == NULL)
return NULLBUFF;
if (size < 0)
return INVSIZE;
char local_buffer[1024];
int total_bytes = 0;
while (size > 0)
{
int current_bytes = size < 1024 ? size : 1024;
copy_from_user(buffer, local_buffer, current_bytes);
if (fd == ERROR)
total_bytes += sys_write_error(local_buffer, current_bytes);
else
total_bytes += sys_write_console(local_buffer, current_bytes);
buffer += 1024;
size -= 1024;
}
update_stats(0);
return total_bytes;
}
int sys_getpid()
{
return current()->PID;
}
int ret_from_fork()
{
return 0;
}
int sys_fork()
{
update_stats(1);
if (list_empty(&freequeue))
{
return EBNRTASK;
}
struct task_struct * parent_task = current();
struct list_head *empty_process = list_pop(&freequeue);
union task_union * child_union = (union task_union *) list_head_to_task_struct(empty_process);
child_union->task = *parent_task;
child_union->task.PID = (zeos_ticks ^ 0xDEADBEEF) & 0x7FFFFFFF;
allocate_DIR(&child_union->task);
page_table_entry * process_child_PT = get_PT(&child_union->task);
page_table_entry * process_parent_PT = get_PT(parent_task);
{
int pag;
int new_ph_pag;
for (pag=0;pag<NUM_PAG_CODE;pag++)
{
process_child_PT[PAG_LOG_INIT_CODE+pag].entry = process_parent_PT[PAG_LOG_INIT_CODE+pag].entry;
process_child_PT[PAG_LOG_INIT_CODE+pag].bits.pbase_addr = process_parent_PT[PAG_LOG_INIT_CODE+pag].bits.pbase_addr;
process_child_PT[PAG_LOG_INIT_CODE+pag].bits.user = process_parent_PT[PAG_LOG_INIT_CODE+pag].bits.user;
process_child_PT[PAG_LOG_INIT_CODE+pag].bits.present = process_parent_PT[PAG_LOG_INIT_CODE+pag].bits.present;
}
for (pag=0;pag<NUM_PAG_DATA;pag++)
{
new_ph_pag=alloc_frame();
process_child_PT[PAG_LOG_INIT_DATA+pag].entry = 0;
process_child_PT[PAG_LOG_INIT_DATA+pag].bits.pbase_addr = new_ph_pag;
process_child_PT[PAG_LOG_INIT_DATA+pag].bits.user = 1;
process_child_PT[PAG_LOG_INIT_DATA+pag].bits.rw = 1;
process_child_PT[PAG_LOG_INIT_DATA+pag].bits.present = 1;
set_ss_pag(process_parent_PT, PAG_LOG_INIT_CODE + NUM_PAG_CODE + pag, new_ph_pag);
}
}
copy_data((void *) (PAG_LOG_INIT_DATA * PAGE_SIZE), (void *) ((PAG_LOG_INIT_CODE + NUM_PAG_CODE) * PAGE_SIZE), PAGE_SIZE * NUM_PAG_DATA);
for (int pag=0;pag<NUM_PAG_DATA;pag++)
del_ss_pag(process_parent_PT, PAG_LOG_INIT_CODE + NUM_PAG_CODE + pag);
set_cr3(parent_task->dir_pages_baseAddr);
child_union->task.esp = (int) &child_union->stack[KERNEL_STACK_SIZE - 0x13];
child_union->stack[KERNEL_STACK_SIZE - 0x13] = 0;
child_union->stack[KERNEL_STACK_SIZE - 0x12] = (int) &ret_from_fork;
for (int i = 0; i < 0x11; i++)
child_union->stack[KERNEL_STACK_SIZE - 1 - i] = ((union task_union *) parent_task)->stack[KERNEL_STACK_SIZE - 1 - i];
list_add(&child_union->task.list, &readyqueue);
child_union->task.stadistics.user_ticks = 0;
child_union->task.stadistics.system_ticks = 0;
child_union->task.stadistics.blocked_ticks = 0;
child_union->task.stadistics.ready_ticks = 0;
child_union->task.stadistics.elapsed_total_ticks = get_ticks();
child_union->task.stadistics.total_trans = 0;
child_union->task.stadistics.remaining_ticks = 0;
update_stats(0);
return child_union->task.PID;
}
void sys_exit()
{
update_stats(1);
struct task_struct * current_process = current();
current_process->PID = 0;
current_process->quantum = 0;
page_table_entry * page_tage = get_PT(current_process);
/* CODE */
for (int pag = 0; pag < NUM_PAG_CODE; pag++)
{
page_tage[PAG_LOG_INIT_DATA+pag].bits.pbase_addr = 0;
page_tage[PAG_LOG_INIT_CODE+pag].entry = 0;
page_tage[PAG_LOG_INIT_CODE+pag].bits.user = 0;
page_tage[PAG_LOG_INIT_CODE+pag].bits.present = 0;
}
/* DATA */
for (int pag = 0; pag < NUM_PAG_DATA; pag++)
{
free_frame(page_tage[PAG_LOG_INIT_DATA+pag].bits.pbase_addr);
page_tage[PAG_LOG_INIT_DATA+pag].entry = 0;
page_tage[PAG_LOG_INIT_DATA+pag].bits.user = 0;
page_tage[PAG_LOG_INIT_DATA+pag].bits.rw = 0;
page_tage[PAG_LOG_INIT_DATA+pag].bits.present = 0;
}
extern struct list_head freequeue;
update_process_state_rr(current_process, &freequeue);
update_stats(0);
sched_next_rr();
}
unsigned int sys_gettime()
{
return zeos_ticks;
}
int sys_get_stats(int pid, struct stats* st)
{
update_stats(1);
if (!st) return -1; // Es pot canviar per un error que indiqui que el punter es invalid
extern union task_union task[NR_TASKS]; /* Vector de tasques */
int found = 0;
union task_union* target;
for (int i = 0; i < NR_TASKS; ++i)
{
target = &task[i];
if (target->task.PID == pid)
{
found = 1;
break;
}
}
if (!found) { printk("not found\n"); return -1; }
struct task_struct* current_ts = (struct task_struct*)target;
struct stats st_temp;
st_temp.user_ticks = current_ts->stadistics.user_ticks;
st_temp.system_ticks = current_ts->stadistics.system_ticks;
st_temp.blocked_ticks = current_ts->stadistics.blocked_ticks;
st_temp.ready_ticks = current_ts->stadistics.ready_ticks;
st_temp.elapsed_total_ticks = current_ts->stadistics.elapsed_total_ticks;
st_temp.total_trans = current_ts->stadistics.total_trans;
st_temp.remaining_ticks = current_ts->stadistics.remaining_ticks;
if (!copy_to_user(&st_temp, st, sizeof(struct stats)))
return -1;
update_stats(0);
return 0;
}