Subversion Repositories shark

Rev

Rev 978 | Rev 988 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
235 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
 *   (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
 * Copyright (C) 2001 Paolo Gai
21
 *
22
 * This program is free software; you can redistribute it and/or modify
23
 * it under the terms of the GNU General Public License as published by
24
 * the Free Software Foundation; either version 2 of the License, or
25
 * (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program; if not, write to the Free Software
34
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35
 *
36
 */
37
 
38
#include "rmstar.h"
39
#include <ll/stdio.h>
40
#include <ll/string.h>
41
#include <kernel/model.h>
42
#include <kernel/descr.h>
43
#include <kernel/var.h>
44
#include <kernel/func.h>
45
 
783 giacomo 46
#include <tracer.h>
47
 
235 giacomo 48
/* for iqueues */
49
/* #include "iqueue.h" Now iqueues are the only queue type into the kernel */
50
 
51
/* for BUDGET_TASK_MODEL */
867 trimarchi 52
#include "fsf_configuration_parameters.h"
53
#include "fsf_core.h"
241 giacomo 54
#include "fsf_server.h"
978 trimarchi 55
#include <posix/posix/comm_message.h>
235 giacomo 56
 
57
/*
58
 * DEBUG stuffs begin
59
 */
60
 
61
//#define RMSTAR_DEBUG
62
 
63
#ifdef RMSTAR_DEBUG
64
 
65
static __inline__ fake_printf(char *fmt, ...) {}
66
 
978 trimarchi 67
#define rmstar_printf kern_printf
68
#define rmstar_printf2 kern_printf
69
#define rmstar_printf3 kern_printf
235 giacomo 70
 
71
//#define rmstar_printf kern_printf
72
//#define rmstar_printf2 kern_printf
73
//#define rmstar_printf3 kern_printf
74
#endif
75
 
76
/*
77
 * DEBUG stuffs end
78
 */
79
 
80
/* Status used in the level */
81
#define RMSTAR_READY         MODULE_STATUS_BASE    /* - Ready status        */
82
#define RMSTAR_IDLE          MODULE_STATUS_BASE+4  /* to wait the deadline  */
83
 
84
/* flags */
978 trimarchi 85
#define RMSTAR_CHANGE_LEVEL     8
813 trimarchi 86
#define RMSTAR_FLAG_NOPREEMPT   4
235 giacomo 87
#define RMSTAR_FLAG_NORAISEEXC  2
394 trimarchi 88
#define RMSTAR_FLAG_SPORADIC    1
235 giacomo 89
 
90
/* the level redefinition for the Earliest Deadline First level */
91
typedef struct {
92
  level_des l;     /* the standard level descriptor          */
93
 
94
  TIME period[MAX_PROC]; /* The task periods; the deadlines are
95
                       stored in the priority field           */
96
  int deadline_timer[MAX_PROC];
97
                   /* The task deadline timers               */
98
 
99
  struct timespec deadline_timespec[MAX_PROC];
100
 
101
  int dline_miss[MAX_PROC]; /* Deadline miss counter */
102
  int wcet_miss[MAX_PROC];  /* Wcet miss counter */
103
 
104
  int nact[MAX_PROC];       /* Wcet miss counter */
105
 
106
  int flag[MAX_PROC];
107
                   /* used to manage the JOB_TASK_MODEL and the
108
                       periodicity                            */
109
 
110
  IQUEUE ready;     /* the ready queue                        */
111
 
112
  PID activated;   /* the task that has been inserted into the
113
                       master module */
114
 
115
  int budget[MAX_PROC];
116
 
117
  int scheduling_level;
118
 
394 trimarchi 119
  int cap_lev;
120
 
121
  struct timespec cap_lasttime;
122
 
978 trimarchi 123
  int new_level[MAX_PROC];
124
  int wcet[MAX_PROC]; /* save the wcet fields */
125
 
235 giacomo 126
} RMSTAR_level_des;
127
 
394 trimarchi 128
static void capacity_handler(void *l)
129
{
130
  RMSTAR_level_des *lev = l;
131
  lev->cap_lev = NIL;
132
  event_need_reschedule();
133
}
134
 
235 giacomo 135
static void RMSTAR_check_preemption(RMSTAR_level_des *lev)
136
{
137
  PID first;
138
 
139
#ifdef RMSTAR_DEBUG
140
  rmstar_printf("(E:chk)");
141
#endif
813 trimarchi 142
  /* check if the task is preempteble or not */
821 trimarchi 143
  if (lev->activated!=NIL && lev->flag[lev->activated] & RMSTAR_FLAG_NOPREEMPT) return;
235 giacomo 144
 
145
  if ((first = iq_query_first(&lev->ready)) != lev->activated) {
146
    if (lev->activated != NIL)
147
      level_table[ lev->scheduling_level ]->
148
        private_extract(lev->scheduling_level, lev->activated);
149
 
150
    lev->activated = first;
151
 
152
    if (first != NIL) {
153
      BUDGET_TASK_MODEL b;
154
      budget_task_default_model(b, lev->budget[first]);
155
 
156
      level_table[ lev->scheduling_level ]->
157
        private_insert(lev->scheduling_level, first, (TASK_MODEL *)&b);
158
    }
159
  }
160
}
161
 
978 trimarchi 162
static int RMSTAR_private_change_level(LEVEL l, PID p)
163
{
164
 
165
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
166
 
167
  /* Change task level */
168
  if (lev->flag[p] & RMSTAR_CHANGE_LEVEL) {
169
 
170
#ifdef RMSTAR_DEBUG
171
    rmstar_printf("(RM:clev)");
172
#endif
173
 
174
    STD_command_message msg;
175
 
176
    proc_table[p].status = SLEEP;
177
    lev->flag[p] &= ~ RMSTAR_CHANGE_LEVEL;
178
 
179
    level_table[lev->scheduling_level]->private_extract(lev->scheduling_level,p);
180
    iq_extract(p,&lev->ready);
181
 
182
    if (lev->deadline_timer[p] != -1)
183
      kern_event_delete(lev->deadline_timer[p]);
184
 
185
    lev->deadline_timer[p]=NIL;
186
 
187
    RMSTAR_check_preemption(lev);
188
 
189
    lev->nact[p] = 0;
190
    lev->budget[p] = -1;
191
    proc_table[p].task_level = lev->new_level[p];
192
 
193
    /* Send change level command to local scheduler */
194
 
195
    msg.command = STD_ACTIVATE_TASK;
196
    msg.param = NULL;
197
 
198
    level_table[ lev->new_level[p] ]->public_message(lev->new_level[p],p,&msg);
199
 
200
    return 1;
201
 
202
  }
203
 
204
  return 0;
205
 
206
}
235 giacomo 207
static void RMSTAR_timer_deadline(void *par);
208
 
209
static void RMSTAR_internal_activate(RMSTAR_level_des *lev, PID p,
210
                                     struct timespec *t)
211
{
212
#ifdef RMSTAR_DEBUG
213
  rmstar_printf("(E:iact)");
214
#endif
215
 
216
  ADDUSEC2TIMESPEC(lev->period[p], t);
217
 
218
  *iq_query_timespec(p, &lev->ready) = *t;
219
  lev->deadline_timespec[p] = *t;
220
 
221
  /* Insert task in the correct position */
222
  proc_table[p].status = RMSTAR_READY;
223
  iq_priority_insert(p,&lev->ready);
224
 
394 trimarchi 225
  proc_table[p].control &= ~CONTROL_CAP;
235 giacomo 226
 
227
  /* check for preemption */
228
  RMSTAR_check_preemption(lev);
229
}
230
 
231
static void RMSTAR_timer_deadline(void *par)
232
{
233
  PID p = (PID) par;
234
  RMSTAR_level_des *lev;
235
 
236
#ifdef RMSTAR_DEBUG
978 trimarchi 237
  rmstar_printf("(E:tdl ");
235 giacomo 238
#endif
239
 
240
  lev = (RMSTAR_level_des *)level_table[proc_table[p].task_level];
799 giacomo 241
  lev->deadline_timer[p] = NIL;
235 giacomo 242
 
243
  switch (proc_table[p].status) {
244
    case RMSTAR_IDLE:
245
#ifdef RMSTAR_DEBUG
978 trimarchi 246
      rmstar_printf2("I%d",p);
235 giacomo 247
#endif
248
      /* set the request time */
394 trimarchi 249
      if (!(lev->flag[p] & RMSTAR_FLAG_SPORADIC))
250
          RMSTAR_internal_activate(lev,p,iq_query_timespec(p, &lev->ready));
235 giacomo 251
 
252
      event_need_reschedule();
253
      break;
254
 
255
    default:
256
#ifdef RMSTAR_DEBUG
978 trimarchi 257
      rmstar_printf2("D%d",p);
235 giacomo 258
#endif
259
      /* else, a deadline miss occurred!!! */
260
      lev->dline_miss[p]++;
783 giacomo 261
      TRACER_LOGEVENT(FTrace_EVT_task_deadline_miss,proc_table[p].context,proc_table[p].task_level);
262
 
394 trimarchi 263
      if (!(lev->flag[p] & RMSTAR_FLAG_SPORADIC)) {
264
         lev->nact[p]++;
265
         ADDUSEC2TIMESPEC(lev->period[p], &lev->deadline_timespec[p]);
266
      }
235 giacomo 267
  }
268
 
269
  /* Set the deadline timer */
394 trimarchi 270
  if (!(lev->flag[p] & RMSTAR_FLAG_SPORADIC))
271
     lev->deadline_timer[p] = kern_event_post(&lev->deadline_timespec[p],
272
                                              RMSTAR_timer_deadline,
273
                                              (void *)p);
235 giacomo 274
 
275
#ifdef RMSTAR_DEBUG
978 trimarchi 276
  rmstar_printf(")");
235 giacomo 277
#endif
278
}
279
 
280
static void RMSTAR_timer_guest_deadline(void *par)
281
{
282
  PID p = (PID) par;
283
 
284
#ifdef RMSTAR_DEBUG
285
  rmstar_printf("(E:gdl)");
286
#endif
287
 
288
  kern_raise(XDEADLINE_MISS,p);
289
}
290
 
394 trimarchi 291
static void RMSTAR_account_capacity(RMSTAR_level_des *lev, PID p)
292
{
293
  struct timespec ty;
294
  TIME tx;
295
 
296
  SUBTIMESPEC(&schedule_time, &lev->cap_lasttime, &ty);
297
  tx = TIMESPEC2USEC(&ty);
298
 
299
  proc_table[p].avail_time -= tx;
300
}
301
 
302
 
235 giacomo 303
static int RMSTAR_public_create(LEVEL l, PID p, TASK_MODEL *m)
304
{
305
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
394 trimarchi 306
 
235 giacomo 307
  /* if the RMSTAR_task_create is called, then the pclass must be a
308
     valid pclass. */
309
  HARD_TASK_MODEL *h;
978 trimarchi 310
#ifdef RMSTAR_DEBUG
311
  rmstar_printf("(RM:tcr)");
312
#endif 
235 giacomo 313
  if (m->pclass != HARD_PCLASS) return -1;
314
  if (m->level != 0 && m->level != l) return -1;
315
  h = (HARD_TASK_MODEL *)m;
394 trimarchi 316
  if (!h->wcet || !h->mit) return -1;
235 giacomo 317
  /* now we know that m is a valid model */
318
 
978 trimarchi 319
 
394 trimarchi 320
 
235 giacomo 321
#ifdef RMSTAR_DEBUG
978 trimarchi 322
  rmstar_printf("(RM:tcr)");
235 giacomo 323
#endif
324
 
325
  lev->period[p] = h->mit;
326
  *iq_query_priority(p, &lev->ready) = h->mit;
327
 
328
  lev->flag[p] = 0;
978 trimarchi 329
 
330
  if (h->periodicity == APERIODIC)
331
     lev->flag[p] |= RMSTAR_FLAG_SPORADIC;
332
 
235 giacomo 333
  lev->deadline_timer[p] = -1;
334
  lev->dline_miss[p]     = 0;
335
  lev->wcet_miss[p]      = 0;
336
  lev->nact[p]           = 0;
337
 
338
  /* Enable wcet check */
339
  proc_table[p].avail_time = h->wcet;
340
  proc_table[p].wcet       = h->wcet;
394 trimarchi 341
  //proc_table[p].control |= CONTROL_CAP;
235 giacomo 342
 
343
  return 0; /* OK, also if the task cannot be guaranteed... */
344
}
345
 
346
static void RMSTAR_public_dispatch(LEVEL l, PID p, int nostop)
347
{
348
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
394 trimarchi 349
  struct timespec ty;
235 giacomo 350
 
351
#ifdef RMSTAR_DEBUG
352
  rmstar_printf("(E:dis)");
353
 
354
  rmstar_printf3("(%d %d)",
355
                  iq_query_timespec(p, &lev->ready)->tv_nsec/1000000,
356
                  schedule_time.tv_nsec/1000000);
357
#endif
358
 
394 trimarchi 359
  if (!nostop || proc_table[exec].task_level==l) {
360
      TIMESPEC_ASSIGN(&ty, &schedule_time);
361
      TIMESPEC_ASSIGN(&lev->cap_lasttime, &schedule_time);
362
 
363
      /* ...and finally, we have to post a capacity event on exec task because the shadow_task consume
364
       *      *        capacity on exe task always */
786 trimarchi 365
      if (proc_table[exec].avail_time > 0) {
366
        ADDUSEC2TIMESPEC(proc_table[exec].avail_time ,&ty);
367
        lev->cap_lev = kern_event_post(&ty,capacity_handler, lev);
368
      }
394 trimarchi 369
      level_table[lev->scheduling_level]->private_dispatch(lev->scheduling_level, p, nostop);
370
  }
371
  else
372
      level_table[proc_table[exec].task_level]->public_dispatch(proc_table[exec].task_level, p, nostop);
373
 
235 giacomo 374
}
375
 
376
static void RMSTAR_public_epilogue(LEVEL l, PID p)
377
{
378
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
379
 
380
#ifdef RMSTAR_DEBUG
381
  rmstar_printf("(E:epi ");
382
#endif
383
 
394 trimarchi 384
  if (lev->cap_lev!=NIL) {
385
    kern_event_delete(lev->cap_lev);
386
    lev->cap_lev=NIL;
387
  }
388
 
389
 
390
  if ( proc_table[exec].task_level==l ) {
391
 
786 trimarchi 392
    if (proc_table[exec].avail_time > 0) RMSTAR_account_capacity(lev,exec);
978 trimarchi 393
 
394
    if (RMSTAR_private_change_level(l, p)) return;
395
 
394 trimarchi 396
    /* check if the wcet is finished... */
981 trimarchi 397
    if (proc_table[exec].avail_time < 0 ) {
394 trimarchi 398
      /* wcet finished: disable wcet event and count wcet miss */
235 giacomo 399
#ifdef RMSTAR_DEBUG
394 trimarchi 400
      rmstar_printf2("W%d",p);
235 giacomo 401
#endif
394 trimarchi 402
      //proc_table[p].control &= ~CONTROL_CAP;
783 giacomo 403
      TRACER_LOGEVENT(FTrace_EVT_task_wcet_violation,proc_table[p].context,proc_table[p].task_level);
394 trimarchi 404
      lev->wcet_miss[p]++;
405
    }
235 giacomo 406
#ifdef RMSTAR_DEBUG
394 trimarchi 407
    rmstar_printf(")");
235 giacomo 408
#endif
394 trimarchi 409
 
410
    level_table[ lev->scheduling_level ]->
411
      private_epilogue(lev->scheduling_level,p);
412
 
413
    proc_table[p].status = RMSTAR_READY;
414
  } else
415
    level_table[proc_table[exec].task_level]->public_epilogue(proc_table[exec].task_level,p);
235 giacomo 416
}
417
 
783 giacomo 418
static void RMSTAR_public_activate(LEVEL l, PID p, struct timespec *o)
235 giacomo 419
{
420
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
421
  struct timespec t;
422
 
423
#ifdef RMSTAR_DEBUG
424
  rmstar_printf("(E:act)");
425
#endif
394 trimarchi 426
 /* Test if we are trying to activate a non sleeping task    */
427
 /* save activation (only if needed... */
235 giacomo 428
 
394 trimarchi 429
 if (proc_table[p].status != SLEEP) {
430
     /* a periodic task cannot be activated when it is already active */
431
     /* but aperiodic task can be reactivate before */
802 giacomo 432
     if (lev->flag[p] & RMSTAR_FLAG_SPORADIC) {
433
        if (proc_table[p].status != RMSTAR_IDLE) {
434
          lev->nact[p]++;
435
          return;
436
        }
437
     } else {
438
        kern_raise(XACTIVATION,p);
394 trimarchi 439
     }
440
  }
441
 
235 giacomo 442
  /* Test if we are trying to activate a non sleeping task    */
443
  /* save activation (only if needed... */
444
 
445
  kern_gettime(&t);
446
 
447
  RMSTAR_internal_activate(lev,p, &t);
448
 
449
  /* Set the deadline timer */
450
  lev->deadline_timer[p] = kern_event_post(&lev->deadline_timespec[p],
451
                                           RMSTAR_timer_deadline,
452
                                           (void *)p);
453
 
454
}
455
 
456
static void RMSTAR_public_unblock(LEVEL l, PID p)
457
{
458
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
459
 
460
#ifdef RMSTAR_DEBUG
461
  rmstar_printf("(E:ins)");
462
#endif
463
 
464
  /* Insert task in the correct position */
465
  proc_table[p].status = RMSTAR_READY;
466
  iq_priority_insert(p,&lev->ready);
467
 
468
  /* and check for preemption! */
469
  RMSTAR_check_preemption(lev);
470
}
471
 
472
static void RMSTAR_public_block(LEVEL l, PID p)
473
{
474
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
475
 
476
#ifdef RMSTAR_DEBUG
477
  rmstar_printf("(E:ext)");
478
#endif
479
 
480
  /* the task is blocked on a synchronization primitive. we have to
481
     remove it from the master module -and- from the local queue! */
482
  iq_extract(p,&lev->ready);
483
 
484
  /* and finally, a preemption check! (it will also call guest_end) */
485
  RMSTAR_check_preemption(lev);
486
}
487
 
488
static int RMSTAR_public_message(LEVEL l, PID p, void *m)
489
{
490
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
491
  struct timespec temp;
492
#ifdef RMSTAR_DEBUG
493
  rmstar_printf("(E:ecy ");
494
#endif
978 trimarchi 495
  STD_command_message *msg;
496
  HARD_TASK_MODEL *h;
235 giacomo 497
 
498
  switch ((long)(m))
499
    {
500
 
501
      /* Task EndCycle */
502
    case (long)(NULL):
503
 
978 trimarchi 504
      if (RMSTAR_private_change_level(l,p)) return 0;
505
 
506
      if (proc_table[p].avail_time > 0) RMSTAR_account_capacity(lev,p);
507
 
508
 
235 giacomo 509
      /* we call guest_end directly here because the same task may
510
         be reinserted in the queue before calling the preemption check! */
511
      level_table[ lev->scheduling_level ]->
512
        private_extract(lev->scheduling_level,p);  lev->activated = NIL;
513
 
514
      iq_extract(p,&lev->ready);
515
 
516
      /* we reset the capacity counters... */
517
      proc_table[p].avail_time = proc_table[p].wcet;
518
 
519
      if (lev->nact[p] > 0) {
520
#ifdef RMSTAR_DEBUG
978 trimarchi 521
        rmstar_printf2("RM%d",p);
235 giacomo 522
#endif
523
 
524
        /* Pending activation: reactivate the thread!!! */
525
        lev->nact[p]--;
526
 
527
        /* see also RMSTAR_timer_deadline */
528
        kern_gettime(&temp);
529
 
530
        RMSTAR_internal_activate(lev,p,&temp);
531
 
532
        /* check if the deadline has already expired */
533
        temp = *iq_query_timespec(p, &lev->ready);
534
        if (TIMESPEC_A_LT_B(&temp, &schedule_time)) {
535
          /* count the deadline miss */
536
          lev->dline_miss[p]++;
537
          kern_event_delete(lev->deadline_timer[p]);
799 giacomo 538
          lev->deadline_timer[p] = NIL;
235 giacomo 539
        }
540
 
541
      }
542
      else {
543
#ifdef RMSTAR_DEBUG
544
        rmstar_printf("e%d",p);
545
#endif
799 giacomo 546
        if (lev->flag[p] & RMSTAR_FLAG_SPORADIC && lev->deadline_timer[p] != NIL) {
394 trimarchi 547
            kern_event_delete(lev->deadline_timer[p]);
799 giacomo 548
            lev->deadline_timer[p] = NIL;
978 trimarchi 549
#ifdef RMSTAR_DEBUG
550
            rmstar_printf("deaddelete%d",p);
551
#endif      
799 giacomo 552
        }
394 trimarchi 553
 
235 giacomo 554
        /* the task has terminated his job before it consume the wcet. All OK! */
799 giacomo 555
        if (lev->flag[p] & RMSTAR_FLAG_SPORADIC)
556
                proc_table[p].status = SLEEP;
557
         else
558
                proc_table[p].status = RMSTAR_IDLE;
235 giacomo 559
 
560
        /* and finally, a preemption check! */
561
        RMSTAR_check_preemption(lev);
562
 
563
        /* when the deadline timer fire, it recognize the situation and set
564
           correctly all the stuffs (like reactivation, etc... ) */
565
      }
566
#ifdef RMSTAR_DEBUG
567
      rmstar_printf(")");
568
#endif
783 giacomo 569
      TRACER_LOGEVENT(FTrace_EVT_task_end_cycle,proc_table[p].context,proc_table[p].task_level);      
235 giacomo 570
      jet_update_endcycle(); /* Update the Jet data... */
571
      break;
572
    default:
978 trimarchi 573
      msg = (STD_command_message *)m;
574
 
575
#ifdef RMSTAR_DEBUG
576
      rmstar_printf("(RM:MSG %d)",msg->command);
577
#endif   
578
 
579
    switch(msg->command) {
580
    case STD_SET_NEW_MODEL:
581
      /* if the RMSTAR_task_create is called, then the pclass must be a
582
         valid pclass. */
583
      h=(HARD_TASK_MODEL *)(msg->param);
584
 
585
      /* now we know that m is a valid model */
586
      lev->wcet[p] = h->wcet;
587
      lev->period[p] = h->mit;
235 giacomo 588
 
978 trimarchi 589
#ifdef RMSTAR_DEBUG      
590
      kern_printf("(RM:NM p%d w%d m%d)", p, h->wcet, h->mit);
591
#endif       
592
      lev->flag[p] = 0;
593
      lev->deadline_timer[p] = -1;
594
      lev->dline_miss[p]     = 0;
595
      lev->wcet_miss[p]      = 0;
596
      lev->nact[p]           = 0;
597
 
235 giacomo 598
      break;
978 trimarchi 599
 
600
    case STD_SET_NEW_LEVEL:
235 giacomo 601
 
978 trimarchi 602
      lev->flag[p] |= RMSTAR_CHANGE_LEVEL;
603
      lev->new_level[p] = (int)(msg->param);
604
 
605
      break;
606
 
607
    case STD_ACTIVATE_TASK:
608
#ifdef RMSTAR_DEBUG
609
      kern_printf("(RM:SA)");
610
#endif       
611
      /* Enable wcet check */
612
      proc_table[p].avail_time = lev->wcet[p];
613
      proc_table[p].wcet       = lev->wcet[p];
614
      proc_table[p].control &= ~CONTROL_CAP;
615
 
616
      RMSTAR_public_activate(l, p,NULL);
617
 
618
      break;
619
 
620
 
235 giacomo 621
    }
978 trimarchi 622
    break;
623
 
624
    }
235 giacomo 625
  return 0;
626
}
627
 
628
static void RMSTAR_public_end(LEVEL l, PID p)
629
{
630
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
631
 
632
#ifdef RMSTAR_DEBUG
633
  rmstar_printf("(E:end)");
634
#endif
635
 
636
  iq_extract(p,&lev->ready);
981 trimarchi 637
  level_table[ lev->scheduling_level ]->
638
    private_extract(lev->scheduling_level, p);
235 giacomo 639
 
640
  /* we finally put the task in the ready queue */
641
  proc_table[p].status = FREE;
642
 
643
  iq_insertfirst(p,&freedesc);
644
 
645
  if (lev->deadline_timer[p] != -1) {
646
    kern_event_delete(lev->deadline_timer[p]);
799 giacomo 647
    lev->deadline_timer[p] = NIL;
235 giacomo 648
  }
649
 
650
  /* and finally, a preemption check! (it will also call guest_end) */
651
  RMSTAR_check_preemption(lev);
652
}
653
 
654
 
655
/* Guest Functions
656
   These functions manages a JOB_TASK_MODEL, that is used to put
657
   a guest task in the RMSTAR ready queue. */
658
 
659
static void RMSTAR_private_insert(LEVEL l, PID p, TASK_MODEL *m)
660
{
661
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
662
  JOB_TASK_MODEL *job;
663
 
664
  if (m->pclass != JOB_PCLASS || (m->level != 0 && m->level != l) ) {
665
    kern_raise(XINVALID_TASK, p);
666
    return;
667
  }
668
 
669
  job = (JOB_TASK_MODEL *)m;
670
 
671
  *iq_query_timespec(p, &lev->ready) = job->deadline;
672
 
673
  lev->deadline_timer[p] = -1;
674
  lev->dline_miss[p]     = 0;
675
  lev->wcet_miss[p]      = 0;
676
  lev->nact[p]           = 0;
677
 
678
  if (job->noraiseexc)
978 trimarchi 679
    lev->flag[p] |= RMSTAR_FLAG_NORAISEEXC;
235 giacomo 680
  else {
978 trimarchi 681
    lev->flag[p] &= ~RMSTAR_FLAG_NORAISEEXC;
235 giacomo 682
    lev->deadline_timer[p] = kern_event_post(iq_query_timespec(p, &lev->ready),
683
                                             RMSTAR_timer_guest_deadline,
684
                                             (void *)p);
685
   }
686
 
687
  lev->period[p] = job->period;
688
  *iq_query_priority(p, &lev->ready) = job->period;
689
 
690
  /* there is no bandwidth guarantee at this level, it is performed
691
     by the level that inserts guest tasks... */
692
 
693
  /* Insert task in the correct position */
694
  iq_priority_insert(p,&lev->ready);
695
  proc_table[p].status = RMSTAR_READY;
696
 
697
  /* check for preemption */
698
  RMSTAR_check_preemption(lev);
699
}
700
 
701
 
702
static void RMSTAR_private_dispatch(LEVEL l, PID p, int nostop)
703
{
704
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
705
 
706
  level_table[ lev->scheduling_level ]->
707
    private_dispatch(lev->scheduling_level,p,nostop);
708
}
709
 
710
static void RMSTAR_private_epilogue(LEVEL l, PID p)
711
{
712
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
713
 
714
  /* the task has been preempted. it returns into the ready queue... */
715
  level_table[ lev->scheduling_level ]->
716
    private_epilogue(lev->scheduling_level,p);
717
 
718
  proc_table[p].status = RMSTAR_READY;
719
}
720
 
721
static void RMSTAR_private_extract(LEVEL l, PID p)
722
{
723
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
724
 
725
#ifdef RMSTAR_DEBUG
978 trimarchi 726
  kern_printf("RMSTAR_guest_end: dline timer %d\n",lev->deadline_timer[p]);
235 giacomo 727
#endif
728
 
729
  iq_extract(p, &lev->ready);
730
 
731
  /* we remove the deadline timer, because the slice is finished */
732
  if (lev->deadline_timer[p] != NIL) {
733
#ifdef RMSTAR_DEBUG
978 trimarchi 734
    kern_printf("RMSTAR_guest_end: dline timer %d\n",lev->deadline_timer[p]);
235 giacomo 735
#endif
736
    kern_event_delete(lev->deadline_timer[p]);
737
    lev->deadline_timer[p] = NIL;
738
  }
739
 
740
  /* and finally, a preemption check! (it will also call guest_end() */
741
  RMSTAR_check_preemption(lev);
742
}
743
 
744
/* Registration functions */
745
 
746
/* Registration function:
747
    int flags                 the init flags ... see RMSTAR.h */
748
LEVEL RMSTAR_register_level(int master)
749
{
750
  LEVEL l;            /* the level that we register */
751
  RMSTAR_level_des *lev;  /* for readableness only */
752
  PID i;              /* a counter */
753
 
754
#ifdef RMSTAR_DEBUG
755
  printk("RMSTAR_register_level\n");
756
#endif
757
 
758
  /* request an entry in the level_table */
759
  l = level_alloc_descriptor(sizeof(RMSTAR_level_des));
760
 
761
  lev = (RMSTAR_level_des *)level_table[l];
762
 
763
  /* fill the standard descriptor */
764
  lev->l.private_insert   = RMSTAR_private_insert;
765
  lev->l.private_extract  = RMSTAR_private_extract;
766
  lev->l.private_dispatch = RMSTAR_private_dispatch;
767
  lev->l.private_epilogue = RMSTAR_private_epilogue;
768
 
769
  lev->l.public_guarantee = NULL;
770
  lev->l.public_create    = RMSTAR_public_create;
771
  lev->l.public_end       = RMSTAR_public_end;
772
  lev->l.public_dispatch  = RMSTAR_public_dispatch;
773
  lev->l.public_epilogue  = RMSTAR_public_epilogue;
774
  lev->l.public_activate  = RMSTAR_public_activate;
775
  lev->l.public_unblock   = RMSTAR_public_unblock;
776
  lev->l.public_block     = RMSTAR_public_block;
777
  lev->l.public_message   = RMSTAR_public_message;
778
 
779
  /* fill the RMSTAR descriptor part */
780
  for(i=0; i<MAX_PROC; i++) {
781
    lev->period[i]         = 0;
782
    lev->deadline_timer[i] = -1;
783
    lev->flag[i]           = 0;
784
    lev->dline_miss[i]     = 0;
785
    lev->wcet_miss[i]      = 0;
786
    lev->nact[i]           = 0;
787
    lev->budget[i]         = NIL;
978 trimarchi 788
    lev->new_level[i]      = -1;
235 giacomo 789
  }
790
 
791
  iq_init(&lev->ready, NULL, 0);
792
  lev->activated = NIL;
793
 
794
  lev->scheduling_level = master;
795
 
394 trimarchi 796
  lev->cap_lev = NIL;
797
  NULL_TIMESPEC(&lev->cap_lasttime);
798
 
235 giacomo 799
  return l;
800
}
801
 
802
int RMSTAR_get_dline_miss(PID p)
803
{
804
  LEVEL l = proc_table[p].task_level;
805
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
806
 
807
  return lev->dline_miss[p];
808
}
809
 
810
int RMSTAR_get_wcet_miss(PID p)
811
{
812
  LEVEL l = proc_table[p].task_level;
813
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
814
 
815
  return lev->wcet_miss[p];
816
}
817
 
818
int RMSTAR_get_nact(PID p)
819
{
820
  LEVEL l = proc_table[p].task_level;
821
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
822
 
823
  return lev->nact[p];
824
}
825
 
826
int RMSTAR_reset_dline_miss(PID p)
827
{
828
  LEVEL l = proc_table[p].task_level;
829
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
830
 
831
  lev->dline_miss[p] = 0;
832
  return 0;
833
}
834
 
835
int RMSTAR_reset_wcet_miss(PID p)
836
{
837
  LEVEL l = proc_table[p].task_level;
838
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
839
 
840
  lev->wcet_miss[p] = 0;
841
  return 0;
842
}
843
 
844
 
845
int RMSTAR_setbudget(LEVEL l, PID p, int budget)
846
{
847
 
848
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
849
 
850
  lev->budget[p] = budget;
851
 
852
  return 0;
853
 
854
}
855
 
856
int RMSTAR_getbudget(LEVEL l, PID p)
857
{
858
 
859
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
860
 
861
  return lev->budget[p];
862
 
863
}
864
 
865
int RMSTAR_budget_has_thread(LEVEL l, int budget)
866
{
867
 
868
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
869
  int i;
870
 
871
  for(i = 0; i< MAX_PROC; i++)
872
    if (lev->budget[i] == budget) return 1;
873
 
874
  return 0;
875
 
876
}
813 trimarchi 877
 
878
 
879
void RMSTAR_set_nopreemtive_current(LEVEL l) {
880
 
881
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
882
 
883
  lev->flag[lev->activated]|=RMSTAR_FLAG_NOPREEMPT;
884
}
885
 
818 trimarchi 886
void RMSTAR_unset_nopreemtive_current(LEVEL l) {
813 trimarchi 887
 
888
  RMSTAR_level_des *lev = (RMSTAR_level_des *)(level_table[l]);
889
 
890
  lev->flag[lev->activated]&=~RMSTAR_FLAG_NOPREEMPT;
891
}
892