module.c 58 KB

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