Subversion Repositories shark

Rev

Details | 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
 *   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
 ------------
23
 CVS :        $Id: rm.c,v 1.1.1.1 2002-03-29 14:12:52 pj Exp $
24
 
25
 File:        $File$
26
 Revision:    $Revision: 1.1.1.1 $
27
 Last update: $Date: 2002-03-29 14:12:52 $
28
 ------------
29
 
30
 This file contains the scheduling module RM (Rate Monotonic)
31
 
32
 Read rm.h for further details.
33
 
34
 This file is equal to EDF.c except for:
35
 
36
 . EDF changed to RM :-)
37
 . q_timespec_insert changed to q_insert
38
 . proc_table[p].priority is also modified when we modify lev->period[p]
39
 
40
 
41
**/
42
 
43
/*
44
 * Copyright (C) 2000 Paolo Gai
45
 *
46
 * This program is free software; you can redistribute it and/or modify
47
 * it under the terms of the GNU General Public License as published by
48
 * the Free Software Foundation; either version 2 of the License, or
49
 * (at your option) any later version.
50
 *
51
 * This program is distributed in the hope that it will be useful,
52
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
53
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
54
 * GNU General Public License for more details.
55
 *
56
 * You should have received a copy of the GNU General Public License
57
 * along with this program; if not, write to the Free Software
58
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
59
 *
60
 */
61
 
62
 
63
#include <modules/rm.h>
64
#include <ll/stdio.h>
65
#include <ll/string.h>
66
#include <kernel/model.h>
67
#include <kernel/descr.h>
68
#include <kernel/var.h>
69
#include <kernel/func.h>
70
#include <kernel/trace.h>
71
 
72
/*+ Status used in the level +*/
73
#define RM_READY         MODULE_STATUS_BASE    /*+ - Ready status        +*/
74
#define RM_DELAY         MODULE_STATUS_BASE+1  /*+ - Delay status        +*/
75
#define RM_WCET_VIOLATED MODULE_STATUS_BASE+2  /*+ when wcet is finished +*/
76
#define RM_WAIT          MODULE_STATUS_BASE+3  /*+ to wait the deadline  +*/
77
#define RM_IDLE          MODULE_STATUS_BASE+4  /*+ to wait the deadline  +*/
78
#define RM_ZOMBIE        MODULE_STATUS_BASE+5  /*+ to wait the free time +*/
79
 
80
/*+ flags +*/
81
#define RM_FLAG_SPORADIC    1
82
#define RM_FLAG_NORAISEEXC  2
83
 
84
/*+ the level redefinition for the Rate Monotonic +*/
85
typedef struct {
86
  level_des l;     /*+ the standard level descriptor          +*/
87
 
88
  TIME period[MAX_PROC]; /*+ The task periods; the deadlines are
89
                       stored in the priority field           +*/
90
  int deadline_timer[MAX_PROC];
91
                   /*+ The task deadline timers               +*/
92
 
93
  int flag[MAX_PROC];
94
                   /*+ used to manage the JOB_TASK_MODEL and the
95
                       periodicity                            +*/
96
 
97
  QUEUE ready;     /*+ the ready queue                        +*/
98
 
99
  int flags;       /*+ the init flags...                      +*/
100
 
101
  bandwidth_t U;   /*+ the used bandwidth                     +*/
102
 
103
} RM_level_des;
104
 
105
 
106
static char *RM_status_to_a(WORD status)
107
{
108
  if (status < MODULE_STATUS_BASE)
109
    return status_to_a(status);
110
 
111
  switch (status) {
112
    case RM_READY        : return "RM_Ready";
113
    case RM_DELAY        : return "RM_Delay";
114
    case RM_WCET_VIOLATED: return "RM_Wcet_Violated";
115
    case RM_WAIT         : return "RM_Sporadic_Wait";
116
    case RM_IDLE         : return "RM_Idle";
117
    case RM_ZOMBIE       : return "RM_Zombie";
118
    default              : return "RM_Unknown";
119
  }
120
}
121
 
122
static void RM_timer_deadline(void *par)
123
{
124
  PID p = (PID) par;
125
  RM_level_des *lev;
126
 
127
 
128
  lev = (RM_level_des *)level_table[proc_table[p].task_level];
129
 
130
  switch (proc_table[p].status) {
131
    case RM_ZOMBIE:
132
      /* we finally put the task in the ready queue */
133
      proc_table[p].status = FREE;
134
      q_insertfirst(p,&freedesc);
135
      /* and free the allocated bandwidth */
136
      lev->U -= (MAX_BANDWIDTH/lev->period[p]) * proc_table[p].wcet;
137
      break;
138
 
139
    case RM_IDLE:
140
      /* tracer stuff */
141
      trc_logevent(TRC_INTACTIVATION,&p);
142
      /* similar to RM_task_activate */
143
      TIMESPEC_ASSIGN(&proc_table[p].request_time,
144
                      &proc_table[p].timespec_priority);
145
      ADDUSEC2TIMESPEC(lev->period[p], &proc_table[p].timespec_priority);
146
      proc_table[p].status = RM_READY;
147
      q_insert(p,&lev->ready);
148
      lev->deadline_timer[p] = kern_event_post(&proc_table[p].timespec_priority,
149
                                               RM_timer_deadline,
150
                                               (void *)p);
151
      //printk("(d%d idle priority set to %d)",p,proc_table[p].priority );
152
      event_need_reschedule();
153
      printk("el%d|",p);
154
      break;
155
 
156
    case RM_WAIT:
157
      /* Without this, the task cannot be reactivated!!! */
158
      proc_table[p].status = SLEEP;
159
      break;
160
 
161
    default:
162
      /* else, a deadline miss occurred!!! */
163
      kern_printf("timer_deadline:AAARRRGGGHHH!!!");
164
      kern_raise(XDEADLINE_MISS,p);
165
  }
166
}
167
 
168
static void RM_timer_guest_deadline(void *par)
169
{
170
  PID p = (PID) par;
171
 
172
  kern_printf("AAARRRGGGHHH!!!");
173
  kern_raise(XDEADLINE_MISS,p);
174
}
175
 
176
/*+ this function is called when a task finish his delay +*/
177
static void RM_timer_delay(void *par)
178
{
179
  PID p = (PID) par;
180
  RM_level_des *lev;
181
 
182
  lev = (RM_level_des *)level_table[proc_table[p].task_level];
183
 
184
  proc_table[p].status = RM_READY;
185
  q_insert(p,&lev->ready);
186
 
187
  proc_table[p].delay_timer = NIL;  /* Paranoia */
188
 
189
  event_need_reschedule();
190
}
191
 
192
 
193
static int RM_level_accept_task_model(LEVEL l, TASK_MODEL *m)
194
{
195
  if (m->pclass == HARD_PCLASS || m->pclass == (HARD_PCLASS | l)) {
196
    HARD_TASK_MODEL *h = (HARD_TASK_MODEL *)m;
197
 
198
    if (h->wcet && h->mit)
199
      return 0;
200
  }
201
 
202
  return -1;
203
}
204
 
205
static int RM_level_accept_guest_model(LEVEL l, TASK_MODEL *m)
206
{
207
  if (m->pclass == JOB_PCLASS || m->pclass == (JOB_PCLASS | l))
208
    return 0;
209
  else
210
    return -1;
211
}
212
 
213
 
214
static char *onoff(int i)
215
{
216
  if (i)
217
    return "On ";
218
  else
219
    return "Off";
220
}
221
 
222
static void RM_level_status(LEVEL l)
223
{
224
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
225
  PID p = lev->ready;
226
 
227
  kern_printf("Wcet     Check    : %s\n",
228
            onoff(lev->flags & RM_ENABLE_WCET_CHECK));
229
  kern_printf("On-line guarantee : %s\n",
230
            onoff(lev->flags & RM_ENABLE_GUARANTEE));
231
  kern_printf("Used Bandwidth    : %u/%u\n",
232
            lev->U, MAX_BANDWIDTH);
233
 
234
  while (p != NIL) {
235
    if ((proc_table[p].pclass) == JOB_PCLASS)
236
      kern_printf("Pid: %2d (GUEST)\n", p);
237
    else
238
      kern_printf("Pid: %2d Name: %10s %s: %9ld Dline: %9ld.%6ld Stat: %s\n",
239
              p,
240
              proc_table[p].name,
241
              lev->flag[p] & RM_FLAG_SPORADIC ? "MinITime" : "Period  ",
242
              lev->period[p],
243
              proc_table[p].timespec_priority.tv_sec,
244
              proc_table[p].timespec_priority.tv_nsec/1000,
245
              RM_status_to_a(proc_table[p].status));
246
    p = proc_table[p].next;
247
  }
248
 
249
  for (p=0; p<MAX_PROC; p++)
250
    if (proc_table[p].task_level == l && proc_table[p].status != RM_READY
251
        && proc_table[p].status != FREE )
252
      kern_printf("Pid: %2d Name: %10s %s: %9ld Dline: %9ld.%6ld Stat: %s\n",
253
                p,
254
                proc_table[p].name,
255
                lev->flag[p] & RM_FLAG_SPORADIC ? "MinITime" : "Period  ",
256
                lev->period[p],
257
                proc_table[p].timespec_priority.tv_sec,
258
                proc_table[p].timespec_priority.tv_nsec/1000,
259
                RM_status_to_a(proc_table[p].status));
260
}
261
 
262
/* The scheduler only gets the first task in the queue */
263
static PID RM_level_scheduler(LEVEL l)
264
{
265
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
266
 
267
/*  {  // print 4 dbg the ready queue
268
    PID p= lev->ready;
269
    kern_printf("(s");
270
    while (p != NIL) {
271
      kern_printf("%d ",p);
272
      p = proc_table[p].next;
273
    }
274
    kern_printf(") ");
275
  }
276
  */
277
  return (PID)lev->ready;
278
}
279
 
280
/* The on-line guarantee is enabled only if the appropriate flag is set... */
281
static int RM_level_guarantee(LEVEL l, bandwidth_t *freebandwidth)
282
{
283
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
284
 
285
  if (lev->flags & RM_FAILED_GUARANTEE) {
286
    *freebandwidth = 0;
287
    return 0;
288
  }
289
  else
290
    if (*freebandwidth >= lev->U) {
291
      *freebandwidth -= lev->U;
292
      return 1;
293
    }
294
    else
295
      return 0;
296
 
297
}
298
 
299
static int RM_task_create(LEVEL l, PID p, TASK_MODEL *m)
300
{
301
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
302
 
303
  /* if the RM_task_create is called, then the pclass must be a
304
     valid pclass. */
305
 
306
  HARD_TASK_MODEL *h = (HARD_TASK_MODEL *)m;
307
 
308
  proc_table[p].priority = lev->period[p] = h->mit;
309
 
310
  if (h->periodicity == APERIODIC)
311
    lev->flag[p] = RM_FLAG_SPORADIC;
312
  else
313
    lev->flag[p] = 0;
314
  lev->deadline_timer[p] = -1;
315
 
316
  /* Enable wcet check */
317
  if (lev->flags & RM_ENABLE_WCET_CHECK) {
318
    proc_table[p].avail_time = h->wcet;
319
    proc_table[p].wcet       = h->wcet;
320
    proc_table[p].control |= CONTROL_CAP;
321
  }
322
 
323
  /* update the bandwidth... */
324
  if (lev->flags & RM_ENABLE_GUARANTEE) {
325
    bandwidth_t b;
326
    b = (MAX_BANDWIDTH / h->mit) * h->wcet;
327
 
328
    /* really update lev->U, checking an overflow... */
329
    if (MAX_BANDWIDTH - lev->U > b)
330
      lev->U += b;
331
    else
332
      /* The task can NOT be guaranteed (U>MAX_BANDWIDTH)...
333
         in this case, we don't raise an exception... in fact, after the
334
         RM_task_create the task_create will call level_guarantee that return
335
         -1... return -1 in RM_task_create isn't correct, because:
336
           . generally, the guarantee must be done when also the resources
337
             are registered
338
           . returning -1 will cause the task_create to return with an errno
339
             ETASK_CREATE instead of ENO_GUARANTEE!!!
340
 
341
         Why I use the flag??? because if the lev->U overflows, if i.e. I set
342
         it to MAX_BANDWIDTH, I lose the correct allocated bandwidth...
343
      */
344
      lev->flags |= RM_FAILED_GUARANTEE;
345
  }
346
 
347
  return 0; /* OK, also if the task cannot be guaranteed... */
348
}
349
 
350
static void RM_task_detach(LEVEL l, PID p)
351
{
352
  /* the RM level doesn't introduce any dinamic allocated new field.
353
     we have only to reset the NO_GUARANTEE FIELD and decrement the allocated
354
     bandwidth */
355
 
356
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
357
 
358
  if (lev->flags & RM_FAILED_GUARANTEE)
359
    lev->flags &= ~RM_FAILED_GUARANTEE;
360
  else
361
    lev->U -= (MAX_BANDWIDTH / lev->period[p]) * proc_table[p].wcet;
362
}
363
 
364
static int RM_task_eligible(LEVEL l, PID p)
365
{
366
  return 0; /* if the task p is chosen, it is always eligible */
367
}
368
 
369
#ifdef __TEST1__
370
extern int testactive;
371
extern struct timespec s_stime[];
372
extern TIME s_curr[];
373
extern TIME s_PID[];
374
extern int useds;
375
#endif
376
 
377
static void RM_task_dispatch(LEVEL l, PID p, int nostop)
378
{
379
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
380
 
381
//  kern_printf("(disp %d)",p);
382
 
383
  /* the task state is set EXE by the scheduler()
384
     we extract the task from the ready queue
385
     NB: we can't assume that p is the first task in the queue!!! */
386
  q_extract(p, &lev->ready);
387
 
388
  #ifdef __TEST1__
389
  if (testactive)
390
  {
391
    TIMESPEC_ASSIGN(&s_stime[useds], &schedule_time);
392
    s_curr[useds] = proc_table[p].avail_time;
393
    s_PID[useds]  = p;
394
    useds++;
395
  }
396
  #endif
397
}
398
 
399
static void RM_task_epilogue(LEVEL l, PID p)
400
{
401
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
402
 
403
//  kern_printf("(epil %d)",p);
404
 
405
  /* check if the wcet is finished... */
406
  if ((lev->flags & RM_ENABLE_WCET_CHECK) && proc_table[p].avail_time <= 0) {
407
    /* if it is, raise a XWCET_VIOLATION exception */
408
    kern_raise(XWCET_VIOLATION,p);
409
    proc_table[p].status = RM_WCET_VIOLATED;
410
  }
411
  else {
412
    /* the task has been preempted. it returns into the ready queue... */
413
    q_insert(p,&lev->ready);
414
    proc_table[p].status = RM_READY;
415
  }
416
}
417
 
418
static void RM_task_activate(LEVEL l, PID p)
419
{
420
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
421
 
422
  if (proc_table[p].status == RM_WAIT) {
423
    kern_raise(XACTIVATION,p);
424
    return;
425
  }
426
 
427
  /* Test if we are trying to activate a non sleeping task    */
428
  /* Ignore this; the task is already active                  */
429
  if (proc_table[p].status != SLEEP &&
430
      proc_table[p].status != RM_WCET_VIOLATED)
431
    return;
432
 
433
 
434
  /* see also RM_timer_deadline */
435
  ll_gettime(TIME_EXACT, &proc_table[p].request_time);
436
 
437
  TIMESPEC_ASSIGN(&proc_table[p].timespec_priority,
438
                  &proc_table[p].request_time);
439
  ADDUSEC2TIMESPEC(lev->period[p], &proc_table[p].timespec_priority);
440
 
441
  /* Insert task in the correct position */
442
  proc_table[p].status = RM_READY;
443
  q_insert(p,&lev->ready);
444
 
445
  /* Set the deadline timer */
446
  lev->deadline_timer[p] = kern_event_post(&proc_table[p].timespec_priority,
447
                                           RM_timer_deadline,
448
                                           (void *)p);
449
}
450
 
451
static void RM_task_insert(LEVEL l, PID p)
452
{
453
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
454
 
455
  /* Similar to RM_task_activate, but we don't check in what state
456
     the task is and we don't set the request_time*/
457
 
458
  /* Insert task in the correct position */
459
  proc_table[p].status = RM_READY;
460
  q_insert(p,&lev->ready);
461
}
462
 
463
static void RM_task_extract(LEVEL l, PID p)
464
{
465
  /* Extract the running task from the level
466
     . we have already extract it from the ready queue at the dispatch time.
467
     . the capacity event have to be removed by the generic kernel
468
     . the wcet don't need modification...
469
     . the state of the task is set by the calling function
470
     . the deadline must remain...
471
 
472
     So, we do nothing!!!
473
  */
474
}
475
 
476
static void RM_task_endcycle(LEVEL l, PID p)
477
{
478
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
479
 
480
  /* the task has terminated his job before it consume the wcet. All OK! */
481
  if (lev->flag[p] & RM_FLAG_SPORADIC)
482
    proc_table[p].status = RM_WAIT;
483
  else /* pclass = sporadic_pclass */
484
    proc_table[p].status = RM_IDLE;
485
 
486
  /* we reset the capacity counters... */
487
  if (lev->flags & RM_ENABLE_WCET_CHECK)
488
    proc_table[p].avail_time = proc_table[p].wcet;
489
 
490
  /* when the deadline timer fire, it recognize the situation and set
491
     correctly all the stuffs (like reactivation, request_time, etc... ) */
492
}
493
 
494
static void RM_task_end(LEVEL l, PID p)
495
{
496
//  RM_level_des *lev = (RM_level_des *)(level_table[l]);
497
 
498
  proc_table[p].status = RM_ZOMBIE;
499
 
500
  /* When the deadline timer fire, it put the task descriptor in
501
     the free queue, and free the allocated bandwidth... */
502
}
503
 
504
static void RM_task_sleep(LEVEL l, PID p)
505
{
506
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
507
 
508
  /* the task has terminated his job before it consume the wcet. All OK! */
509
  proc_table[p].status = RM_WAIT;
510
 
511
  /* we reset the capacity counters... */
512
  if (lev->flags & RM_ENABLE_WCET_CHECK)
513
    proc_table[p].avail_time = proc_table[p].wcet;
514
 
515
  /* when the deadline timer fire, it recognize the situation and set
516
     correctly the task state to sleep... */
517
}
518
 
519
static void RM_task_delay(LEVEL l, PID p, TIME usdelay)
520
{
521
  struct timespec wakeuptime;
522
//  RM_level_des *lev = (RM_level_des *)(level_table[l]);
523
 
524
  /* equal to RM_task_endcycle */
525
  proc_table[p].status = RM_DELAY;
526
 
527
  /* we need to delete this event if we kill the task while it is sleeping */
528
  ll_gettime(TIME_EXACT, &wakeuptime);
529
  ADDUSEC2TIMESPEC(usdelay, &wakeuptime);
530
  proc_table[p].delay_timer = kern_event_post(&wakeuptime,
531
                                              RM_timer_delay,
532
                                              (void *)p);
533
}
534
 
535
/* Guest Functions
536
   These functions manages a JOB_TASK_MODEL, that is used to put
537
   a guest task in the RM ready queue. */
538
 
539
static int RM_guest_create(LEVEL l, PID p, TASK_MODEL *m)
540
{
541
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
542
  JOB_TASK_MODEL *job = (JOB_TASK_MODEL *)m;
543
 
544
  /* if the RM_guest_create is called, then the pclass must be a
545
     valid pclass. */
546
 
547
 
548
  TIMESPEC_ASSIGN(&proc_table[p].timespec_priority, &job->deadline);
549
 
550
  lev->deadline_timer[p] = -1;
551
 
552
  if (job->noraiseexc)
553
    lev->flag[p] = RM_FLAG_NORAISEEXC;
554
  else
555
    lev->flag[p] = 0;
556
 
557
  proc_table[p].priority = lev->period[p] = job->period;
558
 
559
  /* there is no bandwidth guarantee at this level, it is performed
560
     by the level that inserts guest tasks... */
561
 
562
  return 0; /* OK, also if the task cannot be guaranteed... */
563
}
564
 
565
static void RM_guest_detach(LEVEL l, PID p)
566
{
567
  /* the RM level doesn't introduce any dinamic allocated new field.
568
     No guarantee is performed on guest tasks... so we don't have to reset
569
     the NO_GUARANTEE FIELD */
570
}
571
 
572
static void RM_guest_dispatch(LEVEL l, PID p, int nostop)
573
{
574
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
575
 
576
  /* the task state is set to EXE by the scheduler()
577
     we extract the task from the ready queue
578
     NB: we can't assume that p is the first task in the queue!!! */
579
  q_extract(p, &lev->ready);
580
}
581
 
582
static void RM_guest_epilogue(LEVEL l, PID p)
583
{
584
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
585
 
586
  /* the task has been preempted. it returns into the ready queue... */
587
  q_insert(p,&lev->ready);
588
  proc_table[p].status = RM_READY;
589
}
590
 
591
static void RM_guest_activate(LEVEL l, PID p)
592
{
593
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
594
 
595
  /* Insert task in the correct position */
596
  q_insert(p,&lev->ready);
597
  proc_table[p].status = RM_READY;
598
 
599
  /* Set the deadline timer */
600
  if (!(lev->flag[p] & RM_FLAG_NORAISEEXC))
601
    lev->deadline_timer[p] = kern_event_post(&proc_table[p].timespec_priority,
602
                                             RM_timer_guest_deadline,
603
                                             (void *)p);
604
 
605
}
606
 
607
static void RM_guest_insert(LEVEL l, PID p)
608
{
609
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
610
 
611
  /* Insert task in the correct position */
612
  q_insert(p,&lev->ready);
613
  proc_table[p].status = RM_READY;
614
}
615
 
616
static void RM_guest_extract(LEVEL l, PID p)
617
{
618
  /* Extract the running task from the level
619
     . we have already extract it from the ready queue at the dispatch time.
620
     . the state of the task is set by the calling function
621
     . the deadline must remain...
622
 
623
     So, we do nothing!!!
624
  */
625
}
626
 
627
static void RM_guest_endcycle(LEVEL l, PID p)
628
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
629
 
630
static void RM_guest_end(LEVEL l, PID p)
631
{
632
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
633
 
634
  //kern_printf("RM_guest_end: dline timer %d\n",lev->deadline_timer[p]);
635
  if (proc_table[p].status == RM_READY)
636
  {
637
    q_extract(p, &lev->ready);
638
    //kern_printf("(g_end rdy extr)");
639
  }
640
  else if (proc_table[p].status == RM_DELAY) {
641
    event_delete(proc_table[p].delay_timer);
642
    proc_table[p].delay_timer = NIL;    /* paranoia */
643
  }
644
 
645
  /* we remove the deadline timer, because the slice is finished */
646
  if (lev->deadline_timer[p] != NIL) {
647
//    kern_printf("RM_guest_end: dline timer %d\n",lev->deadline_timer[p]);
648
    event_delete(lev->deadline_timer[p]);
649
    lev->deadline_timer[p] = NIL;
650
  }
651
 
652
}
653
 
654
static void RM_guest_sleep(LEVEL l, PID p)
655
{ kern_raise(XUNVALID_GUEST,exec_shadow); }
656
 
657
static void RM_guest_delay(LEVEL l, PID p, TIME usdelay)
658
{
659
  struct timespec wakeuptime;
660
//  RM_level_des *lev = (RM_level_des *)(level_table[l]);
661
 
662
  /* equal to RM_task_endcycle */
663
  proc_table[p].status = RM_DELAY;
664
 
665
  /* we need to delete this event if we kill the task while it is sleeping */
666
  ll_gettime(TIME_EXACT, &wakeuptime);
667
  ADDUSEC2TIMESPEC(usdelay, &wakeuptime);
668
  proc_table[p].delay_timer = kern_event_post(&wakeuptime,
669
                                              RM_timer_delay,
670
                                              (void *)p);
671
}
672
 
673
 
674
 
675
 
676
/* Registration functions */
677
 
678
/*+ Registration function:
679
    int flags                 the init flags ... see rm.h +*/
680
void RM_register_level(int flags)
681
{
682
  LEVEL l;            /* the level that we register */
683
  RM_level_des *lev;  /* for readableness only */
684
  PID i;              /* a counter */
685
 
686
  printk("RM_register_level\n");
687
 
688
  /* request an entry in the level_table */
689
  l = level_alloc_descriptor();
690
 
691
  /* alloc the space needed for the RM_level_des */
692
  lev = (RM_level_des *)kern_alloc(sizeof(RM_level_des));
693
 
694
  printk("    lev=%d\n",(int)lev);
695
 
696
  /* update the level_table with the new entry */
697
  level_table[l] = (level_des *)lev;
698
 
699
  /* fill the standard descriptor */
700
  strncpy(lev->l.level_name,  RM_LEVELNAME, MAX_LEVELNAME);
701
  lev->l.level_code               = RM_LEVEL_CODE;
702
  lev->l.level_version            = RM_LEVEL_VERSION;
703
 
704
  lev->l.level_accept_task_model  = RM_level_accept_task_model;
705
  lev->l.level_accept_guest_model = RM_level_accept_guest_model;
706
  lev->l.level_status             = RM_level_status;
707
  lev->l.level_scheduler          = RM_level_scheduler;
708
 
709
  if (flags & RM_ENABLE_GUARANTEE)
710
    lev->l.level_guarantee        = RM_level_guarantee;
711
  else
712
    lev->l.level_guarantee        = NULL;
713
 
714
  lev->l.task_create              = RM_task_create;
715
  lev->l.task_detach              = RM_task_detach;
716
  lev->l.task_eligible            = RM_task_eligible;
717
  lev->l.task_dispatch            = RM_task_dispatch;
718
  lev->l.task_epilogue            = RM_task_epilogue;
719
  lev->l.task_activate            = RM_task_activate;
720
  lev->l.task_insert              = RM_task_insert;
721
  lev->l.task_extract             = RM_task_extract;
722
  lev->l.task_endcycle            = RM_task_endcycle;
723
  lev->l.task_end                 = RM_task_end;
724
  lev->l.task_sleep               = RM_task_sleep;
725
  lev->l.task_delay               = RM_task_delay;
726
 
727
  lev->l.guest_create             = RM_guest_create;
728
  lev->l.guest_detach             = RM_guest_detach;
729
  lev->l.guest_dispatch           = RM_guest_dispatch;
730
  lev->l.guest_epilogue           = RM_guest_epilogue;
731
  lev->l.guest_activate           = RM_guest_activate;
732
  lev->l.guest_insert             = RM_guest_insert;
733
  lev->l.guest_extract            = RM_guest_extract;
734
  lev->l.guest_endcycle           = RM_guest_endcycle;
735
  lev->l.guest_end                = RM_guest_end;
736
  lev->l.guest_sleep              = RM_guest_sleep;
737
  lev->l.guest_delay              = RM_guest_delay;
738
 
739
  /* fill the RM descriptor part */
740
  for(i=0; i<MAX_PROC; i++) {
741
    lev->period[i]         = 0;
742
    lev->deadline_timer[i] = -1;
743
    lev->flag[i]          = 0;
744
  }
745
 
746
  lev->ready = NIL;
747
  lev->flags = flags & 0x07;
748
  lev->U     = 0;
749
}
750
 
751
bandwidth_t RM_usedbandwidth(LEVEL l)
752
{
753
  RM_level_des *lev = (RM_level_des *)(level_table[l]);
754
  if (lev->l.level_code    == RM_LEVEL_CODE &&
755
      lev->l.level_version == RM_LEVEL_VERSION)
756
    return lev->U;
757
  else
758
    return 0;
759
}
760