vdso.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * linux/arch/ppc64/kernel/vdso.c
  3. *
  4. * Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp.
  5. * <benh@kernel.crashing.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/config.h>
  13. #include <linux/module.h>
  14. #include <linux/errno.h>
  15. #include <linux/sched.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/smp.h>
  19. #include <linux/smp_lock.h>
  20. #include <linux/stddef.h>
  21. #include <linux/unistd.h>
  22. #include <linux/slab.h>
  23. #include <linux/user.h>
  24. #include <linux/elf.h>
  25. #include <linux/security.h>
  26. #include <linux/bootmem.h>
  27. #include <asm/pgtable.h>
  28. #include <asm/system.h>
  29. #include <asm/processor.h>
  30. #include <asm/mmu.h>
  31. #include <asm/mmu_context.h>
  32. #include <asm/machdep.h>
  33. #include <asm/cputable.h>
  34. #include <asm/sections.h>
  35. #include <asm/vdso.h>
  36. #undef DEBUG
  37. #ifdef DEBUG
  38. #define DBG(fmt...) printk(fmt)
  39. #else
  40. #define DBG(fmt...)
  41. #endif
  42. /*
  43. * The vDSOs themselves are here
  44. */
  45. extern char vdso64_start, vdso64_end;
  46. extern char vdso32_start, vdso32_end;
  47. static void *vdso64_kbase = &vdso64_start;
  48. static void *vdso32_kbase = &vdso32_start;
  49. unsigned int vdso64_pages;
  50. unsigned int vdso32_pages;
  51. /* Signal trampolines user addresses */
  52. unsigned long vdso64_rt_sigtramp;
  53. unsigned long vdso32_sigtramp;
  54. unsigned long vdso32_rt_sigtramp;
  55. /* Format of the patch table */
  56. struct vdso_patch_def
  57. {
  58. u32 pvr_mask, pvr_value;
  59. const char *gen_name;
  60. const char *fix_name;
  61. };
  62. /* Table of functions to patch based on the CPU type/revision
  63. *
  64. * TODO: Improve by adding whole lists for each entry
  65. */
  66. static struct vdso_patch_def vdso_patches[] = {
  67. {
  68. 0xffff0000, 0x003a0000, /* POWER5 */
  69. "__kernel_sync_dicache", "__kernel_sync_dicache_p5"
  70. },
  71. {
  72. 0xffff0000, 0x003b0000, /* POWER5 */
  73. "__kernel_sync_dicache", "__kernel_sync_dicache_p5"
  74. },
  75. };
  76. /*
  77. * Some infos carried around for each of them during parsing at
  78. * boot time.
  79. */
  80. struct lib32_elfinfo
  81. {
  82. Elf32_Ehdr *hdr; /* ptr to ELF */
  83. Elf32_Sym *dynsym; /* ptr to .dynsym section */
  84. unsigned long dynsymsize; /* size of .dynsym section */
  85. char *dynstr; /* ptr to .dynstr section */
  86. unsigned long text; /* offset of .text section in .so */
  87. };
  88. struct lib64_elfinfo
  89. {
  90. Elf64_Ehdr *hdr;
  91. Elf64_Sym *dynsym;
  92. unsigned long dynsymsize;
  93. char *dynstr;
  94. unsigned long text;
  95. };
  96. #ifdef __DEBUG
  97. static void dump_one_vdso_page(struct page *pg, struct page *upg)
  98. {
  99. printk("kpg: %p (c:%d,f:%08lx)", __va(page_to_pfn(pg) << PAGE_SHIFT),
  100. page_count(pg),
  101. pg->flags);
  102. if (upg/* && pg != upg*/) {
  103. printk(" upg: %p (c:%d,f:%08lx)", __va(page_to_pfn(upg) << PAGE_SHIFT),
  104. page_count(upg),
  105. upg->flags);
  106. }
  107. printk("\n");
  108. }
  109. static void dump_vdso_pages(struct vm_area_struct * vma)
  110. {
  111. int i;
  112. if (!vma || test_thread_flag(TIF_32BIT)) {
  113. printk("vDSO32 @ %016lx:\n", (unsigned long)vdso32_kbase);
  114. for (i=0; i<vdso32_pages; i++) {
  115. struct page *pg = virt_to_page(vdso32_kbase + i*PAGE_SIZE);
  116. struct page *upg = (vma && vma->vm_mm) ?
  117. follow_page(vma->vm_mm, vma->vm_start + i*PAGE_SIZE, 0)
  118. : NULL;
  119. dump_one_vdso_page(pg, upg);
  120. }
  121. }
  122. if (!vma || !test_thread_flag(TIF_32BIT)) {
  123. printk("vDSO64 @ %016lx:\n", (unsigned long)vdso64_kbase);
  124. for (i=0; i<vdso64_pages; i++) {
  125. struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
  126. struct page *upg = (vma && vma->vm_mm) ?
  127. follow_page(vma->vm_mm, vma->vm_start + i*PAGE_SIZE, 0)
  128. : NULL;
  129. dump_one_vdso_page(pg, upg);
  130. }
  131. }
  132. }
  133. #endif /* DEBUG */
  134. /*
  135. * Keep a dummy vma_close for now, it will prevent VMA merging.
  136. */
  137. static void vdso_vma_close(struct vm_area_struct * vma)
  138. {
  139. }
  140. /*
  141. * Our nopage() function, maps in the actual vDSO kernel pages, they will
  142. * be mapped read-only by do_no_page(), and eventually COW'ed, either
  143. * right away for an initial write access, or by do_wp_page().
  144. */
  145. static struct page * vdso_vma_nopage(struct vm_area_struct * vma,
  146. unsigned long address, int *type)
  147. {
  148. unsigned long offset = address - vma->vm_start;
  149. struct page *pg;
  150. void *vbase = test_thread_flag(TIF_32BIT) ? vdso32_kbase : vdso64_kbase;
  151. DBG("vdso_vma_nopage(current: %s, address: %016lx, off: %lx)\n",
  152. current->comm, address, offset);
  153. if (address < vma->vm_start || address > vma->vm_end)
  154. return NOPAGE_SIGBUS;
  155. /*
  156. * Last page is systemcfg, special handling here, no get_page() a
  157. * this is a reserved page
  158. */
  159. if ((vma->vm_end - address) <= PAGE_SIZE)
  160. return virt_to_page(systemcfg);
  161. pg = virt_to_page(vbase + offset);
  162. get_page(pg);
  163. DBG(" ->page count: %d\n", page_count(pg));
  164. return pg;
  165. }
  166. static struct vm_operations_struct vdso_vmops = {
  167. .close = vdso_vma_close,
  168. .nopage = vdso_vma_nopage,
  169. };
  170. /*
  171. * This is called from binfmt_elf, we create the special vma for the
  172. * vDSO and insert it into the mm struct tree
  173. */
  174. int arch_setup_additional_pages(struct linux_binprm *bprm, int executable_stack)
  175. {
  176. struct mm_struct *mm = current->mm;
  177. struct vm_area_struct *vma;
  178. unsigned long vdso_pages;
  179. unsigned long vdso_base;
  180. if (test_thread_flag(TIF_32BIT)) {
  181. vdso_pages = vdso32_pages;
  182. vdso_base = VDSO32_MBASE;
  183. } else {
  184. vdso_pages = vdso64_pages;
  185. vdso_base = VDSO64_MBASE;
  186. }
  187. current->thread.vdso_base = 0;
  188. /* vDSO has a problem and was disabled, just don't "enable" it for the
  189. * process
  190. */
  191. if (vdso_pages == 0)
  192. return 0;
  193. vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  194. if (vma == NULL)
  195. return -ENOMEM;
  196. memset(vma, 0, sizeof(*vma));
  197. /*
  198. * pick a base address for the vDSO in process space. We try to put it
  199. * at vdso_base which is the "natural" base for it, but we might fail
  200. * and end up putting it elsewhere.
  201. */
  202. vdso_base = get_unmapped_area(NULL, vdso_base,
  203. vdso_pages << PAGE_SHIFT, 0, 0);
  204. if (vdso_base & ~PAGE_MASK) {
  205. kmem_cache_free(vm_area_cachep, vma);
  206. return (int)vdso_base;
  207. }
  208. current->thread.vdso_base = vdso_base;
  209. vma->vm_mm = mm;
  210. vma->vm_start = current->thread.vdso_base;
  211. /*
  212. * the VMA size is one page more than the vDSO since systemcfg
  213. * is mapped in the last one
  214. */
  215. vma->vm_end = vma->vm_start + ((vdso_pages + 1) << PAGE_SHIFT);
  216. /*
  217. * our vma flags don't have VM_WRITE so by default, the process isn't allowed
  218. * to write those pages.
  219. * gdb can break that with ptrace interface, and thus trigger COW on those
  220. * pages but it's then your responsibility to never do that on the "data" page
  221. * of the vDSO or you'll stop getting kernel updates and your nice userland
  222. * gettimeofday will be totally dead. It's fine to use that for setting
  223. * breakpoints in the vDSO code pages though
  224. */
  225. vma->vm_flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  226. vma->vm_flags |= mm->def_flags;
  227. vma->vm_page_prot = protection_map[vma->vm_flags & 0x7];
  228. vma->vm_ops = &vdso_vmops;
  229. down_write(&mm->mmap_sem);
  230. if (insert_vm_struct(mm, vma)) {
  231. up_write(&mm->mmap_sem);
  232. kmem_cache_free(vm_area_cachep, vma);
  233. return -ENOMEM;
  234. }
  235. mm->total_vm += (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  236. up_write(&mm->mmap_sem);
  237. return 0;
  238. }
  239. static void * __init find_section32(Elf32_Ehdr *ehdr, const char *secname,
  240. unsigned long *size)
  241. {
  242. Elf32_Shdr *sechdrs;
  243. unsigned int i;
  244. char *secnames;
  245. /* Grab section headers and strings so we can tell who is who */
  246. sechdrs = (void *)ehdr + ehdr->e_shoff;
  247. secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
  248. /* Find the section they want */
  249. for (i = 1; i < ehdr->e_shnum; i++) {
  250. if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0) {
  251. if (size)
  252. *size = sechdrs[i].sh_size;
  253. return (void *)ehdr + sechdrs[i].sh_offset;
  254. }
  255. }
  256. *size = 0;
  257. return NULL;
  258. }
  259. static void * __init find_section64(Elf64_Ehdr *ehdr, const char *secname,
  260. unsigned long *size)
  261. {
  262. Elf64_Shdr *sechdrs;
  263. unsigned int i;
  264. char *secnames;
  265. /* Grab section headers and strings so we can tell who is who */
  266. sechdrs = (void *)ehdr + ehdr->e_shoff;
  267. secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
  268. /* Find the section they want */
  269. for (i = 1; i < ehdr->e_shnum; i++) {
  270. if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0) {
  271. if (size)
  272. *size = sechdrs[i].sh_size;
  273. return (void *)ehdr + sechdrs[i].sh_offset;
  274. }
  275. }
  276. if (size)
  277. *size = 0;
  278. return NULL;
  279. }
  280. static Elf32_Sym * __init find_symbol32(struct lib32_elfinfo *lib, const char *symname)
  281. {
  282. unsigned int i;
  283. char name[32], *c;
  284. for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
  285. if (lib->dynsym[i].st_name == 0)
  286. continue;
  287. strlcpy(name, lib->dynstr + lib->dynsym[i].st_name, 32);
  288. c = strchr(name, '@');
  289. if (c)
  290. *c = 0;
  291. if (strcmp(symname, name) == 0)
  292. return &lib->dynsym[i];
  293. }
  294. return NULL;
  295. }
  296. static Elf64_Sym * __init find_symbol64(struct lib64_elfinfo *lib, const char *symname)
  297. {
  298. unsigned int i;
  299. char name[32], *c;
  300. for (i = 0; i < (lib->dynsymsize / sizeof(Elf64_Sym)); i++) {
  301. if (lib->dynsym[i].st_name == 0)
  302. continue;
  303. strlcpy(name, lib->dynstr + lib->dynsym[i].st_name, 32);
  304. c = strchr(name, '@');
  305. if (c)
  306. *c = 0;
  307. if (strcmp(symname, name) == 0)
  308. return &lib->dynsym[i];
  309. }
  310. return NULL;
  311. }
  312. /* Note that we assume the section is .text and the symbol is relative to
  313. * the library base
  314. */
  315. static unsigned long __init find_function32(struct lib32_elfinfo *lib, const char *symname)
  316. {
  317. Elf32_Sym *sym = find_symbol32(lib, symname);
  318. if (sym == NULL) {
  319. printk(KERN_WARNING "vDSO32: function %s not found !\n", symname);
  320. return 0;
  321. }
  322. return sym->st_value - VDSO32_LBASE;
  323. }
  324. /* Note that we assume the section is .text and the symbol is relative to
  325. * the library base
  326. */
  327. static unsigned long __init find_function64(struct lib64_elfinfo *lib, const char *symname)
  328. {
  329. Elf64_Sym *sym = find_symbol64(lib, symname);
  330. if (sym == NULL) {
  331. printk(KERN_WARNING "vDSO64: function %s not found !\n", symname);
  332. return 0;
  333. }
  334. #ifdef VDS64_HAS_DESCRIPTORS
  335. return *((u64 *)(vdso64_kbase + sym->st_value - VDSO64_LBASE)) - VDSO64_LBASE;
  336. #else
  337. return sym->st_value - VDSO64_LBASE;
  338. #endif
  339. }
  340. static __init int vdso_do_find_sections(struct lib32_elfinfo *v32,
  341. struct lib64_elfinfo *v64)
  342. {
  343. void *sect;
  344. /*
  345. * Locate symbol tables & text section
  346. */
  347. v32->dynsym = find_section32(v32->hdr, ".dynsym", &v32->dynsymsize);
  348. v32->dynstr = find_section32(v32->hdr, ".dynstr", NULL);
  349. if (v32->dynsym == NULL || v32->dynstr == NULL) {
  350. printk(KERN_ERR "vDSO32: a required symbol section was not found\n");
  351. return -1;
  352. }
  353. sect = find_section32(v32->hdr, ".text", NULL);
  354. if (sect == NULL) {
  355. printk(KERN_ERR "vDSO32: the .text section was not found\n");
  356. return -1;
  357. }
  358. v32->text = sect - vdso32_kbase;
  359. v64->dynsym = find_section64(v64->hdr, ".dynsym", &v64->dynsymsize);
  360. v64->dynstr = find_section64(v64->hdr, ".dynstr", NULL);
  361. if (v64->dynsym == NULL || v64->dynstr == NULL) {
  362. printk(KERN_ERR "vDSO64: a required symbol section was not found\n");
  363. return -1;
  364. }
  365. sect = find_section64(v64->hdr, ".text", NULL);
  366. if (sect == NULL) {
  367. printk(KERN_ERR "vDSO64: the .text section was not found\n");
  368. return -1;
  369. }
  370. v64->text = sect - vdso64_kbase;
  371. return 0;
  372. }
  373. static __init void vdso_setup_trampolines(struct lib32_elfinfo *v32,
  374. struct lib64_elfinfo *v64)
  375. {
  376. /*
  377. * Find signal trampolines
  378. */
  379. vdso64_rt_sigtramp = find_function64(v64, "__kernel_sigtramp_rt64");
  380. vdso32_sigtramp = find_function32(v32, "__kernel_sigtramp32");
  381. vdso32_rt_sigtramp = find_function32(v32, "__kernel_sigtramp_rt32");
  382. }
  383. static __init int vdso_fixup_datapage(struct lib32_elfinfo *v32,
  384. struct lib64_elfinfo *v64)
  385. {
  386. Elf32_Sym *sym32;
  387. Elf64_Sym *sym64;
  388. sym32 = find_symbol32(v32, "__kernel_datapage_offset");
  389. if (sym32 == NULL) {
  390. printk(KERN_ERR "vDSO32: Can't find symbol __kernel_datapage_offset !\n");
  391. return -1;
  392. }
  393. *((int *)(vdso32_kbase + (sym32->st_value - VDSO32_LBASE))) =
  394. (vdso32_pages << PAGE_SHIFT) - (sym32->st_value - VDSO32_LBASE);
  395. sym64 = find_symbol64(v64, "__kernel_datapage_offset");
  396. if (sym64 == NULL) {
  397. printk(KERN_ERR "vDSO64: Can't find symbol __kernel_datapage_offset !\n");
  398. return -1;
  399. }
  400. *((int *)(vdso64_kbase + sym64->st_value - VDSO64_LBASE)) =
  401. (vdso64_pages << PAGE_SHIFT) - (sym64->st_value - VDSO64_LBASE);
  402. return 0;
  403. }
  404. static int vdso_do_func_patch32(struct lib32_elfinfo *v32,
  405. struct lib64_elfinfo *v64,
  406. const char *orig, const char *fix)
  407. {
  408. Elf32_Sym *sym32_gen, *sym32_fix;
  409. sym32_gen = find_symbol32(v32, orig);
  410. if (sym32_gen == NULL) {
  411. printk(KERN_ERR "vDSO32: Can't find symbol %s !\n", orig);
  412. return -1;
  413. }
  414. sym32_fix = find_symbol32(v32, fix);
  415. if (sym32_fix == NULL) {
  416. printk(KERN_ERR "vDSO32: Can't find symbol %s !\n", fix);
  417. return -1;
  418. }
  419. sym32_gen->st_value = sym32_fix->st_value;
  420. sym32_gen->st_size = sym32_fix->st_size;
  421. sym32_gen->st_info = sym32_fix->st_info;
  422. sym32_gen->st_other = sym32_fix->st_other;
  423. sym32_gen->st_shndx = sym32_fix->st_shndx;
  424. return 0;
  425. }
  426. static int vdso_do_func_patch64(struct lib32_elfinfo *v32,
  427. struct lib64_elfinfo *v64,
  428. const char *orig, const char *fix)
  429. {
  430. Elf64_Sym *sym64_gen, *sym64_fix;
  431. sym64_gen = find_symbol64(v64, orig);
  432. if (sym64_gen == NULL) {
  433. printk(KERN_ERR "vDSO64: Can't find symbol %s !\n", orig);
  434. return -1;
  435. }
  436. sym64_fix = find_symbol64(v64, fix);
  437. if (sym64_fix == NULL) {
  438. printk(KERN_ERR "vDSO64: Can't find symbol %s !\n", fix);
  439. return -1;
  440. }
  441. sym64_gen->st_value = sym64_fix->st_value;
  442. sym64_gen->st_size = sym64_fix->st_size;
  443. sym64_gen->st_info = sym64_fix->st_info;
  444. sym64_gen->st_other = sym64_fix->st_other;
  445. sym64_gen->st_shndx = sym64_fix->st_shndx;
  446. return 0;
  447. }
  448. static __init int vdso_fixup_alt_funcs(struct lib32_elfinfo *v32,
  449. struct lib64_elfinfo *v64)
  450. {
  451. u32 pvr;
  452. int i;
  453. pvr = mfspr(SPRN_PVR);
  454. for (i = 0; i < ARRAY_SIZE(vdso_patches); i++) {
  455. struct vdso_patch_def *patch = &vdso_patches[i];
  456. int match = (pvr & patch->pvr_mask) == patch->pvr_value;
  457. DBG("patch %d (mask: %x, pvr: %x) : %s\n",
  458. i, patch->pvr_mask, patch->pvr_value, match ? "match" : "skip");
  459. if (!match)
  460. continue;
  461. DBG("replacing %s with %s...\n", patch->gen_name, patch->fix_name);
  462. /*
  463. * Patch the 32 bits and 64 bits symbols. Note that we do not patch
  464. * the "." symbol on 64 bits. It would be easy to do, but doesn't
  465. * seem to be necessary, patching the OPD symbol is enough.
  466. */
  467. vdso_do_func_patch32(v32, v64, patch->gen_name, patch->fix_name);
  468. vdso_do_func_patch64(v32, v64, patch->gen_name, patch->fix_name);
  469. }
  470. return 0;
  471. }
  472. static __init int vdso_setup(void)
  473. {
  474. struct lib32_elfinfo v32;
  475. struct lib64_elfinfo v64;
  476. v32.hdr = vdso32_kbase;
  477. v64.hdr = vdso64_kbase;
  478. if (vdso_do_find_sections(&v32, &v64))
  479. return -1;
  480. if (vdso_fixup_datapage(&v32, &v64))
  481. return -1;
  482. if (vdso_fixup_alt_funcs(&v32, &v64))
  483. return -1;
  484. vdso_setup_trampolines(&v32, &v64);
  485. return 0;
  486. }
  487. void __init vdso_init(void)
  488. {
  489. int i;
  490. vdso64_pages = (&vdso64_end - &vdso64_start) >> PAGE_SHIFT;
  491. vdso32_pages = (&vdso32_end - &vdso32_start) >> PAGE_SHIFT;
  492. DBG("vdso64_kbase: %p, 0x%x pages, vdso32_kbase: %p, 0x%x pages\n",
  493. vdso64_kbase, vdso64_pages, vdso32_kbase, vdso32_pages);
  494. /*
  495. * Initialize the vDSO images in memory, that is do necessary
  496. * fixups of vDSO symbols, locate trampolines, etc...
  497. */
  498. if (vdso_setup()) {
  499. printk(KERN_ERR "vDSO setup failure, not enabled !\n");
  500. /* XXX should free pages here ? */
  501. vdso64_pages = vdso32_pages = 0;
  502. return;
  503. }
  504. /* Make sure pages are in the correct state */
  505. for (i = 0; i < vdso64_pages; i++) {
  506. struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
  507. ClearPageReserved(pg);
  508. get_page(pg);
  509. }
  510. for (i = 0; i < vdso32_pages; i++) {
  511. struct page *pg = virt_to_page(vdso32_kbase + i*PAGE_SIZE);
  512. ClearPageReserved(pg);
  513. get_page(pg);
  514. }
  515. }
  516. int in_gate_area_no_task(unsigned long addr)
  517. {
  518. return 0;
  519. }
  520. int in_gate_area(struct task_struct *task, unsigned long addr)
  521. {
  522. return 0;
  523. }
  524. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  525. {
  526. return NULL;
  527. }