Rev 157 | Rev 385 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 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 | * (see the web pages for full authors list) |
||
11 | * |
||
12 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
13 | * |
||
14 | * http://www.sssup.it |
||
15 | * http://retis.sssup.it |
||
16 | * http://shark.sssup.it |
||
17 | */ |
||
18 | |||
19 | /** |
||
20 | ------------ |
||
353 | giacomo | 21 | CVS : $Id: kern.c,v 1.6 2003-12-10 16:54:59 giacomo Exp $ |
2 | pj | 22 | |
23 | File: $File$ |
||
353 | giacomo | 24 | Revision: $Revision: 1.6 $ |
25 | Last update: $Date: 2003-12-10 16:54:59 $ |
||
2 | pj | 26 | ------------ |
27 | |||
28 | This file contains: |
||
29 | |||
30 | - the kernel system variables |
||
31 | |||
32 | - the errno functions |
||
33 | |||
34 | - the scheduler, capacity timer, and grarantee |
||
35 | |||
36 | - the sys_abort, sys_end, sys_gettime |
||
37 | |||
38 | |||
39 | **/ |
||
40 | |||
41 | /* |
||
42 | * Copyright (C) 2000 Paolo Gai |
||
43 | * |
||
44 | * This program is free software; you can redistribute it and/or modify |
||
45 | * it under the terms of the GNU General Public License as published by |
||
46 | * the Free Software Foundation; either version 2 of the License, or |
||
47 | * (at your option) any later version. |
||
48 | * |
||
49 | * This program is distributed in the hope that it will be useful, |
||
50 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
51 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
52 | * GNU General Public License for more details. |
||
53 | * |
||
54 | * You should have received a copy of the GNU General Public License |
||
55 | * along with this program; if not, write to the Free Software |
||
56 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
57 | * |
||
58 | */ |
||
59 | |||
60 | #include <stdarg.h> |
||
61 | #include <ll/ll.h> |
||
62 | #include <ll/stdlib.h> |
||
63 | #include <ll/stdio.h> |
||
64 | #include <ll/string.h> |
||
65 | #include <kernel/config.h> |
||
66 | #include <kernel/model.h> |
||
67 | #include <kernel/const.h> |
||
68 | #include <sys/types.h> |
||
69 | #include <kernel/types.h> |
||
70 | #include <kernel/descr.h> |
||
71 | #include <errno.h> |
||
72 | #include <kernel/var.h> |
||
73 | #include <kernel/func.h> |
||
74 | |||
353 | giacomo | 75 | #include <tracer.h> |
76 | |||
2 | pj | 77 | /*----------------------------------------------------------------------*/ |
78 | /* Kernel System variables */ |
||
79 | /*----------------------------------------------------------------------*/ |
||
80 | |||
81 | int global_errnumber; /*+ Errno used in system initialization +*/ |
||
82 | CONTEXT global_context; /*+ Context used during initialization; |
||
83 | It references also a safe stack +*/ |
||
84 | |||
85 | int task_counter; /*+ Application task counter. It represent |
||
86 | the number of Application tasks in the |
||
87 | system. When all Application Tasks end, |
||
88 | also the system ends. +*/ |
||
89 | |||
90 | int system_counter; /*+ System task counter. It represent |
||
91 | the number of System tasks in the |
||
92 | system with the NO_KILL flag reset. |
||
93 | When all Application Tasks end, |
||
94 | the system waits for the end of the |
||
95 | system tasks and then it ends. +*/ |
||
96 | |||
97 | PID exec; /*+ Task advised by the scheduler +*/ |
||
98 | PID exec_shadow; /*+ Currently executing task +*/ |
||
99 | |||
29 | pj | 100 | IQUEUE freedesc; /*+ Free descriptor handled as a queue +*/ |
2 | pj | 101 | |
102 | DWORD sys_tick; /*+ System tick (in usec) +*/ |
||
103 | struct timespec schedule_time; |
||
104 | /*+ Timer read at each call to schedule()+*/ |
||
105 | |||
106 | int cap_timer; /*+ the capacity event posted when the |
||
107 | task starts +*/ |
||
108 | struct timespec cap_lasttime; |
||
109 | /*+ the time at whitch the capacity |
||
110 | event is posted. Normally, it is |
||
111 | equal to schedule_time +*/ |
||
112 | |||
113 | |||
114 | |||
115 | DWORD sched_levels; /*+ Schedule levels active in the system +*/ |
||
116 | DWORD res_levels; /*+ Resource levels active in the system +*/ |
||
117 | |||
118 | /*+ Process descriptor table +*/ |
||
119 | proc_des proc_table[MAX_PROC]; |
||
120 | |||
38 | pj | 121 | /* Scheduling modules descriptor table */ |
122 | /* ------------------------------------------------------------------------ */ |
||
123 | |||
124 | /* the descriptor table */ |
||
2 | pj | 125 | level_des *level_table[MAX_SCHED_LEVEL]; |
38 | pj | 126 | /* ... and the size of each descriptor */ |
127 | size_t level_size[MAX_SCHED_LEVEL]; |
||
2 | pj | 128 | |
38 | pj | 129 | /* an utilization counter incremented if a level is used by another module */ |
130 | int level_used[MAX_SCHED_LEVEL]; |
||
131 | /* these data structures (first, last, free, next & prev) |
||
132 | are used to implement a double linked list of scheduling modules. |
||
133 | That list is used by the scheduler to call the module's schedulers. */ |
||
134 | int level_first; /* first module in the list */ |
||
135 | int level_last; /* last module in the list */ |
||
136 | int level_free; /* free single linked list of free module descriptors. */ |
||
137 | int level_next[MAX_SCHED_LEVEL]; |
||
138 | int level_prev[MAX_SCHED_LEVEL]; |
||
139 | /* ------------------------------------------------------------------------ */ |
||
140 | |||
2 | pj | 141 | /*+ Resource descriptor table +*/ |
142 | resource_des *resource_table[MAX_RES_LEVEL]; |
||
143 | |||
144 | /*+ This variable is set by the system call sys_end() or sys_abort(). |
||
145 | When a sys_end() or sys_abort is called into an event handler, |
||
146 | we don't have to change context in the reschedule(). |
||
147 | look at kernel/event.c +*/ |
||
148 | int mustexit = 0; |
||
149 | |||
150 | /*+ this is the system runlevel... it may be from 0 to 4: |
||
151 | |||
152 | 1 - running |
||
153 | 2 - shutdown |
||
154 | 3 - before halting |
||
155 | 4 - halting |
||
156 | +*/ |
||
157 | int runlevel; |
||
158 | |||
159 | /*+ this variable is set to 1 into call_runlevel_func (look at init.c) |
||
160 | ad it is used because the task_activate (look at activate.c) must |
||
161 | work in a different way when the system is in the global_context +*/ |
||
162 | int calling_runlevel_func; |
||
163 | |||
164 | |||
165 | /*----------------------------------------------------------------------*/ |
||
166 | /* Kernel internal functions */ |
||
167 | /*----------------------------------------------------------------------*/ |
||
168 | |||
169 | /*+ errno Handling: this functions returns the correct address for errno. |
||
170 | The address returned can be either the global errno or the errno local |
||
171 | to the execution task */ |
||
172 | static int *__errnumber() |
||
173 | { |
||
174 | if (exec_shadow == -1) |
||
175 | return &global_errnumber; |
||
176 | else |
||
177 | return &(proc_table[exec_shadow].errnumber); |
||
178 | } |
||
179 | |||
180 | /*+ this is the capacity timer. it fires when the running task has expired |
||
181 | his time contained in the avail_time field. The event is tipically |
||
182 | posted in the scheduler() after the task_dispatch. The task_dispatch |
||
183 | can modify the avail_time field to reach his scheduling purposes. |
||
184 | The wcet field is NOT used in the Generic kernel. it is initialized at |
||
185 | init time to 0. +*/ |
||
186 | void capacity_timer(void *arg) |
||
187 | { |
||
188 | /* the capacity event is served, so at the epilogue we |
||
189 | don't have to erase it */ |
||
190 | cap_timer = NIL; |
||
191 | |||
192 | // kern_printf("cap%d ",exec_shadow); |
||
193 | |||
194 | /* When we reschedule, the call to task_epilogue check the slice and |
||
195 | put the task in the queue's tail */ |
||
196 | event_need_reschedule(); |
||
197 | } |
||
198 | |||
199 | /*+ |
||
200 | Generic Scheduler: |
||
201 | This function select the next task that should be executed. |
||
202 | The selection is made calling the level schedulers. |
||
203 | It assume that THERE IS a task that can be scheduled in one |
||
204 | level. |
||
205 | |||
206 | The general scheduler: |
||
207 | - first, it checks for interrupts. |
||
208 | - then, it calls the epilogue of the task pointed in exec_shadow |
||
209 | - after that, it calls the level schedulers |
||
210 | - then it sets exec and it follows the shadow chain |
||
211 | - finally it calls task_dispatch for the new task (the shadow!!!), |
||
212 | saying if exec != exec_shadow |
||
213 | |||
214 | +*/ |
||
215 | void scheduler(void) |
||
216 | { |
||
217 | LEVEL l; /* a counter */ |
||
218 | struct timespec ty; /* a dummy used for time computation */ |
||
219 | |||
220 | PID p; /* p is the task chosen by the level scheduler */ |
||
221 | int ok; /* 1 only if the task chosen by the level scheduler |
||
222 | is eligible (normally, it is; but in some server |
||
223 | it is not always true (i.e., CBS)) */ |
||
224 | |||
225 | PID old_exec_shadow; |
||
226 | |||
227 | if ( (exec_shadow != -1 && |
||
228 | (proc_table[exec_shadow].control & NO_PREEMPT) ) ) |
||
229 | return; |
||
230 | |||
38 | pj | 231 | // kern_printf("(!"); |
232 | |||
2 | pj | 233 | /* |
234 | exec_shadow = exec = -1 only if the scheduler is called from: |
||
235 | . task_endcycle |
||
236 | . task_kill |
||
237 | . task_extract |
||
238 | . task_sleep |
||
239 | . task_delay |
||
240 | and from the system startup routines. |
||
241 | |||
242 | Normally, the scheduler is called with exec & co != -1... |
||
243 | |||
244 | if exec & co. is set to -1 before calling scheduler(), the following |
||
245 | stuffs have to be executed before the call |
||
246 | - get the schedule_time |
||
247 | - account the capacity if necessary |
||
248 | - call an epilogue |
||
249 | */ |
||
250 | |||
38 | pj | 251 | /* then, we call the epilogue. the epilogue tipically checks the |
252 | avail_time field... */ |
||
2 | pj | 253 | if (exec_shadow != -1) { |
38 | pj | 254 | kern_epilogue_macro(); |
2 | pj | 255 | |
256 | l = proc_table[exec_shadow].task_level; |
||
38 | pj | 257 | level_table[l]->public_epilogue(l,exec_shadow); |
2 | pj | 258 | } |
259 | |||
38 | pj | 260 | // kern_printf("["); |
261 | |||
262 | l = level_first; |
||
2 | pj | 263 | for(;;) { |
264 | do { |
||
38 | pj | 265 | p = level_table[l]->public_scheduler(l); |
266 | // kern_printf("p=%d",p); |
||
2 | pj | 267 | if (p != NIL) |
268 | ok = level_table[ proc_table[p].task_level ]-> |
||
38 | pj | 269 | public_eligible(proc_table[p].task_level,p); |
2 | pj | 270 | else |
271 | ok = 0; |
||
38 | pj | 272 | // kern_printf(" ok=%d",ok); |
2 | pj | 273 | } while (ok < 0); /* repeat the level scheduler if the task isn't |
274 | eligible... (ex. in the aperiodic servers...) */ |
||
275 | if (p != NIL) break; |
||
276 | |||
38 | pj | 277 | l = level_next[l]; /* THERE MUST BE a level with a task to schedule */ |
278 | // kern_printf(" l=%d",l); |
||
2 | pj | 279 | }; |
280 | |||
38 | pj | 281 | // kern_printf("]"); |
282 | |||
2 | pj | 283 | /* we follow the shadow chain */ |
284 | old_exec_shadow=exec_shadow; |
||
285 | exec_shadow = exec = p; |
||
286 | while (exec_shadow != proc_table[exec_shadow].shadow) |
||
287 | exec_shadow = proc_table[exec_shadow].shadow; |
||
288 | |||
289 | /* tracer stuff */ |
||
290 | if (old_exec_shadow!=exec_shadow) |
||
353 | giacomo | 291 | TRACER_LOGEVENT(FTrace_EVT_task_schedule,1,exec_shadow,0); |
38 | pj | 292 | // kern_printf("[%i->%i]",old_exec_shadow,exec_shadow); |
2 | pj | 293 | |
294 | /* we control the correctness of the shadows when we kill */ |
||
295 | proc_table[exec_shadow].status = EXE; |
||
296 | |||
38 | pj | 297 | // kern_printf("(d%d)",exec_shadow); |
2 | pj | 298 | l = proc_table[exec_shadow].task_level; |
38 | pj | 299 | level_table[l]->public_dispatch(l, exec_shadow, exec!=exec_shadow); |
2 | pj | 300 | |
38 | pj | 301 | // kern_printf("*"); |
302 | |||
2 | pj | 303 | /* Finally,we post the capacity event, BUT |
304 | . only if the task require that |
||
305 | . only if exec==exec_shadow (if a task is blocked we don't want |
||
306 | to check the capacity!!!) */ |
||
307 | if ((proc_table[exec_shadow].control & CONTROL_CAP) |
||
308 | && exec==exec_shadow) { |
||
309 | TIMESPEC_ASSIGN(&ty, &schedule_time); |
||
310 | ADDUSEC2TIMESPEC(proc_table[exec_shadow].avail_time,&ty); |
||
38 | pj | 311 | // kern_printf("³s%d ns%d sched s%d ns%d³",ty.tv_sec,ty.tv_nsec, schedule_time.tv_sec, schedule_time.tv_nsec); |
2 | pj | 312 | cap_timer = kern_event_post(&ty, capacity_timer, NULL); |
313 | } |
||
314 | /* set the time at witch the task is scheduled */ |
||
315 | TIMESPEC_ASSIGN(&cap_lasttime, &schedule_time); |
||
316 | |||
38 | pj | 317 | // kern_printf("(s%d)",exec_shadow); |
2 | pj | 318 | } |
319 | |||
320 | |||
321 | /*+ |
||
322 | Guarantee: |
||
323 | This function guarantees the system: it calls the |
||
324 | level_guarantee of each level that have that function != NULL |
||
325 | |||
326 | The guarantee is based on a utilization factor basis. |
||
327 | We mantain only a DWORD. num has to be interpreted as num/MAX_DWORD |
||
328 | free bandwidth. |
||
329 | +*/ |
||
330 | int guarantee() |
||
331 | { |
||
332 | bandwidth_t num=MAX_BANDWIDTH; |
||
333 | int l; |
||
334 | |||
38 | pj | 335 | for (l =0; l<MAX_SCHED_LEVEL && level_table[l]->public_guarantee; l++) |
336 | if (!level_table[l]->public_guarantee(l,&num)) |
||
2 | pj | 337 | return -1; |
338 | |||
339 | return 0; /* OK */ |
||
340 | } |
||
341 | |||
342 | /*----------------------------------------------------------------------*/ |
||
343 | /* Context switch handling functions */ |
||
344 | /*----------------------------------------------------------------------*/ |
||
345 | /* this function is called every time a context change occurs, |
||
346 | when a task is preempted by an event called into an IRQ */ |
||
347 | void kern_after_dispatch() |
||
348 | { |
||
349 | /* every time a task wakes up from an IRQ, it has to check for async |
||
350 | cancellation */ |
||
351 | check_killed_async(); |
||
352 | |||
353 | /* Then, look for pending signal delivery */ |
||
354 | kern_deliver_pending_signals(); |
||
355 | } |
||
356 | |||
357 | /*----------------------------------------------------------------------*/ |
||
157 | pj | 358 | /* Abort strings */ |
359 | /*----------------------------------------------------------------------*/ |
||
360 | |||
361 | const char *const _sys_abrtlist[] = { |
||
362 | "zero - no error", |
||
363 | "OSLib exception", |
||
364 | "generic unhandled signal raised", |
||
365 | "error in signal_init", |
||
366 | "default exception handler code", |
||
367 | "ARP table full" |
||
368 | }; |
||
369 | |||
370 | /*----------------------------------------------------------------------*/ |
||
2 | pj | 371 | /* Kernel main system functions */ |
372 | /*----------------------------------------------------------------------*/ |
||
373 | |||
374 | /*+ |
||
375 | This function initialize |
||
376 | - the virtual machine (timer, interrupt, mem) |
||
377 | the system's structures (queues, tables) , & the two task main & |
||
378 | dummy, that are always present |
||
379 | +*/ |
||
45 | pj | 380 | void __kernel_init__(/* struct multiboot_info *multiboot */ void) |
2 | pj | 381 | { |
382 | int i,j; /* counters */ |
||
383 | |||
384 | struct ll_initparms parms; /* for the VM */ |
||
385 | |||
386 | // extern void C8042_restore(void); /* an exit function */ |
||
387 | int aborting; /* it is set if we are aborting the system */ |
||
388 | |||
45 | pj | 389 | struct multiboot_info *multiboot=mbi_address(); |
2 | pj | 390 | |
391 | |||
392 | |||
393 | /* |
||
394 | * Runlevel 0: kernel startup |
||
395 | * |
||
396 | * |
||
397 | */ |
||
398 | |||
38 | pj | 399 | runlevel = RUNLEVEL_STARTUP; |
2 | pj | 400 | |
401 | /* The kernel startup MUST proceed with int disabled! */ |
||
402 | kern_cli(); |
||
403 | |||
404 | /* First we initialize the memory allocator, because it is needed by |
||
405 | __kernel_register_levels__ */ |
||
406 | kern_mem_init(multiboot); |
||
407 | |||
408 | /* Clear the task descriptors */ |
||
409 | for (i = 0; i < MAX_PROC; i++) { |
||
410 | proc_table[i].task_level = -1; |
||
411 | proc_table[i].stack = NULL; |
||
412 | proc_table[i].name[0] = 0; |
||
413 | proc_table[i].status = FREE; |
||
414 | proc_table[i].pclass = 0; |
||
415 | proc_table[i].group = 0; |
||
416 | proc_table[i].stacksize = 0; |
||
417 | proc_table[i].control = 0; |
||
418 | proc_table[i].frozen_activations = 0; |
||
419 | proc_table[i].sigmask = 0; |
||
420 | proc_table[i].sigpending = 0; |
||
421 | proc_table[i].avail_time = 0; |
||
422 | proc_table[i].shadow = i; |
||
423 | proc_table[i].cleanup_stack= NULL; |
||
424 | proc_table[i].errnumber = 0; |
||
29 | pj | 425 | //proc_table[i].priority = 0; |
426 | //NULL_TIMESPEC(&proc_table[i].timespec_priority); |
||
2 | pj | 427 | proc_table[i].delay_timer = -1; |
428 | proc_table[i].wcet = -1; |
||
429 | |||
430 | proc_table[i].jet_tvalid = 0; |
||
431 | proc_table[i].jet_curr = 0; |
||
432 | proc_table[i].jet_max = 0; |
||
433 | proc_table[i].jet_sum = 0; |
||
434 | proc_table[i].jet_n = 0; |
||
435 | for (j=0; j<JET_TABLE_DIM; j++) |
||
436 | proc_table[i].jet_table[j] = 0; |
||
437 | |||
438 | proc_table[i].waiting_for_me = NIL; |
||
439 | proc_table[i].return_value = NULL; |
||
440 | |||
441 | for (j=0; j<PTHREAD_KEYS_MAX; j++) |
||
442 | proc_table[i].keys[j] = NULL; |
||
443 | } |
||
444 | |||
29 | pj | 445 | /* set up the free descriptor queue */ |
446 | // for (i = 0; i < MAX_PROC-1; i++) proc_table[i].next = i+1; |
||
447 | // proc_table[MAX_PROC-1].next = NIL; |
||
448 | // for (i = MAX_PROC-1; i > 0; i--) proc_table[i].prev = i-1; |
||
449 | // proc_table[0].prev = NIL; |
||
450 | // freedesc = 0; |
||
451 | iq_init(&freedesc, NULL, 0); |
||
452 | for (i = 0; i < MAX_PROC; i++) |
||
453 | iq_insertlast(i,&freedesc); |
||
454 | |||
2 | pj | 455 | /* Set up the varius stuff */ |
456 | global_errnumber = 0; |
||
457 | task_counter = 0; |
||
458 | system_counter = 0; |
||
459 | exec = -1; |
||
460 | exec_shadow = -1; |
||
461 | cap_timer = -1; |
||
462 | NULL_TIMESPEC(&cap_lasttime); |
||
463 | sched_levels = 0; /* They are not registered yet... */ |
||
464 | res_levels = 0; |
||
465 | calling_runlevel_func = 0; |
||
466 | |||
467 | /* Clear the key-specific data */ |
||
468 | task_specific_data_init(); |
||
469 | |||
470 | /* Clear exit and init functions */ |
||
471 | runlevel_init(); |
||
472 | |||
473 | /* Init VM layer (Interrupts, levels & memory management) */ |
||
474 | /* for old exception handling, use excirq_init() */ |
||
475 | signals_init(); |
||
38 | pj | 476 | set_default_exception_handler(); |
2 | pj | 477 | |
38 | pj | 478 | /* Clear scheduling modules registration data */ |
479 | levels_init(); |
||
480 | |||
2 | pj | 481 | sys_tick = __kernel_register_levels__(multiboot); |
482 | |||
483 | /* test on system tick */ |
||
484 | if (sys_tick>=55000) { |
||
485 | printk("The system tick must be less than 55 mSec!"); |
||
486 | l1_exit(0); |
||
487 | } |
||
488 | |||
489 | /* OSLib initialization */ |
||
490 | if (sys_tick) |
||
491 | parms.mode = LL_PERIODIC; |
||
492 | else |
||
493 | parms.mode = LL_ONESHOT; // one shot!!! |
||
494 | |||
495 | parms.tick = sys_tick; |
||
496 | |||
497 | /* |
||
38 | pj | 498 | * Runlevel INIT: Let's go!!!! |
2 | pj | 499 | * |
500 | * |
||
501 | */ |
||
502 | |||
503 | runlevel = RUNLEVEL_INIT; |
||
504 | |||
505 | ll_init(); |
||
506 | event_init(&parms); |
||
507 | seterrnumber(__errnumber); |
||
508 | event_setprologue(event_resetepilogue); |
||
509 | event_setlasthandler(kern_after_dispatch); |
||
510 | |||
511 | /* call the init functions */ |
||
512 | call_runlevel_func(RUNLEVEL_INIT, 0); |
||
513 | |||
38 | pj | 514 | |
515 | |||
516 | |||
517 | /* |
||
518 | * Runlevel RUNNING: Hoping that all works fine ;-) |
||
519 | * |
||
520 | * |
||
521 | */ |
||
522 | |||
523 | runlevel = RUNLEVEL_RUNNING; |
||
524 | |||
2 | pj | 525 | /* reset keyboard after exit */ |
526 | // sys_atexit((void(*)(void *))C8042_restore,NULL,AFTER_EXIT); |
||
527 | |||
528 | /* tracer stuff */ |
||
353 | giacomo | 529 | #ifdef __OLD_TRACER__ |
530 | trc_resume(); |
||
531 | #endif |
||
2 | pj | 532 | |
533 | /* exec and exec_shadow are already = -1 */ |
||
38 | pj | 534 | kern_gettime(&schedule_time); |
2 | pj | 535 | scheduler(); |
536 | global_context = ll_context_from(); /* It will be used by sys_end */ |
||
537 | ll_context_to(proc_table[exec_shadow].context); |
||
538 | |||
539 | /* |
||
540 | * |
||
541 | * Now the system starts!!! |
||
542 | * (hoping that someone has created some task(s) ) |
||
543 | * The function returns only at system end... |
||
544 | * |
||
545 | */ |
||
546 | |||
547 | |||
548 | /* |
||
38 | pj | 549 | * Runlevel SHUTDOWN: Shutting down the system... :-( |
2 | pj | 550 | * |
551 | * |
||
552 | */ |
||
553 | |||
554 | event_setlasthandler(NULL); |
||
555 | |||
556 | // ll_abort(666); |
||
557 | /* tracer stuff */ |
||
353 | giacomo | 558 | #ifdef __OLD_TRACER__ |
559 | trc_suspend(); |
||
560 | #endif |
||
2 | pj | 561 | |
562 | runlevel = RUNLEVEL_SHUTDOWN; |
||
563 | |||
564 | /* 1 when the error code is != 0 */ |
||
565 | aborting = global_errnumber > 0; |
||
566 | |||
38 | pj | 567 | //kern_printf("after - system_counter=%d, task_counter = %d\n", system_counter,task_counter); |
2 | pj | 568 | |
569 | call_runlevel_func(RUNLEVEL_SHUTDOWN, aborting); |
||
570 | |||
38 | pj | 571 | //kern_printf("before - system_counter=%d, task_counter = %d\n", system_counter,task_counter); |
2 | pj | 572 | |
573 | if (system_counter) { |
||
574 | /* To shutdown the kernel correctly, we have to wait that all the SYSTEM |
||
575 | tasks that are killable will die... |
||
576 | |||
577 | We don't mess about the user task... we only kill them and reschedule |
||
578 | The only thing important is that the system tasks shut down correctly. |
||
579 | We do nothing for user tasks that remain active (because, for example, |
||
580 | they have the cancelability set to deferred) when the system goes to |
||
581 | runlevel 3 */ |
||
38 | pj | 582 | //kern_printf("Û%lu",kern_gettime(NULL)); |
2 | pj | 583 | kill_user_tasks(); |
38 | pj | 584 | //kern_printf("Û%lu",kern_gettime(NULL)); |
2 | pj | 585 | |
586 | /* we have to go again in multitasking mode!!! */ |
||
587 | mustexit = 0; |
||
588 | |||
589 | /* exec and exec_shadow are already = -1 */ |
||
38 | pj | 590 | kern_gettime(&schedule_time); |
2 | pj | 591 | global_context = ll_context_from(); /* It will be used by sys_end */ |
592 | scheduler(); |
||
593 | |||
594 | event_setlasthandler(kern_after_dispatch); |
||
595 | ll_context_to(proc_table[exec_shadow].context); |
||
596 | event_setlasthandler(NULL); |
||
597 | } |
||
598 | |||
599 | |||
600 | |||
601 | |||
602 | /* |
||
38 | pj | 603 | * Runlevel BEFORE_EXIT: Before Halting the system |
2 | pj | 604 | * |
605 | * |
||
606 | */ |
||
607 | |||
608 | runlevel = RUNLEVEL_BEFORE_EXIT; |
||
609 | |||
610 | |||
611 | /* the field global_errnumber is |
||
612 | =0 if the system normally ends |
||
613 | !=0 if an abort is issued |
||
614 | */ |
||
615 | |||
616 | //kern_printf("Chiamo exit Functions\n"); |
||
617 | |||
618 | call_runlevel_func(RUNLEVEL_BEFORE_EXIT, aborting); |
||
619 | |||
620 | //kern_printf("Dopo exit Functions\n"); |
||
621 | |||
622 | /* Shut down the VM layer */ |
||
623 | ll_end(); |
||
624 | |||
625 | |||
626 | /* |
||
38 | pj | 627 | * Runlevel AFTER_EXIT: After halting... |
2 | pj | 628 | * |
629 | * |
||
630 | */ |
||
631 | |||
632 | runlevel = RUNLEVEL_AFTER_EXIT; |
||
633 | |||
634 | //kern_printf("prima before Functions\n"); |
||
635 | |||
636 | call_runlevel_func(RUNLEVEL_AFTER_EXIT, 0); |
||
637 | |||
638 | //kern_printf("dopo before Functions\n"); |
||
639 | kern_cli(); |
||
640 | if (global_errnumber) { |
||
641 | /* vm_abort called */ |
||
157 | pj | 642 | kern_printf("Abort detected\nCode : %u (%s)\n",global_errnumber, |
643 | global_errnumber<=LAST_ABORT_NUMBER ? _sys_abrtlist[global_errnumber] : "no description" ); |
||
2 | pj | 644 | l1_exit(-1); |
645 | } |
||
646 | |||
647 | l1_exit(0); // System terminated normally |
||
648 | |||
649 | } |
||
650 | |||
38 | pj | 651 | /* IMPORTANT!!! |
652 | I'm almost sure the shutdown procedure does not work into interrupts. */ |
||
2 | pj | 653 | void internal_sys_end(int i) |
654 | { |
||
655 | LEVEL l; /* a counter */ |
||
38 | pj | 656 | |
657 | /* if something goes wron during the real mode */ |
||
658 | if (runlevel==RUNLEVEL_STARTUP || runlevel==RUNLEVEL_AFTER_EXIT) |
||
659 | l1_exit(i); |
||
2 | pj | 660 | |
661 | //kern_printf("mustexit=%d",mustexit); |
||
38 | pj | 662 | if (mustexit) |
663 | return; |
||
2 | pj | 664 | |
38 | pj | 665 | mustexit = 1; |
2 | pj | 666 | |
38 | pj | 667 | global_errnumber = i; |
668 | |||
669 | |||
670 | if (!ll_ActiveInt()) { |
||
671 | proc_table[exec_shadow].context = kern_context_save(); |
||
672 | |||
2 | pj | 673 | if (exec_shadow != -1) { |
38 | pj | 674 | kern_gettime(&schedule_time); |
675 | |||
676 | kern_epilogue_macro(); |
||
677 | |||
2 | pj | 678 | /* then, we call the epilogue. the epilogue tipically checks the |
38 | pj | 679 | avail_time field... */ |
2 | pj | 680 | l = proc_table[exec_shadow].task_level; |
38 | pj | 681 | level_table[l]->public_epilogue(l,exec_shadow); |
682 | |||
2 | pj | 683 | exec_shadow = exec = -1; |
684 | } |
||
38 | pj | 685 | kern_context_load(global_context); |
686 | } |
||
2 | pj | 687 | |
38 | pj | 688 | if (ll_ActiveInt()) { |
689 | ll_context_to(global_context); |
||
690 | /* The context change will be done when all the interrupts end!!! */ |
||
2 | pj | 691 | } |
38 | pj | 692 | |
2 | pj | 693 | //kern_printf("fine sysend"); |
694 | |||
695 | /* the control reach this line only if we call sys_end() into an event |
||
696 | handler (for example, if the event raises an exception with |
||
697 | SA_USEFAST active and the exception calls sys_end() ) */ |
||
698 | } |
||
699 | |||
700 | |||
38 | pj | 701 | /* |
702 | Close the system & return to HOST OS. |
||
703 | Can be called from tasks and from ISRS |
||
704 | |||
705 | |||
706 | */ |
||
707 | void sys_abort(int err) |
||
2 | pj | 708 | { |
709 | SYS_FLAGS f; |
||
710 | |||
711 | f = kern_fsave(); |
||
38 | pj | 712 | internal_sys_end(err); |
2 | pj | 713 | kern_frestore(f); |
714 | } |
||
715 | |||
38 | pj | 716 | void sys_end(void) |
2 | pj | 717 | { |
38 | pj | 718 | sys_abort(0); |
2 | pj | 719 | } |
720 | |||
721 | void _exit(int status) |
||
722 | { |
||
38 | pj | 723 | sys_abort(status); |
2 | pj | 724 | } |
725 | |||
726 | |||
727 | |||
728 | /* this function is never called... used for the OSLib */ |
||
729 | void sys_abort_tail(int code) |
||
730 | { |
||
731 | //DUMMY!!!! |
||
732 | } |
||
733 | |||
734 | |||
735 | |||
736 | /*+ this primitive returns the time read from the system timer +*/ |
||
737 | TIME sys_gettime(struct timespec *t) |
||
738 | { |
||
739 | SYS_FLAGS f; |
||
740 | TIME x; |
||
741 | |||
742 | f = kern_fsave(); |
||
38 | pj | 743 | x = kern_gettime(t); |
2 | pj | 744 | kern_frestore(f); |
745 | |||
746 | return x; |
||
747 | } |
||
748 | |||
749 |