moduleparam.h 19 KB

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