-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtrngd.cpp
2137 lines (1881 loc) · 53 KB
/
mtrngd.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
/////
//
// Copyright 2019 Martin Peck <[email protected]>
// this work is derived from:
//
// * rngd.c -- Random Number Generator daemon
// Copyright (C) 2001 Philipp Rumpf <[email protected]>
//
// 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 2 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
/////
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <argp.h>
#include <ctype.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/poll.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <linux/types.h>
#include <linux/random.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include <string>
#include <list>
#include "fips.h"
using namespace std;
// MUST be same as FIPS buffer size expectation: 20,000 bits
#define RND_BLOCK_SIZE 2500
typedef unsigned char byte;
typedef unsigned long u32;
typedef unsigned long long u64;
static volatile bool terminate_s = false;
void
shutdown ()
{
terminate_s = true;
// other options...
// kill (getpid(), 9);
// _exit (0);
}
string
tostr (u32 val)
{
char buf[16];
snprintf (buf, sizeof(buf), "%lu", val);
return ((string)buf);
}
string
tostr (int val)
{
char buf[16];
snprintf (buf, sizeof(buf), "%d", val);
return ((string)buf);
}
string
tostr (long val)
{
char buf[16];
snprintf (buf, sizeof(buf), "%ld", val);
return ((string)buf);
}
string
tostr (float val)
{
char buf[32];
snprintf (buf, sizeof(buf), "%f", val);
return ((string)buf);
}
class Mutex
{
public:
Mutex ();
~Mutex ();
void acquire (void);
bool tryAcquire (void);
void release (void);
private:
pthread_mutex_t mutex_;
};
Mutex::Mutex ()
{
pthread_mutex_init (&mutex_, 0);
}
Mutex::~Mutex ()
{
pthread_mutex_destroy (&mutex_);
}
void
Mutex::acquire ()
{
pthread_mutex_lock (&mutex_);
}
bool
Mutex::tryAcquire ()
{
int result;
result = pthread_mutex_trylock (&mutex_);
if (result < 0) {
return (false);
}
return (true);
}
void
Mutex::release ()
{
pthread_mutex_unlock (&mutex_);
}
class MutexLock
{
public:
MutexLock (Mutex & mutex);
~MutexLock ();
private:
Mutex * mutex_;
};
MutexLock::MutexLock (Mutex & mutex)
{
mutex_ = &mutex;
mutex_->acquire ();
}
MutexLock::~MutexLock ()
{
mutex_->release ();
}
class Timer
{
public:
Timer ();
void start (void);
void stop (void);
u64 elapsed (void) const;
private:
struct timeval start_;
struct timeval end_;
u64 diff_;
};
Timer::Timer ()
{
memset (&start_, 0, sizeof(start_));
memset (&end_, 0, sizeof(end_));
diff_ = 0;
}
void
Timer::start ()
{
gettimeofday (&start_, 0);
}
void
Timer::stop ()
{
gettimeofday (&end_, 0);
diff_ = (end_.tv_sec - start_.tv_sec) * 1000000L;
if (end_.tv_usec > start_.tv_usec) {
diff_ += end_.tv_usec - start_.tv_usec;
}
else {
diff_ -= 1000000L;
diff_ += (1000000L - start_.tv_usec) + end_.tv_usec;
}
}
u64
Timer::elapsed () const
{
return (diff_);
}
class Stats
{
public:
Stats ();
void reset (void);
void add (const Timer & timer);
void result (u64 & min,
u64 & avg,
u64 & max,
u64 & total);
private:
u32 count_;
u64 min_;
u64 max_;
u64 total_;
};
Stats::Stats ()
{
reset ();
}
void
Stats::reset ()
{
count_ = min_ = max_ = total_ = 0L;
}
void
Stats::add (const Timer & timer)
{
u64 cval = timer.elapsed ();
count_++;
total_ += cval;
if (min_ == 0 || cval < min_) min_ = cval;
if (max_ == 0 || cval > max_) max_ = cval;
}
void
Stats::result (u64 & min,
u64 & avg,
u64 & max,
u64 & total)
{
if (count_ == 0) {
min = avg = max = total = 0;
return;
}
min = min_;
avg = total_ ? total_ / count_ : 0;
max = max_;
total = total_;
}
class Log
{
public:
Log () : fd_(-1) {}
~Log ();
bool open (const string & filename);
bool open (int fd);
void write (const string & msg);
static string timestamp (void);
private:
int fd_;
Mutex lock_;
};
Log::~Log ()
{
if (fd_ >= 0) {
close (fd_);
fd_ = -1;
}
}
bool
Log::open (const string & filename)
{
int flags = O_WRONLY;
int mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
struct stat filestat;
MutexLock lock(lock_);
if (fd_ >= 0) {
close (fd_);
fd_ = -1;
}
if (stat (filename.c_str(), &filestat) != 0) {
flags |= O_CREAT;
}
else {
flags |= O_APPEND;
}
fd_ = ::open (filename.c_str(), flags, mode);
if (fd_ < 0)
return (false);
return (true);
}
bool
Log::open (int fd)
{
MutexLock lock(lock_);
if (fd_ >= 0) {
close (fd_);
fd_ = -1;
}
fd_ = dup (fd);
if (fd_ < 0)
return (false);
return (true);
}
void
Log::write (const string & msg)
{
MutexLock lock(lock_);
if (fd_ < 0)
return;
string currtime = timestamp();
::write (fd_, currtime.c_str(), currtime.size());
::write (fd_, msg.c_str(), msg.size());
::write (fd_, "\n", 1);
fsync(fd_);
}
string
Log::timestamp ()
{
const int buffMax = 128;
char tmpBuff[buffMax];
struct tm today;
time_t now;
struct timeval hrTime;
const char * dayArray[] = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };
const char * monthArray[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
gettimeofday (&hrTime, 0);
now = time(0);
localtime_r (&now, &today);
snprintf (tmpBuff, buffMax, "[%s %s %2.2d %2.2d:%2.2d:%2.2d-%6.6lu] ",
dayArray[(today.tm_wday)], monthArray[(today.tm_mon)],
today.tm_mday, today.tm_hour, today.tm_min, today.tm_sec, hrTime.tv_usec);
return ((string)tmpBuff);
}
// global application log handle. If this cannot be initialized
// application will immediately exit.
//
static Log * log_s = 0;
class LogQueue
{
public:
LogQueue ();
~LogQueue ();
bool initialize (const string & logdir,
const string & prefix,
u32 filemax);
bool currLog (Log *& log);
bool newLog (Log *& log);
bool closeAll (void);
typedef list<string> t_FileList;
private:
string logdir_;
string prefix_;
Log * log_;
u32 filemax_;
t_FileList files_;
string createFilePath (void);
};
LogQueue::LogQueue ()
{
log_ = 0;
}
LogQueue::~LogQueue ()
{
if (log_)
delete log_;
}
bool
LogQueue::initialize (const string & logdir,
const string & prefix,
u32 filemax)
{
logdir_ = logdir;
if (*(logdir_.c_str() + logdir_.size() -1) != '/') {
logdir_ += (string)"/";
}
prefix_ = prefix;
filemax_ = filemax;
return (true);
}
bool
LogQueue::currLog (Log *& log)
{
if (!log_)
return (newLog (log));
log = log_;
return (true);
}
bool
LogQueue::newLog (Log *& log)
{
string path = createFilePath ();
if (log_)
delete log_;
log_ = new Log;
if (log_->open (path) == false) {
delete log_;
log_ = 0;
return (false);
}
if (filemax_ > 0) {
files_.push_front (path);
if (files_.size () > filemax_) {
path = files_.back ();
files_.pop_back ();
unlink (path.c_str());
}
}
log = log_;
return (true);
}
bool
LogQueue::closeAll ()
{
if (log_) {
delete log_;
log_ = 0;
}
return (true);
}
string
LogQueue::createFilePath ()
{
const int max = 256;
char buf[max];
snprintf (buf, max, "%s-%d.log", prefix_.c_str(), time(0));
return (logdir_ + (string)buf);
}
typedef struct s_rng_stats {
u64 bad_fips_blocks;
u64 fips_monobit;
u64 fips_poker;
u64 fips_runs;
u64 fips_longruns;
u64 fips_contruns;
u64 good_fips_blocks;
u64 total_hwrng_bytes;
u64 total_entadd_bytes;
u64 total_devrnd_writeable;
Stats hwrng_block_stats;
Stats hwrng_fips_stats;
Stats random_starve_stats;
} t_rng_stats;
static t_rng_stats curr_rng_stats_s = { 0 };
static t_rng_stats all_rng_stats_s = { 0 };
string
prepareStatDesc (t_rng_stats * stats)
{
string desc;
const int len = 1024;
char * buf = new char[len];
u64 min, avg, max, total;
snprintf (buf, len,
" bad fips blocks ......: %llu\n"
" monobit failures ___: %llu\n"
" poker run failures _: %llu\n"
" bit run failures __: %llu\n"
" long run failures __: %llu\n"
" cont run failures __: %llu\n"
" good fips blocks .....: %llu\n"
" hwrng read bytes .....: %llu\n"
" entropy add bytes ....: %llu\n"
" random writeable cnt .: %llu\n",
stats->bad_fips_blocks,
stats->fips_monobit,
stats->fips_poker,
stats->fips_runs,
stats->fips_longruns,
stats->fips_contruns,
stats->good_fips_blocks,
stats->total_hwrng_bytes,
stats->total_entadd_bytes,
stats->total_devrnd_writeable);
desc = (string)buf;
stats->hwrng_block_stats.result (min, avg, max, total);
snprintf (buf, len, " hw entropy read stats ....: \tmin: %llu\tavg: %llu\tmax: %llu\ttotal: %llu\n", min, avg, max, total);
desc = desc + (string)buf;
stats->hwrng_fips_stats.result (min, avg, max, total);
snprintf (buf, len, " rng fips check stats .....: \tmin: %llu\tavg: %llu\tmax: %llu\ttotal: %llu\n", min, avg, max, total);
desc = desc + (string)buf;
stats->random_starve_stats.result (min, avg, max, total);
snprintf (buf, len, " random recv starve stats .: \tmin: %llu\tavg: %llu\tmax: %llu\ttotal: %llu\n", min, avg, max, total);
desc = desc + (string)buf;
delete [] buf;
return (desc);
}
void
writeStats (Log * log) {
string msg;
t_rng_stats currstats = curr_rng_stats_s;
memset (&curr_rng_stats_s, 0, sizeof(curr_rng_stats_s));
t_rng_stats allstats = all_rng_stats_s;
msg = msg + (string)"\n--- current mtrngd stats:\n";
msg = msg + prepareStatDesc (&currstats);
msg = msg + (string)"\n\n--- TOTAL mtrngd stats:\n";
msg = msg + prepareStatDesc (&allstats);
log->write (msg);
return;
}
void
dumpStatus (const string & logfile) {
Log * log = new Log;
struct stat st;
if (!stat (logfile.c_str(), &st)) {
unlink (logfile.c_str());
}
if (log->open (logfile) == true) {
string msg;
t_rng_stats allstats = all_rng_stats_s;
msg = "Current MTRNGD Status:\n";
msg = msg + prepareStatDesc (&allstats);
log->write (msg);
}
delete log;
}
void
setFillSigMask () {
int how = SIG_SETMASK;
sigset_t signals;
sigfillset (&signals);
pthread_sigmask (how, &signals, 0);
sigfillset (&signals);
sigprocmask (how, &signals, 0);
}
bool
handleSignal (int num) {
switch (num) {
case SIGQUIT: terminate_s = true; break;
case SIGTERM: terminate_s = true; break;
default:
break;
};
return (terminate_s);
}
void
processSignals (const string & logfile) {
bool done = false;
int result;
int sigNumber;
sigset_t signals;
sigfillset (&signals);
while (!done) {
result = sigwait (&signals, &sigNumber);
if (result == 0) {
if (sigNumber == SIGUSR1) { dumpStatus (logfile); }
if (handleSignal (sigNumber) == true) {
return;
}
}
}
}
class LockedMem
{
public:
LockedMem ();
~LockedMem ();
bool allocate (u32 size);
byte * buffer(void);
u32 length(void);
u32 datalen (void);
void datalen (u32 datalen);
private:
byte * buf_;
u32 len_;
u32 datalen_;
LockedMem (const LockedMem & copy);
LockedMem & operator = (const LockedMem & copy);
};
LockedMem::LockedMem ()
{
buf_ = 0;
len_ = datalen_ = 0;
}
LockedMem::~LockedMem ()
{
if (buf_) {
munlock (buf_, len_);
delete [] buf_;
buf_ = 0;
}
}
bool
LockedMem::allocate (u32 size)
{
if (buf_) {
delete [] buf_;
}
datalen_ = 0;
buf_ = new byte[size];
len_ = size;
if (mlock (buf_, len_) != 0) {
delete [] buf_;
buf_ = 0;
len_ = 0;
return (false);
}
return (true);
}
byte *
LockedMem::buffer()
{
return (buf_);
}
u32
LockedMem::length()
{
return (len_);
}
u32
LockedMem::datalen ()
{
return (datalen_);
}
void
LockedMem::datalen (u32 datalen)
{
datalen_ = datalen;
}
class RandXfer
{
public:
static bool initialize (u32 blockSize);
static bool getDestMem (LockedMem *& mem);
static bool getRandMem (LockedMem *& mem);
static void destroy (void);
private:
static bool isActive_s;
static u32 blockSize_s;
static LockedMem * currDestMem_s;
static LockedMem * currRandMem_s;
static LockedMem * currSpareMem_s;
static Mutex dataLock_s;
static Mutex waitLock_s;
static bool isWaiting_s;
static Mutex blockLock_s;
};
bool RandXfer::isActive_s = false;
u32 RandXfer::blockSize_s = 0;
LockedMem * RandXfer::currDestMem_s = 0;
LockedMem * RandXfer::currRandMem_s = 0;
LockedMem * RandXfer::currSpareMem_s = 0;
Mutex RandXfer::dataLock_s;
Mutex RandXfer::waitLock_s;
bool RandXfer::isWaiting_s = false;
Mutex RandXfer::blockLock_s;
bool
RandXfer::initialize (u32 blockSize)
{
MutexLock lock(dataLock_s);
if (isActive_s) return (false);
blockSize_s = blockSize;
currDestMem_s = new LockedMem();
currRandMem_s = new LockedMem();
currSpareMem_s = new LockedMem();
if ( (currDestMem_s->allocate (blockSize) == false) ||
(currRandMem_s->allocate (blockSize) == false) ||
(currSpareMem_s->allocate (blockSize) == false) ) {
log_s->write ((string)"Unable to allocate memory buffers in call to RandXfer::initialize.");
return (false);
}
isActive_s = true;
isWaiting_s = false;
log_s->write ((string)"Random xfer memory buffers allocated and locked.");
return (true);
}
bool
RandXfer::getDestMem (LockedMem *& mem)
{
mem = 0;
{
MutexLock lock(dataLock_s);
if (isActive_s == false) return (false);
if (currDestMem_s->datalen() < currDestMem_s->length()) {
mem = currDestMem_s;
}
else if (currSpareMem_s->datalen() < currSpareMem_s->length()) {
mem = currSpareMem_s;
currSpareMem_s = currDestMem_s;
currDestMem_s = mem;
}
else { // prepare to wait for other thread to pass us an empty buffer
waitLock_s.acquire ();
}
// if we have a buffer, check to see if we need to unblock the entropy writer
if (mem) {
waitLock_s.acquire ();
if (isWaiting_s) {
isWaiting_s = false;
blockLock_s.release ();
}
waitLock_s.release ();
}
}
if (mem == 0) {
// now outside dataLock scope, perform blocking wait
isWaiting_s = true;
blockLock_s.acquire ();
waitLock_s.release ();
blockLock_s.acquire ();
blockLock_s.release ();
// grab new empty record
{
MutexLock lock(dataLock_s);
mem = currSpareMem_s;
currSpareMem_s = currDestMem_s;
currDestMem_s = mem;
}
}
return (true);
}
bool
RandXfer::getRandMem (LockedMem *& mem)
{
mem = 0;
{
MutexLock lock(dataLock_s);
if (isActive_s == false) return (false);
if (currSpareMem_s->datalen() > 0) {
mem = currSpareMem_s;
currSpareMem_s = currRandMem_s;
currRandMem_s = mem;
}
else { // prepare to wait for other thread to pass us a buffer of random
waitLock_s.acquire ();
}
if (mem) {
waitLock_s.acquire ();
if (isWaiting_s) {
isWaiting_s = false;
blockLock_s.release ();
}
waitLock_s.release ();
}
}
if (mem == 0) {
// now outside dataLock scope, perform blocking wait
isWaiting_s = true;
blockLock_s.acquire ();
waitLock_s.release ();
blockLock_s.acquire ();
blockLock_s.release ();
// grab new empty record
{
MutexLock lock(dataLock_s);
mem = currSpareMem_s;
currSpareMem_s = currRandMem_s;
currRandMem_s = mem;
}
}
return (true);
}
void
RandXfer::destroy ()
{
MutexLock lock(dataLock_s);
isActive_s = false;
// Don't delete memory, as it may be in use. Let host
// clean up when process exits.
return;
}
enum {
/* MRP register layout
* 31:22 reserved
* 21:16 string filter count
* 15:15 string filter failed
* 14:14 string filter enabled
* 13:13 raw bits enabled
* 12:10 dc bias value
* 09:08 noise source select
* 07:07 reserved
* 06:06 rng enabled
* 05:05 reserved
* 04:00 current byte count
*/
MSR_VIA_RNG = 0x110b,
VIA_STRFILT_CNT_SHIFT = 16,
VIA_STRFILT_FAIL = (1 << 15),
VIA_STRFILT_ENABLE = (1 << 14),
VIA_STRFILT_MIN = 8,
VIA_STRFILT_MAX = 63,
VIA_STRFILT_MASK = (VIA_STRFILT_MAX << VIA_STRFILT_CNT_SHIFT),
VIA_RAWBITS_ENABLE = (1 << 13),
VIA_NOISE_SRC_SHIFT = 8,
VIA_NOISE_SRC_MASK = (3 << VIA_NOISE_SRC_SHIFT),
VIA_RNG_ENABLE = (1 << 6),
VIA_DCBIAS_SHIFT = 10,
VIA_DCBIAS_MAX = 7,
VIA_DCBIAS_MASK = (VIA_DCBIAS_MAX << VIA_DCBIAS_SHIFT),
VIA_XSTORE_CNT_MASK = 0x0F,
VIA_RNG_CHUNK_8 = 0x00, /* 64 rand bits, 64 stored bits */
};
class ViaMsrDevice;
class ViaMsrRngConfig
{
public:
ViaMsrRngConfig () {}
ViaMsrRngConfig (u32 lomsr) : lomsr_(lomsr) {}
bool getRngEnable (void);
void setRngEnable (bool enabled);
u32 getDcBias (void);
void setDcBias (u32 bias);
bool getStrfltEnable (void);
void setStrfltEnable (bool enabled);
u32 getStrfltLength (void);
void setStrfltLength (u32 length);
bool getStrfltFault (void);
void setStrfltFault (bool faulted);
u32 getNoiseSrc (void);
void setNoiseSrc (u32 source);
bool getRawbitEnable (void);
void setRawbitEnable (bool enabled);
string asString (void);
bool operator == (const ViaMsrRngConfig & b) {
if ( ((lomsr_ & VIA_RNG_ENABLE) == (b.lomsr_ & VIA_RNG_ENABLE)) &&
((lomsr_ & VIA_STRFILT_ENABLE) == (b.lomsr_ & VIA_STRFILT_ENABLE)) &&
((lomsr_ & VIA_STRFILT_MASK) == (b.lomsr_ & VIA_STRFILT_MASK)) &&
((lomsr_ & VIA_DCBIAS_MASK) == (b.lomsr_ & VIA_DCBIAS_MASK)) &&
((lomsr_ & VIA_RAWBITS_ENABLE) == (b.lomsr_ & VIA_RAWBITS_ENABLE)) &&
((lomsr_ & VIA_NOISE_SRC_MASK) == (b.lomsr_ & VIA_NOISE_SRC_MASK)) ) {
return (true);
}
return (false);
}
bool operator != (const ViaMsrRngConfig & b) { (*this == b) ? false : true; }
private:
u32 lomsr_;
u32 applyConfig (u32 srcMsrBits);
friend class ViaMsrDevice;
};
bool
ViaMsrRngConfig::getRngEnable ()
{
return ((lomsr_ & VIA_RNG_ENABLE) ? true : false);
}
void
ViaMsrRngConfig::setRngEnable (bool enabled)
{
if (enabled)
lomsr_ |= VIA_RNG_ENABLE;
else
lomsr_ &= ~(VIA_RNG_ENABLE);
}
u32
ViaMsrRngConfig::getDcBias ()
{
return ((lomsr_ & VIA_DCBIAS_MASK) >> VIA_DCBIAS_SHIFT);
}
void
ViaMsrRngConfig::setDcBias (u32 bias)
{
lomsr_ |= VIA_DCBIAS_MASK & (bias << VIA_DCBIAS_SHIFT);
}
bool
ViaMsrRngConfig::getStrfltEnable ()
{
return ((lomsr_ & VIA_STRFILT_ENABLE) ? true : false);
}
void
ViaMsrRngConfig::setStrfltEnable (bool enabled)
{
if (enabled)
lomsr_ |= VIA_STRFILT_ENABLE;
else
lomsr_ &= ~(VIA_STRFILT_ENABLE);
}
u32
ViaMsrRngConfig::getStrfltLength ()
{
return ((lomsr_ & VIA_STRFILT_MASK) >> VIA_STRFILT_CNT_SHIFT);
}
void
ViaMsrRngConfig::setStrfltLength (u32 length)
{
lomsr_ |= VIA_STRFILT_MASK & (length << VIA_STRFILT_CNT_SHIFT);
}
bool
ViaMsrRngConfig::getStrfltFault ()
{
return ((lomsr_ & VIA_STRFILT_FAIL) ? true : false);
}