-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrvos.cxx
3515 lines (3101 loc) · 131 KB
/
rvos.cxx
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
/*
#ifdef ARMOS
This emulates an extemely simple ARM64 Linux-like OS.
It can load and execute ARM64 apps built with Gnu tools in .elf files targeting Linux.
Written by David Lee in October 2024
#else // RVOS
This emulates an extemely simple RISC-V OS.
Per https://riscv.org/wp-content/uploads/2017/05/riscv-privileged-v1.10.pdf
It's an AEE (Application Execution Environment) that exposes an ABI (Application Binary Inferface) for an Application.
It runs in RISC-V M mode, similar to embedded systems.
It can load and execute 64-bit RISC-V apps built with Gnu tools in .elf files targeting Linux.
Written by David Lee in February 2023
Useful: https://github.com/jart/cosmopolitan/blob/1.0/libc/sysv/consts.sh
#endif
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <errno.h>
#include <vector>
#include <chrono>
#include <locale.h>
#include <djl_os.hxx>
#ifdef _WIN32
#include <io.h>
struct iovec
{
void * iov_base; /* Starting address */
size_t iov_len; /* Length in bytes */
};
struct tms
{
uint64_t tms_utime;
uint64_t tms_stime;
uint64_t tms_cutime;
uint64_t tms_cstime;
};
typedef SSIZE_T ssize_t;
#define local_KERNEL_NCCS 19
struct local_kernel_termios
{
uint32_t c_iflag; /* input mode flags */
uint32_t c_oflag; /* output mode flags */
uint32_t c_cflag; /* control mode flags */
uint32_t c_lflag; /* local mode flags */
uint8_t c_line; /* line discipline */
uint8_t c_cc[local_KERNEL_NCCS]; /* control characters */
};
#else
#include <unistd.h>
#ifndef OLDGCC // the several-years-old Gnu C compiler for the RISC-V development boards
#include <termios.h>
#include <sys/random.h>
#include <sys/uio.h>
#include <sys/times.h>
#include <sys/resource.h>
#include <dirent.h>
#ifndef __APPLE__
#include <sys/sysinfo.h>
#endif
// this structure is smaller than the usermode version. I don't know of a cross-platform header with it.
#define local_KERNEL_NCCS 19
struct local_kernel_termios
{
tcflag_t c_iflag; /* input mode flags */
tcflag_t c_oflag; /* output mode flags */
tcflag_t c_cflag; /* control mode flags */
tcflag_t c_lflag; /* local mode flags */
cc_t c_line; /* line discipline */
cc_t c_cc[local_KERNEL_NCCS]; /* control characters */
};
#endif
struct LINUX_FIND_DATA
{
char cFileName[ MAX_PATH ];
};
#endif
#include <djltrace.hxx>
#include <djl_con.hxx>
#include <djl_mmap.hxx>
using namespace std;
using namespace std::chrono;
#include "linuxem.h"
#ifdef ARMOS
#include "arm64.hxx"
#define CPUClass Arm64
#define ELF_MACHINE_ISA 0xb7
#define APP_NAME "ARMOS"
#define LOGFILE_NAME L"armos.log"
#define REG_SYSCALL 8
#define REG_RESULT 0
#define REG_ARG0 0
#define REG_ARG1 1
#define REG_ARG2 2
#define REG_ARG3 3
#define REG_ARG4 4
#define REG_ARG5 5
#elif defined( RVOS )
#include "riscv.hxx"
#define CPUClass RiscV
#define ELF_MACHINE_ISA 0xf3
#define APP_NAME "RVOS"
#define LOGFILE_NAME L"rvos.log"
#define REG_SYSCALL RiscV::a7
#define REG_RESULT RiscV::a0
#define REG_ARG0 RiscV::a0
#define REG_ARG1 RiscV::a1
#define REG_ARG2 RiscV::a2
#define REG_ARG3 RiscV::a3
#define REG_ARG4 RiscV::a4
#define REG_ARG5 RiscV::a5
#else
#error "One of ARMOS or RVOS must be defined for compilation"
#endif
CDJLTrace tracer;
ConsoleConfiguration g_consoleConfig;
bool g_compressed_rvc = false; // is the app compressed risc-v?
const uint64_t g_args_commit = 1024; // storage spot for command-line arguments and environment variables
const uint64_t g_stack_commit = 128 * 1024; // RAM to allocate for the fixed stack
uint64_t g_brk_commit = 40 * 1024 * 1024; // RAM to reserve if the app calls brk to allocate space. 40 meg default
uint64_t g_mmap_commit = 40 * 1024 * 1024; // RAM to reserve if the app mmap to allocate space. 40 meg default
bool g_terminate = false; // has the app asked to shut down?
int g_exit_code = 0; // exit code of the app in the vm
vector<uint8_t> memory; // RAM for the vm
uint64_t g_base_address = 0; // vm address of start of memory
uint64_t g_execution_address = 0; // where the program counter starts
uint64_t g_brk_offset = 0; // offset of brk, initially g_end_of_data
uint64_t g_mmap_offset = 0; // offset of where mmap allocations start
uint64_t g_highwater_brk = 0; // highest brk seen during app; peak dynamically-allocated RAM
uint64_t g_end_of_data = 0; // official end of the loaded app
uint64_t g_bottom_of_stack = 0; // just beyond where brk might move
uint64_t g_top_of_stack = 0; // argc, argv, penv, aux records sit above this
CMMap g_mmap; // for mmap and munmap system calls
// fake descriptors.
// /etc/timezone is not implemented, so apps running in the emulator on Windows assume UTC
const uint64_t findFirstDescriptor = 3000;
const uint64_t timebaseFrequencyDescriptor = 3001;
const uint64_t osreleaseDescriptor = 3002;
#pragma warning(disable: 4200) // 0-sized array
struct linux_dirent64_syscall {
uint64_t d_ino; /* Inode number */
uint64_t d_off; /* Offset to next linux_dirent */
uint16_t d_reclen; /* Length of this linux_dirent */
uint8_t d_type; /* DT_DIR (4) if a dir, DT_REG (8) if a regular file */
char d_name[];
/* optional and not implemented. must be 0-filled
char pad
char d_type
*/
};
struct linux_timeval
{
uint64_t tv_sec; // time_t
uint64_t tv_usec; // suseconds_t
};
struct linux_tms_syscall
{
uint64_t tms_utime;
uint64_t tms_stime;
uint64_t tms_cutime;
uint64_t tms_cstime;
};
struct linux_rusage_syscall {
struct linux_timeval ru_utime; /* user CPU time used */
struct linux_timeval ru_stime; /* system CPU time used */
long ru_maxrss; /* maximum resident set size */
long ru_ixrss; /* integral shared memory size */
long ru_idrss; /* integral unshared data size */
long ru_isrss; /* integral unshared stack size */
long ru_minflt; /* page reclaims (soft page faults) */
long ru_majflt; /* page faults (hard page faults) */
long ru_nswap; /* swaps */
long ru_inblock; /* block input operations */
long ru_oublock; /* block output operations */
long ru_msgsnd; /* IPC messages sent */
long ru_msgrcv; /* IPC messages received */
long ru_nsignals; /* signals received */
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary context switches */
};
struct pollfd_syscall {
int fd;
short events;
short revents;
};
struct timespec_syscall {
uint64_t tv_sec;
uint64_t tv_nsec;
};
#define SYS_NMLN 65 // appears to be true for Arm64.
struct utsname_syscall {
/** The information returned by uname(). */
/** The OS name. "Linux" on Android. */
char sysname[SYS_NMLN];
/** The name on the network. Typically "localhost" on Android. */
char nodename[SYS_NMLN];
/** The OS release. Typically something like "4.4.115-g442ad7fba0d" on Android. */
char release[SYS_NMLN];
/** The OS version. Typically something like "#1 SMP PREEMPT" on Android. */
char version[SYS_NMLN];
/** The hardware architecture. Typically "aarch64" on Android. */
char machine[SYS_NMLN];
/** The domain name set by setdomainname(). Typically "localdomain" on Android. */
char domainname[SYS_NMLN];
};
struct stat_linux_syscall {
/*
struct stat info run on a 64-bit RISC-V system
sizeof s: 128
offset size field
0 8 st_dev
8 8 st_ino
16 4 st_mode
20 4 st_nlink
24 4 st_uid
28 4 st_gid
32 8 st_rdev
48 8 st_size
56 4 st_blksize
64 8 st_blocks
72 16 st_atim
88 16 st_mtim
104 16 st_ctim
*/
uint64_t st_dev; /* ID of device containing file */
uint64_t st_ino; /* Inode number */
uint32_t st_mode; /* File type and mode */
uint32_t st_nlink; /* Number of hard links */
uint32_t st_uid; /* User ID of owner */
uint32_t st_gid; /* Group ID of owner */
uint64_t st_rdev; /* Device ID (if special file) */
uint64_t st_mystery_spot;
uint64_t st_size; /* Total size, in bytes */
uint32_t st_blksize; /* Block size for filesystem I/O */
uint64_t st_blocks; /* Number of 512 B blocks allocated */
/* Since POSIX.1-2008, this structure supports nanosecond
precision for the following timestamp fields.
For the details before POSIX.1-2008, see VERSIONS. */
struct timespec st_atim; /* Time of last access */
struct timespec st_mtim; /* Time of last modification */
struct timespec st_ctim; /* Time of last status change */
#ifndef st_atime
#ifndef OLDGCC
#define st_atime st_atim.tv_sec /* Backward compatibility */
#define st_mtine st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
#endif
#endif
uint64_t st_mystery_spot_2;
};
#pragma pack( push, 1 )
struct AuxProcessStart
{
uint64_t a_type; // AT_xxx ID from elf.h
union
{
uint64_t a_val;
void * a_ptr;
void ( * a_fcn )();
} a_un;
};
struct ElfHeader64
{
uint32_t magic;
uint8_t bit_width;
uint8_t endianness;
uint8_t elf_version;
uint8_t os_abi;
uint8_t os_avi_version;
uint8_t padding[ 7 ];
uint16_t type;
uint16_t machine;
uint32_t version;
uint64_t entry_point;
uint64_t program_header_table;
uint64_t section_header_table;
uint32_t flags;
uint16_t header_size;
uint16_t program_header_table_size;
uint16_t program_header_table_entries;
uint16_t section_header_table_size;
uint16_t section_header_table_entries;
uint16_t section_with_section_names;
};
struct ElfSymbol64
{
uint32_t name; // index into the symbol string table
uint8_t info; // value of the symbol
uint8_t other;
uint16_t shndx;
uint64_t value; // address where the symbol resides in memory
uint64_t size; // length in memory of the symbol
const char * show_info() const
{
if ( 0 == info )
return "local";
if ( 1 == info )
return "global";
if ( 2 == info )
return "weak";
if ( 3 == info )
return "num";
if ( 4 == info )
return "file";
if ( 5 == info )
return "common";
if ( 6 == info )
return "tls";
if ( 7 == info )
return "num";
if ( 10 == info )
return "loos / gnu_ifunc";
if ( 12 == info )
return "hios";
if ( 13 == info )
return "loproc";
if ( 15 == info )
return "hiproc";
return "unknown";
} //show_info
const char * show_other() const
{
if ( 0 == other )
return "default";
if ( 1 == other )
return "internal";
if ( 2 == other )
return "hidden";
if ( 3 == other )
return "protected";
return "unknown";
}
};
struct ElfProgramHeader64
{
uint32_t type;
uint32_t flags;
uint64_t offset_in_image;
uint64_t virtual_address;
uint64_t physical_address;
uint64_t file_size;
uint64_t memory_size;
uint64_t alignment;
const char * show_type() const
{
uint32_t basetype = ( type & 0xf );
if ( 0 == basetype )
return "unused";
if ( 1 == basetype )
return "load";
if ( 2 == basetype )
return "dynamic";
if ( 3 == basetype )
return "interp";
if ( 4 == basetype )
return "note";
if ( 5 == basetype )
return "shlib";
if ( 6 == basetype )
return "phdr";
if ( 7 == basetype )
return "tls";
if ( 8 == basetype )
return "num";
return "unknown";
}
};
struct ElfSectionHeader64
{
uint32_t name_offset;
uint32_t type;
uint64_t flags;
uint64_t address;
uint64_t offset;
uint64_t size;
uint32_t link;
uint32_t info;
uint64_t address_alignment;
uint64_t entry_size;
const char * show_type() const
{
uint32_t basetype = ( type & 0xf );
if ( 0 == basetype )
return "unused";
if ( 1 == basetype )
return "program data";
if ( 2 == basetype )
return "symbol table";
if ( 3 == basetype )
return "string table";
if ( 4 == basetype )
return "relocation entries with addends";
if ( 5 == basetype )
return "symbol hash table";
if ( 6 == basetype )
return "dynamic";
if ( 7 == basetype )
return "note";
if ( 8 == basetype )
return "nobits";
if ( 9 == basetype )
return "relocation entries without addends";
if ( 10 == basetype )
return "shlib";
if ( 11 == basetype )
return "dynsym";
if ( 12 == basetype )
return "num";
if ( 14 == basetype )
return "initialization functions";
if ( 15 == basetype )
return "termination functions";
return "unknown";
}
const char * show_flags() const
{
static char ac[ 80 ];
ac[0] = 0;
if ( flags & 0x1 )
strcat( ac, "write, " );
if ( flags & 0x2 )
strcat( ac, "alloc, " );
if ( flags & 0x4 )
strcat( ac, "executable, " );
if ( flags & 0x10 )
strcat( ac, "merge, " );
if ( flags & 0x20 )
strcat( ac, "asciz strings, " );
return ac;
}
};
#pragma pack(pop)
static void usage( char const * perror = 0 )
{
g_consoleConfig.RestoreConsole( false );
if ( 0 != perror )
printf( "error: %s\n", perror );
printf( "usage: %s <%s arguments> <elf_executable> <app arguments>\n", APP_NAME, APP_NAME );
printf( " arguments: -e just show information about the elf executable; don't actually run it\n" );
#ifdef RVOS
printf( " -g (internal) generate rcvtable.txt then exit\n" );
#endif
printf( " -h:X # of meg for the heap (brk space). 0..1024 are valid. default is 40\n" );
printf( " -i if -t is set, also enables instruction tracing\n" );
printf( " -m:X # of meg for mmap space. 0..1024 are valid. default is 40\n" );
printf( " -p shows performance information at app exit\n" );
printf( " -t enable debug tracing to %ls\n", LOGFILE_NAME );
printf( " -v used with -e shows verbose information (e.g. symbols)\n" );
printf( " %s\n", build_string() );
exit( 1 );
} //usage
static int gettimeofday( linux_timeval * tp )
{
// the OLDGCC's chrono implementation is built on time(), which calls this but
// has only second resolution. So the microseconds returned here are lost.
// All other C++ implementations do the right thing.
namespace sc = std::chrono;
sc::system_clock::duration d = sc::system_clock::now().time_since_epoch();
sc::seconds s = sc::duration_cast<sc::seconds>( d );
tp->tv_sec = s.count();
tp->tv_usec = sc::duration_cast<sc::microseconds>( d - s ).count();
return 0;
} //gettimeofday
static uint64_t rand64()
{
uint64_t r = 0;
for ( int i = 0; i < 7; i++ )
r = ( r << 15 ) | ( rand() & 0x7FFF );
return r;
} //rand64
static void backslash_to_slash( char * p )
{
while ( *p )
{
if ( '\\' == *p )
*p = '/';
p++;
}
} //backslash_to_slash
#ifdef _WIN32
static void slash_to_backslash( char * p )
{
while ( *p )
{
if ( '/' == *p )
*p = '\\';
p++;
}
} //slash_to_backslash
static int windows_translate_flags( int flags )
{
// Translate open() flags from Linux to Windows
// Microsoft C uses different constants for flags than linux:
// msft (win+dos) linux
// -------------- -----
// 0 O_RDONLY O_RDONLY
// 1 O_WRONLY O_WRONLY
// 2 O_RDRW O_RDWR
// 0x8 O_APPEND n/a
// 0x10 O_RANDOM _FMARK / O_SHLOCK / _FSHLOCK / O_SYNC
// 0x20 O_SEQUENTIAL _FDEFER / O_EXLOCK / _FEXLOCK
// 0x40 O_TEMPORARY O_CREAT
// 0x80 O_NOINHERIT O_EXCL
// 0x100 O_CREAT n/a
// 0x200 O_TRUNC O_TRUNC
// 0x400 O_EXCL O_APPEND
// 0x2000 O_OBTAINDIR O_ASYNC
// 0x4000 O_TEXT n/a
// 0x8000 O_BINARY n/a
// 0x10100 n/a O_FSYNC, O_SYNC
int f = flags & 3; // copy rd/wr/rdrw
f |= O_BINARY; // this is assumed on Linux systems
if ( 0x40 & flags )
f |= O_CREAT;
if ( 0x80 & flags )
f |= O_EXCL;
if ( 0x200 & flags )
f |= O_TRUNC;
if ( 0x400 & flags )
f |= O_APPEND;
tracer.Trace( " flags translated from linux %x to Microsoft %x\n", flags, f );
return f;
} //windows_translate_flags
static uint32_t epoch_days( uint16_t y, uint16_t m, uint16_t d ) // taken from https://blog.reverberate.org/2020/05/12/optimizing-date-algorithms.html
{
const uint32_t year_base = 4800; /* Before min year, multiple of 400. */
const uint32_t m_adj = m - 3; /* March-based month. */
const uint32_t carry = m_adj > m ? 1 : 0;
const uint32_t adjust = carry ? 12 : 0;
const uint32_t y_adj = y + year_base - carry;
const uint32_t month_days = ((m_adj + adjust) * 62719 + 769) / 2048;
const uint32_t leap_days = y_adj / 4 - y_adj / 100 + y_adj / 400;
return y_adj * 365 + leap_days + month_days + (d - 1) - 2472632;
} //epoch_days
static uint64_t SystemTime_to_esecs( SYSTEMTIME & st )
{
// takes a Windows system time and returns linux epoch seconds
uint32_t edays = epoch_days( st. wYear, st.wMonth, st.wDay );
uint32_t secs = ( st.wHour * 3600 ) + ( st.wMinute * 60 ) + st.wSecond;
return ( edays * 24ull * 3600ull ) + secs;
} //SystemTime_to_esecs
static int fill_pstat_windows( int descriptor, struct stat_linux_syscall * pstat, const char * path )
{
char ac[ MAX_PATH ];
if ( 0 != path )
{
strcpy( ac, path );
slash_to_backslash( ac );
}
else
ac[ 0 ] = 0;
memset( pstat, 0, sizeof( struct stat_linux_syscall ) );
// default values, most of which are lies
pstat->st_ino = 3;
pstat->st_nlink = 1;
pstat->st_uid = 1000;
pstat->st_gid = 5;
pstat->st_rdev = 1024; // this is st_blksize on linux
pstat->st_size = 0;
if ( descriptor >= 0 && descriptor <= 2 ) // stdin / stdout / stderr
{
if ( isatty( descriptor ) )
pstat->st_mode = S_IFCHR;
else
{
pstat->st_mode = S_IFREG;
pstat->st_rdev = 4096; // this is st_blksize on linux
}
}
else if ( findFirstDescriptor == descriptor )
{
pstat->st_mode = S_IFDIR;
pstat->st_rdev = 4096; // this is st_blksize on linux
}
else if ( timebaseFrequencyDescriptor == descriptor )
{
pstat->st_mode = S_IFREG;
pstat->st_rdev = 4096; // this is st_blksize on linux
}
else if ( osreleaseDescriptor == descriptor )
{
pstat->st_mode = S_IFREG;
pstat->st_rdev = 4096; // this is st_blksize on linux
}
else
{
WIN32_FILE_ATTRIBUTE_DATA data = {0};
BOOL ok = FALSE;
if ( ac[ 0 ] )
{
ok = GetFileAttributesExA( ac, GetFileExInfoStandard, & data );
tracer.Trace( " result of GetFileAttributesEx on '%s': %d\n", ac, ok );
}
if ( !ok && ( descriptor < 0 ) )
{
errno = 2; // not found;
return -1;
}
if ( ok )
{
if ( data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
pstat->st_mode = S_IFDIR;
else
pstat->st_mode = S_IFREG;
// in spirit: st_mtim = data.ftLastWriteTime;
SYSTEMTIME st = {0};
FileTimeToSystemTime( &data.ftLastWriteTime, &st );
pstat->st_mtim.tv_sec = SystemTime_to_esecs( st );
}
pstat->st_rdev = 4096; // this is st_blksize on linux
if ( !ok && ( descriptor > 0 ) )
{
struct _stat statbuf;
int result = _fstat( descriptor, &statbuf );
if ( 0 == result )
{
pstat->st_mtim.tv_sec = statbuf.st_mtime;
data.nFileSizeLow = statbuf.st_size;
}
else
return -1;
}
pstat->st_size = data.nFileSizeLow;
}
return 0;
} //fill_pstat_windows
// taken from gnu's time.h.
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 1
#define CLOCK_PROCESS_CPUTIME_ID 2
#define CLOCK_THREAD_CPUTIME_ID 3
#define CLOCK_MONOTONIC_RAW 4
#ifndef CLOCK_REALTIME_COARSE // mingw64's pthread_time.h defines this as 4 because standards
#define CLOCK_REALTIME_COARSE 5
#endif
#define CLOCK_MONOTONIC_COARSE 6
static const char * clockids[] =
{
"realtime",
"monotonic",
"process_cputime_id",
"thread_cputime_id",
"monotonic_raw",
"realtime_coarse",
"monotonic_coarse",
};
typedef int clockid_t;
static const char * get_clockid( clockid_t clockid )
{
if ( clockid < _countof( clockids ) )
return clockids[ clockid ];
return "unknown";
} //get_clockid
high_resolution_clock::time_point g_tAppStart;
static int msc_clock_gettime( clockid_t clockid, struct timespec_syscall * tv )
{
tracer.Trace( " msc_clock_gettime, clockid %d == %s\n", clockid, get_clockid( clockid ) );
uint64_t diff = 0;
if ( CLOCK_REALTIME == clockid || CLOCK_REALTIME_COARSE == clockid )
{
system_clock::duration d = system_clock::now().time_since_epoch();
diff = duration_cast<nanoseconds>( d ).count();
}
else if ( CLOCK_MONOTONIC == clockid || CLOCK_MONOTONIC_COARSE == clockid || CLOCK_MONOTONIC_RAW == clockid ||
CLOCK_PROCESS_CPUTIME_ID == clockid || CLOCK_THREAD_CPUTIME_ID == clockid )
{
high_resolution_clock::time_point tNow = high_resolution_clock::now();
diff = duration_cast<std::chrono::nanoseconds>( tNow - g_tAppStart ).count();
}
tv->tv_sec = diff / 1000000000ULL;
tv->tv_nsec = diff % 1000000000ULL;
return 0;
} //msc_clock_gettime
#endif
#ifdef __APPLE__
// For each of these flag fields o/i/l/c this code translates the subset of the values actually used in the apps
// I validated. There are certainly other cases that are still broken (though many aren't implemented in MacOS,
// so translating them wouldn't have any impact). If anyone understands the historical reasons for the differences
// in these flags I'd love to hear about it. Why did coders knowngly pick different constant values?
static tcflag_t map_termios_oflag_linux_to_macos( tcflag_t f )
{
tcflag_t r = 0;
if ( f & 1 ) // OPOST is the same bit
r |= 1;
if ( f & 4 ) // ONLCR
r |= 2;
if ( f & 8 ) // OCRNL
r |= 0x10;
if ( f & 0x10 ) // ONOCR
r |= 0x20;
if ( f & 0x20 ) // ONLRET
r |= 0x40;
return r;
} //map_termios_oflag_linux_to_macos
static tcflag_t map_termios_oflag_macos_to_linux( tcflag_t f )
{
tcflag_t r = 0;
if ( f & 1 ) // OPOST is the same bit for both
r |= 1;
if ( f & 2 ) // ONLCR
r |= 4;
if ( f & 0x10 ) // OCRNL
r |= 8;
if ( f & 0x20 ) // ONOCR
r |= 0x10;
if ( f & 0x40 ) // ONLRET
r |= 0x20;
return r;
} //map_termios_oflag_linux_to_macos
static tcflag_t map_termios_iflag_linux_to_macos( tcflag_t f )
{
tcflag_t r = f;
if ( f & 0x400 ) // IXON
{
r &= ~ 0x400;
r |= 0x200;
}
else if ( f & 0x1000 ) // IXOFF
{
r &= ~ 0x1000;
r |= 0x400;
}
return r;
} //map_termios_oflag_linux_to_macos
static tcflag_t map_termios_iflag_macos_to_linux( tcflag_t f )
{
tcflag_t r = f;
if ( f & 0x200 ) // IXON
{
r &= ~ 0x200;
r |= 0x400;
}
else if ( f & 0x400 ) // IXOFF
{
r &= ~ 0x400;
r |= 0x1000;
}
return r;
} //map_termios_oflag_linux_to_macos
static tcflag_t map_termios_lflag_linux_to_macos( tcflag_t f )
{
tcflag_t r = 0;
if ( f & 1 ) // ICANON
r |= 0x80;
if ( f & 2 ) // ECHONL
r |= 0x100;
if ( f & 8 ) // ECHOK
r |= 8;
if ( f & 0x10 ) // ECHOKE
r |= 2;
if ( f & 0x20 ) // ECHOE
r |= 4;
if ( f & 0x40 ) // ECHO
r |= 0x10;
if ( f & 0x100 ) // EXTPROC
r |= 0x400000;
if ( f & 0x200 ) // ECHOPRT
r |= 0x40;
if ( f & 0x400 ) // ECONL
r |= 0x20;
if ( f & 0x8000 ) // ISIG
r |= 0x400;
if ( f & 0x10000 ) // IEXTEN
r |= 0x800;
return r;
} //map_termios_lflag_linux_to_macos
static tcflag_t map_termios_lflag_macos_to_linux( tcflag_t f )
{
tcflag_t r = 0;
if ( f & 0x80 ) // ICANON
r |= 1;
if ( f & 0x100 ) // ECHONL
r |= 2;
if ( f & 8 ) // ECHOK
r |= 8;
if ( f & 2 ) // ECHOKE
r |= 0x10;
if ( f & 4 ) // ECHOE
r |= 0x20;
if ( f & 0x10 ) // ECHO
r |= 0x40;
if ( f & 0x400000 ) // EXTPROC
r |= 0x100;
if ( f & 0x40 ) // ECHOPRT
r |= 0x200;
if ( f & 0x20 ) // ECONL
r |= 0x400;
if ( f & 0x400 ) // ISIG
r |= 0x8000;
if ( f & 0x800 ) // IEXTEN
r |= 0x10000;
return r;
} //map_termios_lflag_linux_to_macos
static tcflag_t map_termios_cflag_linux_to_macos( tcflag_t f )
{
tcflag_t r = 0;
if ( f & 0x10 ) // CS5
r |= 0x100;
if ( f & 0x20 ) // CS6 CLOCAL is also 0x20 on linux and 0x400 on MacOS
r |= 0x200;
if ( f & 0x30 ) // CS7
r |= 0x300;
if ( f & 0x40 ) // CSIZE
r |= 0x400;
if ( f & 0x80 ) // CSTOPB
r |= 0x800;
if ( f & 0x100 ) // CREAD
r |= 0x1000;
if ( f & 0x200 ) // PARENB
r |= 0x2000;
if ( f & 0x400 ) // CS8
r |= 0x4000;
if ( f & 0x800 ) // HUPCL
r |= 0x8000;
return r;
} //map_termios_cflag_linux_to_macos
static tcflag_t map_termios_cflag_macos_to_linux( tcflag_t f )
{
tcflag_t r = 0;
if ( f & 0x100 ) // CS5
r |= 0x10;
if ( f & 0x200 ) // CS6 CLOCAL is also 0x20 on linux and 0x400 on MacOS
r |= 0x20;
if ( f & 0x300 ) // CS7
r |= 0x30;
if ( f & 0x400 ) // CSIZE
r |= 0x40;
if ( f & 0x800 ) // CSTOPB
r |= 0x80;
if ( f & 0x1000 ) // CREAD
r |= 0x100;
if ( f & 0x2000 ) // PARENB
r |= 0x200;
if ( f & 0x4000 ) // CS8
r |= 0x400;
if ( f & 0x8000 ) // HUPCL
r |= 0x800;
return r;
} //map_termios_cflag_linux_to_macos
#endif
#if !defined(__APPLE__)
#if defined(__ARM_32BIT_STATE) || defined(__ARM_64BIT_STATE) || defined(__riscv)
static int linux_swap_riscv64_arm_dir_open_flags( int flags )
{
// values are the same aside from these, which are flipped:
// riscv64 arm32 and arm64
// O_DIRECT 0x4000 0x10000
// O_DIRECTORY 0x10000 0x4000
int r = flags;
if ( flags & 0x4000 )
{
r &= ~0x4000;
r |= 0x10000;
}
if ( flags & 0x10000 )
{
r &= ~0x10000;
r |= 0x4000;
}
tracer.Trace( " O_DIRECT %#x, O_DIRECTORY %#x\n", O_DIRECT, O_DIRECTORY );
tracer.Trace( " mapped from flags %#x to flags %#x\n", flags, r );
return r;