-
Notifications
You must be signed in to change notification settings - Fork 80
/
neco.c
8761 lines (8008 loc) · 262 KB
/
neco.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
// https://github.com/tidwall/neco
//
// Copyright 2024 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
//
// Neco: Coroutine library for C
/*
////////////////////////////////////////////////////////////////////////////////
// Compilation options
////////////////////////////////////////////////////////////////////////////////
NECO_STACKSIZE // Size of each stack
NECO_DEFCAP // Default stack_group capacity
NECO_MAXCAP // Max stack_group capacity
NECO_GAPSIZE // Size of gap (guard) pages
NECO_SIGSTKSZ // Size of signal stack on main thread
NECO_BURST // Number of read attempts before waiting, def: disabled
NECO_MAXWORKERS // Max number of worker threads, def: 64
NECO_MAXIOWORKERS // Max number of io threads, def: 2
// Additional options that activate features
NECO_USEGUARDS // Use mprotect'ed guard pages
NECO_NOPAGERELEASE // Do not early release mmapped pages
NECO_NOSTACKFREELIST // Do not use a stack free list
NECO_NOPOOL // Do not use a coroutine and channel pools
NECO_USEHEAPSTACK // Allocate stacks on the heap using malloc
NECO_NOSIGNALS // Disable signal handling
NECO_NOWORKERS // Disable all worker threads, work will run in coroutine
NECO_USEREADWORKERS // Use read workers, disabled by default
NECO_USEWRITEWORKERS // Use write workers, enabled by default on Linux
NECO_NOREADWORKERS // Disable all read workers
NECO_NOWRITEWORKERS // Disable all write workers
*/
// Windows and Webassembly have limited features.
#if defined(__EMSCRIPTEN__) || defined(__WIN32)
#define DEF_STACKSIZE 1048576
#define DEF_DEFCAP 0
#define DEF_MAXCAP 0
#define DEF_GAPSIZE 0
#define DEF_SIGSTKSZ 0
#define DEF_BURST -1
#define NECO_USEHEAPSTACK
#define NECO_NOSIGNALS
#define NECO_NOWORKERS
#else
#define DEF_STACKSIZE 8388608
#define DEF_DEFCAP 4
#define DEF_MAXCAP 8192
#define DEF_GAPSIZE 1048576
#define DEF_SIGSTKSZ 1048576
#define DEF_BURST -1
#define DEF_MAXWORKERS 64
#define DEF_MAXRINGSIZE 32
#define DEF_MAXIOWORKERS 2
#endif
#ifdef __linux__
#ifndef NECO_USEWRITEWORKERS
#define NECO_USEWRITEWORKERS
#endif
#endif
#ifndef NECO_STACKSIZE
#define NECO_STACKSIZE DEF_STACKSIZE
#endif
#ifndef NECO_DEFCAP
#define NECO_DEFCAP DEF_DEFCAP
#endif
#ifndef NECO_MAXCAP
#define NECO_MAXCAP DEF_MAXCAP
#endif
#ifndef NECO_GAPSIZE
#define NECO_GAPSIZE DEF_GAPSIZE
#endif
#ifndef NECO_SIGSTKSZ
#define NECO_SIGSTKSZ DEF_SIGSTKSZ
#endif
#ifndef NECO_BURST
#define NECO_BURST DEF_BURST
#endif
#ifndef NECO_MAXWORKERS
#define NECO_MAXWORKERS DEF_MAXWORKERS
#endif
#ifndef NECO_MAXRINGSIZE
#define NECO_MAXRINGSIZE DEF_MAXRINGSIZE
#endif
#ifndef NECO_MAXIOWORKERS
#define NECO_MAXIOWORKERS DEF_MAXIOWORKERS
#endif
#ifdef NECO_TESTING
#if NECO_BURST <= 0
#undef NECO_BURST
#define NECO_BURST 1
#endif
#if NECO_MAXWORKERS > 8
#undef NECO_MAXWORKERS
#define NECO_MAXWORKERS 8
#endif
#if NECO_MAXRINGSIZE > 4
#undef NECO_MAXRINGSIZE
#define NECO_MAXRINGSIZE 4
#endif
#endif
// The following is only needed when LLCO_NOASM or LLCO_STACKJMP is defined.
// This same block is duplicated in the llco.c block below.
#ifdef _FORTIFY_SOURCE
#define LLCO_FORTIFY_SOURCE _FORTIFY_SOURCE
// Disable __longjmp_chk validation so that we can jump between stacks.
#pragma push_macro("_FORTIFY_SOURCE")
#undef _FORTIFY_SOURCE
#include <setjmp.h>
#define _FORTIFY_SOURCE LLCO_FORTIFY_SOURCE
#undef LLCO_FORTIFY_SOURCE
#pragma pop_macro("_FORTIFY_SOURCE")
#endif
#ifdef _WIN32
#define _POSIX
#define __USE_MINGW_ALARM
#endif
////////////////////////////////////////////////////////////////////////////////
// Embedded dependencies
// The deps/embed.sh command embeds all dependencies into this source file to
// create a single amalgamation file.
////////////////////////////////////////////////////////////////////////////////
#ifdef NECO_NOAMALGA
#include "deps/sco.h"
#include "deps/aat.h"
#include "deps/stack.h"
#ifndef NECO_NOWORKERS
#include "deps/worker.h"
#endif
#else
#define SCO_STATIC
#define STACK_STATIC
#define WORKER_STATIC
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
// BEGIN sco.c
// https://github.com/tidwall/sco
//
// Copyright 2023 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// Coroutine scheduler
#include <stdatomic.h>
#include <stdbool.h>
#ifndef SCO_STATIC
#include "sco.h"
#else
#define SCO_EXTERN static
#include <stddef.h>
#include <stdint.h>
struct sco_desc {
void *stack;
size_t stack_size;
void (*entry)(void *udata);
void (*cleanup)(void *stack, size_t stack_size, void *udata);
void *udata;
};
struct sco_symbol {
void *cfa; // Canonical Frame Address
void *ip; // Instruction Pointer
const char *fname; // Pathname of shared object
void *fbase; // Base address of shared object
const char *sname; // Name of nearest symbol
void *saddr; // Address of nearest symbol
};
#define SCO_MINSTACKSIZE 131072
#endif
#ifndef SCO_EXTERN
#define SCO_EXTERN
#endif
////////////////////////////////////////////////////////////////////////////////
// llco.c
////////////////////////////////////////////////////////////////////////////////
#ifdef SCO_NOAMALGA
#include "deps/llco.h"
#else
#define LLCO_STATIC
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
// BEGIN llco.c
// https://github.com/tidwall/llco
//
// Copyright (c) 2024 Joshua J Baker.
// This software is available as a choice of Public Domain or MIT-0.
#ifdef _FORTIFY_SOURCE
#define LLCO_FORTIFY_SOURCE _FORTIFY_SOURCE
// Disable __longjmp_chk validation so that we can jump between stacks.
#pragma push_macro("_FORTIFY_SOURCE")
#undef _FORTIFY_SOURCE
#include <setjmp.h>
#define _FORTIFY_SOURCE LLCO_FORTIFY_SOURCE
#undef LLCO_FORTIFY_SOURCE
#pragma pop_macro("_FORTIFY_SOURCE")
#endif
#ifndef LLCO_STATIC
#include "llco.h"
#else
#include <stddef.h>
#include <stdbool.h>
#define LLCO_MINSTACKSIZE 16384
#define LLCO_EXTERN static
struct llco_desc {
void *stack;
size_t stack_size;
void (*entry)(void *udata);
void (*cleanup)(void *stack, size_t stack_size, void *udata);
void *udata;
};
struct llco_symbol {
void *cfa;
void *ip;
const char *fname;
void *fbase;
const char *sname;
void *saddr;
};
#endif
#include <stdlib.h>
#ifdef LLCO_VALGRIND
#include <valgrind/valgrind.h>
#endif
#ifndef LLCO_EXTERN
#define LLCO_EXTERN
#endif
#if defined(__GNUC__)
#ifdef noinline
#define LLCO_NOINLINE noinline
#else
#define LLCO_NOINLINE __attribute__ ((noinline))
#endif
#ifdef noreturn
#define LLCO_NORETURN noreturn
#else
#define LLCO_NORETURN __attribute__ ((noreturn))
#endif
#else
#define LLCO_NOINLINE
#define LLCO_NORETURN
#endif
#if defined(_MSC_VER)
#define __thread __declspec(thread)
#endif
static void llco_entry(void *arg);
LLCO_NORETURN
static void llco_exit(void) {
_Exit(0);
}
#ifdef LLCO_ASM
#error LLCO_ASM must not be defined
#endif
#if defined(__COSMOCC__) && !defined(LLCO_NOASM)
// Cosmopolitan has issues with asm code
#define LLCO_NOASM
#endif
// Passing the entry function into assembly requires casting the function
// pointer to an object pointer, which is forbidden in the ISO C spec but
// allowed in posix. Ignore the warning attributed to this requirement when
// the -pedantic compiler flag is provide.
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#endif
////////////////////////////////////////////////////////////////////////////////
// Below is various assembly code adapted from the Lua Coco [MIT] and Minicoro
// [MIT-0] projects by Mike Pall and Eduardo Bart respectively.
////////////////////////////////////////////////////////////////////////////////
/*
Lua Coco (coco.luajit.org)
Copyright (C) 2004-2016 Mike Pall. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
////////////////////////////////////////////////////////////////////////////////
// ARM
////////////////////////////////////////////////////////////////////////////////
#if defined(__ARM_EABI__) && !defined(LLCO_NOASM)
#define LLCO_ASM
#define LLCO_READY
#define LLCO_METHOD "asm,arm_eabi"
struct llco_asmctx {
#ifndef __SOFTFP__
void* f[16];
#endif
void *d[4]; /* d8-d15 */
void *r[4]; /* r4-r11 */
void *lr;
void *sp;
};
void _llco_asm_entry(void);
int _llco_asm_switch(struct llco_asmctx *from, struct llco_asmctx *to);
__asm__(
".text\n"
#ifdef __APPLE__
".globl __llco_asm_switch\n"
"__llco_asm_switch:\n"
#else
".globl _llco_asm_switch\n"
".type _llco_asm_switch #function\n"
".hidden _llco_asm_switch\n"
"_llco_asm_switch:\n"
#endif
#ifndef __SOFTFP__
" vstmia r0!, {d8-d15}\n"
#endif
" stmia r0, {r4-r11, lr}\n"
" str sp, [r0, #9*4]\n"
#ifndef __SOFTFP__
" vldmia r1!, {d8-d15}\n"
#endif
" ldr sp, [r1, #9*4]\n"
" ldmia r1, {r4-r11, pc}\n"
#ifndef __APPLE__
".size _llco_asm_switch, .-_llco_asm_switch\n"
#endif
);
__asm__(
".text\n"
#ifdef __APPLE__
".globl __llco_asm_entry\n"
"__llco_asm_entry:\n"
#else
".globl _llco_asm_entry\n"
".type _llco_asm_entry #function\n"
".hidden _llco_asm_entry\n"
"_llco_asm_entry:\n"
#endif
" mov r0, r4\n"
" mov ip, r5\n"
" mov lr, r6\n"
" bx ip\n"
#ifndef __APPLE__
".size _llco_asm_entry, .-_llco_asm_entry\n"
#endif
);
static void llco_asmctx_make(struct llco_asmctx *ctx, void* stack_base,
size_t stack_size, void *arg)
{
ctx->d[0] = (void*)(arg);
ctx->d[1] = (void*)(llco_entry);
ctx->d[2] = (void*)(0xdeaddead); /* Dummy return address. */
ctx->lr = (void*)(_llco_asm_entry);
ctx->sp = (void*)((size_t)stack_base + stack_size);
}
#endif
////////////////////////////////////////////////////////////////////////////////
// ARM 64-bit
////////////////////////////////////////////////////////////////////////////////
#if defined(__aarch64__) && !defined(LLCO_NOASM)
#define LLCO_ASM
#define LLCO_READY
#define LLCO_METHOD "asm,aarch64"
struct llco_asmctx {
void *x[12]; /* x19-x30 */
void *sp;
void *lr;
void *d[8]; /* d8-d15 */
};
void _llco_asm_entry(void);
int _llco_asm_switch(struct llco_asmctx *from, struct llco_asmctx *to);
__asm__(
".text\n"
#ifdef __APPLE__
".globl __llco_asm_switch\n"
"__llco_asm_switch:\n"
#else
".globl _llco_asm_switch\n"
".type _llco_asm_switch #function\n"
".hidden _llco_asm_switch\n"
"_llco_asm_switch:\n"
#endif
" mov x10, sp\n"
" mov x11, x30\n"
" stp x19, x20, [x0, #(0*16)]\n"
" stp x21, x22, [x0, #(1*16)]\n"
" stp d8, d9, [x0, #(7*16)]\n"
" stp x23, x24, [x0, #(2*16)]\n"
" stp d10, d11, [x0, #(8*16)]\n"
" stp x25, x26, [x0, #(3*16)]\n"
" stp d12, d13, [x0, #(9*16)]\n"
" stp x27, x28, [x0, #(4*16)]\n"
" stp d14, d15, [x0, #(10*16)]\n"
" stp x29, x30, [x0, #(5*16)]\n"
" stp x10, x11, [x0, #(6*16)]\n"
" ldp x19, x20, [x1, #(0*16)]\n"
" ldp x21, x22, [x1, #(1*16)]\n"
" ldp d8, d9, [x1, #(7*16)]\n"
" ldp x23, x24, [x1, #(2*16)]\n"
" ldp d10, d11, [x1, #(8*16)]\n"
" ldp x25, x26, [x1, #(3*16)]\n"
" ldp d12, d13, [x1, #(9*16)]\n"
" ldp x27, x28, [x1, #(4*16)]\n"
" ldp d14, d15, [x1, #(10*16)]\n"
" ldp x29, x30, [x1, #(5*16)]\n"
" ldp x10, x11, [x1, #(6*16)]\n"
" mov sp, x10\n"
" br x11\n"
#ifndef __APPLE__
".size _llco_asm_switch, .-_llco_asm_switch\n"
#endif
);
__asm__(
".text\n"
#ifdef __APPLE__
".globl __llco_asm_entry\n"
"__llco_asm_entry:\n"
#else
".globl _llco_asm_entry\n"
".type _llco_asm_entry #function\n"
".hidden _llco_asm_entry\n"
"_llco_asm_entry:\n"
#endif
" mov x0, x19\n"
" mov x30, x21\n"
" br x20\n"
#ifndef __APPLE__
".size _llco_asm_entry, .-_llco_asm_entry\n"
#endif
);
static void llco_asmctx_make(struct llco_asmctx *ctx, void* stack_base,
size_t stack_size, void *arg)
{
ctx->x[0] = (void*)(arg);
ctx->x[1] = (void*)(llco_entry);
ctx->x[2] = (void*)(0xdeaddeaddeaddead); /* Dummy return address. */
ctx->sp = (void*)((size_t)stack_base + stack_size);
ctx->lr = (void*)(_llco_asm_entry);
}
#endif
////////////////////////////////////////////////////////////////////////////////
// RISC-V (rv64/rv32)
////////////////////////////////////////////////////////////////////////////////
#if defined(__riscv) && !defined(LLCO_NOASM)
#define LLCO_ASM
#define LLCO_READY
#define LLCO_METHOD "asm,riscv"
struct llco_asmctx {
void* s[12]; /* s0-s11 */
void* ra;
void* pc;
void* sp;
#ifdef __riscv_flen
#if __riscv_flen == 64
double fs[12]; /* fs0-fs11 */
#elif __riscv_flen == 32
float fs[12]; /* fs0-fs11 */
#endif
#endif /* __riscv_flen */
};
void _llco_asm_entry(void);
int _llco_asm_switch(struct llco_asmctx *from, struct llco_asmctx *to);
__asm__(
".text\n"
".globl _llco_asm_entry\n"
".type _llco_asm_entry @function\n"
".hidden _llco_asm_entry\n"
"_llco_asm_entry:\n"
" mv a0, s0\n"
" jr s1\n"
".size _llco_asm_entry, .-_llco_asm_entry\n"
);
__asm__(
".text\n"
".globl _llco_asm_switch\n"
".type _llco_asm_switch @function\n"
".hidden _llco_asm_switch\n"
"_llco_asm_switch:\n"
#if __riscv_xlen == 64
" sd s0, 0x00(a0)\n"
" sd s1, 0x08(a0)\n"
" sd s2, 0x10(a0)\n"
" sd s3, 0x18(a0)\n"
" sd s4, 0x20(a0)\n"
" sd s5, 0x28(a0)\n"
" sd s6, 0x30(a0)\n"
" sd s7, 0x38(a0)\n"
" sd s8, 0x40(a0)\n"
" sd s9, 0x48(a0)\n"
" sd s10, 0x50(a0)\n"
" sd s11, 0x58(a0)\n"
" sd ra, 0x60(a0)\n"
" sd ra, 0x68(a0)\n" /* pc */
" sd sp, 0x70(a0)\n"
#ifdef __riscv_flen
#if __riscv_flen == 64
" fsd fs0, 0x78(a0)\n"
" fsd fs1, 0x80(a0)\n"
" fsd fs2, 0x88(a0)\n"
" fsd fs3, 0x90(a0)\n"
" fsd fs4, 0x98(a0)\n"
" fsd fs5, 0xa0(a0)\n"
" fsd fs6, 0xa8(a0)\n"
" fsd fs7, 0xb0(a0)\n"
" fsd fs8, 0xb8(a0)\n"
" fsd fs9, 0xc0(a0)\n"
" fsd fs10, 0xc8(a0)\n"
" fsd fs11, 0xd0(a0)\n"
" fld fs0, 0x78(a1)\n"
" fld fs1, 0x80(a1)\n"
" fld fs2, 0x88(a1)\n"
" fld fs3, 0x90(a1)\n"
" fld fs4, 0x98(a1)\n"
" fld fs5, 0xa0(a1)\n"
" fld fs6, 0xa8(a1)\n"
" fld fs7, 0xb0(a1)\n"
" fld fs8, 0xb8(a1)\n"
" fld fs9, 0xc0(a1)\n"
" fld fs10, 0xc8(a1)\n"
" fld fs11, 0xd0(a1)\n"
#else
#error "Unsupported RISC-V FLEN"
#endif
#endif /* __riscv_flen */
" ld s0, 0x00(a1)\n"
" ld s1, 0x08(a1)\n"
" ld s2, 0x10(a1)\n"
" ld s3, 0x18(a1)\n"
" ld s4, 0x20(a1)\n"
" ld s5, 0x28(a1)\n"
" ld s6, 0x30(a1)\n"
" ld s7, 0x38(a1)\n"
" ld s8, 0x40(a1)\n"
" ld s9, 0x48(a1)\n"
" ld s10, 0x50(a1)\n"
" ld s11, 0x58(a1)\n"
" ld ra, 0x60(a1)\n"
" ld a2, 0x68(a1)\n" /* pc */
" ld sp, 0x70(a1)\n"
" jr a2\n"
#elif __riscv_xlen == 32
" sw s0, 0x00(a0)\n"
" sw s1, 0x04(a0)\n"
" sw s2, 0x08(a0)\n"
" sw s3, 0x0c(a0)\n"
" sw s4, 0x10(a0)\n"
" sw s5, 0x14(a0)\n"
" sw s6, 0x18(a0)\n"
" sw s7, 0x1c(a0)\n"
" sw s8, 0x20(a0)\n"
" sw s9, 0x24(a0)\n"
" sw s10, 0x28(a0)\n"
" sw s11, 0x2c(a0)\n"
" sw ra, 0x30(a0)\n"
" sw ra, 0x34(a0)\n" /* pc */
" sw sp, 0x38(a0)\n"
#ifdef __riscv_flen
#if __riscv_flen == 64
" fsd fs0, 0x3c(a0)\n"
" fsd fs1, 0x44(a0)\n"
" fsd fs2, 0x4c(a0)\n"
" fsd fs3, 0x54(a0)\n"
" fsd fs4, 0x5c(a0)\n"
" fsd fs5, 0x64(a0)\n"
" fsd fs6, 0x6c(a0)\n"
" fsd fs7, 0x74(a0)\n"
" fsd fs8, 0x7c(a0)\n"
" fsd fs9, 0x84(a0)\n"
" fsd fs10, 0x8c(a0)\n"
" fsd fs11, 0x94(a0)\n"
" fld fs0, 0x3c(a1)\n"
" fld fs1, 0x44(a1)\n"
" fld fs2, 0x4c(a1)\n"
" fld fs3, 0x54(a1)\n"
" fld fs4, 0x5c(a1)\n"
" fld fs5, 0x64(a1)\n"
" fld fs6, 0x6c(a1)\n"
" fld fs7, 0x74(a1)\n"
" fld fs8, 0x7c(a1)\n"
" fld fs9, 0x84(a1)\n"
" fld fs10, 0x8c(a1)\n"
" fld fs11, 0x94(a1)\n"
#elif __riscv_flen == 32
" fsw fs0, 0x3c(a0)\n"
" fsw fs1, 0x40(a0)\n"
" fsw fs2, 0x44(a0)\n"
" fsw fs3, 0x48(a0)\n"
" fsw fs4, 0x4c(a0)\n"
" fsw fs5, 0x50(a0)\n"
" fsw fs6, 0x54(a0)\n"
" fsw fs7, 0x58(a0)\n"
" fsw fs8, 0x5c(a0)\n"
" fsw fs9, 0x60(a0)\n"
" fsw fs10, 0x64(a0)\n"
" fsw fs11, 0x68(a0)\n"
" flw fs0, 0x3c(a1)\n"
" flw fs1, 0x40(a1)\n"
" flw fs2, 0x44(a1)\n"
" flw fs3, 0x48(a1)\n"
" flw fs4, 0x4c(a1)\n"
" flw fs5, 0x50(a1)\n"
" flw fs6, 0x54(a1)\n"
" flw fs7, 0x58(a1)\n"
" flw fs8, 0x5c(a1)\n"
" flw fs9, 0x60(a1)\n"
" flw fs10, 0x64(a1)\n"
" flw fs11, 0x68(a1)\n"
#else
#error "Unsupported RISC-V FLEN"
#endif
#endif /* __riscv_flen */
" lw s0, 0x00(a1)\n"
" lw s1, 0x04(a1)\n"
" lw s2, 0x08(a1)\n"
" lw s3, 0x0c(a1)\n"
" lw s4, 0x10(a1)\n"
" lw s5, 0x14(a1)\n"
" lw s6, 0x18(a1)\n"
" lw s7, 0x1c(a1)\n"
" lw s8, 0x20(a1)\n"
" lw s9, 0x24(a1)\n"
" lw s10, 0x28(a1)\n"
" lw s11, 0x2c(a1)\n"
" lw ra, 0x30(a1)\n"
" lw a2, 0x34(a1)\n" /* pc */
" lw sp, 0x38(a1)\n"
" jr a2\n"
#else
#error "Unsupported RISC-V XLEN"
#endif /* __riscv_xlen */
".size _llco_asm_switch, .-_llco_asm_switch\n"
);
static void llco_asmctx_make(struct llco_asmctx *ctx,
void* stack_base, size_t stack_size, void *arg)
{
ctx->s[0] = (void*)(arg);
ctx->s[1] = (void*)(llco_entry);
ctx->pc = (void*)(_llco_asm_entry);
#if __riscv_xlen == 64
ctx->ra = (void*)(0xdeaddeaddeaddead);
#elif __riscv_xlen == 32
ctx->ra = (void*)(0xdeaddead);
#endif
ctx->sp = (void*)((size_t)stack_base + stack_size);
}
#endif // riscv
////////////////////////////////////////////////////////////////////////////////
// x86
////////////////////////////////////////////////////////////////////////////////
#if (defined(__i386) || defined(__i386__)) && !defined(LLCO_NOASM)
#define LLCO_ASM
#define LLCO_READY
#define LLCO_METHOD "asm,i386"
struct llco_asmctx {
void *eip, *esp, *ebp, *ebx, *esi, *edi;
};
void _llco_asm_switch(struct llco_asmctx *from, struct llco_asmctx *to);
__asm__(
#ifdef __DJGPP__ /* DOS compiler */
"__llco_asm_switch:\n"
#else
".text\n"
".globl _llco_asm_switch\n"
".type _llco_asm_switch @function\n"
".hidden _llco_asm_switch\n"
"_llco_asm_switch:\n"
#endif
" call 1f\n"
" 1:\n"
" popl %ecx\n"
" addl $(2f-1b), %ecx\n"
" movl 4(%esp), %eax\n"
" movl 8(%esp), %edx\n"
" movl %ecx, (%eax)\n"
" movl %esp, 4(%eax)\n"
" movl %ebp, 8(%eax)\n"
" movl %ebx, 12(%eax)\n"
" movl %esi, 16(%eax)\n"
" movl %edi, 20(%eax)\n"
" movl 20(%edx), %edi\n"
" movl 16(%edx), %esi\n"
" movl 12(%edx), %ebx\n"
" movl 8(%edx), %ebp\n"
" movl 4(%edx), %esp\n"
" jmp *(%edx)\n"
" 2:\n"
" ret\n"
#ifndef __DJGPP__
".size _llco_asm_switch, .-_llco_asm_switch\n"
#endif
);
static void llco_asmctx_make(struct llco_asmctx *ctx,
void* stack_base, size_t stack_size, void *arg)
{
void** stack_high_ptr = (void**)((size_t)stack_base + stack_size - 16 -
1*sizeof(size_t));
stack_high_ptr[0] = (void*)(0xdeaddead); // Dummy return address.
stack_high_ptr[1] = (void*)(arg);
ctx->eip = (void*)(llco_entry);
ctx->esp = (void*)(stack_high_ptr);
}
#endif // __i386__
////////////////////////////////////////////////////////////////////////////////
// x64
////////////////////////////////////////////////////////////////////////////////
#if (defined(__x86_64__) || defined(_M_X64)) && !defined(LLCO_NOASM)
#define LLCO_ASM
#define LLCO_READY
#define LLCO_METHOD "asm,x64"
#ifdef _WIN32
struct llco_asmctx {
void *rip, *rsp, *rbp, *rbx, *r12, *r13, *r14, *r15, *rdi, *rsi;
void* xmm[20]; /* xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13,
xmm14, xmm15 */
void* fiber_storage;
void* dealloc_stack;
void* stack_limit;
void* stack_base;
};
#if defined(__GNUC__)
#define LLCO_ASM_BLOB __attribute__((section(".text")))
#elif defined(_MSC_VER)
#define LLCO_ASM_BLOB __declspec(allocate(".text"))
#pragma section(".text")
#endif
LLCO_ASM_BLOB static unsigned char llco_wrap_main_code_entry[] = {
0x4c,0x89,0xe9, // mov %r13,%rcx
0x41,0xff,0xe4, // jmpq *%r12
0xc3, // retq
0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90 // nop
};
LLCO_ASM_BLOB static unsigned char llco_asm_switch_code[] = {
0x48,0x8d,0x05,0x3e,0x01,0x00,0x00, // lea 0x13e(%rip),%rax
0x48,0x89,0x01, // mov %rax,(%rcx)
0x48,0x89,0x61,0x08, // mov %rsp,0x8(%rcx)
0x48,0x89,0x69,0x10, // mov %rbp,0x10(%rcx)
0x48,0x89,0x59,0x18, // mov %rbx,0x18(%rcx)
0x4c,0x89,0x61,0x20, // mov %r12,0x20(%rcx)
0x4c,0x89,0x69,0x28, // mov %r13,0x28(%rcx)
0x4c,0x89,0x71,0x30, // mov %r14,0x30(%rcx)
0x4c,0x89,0x79,0x38, // mov %r15,0x38(%rcx)
0x48,0x89,0x79,0x40, // mov %rdi,0x40(%rcx)
0x48,0x89,0x71,0x48, // mov %rsi,0x48(%rcx)
0x0f,0x11,0x71,0x50, // movups %xmm6,0x50(%rcx)
0x0f,0x11,0x79,0x60, // movups %xmm7,0x60(%rcx)
0x44,0x0f,0x11,0x41,0x70, // movups %xmm8,0x70(%rcx)
0x44,0x0f,0x11,0x89,0x80,0x00,0x00,0x00, // movups %xmm9,0x80(%rcx)
0x44,0x0f,0x11,0x91,0x90,0x00,0x00,0x00, // movups %xmm10,0x90(%rcx)
0x44,0x0f,0x11,0x99,0xa0,0x00,0x00,0x00, // movups %xmm11,0xa0(%rcx)
0x44,0x0f,0x11,0xa1,0xb0,0x00,0x00,0x00, // movups %xmm12,0xb0(%rcx)
0x44,0x0f,0x11,0xa9,0xc0,0x00,0x00,0x00, // movups %xmm13,0xc0(%rcx)
0x44,0x0f,0x11,0xb1,0xd0,0x00,0x00,0x00, // movups %xmm14,0xd0(%rcx)
0x44,0x0f,0x11,0xb9,0xe0,0x00,0x00,0x00, // movups %xmm15,0xe0(%rcx)
0x65,0x4c,0x8b,0x14,0x25,0x30,0x00,0x00,0x00, // mov %gs:0x30,%r10
0x49,0x8b,0x42,0x20, // mov 0x20(%r10),%rax
0x48,0x89,0x81,0xf0,0x00,0x00,0x00, // mov %rax,0xf0(%rcx)
0x49,0x8b,0x82,0x78,0x14,0x00,0x00, // mov 0x1478(%r10),%rax
0x48,0x89,0x81,0xf8,0x00,0x00,0x00, // mov %rax,0xf8(%rcx)
0x49,0x8b,0x42,0x10, // mov 0x10(%r10),%rax
0x48,0x89,0x81,0x00,0x01,0x00,0x00, // mov %rax,0x100(%rcx)
0x49,0x8b,0x42,0x08, // mov 0x8(%r10),%rax
0x48,0x89,0x81,0x08,0x01,0x00,0x00, // mov %rax,0x108(%rcx)
0x48,0x8b,0x82,0x08,0x01,0x00,0x00, // mov 0x108(%rdx),%rax
0x49,0x89,0x42,0x08, // mov %rax,0x8(%r10)
0x48,0x8b,0x82,0x00,0x01, 0x00, 0x00, // mov 0x100(%rdx),%rax
0x49,0x89,0x42,0x10, // mov %rax,0x10(%r10)
0x48,0x8b,0x82,0xf8,0x00, 0x00, 0x00, // mov 0xf8(%rdx),%rax
0x49,0x89,0x82,0x78,0x14, 0x00, 0x00, // mov %rax,0x1478(%r10)
0x48,0x8b,0x82,0xf0,0x00, 0x00, 0x00, // mov 0xf0(%rdx),%rax
0x49,0x89,0x42,0x20, // mov %rax,0x20(%r10)
0x44,0x0f,0x10,0xba,0xe0,0x00,0x00,0x00, // movups 0xe0(%rdx),%xmm15
0x44,0x0f,0x10,0xb2,0xd0,0x00,0x00,0x00, // movups 0xd0(%rdx),%xmm14
0x44,0x0f,0x10,0xaa,0xc0,0x00,0x00,0x00, // movups 0xc0(%rdx),%xmm13
0x44,0x0f,0x10,0xa2,0xb0,0x00,0x00,0x00, // movups 0xb0(%rdx),%xmm12
0x44,0x0f,0x10,0x9a,0xa0,0x00,0x00,0x00, // movups 0xa0(%rdx),%xmm11
0x44,0x0f,0x10,0x92,0x90,0x00,0x00,0x00, // movups 0x90(%rdx),%xmm10
0x44,0x0f,0x10,0x8a,0x80,0x00,0x00,0x00, // movups 0x80(%rdx),%xmm9
0x44,0x0f,0x10,0x42,0x70, // movups 0x70(%rdx),%xmm8
0x0f,0x10,0x7a,0x60, // movups 0x60(%rdx),%xmm7
0x0f,0x10,0x72,0x50, // movups 0x50(%rdx),%xmm6
0x48,0x8b,0x72,0x48, // mov 0x48(%rdx),%rsi
0x48,0x8b,0x7a,0x40, // mov 0x40(%rdx),%rdi
0x4c,0x8b,0x7a,0x38, // mov 0x38(%rdx),%r15
0x4c,0x8b,0x72,0x30, // mov 0x30(%rdx),%r14
0x4c,0x8b,0x6a,0x28, // mov 0x28(%rdx),%r13
0x4c,0x8b,0x62,0x20, // mov 0x20(%rdx),%r12
0x48,0x8b,0x5a,0x18, // mov 0x18(%rdx),%rbx
0x48,0x8b,0x6a,0x10, // mov 0x10(%rdx),%rbp
0x48,0x8b,0x62,0x08, // mov 0x8(%rdx),%rsp
0xff,0x22, // jmpq *(%rdx)
0xc3, // retq
0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90, // nop
0x90,0x90, // nop
};
void (*_llco_asm_entry)(void) =
(void(*)(void))(void*)llco_wrap_main_code_entry;
void (*_llco_asm_switch)(struct llco_asmctx *from,
struct llco_asmctx *to) = (void(*)(struct llco_asmctx *from,
struct llco_asmctx *to))(void*)llco_asm_switch_code;
static void llco_asmctx_make(struct llco_asmctx *ctx,
void* stack_base, size_t stack_size, void *arg)
{
stack_size = stack_size - 32; // Reserve 32 bytes for the shadow space.
void** stack_high_ptr = (void**)((size_t)stack_base + stack_size -
sizeof(size_t));
stack_high_ptr[0] = (void*)(0xdeaddeaddeaddead); // Dummy return address.
ctx->rip = (void*)(_llco_asm_entry);
ctx->rsp = (void*)(stack_high_ptr);
ctx->r12 = (void*)(llco_entry);
ctx->r13 = (void*)(arg);
void* stack_top = (void*)((size_t)stack_base + stack_size);
ctx->stack_base = stack_top;
ctx->stack_limit = stack_base;
ctx->dealloc_stack = stack_base;
}
#else
struct llco_asmctx {
void *rip, *rsp, *rbp, *rbx, *r12, *r13, *r14, *r15;
};
void _llco_asm_entry(void);
int _llco_asm_switch(struct llco_asmctx *from, struct llco_asmctx *to);
__asm__(
".text\n"
#ifdef __MACH__ /* Mac OS X assembler */
".globl __llco_asm_entry\n"
"__llco_asm_entry:\n"
#else /* Linux assembler */
".globl _llco_asm_entry\n"
".type _llco_asm_entry @function\n"
".hidden _llco_asm_entry\n"
"_llco_asm_entry:\n"
#endif
" movq %r13, %rdi\n"
" jmpq *%r12\n"
#ifndef __MACH__
".size _llco_asm_entry, .-_llco_asm_entry\n"
#endif
);
__asm__(
".text\n"
#ifdef __MACH__ /* Mac OS assembler */
".globl __llco_asm_switch\n"
"__llco_asm_switch:\n"
#else /* Linux assembler */
".globl _llco_asm_switch\n"
".type _llco_asm_switch @function\n"
".hidden _llco_asm_switch\n"
"_llco_asm_switch:\n"
#endif
" leaq 0x3d(%rip), %rax\n"
" movq %rax, (%rdi)\n"
" movq %rsp, 8(%rdi)\n"
" movq %rbp, 16(%rdi)\n"
" movq %rbx, 24(%rdi)\n"
" movq %r12, 32(%rdi)\n"
" movq %r13, 40(%rdi)\n"
" movq %r14, 48(%rdi)\n"
" movq %r15, 56(%rdi)\n"
" movq 56(%rsi), %r15\n"
" movq 48(%rsi), %r14\n"
" movq 40(%rsi), %r13\n"
" movq 32(%rsi), %r12\n"
" movq 24(%rsi), %rbx\n"
" movq 16(%rsi), %rbp\n"
" movq 8(%rsi), %rsp\n"
" jmpq *(%rsi)\n"
" ret\n"
#ifndef __MACH__
".size _llco_asm_switch, .-_llco_asm_switch\n"
#endif
);
static void llco_asmctx_make(struct llco_asmctx *ctx,
void* stack_base, size_t stack_size, void *arg)
{
// Reserve 128 bytes for the Red Zone space (System V AMD64 ABI).
stack_size = stack_size - 128;
void** stack_high_ptr = (void**)((size_t)stack_base + stack_size -
sizeof(size_t));
stack_high_ptr[0] = (void*)(0xdeaddeaddeaddead); // Dummy return address.
ctx->rip = (void*)(_llco_asm_entry);
ctx->rsp = (void*)(stack_high_ptr);
ctx->r12 = (void*)(llco_entry);
ctx->r13 = (void*)(arg);
}
#endif
#endif // x64
// --- END ASM Code --- //
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
////////////////////////////////////////////////////////////////////////////////
// ASM with stackjmp activated
////////////////////////////////////////////////////////////////////////////////
#if defined(LLCO_READY) && defined(LLCO_STACKJMP)
LLCO_NOINLINE LLCO_NORETURN
static void llco_stackjmp(void *stack, size_t stack_size,
void(*entry)(void *arg))
{
struct llco_asmctx ctx = { 0 };
llco_asmctx_make(&ctx, stack, stack_size, 0);
struct llco_asmctx ctx0 = { 0 };
_llco_asm_switch(&ctx0, &ctx);
llco_exit();
}
#endif
////////////////////////////////////////////////////////////////////////////////