random.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. struct Rb
  15. {
  16. QLock;
  17. Rendez producer;
  18. Rendez consumer;
  19. uint32_t randomcount;
  20. //unsigned char buf[1024];
  21. /** WORKAROUND **/
  22. unsigned char buf[10];
  23. /** END WORKAROUND **/
  24. unsigned char *ep;
  25. unsigned char *rp;
  26. unsigned char *wp;
  27. unsigned char next;
  28. unsigned char wakeme;
  29. uint16_t bits;
  30. uint32_t randn;
  31. } rb;
  32. static int
  33. rbnotfull(void* v)
  34. {
  35. int i;
  36. i = rb.rp - rb.wp;
  37. return i != 1 && i != (1 - sizeof(rb.buf));
  38. }
  39. static int
  40. rbnotempty(void* v)
  41. {
  42. return rb.wp != rb.rp;
  43. }
  44. static void
  45. genrandom(void* v)
  46. {
  47. Proc *up = machp()->externup;
  48. up->basepri = PriNormal;
  49. up->priority = up->basepri;
  50. for(;;){
  51. for(;;)
  52. if(++rb.randomcount > 100000)
  53. break;
  54. if(anyhigher())
  55. sched();
  56. if(!rbnotfull(0))
  57. sleep(&rb.producer, rbnotfull, 0);
  58. }
  59. }
  60. /*
  61. * produce random bits in a circular buffer
  62. */
  63. static void
  64. randomclock(void)
  65. {
  66. if(rb.randomcount == 0 || !rbnotfull(0))
  67. return;
  68. rb.bits = (rb.bits<<2) ^ rb.randomcount;
  69. rb.randomcount = 0;
  70. rb.next++;
  71. if(rb.next != 8/2)
  72. return;
  73. rb.next = 0;
  74. *rb.wp ^= rb.bits;
  75. if(rb.wp+1 == rb.ep)
  76. rb.wp = rb.buf;
  77. else
  78. rb.wp = rb.wp+1;
  79. if(rb.wakeme)
  80. wakeup(&rb.consumer);
  81. }
  82. void
  83. randominit(void)
  84. {
  85. /* Frequency close but not equal to HZ */
  86. addclock0link(randomclock, 13);
  87. rb.ep = rb.buf + sizeof(rb.buf);
  88. rb.rp = rb.wp = rb.buf;
  89. kproc("genrandom", genrandom, 0);
  90. }
  91. /*
  92. * consume random bytes from a circular buffer
  93. */
  94. uint32_t
  95. randomread(void *xp, uint32_t n)
  96. {
  97. Proc *up = machp()->externup;
  98. uint8_t *e, *p;
  99. uint32_t x;
  100. p = xp;
  101. if(waserror()){
  102. qunlock(&rb);
  103. nexterror();
  104. }
  105. qlock(&rb);
  106. /** WORKAROUND **/
  107. for(e = p + n; p < e; ){
  108. x = (2 * rb.randn +1)%1103515245;
  109. *p++ = rb.randn = x;
  110. }
  111. qunlock(&rb);
  112. poperror();
  113. return n;
  114. /** END WORKAROUND **/
  115. for(e = p + n; p < e; ){
  116. if(rb.wp == rb.rp){
  117. rb.wakeme = 1;
  118. wakeup(&rb.producer);
  119. sleep(&rb.consumer, rbnotempty, 0);
  120. rb.wakeme = 0;
  121. continue;
  122. }
  123. /*
  124. * beating clocks will be precictable if
  125. * they are synchronized. Use a cheap pseudo
  126. * random number generator to obscure any cycles.
  127. */
  128. x = rb.randn*1103515245 ^ *rb.rp;
  129. *p++ = rb.randn = x;
  130. if(rb.rp+1 == rb.ep)
  131. rb.rp = rb.buf;
  132. else
  133. rb.rp = rb.rp+1;
  134. }
  135. qunlock(&rb);
  136. poperror();
  137. wakeup(&rb.producer);
  138. return n;
  139. }
  140. /**
  141. * Fast random generator
  142. **/
  143. uint32_t
  144. urandomread(void *xp, uint32_t n)
  145. {
  146. Proc *up = machp()->externup;
  147. uint64_t seed[16];
  148. uint8_t *e, *p;
  149. uint32_t x=0;
  150. uint64_t s0;
  151. uint64_t s1;
  152. if(waserror()){
  153. nexterror();
  154. }
  155. //The initial seed is from a good random pool.
  156. randomread(seed, sizeof(seed));
  157. p = xp;
  158. for(e = p + n; p < e; ){
  159. s0 = seed[ x ];
  160. s1 = seed[ x = (x+1) & 15 ];
  161. s1 ^= s1 << 31;
  162. s1 ^= s1 >> 11;
  163. s0 ^= s0 >> 30;
  164. *p++=( seed[x] = s0 ^ s1 ) * 1181783497276652981LL;
  165. }
  166. poperror();
  167. return n;
  168. }