module.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220
  1. /*
  2. Copyright (C) 2002 Richard Henderson
  3. Copyright (C) 2001 Rusty Russell, 2002 Rusty Russell IBM.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include <linux/config.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleloader.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/slab.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/elf.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/fcntl.h>
  27. #include <linux/rcupdate.h>
  28. #include <linux/capability.h>
  29. #include <linux/cpu.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/errno.h>
  32. #include <linux/err.h>
  33. #include <linux/vermagic.h>
  34. #include <linux/notifier.h>
  35. #include <linux/stop_machine.h>
  36. #include <linux/device.h>
  37. #include <linux/string.h>
  38. #include <linux/sched.h>
  39. #include <linux/mutex.h>
  40. #include <linux/unwind.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/semaphore.h>
  43. #include <asm/cacheflush.h>
  44. #include <linux/license.h>
  45. #if 0
  46. #define DEBUGP printk
  47. #else
  48. #define DEBUGP(fmt , a...)
  49. #endif
  50. #ifndef ARCH_SHF_SMALL
  51. #define ARCH_SHF_SMALL 0
  52. #endif
  53. /* If this is set, the section belongs in the init part of the module */
  54. #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
  55. /* Protects module list */
  56. static DEFINE_SPINLOCK(modlist_lock);
  57. /* List of modules, protected by module_mutex AND modlist_lock */
  58. static DEFINE_MUTEX(module_mutex);
  59. static LIST_HEAD(modules);
  60. static BLOCKING_NOTIFIER_HEAD(module_notify_list);
  61. int register_module_notifier(struct notifier_block * nb)
  62. {
  63. return blocking_notifier_chain_register(&module_notify_list, nb);
  64. }
  65. EXPORT_SYMBOL(register_module_notifier);
  66. int unregister_module_notifier(struct notifier_block * nb)
  67. {
  68. return blocking_notifier_chain_unregister(&module_notify_list, nb);
  69. }
  70. EXPORT_SYMBOL(unregister_module_notifier);
  71. /* We require a truly strong try_module_get() */
  72. static inline int strong_try_module_get(struct module *mod)
  73. {
  74. if (mod && mod->state == MODULE_STATE_COMING)
  75. return 0;
  76. return try_module_get(mod);
  77. }
  78. /* A thread that wants to hold a reference to a module only while it
  79. * is running can call ths to safely exit.
  80. * nfsd and lockd use this.
  81. */
  82. void __module_put_and_exit(struct module *mod, long code)
  83. {
  84. module_put(mod);
  85. do_exit(code);
  86. }
  87. EXPORT_SYMBOL(__module_put_and_exit);
  88. /* Find a module section: 0 means not found. */
  89. static unsigned int find_sec(Elf_Ehdr *hdr,
  90. Elf_Shdr *sechdrs,
  91. const char *secstrings,
  92. const char *name)
  93. {
  94. unsigned int i;
  95. for (i = 1; i < hdr->e_shnum; i++)
  96. /* Alloc bit cleared means "ignore it." */
  97. if ((sechdrs[i].sh_flags & SHF_ALLOC)
  98. && strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
  99. return i;
  100. return 0;
  101. }
  102. /* Provided by the linker */
  103. extern const struct kernel_symbol __start___ksymtab[];
  104. extern const struct kernel_symbol __stop___ksymtab[];
  105. extern const struct kernel_symbol __start___ksymtab_gpl[];
  106. extern const struct kernel_symbol __stop___ksymtab_gpl[];
  107. extern const struct kernel_symbol __start___ksymtab_gpl_future[];
  108. extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
  109. extern const struct kernel_symbol __start___ksymtab_unused[];
  110. extern const struct kernel_symbol __stop___ksymtab_unused[];
  111. extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
  112. extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
  113. extern const struct kernel_symbol __start___ksymtab_gpl_future[];
  114. extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
  115. extern const unsigned long __start___kcrctab[];
  116. extern const unsigned long __start___kcrctab_gpl[];
  117. extern const unsigned long __start___kcrctab_gpl_future[];
  118. extern const unsigned long __start___kcrctab_unused[];
  119. extern const unsigned long __start___kcrctab_unused_gpl[];
  120. #ifndef CONFIG_MODVERSIONS
  121. #define symversion(base, idx) NULL
  122. #else
  123. #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
  124. #endif
  125. /* lookup symbol in given range of kernel_symbols */
  126. static const struct kernel_symbol *lookup_symbol(const char *name,
  127. const struct kernel_symbol *start,
  128. const struct kernel_symbol *stop)
  129. {
  130. const struct kernel_symbol *ks = start;
  131. for (; ks < stop; ks++)
  132. if (strcmp(ks->name, name) == 0)
  133. return ks;
  134. return NULL;
  135. }
  136. static void printk_unused_warning(const char *name)
  137. {
  138. printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
  139. "however this module is using it.\n", name);
  140. printk(KERN_WARNING "This symbol will go away in the future.\n");
  141. printk(KERN_WARNING "Please evalute if this is the right api to use, "
  142. "and if it really is, submit a report the linux kernel "
  143. "mailinglist together with submitting your code for "
  144. "inclusion.\n");
  145. }
  146. /* Find a symbol, return value, crc and module which owns it */
  147. static unsigned long __find_symbol(const char *name,
  148. struct module **owner,
  149. const unsigned long **crc,
  150. int gplok)
  151. {
  152. struct module *mod;
  153. const struct kernel_symbol *ks;
  154. /* Core kernel first. */
  155. *owner = NULL;
  156. ks = lookup_symbol(name, __start___ksymtab, __stop___ksymtab);
  157. if (ks) {
  158. *crc = symversion(__start___kcrctab, (ks - __start___ksymtab));
  159. return ks->value;
  160. }
  161. if (gplok) {
  162. ks = lookup_symbol(name, __start___ksymtab_gpl,
  163. __stop___ksymtab_gpl);
  164. if (ks) {
  165. *crc = symversion(__start___kcrctab_gpl,
  166. (ks - __start___ksymtab_gpl));
  167. return ks->value;
  168. }
  169. }
  170. ks = lookup_symbol(name, __start___ksymtab_gpl_future,
  171. __stop___ksymtab_gpl_future);
  172. if (ks) {
  173. if (!gplok) {
  174. printk(KERN_WARNING "Symbol %s is being used "
  175. "by a non-GPL module, which will not "
  176. "be allowed in the future\n", name);
  177. printk(KERN_WARNING "Please see the file "
  178. "Documentation/feature-removal-schedule.txt "
  179. "in the kernel source tree for more "
  180. "details.\n");
  181. }
  182. *crc = symversion(__start___kcrctab_gpl_future,
  183. (ks - __start___ksymtab_gpl_future));
  184. return ks->value;
  185. }
  186. ks = lookup_symbol(name, __start___ksymtab_unused,
  187. __stop___ksymtab_unused);
  188. if (ks) {
  189. printk_unused_warning(name);
  190. *crc = symversion(__start___kcrctab_unused,
  191. (ks - __start___ksymtab_unused));
  192. return ks->value;
  193. }
  194. if (gplok)
  195. ks = lookup_symbol(name, __start___ksymtab_unused_gpl,
  196. __stop___ksymtab_unused_gpl);
  197. if (ks) {
  198. printk_unused_warning(name);
  199. *crc = symversion(__start___kcrctab_unused_gpl,
  200. (ks - __start___ksymtab_unused_gpl));
  201. return ks->value;
  202. }
  203. /* Now try modules. */
  204. list_for_each_entry(mod, &modules, list) {
  205. *owner = mod;
  206. ks = lookup_symbol(name, mod->syms, mod->syms + mod->num_syms);
  207. if (ks) {
  208. *crc = symversion(mod->crcs, (ks - mod->syms));
  209. return ks->value;
  210. }
  211. if (gplok) {
  212. ks = lookup_symbol(name, mod->gpl_syms,
  213. mod->gpl_syms + mod->num_gpl_syms);
  214. if (ks) {
  215. *crc = symversion(mod->gpl_crcs,
  216. (ks - mod->gpl_syms));
  217. return ks->value;
  218. }
  219. }
  220. ks = lookup_symbol(name, mod->unused_syms, mod->unused_syms + mod->num_unused_syms);
  221. if (ks) {
  222. printk_unused_warning(name);
  223. *crc = symversion(mod->unused_crcs, (ks - mod->unused_syms));
  224. return ks->value;
  225. }
  226. if (gplok) {
  227. ks = lookup_symbol(name, mod->unused_gpl_syms,
  228. mod->unused_gpl_syms + mod->num_unused_gpl_syms);
  229. if (ks) {
  230. printk_unused_warning(name);
  231. *crc = symversion(mod->unused_gpl_crcs,
  232. (ks - mod->unused_gpl_syms));
  233. return ks->value;
  234. }
  235. }
  236. ks = lookup_symbol(name, mod->gpl_future_syms,
  237. (mod->gpl_future_syms +
  238. mod->num_gpl_future_syms));
  239. if (ks) {
  240. if (!gplok) {
  241. printk(KERN_WARNING "Symbol %s is being used "
  242. "by a non-GPL module, which will not "
  243. "be allowed in the future\n", name);
  244. printk(KERN_WARNING "Please see the file "
  245. "Documentation/feature-removal-schedule.txt "
  246. "in the kernel source tree for more "
  247. "details.\n");
  248. }
  249. *crc = symversion(mod->gpl_future_crcs,
  250. (ks - mod->gpl_future_syms));
  251. return ks->value;
  252. }
  253. }
  254. DEBUGP("Failed to find symbol %s\n", name);
  255. return 0;
  256. }
  257. /* Search for module by name: must hold module_mutex. */
  258. static struct module *find_module(const char *name)
  259. {
  260. struct module *mod;
  261. list_for_each_entry(mod, &modules, list) {
  262. if (strcmp(mod->name, name) == 0)
  263. return mod;
  264. }
  265. return NULL;
  266. }
  267. #ifdef CONFIG_SMP
  268. /* Number of blocks used and allocated. */
  269. static unsigned int pcpu_num_used, pcpu_num_allocated;
  270. /* Size of each block. -ve means used. */
  271. static int *pcpu_size;
  272. static int split_block(unsigned int i, unsigned short size)
  273. {
  274. /* Reallocation required? */
  275. if (pcpu_num_used + 1 > pcpu_num_allocated) {
  276. int *new = kmalloc(sizeof(new[0]) * pcpu_num_allocated*2,
  277. GFP_KERNEL);
  278. if (!new)
  279. return 0;
  280. memcpy(new, pcpu_size, sizeof(new[0])*pcpu_num_allocated);
  281. pcpu_num_allocated *= 2;
  282. kfree(pcpu_size);
  283. pcpu_size = new;
  284. }
  285. /* Insert a new subblock */
  286. memmove(&pcpu_size[i+1], &pcpu_size[i],
  287. sizeof(pcpu_size[0]) * (pcpu_num_used - i));
  288. pcpu_num_used++;
  289. pcpu_size[i+1] -= size;
  290. pcpu_size[i] = size;
  291. return 1;
  292. }
  293. static inline unsigned int block_size(int val)
  294. {
  295. if (val < 0)
  296. return -val;
  297. return val;
  298. }
  299. /* Created by linker magic */
  300. extern char __per_cpu_start[], __per_cpu_end[];
  301. static void *percpu_modalloc(unsigned long size, unsigned long align,
  302. const char *name)
  303. {
  304. unsigned long extra;
  305. unsigned int i;
  306. void *ptr;
  307. if (align > SMP_CACHE_BYTES) {
  308. printk(KERN_WARNING "%s: per-cpu alignment %li > %i\n",
  309. name, align, SMP_CACHE_BYTES);
  310. align = SMP_CACHE_BYTES;
  311. }
  312. ptr = __per_cpu_start;
  313. for (i = 0; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
  314. /* Extra for alignment requirement. */
  315. extra = ALIGN((unsigned long)ptr, align) - (unsigned long)ptr;
  316. BUG_ON(i == 0 && extra != 0);
  317. if (pcpu_size[i] < 0 || pcpu_size[i] < extra + size)
  318. continue;
  319. /* Transfer extra to previous block. */
  320. if (pcpu_size[i-1] < 0)
  321. pcpu_size[i-1] -= extra;
  322. else
  323. pcpu_size[i-1] += extra;
  324. pcpu_size[i] -= extra;
  325. ptr += extra;
  326. /* Split block if warranted */
  327. if (pcpu_size[i] - size > sizeof(unsigned long))
  328. if (!split_block(i, size))
  329. return NULL;
  330. /* Mark allocated */
  331. pcpu_size[i] = -pcpu_size[i];
  332. return ptr;
  333. }
  334. printk(KERN_WARNING "Could not allocate %lu bytes percpu data\n",
  335. size);
  336. return NULL;
  337. }
  338. static void percpu_modfree(void *freeme)
  339. {
  340. unsigned int i;
  341. void *ptr = __per_cpu_start + block_size(pcpu_size[0]);
  342. /* First entry is core kernel percpu data. */
  343. for (i = 1; i < pcpu_num_used; ptr += block_size(pcpu_size[i]), i++) {
  344. if (ptr == freeme) {
  345. pcpu_size[i] = -pcpu_size[i];
  346. goto free;
  347. }
  348. }
  349. BUG();
  350. free:
  351. /* Merge with previous? */
  352. if (pcpu_size[i-1] >= 0) {
  353. pcpu_size[i-1] += pcpu_size[i];
  354. pcpu_num_used--;
  355. memmove(&pcpu_size[i], &pcpu_size[i+1],
  356. (pcpu_num_used - i) * sizeof(pcpu_size[0]));
  357. i--;
  358. }
  359. /* Merge with next? */
  360. if (i+1 < pcpu_num_used && pcpu_size[i+1] >= 0) {
  361. pcpu_size[i] += pcpu_size[i+1];
  362. pcpu_num_used--;
  363. memmove(&pcpu_size[i+1], &pcpu_size[i+2],
  364. (pcpu_num_used - (i+1)) * sizeof(pcpu_size[0]));
  365. }
  366. }
  367. static unsigned int find_pcpusec(Elf_Ehdr *hdr,
  368. Elf_Shdr *sechdrs,
  369. const char *secstrings)
  370. {
  371. return find_sec(hdr, sechdrs, secstrings, ".data.percpu");
  372. }
  373. static int percpu_modinit(void)
  374. {
  375. pcpu_num_used = 2;
  376. pcpu_num_allocated = 2;
  377. pcpu_size = kmalloc(sizeof(pcpu_size[0]) * pcpu_num_allocated,
  378. GFP_KERNEL);
  379. /* Static in-kernel percpu data (used). */
  380. pcpu_size[0] = -ALIGN(__per_cpu_end-__per_cpu_start, SMP_CACHE_BYTES);
  381. /* Free room. */
  382. pcpu_size[1] = PERCPU_ENOUGH_ROOM + pcpu_size[0];
  383. if (pcpu_size[1] < 0) {
  384. printk(KERN_ERR "No per-cpu room for modules.\n");
  385. pcpu_num_used = 1;
  386. }
  387. return 0;
  388. }
  389. __initcall(percpu_modinit);
  390. #else /* ... !CONFIG_SMP */
  391. static inline void *percpu_modalloc(unsigned long size, unsigned long align,
  392. const char *name)
  393. {
  394. return NULL;
  395. }
  396. static inline void percpu_modfree(void *pcpuptr)
  397. {
  398. BUG();
  399. }
  400. static inline unsigned int find_pcpusec(Elf_Ehdr *hdr,
  401. Elf_Shdr *sechdrs,
  402. const char *secstrings)
  403. {
  404. return 0;
  405. }
  406. static inline void percpu_modcopy(void *pcpudst, const void *src,
  407. unsigned long size)
  408. {
  409. /* pcpusec should be 0, and size of that section should be 0. */
  410. BUG_ON(size != 0);
  411. }
  412. #endif /* CONFIG_SMP */
  413. #define MODINFO_ATTR(field) \
  414. static void setup_modinfo_##field(struct module *mod, const char *s) \
  415. { \
  416. mod->field = kstrdup(s, GFP_KERNEL); \
  417. } \
  418. static ssize_t show_modinfo_##field(struct module_attribute *mattr, \
  419. struct module *mod, char *buffer) \
  420. { \
  421. return sprintf(buffer, "%s\n", mod->field); \
  422. } \
  423. static int modinfo_##field##_exists(struct module *mod) \
  424. { \
  425. return mod->field != NULL; \
  426. } \
  427. static void free_modinfo_##field(struct module *mod) \
  428. { \
  429. kfree(mod->field); \
  430. mod->field = NULL; \
  431. } \
  432. static struct module_attribute modinfo_##field = { \
  433. .attr = { .name = __stringify(field), .mode = 0444, \
  434. .owner = THIS_MODULE }, \
  435. .show = show_modinfo_##field, \
  436. .setup = setup_modinfo_##field, \
  437. .test = modinfo_##field##_exists, \
  438. .free = free_modinfo_##field, \
  439. };
  440. MODINFO_ATTR(version);
  441. MODINFO_ATTR(srcversion);
  442. #ifdef CONFIG_MODULE_UNLOAD
  443. /* Init the unload section of the module. */
  444. static void module_unload_init(struct module *mod)
  445. {
  446. unsigned int i;
  447. INIT_LIST_HEAD(&mod->modules_which_use_me);
  448. for (i = 0; i < NR_CPUS; i++)
  449. local_set(&mod->ref[i].count, 0);
  450. /* Hold reference count during initialization. */
  451. local_set(&mod->ref[raw_smp_processor_id()].count, 1);
  452. /* Backwards compatibility macros put refcount during init. */
  453. mod->waiter = current;
  454. }
  455. /* modules using other modules */
  456. struct module_use
  457. {
  458. struct list_head list;
  459. struct module *module_which_uses;
  460. };
  461. /* Does a already use b? */
  462. static int already_uses(struct module *a, struct module *b)
  463. {
  464. struct module_use *use;
  465. list_for_each_entry(use, &b->modules_which_use_me, list) {
  466. if (use->module_which_uses == a) {
  467. DEBUGP("%s uses %s!\n", a->name, b->name);
  468. return 1;
  469. }
  470. }
  471. DEBUGP("%s does not use %s!\n", a->name, b->name);
  472. return 0;
  473. }
  474. /* Module a uses b */
  475. static int use_module(struct module *a, struct module *b)
  476. {
  477. struct module_use *use;
  478. if (b == NULL || already_uses(a, b)) return 1;
  479. if (!strong_try_module_get(b))
  480. return 0;
  481. DEBUGP("Allocating new usage for %s.\n", a->name);
  482. use = kmalloc(sizeof(*use), GFP_ATOMIC);
  483. if (!use) {
  484. printk("%s: out of memory loading\n", a->name);
  485. module_put(b);
  486. return 0;
  487. }
  488. use->module_which_uses = a;
  489. list_add(&use->list, &b->modules_which_use_me);
  490. return 1;
  491. }
  492. /* Clear the unload stuff of the module. */
  493. static void module_unload_free(struct module *mod)
  494. {
  495. struct module *i;
  496. list_for_each_entry(i, &modules, list) {
  497. struct module_use *use;
  498. list_for_each_entry(use, &i->modules_which_use_me, list) {
  499. if (use->module_which_uses == mod) {
  500. DEBUGP("%s unusing %s\n", mod->name, i->name);
  501. module_put(i);
  502. list_del(&use->list);
  503. kfree(use);
  504. /* There can be at most one match. */
  505. break;
  506. }
  507. }
  508. }
  509. }
  510. #ifdef CONFIG_MODULE_FORCE_UNLOAD
  511. static inline int try_force_unload(unsigned int flags)
  512. {
  513. int ret = (flags & O_TRUNC);
  514. if (ret)
  515. add_taint(TAINT_FORCED_RMMOD);
  516. return ret;
  517. }
  518. #else
  519. static inline int try_force_unload(unsigned int flags)
  520. {
  521. return 0;
  522. }
  523. #endif /* CONFIG_MODULE_FORCE_UNLOAD */
  524. struct stopref
  525. {
  526. struct module *mod;
  527. int flags;
  528. int *forced;
  529. };
  530. /* Whole machine is stopped with interrupts off when this runs. */
  531. static int __try_stop_module(void *_sref)
  532. {
  533. struct stopref *sref = _sref;
  534. /* If it's not unused, quit unless we are told to block. */
  535. if ((sref->flags & O_NONBLOCK) && module_refcount(sref->mod) != 0) {
  536. if (!(*sref->forced = try_force_unload(sref->flags)))
  537. return -EWOULDBLOCK;
  538. }
  539. /* Mark it as dying. */
  540. sref->mod->state = MODULE_STATE_GOING;
  541. return 0;
  542. }
  543. static int try_stop_module(struct module *mod, int flags, int *forced)
  544. {
  545. struct stopref sref = { mod, flags, forced };
  546. return stop_machine_run(__try_stop_module, &sref, NR_CPUS);
  547. }
  548. unsigned int module_refcount(struct module *mod)
  549. {
  550. unsigned int i, total = 0;
  551. for (i = 0; i < NR_CPUS; i++)
  552. total += local_read(&mod->ref[i].count);
  553. return total;
  554. }
  555. EXPORT_SYMBOL(module_refcount);
  556. /* This exists whether we can unload or not */
  557. static void free_module(struct module *mod);
  558. static void wait_for_zero_refcount(struct module *mod)
  559. {
  560. /* Since we might sleep for some time, drop the semaphore first */
  561. mutex_unlock(&module_mutex);
  562. for (;;) {
  563. DEBUGP("Looking at refcount...\n");
  564. set_current_state(TASK_UNINTERRUPTIBLE);
  565. if (module_refcount(mod) == 0)
  566. break;
  567. schedule();
  568. }
  569. current->state = TASK_RUNNING;
  570. mutex_lock(&module_mutex);
  571. }
  572. asmlinkage long
  573. sys_delete_module(const char __user *name_user, unsigned int flags)
  574. {
  575. struct module *mod;
  576. char name[MODULE_NAME_LEN];
  577. int ret, forced = 0;
  578. if (!capable(CAP_SYS_MODULE))
  579. return -EPERM;
  580. if (strncpy_from_user(name, name_user, MODULE_NAME_LEN-1) < 0)
  581. return -EFAULT;
  582. name[MODULE_NAME_LEN-1] = '\0';
  583. if (mutex_lock_interruptible(&module_mutex) != 0)
  584. return -EINTR;
  585. mod = find_module(name);
  586. if (!mod) {
  587. ret = -ENOENT;
  588. goto out;
  589. }
  590. if (!list_empty(&mod->modules_which_use_me)) {
  591. /* Other modules depend on us: get rid of them first. */
  592. ret = -EWOULDBLOCK;
  593. goto out;
  594. }
  595. /* Doing init or already dying? */
  596. if (mod->state != MODULE_STATE_LIVE) {
  597. /* FIXME: if (force), slam module count and wake up
  598. waiter --RR */
  599. DEBUGP("%s already dying\n", mod->name);
  600. ret = -EBUSY;
  601. goto out;
  602. }
  603. /* If it has an init func, it must have an exit func to unload */
  604. if ((mod->init != NULL && mod->exit == NULL)
  605. || mod->unsafe) {
  606. forced = try_force_unload(flags);
  607. if (!forced) {
  608. /* This module can't be removed */
  609. ret = -EBUSY;
  610. goto out;
  611. }
  612. }
  613. /* Set this up before setting mod->state */
  614. mod->waiter = current;
  615. /* Stop the machine so refcounts can't move and disable module. */
  616. ret = try_stop_module(mod, flags, &forced);
  617. if (ret != 0)
  618. goto out;
  619. /* Never wait if forced. */
  620. if (!forced && module_refcount(mod) != 0)
  621. wait_for_zero_refcount(mod);
  622. /* Final destruction now noone is using it. */
  623. if (mod->exit != NULL) {
  624. mutex_unlock(&module_mutex);
  625. mod->exit();
  626. mutex_lock(&module_mutex);
  627. }
  628. free_module(mod);
  629. out:
  630. mutex_unlock(&module_mutex);
  631. return ret;
  632. }
  633. static void print_unload_info(struct seq_file *m, struct module *mod)
  634. {
  635. struct module_use *use;
  636. int printed_something = 0;
  637. seq_printf(m, " %u ", module_refcount(mod));
  638. /* Always include a trailing , so userspace can differentiate
  639. between this and the old multi-field proc format. */
  640. list_for_each_entry(use, &mod->modules_which_use_me, list) {
  641. printed_something = 1;
  642. seq_printf(m, "%s,", use->module_which_uses->name);
  643. }
  644. if (mod->unsafe) {
  645. printed_something = 1;
  646. seq_printf(m, "[unsafe],");
  647. }
  648. if (mod->init != NULL && mod->exit == NULL) {
  649. printed_something = 1;
  650. seq_printf(m, "[permanent],");
  651. }
  652. if (!printed_something)
  653. seq_printf(m, "-");
  654. }
  655. void __symbol_put(const char *symbol)
  656. {
  657. struct module *owner;
  658. unsigned long flags;
  659. const unsigned long *crc;
  660. spin_lock_irqsave(&modlist_lock, flags);
  661. if (!__find_symbol(symbol, &owner, &crc, 1))
  662. BUG();
  663. module_put(owner);
  664. spin_unlock_irqrestore(&modlist_lock, flags);
  665. }
  666. EXPORT_SYMBOL(__symbol_put);
  667. void symbol_put_addr(void *addr)
  668. {
  669. struct module *modaddr;
  670. if (core_kernel_text((unsigned long)addr))
  671. return;
  672. if (!(modaddr = module_text_address((unsigned long)addr)))
  673. BUG();
  674. module_put(modaddr);
  675. }
  676. EXPORT_SYMBOL_GPL(symbol_put_addr);
  677. static ssize_t show_refcnt(struct module_attribute *mattr,
  678. struct module *mod, char *buffer)
  679. {
  680. /* sysfs holds a reference */
  681. return sprintf(buffer, "%u\n", module_refcount(mod)-1);
  682. }
  683. static struct module_attribute refcnt = {
  684. .attr = { .name = "refcnt", .mode = 0444, .owner = THIS_MODULE },
  685. .show = show_refcnt,
  686. };
  687. #else /* !CONFIG_MODULE_UNLOAD */
  688. static void print_unload_info(struct seq_file *m, struct module *mod)
  689. {
  690. /* We don't know the usage count, or what modules are using. */
  691. seq_printf(m, " - -");
  692. }
  693. static inline void module_unload_free(struct module *mod)
  694. {
  695. }
  696. static inline int use_module(struct module *a, struct module *b)
  697. {
  698. return strong_try_module_get(b);
  699. }
  700. static inline void module_unload_init(struct module *mod)
  701. {
  702. }
  703. #endif /* CONFIG_MODULE_UNLOAD */
  704. static struct module_attribute *modinfo_attrs[] = {
  705. &modinfo_version,
  706. &modinfo_srcversion,
  707. #ifdef CONFIG_MODULE_UNLOAD
  708. &refcnt,
  709. #endif
  710. NULL,
  711. };
  712. static const char vermagic[] = VERMAGIC_STRING;
  713. #ifdef CONFIG_MODVERSIONS
  714. static int check_version(Elf_Shdr *sechdrs,
  715. unsigned int versindex,
  716. const char *symname,
  717. struct module *mod,
  718. const unsigned long *crc)
  719. {
  720. unsigned int i, num_versions;
  721. struct modversion_info *versions;
  722. /* Exporting module didn't supply crcs? OK, we're already tainted. */
  723. if (!crc)
  724. return 1;
  725. versions = (void *) sechdrs[versindex].sh_addr;
  726. num_versions = sechdrs[versindex].sh_size
  727. / sizeof(struct modversion_info);
  728. for (i = 0; i < num_versions; i++) {
  729. if (strcmp(versions[i].name, symname) != 0)
  730. continue;
  731. if (versions[i].crc == *crc)
  732. return 1;
  733. printk("%s: disagrees about version of symbol %s\n",
  734. mod->name, symname);
  735. DEBUGP("Found checksum %lX vs module %lX\n",
  736. *crc, versions[i].crc);
  737. return 0;
  738. }
  739. /* Not in module's version table. OK, but that taints the kernel. */
  740. if (!(tainted & TAINT_FORCED_MODULE)) {
  741. printk("%s: no version for \"%s\" found: kernel tainted.\n",
  742. mod->name, symname);
  743. add_taint(TAINT_FORCED_MODULE);
  744. }
  745. return 1;
  746. }
  747. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  748. unsigned int versindex,
  749. struct module *mod)
  750. {
  751. const unsigned long *crc;
  752. struct module *owner;
  753. if (!__find_symbol("struct_module", &owner, &crc, 1))
  754. BUG();
  755. return check_version(sechdrs, versindex, "struct_module", mod,
  756. crc);
  757. }
  758. /* First part is kernel version, which we ignore. */
  759. static inline int same_magic(const char *amagic, const char *bmagic)
  760. {
  761. amagic += strcspn(amagic, " ");
  762. bmagic += strcspn(bmagic, " ");
  763. return strcmp(amagic, bmagic) == 0;
  764. }
  765. #else
  766. static inline int check_version(Elf_Shdr *sechdrs,
  767. unsigned int versindex,
  768. const char *symname,
  769. struct module *mod,
  770. const unsigned long *crc)
  771. {
  772. return 1;
  773. }
  774. static inline int check_modstruct_version(Elf_Shdr *sechdrs,
  775. unsigned int versindex,
  776. struct module *mod)
  777. {
  778. return 1;
  779. }
  780. static inline int same_magic(const char *amagic, const char *bmagic)
  781. {
  782. return strcmp(amagic, bmagic) == 0;
  783. }
  784. #endif /* CONFIG_MODVERSIONS */
  785. /* Resolve a symbol for this module. I.e. if we find one, record usage.
  786. Must be holding module_mutex. */
  787. static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
  788. unsigned int versindex,
  789. const char *name,
  790. struct module *mod)
  791. {
  792. struct module *owner;
  793. unsigned long ret;
  794. const unsigned long *crc;
  795. ret = __find_symbol(name, &owner, &crc, mod->license_gplok);
  796. if (ret) {
  797. /* use_module can fail due to OOM, or module unloading */
  798. if (!check_version(sechdrs, versindex, name, mod, crc) ||
  799. !use_module(mod, owner))
  800. ret = 0;
  801. }
  802. return ret;
  803. }
  804. /*
  805. * /sys/module/foo/sections stuff
  806. * J. Corbet <corbet@lwn.net>
  807. */
  808. #ifdef CONFIG_KALLSYMS
  809. static ssize_t module_sect_show(struct module_attribute *mattr,
  810. struct module *mod, char *buf)
  811. {
  812. struct module_sect_attr *sattr =
  813. container_of(mattr, struct module_sect_attr, mattr);
  814. return sprintf(buf, "0x%lx\n", sattr->address);
  815. }
  816. static void add_sect_attrs(struct module *mod, unsigned int nsect,
  817. char *secstrings, Elf_Shdr *sechdrs)
  818. {
  819. unsigned int nloaded = 0, i, size[2];
  820. struct module_sect_attrs *sect_attrs;
  821. struct module_sect_attr *sattr;
  822. struct attribute **gattr;
  823. /* Count loaded sections and allocate structures */
  824. for (i = 0; i < nsect; i++)
  825. if (sechdrs[i].sh_flags & SHF_ALLOC)
  826. nloaded++;
  827. size[0] = ALIGN(sizeof(*sect_attrs)
  828. + nloaded * sizeof(sect_attrs->attrs[0]),
  829. sizeof(sect_attrs->grp.attrs[0]));
  830. size[1] = (nloaded + 1) * sizeof(sect_attrs->grp.attrs[0]);
  831. if (! (sect_attrs = kmalloc(size[0] + size[1], GFP_KERNEL)))
  832. return;
  833. /* Setup section attributes. */
  834. sect_attrs->grp.name = "sections";
  835. sect_attrs->grp.attrs = (void *)sect_attrs + size[0];
  836. sattr = &sect_attrs->attrs[0];
  837. gattr = &sect_attrs->grp.attrs[0];
  838. for (i = 0; i < nsect; i++) {
  839. if (! (sechdrs[i].sh_flags & SHF_ALLOC))
  840. continue;
  841. sattr->address = sechdrs[i].sh_addr;
  842. strlcpy(sattr->name, secstrings + sechdrs[i].sh_name,
  843. MODULE_SECT_NAME_LEN);
  844. sattr->mattr.show = module_sect_show;
  845. sattr->mattr.store = NULL;
  846. sattr->mattr.attr.name = sattr->name;
  847. sattr->mattr.attr.owner = mod;
  848. sattr->mattr.attr.mode = S_IRUGO;
  849. *(gattr++) = &(sattr++)->mattr.attr;
  850. }
  851. *gattr = NULL;
  852. if (sysfs_create_group(&mod->mkobj.kobj, &sect_attrs->grp))
  853. goto out;
  854. mod->sect_attrs = sect_attrs;
  855. return;
  856. out:
  857. kfree(sect_attrs);
  858. }
  859. static void remove_sect_attrs(struct module *mod)
  860. {
  861. if (mod->sect_attrs) {
  862. sysfs_remove_group(&mod->mkobj.kobj,
  863. &mod->sect_attrs->grp);
  864. /* We are positive that no one is using any sect attrs
  865. * at this point. Deallocate immediately. */
  866. kfree(mod->sect_attrs);
  867. mod->sect_attrs = NULL;
  868. }
  869. }
  870. #else
  871. static inline void add_sect_attrs(struct module *mod, unsigned int nsect,
  872. char *sectstrings, Elf_Shdr *sechdrs)
  873. {
  874. }
  875. static inline void remove_sect_attrs(struct module *mod)
  876. {
  877. }
  878. #endif /* CONFIG_KALLSYMS */
  879. static int module_add_modinfo_attrs(struct module *mod)
  880. {
  881. struct module_attribute *attr;
  882. struct module_attribute *temp_attr;
  883. int error = 0;
  884. int i;
  885. mod->modinfo_attrs = kzalloc((sizeof(struct module_attribute) *
  886. (ARRAY_SIZE(modinfo_attrs) + 1)),
  887. GFP_KERNEL);
  888. if (!mod->modinfo_attrs)
  889. return -ENOMEM;
  890. temp_attr = mod->modinfo_attrs;
  891. for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) {
  892. if (!attr->test ||
  893. (attr->test && attr->test(mod))) {
  894. memcpy(temp_attr, attr, sizeof(*temp_attr));
  895. temp_attr->attr.owner = mod;
  896. error = sysfs_create_file(&mod->mkobj.kobj,&temp_attr->attr);
  897. ++temp_attr;
  898. }
  899. }
  900. return error;
  901. }
  902. static void module_remove_modinfo_attrs(struct module *mod)
  903. {
  904. struct module_attribute *attr;
  905. int i;
  906. for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) {
  907. /* pick a field to test for end of list */
  908. if (!attr->attr.name)
  909. break;
  910. sysfs_remove_file(&mod->mkobj.kobj,&attr->attr);
  911. if (attr->free)
  912. attr->free(mod);
  913. }
  914. kfree(mod->modinfo_attrs);
  915. }
  916. static int mod_sysfs_setup(struct module *mod,
  917. struct kernel_param *kparam,
  918. unsigned int num_params)
  919. {
  920. int err;
  921. memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj));
  922. err = kobject_set_name(&mod->mkobj.kobj, "%s", mod->name);
  923. if (err)
  924. goto out;
  925. kobj_set_kset_s(&mod->mkobj, module_subsys);
  926. mod->mkobj.mod = mod;
  927. err = kobject_register(&mod->mkobj.kobj);
  928. if (err)
  929. goto out;
  930. err = module_param_sysfs_setup(mod, kparam, num_params);
  931. if (err)
  932. goto out_unreg;
  933. err = module_add_modinfo_attrs(mod);
  934. if (err)
  935. goto out_unreg;
  936. return 0;
  937. out_unreg:
  938. kobject_unregister(&mod->mkobj.kobj);
  939. out:
  940. return err;
  941. }
  942. static void mod_kobject_remove(struct module *mod)
  943. {
  944. module_remove_modinfo_attrs(mod);
  945. module_param_sysfs_remove(mod);
  946. kobject_unregister(&mod->mkobj.kobj);
  947. }
  948. /*
  949. * unlink the module with the whole machine is stopped with interrupts off
  950. * - this defends against kallsyms not taking locks
  951. */
  952. static int __unlink_module(void *_mod)
  953. {
  954. struct module *mod = _mod;
  955. list_del(&mod->list);
  956. return 0;
  957. }
  958. /* Free a module, remove from lists, etc (must hold module mutex). */
  959. static void free_module(struct module *mod)
  960. {
  961. /* Delete from various lists */
  962. stop_machine_run(__unlink_module, mod, NR_CPUS);
  963. remove_sect_attrs(mod);
  964. mod_kobject_remove(mod);
  965. unwind_remove_table(mod->unwind_info, 0);
  966. /* Arch-specific cleanup. */
  967. module_arch_cleanup(mod);
  968. /* Module unload stuff */
  969. module_unload_free(mod);
  970. /* This may be NULL, but that's OK */
  971. module_free(mod, mod->module_init);
  972. kfree(mod->args);
  973. if (mod->percpu)
  974. percpu_modfree(mod->percpu);
  975. /* Finally, free the core (containing the module structure) */
  976. module_free(mod, mod->module_core);
  977. }
  978. void *__symbol_get(const char *symbol)
  979. {
  980. struct module *owner;
  981. unsigned long value, flags;
  982. const unsigned long *crc;
  983. spin_lock_irqsave(&modlist_lock, flags);
  984. value = __find_symbol(symbol, &owner, &crc, 1);
  985. if (value && !strong_try_module_get(owner))
  986. value = 0;
  987. spin_unlock_irqrestore(&modlist_lock, flags);
  988. return (void *)value;
  989. }
  990. EXPORT_SYMBOL_GPL(__symbol_get);
  991. /*
  992. * Ensure that an exported symbol [global namespace] does not already exist
  993. * in the Kernel or in some other modules exported symbol table.
  994. */
  995. static int verify_export_symbols(struct module *mod)
  996. {
  997. const char *name = NULL;
  998. unsigned long i, ret = 0;
  999. struct module *owner;
  1000. const unsigned long *crc;
  1001. for (i = 0; i < mod->num_syms; i++)
  1002. if (__find_symbol(mod->syms[i].name, &owner, &crc, 1)) {
  1003. name = mod->syms[i].name;
  1004. ret = -ENOEXEC;
  1005. goto dup;
  1006. }
  1007. for (i = 0; i < mod->num_gpl_syms; i++)
  1008. if (__find_symbol(mod->gpl_syms[i].name, &owner, &crc, 1)) {
  1009. name = mod->gpl_syms[i].name;
  1010. ret = -ENOEXEC;
  1011. goto dup;
  1012. }
  1013. dup:
  1014. if (ret)
  1015. printk(KERN_ERR "%s: exports duplicate symbol %s (owned by %s)\n",
  1016. mod->name, name, module_name(owner));
  1017. return ret;
  1018. }
  1019. /* Change all symbols so that sh_value encodes the pointer directly. */
  1020. static int simplify_symbols(Elf_Shdr *sechdrs,
  1021. unsigned int symindex,
  1022. const char *strtab,
  1023. unsigned int versindex,
  1024. unsigned int pcpuindex,
  1025. struct module *mod)
  1026. {
  1027. Elf_Sym *sym = (void *)sechdrs[symindex].sh_addr;
  1028. unsigned long secbase;
  1029. unsigned int i, n = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1030. int ret = 0;
  1031. for (i = 1; i < n; i++) {
  1032. switch (sym[i].st_shndx) {
  1033. case SHN_COMMON:
  1034. /* We compiled with -fno-common. These are not
  1035. supposed to happen. */
  1036. DEBUGP("Common symbol: %s\n", strtab + sym[i].st_name);
  1037. printk("%s: please compile with -fno-common\n",
  1038. mod->name);
  1039. ret = -ENOEXEC;
  1040. break;
  1041. case SHN_ABS:
  1042. /* Don't need to do anything */
  1043. DEBUGP("Absolute symbol: 0x%08lx\n",
  1044. (long)sym[i].st_value);
  1045. break;
  1046. case SHN_UNDEF:
  1047. sym[i].st_value
  1048. = resolve_symbol(sechdrs, versindex,
  1049. strtab + sym[i].st_name, mod);
  1050. /* Ok if resolved. */
  1051. if (sym[i].st_value != 0)
  1052. break;
  1053. /* Ok if weak. */
  1054. if (ELF_ST_BIND(sym[i].st_info) == STB_WEAK)
  1055. break;
  1056. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  1057. mod->name, strtab + sym[i].st_name);
  1058. ret = -ENOENT;
  1059. break;
  1060. default:
  1061. /* Divert to percpu allocation if a percpu var. */
  1062. if (sym[i].st_shndx == pcpuindex)
  1063. secbase = (unsigned long)mod->percpu;
  1064. else
  1065. secbase = sechdrs[sym[i].st_shndx].sh_addr;
  1066. sym[i].st_value += secbase;
  1067. break;
  1068. }
  1069. }
  1070. return ret;
  1071. }
  1072. /* Update size with this section: return offset. */
  1073. static long get_offset(unsigned long *size, Elf_Shdr *sechdr)
  1074. {
  1075. long ret;
  1076. ret = ALIGN(*size, sechdr->sh_addralign ?: 1);
  1077. *size = ret + sechdr->sh_size;
  1078. return ret;
  1079. }
  1080. /* Lay out the SHF_ALLOC sections in a way not dissimilar to how ld
  1081. might -- code, read-only data, read-write data, small data. Tally
  1082. sizes, and place the offsets into sh_entsize fields: high bit means it
  1083. belongs in init. */
  1084. static void layout_sections(struct module *mod,
  1085. const Elf_Ehdr *hdr,
  1086. Elf_Shdr *sechdrs,
  1087. const char *secstrings)
  1088. {
  1089. static unsigned long const masks[][2] = {
  1090. /* NOTE: all executable code must be the first section
  1091. * in this array; otherwise modify the text_size
  1092. * finder in the two loops below */
  1093. { SHF_EXECINSTR | SHF_ALLOC, ARCH_SHF_SMALL },
  1094. { SHF_ALLOC, SHF_WRITE | ARCH_SHF_SMALL },
  1095. { SHF_WRITE | SHF_ALLOC, ARCH_SHF_SMALL },
  1096. { ARCH_SHF_SMALL | SHF_ALLOC, 0 }
  1097. };
  1098. unsigned int m, i;
  1099. for (i = 0; i < hdr->e_shnum; i++)
  1100. sechdrs[i].sh_entsize = ~0UL;
  1101. DEBUGP("Core section allocation order:\n");
  1102. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1103. for (i = 0; i < hdr->e_shnum; ++i) {
  1104. Elf_Shdr *s = &sechdrs[i];
  1105. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1106. || (s->sh_flags & masks[m][1])
  1107. || s->sh_entsize != ~0UL
  1108. || strncmp(secstrings + s->sh_name,
  1109. ".init", 5) == 0)
  1110. continue;
  1111. s->sh_entsize = get_offset(&mod->core_size, s);
  1112. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1113. }
  1114. if (m == 0)
  1115. mod->core_text_size = mod->core_size;
  1116. }
  1117. DEBUGP("Init section allocation order:\n");
  1118. for (m = 0; m < ARRAY_SIZE(masks); ++m) {
  1119. for (i = 0; i < hdr->e_shnum; ++i) {
  1120. Elf_Shdr *s = &sechdrs[i];
  1121. if ((s->sh_flags & masks[m][0]) != masks[m][0]
  1122. || (s->sh_flags & masks[m][1])
  1123. || s->sh_entsize != ~0UL
  1124. || strncmp(secstrings + s->sh_name,
  1125. ".init", 5) != 0)
  1126. continue;
  1127. s->sh_entsize = (get_offset(&mod->init_size, s)
  1128. | INIT_OFFSET_MASK);
  1129. DEBUGP("\t%s\n", secstrings + s->sh_name);
  1130. }
  1131. if (m == 0)
  1132. mod->init_text_size = mod->init_size;
  1133. }
  1134. }
  1135. static void set_license(struct module *mod, const char *license)
  1136. {
  1137. if (!license)
  1138. license = "unspecified";
  1139. mod->license_gplok = license_is_gpl_compatible(license);
  1140. if (!mod->license_gplok && !(tainted & TAINT_PROPRIETARY_MODULE)) {
  1141. printk(KERN_WARNING "%s: module license '%s' taints kernel.\n",
  1142. mod->name, license);
  1143. add_taint(TAINT_PROPRIETARY_MODULE);
  1144. }
  1145. }
  1146. /* Parse tag=value strings from .modinfo section */
  1147. static char *next_string(char *string, unsigned long *secsize)
  1148. {
  1149. /* Skip non-zero chars */
  1150. while (string[0]) {
  1151. string++;
  1152. if ((*secsize)-- <= 1)
  1153. return NULL;
  1154. }
  1155. /* Skip any zero padding. */
  1156. while (!string[0]) {
  1157. string++;
  1158. if ((*secsize)-- <= 1)
  1159. return NULL;
  1160. }
  1161. return string;
  1162. }
  1163. static char *get_modinfo(Elf_Shdr *sechdrs,
  1164. unsigned int info,
  1165. const char *tag)
  1166. {
  1167. char *p;
  1168. unsigned int taglen = strlen(tag);
  1169. unsigned long size = sechdrs[info].sh_size;
  1170. for (p = (char *)sechdrs[info].sh_addr; p; p = next_string(p, &size)) {
  1171. if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
  1172. return p + taglen + 1;
  1173. }
  1174. return NULL;
  1175. }
  1176. static void setup_modinfo(struct module *mod, Elf_Shdr *sechdrs,
  1177. unsigned int infoindex)
  1178. {
  1179. struct module_attribute *attr;
  1180. int i;
  1181. for (i = 0; (attr = modinfo_attrs[i]); i++) {
  1182. if (attr->setup)
  1183. attr->setup(mod,
  1184. get_modinfo(sechdrs,
  1185. infoindex,
  1186. attr->attr.name));
  1187. }
  1188. }
  1189. #ifdef CONFIG_KALLSYMS
  1190. int is_exported(const char *name, const struct module *mod)
  1191. {
  1192. if (!mod && lookup_symbol(name, __start___ksymtab, __stop___ksymtab))
  1193. return 1;
  1194. else
  1195. if (mod && lookup_symbol(name, mod->syms, mod->syms + mod->num_syms))
  1196. return 1;
  1197. else
  1198. return 0;
  1199. }
  1200. /* As per nm */
  1201. static char elf_type(const Elf_Sym *sym,
  1202. Elf_Shdr *sechdrs,
  1203. const char *secstrings,
  1204. struct module *mod)
  1205. {
  1206. if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
  1207. if (ELF_ST_TYPE(sym->st_info) == STT_OBJECT)
  1208. return 'v';
  1209. else
  1210. return 'w';
  1211. }
  1212. if (sym->st_shndx == SHN_UNDEF)
  1213. return 'U';
  1214. if (sym->st_shndx == SHN_ABS)
  1215. return 'a';
  1216. if (sym->st_shndx >= SHN_LORESERVE)
  1217. return '?';
  1218. if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR)
  1219. return 't';
  1220. if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC
  1221. && sechdrs[sym->st_shndx].sh_type != SHT_NOBITS) {
  1222. if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE))
  1223. return 'r';
  1224. else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1225. return 'g';
  1226. else
  1227. return 'd';
  1228. }
  1229. if (sechdrs[sym->st_shndx].sh_type == SHT_NOBITS) {
  1230. if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL)
  1231. return 's';
  1232. else
  1233. return 'b';
  1234. }
  1235. if (strncmp(secstrings + sechdrs[sym->st_shndx].sh_name,
  1236. ".debug", strlen(".debug")) == 0)
  1237. return 'n';
  1238. return '?';
  1239. }
  1240. static void add_kallsyms(struct module *mod,
  1241. Elf_Shdr *sechdrs,
  1242. unsigned int symindex,
  1243. unsigned int strindex,
  1244. const char *secstrings)
  1245. {
  1246. unsigned int i;
  1247. mod->symtab = (void *)sechdrs[symindex].sh_addr;
  1248. mod->num_symtab = sechdrs[symindex].sh_size / sizeof(Elf_Sym);
  1249. mod->strtab = (void *)sechdrs[strindex].sh_addr;
  1250. /* Set types up while we still have access to sections. */
  1251. for (i = 0; i < mod->num_symtab; i++)
  1252. mod->symtab[i].st_info
  1253. = elf_type(&mod->symtab[i], sechdrs, secstrings, mod);
  1254. }
  1255. #else
  1256. static inline void add_kallsyms(struct module *mod,
  1257. Elf_Shdr *sechdrs,
  1258. unsigned int symindex,
  1259. unsigned int strindex,
  1260. const char *secstrings)
  1261. {
  1262. }
  1263. #endif /* CONFIG_KALLSYMS */
  1264. /* Allocate and load the module: note that size of section 0 is always
  1265. zero, and we rely on this for optional sections. */
  1266. static struct module *load_module(void __user *umod,
  1267. unsigned long len,
  1268. const char __user *uargs)
  1269. {
  1270. Elf_Ehdr *hdr;
  1271. Elf_Shdr *sechdrs;
  1272. char *secstrings, *args, *modmagic, *strtab = NULL;
  1273. unsigned int i;
  1274. unsigned int symindex = 0;
  1275. unsigned int strindex = 0;
  1276. unsigned int setupindex;
  1277. unsigned int exindex;
  1278. unsigned int exportindex;
  1279. unsigned int modindex;
  1280. unsigned int obsparmindex;
  1281. unsigned int infoindex;
  1282. unsigned int gplindex;
  1283. unsigned int crcindex;
  1284. unsigned int gplcrcindex;
  1285. unsigned int versindex;
  1286. unsigned int pcpuindex;
  1287. unsigned int gplfutureindex;
  1288. unsigned int gplfuturecrcindex;
  1289. unsigned int unwindex = 0;
  1290. unsigned int unusedindex;
  1291. unsigned int unusedcrcindex;
  1292. unsigned int unusedgplindex;
  1293. unsigned int unusedgplcrcindex;
  1294. struct module *mod;
  1295. long err = 0;
  1296. void *percpu = NULL, *ptr = NULL; /* Stops spurious gcc warning */
  1297. struct exception_table_entry *extable;
  1298. mm_segment_t old_fs;
  1299. DEBUGP("load_module: umod=%p, len=%lu, uargs=%p\n",
  1300. umod, len, uargs);
  1301. if (len < sizeof(*hdr))
  1302. return ERR_PTR(-ENOEXEC);
  1303. /* Suck in entire file: we'll want most of it. */
  1304. /* vmalloc barfs on "unusual" numbers. Check here */
  1305. if (len > 64 * 1024 * 1024 || (hdr = vmalloc(len)) == NULL)
  1306. return ERR_PTR(-ENOMEM);
  1307. if (copy_from_user(hdr, umod, len) != 0) {
  1308. err = -EFAULT;
  1309. goto free_hdr;
  1310. }
  1311. /* Sanity checks against insmoding binaries or wrong arch,
  1312. weird elf version */
  1313. if (memcmp(hdr->e_ident, ELFMAG, 4) != 0
  1314. || hdr->e_type != ET_REL
  1315. || !elf_check_arch(hdr)
  1316. || hdr->e_shentsize != sizeof(*sechdrs)) {
  1317. err = -ENOEXEC;
  1318. goto free_hdr;
  1319. }
  1320. if (len < hdr->e_shoff + hdr->e_shnum * sizeof(Elf_Shdr))
  1321. goto truncated;
  1322. /* Convenience variables */
  1323. sechdrs = (void *)hdr + hdr->e_shoff;
  1324. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  1325. sechdrs[0].sh_addr = 0;
  1326. for (i = 1; i < hdr->e_shnum; i++) {
  1327. if (sechdrs[i].sh_type != SHT_NOBITS
  1328. && len < sechdrs[i].sh_offset + sechdrs[i].sh_size)
  1329. goto truncated;
  1330. /* Mark all sections sh_addr with their address in the
  1331. temporary image. */
  1332. sechdrs[i].sh_addr = (size_t)hdr + sechdrs[i].sh_offset;
  1333. /* Internal symbols and strings. */
  1334. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  1335. symindex = i;
  1336. strindex = sechdrs[i].sh_link;
  1337. strtab = (char *)hdr + sechdrs[strindex].sh_offset;
  1338. }
  1339. #ifndef CONFIG_MODULE_UNLOAD
  1340. /* Don't load .exit sections */
  1341. if (strncmp(secstrings+sechdrs[i].sh_name, ".exit", 5) == 0)
  1342. sechdrs[i].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1343. #endif
  1344. }
  1345. modindex = find_sec(hdr, sechdrs, secstrings,
  1346. ".gnu.linkonce.this_module");
  1347. if (!modindex) {
  1348. printk(KERN_WARNING "No module found in object\n");
  1349. err = -ENOEXEC;
  1350. goto free_hdr;
  1351. }
  1352. mod = (void *)sechdrs[modindex].sh_addr;
  1353. if (symindex == 0) {
  1354. printk(KERN_WARNING "%s: module has no symbols (stripped?)\n",
  1355. mod->name);
  1356. err = -ENOEXEC;
  1357. goto free_hdr;
  1358. }
  1359. /* Optional sections */
  1360. exportindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab");
  1361. gplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl");
  1362. gplfutureindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_gpl_future");
  1363. unusedindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused");
  1364. unusedgplindex = find_sec(hdr, sechdrs, secstrings, "__ksymtab_unused_gpl");
  1365. crcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab");
  1366. gplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl");
  1367. gplfuturecrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_gpl_future");
  1368. unusedcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused");
  1369. unusedgplcrcindex = find_sec(hdr, sechdrs, secstrings, "__kcrctab_unused_gpl");
  1370. setupindex = find_sec(hdr, sechdrs, secstrings, "__param");
  1371. exindex = find_sec(hdr, sechdrs, secstrings, "__ex_table");
  1372. obsparmindex = find_sec(hdr, sechdrs, secstrings, "__obsparm");
  1373. versindex = find_sec(hdr, sechdrs, secstrings, "__versions");
  1374. infoindex = find_sec(hdr, sechdrs, secstrings, ".modinfo");
  1375. pcpuindex = find_pcpusec(hdr, sechdrs, secstrings);
  1376. #ifdef ARCH_UNWIND_SECTION_NAME
  1377. unwindex = find_sec(hdr, sechdrs, secstrings, ARCH_UNWIND_SECTION_NAME);
  1378. #endif
  1379. /* Don't keep modinfo section */
  1380. sechdrs[infoindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1381. #ifdef CONFIG_KALLSYMS
  1382. /* Keep symbol and string tables for decoding later. */
  1383. sechdrs[symindex].sh_flags |= SHF_ALLOC;
  1384. sechdrs[strindex].sh_flags |= SHF_ALLOC;
  1385. #endif
  1386. if (unwindex)
  1387. sechdrs[unwindex].sh_flags |= SHF_ALLOC;
  1388. /* Check module struct version now, before we try to use module. */
  1389. if (!check_modstruct_version(sechdrs, versindex, mod)) {
  1390. err = -ENOEXEC;
  1391. goto free_hdr;
  1392. }
  1393. modmagic = get_modinfo(sechdrs, infoindex, "vermagic");
  1394. /* This is allowed: modprobe --force will invalidate it. */
  1395. if (!modmagic) {
  1396. add_taint(TAINT_FORCED_MODULE);
  1397. printk(KERN_WARNING "%s: no version magic, tainting kernel.\n",
  1398. mod->name);
  1399. } else if (!same_magic(modmagic, vermagic)) {
  1400. printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
  1401. mod->name, modmagic, vermagic);
  1402. err = -ENOEXEC;
  1403. goto free_hdr;
  1404. }
  1405. /* Now copy in args */
  1406. args = strndup_user(uargs, ~0UL >> 1);
  1407. if (IS_ERR(args)) {
  1408. err = PTR_ERR(args);
  1409. goto free_hdr;
  1410. }
  1411. if (find_module(mod->name)) {
  1412. err = -EEXIST;
  1413. goto free_mod;
  1414. }
  1415. mod->state = MODULE_STATE_COMING;
  1416. /* Allow arches to frob section contents and sizes. */
  1417. err = module_frob_arch_sections(hdr, sechdrs, secstrings, mod);
  1418. if (err < 0)
  1419. goto free_mod;
  1420. if (pcpuindex) {
  1421. /* We have a special allocation for this section. */
  1422. percpu = percpu_modalloc(sechdrs[pcpuindex].sh_size,
  1423. sechdrs[pcpuindex].sh_addralign,
  1424. mod->name);
  1425. if (!percpu) {
  1426. err = -ENOMEM;
  1427. goto free_mod;
  1428. }
  1429. sechdrs[pcpuindex].sh_flags &= ~(unsigned long)SHF_ALLOC;
  1430. mod->percpu = percpu;
  1431. }
  1432. /* Determine total sizes, and put offsets in sh_entsize. For now
  1433. this is done generically; there doesn't appear to be any
  1434. special cases for the architectures. */
  1435. layout_sections(mod, hdr, sechdrs, secstrings);
  1436. /* Do the allocs. */
  1437. ptr = module_alloc(mod->core_size);
  1438. if (!ptr) {
  1439. err = -ENOMEM;
  1440. goto free_percpu;
  1441. }
  1442. memset(ptr, 0, mod->core_size);
  1443. mod->module_core = ptr;
  1444. ptr = module_alloc(mod->init_size);
  1445. if (!ptr && mod->init_size) {
  1446. err = -ENOMEM;
  1447. goto free_core;
  1448. }
  1449. memset(ptr, 0, mod->init_size);
  1450. mod->module_init = ptr;
  1451. /* Transfer each section which specifies SHF_ALLOC */
  1452. DEBUGP("final section addresses:\n");
  1453. for (i = 0; i < hdr->e_shnum; i++) {
  1454. void *dest;
  1455. if (!(sechdrs[i].sh_flags & SHF_ALLOC))
  1456. continue;
  1457. if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
  1458. dest = mod->module_init
  1459. + (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
  1460. else
  1461. dest = mod->module_core + sechdrs[i].sh_entsize;
  1462. if (sechdrs[i].sh_type != SHT_NOBITS)
  1463. memcpy(dest, (void *)sechdrs[i].sh_addr,
  1464. sechdrs[i].sh_size);
  1465. /* Update sh_addr to point to copy in image. */
  1466. sechdrs[i].sh_addr = (unsigned long)dest;
  1467. DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
  1468. }
  1469. /* Module has been moved. */
  1470. mod = (void *)sechdrs[modindex].sh_addr;
  1471. /* Now we've moved module, initialize linked lists, etc. */
  1472. module_unload_init(mod);
  1473. /* Set up license info based on the info section */
  1474. set_license(mod, get_modinfo(sechdrs, infoindex, "license"));
  1475. if (strcmp(mod->name, "ndiswrapper") == 0)
  1476. add_taint(TAINT_PROPRIETARY_MODULE);
  1477. if (strcmp(mod->name, "driverloader") == 0)
  1478. add_taint(TAINT_PROPRIETARY_MODULE);
  1479. /* Set up MODINFO_ATTR fields */
  1480. setup_modinfo(mod, sechdrs, infoindex);
  1481. /* Fix up syms, so that st_value is a pointer to location. */
  1482. err = simplify_symbols(sechdrs, symindex, strtab, versindex, pcpuindex,
  1483. mod);
  1484. if (err < 0)
  1485. goto cleanup;
  1486. /* Set up EXPORTed & EXPORT_GPLed symbols (section 0 is 0 length) */
  1487. mod->num_syms = sechdrs[exportindex].sh_size / sizeof(*mod->syms);
  1488. mod->syms = (void *)sechdrs[exportindex].sh_addr;
  1489. if (crcindex)
  1490. mod->crcs = (void *)sechdrs[crcindex].sh_addr;
  1491. mod->num_gpl_syms = sechdrs[gplindex].sh_size / sizeof(*mod->gpl_syms);
  1492. mod->gpl_syms = (void *)sechdrs[gplindex].sh_addr;
  1493. if (gplcrcindex)
  1494. mod->gpl_crcs = (void *)sechdrs[gplcrcindex].sh_addr;
  1495. mod->num_gpl_future_syms = sechdrs[gplfutureindex].sh_size /
  1496. sizeof(*mod->gpl_future_syms);
  1497. mod->num_unused_syms = sechdrs[unusedindex].sh_size /
  1498. sizeof(*mod->unused_syms);
  1499. mod->num_unused_gpl_syms = sechdrs[unusedgplindex].sh_size /
  1500. sizeof(*mod->unused_gpl_syms);
  1501. mod->gpl_future_syms = (void *)sechdrs[gplfutureindex].sh_addr;
  1502. if (gplfuturecrcindex)
  1503. mod->gpl_future_crcs = (void *)sechdrs[gplfuturecrcindex].sh_addr;
  1504. mod->unused_syms = (void *)sechdrs[unusedindex].sh_addr;
  1505. if (unusedcrcindex)
  1506. mod->unused_crcs = (void *)sechdrs[unusedcrcindex].sh_addr;
  1507. mod->unused_gpl_syms = (void *)sechdrs[unusedgplindex].sh_addr;
  1508. if (unusedgplcrcindex)
  1509. mod->unused_crcs = (void *)sechdrs[unusedgplcrcindex].sh_addr;
  1510. #ifdef CONFIG_MODVERSIONS
  1511. if ((mod->num_syms && !crcindex) ||
  1512. (mod->num_gpl_syms && !gplcrcindex) ||
  1513. (mod->num_gpl_future_syms && !gplfuturecrcindex) ||
  1514. (mod->num_unused_syms && !unusedcrcindex) ||
  1515. (mod->num_unused_gpl_syms && !unusedgplcrcindex)) {
  1516. printk(KERN_WARNING "%s: No versions for exported symbols."
  1517. " Tainting kernel.\n", mod->name);
  1518. add_taint(TAINT_FORCED_MODULE);
  1519. }
  1520. #endif
  1521. /* Now do relocations. */
  1522. for (i = 1; i < hdr->e_shnum; i++) {
  1523. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  1524. unsigned int info = sechdrs[i].sh_info;
  1525. /* Not a valid relocation section? */
  1526. if (info >= hdr->e_shnum)
  1527. continue;
  1528. /* Don't bother with non-allocated sections */
  1529. if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  1530. continue;
  1531. if (sechdrs[i].sh_type == SHT_REL)
  1532. err = apply_relocate(sechdrs, strtab, symindex, i,mod);
  1533. else if (sechdrs[i].sh_type == SHT_RELA)
  1534. err = apply_relocate_add(sechdrs, strtab, symindex, i,
  1535. mod);
  1536. if (err < 0)
  1537. goto cleanup;
  1538. }
  1539. /* Find duplicate symbols */
  1540. err = verify_export_symbols(mod);
  1541. if (err < 0)
  1542. goto cleanup;
  1543. /* Set up and sort exception table */
  1544. mod->num_exentries = sechdrs[exindex].sh_size / sizeof(*mod->extable);
  1545. mod->extable = extable = (void *)sechdrs[exindex].sh_addr;
  1546. sort_extable(extable, extable + mod->num_exentries);
  1547. /* Finally, copy percpu area over. */
  1548. percpu_modcopy(mod->percpu, (void *)sechdrs[pcpuindex].sh_addr,
  1549. sechdrs[pcpuindex].sh_size);
  1550. add_kallsyms(mod, sechdrs, symindex, strindex, secstrings);
  1551. err = module_finalize(hdr, sechdrs, mod);
  1552. if (err < 0)
  1553. goto cleanup;
  1554. /* flush the icache in correct context */
  1555. old_fs = get_fs();
  1556. set_fs(KERNEL_DS);
  1557. /*
  1558. * Flush the instruction cache, since we've played with text.
  1559. * Do it before processing of module parameters, so the module
  1560. * can provide parameter accessor functions of its own.
  1561. */
  1562. if (mod->module_init)
  1563. flush_icache_range((unsigned long)mod->module_init,
  1564. (unsigned long)mod->module_init
  1565. + mod->init_size);
  1566. flush_icache_range((unsigned long)mod->module_core,
  1567. (unsigned long)mod->module_core + mod->core_size);
  1568. set_fs(old_fs);
  1569. mod->args = args;
  1570. if (obsparmindex)
  1571. printk(KERN_WARNING "%s: Ignoring obsolete parameters\n",
  1572. mod->name);
  1573. /* Size of section 0 is 0, so this works well if no params */
  1574. err = parse_args(mod->name, mod->args,
  1575. (struct kernel_param *)
  1576. sechdrs[setupindex].sh_addr,
  1577. sechdrs[setupindex].sh_size
  1578. / sizeof(struct kernel_param),
  1579. NULL);
  1580. if (err < 0)
  1581. goto arch_cleanup;
  1582. err = mod_sysfs_setup(mod,
  1583. (struct kernel_param *)
  1584. sechdrs[setupindex].sh_addr,
  1585. sechdrs[setupindex].sh_size
  1586. / sizeof(struct kernel_param));
  1587. if (err < 0)
  1588. goto arch_cleanup;
  1589. add_sect_attrs(mod, hdr->e_shnum, secstrings, sechdrs);
  1590. /* Size of section 0 is 0, so this works well if no unwind info. */
  1591. mod->unwind_info = unwind_add_table(mod,
  1592. (void *)sechdrs[unwindex].sh_addr,
  1593. sechdrs[unwindex].sh_size);
  1594. /* Get rid of temporary copy */
  1595. vfree(hdr);
  1596. /* Done! */
  1597. return mod;
  1598. arch_cleanup:
  1599. module_arch_cleanup(mod);
  1600. cleanup:
  1601. module_unload_free(mod);
  1602. module_free(mod, mod->module_init);
  1603. free_core:
  1604. module_free(mod, mod->module_core);
  1605. free_percpu:
  1606. if (percpu)
  1607. percpu_modfree(percpu);
  1608. free_mod:
  1609. kfree(args);
  1610. free_hdr:
  1611. vfree(hdr);
  1612. return ERR_PTR(err);
  1613. truncated:
  1614. printk(KERN_ERR "Module len %lu truncated\n", len);
  1615. err = -ENOEXEC;
  1616. goto free_hdr;
  1617. }
  1618. /*
  1619. * link the module with the whole machine is stopped with interrupts off
  1620. * - this defends against kallsyms not taking locks
  1621. */
  1622. static int __link_module(void *_mod)
  1623. {
  1624. struct module *mod = _mod;
  1625. list_add(&mod->list, &modules);
  1626. return 0;
  1627. }
  1628. /* This is where the real work happens */
  1629. asmlinkage long
  1630. sys_init_module(void __user *umod,
  1631. unsigned long len,
  1632. const char __user *uargs)
  1633. {
  1634. struct module *mod;
  1635. int ret = 0;
  1636. /* Must have permission */
  1637. if (!capable(CAP_SYS_MODULE))
  1638. return -EPERM;
  1639. /* Only one module load at a time, please */
  1640. if (mutex_lock_interruptible(&module_mutex) != 0)
  1641. return -EINTR;
  1642. /* Do all the hard work */
  1643. mod = load_module(umod, len, uargs);
  1644. if (IS_ERR(mod)) {
  1645. mutex_unlock(&module_mutex);
  1646. return PTR_ERR(mod);
  1647. }
  1648. /* Now sew it into the lists. They won't access us, since
  1649. strong_try_module_get() will fail. */
  1650. stop_machine_run(__link_module, mod, NR_CPUS);
  1651. /* Drop lock so they can recurse */
  1652. mutex_unlock(&module_mutex);
  1653. blocking_notifier_call_chain(&module_notify_list,
  1654. MODULE_STATE_COMING, mod);
  1655. /* Start the module */
  1656. if (mod->init != NULL)
  1657. ret = mod->init();
  1658. if (ret < 0) {
  1659. /* Init routine failed: abort. Try to protect us from
  1660. buggy refcounters. */
  1661. mod->state = MODULE_STATE_GOING;
  1662. synchronize_sched();
  1663. if (mod->unsafe)
  1664. printk(KERN_ERR "%s: module is now stuck!\n",
  1665. mod->name);
  1666. else {
  1667. module_put(mod);
  1668. mutex_lock(&module_mutex);
  1669. free_module(mod);
  1670. mutex_unlock(&module_mutex);
  1671. }
  1672. return ret;
  1673. }
  1674. /* Now it's a first class citizen! */
  1675. mutex_lock(&module_mutex);
  1676. mod->state = MODULE_STATE_LIVE;
  1677. /* Drop initial reference. */
  1678. module_put(mod);
  1679. unwind_remove_table(mod->unwind_info, 1);
  1680. module_free(mod, mod->module_init);
  1681. mod->module_init = NULL;
  1682. mod->init_size = 0;
  1683. mod->init_text_size = 0;
  1684. mutex_unlock(&module_mutex);
  1685. return 0;
  1686. }
  1687. static inline int within(unsigned long addr, void *start, unsigned long size)
  1688. {
  1689. return ((void *)addr >= start && (void *)addr < start + size);
  1690. }
  1691. #ifdef CONFIG_KALLSYMS
  1692. /*
  1693. * This ignores the intensely annoying "mapping symbols" found
  1694. * in ARM ELF files: $a, $t and $d.
  1695. */
  1696. static inline int is_arm_mapping_symbol(const char *str)
  1697. {
  1698. return str[0] == '$' && strchr("atd", str[1])
  1699. && (str[2] == '\0' || str[2] == '.');
  1700. }
  1701. static const char *get_ksymbol(struct module *mod,
  1702. unsigned long addr,
  1703. unsigned long *size,
  1704. unsigned long *offset)
  1705. {
  1706. unsigned int i, best = 0;
  1707. unsigned long nextval;
  1708. /* At worse, next value is at end of module */
  1709. if (within(addr, mod->module_init, mod->init_size))
  1710. nextval = (unsigned long)mod->module_init+mod->init_text_size;
  1711. else
  1712. nextval = (unsigned long)mod->module_core+mod->core_text_size;
  1713. /* Scan for closest preceeding symbol, and next symbol. (ELF
  1714. starts real symbols at 1). */
  1715. for (i = 1; i < mod->num_symtab; i++) {
  1716. if (mod->symtab[i].st_shndx == SHN_UNDEF)
  1717. continue;
  1718. /* We ignore unnamed symbols: they're uninformative
  1719. * and inserted at a whim. */
  1720. if (mod->symtab[i].st_value <= addr
  1721. && mod->symtab[i].st_value > mod->symtab[best].st_value
  1722. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  1723. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  1724. best = i;
  1725. if (mod->symtab[i].st_value > addr
  1726. && mod->symtab[i].st_value < nextval
  1727. && *(mod->strtab + mod->symtab[i].st_name) != '\0'
  1728. && !is_arm_mapping_symbol(mod->strtab + mod->symtab[i].st_name))
  1729. nextval = mod->symtab[i].st_value;
  1730. }
  1731. if (!best)
  1732. return NULL;
  1733. *size = nextval - mod->symtab[best].st_value;
  1734. *offset = addr - mod->symtab[best].st_value;
  1735. return mod->strtab + mod->symtab[best].st_name;
  1736. }
  1737. /* For kallsyms to ask for address resolution. NULL means not found.
  1738. We don't lock, as this is used for oops resolution and races are a
  1739. lesser concern. */
  1740. const char *module_address_lookup(unsigned long addr,
  1741. unsigned long *size,
  1742. unsigned long *offset,
  1743. char **modname)
  1744. {
  1745. struct module *mod;
  1746. list_for_each_entry(mod, &modules, list) {
  1747. if (within(addr, mod->module_init, mod->init_size)
  1748. || within(addr, mod->module_core, mod->core_size)) {
  1749. *modname = mod->name;
  1750. return get_ksymbol(mod, addr, size, offset);
  1751. }
  1752. }
  1753. return NULL;
  1754. }
  1755. struct module *module_get_kallsym(unsigned int symnum,
  1756. unsigned long *value,
  1757. char *type,
  1758. char namebuf[128])
  1759. {
  1760. struct module *mod;
  1761. mutex_lock(&module_mutex);
  1762. list_for_each_entry(mod, &modules, list) {
  1763. if (symnum < mod->num_symtab) {
  1764. *value = mod->symtab[symnum].st_value;
  1765. *type = mod->symtab[symnum].st_info;
  1766. strncpy(namebuf,
  1767. mod->strtab + mod->symtab[symnum].st_name,
  1768. 127);
  1769. mutex_unlock(&module_mutex);
  1770. return mod;
  1771. }
  1772. symnum -= mod->num_symtab;
  1773. }
  1774. mutex_unlock(&module_mutex);
  1775. return NULL;
  1776. }
  1777. static unsigned long mod_find_symname(struct module *mod, const char *name)
  1778. {
  1779. unsigned int i;
  1780. for (i = 0; i < mod->num_symtab; i++)
  1781. if (strcmp(name, mod->strtab+mod->symtab[i].st_name) == 0 &&
  1782. mod->symtab[i].st_info != 'U')
  1783. return mod->symtab[i].st_value;
  1784. return 0;
  1785. }
  1786. /* Look for this name: can be of form module:name. */
  1787. unsigned long module_kallsyms_lookup_name(const char *name)
  1788. {
  1789. struct module *mod;
  1790. char *colon;
  1791. unsigned long ret = 0;
  1792. /* Don't lock: we're in enough trouble already. */
  1793. if ((colon = strchr(name, ':')) != NULL) {
  1794. *colon = '\0';
  1795. if ((mod = find_module(name)) != NULL)
  1796. ret = mod_find_symname(mod, colon+1);
  1797. *colon = ':';
  1798. } else {
  1799. list_for_each_entry(mod, &modules, list)
  1800. if ((ret = mod_find_symname(mod, name)) != 0)
  1801. break;
  1802. }
  1803. return ret;
  1804. }
  1805. #endif /* CONFIG_KALLSYMS */
  1806. /* Called by the /proc file system to return a list of modules. */
  1807. static void *m_start(struct seq_file *m, loff_t *pos)
  1808. {
  1809. struct list_head *i;
  1810. loff_t n = 0;
  1811. mutex_lock(&module_mutex);
  1812. list_for_each(i, &modules) {
  1813. if (n++ == *pos)
  1814. break;
  1815. }
  1816. if (i == &modules)
  1817. return NULL;
  1818. return i;
  1819. }
  1820. static void *m_next(struct seq_file *m, void *p, loff_t *pos)
  1821. {
  1822. struct list_head *i = p;
  1823. (*pos)++;
  1824. if (i->next == &modules)
  1825. return NULL;
  1826. return i->next;
  1827. }
  1828. static void m_stop(struct seq_file *m, void *p)
  1829. {
  1830. mutex_unlock(&module_mutex);
  1831. }
  1832. static int m_show(struct seq_file *m, void *p)
  1833. {
  1834. struct module *mod = list_entry(p, struct module, list);
  1835. seq_printf(m, "%s %lu",
  1836. mod->name, mod->init_size + mod->core_size);
  1837. print_unload_info(m, mod);
  1838. /* Informative for users. */
  1839. seq_printf(m, " %s",
  1840. mod->state == MODULE_STATE_GOING ? "Unloading":
  1841. mod->state == MODULE_STATE_COMING ? "Loading":
  1842. "Live");
  1843. /* Used by oprofile and other similar tools. */
  1844. seq_printf(m, " 0x%p", mod->module_core);
  1845. seq_printf(m, "\n");
  1846. return 0;
  1847. }
  1848. /* Format: modulename size refcount deps address
  1849. Where refcount is a number or -, and deps is a comma-separated list
  1850. of depends or -.
  1851. */
  1852. struct seq_operations modules_op = {
  1853. .start = m_start,
  1854. .next = m_next,
  1855. .stop = m_stop,
  1856. .show = m_show
  1857. };
  1858. /* Given an address, look for it in the module exception tables. */
  1859. const struct exception_table_entry *search_module_extables(unsigned long addr)
  1860. {
  1861. unsigned long flags;
  1862. const struct exception_table_entry *e = NULL;
  1863. struct module *mod;
  1864. spin_lock_irqsave(&modlist_lock, flags);
  1865. list_for_each_entry(mod, &modules, list) {
  1866. if (mod->num_exentries == 0)
  1867. continue;
  1868. e = search_extable(mod->extable,
  1869. mod->extable + mod->num_exentries - 1,
  1870. addr);
  1871. if (e)
  1872. break;
  1873. }
  1874. spin_unlock_irqrestore(&modlist_lock, flags);
  1875. /* Now, if we found one, we are running inside it now, hence
  1876. we cannot unload the module, hence no refcnt needed. */
  1877. return e;
  1878. }
  1879. /* Is this a valid kernel address? We don't grab the lock: we are oopsing. */
  1880. struct module *__module_text_address(unsigned long addr)
  1881. {
  1882. struct module *mod;
  1883. list_for_each_entry(mod, &modules, list)
  1884. if (within(addr, mod->module_init, mod->init_text_size)
  1885. || within(addr, mod->module_core, mod->core_text_size))
  1886. return mod;
  1887. return NULL;
  1888. }
  1889. struct module *module_text_address(unsigned long addr)
  1890. {
  1891. struct module *mod;
  1892. unsigned long flags;
  1893. spin_lock_irqsave(&modlist_lock, flags);
  1894. mod = __module_text_address(addr);
  1895. spin_unlock_irqrestore(&modlist_lock, flags);
  1896. return mod;
  1897. }
  1898. /* Don't grab lock, we're oopsing. */
  1899. void print_modules(void)
  1900. {
  1901. struct module *mod;
  1902. printk("Modules linked in:");
  1903. list_for_each_entry(mod, &modules, list)
  1904. printk(" %s", mod->name);
  1905. printk("\n");
  1906. }
  1907. void module_add_driver(struct module *mod, struct device_driver *drv)
  1908. {
  1909. if (!mod || !drv)
  1910. return;
  1911. /* Don't check return code; this call is idempotent */
  1912. sysfs_create_link(&drv->kobj, &mod->mkobj.kobj, "module");
  1913. }
  1914. EXPORT_SYMBOL(module_add_driver);
  1915. void module_remove_driver(struct device_driver *drv)
  1916. {
  1917. if (!drv)
  1918. return;
  1919. sysfs_remove_link(&drv->kobj, "module");
  1920. }
  1921. EXPORT_SYMBOL(module_remove_driver);
  1922. #ifdef CONFIG_MODVERSIONS
  1923. /* Generate the signature for struct module here, too, for modversions. */
  1924. void struct_module(struct module *mod) { return; }
  1925. EXPORT_SYMBOL(struct_module);
  1926. #endif