-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_twister.c
175 lines (159 loc) · 4.78 KB
/
mod_twister.c
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
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/timex.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("dimadivan");
MODULE_DESCRIPTION("Pseudo-random number generator (Twister)");
MODULE_VERSION("0.01");
#define DEVICE_NAME "prng2"
// объявление функций символьного устройства
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
static int major_num;
static int device_open_count = 0;
// static char onebuf[1024];
// static long rbuf[129];
static char rbuf[1024];
#define P 624
#define Q 397
#define MATRIX_A 0x9908b0dfUL
#define UPPER_MASK 0x80000000UL
#define LOWER_MASK 0x7fffffffUL
static unsigned long mt[P];
static int mti = P + 1;
// эта структура указывает на все функции нашего устройства
static struct file_operations file_ops = {
.owner = THIS_MODULE,
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
void init_genrand(unsigned long seed)
{
mt[0] = seed & 0xffffffffUL;
// for (mti=1;mti<P;mti++)
mti = 1;
while(mti < P)
{
mt[mti] = (1664525UL * mt[mti-1] + 1UL);
mt[mti] &= 0xffffffffUL;
mti++;
}
}
unsigned long genrand(void)
{
unsigned long y;
static unsigned long mag01[2] = {0x0UL, MATRIX_A};
if (mti >= P)
{
int kk;
if (mti == P + 1)
init_genrand(5489UL);
// for (kk = 0;kk < P - Q;kk++)
kk = 0;
while (kk < P - Q)
{
y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
mt[kk] = mt[kk+Q] ^ (y>>1) ^ mag01[y & 0x1UL];
kk++;
}
// for (;kk < P - 1;kk++)
while (kk < P - 1)
{
y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
mt[kk] = mt[kk+(Q-P)] ^ (y>>1) ^ mag01[y & 0x1UL];
kk++;
}
y = (mt[P-1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
mt[P-1] = mt[Q-1] ^ (y>>1) ^ mag01[y & 0x1UL];
mti = 0;
}
y = mt[mti++];
// темперирование
y ^= (y>>11);
y ^= (y<<7) & 0x9d2c5680UL;
y ^= (y<<15) & 0xefc60000UL;
y ^= (y>>18);
return y;
}
long genrand_31(void) { return (long)(genrand()>>1); }
// когда процесс читает наше устройство, вызывается эта функция
static ssize_t device_read(struct file *flip, char *buffer, size_t len, loff_t *offset)
{
int amount = 0;
int counter = 0;
unsigned long res;
while (len)
{
int i=0;
while(i<256)
{
long tmp = genrand_31();
memcpy(rbuf+i*4, &tmp, 4);
i++;
}
if (len > 1024)
amount = 1024;
else
amount = len;
res = copy_to_user(buffer, rbuf, amount);
buffer += amount;
if (res != 0)
{
printk(KERN_INFO "Tried to copy %d bytes, %ld bytes failed.\n", amount, res);
amount -= res;
}
len -= amount;
counter += amount;
if (signal_pending(current))
break;
cond_resched();
}
return counter;
}
// вызывается, когда процесс пытается записать что-то в наше устройство
static ssize_t device_write(struct file *flip, const char *buffer, size_t len, loff_t *offset)
{
printk(KERN_ALERT "This operation is not supported.\n");
return -EINVAL;
}
// вызывается, когда процесс открывает наше устройство
static int device_open(struct inode *inode, struct file *file)
{
init_genrand((unsigned long)random_get_entropy());
device_open_count++;
return 0;
}
// вызывается, когда процесс закрывает наше устройство
static int device_release(struct inode *inode, struct file *file)
{
return 0;
}
static int __init chrdev2_init(void)
{
major_num = register_chrdev(0, DEVICE_NAME, &file_ops);
if (major_num < 0)
{
printk(KERN_ALERT "Could not register device: %d\n", major_num);
return major_num;
}
else
{
printk(KERN_INFO "Module3 loaded with device major number %d\n", major_num);
return 0;
}
}
static void __exit chrdev2_exit(void)
{
unregister_chrdev(major_num, DEVICE_NAME);
printk(KERN_INFO "Unloading module3, device was opened %d times.\n", device_open_count);
}
// регистрируем функции модуля
module_init(chrdev2_init);
module_exit(chrdev2_exit);