-
Notifications
You must be signed in to change notification settings - Fork 5
/
cosmix_runtime.cpp
2263 lines (1813 loc) · 52 KB
/
cosmix_runtime.cpp
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
/* CoSMIX Runtime Layer */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include "../include/common.h"
#ifndef SDK_BUILD
#include <strings.h>
#include <assert.h>
#include <stdarg.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <time.h>
#include <utime.h>
#include <dirent.h>
#include <setjmp.h>
#include <mntent.h>
#include <libgen.h>
#include <getopt.h>
#include <ftw.h>
#include <poll.h>
#include <pthread.h>
#include <libintl.h>
#include <nl_types.h>
#include <iconv.h>
#include <locale.h>
#include <langinfo.h>
#include <monetary.h>
#include <wchar.h>
#include <semaphore.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <grp.h>
#include <pwd.h>
#include <shadow.h>
#include <signal.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/timeb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <sys/statvfs.h>
#include <sys/resource.h>
#include <sys/utsname.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <sys/sem.h>
#include <sys/mman.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <malloc.h>
#endif
#ifdef ANJUNA_BUILD
#define __NR_alloc_untrusted (346)
#endif
#ifdef __cplusplus
extern "C" {
#endif
#define TRUE 1
#define FALSE 0
// global print function - declared in common header
DBG_FUNC g_debug;
// TLB Section
#define TLB_SIZE 5
struct s_victim_cache {
int32_t bs_page_index;
uint16_t epc_page_index;
uint8_t is_dirty;
};
// Helper functions typedefs
typedef int (*t_mem_function)(void*, void*, size_t);
typedef uintptr_t (*t_mem_function_single)(void*, int, size_t);
#ifdef JEMALLOC
extern void* je_malloc(size_t size);
extern void* je_calloc(size_t count, size_t size);
extern void* je_realloc(void* ptr, size_t size);
extern size_t je_malloc_usable_size(void* ptr);
extern void je_free(void* ptr);
#endif
#ifdef TCMALLOC
extern void* tc_malloc(size_t size);
extern void* tc_calloc(size_t count, size_t size);
extern void* tc_realloc(void* ptr, size_t size);
extern void tc_free(void* ptr);
#endif
__thread struct s_victim_cache gt_TLB[TLB_SIZE];
static __thread void* gt_direct_buffer;
// Optimization to not instrument file I/O libc wrappers if there is no mmap mstore in action for this application
//
static char g_storage_used = FALSE;
#define MAX_ACCESS_SIZE 64
__thread char gt_CrossPageBuffer[MAX_ACCESS_SIZE];
__thread uintptr_t gt_crosspage_access;
__thread int gt_crosspage_access_size;
static void* __cosmix_get_mstore_direct_buffer(size_t mpage_size)
{
if (!gt_direct_buffer)
{
gt_direct_buffer = memalign(mpage_size, mpage_size);
ASSERT(gt_direct_buffer);
}
return gt_direct_buffer;
}
/* Helper debugging functions */
#ifdef SDK_BUILD
extern "C" void ocall_debug(const char* str);
extern "C" void ocall_untrusted_alloc(void** ptr, size_t size);
#endif
void __cosmix_debug(const char *fmt, ...)
{
char buf[BUFSIZ] = {'\0'};
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, BUFSIZ, fmt, ap);
va_end(ap);
#ifdef SDK_BUILD
ocall_debug(buf);
#else
fprintf(stderr,"%s", buf);
#endif
}
void __cosmix_initialize()
{
g_debug = __cosmix_debug;
g_storage_used = FALSE;
}
void __cosmix_fail_asm(const void* arg)
{
if (IS_COSMIX_PTR(arg))
{
g_debug("[ERROR] - Calling inline ASM with COSMIX pointer\n");
abort();
}
}
void __cosmix_fail(const void* funcName, const void* arg)
{
if (IS_COSMIX_PTR(arg))
{
g_debug("[ERROR] - Calling a function without wrapper...exiting: %s %p\n", (char*)funcName, arg);
abort();
}
}
void __cosmix_test_atomic(const void* ptr)
{
if (IS_COSMIX_PTR(ptr))
{
g_debug("[ERROR] - attempted to perform atomic operation on a COSMIX ptr...aborting\n");
abort();
}
}
// Helper function to validate all counters (and reference counters in the PT)
//
void __cosmix_debug_interal()
{
// TODO: foreach cached-mstore call a helper method for debugging correct release of handlers
// the compiler should insturment the debug methods for the actual mstore debug methods
// debug_ref_count();
}
/* Forward declarations */
void* __cosmix_memset(void *dest, int c, size_t n);
int __cosmix_memcmp(const void *vl, const void *vr, size_t n);
void* __cosmix_memcpy(void *__restrict__ dest, const void *__restrict__ src, size_t n);
void* __cosmix_memmove(void *dest, const void *src, size_t n);
/* Init & Cleanup wrappers */
void* __cosmix_allocate_buffer(size_t size)
{
// Note: we always allocate with extra HW PAGE to later align to hardware pages for better performance.
//
size_t alloc_size = size + 0x1000;
void* bs_ptr = NULL;
#ifdef SDK_BUILD
ocall_untrusted_alloc(&bs_ptr, alloc_size);
#elif ANJUNA_BUILD
#warning Using alloc_untrusted system call exported by Anjuna Runtime
syscall(__NR_alloc_untrusted, alloc_size, &bs_ptr);
#else
// Note: workaround for SCONE. They don't have page cache,
// so allocating annonymous memory backed by "a file" will actually be untrusted memory
//
int fd = open("/dev/zero", O_RDWR);
bs_ptr = mmap(0, alloc_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
close(fd);
ASSERT (bs_ptr != MAP_FAILED);
#endif
ASSERT (bs_ptr);
return bs_ptr;
}
extern struct s_victim_cache* mstore_get_tlb();
struct s_victim_cache* mstore_get_tlb_template()
{
return gt_TLB;
}
// extern struct s_victim_cache* mstore_get_tlb();
extern int mstore_init(void* priv_data);
extern int mstore_cleanup();
extern uintptr_t mstore_get_mstorage_page(uintptr_t ptr);
extern uintptr_t mstore_get_mpage_cache_base_ptr();
extern void* mstore_alloc(size_t size, void* priv_data);
extern size_t mstore_get_mpage_size();
extern int mstore_get_mpage_bits();
extern void notify_tlb_cached(void* ptr);
extern void notify_tlb_dropped(void* ptr, bool dirty);
extern void* mpf_handler_c(void* ptr);
extern void mpf_handler_d(void* ptr, void* dst, size_t size);
extern void mstore_write_back(void* ptr, void* src, size_t size);
extern void mstore_open(char* filename, int fd, char always_register, char* registered);
extern void mstore_close(int fd);
#ifndef SDK_BUILD
extern ssize_t mstore_write(int fd, const void* buf, size_t count);
extern ssize_t mstore_read(int fd, void* buf, size_t count);
ssize_t default_mstore_write(int fd, const void* buf, size_t count)
{
return write(fd,buf,count);
}
ssize_t default_mstore_read(int fd, void* buf, size_t count)
{
return read(fd,buf,count);
}
#endif
uintptr_t direct_mstore_get_mpage_cache_base_ptr()
{
return 0;
}
uintptr_t direct_mstore_get_mstorage_page(uintptr_t ptr)
{
g_debug("direct_mstore_get_mstorage_page not supported\n");
abort();
return 0;
}
void default_mstore_open(char* filename, int fd, char always_register, char* registered)
{
}
void default_mstore_close(int fd)
{
}
void* default_mstore_alloc(size_t size, void* priv_data)
{
return nullptr;
}
extern void* _0__cosmix_link(const void* ptr, int ptr_size, char is_vector_type, char dirty);
extern void* _1__cosmix_link(const void* ptr, int ptr_size, char is_vector_type, char dirty);
extern void* _2__cosmix_link(const void* ptr, int ptr_size, char is_vector_type, char dirty);
extern void* _3__cosmix_link(const void* ptr, int ptr_size, char is_vector_type, char dirty);
extern void _0__cosmix_writeback_direct(const void* ptr, int ptr_size);
extern void _1__cosmix_writeback_direct(const void* ptr, int ptr_size);
extern void _2__cosmix_writeback_direct(const void* ptr, int ptr_size);
extern void _3__cosmix_writeback_direct(const void* ptr, int ptr_size);
extern int32_t _0__cosmix_get_valid_iterations(const void* unlinked_ptr, const void* ptr, int64_t stepVal, int32_t ptrSize);
extern int32_t _1__cosmix_get_valid_iterations(const void* unlinked_ptr, const void* ptr, int64_t stepVal, int32_t ptrSize);
extern int32_t _2__cosmix_get_valid_iterations(const void* unlinked_ptr, const void* ptr, int64_t stepVal, int32_t ptrSize);
extern int32_t _3__cosmix_get_valid_iterations(const void* unlinked_ptr, const void* ptr, int64_t stepVal, int32_t ptrSize);
extern void _0_mstore_free(void* ptr);
extern void _1_mstore_free(void* ptr);
extern void _2_mstore_free(void* ptr);
extern void _3_mstore_free(void* ptr);
extern void* _0_mstore_alloc(size_t size, void* private_data);
extern size_t _0_mstore_alloc_size(void* ptr);
extern void* _1_mstore_alloc(size_t size, void* private_data);
extern size_t _1_mstore_alloc_size(void* ptr);
extern void* _2_mstore_alloc(size_t size, void* private_data);
extern size_t _2_mstore_alloc_size(void* ptr);
extern void* _3_mstore_alloc(size_t size, void* private_data);
extern size_t _3_mstore_alloc_size(void* ptr);
extern uintptr_t _0_mstore_get_mpage_cache_base_ptr();
extern size_t _0_mstore_get_mpage_size();
extern uintptr_t _1_mstore_get_mpage_cache_base_ptr();
extern size_t _1_mstore_get_mpage_size();
extern uintptr_t _2_mstore_get_mpage_cache_base_ptr();
extern size_t _2_mstore_get_mpage_size();
extern uintptr_t _3_mstore_get_mpage_cache_base_ptr();
extern size_t _3_mstore_get_mpage_size();
extern char _0_is_direct();
extern char _1_is_direct();
extern char _2_is_direct();
extern char _3_is_direct();
extern char mstore_is_direct();
char mstore_is_direct_true()
{
return TRUE;
}
char mstore_is_direct_false()
{
return FALSE;
}
char _10_is_direct()
{
g_debug("Invalid usage");
abort();
return 0;
}
int32_t _10__cosmix_get_valid_iterations(const void* unlinked_ptr, const void* ptr, int64_t stepVal, int32_t ptrSize)
{
g_debug("Invalid usage");
abort();
return 0;
}
void _10__cosmix_writeback_direct(const void* ptr, int ptr_size)
{
// do nothing since if this is an invalid mstore we will catch it in the link method
// if this is not a direct mstore we cannot abbort
}
void* _10__cosmix_link(const void* ptr, int ptr_size, char is_vector_type, char dirty)
{
g_debug("Invalid usage");
abort();
return nullptr;
}
uintptr_t _10_mstore_get_mpage_cache_base_ptr()
{
g_debug("Invalid usage");
abort();
return 0;
}
size_t _10_mstore_get_mpage_size()
{
g_debug("Invalid usage");
abort();
return 0;
}
void _10_mstore_free(void* ptr)
{
g_debug("Invalid usage");
abort();
}
void* _10_mstore_alloc(size_t size, void* private_data)
{
ASSERT("Invalid usage");
abort();
return nullptr;
}
size_t _10_mstore_alloc_size(void* ptr)
{
g_debug("Invalid usage");
abort();
return 0;
}
// Template method for initializing mstores. Priv_data will be sent by the compiler based on the parsed config file when needed
//
int __cosmix_initialize_template(void* priv_data)
{
// Note: the compiler will replace this called function with the correct mstore_init callback
//
int res = mstore_init(priv_data);
if (res)
{
g_debug("Failed initializing mstore, returned error is %d\n", res);
abort();
}
if (!mstore_is_direct())
{
memset(gt_TLB, 0xFF, sizeof(struct s_victim_cache) * TLB_SIZE);
gt_crosspage_access = 0;
gt_crosspage_access_size = 0;
}
return res;
}
int __cosmix_cleanup_template()
{
int res = mstore_cleanup();
if (res)
{
g_debug("Failed releasing mstore resources, returned error is %d\n", res);
abort();
}
return res;
}
extern size_t mstore_get_min_size();
extern size_t mstore_get_max_size();
extern uintptr_t mstore_tag(void* ptr);
size_t deafult_mstore_get_min_size()
{
return 0;
}
size_t deafult_mstore_get_max_size()
{
return 1000000000000;
}
uintptr_t mstore_tag_0(void* ptr)
{
return MAKE_MSTORE0_PTR(ptr);
}
uintptr_t mstore_tag_1(void* ptr)
{
return MAKE_MSTORE1_PTR(ptr);
}
uintptr_t mstore_tag_2(void* ptr)
{
return MAKE_MSTORE2_PTR(ptr);
}
uintptr_t mstore_tag_3(void* ptr)
{
return MAKE_MSTORE3_PTR(ptr);
}
void* __cosmix_malloc_template(size_t size)
{
if ((size < mstore_get_min_size() || size >= mstore_get_max_size()))
{
return malloc(size);
}
void* bs_ptr = mstore_alloc(size, nullptr);
void* masked_bs_ptr = (void*) mstore_tag(bs_ptr);
return masked_bs_ptr;
}
void* __cosmix_calloc_template(size_t num_of_elements, size_t elements_size) {
void* ptr = __cosmix_malloc_template(num_of_elements * elements_size);
__cosmix_memset(ptr, 0, num_of_elements * elements_size);
return ptr;
}
void* __attribute__((noinline)) __cosmix_mstore_annotation(void* ptr)
{
return ptr;
}
void* __cosmix_memalign_template(size_t alignment, size_t n) {
// Alignment is inherited in mstore page cache
//
void* bs_mem = mstore_alloc(n + (alignment-1) + sizeof(void*), nullptr);
if(!bs_mem) {
return NULL;
}
char *bs_aligned_mem = ((char*)bs_mem) + sizeof(void*);
bs_aligned_mem += (alignment - ((uintptr_t)bs_aligned_mem & (alignment - 1)) & (alignment-1));
((void**)bs_aligned_mem)[-1] = bs_mem;
return (void*)mstore_tag(bs_aligned_mem);
}
/* Allocation & Deallocation Methods */
void __cosmix_free(void* p) {
if (!IS_COSMIX_PTR(p))
{
free(p);
return;
}
void* unmasked_bs_ptr = (void*) (uintptr_t)MAKE_ORIGINAL_POINTER(p);
int topByte = (uintptr_t)p >> 60;
switch (topByte)
{
case MSTORE0_SHIFT60_VAL:
_0_mstore_free(unmasked_bs_ptr);
break;
case MSTORE1_SHIFT60_VAL:
_1_mstore_free(unmasked_bs_ptr);
break;
case MSTORE2_SHIFT60_VAL:
_2_mstore_free(unmasked_bs_ptr);
break;
case MSTORE3_SHIFT60_VAL:
_3_mstore_free(unmasked_bs_ptr);
break;
default:
abort();
}
}
void* __cosmix_realloc(void* ptr, size_t size) {
if (!IS_COSMIX_PTR(ptr)) {
void* res = realloc(ptr,size);
return res;
}
int orig_ptr_size = -1;
void* res = nullptr;
int topByte = (uintptr_t)ptr >> 60;
void* unamsked_ptr = (void*)MAKE_ORIGINAL_POINTER(ptr);
switch (topByte)
{
case MSTORE0_SHIFT60_VAL:
orig_ptr_size = _0_mstore_alloc_size(unamsked_ptr);
break;
case MSTORE1_SHIFT60_VAL:
orig_ptr_size = _1_mstore_alloc_size(unamsked_ptr);
break;
case MSTORE2_SHIFT60_VAL:
orig_ptr_size = _2_mstore_alloc_size(unamsked_ptr);
break;
case MSTORE3_SHIFT60_VAL:
orig_ptr_size = _3_mstore_alloc_size(unamsked_ptr);
break;
default:
abort();
}
if (orig_ptr_size >= size) {
return ptr; // just reuse the same memory
}
switch (topByte)
{
case MSTORE0_SHIFT60_VAL:
res = (void*)MAKE_MSTORE0_PTR(_0_mstore_alloc(size, nullptr));
break;
case MSTORE1_SHIFT60_VAL:
res = (void*)MAKE_MSTORE1_PTR(_1_mstore_alloc(size, nullptr));
break;
case MSTORE2_SHIFT60_VAL:
res = (void*)MAKE_MSTORE2_PTR(_2_mstore_alloc(size, nullptr));
break;
case MSTORE3_SHIFT60_VAL:
res = (void*)MAKE_MSTORE3_PTR(_3_mstore_alloc(size, nullptr));
break;
default:
abort();
}
__cosmix_memcpy(res, ptr, orig_ptr_size);
__cosmix_free(ptr);
return res;
}
int32_t __cosmix_get_valid_iterations_template(const void* unlinked_ptr, const void* ptr, int64_t stepVal, int32_t ptrSize)
{
if (likely(!IS_COSMIX_PTR(unlinked_ptr)))
{
// Return max val so native pointers will enjoy interuptless loops
//
return (1<<30);
}
if (ptr == gt_CrossPageBuffer)
{
return 1;
}
int page_offset = (uintptr_t) ((uintptr_t)ptr - mstore_get_mpage_cache_base_ptr()) & (mstore_get_mpage_size() - 1);
int realStep = stepVal;
int left_bytes = (realStep > 0) ? (mstore_get_mpage_size() - page_offset) : -(page_offset+1);
int32_t res = left_bytes / realStep;
if (page_offset + ((res-1) * realStep) + ptrSize > mstore_get_mpage_size())
{
res--;
}
if (res <= 0)
{
return 1;
}
// TODO: theoretically we don't need MAKE_ORIGINAL_POINTER(ptr) but just ptr since its linked, but need to test this before removing
//
// ASSERT((((uintptr_t)(MAKE_ORIGINAL_POINTER(ptr) - mstore_get_mpage_cache_base_ptr()) & (mstore_get_mpage_size()-1)) + realStep * (res-1) + ptrSize) <= mstore_get_mpage_size());
return res;
}
INLINEATTR void* __cosmix_link_cached_template(const void* ptr, int ptr_size, char is_vector_type, char dirty)
{
if (likely(!IS_COSMIX_PTR(ptr)))
{
return (void*)ptr;
}
ASSERT(!is_vector_type);
uintptr_t unmasked_bs_ptr = MAKE_ORIGINAL_POINTER(ptr);
uintptr_t mstorage_page = mstore_get_mstorage_page(unmasked_bs_ptr);
unsigned bs_page_index = (mstorage_page >> mstore_get_mpage_bits());
unsigned page_offset= (mstorage_page & (mstore_get_mpage_size()-1));
#ifndef DISABLE_TLB
struct s_victim_cache* tlb = mstore_get_tlb();
int tlb_index=0;
do {
if (tlb[tlb_index].bs_page_index == bs_page_index) {
tlb[tlb_index].is_dirty |= dirty;
uintptr_t res = mstore_get_mpage_cache_base_ptr() + tlb[tlb_index].epc_page_index * mstore_get_mpage_size() + page_offset;
ASSERT((page_offset + ptr_size) <= mstore_get_mpage_size());
return (void*)res;
}
tlb_index++;
} while (tlb_index < TLB_SIZE);
#endif
// TODO: res should be actually for EPC page but not with offset
void* res = mpf_handler_c((void*)unmasked_bs_ptr);
#ifndef DISABLE_TLB
// Decrease ref count since no one else in this thread is currently using this value
// No need for lock because its per thread
//
int removed_page_index = tlb[TLB_SIZE-1].bs_page_index;
if (removed_page_index >= 0)
{
void* removed_bs_page = (void*)(unmasked_bs_ptr - ((bs_page_index - removed_page_index) * mstore_get_mpage_size()));
notify_tlb_dropped(removed_bs_page, tlb[TLB_SIZE-1].is_dirty);
}
// Now, add the new page to the TLB, in FIFO way
memmove(&tlb[1], &tlb[0], sizeof(struct s_victim_cache) * (TLB_SIZE-1));
notify_tlb_cached((void*)unmasked_bs_ptr);
tlb[0].bs_page_index = bs_page_index;// (unmasked_bs_ptr - g_mstorage_base_ptr) >> COSMIX_PAGE_BITS;
tlb[0].epc_page_index = ((uintptr_t)res - mstore_get_mpage_cache_base_ptr()) >> mstore_get_mpage_bits();
tlb[0].is_dirty = dirty;
#endif
ASSERT((((uintptr_t)((uintptr_t)res - mstore_get_mpage_cache_base_ptr()) & (mstore_get_mpage_size()-1)) + ptr_size) <= mstore_get_mpage_size());
return res;
}
void* __cosmix_page_fault(const void* bs_ptr, char dirty) {
int topByte = (uintptr_t)bs_ptr >> 60;
void* res = NULL;
switch (topByte)
{
case MSTORE0_SHIFT60_VAL:
res = _0__cosmix_link(bs_ptr, 0, 0, dirty);
break;
case MSTORE1_SHIFT60_VAL:
res = _1__cosmix_link(bs_ptr, 0, 0, dirty);
break;
case MSTORE2_SHIFT60_VAL:
res = _2__cosmix_link(bs_ptr, 0, 0, dirty);
break;
case MSTORE3_SHIFT60_VAL:
res = _3__cosmix_link(bs_ptr, 0, 0, dirty);
break;
default:
abort();
}
return res;
}
INLINEATTR void __cosmix_writeback_cached_cross_page_template()
{
if (gt_crosspage_access != 0)
{
ASSERT(gt_crosspage_access_size != 0);
uintptr_t bs_ptr = gt_crosspage_access;
int size = gt_crosspage_access_size;
// Reset for next cross page access
//
gt_crosspage_access = 0;
gt_crosspage_access_size = 0;
// writeback for old value
//
uintptr_t mstorage_page1 = mstore_get_mstorage_page(bs_ptr);
unsigned page_offset1= (mstorage_page1 & (mstore_get_mpage_size()-1));
unsigned bytes_to_copy_from_first_page = mstore_get_mpage_size() - page_offset1;
void* first_page_ptr = __cosmix_page_fault((void*)bs_ptr, TRUE);
void* second_page_ptr = __cosmix_page_fault((char*)bs_ptr + bytes_to_copy_from_first_page, TRUE);
memcpy(first_page_ptr, gt_CrossPageBuffer, bytes_to_copy_from_first_page);
memcpy(second_page_ptr, gt_CrossPageBuffer + bytes_to_copy_from_first_page, size - bytes_to_copy_from_first_page);
}
}
void __cosmix_init_global_template(void* newGlobal, void* global, int size)
{
// alloc
//
void* new_global_memory = (void*)mstore_tag(mstore_alloc(size, nullptr));
// copy global data into mstore memory
//
__cosmix_memcpy(new_global_memory, global, size);
// set newGlobal to point to the new global memory
//
*(void**)newGlobal = new_global_memory;
}
INLINEATTR void* __cosmix_link_cached_cross_page_template(const void* ptr, int ptr_size, char is_vector_type, char dirty)
{
if (likely(!IS_COSMIX_PTR(ptr)))
{
return (void*)ptr;
}
ASSERT(!is_vector_type);
uintptr_t unmasked_bs_ptr = MAKE_ORIGINAL_POINTER(ptr);
uintptr_t mstorage_page = mstore_get_mstorage_page(unmasked_bs_ptr);
unsigned page_offset= (mstorage_page & (mstore_get_mpage_size()-1));
if (page_offset + ptr_size > mstore_get_mpage_size())
{
// load from both pages into gt_CrossPageBuffer temp buffer
//
unsigned bytes_to_copy_from_first_page = mstore_get_mpage_size() - page_offset;
void* first_page_ptr = __cosmix_page_fault(ptr, dirty);
void* second_page_ptr = __cosmix_page_fault((char*)ptr + bytes_to_copy_from_first_page, dirty);
memcpy(gt_CrossPageBuffer, first_page_ptr, bytes_to_copy_from_first_page);
memcpy(gt_CrossPageBuffer + bytes_to_copy_from_first_page, second_page_ptr, ptr_size - bytes_to_copy_from_first_page);
gt_crosspage_access = (uintptr_t)ptr;
gt_crosspage_access_size = ptr_size;
return gt_CrossPageBuffer;
}
unsigned bs_page_index = (mstorage_page >> mstore_get_mpage_bits());
#ifndef DISABLE_TLB
struct s_victim_cache* tlb = mstore_get_tlb();
int tlb_index=0;
do {
if (tlb[tlb_index].bs_page_index == bs_page_index) {
tlb[tlb_index].is_dirty |= dirty;
uintptr_t res = mstore_get_mpage_cache_base_ptr() + tlb[tlb_index].epc_page_index * mstore_get_mpage_size() + page_offset;
ASSERT((page_offset + ptr_size) <= mstore_get_mpage_size());
return (void*)res;
}
tlb_index++;
} while (tlb_index < TLB_SIZE);
#endif
// TODO: res should be actually for EPC page but not with offset
void* res = mpf_handler_c((void*)unmasked_bs_ptr);
#ifndef DISABLE_TLB
// Decrease ref count since no one else in this thread is currently using this value
// No need for lock because its per thread
//
int removed_page_index = tlb[TLB_SIZE-1].bs_page_index;
if (removed_page_index >= 0)
{
void* removed_bs_page = (void*)(unmasked_bs_ptr - ((bs_page_index - removed_page_index) * mstore_get_mpage_size()));
notify_tlb_dropped(removed_bs_page, tlb[TLB_SIZE-1].is_dirty);
}
// Now, add the new page to the TLB, in FIFO way
memmove(&tlb[1], &tlb[0], sizeof(struct s_victim_cache) * (TLB_SIZE-1));
notify_tlb_cached((void*)unmasked_bs_ptr);
tlb[0].bs_page_index = bs_page_index;// (unmasked_bs_ptr - g_mstorage_base_ptr) >> COSMIX_PAGE_BITS;
tlb[0].epc_page_index = ((uintptr_t)res - mstore_get_mpage_cache_base_ptr()) >> mstore_get_mpage_bits();
tlb[0].is_dirty = dirty;
#endif
ASSERT((((uintptr_t)((uintptr_t)res - mstore_get_mpage_cache_base_ptr()) & (mstore_get_mpage_size()-1)) + ptr_size) <= mstore_get_mpage_size());
return res;
}
int32_t __cosmix_get_valid_iterations_generic(const void* unlinked_ptr, const void* ptr, int64_t stepVal, int32_t ptrSize) {
if (likely(!IS_COSMIX_PTR(unlinked_ptr))) {
// Return max val so native pointers will enjoy interuptless loops
//
return (1<<30);
}
int topByte = (uintptr_t)unlinked_ptr >> 60;
int32_t res;
switch (topByte)
{
case MSTORE0_SHIFT60_VAL:
res = _0__cosmix_get_valid_iterations(unlinked_ptr, ptr, stepVal, ptrSize);
break;
case MSTORE1_SHIFT60_VAL:
res = _1__cosmix_get_valid_iterations(unlinked_ptr, ptr, stepVal, ptrSize);
break;
case MSTORE2_SHIFT60_VAL:
res = _2__cosmix_get_valid_iterations(unlinked_ptr, ptr, stepVal, ptrSize);
break;
case MSTORE3_SHIFT60_VAL:
res = _3__cosmix_get_valid_iterations(unlinked_ptr, ptr, stepVal, ptrSize);
break;
default:
abort();
}
return res;
}
// Invokes mstore_write_back
//
INLINEATTR void __cosmix_writeback_direct_template(const void* ptr, int ptr_size)
{
if (likely(!IS_COSMIX_PTR(ptr)))
{
return;
}
void* unmasked_bs_ptr = (void*)MAKE_ORIGINAL_POINTER(ptr);
int topByte = (uintptr_t)ptr >> 60;
size_t mpage_size;
switch (topByte)
{
case MSTORE0_SHIFT60_VAL:
mpage_size = _0_mstore_get_mpage_size();
break;
case MSTORE1_SHIFT60_VAL:
mpage_size = _1_mstore_get_mpage_size();
break;
case MSTORE2_SHIFT60_VAL:
mpage_size = _2_mstore_get_mpage_size();
break;
case MSTORE3_SHIFT60_VAL:
mpage_size = _3_mstore_get_mpage_size();
default:
abort();
}
void* dest = __cosmix_get_mstore_direct_buffer(mpage_size);
// Note: internally, there might be multiple fetches from mstorage if the offset is between mpages
//
mstore_write_back(unmasked_bs_ptr, dest, ptr_size);
}
void __cosmix_writeback_generic(const void* ptr, int ptr_size)
{
if (likely(!IS_COSMIX_PTR(ptr)))
{
return;
}
int topByte = (uintptr_t)ptr >> 60;
switch (topByte)
{
case MSTORE0_SHIFT60_VAL:
_0__cosmix_writeback_direct(ptr, ptr_size);
break;
case MSTORE1_SHIFT60_VAL:
_1__cosmix_writeback_direct(ptr, ptr_size);
break;
case MSTORE2_SHIFT60_VAL:
_2__cosmix_writeback_direct(ptr, ptr_size);
break;
case MSTORE3_SHIFT60_VAL:
_3__cosmix_writeback_direct(ptr, ptr_size);
break;
default:
abort();
}
}
INLINEATTR void* __cosmix_link_direct_template(const void* ptr, int ptr_size, char is_vector_type, char dirty)
{
if (likely(!IS_COSMIX_PTR(ptr)))
{
return (void*)ptr;
}
void* unmasked_bs_ptr = (void*)MAKE_ORIGINAL_POINTER(ptr);
int topByte = (uintptr_t)ptr >> 60;
size_t mpage_size;
switch (topByte)
{
case MSTORE0_SHIFT60_VAL:
mpage_size = _0_mstore_get_mpage_size();
break;
case MSTORE1_SHIFT60_VAL:
mpage_size = _1_mstore_get_mpage_size();
break;
case MSTORE2_SHIFT60_VAL:
mpage_size = _2_mstore_get_mpage_size();
break;
case MSTORE3_SHIFT60_VAL:
mpage_size = _3_mstore_get_mpage_size();
default:
abort();
}
// Note: for now, just assert it, otherwise should divide to two separate requests
//
ASSERT(mpage_size >= ptr_size);
void* dest = __cosmix_get_mstore_direct_buffer(mpage_size);
// Note: internally, there might be multiple fetches from mstorage if the offset is between mpages