-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathrtai_rtapi.c
More file actions
1752 lines (1608 loc) · 49.7 KB
/
rtai_rtapi.c
File metadata and controls
1752 lines (1608 loc) · 49.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** RTAPI is a library providing a uniform API for several real time
operating systems. As of ver 2.0, RTLinux and RTAI are supported.
*/
/********************************************************************
* Description: rtai_rtapi.c
* Realtime RTAPI implementation for the RTAI platform.
*
* Author: John Kasunich, Paul Corner
* License: GPL Version 2
*
* Copyright (c) 2004 All rights reserved.
*
* Last change:
********************************************************************/
/** This file, 'rtai_rtapi.c', implements the realtime portion of the
API for the RTAI platform. The API is defined in rtapi.h, which
includes documentation for all of the API functions. The non-
real-time portion of the API is implemented in rtai_ulapi.c (for
the RTAI platform). This implementation attempts to prevent
kernel panics, 'oops'es, and other nasty stuff that can happen
when writing and testing realtime code. Wherever possible,
common errors are detected and corrected before they can cause a
crash. This implementation also includes several /proc filesystem
entries and numerous debugging print statements.
*/
/** Copyright (C) 2003 John Kasunich
<jmkasunich AT users DOT sourceforge DOT net>
Copyright (C) 2003 Paul Corner
<paul_c AT users DOT sourceforge DOT net>
This library is based on version 1.0, which was released into
the public domain by its author, Fred Proctor. Thanks Fred!
*/
/* This library is free software; you can redistribute it and/or
modify it under the terms of version 2 of the GNU General Public
License as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/** THE AUTHORS OF THIS LIBRARY ACCEPT ABSOLUTELY NO LIABILITY FOR
ANY HARM OR LOSS RESULTING FROM ITS USE. IT IS _EXTREMELY_ UNWISE
TO RELY ON SOFTWARE ALONE FOR SAFETY. Any machinery capable of
harming persons must have provisions for completely removing power
from all motors, etc, before persons enter any danger area. All
machinery must be designed to comply with local and national safety
codes, and the authors of this software can not, and do not, take
any responsibility for such compliance.
This code was written as part of the EMC HAL project. For more
information, go to www.linuxcnc.org.
*/
#include <stdarg.h> /* va_* */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h> /* replaces malloc.h in recent kernels */
#include <linux/ctype.h> /* isdigit */
#include <linux/uaccess.h> /* copy_from_user() */
#include <linux/version.h> /* LINUX_VERSION_CODE */
#include <asm/msr.h> /* rdtsc_ordered() */
/* get inb(), outb(), ioperm() */
#include <asm/io.h>
#include <linux/cpumask.h> /* NR_CPUS, cpu_online() */
#include "rtapi_vsnprintf.h"
#include <rtai.h>
#include <rtai_sched.h>
#if RTAI > 2
#include <rtai_sem.h>
#endif
#include <rtai_shm.h>
#include <rtai_fifos.h>
#define RTAPI_HAL_PRIV /* Use rtapi/hal private functions */
#include "rtapi.h" /* public RTAPI decls */
#include <rtapi_mutex.h>
#include "rtapi_common.h" /* shared realtime/nonrealtime stuff */
#ifndef RTAI_NR_TRAPS
#define RTAI_NR_TRAPS HAL_NR_FAULTS
#endif
#if RTAI < 5
#define rt_free_timers rt_free_timer
#endif
/* resource data unique to kernel space */
static RT_TASK *ostask_array[RTAPI_MAX_TASKS + 1];
static void *shmem_addr_array[RTAPI_MAX_SHMEMS + 1];
static SEM ossem_array[RTAPI_MAX_SEMS + 1];
#define DEFAULT_MAX_DELAY 10000
static long int max_delay = DEFAULT_MAX_DELAY;
// Actual number of RTAI timer counts of the periodic timer
static unsigned long timer_counts;
/* module parameters */
static int msg_level = RTAPI_MSG_ERR; /* message printing level */
RTAPI_MP_INT(msg_level, "debug message level (default=1)");
/* other module information */
MODULE_AUTHOR("John Kasunich, Fred Proctor, & Paul Corner");
MODULE_DESCRIPTION("Portable Real Time API for RTAI");
MODULE_LICENSE("GPL");
#include "rtapi_proc.h" /* proc filesystem decls & code */
/* the following are internal functions that do the real work associated
with deleting tasks, etc. They do not check the mutex that protects
the internal data structures. When someone calls an rtapi_xxx_delete()
function, the rtapi funct gets the mutex before calling one of these
internal functions. When internal code that already has the mutex
needs to delete something, it calls these functions directly.
*/
static int module_delete(int module_id);
static int task_delete(int task_id);
static int shmem_delete(int shmem_id, int module_id);
static int sem_delete(int sem_id, int module_id);
static int fifo_delete(int fifo_id, int module_id);
static int irq_delete(unsigned int irq_num);
/***********************************************************************
* INIT AND SHUTDOWN FUNCTIONS *
************************************************************************/
int init_module(void)
{
int n;
/* say hello */
rtapi_print_msg(RTAPI_MSG_INFO, "RTAPI: Init\n");
/* get master shared memory block from OS and save its address */
rtapi_data = rtai_kmalloc(RTAPI_KEY, sizeof(rtapi_data_t));
if (rtapi_data == NULL) {
rtapi_print_msg(RTAPI_MSG_ERR,
"RTAPI: ERROR: could not open shared memory\n");
return -ENOMEM;
}
/* perform a global init if needed */
init_rtapi_data(rtapi_data);
/* check revision code */
if (rtapi_data->rev_code != rev_code) {
/* mismatch - release master shared memory block */
rtapi_print_msg(RTAPI_MSG_ERR, "RTAPI: ERROR: version mismatch %d vs %d\n", rtapi_data->rev_code, rev_code);
rtai_kfree(RTAPI_KEY);
return -EINVAL;
}
/* set up local pointers to global data */
module_array = rtapi_data->module_array;
task_array = rtapi_data->task_array;
shmem_array = rtapi_data->shmem_array;
sem_array = rtapi_data->sem_array;
fifo_array = rtapi_data->fifo_array;
irq_array = rtapi_data->irq_array;
/* perform local init */
for (n = 0; n <= RTAPI_MAX_TASKS; n++) {
ostask_array[n] = NULL;
}
for (n = 0; n <= RTAPI_MAX_SHMEMS; n++) {
shmem_addr_array[n] = NULL;
}
rtapi_data->timer_running = 0;
rtapi_data->timer_period = 0;
max_delay = DEFAULT_MAX_DELAY;
rt_linux_use_fpu(1);
/* on SMP machines, we want to put RT code on the last CPU */
n = NR_CPUS-1;
while ( ! cpu_online(n) ) {
n--;
}
rtapi_data->rt_cpu = n;
#ifdef RTAPI_USE_PROCFS
/* set up /proc/rtapi */
if (proc_init() != 0) {
rtapi_print_msg(RTAPI_MSG_WARN,
"RTAPI: WARNING: Could not activate /proc entries\n");
proc_clean();
}
#endif
/* done */
rtapi_print_msg(RTAPI_MSG_INFO, "RTAPI: Init complete\n");
return 0;
}
/* This cleanup code attempts to fix any messes left by modules
that fail to load properly, or fail to clean up after themselves */
void cleanup_module(void)
{
int n;
if (rtapi_data == NULL) {
/* never got inited, nothing to do */
return;
}
/* grab the mutex */
rtapi_mutex_get(&(rtapi_data->mutex));
rtapi_print_msg(RTAPI_MSG_INFO, "RTAPI: Exiting\n");
/* clean up leftover modules (start at 1, we don't use ID 0 */
for (n = 1; n <= RTAPI_MAX_MODULES; n++) {
if (module_array[n].state == REALTIME) {
rtapi_print_msg(RTAPI_MSG_WARN,
"RTAPI: WARNING: module '%s' (ID: %02d) did not call rtapi_exit()\n",
module_array[n].name, n);
module_delete(n);
}
}
/* cleaning up modules should clean up everything, if not there has
probably been an unrecoverable internal error.... */
for (n = 1; n <= RTAPI_MAX_IRQS; n++) {
if (irq_array[n].irq_num != 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"RTAPI: ERROR: interrupt handler %02d not deleted (IRQ %d)\n",
n, irq_array[n].irq_num);
/* probably un-recoverable, but try anyway */
irq_delete(irq_array[n].irq_num);
}
}
for (n = 1; n <= RTAPI_MAX_FIFOS; n++) {
if (fifo_array[n].state != UNUSED) {
rtapi_print_msg(RTAPI_MSG_ERR,
"RTAPI: ERROR: FIFO %02d not deleted\n", n);
}
}
for (n = 1; n <= RTAPI_MAX_SEMS; n++) {
while (sem_array[n].users > 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"RTAPI: ERROR: semaphore %02d not deleted\n", n);
}
}
for (n = 1; n <= RTAPI_MAX_SHMEMS; n++) {
if (shmem_array[n].rtusers > 0) {
rtapi_print_msg(RTAPI_MSG_ERR,
"RTAPI: ERROR: shared memory block %02d not deleted\n", n);
}
}
for (n = 1; n <= RTAPI_MAX_TASKS; n++) {
if (task_array[n].state != EMPTY) {
rtapi_print_msg(RTAPI_MSG_ERR,
"RTAPI: ERROR: task %02d not deleted\n", n);
/* probably un-recoverable, but try anyway */
rtapi_task_pause(n);
task_delete(n);
}
}
if (rtapi_data->timer_running != 0) {
stop_rt_timer();
rt_free_timers();
rtapi_data->timer_period = 0;
timer_counts = 0;
rtapi_data->timer_running = 0;
max_delay = DEFAULT_MAX_DELAY;
}
rtapi_mutex_give(&(rtapi_data->mutex));
#ifdef RTAPI_USE_PROCFS
proc_clean();
#endif
/* release master shared memory block */
rtai_kfree(RTAPI_KEY);
rtapi_print_msg(RTAPI_MSG_INFO, "RTAPI: Exit complete\n");
return;
}
/***********************************************************************
* GENERAL PURPOSE FUNCTIONS *
************************************************************************/
/* all RTAPI init is done when the rtapi kernel module
is insmoded. The rtapi_init() and rtapi_exit() functions
simply register that another module is using the RTAPI.
For other RTOSes, things might be different, especially
if the RTOS does not use modules. */
int rtapi_init(const char *modname)
{
int n, module_id;
module_data *module;
rtapi_print_msg(RTAPI_MSG_DBG, "RTAPI: initing module %s\n", modname);
/* get the mutex */
rtapi_mutex_get(&(rtapi_data->mutex));
/* find empty spot in module array */
n = 1;
while ((n <= RTAPI_MAX_MODULES) && (module_array[n].state != NO_MODULE)) {
n++;
}
if (n > RTAPI_MAX_MODULES) {
/* no room */
rtapi_mutex_give(&(rtapi_data->mutex));
rtapi_print_msg(RTAPI_MSG_ERR, "RTAPI: ERROR: reached module limit %d\n",
n);
return -EMFILE;
}
/* we have space for the module */
module_id = n;
module = &(module_array[n]);
/* update module data */
module->state = REALTIME;
if (modname != NULL) {
/* use name supplied by caller, truncating if needed */
rtapi_snprintf(module->name, RTAPI_NAME_LEN, "%s", modname);
} else {
/* make up a name */
rtapi_snprintf(module->name, RTAPI_NAME_LEN, "RTMOD%03d", module_id);
}
rtapi_data->rt_module_count++;
rtapi_print_msg(RTAPI_MSG_DBG, "RTAPI: module '%s' loaded, ID: %d\n",
module->name, module_id);
rtapi_mutex_give(&(rtapi_data->mutex));
return module_id;
}
int rtapi_exit(int module_id)
{
int retval;
rtapi_mutex_get(&(rtapi_data->mutex));
retval = module_delete(module_id);
rtapi_mutex_give(&(rtapi_data->mutex));
return retval;
}
static int module_delete(int module_id)
{
module_data *module;
char name[RTAPI_NAME_LEN + 1];
int n;
rtapi_print_msg(RTAPI_MSG_DBG, "RTAPI: module %d exiting\n", module_id);
/* validate module ID */
if ((module_id < 1) || (module_id > RTAPI_MAX_MODULES)) {
return -EINVAL;
}
/* point to the module's data */
module = &(module_array[module_id]);
/* check module status */
if (module->state != REALTIME) {
/* not an active realtime module */
return -EINVAL;
}
/* clean up any mess left behind by the module */
for (n = 1; n <= RTAPI_MAX_TASKS; n++) {
if ((task_array[n].state != EMPTY)
&& (task_array[n].owner == module_id)) {
rtapi_print_msg(RTAPI_MSG_WARN,
"RTAPI: WARNING: module '%s' failed to delete task %02d\n",
module->name, n);
task_delete(n);
}
}
for (n = 1; n <= RTAPI_MAX_SHMEMS; n++) {
if (test_bit(module_id, shmem_array[n].bitmap)) {
rtapi_print_msg(RTAPI_MSG_WARN,
"RTAPI: WARNING: module '%s' failed to delete shmem %02d\n",
module->name, n);
shmem_delete(n, module_id);
}
}
for (n = 1; n <= RTAPI_MAX_SEMS; n++) {
if (test_bit(module_id, sem_array[n].bitmap)) {
rtapi_print_msg(RTAPI_MSG_WARN,
"RTAPI: WARNING: module '%s' failed to delete sem %02d\n",
module->name, n);
sem_delete(n, module_id);
}
}
for (n = 1; n <= RTAPI_MAX_FIFOS; n++) {
if ((fifo_array[n].reader == module_id) ||
(fifo_array[n].writer == module_id)) {
rtapi_print_msg(RTAPI_MSG_WARN,
"RTAPI: WARNING: module '%s' failed to delete fifo %02d\n",
module->name, n);
fifo_delete(n, module_id);
}
}
for (n = 1; n <= RTAPI_MAX_IRQS; n++) {
if (irq_array[n].owner == module_id) {
rtapi_print_msg(RTAPI_MSG_WARN,
"RTAPI: WARNING: module '%s' failed to delete handler for IRQ %d\n",
module->name, irq_array[n].irq_num);
irq_delete(irq_array[n].irq_num);
}
}
/* use snprintf() to do strncpy(), since we don't have string.h */
rtapi_snprintf(name, RTAPI_NAME_LEN, "%s", module->name);
/* update module data */
module->state = NO_MODULE;
module->name[0] = '\0';
rtapi_data->rt_module_count--;
if (rtapi_data->rt_module_count == 0) {
if (rtapi_data->timer_running != 0) {
stop_rt_timer();
rt_free_timers();
rtapi_data->timer_period = 0;
timer_counts = 0;
max_delay = DEFAULT_MAX_DELAY;
rtapi_data->timer_running = 0;
}
}
rtapi_print_msg(RTAPI_MSG_DBG, "RTAPI: module %d exited, name: '%s'\n",
module_id, name);
return 0;
}
int rtapi_snprintf(char *buf, unsigned long int size, const char *fmt, ...)
{
va_list args;
int i;
va_start(args, fmt);
i = rtapi_vsnprintf(buf, size, fmt, args);
va_end(args);
return i;
}
#define BUFFERLEN 1024
void default_rtapi_msg_handler(msg_level_t level, const char *fmt, va_list ap) {
char buf[BUFFERLEN];
rtapi_vsnprintf(buf, BUFFERLEN, fmt, ap);
rt_printk(buf);
}
static rtapi_msg_handler_t rtapi_msg_handler = default_rtapi_msg_handler;
rtapi_msg_handler_t rtapi_get_msg_handler(void) {
return rtapi_msg_handler;
}
void rtapi_set_msg_handler(rtapi_msg_handler_t handler) {
if(handler == NULL) rtapi_msg_handler = default_rtapi_msg_handler;
else rtapi_msg_handler = handler;
}
void rtapi_print(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
rtapi_msg_handler(RTAPI_MSG_ALL, fmt, args);
va_end(args);
}
void rtapi_print_msg(msg_level_t level, const char *fmt, ...)
{
va_list args;
if ((level <= msg_level) && (msg_level != RTAPI_MSG_NONE)) {
va_start(args, fmt);
rtapi_msg_handler(level, fmt, args);
va_end(args);
}
}
int rtapi_set_msg_level(int level)
{
if ((level < RTAPI_MSG_NONE) || (level > RTAPI_MSG_ALL)) {
return -EINVAL;
}
msg_level = level;
return 0;
}
int rtapi_get_msg_level(void)
{
return msg_level;
}
/***********************************************************************
* CLOCK RELATED FUNCTIONS *
************************************************************************/
long int rtapi_clock_set_period(long int nsecs)
{
RTIME counts, got_counts;
if (nsecs == 0) {
/* it's a query, not a command */
return rtapi_data->timer_period;
}
if (rtapi_data->timer_running) {
/* already started, can't restart */
return -EINVAL;
}
/* limit period to 2 micro-seconds min, 1 second max */
if ((nsecs < 2000) || (nsecs > 1000000000L)) {
rtapi_print_msg(RTAPI_MSG_ERR,
"RTAPI: ERR: clock_set_period: %ld nsecs, out of range\n",
nsecs);
return -EINVAL;
}
rt_set_periodic_mode();
counts = nano2count((RTIME) nsecs);
if(count2nano(counts) > nsecs) counts--;
got_counts = start_rt_timer(counts);
rtapi_data->timer_period = count2nano(got_counts);
timer_counts = got_counts;
rtapi_print_msg(RTAPI_MSG_DBG,
"RTAPI: clock_set_period requested: %ld actual: %ld counts requested: %d actual: %d\n",
nsecs, rtapi_data->timer_period, (int)counts, (int)got_counts);
rtapi_data->timer_running = 1;
max_delay = rtapi_data->timer_period / 4;
return rtapi_data->timer_period;
}
long long int rtapi_get_time(void)
{
return rt_get_cpu_time_ns();
}
void rtapi_delay(long int nsec)
{
if (nsec > max_delay) {
nsec = max_delay;
}
rt_busy_sleep(nsec);
}
long int rtapi_delay_max(void)
{
return max_delay;
}
/***********************************************************************
* TASK RELATED FUNCTIONS *
************************************************************************/
/* Priority functions. RTAI uses 0 as the highest priority, as the
number increases, the actual priority of the task decreases. */
int rtapi_prio_highest(void)
{
return 0;
}
int rtapi_prio_lowest(void)
{
/* RTAI has LOTS of different priorities - RT_LOWEST_PRIORITY is
0x3FFFFFFF! I don't want such ugly numbers, and we only need a few
levels, so we use 0xFFF (4095) instead */
return 0xFFF;
}
int rtapi_prio_next_higher(int prio)
{
/* return a valid priority for out of range arg */
if (prio <= rtapi_prio_highest()) {
return rtapi_prio_highest();
}
if (prio > rtapi_prio_lowest()) {
return rtapi_prio_lowest();
}
/* return next higher priority for in-range arg */
return prio - 1;
}
int rtapi_prio_next_lower(int prio)
{
/* return a valid priority for out of range arg */
if (prio >= rtapi_prio_lowest()) {
return rtapi_prio_lowest();
}
if (prio < rtapi_prio_highest()) {
return rtapi_prio_highest();
}
/* return next lower priority for in-range arg */
return prio + 1;
}
/* We define taskcode as taking a void pointer and returning void, but
rtai wants it to take an int and return void.
We solve this with a wrapper function that meets rtai's needs.
The wrapper functions also properly deals with tasks that return.
(Most tasks are infinite loops, and don't return.)
*/
static void wrapper(long task_id)
{
task_data *task;
/* point to the task data */
task = &task_array[task_id];
/* call the task function with the task argument */
(task->taskcode) (task->arg);
/* if the task ever returns, we record that fact */
task->state = ENDED;
/* and return to end the thread */
return;
}
#define IP(x) ((x)->ip)
static int rtapi_trap_handler(int vec, int signo, struct pt_regs *regs,
void *task) {
int self = rtapi_task_self();
rtapi_print_msg(RTAPI_MSG_ERR,
"RTAPI: Task %d[%p]: Fault with vec=%d, signo=%d ip=%08lx.\n"
"RTAPI: This fault may not be recoverable without rebooting.\n",
self, task, vec, signo, IP(regs));
rtapi_task_pause(self);
return 0;
}
int rtapi_task_new(void (*taskcode) (void *), void *arg,
int prio, int owner, unsigned long int stacksize, int uses_fp)
{
int n;
long task_id;
int retval;
task_data *task;
/* get the mutex */
rtapi_mutex_get(&(rtapi_data->mutex));
/* validate owner */
if ((owner < 1) || (owner > RTAPI_MAX_MODULES)) {
rtapi_mutex_give(&(rtapi_data->mutex));
return -EINVAL;
}
if (module_array[owner].state != REALTIME) {
rtapi_mutex_give(&(rtapi_data->mutex));
return -EINVAL;
}
/* find empty spot in task array */
n = 1;
while ((n <= RTAPI_MAX_TASKS) && (task_array[n].state != EMPTY)) {
n++;
}
if (n > RTAPI_MAX_TASKS) {
/* no room */
rtapi_mutex_give(&(rtapi_data->mutex));
return -EMFILE;
}
/* we have space for the task */
task_id = n;
task = &(task_array[n]);
/* check requested priority */
if ((prio < rtapi_prio_highest()) || (prio > rtapi_prio_lowest())) {
rtapi_mutex_give(&(rtapi_data->mutex));
return -EINVAL;
}
/* get space for the OS's task data - this is around 900 bytes, */
/* so we don't want to statically allocate it for unused tasks. */
ostask_array[task_id] = kmalloc(sizeof(RT_TASK), GFP_USER);
if (ostask_array[task_id] == NULL) {
rtapi_mutex_give(&(rtapi_data->mutex));
return -ENOMEM;
}
task->taskcode = taskcode;
task->arg = arg;
/* call OS to initialize the task - use predetermined CPU.
uses_fp is deprecated and ignored; always save FPU state. */
retval = rt_task_init_cpuid(ostask_array[task_id], wrapper, task_id,
stacksize, prio, 1 /* always save FPU */, 0 /* signal */, rtapi_data->rt_cpu );
if (retval != 0) {
/* couldn't create task, free task data memory */
kfree(ostask_array[task_id]);
rtapi_mutex_give(&(rtapi_data->mutex));
if (retval == ENOMEM) {
/* not enough space for stack */
return -ENOMEM;
}
/* unknown error */
return -EINVAL;
}
/* request to handle traps in the new task */
{
int v;
for(v=0; v<RTAI_NR_TRAPS; v++)
rt_set_task_trap_handler(ostask_array[task_id], v, rtapi_trap_handler);
}
/* the task has been created, update data */
task->state = PAUSED;
task->prio = prio;
task->owner = owner;
task->taskcode = taskcode;
rtapi_data->task_count++;
/* announce the birth of a brand new baby task */
rtapi_print_msg(RTAPI_MSG_DBG,
"RTAPI: task %02ld installed by module %02d, priority %d, code: %p\n",
task_id, task->owner, task->prio, taskcode);
/* and return the ID to the proud parent */
rtapi_mutex_give(&(rtapi_data->mutex));
return task_id;
}
int rtapi_task_delete(int task_id)
{
int retval;
rtapi_mutex_get(&(rtapi_data->mutex));
retval = task_delete(task_id);
rtapi_mutex_give(&(rtapi_data->mutex));
return retval;
}
static int task_delete(int task_id)
{
task_data *task;
/* validate task ID */
if ((task_id < 1) || (task_id > RTAPI_MAX_TASKS)) {
return -EINVAL;
}
/* point to the task's data */
task = &(task_array[task_id]);
/* check task status */
if (task->state == EMPTY) {
/* nothing to delete */
return -EINVAL;
}
if ((task->state == PERIODIC) || (task->state == FREERUN)) {
/* task is running, need to stop it */
rtapi_print_msg(RTAPI_MSG_WARN,
"RTAPI: WARNING: tried to delete task %02d while running\n",
task_id);
int retval = rtapi_task_pause(task_id);
if (retval != 0){
rtapi_print_msg(RTAPI_MSG_WARN,
"RTAPI: WARNING: Failed to pause task %02d, return %i\n", task_id, retval);
return -EINVAL;
}
}
/* get rid of it */
rt_task_delete(ostask_array[task_id]);
/* free kernel memory */
kfree(ostask_array[task_id]);
/* update data */
task->state = EMPTY;
task->prio = 0;
task->owner = 0;
task->taskcode = NULL;
ostask_array[task_id] = NULL;
rtapi_data->task_count--;
/* if no more tasks, stop the timer */
if (rtapi_data->task_count == 0) {
if (rtapi_data->timer_running != 0) {
stop_rt_timer();
rt_free_timers();
rtapi_data->timer_period = 0;
max_delay = DEFAULT_MAX_DELAY;
rtapi_data->timer_running = 0;
}
}
/* done */
rtapi_print_msg(RTAPI_MSG_DBG, "RTAPI: task %02d deleted\n", task_id);
return 0;
}
int rtapi_task_start(int task_id, unsigned long int period_nsec)
{
int retval;
unsigned long int quo, period_counts;
task_data *task;
/* validate task ID */
if ((task_id < 1) || (task_id > RTAPI_MAX_TASKS)) {
return -EINVAL;
}
/* point to the task's data */
task = &(task_array[task_id]);
/* is task ready to be started? */
if (task->state != PAUSED) {
return -EINVAL;
}
/* can't start periodic tasks if timer isn't running */
if ((rtapi_data->timer_running == 0) || (rtapi_data->timer_period == 0)) {
rtapi_print_msg(RTAPI_MSG_ERR,
"RTAPI: could not start task: timer isn't running\n");
return -EINVAL;
}
period_counts = nano2count((RTIME)period_nsec);
quo = (period_counts + timer_counts / 2) / timer_counts;
period_counts = quo * timer_counts;
period_nsec = count2nano(period_counts);
/* start the task */
retval = rt_task_make_periodic(ostask_array[task_id],
rt_get_time() + period_counts, period_counts);
if (retval != 0) {
return -EINVAL;
}
/* ok, task is started */
task->state = PERIODIC;
rtapi_print_msg(RTAPI_MSG_DBG, "RTAPI: start_task id: %02d\n", task_id);
rtapi_print_msg(RTAPI_MSG_DBG, "RTAPI: period_nsec: %ld\n", period_nsec);
rtapi_print_msg(RTAPI_MSG_DBG, "RTAPI: count: %ld\n", period_counts);
return retval;
}
void rtapi_wait(void)
{
int result = rt_task_wait_period();
if(result != 0) {
static int error_printed = 0;
if(error_printed < 10) {
#ifdef RTE_TMROVRN
if(result == RTE_TMROVRN) {
rtapi_print_msg(
error_printed == 0 ? RTAPI_MSG_ERR : RTAPI_MSG_WARN,
"RTAPI: ERROR: Unexpected realtime delay on task %d\n"
"This Message will only display once per session.\n"
"Run the Latency Test and resolve before continuing.\n",
rtapi_task_self());
} else
#endif
#ifdef RTE_UNBLKD
if(result == RTE_UNBLKD) {
rtapi_print_msg(
error_printed == 0 ? RTAPI_MSG_ERR : RTAPI_MSG_WARN,
"RTAPI: ERROR: rt_task_wait_period() returned RTE_UNBLKD (%d).\n", result);
} else
#endif
{
rtapi_print_msg(
error_printed == 0 ? RTAPI_MSG_ERR : RTAPI_MSG_WARN,
"RTAPI: ERROR: rt_task_wait_period() returned %d.\n", result);
}
error_printed++;
if(error_printed == 10)
rtapi_print_msg(
error_printed == 0 ? RTAPI_MSG_ERR : RTAPI_MSG_WARN,
"RTAPI: (further messages will be suppressed)\n");
}
}
}
int rtapi_task_resume(int task_id)
{
int retval;
task_data *task;
/* validate task ID */
if ((task_id < 1) || (task_id > RTAPI_MAX_TASKS)) {
return -EINVAL;
}
/* point to the task's data */
task = &(task_array[task_id]);
/* is task ready to be started? */
if (task->state != PAUSED) {
return -EINVAL;
}
/* start the task */
retval = rt_task_resume(ostask_array[task_id]);
if (retval != 0) {
return -EINVAL;
}
/* update task data */
task->state = FREERUN;
return 0;
}
int rtapi_task_pause(int task_id)
{
int retval;
int oldstate;
task_data *task;
/* validate task ID */
if ((task_id < 1) || (task_id > RTAPI_MAX_TASKS)) {
return -EINVAL;
}
/* point to the task's data */
task = &(task_array[task_id]);
/* is it running? */
if ((task->state != PERIODIC) && (task->state != FREERUN)) {
return -EINVAL;
}
/* pause the task */
oldstate = task->state;
task->state = PAUSED;
retval = rt_task_suspend(ostask_array[task_id]);
if (retval < 0) {
task->state = oldstate;
return -EINVAL;
}
/* update task data */
return 0;
}
void rtapi_task_self_resync(void)
{
/* RTAI backend stub: re-anchoring the period from inside an RTAI task
requires per-task period storage that is not currently kept. The
primary consumer (EtherCAT init via initf) runs on the uspace
backend. If RTAI support is needed, store period_counts per task in
rtapi_task_start() and call
rt_task_make_periodic(rt_whoami(), rt_get_time() + period_counts,
period_counts) here. */
static int warned = 0;
if (!warned) {
rtapi_print_msg(RTAPI_MSG_WARN,
"RTAPI: rtapi_task_self_resync() is a no-op on the RTAI backend\n");
warned = 1;
}
}
int rtapi_task_self(void)
{
RT_TASK *ptr;
int n;
/* ask OS for pointer to its data for the current task */
ptr = rt_whoami();
if (ptr == NULL) {
/* called from outside a task? */
return -EINVAL;
}
/* find matching entry in task array */
n = 1;
while (n <= RTAPI_MAX_TASKS) {
if (ostask_array[n] == ptr) {
/* found a match */
return n;
}
n++;
}
return -EINVAL;
}
/***********************************************************************
* SHARED MEMORY RELATED FUNCTIONS *
************************************************************************/
int rtapi_shmem_new(int key, int module_id, unsigned long int size)
{
int n;
int shmem_id;
shmem_data *shmem;
/* key must be non-zero, and also cannot match the key that RTAPI uses */
if ((key == 0) || (key == RTAPI_KEY)) {
return -EINVAL;
}
/* get the mutex */
rtapi_mutex_get(&(rtapi_data->mutex));
/* validate module_id */
if ((module_id < 1) || (module_id > RTAPI_MAX_MODULES)) {
rtapi_mutex_give(&(rtapi_data->mutex));
return -EINVAL;
}
if (module_array[module_id].state != REALTIME) {
rtapi_mutex_give(&(rtapi_data->mutex));
return -EINVAL;
}
/* check if a block is already open for this key */
for (n = 1; n <= RTAPI_MAX_SHMEMS; n++) {
if (shmem_array[n].key == key) {
/* found a match */
shmem_id = n;
shmem = &(shmem_array[n]);
/* is it big enough? */
if (shmem->size < size) {
rtapi_mutex_give(&(rtapi_data->mutex));
return -EINVAL;
}
/* yes, has it been mapped into kernel space? */
if (shmem->rtusers == 0) {
/* no, map it and save the address */
shmem_addr_array[shmem_id] = rtai_kmalloc(key, shmem->size);
if (shmem_addr_array[shmem_id] == NULL) {
rtapi_mutex_give(&(rtapi_data->mutex));
return -ENOMEM;
}
}
/* is this module already using it? */
if (test_bit(module_id, shmem->bitmap)) {
rtapi_mutex_give(&(rtapi_data->mutex));
return -EINVAL;
}
/* update usage data */
set_bit(module_id, shmem->bitmap);
shmem->rtusers++;
/* announce another user for this shmem */
rtapi_print_msg(RTAPI_MSG_DBG,
"RTAPI: shmem %02d opened by module %02d\n",
shmem_id, module_id);