vdso.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /*
  2. * Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp.
  3. * <benh@kernel.crashing.org>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/sched.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mm.h>
  14. #include <linux/smp.h>
  15. #include <linux/stddef.h>
  16. #include <linux/unistd.h>
  17. #include <linux/slab.h>
  18. #include <linux/user.h>
  19. #include <linux/elf.h>
  20. #include <linux/security.h>
  21. #include <linux/memblock.h>
  22. #include <asm/cpu_has_feature.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/processor.h>
  25. #include <asm/mmu.h>
  26. #include <asm/mmu_context.h>
  27. #include <asm/prom.h>
  28. #include <asm/machdep.h>
  29. #include <asm/cputable.h>
  30. #include <asm/sections.h>
  31. #include <asm/firmware.h>
  32. #include <asm/vdso.h>
  33. #include <asm/vdso_datapage.h>
  34. #include <asm/setup.h>
  35. #undef DEBUG
  36. #ifdef DEBUG
  37. #define DBG(fmt...) printk(fmt)
  38. #else
  39. #define DBG(fmt...)
  40. #endif
  41. /* Max supported size for symbol names */
  42. #define MAX_SYMNAME 64
  43. /* The alignment of the vDSO */
  44. #define VDSO_ALIGNMENT (1 << 16)
  45. static unsigned int vdso32_pages;
  46. static void *vdso32_kbase;
  47. static struct page **vdso32_pagelist;
  48. unsigned long vdso32_sigtramp;
  49. unsigned long vdso32_rt_sigtramp;
  50. #ifdef CONFIG_VDSO32
  51. extern char vdso32_start, vdso32_end;
  52. #endif
  53. #ifdef CONFIG_PPC64
  54. extern char vdso64_start, vdso64_end;
  55. static void *vdso64_kbase = &vdso64_start;
  56. static unsigned int vdso64_pages;
  57. static struct page **vdso64_pagelist;
  58. unsigned long vdso64_rt_sigtramp;
  59. #endif /* CONFIG_PPC64 */
  60. static int vdso_ready;
  61. /*
  62. * The vdso data page (aka. systemcfg for old ppc64 fans) is here.
  63. * Once the early boot kernel code no longer needs to muck around
  64. * with it, it will become dynamically allocated
  65. */
  66. static union {
  67. struct vdso_data data;
  68. u8 page[PAGE_SIZE];
  69. } vdso_data_store __page_aligned_data;
  70. struct vdso_data *vdso_data = &vdso_data_store.data;
  71. /* Format of the patch table */
  72. struct vdso_patch_def
  73. {
  74. unsigned long ftr_mask, ftr_value;
  75. const char *gen_name;
  76. const char *fix_name;
  77. };
  78. /* Table of functions to patch based on the CPU type/revision
  79. *
  80. * Currently, we only change sync_dicache to do nothing on processors
  81. * with a coherent icache
  82. */
  83. static struct vdso_patch_def vdso_patches[] = {
  84. {
  85. CPU_FTR_COHERENT_ICACHE, CPU_FTR_COHERENT_ICACHE,
  86. "__kernel_sync_dicache", "__kernel_sync_dicache_p5"
  87. },
  88. #ifdef CONFIG_PPC32
  89. {
  90. CPU_FTR_USE_RTC, CPU_FTR_USE_RTC,
  91. "__kernel_gettimeofday", NULL
  92. },
  93. {
  94. CPU_FTR_USE_RTC, CPU_FTR_USE_RTC,
  95. "__kernel_clock_gettime", NULL
  96. },
  97. {
  98. CPU_FTR_USE_RTC, CPU_FTR_USE_RTC,
  99. "__kernel_clock_getres", NULL
  100. },
  101. {
  102. CPU_FTR_USE_RTC, CPU_FTR_USE_RTC,
  103. "__kernel_get_tbfreq", NULL
  104. },
  105. {
  106. CPU_FTR_USE_RTC, CPU_FTR_USE_RTC,
  107. "__kernel_time", NULL
  108. },
  109. #endif
  110. };
  111. /*
  112. * Some infos carried around for each of them during parsing at
  113. * boot time.
  114. */
  115. struct lib32_elfinfo
  116. {
  117. Elf32_Ehdr *hdr; /* ptr to ELF */
  118. Elf32_Sym *dynsym; /* ptr to .dynsym section */
  119. unsigned long dynsymsize; /* size of .dynsym section */
  120. char *dynstr; /* ptr to .dynstr section */
  121. unsigned long text; /* offset of .text section in .so */
  122. };
  123. struct lib64_elfinfo
  124. {
  125. Elf64_Ehdr *hdr;
  126. Elf64_Sym *dynsym;
  127. unsigned long dynsymsize;
  128. char *dynstr;
  129. unsigned long text;
  130. };
  131. /*
  132. * This is called from binfmt_elf, we create the special vma for the
  133. * vDSO and insert it into the mm struct tree
  134. */
  135. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  136. {
  137. struct mm_struct *mm = current->mm;
  138. struct page **vdso_pagelist;
  139. unsigned long vdso_pages;
  140. unsigned long vdso_base;
  141. int rc;
  142. if (!vdso_ready)
  143. return 0;
  144. #ifdef CONFIG_PPC64
  145. if (is_32bit_task()) {
  146. vdso_pagelist = vdso32_pagelist;
  147. vdso_pages = vdso32_pages;
  148. vdso_base = VDSO32_MBASE;
  149. } else {
  150. vdso_pagelist = vdso64_pagelist;
  151. vdso_pages = vdso64_pages;
  152. /*
  153. * On 64bit we don't have a preferred map address. This
  154. * allows get_unmapped_area to find an area near other mmaps
  155. * and most likely share a SLB entry.
  156. */
  157. vdso_base = 0;
  158. }
  159. #else
  160. vdso_pagelist = vdso32_pagelist;
  161. vdso_pages = vdso32_pages;
  162. vdso_base = VDSO32_MBASE;
  163. #endif
  164. current->mm->context.vdso_base = 0;
  165. /* vDSO has a problem and was disabled, just don't "enable" it for the
  166. * process
  167. */
  168. if (vdso_pages == 0)
  169. return 0;
  170. /* Add a page to the vdso size for the data page */
  171. vdso_pages ++;
  172. /*
  173. * pick a base address for the vDSO in process space. We try to put it
  174. * at vdso_base which is the "natural" base for it, but we might fail
  175. * and end up putting it elsewhere.
  176. * Add enough to the size so that the result can be aligned.
  177. */
  178. if (down_write_killable(&mm->mmap_sem))
  179. return -EINTR;
  180. vdso_base = get_unmapped_area(NULL, vdso_base,
  181. (vdso_pages << PAGE_SHIFT) +
  182. ((VDSO_ALIGNMENT - 1) & PAGE_MASK),
  183. 0, 0);
  184. if (IS_ERR_VALUE(vdso_base)) {
  185. rc = vdso_base;
  186. goto fail_mmapsem;
  187. }
  188. /* Add required alignment. */
  189. vdso_base = ALIGN(vdso_base, VDSO_ALIGNMENT);
  190. /*
  191. * Put vDSO base into mm struct. We need to do this before calling
  192. * install_special_mapping or the perf counter mmap tracking code
  193. * will fail to recognise it as a vDSO (since arch_vma_name fails).
  194. */
  195. current->mm->context.vdso_base = vdso_base;
  196. /*
  197. * our vma flags don't have VM_WRITE so by default, the process isn't
  198. * allowed to write those pages.
  199. * gdb can break that with ptrace interface, and thus trigger COW on
  200. * those pages but it's then your responsibility to never do that on
  201. * the "data" page of the vDSO or you'll stop getting kernel updates
  202. * and your nice userland gettimeofday will be totally dead.
  203. * It's fine to use that for setting breakpoints in the vDSO code
  204. * pages though.
  205. */
  206. rc = install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
  207. VM_READ|VM_EXEC|
  208. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  209. vdso_pagelist);
  210. if (rc) {
  211. current->mm->context.vdso_base = 0;
  212. goto fail_mmapsem;
  213. }
  214. up_write(&mm->mmap_sem);
  215. return 0;
  216. fail_mmapsem:
  217. up_write(&mm->mmap_sem);
  218. return rc;
  219. }
  220. const char *arch_vma_name(struct vm_area_struct *vma)
  221. {
  222. if (vma->vm_mm && vma->vm_start == vma->vm_mm->context.vdso_base)
  223. return "[vdso]";
  224. return NULL;
  225. }
  226. #ifdef CONFIG_VDSO32
  227. static void * __init find_section32(Elf32_Ehdr *ehdr, const char *secname,
  228. unsigned long *size)
  229. {
  230. Elf32_Shdr *sechdrs;
  231. unsigned int i;
  232. char *secnames;
  233. /* Grab section headers and strings so we can tell who is who */
  234. sechdrs = (void *)ehdr + ehdr->e_shoff;
  235. secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
  236. /* Find the section they want */
  237. for (i = 1; i < ehdr->e_shnum; i++) {
  238. if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0) {
  239. if (size)
  240. *size = sechdrs[i].sh_size;
  241. return (void *)ehdr + sechdrs[i].sh_offset;
  242. }
  243. }
  244. *size = 0;
  245. return NULL;
  246. }
  247. static Elf32_Sym * __init find_symbol32(struct lib32_elfinfo *lib,
  248. const char *symname)
  249. {
  250. unsigned int i;
  251. char name[MAX_SYMNAME], *c;
  252. for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
  253. if (lib->dynsym[i].st_name == 0)
  254. continue;
  255. strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
  256. MAX_SYMNAME);
  257. c = strchr(name, '@');
  258. if (c)
  259. *c = 0;
  260. if (strcmp(symname, name) == 0)
  261. return &lib->dynsym[i];
  262. }
  263. return NULL;
  264. }
  265. /* Note that we assume the section is .text and the symbol is relative to
  266. * the library base
  267. */
  268. static unsigned long __init find_function32(struct lib32_elfinfo *lib,
  269. const char *symname)
  270. {
  271. Elf32_Sym *sym = find_symbol32(lib, symname);
  272. if (sym == NULL) {
  273. printk(KERN_WARNING "vDSO32: function %s not found !\n",
  274. symname);
  275. return 0;
  276. }
  277. return sym->st_value - VDSO32_LBASE;
  278. }
  279. static int __init vdso_do_func_patch32(struct lib32_elfinfo *v32,
  280. struct lib64_elfinfo *v64,
  281. const char *orig, const char *fix)
  282. {
  283. Elf32_Sym *sym32_gen, *sym32_fix;
  284. sym32_gen = find_symbol32(v32, orig);
  285. if (sym32_gen == NULL) {
  286. printk(KERN_ERR "vDSO32: Can't find symbol %s !\n", orig);
  287. return -1;
  288. }
  289. if (fix == NULL) {
  290. sym32_gen->st_name = 0;
  291. return 0;
  292. }
  293. sym32_fix = find_symbol32(v32, fix);
  294. if (sym32_fix == NULL) {
  295. printk(KERN_ERR "vDSO32: Can't find symbol %s !\n", fix);
  296. return -1;
  297. }
  298. sym32_gen->st_value = sym32_fix->st_value;
  299. sym32_gen->st_size = sym32_fix->st_size;
  300. sym32_gen->st_info = sym32_fix->st_info;
  301. sym32_gen->st_other = sym32_fix->st_other;
  302. sym32_gen->st_shndx = sym32_fix->st_shndx;
  303. return 0;
  304. }
  305. #else /* !CONFIG_VDSO32 */
  306. static unsigned long __init find_function32(struct lib32_elfinfo *lib,
  307. const char *symname)
  308. {
  309. return 0;
  310. }
  311. static int __init vdso_do_func_patch32(struct lib32_elfinfo *v32,
  312. struct lib64_elfinfo *v64,
  313. const char *orig, const char *fix)
  314. {
  315. return 0;
  316. }
  317. #endif /* CONFIG_VDSO32 */
  318. #ifdef CONFIG_PPC64
  319. static void * __init find_section64(Elf64_Ehdr *ehdr, const char *secname,
  320. unsigned long *size)
  321. {
  322. Elf64_Shdr *sechdrs;
  323. unsigned int i;
  324. char *secnames;
  325. /* Grab section headers and strings so we can tell who is who */
  326. sechdrs = (void *)ehdr + ehdr->e_shoff;
  327. secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
  328. /* Find the section they want */
  329. for (i = 1; i < ehdr->e_shnum; i++) {
  330. if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0) {
  331. if (size)
  332. *size = sechdrs[i].sh_size;
  333. return (void *)ehdr + sechdrs[i].sh_offset;
  334. }
  335. }
  336. if (size)
  337. *size = 0;
  338. return NULL;
  339. }
  340. static Elf64_Sym * __init find_symbol64(struct lib64_elfinfo *lib,
  341. const char *symname)
  342. {
  343. unsigned int i;
  344. char name[MAX_SYMNAME], *c;
  345. for (i = 0; i < (lib->dynsymsize / sizeof(Elf64_Sym)); i++) {
  346. if (lib->dynsym[i].st_name == 0)
  347. continue;
  348. strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
  349. MAX_SYMNAME);
  350. c = strchr(name, '@');
  351. if (c)
  352. *c = 0;
  353. if (strcmp(symname, name) == 0)
  354. return &lib->dynsym[i];
  355. }
  356. return NULL;
  357. }
  358. /* Note that we assume the section is .text and the symbol is relative to
  359. * the library base
  360. */
  361. static unsigned long __init find_function64(struct lib64_elfinfo *lib,
  362. const char *symname)
  363. {
  364. Elf64_Sym *sym = find_symbol64(lib, symname);
  365. if (sym == NULL) {
  366. printk(KERN_WARNING "vDSO64: function %s not found !\n",
  367. symname);
  368. return 0;
  369. }
  370. #ifdef VDS64_HAS_DESCRIPTORS
  371. return *((u64 *)(vdso64_kbase + sym->st_value - VDSO64_LBASE)) -
  372. VDSO64_LBASE;
  373. #else
  374. return sym->st_value - VDSO64_LBASE;
  375. #endif
  376. }
  377. static int __init vdso_do_func_patch64(struct lib32_elfinfo *v32,
  378. struct lib64_elfinfo *v64,
  379. const char *orig, const char *fix)
  380. {
  381. Elf64_Sym *sym64_gen, *sym64_fix;
  382. sym64_gen = find_symbol64(v64, orig);
  383. if (sym64_gen == NULL) {
  384. printk(KERN_ERR "vDSO64: Can't find symbol %s !\n", orig);
  385. return -1;
  386. }
  387. if (fix == NULL) {
  388. sym64_gen->st_name = 0;
  389. return 0;
  390. }
  391. sym64_fix = find_symbol64(v64, fix);
  392. if (sym64_fix == NULL) {
  393. printk(KERN_ERR "vDSO64: Can't find symbol %s !\n", fix);
  394. return -1;
  395. }
  396. sym64_gen->st_value = sym64_fix->st_value;
  397. sym64_gen->st_size = sym64_fix->st_size;
  398. sym64_gen->st_info = sym64_fix->st_info;
  399. sym64_gen->st_other = sym64_fix->st_other;
  400. sym64_gen->st_shndx = sym64_fix->st_shndx;
  401. return 0;
  402. }
  403. #endif /* CONFIG_PPC64 */
  404. static __init int vdso_do_find_sections(struct lib32_elfinfo *v32,
  405. struct lib64_elfinfo *v64)
  406. {
  407. void *sect;
  408. /*
  409. * Locate symbol tables & text section
  410. */
  411. #ifdef CONFIG_VDSO32
  412. v32->dynsym = find_section32(v32->hdr, ".dynsym", &v32->dynsymsize);
  413. v32->dynstr = find_section32(v32->hdr, ".dynstr", NULL);
  414. if (v32->dynsym == NULL || v32->dynstr == NULL) {
  415. printk(KERN_ERR "vDSO32: required symbol section not found\n");
  416. return -1;
  417. }
  418. sect = find_section32(v32->hdr, ".text", NULL);
  419. if (sect == NULL) {
  420. printk(KERN_ERR "vDSO32: the .text section was not found\n");
  421. return -1;
  422. }
  423. v32->text = sect - vdso32_kbase;
  424. #endif
  425. #ifdef CONFIG_PPC64
  426. v64->dynsym = find_section64(v64->hdr, ".dynsym", &v64->dynsymsize);
  427. v64->dynstr = find_section64(v64->hdr, ".dynstr", NULL);
  428. if (v64->dynsym == NULL || v64->dynstr == NULL) {
  429. printk(KERN_ERR "vDSO64: required symbol section not found\n");
  430. return -1;
  431. }
  432. sect = find_section64(v64->hdr, ".text", NULL);
  433. if (sect == NULL) {
  434. printk(KERN_ERR "vDSO64: the .text section was not found\n");
  435. return -1;
  436. }
  437. v64->text = sect - vdso64_kbase;
  438. #endif /* CONFIG_PPC64 */
  439. return 0;
  440. }
  441. static __init void vdso_setup_trampolines(struct lib32_elfinfo *v32,
  442. struct lib64_elfinfo *v64)
  443. {
  444. /*
  445. * Find signal trampolines
  446. */
  447. #ifdef CONFIG_PPC64
  448. vdso64_rt_sigtramp = find_function64(v64, "__kernel_sigtramp_rt64");
  449. #endif
  450. vdso32_sigtramp = find_function32(v32, "__kernel_sigtramp32");
  451. vdso32_rt_sigtramp = find_function32(v32, "__kernel_sigtramp_rt32");
  452. }
  453. static __init int vdso_fixup_datapage(struct lib32_elfinfo *v32,
  454. struct lib64_elfinfo *v64)
  455. {
  456. #ifdef CONFIG_VDSO32
  457. Elf32_Sym *sym32;
  458. #endif
  459. #ifdef CONFIG_PPC64
  460. Elf64_Sym *sym64;
  461. sym64 = find_symbol64(v64, "__kernel_datapage_offset");
  462. if (sym64 == NULL) {
  463. printk(KERN_ERR "vDSO64: Can't find symbol "
  464. "__kernel_datapage_offset !\n");
  465. return -1;
  466. }
  467. *((int *)(vdso64_kbase + sym64->st_value - VDSO64_LBASE)) =
  468. (vdso64_pages << PAGE_SHIFT) -
  469. (sym64->st_value - VDSO64_LBASE);
  470. #endif /* CONFIG_PPC64 */
  471. #ifdef CONFIG_VDSO32
  472. sym32 = find_symbol32(v32, "__kernel_datapage_offset");
  473. if (sym32 == NULL) {
  474. printk(KERN_ERR "vDSO32: Can't find symbol "
  475. "__kernel_datapage_offset !\n");
  476. return -1;
  477. }
  478. *((int *)(vdso32_kbase + (sym32->st_value - VDSO32_LBASE))) =
  479. (vdso32_pages << PAGE_SHIFT) -
  480. (sym32->st_value - VDSO32_LBASE);
  481. #endif
  482. return 0;
  483. }
  484. static __init int vdso_fixup_features(struct lib32_elfinfo *v32,
  485. struct lib64_elfinfo *v64)
  486. {
  487. unsigned long size;
  488. void *start;
  489. #ifdef CONFIG_PPC64
  490. start = find_section64(v64->hdr, "__ftr_fixup", &size);
  491. if (start)
  492. do_feature_fixups(cur_cpu_spec->cpu_features,
  493. start, start + size);
  494. start = find_section64(v64->hdr, "__mmu_ftr_fixup", &size);
  495. if (start)
  496. do_feature_fixups(cur_cpu_spec->mmu_features,
  497. start, start + size);
  498. start = find_section64(v64->hdr, "__fw_ftr_fixup", &size);
  499. if (start)
  500. do_feature_fixups(powerpc_firmware_features,
  501. start, start + size);
  502. start = find_section64(v64->hdr, "__lwsync_fixup", &size);
  503. if (start)
  504. do_lwsync_fixups(cur_cpu_spec->cpu_features,
  505. start, start + size);
  506. #endif /* CONFIG_PPC64 */
  507. #ifdef CONFIG_VDSO32
  508. start = find_section32(v32->hdr, "__ftr_fixup", &size);
  509. if (start)
  510. do_feature_fixups(cur_cpu_spec->cpu_features,
  511. start, start + size);
  512. start = find_section32(v32->hdr, "__mmu_ftr_fixup", &size);
  513. if (start)
  514. do_feature_fixups(cur_cpu_spec->mmu_features,
  515. start, start + size);
  516. #ifdef CONFIG_PPC64
  517. start = find_section32(v32->hdr, "__fw_ftr_fixup", &size);
  518. if (start)
  519. do_feature_fixups(powerpc_firmware_features,
  520. start, start + size);
  521. #endif /* CONFIG_PPC64 */
  522. start = find_section32(v32->hdr, "__lwsync_fixup", &size);
  523. if (start)
  524. do_lwsync_fixups(cur_cpu_spec->cpu_features,
  525. start, start + size);
  526. #endif
  527. return 0;
  528. }
  529. static __init int vdso_fixup_alt_funcs(struct lib32_elfinfo *v32,
  530. struct lib64_elfinfo *v64)
  531. {
  532. int i;
  533. for (i = 0; i < ARRAY_SIZE(vdso_patches); i++) {
  534. struct vdso_patch_def *patch = &vdso_patches[i];
  535. int match = (cur_cpu_spec->cpu_features & patch->ftr_mask)
  536. == patch->ftr_value;
  537. if (!match)
  538. continue;
  539. DBG("replacing %s with %s...\n", patch->gen_name,
  540. patch->fix_name ? "NONE" : patch->fix_name);
  541. /*
  542. * Patch the 32 bits and 64 bits symbols. Note that we do not
  543. * patch the "." symbol on 64 bits.
  544. * It would be easy to do, but doesn't seem to be necessary,
  545. * patching the OPD symbol is enough.
  546. */
  547. vdso_do_func_patch32(v32, v64, patch->gen_name,
  548. patch->fix_name);
  549. #ifdef CONFIG_PPC64
  550. vdso_do_func_patch64(v32, v64, patch->gen_name,
  551. patch->fix_name);
  552. #endif /* CONFIG_PPC64 */
  553. }
  554. return 0;
  555. }
  556. static __init int vdso_setup(void)
  557. {
  558. struct lib32_elfinfo v32;
  559. struct lib64_elfinfo v64;
  560. v32.hdr = vdso32_kbase;
  561. #ifdef CONFIG_PPC64
  562. v64.hdr = vdso64_kbase;
  563. #endif
  564. if (vdso_do_find_sections(&v32, &v64))
  565. return -1;
  566. if (vdso_fixup_datapage(&v32, &v64))
  567. return -1;
  568. if (vdso_fixup_features(&v32, &v64))
  569. return -1;
  570. if (vdso_fixup_alt_funcs(&v32, &v64))
  571. return -1;
  572. vdso_setup_trampolines(&v32, &v64);
  573. return 0;
  574. }
  575. /*
  576. * Called from setup_arch to initialize the bitmap of available
  577. * syscalls in the systemcfg page
  578. */
  579. static void __init vdso_setup_syscall_map(void)
  580. {
  581. unsigned int i;
  582. extern unsigned long *sys_call_table;
  583. extern unsigned long sys_ni_syscall;
  584. for (i = 0; i < NR_syscalls; i++) {
  585. #ifdef CONFIG_PPC64
  586. if (sys_call_table[i*2] != sys_ni_syscall)
  587. vdso_data->syscall_map_64[i >> 5] |=
  588. 0x80000000UL >> (i & 0x1f);
  589. if (sys_call_table[i*2+1] != sys_ni_syscall)
  590. vdso_data->syscall_map_32[i >> 5] |=
  591. 0x80000000UL >> (i & 0x1f);
  592. #else /* CONFIG_PPC64 */
  593. if (sys_call_table[i] != sys_ni_syscall)
  594. vdso_data->syscall_map_32[i >> 5] |=
  595. 0x80000000UL >> (i & 0x1f);
  596. #endif /* CONFIG_PPC64 */
  597. }
  598. }
  599. #ifdef CONFIG_PPC64
  600. int vdso_getcpu_init(void)
  601. {
  602. unsigned long cpu, node, val;
  603. /*
  604. * SPRG_VDSO contains the CPU in the bottom 16 bits and the NUMA node
  605. * in the next 16 bits. The VDSO uses this to implement getcpu().
  606. */
  607. cpu = get_cpu();
  608. WARN_ON_ONCE(cpu > 0xffff);
  609. node = cpu_to_node(cpu);
  610. WARN_ON_ONCE(node > 0xffff);
  611. val = (cpu & 0xfff) | ((node & 0xffff) << 16);
  612. mtspr(SPRN_SPRG_VDSO_WRITE, val);
  613. get_paca()->sprg_vdso = val;
  614. put_cpu();
  615. return 0;
  616. }
  617. /* We need to call this before SMP init */
  618. early_initcall(vdso_getcpu_init);
  619. #endif
  620. static int __init vdso_init(void)
  621. {
  622. int i;
  623. #ifdef CONFIG_PPC64
  624. /*
  625. * Fill up the "systemcfg" stuff for backward compatibility
  626. */
  627. strcpy((char *)vdso_data->eye_catcher, "SYSTEMCFG:PPC64");
  628. vdso_data->version.major = SYSTEMCFG_MAJOR;
  629. vdso_data->version.minor = SYSTEMCFG_MINOR;
  630. vdso_data->processor = mfspr(SPRN_PVR);
  631. /*
  632. * Fake the old platform number for pSeries and add
  633. * in LPAR bit if necessary
  634. */
  635. vdso_data->platform = 0x100;
  636. if (firmware_has_feature(FW_FEATURE_LPAR))
  637. vdso_data->platform |= 1;
  638. vdso_data->physicalMemorySize = memblock_phys_mem_size();
  639. vdso_data->dcache_size = ppc64_caches.l1d.size;
  640. vdso_data->dcache_line_size = ppc64_caches.l1d.line_size;
  641. vdso_data->icache_size = ppc64_caches.l1i.size;
  642. vdso_data->icache_line_size = ppc64_caches.l1i.line_size;
  643. vdso_data->dcache_block_size = ppc64_caches.l1d.block_size;
  644. vdso_data->icache_block_size = ppc64_caches.l1i.block_size;
  645. vdso_data->dcache_log_block_size = ppc64_caches.l1d.log_block_size;
  646. vdso_data->icache_log_block_size = ppc64_caches.l1i.log_block_size;
  647. /*
  648. * Calculate the size of the 64 bits vDSO
  649. */
  650. vdso64_pages = (&vdso64_end - &vdso64_start) >> PAGE_SHIFT;
  651. DBG("vdso64_kbase: %p, 0x%x pages\n", vdso64_kbase, vdso64_pages);
  652. #else
  653. vdso_data->dcache_block_size = L1_CACHE_BYTES;
  654. vdso_data->dcache_log_block_size = L1_CACHE_SHIFT;
  655. vdso_data->icache_block_size = L1_CACHE_BYTES;
  656. vdso_data->icache_log_block_size = L1_CACHE_SHIFT;
  657. #endif /* CONFIG_PPC64 */
  658. #ifdef CONFIG_VDSO32
  659. vdso32_kbase = &vdso32_start;
  660. /*
  661. * Calculate the size of the 32 bits vDSO
  662. */
  663. vdso32_pages = (&vdso32_end - &vdso32_start) >> PAGE_SHIFT;
  664. DBG("vdso32_kbase: %p, 0x%x pages\n", vdso32_kbase, vdso32_pages);
  665. #endif
  666. /*
  667. * Setup the syscall map in the vDOS
  668. */
  669. vdso_setup_syscall_map();
  670. /*
  671. * Initialize the vDSO images in memory, that is do necessary
  672. * fixups of vDSO symbols, locate trampolines, etc...
  673. */
  674. if (vdso_setup()) {
  675. printk(KERN_ERR "vDSO setup failure, not enabled !\n");
  676. vdso32_pages = 0;
  677. #ifdef CONFIG_PPC64
  678. vdso64_pages = 0;
  679. #endif
  680. return 0;
  681. }
  682. #ifdef CONFIG_VDSO32
  683. /* Make sure pages are in the correct state */
  684. vdso32_pagelist = kcalloc(vdso32_pages + 2, sizeof(struct page *),
  685. GFP_KERNEL);
  686. BUG_ON(vdso32_pagelist == NULL);
  687. for (i = 0; i < vdso32_pages; i++) {
  688. struct page *pg = virt_to_page(vdso32_kbase + i*PAGE_SIZE);
  689. ClearPageReserved(pg);
  690. get_page(pg);
  691. vdso32_pagelist[i] = pg;
  692. }
  693. vdso32_pagelist[i++] = virt_to_page(vdso_data);
  694. vdso32_pagelist[i] = NULL;
  695. #endif
  696. #ifdef CONFIG_PPC64
  697. vdso64_pagelist = kcalloc(vdso64_pages + 2, sizeof(struct page *),
  698. GFP_KERNEL);
  699. BUG_ON(vdso64_pagelist == NULL);
  700. for (i = 0; i < vdso64_pages; i++) {
  701. struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
  702. ClearPageReserved(pg);
  703. get_page(pg);
  704. vdso64_pagelist[i] = pg;
  705. }
  706. vdso64_pagelist[i++] = virt_to_page(vdso_data);
  707. vdso64_pagelist[i] = NULL;
  708. #endif /* CONFIG_PPC64 */
  709. get_page(virt_to_page(vdso_data));
  710. smp_wmb();
  711. vdso_ready = 1;
  712. return 0;
  713. }
  714. arch_initcall(vdso_init);