| 1 | |
| 2 | |
| 3 | * vdso setup for s390
|
| 4 | *
|
| 5 | * Copyright IBM Corp. 2008
|
| 6 | * Author(s): Martin Schwidefsky ([email protected])
|
| 7 |
|
| 8 |
|
| 9 | #include <linux/binfmts.h>
|
| 10 | #include <linux/elf.h>
|
| 11 | #include <linux/errno.h>
|
| 12 | #include <linux/init.h>
|
| 13 | #include <linux/kernel.h>
|
| 14 | #include <linux/module.h>
|
| 15 | #include <linux/mm.h>
|
| 16 | #include <linux/slab.h>
|
| 17 | #include <linux/smp.h>
|
| 18 | #include <linux/random.h>
|
| 19 | #include <linux/vdso_datastore.h>
|
| 20 | #include <vdso/datapage.h>
|
| 21 | #include <asm/vdso/vsyscall.h>
|
| 22 | #include <asm/alternative.h>
|
| 23 | #include <asm/vdso.h>
|
| 24 |
|
| 25 | extern char vdso_start[], vdso_end[];
|
| 26 |
|
| 27 | static int vdso_mremap(const struct vm_special_mapping *sm,
|
| 28 | struct vm_area_struct *vma)
|
| 29 | {
|
| 30 | current->mm->context.vdso_base = vma->vm_start;
|
| 31 | return 0;
|
| 32 | }
|
| 33 |
|
| 34 | static struct vm_special_mapping vdso_mapping = {
|
| 35 | .name = "[vdso]",
|
| 36 | .mremap = vdso_mremap,
|
| 37 | };
|
| 38 |
|
| 39 | int vdso_getcpu_init(void)
|
| 40 | {
|
| 41 | set_tod_programmable_field(smp_processor_id());
|
| 42 | return 0;
|
| 43 | }
|
| 44 | early_initcall(vdso_getcpu_init);
|
| 45 |
|
| 46 | static int map_vdso(unsigned long addr, unsigned long vdso_mapping_len)
|
| 47 | {
|
| 48 | unsigned long vvar_start, vdso_text_start, vdso_text_len;
|
| 49 | struct mm_struct *mm = current->mm;
|
| 50 | struct vm_area_struct *vma;
|
| 51 | int rc;
|
| 52 |
|
| 53 | BUILD_BUG_ON(VDSO_NR_PAGES != __VDSO_PAGES);
|
| 54 | if (mmap_write_lock_killable(mm))
|
| 55 | return -EINTR;
|
| 56 |
|
| 57 | vdso_text_len = vdso_end - vdso_start;
|
| 58 | vvar_start = get_unmapped_area(NULL, addr, vdso_mapping_len, 0, 0);
|
| 59 | rc = vvar_start;
|
| 60 | if (IS_ERR_VALUE(vvar_start))
|
| 61 | goto out;
|
| 62 | vma = vdso_install_vvar_mapping(mm, vvar_start);
|
| 63 | rc = PTR_ERR(vma);
|
| 64 | if (IS_ERR(vma))
|
| 65 | goto out;
|
| 66 | vdso_text_start = vvar_start + VDSO_NR_PAGES * PAGE_SIZE;
|
| 67 |
|
| 68 | vma = _install_special_mapping(mm, vdso_text_start, vdso_text_len,
|
| 69 | VM_READ|VM_EXEC|VM_SEALED_SYSMAP|
|
| 70 | VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
|
| 71 | &vdso_mapping);
|
| 72 | if (IS_ERR(vma)) {
|
| 73 | do_munmap(mm, vvar_start, PAGE_SIZE, NULL);
|
| 74 | rc = PTR_ERR(vma);
|
| 75 | } else {
|
| 76 | current->mm->context.vdso_base = vdso_text_start;
|
| 77 | rc = 0;
|
| 78 | }
|
| 79 | out:
|
| 80 | mmap_write_unlock(mm);
|
| 81 | return rc;
|
| 82 | }
|
| 83 |
|
| 84 | static unsigned long vdso_addr(unsigned long start, unsigned long len)
|
| 85 | {
|
| 86 | unsigned long addr, end, offset;
|
| 87 |
|
| 88 | |
| 89 | * Round up the start address. It can start out unaligned as a result
|
| 90 | * of stack start randomization.
|
| 91 |
|
| 92 | start = PAGE_ALIGN(start);
|
| 93 |
|
| 94 |
|
| 95 | end = (start + len + PMD_SIZE - 1) & PMD_MASK;
|
| 96 | if (end >= VDSO_BASE)
|
| 97 | end = VDSO_BASE;
|
| 98 | end -= len;
|
| 99 |
|
| 100 | if (end > start) {
|
| 101 | offset = get_random_u32_below(((end - start) >> PAGE_SHIFT) + 1);
|
| 102 | addr = start + (offset << PAGE_SHIFT);
|
| 103 | } else {
|
| 104 | addr = start;
|
| 105 | }
|
| 106 | return addr;
|
| 107 | }
|
| 108 |
|
| 109 | unsigned long vdso_text_size(void)
|
| 110 | {
|
| 111 | return PAGE_ALIGN(vdso_end - vdso_start);
|
| 112 | }
|
| 113 |
|
| 114 | unsigned long vdso_size(void)
|
| 115 | {
|
| 116 | return vdso_text_size() + VDSO_NR_PAGES * PAGE_SIZE;
|
| 117 | }
|
| 118 |
|
| 119 | int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
|
| 120 | {
|
| 121 | unsigned long addr = VDSO_BASE;
|
| 122 | unsigned long size = vdso_size();
|
| 123 |
|
| 124 | if (current->flags & PF_RANDOMIZE)
|
| 125 | addr = vdso_addr(current->mm->start_stack + PAGE_SIZE, size);
|
| 126 | return map_vdso(addr, size);
|
| 127 | }
|
| 128 |
|
| 129 | static struct page ** __init vdso_setup_pages(void *start, void *end)
|
| 130 | {
|
| 131 | int pages = (end - start) >> PAGE_SHIFT;
|
| 132 | struct page **pagelist;
|
| 133 | int i;
|
| 134 |
|
| 135 | pagelist = kzalloc_objs(struct page *, pages + 1);
|
| 136 | if (!pagelist)
|
| 137 | panic("%s: Cannot allocate page list for VDSO", __func__);
|
| 138 | for (i = 0; i < pages; i++)
|
| 139 | pagelist[i] = virt_to_page(start + i * PAGE_SIZE);
|
| 140 | return pagelist;
|
| 141 | }
|
| 142 |
|
| 143 | static void vdso_apply_alternatives(void)
|
| 144 | {
|
| 145 | const struct elf64_shdr *alt, *shdr;
|
| 146 | struct alt_instr *start, *end;
|
| 147 | const struct elf64_hdr *hdr;
|
| 148 |
|
| 149 | hdr = (struct elf64_hdr *)vdso_start;
|
| 150 | shdr = (void *)hdr + hdr->e_shoff;
|
| 151 | alt = find_section(hdr, shdr, ".altinstructions");
|
| 152 | if (!alt)
|
| 153 | return;
|
| 154 | start = (void *)hdr + alt->sh_offset;
|
| 155 | end = (void *)hdr + alt->sh_offset + alt->sh_size;
|
| 156 | apply_alternatives(start, end);
|
| 157 | }
|
| 158 |
|
| 159 | static int __init vdso_init(void)
|
| 160 | {
|
| 161 | vdso_apply_alternatives();
|
| 162 | vdso_mapping.pages = vdso_setup_pages(vdso_start, vdso_end);
|
| 163 | return 0;
|
| 164 | }
|
| 165 | arch_initcall(vdso_init);
|