vdso.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. if (security_vm_enough_memory(vdso_pages)) {
  197. kmem_cache_free(vm_area_cachep, vma);
  198. return -ENOMEM;
  199. }
  200. memset(vma, 0, sizeof(*vma));
  201. /*
  202. * pick a base address for the vDSO in process space. We try to put it
  203. * at vdso_base which is the "natural" base for it, but we might fail
  204. * and end up putting it elsewhere.
  205. */
  206. vdso_base = get_unmapped_area(NULL, vdso_base,
  207. vdso_pages << PAGE_SHIFT, 0, 0);
  208. if (vdso_base & ~PAGE_MASK)
  209. return (int)vdso_base;
  210. current->thread.vdso_base = vdso_base;
  211. vma->vm_mm = mm;
  212. vma->vm_start = current->thread.vdso_base;
  213. /*
  214. * the VMA size is one page more than the vDSO since systemcfg
  215. * is mapped in the last one
  216. */
  217. vma->vm_end = vma->vm_start + ((vdso_pages + 1) << PAGE_SHIFT);
  218. /*
  219. * our vma flags don't have VM_WRITE so by default, the process isn't allowed
  220. * to write those pages.
  221. * gdb can break that with ptrace interface, and thus trigger COW on those
  222. * pages but it's then your responsibility to never do that on the "data" page
  223. * of the vDSO or you'll stop getting kernel updates and your nice userland
  224. * gettimeofday will be totally dead. It's fine to use that for setting
  225. * breakpoints in the vDSO code pages though
  226. */
  227. vma->vm_flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  228. vma->vm_flags |= mm->def_flags;
  229. vma->vm_page_prot = protection_map[vma->vm_flags & 0x7];
  230. vma->vm_ops = &vdso_vmops;
  231. down_write(&mm->mmap_sem);
  232. insert_vm_struct(mm, vma);
  233. mm->total_vm += (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  234. up_write(&mm->mmap_sem);
  235. return 0;
  236. }
  237. static void * __init find_section32(Elf32_Ehdr *ehdr, const char *secname,
  238. unsigned long *size)
  239. {
  240. Elf32_Shdr *sechdrs;
  241. unsigned int i;
  242. char *secnames;
  243. /* Grab section headers and strings so we can tell who is who */
  244. sechdrs = (void *)ehdr + ehdr->e_shoff;
  245. secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
  246. /* Find the section they want */
  247. for (i = 1; i < ehdr->e_shnum; i++) {
  248. if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0) {
  249. if (size)
  250. *size = sechdrs[i].sh_size;
  251. return (void *)ehdr + sechdrs[i].sh_offset;
  252. }
  253. }
  254. *size = 0;
  255. return NULL;
  256. }
  257. static void * __init find_section64(Elf64_Ehdr *ehdr, const char *secname,
  258. unsigned long *size)
  259. {
  260. Elf64_Shdr *sechdrs;
  261. unsigned int i;
  262. char *secnames;
  263. /* Grab section headers and strings so we can tell who is who */
  264. sechdrs = (void *)ehdr + ehdr->e_shoff;
  265. secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
  266. /* Find the section they want */
  267. for (i = 1; i < ehdr->e_shnum; i++) {
  268. if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0) {
  269. if (size)
  270. *size = sechdrs[i].sh_size;
  271. return (void *)ehdr + sechdrs[i].sh_offset;
  272. }
  273. }
  274. if (size)
  275. *size = 0;
  276. return NULL;
  277. }
  278. static Elf32_Sym * __init find_symbol32(struct lib32_elfinfo *lib, const char *symname)
  279. {
  280. unsigned int i;
  281. char name[32], *c;
  282. for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
  283. if (lib->dynsym[i].st_name == 0)
  284. continue;
  285. strlcpy(name, lib->dynstr + lib->dynsym[i].st_name, 32);
  286. c = strchr(name, '@');
  287. if (c)
  288. *c = 0;
  289. if (strcmp(symname, name) == 0)
  290. return &lib->dynsym[i];
  291. }
  292. return NULL;
  293. }
  294. static Elf64_Sym * __init find_symbol64(struct lib64_elfinfo *lib, const char *symname)
  295. {
  296. unsigned int i;
  297. char name[32], *c;
  298. for (i = 0; i < (lib->dynsymsize / sizeof(Elf64_Sym)); i++) {
  299. if (lib->dynsym[i].st_name == 0)
  300. continue;
  301. strlcpy(name, lib->dynstr + lib->dynsym[i].st_name, 32);
  302. c = strchr(name, '@');
  303. if (c)
  304. *c = 0;
  305. if (strcmp(symname, name) == 0)
  306. return &lib->dynsym[i];
  307. }
  308. return NULL;
  309. }
  310. /* Note that we assume the section is .text and the symbol is relative to
  311. * the library base
  312. */
  313. static unsigned long __init find_function32(struct lib32_elfinfo *lib, const char *symname)
  314. {
  315. Elf32_Sym *sym = find_symbol32(lib, symname);
  316. if (sym == NULL) {
  317. printk(KERN_WARNING "vDSO32: function %s not found !\n", symname);
  318. return 0;
  319. }
  320. return sym->st_value - VDSO32_LBASE;
  321. }
  322. /* Note that we assume the section is .text and the symbol is relative to
  323. * the library base
  324. */
  325. static unsigned long __init find_function64(struct lib64_elfinfo *lib, const char *symname)
  326. {
  327. Elf64_Sym *sym = find_symbol64(lib, symname);
  328. if (sym == NULL) {
  329. printk(KERN_WARNING "vDSO64: function %s not found !\n", symname);
  330. return 0;
  331. }
  332. #ifdef VDS64_HAS_DESCRIPTORS
  333. return *((u64 *)(vdso64_kbase + sym->st_value - VDSO64_LBASE)) - VDSO64_LBASE;
  334. #else
  335. return sym->st_value - VDSO64_LBASE;
  336. #endif
  337. }
  338. static __init int vdso_do_find_sections(struct lib32_elfinfo *v32,
  339. struct lib64_elfinfo *v64)
  340. {
  341. void *sect;
  342. /*
  343. * Locate symbol tables & text section
  344. */
  345. v32->dynsym = find_section32(v32->hdr, ".dynsym", &v32->dynsymsize);
  346. v32->dynstr = find_section32(v32->hdr, ".dynstr", NULL);
  347. if (v32->dynsym == NULL || v32->dynstr == NULL) {
  348. printk(KERN_ERR "vDSO32: a required symbol section was not found\n");
  349. return -1;
  350. }
  351. sect = find_section32(v32->hdr, ".text", NULL);
  352. if (sect == NULL) {
  353. printk(KERN_ERR "vDSO32: the .text section was not found\n");
  354. return -1;
  355. }
  356. v32->text = sect - vdso32_kbase;
  357. v64->dynsym = find_section64(v64->hdr, ".dynsym", &v64->dynsymsize);
  358. v64->dynstr = find_section64(v64->hdr, ".dynstr", NULL);
  359. if (v64->dynsym == NULL || v64->dynstr == NULL) {
  360. printk(KERN_ERR "vDSO64: a required symbol section was not found\n");
  361. return -1;
  362. }
  363. sect = find_section64(v64->hdr, ".text", NULL);
  364. if (sect == NULL) {
  365. printk(KERN_ERR "vDSO64: the .text section was not found\n");
  366. return -1;
  367. }
  368. v64->text = sect - vdso64_kbase;
  369. return 0;
  370. }
  371. static __init void vdso_setup_trampolines(struct lib32_elfinfo *v32,
  372. struct lib64_elfinfo *v64)
  373. {
  374. /*
  375. * Find signal trampolines
  376. */
  377. vdso64_rt_sigtramp = find_function64(v64, "__kernel_sigtramp_rt64");
  378. vdso32_sigtramp = find_function32(v32, "__kernel_sigtramp32");
  379. vdso32_rt_sigtramp = find_function32(v32, "__kernel_sigtramp_rt32");
  380. }
  381. static __init int vdso_fixup_datapage(struct lib32_elfinfo *v32,
  382. struct lib64_elfinfo *v64)
  383. {
  384. Elf32_Sym *sym32;
  385. Elf64_Sym *sym64;
  386. sym32 = find_symbol32(v32, "__kernel_datapage_offset");
  387. if (sym32 == NULL) {
  388. printk(KERN_ERR "vDSO32: Can't find symbol __kernel_datapage_offset !\n");
  389. return -1;
  390. }
  391. *((int *)(vdso32_kbase + (sym32->st_value - VDSO32_LBASE))) =
  392. (vdso32_pages << PAGE_SHIFT) - (sym32->st_value - VDSO32_LBASE);
  393. sym64 = find_symbol64(v64, "__kernel_datapage_offset");
  394. if (sym64 == NULL) {
  395. printk(KERN_ERR "vDSO64: Can't find symbol __kernel_datapage_offset !\n");
  396. return -1;
  397. }
  398. *((int *)(vdso64_kbase + sym64->st_value - VDSO64_LBASE)) =
  399. (vdso64_pages << PAGE_SHIFT) - (sym64->st_value - VDSO64_LBASE);
  400. return 0;
  401. }
  402. static int vdso_do_func_patch32(struct lib32_elfinfo *v32,
  403. struct lib64_elfinfo *v64,
  404. const char *orig, const char *fix)
  405. {
  406. Elf32_Sym *sym32_gen, *sym32_fix;
  407. sym32_gen = find_symbol32(v32, orig);
  408. if (sym32_gen == NULL) {
  409. printk(KERN_ERR "vDSO32: Can't find symbol %s !\n", orig);
  410. return -1;
  411. }
  412. sym32_fix = find_symbol32(v32, fix);
  413. if (sym32_fix == NULL) {
  414. printk(KERN_ERR "vDSO32: Can't find symbol %s !\n", fix);
  415. return -1;
  416. }
  417. sym32_gen->st_value = sym32_fix->st_value;
  418. sym32_gen->st_size = sym32_fix->st_size;
  419. sym32_gen->st_info = sym32_fix->st_info;
  420. sym32_gen->st_other = sym32_fix->st_other;
  421. sym32_gen->st_shndx = sym32_fix->st_shndx;
  422. return 0;
  423. }
  424. static int vdso_do_func_patch64(struct lib32_elfinfo *v32,
  425. struct lib64_elfinfo *v64,
  426. const char *orig, const char *fix)
  427. {
  428. Elf64_Sym *sym64_gen, *sym64_fix;
  429. sym64_gen = find_symbol64(v64, orig);
  430. if (sym64_gen == NULL) {
  431. printk(KERN_ERR "vDSO64: Can't find symbol %s !\n", orig);
  432. return -1;
  433. }
  434. sym64_fix = find_symbol64(v64, fix);
  435. if (sym64_fix == NULL) {
  436. printk(KERN_ERR "vDSO64: Can't find symbol %s !\n", fix);
  437. return -1;
  438. }
  439. sym64_gen->st_value = sym64_fix->st_value;
  440. sym64_gen->st_size = sym64_fix->st_size;
  441. sym64_gen->st_info = sym64_fix->st_info;
  442. sym64_gen->st_other = sym64_fix->st_other;
  443. sym64_gen->st_shndx = sym64_fix->st_shndx;
  444. return 0;
  445. }
  446. static __init int vdso_fixup_alt_funcs(struct lib32_elfinfo *v32,
  447. struct lib64_elfinfo *v64)
  448. {
  449. u32 pvr;
  450. int i;
  451. pvr = mfspr(SPRN_PVR);
  452. for (i = 0; i < ARRAY_SIZE(vdso_patches); i++) {
  453. struct vdso_patch_def *patch = &vdso_patches[i];
  454. int match = (pvr & patch->pvr_mask) == patch->pvr_value;
  455. DBG("patch %d (mask: %x, pvr: %x) : %s\n",
  456. i, patch->pvr_mask, patch->pvr_value, match ? "match" : "skip");
  457. if (!match)
  458. continue;
  459. DBG("replacing %s with %s...\n", patch->gen_name, patch->fix_name);
  460. /*
  461. * Patch the 32 bits and 64 bits symbols. Note that we do not patch
  462. * the "." symbol on 64 bits. It would be easy to do, but doesn't
  463. * seem to be necessary, patching the OPD symbol is enough.
  464. */
  465. vdso_do_func_patch32(v32, v64, patch->gen_name, patch->fix_name);
  466. vdso_do_func_patch64(v32, v64, patch->gen_name, patch->fix_name);
  467. }
  468. return 0;
  469. }
  470. static __init int vdso_setup(void)
  471. {
  472. struct lib32_elfinfo v32;
  473. struct lib64_elfinfo v64;
  474. v32.hdr = vdso32_kbase;
  475. v64.hdr = vdso64_kbase;
  476. if (vdso_do_find_sections(&v32, &v64))
  477. return -1;
  478. if (vdso_fixup_datapage(&v32, &v64))
  479. return -1;
  480. if (vdso_fixup_alt_funcs(&v32, &v64))
  481. return -1;
  482. vdso_setup_trampolines(&v32, &v64);
  483. return 0;
  484. }
  485. void __init vdso_init(void)
  486. {
  487. int i;
  488. vdso64_pages = (&vdso64_end - &vdso64_start) >> PAGE_SHIFT;
  489. vdso32_pages = (&vdso32_end - &vdso32_start) >> PAGE_SHIFT;
  490. DBG("vdso64_kbase: %p, 0x%x pages, vdso32_kbase: %p, 0x%x pages\n",
  491. vdso64_kbase, vdso64_pages, vdso32_kbase, vdso32_pages);
  492. /*
  493. * Initialize the vDSO images in memory, that is do necessary
  494. * fixups of vDSO symbols, locate trampolines, etc...
  495. */
  496. if (vdso_setup()) {
  497. printk(KERN_ERR "vDSO setup failure, not enabled !\n");
  498. /* XXX should free pages here ? */
  499. vdso64_pages = vdso32_pages = 0;
  500. return;
  501. }
  502. /* Make sure pages are in the correct state */
  503. for (i = 0; i < vdso64_pages; i++) {
  504. struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
  505. ClearPageReserved(pg);
  506. get_page(pg);
  507. }
  508. for (i = 0; i < vdso32_pages; i++) {
  509. struct page *pg = virt_to_page(vdso32_kbase + i*PAGE_SIZE);
  510. ClearPageReserved(pg);
  511. get_page(pg);
  512. }
  513. }
  514. int in_gate_area_no_task(unsigned long addr)
  515. {
  516. return 0;
  517. }
  518. int in_gate_area(struct task_struct *task, unsigned long addr)
  519. {
  520. return 0;
  521. }
  522. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  523. {
  524. return NULL;
  525. }