diff mbox series

[1/2] linux-user: Build vdso for x64.

Message ID 20200519194452.9009-2-richard.henderson@linaro.org
State New
Headers show
Series linux-user: Load a vdso for x86_64 | expand

Commit Message

Richard Henderson May 19, 2020, 7:44 p.m. UTC
From: Richard Henderson <rth@twiddle.net>


... Well, sortof.  The Makefile bits are broken.
Patch to load the vdso into the running program to follow.

Signed-off-by: Richard Henderson <rth@twiddle.net>

---
 Makefile                  |   4 +-
 pc-bios/Makefile          |   5 ++
 pc-bios/vdso-linux-x64.S  | 115 ++++++++++++++++++++++++++++++++++++++
 pc-bios/vdso-linux-x64.ld |  81 +++++++++++++++++++++++++++
 pc-bios/vdso-linux-x64.so | Bin 0 -> 7500 bytes
 5 files changed, 203 insertions(+), 2 deletions(-)
 create mode 100644 pc-bios/vdso-linux-x64.S
 create mode 100644 pc-bios/vdso-linux-x64.ld
 create mode 100755 pc-bios/vdso-linux-x64.so

diff --git a/pc-bios/vdso-linux-x64.so b/pc-bios/vdso-linux-x64.so
new file mode 100755
index 0000000000000000000000000000000000000000..07cba76a4ab52b9bc5be22e2a6e2fe8295691717
GIT binary patch
literal 7500
zcmeHMO=w(I6h4zlnxv*p>K|&V4$_uV>LZaN1%F7|CQa!iDVksv@pYP+W<rx0O<u}G
zBvLVIR3wWof(t?1xGLEQS!tyxR$O%H#+^SXC<>aDI-c*n^G)a7Ok9bAyqnzl?)}a=
z_uYHmeKU{mzBrH_Y)YksB`uFi*t<Pq`pj_M`snt_VcDjiozkHw(j0QIz!nwaDI1#D
zW_Hks-KwmnBMVU^6B3AP(Yks|*xiI5G(|j-aiB5J6`PNX(W2GC?3^cJP_N&r9}RF%
zL<<qVD5t&&@@W5RaV{PVy+nwN+iCnUL-JhjZ+}+HFnww3M=nq@b{*z)#qzK{GRrfD
zHyPex_<J=PZ`I=eu)c-M&(>cwyM_HHke2pZJ~;v0r~5=#W&4x&4c(iS?{fIN&EMq@
zn19Cc?=^py$2P(E?sdnM-zC3P6=LN<?ZLmt#>YHR9?^>VyL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7ue_y%r#$m5Bjow_8!`n*C6u!-v;qTsrLchNN5$+*|

literal 0
HcmV?d00001

-- 
2.20.1
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index 40e4f7677b..73e380ac6a 100644
--- a/Makefile
+++ b/Makefile
@@ -848,8 +848,8 @@  qemu_vga.ndrv \
 edk2-licenses.txt \
 hppa-firmware.img \
 opensbi-riscv32-sifive_u-fw_jump.bin opensbi-riscv32-virt-fw_jump.bin \
-opensbi-riscv64-sifive_u-fw_jump.bin opensbi-riscv64-virt-fw_jump.bin
-
+opensbi-riscv64-sifive_u-fw_jump.bin opensbi-riscv64-virt-fw_jump.bin \
+vdso-linux-x64.so
 
 DESCS=50-edk2-i386-secure.json 50-edk2-x86_64-secure.json \
 60-edk2-aarch64.json 60-edk2-arm.json 60-edk2-i386.json 60-edk2-x86_64.json
diff --git a/pc-bios/Makefile b/pc-bios/Makefile
index 315288df84..70e2485e2e 100644
--- a/pc-bios/Makefile
+++ b/pc-bios/Makefile
@@ -15,5 +15,10 @@  all: $(TARGETS)
 %.dtb: %.dts
 	dtc -I dts -O dtb -o $@ $<
 
+vdso-linux-x64.so: vdso-linux-x64.o vdso-linux-x64.ld
+	$(CC) -nostdlib -shared -Wl,-T,vdso-linux-x64.ld \
+	  -Wl,-h,linux-vdso.so.1 -Wl,--hash-style=both \
+	  vdso-linux-x64.o -o $@
+
 clean:
 	rm -f $(TARGETS) *.o *~
diff --git a/pc-bios/vdso-linux-x64.S b/pc-bios/vdso-linux-x64.S
new file mode 100644
index 0000000000..090d82c26a
--- /dev/null
+++ b/pc-bios/vdso-linux-x64.S
@@ -0,0 +1,115 @@ 
+/*
+ *  x86-64 linux replacement vdso.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <asm/unistd.h>
+
+	.globl	__vdso_clock_gettime
+	.type	__vdso_clock_gettime, @function
+	.balign	16
+	.cfi_startproc
+__vdso_clock_gettime:
+	mov	$__NR_clock_gettime, %eax
+	syscall
+	ret
+	.cfi_endproc
+	.size	__vdso_clock_gettime, . - __vdso_clock_gettime
+
+clock_gettime = __vdso_clock_gettime
+	.weak	clock_gettime
+
+
+	.globl	__vdso_gettimeofday
+	.type	__vdso_gettimeofday, @function
+	.balign	16
+	.cfi_startproc
+__vdso_gettimeofday:
+	mov	$__NR_gettimeofday, %eax
+	syscall
+	ret
+	.cfi_endproc
+	.size	__vdso_gettimeofday, . - __vdso_gettimeofday
+
+gettimeofday = __vdso_gettimeofday
+	.weak	gettimeofday
+
+
+	.globl	__vdso_time
+	.type	__vdso_time, @function
+	.balign	16
+	.cfi_startproc
+__vdso_time:
+	mov	$__NR_time, %eax
+	syscall
+	ret
+	.cfi_endproc
+	.size	__vdso_time, . - __vdso_time
+
+time = __vdso_time
+	.weak	time
+
+
+	.globl	__vdso_getcpu
+	.type	__vdso_getcpu, @function
+	.balign	16
+	.cfi_startproc
+__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
+	.cfi_endproc
+	.size	__vdso_getcpu, . - __vdso_getcpu
+
+getcpu = __vdso_getcpu
+	.weak	getcpu
+
+/* ??? Perhaps add elf notes.  E.g.
+
+   #include <linux/elfnote.h>
+   ELFNOTE_START(Linux, 0, "a")
+	.long LINUX_VERSION_CODE
+   ELFNOTE_END
+
+   but what version number would we set for QEMU?  */
diff --git a/pc-bios/vdso-linux-x64.ld b/pc-bios/vdso-linux-x64.ld
new file mode 100644
index 0000000000..2ef21fbcab
--- /dev/null
+++ b/pc-bios/vdso-linux-x64.ld
@@ -0,0 +1,81 @@ 
+/*
+ * Linker script for linux x64 replacement vdso.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+VERSION {
+	LINUX_2.6 {
+	global:
+		clock_gettime;
+		__vdso_clock_gettime;
+		gettimeofday;
+		__vdso_gettimeofday;
+		getcpu;
+		__vdso_getcpu;
+		time;
+		__vdso_time;
+	local: *;
+	};
+}
+
+PHDRS {
+	phdr		PT_PHDR		FLAGS(4) PHDRS;
+	data		PT_LOAD		FLAGS(6) FILEHDR PHDRS;
+	text		PT_LOAD		FLAGS(5);
+	dynamic		PT_DYNAMIC	FLAGS(4);
+	note		PT_NOTE		FLAGS(4);
+	/* ??? Various versions of ld don't know PT_GNU_EH_FRAME. */
+	eh_frame_hdr	0x6474e550;
+}
+
+SECTIONS {
+	/* ??? We can't really 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.  */
+	.dynamic	: { *(.dynamic) }	:data :dynamic
+	.dynsym		: { *(.dynsym) }	:data
+	.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.*)
+	}
+
+	. += 4096;
+	.hash		: { *(.hash) }		:text
+	.gnu.hash	: { *(.gnu.hash) }
+	.dynstr		: { *(.dynstr) }
+	.gnu.version	: { *(.gnu.version) }
+	.gnu.version_d	: { *(.gnu.version_d) }
+	.gnu.version_r	: { *(.gnu.version_r) }
+	.note		: { *(.note*) }		:text :note
+	.eh_frame_hdr	: { *(.eh_frame_hdr) }	:text :eh_frame_hdr
+	.eh_frame	: { *(.eh_frame) }	:text
+	.rodata		: { *(.rodata) }
+
+	. = ALIGN(0x100);
+	.text		: { *(.text*) }		:text	=0x90909090
+}