forked from ptitSeb/box86
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·1805 lines (1754 loc) · 64.3 KB
/
main.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
#ifndef __USE_FILE_OFFSET64
#define __USE_FILE_OFFSET64
#endif
#ifndef __USE_LARGEFILE64
#define __USE_LARGEFILE64
#endif
#ifndef _LARGEFILE64_SOURCE
#define _LARGEFILE64_SOURCE
#endif
#ifndef _FILE_OFFSET_BIT
#define _FILE_OFFSET_BIT 64
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <errno.h>
#include <unistd.h>
#include <dirent.h>
#include <signal.h>
#include <sys/syscall.h>
#include <sys/prctl.h>
#ifdef DYNAREC
#ifdef ARM
#include <linux/auxvec.h>
#include <asm/hwcap.h>
#endif
#endif
#include "build_info.h"
#include "debug.h"
#include "box86context.h"
#include "fileutils.h"
#include "elfloader.h"
#include "box86stack.h"
#include "x86emu.h"
#include "x86run.h"
#include "x86trace.h"
#include "librarian.h"
#include "library.h"
#include "auxval.h"
#include "wine_tools.h"
#include "rcfile.h"
box86context_t *my_context = NULL;
int box86_log = LOG_NONE;
int box86_dump = 0;
int box86_nobanner = 0;
int box86_dynarec_log = LOG_NONE;
uintptr_t box86_pagesize;
uintptr_t box86_load_addr = 0;
int box86_showbt = 0;
int box86_isglibc234 = 0;
int box86_nosandbox = 0;
int box86_malloc_hack = 0;
int box86_mutex_aligned = 0;
int box86_quit = 0;
#ifdef DYNAREC
int box86_dynarec = 1;
int box86_dynarec_dump = 0;
int box86_dynarec_forced = 0;
int box86_dynarec_largest = 0;
int box86_dynarec_bigblock = 1;
int box86_dynarec_forward = 128;
int box86_dynarec_strongmem = 0;
int box86_dynarec_x87double = 0;
int box86_dynarec_fastnan = 1;
int box86_dynarec_fastround = 1;
int box86_dynarec_safeflags = 1;
int box86_dynarec_hotpage = 16;
int box86_dynarec_bleeding_edge = 1;
int box86_dynarec_jvm = 1;
int box86_dynarec_wait = 1;
int box86_dynarec_fastpage = 0;
uintptr_t box86_nodynarec_start = 0;
uintptr_t box86_nodynarec_end = 0;
int box86_dynarec_test = 0;
int box86_dynarec_missing = 0;
#ifdef ARM
int arm_vfp = 0; // vfp version (3 or 4), with 32 registers is mendatory
int arm_swap = 0;
int arm_div = 0;
int arm_v8 = 0;
int arm_aes = 0;
int arm_pmull = 0;
#endif
#else //DYNAREC
int box86_dynarec = 0;
#endif
int box86_libcef = 1;
int box86_sdl2_jguid = 0;
int dlsym_error = 0;
int cycle_log = 0;
int trace_xmm = 0;
int trace_emm = 0;
char* trace_init = NULL;
char* box86_trace = NULL;
#ifdef HAVE_TRACE
uint64_t start_cnt = 0;
#ifdef DYNAREC
int box86_dynarec_trace = 0;
#endif
#endif
#ifdef PANDORA
int x11color16 = 0;
#endif
#if defined(RPI) || defined(RK3399) || defined(RK3288) || defined(GOA_CLONE) || defined(PYRA) || defined(PANDORA)
int box86_tokitori2 = 0;
#endif
int box86_sc3u = 0;
int box86_mapclean = 0;
int box86_zoom = 0;
int box86_x11threads = 0;
int box86_x11glx = 1;
int box86_sse_flushto0 = 0;
int box86_x87_no80bits = 0;
int allow_missing_libs = 0;
int allow_missing_symbols = 0;
int fix_64bit_inodes = 0;
int box86_prefer_wrapped = 0;
int box86_prefer_emulated = 0;
int box86_steam = 0;
int box86_wine = 0;
int box86_musl = 0;
int box86_nopulse = 0;
int box86_nogtk = 0;
int box86_novulkan = 0;
#ifdef BAD_SIGNAL
int box86_futex_waitv = 0;
#else
int box86_futex_waitv = 1;
#endif
int box86_showsegv = 0;
char* box86_libGL = NULL;
uintptr_t trace_start = 0, trace_end = 0;
char* trace_func = NULL;
uint32_t default_fs = 0;
int jit_gdb = 0;
int box86_tcmalloc_minimal = 0;
FILE* ftrace = NULL;
int ftrace_has_pid = 0;
void openFTrace(const char* newtrace)
{
const char* t = newtrace?newtrace:getenv("BOX86_TRACE_FILE");
char tmp[500];
const char* p = t;
if(p && strstr(t, "%pid")) {
int next = 0;
do {
strcpy(tmp, p);
char* c = strstr(tmp, "%pid");
*c = 0; // cut
char pid[16];
if(next)
sprintf(pid, "%d-%d", getpid(), next);
else
sprintf(pid, "%d", getpid());
strcat(tmp, pid);
c = strstr(p, "%pid") + strlen("%pid");
strcat(tmp, c);
++next;
} while (FileExist(tmp, IS_FILE));
p = tmp;
ftrace_has_pid = 1;
}
if(p) {
if(!strcmp(p, "stderr"))
ftrace = stderr;
else {
ftrace = fopen64(p, "w");
if(!ftrace) {
ftrace = stdout;
printf_log(LOG_INFO, "Cannot open trace file \"%s\" for writing (error=%s)\n", p, strerror(errno));
} else {
if(!box86_nobanner)
printf("BOX86 Trace redirected to \"%s\"\n", p);
}
}
}
}
void my_child_fork()
{
if(ftrace_has_pid) {
// open a new ftrace...
fclose(ftrace);
openFTrace(NULL);
}
}
int getNCpu();
const char* getCpuName();
#ifdef DYNAREC
void GatherDynarecExtensions()
{
if(box86_dynarec==0) // no need to check if no dynarec
return;
#ifdef ARM
unsigned long hwcap = real_getauxval(AT_HWCAP);
if(!hwcap) // no HWCap: provide a default...
hwcap = HWCAP_HALF|HWCAP_FAST_MULT|HWCAP_EDSP|HWCAP_NEON|HWCAP_VFPv3;
// first, check all needed extensions, lif half, edsp and fastmult
if((hwcap&HWCAP_HALF) == 0) {
printf_log(LOG_INFO, "Missing HALF cpu support, disabling Dynarec\n");
box86_dynarec=0;
return;
}
if((hwcap&HWCAP_FAST_MULT) == 0) {
printf_log(LOG_INFO, "Missing FAST_MULT cpu support, disabling Dynarec\n");
box86_dynarec=0;
return;
}
if((hwcap&HWCAP_EDSP) == 0) {
printf_log(LOG_INFO, "Missing EDSP cpu support, disabling Dynarec\n");
box86_dynarec=0;
return;
}
if((hwcap&HWCAP_NEON) == 0) {
printf_log(LOG_INFO, "Missing NEON support, disabling Dynarec\n");
box86_dynarec=0;
return;
}
if(hwcap&HWCAP_VFPv3D16) {
printf_log(LOG_INFO, "VFPU only have 16 registers, disabling Dynarec\n");
box86_dynarec=0;
return;
}
if(hwcap&HWCAP_VFPv3)
arm_vfp = 3;
if(hwcap&HWCAP_VFPv4)
arm_vfp = 4;
if(!arm_vfp) {
printf_log(LOG_INFO, "VFPUv3+ not detected, disabling Dynarec\n");
box86_dynarec=0;
return;
}
if(hwcap&HWCAP_SWP)
arm_swap = 1;
if(hwcap&HWCAP_IDIVA)
arm_div = 1;
#ifdef AT_HWCAP2
unsigned long hwcap2 = real_getauxval(AT_HWCAP2);
if(hwcap2&HWCAP2_AES)
arm_aes = 1;
if(hwcap2&HWCAP2_PMULL)
arm_pmull = 1;
if((hwcap2&HWCAP2_AES) || (hwcap2&HWCAP2_CRC32))
arm_v8 = 1;
#endif
printf_log(LOG_INFO, "Dynarec for ARMv%d, with extension: HALF FAST_MULT EDSP NEON VFPv%d", 7+arm_v8, arm_vfp);
if(arm_swap)
printf_log(LOG_INFO, " SWP");
if(arm_div)
printf_log(LOG_INFO, " IDIVA");
if(arm_aes)
printf_log(LOG_INFO, " AES");
if(arm_pmull)
printf_log(LOG_INFO, " PMULL");
printf_log(LOG_INFO, " PageSize:%zd ", box86_pagesize);
#endif
}
#endif
EXPORTDYN
void LoadLogEnv()
{
ftrace = stdout;
box86_nobanner = isatty(fileno(stdout))?0:1;
const char *p = getenv("BOX86_NOBANNER");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='1')
box86_nobanner = p[0]-'0';
}
}
// grab BOX86_TRACE_FILE envvar, and change %pid to actual pid is present in the name
openFTrace(NULL);
//box86_log = isatty(fileno(ftrace))?LOG_INFO:LOG_NONE; //default LOG value different if stdout is redirected or not
p = getenv("BOX86_LOG");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0'+LOG_NONE && p[0]<='0'+LOG_NEVER) {
box86_log = p[0]-'0';
if(box86_log == LOG_NEVER) {
--box86_log;
box86_dump = 1;
}
}
} else {
if(!strcasecmp(p, "NONE"))
box86_log = LOG_NONE;
else if(!strcasecmp(p, "INFO"))
box86_log = LOG_INFO;
else if(!strcasecmp(p, "DEBUG"))
box86_log = LOG_DEBUG;
else if(!strcasecmp(p, "DUMP")) {
box86_log = LOG_DEBUG;
box86_dump = 1;
}
}
if(!box86_nobanner)
printf_log(LOG_INFO, "Debug level is %d\n", box86_log);
}
p = getenv("BOX86_ROLLING_LOG");
if(p) {
int cycle = 0;
if(sscanf(p, "%d", &cycle)==1)
cycle_log = cycle;
if(cycle_log==1)
cycle_log = 16;
if(cycle_log<0)
cycle_log = 0;
if(cycle_log && box86_log>LOG_INFO) {
cycle_log = 0;
printf_log(LOG_NONE, "Incompatible Rolling log and Debug Log, disabling Rolling log\n");
}
}
if(!box86_nobanner && cycle_log)
printf_log(LOG_INFO, "Rolling log, showing last %d function call on signals\n", cycle_log);
p = getenv("BOX86_DUMP");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='1')
box86_dump = p[0]-'0';
}
}
if(!box86_nobanner && box86_dump)
printf_log(LOG_INFO, "Elf Dump if ON\n");
#ifdef DYNAREC
p = getenv("BOX86_DYNAREC_DUMP");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='2')
box86_dynarec_dump = p[0]-'0';
}
if (box86_dynarec_dump) printf_log(LOG_INFO, "Dynarec blocks are dumped%s\n", (box86_dynarec_dump>1)?" in color":"");
}
p = getenv("BOX86_DYNAREC_LOG");
if(p) {
if(strlen(p)==1) {
if((p[0]>='0'+LOG_NONE) && (p[0]<='0'+LOG_VERBOSE))
box86_dynarec_log = p[0]-'0';
} else {
if(!strcasecmp(p, "NONE"))
box86_dynarec_log = LOG_NONE;
else if(!strcasecmp(p, "INFO"))
box86_dynarec_log = LOG_INFO;
else if(!strcasecmp(p, "DEBUG"))
box86_dynarec_log = LOG_DEBUG;
else if(!strcasecmp(p, "VERBOSE"))
box86_dynarec_log = LOG_VERBOSE;
}
printf_log(LOG_INFO, "Dynarec log level is %d\n", box86_dynarec_log);
}
p = getenv("BOX86_DYNAREC");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='1')
box86_dynarec = p[0]-'0';
}
printf_log(LOG_INFO, "Dynarec is %s\n", box86_dynarec?"on":"off");
}
p = getenv("BOX86_DYNAREC_FORCED");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='1')
box86_dynarec_forced = p[0]-'0';
}
if(box86_dynarec_forced)
printf_log(LOG_INFO, "Dynarec is forced on all addresses\n");
}
p = getenv("BOX86_DYNAREC_BIGBLOCK");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='2')
box86_dynarec_bigblock = p[0]-'0';
}
if(!box86_dynarec_bigblock)
printf_log(LOG_INFO, "Dynarec will not try to make big blocks\n");
else if (box86_dynarec_bigblock>1)
printf_log(LOG_INFO, "Dynarec will try to make bigger blocks\n");
}
p = getenv("BOX86_DYNAREC_FORWARD");
if(p) {
int val = -1;
if(sscanf(p, "%d", &val)==1) {
if(val>=0)
box86_dynarec_forward = val;
}
if(box86_dynarec_forward)
printf_log(LOG_INFO, "Dynarec will continue block for %d bytes on forward jump\n", box86_dynarec_forward);
else
printf_log(LOG_INFO, "Dynarec will not continue block on forward jump\n");
}
p = getenv("BOX86_DYNAREC_STRONGMEM");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='2')
box86_dynarec_strongmem = p[0]-'0';
}
if(box86_dynarec_strongmem)
printf_log(LOG_INFO, "Dynarec will try to emulate a strong memory model%s\n", (box86_dynarec_strongmem==1)?" with limited performace loss":"");
}
p = getenv("BOX86_DYNAREC_X87DOUBLE");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='1')
box86_dynarec_x87double = p[0]-'0';
}
if(box86_dynarec_x87double)
printf_log(LOG_INFO, "Dynarec will use only double for x87 emulation\n");
}
p = getenv("BOX86_DYNAREC_FASTNAN");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='1')
box86_dynarec_fastnan = p[0]-'0';
}
if(!box86_dynarec_fastnan)
printf_log(LOG_INFO, "Dynarec will try to normalize generated NAN\n");
}
p = getenv("BOX86_DYNAREC_FASTROUND");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='1')
box86_dynarec_fastround = p[0]-'0';
}
if(!box86_dynarec_fastround)
printf_log(LOG_INFO, "Dynarec will try to generate x86 precise IEEE->int rounding\n");
}
p = getenv("BOX86_DYNAREC_SAFEFLAGS");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='2')
box86_dynarec_safeflags = p[0]-'0';
}
if(!box86_dynarec_safeflags)
printf_log(LOG_INFO, "Dynarec will not play it safe with x86 flags\n");
else
printf_log(LOG_INFO, "Dynarec will play %s safe with x86 flags\n", (box86_dynarec_safeflags==1)?"moderatly":"it");
}
p = getenv("BOX86_DYNAREC_WAIT");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='1')
box86_dynarec_wait = p[0]-'0';
}
if(!box86_dynarec_wait)
printf_log(LOG_INFO, "Dynarec will not wait for FillBlock to ready and use Interpreter instead\n");
}
p = getenv("BOX86_DYNAREC_HOTPAGE");
if(p) {
int val = -1;
if(sscanf("%d", p, &val)==1) {
if(val>=0)
box86_dynarec_hotpage = val;
}
if(!box86_dynarec_hotpage)
printf_log(LOG_INFO, "Dynarec will have HotPage tagged for %d attempts\n", box86_dynarec_hotpage);
else
printf_log(LOG_INFO, "Dynarec will not tag HotPage\n");
}
p = getenv("BOX86_DYNAREC_FASTPAGE");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='1')
box86_dynarec_fastpage = p[0]-'0';
}
if(box86_dynarec_fastpage)
printf_log(LOG_INFO, "Dynarec will use Fast HotPage\n");
}
p = getenv("BOX86_DYNAREC_BLEEDING_EDGE");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='1')
box86_dynarec_bleeding_edge = p[0]-'0';
}
if(!box86_dynarec_bleeding_edge)
printf_log(LOG_INFO, "Dynarec will not detect MonoBleedingEdge\n");
}
p = getenv("BOX86_DYNAREC_JVM");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='1')
box86_dynarec_jvm = p[0]-'0';
}
if(!box86_dynarec_jvm)
printf_log(LOG_INFO, "Dynarec will not detect libjvm\n");
}
p = getenv("BOX86_DYNAREC_MISSING");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='1')
box86_dynarec_missing = p[0]-'0';
}
if(box86_dynarec_missing)
printf_log(LOG_INFO, "Dynarec will print missing opcodes\n");
}
p = getenv("BOX86_NODYNAREC");
if(p) {
if (strchr(p,'-')) {
if(sscanf(p, "%d-%d", &box86_nodynarec_start, &box86_nodynarec_end)!=2) {
if(sscanf(p, "0x%X-0x%X", &box86_nodynarec_start, &box86_nodynarec_end)!=2)
sscanf(p, "%x-%x", &box86_nodynarec_start, &box86_nodynarec_end);
}
printf_log(LOG_INFO, "No Dynablock creation that start in %p - %p range\n", (void*)box86_nodynarec_start, (void*)box86_nodynarec_end);
}
}
p = getenv("BOX86_DYNAREC_TEST");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='1')
box86_dynarec_test = p[0]-'0';
}
if(box86_dynarec_test) {
box86_dynarec_fastnan = 0;
box86_dynarec_fastround = 0;
printf_log(LOG_INFO, "Dynarec will compare it's execution with the interpreter (super slow, only for testing)\n");
}
}
#endif
#ifdef HAVE_TRACE
p = getenv("BOX86_TRACE_XMM");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+1)
trace_xmm = p[0]-'0';
}
}
p = getenv("BOX86_TRACE_EMM");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+1)
trace_emm = p[0]-'0';
}
}
p = getenv("BOX86_TRACE_START");
if(p) {
char* p2;
start_cnt = strtoll(p, &p2, 10);
printf_log(LOG_INFO, "Will start trace only after %llu instructions\n", start_cnt);
}
#ifdef DYNAREC
p = getenv("BOX86_DYNAREC_TRACE");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+1)
box86_dynarec_trace = p[0]-'0';
if(box86_dynarec_trace)
printf_log(LOG_INFO, "Dynarec generated code will also print a trace\n");
}
}
#endif
#endif
// Other BOX86 env. var.
p = getenv("BOX86_LIBCEF");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='1')
box86_libcef = p[0]-'0';
}
if(!box86_libcef)
printf_log(LOG_INFO, "Dynarec will not detect libcef\n");
}
p = getenv("BOX86_SDL2_JGUID");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='1')
box86_sdl2_jguid = p[0]-'0';
}
if(!box86_sdl2_jguid)
printf_log(LOG_INFO, "BOX86 will workaround the use of SDL_GetJoystickGUIDInfo with 4 args instead of 5\n");
}
p = getenv("BOX86_LOAD_ADDR");
if(p) {
if(sscanf(p, "0x%zx", &box86_load_addr)!=1)
box86_load_addr = 0;
if(box86_load_addr)
printf_log(LOG_INFO, "Use a starting load address of %p\n", (void*)box86_load_addr);
}
p = getenv("BOX86_DLSYM_ERROR");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+1)
dlsym_error = p[0]-'0';
}
printf_log(LOG_INFO, "Shows details of dlopen / dlsym /dlclose : %s\n", dlsym_error?"Yes":"No");
}
#ifdef PANDORA
p = getenv("BOX86_X11COLOR16");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+1)
x11color16 = p[0]-'0';
}
printf_log(LOG_INFO, "Try to adjust X11 Color (32->16bits) : %s\n", x11color16?"Yes":"No");
}
#endif
p = getenv("BOX86_X11THREADS");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+1)
box86_x11threads = p[0]-'0';
}
if(box86_x11threads)
printf_log(LOG_INFO, "Try to Call XInitThreads if libX11 is loaded\n");
}
p = getenv("BOX86_X11GLX");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+1)
box86_x11glx = p[0]-'0';
}
if(box86_x11glx)
printf_log(LOG_INFO, "Hack to force libX11 GLX extension present\n");
else
printf_log(LOG_INFO, "Disabled Hack to force libX11 GLX extension present\n");
}
p = getenv("BOX86_LIBGL");
if(p)
box86_libGL = box_strdup(p);
if(!box86_libGL) {
p = getenv("SDL_VIDEO_GL_DRIVER");
if(p)
box86_libGL = box_strdup(p);
}
if(box86_libGL) {
printf_log(LOG_INFO, "BOX86 using \"%s\" as libGL.so.1\n", p);
}
p = getenv("BOX86_ALLOWMISSING_LIBS");
if(!p)
p = getenv("BOX86_ALLOWMISSINGLIBS");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+1)
allow_missing_libs = p[0]-'0';
}
if(allow_missing_libs)
printf_log(LOG_INFO, "Allow missing needed libs\n");
}
p = getenv("BOX86_ALLOWMISSING_SYMBOLS");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+1)
allow_missing_symbols = p[0]-'0';
}
if(allow_missing_symbols)
printf_log(LOG_INFO, "Allow missing needed symbols\n");
}
p = getenv("BOX86_MALLOC_HACK");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+2)
box86_malloc_hack = p[0]-'0';
}
if(!box86_malloc_hack) {
if(box86_malloc_hack==1) {
printf_log(LOG_INFO, "Malloc hook will not be redirected\n");
} else
printf_log(LOG_INFO, "Malloc hook will check for mmap/free occurrences\n");
}
}
p = getenv("BOX86_MUTEX_ALIGNED");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+1)
box86_mutex_aligned = p[0]-'0';
}
if(!box86_mutex_aligned) {
if(box86_mutex_aligned==1) {
printf_log(LOG_INFO, "BOX86 will not aligned mutexes\n");
} else
printf_log(LOG_INFO, "BOX86 will wrap mutex to for them aligned\n");
}
}
p = getenv("BOX86_NOPULSE");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+1)
box86_nopulse = p[0]-'0';
}
if(box86_nopulse)
printf_log(LOG_INFO, "Disable the use of pulseaudio libs\n");
}
p = getenv("BOX86_NOGTK");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+1)
box86_nogtk = p[0]-'0';
}
if(box86_nogtk)
printf_log(LOG_INFO, "Disable the use of wrapped gtk libs\n");
}
p = getenv("BOX86_NOVULKAN");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+1)
box86_novulkan = p[0]-'0';
}
if(box86_novulkan)
printf_log(LOG_INFO, "Disable the use of wrapped vulkan libs\n");
}
p = getenv("BOX86_FUTEX_WAITV");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+1)
box86_futex_waitv = p[0]-'0';
}
#ifdef BAD_SIGNAL
if(box86_futex_waitv)
printf_log(LOG_INFO, "Enable the use of futex waitv syscall (if available on the system\n");
#else
if(!box86_futex_waitv)
printf_log(LOG_INFO, "Disable the use of futex waitv syscall\n");
#endif
}
p = getenv("BOX86_FIX_64BIT_INODES");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+1)
fix_64bit_inodes = p[0]-'0';
}
if(fix_64bit_inodes)
printf_log(LOG_INFO, "Fix 64bit inodes\n");
}
p = getenv("BOX86_JITGDB");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+2)
jit_gdb = p[0]-'0';
}
if(jit_gdb)
printf_log(LOG_INFO, "Launch %s on segfault\n", (jit_gdb==2)?"gdbserver":"gdb");
}
p = getenv("BOX86_SHOWSEGV");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+1)
box86_showsegv = p[0]-'0';
}
if(box86_showsegv)
printf_log(LOG_INFO, "Show Segfault signal even if a signal handler is present\n");
}
p = getenv("BOX86_SHOWBT");
if(p) {
if(strlen(p)==1) {
if(p[0]>='0' && p[0]<='0'+1)
box86_showbt = p[0]-'0';
}
if(box86_showbt)
printf_log(LOG_INFO, "Show Backtrace for signals\n");
}
box86_pagesize = sysconf(_SC_PAGESIZE);
if(!box86_pagesize)
box86_pagesize = 4096;
#ifdef DYNAREC
GatherDynarecExtensions();
#endif
int ncpu = getNCpu();
const char* cpuname = getCpuName();
printf_log(LOG_INFO, "Running on %s with %d Cores\n", cpuname, ncpu);
}
EXPORTDYN
void LoadEnvPath(path_collection_t *col, const char* defpath, const char* env)
{
const char* p = getenv(env);
if(p) {
printf_log(LOG_INFO, "%s: ", env);
ParseList(p, col, 1);
} else {
printf_log(LOG_INFO, "Using default %s: ", env);
ParseList(defpath, col, 1);
}
if(LOG_INFO<=box86_log) {
for(int i=0; i<col->size; i++)
printf_log(LOG_INFO, "%s%s", col->paths[i], (i==col->size-1)?"\n":":");
}
}
EXPORTDYN
int CountEnv(char** env)
{
// count, but remove all BOX86_* environnement
// also remove PATH and LD_LIBRARY_PATH
// but add 2 for default BOX86_PATH and BOX86_LD_LIBRARY_PATH
char** p = env;
int c = 0;
while(*p) {
if(strncmp(*p, "BOX86_PATH=", 11)==0) {
} else if(strncmp(*p, "BOX86_LD_LIBRARY_PATH=", 22)==0) {
} else if(strncmp(*p, "BOX86_", 6)!=0) {
++c;
}
++p;
}
return c+2;
}
EXPORTDYN
int GatherEnv(char*** dest, char** env, const char* prog)
{
// Add every BOX86_* environment variables
char** p = env;
int idx = 0;
int box86_path = 0;
int box86_ld_path = 0;
while(*p) {
if(strncmp(*p, "BOX86_PATH=", 11)==0) {
(*dest)[idx++] = box_strdup(*p);
box86_path = 1;
} else if(strncmp(*p, "BOX86_LD_LIBRARY_PATH=", 22)==0) {
(*dest)[idx++] = box_strdup(*p);
box86_ld_path = 1;
} else if(strncmp(*p, "BOX86_", 6)!=0) {
(*dest)[idx++] = box_strdup(*p);
}
++p;
}
// Add default values for BOX86_PATH and BOX86_LD_LIBRARY_PATH
if(!box86_path) {
(*dest)[idx++] = box_strdup("BOX86_PATH=.:bin");
}
if(!box86_ld_path) {
(*dest)[idx++] = box_strdup("BOX86_LD_LIBRARY_PATH=.:lib");
}
// Add "_=prog" at the end...
if(prog) {
int l = strlen(prog);
char tmp[l+3];
strcpy(tmp, "_=");
strcat(tmp, prog);
(*dest)[idx++] = box_strdup(tmp);
}
// and a final NULL
(*dest)[idx++] = 0;
return 0;
}
void PrintHelp() {
printf("\n\nThis is Box86, the Linux x86 emulator with a twist\n");
printf("\nUsage is box86 [options] path/to/software [args]\n");
printf("to launch x86 software\n");
printf(" options can be :\n");
printf(" '-v'|'--version' to print box86 version and quit\n");
printf(" '-h'|'--help' to print box86 help and quit\n");
printf("You can also set some environment variables:\n");
printf(" BOX86_PATH is the box86 version of PATH (default is '.:bin')\n");
printf(" BOX86_LD_LIBRARY_PATH is the box86 version LD_LIBRARY_PATH (default is '.:lib')\n");
printf(" BOX86_LOG with 0/1/2/3 or NONE/INFO/DEBUG/DUMP to set the printed debug info (level 3 is level 2 + BOX86_DUMP)\n");
printf(" BOX86_ROLLING_LOG 0/1 use a rolling level 2 log of 16 entries\n");
printf(" BOX86_DUMP with 0/1 to dump elf infos\n");
printf(" BOX86_NOBANNER with 0/1 to enable/disable the printing of box86 version and build at start\n");
#ifdef DYNAREC
printf(" BOX86_DYNAREC_LOG with 0/1/2/3 or NONE/INFO/DEBUG/DUMP to set the printed dynarec info\n");
printf(" BOX86_DYNAREC with 0/1 to disable or enable Dynarec (On by default)\n");
printf(" BOX86_NODYNAREC with address interval (0x1234-0x4567) to forbid dynablock creation in the interval specified\n");
printf(" BOX86_DYNAREC_BIGBLOCK 0/1/2 to control Dynarec building BigBlock or not (default: 1)\n");
printf(" BOX86_DYNAREC_STRONGMEM 0/1/2 to control Dynarec emulation attempt of Stong memory model (default: 0)\n");
printf(" BOX86_DYNAREC_X87DOUBLE 0/1 to force Dynarec to use Double for x87 emulation (default: 0)\n");
printf(" BOX86_DYNAREC_FASTNAN 0/1 to control if NaN are x86 accurate or not (default: 1)\n");
printf(" BOX86_DYNAREC_SAFEFLAGS 0/1/2 to control flags generation on RET/CALL opcodes (default: 1)\n");
#endif
#ifdef HAVE_TRACE
printf(" BOX86_TRACE with 1 to enable x86 execution trace\n");
printf(" or with XXXXXX-YYYYYY to enable x86 execution trace only between address\n");
printf(" or with FunctionName to enable x86 execution trace only in one specific function\n");
printf(" use BOX86_TRACE_INIT instead of BOX_TRACE to start trace before init of Libs and main program\n\t (function name will probably not work then)\n");
printf(" BOX86_TRACE_XMM with 1 to enable dump of SSE/SSE2 register along with regular registers\n");
printf(" BOX86_TRACE_START with N to enable trace after N instructions\n");
#ifdef DYNAREC
printf(" BOX86_DYNAREC_TRACE with 0/1 to disable or enable Trace on generated code too\n");
#endif
#endif
printf(" BOX86_TRACE_FILE with FileName to redirect logs in a file (or stderr to use stderr instead of stdout)");
printf(" BOX86_DLSYM_ERROR with 1 to log dlsym errors\n");
printf(" BOX86_LOAD_ADDR=0xXXXXXX try to load at 0xXXXXXX main binary (if binary is a PIE)\n");
printf(" BOX86_NOSIGSEGV=1 to disable handling of SigSEGV\n");
printf(" BOX86_NOSIGILL=1 to disable handling of SigILL\n");
printf(" BOX86_SHOWSEGV=1 to show Segfault signal even if a signal handler is present\n");
#ifdef PANDORA
printf(" BOX86_X11COLOR16=1 to try convert X11 color from 32 bits to 16 bits (to avoid light green on light cyan windows\n");
#endif
printf(" BOX86_X11THREADS=1 to call XInitThreads when loading X11 (for old Loki games with Loki_Compat lib)");
printf(" BOX86_LIBGL=libXXXX set the name (and optionnaly full path) for libGL.so.1\n");
printf(" BOX86_LD_PRELOAD=XXXX[:YYYYY] force loading XXXX (and YYYY...) libraries with the binary\n");
printf(" BOX86_ALLOWMISSINGLIBS with 1 to allow to continue even if a lib is missing (unadvised, will probably crash later)\n");
printf(" BOX64_PREFER_EMULATED=1 to prefer emulated libs first (execpt for glibc, alsa, pulse, GL, vulkan and X11\n");
printf(" BOX64_PREFER_WRAPPED if box86 will use wrapped libs even if the lib is specified with absolute path\n");
printf(" BOX86_NOPULSE=1 to disable the loading of pulseaudio libs\n");
printf(" BOX86_NOGTK=1 to disable the loading of wrapped gtk libs\n");
printf(" BOX86_NOVULKAN=1 to disable the loading of wrapped vulkan libs\n");
printf(" BOX86_ENV='XXX=yyyy' will add XXX=yyyy env. var.\n");
printf(" BOX86_ENV1='XXX=yyyy' will add XXX=yyyy env. var. and continue with BOX86_ENV2 ... until var doesn't exist\n");
printf(" BOX86_JITGDB with 1 to launch \"gdb\" when a segfault is trapped, attached to the offending process\n");
}
void addNewEnvVar(const char* s)
{
if(!s)
return;
char* p = box_strdup(s);
char* e = strchr(p, '=');
if(!e) {
printf_log(LOG_INFO, "Invalid specific env. var. '%s'\n", s);
box_free(p);
return;
}
*e='\0';
++e;
setenv(p, e, 1);
box_free(p);
}
EXPORTDYN
void LoadEnvVars(box86context_t *context)
{
char *p;
// Check custom env. var. and add them if needed
p = getenv("BOX86_ENV");
if(p)
addNewEnvVar(p);
int i = 1;
char box86_env[50];
do {
sprintf(box86_env, "BOX86_ENV%d", i);
p = getenv(box86_env);
if(p) {
addNewEnvVar(p);
++i;
}
} while(p);
// check BOX86_LD_LIBRARY_PATH and load it
LoadEnvPath(&context->box86_ld_lib, ".:lib:lib32:x86:i686", "BOX86_LD_LIBRARY_PATH");
#ifdef PANDORA
if(FileExist("/mnt/utmp/codeblocks/usr/lib/i386-linux-gnu", 0))
AddPath("/mnt/utmp/codeblocks/usr/lib/i386-linux-gnu", &context->box86_ld_lib, 1);
if(FileExist("/mnt/utmp/box86/lib/i386-linux-gnu", 0))
AddPath("/mnt/utmp/box86/lib/i386-linux-gnu", &context->box86_ld_lib, 1);
//TODO: add relative path to box86 location
#endif
if(FileExist("/lib/box86", 0))
AddPath("/lib/box86", &context->box86_ld_lib, 1);
if(FileExist("/usr/lib/box86", 0))
AddPath("/usr/lib/box86", &context->box86_ld_lib, 1);
if(FileExist("/lib/i386-linux-gnu", 0))
AddPath("/lib/i386-linux-gnu", &context->box86_ld_lib, 1);
if(FileExist("/usr/lib/i386-linux-gnu", 0))
AddPath("/usr/lib/i386-linux-gnu", &context->box86_ld_lib, 1);
if(FileExist("/lib/i686-pc-linux-gnu", 0))
AddPath("/lib/i686-pc-linux-gnu", &context->box86_ld_lib, 1);
if(FileExist("/usr/lib/i686-pc-linux-gnu", 0))
AddPath("/usr/lib/i686-pc-linux-gnu", &context->box86_ld_lib, 1);
if(FileExist("/usr/lib32", 0))
AddPath("/usr/lib32", &context->box86_ld_lib, 1);
if(getenv("LD_LIBRARY_PATH"))
PrependList(&context->box86_ld_lib, getenv("LD_LIBRARY_PATH"), 1); // in case some of the path are for x86 world
if(getenv("BOX86_EMULATED_LIBS")) {
char* p = getenv("BOX86_EMULATED_LIBS");
ParseList(p, &context->box86_emulated_libs, 0);
if (my_context->box86_emulated_libs.size && box86_log) {
printf_log(LOG_INFO, "BOX86 will force the used of emulated libs for ");
for (int i=0; i<context->box86_emulated_libs.size; ++i)
printf_log(LOG_INFO, "%s ", context->box86_emulated_libs.paths[i]);
printf_log(LOG_INFO, "\n");
}
}
// add libssl and libcrypto, prefer emulated version because of multiple version exist
AddPath("libssl.so.1", &context->box86_emulated_libs, 0);
AddPath("libssl.so.1.0.0", &context->box86_emulated_libs, 0);
AddPath("libcrypto.so.1", &context->box86_emulated_libs, 0);
AddPath("libcrypto.so.1.0.0", &context->box86_emulated_libs, 0);
// check BOX86_PATH and load it
LoadEnvPath(&context->box86_path, ".:bin", "BOX86_PATH");
if(getenv("PATH"))
AppendList(&context->box86_path, getenv("PATH"), 1); // in case some of the path are for x86 world
#define READENV(v, name, dest, eqn) \
do { \
p = getenv(name); \
if (p) { \
if (!strcmp(p, v)) { \
dest = 1; \
eqn; \
} \
} \
} while (0)
#define READENV0(name, dest, eqn) READENV("0", name, dest, eqn)
#define READENV1(name, dest, log) READENV("1", name, dest, printf_log(LOG_INFO, log))
READENV1("BOX86_SSE_FLUSHTO0", box86_sse_flushto0, "BOX86: Direct apply of SSE Flush to 0 flag\n");
READENV1("BOX86_X87_NO80BITS", box86_x87_no80bits, "BOX86: all 80bits x87 long double will be handle as double\n");
READENV1("BOX86_PREFER_WRAPPED", box86_prefer_wrapped, "BOX86: Prefer Wrapped libs\n");
READENV1("BOX86_PREFER_EMULATED", box86_prefer_emulated, "BOX86: Prefer Emulated libs\n");
READENV1("BOX86_NOSIGSEGV", context->no_sigsegv, "BOX86: Disabling handling of SigSEGV\n");
READENV1("BOX86_NOSIGILL", context->no_sigill, "BOX86: Disabling handling of SigILL\n");