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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
|
/*
* Copyright (c) 2003-2005 The Regents of The University of Michigan
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer;
* redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution;
* neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Authors: Steve Reinhardt
* Ali Saidi
*/
#include "sim/syscall_emul.hh"
#include <fcntl.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <csignal>
#include <iostream>
#include <mutex>
#include <string>
#include "arch/utility.hh"
#include "base/chunk_generator.hh"
#include "base/trace.hh"
#include "config/the_isa.hh"
#include "cpu/thread_context.hh"
#include "dev/net/dist_iface.hh"
#include "mem/page_table.hh"
#include "sim/byteswap.hh"
#include "sim/process.hh"
#include "sim/sim_exit.hh"
#include "sim/syscall_debug_macros.hh"
#include "sim/syscall_desc.hh"
#include "sim/system.hh"
using namespace std;
using namespace TheISA;
SyscallReturn
unimplementedFunc(SyscallDesc *desc, int callnum, Process *process,
ThreadContext *tc)
{
fatal("syscall %s (#%d) unimplemented.", desc->name(), callnum);
return 1;
}
SyscallReturn
ignoreFunc(SyscallDesc *desc, int callnum, Process *process,
ThreadContext *tc)
{
if (desc->needWarning()) {
warn("ignoring syscall %s(...)%s", desc->name(), desc->warnOnce() ?
"\n (further warnings will be suppressed)" : "");
}
return 0;
}
static void
exitFutexWake(ThreadContext *tc, Addr addr, uint64_t tgid)
{
// Clear value at address pointed to by thread's childClearTID field.
BufferArg ctidBuf(addr, sizeof(long));
long *ctid = (long *)ctidBuf.bufferPtr();
*ctid = 0;
ctidBuf.copyOut(tc->getMemProxy());
FutexMap &futex_map = tc->getSystemPtr()->futexMap;
// Wake one of the waiting threads.
futex_map.wakeup(addr, tgid, 1);
}
static SyscallReturn
exitImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
bool group)
{
int index = 0;
int status = p->getSyscallArg(tc, index);
System *sys = tc->getSystemPtr();
if (group)
*p->exitGroup = true;
if (p->childClearTID)
exitFutexWake(tc, p->childClearTID, p->tgid());
bool last_thread = true;
Process *parent = nullptr, *tg_lead = nullptr;
for (int i = 0; last_thread && i < sys->numContexts(); i++) {
Process *walk;
if (!(walk = sys->threadContexts[i]->getProcessPtr()))
continue;
/**
* Threads in a thread group require special handing. For instance,
* we send the SIGCHLD signal so that it appears that it came from
* the head of the group. We also only delete file descriptors if
* we are the last thread in the thread group.
*/
if (walk->pid() == p->tgid())
tg_lead = walk;
if ((sys->threadContexts[i]->status() != ThreadContext::Halted) &&
(sys->threadContexts[i]->status() != ThreadContext::Halting) &&
(walk != p)) {
/**
* Check if we share thread group with the pointer; this denotes
* that we are not the last thread active in the thread group.
* Note that setting this to false also prevents further
* iterations of the loop.
*/
if (walk->tgid() == p->tgid()) {
/**
* If p is trying to exit_group and both walk and p are in
* the same thread group (i.e., sharing the same tgid),
* we need to halt walk's thread context. After all threads
* except p are halted, p becomes the last thread in the
* group.
*
* If p is not doing exit_group and there exists another
* active thread context in the group, last_thread is
* set to false to prevent the parent thread from killing
* all threads in the group.
*/
if (*(p->exitGroup)) {
sys->threadContexts[i]->halt();
} else {
last_thread = false;
}
}
/**
* A corner case exists which involves execve(). After execve(),
* the execve will enable SIGCHLD in the process. The problem
* occurs when the exiting process is the root process in the
* system; there is no parent to receive the signal. We obviate
* this problem by setting the root process' ppid to zero in the
* Python configuration files. We really should handle the
* root/execve specific case more gracefully.
*/
if (*p->sigchld && (p->ppid() != 0) && (walk->pid() == p->ppid()))
parent = walk;
}
}
if (last_thread) {
if (parent) {
assert(tg_lead);
sys->signalList.push_back(BasicSignal(tg_lead, parent, SIGCHLD));
}
/**
* Run though FD array of the exiting process and close all file
* descriptors except for the standard file descriptors.
* (The standard file descriptors are shared with gem5.)
*/
for (int i = 0; i < p->fds->getSize(); i++) {
if ((*p->fds)[i])
p->fds->closeFDEntry(i);
}
}
tc->halt();
/**
* check to see if there is no more active thread in the system. If so,
* exit the simulation loop
*/
int activeContexts = 0;
for (auto &system: sys->systemList)
activeContexts += system->numRunningContexts();
if (activeContexts == 0) {
/**
* Even though we are terminating the final thread context, dist-gem5
* requires the simulation to remain active and provide
* synchronization messages to the switch process. So we just halt
* the last thread context and return. The simulation will be
* terminated by dist-gem5 in a coordinated manner once all nodes
* have signaled their readiness to exit. For non dist-gem5
* simulations, readyToExit() always returns true.
*/
if (!DistIface::readyToExit(0)) {
return status;
}
exitSimLoop("exiting with last active thread context", status & 0xff);
return status;
}
return status;
}
SyscallReturn
exitFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
{
return exitImpl(desc, callnum, p, tc, false);
}
SyscallReturn
exitGroupFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
{
return exitImpl(desc, callnum, p, tc, true);
}
SyscallReturn
getpagesizeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
return (int)PageBytes;
}
SyscallReturn
brkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
// change brk addr to first arg
int index = 0;
Addr new_brk = p->getSyscallArg(tc, index);
std::shared_ptr<MemState> mem_state = p->memState;
Addr brk_point = mem_state->getBrkPoint();
// in Linux at least, brk(0) returns the current break value
// (note that the syscall and the glibc function have different behavior)
if (new_brk == 0)
return brk_point;
if (new_brk > brk_point) {
// might need to allocate some new pages
for (ChunkGenerator gen(brk_point,
new_brk - brk_point,
PageBytes); !gen.done(); gen.next()) {
if (!p->pTable->translate(gen.addr()))
p->allocateMem(roundDown(gen.addr(), PageBytes), PageBytes);
// if the address is already there, zero it out
else {
uint8_t zero = 0;
SETranslatingPortProxy &tp = tc->getMemProxy();
// split non-page aligned accesses
Addr next_page = roundUp(gen.addr(), PageBytes);
uint32_t size_needed = next_page - gen.addr();
tp.memsetBlob(gen.addr(), zero, size_needed);
if (gen.addr() + PageBytes > next_page &&
next_page < new_brk &&
p->pTable->translate(next_page)) {
size_needed = PageBytes - size_needed;
tp.memsetBlob(next_page, zero, size_needed);
}
}
}
}
mem_state->setBrkPoint(new_brk);
DPRINTF_SYSCALL(Verbose, "brk: break point changed to: %#X\n",
mem_state->getBrkPoint());
return mem_state->getBrkPoint();
}
SyscallReturn
setTidAddressFunc(SyscallDesc *desc, int callnum, Process *process,
ThreadContext *tc)
{
int index = 0;
uint64_t tidPtr = process->getSyscallArg(tc, index);
process->childClearTID = tidPtr;
return process->pid();
}
SyscallReturn
closeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
return p->fds->closeFDEntry(tgt_fd);
}
SyscallReturn
lseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
uint64_t offs = p->getSyscallArg(tc, index);
int whence = p->getSyscallArg(tc, index);
auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
if (!ffdp)
return -EBADF;
int sim_fd = ffdp->getSimFD();
off_t result = lseek(sim_fd, offs, whence);
return (result == (off_t)-1) ? -errno : result;
}
SyscallReturn
_llseekFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
uint64_t offset_high = p->getSyscallArg(tc, index);
uint32_t offset_low = p->getSyscallArg(tc, index);
Addr result_ptr = p->getSyscallArg(tc, index);
int whence = p->getSyscallArg(tc, index);
auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
if (!ffdp)
return -EBADF;
int sim_fd = ffdp->getSimFD();
uint64_t offset = (offset_high << 32) | offset_low;
uint64_t result = lseek(sim_fd, offset, whence);
result = TheISA::htog(result);
if (result == (off_t)-1)
return -errno;
// Assuming that the size of loff_t is 64 bits on the target platform
BufferArg result_buf(result_ptr, sizeof(result));
memcpy(result_buf.bufferPtr(), &result, sizeof(result));
result_buf.copyOut(tc->getMemProxy());
return 0;
}
SyscallReturn
munmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
// With mmap more fully implemented, it might be worthwhile to bite
// the bullet and implement munmap. Should allow us to reuse simulated
// memory.
return 0;
}
const char *hostname = "m5.eecs.umich.edu";
SyscallReturn
gethostnameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
Addr buf_ptr = p->getSyscallArg(tc, index);
int name_len = p->getSyscallArg(tc, index);
BufferArg name(buf_ptr, name_len);
strncpy((char *)name.bufferPtr(), hostname, name_len);
name.copyOut(tc->getMemProxy());
return 0;
}
SyscallReturn
getcwdFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int result = 0;
int index = 0;
Addr buf_ptr = p->getSyscallArg(tc, index);
unsigned long size = p->getSyscallArg(tc, index);
BufferArg buf(buf_ptr, size);
// Is current working directory defined?
string cwd = p->tgtCwd;
if (!cwd.empty()) {
if (cwd.length() >= size) {
// Buffer too small
return -ERANGE;
}
strncpy((char *)buf.bufferPtr(), cwd.c_str(), size);
result = cwd.length();
} else {
if (getcwd((char *)buf.bufferPtr(), size)) {
result = strlen((char *)buf.bufferPtr());
} else {
result = -1;
}
}
buf.copyOut(tc->getMemProxy());
return (result == -1) ? -errno : result;
}
SyscallReturn
readlinkFunc(SyscallDesc *desc, int callnum, Process *process,
ThreadContext *tc)
{
return readlinkFunc(desc, callnum, process, tc, 0);
}
SyscallReturn
readlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
int index)
{
string path;
if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
return -EFAULT;
// Adjust path for cwd and redirection
path = p->checkPathRedirect(path);
Addr buf_ptr = p->getSyscallArg(tc, index);
size_t bufsiz = p->getSyscallArg(tc, index);
BufferArg buf(buf_ptr, bufsiz);
int result = -1;
if (path != "/proc/self/exe") {
result = readlink(path.c_str(), (char *)buf.bufferPtr(), bufsiz);
} else {
// Emulate readlink() called on '/proc/self/exe' should return the
// absolute path of the binary running in the simulated system (the
// Process' executable). It is possible that using this path in
// the simulated system will result in unexpected behavior if:
// 1) One binary runs another (e.g., -c time -o "my_binary"), and
// called binary calls readlink().
// 2) The host's full path to the running benchmark changes from one
// simulation to another. This can result in different simulated
// performance since the simulated system will process the binary
// path differently, even if the binary itself does not change.
// Get the absolute canonical path to the running application
char real_path[PATH_MAX];
char *check_real_path = realpath(p->progName(), real_path);
if (!check_real_path) {
fatal("readlink('/proc/self/exe') unable to resolve path to "
"executable: %s", p->progName());
}
strncpy((char*)buf.bufferPtr(), real_path, bufsiz);
size_t real_path_len = strlen(real_path);
if (real_path_len > bufsiz) {
// readlink will truncate the contents of the
// path to ensure it is no more than bufsiz
result = bufsiz;
} else {
result = real_path_len;
}
// Issue a warning about potential unexpected results
warn_once("readlink() called on '/proc/self/exe' may yield unexpected "
"results in various settings.\n Returning '%s'\n",
(char*)buf.bufferPtr());
}
buf.copyOut(tc->getMemProxy());
return (result == -1) ? -errno : result;
}
SyscallReturn
unlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
return unlinkHelper(desc, num, p, tc, 0);
}
SyscallReturn
unlinkHelper(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
int index)
{
string path;
if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
return -EFAULT;
path = p->checkPathRedirect(path);
int result = unlink(path.c_str());
return (result == -1) ? -errno : result;
}
SyscallReturn
linkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
string path;
string new_path;
int index = 0;
auto &virt_mem = tc->getMemProxy();
if (!virt_mem.tryReadString(path, p->getSyscallArg(tc, index)))
return -EFAULT;
if (!virt_mem.tryReadString(new_path, p->getSyscallArg(tc, index)))
return -EFAULT;
path = p->absolutePath(path, true);
new_path = p->absolutePath(new_path, true);
int result = link(path.c_str(), new_path.c_str());
return (result == -1) ? -errno : result;
}
SyscallReturn
symlinkFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
string path;
string new_path;
int index = 0;
auto &virt_mem = tc->getMemProxy();
if (!virt_mem.tryReadString(path, p->getSyscallArg(tc, index)))
return -EFAULT;
if (!virt_mem.tryReadString(new_path, p->getSyscallArg(tc, index)))
return -EFAULT;
path = p->absolutePath(path, true);
new_path = p->absolutePath(new_path, true);
int result = symlink(path.c_str(), new_path.c_str());
return (result == -1) ? -errno : result;
}
SyscallReturn
mkdirFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
std::string path;
if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
return -EFAULT;
path = p->checkPathRedirect(path);
mode_t mode = p->getSyscallArg(tc, index);
auto result = mkdir(path.c_str(), mode);
return (result == -1) ? -errno : result;
}
SyscallReturn
renameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
string old_name;
int index = 0;
if (!tc->getMemProxy().tryReadString(old_name, p->getSyscallArg(tc, index)))
return -EFAULT;
string new_name;
if (!tc->getMemProxy().tryReadString(new_name, p->getSyscallArg(tc, index)))
return -EFAULT;
// Adjust path for cwd and redirection
old_name = p->checkPathRedirect(old_name);
new_name = p->checkPathRedirect(new_name);
int64_t result = rename(old_name.c_str(), new_name.c_str());
return (result == -1) ? -errno : result;
}
SyscallReturn
truncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
string path;
int index = 0;
if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
return -EFAULT;
off_t length = p->getSyscallArg(tc, index);
// Adjust path for cwd and redirection
path = p->checkPathRedirect(path);
int result = truncate(path.c_str(), length);
return (result == -1) ? -errno : result;
}
SyscallReturn
ftruncateFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
off_t length = p->getSyscallArg(tc, index);
auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
if (!ffdp)
return -EBADF;
int sim_fd = ffdp->getSimFD();
int result = ftruncate(sim_fd, length);
return (result == -1) ? -errno : result;
}
SyscallReturn
truncate64Func(SyscallDesc *desc, int num,
Process *process, ThreadContext *tc)
{
int index = 0;
string path;
if (!tc->getMemProxy().tryReadString(path, process->getSyscallArg(tc, index)))
return -EFAULT;
int64_t length = process->getSyscallArg(tc, index, 64);
// Adjust path for cwd and redirection
path = process->checkPathRedirect(path);
#if NO_STAT64
int result = truncate(path.c_str(), length);
#else
int result = truncate64(path.c_str(), length);
#endif
return (result == -1) ? -errno : result;
}
SyscallReturn
ftruncate64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
int64_t length = p->getSyscallArg(tc, index, 64);
auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
if (!ffdp)
return -EBADF;
int sim_fd = ffdp->getSimFD();
#if NO_STAT64
int result = ftruncate(sim_fd, length);
#else
int result = ftruncate64(sim_fd, length);
#endif
return (result == -1) ? -errno : result;
}
SyscallReturn
umaskFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
{
// Letting the simulated program change the simulator's umask seems like
// a bad idea. Compromise by just returning the current umask but not
// changing anything.
mode_t oldMask = umask(0);
umask(oldMask);
return (int)oldMask;
}
SyscallReturn
chownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
string path;
int index = 0;
if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
return -EFAULT;
/* XXX endianess */
uint32_t owner = p->getSyscallArg(tc, index);
uid_t hostOwner = owner;
uint32_t group = p->getSyscallArg(tc, index);
gid_t hostGroup = group;
// Adjust path for cwd and redirection
path = p->checkPathRedirect(path);
int result = chown(path.c_str(), hostOwner, hostGroup);
return (result == -1) ? -errno : result;
}
SyscallReturn
fchownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
if (!ffdp)
return -EBADF;
int sim_fd = ffdp->getSimFD();
/* XXX endianess */
uint32_t owner = p->getSyscallArg(tc, index);
uid_t hostOwner = owner;
uint32_t group = p->getSyscallArg(tc, index);
gid_t hostGroup = group;
int result = fchown(sim_fd, hostOwner, hostGroup);
return (result == -1) ? -errno : result;
}
/**
* FIXME: The file description is not shared among file descriptors created
* with dup. Really, it's difficult to maintain fields like file offset or
* flags since an update to such a field won't be reflected in the metadata
* for the fd entries that we maintain for checkpoint restoration.
*/
SyscallReturn
dupFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
auto old_hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
if (!old_hbfdp)
return -EBADF;
int sim_fd = old_hbfdp->getSimFD();
int result = dup(sim_fd);
if (result == -1)
return -errno;
auto new_hbfdp = std::dynamic_pointer_cast<HBFDEntry>(old_hbfdp->clone());
new_hbfdp->setSimFD(result);
new_hbfdp->setCOE(false);
return p->fds->allocFD(new_hbfdp);
}
SyscallReturn
dup2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int old_tgt_fd = p->getSyscallArg(tc, index);
auto old_hbp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[old_tgt_fd]);
if (!old_hbp)
return -EBADF;
int old_sim_fd = old_hbp->getSimFD();
/**
* We need a valid host file descriptor number to be able to pass into
* the second parameter for dup2 (newfd), but we don't know what the
* viable numbers are; we execute the open call to retrieve one.
*/
int res_fd = dup2(old_sim_fd, open("/dev/null", O_RDONLY));
if (res_fd == -1)
return -errno;
int new_tgt_fd = p->getSyscallArg(tc, index);
auto new_hbp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[new_tgt_fd]);
if (new_hbp)
p->fds->closeFDEntry(new_tgt_fd);
new_hbp = std::dynamic_pointer_cast<HBFDEntry>(old_hbp->clone());
new_hbp->setSimFD(res_fd);
new_hbp->setCOE(false);
return p->fds->allocFD(new_hbp);
}
SyscallReturn
fcntlFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int arg;
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
int cmd = p->getSyscallArg(tc, index);
auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
if (!hbfdp)
return -EBADF;
int sim_fd = hbfdp->getSimFD();
int coe = hbfdp->getCOE();
switch (cmd) {
case F_GETFD:
return coe & FD_CLOEXEC;
case F_SETFD: {
arg = p->getSyscallArg(tc, index);
arg ? hbfdp->setCOE(true) : hbfdp->setCOE(false);
return 0;
}
// Rely on the host to maintain the file status flags for this file
// description rather than maintain it ourselves. Admittedly, this
// is suboptimal (and possibly error prone), but it is difficult to
// maintain the flags by tracking them across the different descriptors
// (that refer to this file description) caused by clone, dup, and
// subsequent fcntls.
case F_GETFL:
case F_SETFL: {
arg = p->getSyscallArg(tc, index);
int rv = fcntl(sim_fd, cmd, arg);
return (rv == -1) ? -errno : rv;
}
default:
warn("fcntl: unsupported command %d\n", cmd);
return 0;
}
}
SyscallReturn
fcntl64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
if (!hbfdp)
return -EBADF;
int sim_fd = hbfdp->getSimFD();
int cmd = p->getSyscallArg(tc, index);
switch (cmd) {
case 33: //F_GETLK64
warn("fcntl64(%d, F_GETLK64) not supported, error returned\n", tgt_fd);
return -EMFILE;
case 34: // F_SETLK64
case 35: // F_SETLKW64
warn("fcntl64(%d, F_SETLK(W)64) not supported, error returned\n",
tgt_fd);
return -EMFILE;
default:
// not sure if this is totally valid, but we'll pass it through
// to the underlying OS
warn("fcntl64(%d, %d) passed through to host\n", tgt_fd, cmd);
return fcntl(sim_fd, cmd);
}
}
SyscallReturn
pipeImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
bool pseudoPipe)
{
Addr tgt_addr = 0;
if (!pseudoPipe) {
int index = 0;
tgt_addr = p->getSyscallArg(tc, index);
}
int sim_fds[2], tgt_fds[2];
int pipe_retval = pipe(sim_fds);
if (pipe_retval == -1)
return -errno;
auto rend = PipeFDEntry::EndType::read;
auto rpfd = std::make_shared<PipeFDEntry>(sim_fds[0], O_WRONLY, rend);
tgt_fds[0] = p->fds->allocFD(rpfd);
auto wend = PipeFDEntry::EndType::write;
auto wpfd = std::make_shared<PipeFDEntry>(sim_fds[1], O_RDONLY, wend);
tgt_fds[1] = p->fds->allocFD(wpfd);
/**
* Now patch the read object to record the target file descriptor chosen
* as the write end of the pipe.
*/
rpfd->setPipeReadSource(tgt_fds[1]);
/**
* Alpha Linux convention for pipe() is that fd[0] is returned as
* the return value of the function, and fd[1] is returned in r20.
*/
if (pseudoPipe) {
tc->setIntReg(SyscallPseudoReturnReg, tgt_fds[1]);
return tgt_fds[0];
}
/**
* Copy the target file descriptors into buffer space and then copy
* the buffer space back into the target address space.
*/
BufferArg tgt_handle(tgt_addr, sizeof(int[2]));
int *buf_ptr = (int*)tgt_handle.bufferPtr();
buf_ptr[0] = tgt_fds[0];
buf_ptr[1] = tgt_fds[1];
tgt_handle.copyOut(tc->getMemProxy());
return 0;
}
SyscallReturn
pipePseudoFunc(SyscallDesc *desc, int callnum, Process *process,
ThreadContext *tc)
{
return pipeImpl(desc, callnum, process, tc, true);
}
SyscallReturn
pipeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
{
return pipeImpl(desc, callnum, process, tc, false);
}
SyscallReturn
setpgidFunc(SyscallDesc *desc, int callnum, Process *process,
ThreadContext *tc)
{
int index = 0;
int pid = process->getSyscallArg(tc, index);
int pgid = process->getSyscallArg(tc, index);
if (pgid < 0)
return -EINVAL;
if (pid == 0) {
process->setpgid(process->pid());
return 0;
}
Process *matched_ph = nullptr;
System *sysh = tc->getSystemPtr();
// Retrieves process pointer from active/suspended thread contexts.
for (int i = 0; i < sysh->numContexts(); i++) {
if (sysh->threadContexts[i]->status() != ThreadContext::Halted) {
Process *temp_h = sysh->threadContexts[i]->getProcessPtr();
Process *walk_ph = (Process*)temp_h;
if (walk_ph && walk_ph->pid() == process->pid())
matched_ph = walk_ph;
}
}
assert(matched_ph);
matched_ph->setpgid((pgid == 0) ? matched_ph->pid() : pgid);
return 0;
}
SyscallReturn
getpidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
ThreadContext *tc)
{
// Make up a PID. There's no interprocess communication in
// fake_syscall mode, so there's no way for a process to know it's
// not getting a unique value.
tc->setIntReg(SyscallPseudoReturnReg, process->ppid());
return process->pid();
}
SyscallReturn
getuidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
ThreadContext *tc)
{
// Make up a UID and EUID... it shouldn't matter, and we want the
// simulation to be deterministic.
// EUID goes in r20.
tc->setIntReg(SyscallPseudoReturnReg, process->euid()); // EUID
return process->uid(); // UID
}
SyscallReturn
getgidPseudoFunc(SyscallDesc *desc, int callnum, Process *process,
ThreadContext *tc)
{
// Get current group ID. EGID goes in r20.
tc->setIntReg(SyscallPseudoReturnReg, process->egid()); // EGID
return process->gid();
}
SyscallReturn
setuidFunc(SyscallDesc *desc, int callnum, Process *process,
ThreadContext *tc)
{
// can't fathom why a benchmark would call this.
int index = 0;
warn("Ignoring call to setuid(%d)\n", process->getSyscallArg(tc, index));
return 0;
}
SyscallReturn
getpidFunc(SyscallDesc *desc, int callnum, Process *process,
ThreadContext *tc)
{
return process->tgid();
}
SyscallReturn
gettidFunc(SyscallDesc *desc, int callnum, Process *process,
ThreadContext *tc)
{
return process->pid();
}
SyscallReturn
getppidFunc(SyscallDesc *desc, int callnum, Process *process,
ThreadContext *tc)
{
return process->ppid();
}
SyscallReturn
getuidFunc(SyscallDesc *desc, int callnum, Process *process,
ThreadContext *tc)
{
return process->uid(); // UID
}
SyscallReturn
geteuidFunc(SyscallDesc *desc, int callnum, Process *process,
ThreadContext *tc)
{
return process->euid(); // UID
}
SyscallReturn
getgidFunc(SyscallDesc *desc, int callnum, Process *process,
ThreadContext *tc)
{
return process->gid();
}
SyscallReturn
getegidFunc(SyscallDesc *desc, int callnum, Process *process,
ThreadContext *tc)
{
return process->egid();
}
SyscallReturn
fallocateFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
{
#if NO_FALLOCATE
warn("Host OS cannot support calls to fallocate. Ignoring syscall");
#else
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
int mode = p->getSyscallArg(tc, index);
off_t offset = p->getSyscallArg(tc, index);
off_t len = p->getSyscallArg(tc, index);
auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
if (!ffdp)
return -EBADF;
int sim_fd = ffdp->getSimFD();
int result = fallocate(sim_fd, mode, offset, len);
if (result < 0)
return -errno;
#endif
return 0;
}
SyscallReturn
accessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
int index)
{
string path;
if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
return -EFAULT;
// Adjust path for cwd and redirection
path = p->checkPathRedirect(path);
mode_t mode = p->getSyscallArg(tc, index);
int result = access(path.c_str(), mode);
return (result == -1) ? -errno : result;
}
SyscallReturn
accessFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
{
return accessFunc(desc, callnum, p, tc, 0);
}
SyscallReturn
mknodFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
std::string path;
if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
return -EFAULT;
path = p->checkPathRedirect(path);
mode_t mode = p->getSyscallArg(tc, index);
dev_t dev = p->getSyscallArg(tc, index);
auto result = mknod(path.c_str(), mode, dev);
return (result == -1) ? -errno : result;
}
SyscallReturn
chdirFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
std::string path;
if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
return -EFAULT;
std::string tgt_cwd;
if (startswith(path, "/")) {
tgt_cwd = path;
} else {
char buf[PATH_MAX];
tgt_cwd = realpath((p->tgtCwd + "/" + path).c_str(), buf);
}
std::string host_cwd = p->checkPathRedirect(tgt_cwd);
int result = chdir(host_cwd.c_str());
if (result == -1)
return -errno;
p->hostCwd = host_cwd;
p->tgtCwd = tgt_cwd;
return result;
}
SyscallReturn
rmdirFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
std::string path;
if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
return -EFAULT;
path = p->checkPathRedirect(path);
auto result = rmdir(path.c_str());
return (result == -1) ? -errno : result;
}
#if defined(SYS_getdents) || defined(SYS_getdents64)
template<typename DE, int SYS_NUM>
static SyscallReturn
getdentsImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
Addr buf_ptr = p->getSyscallArg(tc, index);
unsigned count = p->getSyscallArg(tc, index);
auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
if (!hbfdp)
return -EBADF;
int sim_fd = hbfdp->getSimFD();
BufferArg buf_arg(buf_ptr, count);
auto status = syscall(SYS_NUM, sim_fd, buf_arg.bufferPtr(), count);
if (status == -1)
return -errno;
unsigned traversed = 0;
while (traversed < status) {
DE *buffer = (DE*)((Addr)buf_arg.bufferPtr() + traversed);
auto host_reclen = buffer->d_reclen;
/**
* Convert the byte ordering from the host to the target before
* passing the data back into the target's address space to preserve
* endianness.
*/
buffer->d_ino = htog(buffer->d_ino);
buffer->d_off = htog(buffer->d_off);
buffer->d_reclen = htog(buffer->d_reclen);
traversed += host_reclen;
}
buf_arg.copyOut(tc->getMemProxy());
return status;
}
#endif
#if defined(SYS_getdents)
SyscallReturn
getdentsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
{
typedef struct linux_dirent {
unsigned long d_ino;
unsigned long d_off;
unsigned short d_reclen;
char dname[];
} LinDent;
return getdentsImpl<LinDent, SYS_getdents>(desc, callnum, p, tc);
}
#endif
#if defined(SYS_getdents64)
SyscallReturn
getdents64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
{
typedef struct linux_dirent64 {
ino64_t d_ino;
off64_t d_off;
unsigned short d_reclen;
char dname[];
} LinDent64;
return getdentsImpl<LinDent64, SYS_getdents64>(desc, callnum, p, tc);
}
#endif
SyscallReturn
shutdownFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
int how = p->getSyscallArg(tc, index);
auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
if (!sfdp)
return -EBADF;
int sim_fd = sfdp->getSimFD();
int retval = shutdown(sim_fd, how);
return (retval == -1) ? -errno : retval;
}
SyscallReturn
bindFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
Addr buf_ptr = p->getSyscallArg(tc, index);
int addrlen = p->getSyscallArg(tc, index);
BufferArg bufSock(buf_ptr, addrlen);
bufSock.copyIn(tc->getMemProxy());
auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
if (!sfdp)
return -EBADF;
int sim_fd = sfdp->getSimFD();
int status = ::bind(sim_fd,
(struct sockaddr *)bufSock.bufferPtr(),
addrlen);
return (status == -1) ? -errno : status;
}
SyscallReturn
listenFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
int backlog = p->getSyscallArg(tc, index);
auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
if (!sfdp)
return -EBADF;
int sim_fd = sfdp->getSimFD();
int status = listen(sim_fd, backlog);
return (status == -1) ? -errno : status;
}
SyscallReturn
connectFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
Addr buf_ptr = p->getSyscallArg(tc, index);
int addrlen = p->getSyscallArg(tc, index);
BufferArg addr(buf_ptr, addrlen);
addr.copyIn(tc->getMemProxy());
auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
if (!sfdp)
return -EBADF;
int sim_fd = sfdp->getSimFD();
int status = connect(sim_fd,
(struct sockaddr *)addr.bufferPtr(),
(socklen_t)addrlen);
return (status == -1) ? -errno : status;
}
SyscallReturn
recvfromFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
Addr bufrPtr = p->getSyscallArg(tc, index);
size_t bufrLen = p->getSyscallArg(tc, index);
int flags = p->getSyscallArg(tc, index);
Addr addrPtr = p->getSyscallArg(tc, index);
Addr addrlenPtr = p->getSyscallArg(tc, index);
auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
if (!sfdp)
return -EBADF;
int sim_fd = sfdp->getSimFD();
// Reserve buffer space.
BufferArg bufrBuf(bufrPtr, bufrLen);
// Get address length.
socklen_t addrLen = 0;
if (addrlenPtr != 0) {
// Read address length parameter.
BufferArg addrlenBuf(addrlenPtr, sizeof(socklen_t));
addrlenBuf.copyIn(tc->getMemProxy());
addrLen = *((socklen_t *)addrlenBuf.bufferPtr());
}
struct sockaddr sa, *sap = NULL;
if (addrLen != 0) {
BufferArg addrBuf(addrPtr, addrLen);
addrBuf.copyIn(tc->getMemProxy());
memcpy(&sa, (struct sockaddr *)addrBuf.bufferPtr(),
sizeof(struct sockaddr));
sap = &sa;
}
ssize_t recvd_size = recvfrom(sim_fd,
(void *)bufrBuf.bufferPtr(),
bufrLen, flags, sap, (socklen_t *)&addrLen);
if (recvd_size == -1)
return -errno;
// Pass the received data out.
bufrBuf.copyOut(tc->getMemProxy());
// Copy address to addrPtr and pass it on.
if (sap != NULL) {
BufferArg addrBuf(addrPtr, addrLen);
memcpy(addrBuf.bufferPtr(), sap, sizeof(sa));
addrBuf.copyOut(tc->getMemProxy());
}
// Copy len to addrlenPtr and pass it on.
if (addrLen != 0) {
BufferArg addrlenBuf(addrlenPtr, sizeof(socklen_t));
*(socklen_t *)addrlenBuf.bufferPtr() = addrLen;
addrlenBuf.copyOut(tc->getMemProxy());
}
return recvd_size;
}
SyscallReturn
sendtoFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
Addr bufrPtr = p->getSyscallArg(tc, index);
size_t bufrLen = p->getSyscallArg(tc, index);
int flags = p->getSyscallArg(tc, index);
Addr addrPtr = p->getSyscallArg(tc, index);
socklen_t addrLen = p->getSyscallArg(tc, index);
auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
if (!sfdp)
return -EBADF;
int sim_fd = sfdp->getSimFD();
// Reserve buffer space.
BufferArg bufrBuf(bufrPtr, bufrLen);
bufrBuf.copyIn(tc->getMemProxy());
struct sockaddr sa, *sap = nullptr;
memset(&sa, 0, sizeof(sockaddr));
if (addrLen != 0) {
BufferArg addrBuf(addrPtr, addrLen);
addrBuf.copyIn(tc->getMemProxy());
memcpy(&sa, (sockaddr*)addrBuf.bufferPtr(), addrLen);
sap = &sa;
}
ssize_t sent_size = sendto(sim_fd,
(void *)bufrBuf.bufferPtr(),
bufrLen, flags, sap, (socklen_t)addrLen);
return (sent_size == -1) ? -errno : sent_size;
}
SyscallReturn
recvmsgFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
Addr msgPtr = p->getSyscallArg(tc, index);
int flags = p->getSyscallArg(tc, index);
auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
if (!sfdp)
return -EBADF;
int sim_fd = sfdp->getSimFD();
/**
* struct msghdr {
* void *msg_name; // optional address
* socklen_t msg_namelen; // size of address
* struct iovec *msg_iov; // iovec array
* size_t msg_iovlen; // number entries in msg_iov
* i // entries correspond to buffer
* void *msg_control; // ancillary data
* size_t msg_controllen; // ancillary data buffer len
* int msg_flags; // flags on received message
* };
*
* struct iovec {
* void *iov_base; // starting address
* size_t iov_len; // number of bytes to transfer
* };
*/
/**
* The plan with this system call is to replace all of the pointers in the
* structure and the substructure with BufferArg class pointers. We will
* copy every field from the structures into our BufferArg classes.
*/
BufferArg msgBuf(msgPtr, sizeof(struct msghdr));
msgBuf.copyIn(tc->getMemProxy());
struct msghdr *msgHdr = (struct msghdr *)msgBuf.bufferPtr();
/**
* We will use these address place holders to retain the pointers which
* we are going to replace with our own buffers in our simulator address
* space.
*/
Addr msg_name_phold = 0;
Addr msg_iov_phold = 0;
Addr iovec_base_phold[msgHdr->msg_iovlen];
Addr msg_control_phold = 0;
/**
* Record msg_name pointer then replace with buffer pointer.
*/
BufferArg *nameBuf = NULL;
if (msgHdr->msg_name) {
/*1*/msg_name_phold = (Addr)msgHdr->msg_name;
/*2*/nameBuf = new BufferArg(msg_name_phold, msgHdr->msg_namelen);
/*3*/nameBuf->copyIn(tc->getMemProxy());
/*4*/msgHdr->msg_name = nameBuf->bufferPtr();
}
/**
* Record msg_iov pointer then replace with buffer pointer. Also, setup
* an array of buffer pointers for the iovec structs record and replace
* their pointers with buffer pointers.
*/
BufferArg *iovBuf = NULL;
BufferArg *iovecBuf[msgHdr->msg_iovlen];
for (int i = 0; i < msgHdr->msg_iovlen; i++) {
iovec_base_phold[i] = 0;
iovecBuf[i] = NULL;
}
if (msgHdr->msg_iov) {
/*1*/msg_iov_phold = (Addr)msgHdr->msg_iov;
/*2*/iovBuf = new BufferArg(msg_iov_phold, msgHdr->msg_iovlen *
sizeof(struct iovec));
/*3*/iovBuf->copyIn(tc->getMemProxy());
for (int i = 0; i < msgHdr->msg_iovlen; i++) {
if (((struct iovec *)iovBuf->bufferPtr())[i].iov_base) {
/*1*/iovec_base_phold[i] =
(Addr)((struct iovec *)iovBuf->bufferPtr())[i].iov_base;
/*2*/iovecBuf[i] = new BufferArg(iovec_base_phold[i],
((struct iovec *)iovBuf->bufferPtr())[i].iov_len);
/*3*/iovecBuf[i]->copyIn(tc->getMemProxy());
/*4*/((struct iovec *)iovBuf->bufferPtr())[i].iov_base =
iovecBuf[i]->bufferPtr();
}
}
/*4*/msgHdr->msg_iov = (struct iovec *)iovBuf->bufferPtr();
}
/**
* Record msg_control pointer then replace with buffer pointer.
*/
BufferArg *controlBuf = NULL;
if (msgHdr->msg_control) {
/*1*/msg_control_phold = (Addr)msgHdr->msg_control;
/*2*/controlBuf = new BufferArg(msg_control_phold,
CMSG_ALIGN(msgHdr->msg_controllen));
/*3*/controlBuf->copyIn(tc->getMemProxy());
/*4*/msgHdr->msg_control = controlBuf->bufferPtr();
}
ssize_t recvd_size = recvmsg(sim_fd, msgHdr, flags);
if (recvd_size < 0)
return -errno;
if (msgHdr->msg_name) {
nameBuf->copyOut(tc->getMemProxy());
delete(nameBuf);
msgHdr->msg_name = (void *)msg_name_phold;
}
if (msgHdr->msg_iov) {
for (int i = 0; i< msgHdr->msg_iovlen; i++) {
if (((struct iovec *)iovBuf->bufferPtr())[i].iov_base) {
iovecBuf[i]->copyOut(tc->getMemProxy());
delete iovecBuf[i];
((struct iovec *)iovBuf->bufferPtr())[i].iov_base =
(void *)iovec_base_phold[i];
}
}
iovBuf->copyOut(tc->getMemProxy());
delete iovBuf;
msgHdr->msg_iov = (struct iovec *)msg_iov_phold;
}
if (msgHdr->msg_control) {
controlBuf->copyOut(tc->getMemProxy());
delete(controlBuf);
msgHdr->msg_control = (void *)msg_control_phold;
}
msgBuf.copyOut(tc->getMemProxy());
return recvd_size;
}
SyscallReturn
sendmsgFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
Addr msgPtr = p->getSyscallArg(tc, index);
int flags = p->getSyscallArg(tc, index);
auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
if (!sfdp)
return -EBADF;
int sim_fd = sfdp->getSimFD();
/**
* Reserve buffer space.
*/
BufferArg msgBuf(msgPtr, sizeof(struct msghdr));
msgBuf.copyIn(tc->getMemProxy());
struct msghdr msgHdr = *((struct msghdr *)msgBuf.bufferPtr());
/**
* Assuming msgHdr.msg_iovlen >= 1, then there is no point calling
* recvmsg without a buffer.
*/
struct iovec *iovPtr = msgHdr.msg_iov;
BufferArg iovBuf((Addr)iovPtr, sizeof(struct iovec) * msgHdr.msg_iovlen);
iovBuf.copyIn(tc->getMemProxy());
struct iovec *iov = (struct iovec *)iovBuf.bufferPtr();
msgHdr.msg_iov = iov;
/**
* Cannot instantiate buffers till inside the loop.
* Create array to hold buffer addresses, to be used during copyIn of
* send data.
*/
BufferArg **bufferArray = (BufferArg **)malloc(msgHdr.msg_iovlen
* sizeof(BufferArg *));
/**
* Iterate through the iovec structures:
* Get the base buffer addreses, reserve iov_len amount of space for each.
* Put the buf address into the bufferArray for later retrieval.
*/
for (int iovIndex = 0 ; iovIndex < msgHdr.msg_iovlen; iovIndex++) {
Addr basePtr = (Addr) iov[iovIndex].iov_base;
bufferArray[iovIndex] = new BufferArg(basePtr, iov[iovIndex].iov_len);
bufferArray[iovIndex]->copyIn(tc->getMemProxy());
iov[iovIndex].iov_base = bufferArray[iovIndex]->bufferPtr();
}
ssize_t sent_size = sendmsg(sim_fd, &msgHdr, flags);
int local_errno = errno;
/**
* Free dynamically allocated memory.
*/
for (int iovIndex = 0 ; iovIndex < msgHdr.msg_iovlen; iovIndex++) {
BufferArg *baseBuf = ( BufferArg *)bufferArray[iovIndex];
delete(baseBuf);
}
/**
* Malloced above.
*/
free(bufferArray);
return (sent_size < 0) ? -local_errno : sent_size;
}
SyscallReturn
getsockoptFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
// union of all possible return value types from getsockopt
union val {
int i_val;
long l_val;
struct linger linger_val;
struct timeval timeval_val;
} val;
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
int level = p->getSyscallArg(tc, index);
int optname = p->getSyscallArg(tc, index);
Addr valPtr = p->getSyscallArg(tc, index);
Addr lenPtr = p->getSyscallArg(tc, index);
auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
if (!sfdp)
return -EBADF;
int sim_fd = sfdp->getSimFD();
socklen_t len = sizeof(val);
int status = getsockopt(sim_fd, level, optname, &val, &len);
if (status == -1)
return -errno;
// copy val to valPtr and pass it on
BufferArg valBuf(valPtr, sizeof(val));
memcpy(valBuf.bufferPtr(), &val, sizeof(val));
valBuf.copyOut(tc->getMemProxy());
// copy len to lenPtr and pass it on
BufferArg lenBuf(lenPtr, sizeof(len));
memcpy(lenBuf.bufferPtr(), &len, sizeof(len));
lenBuf.copyOut(tc->getMemProxy());
return status;
}
SyscallReturn
getsocknameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
Addr addrPtr = p->getSyscallArg(tc, index);
Addr lenPtr = p->getSyscallArg(tc, index);
auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
if (!sfdp)
return -EBADF;
int sim_fd = sfdp->getSimFD();
// lenPtr is an in-out paramenter:
// sending the address length in, conveying the final length out
// Read in the value of len from the passed pointer.
BufferArg lenBuf(lenPtr, sizeof(socklen_t));
lenBuf.copyIn(tc->getMemProxy());
socklen_t len = *(socklen_t *)lenBuf.bufferPtr();
struct sockaddr sa;
int status = getsockname(sim_fd, &sa, &len);
if (status == -1)
return -errno;
// Copy address to addrPtr and pass it on.
BufferArg addrBuf(addrPtr, sizeof(sa));
memcpy(addrBuf.bufferPtr(), &sa, sizeof(sa));
addrBuf.copyOut(tc->getMemProxy());
// Copy len to lenPtr and pass it on.
*(socklen_t *)lenBuf.bufferPtr() = len;
lenBuf.copyOut(tc->getMemProxy());
return status;
}
SyscallReturn
getpeernameFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
Addr sockAddrPtr = p->getSyscallArg(tc, index);
Addr addrlenPtr = p->getSyscallArg(tc, index);
auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
if (!sfdp)
return -EBADF;
int sim_fd = sfdp->getSimFD();
BufferArg bufAddrlen(addrlenPtr, sizeof(unsigned));
bufAddrlen.copyIn(tc->getMemProxy());
BufferArg bufSock(sockAddrPtr, *(unsigned *)bufAddrlen.bufferPtr());
int retval = getpeername(sim_fd,
(struct sockaddr *)bufSock.bufferPtr(),
(unsigned *)bufAddrlen.bufferPtr());
if (retval != -1) {
bufSock.copyOut(tc->getMemProxy());
bufAddrlen.copyOut(tc->getMemProxy());
}
return (retval == -1) ? -errno : retval;
}
SyscallReturn
setsockoptFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
{
int index = 0;
int tgt_fd = p->getSyscallArg(tc, index);
int level = p->getSyscallArg(tc, index);
int optname = p->getSyscallArg(tc, index);
Addr valPtr = p->getSyscallArg(tc, index);
socklen_t len = p->getSyscallArg(tc, index);
BufferArg valBuf(valPtr, len);
valBuf.copyIn(tc->getMemProxy());
auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
if (!sfdp)
return -EBADF;
int sim_fd = sfdp->getSimFD();
int status = setsockopt(sim_fd, level, optname,
(struct sockaddr *)valBuf.bufferPtr(), len);
return (status == -1) ? -errno : status;
}
|