Rev 221 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
221 | giacomo | 1 | /* |
2 | * Project: S.Ha.R.K. |
||
3 | * |
||
4 | * Coordinators: |
||
5 | * Giorgio Buttazzo <giorgio@sssup.it> |
||
6 | * Paolo Gai <pj@gandalf.sssup.it> |
||
7 | * |
||
8 | * Authors : |
||
9 | * Paolo Gai <pj@gandalf.sssup.it> |
||
10 | * Massimiliano Giorgi <massy@gandalf.sssup.it> |
||
11 | * Luca Abeni <luca@gandalf.sssup.it> |
||
12 | * (see the web pages for full authors list) |
||
13 | * |
||
14 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
15 | * |
||
16 | * http://www.sssup.it |
||
17 | * http://retis.sssup.it |
||
18 | * http://shark.sssup.it |
||
19 | */ |
||
20 | |||
21 | /* |
||
22 | * Copyright (C) 2000 Paolo Gai |
||
23 | * |
||
24 | * This program is free software; you can redistribute it and/or modify |
||
25 | * it under the terms of the GNU General Public License as published by |
||
26 | * the Free Software Foundation; either version 2 of the License, or |
||
27 | * (at your option) any later version. |
||
28 | * |
||
29 | * This program is distributed in the hope that it will be useful, |
||
30 | * but WITHOUT ANY WARR2ANTY; without even the implied waRR2anty of |
||
31 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
32 | * GNU General Public License for more details. |
||
33 | * |
||
34 | * You should have received a copy of the GNU General Public License |
||
35 | * along with this program; if not, write to the Free Software |
||
36 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
37 | * |
||
38 | */ |
||
39 | |||
40 | |||
41 | #include <posix.h> |
||
42 | #include <ll/stdio.h> |
||
43 | #include <ll/string.h> |
||
44 | #include <kernel/model.h> |
||
45 | #include <kernel/descr.h> |
||
46 | #include <kernel/var.h> |
||
47 | #include <kernel/func.h> |
||
48 | #include <kernel/trace.h> |
||
49 | |||
50 | #include <comm_message.h> |
||
51 | |||
52 | //#define POSIX_DEBUG |
||
53 | |||
54 | /*+ Status used in the level +*/ |
||
55 | #define POSIX_READY MODULE_STATUS_BASE |
||
56 | |||
57 | #define POSIX_CHANGE_LEVEL 1 |
||
58 | |||
59 | /*+ the level redefinition for the Round Robin level +*/ |
||
60 | typedef struct { |
||
61 | level_des l; /*+ the standard level descriptor +*/ |
||
62 | |||
63 | int nact[MAX_PROC]; /*+ number of pending activations +*/ |
||
64 | |||
65 | int priority[MAX_PROC]; /*+ priority of each task +*/ |
||
66 | |||
67 | IQUEUE *ready; /*+ the ready queue array +*/ |
||
68 | |||
69 | int slice; /*+ the level's time slice +*/ |
||
70 | |||
71 | struct multiboot_info *multiboot; /*+ used if the level have to insert |
||
72 | the main task +*/ |
||
73 | int maxpriority; /*+ the priority are from 0 to maxpriority |
||
74 | (i.e 0 to 31) +*/ |
||
75 | |||
76 | int yielding; /*+ equal to 1 when a sched_yield is called +*/ |
||
77 | |||
78 | int flag[MAX_PROC]; |
||
79 | |||
80 | int new_level[MAX_PROC]; |
||
81 | int new_slice[MAX_PROC]; |
||
82 | int new_control[MAX_PROC]; |
||
83 | |||
84 | } POSIX_level_des; |
||
85 | |||
86 | /* This is not efficient but very fair :-) |
||
87 | The need of all this stuff is because if a task execute a long time |
||
88 | due to (shadow!) priority inheritance, then the task shall go to the |
||
89 | tail of the queue many times... */ |
||
90 | static PID POSIX_public_scheduler(LEVEL l) |
||
91 | { |
||
92 | POSIX_level_des *lev = (POSIX_level_des *)(level_table[l]); |
||
93 | |||
94 | PID p; |
||
95 | |||
96 | int prio; |
||
97 | |||
98 | prio = lev->maxpriority; |
||
99 | |||
100 | for (;;) { |
||
101 | p = iq_query_first(&lev->ready[prio]); |
||
102 | if (p == NIL) { |
||
103 | if (prio) { |
||
104 | prio--; |
||
105 | continue; |
||
106 | } |
||
107 | else |
||
108 | return NIL; |
||
109 | } |
||
110 | |||
111 | if ((proc_table[p].control & CONTROL_CAP) && |
||
112 | (proc_table[p].avail_time <= 0)) { |
||
113 | proc_table[p].avail_time += proc_table[p].wcet; |
||
114 | iq_extract(p,&lev->ready[prio]); |
||
115 | iq_insertlast(p,&lev->ready[prio]); |
||
116 | } |
||
117 | else |
||
118 | return p; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | static int POSIX_public_create(LEVEL l, PID p, TASK_MODEL *m) |
||
123 | { |
||
124 | POSIX_level_des *lev = (POSIX_level_des *)(level_table[l]); |
||
125 | NRT_TASK_MODEL *nrt; |
||
126 | |||
127 | if (m->pclass != NRT_PCLASS) return -1; |
||
128 | if (m->level != 0 && m->level != l) return -1; |
||
129 | |||
130 | nrt = (NRT_TASK_MODEL *)m; |
||
131 | |||
132 | /* the task state is set at SLEEP by the general task_create */ |
||
133 | |||
134 | /* I used the wcet field because using wcet can account if a task |
||
135 | consume more than the timeslice... */ |
||
136 | |||
137 | if (nrt->inherit == NRT_INHERIT_SCHED && |
||
138 | proc_table[exec_shadow].task_level == l) { |
||
139 | /* We inherit the scheduling properties if the scheduling level |
||
140 | *is* the same */ |
||
141 | lev->priority[p] = lev->priority[exec_shadow]; |
||
142 | |||
143 | proc_table[p].avail_time = proc_table[exec_shadow].avail_time; |
||
144 | proc_table[p].wcet = proc_table[exec_shadow].wcet; |
||
145 | |||
146 | proc_table[p].control = (proc_table[p].control & ~CONTROL_CAP) | |
||
147 | (proc_table[exec_shadow].control & CONTROL_CAP); |
||
148 | |||
149 | lev->nact[p] = (lev->nact[exec_shadow] == -1) ? -1 : 0; |
||
150 | } |
||
151 | else { |
||
152 | lev->priority[p] = nrt->weight; |
||
153 | |||
154 | if (nrt->slice) { |
||
155 | proc_table[p].avail_time = nrt->slice; |
||
156 | proc_table[p].wcet = nrt->slice; |
||
157 | } |
||
158 | else { |
||
159 | proc_table[p].avail_time = lev->slice; |
||
160 | proc_table[p].wcet = lev->slice; |
||
161 | } |
||
162 | |||
163 | if (nrt->policy == NRT_RR_POLICY) |
||
164 | proc_table[p].control |= CONTROL_CAP; |
||
165 | |||
166 | if (nrt->arrivals == SAVE_ARRIVALS) |
||
167 | lev->nact[p] = 0; |
||
168 | else |
||
169 | lev->nact[p] = -1; |
||
170 | } |
||
171 | |||
172 | lev->flag[p] = 0; |
||
173 | |||
174 | return 0; /* OK */ |
||
175 | } |
||
176 | |||
177 | static void POSIX_public_dispatch(LEVEL l, PID p, int nostop) |
||
178 | { |
||
179 | POSIX_level_des *lev = (POSIX_level_des *)(level_table[l]); |
||
180 | |||
181 | /* the task state is set EXE by the scheduler() |
||
182 | we extract the task from the ready queue |
||
183 | NB: we can't assume that p is the first task in the queue!!! */ |
||
184 | iq_extract(p, &lev->ready[lev->priority[p]]); |
||
185 | } |
||
186 | |||
187 | static void POSIX_public_epilogue(LEVEL l, PID p) |
||
188 | { |
||
189 | POSIX_level_des *lev = (POSIX_level_des *)(level_table[l]); |
||
190 | |||
191 | /* Change task level */ |
||
192 | if (lev->flag[p] & POSIX_CHANGE_LEVEL) { |
||
193 | |||
226 | giacomo | 194 | #ifdef POSIX_DEBUG |
195 | kern_printf("(P:NewLev %d)",lev->new_level[p]); |
||
196 | #endif |
||
197 | |||
221 | giacomo | 198 | STD_command_message msg; |
199 | |||
200 | proc_table[p].status = SLEEP; |
||
201 | proc_table[p].task_level = lev->new_level[p]; |
||
202 | msg.command = STD_ACTIVATE_TASK; |
||
203 | level_table[lev->new_level[p]] -> public_message(lev->new_level[p],p,&msg); |
||
204 | |||
205 | return; |
||
206 | |||
207 | } |
||
208 | |||
209 | if (lev->yielding) { |
||
210 | lev->yielding = 0; |
||
211 | iq_insertlast(p,&lev->ready[lev->priority[p]]); |
||
212 | } |
||
213 | /* check if the slice is finished and insert the task in the coPOSIXect |
||
214 | qqueue position */ |
||
215 | else if (proc_table[p].control & CONTROL_CAP && |
||
216 | proc_table[p].avail_time <= 0) { |
||
217 | proc_table[p].avail_time += proc_table[p].wcet; |
||
218 | iq_insertlast(p,&lev->ready[lev->priority[p]]); |
||
219 | } |
||
220 | else |
||
221 | iq_insertfirst(p,&lev->ready[lev->priority[p]]); |
||
222 | |||
223 | proc_table[p].status = POSIX_READY; |
||
224 | } |
||
225 | |||
226 | static void POSIX_public_activate(LEVEL l, PID p) |
||
227 | { |
||
228 | POSIX_level_des *lev = (POSIX_level_des *)(level_table[l]); |
||
229 | |||
230 | /* Test if we are trying to activate a non sleeping task */ |
||
231 | /* save activation (only if needed...) */ |
||
232 | if (proc_table[p].status != SLEEP) { |
||
233 | if (lev->nact[p] != -1) |
||
234 | lev->nact[p]++; |
||
235 | return; |
||
236 | } |
||
237 | |||
238 | /* Insert task in the correct position */ |
||
239 | proc_table[p].status = POSIX_READY; |
||
240 | iq_insertlast(p,&lev->ready[lev->priority[p]]); |
||
241 | } |
||
242 | |||
243 | static void POSIX_public_unblock(LEVEL l, PID p) |
||
244 | { |
||
245 | POSIX_level_des *lev = (POSIX_level_des *)(level_table[l]); |
||
246 | |||
247 | /* Similar to POSIX_task_activate, but we don't check in what state |
||
248 | the task is */ |
||
249 | |||
250 | /* Insert task in the coPOSIXect position */ |
||
251 | proc_table[p].status = POSIX_READY; |
||
252 | iq_insertlast(p,&lev->ready[lev->priority[p]]); |
||
253 | } |
||
254 | |||
255 | static void POSIX_public_block(LEVEL l, PID p) |
||
256 | { |
||
257 | /* Extract the running task from the level |
||
258 | . we have already extract it from the ready queue at the dispatch time. |
||
259 | . the capacity event have to be removed by the generic kernel |
||
260 | . the wcet don't need modification... |
||
261 | . the state of the task is set by the calling function |
||
262 | |||
263 | So, we do nothing!!! |
||
264 | */ |
||
265 | } |
||
266 | |||
267 | static int POSIX_public_message(LEVEL l, PID p, void *m) |
||
268 | { |
||
269 | POSIX_level_des *lev = (POSIX_level_des *)(level_table[l]); |
||
270 | STD_command_message *msg; |
||
271 | |||
272 | NRT_TASK_MODEL *nrt; |
||
273 | |||
274 | /* Task Endcycle */ |
||
275 | switch ((long)(m)) { |
||
276 | |||
277 | case (long)(NULL): |
||
278 | if (lev->nact[p] > 0) { |
||
279 | /* continue!!!! */ |
||
280 | lev->nact[p]--; |
||
281 | iq_insertfirst(p,&lev->ready[lev->priority[p]]); |
||
282 | proc_table[p].status = POSIX_READY; |
||
283 | } else |
||
284 | proc_table[p].status = SLEEP; |
||
285 | |||
286 | jet_update_endcycle(); /* Update the Jet data... */ |
||
287 | trc_logevent(TRC_ENDCYCLE,&exec_shadow); /* tracer stuff */ |
||
288 | |||
289 | break; |
||
290 | |||
291 | /* Task Disable */ |
||
292 | case (long)(1): |
||
293 | |||
294 | break; |
||
295 | |||
296 | default: |
||
297 | |||
298 | msg = (STD_command_message *)m; |
||
299 | |||
226 | giacomo | 300 | #ifdef POSIX_DEBUG |
301 | kern_printf("(P:MSG %d)",msg->command); |
||
302 | #endif |
||
303 | |||
221 | giacomo | 304 | switch(msg->command) { |
305 | case STD_SET_NEW_LEVEL: |
||
306 | |||
307 | lev->flag[p] |= POSIX_CHANGE_LEVEL; |
||
308 | lev->new_level[p] = (int)(msg->param); |
||
309 | |||
310 | break; |
||
311 | case STD_SET_NEW_MODEL: |
||
312 | |||
313 | nrt = (NRT_TASK_MODEL *)(msg->param); |
||
314 | |||
315 | lev->priority[p] = nrt->weight; |
||
316 | |||
317 | if (nrt->slice) { |
||
318 | lev->new_slice[p] = nrt->slice; |
||
319 | } else { |
||
320 | lev->new_slice[p] = 0; |
||
321 | } |
||
226 | giacomo | 322 | |
323 | lev->new_control[p] = 0; |
||
324 | |||
221 | giacomo | 325 | if (nrt->policy == NRT_RR_POLICY) |
326 | lev->new_control[p] |= CONTROL_CAP; |
||
327 | |||
328 | if (nrt->arrivals == SAVE_ARRIVALS) |
||
329 | lev->nact[p] = 0; |
||
330 | else |
||
331 | lev->nact[p] = -1; |
||
332 | |||
333 | lev->flag[p] = 0; |
||
334 | |||
335 | break; |
||
336 | |||
337 | case STD_ACTIVATE_TASK: |
||
338 | |||
339 | if (lev->new_slice[p]) { |
||
340 | proc_table[p].avail_time = lev->new_slice[p]; |
||
341 | proc_table[p].wcet = lev->new_slice[p]; |
||
342 | } else { |
||
343 | proc_table[p].avail_time = lev->slice; |
||
344 | proc_table[p].wcet = lev->slice; |
||
345 | } |
||
346 | |||
226 | giacomo | 347 | proc_table[p].control |= lev->new_control[p]; |
221 | giacomo | 348 | |
349 | POSIX_public_activate(l,p); |
||
350 | |||
351 | break; |
||
352 | |||
353 | } |
||
354 | |||
355 | break; |
||
356 | |||
357 | } |
||
358 | |||
359 | return 0; |
||
360 | } |
||
361 | |||
362 | static void POSIX_public_end(LEVEL l, PID p) |
||
363 | { |
||
364 | POSIX_level_des *lev = (POSIX_level_des *)(level_table[l]); |
||
365 | |||
366 | lev->nact[p] = -1; |
||
367 | |||
368 | /* then, we insert the task in the free queue */ |
||
369 | proc_table[p].status = FREE; |
||
370 | iq_priority_insert(p,&freedesc); |
||
371 | } |
||
372 | |||
373 | /* Registration functions */ |
||
374 | |||
375 | /*+ This init function install the "main" task +*/ |
||
376 | static void POSIX_call_main(void *l) |
||
377 | { |
||
378 | LEVEL lev; |
||
379 | PID p; |
||
380 | NRT_TASK_MODEL m; |
||
381 | void *mb; |
||
382 | |||
383 | lev = (LEVEL)l; |
||
384 | |||
385 | nrt_task_default_model(m); |
||
386 | nrt_task_def_level(m,lev); /* with this we are sure that the task aPOSIXives |
||
387 | to the coPOSIXect level */ |
||
388 | |||
389 | mb = ((POSIX_level_des *)level_table[lev])->multiboot; |
||
390 | nrt_task_def_arg(m,mb); |
||
391 | nrt_task_def_usemath(m); |
||
392 | nrt_task_def_nokill(m); |
||
393 | nrt_task_def_ctrl_jet(m); |
||
394 | nrt_task_def_weight(m,0); |
||
395 | nrt_task_def_policy(m,NRT_RR_POLICY); |
||
396 | nrt_task_def_inherit(m,NRT_EXPLICIT_SCHED); |
||
397 | |||
398 | p = task_create("Main", __init__, (TASK_MODEL *)&m, NULL); |
||
399 | |||
400 | if (p == NIL) |
||
401 | kern_printf("\nPanic!!! can't create main task...\n"); |
||
402 | |||
403 | POSIX_public_activate(lev,p); |
||
404 | } |
||
405 | |||
406 | |||
407 | /*+ Registration function: |
||
408 | TIME slice the slice for the Round Robin queue |
||
409 | int createmain 1 if the level creates the main task 0 otherwise |
||
410 | struct multiboot_info *mb used if createmain specified +*/ |
||
411 | LEVEL POSIX_register_level(TIME slice, |
||
412 | int createmain, |
||
413 | struct multiboot_info *mb, |
||
414 | int prioritylevels) |
||
415 | { |
||
416 | LEVEL l; /* the level that we register */ |
||
417 | POSIX_level_des *lev; /* for readableness only */ |
||
418 | PID i; /* a counter */ |
||
419 | int x; /* a counter */ |
||
420 | |||
421 | kern_printf("POSIX_register_level\n"); |
||
422 | |||
423 | l = level_alloc_descriptor(sizeof(POSIX_level_des)); |
||
424 | |||
425 | lev = (POSIX_level_des *)level_table[l]; |
||
426 | |||
427 | /* fill the standard descriptor */ |
||
428 | lev->l.public_scheduler = POSIX_public_scheduler; |
||
429 | lev->l.public_create = POSIX_public_create; |
||
430 | lev->l.public_end = POSIX_public_end; |
||
431 | lev->l.public_dispatch = POSIX_public_dispatch; |
||
432 | lev->l.public_epilogue = POSIX_public_epilogue; |
||
433 | lev->l.public_activate = POSIX_public_activate; |
||
434 | lev->l.public_unblock = POSIX_public_unblock; |
||
435 | lev->l.public_block = POSIX_public_block; |
||
436 | lev->l.public_message = POSIX_public_message; |
||
437 | |||
438 | /* fill the POSIX descriptor part */ |
||
439 | for (i = 0; i < MAX_PROC; i++) { |
||
440 | lev->nact[i] = -1; |
||
441 | lev->new_level[i] = 0; |
||
442 | lev->new_slice[i] = 0; |
||
443 | lev->new_control[i] = 0; |
||
444 | } |
||
445 | |||
446 | lev->maxpriority = prioritylevels -1; |
||
447 | |||
448 | lev->ready = (IQUEUE *)kern_alloc(sizeof(IQUEUE) * prioritylevels); |
||
449 | |||
450 | for (x = 0; x < prioritylevels; x++) |
||
451 | iq_init(&lev->ready[x], &freedesc, 0); |
||
452 | |||
453 | if (slice < POSIX_MINIMUM_SLICE) slice = POSIX_MINIMUM_SLICE; |
||
454 | if (slice > POSIX_MAXIMUM_SLICE) slice = POSIX_MAXIMUM_SLICE; |
||
455 | lev->slice = slice; |
||
456 | |||
457 | lev->multiboot = mb; |
||
458 | |||
459 | if (createmain) |
||
460 | sys_atrunlevel(POSIX_call_main,(void *) l, RUNLEVEL_INIT); |
||
461 | |||
462 | return l; |
||
463 | } |
||
464 | |||
465 | /*+ this function forces the running task to go to his queue tail; |
||
466 | (it works only on the POSIX level) +*/ |
||
467 | int POSIX_sched_yield(LEVEL l) |
||
468 | { |
||
469 | POSIX_level_des *lev = (POSIX_level_des *)(level_table[l]); |
||
470 | |||
471 | if (proc_table[exec_shadow].task_level != l) |
||
472 | return -1; |
||
473 | |||
474 | proc_table[exec_shadow].context = kern_context_save(); |
||
475 | lev->yielding = 1; |
||
476 | scheduler(); |
||
477 | kern_context_load(proc_table[exec_shadow].context); |
||
478 | return 0; |
||
479 | } |
||
480 | |||
481 | /*+ this function returns the maximum level allowed for the POSIX level +*/ |
||
482 | int POSIX_get_priority_max(LEVEL l) |
||
483 | { |
||
484 | POSIX_level_des *lev = (POSIX_level_des *)(level_table[l]); |
||
485 | return lev->maxpriority; |
||
486 | } |
||
487 | |||
488 | /*+ this function returns the default timeslice for the POSIX level +*/ |
||
489 | int POSIX_rr_get_interval(LEVEL l) |
||
490 | { |
||
491 | POSIX_level_des *lev = (POSIX_level_des *)(level_table[l]); |
||
492 | return lev->slice; |
||
493 | } |
||
494 | |||
495 | /*+ this functions returns some paramaters of a task; |
||
496 | policy must be NRT_RR_POLICY or NRT_FIFO_POLICY; |
||
497 | priority must be in the range [0..prioritylevels] |
||
498 | returns ENOSYS or ESRCH if there are problems +*/ |
||
499 | int POSIX_getschedparam(LEVEL l, PID p, int *policy, int *priority) |
||
500 | { |
||
501 | if (p<0 || p>= MAX_PROC || proc_table[p].status == FREE) |
||
502 | return ESRCH; |
||
503 | |||
504 | if (proc_table[p].task_level != l) |
||
505 | return ENOSYS; |
||
506 | |||
507 | if (proc_table[p].control & CONTROL_CAP) |
||
508 | *policy = NRT_RR_POLICY; |
||
509 | else |
||
510 | *policy = NRT_FIFO_POLICY; |
||
511 | |||
512 | *priority = ((POSIX_level_des *)(level_table[l]))->priority[p]; |
||
513 | |||
514 | return 0; |
||
515 | } |
||
516 | |||
517 | /*+ this functions sets paramaters of a task +*/ |
||
518 | int POSIX_setschedparam(LEVEL l, PID p, int policy, int priority) |
||
519 | { |
||
520 | POSIX_level_des *lev = (POSIX_level_des *)(level_table[l]); |
||
521 | |||
522 | if (p<0 || p>= MAX_PROC || proc_table[p].status == FREE) |
||
523 | return ESRCH; |
||
524 | |||
525 | if (proc_table[p].task_level != l) |
||
526 | return ENOSYS; |
||
527 | |||
528 | if (policy == SCHED_RR) |
||
529 | proc_table[p].control |= CONTROL_CAP; |
||
530 | else if (policy == SCHED_FIFO) |
||
531 | proc_table[p].control &= ~CONTROL_CAP; |
||
532 | else |
||
533 | return EINVAL; |
||
534 | |||
535 | if (lev->priority[p] != priority) { |
||
536 | if (proc_table[p].status == POSIX_READY) { |
||
537 | iq_extract(p,&lev->ready[lev->priority[p]]); |
||
538 | lev->priority[p] = priority; |
||
539 | iq_insertlast(p,&lev->ready[priority]); |
||
540 | } |
||
541 | else |
||
542 | lev->priority[p] = priority; |
||
543 | } |
||
544 | |||
545 | return 0; |
||
546 | } |
||
547 | |||
548 | |||
549 |