vdso.c 21 KB

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