forked from westerndigitalcorporation/swerv-ISS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.cpp
925 lines (775 loc) · 21.7 KB
/
Server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
//
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2018 Western Digital Corporation or its affiliates.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.
//
#include <iostream>
#include <sstream>
#include <map>
#include <algorithm>
#include <boost/format.hpp>
#include <cstring>
#ifdef __MINGW64__
#include <winsock2.h>
#else
#include <arpa/inet.h>
#include <sys/socket.h>
#endif
#define __STDC_FORMAT_MACROS
#include <cinttypes>
#include "WhisperMessage.h"
#include "Server.hpp"
using namespace WdRiscv;
/// Return format string suitable for printing an integer of type URV
/// in hexadecimal form.
template <typename URV>
static
const char*
getHexForm()
{
if (sizeof(URV) == 4)
return "0x%08x";
if (sizeof(URV) == 8)
return "0x%016x";
if (sizeof(URV) == 16)
return "0x%032x";
return "0x%x";
}
/// Unpack socket message (received in server mode) into the given
/// WhisperMessage object.
static
void
deserializeMessage(const char buffer[], size_t bufferLen,
WhisperMessage& msg)
{
assert (bufferLen >= sizeof(msg));
const char* p = buffer;
uint32_t x = ntohl(*((uint32_t*)p));
msg.hart = x;
p += sizeof(x);
x = ntohl(*((uint32_t*)p));
msg.type = x;
p += sizeof(x);
x = ntohl(*((uint32_t*)p));
msg.resource = x;
p += sizeof(x);
x = ntohl(*((uint32_t*)p));
msg.flags = x;
p += sizeof(x);
uint32_t part = ntohl(*((uint32_t*)p));
msg.rank = uint64_t(part) << 32;
p += sizeof(part);
part = ntohl(*(uint32_t*)p);
msg.rank |= part;
p += sizeof(part);
part = ntohl(*((uint32_t*)p));
msg.address = uint64_t(part) << 32;
p += sizeof(part);
part = ntohl(*((uint32_t*)p));
msg.address |= part;
p += sizeof(part);
part = ntohl(*((uint32_t*)p));
msg.value = uint64_t(part) << 32;
p += sizeof(part);
part = ntohl(*((uint32_t*)p));
msg.value |= part;
p += sizeof(part);
memcpy(msg.buffer, p, sizeof(msg.buffer));
p += sizeof(msg.buffer);
memcpy(msg.tag, p, sizeof(msg.tag));
p += sizeof(msg.tag);
assert(size_t(p - buffer) <= bufferLen);
}
/// Serialize the given WhisperMessage into the given buffer in
/// preparation for socket send. Return the number of bytes written
/// into buffer.
static
size_t
serializeMessage(const WhisperMessage& msg, char buffer[],
size_t bufferLen)
{
assert (bufferLen >= sizeof(msg));
char* p = buffer;
uint32_t x = htonl(msg.hart);
memcpy(p, &x, sizeof(x));
p += sizeof(x);
x = htonl(msg.type);
memcpy(p, &x, sizeof(x));
p += sizeof(x);
x = htonl(msg.resource);
memcpy(p, &x, sizeof(x));
p += sizeof(x);
x = htonl(msg.flags);
memcpy(p, &x, sizeof(x));
p += sizeof(x);
uint32_t part = static_cast<uint32_t>(msg.rank >> 32);
x = htonl(part);
memcpy(p, &x, sizeof(x));
p += sizeof(x);
part = (msg.rank) & 0xffffffff;
x = htonl(part);
memcpy(p, &x, sizeof(x));
p += sizeof(x);
part = static_cast<uint32_t>(msg.address >> 32);
x = htonl(part);
memcpy(p, &x, sizeof(x));
p += sizeof(x);
part = (msg.address) & 0xffffffff;
x = htonl(part);
memcpy(p, &x, sizeof(x));
p += sizeof(x);
part = static_cast<uint32_t>(msg.value >> 32);
x = htonl(part);
memcpy(p, &x, sizeof(x));
p += sizeof(x);
part = msg.value & 0xffffffff;
x = htonl(part);
memcpy(p, &x, sizeof(x));
p += sizeof(x);
memcpy(p, msg.buffer, sizeof(msg.buffer));
p += sizeof(msg.buffer);
memcpy(p, msg.tag, sizeof(msg.tag));
p += sizeof(msg.tag);
size_t len = p - buffer;
assert(len <= bufferLen);
assert(len <= sizeof(msg));
for (size_t i = len; i < sizeof(msg); ++i)
buffer[i] = 0;
return sizeof(msg);
}
static bool
receiveMessage(int soc, WhisperMessage& msg)
{
char buffer[sizeof(msg)];
char* p = buffer;
size_t remain = sizeof(msg);
while (remain > 0)
{
ssize_t l = recv(soc, p, remain, 0);
if (l < 0)
{
if (errno == EINTR)
continue;
std::cerr << "Failed to receive socket message\n";
return false;
}
if (l == 0)
{
msg.type = Quit;
return true;
}
remain -= l;
p += l;
}
deserializeMessage(buffer, sizeof(buffer), msg);
return true;
}
static bool
sendMessage(int soc, WhisperMessage& msg)
{
char buffer[sizeof(msg)];
serializeMessage(msg, buffer, sizeof(buffer));
// Send command.
ssize_t remain = sizeof(msg);
char* p = buffer;
while (remain > 0)
{
ssize_t l = send(soc, p, remain , 0);
if (l < 0)
{
if (errno == EINTR)
continue;
std::cerr << "Failed to send socket command\n";
return false;
}
remain -= l;
p += l;
}
return true;
}
template <typename URV>
Server<URV>::Server(std::vector< Hart<URV>* >& harts)
: harts_(harts)
{
}
template <typename URV>
bool
Server<URV>::pokeCommand(const WhisperMessage& req, WhisperMessage& reply)
{
reply = req;
if (not checkHartId(req, reply))
return false;
uint32_t hartId = req.hart;
auto& hart = *(harts_.at(hartId));
switch (req.resource)
{
case 'r':
{
unsigned reg = static_cast<unsigned>(req.address);
URV val = static_cast<URV>(req.value);
if (reg == req.address)
if (hart.pokeIntReg(reg, val))
return true;
}
break;
case 'c':
{
URV val = static_cast<URV>(req.value);
if (hart.pokeCsr(CsrNumber(req.address), val))
return true;
}
break;
case 'm':
if (sizeof(URV) == 4)
{
// Poke a word in 32-bit harts.
if (hart.pokeMemory(req.address, uint32_t(req.value)))
return true;
}
else if (hart.pokeMemory(req.address, req.value))
return true;
break;
}
reply.type = Invalid;
return false;
}
template <typename URV>
bool
Server<URV>::peekCommand(const WhisperMessage& req, WhisperMessage& reply)
{
reply = req;
if (not checkHartId(req, reply))
return false;
uint32_t hartId = req.hart;
auto& hart = *(harts_.at(hartId));
URV value;
switch (req.resource)
{
case 'r':
{
unsigned reg = static_cast<unsigned>(req.address);
if (reg == req.address)
if (hart.peekIntReg(reg, value))
{
reply.value = value;
return true;
}
}
break;
case 'f':
{
unsigned reg = static_cast<unsigned>(req.address);
uint64_t fpVal = 0;
if (reg == req.address)
if (hart.peekFpReg(reg, fpVal))
{
reply.value = fpVal;
return true;
}
}
break;
case 'c':
if (hart.peekCsr(CsrNumber(req.address), value))
{
reply.value = value;
return true;
}
break;
case 'm':
if (hart.peekMemory(req.address, value))
{
reply.value = value;
return true;
}
break;
}
reply.type = Invalid;
return true;
}
template <typename URV>
void
Server<URV>::disassembleAnnotateInst(Hart<URV>& hart,
uint32_t inst, bool interrupted,
bool hasPreTrigger, bool hasPostTrigger,
std::string& text)
{
hart.disassembleInst(inst, text);
uint32_t op0 = 0, op1 = 0, op2 = 0, op3 = 0;
const InstEntry& entry = hart.decode(inst, op0, op1, op2, op3);
if (entry.isBranch())
{
if (hart.lastPc() + instructionSize(inst) != hart.peekPc())
text += " (T)";
else
text += " (NT)";
}
if (entry.isLoad() or entry.isStore() or entry.isAtomic())
{
URV addr = hart.lastLdStAddress();
std::ostringstream oss;
oss << " [0x" << std::hex << addr << "]" << std::dec;
text += oss.str();
}
if (interrupted)
text += " (interrupted)";
else if (hasPreTrigger)
text += " (pre-trigger)";
else if (hasPostTrigger)
text += " (post-trigger)";
}
template <typename URV>
void
Server<URV>::processStepCahnges(Hart<URV>& hart,
uint32_t inst,
std::vector<WhisperMessage>& pendingChanges,
bool interrupted, bool hasPre, bool hasPost,
WhisperMessage& reply)
{
// Get executed instruction.
URV pc = hart.lastPc();
// Add pc and instruction to reply.
reply.type = ChangeCount;
reply.address = pc;
reply.resource = inst;
// Add disassembly of instruction to reply.
std::string text;
disassembleAnnotateInst(hart, inst, interrupted, hasPre, hasPost, text);
strncpy(reply.buffer, text.c_str(), sizeof(reply.buffer) - 1);
reply.buffer[sizeof(reply.buffer) -1] = 0;
// Collect integer register change caused by execution of instruction.
pendingChanges.clear();
int regIx = hart.lastIntReg();
if (regIx > 0)
{
URV value = 0;
if (hart.peekIntReg(regIx, value))
{
WhisperMessage msg;
msg.type = Change;
msg.resource = 'r';
msg.address = regIx;
msg.value = value;
pendingChanges.push_back(msg);
}
}
// Collect floating point register change.
int fpRegIx = hart.lastFpReg();
if (fpRegIx >= 0)
{
uint64_t val = 0;
if (hart.peekFpReg(fpRegIx, val))
{
WhisperMessage msg;
msg.type = Change;
msg.resource = 'f';
msg.address = fpRegIx;
msg.value = val;
pendingChanges.push_back(msg);
}
}
// Collect CSR and trigger changes.
std::vector<CsrNumber> csrs;
std::vector<unsigned> triggers;
hart.lastCsr(csrs, triggers);
// Map to keep CSRs in order and to drop duplicate entries.
std::map<URV,URV> csrMap;
// Components of the triggers that changed (if any).
std::vector<bool> tdataChanged(3);
// Collect changed CSRs and their values. Collect components of
// changed trigger.
for (CsrNumber csr : csrs)
{
URV value;
if (hart.peekCsr(csr, value))
{
if (csr >= CsrNumber::TDATA1 and csr <= CsrNumber::TDATA3)
{
size_t ix = size_t(csr) - size_t(CsrNumber::TDATA1);
tdataChanged.at(ix) = true;
}
else
csrMap[URV(csr)] = value;
}
}
// Collect changes associated with trigger register.
for (unsigned trigger : triggers)
{
URV data1(0), data2(0), data3(0);
if (not hart.peekTrigger(trigger, data1, data2, data3))
continue;
if (tdataChanged.at(0))
{
URV addr = (trigger << 16) | unsigned(CsrNumber::TDATA1);
csrMap[addr] = data1;
}
if (tdataChanged.at(1))
{
URV addr = (trigger << 16) | unsigned(CsrNumber::TDATA2);
csrMap[addr] = data2;
}
if (tdataChanged.at(2))
{
URV addr = (trigger << 16) | unsigned(CsrNumber::TDATA3);
csrMap[addr] = data3;
}
}
for (const auto& [key, val] : csrMap)
{
WhisperMessage msg(0, Change, 'c', key, val);
pendingChanges.push_back(msg);
}
std::vector<size_t> addresses;
std::vector<uint32_t> words;
hart.lastMemory(addresses, words);
assert(addresses.size() == words.size());
for (size_t i = 0; i < addresses.size(); ++i)
{
WhisperMessage msg(0, Change, 'm', addresses.at(i), words.at(i));
pendingChanges.push_back(msg);
}
// Add count of changes to reply.
reply.value = pendingChanges.size();
// The changes will be retrieved one at a time from the back of the
// pendigChanges vector: Put the vector in reverse order. Changes
// are retrieved using a Change request (see interactUsingSocket).
std::reverse(pendingChanges.begin(), pendingChanges.end());
}
template <typename URV>
bool
Server<URV>::checkHartId(const WhisperMessage& req, WhisperMessage& reply)
{
uint32_t hartId = req.hart;
if (hartId >= harts_.size())
{
std::cerr << "Error: Hart ID (" << std::dec << hartId
<< ") out of bounds\n";
reply.type = Invalid;
return false;
}
return true;
}
template <typename URV>
bool
Server<URV>::checkHart(const WhisperMessage& req, const std::string& command,
WhisperMessage& reply)
{
if (not checkHartId(req, reply))
return false;
uint32_t hartId = req.hart;
auto& hart = *(harts_.at(hartId));
if (not hart.isStarted())
{
std::cerr << "Error: Command " << command
<< " received for a non-started hart\n";
reply.type = Invalid;
return false;
}
return true;
}
// Server mode step command.
template <typename URV>
bool
Server<URV>::stepCommand(const WhisperMessage& req,
std::vector<WhisperMessage>& pendingChanges,
WhisperMessage& reply,
FILE* traceFile)
{
reply = req;
// Hart id must be valid. Hart must be started.
if (not checkHart(req, "step", reply))
return false;
uint32_t hartId = req.hart;
auto& hart = *(harts_.at(hartId));
// Step is not allowed in debug mode unless we are in debug_step as
// well.
if (hart.inDebugMode() and not hart.inDebugStepMode())
{
std::cerr << "Error: Single step while in debug-halt mode\n";
reply.type = Invalid;
return false;
}
// Get instruction before execution (in case code is self-modifying).
uint32_t inst = 0;
hart.readInst(hart.peekPc(), inst);
// Execute instruction. Determine if an interrupt was taken or if a
// trigger got tripped.
uint64_t interruptCount = hart.getInterruptCount();
hart.singleStep(traceFile);
bool interrupted = hart.getInterruptCount() != interruptCount;
unsigned preCount = 0, postCount = 0;
hart.countTrippedTriggers(preCount, postCount);
bool hasPre = preCount > 0;
bool hasPost = postCount > 0;
processStepCahnges(hart, inst, pendingChanges, interrupted, hasPre,
hasPost, reply);
hart.clearTraceData();
return true;
}
// Server mode exception command.
template <typename URV>
bool
Server<URV>::exceptionCommand(const WhisperMessage& req,
WhisperMessage& reply,
std::string& text)
{
reply = req;
if (not checkHart(req, "exception", reply))
return false;
uint32_t hartId = req.hart;
auto& hart = *(harts_.at(hartId));
std::ostringstream oss;
bool ok = true;
URV addr = static_cast<URV>(req.address);
if (addr != req.address)
std::cerr << "Error: Address too large (" << std::hex << req.address
<< ") in exception command.\n" << std::dec;
WhisperExceptionType expType = WhisperExceptionType(req.value);
switch (expType)
{
case InstAccessFault:
hart.postInstAccessFault(addr);
oss << "exception inst " << addr;
break;
case DataAccessFault:
hart.postDataAccessFault(addr);
oss << "exception data " << addr;
break;
case ImpreciseStoreFault:
{
unsigned count = 0;
ok = hart.applyStoreException(addr, count);
reply.value = count;
}
oss << "exception store 0x" << std::hex << addr << std::dec;
break;
case ImpreciseLoadFault:
{
unsigned tag = reply.flags; // Tempoary.
unsigned count = 0;
ok = hart.applyLoadException(addr, tag, count);
reply.value = count;
oss << "exception load 0x" << std::hex << addr << std::dec << ' '
<< tag;
}
break;
case NonMaskableInterrupt:
if (hart.isNmiEnabled())
hart.setPendingNmi(NmiCause(addr));
oss << "exception nmi 0x" << std::hex << addr << std::dec;
break;
case DataMemoryError:
oss << "exception memory_data 0x" << std::hex << addr << std::dec;
ok = false;
break;
case InstMemoryError:
oss << "exception memory_inst 0x" << std::hex << addr << std::dec;
ok = false;
break;
default:
oss << "exception ? 0x" << std::hex << addr << std::dec;
ok = false;
break;
}
if (not ok)
reply.type = Invalid;
text = oss.str();
return ok;
}
// Server mode loop: Receive command and send reply till a quit
// command is received. Return true on successful termination (quit
// received). Return false otherwise.
template <typename URV>
bool
Server<URV>::interact(int soc, FILE* traceFile, FILE* commandLog)
{
std::vector<WhisperMessage> pendingChanges;
auto hexForm = getHexForm<URV>(); // Format string for printing a hex val
// Initial resets do not reset memory mapped registers.
bool resetMemoryMappedReg = false;
while (true)
{
WhisperMessage msg;
if (not receiveMessage(soc, msg))
return false;
WhisperMessage reply = msg;
if (checkHartId(msg, reply))
{
std::string timeStamp = std::to_string(msg.rank);
uint32_t hartId = msg.hart;
auto& hart = *(harts_.at(hartId));
if (msg.type == Step or msg.type == Until)
resetMemoryMappedReg = true;
switch (msg.type)
{
case Quit:
if (commandLog)
fprintf(commandLog, "hart=%d quit\n", hartId);
return true;
case Poke:
pokeCommand(msg, reply);
if (commandLog)
fprintf(commandLog, "hart=%d poke %c %s %s # ts=%s tag=%s\n", hartId,
msg.resource,
(boost::format(hexForm) % msg.address).str().c_str(),
(boost::format(hexForm) % msg.value).str().c_str(),
timeStamp.c_str(), msg.tag);
break;
case Peek:
peekCommand(msg, reply);
if (commandLog)
fprintf(commandLog, "hart=%d peek %c %s # ts=%s\n", hartId,
msg.resource,
(boost::format(hexForm) % msg.address).str().c_str(),
timeStamp.c_str());
break;
case Step:
stepCommand(msg, pendingChanges, reply, traceFile);
if (commandLog)
fprintf(commandLog, "hart=%d step #%" PRId64 " # ts=%s\n",
hartId, hart.getInstructionCount(), timeStamp.c_str());
break;
case ChangeCount:
reply.type = ChangeCount;
reply.value = pendingChanges.size();
reply.address = hart.lastPc();
{
uint32_t inst = 0;
hart.readInst(hart.lastPc(), inst);
reply.resource = inst;
std::string text;
hart.disassembleInst(inst, text);
uint32_t op0 = 0, op1 = 0, op2 = 0, op3 = 0;
const InstEntry& entry = hart.decode(inst, op0, op1, op2, op3);
if (entry.isBranch())
{
if (hart.lastPc() + instructionSize(inst) != hart.peekPc())
text += " (T)";
else
text += " (NT)";
}
strncpy(reply.buffer, text.c_str(), sizeof(reply.buffer) - 1);
reply.buffer[sizeof(reply.buffer) -1] = 0;
}
break;
case Change:
if (pendingChanges.empty())
reply.type = Invalid;
else
{
reply = pendingChanges.back();
pendingChanges.pop_back();
}
break;
case Reset:
{
URV addr = static_cast<URV>(msg.address);
if (addr != msg.address)
std::cerr << "Error: Address too large (" << std::hex
<< msg.address << ") in reset command.\n" << std::dec;
pendingChanges.clear();
if (msg.value != 0)
hart.defineResetPc(addr);
hart.reset(resetMemoryMappedReg);
if (commandLog)
{
if (msg.value != 0)
fprintf(commandLog, "hart=%d reset %s # ts=%s\n", hartId,
(boost::format(hexForm) % addr).str().c_str(),
timeStamp.c_str());
else
fprintf(commandLog, "hart=%d reset # ts=%s\n", hartId,
timeStamp.c_str());
}
}
break;
case Exception:
{
std::string text;
exceptionCommand(msg, reply, text);
if (commandLog)
fprintf(commandLog, "hart=%d %s # ts=%s\n", hartId,
text.c_str(), timeStamp.c_str());
}
break;
case EnterDebug:
if (checkHart(msg, "enter_debug", reply))
{
hart.enterDebugMode(hart.peekPc());
if (commandLog)
fprintf(commandLog, "hart=%d enter_debug # ts=%s\n", hartId,
timeStamp.c_str());
}
break;
case ExitDebug:
if (checkHart(msg, "exit_debug", reply))
{
hart.exitDebugMode();
if (commandLog)
fprintf(commandLog, "hart=%d exit_debug # ts=%s\n", hartId,
timeStamp.c_str());
}
break;
case LoadFinished:
if (checkHart(msg, "load_finished", reply))
{
URV addr = static_cast<URV>(msg.address);
if (addr != msg.address)
std::cerr << "Error: Address too large (" << std::hex
<< msg.address << ") in load finished command.\n"
<< std::dec;
unsigned tag = msg.flags;
unsigned matchCount = 0;
hart.applyLoadFinished(addr, tag, matchCount);
reply.value = matchCount;
if (commandLog)
{
fprintf(commandLog, "hart=%d load_finished 0x%0*" PRIx64 " %d # ts=%s\n",
hartId,
( (sizeof(URV) == 4) ? 8 : 16 ), uint64_t(addr),
tag, timeStamp.c_str());
}
}
break;
case CancelDiv:
if (checkHart(msg, "cancel_div", reply))
{
if (not hart.cancelLastDiv())
reply.type = Invalid;
if (commandLog)
fprintf(commandLog, "hart=%d cancel_div # ts=%s\n", hartId,
timeStamp.c_str());
}
break;
case CancelLr:
if (checkHart(msg, "cancel_lr", reply))
{
hart.cancelLr();
if (commandLog)
fprintf(commandLog, "hart=%d cancel_lr # ts=%s\n", hartId,
timeStamp.c_str());
}
break;
default:
std::cerr << "Unknown command\n";
reply.type = Invalid;
}
}
if (not sendMessage(soc, reply))
return false;
}
return false;
}
template class WdRiscv::Server<uint32_t>;
template class WdRiscv::Server<uint64_t>;