Subversion Repositories shark

Rev

Rev 567 | Rev 620 | 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
 *   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
 ------------
571 giacomo 24
 CVS :        $Id: func.h,v 1.13 2004-04-19 15:25:38 giacomo Exp $
2 pj 25
 
26
 File:        $File$
571 giacomo 27
 Revision:    $Revision: 1.13 $
28
 Last update: $Date: 2004-04-19 15:25:38 $
2 pj 29
 ------------
30
 
31
Kernel functions:
32
 
33
- Debug stuff
34
 
35
- Primitives called at initialization time
36
 
37
- Kernel global functions (scheduler, queues)
38
 
39
- Kernel VM hooks
40
 
41
- IRQ, errors & exception handling
42
 
43
- System management primitives
44
 
45
- Jet management primitives
46
 
47
- Task management primitives
48
 
49
- Mutex primitives
50
 
51
**/
52
 
53
/*
54
 * Copyright (C) 2000 Paolo Gai
55
 *
56
 * This program is free software; you can redistribute it and/or modify
57
 * it under the terms of the GNU General Public License as published by
58
 * the Free Software Foundation; either version 2 of the License, or
59
 * (at your option) any later version.
60
 *
61
 * This program is distributed in the hope that it will be useful,
62
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
63
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
64
 * GNU General Public License for more details.
65
 *
66
 * You should have received a copy of the GNU General Public License
67
 * along with this program; if not, write to the Free Software
68
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
69
 *
70
 */
71
 
72
#ifndef __KERNEL_FUNC_H__
73
#define __KERNEL_FUNC_H__
74
 
75
#include <ll/ll.h>
76
#include <ll/stdio.h>
77
#include <kernel/types.h>
78
#include <kernel/model.h>
79
#include <kernel/mem.h>
80
#include <signal.h>
81
#include <kernel/var.h>
82
#include <errno.h>
83
 
84
 
85
/*---------------------------------------------------------------------*/
86
/* Debug stuff                                                         */
87
/*---------------------------------------------------------------------*/
88
 
89
/* if a source use printk() it should include log.h not func.h */
90
#include <kernel/log.h>
91
 
79 pj 92
#include "ll/sys/cdefs.h"
93
 
94
__BEGIN_DECLS
95
 
2 pj 96
/*---------------------------------------------------------------------*/
97
/* Kernel global functions: initialization & termination...            */
98
/*---------------------------------------------------------------------*/
99
 
100
/*+ this function is called by __kernel_init__.
101
    It register the modules in the system at init time +*/
102
TIME __kernel_register_levels__(void *arg);
103
 
104
/*+ This function returns a level_des **. the value returned shall be
38 pj 105
    used to register a level module.
2 pj 106
 
38 pj 107
    The function is usually called at module registration time.  The
108
    function can also be called when the system is already started, to
109
    allow the implementation of dynamic module registration.  
110
 
111
    The argument must be the size of the data block that have to be allocated
112
 
113
    The function returns the number of the descriptor allocated for the module
114
    or -1 in case there are no free descriptors.
115
 
116
    The function also reserves a descriptor with size s, initialized
117
    with default function pointers.
118
 
119
+*/
120
LEVEL level_alloc_descriptor(size_t s);
121
 
122
/*+ This function release a level descriptor previously allocated using
123
  level_alloc_descriptor().
124
 
125
  The function returns 0 if the level has been freed, or -1 if someone is
126
  using it, -2 if the level has never been registered.
127
 
128
+*/
129
int level_free_descriptor(LEVEL l);
130
 
131
/* Call this if you want to say that your module is using module l
132
   (e.g., for calling its private functions) */
133
int level_use_descriptor(LEVEL l);
134
 
135
/* Call this when you no more need the module l */
136
int level_unuse_descriptor(LEVEL l);
137
 
2 pj 138
/*+ This function returns a resource_des **. the value returned shall be
139
    used to register a resource module. The function shall be called only at
140
    module registration time. It assume that the system is not yet
141
    initialized, so we shall not call sys_abort...                     +*/
142
RLEVEL resource_alloc_descriptor();
143
 
144
/*+ This function compute the command line parameters from the multiboot_info
145
    NOTE: this function modify the multiboot struct, so this function and
146
    __call_main__ are mutually exclusives!!! +*/
147
void __compute_args__(struct multiboot_info *mbi, int *_argc, char **_argv);
148
 
149
/*+ This function calls the standard C main() function, with a
150
    parameter list up to 100 parameters                        +*/
151
int __call_main__(struct multiboot_info *mb);
152
 
153
/*+ This task initialize the system and calls the main() with
154
    __call_mail__ .
155
    It should be created by a level registered in the system by
156
    __kernel_register_levels__ +*/
157
TASK __init__(void *arg);
158
 
159
/*+ Use this function to post your own exit operations
160
    (when uses some defines contained in const.h) +*/
161
int sys_atrunlevel(void (*func_code)(void *),void *parm, BYTE when);
162
 
163
/*---------------------------------------------------------------------*/
29 pj 164
/* Kernel global functions: scheduler,                                 */
2 pj 165
/*---------------------------------------------------------------------*/
166
 
167
/*+ This is the generic scheduler.
168
    The scheduler calls the appropriates level schedulers and then
169
    dispatch the task chosen. +*/
170
void scheduler(void);
171
 
172
/*+ called in the events to force the system to execute the scheduler at
173
    the end of an event list +*/
174
void event_need_reschedule();
175
 
176
void task_makefree(void *ret);
177
void check_killed_async(void);
178
 
179
int guarantee();
180
 
38 pj 181
void levels_init(void); /* see init.c */
182
 
2 pj 183
void runlevel_init();
184
void call_runlevel_func(int runlevel, int aborting);
185
 
186
// in kill.c
187
void kill_user_tasks();
188
 
189
void event_resetepilogue();
190
 
191
 
192
void call_task_specific_data_destructors();
193
void task_specific_data_init();
194
 
195
// in kill.c
196
void register_cancellation_point(int (*func)(PID p, void *arg), void *arg);
197
 
198
// in signal.c
199
void register_interruptable_point(int (*func)(PID p, void *arg), void *arg);
200
 
201
 
202
/*---------------------------------------------------------------------*/
203
/* Kernel VM hooks                                                     */
204
/*---------------------------------------------------------------------*/
205
 
206
/* this hooks redefines the VM functions to use them into the kernel...
207
 
208
   the only VM functions called directly from the kernel are VM_init and
209
   VM_end
210
*/
211
 
212
/* Context management routines */
213
#define kern_context_create    ll_context_create
214
#define kern_context_delete    ll_context_delete
215
 
216
extern __inline__ CONTEXT kern_context_save()
217
{
218
  cli();
219
  return ll_context_from();
220
}
221
 
222
/*+ this functions are called every time a context is changed +*/
223
void kern_after_dispatch(void);
224
 
225
/* Warning: if modified, modify also:
226
   - task_join
227
   - cond_wait
228
   - cond_timedwait
229
   - internal_sem_wait
230
*/
82 pj 231
extern __inline__ void kern_context_load(CONTEXT c)
2 pj 232
{
233
  ll_context_to(c);
234
  kern_after_dispatch();
235
  sti();
236
}
237
 
238
 
239
/* Interrupt enabling/disabling */
240
#define kern_cli          cli
241
#define kern_sti          sti
242
 
243
/* Interrupt enabling/disabling with flag save */
244
#define kern_fsave        ll_fsave
245
#define kern_frestore     ll_frestore
246
 
247
/* interrupt handling */
248
#define kern_irq_unmask   VM_irq_unmask
249
#define kern_irq_mask     VM_irq_mask
250
 
251
extern __inline__ int kern_event_post(const struct timespec *time,
252
                                      void (*handler)(void *p),
253
                                      void *par)
254
{
255
  int e;
256
  e = event_post(*time,handler,par);
257
 
258
  if (e == -1)
259
    kern_raise(XNOMORE_EVENTS,exec_shadow);
260
 
261
  return e;
262
}
263
 
38 pj 264
#define kern_event_delete event_delete
265
 
2 pj 266
/*+ the default capacity timer used by the kernel... +*/
267
void capacity_timer(void *arg);
268
 
269
 
270
#define kern_printf message
271
 
38 pj 272
extern __inline__ TIME kern_gettime(struct timespec *t)
273
{
45 pj 274
  return ll_gettime(TIME_NEW, t);
38 pj 275
}
276
 
277
 
278
 
2 pj 279
/*---------------------------------------------------------------------*/
38 pj 280
/* Kernel global functions: IRQ handling                               */
2 pj 281
/*---------------------------------------------------------------------*/
282
 
283
/*+ Interrupt handler installation +*/
496 giacomo 284
int handler_set(int no, void (*fast)(int), PID pi, BYTE lock);
2 pj 285
 
286
/*+ Interrupt handler removal      +*/
287
int handler_remove(int no);
288
 
289
 
290
/*---------------------------------------------------------------------*/
291
/* System management primitives                                        */
292
/*---------------------------------------------------------------------*/
293
 
294
/*+ Close the system & return to HOST OS.
295
    Can be called from all the tasks...
296
    The first time it is called it jumps to the global context
297
    The second time it jumps only if there are no system task remaining
298
    The error code passed is 0... (it is saved on the first call!!!) +*/
299
void sys_end(void);
300
 
301
/*+ Close the system & return to HOST OS.
302
    Can be called from all the tasks...
303
    The first time it is called it works as the sys_end
304
    The second time it jumps every time
305
    The error code passed is 0... +*/
306
void sys_abort(int err);
307
 
571 giacomo 308
/*+ As sys_abort, but it works when the system is
309
    in shutdown mode  +*/
310
void sys_abort_shutdown(int err);
311
 
2 pj 312
/* The system implements also exit and _exit as a redefinition of sys_end
313
   This is not the correct behaviour, and should be fixed.
314
   The definitions for these functions are in kernel/kern.c
315
*/
316
 
317
/*+ Print a message then call sys_abort(333).
318
    Can be called from all the tasks...  +*/
319
void sys_panic(const char * fmt, ...) __attribute__ ((format (printf, 1, 2)));
320
 
321
/*+ prints an error message (see perror.c) +*/
322
void perror (const char *s);
323
 
324
/*+ this primitive returns the time read from the system timer +*/
325
TIME sys_gettime(struct timespec *t);
326
 
158 pj 327
/*+ this primitive can be used to set a message that will be printed
328
    at shutdown +*/
329
int sys_shutdown_message(char *fmt,...);
330
 
2 pj 331
/*---------------------------------------------------------------------*/
332
/* Jet management primitives                                           */
333
/*---------------------------------------------------------------------*/
334
 
335
/*+ This primitive returns the maximum execution time and the total
336
    execution time from the task_create or the last jet_delstat
337
    It returns also the number of instances to use to calculate the mean
338
    time and the current job execution time.
339
    The value returned is 0 if all ok, -1 if the PID is not correct or
340
    the task doesn't have the JET_ENABLE bit set.
341
+*/
342
int jet_getstat(PID p, TIME *sum, TIME *max, int *n, TIME *curr);
343
 
344
/*+ This primitive reset to 0 the maximum execution time and the mean
345
    execution time of the task p, and reset to 0 all the entries in
346
    jet_table.
347
    The value returned is 0 if all ok, -1 if the PID is not correct or
348
    the task doesn't have the JET_ENABLE bit set.                     +*/
349
int jet_delstat(PID p);
350
 
351
/*+ This primitive returns the last n values of the task execution time
352
    recorded after the last call to jet_gettable or jet_delstat.
353
    If n is
354
    <0 it will be set only the last values inserted in the table
355
       since the last call of jet_gettable.
356
    >0 it will be set up to JET_TABLE_DIM datas.
357
 
358
    The value returned is -1 if the PID is not correct or
359
    the task doesn't have the JET_ENABLE bit set, otherwise it returns the
360
    number of values set in the parameter table.
361
    (can be from 0 to JET_TABLE_DIM-1)
362
+*/
363
int jet_gettable(PID p, TIME *table, int n);
364
 
365
/*+ This function updates the jet information. +*/
366
void jet_update_slice(TIME t);
367
 
368
/*+ This function updates the jet information at the task end period
369
    it is called in task_endcycle and task_sleep +*/
370
void jet_update_endcycle();
371
 
372
/*---------------------------------------------------------------------*/
38 pj 373
/* Internal Macros                                                     */
374
/*---------------------------------------------------------------------*/
375
 
376
extern __inline__ void kern_epilogue_macro(void)
377
{
378
  TIME tx;    /* a dummy used for time computation             */
379
  struct timespec ty; /* a dummy used for time computation     */
380
 
381
  kern_gettime(&schedule_time);
382
 
383
  /* manage the capacity event */
384
  SUBTIMESPEC(&schedule_time, &cap_lasttime, &ty);
385
  tx = TIMESPEC2USEC(&ty);
393 giacomo 386
  if (proc_table[exec_shadow].control & CONTROL_CAP) proc_table[exec_shadow].avail_time -= tx;
38 pj 387
  jet_update_slice(tx);
388
 
389
  /* if the event didn't fire before, we delete it. */
390
  if (cap_timer != NIL) {
391
    kern_event_delete(cap_timer);
392
    cap_timer = NIL;
393
  }
394
}
395
 
396
/* This function is called by the kernel into kern.c to register a default
397
   exception handler */
398
int set_default_exception_handler(void);
567 giacomo 399
int remove_default_exception_handler(void);
38 pj 400
 
401
/*---------------------------------------------------------------------*/
2 pj 402
/* Task management primitives                                          */
403
/*---------------------------------------------------------------------*/
404
 
405
 
406
/*+ This primitive:
407
    - Reserve a task descriptor
408
    - Create the task based on the task model passed
409
    - Initialize the resources used by the task
410
    - Guarantees the task set
411
+*/
412
PID task_createn(char *name,      /*+ the symbolic name of the task +*/
413
                 TASK (*body)(),  /*+ a pointer to the task body    +*/
414
                 TASK_MODEL *m,   /*+ the task model                +*/
415
                                  /*+ the resources models, a list  +*/
416
                 ...);            /*+ of models terminated by NULL  +*/
417
 
418
 
419
/*+ a redefinition of task_createn +*/
420
extern __inline PID task_create(char *name, TASK (*body)(),
421
                                void *m, void *r)
422
{
423
   return task_createn(name, body, (TASK_MODEL *)m, (RES_MODEL *)r, NULL);
424
}
425
 
426
 
427
/* This function allow to create a set of tasks without guarantee.
428
   It must be called with interrupts disabled and it must be used with
429
   group_create_accept and group_create_reject.
430
 
431
   This function allocates a task descriptor and fills it.
432
   After that, the guarantee() function should be called to check for task(s)
433
   admission.
434
   Next, each task created with group_create must be accepted with a call to
435
   group_create_accept() or rejected with a call to group_create_reject.
436
 
437
   The function returns the PID of the allocated descriptor, or NIL(-1)
438
   if the descriptor cannot be allocated or some problems arises the creation.
439
   If -1 is returned, errno is set to a value that represent the error:
440
      ENO_AVAIL_TASK       -> no free descriptors available
441
      ENO_AVAIL_SCHEDLEVEL -> there were no scheduling modules that can handle
442
                              the TASK_MODEL *m passed as parameter
443
      ETASK_CREATE         -> there was an error during the creation of the
444
                              task into the scheduling module
445
      ENO_AVAIL_RESLEVEL   -> there were no resource modules that can handle
446
                              one of the RES_MODEL * passed as parameter
447
*/
448
PID group_create(char *name,
449
                 TASK (*body)(),
450
                 TASK_MODEL *m,
451
                 ...);
452
 
453
 
454
/*
455
  This function should be called when a task created with group_create
456
  is successfully guaranteed and accepted in the system.
457
  This function finish the creation process allocating the last resources
458
  for the task (i.e., the stack and the context).
459
  it returns:
460
 
461
  -1 if something goes wrong. In this case, THE TASK IS AUTOMATICALLY REJECTED
462
     AND THE GROUP_CREATE_REJECT MUST NOT BE CALLED.
463
     errno is set to a value that explains the problem occurred:
464
 
465
     ENO_AVAIL_STACK_MEM -> No stack memory available for the task
466
     ENO_AVAIL_TSS       -> No context available for the task (This is a
467
                            CRITICAL error, and usually never happens...)
468
*/
469
int group_create_accept(PID i, TASK_MODEL *m);
470
 
471
/*
472
  This function should be called when a task created with group_create
473
  can not be successfully guaranteed.
474
  This function reject the task from the system.
475
  You cannot use the PID of a rejected task after this call.
476
*/
477
void group_create_reject(PID i);
478
 
479
/*+
480
  It blocks all explicit activation of a task made with task_activate and
481
  group_activate. These activations are registered in an internal counter,
482
  returned by task_unblock_activation.
483
  it returns 0 if all ok, or -1 otherwise. errno is set accordingly.
484
+*/
485
int task_block_activation(PID p);
486
 
487
/*+
488
  It unblocks all explicit activations of a task, and returns the number of
489
  "frozen" activations. It not call the task_activate!!!!
490
  it returns -1 if an error occurs. errno is set accordingly.
491
+*/
492
int task_unblock_activation(PID p);
493
 
494
 
495
/*+ Activate a task specified via pid returned from task_create +*/
496
int task_activate(PID pid);
497
 
498
/*+ Kill a task specified via pid returned from task_create +*/
499
int task_kill(PID pid);
500
 
501
/*+
502
  This primitive autokills the excuting task; it was used to avoid
503
  that returning from a task cause a jmp to an unpredictable location.
504
 
505
  Now it is obsolete, the task_create_stub do all the works.
506
 
507
  It is used by the Posix layer to implement pthread_exit
508
+*/
509
void task_abort(void *returnvalue);
510
 
511
/*+ Creates a cancellation point in the calling task +*/
512
void task_testcancel(void);
513
 
514
/*+ Set the cancellation state of the task +*/
515
int task_setcancelstate(int state, int *oldstate);
516
 
517
/*+ Set the cancellation type of the task +*/
518
int task_setcanceltype(int type, int *oldtype);
519
 
520
/*+ this function suspends execution of the calling task until the target
521
    task terminates, unless the target task has already terminated.
522
    It works like the pthread_join +*/
523
int task_join(PID p, void **value);
524
 
525
/*+ this function set the detach state of a task to joinable. This function
526
    is not present in Posix standard...
527
    returns ESRCH if p is non correct +*/
528
int task_joinable(PID p);
529
 
530
/*+ this function set the detach state of a task to detached. This function
531
    works as the posix's pthread_detach
532
    returns EINVAL if p can't be joined (or currently a task has done a
533
    join on it (condition not provided in posix)
534
    ESRCH if p is not correct +*/
535
int task_unjoinable(PID p);
536
 
537
/*+ Disable the preemption mechanism on the task.
538
    This primitive is very dangerous!!!!         +*/
539
void task_nopreempt(void);
540
 
541
/*+ Enable the preemption mechanism on the task. +*/
542
void task_preempt(void);
543
 
38 pj 544
/*+ sends a message to the scheduling module that is handling the task +*/
208 giacomo 545
int task_message(void *m, PID p, int reschedule);
38 pj 546
 
2 pj 547
/*+ This function signals to the kernel that the current istance of
548
    the task (periodic or aperiodic) is ended; so the task can be
549
    suspended until it is activated again. Pending activations may be saved
550
    depending on the task model +*/
38 pj 551
extern __inline__ void task_endcycle(void)
552
{
210 giacomo 553
  task_message(NULL, NIL, 1);
38 pj 554
}
2 pj 555
 
208 giacomo 556
/*+ This function signals to the kernel that the current istance of
557
    the task (periodic or aperiodic) is ended; so the task can be
558
    suspended until it is activated again. Pending activations may be saved
559
    depending on the task model +*/
560
extern __inline__ void task_disable(PID p)
561
{
562
  task_message((void *)(1), p, 0);
563
}
564
 
2 pj 565
/*+ This primitives refers the group id which is supplied
566
    by the application, not by the kernel                 +*/
567
int group_activate(WORD g);
568
int group_kill(WORD g);
569
 
570
 
571
/*---------------------------------------------------------------------*/
572
/* Mutex primitives                                                    */
573
/*---------------------------------------------------------------------*/
574
 
575
/* This primitives manages a mutex in the system.
576
   The behavior of the functions is similar to the POSIX ones. */
577
 
578
 
579
/*+ Alloc a mutex descriptor +*/
580
int mutex_init(mutex_t *mutex, const mutexattr_t *attr);
581
 
582
/*+ Free a mutex descriptor  +*/
583
int mutex_destroy(mutex_t *mutex);
584
 
585
/*+ Block wait               +*/
586
int mutex_lock(mutex_t *mutex);
587
 
588
/*+ Non-block wait           +*/
589
int mutex_trylock(mutex_t *mutex);
590
 
591
/*+ unlock primitive +*/
592
int mutex_unlock(mutex_t *mutex);
593
 
594
/*---------------------------------------------------------------------*/
595
/* Condition variables                                                 */
596
/*---------------------------------------------------------------------*/
597
 
598
/*+ Initialization of the condition variable +*/
599
int cond_init(cond_t *cond);
600
 
601
/*+ free a condition variable descriptor +*/
602
int cond_destroy(cond_t *cond);
603
 
604
/*+ signal on a condition variable, it unlocks only one task +*/
605
int cond_signal(cond_t *cond);
606
 
607
/*+ broadcast on a condition variable, it unlocks all the blocked tasks +*/
608
int cond_broadcast(cond_t *cond);
609
 
610
/*+ wait on a condition variable +*/
611
int cond_wait(cond_t *cond, mutex_t *mutex);
612
 
613
/*+ wait on a condition variable (with timer) +*/
614
int cond_timedwait(cond_t *cond, mutex_t *mutex,
615
                   const struct timespec *abstime);
616
 
617
/*---------------------------------------------------------------------*/
618
/* Task specific data primitives                                       */
619
/*---------------------------------------------------------------------*/
620
 
621
/* they are similar to the POSIX standard */
622
 
623
int task_key_create(task_key_t *key, void (*destructor)(void *));
624
void *task_getspecific(task_key_t key);
625
int task_setspecific(task_key_t key, const void *value);
626
int task_key_delete(task_key_t key);
627
 
628
/*---------------------------------------------------------------------*/
629
/* Task cancellation handlers                                          */
630
/*---------------------------------------------------------------------*/
631
 
632
/*+ push the specified cancellation cleanup handler routine onto
633
    the cancellation cleanup stack
634
void task_cleanup_push(void (*routine)(void *), void *arg); +*/
635
#define task_cleanup_push(rtn,arg) { \
636
        struct _task_handler_rec __cleanup_handler, **__head; \
637
        __cleanup_handler.f = rtn; \
638
        __cleanup_handler.a = arg; \
639
        __head = task_getspecific(0); \
640
        __cleanup_handler.next = *__head; \
641
        *__head = &__cleanup_handler;
642
 
643
/*+
644
    removes the routine at the top of the cancellation cleanup stack
645
    of the calling thread
646
void task_cleanup_pop(int execute); +*/
647
#define task_cleanup_pop(ex) \
648
        *__head = __cleanup_handler.next; \
649
        if (ex) (*__cleanup_handler.f) (__cleanup_handler.a); \
650
}
651
 
79 pj 652
__END_DECLS
2 pj 653
#endif /* __FUNC_H__ */