Commit 6f121e54 authored by Andy Lutomirski's avatar Andy Lutomirski Committed by H. Peter Anvin

x86, vdso: Reimplement vdso.so preparation in build-time C

Currently, vdso.so files are prepared and analyzed by a combination
of objcopy, nm, some linker script tricks, and some simple ELF
parsers in the kernel.  Replace all of that with plain C code that
runs at build time.

All five vdso images now generate .c files that are compiled and
linked in to the kernel image.

This should cause only one userspace-visible change: the loaded vDSO
images are stripped more heavily than they used to be.  Everything
outside the loadable segment is dropped.  In particular, this causes
the section table and section name strings to be missing.  This
should be fine: real dynamic loaders don't load or inspect these
tables anyway.  The result is roughly equivalent to eu-strip's
--strip-sections option.

The purpose of this change is to enable the vvar and hpet mappings
to be moved to the page following the vDSO load segment.  Currently,
it is possible for the section table to extend into the page after
the load segment, so, if we map it, it risks overlapping the vvar or
hpet page.  This happens whenever the load segment is just under a
multiple of PAGE_SIZE.

The only real subtlety here is that the old code had a C file with
inline assembler that did 'call VDSO32_vsyscall' and a linker script
that defined 'VDSO32_vsyscall = __kernel_vsyscall'.  This most
likely worked by accident: the linker script entry defines a symbol
associated with an address as opposed to an alias for the real
dynamic symbol __kernel_vsyscall.  That caused ld to relocate the
reference at link time instead of leaving an interposable dynamic
relocation.  Since the VDSO32_vsyscall hack is no longer needed, I
now use 'call __kernel_vsyscall', and I added -Bsymbolic to make it
work.  vdso2c will generate an error and abort the build if the
resulting image contains any dynamic relocations, so we won't
silently generate bad vdso images.

(Dynamic relocations are a problem because nothing will even attempt
to relocate the vdso.)
Signed-off-by: default avatarAndy Lutomirski <luto@amacapital.net>
Link: http://lkml.kernel.org/r/2c4fcf45524162a34d87fdda1eb046b2a5cecee7.1399317206.git.luto@amacapital.netSigned-off-by: default avatarH. Peter Anvin <hpa@linux.intel.com>
parent cfda7bb9
...@@ -383,8 +383,8 @@ int ia32_setup_frame(int sig, struct ksignal *ksig, ...@@ -383,8 +383,8 @@ int ia32_setup_frame(int sig, struct ksignal *ksig,
} else { } else {
/* Return stub is in 32bit vsyscall page */ /* Return stub is in 32bit vsyscall page */
if (current->mm->context.vdso) if (current->mm->context.vdso)
restorer = VDSO32_SYMBOL(current->mm->context.vdso, restorer = current->mm->context.vdso +
sigreturn); selected_vdso32->sym___kernel_sigreturn;
else else
restorer = &frame->retcode; restorer = &frame->retcode;
} }
...@@ -462,8 +462,8 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig, ...@@ -462,8 +462,8 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig,
if (ksig->ka.sa.sa_flags & SA_RESTORER) if (ksig->ka.sa.sa_flags & SA_RESTORER)
restorer = ksig->ka.sa.sa_restorer; restorer = ksig->ka.sa.sa_restorer;
else else
restorer = VDSO32_SYMBOL(current->mm->context.vdso, restorer = current->mm->context.vdso +
rt_sigreturn); selected_vdso32->sym___kernel_rt_sigreturn;
put_user_ex(ptr_to_compat(restorer), &frame->pretcode); put_user_ex(ptr_to_compat(restorer), &frame->pretcode);
/* /*
......
...@@ -299,7 +299,7 @@ do { \ ...@@ -299,7 +299,7 @@ do { \
do { \ do { \
if (vdso64_enabled) \ if (vdso64_enabled) \
NEW_AUX_ENT(AT_SYSINFO_EHDR, \ NEW_AUX_ENT(AT_SYSINFO_EHDR, \
(unsigned long)current->mm->context.vdso); \ (unsigned long __force)current->mm->context.vdso); \
} while (0) } while (0)
/* As a historical oddity, the x32 and x86_64 vDSOs are controlled together. */ /* As a historical oddity, the x32 and x86_64 vDSOs are controlled together. */
...@@ -307,7 +307,7 @@ do { \ ...@@ -307,7 +307,7 @@ do { \
do { \ do { \
if (vdso64_enabled) \ if (vdso64_enabled) \
NEW_AUX_ENT(AT_SYSINFO_EHDR, \ NEW_AUX_ENT(AT_SYSINFO_EHDR, \
(unsigned long)current->mm->context.vdso); \ (unsigned long __force)current->mm->context.vdso); \
} while (0) } while (0)
#define AT_SYSINFO 32 #define AT_SYSINFO 32
...@@ -325,7 +325,8 @@ else \ ...@@ -325,7 +325,8 @@ else \
#define VDSO_CURRENT_BASE ((unsigned long)current->mm->context.vdso) #define VDSO_CURRENT_BASE ((unsigned long)current->mm->context.vdso)
#define VDSO_ENTRY \ #define VDSO_ENTRY \
((unsigned long)VDSO32_SYMBOL(VDSO_CURRENT_BASE, vsyscall)) ((unsigned long)current->mm->context.vdso + \
selected_vdso32->sym___kernel_vsyscall)
struct linux_binprm; struct linux_binprm;
......
...@@ -18,7 +18,7 @@ typedef struct { ...@@ -18,7 +18,7 @@ typedef struct {
#endif #endif
struct mutex lock; struct mutex lock;
void *vdso; void __user *vdso;
} mm_context_t; } mm_context_t;
#ifdef CONFIG_SMP #ifdef CONFIG_SMP
......
...@@ -3,63 +3,43 @@ ...@@ -3,63 +3,43 @@
#include <asm/page_types.h> #include <asm/page_types.h>
#include <linux/linkage.h> #include <linux/linkage.h>
#include <linux/init.h>
#ifdef __ASSEMBLER__ #ifndef __ASSEMBLER__
#define DEFINE_VDSO_IMAGE(symname, filename) \ struct vdso_image {
__PAGE_ALIGNED_DATA ; \ void *data;
.globl symname##_start, symname##_end ; \ unsigned long size; /* Always a multiple of PAGE_SIZE */
.align PAGE_SIZE ; \ struct page **pages; /* Big enough for data/size page pointers */
symname##_start: ; \
.incbin filename ; \
symname##_end: ; \
.align PAGE_SIZE /* extra data here leaks to userspace. */ ; \
\
.previous ; \
\
.globl symname##_pages ; \
.bss ; \
.align 8 ; \
.type symname##_pages, @object ; \
symname##_pages: ; \
.zero (symname##_end - symname##_start + PAGE_SIZE - 1) / PAGE_SIZE * (BITS_PER_LONG / 8) ; \
.size symname##_pages, .-symname##_pages
#else unsigned long alt, alt_len;
#define DECLARE_VDSO_IMAGE(symname) \ unsigned long sym_VDSO32_NOTE_MASK;
extern char symname##_start[], symname##_end[]; \ unsigned long sym___kernel_sigreturn;
extern struct page *symname##_pages[] unsigned long sym___kernel_rt_sigreturn;
unsigned long sym___kernel_vsyscall;
unsigned long sym_VDSO32_SYSENTER_RETURN;
};
#if defined CONFIG_X86_32 || defined CONFIG_COMPAT #ifdef CONFIG_X86_64
extern const struct vdso_image vdso_image_64;
#endif
#include <asm/vdso32.h> #ifdef CONFIG_X86_X32
extern const struct vdso_image vdso_image_x32;
#endif
DECLARE_VDSO_IMAGE(vdso32_int80); #if defined CONFIG_X86_32 || defined CONFIG_COMPAT
extern const struct vdso_image vdso_image_32_int80;
#ifdef CONFIG_COMPAT #ifdef CONFIG_COMPAT
DECLARE_VDSO_IMAGE(vdso32_syscall); extern const struct vdso_image vdso_image_32_syscall;
#endif #endif
DECLARE_VDSO_IMAGE(vdso32_sysenter); extern const struct vdso_image vdso_image_32_sysenter;
/* extern const struct vdso_image *selected_vdso32;
* Given a pointer to the vDSO image, find the pointer to VDSO32_name
* as that symbol is defined in the vDSO sources or linker script.
*/
#define VDSO32_SYMBOL(base, name) \
({ \
extern const char VDSO32_##name[]; \
(void __user *)(VDSO32_##name + (unsigned long)(base)); \
})
#endif #endif
/* extern void __init init_vdso_image(const struct vdso_image *image);
* These symbols are defined with the addresses in the vsyscall page.
* See vsyscall-sigreturn.S.
*/
extern void __user __kernel_sigreturn;
extern void __user __kernel_rt_sigreturn;
void __init patch_vdso32(void *vdso, size_t len);
#endif /* __ASSEMBLER__ */ #endif /* __ASSEMBLER__ */
......
...@@ -298,7 +298,8 @@ __setup_frame(int sig, struct ksignal *ksig, sigset_t *set, ...@@ -298,7 +298,8 @@ __setup_frame(int sig, struct ksignal *ksig, sigset_t *set,
} }
if (current->mm->context.vdso) if (current->mm->context.vdso)
restorer = VDSO32_SYMBOL(current->mm->context.vdso, sigreturn); restorer = current->mm->context.vdso +
selected_vdso32->sym___kernel_sigreturn;
else else
restorer = &frame->retcode; restorer = &frame->retcode;
if (ksig->ka.sa.sa_flags & SA_RESTORER) if (ksig->ka.sa.sa_flags & SA_RESTORER)
...@@ -361,7 +362,8 @@ static int __setup_rt_frame(int sig, struct ksignal *ksig, ...@@ -361,7 +362,8 @@ static int __setup_rt_frame(int sig, struct ksignal *ksig,
save_altstack_ex(&frame->uc.uc_stack, regs->sp); save_altstack_ex(&frame->uc.uc_stack, regs->sp);
/* Set up to return from userspace. */ /* Set up to return from userspace. */
restorer = VDSO32_SYMBOL(current->mm->context.vdso, rt_sigreturn); restorer = current->mm->context.vdso +
selected_vdso32->sym___kernel_sigreturn;
if (ksig->ka.sa.sa_flags & SA_RESTORER) if (ksig->ka.sa.sa_flags & SA_RESTORER)
restorer = ksig->ka.sa.sa_restorer; restorer = ksig->ka.sa.sa_restorer;
put_user_ex(restorer, &frame->pretcode); put_user_ex(restorer, &frame->pretcode);
......
...@@ -1223,7 +1223,8 @@ int in_gate_area_no_mm(unsigned long addr) ...@@ -1223,7 +1223,8 @@ int in_gate_area_no_mm(unsigned long addr)
const char *arch_vma_name(struct vm_area_struct *vma) const char *arch_vma_name(struct vm_area_struct *vma)
{ {
if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso) if (vma->vm_mm && vma->vm_start ==
(long __force)vma->vm_mm->context.vdso)
return "[vdso]"; return "[vdso]";
if (vma == &gate_vma) if (vma == &gate_vma)
return "[vsyscall]"; return "[vsyscall]";
......
vdso.lds vdso.lds
vdso-syms.lds
vdsox32.lds vdsox32.lds
vdsox32-syms.lds
vdso32-syms.lds
vdso32-syscall-syms.lds vdso32-syscall-syms.lds
vdso32-sysenter-syms.lds vdso32-sysenter-syms.lds
vdso32-int80-syms.lds vdso32-int80-syms.lds
vdso-image-*.c
vdso2c
...@@ -24,15 +24,30 @@ vobj64s := $(filter-out $(vobjx32s-compat),$(vobjs-y)) ...@@ -24,15 +24,30 @@ vobj64s := $(filter-out $(vobjx32s-compat),$(vobjs-y))
# files to link into kernel # files to link into kernel
obj-y += vma.o obj-y += vma.o
obj-$(VDSO64-y) += vdso.o
obj-$(VDSOX32-y) += vdsox32.o # vDSO images to build
obj-$(VDSO32-y) += vdso32.o vdso32-setup.o vdso_img-$(VDSO64-y) += 64
vdso_img-$(VDSOX32-y) += x32
vdso_img-$(VDSO32-y) += 32-int80
vdso_img-$(CONFIG_COMPAT) += 32-syscall
vdso_img-$(VDSO32-y) += 32-sysenter
obj-$(VDSO32-y) += vdso32-setup.o
vobjs := $(foreach F,$(vobj64s),$(obj)/$F) vobjs := $(foreach F,$(vobj64s),$(obj)/$F)
$(obj)/vdso.o: $(obj)/vdso.so $(obj)/vdso.o: $(obj)/vdso.so
targets += vdso.so vdso.so.dbg vdso.lds $(vobjs-y) targets += vdso.lds $(vobjs-y)
# Build the vDSO image C files and link them in.
vdso_img_objs := $(vdso_img-y:%=vdso-image-%.o)
vdso_img_cfiles := $(vdso_img-y:%=vdso-image-%.c)
vdso_img_sodbg := $(vdso_img-y:%=vdso%.so.dbg)
obj-y += $(vdso_img_objs)
targets += $(vdso_img_cfiles)
targets += $(vdso_img_sodbg)
.SECONDARY: $(vdso_img-y:%=$(obj)/vdso-image-%.c)
export CPPFLAGS_vdso.lds += -P -C export CPPFLAGS_vdso.lds += -P -C
...@@ -41,14 +56,18 @@ VDSO_LDFLAGS_vdso.lds = -m64 -Wl,-soname=linux-vdso.so.1 \ ...@@ -41,14 +56,18 @@ VDSO_LDFLAGS_vdso.lds = -m64 -Wl,-soname=linux-vdso.so.1 \
-Wl,-z,max-page-size=4096 -Wl,-z,common-page-size=4096 \ -Wl,-z,max-page-size=4096 -Wl,-z,common-page-size=4096 \
$(DISABLE_LTO) $(DISABLE_LTO)
$(obj)/vdso.o: $(src)/vdso.S $(obj)/vdso.so $(obj)/vdso64.so.dbg: $(src)/vdso.lds $(vobjs) FORCE
$(obj)/vdso.so.dbg: $(src)/vdso.lds $(vobjs) FORCE
$(call if_changed,vdso) $(call if_changed,vdso)
$(obj)/%.so: OBJCOPYFLAGS := -S hostprogs-y += vdso2c
$(obj)/%.so: $(obj)/%.so.dbg FORCE
$(call if_changed,objcopy) quiet_cmd_vdso2c = VDSO2C $@
define cmd_vdso2c
$(obj)/vdso2c $< $@
endef
$(obj)/vdso-image-%.c: $(obj)/vdso%.so.dbg $(obj)/vdso2c FORCE
$(call if_changed,vdso2c)
# #
# Don't omit frame pointers for ease of userspace debugging, but do # Don't omit frame pointers for ease of userspace debugging, but do
...@@ -68,22 +87,6 @@ CFLAGS_REMOVE_vclock_gettime.o = -pg ...@@ -68,22 +87,6 @@ CFLAGS_REMOVE_vclock_gettime.o = -pg
CFLAGS_REMOVE_vgetcpu.o = -pg CFLAGS_REMOVE_vgetcpu.o = -pg
CFLAGS_REMOVE_vvar.o = -pg CFLAGS_REMOVE_vvar.o = -pg
targets += vdso-syms.lds
obj-$(VDSO64-y) += vdso-syms.lds
#
# Match symbols in the DSO that look like VDSO*; produce a file of constants.
#
sed-vdsosym := -e 's/^00*/0/' \
-e 's/^\([0-9a-fA-F]*\) . \(VDSO[a-zA-Z0-9_]*\)$$/\2 = 0x\1;/p'
quiet_cmd_vdsosym = VDSOSYM $@
define cmd_vdsosym
$(NM) $< | LC_ALL=C sed -n $(sed-vdsosym) | LC_ALL=C sort > $@
endef
$(obj)/%-syms.lds: $(obj)/%.so.dbg FORCE
$(call if_changed,vdsosym)
# #
# X32 processes use x32 vDSO to access 64bit kernel data. # X32 processes use x32 vDSO to access 64bit kernel data.
# #
...@@ -94,9 +97,6 @@ $(obj)/%-syms.lds: $(obj)/%.so.dbg FORCE ...@@ -94,9 +97,6 @@ $(obj)/%-syms.lds: $(obj)/%.so.dbg FORCE
# so that it can reach 64bit address space with 64bit pointers. # so that it can reach 64bit address space with 64bit pointers.
# #
targets += vdsox32-syms.lds
obj-$(VDSOX32-y) += vdsox32-syms.lds
CPPFLAGS_vdsox32.lds = $(CPPFLAGS_vdso.lds) CPPFLAGS_vdsox32.lds = $(CPPFLAGS_vdso.lds)
VDSO_LDFLAGS_vdsox32.lds = -Wl,-m,elf32_x86_64 \ VDSO_LDFLAGS_vdsox32.lds = -Wl,-m,elf32_x86_64 \
-Wl,-soname=linux-vdso.so.1 \ -Wl,-soname=linux-vdso.so.1 \
...@@ -113,9 +113,7 @@ quiet_cmd_x32 = X32 $@ ...@@ -113,9 +113,7 @@ quiet_cmd_x32 = X32 $@
$(obj)/%-x32.o: $(obj)/%.o FORCE $(obj)/%-x32.o: $(obj)/%.o FORCE
$(call if_changed,x32) $(call if_changed,x32)
targets += vdsox32.so vdsox32.so.dbg vdsox32.lds $(vobjx32s-y) targets += vdsox32.lds $(vobjx32s-y)
$(obj)/vdsox32.o: $(src)/vdsox32.S $(obj)/vdsox32.so
$(obj)/vdsox32.so.dbg: $(src)/vdsox32.lds $(vobjx32s) FORCE $(obj)/vdsox32.so.dbg: $(src)/vdsox32.lds $(vobjx32s) FORCE
$(call if_changed,vdso) $(call if_changed,vdso)
...@@ -123,7 +121,6 @@ $(obj)/vdsox32.so.dbg: $(src)/vdsox32.lds $(vobjx32s) FORCE ...@@ -123,7 +121,6 @@ $(obj)/vdsox32.so.dbg: $(src)/vdsox32.lds $(vobjx32s) FORCE
# #
# Build multiple 32-bit vDSO images to choose from at boot time. # Build multiple 32-bit vDSO images to choose from at boot time.
# #
obj-$(VDSO32-y) += vdso32-syms.lds
vdso32.so-$(VDSO32-y) += int80 vdso32.so-$(VDSO32-y) += int80
vdso32.so-$(CONFIG_COMPAT) += syscall vdso32.so-$(CONFIG_COMPAT) += syscall
vdso32.so-$(VDSO32-y) += sysenter vdso32.so-$(VDSO32-y) += sysenter
...@@ -138,10 +135,8 @@ VDSO_LDFLAGS_vdso32.lds = -m32 -Wl,-m,elf_i386 -Wl,-soname=linux-gate.so.1 ...@@ -138,10 +135,8 @@ VDSO_LDFLAGS_vdso32.lds = -m32 -Wl,-m,elf_i386 -Wl,-soname=linux-gate.so.1
override obj-dirs = $(dir $(obj)) $(obj)/vdso32/ override obj-dirs = $(dir $(obj)) $(obj)/vdso32/
targets += vdso32/vdso32.lds targets += vdso32/vdso32.lds
targets += $(vdso32-images) $(vdso32-images:=.dbg)
targets += vdso32/note.o vdso32/vclock_gettime.o $(vdso32.so-y:%=vdso32/%.o) targets += vdso32/note.o vdso32/vclock_gettime.o $(vdso32.so-y:%=vdso32/%.o)
targets += vdso32/vclock_gettime.o
extra-y += $(vdso32-images)
$(obj)/vdso32.o: $(vdso32-images:%=$(obj)/%) $(obj)/vdso32.o: $(vdso32-images:%=$(obj)/%)
...@@ -166,27 +161,6 @@ $(vdso32-images:%=$(obj)/%.dbg): $(obj)/vdso32-%.so.dbg: FORCE \ ...@@ -166,27 +161,6 @@ $(vdso32-images:%=$(obj)/%.dbg): $(obj)/vdso32-%.so.dbg: FORCE \
$(obj)/vdso32/%.o $(obj)/vdso32/%.o
$(call if_changed,vdso) $(call if_changed,vdso)
# Make vdso32-*-syms.lds from each image, and then make sure they match.
# The only difference should be that some do not define VDSO32_SYSENTER_RETURN.
targets += vdso32-syms.lds $(vdso32.so-y:%=vdso32-%-syms.lds)
quiet_cmd_vdso32sym = VDSOSYM $@
define cmd_vdso32sym
if LC_ALL=C sort -u $(filter-out FORCE,$^) > $(@D)/.tmp_$(@F) && \
$(foreach H,$(filter-out FORCE,$^),\
if grep -q VDSO32_SYSENTER_RETURN $H; \
then diff -u $(@D)/.tmp_$(@F) $H; \
else sed /VDSO32_SYSENTER_RETURN/d $(@D)/.tmp_$(@F) | \
diff -u - $H; fi &&) : ;\
then mv -f $(@D)/.tmp_$(@F) $@; \
else rm -f $(@D)/.tmp_$(@F); exit 1; \
fi
endef
$(obj)/vdso32-syms.lds: $(vdso32.so-y:%=$(obj)/vdso32-%-syms.lds) FORCE
$(call if_changed,vdso32sym)
# #
# The DSO images are built using a special linker script. # The DSO images are built using a special linker script.
# #
...@@ -197,7 +171,7 @@ quiet_cmd_vdso = VDSO $@ ...@@ -197,7 +171,7 @@ quiet_cmd_vdso = VDSO $@
sh $(srctree)/$(src)/checkundef.sh '$(NM)' '$@' sh $(srctree)/$(src)/checkundef.sh '$(NM)' '$@'
VDSO_LDFLAGS = -fPIC -shared $(call cc-ldoption, -Wl$(comma)--hash-style=sysv) \ VDSO_LDFLAGS = -fPIC -shared $(call cc-ldoption, -Wl$(comma)--hash-style=sysv) \
$(LTO_CFLAGS) -Wl,-Bsymbolic $(LTO_CFLAGS)
GCOV_PROFILE := n GCOV_PROFILE := n
# #
......
...@@ -154,7 +154,7 @@ notrace static long vdso_fallback_gettime(long clock, struct timespec *ts) ...@@ -154,7 +154,7 @@ notrace static long vdso_fallback_gettime(long clock, struct timespec *ts)
asm( asm(
"mov %%ebx, %%edx \n" "mov %%ebx, %%edx \n"
"mov %2, %%ebx \n" "mov %2, %%ebx \n"
"call VDSO32_vsyscall \n" "call __kernel_vsyscall \n"
"mov %%edx, %%ebx \n" "mov %%edx, %%ebx \n"
: "=a" (ret) : "=a" (ret)
: "0" (__NR_clock_gettime), "g" (clock), "c" (ts) : "0" (__NR_clock_gettime), "g" (clock), "c" (ts)
...@@ -169,7 +169,7 @@ notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz) ...@@ -169,7 +169,7 @@ notrace static long vdso_fallback_gtod(struct timeval *tv, struct timezone *tz)
asm( asm(
"mov %%ebx, %%edx \n" "mov %%ebx, %%edx \n"
"mov %2, %%ebx \n" "mov %2, %%ebx \n"
"call VDSO32_vsyscall \n" "call __kernel_vsyscall \n"
"mov %%edx, %%ebx \n" "mov %%edx, %%ebx \n"
: "=a" (ret) : "=a" (ret)
: "0" (__NR_gettimeofday), "g" (tv), "c" (tz) : "0" (__NR_gettimeofday), "g" (tv), "c" (tz)
......
#include <asm/vdso.h>
DEFINE_VDSO_IMAGE(vdso, "arch/x86/vdso/vdso.so")
#include <inttypes.h>
#include <stdint.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <err.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <linux/elf.h>
#include <linux/types.h>
/* Symbols that we need in vdso2c. */
char const * const required_syms[] = {
"VDSO32_NOTE_MASK",
"VDSO32_SYSENTER_RETURN",
"__kernel_vsyscall",
"__kernel_sigreturn",
"__kernel_rt_sigreturn",
};
__attribute__((format(printf, 1, 2))) __attribute__((noreturn))
static void fail(const char *format, ...)
{
va_list ap;
va_start(ap, format);
fprintf(stderr, "Error: ");
vfprintf(stderr, format, ap);
exit(1);
va_end(ap);
}
#define NSYMS (sizeof(required_syms) / sizeof(required_syms[0]))
#define BITS 64
#define GOFUNC go64
#define Elf_Ehdr Elf64_Ehdr
#define Elf_Shdr Elf64_Shdr
#define Elf_Phdr Elf64_Phdr
#define Elf_Sym Elf64_Sym
#define Elf_Dyn Elf64_Dyn
#include "vdso2c.h"
#undef BITS
#undef GOFUNC
#undef Elf_Ehdr
#undef Elf_Shdr
#undef Elf_Phdr
#undef Elf_Sym
#undef Elf_Dyn
#define BITS 32
#define GOFUNC go32
#define Elf_Ehdr Elf32_Ehdr
#define Elf_Shdr Elf32_Shdr
#define Elf_Phdr Elf32_Phdr
#define Elf_Sym Elf32_Sym
#define Elf_Dyn Elf32_Dyn
#include "vdso2c.h"
#undef BITS
#undef GOFUNC
#undef Elf_Ehdr
#undef Elf_Shdr
#undef Elf_Phdr
#undef Elf_Sym
#undef Elf_Dyn
static int go(void *addr, size_t len, FILE *outfile, const char *name)
{
Elf64_Ehdr *hdr = (Elf64_Ehdr *)addr;
if (hdr->e_ident[EI_CLASS] == ELFCLASS64) {
return go64(addr, len, outfile, name);
} else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) {
return go32(addr, len, outfile, name);
} else {
fprintf(stderr, "Error: unknown ELF class\n");
return 1;
}
}
int main(int argc, char **argv)
{
int fd;
off_t len;
void *addr;
FILE *outfile;
int ret;
char *name, *tmp;
int namelen;
if (argc != 3) {
printf("Usage: vdso2c INPUT OUTPUT\n");
return 1;
}
/*
* Figure out the struct name. If we're writing to a .so file,
* generate raw output insted.
*/
name = strdup(argv[2]);
namelen = strlen(name);
if (namelen >= 3 && !strcmp(name + namelen - 3, ".so")) {
name = NULL;
} else {
tmp = strrchr(name, '/');
if (tmp)
name = tmp + 1;
tmp = strchr(name, '.');
if (tmp)
*tmp = '\0';
for (tmp = name; *tmp; tmp++)
if (*tmp == '-')
*tmp = '_';
}
fd = open(argv[1], O_RDONLY);
if (fd == -1)
err(1, "%s", argv[1]);
len = lseek(fd, 0, SEEK_END);
if (len == (off_t)-1)
err(1, "lseek");
addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
if (addr == MAP_FAILED)
err(1, "mmap");
outfile = fopen(argv[2], "w");
if (!outfile)
err(1, "%s", argv[2]);
ret = go(addr, (size_t)len, outfile, name);
munmap(addr, len);
fclose(outfile);
return ret;
}
/*
* This file is included twice from vdso2c.c. It generates code for 32-bit
* and 64-bit vDSOs. We need both for 64-bit builds, since 32-bit vDSOs
* are built for 32-bit userspace.
*/
static int GOFUNC(void *addr, size_t len, FILE *outfile, const char *name)
{
int found_load = 0;
unsigned long load_size = -1; /* Work around bogus warning */
unsigned long data_size;
Elf_Ehdr *hdr = (Elf_Ehdr *)addr;
int i;
unsigned long j;
Elf_Shdr *symtab_hdr = NULL, *strtab_hdr, *secstrings_hdr,
*alt_sec = NULL;
Elf_Dyn *dyn = 0, *dyn_end = 0;
const char *secstrings;
uint64_t syms[NSYMS] = {};
Elf_Phdr *pt = (Elf_Phdr *)(addr + hdr->e_phoff);
/* Walk the segment table. */
for (i = 0; i < hdr->e_phnum; i++) {
if (pt[i].p_type == PT_LOAD) {
if (found_load)
fail("multiple PT_LOAD segs\n");
if (pt[i].p_offset != 0 || pt[i].p_vaddr != 0)
fail("PT_LOAD in wrong place\n");
if (pt[i].p_memsz != pt[i].p_filesz)
fail("cannot handle memsz != filesz\n");
load_size = pt[i].p_memsz;
found_load = 1;
} else if (pt[i].p_type == PT_DYNAMIC) {
dyn = addr + pt[i].p_offset;
dyn_end = addr + pt[i].p_offset + pt[i].p_memsz;
}
}
if (!found_load)
fail("no PT_LOAD seg\n");
data_size = (load_size + 4095) / 4096 * 4096;
/* Walk the dynamic table */
for (i = 0; dyn + i < dyn_end && dyn[i].d_tag != DT_NULL; i++) {
if (dyn[i].d_tag == DT_REL || dyn[i].d_tag == DT_RELSZ ||
dyn[i].d_tag == DT_RELENT || dyn[i].d_tag == DT_TEXTREL)
fail("vdso image contains dynamic relocations\n");
}
/* Walk the section table */
secstrings_hdr = addr + hdr->e_shoff + hdr->e_shentsize*hdr->e_shstrndx;
secstrings = addr + secstrings_hdr->sh_offset;
for (i = 0; i < hdr->e_shnum; i++) {
Elf_Shdr *sh = addr + hdr->e_shoff + hdr->e_shentsize * i;
if (sh->sh_type == SHT_SYMTAB)
symtab_hdr = sh;
if (!strcmp(secstrings + sh->sh_name, ".altinstructions"))
alt_sec = sh;
}
if (!symtab_hdr) {
fail("no symbol table\n");
return 1;
}
strtab_hdr = addr + hdr->e_shoff +
hdr->e_shentsize * symtab_hdr->sh_link;
/* Walk the symbol table */
for (i = 0; i < symtab_hdr->sh_size / symtab_hdr->sh_entsize; i++) {
int k;
Elf_Sym *sym = addr + symtab_hdr->sh_offset +
symtab_hdr->sh_entsize * i;
const char *name = addr + strtab_hdr->sh_offset + sym->st_name;
for (k = 0; k < NSYMS; k++) {
if (!strcmp(name, required_syms[k])) {
if (syms[k]) {
fail("duplicate symbol %s\n",
required_syms[k]);
}
syms[k] = sym->st_value;
}
}
}
/* Remove sections. */
hdr->e_shoff = 0;
hdr->e_shentsize = 0;
hdr->e_shnum = 0;
hdr->e_shstrndx = SHN_UNDEF;
if (!name) {
fwrite(addr, load_size, 1, outfile);
return 0;
}
fprintf(outfile, "/* AUTOMATICALLY GENERATED -- DO NOT EDIT */\n\n");
fprintf(outfile, "#include <linux/linkage.h>\n");
fprintf(outfile, "#include <asm/page_types.h>\n");
fprintf(outfile, "#include <asm/vdso.h>\n");
fprintf(outfile, "\n");
fprintf(outfile,
"static unsigned char raw_data[%lu] __page_aligned_data = {",
data_size);
for (j = 0; j < load_size; j++) {
if (j % 10 == 0)
fprintf(outfile, "\n\t");
fprintf(outfile, "0x%02X, ", (int)((unsigned char *)addr)[j]);
}
fprintf(outfile, "\n};\n\n");
fprintf(outfile, "static struct page *pages[%lu];\n\n",
data_size / 4096);
fprintf(outfile, "const struct vdso_image %s = {\n", name);
fprintf(outfile, "\t.data = raw_data,\n");
fprintf(outfile, "\t.size = %lu,\n", data_size);
fprintf(outfile, "\t.pages = pages,\n");
if (alt_sec) {
fprintf(outfile, "\t.alt = %lu,\n",
(unsigned long)alt_sec->sh_offset);
fprintf(outfile, "\t.alt_len = %lu,\n",
(unsigned long)alt_sec->sh_size);
}
for (i = 0; i < NSYMS; i++) {
if (syms[i])
fprintf(outfile, "\t.sym_%s = 0x%" PRIx64 ",\n",
required_syms[i], syms[i]);
}
fprintf(outfile, "};\n");
return 0;
}
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <asm/fixmap.h> #include <asm/fixmap.h>
#include <asm/hpet.h> #include <asm/hpet.h>
#include <asm/vvar.h> #include <asm/vvar.h>
#include <asm/vdso32.h>
#ifdef CONFIG_COMPAT_VDSO #ifdef CONFIG_COMPAT_VDSO
#define VDSO_DEFAULT 0 #define VDSO_DEFAULT 0
...@@ -67,9 +68,6 @@ __setup("vdso32=", vdso32_setup); ...@@ -67,9 +68,6 @@ __setup("vdso32=", vdso32_setup);
__setup_param("vdso=", vdso_setup, vdso32_setup, 0); __setup_param("vdso=", vdso_setup, vdso32_setup, 0);
#endif #endif
static struct page **vdso32_pages;
static unsigned vdso32_size;
#ifdef CONFIG_X86_64 #ifdef CONFIG_X86_64
#define vdso32_sysenter() (boot_cpu_has(X86_FEATURE_SYSENTER32)) #define vdso32_sysenter() (boot_cpu_has(X86_FEATURE_SYSENTER32))
...@@ -82,34 +80,23 @@ static unsigned vdso32_size; ...@@ -82,34 +80,23 @@ static unsigned vdso32_size;
#endif /* CONFIG_X86_64 */ #endif /* CONFIG_X86_64 */
#if defined(CONFIG_X86_32) || defined(CONFIG_COMPAT)
const struct vdso_image *selected_vdso32;
#endif
int __init sysenter_setup(void) int __init sysenter_setup(void)
{ {
char *vdso32_start, *vdso32_end;
int npages, i;
#ifdef CONFIG_COMPAT #ifdef CONFIG_COMPAT
if (vdso32_syscall()) { if (vdso32_syscall())
vdso32_start = vdso32_syscall_start; selected_vdso32 = &vdso_image_32_syscall;
vdso32_end = vdso32_syscall_end; else
vdso32_pages = vdso32_syscall_pages;
} else
#endif #endif
if (vdso32_sysenter()) { if (vdso32_sysenter())
vdso32_start = vdso32_sysenter_start; selected_vdso32 = &vdso_image_32_sysenter;
vdso32_end = vdso32_sysenter_end; else
vdso32_pages = vdso32_sysenter_pages; selected_vdso32 = &vdso_image_32_int80;
} else {
vdso32_start = vdso32_int80_start;
vdso32_end = vdso32_int80_end;
vdso32_pages = vdso32_int80_pages;
}
npages = ((vdso32_end - vdso32_start) + PAGE_SIZE - 1) / PAGE_SIZE;
vdso32_size = npages << PAGE_SHIFT;
for (i = 0; i < npages; i++)
vdso32_pages[i] = virt_to_page(vdso32_start + i*PAGE_SIZE);
patch_vdso32(vdso32_start, vdso32_size); init_vdso_image(selected_vdso32);
return 0; return 0;
} }
...@@ -121,6 +108,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) ...@@ -121,6 +108,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
unsigned long addr; unsigned long addr;
int ret = 0; int ret = 0;
struct vm_area_struct *vma; struct vm_area_struct *vma;
unsigned long vdso32_size = selected_vdso32->size;
#ifdef CONFIG_X86_X32_ABI #ifdef CONFIG_X86_X32_ABI
if (test_thread_flag(TIF_X32)) if (test_thread_flag(TIF_X32))
...@@ -140,7 +128,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) ...@@ -140,7 +128,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
addr += VDSO_OFFSET(VDSO_PREV_PAGES); addr += VDSO_OFFSET(VDSO_PREV_PAGES);
current->mm->context.vdso = (void *)addr; current->mm->context.vdso = (void __user *)addr;
/* /*
* MAYWRITE to allow gdb to COW and set breakpoints * MAYWRITE to allow gdb to COW and set breakpoints
...@@ -150,7 +138,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) ...@@ -150,7 +138,7 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
vdso32_size, vdso32_size,
VM_READ|VM_EXEC| VM_READ|VM_EXEC|
VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
vdso32_pages); selected_vdso32->pages);
if (ret) if (ret)
goto up_fail; goto up_fail;
...@@ -188,8 +176,10 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) ...@@ -188,8 +176,10 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
} }
#endif #endif
current_thread_info()->sysenter_return = if (selected_vdso32->sym_VDSO32_SYSENTER_RETURN)
VDSO32_SYMBOL(addr, SYSENTER_RETURN); current_thread_info()->sysenter_return =
current->mm->context.vdso +
selected_vdso32->sym_VDSO32_SYSENTER_RETURN;
up_fail: up_fail:
if (ret) if (ret)
......
#include <asm/vdso.h>
DEFINE_VDSO_IMAGE(vdso32_int80, "arch/x86/vdso/vdso32-int80.so")
#ifdef CONFIG_COMPAT
DEFINE_VDSO_IMAGE(vdso32_syscall, "arch/x86/vdso/vdso32-syscall.so")
#endif
DEFINE_VDSO_IMAGE(vdso32_sysenter, "arch/x86/vdso/vdso32-sysenter.so")
...@@ -38,13 +38,3 @@ VERSION ...@@ -38,13 +38,3 @@ VERSION
local: *; local: *;
}; };
} }
/*
* Symbols we define here called VDSO* get their values into vdso32-syms.h.
*/
VDSO32_vsyscall = __kernel_vsyscall;
VDSO32_sigreturn = __kernel_sigreturn;
VDSO32_rt_sigreturn = __kernel_rt_sigreturn;
VDSO32_clock_gettime = clock_gettime;
VDSO32_gettimeofday = gettimeofday;
VDSO32_time = time;
#include <asm/vdso.h>
DEFINE_VDSO_IMAGE(vdsox32, "arch/x86/vdso/vdsox32.so")
...@@ -19,99 +19,31 @@ ...@@ -19,99 +19,31 @@
#if defined(CONFIG_X86_64) #if defined(CONFIG_X86_64)
unsigned int __read_mostly vdso64_enabled = 1; unsigned int __read_mostly vdso64_enabled = 1;
DECLARE_VDSO_IMAGE(vdso);
extern unsigned short vdso_sync_cpuid; extern unsigned short vdso_sync_cpuid;
static unsigned vdso_size;
#ifdef CONFIG_X86_X32_ABI
DECLARE_VDSO_IMAGE(vdsox32);
static unsigned vdsox32_size;
#endif
#endif #endif
#if defined(CONFIG_X86_32) || defined(CONFIG_X86_X32_ABI) || \ void __init init_vdso_image(const struct vdso_image *image)
defined(CONFIG_COMPAT)
void __init patch_vdso32(void *vdso, size_t len)
{ {
Elf32_Ehdr *hdr = vdso;
Elf32_Shdr *sechdrs, *alt_sec = 0;
char *secstrings;
void *alt_data;
int i; int i;
int npages = (image->size) / PAGE_SIZE;
BUG_ON(len < sizeof(Elf32_Ehdr)); BUG_ON(image->size % PAGE_SIZE != 0);
BUG_ON(memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0); for (i = 0; i < npages; i++)
image->pages[i] = virt_to_page(image->data + i*PAGE_SIZE);
sechdrs = (void *)hdr + hdr->e_shoff;
secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
for (i = 1; i < hdr->e_shnum; i++) {
Elf32_Shdr *shdr = &sechdrs[i];
if (!strcmp(secstrings + shdr->sh_name, ".altinstructions")) {
alt_sec = shdr;
goto found;
}
}
/* If we get here, it's probably a bug. */
pr_warning("patch_vdso32: .altinstructions not found\n");
return; /* nothing to patch */
found: apply_alternatives((struct alt_instr *)(image->data + image->alt),
alt_data = (void *)hdr + alt_sec->sh_offset; (struct alt_instr *)(image->data + image->alt +
apply_alternatives(alt_data, alt_data + alt_sec->sh_size); image->alt_len));
} }
#endif
#if defined(CONFIG_X86_64)
static void __init patch_vdso64(void *vdso, size_t len)
{
Elf64_Ehdr *hdr = vdso;
Elf64_Shdr *sechdrs, *alt_sec = 0;
char *secstrings;
void *alt_data;
int i;
BUG_ON(len < sizeof(Elf64_Ehdr));
BUG_ON(memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0);
sechdrs = (void *)hdr + hdr->e_shoff;
secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
for (i = 1; i < hdr->e_shnum; i++) {
Elf64_Shdr *shdr = &sechdrs[i];
if (!strcmp(secstrings + shdr->sh_name, ".altinstructions")) {
alt_sec = shdr;
goto found;
}
}
/* If we get here, it's probably a bug. */
pr_warning("patch_vdso64: .altinstructions not found\n");
return; /* nothing to patch */
found:
alt_data = (void *)hdr + alt_sec->sh_offset;
apply_alternatives(alt_data, alt_data + alt_sec->sh_size);
}
#if defined(CONFIG_X86_64)
static int __init init_vdso(void) static int __init init_vdso(void)
{ {
int npages = (vdso_end - vdso_start + PAGE_SIZE - 1) / PAGE_SIZE; init_vdso_image(&vdso_image_64);
int i;
patch_vdso64(vdso_start, vdso_end - vdso_start);
vdso_size = npages << PAGE_SHIFT;
for (i = 0; i < npages; i++)
vdso_pages[i] = virt_to_page(vdso_start + i*PAGE_SIZE);
#ifdef CONFIG_X86_X32_ABI #ifdef CONFIG_X86_X32_ABI
patch_vdso32(vdsox32_start, vdsox32_end - vdsox32_start); init_vdso_image(&vdso_image_x32);
npages = (vdsox32_end - vdsox32_start + PAGE_SIZE - 1) / PAGE_SIZE;
vdsox32_size = npages << PAGE_SHIFT;
for (i = 0; i < npages; i++)
vdsox32_pages[i] = virt_to_page(vdsox32_start + i*PAGE_SIZE);
#endif #endif
return 0; return 0;
...@@ -171,7 +103,7 @@ static int setup_additional_pages(struct linux_binprm *bprm, ...@@ -171,7 +103,7 @@ static int setup_additional_pages(struct linux_binprm *bprm,
goto up_fail; goto up_fail;
} }
current->mm->context.vdso = (void *)addr; current->mm->context.vdso = (void __user *)addr;
ret = install_special_mapping(mm, addr, size, ret = install_special_mapping(mm, addr, size,
VM_READ|VM_EXEC| VM_READ|VM_EXEC|
...@@ -189,15 +121,15 @@ static int setup_additional_pages(struct linux_binprm *bprm, ...@@ -189,15 +121,15 @@ static int setup_additional_pages(struct linux_binprm *bprm,
int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
{ {
return setup_additional_pages(bprm, uses_interp, vdso_pages, return setup_additional_pages(bprm, uses_interp, vdso_image_64.pages,
vdso_size); vdso_image_64.size);
} }
#ifdef CONFIG_X86_X32_ABI #ifdef CONFIG_X86_X32_ABI
int x32_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) int x32_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
{ {
return setup_additional_pages(bprm, uses_interp, vdsox32_pages, return setup_additional_pages(bprm, uses_interp, vdso_image_x32.pages,
vdsox32_size); vdso_image_x32.size);
} }
#endif #endif
......
...@@ -516,10 +516,17 @@ char * __init xen_memory_setup(void) ...@@ -516,10 +516,17 @@ char * __init xen_memory_setup(void)
static void __init fiddle_vdso(void) static void __init fiddle_vdso(void)
{ {
#ifdef CONFIG_X86_32 #ifdef CONFIG_X86_32
/*
* This could be called before selected_vdso32 is initialized, so
* just fiddle with both possible images. vdso_image_32_syscall
* can't be selected, since it only exists on 64-bit systems.
*/
u32 *mask; u32 *mask;
mask = VDSO32_SYMBOL(&vdso32_int80_start, NOTE_MASK); mask = vdso_image_32_int80.data +
vdso_image_32_int80.sym_VDSO32_NOTE_MASK;
*mask |= 1 << VDSO_NOTE_NONEGSEG_BIT; *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
mask = VDSO32_SYMBOL(&vdso32_sysenter_start, NOTE_MASK); mask = vdso_image_32_sysenter.data +
vdso_image_32_sysenter.sym_VDSO32_NOTE_MASK;
*mask |= 1 << VDSO_NOTE_NONEGSEG_BIT; *mask |= 1 << VDSO_NOTE_NONEGSEG_BIT;
#endif #endif
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment