qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RFC v3 03/10] build: Implement libnative library and configure options


From: Yeqi Fu
Subject: [RFC v3 03/10] build: Implement libnative library and configure options
Date: Mon, 26 Jun 2023 05:27:00 +0800

This commit implements a shared library, where native functions are
rewritten as specialized instructions. At runtime, user programs load
the shared library, and specialized instructions are executed when
native functions are called.

Signed-off-by: Yeqi Fu <fufuyqqqqqq@gmail.com>
---
 Makefile                            |   2 +
 common-user/native/Makefile.include |   9 +++
 common-user/native/Makefile.target  |  26 +++++++
 common-user/native/libnative.c      | 112 ++++++++++++++++++++++++++++
 configure                           |  84 ++++++++++++++++-----
 include/native/libnative.h          |  12 +++
 include/native/native-defs.h        |  65 ++++++++++++++++
 7 files changed, 293 insertions(+), 17 deletions(-)
 create mode 100644 common-user/native/Makefile.include
 create mode 100644 common-user/native/Makefile.target
 create mode 100644 common-user/native/libnative.c
 create mode 100644 include/native/libnative.h
 create mode 100644 include/native/native-defs.h

diff --git a/Makefile b/Makefile
index 3c7d67142f..787b8954a6 100644
--- a/Makefile
+++ b/Makefile
@@ -185,6 +185,8 @@ SUBDIR_MAKEFLAGS=$(if $(V),,--no-print-directory --quiet)
 
 include $(SRC_PATH)/tests/Makefile.include
 
+include $(SRC_PATH)/common-user/native/Makefile.include
+
 all: recurse-all
 
 ROMS_RULES=$(foreach t, all clean distclean, $(addsuffix /$(t), $(ROMS)))
diff --git a/common-user/native/Makefile.include 
b/common-user/native/Makefile.include
new file mode 100644
index 0000000000..40d20bcd4c
--- /dev/null
+++ b/common-user/native/Makefile.include
@@ -0,0 +1,9 @@
+.PHONY: build-native
+build-native: $(NATIVE_TARGETS:%=build-native-library-%)
+$(NATIVE_TARGETS:%=build-native-library-%): build-native-library-%:
+       $(call quiet-command, \
+           $(MAKE) -C common-user/native/$* $(SUBDIR_MAKEFLAGS), \
+       "BUILD","$* native library")
+# endif
+
+all: build-native
diff --git a/common-user/native/Makefile.target 
b/common-user/native/Makefile.target
new file mode 100644
index 0000000000..1bb468a2ec
--- /dev/null
+++ b/common-user/native/Makefile.target
@@ -0,0 +1,26 @@
+# -*- Mode: makefile -*-
+#
+# Library for native calls 
+#
+
+all:
+-include ../config-host.mak
+-include config-target.mak
+
+CFLAGS+=-O1 -fPIC -shared -fno-stack-protector -I$(SRC_PATH)/include 
-D$(TARGET_NAME) 
+LDFLAGS+=
+
+ifeq ($(TARGET_NAME),arm)
+EXTRA_CFLAGS+=-marm
+endif
+
+SRC = $(SRC_PATH)/common-user/native/libnative.c
+LIBNATIVE = libnative.so
+
+all: $(LIBNATIVE)
+
+$(LIBNATIVE): $(SRC)
+       $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $< -o $@ $(LDFLAGS)
+
+clean:
+       rm -f $(LIBNATIVE)
diff --git a/common-user/native/libnative.c b/common-user/native/libnative.c
new file mode 100644
index 0000000000..26a004e3b4
--- /dev/null
+++ b/common-user/native/libnative.c
@@ -0,0 +1,112 @@
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "native/libnative.h"
+#include "native/native-defs.h"
+
+#if defined(i386) || defined(x86_64)
+/* unused opcode */
+#define WRAP_NATIVE_CALL(func_id, abi_map)                          \
+    do {                                                            \
+        __asm__ volatile(".byte 0x0f,0xff\n\t"                      \
+                         ".word %c[imm1],%c[imm2]\n\t"              \
+                         : /* no outputs */                         \
+                         : [imm1] "i"(func_id), [imm2] "i"(abi_map) \
+                         :);                                        \
+    } while (0)
+
+#endif
+
+#if defined(arm) || defined(aarch64)
+/*
+ * HLT is an invalid instruction for userspace and usefully has 16
+ * bits of spare immeadiate data which we can stuff data in.
+ */
+#define WRAP_NATIVE_CALL(func_id, abi_map)            \
+    do {                                              \
+        __asm__ volatile("hlt  %c0\n\t"               \
+                         "hlt  %c1\n\t"               \
+                         : /* no outputs */           \
+                         : "i"(func_id), "i"(abi_map) \
+                         : "memory");                 \
+    } while (0)
+
+#endif
+
+#if defined(mips) || defined(mips64)
+/*
+ * There are some unused bytes in the syscall instruction
+ */
+#define WRAP_NATIVE_CALL(func_id, abi_map)            \
+    do {                                              \
+        __asm__ volatile("syscall  %c0\n\t"           \
+                         "syscall  %c1\n\t"           \
+                         : /* no outputs */           \
+                         : "i"(func_id), "i"(abi_map) \
+                         : "memory");                 \
+    } while (0)
+
+#endif
+
+static inline const uint32_t encode_1out_3in(int rtype, int arg1, int arg2,
+                                             int arg3)
+{
+    return (rtype & 0xf) | ((arg1 & 0xf) << 4) | ((arg2 & 0xf) << 8) |
+           ((arg3 & 0xf) << 12);
+}
+
+static inline const uint32_t encode_0out_3in(int arg1, int arg2, int arg3)
+{
+    return encode_1out_3in(TYPE_NO_ARG, arg1, arg2, arg3);
+}
+
+static inline const uint32_t encode_1out_2in(int rtype, int arg1, int arg2)
+{
+    return encode_1out_3in(rtype, arg1, arg2, TYPE_NO_ARG);
+}
+
+void *memcpy(void *dest, const void *src, size_t n)
+{
+    const uint32_t args = encode_1out_3in(TYPE_PTR_ARG, TYPE_PTR_ARG_W,
+                                          TYPE_PTR_ARG_R, TYPE_IPTR_ARG);
+    WRAP_NATIVE_CALL(NATIVE_MEMCPY, args);
+}
+
+int memcmp(const void *s1, const void *s2, size_t n)
+{
+    const uint32_t args = encode_1out_3in(TYPE_INT_ARG, TYPE_PTR_ARG_R,
+                                          TYPE_PTR_ARG_R, TYPE_IPTR_ARG);
+    WRAP_NATIVE_CALL(NATIVE_MEMCMP, args);
+}
+
+void *memset(void *s, int c, size_t n)
+{
+    const uint32_t args = encode_1out_3in(TYPE_PTR_ARG, TYPE_PTR_ARG_W,
+                                          TYPE_INT_ARG, TYPE_IPTR_ARG);
+    WRAP_NATIVE_CALL(NATIVE_MEMSET, args);
+}
+char *strncpy(char *dest, const char *src, size_t n)
+{
+    const uint32_t args = encode_1out_3in(TYPE_PTR_ARG, TYPE_PTR_ARG_W,
+                                          TYPE_PTR_ARG_R, TYPE_IPTR_ARG);
+    WRAP_NATIVE_CALL(NATIVE_STRNCPY, args);
+}
+int strncmp(const char *s1, const char *s2, size_t n)
+{
+    const uint32_t args = encode_1out_3in(TYPE_INT_ARG, TYPE_PTR_ARG_R,
+                                          TYPE_PTR_ARG_R, TYPE_IPTR_ARG);
+    WRAP_NATIVE_CALL(NATIVE_STRNCMP, args);
+}
+char *strcpy(char *dest, const char *src)
+{
+    const uint32_t args =
+        encode_1out_2in(TYPE_PTR_ARG, TYPE_PTR_ARG_W, TYPE_PTR_ARG_R);
+    WRAP_NATIVE_CALL(NATIVE_STRCPY, args);
+}
+int strcmp(const char *s1, const char *s2)
+{
+    const uint32_t args =
+        encode_1out_2in(TYPE_INT_ARG, TYPE_PTR_ARG_R, TYPE_PTR_ARG_R);
+    WRAP_NATIVE_CALL(NATIVE_STRCMP, args);
+}
diff --git a/configure b/configure
index 2a556d14c9..64edbda892 100755
--- a/configure
+++ b/configure
@@ -1838,48 +1838,42 @@ if test "$ccache_cpp2" = "yes"; then
   echo "export CCACHE_CPP2=y" >> $config_host_mak
 fi
 
-# tests/tcg configuration
-(config_host_mak=tests/tcg/config-host.mak
-mkdir -p tests/tcg
-echo "# Automatically generated by configure - do not modify" > 
$config_host_mak
-echo "SRC_PATH=$source_path" >> $config_host_mak
-echo "HOST_CC=$host_cc" >> $config_host_mak
+# prepare config files for cross build
+config_corss_build_host_mak=cross-build/config-host.mak
+mkdir -p cross-build
+echo "# Automatically generated by configure - do not modify" > 
$config_corss_build_host_mak
+echo "SRC_PATH=$source_path" >> $config_corss_build_host_mak
+echo "HOST_CC=$host_cc" >> $config_corss_build_host_mak
 
 # versioned checked in the main config_host.mak above
 if test -n "$gdb_bin"; then
-    echo "HAVE_GDB_BIN=$gdb_bin" >> $config_host_mak
+    echo "HAVE_GDB_BIN=$gdb_bin" >> $config_corss_build_host_mak
 fi
 if test "$plugins" = "yes" ; then
-    echo "CONFIG_PLUGIN=y" >> $config_host_mak
+    echo "CONFIG_PLUGIN=y" >> $config_corss_build_host_mak
 fi
 
-tcg_tests_targets=
 for target in $target_list; do
   arch=${target%%-*}
-
   case $target in
     xtensa*-linux-user)
-      # the toolchain is not complete with headers, only build softmmu tests
       continue
       ;;
     *-softmmu)
-      test -f "$source_path/tests/tcg/$arch/Makefile.softmmu-target" || 
continue
       qemu="qemu-system-$arch"
       ;;
     *-linux-user|*-bsd-user)
       qemu="qemu-$arch"
       ;;
   esac
-
   if probe_target_compiler $target || test -n "$container_image"; then
       test -n "$container_image" && build_static=y
-      mkdir -p "tests/tcg/$target"
-      config_target_mak=tests/tcg/$target/config-target.mak
-      ln -sf "$source_path/tests/tcg/Makefile.target" 
"tests/tcg/$target/Makefile"
+      mkdir -p "cross-build/$target"
+      config_target_mak=cross-build/$target/config-target.mak
       echo "# Automatically generated by configure - do not modify" > 
"$config_target_mak"
       echo "TARGET_NAME=$arch" >> "$config_target_mak"
       echo "TARGET=$target" >> "$config_target_mak"
-      write_target_makefile "build-tcg-tests-$target" >> "$config_target_mak"
+      write_target_makefile "$target" >> "$config_target_mak"
       echo "BUILD_STATIC=$build_static" >> "$config_target_mak"
       echo "QEMU=$PWD/$qemu" >> "$config_target_mak"
 
@@ -1887,7 +1881,34 @@ for target in $target_list; do
       if test "${gdb_arches#*$arch}" != "$gdb_arches"; then
           echo "HOST_GDB_SUPPORTS_ARCH=y" >> "$config_target_mak"
       fi
+  fi
+done
+
+
+# tests/tcg configuration
+(mkdir -p tests/tcg
+ln -srf $config_corss_build_host_mak tests/tcg/config-host.mak
+
+tcg_tests_targets=
+for target in $target_list; do
+  arch=${target%%-*}
+
+  case $target in
+    xtensa*-linux-user)
+      # the toolchain is not complete with headers, only build softmmu tests
+      continue
+      ;;
+    *-softmmu)
+      test -f "$source_path/tests/tcg/$arch/Makefile.softmmu-target" || 
continue
+      ;;
+    *-linux-user|*-bsd-user)
+      ;;
+  esac
 
+  if probe_target_compiler $target || test -n "$container_image"; then
+      mkdir -p "tests/tcg/$target"
+      ln -srf cross-build/$target/config-target.mak 
tests/tcg/$target/config-target.mak
+      ln -sf $source_path/tests/tcg/Makefile.target tests/tcg/$target/Makefile
       echo "run-tcg-tests-$target: $qemu\$(EXESUF)" >> Makefile.prereqs
       tcg_tests_targets="$tcg_tests_targets $target"
   fi
@@ -1898,6 +1919,35 @@ if test "$tcg" = "enabled"; then
 fi
 )
 
+# common-user/native configuration
+(mkdir -p common-user/native
+# reuse the same config-host.mak as tests/tcg
+ln -srf $config_corss_build_host_mak common-user/native/config-host.mak
+
+native_targets=
+for target in $target_list; do
+  arch=${target%%-*}
+  # Skip targets that are not supported
+  case "$arch" in
+    i386|x86_64|arm|aarch64|mips|mips64)
+      case $target in
+        *-linux-user|*-bsd-user)
+          if probe_target_compiler $target || test -n "$container_image"; then
+            mkdir -p "common-user/native/$target"
+            # reuse the same config-target.mak as tests/tcg
+            ln -srf cross-build/$target/config-target.mak 
common-user/native/$target/config-target.mak
+            ln -sf $source_path/common-user/native/Makefile.target 
common-user/native/$target/Makefile
+            native_targets="$native_targets $target"
+          fi
+        ;;
+      esac
+    ;;
+  esac
+done
+
+echo "NATIVE_TARGETS=$native_targets" >> config-host.mak
+)
+
 if test "$skip_meson" = no; then
   cross="config-meson.cross.new"
   meson_quote() {
diff --git a/include/native/libnative.h b/include/native/libnative.h
new file mode 100644
index 0000000000..25a59833db
--- /dev/null
+++ b/include/native/libnative.h
@@ -0,0 +1,12 @@
+#ifndef __LIBNATIVE_H__
+#define __LIBNATIVE_H__
+
+void *memcpy(void *dest, const void *src, size_t n);
+int memcmp(const void *s1, const void *s2, size_t n);
+void *memset(void *s, int c, size_t n);
+char *strncpy(char *dest, const char *src, size_t n);
+int strncmp(const char *s1, const char *s2, size_t n);
+char *strcpy(char *dest, const char *src);
+int strcmp(const char *s1, const char *s2);
+
+#endif /* __LIBNATIVE_H__ */
diff --git a/include/native/native-defs.h b/include/native/native-defs.h
new file mode 100644
index 0000000000..37ed479408
--- /dev/null
+++ b/include/native/native-defs.h
@@ -0,0 +1,65 @@
+#ifndef __NATIVE_FUNC_H__
+#define __NATIVE_FUNC_H__
+
+/*
+ * Native function IDs. These are used to identify the native function
+ */
+#define NATIVE_MEMCPY 0x1001
+#define NATIVE_MEMCMP 0x1002
+#define NATIVE_MEMSET 0x1003
+#define NATIVE_STRNCPY 0x1004
+#define NATIVE_STRNCMP 0x1005
+#define NATIVE_STRCPY 0x2001
+#define NATIVE_STRCMP 0x2002
+
+/*
+ * Argument encoding. We only really care about 3 types. The two base
+ * register sizes (32 and 64) and if the value is a pointer (in which
+ * case we need to adjust it g2h before passing to the native
+ * function).
+ */
+#define TYPE_NO_ARG 0x0
+#define TYPE_I32_ARG 0x1 /* uint32_t */
+#define TYPE_I64_ARG 0x2 /* uint64_t */
+#define TYPE_INT_ARG 0x3 /* int */
+#define TYPE_PTR_ARG 0x4 /* void* */
+#define TYPE_PTR_ARG_R 0x5
+#define TYPE_PTR_ARG_W 0x6
+
+/*
+ * Add an alias for the natural register size, it might be easier to
+ * pass this in.
+ */
+#if UINTPTR_MAX == 0xFFFFFFFF
+#define TYPE_IPTR_ARG TYPE_I32_ARG
+#elif UINTPTR_MAX == 0xFFFFFFFFFFFFFFFFu
+#define TYPE_IPTR_ARG TYPE_I64_ARG
+#else
+#error TBD pointer size
+#endif
+
+#define GET_RVALUE(types) ((types)&0xf)
+#define GET_ARG1(types) (((types) >> 4) & 0xf)
+#define GET_ARG2(types) (((types) >> 8) & 0xf)
+#define GET_ARG3(types) (((types) >> 12) & 0xf)
+
+#define IS_PTR_RVALUE(types) (GET_RVALUE(types) >= TYPE_PTR_ARG)
+#define IS_PTR_ARG1(types) (GET_ARG1(types) >= TYPE_PTR_ARG)
+#define IS_PTR_ARG2(types) (GET_ARG2(types) >= TYPE_PTR_ARG)
+#define IS_PTR_ARG3(types) (GET_ARG3(types) >= TYPE_PTR_ARG)
+
+#define IS_ARG_PTR(type) (type >= TYPE_PTR_ARG)
+#define IS_ARG_READ(type) (type == TYPE_PTR_ARG_R)
+#define IS_ARG_WRITE(type) (type == TYPE_PTR_ARG_W)
+
+/*
+ * Check if the native bypass feature is enabled.
+ */
+#if defined(CONFIG_USER_ONLY) && defined(CONFIG_NATIVE_CALL)
+extern bool native_bypass_enabled;
+#define native_call_enabled() (native_bypass_enabled)
+#else
+#define native_call_enabled() 0
+#endif
+
+#endif /* __NATIVE_FUNC_H__ */
-- 
2.34.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]