hc
2024-05-10 23fa18eaa71266feff7ba8d83022d9e1cc83c65a
kernel/include/linux/random.h
....@@ -1,52 +1,35 @@
11 /* SPDX-License-Identifier: GPL-2.0 */
2
-/*
3
- * include/linux/random.h
4
- *
5
- * Include file for the random number generator.
6
- */
2
+
73 #ifndef _LINUX_RANDOM_H
84 #define _LINUX_RANDOM_H
95
6
+#include <linux/bug.h>
7
+#include <linux/kernel.h>
108 #include <linux/list.h>
119 #include <linux/once.h>
1210
1311 #include <uapi/linux/random.h>
1412
15
-struct random_ready_callback {
16
- struct list_head list;
17
- void (*func)(struct random_ready_callback *rdy);
18
- struct module *owner;
19
-};
13
+struct notifier_block;
2014
21
-extern void add_device_randomness(const void *, unsigned int);
22
-extern void add_bootloader_randomness(const void *, unsigned int);
15
+void add_device_randomness(const void *buf, unsigned int len);
16
+void add_bootloader_randomness(const void *buf, size_t len);
17
+void add_input_randomness(unsigned int type, unsigned int code,
18
+ unsigned int value) __latent_entropy;
19
+void add_interrupt_randomness(int irq) __latent_entropy;
20
+void add_hwgenerator_randomness(const void *buf, size_t len, size_t entropy);
2321
24
-#if defined(CONFIG_GCC_PLUGIN_LATENT_ENTROPY) && !defined(__CHECKER__)
2522 static inline void add_latent_entropy(void)
2623 {
27
- add_device_randomness((const void *)&latent_entropy,
28
- sizeof(latent_entropy));
29
-}
24
+#if defined(LATENT_ENTROPY_PLUGIN) && !defined(__CHECKER__)
25
+ add_device_randomness((const void *)&latent_entropy, sizeof(latent_entropy));
3026 #else
31
-static inline void add_latent_entropy(void) {}
27
+ add_device_randomness(NULL, 0);
3228 #endif
29
+}
3330
34
-extern void add_input_randomness(unsigned int type, unsigned int code,
35
- unsigned int value) __latent_entropy;
36
-extern void add_interrupt_randomness(int irq, int irq_flags) __latent_entropy;
37
-
38
-extern void get_random_bytes(void *buf, int nbytes);
39
-extern int wait_for_random_bytes(void);
40
-extern int __init rand_initialize(void);
41
-extern bool rng_is_initialized(void);
42
-extern int add_random_ready_callback(struct random_ready_callback *rdy);
43
-extern void del_random_ready_callback(struct random_ready_callback *rdy);
44
-extern int __must_check get_random_bytes_arch(void *buf, int nbytes);
45
-
46
-#ifndef MODULE
47
-extern const struct file_operations random_fops, urandom_fops;
48
-#endif
49
-
31
+void get_random_bytes(void *buf, int len);
32
+int __must_check get_random_bytes_arch(void *buf, int len);
5033 u32 get_random_u32(void);
5134 u64 get_random_u64(void);
5235 static inline unsigned int get_random_int(void)
....@@ -78,35 +61,37 @@
7861
7962 static inline unsigned long get_random_canary(void)
8063 {
81
- unsigned long val = get_random_long();
82
-
83
- return val & CANARY_MASK;
64
+ return get_random_long() & CANARY_MASK;
8465 }
66
+
67
+int __init random_init(const char *command_line);
68
+bool rng_is_initialized(void);
69
+int wait_for_random_bytes(void);
70
+int register_random_ready_notifier(struct notifier_block *nb);
71
+int unregister_random_ready_notifier(struct notifier_block *nb);
8572
8673 /* Calls wait_for_random_bytes() and then calls get_random_bytes(buf, nbytes).
8774 * Returns the result of the call to wait_for_random_bytes. */
88
-static inline int get_random_bytes_wait(void *buf, int nbytes)
75
+static inline int get_random_bytes_wait(void *buf, size_t nbytes)
8976 {
9077 int ret = wait_for_random_bytes();
9178 get_random_bytes(buf, nbytes);
9279 return ret;
9380 }
9481
95
-#define declare_get_random_var_wait(var) \
96
- static inline int get_random_ ## var ## _wait(var *out) { \
82
+#define declare_get_random_var_wait(name, ret_type) \
83
+ static inline int get_random_ ## name ## _wait(ret_type *out) { \
9784 int ret = wait_for_random_bytes(); \
9885 if (unlikely(ret)) \
9986 return ret; \
100
- *out = get_random_ ## var(); \
87
+ *out = get_random_ ## name(); \
10188 return 0; \
10289 }
103
-declare_get_random_var_wait(u32)
104
-declare_get_random_var_wait(u64)
105
-declare_get_random_var_wait(int)
106
-declare_get_random_var_wait(long)
90
+declare_get_random_var_wait(u32, u32)
91
+declare_get_random_var_wait(u64, u32)
92
+declare_get_random_var_wait(int, unsigned int)
93
+declare_get_random_var_wait(long, unsigned long)
10794 #undef declare_get_random_var
108
-
109
-unsigned long randomize_page(unsigned long start, unsigned long range);
11095
11196 /*
11297 * This is designed to be standalone for just prandom
....@@ -118,22 +103,52 @@
118103 #ifdef CONFIG_ARCH_RANDOM
119104 # include <asm/archrandom.h>
120105 #else
121
-static inline bool __must_check arch_get_random_long(unsigned long *v)
106
+static inline bool __must_check arch_get_random_long(unsigned long *v) { return false; }
107
+static inline bool __must_check arch_get_random_int(unsigned int *v) { return false; }
108
+static inline bool __must_check arch_get_random_seed_long(unsigned long *v) { return false; }
109
+static inline bool __must_check arch_get_random_seed_int(unsigned int *v) { return false; }
110
+#endif
111
+
112
+/*
113
+ * Called from the boot CPU during startup; not valid to call once
114
+ * secondary CPUs are up and preemption is possible.
115
+ */
116
+#ifndef arch_get_random_seed_long_early
117
+static inline bool __init arch_get_random_seed_long_early(unsigned long *v)
122118 {
123
- return false;
124
-}
125
-static inline bool __must_check arch_get_random_int(unsigned int *v)
126
-{
127
- return false;
128
-}
129
-static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
130
-{
131
- return false;
132
-}
133
-static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
134
-{
135
- return false;
119
+ WARN_ON(system_state != SYSTEM_BOOTING);
120
+ return arch_get_random_seed_long(v);
136121 }
137122 #endif
138123
124
+#ifndef arch_get_random_long_early
125
+static inline bool __init arch_get_random_long_early(unsigned long *v)
126
+{
127
+ WARN_ON(system_state != SYSTEM_BOOTING);
128
+ return arch_get_random_long(v);
129
+}
130
+#endif
131
+
132
+#ifdef CONFIG_SMP
133
+int random_prepare_cpu(unsigned int cpu);
134
+int random_online_cpu(unsigned int cpu);
135
+#endif
136
+
137
+#ifndef MODULE
138
+extern const struct file_operations random_fops, urandom_fops;
139
+#endif
140
+
141
+/*
142
+ * Android KABI fixups
143
+ * Added back the following structure and calls to preserve the ABI for
144
+ * out-of-tree drivers that were using them.
145
+ */
146
+struct random_ready_callback {
147
+ struct list_head list;
148
+ void (*func)(struct random_ready_callback *rdy);
149
+ struct module *owner;
150
+};
151
+extern int add_random_ready_callback(struct random_ready_callback *rdy);
152
+extern void del_random_ready_callback(struct random_ready_callback *rdy);
153
+
139154 #endif /* _LINUX_RANDOM_H */