-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathisol.c
5209 lines (4836 loc) · 127 KB
/
isol.c
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
/*
* Task isolation support.
*
* By Alex Belits <[email protected]>
*
* This is the userspace part of the task isolation support for Linux.
* It requires task isolation patch applied to he kernel. That patch
* is based on the task isolation patch v15 by Chris Metcalf
* <[email protected]> ported to kernel 4.9.x, plus the update
* that disables timers while isolated tasks are running.
*
* The kernel configuration requires the following:
*
* Should be set:
*
* CONFIG_TASK_ISOLATION
* CONFIG_TICK_ONESHOT
* CONFIG_NO_HZ_COMMON
* CONFIG_NO_HZ_FULL
* CONFIG_HIGH_RES_TIMERS
* CONFIG_RCU_NOCB_CPU
*
* Should NOT be set:
*
* CONFIG_NO_HZ_FULL_SYSIDLE
* CONFIG_HZ_PERIODIC
* CONFIG_NO_HZ_IDLE
* CONFIG_NO_HZ_FULL_SYSIDLE
* CONFIG_NO_HZ
*
* May be set for convenience:
*
* CONFIG_TASK_ISOLATION_ALL
* CONFIG_NO_HZ_FULL_ALL
* CONFIG_RCU_NOCB_CPU_ALL
*
* Please use "make menuconfig" and provided examples to create
* compatible kernel configuration. Use the list above to verify the
* configuration results.
*
*
* This is the thread-based implementation, intended to run the
* manager and all isolated tasks as threads within a single process.
*
*/
/*
Some functions may ignore parameters intended for future functionality.
This is intentional.
*/
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-result"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <errno.h>
#include <dirent.h>
#include <sched.h>
#include <pthread.h>
#include <signal.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/file.h>
#include <sys/prctl.h>
/* Internal functions */
#include "isol-internals.h"
/* Server */
#include "isol-server.h"
/* TMC for ODP */
#include <tmc/isol.h>
/* Compile-time options for debugging output */
/* Enable message on startup */
#ifndef DEBUG_ISOL_STARTUP_MESSAGE
#define DEBUG_ISOL_STARTUP_MESSAGE 0
#endif
/* Show all timers for debugging */
#ifndef DEBUG_ISOL_ALWAYS_SHOW_ALL_TIMERS
#define DEBUG_ISOL_ALWAYS_SHOW_ALL_TIMERS 0
#endif
/* List all timer entries, not just high resolution timers. */
#ifndef DEBUG_ISOL_LIST_ALL_TIMER_ENTRIES
#define DEBUG_ISOL_LIST_ALL_TIMER_ENTRIES 0
#endif
/* Maintain and show names for processes and timers. */
#ifndef DEBUG_ISOL_NAMES
#define DEBUG_ISOL_NAMES 0
#endif
/* Verbose debugging messages. */
#ifndef DEBUG_ISOL_VERBOSE
#define DEBUG_ISOL_VERBOSE 0
#endif
/* Compile-time options for monitoring implementation. */
/* Two monitoring methods are supported */
#if (!defined(ISOLATION_MONITOR_IN_MASTER) \
&& !defined(ISOLATION_MONITOR_IN_SLAVE))
#define ISOLATION_MONITOR_IN_MASTER 1
#define ISOLATION_MONITOR_IN_SLAVE 0
#else
#if (defined(ISOLATION_MONITOR_IN_MASTER) \
&& !defined(ISOLATION_MONITOR_IN_SLAVE))
#define ISOLATION_MONITOR_IN_SLAVE (1 - ISOLATION_MONITOR_IN_MASTER)
#else
#if (defined(ISOLATION_MONITOR_IN_SLAVE) \
&& !defined(ISOLATION_MONITOR_IN_MASTER))
#define ISOLATION_MONITOR_IN_MASTER (1 - ISOLATION_MONITOR_IN_SLAVE)
#endif
#endif
#endif
/* Use CPU subsets to support multiple applications. */
#ifndef USE_CPU_SUBSETS
#define USE_CPU_SUBSETS 1
#endif
/*
CPU subsets file. Used only if CPU subsets feature is
enabled. Contains entries in the format:
<subset name>:<cpu list>
ex:
1:1-12
2:13-23
*/
#if USE_CPU_SUBSETS
#define CPU_SUBSETS_FILE "/etc/cpu_subsets"
#endif
/*
The following is specific to the patched kernel, and may be
incompatible with other kernel versions. If the build environment
includes patched headers (with PR_SET_TASK_ISOLATION defined), the
values from there will be used.
*/
#ifndef PR_SET_TASK_ISOLATION
#define PR_SET_TASK_ISOLATION 48
#define PR_GET_TASK_ISOLATION 49
#define PR_TASK_ISOLATION_ENABLE (1 << 0)
#define PR_TASK_ISOLATION_USERSIG (1 << 1)
#define PR_TASK_ISOLATION_SET_SIG(sig) (((sig) & 0x7f) << 8)
#define PR_TASK_ISOLATION_GET_SIG(bits) (((bits) >> 8) & 0x7f)
#define PR_TASK_ISOLATION_NOSIG \
(PR_TASK_ISOLATION_USERSIG | PR_TASK_ISOLATION_SET_SIG(0))
#endif
/*
* IPC mechanism based entirely on shared or common memory.
*
* This is a work in progress, so currently it is written for easier
* debugging, not performance. The interface will mostly remain the
* same, however some functions will have to be changed to inline and
* macros to avoid overhead. Internals are subject to change.
*/
/* For now, user and encoded and block sizes will look like this. */
#define SEVEN (7)
#define EIGHT (8)
/* Memory area. */
#define AREA_SIZE (4096)
/*
* Per-process or per-thread ID.
* For now, use pthread_t, however it may be changed to pid_t for external
* manager process.
*/
static __thread pthread_t memipc_my_pid = 0;
static __thread int memipc_thread_launch_confirmed = 0;
__thread int memipc_thread_continue_flag = 1;
__thread int memipc_thread_ok_leave_flag = 0;
static unsigned char newdata_one = 1;
__thread volatile unsigned char *memipc_check_newdata_ptr = &newdata_one;
__thread volatile unsigned char memipc_check_signal = 0;
struct memipc_thread_params;
static __thread struct memipc_thread_params *memipc_thread_self = NULL;
static __thread int memipc_thread_fd = -1;
/* Memory area descriptor. */
struct memipc_area
{
unsigned char volatile *area;
unsigned char volatile *wptr;
unsigned char volatile *rptr;
size_t size;
size_t inbuffer;
pthread_t writer;
pthread_t reader;
};
/*
* Request header.
* Does not exist in memory but assumed in message layout.
*/
struct memipc_req_header
{
char t;
uint32_t size;
} __attribute__((packed));
char *memipc_area_name(int cpu)
{
char *s;
s = (char *)malloc(37);
if (s == NULL) return NULL;
/* 0123456789012345 <- 16 bytes*/
snprintf(s, 37, "/isol_server_CPU%d", cpu);
return s;
}
/*
* Create an area descriptor and allocate the area.
*/
struct memipc_area *memipc_area_create(size_t size, size_t map_size,
size_t offset, int fd,
unsigned char *ptr)
{
unsigned char *p;
struct memipc_area *area;
area = malloc(sizeof(struct memipc_area));
if (area == NULL)
return NULL;
if (ptr == NULL) {
p = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, offset);
if ( p == MAP_FAILED) {
free(area);
return NULL;
}
} else
p = ptr + offset;
area->area = p;
area->wptr = p;
area->rptr = p;
area->size = size;
area->inbuffer = 0;
area->writer = 0;
area->reader = 0;
return area;
}
/*
* Create a duplicate area descriptor for threaded model.
*
* This is necessary because all indexes and counters are supposed to
* be private to the reader and writer threads or processes while
* buffer itself is shared. So the same buffer is visible from those
* threads through two initially identical descriptors.
*
* The mechanism will not work properly if area descriptors are shared
* between threads.
*/
struct memipc_area *memipc_area_dup(struct memipc_area *src)
{
struct memipc_area *dst;
if (src == NULL)
return NULL;
dst = malloc(sizeof(struct memipc_area));
if (dst == NULL)
return NULL;
memcpy(dst, src, sizeof(struct memipc_area));
return dst;
}
/*
* Delete area and its descriptor.
*/
void memipc_area_delete(struct memipc_area *area)
{
if (area == NULL)
return;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
munmap((void *)area->area, area->size);
#pragma GCC diagnostic pop
free(area);
}
/*
* Delete area descriptor that was created as a duplicate.
*
* There is no reference counting in this mechanism, caller is
* responsible to make sure that one of the two descriptors is deleted
* with memipc_area_delete() and another with
* memipc_area_delete_duplicate().
*/
void memipc_area_delete_duplicate(struct memipc_area *area)
{
if (area == NULL)
return;
free(area);
}
/*
* Write SEVEN bytes encoded as EIGHT bytes.
*
* Nonzero return value means that write area is not ready.
*/
static int write_encode_bytes(unsigned char volatile *dst,
const unsigned char *src,
unsigned int size)
{
unsigned char src0, src1, src2, src3, src4, src5, src6,
dst0, dst1, dst2, dst3, dst4, dst5, dst6, dst7;
__atomic_thread_fence(__ATOMIC_SEQ_CST);
/*
Do not write if all data in the block is not read yet,
or if read marker was not yet propagated to this core.
*/
if ((1 & (dst[0] | dst[1] | dst[2] | dst[3]
| dst[4] | dst[5] | dst[6] | dst[7])) != 0)
return -1;
/* Copy everything to the local variables */
switch (size) {
case 0:
src0 = 0;
/* Falls through */
case 1:
src1 = 0;
/* Falls through */
case 2:
src2 = 0;
/* Falls through */
case 3:
src3 = 0;
/* Falls through */
case 4:
src4 = 0;
/* Falls through */
case 5:
src5 = 0;
/* Falls through */
case 6:
src6 = 0;
/* Falls through */
default:
break;
}
switch (size) {
case 7:
src6 = src[6];
/* Falls through */
case 6:
src5 = src[5];
/* Falls through */
case 5:
src4 = src[4];
/* Falls through */
case 4:
src3 = src[3];
/* Falls through */
case 3:
src2 = src[2];
/* Falls through */
case 2:
src1 = src[1];
/* Falls through */
case 1:
src0 = src[0];
/* Falls through */
default:
break;
}
/* Encode */
dst0 = (src0 << 1) | 1;
dst1 = ((src0 & 0x80 ) >> 6) | (src1 << 2) | 1;
dst2 = ((src1 & 0xc0 ) >> 5) | (src2 << 3) | 1;
dst3 = ((src2 & 0xe0 ) >> 4) | (src3 << 4) | 1;
dst4 = ((src3 & 0xf0 ) >> 3) | (src4 << 5) | 1;
dst5 = ((src4 & 0xf8 ) >> 2) | (src5 << 6) | 1;
dst6 = ((src5 & 0xfc ) >> 1) | (src6 << 7) | 1;
dst7 = src6 | 1;
/* Write */
dst[0] = dst0;
dst[1] = dst1;
dst[2] = dst2;
dst[3] = dst3;
dst[4] = dst4;
dst[5] = dst5;
dst[6] = dst6;
dst[7] = dst7;
__atomic_thread_fence(__ATOMIC_SEQ_CST);
return 0;
}
/*
* Write SEVEN bytes encoded as EIGHT bytes.
*
* Nonzero return value means that write area is not ready.
*/
static int write_encode_bytes_with_header(unsigned char volatile *dst,
const unsigned char *src,
unsigned int size,
char t, uint32_t msize)
{
unsigned char src0, src1, src2, src3, src4, src5, src6,
dst0, dst1, dst2, dst3, dst4, dst5, dst6, dst7;
__atomic_thread_fence(__ATOMIC_SEQ_CST);
/*
Do not write if all data in the block is not read yet,
or if read marker was not yet propagated to this core.
*/
if ((1 & (dst[0] | dst[1] | dst[2] | dst[3]
| dst[4] | dst[5] | dst[6] | dst[7])) != 0)
return -1;
/* Copy everything to the local variables */
switch (size) {
case 0:
src5 = 0;
/* Falls through */
case 1:
src6 = 0;
/* Falls through */
default:
break;
}
switch (size) {
case 2:
src6 = src[1];
/* Falls through */
case 1:
src5 = src[0];
/* Falls through */
default:
src4 = (unsigned char)((msize >> 24) & 0xff);
src3 = (unsigned char)((msize >> 16) & 0xff);
src2 = (unsigned char)((msize >> 8) & 0xff);
src1 = (unsigned char)(msize & 0xff);
src0 = (unsigned char)t;
break;
}
/* Encode */
dst0 = (src0 << 1) | 1;
dst1 = ((src0 & 0x80 ) >> 6) | (src1 << 2) | 1;
dst2 = ((src1 & 0xc0 ) >> 5) | (src2 << 3) | 1;
dst3 = ((src2 & 0xe0 ) >> 4) | (src3 << 4) | 1;
dst4 = ((src3 & 0xf0 ) >> 3) | (src4 << 5) | 1;
dst5 = ((src4 & 0xf8 ) >> 2) | (src5 << 6) | 1;
dst6 = ((src5 & 0xfc ) >> 1) | (src6 << 7) | 1;
dst7 = src6 | 1;
/* Write */
dst[0] = dst0;
dst[1] = dst1;
dst[2] = dst2;
dst[3] = dst3;
dst[4] = dst4;
dst[5] = dst5;
dst[6] = dst6;
dst[7] = dst7;
__atomic_thread_fence(__ATOMIC_SEQ_CST);
return 0;
}
/*
* Read SEVEN bytes encoded as EIGHT bytes.
*
* Nonzero return value means that data is not available.
*/
static int read_decode_bytes(unsigned char *dst,
unsigned char volatile *src,
unsigned int size)
{
unsigned char src0, src1, src2, src3, src4, src5, src6, src7;
__atomic_thread_fence(__ATOMIC_SEQ_CST);
/* Read all values */
src0 = src[0];
src1 = src[1];
src2 = src[2];
src3 = src[3];
src4 = src[4];
src5 = src[5];
src6 = src[6];
src7 = src[7];
/*
Do not read if the data is not marked as written, or
markers are not yet propagated to this core.
*/
if ((1 & src0 & src1 & src2 & src3
& src4 & src5 & src6 & src7) != 1)
return -1;
/* Decode */
switch (size) {
case 7:
dst[6] = src6 >> 7 | (src7 & 0xfe);
/* Falls through */
case 6:
dst[5] = src5 >> 6 | ((src6 << 1) & 0xfc);
/* Falls through */
case 5:
dst[4] = src4 >> 5 | ((src5 << 2) & 0xf8);
/* Falls through */
case 4:
dst[3] = src3 >> 4 | ((src4 << 3) & 0xf0);
/* Falls through */
case 3:
dst[2] = src2 >> 3 | ((src3 << 4) & 0xe0);
/* Falls through */
case 2:
dst[1] = src1 >> 2 | ((src2 << 5) & 0xc0);
/* Falls through */
case 1:
dst[0] = src0 >> 1 | ((src1 << 6) & 0x80);
/* Falls through */
default:
break;
}
__atomic_thread_fence(__ATOMIC_SEQ_CST);
return 0;
}
/*
* Read SEVEN bytes encoded as EIGHT bytes.
*
* Nonzero return value means that data is not available.
*/
static int read_decode_bytes_with_header(unsigned char *dst,
unsigned char volatile *src,
unsigned int size,
char *type,
uint32_t *msize)
{
unsigned char src0, src1, src2, src3, src4, src5, src6, src7;
__atomic_thread_fence(__ATOMIC_SEQ_CST);
/* Read all values */
src0 = src[0];
src1 = src[1];
src2 = src[2];
src3 = src[3];
src4 = src[4];
src5 = src[5];
src6 = src[6];
src7 = src[7];
/*
Do not read if the data is not marked as written, or
markers are not yet propagated to this core.
*/
if ((1 & src0 & src1 & src2 & src3
& src4 & src5 & src6 & src7) != 1)
return -1;
/* Decode */
switch (size) {
case 2:
dst[1] = src6 >> 7 | (src7 & 0xfe);
/* Falls through */
case 1:
dst[0] = src5 >> 6 | ((src6 << 1) & 0xfc);
/* Falls through */
default:
*msize =
(uint32_t)((src4 >> 5 | ((src5 << 2) & 0xf8)) << 24)
| (uint32_t)((src3 >> 4 | ((src4 << 3) & 0xf0)) << 16)
| (uint32_t)((src2 >> 3 | ((src3 << 4) & 0xe0)) << 8)
| (uint32_t)(src1 >> 2 | ((src2 << 5) & 0xc0));
*type = (char)(src0 >> 1 | ((src1 << 6) & 0x80));
break;
}
__atomic_thread_fence(__ATOMIC_SEQ_CST);
return 0;
}
/*
* Create a request in a given area.
*/
int memipc_add_req(struct memipc_area *area, enum memipc_req_type req_type,
ssize_t req_size, unsigned char *req_data)
{
size_t total_req_size, blocks_count, remainder,
blocks_available, blocks_available_1, blocks_available_2,
blocks_write, srcsize;
unsigned i;
unsigned char volatile *localptr, *endptr, *next_wptr;
unsigned char *srcptr;
unsigned inbuffer;
/* Are we supposed to write here? */
if (area->writer != memipc_my_pid) {
fprintf(stderr, "Process %lu attempted to write a request "
"to memipc area writable by a process %lu\n",
(unsigned long)memipc_my_pid,
(unsigned long)area->writer);
return -1;
}
localptr = area->rptr;
endptr = area->area + area->size;
inbuffer = area->inbuffer;
while ((((*localptr) & 1) == 0) && (inbuffer > 0)) {
localptr ++;
if (localptr >= endptr)
localptr = area->area;
inbuffer --;
}
area->rptr = localptr;
area->inbuffer = inbuffer;
/* Check if the area is full */
if (area->inbuffer == area->size)
return -1;
/*
Determine the amount of data to be written. We use SEVEN-byte
blocks, each consuming EIGHT bytes in the buffer.
*/
total_req_size = req_size + sizeof(struct memipc_req_header);
blocks_count = total_req_size / SEVEN;
remainder = total_req_size - (blocks_count * SEVEN);
if (remainder)
blocks_count++;
if (area->wptr < area->rptr) {
blocks_available_1 = (area->rptr - area->wptr) / EIGHT;
blocks_available_2 = 0;
blocks_available = blocks_available_1;
} else {
blocks_available_1 = (endptr - area->wptr) / EIGHT;
blocks_available_2 = (area->rptr - area->area) / EIGHT;
blocks_available = blocks_available_1 + blocks_available_2;
}
/* Check if there is enough space to write the request */
if (blocks_count > blocks_available)
return -1;
/* If there is a wrap around, start from writing wrapped data */
if (blocks_count > blocks_available_1) {
next_wptr = area->area
+ (blocks_count - blocks_available_1) * EIGHT;
for (i = blocks_count - blocks_available_1; i > 0; i --) {
srcptr = req_data + blocks_available_1 * SEVEN
+ i * SEVEN - sizeof(struct memipc_req_header);
if ((srcptr - req_data) >= SEVEN) {
srcsize = req_size
- (srcptr - SEVEN - req_data);
if (srcsize > SEVEN)
srcsize = SEVEN;
if (write_encode_bytes(area->area
+ ((i - 1) * EIGHT),
srcptr - SEVEN, srcsize))
return -1;
} else {
if (srcptr - req_data > req_size)
srcptr = req_data + req_size;
srcsize = srcptr - req_data;
if (write_encode_bytes_with_header(
area->area + ((i - 1) * EIGHT),
req_data, srcsize,
req_type,
req_size + sizeof(struct memipc_req_header)))
return -1;
}
}
blocks_write = blocks_available_1;
} else {
/* No wrap around */
blocks_write = blocks_count;
next_wptr = area->wptr + (blocks_write) * EIGHT;
if (next_wptr >= endptr)
next_wptr = area->area;
}
/* Write data that was not wrapped around */
for (i = blocks_write; i > 0 ; i --) {
srcptr = req_data
+ i * SEVEN - sizeof(struct memipc_req_header);
if ((srcptr - req_data) >= SEVEN) {
srcsize = req_size - (srcptr - SEVEN - req_data);
if (srcsize > SEVEN)
srcsize = SEVEN;
if (write_encode_bytes(area->wptr + ((i - 1) * EIGHT),
srcptr - SEVEN, srcsize))
return -1;
} else {
if (srcptr - req_data > req_size)
srcptr = req_data + req_size;
srcsize = srcptr - req_data;
if (write_encode_bytes_with_header(
area->wptr + ((i - 1) * EIGHT),
req_data, srcsize,
req_type,
req_size + sizeof(struct memipc_req_header)))
return -1;
}
}
area->inbuffer += blocks_count * EIGHT;
area->wptr = next_wptr;
__atomic_thread_fence(__ATOMIC_SEQ_CST);
return 0;
}
/*
* Clear memory.
*
* memset() can not be used here because it may write multiple times
* to the same location.
*/
static inline void memipc_clearmem(volatile unsigned char *p, size_t size)
{
while (size-- > 0) *p++ = 0;
}
static inline int memipc_check_newdata(void)
{
return (*memipc_check_newdata_ptr) & 1;
}
static inline struct memipc_area *get_s_memipc_mosi(struct
memipc_thread_params
*arg);
/*
* Get request from a given area.
*/
int memipc_get_req(struct memipc_area *area, enum memipc_req_type *req_type,
ssize_t *req_size, unsigned char *req_data)
{
size_t total_req_size, blocks_count, remainder,
blocks_count_1, blocks_count_2, dstsize;
unsigned i;
unsigned char volatile *localptr, *endptr, *curr_rptr;
unsigned char *dstptr;
unsigned inbuffer;
/* Are we supposed to read here? */
if (area->reader != memipc_my_pid) {
fprintf(stderr, "Process %lu attempted to read a request from "
"memipc area readable by a process %lu\n",
(unsigned long)memipc_my_pid,
(unsigned long)area->reader);
return -1;
}
localptr = area->wptr;
endptr = area->area + area->size;
/* Determine the amount of data in the buffer */
inbuffer = area->inbuffer;
while ((((*localptr) & 1) == 1) && (inbuffer < area->size)) {
localptr ++;
if (localptr >= endptr)
localptr = area->area;
inbuffer ++;
}
area->wptr = localptr;
area->inbuffer = inbuffer;
/* Check if the area has no complete blocks */
if (area->inbuffer < EIGHT)
return -1;
/* Read the first header */
uint32_t l_req_size;
char l_req_type;
if (read_decode_bytes_with_header(req_data, area->rptr,
SEVEN
- sizeof(struct memipc_req_header),
&l_req_type, &l_req_size))
return -1;
total_req_size = l_req_size;
/* Check if there is sufficient amount of space in the destination */
if (total_req_size > (*req_size + sizeof(struct memipc_req_header)))
return -2;
*req_size = total_req_size - sizeof(struct memipc_req_header);
*req_type = l_req_type;
if (total_req_size <= SEVEN) {
/* Just one block, already written */
/* Mark data as read */
memipc_clearmem(area->rptr, EIGHT);
__atomic_thread_fence(__ATOMIC_SEQ_CST);
area->rptr += EIGHT;
if (area->rptr >= endptr)
area->rptr = area->area;
area->inbuffer -= EIGHT;
/* Finished */
} else {
/* Multiple blocks */
blocks_count = total_req_size / SEVEN;
remainder = total_req_size - blocks_count * SEVEN;
if (remainder)
blocks_count++;
/* Check if there is sufficient amount of data in the buffer */
if (area->inbuffer < (blocks_count * EIGHT))
return -1;
blocks_count_1 = (endptr - area->rptr) / EIGHT;
if (blocks_count <= blocks_count_1) {
blocks_count_1 = blocks_count;
blocks_count_2 = 0;
} else {
blocks_count_2 = blocks_count - blocks_count_1;
}
/* First block already copied */
dstptr = req_data + SEVEN - sizeof(struct memipc_req_header);
/* Read remaining data that was not wrapped around, if any */
curr_rptr = area->rptr;
for (i = 1; i < blocks_count_1; i++) {
dstsize = total_req_size - i * SEVEN;
if (dstsize > SEVEN)
dstsize = SEVEN;
if (read_decode_bytes(dstptr, curr_rptr + i * EIGHT,
dstsize))
return -1;
dstptr += dstsize;
}
curr_rptr += blocks_count_1 * EIGHT;
/* If there is a wrap around, read wrapped around data */
if (curr_rptr >= endptr) {
curr_rptr = area->area;
if (blocks_count_2 > 0) {
for (i = 0; i < blocks_count_2; i++) {
dstsize = total_req_size
- (blocks_count_1 + i) * SEVEN;
if (dstsize > SEVEN)
dstsize = SEVEN;
if (read_decode_bytes(dstptr,
curr_rptr + i * EIGHT,
dstsize))
return -1;
dstptr += dstsize;
}
curr_rptr += blocks_count_2 * EIGHT;
memipc_clearmem(area->area,
blocks_count_2 * EIGHT);
}
}
memipc_clearmem(area->rptr, blocks_count_1 * EIGHT);
__atomic_thread_fence(__ATOMIC_SEQ_CST);
area->rptr = curr_rptr;
area->inbuffer -= blocks_count * EIGHT;
/* Finished */
}
/* If we are reading from the thread input area, update the pointer */
if ((memipc_thread_self != NULL)
&& (area == get_s_memipc_mosi(memipc_thread_self)))
memipc_check_newdata_ptr = area->rptr;
return 0;
}
/*
* Enter isolation mode.
*
* This should be only called internally, from the request handler.
*/
static int start_isolation(int cpu)
{
cpu_set_t set;
/* Exit from isolation, if still in isolation mode */
prctl(PR_SET_TASK_ISOLATION, 0, 0, 0, 0);
if (mlockall(MCL_CURRENT))
return -1;
CPU_ZERO(&set);
CPU_SET(cpu, &set);
if (sched_setaffinity(0, sizeof(cpu_set_t), &set))
return -1;
return prctl(PR_SET_TASK_ISOLATION,
PR_TASK_ISOLATION_ENABLE
| PR_TASK_ISOLATION_USERSIG
| PR_TASK_ISOLATION_SET_SIG(SIGUSR1), 0, 0, 0);
}
/* CPU sets for isolation */
static cpu_set_t _global_nonisol_cpuset, _global_isol_cpuset,
_global_running_cpuset;
#if USE_CPU_SUBSETS
static char *server_socket_name = NULL;
#endif
/*
* Exit from isolation mode.
*
* This should be only called internally, from the request handler.
*/
static void exit_isolation(void)
{
/* Exit from isolation */
prctl(PR_SET_TASK_ISOLATION, 0, 0, 0, 0);
/*
The following, plus the mechanism that pushes processes away
from cores used for isolation, causes CPU to [eventually] become
idle, so scheduling timer will not be restarted. This seems to
be the only way to stop the scheduler timer once it started on
an isolated core despite the lack of other processes and threads
to schedule there.
*/
sched_setaffinity(0, sizeof(cpu_set_t), &_global_nonisol_cpuset);
}
/*
* Text parser internals
*/
/* Skip whitespace. */
static void skip_whitespace(const char **p)
{
while ((**p != 0) && ((unsigned const char)(**p) <= ' '))
(*p)++;
}
/* Find the end of a non-whitespace token. */
static const char *find_endtoken(const char *p)
{
while ((*p != 0) && ((unsigned const char)(*p) > ' '))
p++;
return p;
}
/* Skip a particular word. */
static int skip_word(const char **p, const char *word)
{
const char *end, *curr;
curr = *p;
skip_whitespace(&curr);
end = find_endtoken(curr);
if (((end - curr) == (ssize_t) strlen(word))
&& !strncmp(curr, word, end - curr)) {
*p = end;
skip_whitespace(p);
return 0;
} else
return -1;
}
#if USE_CPU_SUBSETS
/* Skip whitespace. */
static void skip_whitespace_nconst(char **p)
{
while ((**p != 0) && ((unsigned char)(**p) <= ' '))
(*p)++;
}
/* Find the end of a non-whitespace token. */
static char *find_endtoken_nconst(char *p)
{
while ((*p != 0) && ((unsigned const char)(*p) > ' '))
p++;
return p;
}
#endif
/* Get value from a hex digit. */
static unsigned char unhex(char c)
{
if ((c >= '0') && (c <= '9'))