[CRIU] [PATCH 1/3] compel: Move string helpers in STD subplugin
Cyrill Gorcunov
gorcunov at openvz.org
Fri Jan 27 09:14:18 PST 2017
We will use these helpers in criu in explicit way.
Note the final STD plugin does link with it and
it not changed from api perspective.
Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
compel/arch/aarch64/plugins/std/string.c | 1 +
compel/arch/arm/plugins/std/string.c | 1 +
compel/arch/ppc64/plugins/std/string.c | 18 ++++
compel/arch/x86/plugins/std/string.S | 32 +++++++
compel/plugins/Makefile | 9 +-
compel/plugins/include/uapi/plugin-std.h | 1 +
compel/plugins/include/uapi/std/print.h | 23 +++++
compel/plugins/include/uapi/std/string.h | 23 +----
compel/plugins/std/print.c | 127 +++++++++++++++++++++++++++
compel/plugins/std/string.c | 146 +++----------------------------
10 files changed, 228 insertions(+), 153 deletions(-)
create mode 120000 compel/arch/aarch64/plugins/std/string.c
create mode 120000 compel/arch/arm/plugins/std/string.c
create mode 100644 compel/arch/ppc64/plugins/std/string.c
create mode 100644 compel/arch/x86/plugins/std/string.S
create mode 100644 compel/plugins/include/uapi/std/print.h
create mode 100644 compel/plugins/std/print.c
diff --git a/compel/arch/aarch64/plugins/std/string.c b/compel/arch/aarch64/plugins/std/string.c
new file mode 120000
index 000000000000..fdea543e4ce7
--- /dev/null
+++ b/compel/arch/aarch64/plugins/std/string.c
@@ -0,0 +1 @@
+../../../ppc64/plugins/std/string.c
\ No newline at end of file
diff --git a/compel/arch/arm/plugins/std/string.c b/compel/arch/arm/plugins/std/string.c
new file mode 120000
index 000000000000..fdea543e4ce7
--- /dev/null
+++ b/compel/arch/arm/plugins/std/string.c
@@ -0,0 +1 @@
+../../../ppc64/plugins/std/string.c
\ No newline at end of file
diff --git a/compel/arch/ppc64/plugins/std/string.c b/compel/arch/ppc64/plugins/std/string.c
new file mode 100644
index 000000000000..159639174219
--- /dev/null
+++ b/compel/arch/ppc64/plugins/std/string.c
@@ -0,0 +1,18 @@
+void *std_memcpy(void *to, const void *from, unsigned int n)
+{
+ char *tmp = to;
+ const char *s = from;
+
+ while (n--)
+ *tmp++ = *s++;
+ return to;
+}
+
+void std_memset(void *s, int c, unsigned int n)
+{
+ char *xs = s;
+
+ while (count--)
+ *xs++ = c;
+ return s;
+}
diff --git a/compel/arch/x86/plugins/std/string.S b/compel/arch/x86/plugins/std/string.S
new file mode 100644
index 000000000000..5fe2ab254a3c
--- /dev/null
+++ b/compel/arch/x86/plugins/std/string.S
@@ -0,0 +1,32 @@
+#include "common/asm/linkage.h"
+
+/*
+ * Adopted from linux kernel, we use REP reature only.
+ */
+
+ENTRY(std_memcpy)
+ movq %rdi, %rax
+ movq %rdx, %rcx
+ shrq $3, %rcx
+ andl $7, %edx
+ rep movsq
+ movl %edx, %ecx
+ rep movsb
+ ret
+END(std_memcpy)
+
+ENTRY(std_memset)
+ movq %rdi,%r9
+ movq %rdx,%rcx
+ andl $7,%edx
+ shrq $3,%rcx
+ /* expand byte value */
+ movzbl %sil,%esi
+ movabs $0x0101010101010101,%rax
+ imulq %rsi,%rax
+ rep stosq
+ movl %edx,%ecx
+ rep stosb
+ movq %r9,%rax
+ ret
+END(std_memset)
diff --git a/compel/plugins/Makefile b/compel/plugins/Makefile
index 58732405752c..c7754d79e943 100644
--- a/compel/plugins/Makefile
+++ b/compel/plugins/Makefile
@@ -43,10 +43,17 @@ target += std
std-obj-y += std/std.o
std-obj-y += std/fds.o
std-obj-y += std/log.o
-std-obj-y += std/string.o
+std-obj-y += std/print.o
std-obj-y += std/infect.o
std-obj-y += ./$(PLUGIN_ARCH_DIR)/std/parasite-head.o
+target += stdstr
+stdstr-obj-y += std/string.o
+stdstr-obj-y += ./$(PLUGIN_ARCH_DIR)/std/string.o
+
+$(obj)/std.built-in.o: $(obj)/stdstr.built-in.o
+std-obj-e += stdstr.built-in.o
+
include ./$(PLUGIN_ARCH_DIR)/std/syscalls/Makefile.syscalls
define syscall-priority
diff --git a/compel/plugins/include/uapi/plugin-std.h b/compel/plugins/include/uapi/plugin-std.h
index 78619ecd75ad..f8093921f0bb 100644
--- a/compel/plugins/include/uapi/plugin-std.h
+++ b/compel/plugins/include/uapi/plugin-std.h
@@ -5,6 +5,7 @@
#include <compel/plugins/std/syscall.h>
#include <compel/plugins/std/string.h>
#include <compel/plugins/std/infect.h>
+#include <compel/plugins/std/print.h>
#include <compel/plugins/std/fds.h>
#endif /* COMPEL_PLUGIN_STD_STD_H__ */
diff --git a/compel/plugins/include/uapi/std/print.h b/compel/plugins/include/uapi/std/print.h
new file mode 100644
index 000000000000..03fa0425676a
--- /dev/null
+++ b/compel/plugins/include/uapi/std/print.h
@@ -0,0 +1,23 @@
+#ifndef COMPEL_PLUGIN_STD_PRINT_H__
+#define COMPEL_PLUGIN_STD_PRINT_H__
+
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdarg.h>
+
+/* Standard file descriptors. */
+#define STDIN_FILENO 0 /* Standard input. */
+#define STDOUT_FILENO 1 /* Standard output. */
+#define STDERR_FILENO 2 /* Standard error output. */
+
+
+extern void __std_putc(int fd, char c);
+extern void __std_puts(int fd, const char *s);
+extern void __std_printk(int fd, const char *format, va_list args);
+extern void __std_printf(int fd, const char *format, ...);
+
+#define std_printf(fmt, ...) __std_printf(STDOUT_FILENO, fmt, ##__VA_ARGS__)
+#define std_puts(s) __std_puts(STDOUT_FILENO, s)
+#define std_putchar(c) __std_putc(STDOUT_FILENO, c)
+
+#endif /* COMPEL_PLUGIN_STD_PRINT_H__ */
diff --git a/compel/plugins/include/uapi/std/string.h b/compel/plugins/include/uapi/std/string.h
index 8aec886bd093..65514db35a02 100644
--- a/compel/plugins/include/uapi/std/string.h
+++ b/compel/plugins/include/uapi/std/string.h
@@ -1,28 +1,11 @@
#ifndef COMPEL_PLUGIN_STD_STRING_H__
#define COMPEL_PLUGIN_STD_STRING_H__
-#include <sys/types.h>
-#include <stdbool.h>
-#include <stdarg.h>
-
-/* Standard file descriptors. */
-#define STDIN_FILENO 0 /* Standard input. */
-#define STDOUT_FILENO 1 /* Standard output. */
-#define STDERR_FILENO 2 /* Standard error output. */
-
-
-extern void __std_putc(int fd, char c);
-extern void __std_puts(int fd, const char *s);
-extern void __std_printk(int fd, const char *format, va_list args);
-extern void __std_printf(int fd, const char *format, ...);
-
-#define std_printf(fmt, ...) __std_printf(STDOUT_FILENO, fmt, ##__VA_ARGS__)
-#define std_puts(s) __std_puts(STDOUT_FILENO, s)
-#define std_putchar(c) __std_putc(STDOUT_FILENO, c)
-
extern unsigned long std_strtoul(const char *nptr, char **endptr, int base);
extern void *std_memcpy(void *to, const void *from, unsigned int n);
-extern int std_memcmp(const void *cs, const void *ct, size_t count);
+extern void std_memset(void *s, int c, unsigned int n);
+extern int std_memcmp(const void *cs, const void *ct, unsigned int count);
extern int std_strcmp(const char *cs, const char *ct);
+extern int std_strncmp(const char *cs, const char *ct, unsigned int count);
#endif /* COMPEL_PLUGIN_STD_STRING_H__ */
diff --git a/compel/plugins/std/print.c b/compel/plugins/std/print.c
new file mode 100644
index 000000000000..068f5d575ce2
--- /dev/null
+++ b/compel/plugins/std/print.c
@@ -0,0 +1,127 @@
+#include <sys/types.h>
+#include <stdbool.h>
+#include <stdarg.h>
+
+#include "uapi/std/syscall.h"
+#include "uapi/std/print.h"
+
+static const char conv_tab[] = "0123456789abcdefghijklmnopqrstuvwxyz";
+
+void __std_putc(int fd, char c)
+{
+ sys_write(fd, &c, 1);
+}
+
+void __std_puts(int fd, const char *s)
+{
+ for (; *s; s++)
+ __std_putc(fd, *s);
+}
+
+static size_t __std_vprint_long_hex(char *buf, size_t blen, unsigned long num, char **ps)
+{
+ char *s = &buf[blen - 2];
+
+ buf[blen - 1] = '\0';
+
+ if (num == 0) {
+ *s = '0', s--;
+ goto done;
+ }
+
+ while (num > 0) {
+ *s = conv_tab[num % 16], s--;
+ num /= 16;
+ }
+
+done:
+ s++;
+ *ps = s;
+ return blen - (s - buf);
+}
+
+static size_t __std_vprint_long(char *buf, size_t blen, long num, char **ps)
+{
+ char *s = &buf[blen - 2];
+ int neg = 0;
+
+ buf[blen - 1] = '\0';
+
+ if (num < 0) {
+ neg = 1;
+ num = -num;
+ } else if (num == 0) {
+ *s = '0';
+ s--;
+ goto done;
+ }
+
+ while (num > 0) {
+ *s = (num % 10) + '0';
+ s--;
+ num /= 10;
+ }
+
+ if (neg) {
+ *s = '-';
+ s--;
+ }
+done:
+ s++;
+ *ps = s;
+ return blen - (s - buf);
+}
+
+void __std_printk(int fd, const char *format, va_list args)
+{
+ const char *s = format;
+
+ for (; *s != '\0'; s++) {
+ char buf[32], *t;
+ int along = 0;
+
+ if (*s != '%') {
+ __std_putc(fd, *s);
+ continue;
+ }
+
+ s++;
+ if (*s == 'l') {
+ along = 1;
+ s++;
+ if (*s == 'l')
+ s++;
+ }
+
+ switch (*s) {
+ case 's':
+ __std_puts(fd, va_arg(args, char *));
+ break;
+ case 'd':
+ __std_vprint_long(buf, sizeof(buf),
+ along ?
+ va_arg(args, long) :
+ (long)va_arg(args, int),
+ &t);
+ __std_puts(fd, t);
+ break;
+ case 'x':
+ __std_vprint_long_hex(buf, sizeof(buf),
+ along ?
+ va_arg(args, long) :
+ (long)va_arg(args, int),
+ &t);
+ __std_puts(fd, t);
+ break;
+ }
+ }
+}
+
+void __std_printf(int fd, const char *format, ...)
+{
+ va_list args;
+
+ va_start(args, format);
+ __std_printk(fd, format, args);
+ va_end(args);
+}
diff --git a/compel/plugins/std/string.c b/compel/plugins/std/string.c
index e987e408ec8b..84e064cb54e1 100644
--- a/compel/plugins/std/string.c
+++ b/compel/plugins/std/string.c
@@ -1,131 +1,10 @@
#include <sys/types.h>
#include <stdbool.h>
-#include <stdarg.h>
-#include "uapi/std/syscall.h"
#include "uapi/std/string.h"
static const char conv_tab[] = "0123456789abcdefghijklmnopqrstuvwxyz";
-void __std_putc(int fd, char c)
-{
- sys_write(fd, &c, 1);
-}
-
-void __std_puts(int fd, const char *s)
-{
- for (; *s; s++)
- __std_putc(fd, *s);
-}
-
-static size_t __std_vprint_long_hex(char *buf, size_t blen, unsigned long num, char **ps)
-{
- char *s = &buf[blen - 2];
-
- buf[blen - 1] = '\0';
-
- if (num == 0) {
- *s = '0', s--;
- goto done;
- }
-
- while (num > 0) {
- *s = conv_tab[num % 16], s--;
- num /= 16;
- }
-
-done:
- s++;
- *ps = s;
- return blen - (s - buf);
-}
-
-static size_t __std_vprint_long(char *buf, size_t blen, long num, char **ps)
-{
- char *s = &buf[blen - 2];
- int neg = 0;
-
- buf[blen - 1] = '\0';
-
- if (num < 0) {
- neg = 1;
- num = -num;
- } else if (num == 0) {
- *s = '0';
- s--;
- goto done;
- }
-
- while (num > 0) {
- *s = (num % 10) + '0';
- s--;
- num /= 10;
- }
-
- if (neg) {
- *s = '-';
- s--;
- }
-done:
- s++;
- *ps = s;
- return blen - (s - buf);
-}
-
-void __std_printk(int fd, const char *format, va_list args)
-{
- const char *s = format;
-
- for (; *s != '\0'; s++) {
- char buf[32], *t;
- int along = 0;
-
- if (*s != '%') {
- __std_putc(fd, *s);
- continue;
- }
-
- s++;
- if (*s == 'l') {
- along = 1;
- s++;
- if (*s == 'l')
- s++;
- }
-
- switch (*s) {
- case 's':
- __std_puts(fd, va_arg(args, char *));
- break;
- case 'd':
- __std_vprint_long(buf, sizeof(buf),
- along ?
- va_arg(args, long) :
- (long)va_arg(args, int),
- &t);
- __std_puts(fd, t);
- break;
- case 'x':
- __std_vprint_long_hex(buf, sizeof(buf),
- along ?
- va_arg(args, long) :
- (long)va_arg(args, int),
- &t);
- __std_puts(fd, t);
- break;
- }
- }
-}
-
-void __std_printf(int fd, const char *format, ...)
-{
- va_list args;
-
- va_start(args, format);
- __std_printk(fd, format, args);
- va_end(args);
-}
-
static inline bool __isspace(unsigned char c)
{
return c == ' ' || c == '\f' ||
@@ -220,17 +99,7 @@ fin:
return neg ? (unsigned long)-num : (unsigned long)num;
}
-void *std_memcpy(void *to, const void *from, unsigned int n)
-{
- char *tmp = to;
- const char *s = from;
-
- while (n--)
- *tmp++ = *s++;
- return to;
-}
-
-int std_memcmp(const void *cs, const void *ct, size_t count)
+int std_memcmp(const void *cs, const void *ct, unsigned int count)
{
const unsigned char *su1, *su2;
int res = 0;
@@ -255,3 +124,16 @@ int std_strcmp(const char *cs, const char *ct)
}
return 0;
}
+
+int std_strncmp(const char *cs, const char *ct, unsigned int count)
+{
+ unsigned int i;
+
+ for (i = 0; i < count; i++) {
+ if (cs[i] != ct[i])
+ return cs[i] < ct[i] ? -1 : 1;
+ if (!cs[i])
+ break;
+ }
+ return 0;
+}
--
2.7.4
More information about the CRIU
mailing list