diff mbox series

[v4,11/18] linux-user/aarch64: Add vdso

Message ID 20230816180338.572576-12-richard.henderson@linaro.org
State New
Headers show
Series linux-user: Implement VDSOs | expand

Commit Message

Richard Henderson Aug. 16, 2023, 6:03 p.m. UTC
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 linux-user/elfload.c             |   4 ++
 linux-user/aarch64/Makefile.vdso |  12 +++++
 linux-user/aarch64/meson.build   |  12 +++++
 linux-user/aarch64/vdso-be.so    | Bin 0 -> 3216 bytes
 linux-user/aarch64/vdso-le.so    | Bin 0 -> 3216 bytes
 linux-user/aarch64/vdso.S        |  73 +++++++++++++++++++++++++++++++
 linux-user/aarch64/vdso.ld       |  72 ++++++++++++++++++++++++++++++
 linux-user/meson.build           |   1 +
 8 files changed, 174 insertions(+)
 create mode 100644 linux-user/aarch64/Makefile.vdso
 create mode 100644 linux-user/aarch64/meson.build
 create mode 100755 linux-user/aarch64/vdso-be.so
 create mode 100755 linux-user/aarch64/vdso-le.so
 create mode 100644 linux-user/aarch64/vdso.S
 create mode 100644 linux-user/aarch64/vdso.ld
diff mbox series

Patch

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index e8a2375ba8..98cb1ff053 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -588,10 +588,14 @@  static const char *get_elf_platform(void)
 #define ELF_CLASS       ELFCLASS64
 #if TARGET_BIG_ENDIAN
 # define ELF_PLATFORM    "aarch64_be"
+# include "vdso-be.c.inc"
 #else
 # define ELF_PLATFORM    "aarch64"
+# include "vdso-le.c.inc"
 #endif
 
+#define vdso_image_info()    &vdso_image_info
+
 static inline void init_thread(struct target_pt_regs *regs,
                                struct image_info *infop)
 {
diff --git a/linux-user/aarch64/Makefile.vdso b/linux-user/aarch64/Makefile.vdso
new file mode 100644
index 0000000000..53c19e1ce9
--- /dev/null
+++ b/linux-user/aarch64/Makefile.vdso
@@ -0,0 +1,12 @@ 
+CROSS_CC ?= aarch64-linux-gnu-gcc
+LDFLAGS := -nostdlib -shared -Wl,-T,vdso.ld \
+	   -Wl,-h,linux-vdso.so.1 -Wl,--hash-style=both -Wl,--build-id=sha1 \
+	   -Wl,-z,max-page-size=4096
+
+all: vdso-le.so vdso-be.so
+
+vdso-le.so: vdso.S vdso.ld Makefile.vdso
+	$(CROSS_CC)  $(LDFLAGS) -mlittle-endian vdso.S -o $@
+
+vdso-be.so: vdso.S vdso.ld Makefile.vdso
+	$(CROSS_CC)  $(LDFLAGS) -mbig-endian vdso.S -o $@
diff --git a/linux-user/aarch64/meson.build b/linux-user/aarch64/meson.build
new file mode 100644
index 0000000000..35e50c9b2c
--- /dev/null
+++ b/linux-user/aarch64/meson.build
@@ -0,0 +1,12 @@ 
+# ??? There does not seem to be a way to do
+#   when: ['TARGET_AARCH64', !'TARGET_WORDS_BIGENDIAN']
+# so we'd need to add TARGET_WORDS_LITTLEENDIAN.
+# In the meantime, build both files for aarch64 and aarch64_be,
+# only one of which will be included.
+
+gen = [
+  gen_vdso.process('vdso-be.so', extra_args: ['-r', '__kernel_rt_sigreturn']),
+  gen_vdso.process('vdso-le.so', extra_args: ['-r', '__kernel_rt_sigreturn'])
+]
+
+linux_user_ss.add(when: 'TARGET_AARCH64', if_true: gen)
diff --git a/linux-user/aarch64/vdso-be.so b/linux-user/aarch64/vdso-be.so
new file mode 100755
index 0000000000000000000000000000000000000000..311f192d88149f744ee05c2492e8e8de6c5b5ec4
GIT binary patch
literal 3216
zcmc&$L2nyH6rS~N;}lA9Dk>rcL6IU<p=K3=1VQb=33i$yHw~esp_i$#H@4HnM)syS
zNI(u;I8np_aY07n0)GI+g&sgE;#L9b1#v>EgoN5dk&tN3_h#N3uPqxPao{Dh-+bS^
zH#0jkZ@h2k3rne#r=0of3)B;WY0295@hi;c)jpL~8TBCe7)oH<p;;S1Z_>(wY8;%#
zNLzi>Gq`121T#caC_J9mApu==4D`^_-fN7P<6l=Ha$L~14uhy3;y^+2mx8LdZrcY@
z<v31|-jCvF!uw9HoKsg(fsy8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ml_jxF@rwH-~2a
zwEhw&?@w+UC^8=zzfBG3htYi}TG`$AhSD(}jHmsExqJ4g<?fjr|1GEz&*w1U^%FGn
zcfqL8zKKz+yq9Q<@*SOe4FY{r9_9uae-fO2Ly~LDE(Ga|Og?86kogjyKp7D~&iA!?
JM*<mL{NMe#CGr3O

literal 0
HcmV?d00001

diff --git a/linux-user/aarch64/vdso-le.so b/linux-user/aarch64/vdso-le.so
new file mode 100755
index 0000000000000000000000000000000000000000..797ab5d9371bb66415f674a9013aae823b92668c
GIT binary patch
literal 3216
zcmcgu&1+Ow6hD&}o0z1=NX0;;wU8Dm^%0d8i(Sm5GclG)w5ej6&2^l4$xKaV<jsqn
zlzzBy<DxXBt8U6D-L(Her4;JIMZrxGTigjkp-?DsQ9;m7&w20sX5QR9u^{v$_nmux
z=j*<E&pGox&E=<hQYivC^gVS}L}?YQ_d`RlL$rmT+i94dVYrt^TE#XWRcGLEBXFPE
zB(V)~$^_2ALh9kCh??rx=Iza^9Py6-9+bqcp?+=tT<Dz$T-Cx@GM`s}HqWXZIO2(o
zY<UZhBeN$(>zZJW%$Q^@v1Bds$%~hE%`bfY((WH;K6-HF?wRYK{qh=%Xq!Bk79tkf
z$@I87`um?&P1YB5E%Ah|>RRqcs3`}>G!QZRK>48ZQRNR?k-xNTkm-@G)tokTJ)!bQ
z>;Hec9C8N@(QVy_rUzve|3h)y$NP+r(7l$uQ@K|0bJg;*OYxsA+_uLWc__z@GnYB;
zL-aI-2std6=m;)D?0%qhiu$AU7Cn)F<sH8L=jV+tuAg4mo^8Io=TYX{J7mmR&#!vR
zX2}bJ%8FN8EEU!|XmPn#JPTO@?0fYN>IbG?S@OML)vwZWrMlYKcfM4wx%|JM@<(S+
z&YJ`7>+Zofcr(0v{K+b%f0uvy>siU?-8_)1@=iT0zjVX&p|0c}gi>S9VZlTv&Er)b
zLb%7&hjY~1cs?~Y^g@V9eh%@N5G5uv?)<uU=Y^YWf9slKehb&zy4TaOb$KL`Mb^nr
zhj)Yej%zsl7ERfxMGoIi6NM3zIb5X^a&Z<poDHbkxX9s~NBwr=eeF8##``I${v6s$
zsM~J+fp*{P#s_I#^Sc}G)B|vN{oaK7kvoxFw9jog#5eni^atsigt$rmcZA;Wwe0gB
z3;l0?FX;;@-siM+{#nM8>i-?allsV#;&vbTSaG{we5v@Reo-u*I#;RBF1s}vFVT2|
zO!mFm%*@fZxcs>Bo7}XSn#;`O%yf2+%#r-D$xPlHo0@tzcfy>=Oy+Z@y8_#F->&e-
zmvUS8zw`)l>uW1P;SBS@k9ZlgYAx`{Ev;1xE0tn6VO{2hpg`_Yb=55w>g5Oz{0KSk
z`SnVz+O;qxa=o%y^b0GVDVJCiD{=#`5%3v4ElFO;cLmB4c?m9g5GVXT0DF-q(ua{g
z$UAUOu8{EU0(<nWS6C{)sfdsM0KOiXhkF!Pu*dK30mcMJeDo3UJuHh8?7`n;q5S3`
zKKczf`Yimz4&``=ZSV7MJnYezz(p=(+eawTq{?8A?+|c&cR*i{6H9uVXRTO#^f~aT
ze{|v_4)`gymv0uV(GS6~f8+?>9~)TYiR=mXkNZx9KF$qm$>F#?`YZTeC@Qz(f1>vI
zhCrVI$Nu5QjvtQ=9pM|QcurqTzAJa~K>SfrV%o|BcN+rwIIRi)T^!`OeSag2CD{*X
Jpo>qi{}(kpCh`CP

literal 0
HcmV?d00001

diff --git a/linux-user/aarch64/vdso.S b/linux-user/aarch64/vdso.S
new file mode 100644
index 0000000000..e436e60fd9
--- /dev/null
+++ b/linux-user/aarch64/vdso.S
@@ -0,0 +1,73 @@ 
+/*
+ * aarch64 linux replacement vdso.
+ *
+ * Copyright 2023 Linaro, Ltd.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <asm/unistd.h>
+
+/* ??? These are in include/elf.h, which is not ready for inclusion in asm. */
+#define NT_GNU_PROPERTY_TYPE_0  5
+#define GNU_PROPERTY_AARCH64_FEATURE_1_AND      0xc0000000
+#define GNU_PROPERTY_AARCH64_FEATURE_1_BTI      (1U << 0)
+#define GNU_PROPERTY_AARCH64_FEATURE_1_PAC      (1U << 1)
+
+#define GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT \
+    (GNU_PROPERTY_AARCH64_FEATURE_1_BTI | GNU_PROPERTY_AARCH64_FEATURE_1_PAC)
+
+	.section .note.gnu.property
+	.align	3
+	.long	2f - 1f
+	.long	6f - 3f
+	.long	NT_GNU_PROPERTY_TYPE_0
+1:	.string	"GNU"
+2:	.align	3
+3:	.long	GNU_PROPERTY_AARCH64_FEATURE_1_AND
+	.long	5f - 4f
+4:	.long	GNU_PROPERTY_AARCH64_FEATURE_1_DEFAULT
+5:	.align	3
+6:
+
+	.text
+
+.macro endf name
+	.globl	\name
+	.type	\name, @function
+	.size	\name, . - \name
+.endm
+
+.macro vdso_syscall name, nr
+\name:
+	bti	c
+	mov	x8, #\nr
+	svc	#0
+	ret
+endf	\name
+.endm
+
+	.cfi_startproc
+
+vdso_syscall __kernel_gettimeofday, __NR_gettimeofday
+vdso_syscall __kernel_clock_gettime, __NR_clock_gettime
+vdso_syscall __kernel_clock_getres, __NR_clock_getres
+
+	.cfi_endproc
+
+
+/*
+ * TODO: The kernel makes a big deal of turning off the .cfi directives,
+ * because they cause libgcc to crash, but that's because they're wrong.
+ *
+ * For now, elide the unwind info for __kernel_rt_sigreturn and rely on
+ * the libgcc fallback routine as we have always done.  This requires
+ * that the code sequence used be exact.
+ */
+__kernel_rt_sigreturn:
+	/* No BTI C insn here -- we arrive via RET. */
+	mov	x8, #__NR_rt_sigreturn
+	svc	#0
+endf	__kernel_rt_sigreturn
+
+/* TODO: Add elf note for LINUX_VERSION_CODE */
diff --git a/linux-user/aarch64/vdso.ld b/linux-user/aarch64/vdso.ld
new file mode 100644
index 0000000000..4c12f33352
--- /dev/null
+++ b/linux-user/aarch64/vdso.ld
@@ -0,0 +1,72 @@ 
+/*
+ * Linker script for linux aarch64 replacement vdso.
+ *
+ * Copyright 2021 Linaro, Ltd.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+VERSION {
+        LINUX_2.6.39 {
+        global:
+                __kernel_rt_sigreturn;
+                __kernel_gettimeofday;
+                __kernel_clock_gettime;
+                __kernel_clock_getres;
+
+        local: *;
+        };
+}
+
+
+PHDRS {
+        phdr            PT_PHDR         FLAGS(4) PHDRS;
+        load            PT_LOAD         FLAGS(7) FILEHDR PHDRS;
+        dynamic         PT_DYNAMIC      FLAGS(4);
+        eh_frame_hdr    PT_GNU_EH_FRAME;
+        note            PT_NOTE         FLAGS(4);
+}
+
+SECTIONS {
+        /*
+         * We can't prelink to any address without knowing something about
+         * the virtual memory space of the host, since that leaks over into
+         * the available memory space of the guest.
+         */
+        . = 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
+        /*
+         * There ought not be any real read-write data.
+         * But since we manipulated the segment layout,
+         * we have to put these sections somewhere.
+         */
+        .data           : {
+                *(.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   =0xd503201f
+}
diff --git a/linux-user/meson.build b/linux-user/meson.build
index e4cb70ed2d..dd24389052 100644
--- a/linux-user/meson.build
+++ b/linux-user/meson.build
@@ -35,6 +35,7 @@  gen_vdso_exe = executable('gen-vdso', 'gen-vdso.c',
 gen_vdso = generator(gen_vdso_exe, output: '@BASENAME@.c.inc',
                      arguments: ['-o', '@OUTPUT@', '@EXTRA_ARGS@', '@INPUT@'])
 
+subdir('aarch64')
 subdir('alpha')
 subdir('arm')
 subdir('hppa')