xref: /linux/include/linux/moduleparam.h (revision be709d48329a500621d2a05835283150ae137b45)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MODULE_PARAMS_H
3 #define _LINUX_MODULE_PARAMS_H
4 /* (C) Copyright 2001, 2002 Rusty Russell IBM Corporation */
5 #include <linux/init.h>
6 #include <linux/stringify.h>
7 #include <linux/kernel.h>
8 
9 /* You can override this manually, but generally this should match the
10    module name. */
11 #ifdef MODULE
12 #define MODULE_PARAM_PREFIX /* empty */
13 #else
14 #define MODULE_PARAM_PREFIX KBUILD_MODNAME "."
15 #endif
16 
17 /* Chosen so that structs with an unsigned long line up. */
18 #define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long))
19 
20 #ifdef MODULE
21 #define __MODULE_INFO(tag, name, info)					  \
22 static const char __UNIQUE_ID(name)[]					  \
23   __used __attribute__((section(".modinfo"), unused, aligned(1)))	  \
24   = __stringify(tag) "=" info
25 #else  /* !MODULE */
26 /* This struct is here for syntactic coherency, it is not used */
27 #define __MODULE_INFO(tag, name, info)					  \
28   struct __UNIQUE_ID(name) {}
29 #endif
30 #define __MODULE_PARM_TYPE(name, _type)					  \
31   __MODULE_INFO(parmtype, name##type, #name ":" _type)
32 
33 /* One for each parameter, describing how to use it.  Some files do
34    multiple of these per line, so can't just use MODULE_INFO. */
35 #define MODULE_PARM_DESC(_parm, desc) \
36 	__MODULE_INFO(parm, _parm, #_parm ":" desc)
37 
38 struct kernel_param;
39 
40 /*
41  * Flags available for kernel_param_ops
42  *
43  * NOARG - the parameter allows for no argument (foo instead of foo=1)
44  */
45 enum {
46 	KERNEL_PARAM_OPS_FL_NOARG = (1 << 0)
47 };
48 
49 struct kernel_param_ops {
50 	/* How the ops should behave */
51 	unsigned int flags;
52 	/* Returns 0, or -errno.  arg is in kp->arg. */
53 	int (*set)(const char *val, const struct kernel_param *kp);
54 	/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
55 	int (*get)(char *buffer, const struct kernel_param *kp);
56 	/* Optional function to free kp->arg when module unloaded. */
57 	void (*free)(void *arg);
58 };
59 
60 /*
61  * Flags available for kernel_param
62  *
63  * UNSAFE - the parameter is dangerous and setting it will taint the kernel
64  * HWPARAM - Hardware param not permitted in lockdown mode
65  */
66 enum {
67 	KERNEL_PARAM_FL_UNSAFE	= (1 << 0),
68 	KERNEL_PARAM_FL_HWPARAM	= (1 << 1),
69 };
70 
71 struct kernel_param {
72 	const char *name;
73 	struct module *mod;
74 	const struct kernel_param_ops *ops;
75 	const u16 perm;
76 	s8 level;
77 	u8 flags;
78 	union {
79 		void *arg;
80 		const struct kparam_string *str;
81 		const struct kparam_array *arr;
82 	};
83 };
84 
85 extern const struct kernel_param __start___param[], __stop___param[];
86 
87 /* Special one for strings we want to copy into */
88 struct kparam_string {
89 	unsigned int maxlen;
90 	char *string;
91 };
92 
93 /* Special one for arrays */
94 struct kparam_array
95 {
96 	unsigned int max;
97 	unsigned int elemsize;
98 	unsigned int *num;
99 	const struct kernel_param_ops *ops;
100 	void *elem;
101 };
102 
103 /**
104  * module_param - typesafe helper for a module/cmdline parameter
105  * @value: the variable to alter, and exposed parameter name.
106  * @type: the type of the parameter
107  * @perm: visibility in sysfs.
108  *
109  * @value becomes the module parameter, or (prefixed by KBUILD_MODNAME and a
110  * ".") the kernel commandline parameter.  Note that - is changed to _, so
111  * the user can use "foo-bar=1" even for variable "foo_bar".
112  *
113  * @perm is 0 if the the variable is not to appear in sysfs, or 0444
114  * for world-readable, 0644 for root-writable, etc.  Note that if it
115  * is writable, you may need to use kernel_param_lock() around
116  * accesses (esp. charp, which can be kfreed when it changes).
117  *
118  * The @type is simply pasted to refer to a param_ops_##type and a
119  * param_check_##type: for convenience many standard types are provided but
120  * you can create your own by defining those variables.
121  *
122  * Standard types are:
123  *	byte, short, ushort, int, uint, long, ulong
124  *	charp: a character pointer
125  *	bool: a bool, values 0/1, y/n, Y/N.
126  *	invbool: the above, only sense-reversed (N = true).
127  */
128 #define module_param(name, type, perm)				\
129 	module_param_named(name, name, type, perm)
130 
131 /**
132  * module_param_unsafe - same as module_param but taints kernel
133  */
134 #define module_param_unsafe(name, type, perm)			\
135 	module_param_named_unsafe(name, name, type, perm)
136 
137 /**
138  * module_param_named - typesafe helper for a renamed module/cmdline parameter
139  * @name: a valid C identifier which is the parameter name.
140  * @value: the actual lvalue to alter.
141  * @type: the type of the parameter
142  * @perm: visibility in sysfs.
143  *
144  * Usually it's a good idea to have variable names and user-exposed names the
145  * same, but that's harder if the variable must be non-static or is inside a
146  * structure.  This allows exposure under a different name.
147  */
148 #define module_param_named(name, value, type, perm)			   \
149 	param_check_##type(name, &(value));				   \
150 	module_param_cb(name, &param_ops_##type, &value, perm);		   \
151 	__MODULE_PARM_TYPE(name, #type)
152 
153 /**
154  * module_param_named_unsafe - same as module_param_named but taints kernel
155  */
156 #define module_param_named_unsafe(name, value, type, perm)		\
157 	param_check_##type(name, &(value));				\
158 	module_param_cb_unsafe(name, &param_ops_##type, &value, perm);	\
159 	__MODULE_PARM_TYPE(name, #type)
160 
161 /**
162  * module_param_cb - general callback for a module/cmdline parameter
163  * @name: a valid C identifier which is the parameter name.
164  * @ops: the set & get operations for this parameter.
165  * @perm: visibility in sysfs.
166  *
167  * The ops can have NULL set or get functions.
168  */
169 #define module_param_cb(name, ops, arg, perm)				      \
170 	__module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, -1, 0)
171 
172 #define module_param_cb_unsafe(name, ops, arg, perm)			      \
173 	__module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, -1,    \
174 			    KERNEL_PARAM_FL_UNSAFE)
175 
176 /**
177  * <level>_param_cb - general callback for a module/cmdline parameter
178  *                    to be evaluated before certain initcall level
179  * @name: a valid C identifier which is the parameter name.
180  * @ops: the set & get operations for this parameter.
181  * @perm: visibility in sysfs.
182  *
183  * The ops can have NULL set or get functions.
184  */
185 #define __level_param_cb(name, ops, arg, perm, level)			\
186 	__module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, level, 0)
187 
188 #define core_param_cb(name, ops, arg, perm)		\
189 	__level_param_cb(name, ops, arg, perm, 1)
190 
191 #define postcore_param_cb(name, ops, arg, perm)		\
192 	__level_param_cb(name, ops, arg, perm, 2)
193 
194 #define arch_param_cb(name, ops, arg, perm)		\
195 	__level_param_cb(name, ops, arg, perm, 3)
196 
197 #define subsys_param_cb(name, ops, arg, perm)		\
198 	__level_param_cb(name, ops, arg, perm, 4)
199 
200 #define fs_param_cb(name, ops, arg, perm)		\
201 	__level_param_cb(name, ops, arg, perm, 5)
202 
203 #define device_param_cb(name, ops, arg, perm)		\
204 	__level_param_cb(name, ops, arg, perm, 6)
205 
206 #define late_param_cb(name, ops, arg, perm)		\
207 	__level_param_cb(name, ops, arg, perm, 7)
208 
209 /* On alpha, ia64 and ppc64 relocations to global data cannot go into
210    read-only sections (which is part of respective UNIX ABI on these
211    platforms). So 'const' makes no sense and even causes compile failures
212    with some compilers. */
213 #if defined(CONFIG_ALPHA) || defined(CONFIG_IA64) || defined(CONFIG_PPC64)
214 #define __moduleparam_const
215 #else
216 #define __moduleparam_const const
217 #endif
218 
219 /* This is the fundamental function for registering boot/module
220    parameters. */
221 #define __module_param_call(prefix, name, ops, arg, perm, level, flags)	\
222 	/* Default value instead of permissions? */			\
223 	static const char __param_str_##name[] = prefix #name;		\
224 	static struct kernel_param __moduleparam_const __param_##name	\
225 	__used								\
226     __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \
227 	= { __param_str_##name, THIS_MODULE, ops,			\
228 	    VERIFY_OCTAL_PERMISSIONS(perm), level, flags, { arg } }
229 
230 /* Obsolete - use module_param_cb() */
231 #define module_param_call(name, _set, _get, arg, perm)			\
232 	static const struct kernel_param_ops __param_ops_##name =	\
233 		{ .flags = 0, .set = _set, .get = _get };		\
234 	__module_param_call(MODULE_PARAM_PREFIX,			\
235 			    name, &__param_ops_##name, arg, perm, -1, 0)
236 
237 #ifdef CONFIG_SYSFS
238 extern void kernel_param_lock(struct module *mod);
239 extern void kernel_param_unlock(struct module *mod);
240 #else
241 static inline void kernel_param_lock(struct module *mod)
242 {
243 }
244 static inline void kernel_param_unlock(struct module *mod)
245 {
246 }
247 #endif
248 
249 #ifndef MODULE
250 /**
251  * core_param - define a historical core kernel parameter.
252  * @name: the name of the cmdline and sysfs parameter (often the same as var)
253  * @var: the variable
254  * @type: the type of the parameter
255  * @perm: visibility in sysfs
256  *
257  * core_param is just like module_param(), but cannot be modular and
258  * doesn't add a prefix (such as "printk.").  This is for compatibility
259  * with __setup(), and it makes sense as truly core parameters aren't
260  * tied to the particular file they're in.
261  */
262 #define core_param(name, var, type, perm)				\
263 	param_check_##type(name, &(var));				\
264 	__module_param_call("", name, &param_ops_##type, &var, perm, -1, 0)
265 
266 /**
267  * core_param_unsafe - same as core_param but taints kernel
268  */
269 #define core_param_unsafe(name, var, type, perm)		\
270 	param_check_##type(name, &(var));				\
271 	__module_param_call("", name, &param_ops_##type, &var, perm,	\
272 			    -1, KERNEL_PARAM_FL_UNSAFE)
273 
274 #endif /* !MODULE */
275 
276 /**
277  * module_param_string - a char array parameter
278  * @name: the name of the parameter
279  * @string: the string variable
280  * @len: the maximum length of the string, incl. terminator
281  * @perm: visibility in sysfs.
282  *
283  * This actually copies the string when it's set (unlike type charp).
284  * @len is usually just sizeof(string).
285  */
286 #define module_param_string(name, string, len, perm)			\
287 	static const struct kparam_string __param_string_##name		\
288 		= { len, string };					\
289 	__module_param_call(MODULE_PARAM_PREFIX, name,			\
290 			    &param_ops_string,				\
291 			    .str = &__param_string_##name, perm, -1, 0);\
292 	__MODULE_PARM_TYPE(name, "string")
293 
294 /**
295  * parameq - checks if two parameter names match
296  * @name1: parameter name 1
297  * @name2: parameter name 2
298  *
299  * Returns true if the two parameter names are equal.
300  * Dashes (-) are considered equal to underscores (_).
301  */
302 extern bool parameq(const char *name1, const char *name2);
303 
304 /**
305  * parameqn - checks if two parameter names match
306  * @name1: parameter name 1
307  * @name2: parameter name 2
308  * @n: the length to compare
309  *
310  * Similar to parameq(), except it compares @n characters.
311  */
312 extern bool parameqn(const char *name1, const char *name2, size_t n);
313 
314 /* Called on module insert or kernel boot */
315 extern char *parse_args(const char *name,
316 		      char *args,
317 		      const struct kernel_param *params,
318 		      unsigned num,
319 		      s16 level_min,
320 		      s16 level_max,
321 		      void *arg,
322 		      int (*unknown)(char *param, char *val,
323 				     const char *doing, void *arg));
324 
325 /* Called by module remove. */
326 #ifdef CONFIG_SYSFS
327 extern void destroy_params(const struct kernel_param *params, unsigned num);
328 #else
329 static inline void destroy_params(const struct kernel_param *params,
330 				  unsigned num)
331 {
332 }
333 #endif /* !CONFIG_SYSFS */
334 
335 /* All the helper functions */
336 /* The macros to do compile-time type checking stolen from Jakub
337    Jelinek, who IIRC came up with this idea for the 2.4 module init code. */
338 #define __param_check(name, p, type) \
339 	static inline type __always_unused *__check_##name(void) { return(p); }
340 
341 extern const struct kernel_param_ops param_ops_byte;
342 extern int param_set_byte(const char *val, const struct kernel_param *kp);
343 extern int param_get_byte(char *buffer, const struct kernel_param *kp);
344 #define param_check_byte(name, p) __param_check(name, p, unsigned char)
345 
346 extern const struct kernel_param_ops param_ops_short;
347 extern int param_set_short(const char *val, const struct kernel_param *kp);
348 extern int param_get_short(char *buffer, const struct kernel_param *kp);
349 #define param_check_short(name, p) __param_check(name, p, short)
350 
351 extern const struct kernel_param_ops param_ops_ushort;
352 extern int param_set_ushort(const char *val, const struct kernel_param *kp);
353 extern int param_get_ushort(char *buffer, const struct kernel_param *kp);
354 #define param_check_ushort(name, p) __param_check(name, p, unsigned short)
355 
356 extern const struct kernel_param_ops param_ops_int;
357 extern int param_set_int(const char *val, const struct kernel_param *kp);
358 extern int param_get_int(char *buffer, const struct kernel_param *kp);
359 #define param_check_int(name, p) __param_check(name, p, int)
360 
361 extern const struct kernel_param_ops param_ops_uint;
362 extern int param_set_uint(const char *val, const struct kernel_param *kp);
363 extern int param_get_uint(char *buffer, const struct kernel_param *kp);
364 #define param_check_uint(name, p) __param_check(name, p, unsigned int)
365 
366 extern const struct kernel_param_ops param_ops_long;
367 extern int param_set_long(const char *val, const struct kernel_param *kp);
368 extern int param_get_long(char *buffer, const struct kernel_param *kp);
369 #define param_check_long(name, p) __param_check(name, p, long)
370 
371 extern const struct kernel_param_ops param_ops_ulong;
372 extern int param_set_ulong(const char *val, const struct kernel_param *kp);
373 extern int param_get_ulong(char *buffer, const struct kernel_param *kp);
374 #define param_check_ulong(name, p) __param_check(name, p, unsigned long)
375 
376 extern const struct kernel_param_ops param_ops_ullong;
377 extern int param_set_ullong(const char *val, const struct kernel_param *kp);
378 extern int param_get_ullong(char *buffer, const struct kernel_param *kp);
379 #define param_check_ullong(name, p) __param_check(name, p, unsigned long long)
380 
381 extern const struct kernel_param_ops param_ops_charp;
382 extern int param_set_charp(const char *val, const struct kernel_param *kp);
383 extern int param_get_charp(char *buffer, const struct kernel_param *kp);
384 extern void param_free_charp(void *arg);
385 #define param_check_charp(name, p) __param_check(name, p, char *)
386 
387 /* We used to allow int as well as bool.  We're taking that away! */
388 extern const struct kernel_param_ops param_ops_bool;
389 extern int param_set_bool(const char *val, const struct kernel_param *kp);
390 extern int param_get_bool(char *buffer, const struct kernel_param *kp);
391 #define param_check_bool(name, p) __param_check(name, p, bool)
392 
393 extern const struct kernel_param_ops param_ops_bool_enable_only;
394 extern int param_set_bool_enable_only(const char *val,
395 				      const struct kernel_param *kp);
396 /* getter is the same as for the regular bool */
397 #define param_check_bool_enable_only param_check_bool
398 
399 extern const struct kernel_param_ops param_ops_invbool;
400 extern int param_set_invbool(const char *val, const struct kernel_param *kp);
401 extern int param_get_invbool(char *buffer, const struct kernel_param *kp);
402 #define param_check_invbool(name, p) __param_check(name, p, bool)
403 
404 /* An int, which can only be set like a bool (though it shows as an int). */
405 extern const struct kernel_param_ops param_ops_bint;
406 extern int param_set_bint(const char *val, const struct kernel_param *kp);
407 #define param_get_bint param_get_int
408 #define param_check_bint param_check_int
409 
410 /**
411  * module_param_array - a parameter which is an array of some type
412  * @name: the name of the array variable
413  * @type: the type, as per module_param()
414  * @nump: optional pointer filled in with the number written
415  * @perm: visibility in sysfs
416  *
417  * Input and output are as comma-separated values.  Commas inside values
418  * don't work properly (eg. an array of charp).
419  *
420  * ARRAY_SIZE(@name) is used to determine the number of elements in the
421  * array, so the definition must be visible.
422  */
423 #define module_param_array(name, type, nump, perm)		\
424 	module_param_array_named(name, name, type, nump, perm)
425 
426 /**
427  * module_param_array_named - renamed parameter which is an array of some type
428  * @name: a valid C identifier which is the parameter name
429  * @array: the name of the array variable
430  * @type: the type, as per module_param()
431  * @nump: optional pointer filled in with the number written
432  * @perm: visibility in sysfs
433  *
434  * This exposes a different name than the actual variable name.  See
435  * module_param_named() for why this might be necessary.
436  */
437 #define module_param_array_named(name, array, type, nump, perm)		\
438 	param_check_##type(name, &(array)[0]);				\
439 	static const struct kparam_array __param_arr_##name		\
440 	= { .max = ARRAY_SIZE(array), .num = nump,                      \
441 	    .ops = &param_ops_##type,					\
442 	    .elemsize = sizeof(array[0]), .elem = array };		\
443 	__module_param_call(MODULE_PARAM_PREFIX, name,			\
444 			    &param_array_ops,				\
445 			    .arr = &__param_arr_##name,			\
446 			    perm, -1, 0);				\
447 	__MODULE_PARM_TYPE(name, "array of " #type)
448 
449 enum hwparam_type {
450 	hwparam_ioport,		/* Module parameter configures an I/O port */
451 	hwparam_iomem,		/* Module parameter configures an I/O mem address */
452 	hwparam_ioport_or_iomem, /* Module parameter could be either, depending on other option */
453 	hwparam_irq,		/* Module parameter configures an IRQ */
454 	hwparam_dma,		/* Module parameter configures a DMA channel */
455 	hwparam_dma_addr,	/* Module parameter configures a DMA buffer address */
456 	hwparam_other,		/* Module parameter configures some other value */
457 };
458 
459 /**
460  * module_param_hw_named - A parameter representing a hw parameters
461  * @name: a valid C identifier which is the parameter name.
462  * @value: the actual lvalue to alter.
463  * @type: the type of the parameter
464  * @hwtype: what the value represents (enum hwparam_type)
465  * @perm: visibility in sysfs.
466  *
467  * Usually it's a good idea to have variable names and user-exposed names the
468  * same, but that's harder if the variable must be non-static or is inside a
469  * structure.  This allows exposure under a different name.
470  */
471 #define module_param_hw_named(name, value, type, hwtype, perm)		\
472 	param_check_##type(name, &(value));				\
473 	__module_param_call(MODULE_PARAM_PREFIX, name,			\
474 			    &param_ops_##type, &value,			\
475 			    perm, -1,					\
476 			    KERNEL_PARAM_FL_HWPARAM | (hwparam_##hwtype & 0));	\
477 	__MODULE_PARM_TYPE(name, #type)
478 
479 #define module_param_hw(name, type, hwtype, perm)		\
480 	module_param_hw_named(name, name, type, hwtype, perm)
481 
482 /**
483  * module_param_hw_array - A parameter representing an array of hw parameters
484  * @name: the name of the array variable
485  * @type: the type, as per module_param()
486  * @hwtype: what the value represents (enum hwparam_type)
487  * @nump: optional pointer filled in with the number written
488  * @perm: visibility in sysfs
489  *
490  * Input and output are as comma-separated values.  Commas inside values
491  * don't work properly (eg. an array of charp).
492  *
493  * ARRAY_SIZE(@name) is used to determine the number of elements in the
494  * array, so the definition must be visible.
495  */
496 #define module_param_hw_array(name, type, hwtype, nump, perm)		\
497 	param_check_##type(name, &(name)[0]);				\
498 	static const struct kparam_array __param_arr_##name		\
499 	= { .max = ARRAY_SIZE(name), .num = nump,			\
500 	    .ops = &param_ops_##type,					\
501 	    .elemsize = sizeof(name[0]), .elem = name };		\
502 	__module_param_call(MODULE_PARAM_PREFIX, name,			\
503 			    &param_array_ops,				\
504 			    .arr = &__param_arr_##name,			\
505 			    perm, -1,					\
506 			    KERNEL_PARAM_FL_HWPARAM | (hwparam_##hwtype & 0));	\
507 	__MODULE_PARM_TYPE(name, "array of " #type)
508 
509 
510 extern const struct kernel_param_ops param_array_ops;
511 
512 extern const struct kernel_param_ops param_ops_string;
513 extern int param_set_copystring(const char *val, const struct kernel_param *);
514 extern int param_get_string(char *buffer, const struct kernel_param *kp);
515 
516 /* for exporting parameters in /sys/module/.../parameters */
517 
518 struct module;
519 
520 #if defined(CONFIG_SYSFS) && defined(CONFIG_MODULES)
521 extern int module_param_sysfs_setup(struct module *mod,
522 				    const struct kernel_param *kparam,
523 				    unsigned int num_params);
524 
525 extern void module_param_sysfs_remove(struct module *mod);
526 #else
527 static inline int module_param_sysfs_setup(struct module *mod,
528 			     const struct kernel_param *kparam,
529 			     unsigned int num_params)
530 {
531 	return 0;
532 }
533 
534 static inline void module_param_sysfs_remove(struct module *mod)
535 { }
536 #endif
537 
538 #endif /* _LINUX_MODULE_PARAMS_H */
539