]> xenbits.xensource.com Git - people/liuw/rumprun.git/commitdiff
Sketch out new interfaces for bootstrap/running programs.
authorAntti Kantee <pooka@iki.fi>
Wed, 22 Apr 2015 11:19:09 +0000 (11:19 +0000)
committerAntti Kantee <pooka@iki.fi>
Wed, 22 Apr 2015 14:05:36 +0000 (14:05 +0000)
The system is "booted" using rumprun_boot().  The opposite of
rumprun_boot() is rumprun_reboot() (logically?)

Between rumprun_boot() and rumprun_reboot() individual applications may
be launched with rumprun() (async) and waited for with rumprun_wait().

Of course, bits for supporting >1 program are still missing, but at
least the sketch is there.

include/rumprun-base/netbsd_initfini.h [deleted file]
include/rumprun-base/rumprun.h [new file with mode: 0644]
lib/librumprun_base/Makefile
lib/librumprun_base/_lwp.c
lib/librumprun_base/netbsd_initfini.c
lib/librumprun_base/rumprun-private.h [new file with mode: 0644]
lib/librumprun_base/rumprun.c [new file with mode: 0644]
lib/librumprun_base/syscall_misc.c
platform/baremetal/init.c
platform/xen/callmain.c

diff --git a/include/rumprun-base/netbsd_initfini.h b/include/rumprun-base/netbsd_initfini.h
deleted file mode 100644 (file)
index 56f8ae6..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-/*-
- * Copyright (c) 2014 Antti Kantee.  All Rights Reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
- * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#ifndef _BMK_BASE_NETBSD_INITFINI_H_
-#define _BMK_BASE_NETBSD_INITFINI_H_
-
-void _netbsd_init(void);
-void __dead _netbsd_fini(void);
-void rumprun_lwp_init(void);
-
-#endif /* _BMK_BASE_NETBSD_INITFINI_H_ */
diff --git a/include/rumprun-base/rumprun.h b/include/rumprun-base/rumprun.h
new file mode 100644 (file)
index 0000000..7e17739
--- /dev/null
@@ -0,0 +1,37 @@
+/*-
+ * Copyright (c) 2015 Antti Kantee.  All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _RUMPRUN_BASE_RUMPRUN_H_
+#define _RUMPRUN_BASE_RUMPRUN_H_
+
+void   rumprun_boot(void);
+
+void * rumprun(int (*)(int, char *[]), int, char *[]);
+int    rumprun_wait(void *);
+void * rumprun_get_finished(void);
+
+void   rumprun_reboot(void) __dead;
+
+#endif /* _RUMPRUN_BASE_RUMPRUN_H_ */
index 7b300a54778430d433e796179badf656020fc775..d4cde20160b972879c65fbace6195f9d5b3dd2c5 100644 (file)
@@ -5,7 +5,8 @@ DPSRCS+=        ${CONFIG_MK}
 LIB=           rumprun_base
 LIBISPRIVATE=  # defined
 
-SRCS=          malloc.c netbsd_initfini.c signals.c syscall_misc.c
+SRCS=          rumprun.c
+SRCS+=         malloc.c netbsd_initfini.c signals.c syscall_misc.c
 SRCS+=         __errno.c _lwp.c libc_stubs.c
 
 CPPFLAGS+=     -I${.CURDIR}/../../include
index 62fe7c896517c3cde7ae2a9d69d58c3f96e803e7..e83f9e2294724d3757ebf799bc0aec2e6cc82208 100644 (file)
 #include <string.h>
 #include <unistd.h>
 
-#include <rumprun-base/netbsd_initfini.h>
-#include <rumprun-base/makelwp.h>
-
 #include <bmk-core/core.h>
 #include <bmk-core/sched.h>
 
+#include <rumprun-base/makelwp.h>
+
+#include "rumprun-private.h"
+
 #if 0
 #define DPRINTF(x) printf x
 #else
index 055c0183524c8a5cf95c767569cf084ea430ba8d..40bc1d353534d6181e7d0982769794db7603f351 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
-#include <rump/rump.h>
-#include <rump/rump_syscalls.h>
-
 #include <bmk-core/core.h>
 
-#include <rumprun-base/netbsd_initfini.h>
 #include <rumprun-base/config.h>
 
+#include "rumprun-private.h"
+
 static char *the_env[1] = { NULL } ;
 extern void *environ;
 void _libc_init(void);
@@ -79,41 +77,23 @@ runfini(void)
 }
 
 void
-_netbsd_init(void)
+_netbsd_userlevel_init(void)
 {
        thestrings.ps_argvstr = (void *)((char *)&myaux - 2);
        __ps_strings = &thestrings;
        pthread__stacksize = 2*bmk_stacksize;
 
-       rump_boot_setsigmodel(RUMP_SIGMODEL_IGNORE);
-       rump_init();
-
        environ = the_env;
-       rumprun_lwp_init();
        runinit();
        _libc_init();
 
        /* XXX: we should probably use csu, but this is quicker for now */
        __progname = "rumprun";
-
-#ifdef RUMP_SYSPROXY
-       rump_init_server("tcp://0:12345");
-#endif
-       _rumprun_config();
-
-       /*
-        * give all threads a chance to run, and ensure that the main
-        * thread has gone through a context switch
-        */
-       sched_yield();
 }
 
-void __dead
-_netbsd_fini(void)
+void
+_netbsd_userlevel_fini(void)
 {
-       _rumprun_deconfig();
-       runfini();
-       rump_sys_reboot(0, 0);
 
-       bmk_platform_halt("reboot returned");
+       runfini();
 }
diff --git a/lib/librumprun_base/rumprun-private.h b/lib/librumprun_base/rumprun-private.h
new file mode 100644 (file)
index 0000000..c58b6f0
--- /dev/null
@@ -0,0 +1,34 @@
+/*-
+ * Copyright (c) 2014 Antti Kantee.  All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _RUMPRUN_BASE_RUMPRUN_PRIVATE_H_
+#define _RUMPRUN_BASE_RUMPRUN_PRIVATE_H_
+
+void _netbsd_userlevel_init(void);
+void _netbsd_userlevel_fini(void);
+
+void rumprun_lwp_init(void);
+
+#endif /* _RUMPRUN_BASE_RUMPRUN_PRIVATE_H_ */
diff --git a/lib/librumprun_base/rumprun.c b/lib/librumprun_base/rumprun.c
new file mode 100644 (file)
index 0000000..adac39a
--- /dev/null
@@ -0,0 +1,206 @@
+/*-
+ * Copyright (c) 2015 Antti Kantee.  All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/queue.h>
+
+#include <assert.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <sched.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <rump/rump.h>
+#include <rump/rump_syscalls.h>
+
+#include <bmk-core/platform.h>
+
+#include <rumprun-base/rumprun.h>
+#include <rumprun-base/config.h>
+
+#include "rumprun-private.h"
+
+static pthread_mutex_t w_mtx;
+static pthread_cond_t w_cv;
+
+void
+rumprun_boot(void)
+{
+
+       rump_boot_setsigmodel(RUMP_SIGMODEL_IGNORE);
+       rump_init();
+
+       /*
+        * XXX: _netbsd_userlevel_init() should technically be called
+        * in mainbouncer() per process.  However, there's currently no way
+        * to run it per process, and besides we need a fully functional
+        * libc to run sysproxy and rumprun_config(), so we just call it
+        * here for the time being.
+        *
+        * Eventually, we of course want bootstrap process which is
+        * rumprun() internally.
+        */
+       rumprun_lwp_init();
+       _netbsd_userlevel_init();
+
+#ifdef RUMP_SYSPROXY
+       rump_init_server("tcp://0:12345");
+#endif
+       _rumprun_config();
+
+       /*
+        * give all threads a chance to run, and ensure that the main
+        * thread has gone through a context switch
+        */
+       sched_yield();
+
+       pthread_mutex_init(&w_mtx, NULL);
+       pthread_cond_init(&w_cv, NULL);
+}
+
+/*
+ * XXX: we have to use pthreads as the main threads for rumprunners
+ * because otherwise libpthread goes haywire because it doesn't understand
+ * the concept of multiple main threads (which is sort of understandable ...)
+ */
+struct rumprunner {
+       int (*rr_mainfun)(int, char *[]);
+       int rr_argc;
+       char **rr_argv;
+
+       pthread_t rr_mainthread;
+
+       int rr_done;
+
+       LIST_ENTRY(rumprunner) rr_entries;
+};
+static LIST_HEAD(,rumprunner) rumprunners = LIST_HEAD_INITIALIZER(&rumprunners);
+static int rumprun_done;
+
+static void *
+mainbouncer(void *arg)
+{
+       struct rumprunner *rr = arg;
+       const char *progname = rr->rr_argv[0];
+       int rv;
+
+       rump_pub_lwproc_rfork(RUMP_RFFDG);
+
+       fprintf(stderr,"\n=== calling \"%s\" main() ===\n\n", progname);
+       rv = rr->rr_mainfun(rr->rr_argc, rr->rr_argv);
+       fprintf(stderr,"\n=== main() of \"%s\" returned %d ===\n",
+           progname, rv);
+
+       /*
+        * XXX: missing _netbsd_userlevel_fini().  See comment in
+        * rumprun_boot()
+        */
+
+       /* XXX: does not yet nuke any pthread that mainfun creates */
+       rr->rr_done = 1;
+
+       pthread_mutex_lock(&w_mtx);
+       rumprun_done++;
+       pthread_cond_broadcast(&w_cv);
+       pthread_mutex_unlock(&w_mtx);
+
+       /* exit() calls rumprun_pub_lwproc_releaselwp() (via _exit()) */
+       exit(rv);
+}
+
+void *
+rumprun(int (*mainfun)(int, char *[]), int argc, char *argv[])
+{
+       struct rumprunner *rr;
+       static int called;
+
+       if (called)
+               bmk_platform_halt(">1 rumprun() calls not implemented yet");
+       called = 1;
+
+       rr = malloc(sizeof(*rr));
+
+       /* XXX: should we deep copy argc? */
+       rr->rr_mainfun = mainfun;
+       rr->rr_argc = argc;
+       rr->rr_argv = argv;
+       rr->rr_done = 0;
+
+       if (pthread_create(&rr->rr_mainthread, NULL, mainbouncer, rr) != 0) {
+               fprintf(stderr, "rumprun: running %s failed\n", argv[0]);
+               free(rr);
+               return NULL;
+       }
+       LIST_INSERT_HEAD(&rumprunners, rr, rr_entries);
+
+       return rr;
+}
+
+int
+rumprun_wait(void *cookie)
+{
+       struct rumprunner *rr = cookie;
+       void *retval;
+
+       pthread_join(rr->rr_mainthread, &retval);
+       assert(rumprun_done > 0);
+       rumprun_done--;
+
+       return (int)(intptr_t)retval;
+}
+
+void *
+rumprun_get_finished(void)
+{
+       struct rumprunner *rr;
+
+       if (LIST_EMPTY(&rumprunners))
+               return NULL;
+
+       pthread_mutex_lock(&w_mtx);
+       while (rumprun_done == 0) {
+               pthread_cond_wait(&w_cv, &w_mtx);
+       }
+       LIST_FOREACH(rr, &rumprunners, rr_entries) {
+               if (rr->rr_done)
+                       break;
+       }
+       pthread_mutex_unlock(&w_mtx);
+       assert(rr);
+
+       return rr;
+}
+
+void __dead
+rumprun_reboot(void)
+{
+
+       _rumprun_deconfig();
+       _netbsd_userlevel_fini();
+       rump_sys_reboot(0, 0);
+
+       bmk_platform_halt("reboot returned");
+}
index b7d5c5c2763a352b82f40fdb3f6e3aecb48a97b3..34f741e3372af8613fbdfa5dff27fddfadcd89e0 100644 (file)
@@ -38,6 +38,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <lwp.h>
+#include <pthread.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -45,8 +46,9 @@
 #include <unistd.h>
 
 #include <bmk-core/core.h>
+#include <bmk-core/sched.h>
 
-#include <rumprun-base/netbsd_initfini.h>
+#include <rump/rump.h>
 
 #ifdef RUMPRUN_MMAP_DEBUG
 #define MMAP_PRINTF(x) printf x
@@ -133,10 +135,9 @@ _exit(int eval)
        } else {
                printf("\n=== _exit(%d) called ===\n", eval);
        }
-       /* XXX: work around the console being slow to attach */
-       sleep(1);
 
-       _netbsd_fini();
+       rump_pub_lwproc_releaselwp();
+       pthread_exit((void *)(uintptr_t)eval);
 }
 
 /* XXX: manual proto.  plug into libc internals some other day */
index bd92c6f9538320ecfd5cb34ca5f01eb6c2553afc..0347e91d434ffcb2bb64ab7b2d5bbc4797fa9e07 100644 (file)
 
 #include <bmk/app.h>
 
-#include <rumprun-base/netbsd_initfini.h>
+#include <rumprun-base/rumprun.h>
 
 void
 bmk_mainthread(void *notused)
 {
         char *argv[] = {"bmk_main", 0};
-       int rv;
+       void *cookie;
 
-       _netbsd_init();
+       rumprun_boot();
 
-       printf("=== calling main() ===\n\n");
-        rv = main(1, argv);
-       printf("=== main() returned %d ===\n\n", rv);
+       cookie = rumprun(main, 1, argv);
+       rumprun_wait(cookie);
 
-       _netbsd_fini();
+       rumprun_reboot();
 }
index c89543ac5921935bc555b8bacc95920d9c95bbc8..352e5715729c92ed8235ef42a72f0f9d854e8ef7 100644 (file)
  * SUCH DAMAGE.
  */
 
-#include <stdint.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-
+#include <mini-os/types.h>
 #include <xen/xen.h>
 
-#include <mini-os/os.h>
+#include <bmk-core/memalloc.h>
 
-#include <rumprun-base/netbsd_initfini.h>
+#include <rumprun-base/rumprun.h>
 
 extern int main(int argc, char **argv);
 
@@ -83,27 +79,28 @@ static void parseargs(void *cmdline, int *nargs, char **outarray) {
        }
 }
 
-int __default_app_main(start_info_t *);
-int
+void __default_app_main(start_info_t *);
+void
 __default_app_main(start_info_t *si)
 {
        char argv0[] = "rumprun-xen";
-       int nargs, r;
+       int nargs;
        char **argv;
-
-        _netbsd_init();
+       void *cookie;
 
        parseargs(si->cmd_line, &nargs, 0);
-       argv = malloc(sizeof(*argv) * (nargs+3));
+       argv = bmk_xmalloc(sizeof(*argv) * (nargs+3));
        argv[0] = argv0;
        parseargs(si->cmd_line, &nargs, argv+1);
        argv[nargs+1] = 0;
        argv[nargs+2] = 0;
 
-       fprintf(stderr,"=== calling main() ===\n\n");
-       r = main(nargs+1, argv);
-       fprintf(stderr,"\n=== main() returned %d ===\n", r);
-       _exit(r);
+       rumprun_boot();
+
+       cookie = rumprun(main, nargs+1, argv);
+       rumprun_wait(cookie);
+
+       rumprun_reboot();
 }
 
 __weak_alias(app_main,__default_app_main)