hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
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
/*
 * Copyright (C) 2010 Gilles Chanteperdrix <gilles.chanteperdrix@xenomai.org>.
 *
 * Xenomai is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Xenomai 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 Xenomai; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include <linux/module.h>
#include <linux/vmalloc.h>
#include <linux/semaphore.h>
#include <cobalt/kernel/sched.h>
#include <cobalt/kernel/synch.h>
#include <cobalt/kernel/thread.h>
#include <cobalt/kernel/trace.h>
#include <rtdm/testing.h>
#include <rtdm/driver.h>
#include <asm/xenomai/fptest.h>
 
MODULE_DESCRIPTION("Cobalt context switch test helper");
MODULE_AUTHOR("Gilles Chanteperdrix <gilles.chanteperdrix@xenomai.org>");
MODULE_VERSION("0.1.1");
MODULE_LICENSE("GPL");
 
#define RTSWITCH_RT      0x10000
#define RTSWITCH_NRT     0
#define RTSWITCH_KERNEL  0x20000
 
struct rtswitch_task {
   struct rttst_swtest_task base;
   rtdm_event_t rt_synch;
   struct semaphore nrt_synch;
   struct xnthread ktask;          /* For kernel-space real-time tasks. */
   unsigned int last_switch;
};
 
struct rtswitch_context {
   struct rtswitch_task *tasks;
   unsigned int tasks_count;
   unsigned int next_index;
   struct semaphore lock;
   unsigned int cpu;
   unsigned int switches_count;
 
   unsigned long pause_us;
   unsigned int next_task;
   rtdm_timer_t wake_up_delay;
 
   unsigned int failed;
   struct rttst_swtest_error error;
 
   struct rtswitch_task *utask;
   rtdm_nrtsig_t wake_utask;
};
 
static int fp_features;
 
static int report(const char *fmt, ...)
{
   va_list ap;
   int ret;
 
   va_start(ap, fmt);
   ret = vprintk(fmt, ap);
   va_end(ap);
 
   return ret;
}
 
static void handle_ktask_error(struct rtswitch_context *ctx, unsigned int fp_val)
{
   struct rtswitch_task *cur = &ctx->tasks[ctx->error.last_switch.to];
   unsigned int i;
 
   ctx->failed = 1;
   ctx->error.fp_val = fp_val;
 
   if ((cur->base.flags & RTSWITCH_RT) == RTSWITCH_RT)
       for (i = 0; i < ctx->tasks_count; i++) {
           struct rtswitch_task *task = &ctx->tasks[i];
 
           /* Find the first non kernel-space task. */
           if ((task->base.flags & RTSWITCH_KERNEL))
               continue;
 
           /* Unblock it. */
           switch(task->base.flags & RTSWITCH_RT) {
           case RTSWITCH_NRT:
               ctx->utask = task;
               rtdm_nrtsig_pend(&ctx->wake_utask);
               break;
 
           case RTSWITCH_RT:
               rtdm_event_signal(&task->rt_synch);
               break;
           }
 
           xnthread_suspend(&cur->ktask,
                    XNSUSP, XN_INFINITE, XN_RELATIVE, NULL);
       }
}
 
static int rtswitch_pend_rt(struct rtswitch_context *ctx,
               unsigned int idx)
{
   struct rtswitch_task *task;
   int rc;
 
   if (idx > ctx->tasks_count)
       return -EINVAL;
 
   task = &ctx->tasks[idx];
   task->base.flags |= RTSWITCH_RT;
 
   rc = rtdm_event_wait(&task->rt_synch);
   if (rc < 0)
       return rc;
 
   if (ctx->failed)
       return 1;
 
   return 0;
}
 
static void timed_wake_up(rtdm_timer_t *timer)
{
   struct rtswitch_context *ctx =
       container_of(timer, struct rtswitch_context, wake_up_delay);
   struct rtswitch_task *task;
 
   task = &ctx->tasks[ctx->next_task];
 
   switch (task->base.flags & RTSWITCH_RT) {
   case RTSWITCH_NRT:
       ctx->utask = task;
       rtdm_nrtsig_pend(&ctx->wake_utask);
       break;
 
   case RTSWITCH_RT:
       rtdm_event_signal(&task->rt_synch);
   }
}
 
static int rtswitch_to_rt(struct rtswitch_context *ctx,
             unsigned int from_idx,
             unsigned int to_idx)
{
   struct rtswitch_task *from, *to;
   int rc;
 
   if (from_idx > ctx->tasks_count || to_idx > ctx->tasks_count)
       return -EINVAL;
 
   /* to == from is a special case which means
      "return to the previous task". */
   if (to_idx == from_idx)
       to_idx = ctx->error.last_switch.from;
 
   from = &ctx->tasks[from_idx];
   to = &ctx->tasks[to_idx];
 
   from->base.flags |= RTSWITCH_RT;
   from->last_switch = ++ctx->switches_count;
   ctx->error.last_switch.from = from_idx;
   ctx->error.last_switch.to = to_idx;
   barrier();
 
   if (ctx->pause_us) {
       ctx->next_task = to_idx;
       barrier();
       rtdm_timer_start(&ctx->wake_up_delay,
                ctx->pause_us * 1000, 0,
                RTDM_TIMERMODE_RELATIVE);
       xnsched_lock();
   } else
       switch (to->base.flags & RTSWITCH_RT) {
       case RTSWITCH_NRT:
           ctx->utask = to;
           barrier();
           rtdm_nrtsig_pend(&ctx->wake_utask);
           xnsched_lock();
           break;
 
       case RTSWITCH_RT:
           xnsched_lock();
           rtdm_event_signal(&to->rt_synch);
           break;
 
       default:
           return -EINVAL;
       }
 
   rc = rtdm_event_wait(&from->rt_synch);
   xnsched_unlock();
 
   if (rc < 0)
       return rc;
 
   if (ctx->failed)
       return 1;
 
   return 0;
}
 
static int rtswitch_pend_nrt(struct rtswitch_context *ctx,
                unsigned int idx)
{
   struct rtswitch_task *task;
 
   if (idx > ctx->tasks_count)
       return -EINVAL;
 
   task = &ctx->tasks[idx];
 
   task->base.flags &= ~RTSWITCH_RT;
 
   if (down_interruptible(&task->nrt_synch))
       return -EINTR;
 
   if (ctx->failed)
       return 1;
 
   return 0;
}
 
static int rtswitch_to_nrt(struct rtswitch_context *ctx,
              unsigned int from_idx,
              unsigned int to_idx)
{
   struct rtswitch_task *from, *to;
   unsigned int expected, fp_val;
   int fp_check;
 
   if (from_idx > ctx->tasks_count || to_idx > ctx->tasks_count)
       return -EINVAL;
 
   /* to == from is a special case which means
      "return to the previous task". */
   if (to_idx == from_idx)
       to_idx = ctx->error.last_switch.from;
 
   from = &ctx->tasks[from_idx];
   to = &ctx->tasks[to_idx];
 
   fp_check = ctx->switches_count == from->last_switch + 1
       && ctx->error.last_switch.from == to_idx
       && ctx->error.last_switch.to == from_idx;
 
   from->base.flags &= ~RTSWITCH_RT;
   from->last_switch = ++ctx->switches_count;
   ctx->error.last_switch.from = from_idx;
   ctx->error.last_switch.to = to_idx;
   barrier();
 
   if (ctx->pause_us) {
       ctx->next_task = to_idx;
       barrier();
       rtdm_timer_start(&ctx->wake_up_delay,
                ctx->pause_us * 1000, 0,
                RTDM_TIMERMODE_RELATIVE);
   } else
       switch (to->base.flags & RTSWITCH_RT) {
       case RTSWITCH_NRT:
       switch_to_nrt:
           up(&to->nrt_synch);
           break;
 
       case RTSWITCH_RT:
 
           if (!fp_check || fp_linux_begin() < 0) {
               fp_check = 0;
               goto signal_nofp;
           }
 
           expected = from_idx + 500 +
               (ctx->switches_count % 4000000) * 1000;
 
           fp_regs_set(fp_features, expected);
           rtdm_event_signal(&to->rt_synch);
           fp_val = fp_regs_check(fp_features, expected, report);
           fp_linux_end();
 
           if(down_interruptible(&from->nrt_synch))
               return -EINTR;
           if (ctx->failed)
               return 1;
           if (fp_val != expected) {
               handle_ktask_error(ctx, fp_val);
               return 1;
           }
 
           from->base.flags &= ~RTSWITCH_RT;
           from->last_switch = ++ctx->switches_count;
           ctx->error.last_switch.from = from_idx;
           ctx->error.last_switch.to = to_idx;
           if ((to->base.flags & RTSWITCH_RT) == RTSWITCH_NRT)
               goto switch_to_nrt;
           expected = from_idx + 500 +
               (ctx->switches_count % 4000000) * 1000;
           barrier();
 
           fp_linux_begin();
           fp_regs_set(fp_features, expected);
           rtdm_event_signal(&to->rt_synch);
           fp_val = fp_regs_check(fp_features, expected, report);
           fp_linux_end();
 
           if (down_interruptible(&from->nrt_synch))
               return -EINTR;
           if (ctx->failed)
               return 1;
           if (fp_val != expected) {
               handle_ktask_error(ctx, fp_val);
               return 1;
           }
 
           from->base.flags &= ~RTSWITCH_RT;
           from->last_switch = ++ctx->switches_count;
           ctx->error.last_switch.from = from_idx;
           ctx->error.last_switch.to = to_idx;
           barrier();
           if ((to->base.flags & RTSWITCH_RT) == RTSWITCH_NRT)
               goto switch_to_nrt;
 
       signal_nofp:
           rtdm_event_signal(&to->rt_synch);
           break;
 
       default:
           return -EINVAL;
       }
 
   if (down_interruptible(&from->nrt_synch))
       return -EINTR;
 
   if (ctx->failed)
       return 1;
 
   return 0;
}
 
static int rtswitch_set_tasks_count(struct rtswitch_context *ctx, unsigned int count)
{
   struct rtswitch_task *tasks;
 
   if (ctx->tasks_count == count)
       return 0;
 
   tasks = vmalloc(count * sizeof(*tasks));
 
   if (!tasks)
       return -ENOMEM;
 
   down(&ctx->lock);
 
   if (ctx->tasks)
       vfree(ctx->tasks);
 
   ctx->tasks = tasks;
   ctx->tasks_count = count;
   ctx->next_index = 0;
 
   up(&ctx->lock);
 
   return 0;
}
 
static int rtswitch_register_task(struct rtswitch_context *ctx,
                 struct rttst_swtest_task *arg)
{
   struct rtswitch_task *t;
 
   down(&ctx->lock);
 
   if (ctx->next_index == ctx->tasks_count) {
       up(&ctx->lock);
       return -EBUSY;
   }
 
   arg->index = ctx->next_index;
   t = &ctx->tasks[arg->index];
   ctx->next_index++;
   t->base = *arg;
   t->last_switch = 0;
   sema_init(&t->nrt_synch, 0);
   rtdm_event_init(&t->rt_synch, 0);
 
   up(&ctx->lock);
 
   return 0;
}
 
struct taskarg {
   struct rtswitch_context *ctx;
   struct rtswitch_task *task;
};
 
static void rtswitch_ktask(void *cookie)
{
   struct taskarg *arg = (struct taskarg *) cookie;
   unsigned int fp_val, expected, to, i = 0;
   struct rtswitch_context *ctx = arg->ctx;
   struct rtswitch_task *task = arg->task;
 
   to = task->base.index;
 
   rtswitch_pend_rt(ctx, task->base.index);
 
   while (!rtdm_task_should_stop()) {
       if (task->base.flags & RTTST_SWTEST_USE_FPU) {
           fp_init();
           fp_regs_set(fp_features, task->base.index + i * 1000);
       }
 
       switch(i % 3) {
       case 0:
           /* to == from means "return to last task" */
           rtswitch_to_rt(ctx, task->base.index, task->base.index);
           break;
       case 1:
           if (++to == task->base.index)
               ++to;
           if (to > ctx->tasks_count - 1)
               to = 0;
           if (to == task->base.index)
               ++to;
 
           fallthrough;
       case 2:
           rtswitch_to_rt(ctx, task->base.index, to);
       }
 
       if (task->base.flags & RTTST_SWTEST_USE_FPU) {
           expected = task->base.index + i * 1000;
           fp_val = fp_regs_check(fp_features, expected, report);
 
           if (fp_val != expected) {
               if (task->base.flags & RTTST_SWTEST_FREEZE)
                   xntrace_user_freeze(0, 0);
               handle_ktask_error(ctx, fp_val);
           }
       }
 
       if (++i == 4000000)
           i = 0;
   }
}
 
static int rtswitch_create_ktask(struct rtswitch_context *ctx,
                struct rttst_swtest_task *ptask)
{
   union xnsched_policy_param param;
   struct xnthread_start_attr sattr;
   struct xnthread_init_attr iattr;
   struct rtswitch_task *task;
   struct taskarg arg;
   int init_flags;
   char name[30];
   int err;
 
   /*
    * Silently disable FP tests in kernel if FPU is not supported
    * there. Typical case is math emulation support: we can use
    * it from userland as a synthetic FPU, but there is no sane
    * way to use it from kernel-based threads (Xenomai or Linux).
    */
   if (!fp_kernel_supported())
       ptask->flags &= ~RTTST_SWTEST_USE_FPU;
 
   ptask->flags |= RTSWITCH_KERNEL;
   err = rtswitch_register_task(ctx, ptask);
 
   if (err)
       return err;
 
   ksformat(name, sizeof(name), "rtk%d/%u", ptask->index, ctx->cpu);
 
   task = &ctx->tasks[ptask->index];
 
   arg.ctx = ctx;
   arg.task = task;
 
   init_flags = (ptask->flags & RTTST_SWTEST_FPU) ? XNFPU : 0;
 
   iattr.name = name;
   iattr.flags = init_flags;
   iattr.personality = &xenomai_personality;
   iattr.affinity = *cpumask_of(ctx->cpu);
   param.rt.prio = 1;
 
   set_cpus_allowed_ptr(current, cpumask_of(ctx->cpu));
 
   err = xnthread_init(&task->ktask,
               &iattr, &xnsched_class_rt, &param);
   if (!err) {
       sattr.mode = 0;
       sattr.entry = rtswitch_ktask;
       sattr.cookie = &arg;
       err = xnthread_start(&task->ktask, &sattr);
       if (err)
           __xnthread_discard(&task->ktask);
   } else
       /*
        * In order to avoid calling xnthread_cancel() for an
        * invalid thread.
        */
       task->base.flags = 0;
   /*
    * Putting the argument on stack is safe, because the new
    * thread, thanks to the above call to set_cpus_allowed_ptr(),
    * will preempt the current thread immediately, and will
    * suspend only once the arguments on stack are used.
    */
 
   return err;
}
 
static void rtswitch_utask_waker(rtdm_nrtsig_t *sig, void *arg)
{
   struct rtswitch_context *ctx = (struct rtswitch_context *)arg;
   up(&ctx->utask->nrt_synch);
}
 
static int rtswitch_open(struct rtdm_fd *fd, int oflags)
{
   struct rtswitch_context *ctx = rtdm_fd_to_private(fd);
 
   ctx->tasks = NULL;
   ctx->tasks_count = ctx->next_index = ctx->cpu = ctx->switches_count = 0;
   sema_init(&ctx->lock, 1);
   ctx->failed = 0;
   ctx->error.last_switch.from = ctx->error.last_switch.to = -1;
   ctx->pause_us = 0;
 
   rtdm_nrtsig_init(&ctx->wake_utask, rtswitch_utask_waker, ctx);
 
   rtdm_timer_init(&ctx->wake_up_delay, timed_wake_up, "switchtest timer");
 
   return 0;
}
 
static void rtswitch_close(struct rtdm_fd *fd)
{
   struct rtswitch_context *ctx = rtdm_fd_to_private(fd);
   unsigned int i;
 
   rtdm_timer_destroy(&ctx->wake_up_delay);
   rtdm_nrtsig_destroy(&ctx->wake_utask);
 
   if (ctx->tasks) {
       set_cpus_allowed_ptr(current, cpumask_of(ctx->cpu));
 
       for (i = 0; i < ctx->next_index; i++) {
           struct rtswitch_task *task = &ctx->tasks[i];
 
           if (task->base.flags & RTSWITCH_KERNEL) {
               rtdm_task_destroy(&task->ktask);
               rtdm_task_join(&task->ktask);
           }
           rtdm_event_destroy(&task->rt_synch);
       }
       vfree(ctx->tasks);
   }
}
 
static int rtswitch_ioctl_nrt(struct rtdm_fd *fd,
                 unsigned int request,
                 void *arg)
{
   struct rtswitch_context *ctx = rtdm_fd_to_private(fd);
   struct rttst_swtest_task task;
   struct rttst_swtest_dir fromto;
   __u32 count;
   int err;
 
   switch (request) {
   case RTTST_RTIOC_SWTEST_SET_TASKS_COUNT:
       return rtswitch_set_tasks_count(ctx,
                       (unsigned long) arg);
 
   case RTTST_RTIOC_SWTEST_SET_CPU:
       if ((unsigned long) arg > num_online_cpus() - 1)
           return -EINVAL;
 
       ctx->cpu = (unsigned long) arg;
       return 0;
 
   case RTTST_RTIOC_SWTEST_SET_PAUSE:
       ctx->pause_us = (unsigned long) arg;
       return 0;
 
   case RTTST_RTIOC_SWTEST_REGISTER_UTASK:
       if (!rtdm_rw_user_ok(fd, arg, sizeof(task)))
           return -EFAULT;
 
       rtdm_copy_from_user(fd, &task, arg, sizeof(task));
 
       err = rtswitch_register_task(ctx, &task);
 
       if (!err)
           rtdm_copy_to_user(fd,
                     arg,
                     &task,
                     sizeof(task));
 
       return err;
 
   case RTTST_RTIOC_SWTEST_CREATE_KTASK:
       if (!rtdm_rw_user_ok(fd, arg, sizeof(task)))
           return -EFAULT;
 
       rtdm_copy_from_user(fd, &task, arg, sizeof(task));
 
       err = rtswitch_create_ktask(ctx, &task);
 
       if (!err)
           rtdm_copy_to_user(fd,
                     arg,
                     &task,
                     sizeof(task));
 
       return err;
 
   case RTTST_RTIOC_SWTEST_PEND:
       if (!rtdm_read_user_ok(fd, arg, sizeof(task)))
           return -EFAULT;
 
       rtdm_copy_from_user(fd, &task, arg, sizeof(task));
 
       return rtswitch_pend_nrt(ctx, task.index);
 
   case RTTST_RTIOC_SWTEST_SWITCH_TO:
       if (!rtdm_read_user_ok(fd, arg, sizeof(fromto)))
           return -EFAULT;
 
       rtdm_copy_from_user(fd,
                   &fromto,
                   arg,
                   sizeof(fromto));
 
       if (fromto.switch_mode) {
           xnthread_harden();
           return rtswitch_to_rt(ctx, fromto.from, fromto.to);
       }
       return rtswitch_to_nrt(ctx, fromto.from, fromto.to);
 
   case RTTST_RTIOC_SWTEST_GET_SWITCHES_COUNT:
       if (!rtdm_rw_user_ok(fd, arg, sizeof(count)))
           return -EFAULT;
 
       count = ctx->switches_count;
 
       rtdm_copy_to_user(fd, arg, &count, sizeof(count));
 
       return 0;
 
   case RTTST_RTIOC_SWTEST_GET_LAST_ERROR:
       if (!rtdm_rw_user_ok(fd, arg, sizeof(ctx->error)))
           return -EFAULT;
 
       rtdm_copy_to_user(fd,
                 arg,
                 &ctx->error,
                 sizeof(ctx->error));
 
       return 0;
 
   default:
       return -ENOSYS;
   }
}
 
static int rtswitch_ioctl_rt(struct rtdm_fd *fd,
                unsigned int request,
                void *arg)
{
   struct rtswitch_context *ctx = rtdm_fd_to_private(fd);
   struct rttst_swtest_task task;
   struct rttst_swtest_dir fromto;
 
   switch (request) {
   case RTTST_RTIOC_SWTEST_PEND:
       if (!rtdm_read_user_ok(fd, arg, sizeof(task)))
           return -EFAULT;
 
       rtdm_copy_from_user(fd, &task, arg, sizeof(task));
 
       return rtswitch_pend_rt(ctx, task.index);
 
   case RTTST_RTIOC_SWTEST_SWITCH_TO:
       if (!rtdm_read_user_ok(fd, arg, sizeof(fromto)))
           return -EFAULT;
 
       rtdm_copy_from_user(fd,
                   &fromto,
                   arg,
                   sizeof(fromto));
 
       if (fromto.switch_mode) {
           xnthread_relax(0, 0);
           return rtswitch_to_nrt(ctx, fromto.from, fromto.to);
       }
       return rtswitch_to_rt(ctx, fromto.from, fromto.to);
 
   case RTTST_RTIOC_SWTEST_GET_LAST_ERROR:
       if (!rtdm_rw_user_ok(fd, arg, sizeof(ctx->error)))
           return -EFAULT;
 
       rtdm_copy_to_user(fd,
                 arg,
                 &ctx->error,
                 sizeof(ctx->error));
 
       return 0;
 
   default:
       return -ENOSYS;
   }
}
 
static struct rtdm_driver switchtest_driver = {
   .profile_info = RTDM_PROFILE_INFO(switchtest,
                     RTDM_CLASS_TESTING,
                     RTDM_SUBCLASS_SWITCHTEST,
                     RTTST_PROFILE_VER),
   .device_flags = RTDM_NAMED_DEVICE,
   .device_count =    1,
   .context_size = sizeof(struct rtswitch_context),
   .ops = {
       .open = rtswitch_open,
       .close = rtswitch_close,
       .ioctl_rt = rtswitch_ioctl_rt,
       .ioctl_nrt = rtswitch_ioctl_nrt,
   },
};
 
static struct rtdm_device device = {
   .driver = &switchtest_driver,
   .label = "switchtest",
};
 
int __init __switchtest_init(void)
{
   fp_features = fp_detect();
 
   return rtdm_dev_register(&device);
}
 
void __switchtest_exit(void)
{
   rtdm_dev_unregister(&device);
}
 
module_init(__switchtest_init);
module_exit(__switchtest_exit);