moduleparam.h 17 KB

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