0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/errno.h>
0009 #include <linux/sched.h>
0010 #include <linux/kernel.h>
0011 #include <linux/mm.h>
0012 #include <linux/smp.h>
0013 #include <linux/stddef.h>
0014 #include <linux/unistd.h>
0015 #include <linux/slab.h>
0016 #include <linux/user.h>
0017 #include <linux/elf.h>
0018 #include <linux/security.h>
0019 #include <linux/memblock.h>
0020 #include <linux/syscalls.h>
0021 #include <linux/time_namespace.h>
0022 #include <vdso/datapage.h>
0023
0024 #include <asm/syscall.h>
0025 #include <asm/processor.h>
0026 #include <asm/mmu.h>
0027 #include <asm/mmu_context.h>
0028 #include <asm/machdep.h>
0029 #include <asm/cputable.h>
0030 #include <asm/sections.h>
0031 #include <asm/firmware.h>
0032 #include <asm/vdso.h>
0033 #include <asm/vdso_datapage.h>
0034 #include <asm/setup.h>
0035
0036
0037 #define VDSO_ALIGNMENT (1 << 16)
0038
0039 extern char vdso32_start, vdso32_end;
0040 extern char vdso64_start, vdso64_end;
0041
0042
0043
0044
0045
0046
0047 static union {
0048 struct vdso_arch_data data;
0049 u8 page[PAGE_SIZE];
0050 } vdso_data_store __page_aligned_data;
0051 struct vdso_arch_data *vdso_data = &vdso_data_store.data;
0052
0053 enum vvar_pages {
0054 VVAR_DATA_PAGE_OFFSET,
0055 VVAR_TIMENS_PAGE_OFFSET,
0056 VVAR_NR_PAGES,
0057 };
0058
0059 static int vdso_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma,
0060 unsigned long text_size)
0061 {
0062 unsigned long new_size = new_vma->vm_end - new_vma->vm_start;
0063
0064 if (new_size != text_size)
0065 return -EINVAL;
0066
0067 current->mm->context.vdso = (void __user *)new_vma->vm_start;
0068
0069 return 0;
0070 }
0071
0072 static int vdso32_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma)
0073 {
0074 return vdso_mremap(sm, new_vma, &vdso32_end - &vdso32_start);
0075 }
0076
0077 static int vdso64_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma)
0078 {
0079 return vdso_mremap(sm, new_vma, &vdso64_end - &vdso64_start);
0080 }
0081
0082 static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
0083 struct vm_area_struct *vma, struct vm_fault *vmf);
0084
0085 static struct vm_special_mapping vvar_spec __ro_after_init = {
0086 .name = "[vvar]",
0087 .fault = vvar_fault,
0088 };
0089
0090 static struct vm_special_mapping vdso32_spec __ro_after_init = {
0091 .name = "[vdso]",
0092 .mremap = vdso32_mremap,
0093 };
0094
0095 static struct vm_special_mapping vdso64_spec __ro_after_init = {
0096 .name = "[vdso]",
0097 .mremap = vdso64_mremap,
0098 };
0099
0100 #ifdef CONFIG_TIME_NS
0101 struct vdso_data *arch_get_vdso_data(void *vvar_page)
0102 {
0103 return ((struct vdso_arch_data *)vvar_page)->data;
0104 }
0105
0106
0107
0108
0109
0110
0111
0112
0113 int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
0114 {
0115 struct mm_struct *mm = task->mm;
0116 struct vm_area_struct *vma;
0117
0118 mmap_read_lock(mm);
0119
0120 for (vma = mm->mmap; vma; vma = vma->vm_next) {
0121 unsigned long size = vma->vm_end - vma->vm_start;
0122
0123 if (vma_is_special_mapping(vma, &vvar_spec))
0124 zap_page_range(vma, vma->vm_start, size);
0125 }
0126
0127 mmap_read_unlock(mm);
0128 return 0;
0129 }
0130
0131 static struct page *find_timens_vvar_page(struct vm_area_struct *vma)
0132 {
0133 if (likely(vma->vm_mm == current->mm))
0134 return current->nsproxy->time_ns->vvar_page;
0135
0136
0137
0138
0139
0140
0141
0142
0143 WARN(1, "vvar_page accessed remotely");
0144
0145 return NULL;
0146 }
0147 #else
0148 static struct page *find_timens_vvar_page(struct vm_area_struct *vma)
0149 {
0150 return NULL;
0151 }
0152 #endif
0153
0154 static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
0155 struct vm_area_struct *vma, struct vm_fault *vmf)
0156 {
0157 struct page *timens_page = find_timens_vvar_page(vma);
0158 unsigned long pfn;
0159
0160 switch (vmf->pgoff) {
0161 case VVAR_DATA_PAGE_OFFSET:
0162 if (timens_page)
0163 pfn = page_to_pfn(timens_page);
0164 else
0165 pfn = virt_to_pfn(vdso_data);
0166 break;
0167 #ifdef CONFIG_TIME_NS
0168 case VVAR_TIMENS_PAGE_OFFSET:
0169
0170
0171
0172
0173
0174
0175
0176 if (!timens_page)
0177 return VM_FAULT_SIGBUS;
0178 pfn = virt_to_pfn(vdso_data);
0179 break;
0180 #endif
0181 default:
0182 return VM_FAULT_SIGBUS;
0183 }
0184
0185 return vmf_insert_pfn(vma, vmf->address, pfn);
0186 }
0187
0188
0189
0190
0191
0192 static int __arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
0193 {
0194 unsigned long vdso_size, vdso_base, mappings_size;
0195 struct vm_special_mapping *vdso_spec;
0196 unsigned long vvar_size = VVAR_NR_PAGES * PAGE_SIZE;
0197 struct mm_struct *mm = current->mm;
0198 struct vm_area_struct *vma;
0199
0200 if (is_32bit_task()) {
0201 vdso_spec = &vdso32_spec;
0202 vdso_size = &vdso32_end - &vdso32_start;
0203 vdso_base = VDSO32_MBASE;
0204 } else {
0205 vdso_spec = &vdso64_spec;
0206 vdso_size = &vdso64_end - &vdso64_start;
0207
0208
0209
0210
0211
0212 vdso_base = 0;
0213 }
0214
0215 mappings_size = vdso_size + vvar_size;
0216 mappings_size += (VDSO_ALIGNMENT - 1) & PAGE_MASK;
0217
0218
0219
0220
0221
0222
0223
0224 vdso_base = get_unmapped_area(NULL, vdso_base, mappings_size, 0, 0);
0225 if (IS_ERR_VALUE(vdso_base))
0226 return vdso_base;
0227
0228
0229 vdso_base = ALIGN(vdso_base, VDSO_ALIGNMENT);
0230
0231
0232
0233
0234
0235
0236 mm->context.vdso = (void __user *)vdso_base + vvar_size;
0237
0238 vma = _install_special_mapping(mm, vdso_base, vvar_size,
0239 VM_READ | VM_MAYREAD | VM_IO |
0240 VM_DONTDUMP | VM_PFNMAP, &vvar_spec);
0241 if (IS_ERR(vma))
0242 return PTR_ERR(vma);
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254 vma = _install_special_mapping(mm, vdso_base + vvar_size, vdso_size,
0255 VM_READ | VM_EXEC | VM_MAYREAD |
0256 VM_MAYWRITE | VM_MAYEXEC, vdso_spec);
0257 if (IS_ERR(vma))
0258 do_munmap(mm, vdso_base, vvar_size, NULL);
0259
0260 return PTR_ERR_OR_ZERO(vma);
0261 }
0262
0263 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
0264 {
0265 struct mm_struct *mm = current->mm;
0266 int rc;
0267
0268 mm->context.vdso = NULL;
0269
0270 if (mmap_write_lock_killable(mm))
0271 return -EINTR;
0272
0273 rc = __arch_setup_additional_pages(bprm, uses_interp);
0274 if (rc)
0275 mm->context.vdso = NULL;
0276
0277 mmap_write_unlock(mm);
0278 return rc;
0279 }
0280
0281 #define VDSO_DO_FIXUPS(type, value, bits, sec) do { \
0282 void *__start = (void *)VDSO##bits##_SYMBOL(&vdso##bits##_start, sec##_start); \
0283 void *__end = (void *)VDSO##bits##_SYMBOL(&vdso##bits##_start, sec##_end); \
0284 \
0285 do_##type##_fixups((value), __start, __end); \
0286 } while (0)
0287
0288 static void __init vdso_fixup_features(void)
0289 {
0290 #ifdef CONFIG_PPC64
0291 VDSO_DO_FIXUPS(feature, cur_cpu_spec->cpu_features, 64, ftr_fixup);
0292 VDSO_DO_FIXUPS(feature, cur_cpu_spec->mmu_features, 64, mmu_ftr_fixup);
0293 VDSO_DO_FIXUPS(feature, powerpc_firmware_features, 64, fw_ftr_fixup);
0294 VDSO_DO_FIXUPS(lwsync, cur_cpu_spec->cpu_features, 64, lwsync_fixup);
0295 #endif
0296
0297 #ifdef CONFIG_VDSO32
0298 VDSO_DO_FIXUPS(feature, cur_cpu_spec->cpu_features, 32, ftr_fixup);
0299 VDSO_DO_FIXUPS(feature, cur_cpu_spec->mmu_features, 32, mmu_ftr_fixup);
0300 #ifdef CONFIG_PPC64
0301 VDSO_DO_FIXUPS(feature, powerpc_firmware_features, 32, fw_ftr_fixup);
0302 #endif
0303 VDSO_DO_FIXUPS(lwsync, cur_cpu_spec->cpu_features, 32, lwsync_fixup);
0304 #endif
0305 }
0306
0307
0308
0309
0310
0311 static void __init vdso_setup_syscall_map(void)
0312 {
0313 unsigned int i;
0314
0315 for (i = 0; i < NR_syscalls; i++) {
0316 if (sys_call_table[i] != (unsigned long)&sys_ni_syscall)
0317 vdso_data->syscall_map[i >> 5] |= 0x80000000UL >> (i & 0x1f);
0318 if (IS_ENABLED(CONFIG_COMPAT) &&
0319 compat_sys_call_table[i] != (unsigned long)&sys_ni_syscall)
0320 vdso_data->compat_syscall_map[i >> 5] |= 0x80000000UL >> (i & 0x1f);
0321 }
0322 }
0323
0324 #ifdef CONFIG_PPC64
0325 int vdso_getcpu_init(void)
0326 {
0327 unsigned long cpu, node, val;
0328
0329
0330
0331
0332
0333 cpu = get_cpu();
0334 WARN_ON_ONCE(cpu > 0xffff);
0335
0336 node = cpu_to_node(cpu);
0337 WARN_ON_ONCE(node > 0xffff);
0338
0339 val = (cpu & 0xffff) | ((node & 0xffff) << 16);
0340 mtspr(SPRN_SPRG_VDSO_WRITE, val);
0341 get_paca()->sprg_vdso = val;
0342
0343 put_cpu();
0344
0345 return 0;
0346 }
0347
0348 early_initcall(vdso_getcpu_init);
0349 #endif
0350
0351 static struct page ** __init vdso_setup_pages(void *start, void *end)
0352 {
0353 int i;
0354 struct page **pagelist;
0355 int pages = (end - start) >> PAGE_SHIFT;
0356
0357 pagelist = kcalloc(pages + 1, sizeof(struct page *), GFP_KERNEL);
0358 if (!pagelist)
0359 panic("%s: Cannot allocate page list for VDSO", __func__);
0360
0361 for (i = 0; i < pages; i++)
0362 pagelist[i] = virt_to_page(start + i * PAGE_SIZE);
0363
0364 return pagelist;
0365 }
0366
0367 static int __init vdso_init(void)
0368 {
0369 #ifdef CONFIG_PPC64
0370
0371
0372
0373 strcpy((char *)vdso_data->eye_catcher, "SYSTEMCFG:PPC64");
0374 vdso_data->version.major = SYSTEMCFG_MAJOR;
0375 vdso_data->version.minor = SYSTEMCFG_MINOR;
0376 vdso_data->processor = mfspr(SPRN_PVR);
0377
0378
0379
0380
0381 vdso_data->platform = 0x100;
0382 if (firmware_has_feature(FW_FEATURE_LPAR))
0383 vdso_data->platform |= 1;
0384 vdso_data->physicalMemorySize = memblock_phys_mem_size();
0385 vdso_data->dcache_size = ppc64_caches.l1d.size;
0386 vdso_data->dcache_line_size = ppc64_caches.l1d.line_size;
0387 vdso_data->icache_size = ppc64_caches.l1i.size;
0388 vdso_data->icache_line_size = ppc64_caches.l1i.line_size;
0389 vdso_data->dcache_block_size = ppc64_caches.l1d.block_size;
0390 vdso_data->icache_block_size = ppc64_caches.l1i.block_size;
0391 vdso_data->dcache_log_block_size = ppc64_caches.l1d.log_block_size;
0392 vdso_data->icache_log_block_size = ppc64_caches.l1i.log_block_size;
0393 #endif
0394
0395 vdso_setup_syscall_map();
0396
0397 vdso_fixup_features();
0398
0399 if (IS_ENABLED(CONFIG_VDSO32))
0400 vdso32_spec.pages = vdso_setup_pages(&vdso32_start, &vdso32_end);
0401
0402 if (IS_ENABLED(CONFIG_PPC64))
0403 vdso64_spec.pages = vdso_setup_pages(&vdso64_start, &vdso64_end);
0404
0405 smp_wmb();
0406
0407 return 0;
0408 }
0409 arch_initcall(vdso_init);