-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathFDP.cpp
1409 lines (1321 loc) · 46.5 KB
/
FDP.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
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
/*
MIT License
Copyright (c) 2015 Nicolas Couffin [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#define FDP_INTERNAL_ONLY
#include "include/FDP.h"
#include "include/FDP_structs.h"
#ifdef _MSC_VER
# define NOMINMAX
# include <intrin.h>
# include <windows.h>
# define FORCE_INLINE __forceinline
# define PAUSE _mm_pause()
#else
# include <fcntl.h>
# include <sys/mman.h>
# include <sys/shm.h>
# include <sys/stat.h>
# include <unistd.h>
# define FORCE_INLINE inline __attribute__((__always_inline__))
# define PAUSE asm("pause")
#endif
#include <cstring>
#include <random>
#include <thread>
namespace
{
template <int WANT, int GOT>
struct expect_eq
{
static_assert(WANT == GOT, "size mismatch");
static constexpr bool ok = true;
};
#define STATIC_ASSERT_EQ(A, B) static_assert(!!expect_eq<A, B>::ok, "");
STATIC_ASSERT_EQ(sizeof(FDP_SHM_CANAL), FDP_MAX_DATA_SIZE + 8);
STATIC_ASSERT_EQ(sizeof(FDP_SHM_SHARED), 2 * sizeof(FDP_SHM_CANAL) + 4);
constexpr size_t max_wait_iters = 0x100000;
constexpr size_t min_backoff_iters = 0x20;
FORCE_INLINE void yield_sleep()
{
std::this_thread::sleep_for(std::chrono::microseconds(500));
}
constexpr size_t max_backoff_iters = 1024;
FORCE_INLINE void backoff(size_t& delay)
{
thread_local auto dist = std::uniform_int_distribution<size_t>{};
thread_local auto gen = std::minstd_rand(std::random_device{}());
const auto spin_iters = dist(gen, decltype(dist)::param_type{0, delay});
delay = std::min(delay * 2, max_backoff_iters);
for(size_t i = 0; i < spin_iters; ++i)
PAUSE;
}
FORCE_INLINE void ttas_spinlock_lock(std::atomic_bool* flag)
{
size_t current_max_delay = min_backoff_iters;
// exponential back-off spinlock
while(true)
{
const auto locked = flag->exchange(true, std::memory_order_acquire);
if(!locked)
return;
backoff(current_max_delay);
}
}
FORCE_INLINE void ttas_spinlock_unlock(std::atomic_bool* flag)
{
flag->store(false, std::memory_order_release);
}
FORCE_INLINE void LockSHM(FDP_SHM_SHARED* FDPShm)
{
ttas_spinlock_lock(&FDPShm->lock);
}
FORCE_INLINE void UnlockSHM(FDP_SHM_SHARED* FDPShm)
{
ttas_spinlock_unlock(&FDPShm->lock);
}
}
static bool WriteFDPDataWithStatus(FDP_SHM_CANAL* pFDPCanal, uint8_t* pData, uint32_t DataSize, bool bStatus)
{
bool dataWritten = false;
if(DataSize > FDP_MAX_DATA_SIZE)
{
return false;
}
do
{
ttas_spinlock_lock(&pFDPCanal->lock);
if(!pFDPCanal->bDataPresent)
{
memcpy((char*) pFDPCanal->data, pData, DataSize);
pFDPCanal->bDataPresent = true;
pFDPCanal->dataSize = DataSize;
pFDPCanal->bStatus = bStatus;
dataWritten = true;
}
ttas_spinlock_unlock(&pFDPCanal->lock);
} while(!dataWritten);
return true;
}
static bool WriteFDPData(FDP_SHM_CANAL* pFDPCanal, uint8_t* pData, uint32_t DataSize)
{
return WriteFDPDataWithStatus(pFDPCanal, pData, DataSize, true);
}
namespace
{
FORCE_INLINE void wait_until_bool_is(std::atomic_bool* lock, bool value)
{
size_t num_iters = 0;
while(lock->load(std::memory_order_relaxed) != value)
{
if(num_iters < max_wait_iters)
{
++num_iters;
PAUSE;
}
else
{
yield_sleep();
}
}
}
}
static uint32_t ReadFDPDataWithStatus(FDP_SHM_CANAL* pFDPCanal, uint8_t* buffer, bool* pbStatus)
{
uint32_t dataReadSize = 0;
do
{
wait_until_bool_is(&pFDPCanal->bDataPresent, true);
ttas_spinlock_lock(&pFDPCanal->lock);
if(pFDPCanal->bDataPresent)
{
if(pFDPCanal->dataSize < FDP_MAX_DATA_SIZE)
memcpy(buffer, (char*) pFDPCanal->data, pFDPCanal->dataSize);
dataReadSize = pFDPCanal->dataSize;
*pbStatus = pFDPCanal->bStatus;
pFDPCanal->bDataPresent = false;
}
ttas_spinlock_unlock(&pFDPCanal->lock);
} while(!dataReadSize);
return dataReadSize;
}
FORCE_INLINE static uint32_t ReadFDPData(FDP_SHM_CANAL* pFDPCanal, uint8_t* buffer)
{
bool bIsSuccess;
return ReadFDPDataWithStatus(pFDPCanal, buffer, &bIsSuccess);
}
FDP_EXPORTED
FDP_SHM* FDP_CreateSHM(const char* shmName)
{
void* pBuf;
#ifdef _MSC_VER
HANDLE hMapFile;
hMapFile = CreateFileMappingA(INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
FDP_SHM_SHARED_SIZE,
shmName);
if(hMapFile == NULL)
{
return NULL;
}
pBuf = MapViewOfFile(hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
FDP_SHM_SHARED_SIZE);
if(pBuf == NULL)
{
CloseHandle(hMapFile);
return NULL;
}
#else
/* create the shared memory segment */
auto fdSHM = shm_open(shmName, O_CREAT | O_RDWR, 0666);
if(fdSHM == -1)
{
return NULL;
}
/* configure the size of the shared memory segment */
auto err = ftruncate(fdSHM, FDP_SHM_SHARED_SIZE);
if(err == -1)
{
shm_unlink(shmName);
return NULL;
}
/* now map the shared memory segment in the address space of the process */
pBuf = mmap(0, FDP_SHM_SHARED_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fdSHM, 0);
close(fdSHM);
if(pBuf == NULL)
{
shm_unlink(shmName);
return NULL;
}
#endif
// Clear SHM
memset(pBuf, 0, FDP_SHM_SHARED_SIZE);
FDP_SHM* pFDPSHM = (FDP_SHM*) malloc(sizeof *pFDPSHM);
// TODO: check !
pFDPSHM->pSharedFDPSHM = (FDP_SHM_SHARED*) pBuf;
return pFDPSHM;
}
void* OpenShm(const char* pShmName, size_t szShmSize)
{
void* pBuf;
#ifdef _MSC_VER
HANDLE hMapFile;
hMapFile = OpenFileMappingA(FILE_MAP_ALL_ACCESS,
FALSE,
pShmName);
if(hMapFile == NULL)
{
// printf("Failed to OpenFIle... %d\n", GetLastError());
return NULL;
}
pBuf = (LPSTR) MapViewOfFile(hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
szShmSize);
if(pBuf == NULL)
{
// printf("Failed to MapFileOfFile... %d\n", GetLastError());
CloseHandle(hMapFile);
return NULL;
}
#else
/* open the shared memory segment */
auto fdSHM = shm_open(pShmName, O_RDWR, 0666);
if(fdSHM == -1)
{
return NULL;
}
/* now map the shared memory segment in the address space of the process */
pBuf = mmap(0, szShmSize, PROT_READ | PROT_WRITE, MAP_SHARED, fdSHM, 0);
close(fdSHM);
if(pBuf == MAP_FAILED)
{
shm_unlink(pShmName);
return NULL;
}
#endif
return pBuf;
}
FDP_EXPORTED FDP_SHM* FDP_OpenSHM(const char* pShmName)
{
void* pSharedFDPSHM = OpenShm(pShmName, FDP_SHM_SHARED_SIZE);
if(pSharedFDPSHM == NULL)
{
return NULL;
}
// TODO : !
char aCpuShmName[512];
strncpy(aCpuShmName, "CPU_", sizeof aCpuShmName - 1);
strncat(aCpuShmName, pShmName, sizeof aCpuShmName - strlen(aCpuShmName) - 1);
void* pCpuShm = OpenShm(aCpuShmName, sizeof(FDP_CPU_CTX));
if(pCpuShm == NULL)
{
return NULL;
}
FDP_SHM* pFDPSHM = (FDP_SHM*) malloc(sizeof *pFDPSHM);
if(pFDPSHM == NULL)
{
// TODO : CloseShm
return NULL;
}
pFDPSHM->pSharedFDPSHM = (FDP_SHM_SHARED*) pSharedFDPSHM;
pFDPSHM->pCpuShm = (FDP_CPU_CTX*) pCpuShm;
return pFDPSHM;
}
FDP_EXPORTED void FDP_ExitSHM(FDP_SHM* pShm)
{
free(pShm);
}
static void RunCmd(FDP_SHM* pFDP, void* pDst, const void* pSrc, size_t szSize)
{
LockSHM(pFDP->pSharedFDPSHM);
{
WriteFDPData(&pFDP->pSharedFDPSHM->ClientToServer, (uint8_t*) pSrc, (uint32_t) szSize);
ReadFDPData(&pFDP->pSharedFDPSHM->ServerToClient, (uint8_t*) pDst); // TODO: return success/fail !
}
UnlockSHM(pFDP->pSharedFDPSHM);
}
static bool CheckRunCmd(FDP_SHM* pFDP, const void* pSrc, size_t szSize)
{
bool bReturnValue = false;
RunCmd(pFDP, &bReturnValue, pSrc, szSize);
return bReturnValue;
}
static bool RunCmdBuffer(FDP_SHM* pFDP, void* pDst, const void* pSrc, size_t szSize)
{
bool bReturnValue = false;
LockSHM(pFDP->pSharedFDPSHM);
{
WriteFDPData(&pFDP->pSharedFDPSHM->ClientToServer, (uint8_t*) pSrc, (uint32_t) szSize);
ReadFDPDataWithStatus(&pFDP->pSharedFDPSHM->ServerToClient, (uint8_t*) pDst, &bReturnValue);
}
UnlockSHM(pFDP->pSharedFDPSHM);
return bReturnValue;
}
FDP_EXPORTED
bool FDP_Pause(FDP_SHM* pFDP)
{
if(pFDP == NULL)
{
return false;
}
FDP_SIMPLE_PKT_REQ TempPkt;
TempPkt.Type = FDPCMD_PAUSE_VM;
return CheckRunCmd(pFDP, &TempPkt, sizeof TempPkt);
}
FDP_EXPORTED
bool FDP_Resume(FDP_SHM* pFDP)
{
if(pFDP == NULL)
{
return false;
}
FDP_SIMPLE_PKT_REQ TempPkt;
TempPkt.Type = FDPCMD_RESUME_VM;
return CheckRunCmd(pFDP, &TempPkt, sizeof TempPkt);
}
FDP_EXPORTED
bool FDP_Reboot(FDP_SHM* pFDP)
{
if(pFDP == NULL)
{
return false;
}
FDP_SIMPLE_PKT_REQ TempPkt;
TempPkt.Type = FDPCMD_REBOOT;
return CheckRunCmd(pFDP, &TempPkt, sizeof TempPkt);
}
bool FDP_ReadPhysicalMemoryInternal(FDP_SHM* pFDP, uint8_t* pDstBuffer, uint32_t ReadSize, uint64_t PhysicalAddress)
{
FDP_READ_PHYSICAL_MEMORY_PKT_REQ tmpPkt;
tmpPkt.Type = FDPCMD_READ_PHYSICAL;
tmpPkt.PhysicalAddress = PhysicalAddress;
tmpPkt.ReadSize = ReadSize;
return RunCmdBuffer(pFDP, pDstBuffer, &tmpPkt, sizeof tmpPkt);
}
FDP_EXPORTED
bool FDP_ReadPhysicalMemory(FDP_SHM* pFDP, uint8_t* pDstBuffer, uint32_t ReadSize, uint64_t PhysicalAddress)
{
if(pFDP == NULL)
{
return false;
}
uint32_t CurrentOffset = 0;
do
{
uint32_t CurrentReadSize = std::min<uint32_t>(ReadSize, FDP_MAX_DATA_SIZE - 1);
if(FDP_ReadPhysicalMemoryInternal(pFDP, pDstBuffer + CurrentOffset, CurrentReadSize,
PhysicalAddress + CurrentOffset)
== false)
{
return false;
}
CurrentOffset += CurrentReadSize;
} while(CurrentOffset < ReadSize);
return true;
}
bool FDP_ReadVirtualMemoryInternal(FDP_SHM* pFDP, uint32_t CpuId, uint8_t* pDstBuffer, uint32_t ReadSize,
uint64_t VirtualAddress)
{
FDP_READ_VIRTUAL_MEMORY_PKT_REQ tmpPkt;
tmpPkt.Type = FDPCMD_READ_VIRTUAL;
tmpPkt.CpuId = CpuId;
tmpPkt.VirtualAddress = VirtualAddress;
tmpPkt.ReadSize = ReadSize;
return RunCmdBuffer(pFDP, pDstBuffer, &tmpPkt, sizeof tmpPkt);
}
FDP_EXPORTED
bool FDP_ReadVirtualMemory(FDP_SHM* pFDP, uint32_t CpuId, uint8_t* pDstBuffer, uint32_t ReadSize,
uint64_t VirtualAddress)
{
if(pFDP == NULL || ReadSize <= 0)
{
return false;
}
uint32_t CurrentOffset = 0;
do
{
uint32_t CurrentReadSize = std::min<uint32_t>(ReadSize, FDP_MAX_DATA_SIZE - 1);
if(FDP_ReadVirtualMemoryInternal(pFDP, CpuId, pDstBuffer + CurrentOffset, CurrentReadSize,
VirtualAddress + CurrentOffset)
== false)
{
return false;
}
CurrentOffset += CurrentReadSize;
} while(CurrentOffset < ReadSize);
return true;
}
FDP_EXPORTED
bool FDP_WritePhysicalMemory(FDP_SHM* pFDP, uint8_t* pSrcBuffer, uint32_t WriteSize, uint64_t PhysicalAddress)
{
if(pFDP == NULL)
{
return false;
}
bool bReturnValue = false;
LockSHM(pFDP->pSharedFDPSHM);
{
FDP_WRITE_PHYSICAL_MEMORY_PKT_REQ* TempPkt = (FDP_WRITE_PHYSICAL_MEMORY_PKT_REQ*) pFDP->OutputBuffer;
TempPkt->Type = FDPCMD_WRITE_PHYSICAL;
TempPkt->PhysicalAddress = PhysicalAddress;
TempPkt->WriteSize = WriteSize;
if(WriteSize < FDP_MAX_DATA_SIZE - sizeof *TempPkt)
{
memcpy(TempPkt->Data, pSrcBuffer, WriteSize);
WriteFDPData(&pFDP->pSharedFDPSHM->ClientToServer, (uint8_t*) TempPkt, sizeof *TempPkt + WriteSize);
ReadFDPData(&pFDP->pSharedFDPSHM->ServerToClient, (uint8_t*) &bReturnValue);
}
}
UnlockSHM(pFDP->pSharedFDPSHM);
return bReturnValue;
}
FDP_EXPORTED
bool FDP_WriteVirtualMemory(FDP_SHM* pFDP, uint32_t CpuId, uint8_t* pSrcBuffer, uint32_t WriteSize,
uint64_t VirtualAddress)
{
if(pFDP == NULL)
{
return false;
}
bool bReturnValue = false;
LockSHM(pFDP->pSharedFDPSHM);
{
FDP_WRITE_VIRTUAL_MEMORY_PKT_REQ* TempPkt = (FDP_WRITE_VIRTUAL_MEMORY_PKT_REQ*) pFDP->OutputBuffer;
TempPkt->Type = FDPCMD_WRITE_VIRTUAL;
TempPkt->CpuId = CpuId;
TempPkt->VirtualAddress = VirtualAddress;
TempPkt->WriteSize = WriteSize;
if(WriteSize < FDP_MAX_DATA_SIZE - sizeof *TempPkt)
{
memcpy(TempPkt->Data, pSrcBuffer, WriteSize);
WriteFDPData(&pFDP->pSharedFDPSHM->ClientToServer, pFDP->OutputBuffer, sizeof *TempPkt + WriteSize);
ReadFDPData(&pFDP->pSharedFDPSHM->ServerToClient, (uint8_t*) &bReturnValue);
}
}
UnlockSHM(pFDP->pSharedFDPSHM);
return bReturnValue;
}
FDP_EXPORTED
uint64_t FDP_SearchPhysicalMemory(FDP_SHM* pFDP, const void* pPatternData, uint32_t PatternSize, uint64_t StartOffset)
{
if(pFDP == NULL)
{
return false;
}
uint64_t FoundAddress = 0x0;
LockSHM(pFDP->pSharedFDPSHM);
{
FDP_SEARCH_PHYSICAL_MEMORY_PKT_REQ* TempPkt = (FDP_SEARCH_PHYSICAL_MEMORY_PKT_REQ*) pFDP->OutputBuffer;
TempPkt->Type = FDPCMD_SEARCH_PHYSICAL_MEMORY;
TempPkt->PatternSize = PatternSize;
TempPkt->StartOffset = StartOffset;
memcpy(TempPkt->PatternData, pPatternData, PatternSize);
WriteFDPData(&pFDP->pSharedFDPSHM->ClientToServer, pFDP->OutputBuffer, sizeof *TempPkt + PatternSize);
ReadFDPData(&pFDP->pSharedFDPSHM->ServerToClient, (uint8_t*) &FoundAddress); // TODO: return success/fail !
}
UnlockSHM(pFDP->pSharedFDPSHM);
return FoundAddress;
}
// TODO !
FDP_EXPORTED
bool FDP_SearchVirtualMemory(FDP_SHM* pFDP, uint32_t CpuId, const void* pPatternData, uint32_t PatternSize,
uint64_t StartOffset)
{
if(pFDP == NULL)
{
return false;
}
uint64_t FoundAddress = 0x0;
bool bReturnCode = false;
LockSHM(pFDP->pSharedFDPSHM);
{
FDP_SEARCH_VIRTUAL_MEMORY_PKT_REQ* TempPkt = (FDP_SEARCH_VIRTUAL_MEMORY_PKT_REQ*) pFDP->OutputBuffer;
TempPkt->Type = FDPCMD_SEARCH_VIRTUAL_MEMORY;
TempPkt->CpuId = CpuId;
TempPkt->PatternSize = PatternSize;
TempPkt->StartOffset = StartOffset;
memcpy(TempPkt->PatternData, pPatternData, PatternSize);
WriteFDPData(&pFDP->pSharedFDPSHM->ClientToServer, pFDP->OutputBuffer, sizeof *TempPkt + PatternSize);
ReadFDPData(&pFDP->pSharedFDPSHM->ServerToClient, (uint8_t*) &FoundAddress); // TODO: return success/fail !
bReturnCode = true;
}
UnlockSHM(pFDP->pSharedFDPSHM);
return bReturnCode;
}
FDP_EXPORTED
bool FDP_ReadRegister(FDP_SHM* pFDP, uint32_t CpuId, FDP_Register RegisterId, uint64_t* pRegisterValue)
{
if(pFDP == NULL)
{
return false;
}
// Fast way...
switch(RegisterId)
{
case FDP_RIP_REGISTER:
*pRegisterValue = pFDP->pCpuShm->rip;
return true;
case FDP_RAX_REGISTER:
*pRegisterValue = pFDP->pCpuShm->rax;
return true;
case FDP_RCX_REGISTER:
*pRegisterValue = pFDP->pCpuShm->rcx;
return true;
case FDP_RDX_REGISTER:
*pRegisterValue = pFDP->pCpuShm->rdx;
return true;
case FDP_RBX_REGISTER:
*pRegisterValue = pFDP->pCpuShm->rbx;
return true;
case FDP_RSP_REGISTER:
*pRegisterValue = pFDP->pCpuShm->rsp;
return true;
case FDP_RBP_REGISTER:
*pRegisterValue = pFDP->pCpuShm->rbp;
return true;
case FDP_RSI_REGISTER:
*pRegisterValue = pFDP->pCpuShm->rsi;
return true;
case FDP_RDI_REGISTER:
*pRegisterValue = pFDP->pCpuShm->rdi;
return true;
case FDP_R8_REGISTER:
*pRegisterValue = pFDP->pCpuShm->r8;
return true;
case FDP_R9_REGISTER:
*pRegisterValue = pFDP->pCpuShm->r9;
return true;
case FDP_R10_REGISTER:
*pRegisterValue = pFDP->pCpuShm->r10;
return true;
case FDP_R11_REGISTER:
*pRegisterValue = pFDP->pCpuShm->r11;
return true;
case FDP_R12_REGISTER:
*pRegisterValue = pFDP->pCpuShm->r12;
return true;
case FDP_R13_REGISTER:
*pRegisterValue = pFDP->pCpuShm->r13;
return true;
case FDP_R14_REGISTER:
*pRegisterValue = pFDP->pCpuShm->r14;
return true;
case FDP_R15_REGISTER:
*pRegisterValue = pFDP->pCpuShm->r15;
return true;
case FDP_CR0_REGISTER:
*pRegisterValue = pFDP->pCpuShm->cr0;
return true;
case FDP_CR2_REGISTER:
*pRegisterValue = pFDP->pCpuShm->cr2;
return true;
case FDP_CR3_REGISTER:
*pRegisterValue = pFDP->pCpuShm->cr3;
return true;
case FDP_CR4_REGISTER:
*pRegisterValue = pFDP->pCpuShm->cr4;
return true;
default:
break;
}
// Old version => low performance
FDP_READ_REGISTER_PKT_REQ TempPkt;
TempPkt.Type = FDPCMD_READ_REGISTER;
TempPkt.CpuId = CpuId;
TempPkt.RegisterId = RegisterId;
RunCmd(pFDP, pRegisterValue, &TempPkt, sizeof TempPkt);
return true;
}
FDP_EXPORTED
bool FDP_ReadMsr(FDP_SHM* pFDP, uint32_t CpuId, uint64_t MsrId, uint64_t* pMsrValue)
{
if(pFDP == NULL)
{
return false;
}
FDP_READ_MSR_PKT_REQ TempPkt;
TempPkt.Type = FDPCMD_READ_MSR;
TempPkt.CpuId = CpuId;
TempPkt.MsrId = MsrId;
RunCmd(pFDP, pMsrValue, &TempPkt, sizeof TempPkt);
return true;
}
FDP_EXPORTED
bool FDP_WriteMsr(FDP_SHM* pFDP, uint32_t CpuId, uint64_t MsrId, uint64_t MsrValue)
{
if(pFDP == NULL)
{
return false;
}
FDP_WRITE_MSR_PKT_REQ TempPkt;
TempPkt.Type = FDPCMD_WRITE_MSR;
TempPkt.CpuId = CpuId;
TempPkt.MsrId = MsrId;
TempPkt.MsrValue = MsrValue;
return CheckRunCmd(pFDP, &TempPkt, sizeof TempPkt);
}
FDP_EXPORTED
bool FDP_WriteRegister(FDP_SHM* pFDP, uint32_t CpuId, FDP_Register RegisterId, uint64_t RegisterValue)
{
if(pFDP == NULL)
{
return false;
}
FDP_WRITE_REGISTER_PKT_REQ TempPkt;
TempPkt.Type = FDPCMD_WRITE_REGISTER;
TempPkt.CpuId = CpuId;
TempPkt.RegisterId = RegisterId;
TempPkt.RegisterValue = RegisterValue;
return CheckRunCmd(pFDP, &TempPkt, sizeof TempPkt);
}
FDP_EXPORTED
bool FDP_UnsetBreakpoint(FDP_SHM* pFDP, int BreakpointId)
{
if(pFDP == NULL)
{
return false;
}
FDP_CLEAR_BREAKPOINT_PKT_REQ TempPkt;
TempPkt.Type = FDPCMD_UNSET_BP;
TempPkt.BreakpointId = BreakpointId;
return CheckRunCmd(pFDP, &TempPkt, sizeof TempPkt);
}
FDP_EXPORTED
int FDP_SetBreakpoint(
FDP_SHM* pFDP,
uint32_t CpuId,
FDP_BreakpointType BreakpointType,
int BreakpointId,
FDP_Access BreakpointAccessType,
FDP_AddressType BreakpointAddressType,
uint64_t BreakpointAddress,
uint64_t BreakpointLength,
uint64_t BreakpointCr3)
{
if(pFDP == NULL)
{
return false;
}
int iReturnedBreakpointId;
FDP_SET_BREAKPOINT_PKT_REQ TempPkt;
TempPkt.Type = FDPCMD_SET_BP;
TempPkt.CpuId = CpuId;
TempPkt.BreakpointType = BreakpointType;
TempPkt.BreakpointId = BreakpointId;
TempPkt.BreakpointAccessType = BreakpointAccessType;
TempPkt.BreakpointAddressType = BreakpointAddressType;
TempPkt.BreakpointAddress = BreakpointAddress;
TempPkt.BreakpointLength = BreakpointLength;
TempPkt.BreakpointCr3 = BreakpointCr3;
RunCmd(pFDP, &iReturnedBreakpointId, &TempPkt, sizeof TempPkt);
return iReturnedBreakpointId;
}
FDP_EXPORTED
bool FDP_VirtualToPhysical(FDP_SHM* pFDP, uint32_t CpuId, uint64_t VirtualAddress, uint64_t* PhysicalAddress)
{
if(pFDP == NULL)
{
return false;
}
FDP_VIRTUAL_PHYSICAL_PKT_REQ TempPkt;
TempPkt.Type = FDPCMD_VIRTUAL_PHYSICAL;
TempPkt.CpuId = CpuId;
TempPkt.VirtualAddress = VirtualAddress;
RunCmd(pFDP, PhysicalAddress, &TempPkt, sizeof TempPkt);
return true;
}
FDP_EXPORTED
bool FDP_GetState(FDP_SHM* pFDP, FDP_State* DebuggeeState)
{
if(pFDP == NULL)
{
return false;
}
FDP_GET_STATE_PKT_REQ TempPkt;
TempPkt.Type = FDPCMD_GET_STATE;
RunCmd(pFDP, DebuggeeState, &TempPkt, sizeof TempPkt);
return true;
}
FDP_EXPORTED
bool FDP_WaitForStateChanged(FDP_SHM* pFDP, FDP_State* DebuggeeState)
{
if(pFDP == NULL)
{
return false;
}
while(true)
{
std::this_thread::yield();
if(FDP_GetStateChanged(pFDP) == true)
{
return FDP_GetState(pFDP, DebuggeeState);
}
}
return true;
}
FDP_EXPORTED
bool FDP_Init(FDP_SHM* pFDP)
{
if(pFDP == NULL)
{
return false;
}
bool bReturnValue = true;
memset(pFDP->pSharedFDPSHM, 0, FDP_SHM_SHARED_SIZE);
return bReturnValue;
}
FDP_EXPORTED
bool FDP_SingleStep(FDP_SHM* pFDP, uint32_t CpuId)
{
if(pFDP == NULL)
{
return false;
}
FDP_SINGLE_STEP_PKT_REQ TempPkt;
TempPkt.Type = FDPCMD_SINGLE_STEP;
TempPkt.CpuId = CpuId;
return CheckRunCmd(pFDP, &TempPkt, sizeof TempPkt);
}
uint8_t FDP_Test(FDP_SHM* pFDP)
{
if(pFDP == NULL)
{
return false;
}
uint8_t DebuggeState;
LockSHM(pFDP->pSharedFDPSHM);
{
FDP_GET_STATE_PKT_REQ* tmpPkt = (FDP_GET_STATE_PKT_REQ*) pFDP->OutputBuffer;
tmpPkt->Type = FDPCMD_TEST;
WriteFDPData(&pFDP->pSharedFDPSHM->ClientToServer, pFDP->OutputBuffer, sizeof *tmpPkt);
ReadFDPData(&pFDP->pSharedFDPSHM->ServerToClient, (uint8_t*) &DebuggeState); // TODO: return success/fail !
}
UnlockSHM(pFDP->pSharedFDPSHM);
return DebuggeState;
}
FDP_EXPORTED
bool FDP_GetFxState64(FDP_SHM* pFDP, uint32_t CpuId, FDP_XSAVE_FORMAT64_T* pFxState)
{
if(pFDP == NULL)
{
return false;
}
LockSHM(pFDP->pSharedFDPSHM);
{
FDP_GET_STATE_PKT_REQ* TempPkt = (FDP_GET_STATE_PKT_REQ*) pFDP->OutputBuffer;
TempPkt->Type = FDPCMD_GET_FXSTATE;
TempPkt->CpuId = CpuId;
WriteFDPData(&pFDP->pSharedFDPSHM->ClientToServer, pFDP->OutputBuffer, sizeof *TempPkt);
ReadFDPData(&pFDP->pSharedFDPSHM->ServerToClient, (uint8_t*) pFxState); // TODO: return success/fail !
}
UnlockSHM(pFDP->pSharedFDPSHM);
return true;
}
FDP_EXPORTED
bool FDP_SetFxState64(FDP_SHM* pFDP, uint32_t CpuId, FDP_XSAVE_FORMAT64_T* pFxState64)
{
if(pFDP == NULL)
{
return false;
}
bool bReturnValue = false;
LockSHM(pFDP->pSharedFDPSHM);
{
FDP_SET_FX_STATE_REQ* TempPkt = (FDP_SET_FX_STATE_REQ*) pFDP->OutputBuffer;
TempPkt->Type = FDPCMD_SET_FXSTATE;
TempPkt->CpuId = CpuId;
memcpy(&TempPkt->FxState64, pFxState64, sizeof *pFxState64);
WriteFDPData(&pFDP->pSharedFDPSHM->ClientToServer, pFDP->OutputBuffer, sizeof *TempPkt);
ReadFDPData(&pFDP->pSharedFDPSHM->ServerToClient, (uint8_t*) &bReturnValue); // TODO: return success/fail !
}
UnlockSHM(pFDP->pSharedFDPSHM);
return bReturnValue;
}
FDP_EXPORTED
bool FDP_GetPhysicalMemorySize(FDP_SHM* pFDP, uint64_t* PhysicalMemorySize)
{
if(pFDP == NULL)
{
return false;
}
bool bReturnValue = true;
FDP_SIMPLE_PKT_REQ TempPkt;
TempPkt.Type = FDPCMD_GET_MEMORYSIZE;
RunCmd(pFDP, PhysicalMemorySize, &TempPkt, sizeof TempPkt);
// TODO return bool !
return bReturnValue;
}
FDP_EXPORTED
bool FDP_GetCpuCount(FDP_SHM* pFDP, uint32_t* CPUCount)
{
if(pFDP == NULL)
{
return false;
}
bool bReturnValue = true;
FDP_SIMPLE_PKT_REQ TempPkt;
TempPkt.Type = FDPCMD_GET_CPU_COUNT;
RunCmd(pFDP, CPUCount, &TempPkt, sizeof TempPkt);
return bReturnValue;
}
FDP_EXPORTED
bool FDP_GetCpuState(FDP_SHM* pFDP, uint32_t CpuId, FDP_State* pDebuggeeState)
{
if(pFDP == NULL)
{
return false;
}
FDP_GET_CPU_STATE_PKT_REQ TempPkt;
TempPkt.Type = FDPCMD_GET_CPU_STATE;
TempPkt.CpuId = CpuId;
RunCmd(pFDP, pDebuggeeState, &TempPkt, sizeof TempPkt);
return true;
}
FDP_EXPORTED
bool FDP_Save(FDP_SHM* pFDP)
{
if(pFDP == NULL)
{
return false;
}
FDP_SIMPLE_PKT_REQ TempPkt;
TempPkt.Type = FDPCMD_SAVE;
return CheckRunCmd(pFDP, &TempPkt, sizeof TempPkt);
}
FDP_EXPORTED
bool FDP_Restore(FDP_SHM* pFDP)
{
if(pFDP == NULL)
{
return false;
}
FDP_SIMPLE_PKT_REQ TempPkt;
TempPkt.Type = FDPCMD_RESTORE;
return CheckRunCmd(pFDP, &TempPkt, sizeof TempPkt);
}
FDP_EXPORTED
bool FDP_GetStateChanged(FDP_SHM* pFDP)
{
if(pFDP == NULL)
{
return false;
}
bool StateChanged;
// LockSHM(pFDP->pSharedFDPSHM);
ttas_spinlock_lock(&pFDP->pSharedFDPSHM->stateChangedLock);
{
StateChanged = pFDP->pSharedFDPSHM->stateChanged;
pFDP->pSharedFDPSHM->stateChanged = false;
}
// UnlockSHM(pFDP->pSharedFDPSHM);
ttas_spinlock_unlock(&pFDP->pSharedFDPSHM->stateChangedLock);
return StateChanged;
}
FDP_EXPORTED
void FDP_SetStateChanged(FDP_SHM* pFDP)
{
if(pFDP == NULL)
{
return;
}
// LockSHM(pFDP->pSharedFDPSHM);
ttas_spinlock_lock(&pFDP->pSharedFDPSHM->stateChangedLock);
{
pFDP->pSharedFDPSHM->stateChanged = true;
}
// UnlockSHM(pFDP->pSharedFDPSHM);
ttas_spinlock_unlock(&pFDP->pSharedFDPSHM->stateChangedLock);
return;
}
FDP_EXPORTED
bool FDP_InjectInterrupt(FDP_SHM* pFDP, uint32_t CpuId, uint32_t uInterruptionCode, uint32_t uErrorCode,
uint64_t Cr2Value)
{
if(pFDP == NULL)
{
return false;
}
bool bReturnValue = false;
LockSHM(pFDP->pSharedFDPSHM);
{
FDP_INJECT_INTERRUPT_PKT_REQ* tmpPkt = (FDP_INJECT_INTERRUPT_PKT_REQ*) pFDP->OutputBuffer;
tmpPkt->Type = FDPCMD_INJECT_INTERRUPT;
tmpPkt->CpuId = CpuId;
tmpPkt->Cr2Value = Cr2Value;
tmpPkt->ErrorCode = uErrorCode;
tmpPkt->InterruptionCode = uInterruptionCode;
WriteFDPData(&pFDP->pSharedFDPSHM->ClientToServer, pFDP->OutputBuffer, sizeof *tmpPkt);
ReadFDPData(&pFDP->pSharedFDPSHM->ServerToClient, (uint8_t*) &bReturnValue); // TODO: return success/fail !
}
UnlockSHM(pFDP->pSharedFDPSHM);
return bReturnValue;
}
// Server Part
FDP_EXPORTED
bool FDP_ServerLoop(FDP_SHM* pFDP)
{
if(pFDP == NULL)
{
return false;
}
uint32_t u32InputBufferSize = 0;
uint32_t u32OutputBuffersize = 0;
pFDP->pFdpServer->bIsRunning = true;
while(pFDP->pFdpServer->bIsRunning)