vdso.c 20 KB

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