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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
/*
 * Written by Gilles Chanteperdrix <gilles.chanteperdrix@xenomai.org>.
 *
 * This program 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.
 *
 * This program 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 program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
#include <linux/stdarg.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <cobalt/kernel/select.h>
#include <rtdm/fd.h>
#include "internal.h"
#include "thread.h"
#include "signal.h"
#include "timer.h"
#include "mqueue.h"
#include "clock.h"
#include <trace/events/cobalt-posix.h>
#include <cobalt/kernel/time.h>
 
#define COBALT_MSGMAX        65536
#define COBALT_MSGSIZEMAX    (16*1024*1024)
#define COBALT_MSGPRIOMAX    32768
 
struct cobalt_mq {
   unsigned magic;
 
   struct list_head link;
 
   struct xnsynch receivers;
   struct xnsynch senders;
   size_t memsize;
   char *mem;
   struct list_head queued;
   struct list_head avail;
   int nrqueued;
 
   /* mq_notify */
   struct siginfo si;
   mqd_t target_qd;
   struct cobalt_thread *target;
 
   struct mq_attr attr;
 
   unsigned refs;
   char name[COBALT_MAXNAME];
   xnhandle_t handle;
 
   DECLARE_XNSELECT(read_select);
   DECLARE_XNSELECT(write_select);
};
 
struct cobalt_mqd {
   struct cobalt_mq *mq;
   struct rtdm_fd fd;
};
 
struct cobalt_msg {
   struct list_head link;
   unsigned int prio;
   size_t len;
   char data[0];
};
 
struct cobalt_mqwait_context {
   struct xnthread_wait_context wc;
   struct cobalt_msg *msg;
};
 
static struct mq_attr default_attr = {
      .mq_maxmsg = 10,
      .mq_msgsize = 8192,
};
 
static LIST_HEAD(cobalt_mqq);
 
#ifdef CONFIG_XENO_OPT_VFILE
 
static int mq_vfile_show(struct xnvfile_regular_iterator *it, void *data)
{
   return 0;
}
 
static struct xnvfile_regular_ops mq_vfile_ops = {
   .show = mq_vfile_show,
};
 
static struct xnpnode_regular __mq_pnode = {
   .node = {
       .dirname = "mqueue",
       .root = &posix_ptree,
       .ops = &xnregistry_vfreg_ops,
   },
   .vfile = {
       .ops = &mq_vfile_ops,
   },
};
 
#else /* !CONFIG_XENO_OPT_VFILE */
 
static struct xnpnode_link __mq_pnode = {
   .node = {
       .dirname = "mqueue",
   }
};
 
#endif /* !CONFIG_XENO_OPT_VFILE */
 
 
static inline struct cobalt_msg *mq_msg_alloc(struct cobalt_mq *mq)
{
   if (list_empty(&mq->avail))
       return NULL;
 
   return list_get_entry(&mq->avail, struct cobalt_msg, link);
}
 
static inline void mq_msg_free(struct cobalt_mq *mq, struct cobalt_msg * msg)
{
   list_add(&msg->link, &mq->avail); /* For earliest re-use of the block. */
}
 
static inline int mq_init(struct cobalt_mq *mq, const struct mq_attr *attr)
{
   unsigned i, msgsize, memsize;
   char *mem;
 
   if (attr == NULL)
       attr = &default_attr;
   else {
       if (attr->mq_maxmsg <= 0 || attr->mq_msgsize <= 0)
           return -EINVAL;
       if (attr->mq_maxmsg > COBALT_MSGMAX)
           return -EINVAL;
       if (attr->mq_msgsize > COBALT_MSGSIZEMAX)
           return -EINVAL;
   }
 
   msgsize = attr->mq_msgsize + sizeof(struct cobalt_msg);
 
   /* Align msgsize on natural boundary. */
   if ((msgsize % sizeof(unsigned long)))
       msgsize +=
           sizeof(unsigned long) - (msgsize % sizeof(unsigned long));
 
   memsize = msgsize * attr->mq_maxmsg;
   memsize = PAGE_ALIGN(memsize);
   if (get_order(memsize) > MAX_ORDER)
       return -ENOSPC;
 
   mem = xnheap_vmalloc(memsize);
   if (mem == NULL)
       return -ENOSPC;
 
   mq->memsize = memsize;
   INIT_LIST_HEAD(&mq->queued);
   mq->nrqueued = 0;
   xnsynch_init(&mq->receivers, XNSYNCH_PRIO, NULL);
   xnsynch_init(&mq->senders, XNSYNCH_PRIO, NULL);
   mq->mem = mem;
 
   /* Fill the pool. */
   INIT_LIST_HEAD(&mq->avail);
   for (i = 0; i < attr->mq_maxmsg; i++) {
       struct cobalt_msg *msg = (struct cobalt_msg *) (mem + i * msgsize);
       mq_msg_free(mq, msg);
   }
 
   mq->attr = *attr;
   mq->target = NULL;
   xnselect_init(&mq->read_select);
   xnselect_init(&mq->write_select);
   mq->magic = COBALT_MQ_MAGIC;
   mq->refs = 2;
   INIT_LIST_HEAD(&mq->link);
 
   return 0;
}
 
static inline void mq_destroy(struct cobalt_mq *mq)
{
   spl_t s;
 
   xnlock_get_irqsave(&nklock, s);
   xnsynch_destroy(&mq->receivers);
   xnsynch_destroy(&mq->senders);
   list_del(&mq->link);
   xnsched_run();
   xnlock_put_irqrestore(&nklock, s);
   xnselect_destroy(&mq->read_select); /* Reschedules. */
   xnselect_destroy(&mq->write_select); /* Ditto. */
   xnregistry_remove(mq->handle);
   xnheap_vfree(mq->mem);
   kfree(mq);
}
 
static int mq_unref_inner(struct cobalt_mq *mq, spl_t s)
{
   int destroy;
 
   destroy = --mq->refs == 0;
   xnlock_put_irqrestore(&nklock, s);
 
   if (destroy)
       mq_destroy(mq);
 
   return destroy;
}
 
static int mq_unref(struct cobalt_mq *mq)
{
   spl_t s;
 
   xnlock_get_irqsave(&nklock, s);
   return mq_unref_inner(mq, s);
}
 
static void mqd_close(struct rtdm_fd *fd)
{
   struct cobalt_mqd *mqd = container_of(fd, struct cobalt_mqd, fd);
   struct cobalt_mq *mq = mqd->mq;
 
   kfree(mqd);
   mq_unref(mq);
}
 
int
mqd_select(struct rtdm_fd *fd, struct xnselector *selector,
      unsigned type, unsigned index)
{
   struct cobalt_mqd *mqd = container_of(fd, struct cobalt_mqd, fd);
   struct xnselect_binding *binding;
   struct cobalt_mq *mq;
   int err;
   spl_t s;
 
   if (type == XNSELECT_READ || type == XNSELECT_WRITE) {
       binding = xnmalloc(sizeof(*binding));
       if (!binding)
           return -ENOMEM;
   } else
       return -EBADF;
 
   xnlock_get_irqsave(&nklock, s);
   mq = mqd->mq;
 
   switch(type) {
   case XNSELECT_READ:
       err = -EBADF;
       if ((rtdm_fd_flags(fd) & COBALT_PERMS_MASK) == O_WRONLY)
           goto unlock_and_error;
 
       err = xnselect_bind(&mq->read_select, binding,
               selector, type, index,
               !list_empty(&mq->queued));
       if (err)
           goto unlock_and_error;
       break;
 
   case XNSELECT_WRITE:
       err = -EBADF;
       if ((rtdm_fd_flags(fd) & COBALT_PERMS_MASK) == O_RDONLY)
           goto unlock_and_error;
 
       err = xnselect_bind(&mq->write_select, binding,
               selector, type, index,
               !list_empty(&mq->avail));
       if (err)
           goto unlock_and_error;
       break;
   }
   xnlock_put_irqrestore(&nklock, s);
   return 0;
 
      unlock_and_error:
   xnlock_put_irqrestore(&nklock, s);
   xnfree(binding);
   return err;
}
 
static struct rtdm_fd_ops mqd_ops = {
   .close = mqd_close,
   .select = mqd_select,
};
 
static inline int mqd_create(struct cobalt_mq *mq, unsigned long flags, int ufd)
{
   struct cobalt_mqd *mqd;
   int ret;
 
   if (cobalt_ppd_get(0) == &cobalt_kernel_ppd)
       return -EPERM;
 
   mqd = kmalloc(sizeof(*mqd), GFP_KERNEL);
   if (mqd == NULL)
       return -ENOSPC;
 
   mqd->fd.oflags = flags;
   mqd->mq = mq;
 
   ret = rtdm_fd_enter(&mqd->fd, ufd, COBALT_MQD_MAGIC, &mqd_ops);
   if (ret < 0)
       return ret;
 
   return rtdm_fd_register(&mqd->fd, ufd);
}
 
static int mq_open(int uqd, const char *name, int oflags,
          int mode, struct mq_attr *attr)
{
   struct cobalt_mq *mq;
   xnhandle_t handle;
   spl_t s;
   int err;
 
   if (name[0] != '/' || name[1] == '\0')
       return -EINVAL;
 
  retry_bind:
   err = xnregistry_bind(&name[1], XN_NONBLOCK, XN_RELATIVE, &handle);
   switch (err) {
   case 0:
       /* Found */
       if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
           return -EEXIST;
 
       xnlock_get_irqsave(&nklock, s);
       mq = xnregistry_lookup(handle, NULL);
       if (mq && mq->magic != COBALT_MQ_MAGIC) {
           xnlock_put_irqrestore(&nklock, s);
           return -EINVAL;
       }
 
       if (mq) {
           ++mq->refs;
           xnlock_put_irqrestore(&nklock, s);
       } else {
           xnlock_put_irqrestore(&nklock, s);
           goto retry_bind;
       }
 
       err = mqd_create(mq, oflags & (O_NONBLOCK | COBALT_PERMS_MASK),
               uqd);
       if (err < 0) {
           mq_unref(mq);
           return err;
       }
       break;
 
   case -EWOULDBLOCK:
       /* Not found */
       if ((oflags & O_CREAT) == 0)
           return (mqd_t)-ENOENT;
 
       mq = kmalloc(sizeof(*mq), GFP_KERNEL);
       if (mq == NULL)
           return -ENOSPC;
 
       err = mq_init(mq, attr);
       if (err) {
           kfree(mq);
           return err;
       }
 
       snprintf(mq->name, sizeof(mq->name), "%s", &name[1]);
 
       err = mqd_create(mq, oflags & (O_NONBLOCK | COBALT_PERMS_MASK),
               uqd);
       if (err < 0) {
           mq_destroy(mq);
           return err;
       }
 
       xnlock_get_irqsave(&nklock, s);
       err = xnregistry_enter(mq->name, mq, &mq->handle,
                      &__mq_pnode.node);
       if (err < 0)
           --mq->refs;
       else
           list_add_tail(&mq->link, &cobalt_mqq);
       xnlock_put_irqrestore(&nklock, s);
       if (err < 0) {
           rtdm_fd_close(uqd, COBALT_MQD_MAGIC);
           if (err == -EEXIST)
               goto retry_bind;
           return err;
       }
       break;
 
   default:
       return err;
   }
 
   return 0;
}
 
static inline int mq_close(mqd_t fd)
{
   int err;
 
   err = rtdm_fd_close(fd, COBALT_MQD_MAGIC);
   return err == -EADV ? -EBADF : err;
}
 
static inline int mq_unlink(const char *name)
{
   struct cobalt_mq *mq;
   xnhandle_t handle;
   spl_t s;
   int err;
 
   if (name[0] != '/' || name[1] == '\0')
       return -EINVAL;
 
   err = xnregistry_bind(&name[1], XN_NONBLOCK, XN_RELATIVE, &handle);
   if (err == -EWOULDBLOCK)
       return -ENOENT;
   if (err)
       return err;
 
   xnlock_get_irqsave(&nklock, s);
   mq = xnregistry_lookup(handle, NULL);
   if (!mq) {
       err = -ENOENT;
       goto err_unlock;
   }
   if (mq->magic != COBALT_MQ_MAGIC) {
       err = -EINVAL;
     err_unlock:
       xnlock_put_irqrestore(&nklock, s);
 
       return err;
   }
   if (mq_unref_inner(mq, s) == 0)
       xnregistry_unlink(&name[1]);
   return 0;
}
 
static inline struct cobalt_msg *
mq_trysend(struct cobalt_mqd *mqd, size_t len)
{
   struct cobalt_msg *msg;
   struct cobalt_mq *mq;
   unsigned flags;
 
   mq = mqd->mq;
   flags = rtdm_fd_flags(&mqd->fd) & COBALT_PERMS_MASK;
 
   if (flags != O_WRONLY && flags != O_RDWR)
       return ERR_PTR(-EBADF);
 
   if (len > mq->attr.mq_msgsize)
       return ERR_PTR(-EMSGSIZE);
 
   msg = mq_msg_alloc(mq);
   if (msg == NULL)
       return ERR_PTR(-EAGAIN);
 
   if (list_empty(&mq->avail))
       xnselect_signal(&mq->write_select, 0);
 
   return msg;
}
 
static inline struct cobalt_msg *
mq_tryrcv(struct cobalt_mqd *mqd, size_t len)
{
   struct cobalt_msg *msg;
   unsigned int flags;
   struct cobalt_mq *mq;
 
   mq = mqd->mq;
   flags = rtdm_fd_flags(&mqd->fd) & COBALT_PERMS_MASK;
 
   if (flags != O_RDONLY && flags != O_RDWR)
       return ERR_PTR(-EBADF);
 
   if (len < mq->attr.mq_msgsize)
       return ERR_PTR(-EMSGSIZE);
 
   if (list_empty(&mq->queued))
       return ERR_PTR(-EAGAIN);
 
   msg = list_get_entry(&mq->queued, struct cobalt_msg, link);
   mq->nrqueued--;
 
   if (list_empty(&mq->queued))
       xnselect_signal(&mq->read_select, 0);
 
   return msg;
}
 
static struct cobalt_msg *
mq_timedsend_inner(struct cobalt_mqd *mqd,
          size_t len, const void __user *u_ts,
          int (*fetch_timeout)(struct timespec64 *ts,
                   const void __user *u_ts))
{
   struct cobalt_mqwait_context mwc;
   struct cobalt_msg *msg;
   struct cobalt_mq *mq;
   struct timespec64 ts;
   xntmode_t tmode;
   xnticks_t to;
   spl_t s;
   int ret;
 
   to = XN_INFINITE;
   tmode = XN_RELATIVE;
redo:
   xnlock_get_irqsave(&nklock, s);
   msg = mq_trysend(mqd, len);
   if (msg != ERR_PTR(-EAGAIN))
       goto out;
 
   if (rtdm_fd_flags(&mqd->fd) & O_NONBLOCK)
       goto out;
 
   if (fetch_timeout) {
       xnlock_put_irqrestore(&nklock, s);
       ret = fetch_timeout(&ts, u_ts);
       if (ret)
           return ERR_PTR(ret);
       if (!timespec64_valid(&ts))
           return ERR_PTR(-EINVAL);
       to = ts2ns(&ts) + 1;
       tmode = XN_REALTIME;
       fetch_timeout = NULL;
       goto redo;
   }
 
   mq = mqd->mq;
   xnthread_prepare_wait(&mwc.wc);
   ret = xnsynch_sleep_on(&mq->senders, to, tmode);
   if (ret) {
       if (ret & XNBREAK)
           msg = ERR_PTR(-EINTR);
       else if (ret & XNTIMEO)
           msg = ERR_PTR(-ETIMEDOUT);
       else if (ret & XNRMID)
           msg = ERR_PTR(-EBADF);
   } else
       msg = mwc.msg;
out:
   xnlock_put_irqrestore(&nklock, s);
 
   return msg;
}
 
static void mq_release_msg(struct cobalt_mq *mq, struct cobalt_msg *msg)
{
   struct cobalt_mqwait_context *mwc;
   struct xnthread_wait_context *wc;
   struct xnthread *thread;
 
   /*
    * Try passing the free message slot to a waiting sender, link
    * it to the free queue otherwise.
    */
   if (xnsynch_pended_p(&mq->senders)) {
       thread = xnsynch_wakeup_one_sleeper(&mq->senders);
       wc = xnthread_get_wait_context(thread);
       mwc = container_of(wc, struct cobalt_mqwait_context, wc);
       mwc->msg = msg;
       xnthread_complete_wait(wc);
   } else {
       mq_msg_free(mq, msg);
       if (list_is_singular(&mq->avail))
           xnselect_signal(&mq->write_select, 1);
   }
}
 
static int
mq_finish_send(struct cobalt_mqd *mqd, struct cobalt_msg *msg)
{
   struct cobalt_mqwait_context *mwc;
   struct xnthread_wait_context *wc;
   struct cobalt_sigpending *sigp;
   struct xnthread *thread;
   struct cobalt_mq *mq;
   spl_t s;
 
   mq = mqd->mq;
 
   xnlock_get_irqsave(&nklock, s);
   /* Can we do pipelined sending? */
   if (xnsynch_pended_p(&mq->receivers)) {
       thread = xnsynch_wakeup_one_sleeper(&mq->receivers);
       wc = xnthread_get_wait_context(thread);
       mwc = container_of(wc, struct cobalt_mqwait_context, wc);
       mwc->msg = msg;
       xnthread_complete_wait(wc);
   } else {
       /* Nope, have to go through the queue. */
       list_add_priff(msg, &mq->queued, prio, link);
       mq->nrqueued++;
 
       /*
        * If first message and no pending reader, send a
        * signal if notification was enabled via mq_notify().
        */
       if (list_is_singular(&mq->queued)) {
           xnselect_signal(&mq->read_select, 1);
           if (mq->target) {
               sigp = cobalt_signal_alloc();
               if (sigp) {
                   cobalt_copy_siginfo(SI_MESGQ, &sigp->si, &mq->si);
                   if (cobalt_signal_send(mq->target, sigp, 0) <= 0)
                       cobalt_signal_free(sigp);
               }
               mq->target = NULL;
           }
       }
   }
   xnsched_run();
   xnlock_put_irqrestore(&nklock, s);
 
   return 0;
}
 
static struct cobalt_msg *
mq_timedrcv_inner(struct cobalt_mqd *mqd,
         size_t len,
         const void __user *u_ts,
         int (*fetch_timeout)(struct timespec64 *ts,
                      const void __user *u_ts))
{
   struct cobalt_mqwait_context mwc;
   struct cobalt_msg *msg;
   struct cobalt_mq *mq;
   struct timespec64 ts;
   xntmode_t tmode;
   xnticks_t to;
   spl_t s;
   int ret;
 
   to = XN_INFINITE;
   tmode = XN_RELATIVE;
redo:
   xnlock_get_irqsave(&nklock, s);
   msg = mq_tryrcv(mqd, len);
   if (msg != ERR_PTR(-EAGAIN))
       goto out;
 
   if (rtdm_fd_flags(&mqd->fd) & O_NONBLOCK)
       goto out;
 
   if (fetch_timeout) {
       xnlock_put_irqrestore(&nklock, s);
       ret = fetch_timeout(&ts, u_ts);
       if (ret)
           return ERR_PTR(ret);
       if (!timespec64_valid(&ts))
           return ERR_PTR(-EINVAL);
       to = ts2ns(&ts) + 1;
       tmode = XN_REALTIME;
       fetch_timeout = NULL;
       goto redo;
   }
 
   mq = mqd->mq;
   xnthread_prepare_wait(&mwc.wc);
   ret = xnsynch_sleep_on(&mq->receivers, to, tmode);
   if (ret == 0)
       msg = mwc.msg;
   else if (ret & XNRMID)
       msg = ERR_PTR(-EBADF);
   else if (ret & XNTIMEO)
       msg = ERR_PTR(-ETIMEDOUT);
   else
       msg = ERR_PTR(-EINTR);
out:
   xnlock_put_irqrestore(&nklock, s);
 
   return msg;
}
 
static int
mq_finish_rcv(struct cobalt_mqd *mqd, struct cobalt_msg *msg)
{
   spl_t s;
 
   xnlock_get_irqsave(&nklock, s);
   mq_release_msg(mqd->mq, msg);
   xnsched_run();
   xnlock_put_irqrestore(&nklock, s);
 
   return 0;
}
 
static inline int mq_getattr(struct cobalt_mqd *mqd, struct mq_attr *attr)
{
   struct cobalt_mq *mq;
   spl_t s;
 
   mq = mqd->mq;
   *attr = mq->attr;
   xnlock_get_irqsave(&nklock, s);
   attr->mq_flags = rtdm_fd_flags(&mqd->fd);
   attr->mq_curmsgs = mq->nrqueued;
   xnlock_put_irqrestore(&nklock, s);
 
   return 0;
}
 
static inline int
mq_notify(struct cobalt_mqd *mqd, unsigned index, const struct sigevent *evp)
{
   struct cobalt_thread *thread = cobalt_current_thread();
   struct cobalt_mq *mq;
   int err;
   spl_t s;
 
   if (evp && ((evp->sigev_notify != SIGEV_SIGNAL &&
            evp->sigev_notify != SIGEV_NONE) ||
           (unsigned int)(evp->sigev_signo - 1) > SIGRTMAX - 1))
       return -EINVAL;
 
   if (xnsched_interrupt_p() || thread == NULL)
       return -EPERM;
 
   xnlock_get_irqsave(&nklock, s);
   mq = mqd->mq;
   if (mq->target && mq->target != thread) {
       err = -EBUSY;
       goto unlock_and_error;
   }
 
   if (evp == NULL || evp->sigev_notify == SIGEV_NONE)
       /* Here, mq->target == cobalt_current_thread() or NULL. */
       mq->target = NULL;
   else {
       mq->target = thread;
       mq->target_qd = index;
       mq->si.si_signo = evp->sigev_signo;
       mq->si.si_errno = 0;
       mq->si.si_code = SI_MESGQ;
       mq->si.si_value = evp->sigev_value;
       /*
        * XXX: we differ from the regular kernel here, which
        * passes the sender's pid/uid data into the
        * receiver's namespaces. We pass the receiver's creds
        * into the init namespace instead.
        */
       mq->si.si_pid = task_pid_nr(current);
       mq->si.si_uid = get_current_uuid();
   }
 
   xnlock_put_irqrestore(&nklock, s);
   return 0;
 
      unlock_and_error:
   xnlock_put_irqrestore(&nklock, s);
   return err;
}
 
static inline struct cobalt_mqd *cobalt_mqd_get(mqd_t ufd)
{
   struct rtdm_fd *fd;
 
   fd = rtdm_fd_get(ufd, COBALT_MQD_MAGIC);
   if (IS_ERR(fd)) {
       int err = PTR_ERR(fd);
       if (err == -EADV)
           err = cobalt_current_process() ? -EBADF : -EPERM;
       return ERR_PTR(err);
   }
 
   return container_of(fd, struct cobalt_mqd, fd);
}
 
static inline void cobalt_mqd_put(struct cobalt_mqd *mqd)
{
   rtdm_fd_put(&mqd->fd);
}
 
int __cobalt_mq_notify(mqd_t fd, const struct sigevent *evp)
{
   struct cobalt_mqd *mqd;
   int ret;
 
   mqd = cobalt_mqd_get(fd);
   if (IS_ERR(mqd))
       ret = PTR_ERR(mqd);
   else {
       trace_cobalt_mq_notify(fd, evp);
       ret = mq_notify(mqd, fd, evp);
       cobalt_mqd_put(mqd);
   }
 
   return ret;
}
 
COBALT_SYSCALL(mq_notify, primary,
          (mqd_t fd, const struct sigevent *__user evp))
{
   struct sigevent sev;
 
   if (evp && cobalt_copy_from_user(&sev, evp, sizeof(sev)))
       return -EFAULT;
 
   return __cobalt_mq_notify(fd, evp ? &sev : NULL);
}
 
int __cobalt_mq_open(const char __user *u_name, int oflags,
            mode_t mode, struct mq_attr *attr)
{
   char name[COBALT_MAXNAME];
   unsigned int len;
   mqd_t uqd;
   int ret;
 
   len = cobalt_strncpy_from_user(name, u_name, sizeof(name));
   if (len < 0)
       return -EFAULT;
 
   if (len >= sizeof(name))
       return -ENAMETOOLONG;
 
   if (len == 0)
       return -EINVAL;
 
   trace_cobalt_mq_open(name, oflags, mode);
 
   uqd = __rtdm_anon_getfd("[cobalt-mq]", oflags);
   if (uqd < 0)
       return uqd;
 
   ret = mq_open(uqd, name, oflags, mode, attr);
   if (ret < 0) {
       __rtdm_anon_putfd(uqd);
       return ret;
   }
 
   return uqd;
}
 
COBALT_SYSCALL(mq_open, lostage,
          (const char __user *u_name, int oflags,
       mode_t mode, struct mq_attr __user *u_attr))
{
   struct mq_attr _attr, *attr = &_attr;
 
   if ((oflags & O_CREAT) && u_attr) {
       if (cobalt_copy_from_user(&_attr, u_attr, sizeof(_attr)))
           return -EFAULT;
   } else
       attr = NULL;
 
   return __cobalt_mq_open(u_name, oflags, mode, attr);
}
 
COBALT_SYSCALL(mq_close, lostage, (mqd_t uqd))
{
   trace_cobalt_mq_close(uqd);
 
   return mq_close(uqd);
}
 
COBALT_SYSCALL(mq_unlink, lostage, (const char __user *u_name))
{
   char name[COBALT_MAXNAME];
   unsigned len;
 
   len = cobalt_strncpy_from_user(name, u_name, sizeof(name));
   if (len < 0)
       return -EFAULT;
   if (len >= sizeof(name))
       return -ENAMETOOLONG;
 
   trace_cobalt_mq_unlink(name);
 
   return mq_unlink(name);
}
 
int __cobalt_mq_getattr(mqd_t uqd, struct mq_attr *attr)
{
   struct cobalt_mqd *mqd;
   int ret;
 
   mqd = cobalt_mqd_get(uqd);
   if (IS_ERR(mqd))
       return PTR_ERR(mqd);
 
   ret = mq_getattr(mqd, attr);
   cobalt_mqd_put(mqd);
   if (ret)
       return ret;
 
   trace_cobalt_mq_getattr(uqd, attr);
 
   return 0;
}
 
COBALT_SYSCALL(mq_getattr, current,
          (mqd_t uqd, struct mq_attr __user *u_attr))
{
   struct mq_attr attr;
   int ret;
 
   ret = __cobalt_mq_getattr(uqd, &attr);
   if (ret)
       return ret;
 
   return cobalt_copy_to_user(u_attr, &attr, sizeof(attr));
}
 
static inline int mq_fetch_timeout(struct timespec64 *ts,
                  const void __user *u_ts)
{
   return u_ts == NULL ? -EFAULT : cobalt_get_u_timespec(ts, u_ts);
}
 
static inline int mq_fetch_timeout64(struct timespec64 *ts,
                    const void __user *u_ts)
{
   return u_ts == NULL ? -EFAULT : cobalt_get_timespec64(ts, u_ts);
}
 
int __cobalt_mq_timedsend(mqd_t uqd, const void __user *u_buf, size_t len,
             unsigned int prio, const void __user *u_ts,
             int (*fetch_timeout)(struct timespec64 *ts,
                          const void __user *u_ts))
{
   struct cobalt_msg *msg;
   struct cobalt_mqd *mqd;
   int ret;
 
   mqd = cobalt_mqd_get(uqd);
   if (IS_ERR(mqd))
       return PTR_ERR(mqd);
 
   if (prio >= COBALT_MSGPRIOMAX) {
       ret = -EINVAL;
       goto out;
   }
 
   if (len > 0 && !access_rok(u_buf, len)) {
       ret = -EFAULT;
       goto out;
   }
 
   trace_cobalt_mq_send(uqd, u_buf, len, prio);
   msg = mq_timedsend_inner(mqd, len, u_ts, fetch_timeout);
   if (IS_ERR(msg)) {
       ret = PTR_ERR(msg);
       goto out;
   }
 
   ret = cobalt_copy_from_user(msg->data, u_buf, len);
   if (ret) {
       mq_finish_rcv(mqd, msg);
       goto out;
   }
   msg->len = len;
   msg->prio = prio;
   ret = mq_finish_send(mqd, msg);
out:
   cobalt_mqd_put(mqd);
 
   return ret;
}
 
int __cobalt_mq_timedsend64(mqd_t uqd, const void __user *u_buf, size_t len,
               unsigned int prio, const void __user *u_ts)
{
   return __cobalt_mq_timedsend(uqd, u_buf, len, prio, u_ts,
                    u_ts ? mq_fetch_timeout64 : NULL);
}
 
COBALT_SYSCALL(mq_timedsend, primary,
          (mqd_t uqd, const void __user *u_buf, size_t len,
       unsigned int prio, const struct __user_old_timespec __user *u_ts))
{
   return __cobalt_mq_timedsend(uqd, u_buf, len, prio,
                    u_ts, u_ts ? mq_fetch_timeout : NULL);
}
 
COBALT_SYSCALL(mq_timedsend64, primary,
          (mqd_t uqd, const void __user *u_buf, size_t len,
       unsigned int prio, const struct __kernel_timespec __user *u_ts))
{
   return __cobalt_mq_timedsend64(uqd, u_buf, len, prio, u_ts);
}
 
int __cobalt_mq_timedreceive(mqd_t uqd, void __user *u_buf,
                ssize_t *lenp,
                unsigned int __user *u_prio,
                const void __user *u_ts,
                int (*fetch_timeout)(struct timespec64 *ts,
                         const void __user *u_ts))
{
   struct cobalt_mqd *mqd;
   struct cobalt_msg *msg;
   unsigned int prio;
   int ret;
 
   mqd = cobalt_mqd_get(uqd);
   if (IS_ERR(mqd))
       return PTR_ERR(mqd);
 
   if (*lenp > 0 && !access_wok(u_buf, *lenp)) {
       ret = -EFAULT;
       goto fail;
   }
 
   msg = mq_timedrcv_inner(mqd, *lenp, u_ts, fetch_timeout);
   if (IS_ERR(msg)) {
       ret = PTR_ERR(msg);
       goto fail;
   }
 
   ret = cobalt_copy_to_user(u_buf, msg->data, msg->len);
   if (ret) {
       mq_finish_rcv(mqd, msg);
       goto fail;
   }
 
   *lenp = msg->len;
   prio = msg->prio;
   ret = mq_finish_rcv(mqd, msg);
   if (ret)
       goto fail;
 
   cobalt_mqd_put(mqd);
 
   if (u_prio && __xn_put_user(prio, u_prio))
       return -EFAULT;
 
   return 0;
fail:
   cobalt_mqd_put(mqd);
 
   return ret;
}
 
int __cobalt_mq_timedreceive64(mqd_t uqd, void __user *u_buf,
                  ssize_t *len,
                  unsigned int __user *u_prio,
                  const void __user *u_ts)
{
   return __cobalt_mq_timedreceive(uqd, u_buf, len, u_prio, u_ts,
                   u_ts ? mq_fetch_timeout64 : NULL);
}
 
COBALT_SYSCALL(mq_timedreceive, primary,
          (mqd_t uqd, void __user *u_buf,
       ssize_t __user *u_len,
       unsigned int __user *u_prio,
       const struct __user_old_timespec __user *u_ts))
{
   ssize_t len;
   int ret;
 
   ret = cobalt_copy_from_user(&len, u_len, sizeof(len));
   if (ret)
       return ret;
 
   ret = __cobalt_mq_timedreceive(uqd, u_buf, &len, u_prio,
                      u_ts, u_ts ? mq_fetch_timeout : NULL);
 
   return ret ?: cobalt_copy_to_user(u_len, &len, sizeof(*u_len));
}
 
COBALT_SYSCALL(mq_timedreceive64, primary,
          (mqd_t uqd, void __user *u_buf, ssize_t __user *u_len,
       unsigned int __user *u_prio,
       const struct __kernel_timespec __user *u_ts))
{
   ssize_t len;
   int ret;
 
   ret = cobalt_copy_from_user(&len, u_len, sizeof(len));
   if (ret)
       return ret;
 
   ret = __cobalt_mq_timedreceive64(uqd, u_buf, &len, u_prio, u_ts);
 
   return ret ?: cobalt_copy_to_user(u_len, &len, sizeof(*u_len));
}