From d209c00a30f474a849ad20515efc3de9f0de4991 Mon Sep 17 00:00:00 2001 From: Tomoki Shirasawa Date: Tue, 26 Dec 2017 10:30:33 +0900 Subject: [PATCH] part of Issue#994 mcexec: open syscall moves to arch_dep do_fork: don't use __NR_fork. use __NR_clone vfork: moves to arch_dep --- arch/arm64/kernel/include/syscall_list.h | 2 - arch/x86_64/kernel/include/syscall_list.h | 2 +- arch/x86_64/kernel/syscall.c | 10 ++++ executer/user/arch/arm64/Makefile.in | 7 ++- executer/user/arch/arm64/arch_syscall.c | 7 +++ executer/user/arch/x86_64/Makefile.in | 7 ++- executer/user/arch/x86_64/arch_syscall.c | 63 +++++++++++++++++++++++ executer/user/archdep.h | 1 + executer/user/mcexec.c | 54 +++---------------- kernel/syscall.c | 7 +-- 10 files changed, 99 insertions(+), 61 deletions(-) create mode 100644 executer/user/arch/arm64/arch_syscall.c create mode 100644 executer/user/arch/x86_64/arch_syscall.c diff --git a/arch/arm64/kernel/include/syscall_list.h b/arch/arm64/kernel/include/syscall_list.h index 80a1222a..f370ad7a 100644 --- a/arch/arm64/kernel/include/syscall_list.h +++ b/arch/arm64/kernel/include/syscall_list.h @@ -144,5 +144,3 @@ SYSCALL_HANDLED(1045, signalfd) SYSCALL_DELEGATED(1049, stat) SYSCALL_DELEGATED(1060, getpgrp) SYSCALL_DELEGATED(1062, time) -SYSCALL_HANDLED(1071, vfork) -SYSCALL_DELEGATED(1079, fork) diff --git a/arch/x86_64/kernel/include/syscall_list.h b/arch/x86_64/kernel/include/syscall_list.h index 7c6edcbc..48b1ea0a 100644 --- a/arch/x86_64/kernel/include/syscall_list.h +++ b/arch/x86_64/kernel/include/syscall_list.h @@ -56,7 +56,7 @@ SYSCALL_HANDLED(36, getitimer) SYSCALL_HANDLED(38, setitimer) SYSCALL_HANDLED(39, getpid) SYSCALL_HANDLED(56, clone) -SYSCALL_DELEGATED(57, fork) +SYSCALL_HANDLED(57, fork) SYSCALL_HANDLED(58, vfork) SYSCALL_HANDLED(59, execve) SYSCALL_HANDLED(60, exit) diff --git a/arch/x86_64/kernel/syscall.c b/arch/x86_64/kernel/syscall.c index 68f1e0b4..1e0f6025 100644 --- a/arch/x86_64/kernel/syscall.c +++ b/arch/x86_64/kernel/syscall.c @@ -1487,6 +1487,16 @@ SYSCALL_DECLARE(clone) ihk_mc_syscall_sp(ctx)); } +SYSCALL_DECLARE(fork) +{ + return do_fork(SIGCHLD, 0, 0, 0, 0, ihk_mc_syscall_pc(ctx), ihk_mc_syscall_sp(ctx)); +} + +SYSCALL_DECLARE(vfork) +{ + return do_fork(CLONE_VFORK|SIGCHLD, 0, 0, 0, 0, ihk_mc_syscall_pc(ctx), ihk_mc_syscall_sp(ctx)); +} + SYSCALL_DECLARE(shmget) { const key_t key = ihk_mc_syscall_arg0(ctx); diff --git a/executer/user/arch/arm64/Makefile.in b/executer/user/arch/arm64/Makefile.in index 29e52115..b913d94e 100644 --- a/executer/user/arch/arm64/Makefile.in +++ b/executer/user/arch/arm64/Makefile.in @@ -9,12 +9,15 @@ LIBS=@LIBS@ all: $(TARGET) -../../libmcexec.a: archdep.o - $(AR) cr ../../libmcexec.a archdep.o +../../libmcexec.a: archdep.o arch_syscall.o + $(AR) cr ../../libmcexec.a archdep.o arch_syscall.o archdep.o: archdep.S $(CC) -c -I${KDIR} $(CFLAGS) $(EXTRA_CFLAGS) -fPIE -pie -pthread $< +arch_syscall.o: arch_syscall.c + $(CC) -c -I${KDIR} $(CFLAGS) $(EXTRA_CFLAGS) -fPIE -pie -pthread $< + clean: $(RM) $(TARGET) *.o diff --git a/executer/user/arch/arm64/arch_syscall.c b/executer/user/arch/arm64/arch_syscall.c new file mode 100644 index 00000000..8aefe8a5 --- /dev/null +++ b/executer/user/arch/arm64/arch_syscall.c @@ -0,0 +1,7 @@ +struct syscall_wait_desc; + +int +archdep_syscall(struct syscall_wait_desc *w, long *ret) +{ + return -1; +} diff --git a/executer/user/arch/x86_64/Makefile.in b/executer/user/arch/x86_64/Makefile.in index 29e52115..b913d94e 100644 --- a/executer/user/arch/x86_64/Makefile.in +++ b/executer/user/arch/x86_64/Makefile.in @@ -9,12 +9,15 @@ LIBS=@LIBS@ all: $(TARGET) -../../libmcexec.a: archdep.o - $(AR) cr ../../libmcexec.a archdep.o +../../libmcexec.a: archdep.o arch_syscall.o + $(AR) cr ../../libmcexec.a archdep.o arch_syscall.o archdep.o: archdep.S $(CC) -c -I${KDIR} $(CFLAGS) $(EXTRA_CFLAGS) -fPIE -pie -pthread $< +arch_syscall.o: arch_syscall.c + $(CC) -c -I${KDIR} $(CFLAGS) $(EXTRA_CFLAGS) -fPIE -pie -pthread $< + clean: $(RM) $(TARGET) *.o diff --git a/executer/user/arch/x86_64/arch_syscall.c b/executer/user/arch/x86_64/arch_syscall.c new file mode 100644 index 00000000..4aa63874 --- /dev/null +++ b/executer/user/arch/x86_64/arch_syscall.c @@ -0,0 +1,63 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../../../include/uprotocol.h" +#include "../../archdep.h" + +//#define DEBUG +#ifndef DEBUG +#define __dprint(msg, ...) +#define __dprintf(arg, ...) +#define __eprint(msg, ...) +#define __eprintf(format, ...) +#else +#define __dprint(msg, ...) {printf("%s: " msg, __FUNCTION__);fflush(stdout);} +#define __dprintf(format, ...) {printf("%s: " format, __FUNCTION__, \ + __VA_ARGS__);fflush(stdout);} +#define __eprint(msg, ...) {fprintf(stderr, "%s: " msg, __FUNCTION__);\ + fflush(stderr);} +#define __eprintf(format, ...) {fprintf(stderr, "%s: " format, __FUNCTION__, \ + __VA_ARGS__);fflush(stderr);} +#endif + +extern char *chgpath(char *, char *); +extern long do_strncpy_from_user(int, void *, void *, unsigned long); +extern int fd; + +#define SET_ERR(ret) if (ret == -1) ret = -errno + +int +archdep_syscall(struct syscall_wait_desc *w, long *ret) +{ + char *fn; + char pathbuf[PATH_MAX]; + char tmpbuf[PATH_MAX]; + + switch (w->sr.number) { + case __NR_open: + *ret = do_strncpy_from_user(fd, pathbuf, + (void *)w->sr.args[0], PATH_MAX); + if (*ret >= PATH_MAX) { + *ret = -ENAMETOOLONG; + } + if (ret < 0) { + return 0; + } + __dprintf("open: %s\n", pathbuf); + + fn = chgpath(pathbuf, tmpbuf); + + *ret = open(fn, w->sr.args[1], w->sr.args[2]); + SET_ERR(*ret); + return 0; + } + return -1; +} diff --git a/executer/user/archdep.h b/executer/user/archdep.h index 47f33142..abe81042 100644 --- a/executer/user/archdep.h +++ b/executer/user/archdep.h @@ -1,3 +1,4 @@ extern int switch_ctx(int fd, unsigned long cmd, void **param, void *lctx, void *rctx); extern unsigned long compare_and_swap(unsigned long *addr, unsigned long old, unsigned long new); extern unsigned int compare_and_swap_int(unsigned int *addr, unsigned int old, unsigned int new); +extern int archdep_syscall(struct syscall_wait_desc *w, long *ret); diff --git a/executer/user/mcexec.c b/executer/user/mcexec.c index 2d0d50ef..e4a99e43 100644 --- a/executer/user/mcexec.c +++ b/executer/user/mcexec.c @@ -206,7 +206,7 @@ struct thread_data_s; int main_loop(struct thread_data_s *); static int mcosid; -static int fd; +int fd; static char *exec_path = NULL; static char *altroot; static const char rlimit_stack_envname[] = "MCKERNEL_RLIMIT_STACK"; @@ -2993,7 +2993,7 @@ out: return rc; } -static long do_strncpy_from_user(int fd, void *dest, void *src, unsigned long n) +long do_strncpy_from_user(int fd, void *dest, void *src, unsigned long n) { struct strncpy_from_user_desc desc; int ret; @@ -3207,8 +3207,6 @@ int main_loop(struct thread_data_s *my_thread) my_thread->remote_cpu = w.cpu; switch (w.sr.number) { -#ifdef POSTK_DEBUG_ARCH_DEP_13 /* arch depend hide */ -#ifdef __aarch64__ case __NR_openat: /* initialize buffer */ memset(tmpbuf, '\0', sizeof(tmpbuf)); @@ -3255,44 +3253,6 @@ int main_loop(struct thread_data_s *my_thread) SET_ERR(ret); do_syscall_return(fd, cpu, ret, 0, 0, 0, 0); break; -#else /* __aarch64__ */ - case __NR_open: - ret = do_strncpy_from_user(fd, pathbuf, (void *)w.sr.args[0], PATH_MAX); - if (ret >= PATH_MAX) { - ret = -ENAMETOOLONG; - } - if (ret < 0) { - do_syscall_return(fd, cpu, ret, 0, 0, 0, 0); - break; - } - __dprintf("open: %s\n", pathbuf); - - fn = chgpath(pathbuf, tmpbuf); - - ret = open(fn, w.sr.args[1], w.sr.args[2]); - SET_ERR(ret); - do_syscall_return(fd, cpu, ret, 0, 0, 0, 0); - break; -#endif /* __aarch64__ */ -#else /* POSTK_DEBUG_ARCH_DEP_13 */ - case __NR_open: - ret = do_strncpy_from_user(fd, pathbuf, (void *)w.sr.args[0], PATH_MAX); - if (ret >= PATH_MAX) { - ret = -ENAMETOOLONG; - } - if (ret < 0) { - do_syscall_return(fd, cpu, ret, 0, 0, 0, 0); - break; - } - __dprintf("open: %s\n", pathbuf); - - fn = chgpath(pathbuf, tmpbuf); - - ret = open(fn, w.sr.args[1], w.sr.args[2]); - SET_ERR(ret); - do_syscall_return(fd, cpu, ret, 0, 0, 0, 0); - break; -#endif /* POSTK_DEBUG_ARCH_DEP_13 */ case __NR_futex: ret = clock_gettime(w.sr.args[1], &tv); @@ -3413,11 +3373,7 @@ gettid_out: break; } -#ifdef POSTK_DEBUG_ARCH_DEP_13 /* arch depend hide */ - case 1079: { -#else /* POSTK_DEBUG_ARCH_DEP_13 */ - case __NR_fork: { -#endif /* POSTK_DEBUG_ARCH_DEP_13 */ + case __NR_clone: { struct fork_sync *fs; struct fork_sync_container *fsc; struct fork_sync_container *fp; @@ -4285,7 +4241,9 @@ return_linux_spawn: } default: - ret = do_generic_syscall(&w); + if (archdep_syscall(&w, &ret)) { + ret = do_generic_syscall(&w); + } do_syscall_return(fd, cpu, ret, 0, 0, 0, 0); break; diff --git a/kernel/syscall.c b/kernel/syscall.c index 6f13d429..66ab98d9 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -2450,7 +2450,7 @@ retry_tid: } /* fork() a new process on the host */ else { - request1.number = __NR_fork; + request1.number = __NR_clone; request1.args[0] = 0; if(clone_flags & CLONE_PARENT){ if(oldproc->ppid_parent->pid != 1) @@ -2614,11 +2614,6 @@ retry_tid: return new->tid; } -SYSCALL_DECLARE(vfork) -{ - return do_fork(CLONE_VFORK|SIGCHLD, 0, 0, 0, 0, ihk_mc_syscall_pc(ctx), ihk_mc_syscall_sp(ctx)); -} - SYSCALL_DECLARE(set_tid_address) { cpu_local_var(current)->clear_child_tid =