OSDN Git Service

linux-user/x86_64: Add vdso
authorRichard Henderson <richard.henderson@linaro.org>
Thu, 17 Jun 2021 21:39:14 +0000 (14:39 -0700)
committerRichard Henderson <richard.henderson@linaro.org>
Mon, 30 Oct 2023 20:41:55 +0000 (13:41 -0700)
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
linux-user/elfload.c
linux-user/x86_64/Makefile.vdso [new file with mode: 0644]
linux-user/x86_64/meson.build
linux-user/x86_64/vdso.S [new file with mode: 0644]
linux-user/x86_64/vdso.ld [new file with mode: 0644]
linux-user/x86_64/vdso.so [new file with mode: 0755]

index 46f73a4..62a3348 100644 (file)
@@ -318,10 +318,10 @@ static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *en
         }                                               \
     } while (0)
 
-#define VDSO_HEADER "vdso.c.inc"
-
 #endif /* TARGET_X86_64 */
 
+#define VDSO_HEADER "vdso.c.inc"
+
 #define USE_ELF_CORE_DUMP
 #define ELF_EXEC_PAGESIZE       4096
 
diff --git a/linux-user/x86_64/Makefile.vdso b/linux-user/x86_64/Makefile.vdso
new file mode 100644 (file)
index 0000000..26552b6
--- /dev/null
@@ -0,0 +1,11 @@
+include $(BUILD_DIR)/tests/tcg/x86_64-linux-user/config-target.mak
+
+SUBDIR = $(SRC_PATH)/linux-user/x86_64
+VPATH += $(SUBDIR)
+
+all: $(SUBDIR)/vdso.so
+
+$(SUBDIR)/vdso.so: vdso.S vdso.ld
+       $(CC) -o $@ -nostdlib -shared -Wl,-h,linux-vdso.so.1 \
+         -Wl,--build-id=sha1 -Wl,--hash-style=both \
+         -Wl,-T,$(SUBDIR)/vdso.ld $<
index 203af9a..8c60da7 100644 (file)
@@ -3,3 +3,7 @@ syscall_nr_generators += {
                       arguments: [ meson.current_source_dir() / 'syscallhdr.sh', '@INPUT@', '@OUTPUT@', '@EXTRA_ARGS@' ],
                       output: '@BASENAME@_nr.h')
 }
+
+vdso_inc = gen_vdso.process('vdso.so')
+
+linux_user_ss.add(when: 'TARGET_X86_64', if_true: vdso_inc)
diff --git a/linux-user/x86_64/vdso.S b/linux-user/x86_64/vdso.S
new file mode 100644 (file)
index 0000000..47d16c0
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * x86-64 linux replacement vdso.
+ *
+ * Copyright 2023 Linaro, Ltd.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <asm/unistd.h>
+
+.macro endf name
+       .globl  \name
+       .type   \name, @function
+       .size   \name, . - \name
+.endm
+
+.macro weakalias name
+\name  = __vdso_\name
+       .weak   \name
+.endm
+
+.macro vdso_syscall name, nr
+__vdso_\name:
+       mov     $\nr, %eax
+       syscall
+       ret
+endf   __vdso_\name
+weakalias \name
+.endm
+
+       .cfi_startproc
+
+vdso_syscall clock_gettime, __NR_clock_gettime
+vdso_syscall clock_getres, __NR_clock_getres
+vdso_syscall gettimeofday, __NR_gettimeofday
+vdso_syscall time, __NR_time
+
+__vdso_getcpu:
+       /*
+         * There is no syscall number for this allocated on x64.
+        * We can handle this several ways:
+         *
+        * (1) Invent a syscall number for use within qemu.
+         *     It should be easy enough to pick a number that
+         *     is well out of the way of the kernel numbers.
+         *
+         * (2) Force the emulated cpu to support the rdtscp insn,
+        *     and initialize the TSC_AUX value the appropriate value.
+         *
+        * (3) Pretend that we're always running on cpu 0.
+         *
+        * This last is the one that's implemented here, with the
+        * tiny bit of extra code to support rdtscp in place.
+         */
+       xor     %ecx, %ecx              /* rdtscp w/ tsc_aux = 0 */
+
+       /* if (cpu != NULL) *cpu = (ecx & 0xfff); */
+       test    %rdi, %rdi
+       jz      1f
+       mov     %ecx, %eax
+       and     $0xfff, %eax
+       mov     %eax, (%rdi)
+
+       /* if (node != NULL) *node = (ecx >> 12); */
+1:     test    %rsi, %rsi
+       jz      2f
+       shr     $12, %ecx
+       mov     %ecx, (%rsi)
+
+2:     xor     %eax, %eax
+       ret
+endf   __vdso_getcpu
+
+weakalias getcpu
+
+       .cfi_endproc
+
+/* TODO: Add elf note for LINUX_VERSION_CODE */
diff --git a/linux-user/x86_64/vdso.ld b/linux-user/x86_64/vdso.ld
new file mode 100644 (file)
index 0000000..ca6001c
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Linker script for linux x86-64 replacement vdso.
+ *
+ * Copyright 2023 Linaro, Ltd.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+VERSION {
+        LINUX_2.6 {
+        global:
+                clock_gettime;
+                __vdso_clock_gettime;
+                gettimeofday;
+                __vdso_gettimeofday;
+                getcpu;
+                __vdso_getcpu;
+                time;
+                __vdso_time;
+                clock_getres;
+                __vdso_clock_getres;
+
+        local: *;
+        };
+}
+
+
+PHDRS {
+        phdr            PT_PHDR         FLAGS(4) PHDRS;
+        load            PT_LOAD         FLAGS(7) FILEHDR PHDRS; /* FLAGS=RWX */
+        dynamic         PT_DYNAMIC      FLAGS(4);
+        eh_frame_hdr    PT_GNU_EH_FRAME;
+        note            PT_NOTE         FLAGS(4);
+}
+
+SECTIONS {
+        . = SIZEOF_HEADERS;
+
+        /*
+         * The following, including the FILEHDRS and PHDRS, are modified
+         * when we relocate the binary.  We want them to be initially
+         * writable for the relocation; we'll force them read-only after.
+         */
+        .note           : { *(.note*) }         :load :note
+        .dynamic        : { *(.dynamic) }       :load :dynamic
+        .dynsym         : { *(.dynsym) }        :load
+        .data           : {
+                /*
+                 * There ought not be any real read-write data.
+                 * But since we manipulated the segment layout,
+                 * we have to put these sections somewhere.
+                 */
+                *(.data*)
+                *(.sdata*)
+                *(.got.plt) *(.got)
+                *(.gnu.linkonce.d.*)
+                *(.bss*)
+                *(.dynbss*)
+                *(.gnu.linkonce.b.*)
+        }
+
+        .rodata         : { *(.rodata*) }
+        .hash           : { *(.hash) }
+        .gnu.hash       : { *(.gnu.hash) }
+        .dynstr         : { *(.dynstr) }
+        .gnu.version    : { *(.gnu.version) }
+        .gnu.version_d  : { *(.gnu.version_d) }
+        .gnu.version_r  : { *(.gnu.version_r) }
+        .eh_frame_hdr   : { *(.eh_frame_hdr) }  :load :eh_frame_hdr
+        .eh_frame       : { *(.eh_frame) }      :load
+
+        .text           : { *(.text*) }         :load   =0x90909090
+}
diff --git a/linux-user/x86_64/vdso.so b/linux-user/x86_64/vdso.so
new file mode 100755 (executable)
index 0000000..c873d6e
Binary files /dev/null and b/linux-user/x86_64/vdso.so differ