Home | History | Annotate | Line # | Download | only in librumpuser
rumpuser_sp.c revision 1.26
      1  1.26  pooka /*      $NetBSD: rumpuser_sp.c,v 1.26 2010/12/16 12:38:20 pooka Exp $	*/
      2   1.1  pooka 
      3   1.1  pooka /*
      4   1.1  pooka  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
      5   1.1  pooka  *
      6   1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7   1.1  pooka  * modification, are permitted provided that the following conditions
      8   1.1  pooka  * are met:
      9   1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10   1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11   1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13   1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14   1.1  pooka  *
     15   1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16   1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17   1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18   1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19   1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20   1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21   1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22   1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23   1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24   1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25   1.1  pooka  * SUCH DAMAGE.
     26   1.1  pooka  */
     27   1.1  pooka 
     28   1.1  pooka /*
     29   1.1  pooka  * Sysproxy routines.  This provides system RPC support over host sockets.
     30   1.1  pooka  * The most notable limitation is that the client and server must share
     31   1.1  pooka  * the same ABI.  This does not mean that they have to be the same
     32   1.1  pooka  * machine or that they need to run the same version of the host OS,
     33   1.1  pooka  * just that they must agree on the data structures.  This even *might*
     34   1.1  pooka  * work correctly from one hardware architecture to another.
     35   1.1  pooka  */
     36   1.1  pooka 
     37   1.1  pooka #include <sys/cdefs.h>
     38  1.26  pooka __RCSID("$NetBSD: rumpuser_sp.c,v 1.26 2010/12/16 12:38:20 pooka Exp $");
     39   1.1  pooka 
     40   1.1  pooka #include <sys/types.h>
     41  1.11  pooka #include <sys/atomic.h>
     42   1.1  pooka #include <sys/mman.h>
     43   1.1  pooka #include <sys/socket.h>
     44   1.1  pooka 
     45   1.1  pooka #include <arpa/inet.h>
     46   1.1  pooka #include <netinet/in.h>
     47   1.1  pooka #include <netinet/tcp.h>
     48   1.1  pooka 
     49   1.1  pooka #include <assert.h>
     50   1.1  pooka #include <errno.h>
     51   1.1  pooka #include <fcntl.h>
     52   1.1  pooka #include <poll.h>
     53   1.1  pooka #include <pthread.h>
     54   1.1  pooka #include <stdarg.h>
     55   1.1  pooka #include <stdio.h>
     56   1.1  pooka #include <stdlib.h>
     57   1.1  pooka #include <string.h>
     58   1.1  pooka #include <unistd.h>
     59   1.1  pooka 
     60   1.1  pooka #include <rump/rumpuser.h>
     61  1.13  pooka #include "rumpuser_int.h"
     62   1.1  pooka 
     63   1.5  pooka #include "sp_common.c"
     64   1.1  pooka 
     65  1.20  pooka #ifndef MAXCLI
     66  1.18  pooka #define MAXCLI 256
     67  1.20  pooka #endif
     68  1.20  pooka #ifndef MAXWORKER
     69  1.20  pooka #define MAXWORKER 128
     70  1.20  pooka #endif
     71  1.20  pooka #ifndef IDLEWORKER
     72  1.20  pooka #define IDLEWORKER 16
     73  1.20  pooka #endif
     74  1.20  pooka int rumpsp_maxworker = MAXWORKER;
     75  1.20  pooka int rumpsp_idleworker = IDLEWORKER;
     76   1.1  pooka 
     77   1.1  pooka static struct pollfd pfdlist[MAXCLI];
     78   1.1  pooka static struct spclient spclist[MAXCLI];
     79  1.11  pooka static unsigned int disco;
     80  1.24  pooka static volatile int spfini;
     81   1.1  pooka 
     82   1.3  pooka static struct rumpuser_sp_ops spops;
     83   1.3  pooka 
     84  1.26  pooka static char banner[MAXBANNER];
     85  1.26  pooka 
     86  1.26  pooka #define PROTOMAJOR 0
     87  1.26  pooka #define PROTOMINOR 0
     88  1.26  pooka 
     89   1.3  pooka /*
     90   1.3  pooka  * Manual wrappers, since librump does not have access to the
     91   1.3  pooka  * user namespace wrapped interfaces.
     92   1.3  pooka  */
     93   1.3  pooka 
     94   1.3  pooka static void
     95   1.3  pooka lwproc_switch(struct lwp *l)
     96   1.3  pooka {
     97   1.3  pooka 
     98   1.4  pooka 	spops.spop_schedule();
     99   1.3  pooka 	spops.spop_lwproc_switch(l);
    100   1.4  pooka 	spops.spop_unschedule();
    101   1.3  pooka }
    102   1.3  pooka 
    103   1.3  pooka static void
    104   1.3  pooka lwproc_release(void)
    105   1.3  pooka {
    106   1.3  pooka 
    107   1.4  pooka 	spops.spop_schedule();
    108   1.3  pooka 	spops.spop_lwproc_release();
    109   1.4  pooka 	spops.spop_unschedule();
    110   1.3  pooka }
    111   1.3  pooka 
    112   1.3  pooka static int
    113  1.10  pooka lwproc_newproc(struct spclient *spc)
    114   1.3  pooka {
    115   1.3  pooka 	int rv;
    116   1.3  pooka 
    117   1.4  pooka 	spops.spop_schedule();
    118  1.10  pooka 	rv = spops.spop_lwproc_newproc(spc);
    119   1.4  pooka 	spops.spop_unschedule();
    120   1.3  pooka 
    121   1.3  pooka 	return rv;
    122   1.3  pooka }
    123   1.3  pooka 
    124   1.8  pooka static int
    125   1.8  pooka lwproc_newlwp(pid_t pid)
    126   1.8  pooka {
    127   1.8  pooka 	int rv;
    128   1.8  pooka 
    129   1.8  pooka 	spops.spop_schedule();
    130   1.8  pooka 	rv = spops.spop_lwproc_newlwp(pid);
    131   1.8  pooka 	spops.spop_unschedule();
    132   1.8  pooka 
    133   1.8  pooka 	return rv;
    134   1.8  pooka }
    135   1.8  pooka 
    136   1.3  pooka static struct lwp *
    137   1.3  pooka lwproc_curlwp(void)
    138   1.3  pooka {
    139   1.3  pooka 	struct lwp *l;
    140   1.3  pooka 
    141   1.4  pooka 	spops.spop_schedule();
    142   1.3  pooka 	l = spops.spop_lwproc_curlwp();
    143   1.4  pooka 	spops.spop_unschedule();
    144   1.3  pooka 
    145   1.3  pooka 	return l;
    146   1.3  pooka }
    147   1.3  pooka 
    148   1.8  pooka static pid_t
    149   1.8  pooka lwproc_getpid(void)
    150   1.8  pooka {
    151   1.8  pooka 	pid_t p;
    152   1.8  pooka 
    153   1.8  pooka 	spops.spop_schedule();
    154   1.8  pooka 	p = spops.spop_getpid();
    155   1.8  pooka 	spops.spop_unschedule();
    156   1.8  pooka 
    157   1.8  pooka 	return p;
    158   1.8  pooka }
    159   1.8  pooka 
    160   1.3  pooka static int
    161   1.3  pooka rumpsyscall(int sysnum, void *data, register_t *retval)
    162   1.3  pooka {
    163   1.3  pooka 	int rv;
    164   1.3  pooka 
    165   1.4  pooka 	spops.spop_schedule();
    166   1.3  pooka 	rv = spops.spop_syscall(sysnum, data, retval);
    167   1.4  pooka 	spops.spop_unschedule();
    168   1.3  pooka 
    169   1.3  pooka 	return rv;
    170   1.3  pooka }
    171   1.1  pooka 
    172   1.7  pooka static uint64_t
    173   1.7  pooka nextreq(struct spclient *spc)
    174   1.7  pooka {
    175   1.7  pooka 	uint64_t nw;
    176   1.7  pooka 
    177   1.7  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    178   1.7  pooka 	nw = spc->spc_nextreq++;
    179   1.7  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    180   1.7  pooka 
    181   1.7  pooka 	return nw;
    182   1.7  pooka }
    183   1.7  pooka 
    184  1.21  pooka static void
    185  1.21  pooka send_error_resp(struct spclient *spc, uint64_t reqno, int error)
    186  1.21  pooka {
    187  1.21  pooka 	struct rsp_hdr rhdr;
    188  1.21  pooka 
    189  1.21  pooka 	rhdr.rsp_len = sizeof(rhdr);
    190  1.21  pooka 	rhdr.rsp_reqno = reqno;
    191  1.21  pooka 	rhdr.rsp_class = RUMPSP_ERROR;
    192  1.21  pooka 	rhdr.rsp_type = 0;
    193  1.21  pooka 	rhdr.rsp_error = error;
    194  1.21  pooka 
    195  1.21  pooka 	sendlock(spc);
    196  1.21  pooka 	(void)dosend(spc, &rhdr, sizeof(rhdr));
    197  1.21  pooka 	sendunlock(spc);
    198  1.21  pooka }
    199  1.21  pooka 
    200   1.1  pooka static int
    201   1.1  pooka send_syscall_resp(struct spclient *spc, uint64_t reqno, int error,
    202   1.7  pooka 	register_t *retval)
    203   1.1  pooka {
    204   1.1  pooka 	struct rsp_hdr rhdr;
    205   1.1  pooka 	struct rsp_sysresp sysresp;
    206   1.7  pooka 	int rv;
    207   1.1  pooka 
    208   1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(sysresp);
    209   1.1  pooka 	rhdr.rsp_reqno = reqno;
    210   1.7  pooka 	rhdr.rsp_class = RUMPSP_RESP;
    211   1.7  pooka 	rhdr.rsp_type = RUMPSP_SYSCALL;
    212   1.1  pooka 	rhdr.rsp_sysnum = 0;
    213   1.1  pooka 
    214   1.1  pooka 	sysresp.rsys_error = error;
    215   1.7  pooka 	memcpy(sysresp.rsys_retval, retval, sizeof(sysresp.rsys_retval));
    216   1.1  pooka 
    217   1.7  pooka 	sendlock(spc);
    218   1.7  pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    219   1.7  pooka 	rv = dosend(spc, &sysresp, sizeof(sysresp));
    220   1.7  pooka 	sendunlock(spc);
    221   1.1  pooka 
    222   1.7  pooka 	return rv;
    223   1.1  pooka }
    224   1.1  pooka 
    225   1.1  pooka static int
    226  1.15  pooka copyin_req(struct spclient *spc, const void *remaddr, size_t *dlen,
    227  1.15  pooka 	int wantstr, void **resp)
    228   1.1  pooka {
    229   1.1  pooka 	struct rsp_hdr rhdr;
    230   1.1  pooka 	struct rsp_copydata copydata;
    231   1.7  pooka 	struct respwait rw;
    232   1.7  pooka 	int rv;
    233   1.7  pooka 
    234  1.15  pooka 	DPRINTF(("copyin_req: %zu bytes from %p\n", *dlen, remaddr));
    235   1.1  pooka 
    236   1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata);
    237   1.7  pooka 	rhdr.rsp_class = RUMPSP_REQ;
    238  1.15  pooka 	if (wantstr)
    239  1.15  pooka 		rhdr.rsp_type = RUMPSP_COPYINSTR;
    240  1.15  pooka 	else
    241  1.15  pooka 		rhdr.rsp_type = RUMPSP_COPYIN;
    242   1.1  pooka 	rhdr.rsp_sysnum = 0;
    243   1.1  pooka 
    244   1.1  pooka 	copydata.rcp_addr = __UNCONST(remaddr);
    245  1.15  pooka 	copydata.rcp_len = *dlen;
    246   1.1  pooka 
    247   1.7  pooka 	putwait(spc, &rw, &rhdr);
    248   1.7  pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    249   1.7  pooka 	rv = dosend(spc, &copydata, sizeof(copydata));
    250  1.13  pooka 	if (rv) {
    251  1.13  pooka 		unputwait(spc, &rw);
    252  1.13  pooka 		return rv;
    253  1.13  pooka 	}
    254   1.7  pooka 
    255   1.7  pooka 	rv = waitresp(spc, &rw);
    256   1.7  pooka 
    257   1.7  pooka 	DPRINTF(("copyin: response %d\n", rv));
    258   1.7  pooka 
    259   1.7  pooka 	*resp = rw.rw_data;
    260  1.15  pooka 	if (wantstr)
    261  1.15  pooka 		*dlen = rw.rw_dlen;
    262  1.15  pooka 
    263   1.7  pooka 	return rv;
    264   1.1  pooka 
    265   1.1  pooka }
    266   1.1  pooka 
    267   1.1  pooka static int
    268   1.1  pooka send_copyout_req(struct spclient *spc, const void *remaddr,
    269   1.1  pooka 	const void *data, size_t dlen)
    270   1.1  pooka {
    271   1.1  pooka 	struct rsp_hdr rhdr;
    272   1.1  pooka 	struct rsp_copydata copydata;
    273   1.7  pooka 	int rv;
    274   1.7  pooka 
    275   1.7  pooka 	DPRINTF(("copyout_req (async): %zu bytes to %p\n", dlen, remaddr));
    276   1.1  pooka 
    277   1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata) + dlen;
    278   1.7  pooka 	rhdr.rsp_reqno = nextreq(spc);
    279   1.7  pooka 	rhdr.rsp_class = RUMPSP_REQ;
    280   1.7  pooka 	rhdr.rsp_type = RUMPSP_COPYOUT;
    281   1.1  pooka 	rhdr.rsp_sysnum = 0;
    282   1.1  pooka 
    283   1.1  pooka 	copydata.rcp_addr = __UNCONST(remaddr);
    284   1.1  pooka 	copydata.rcp_len = dlen;
    285   1.1  pooka 
    286   1.7  pooka 	sendlock(spc);
    287   1.7  pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    288   1.7  pooka 	rv = dosend(spc, &copydata, sizeof(copydata));
    289   1.7  pooka 	rv = dosend(spc, data, dlen);
    290   1.7  pooka 	sendunlock(spc);
    291   1.1  pooka 
    292   1.7  pooka 	return rv;
    293   1.1  pooka }
    294   1.1  pooka 
    295   1.1  pooka static int
    296   1.7  pooka anonmmap_req(struct spclient *spc, size_t howmuch, void **resp)
    297   1.1  pooka {
    298   1.1  pooka 	struct rsp_hdr rhdr;
    299   1.7  pooka 	struct respwait rw;
    300   1.7  pooka 	int rv;
    301   1.7  pooka 
    302   1.7  pooka 	DPRINTF(("anonmmap_req: %zu bytes\n", howmuch));
    303   1.1  pooka 
    304   1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(howmuch);
    305   1.7  pooka 	rhdr.rsp_class = RUMPSP_REQ;
    306   1.7  pooka 	rhdr.rsp_type = RUMPSP_ANONMMAP;
    307   1.1  pooka 	rhdr.rsp_sysnum = 0;
    308   1.1  pooka 
    309   1.7  pooka 	putwait(spc, &rw, &rhdr);
    310   1.7  pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    311   1.7  pooka 	rv = dosend(spc, &howmuch, sizeof(howmuch));
    312  1.13  pooka 	if (rv) {
    313  1.13  pooka 		unputwait(spc, &rw);
    314  1.13  pooka 		return rv;
    315  1.13  pooka 	}
    316   1.1  pooka 
    317   1.7  pooka 	rv = waitresp(spc, &rw);
    318  1.13  pooka 
    319   1.7  pooka 	*resp = rw.rw_data;
    320   1.7  pooka 
    321   1.7  pooka 	DPRINTF(("anonmmap: mapped at %p\n", **(void ***)resp));
    322   1.7  pooka 
    323   1.7  pooka 	return rv;
    324   1.1  pooka }
    325   1.1  pooka 
    326   1.1  pooka static void
    327  1.11  pooka spcref(struct spclient *spc)
    328  1.11  pooka {
    329  1.11  pooka 
    330  1.11  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    331  1.11  pooka 	spc->spc_refcnt++;
    332  1.11  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    333  1.11  pooka }
    334  1.11  pooka 
    335  1.11  pooka static void
    336  1.11  pooka spcrelease(struct spclient *spc)
    337  1.11  pooka {
    338  1.11  pooka 	int ref;
    339  1.11  pooka 
    340  1.11  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    341  1.11  pooka 	ref = --spc->spc_refcnt;
    342  1.11  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    343  1.11  pooka 
    344  1.11  pooka 	if (ref > 0)
    345  1.11  pooka 		return;
    346  1.11  pooka 
    347  1.12  pooka 	DPRINTF(("spcrelease: spc %p fd %d\n", spc, spc->spc_fd));
    348  1.12  pooka 
    349  1.13  pooka 	_DIAGASSERT(TAILQ_EMPTY(&spc->spc_respwait));
    350  1.11  pooka 	_DIAGASSERT(spc->spc_buf == NULL);
    351  1.11  pooka 
    352  1.11  pooka 	lwproc_switch(spc->spc_mainlwp);
    353  1.11  pooka 	lwproc_release();
    354  1.11  pooka 	spc->spc_mainlwp = NULL;
    355  1.11  pooka 
    356  1.11  pooka 	close(spc->spc_fd);
    357  1.11  pooka 	spc->spc_fd = -1;
    358  1.12  pooka 	spc->spc_dying = 0;
    359  1.11  pooka 
    360  1.11  pooka 	atomic_inc_uint(&disco);
    361  1.11  pooka }
    362  1.11  pooka 
    363  1.11  pooka static void
    364   1.2  pooka serv_handledisco(unsigned int idx)
    365   1.1  pooka {
    366   1.1  pooka 	struct spclient *spc = &spclist[idx];
    367   1.1  pooka 
    368   1.1  pooka 	DPRINTF(("rump_sp: disconnecting [%u]\n", idx));
    369   1.1  pooka 
    370  1.12  pooka 	pfdlist[idx].fd = -1;
    371  1.12  pooka 	pfdlist[idx].revents = 0;
    372  1.12  pooka 	pthread_mutex_lock(&spc->spc_mtx);
    373  1.12  pooka 	spc->spc_dying = 1;
    374  1.12  pooka 	kickall(spc);
    375  1.12  pooka 	pthread_mutex_unlock(&spc->spc_mtx);
    376  1.17  pooka 
    377  1.17  pooka 	/*
    378  1.17  pooka 	 * Nobody's going to attempt to send/receive anymore,
    379  1.17  pooka 	 * so reinit info relevant to that.
    380  1.17  pooka 	 */
    381  1.22  pooka 	/*LINTED:pointer casts may be ok*/
    382  1.17  pooka 	memset((char *)spc + SPC_ZEROFF, 0, sizeof(*spc) - SPC_ZEROFF);
    383  1.17  pooka 
    384  1.11  pooka 	spcrelease(spc);
    385   1.1  pooka }
    386   1.1  pooka 
    387  1.24  pooka static void
    388  1.24  pooka serv_shutdown(void)
    389  1.24  pooka {
    390  1.24  pooka 	struct spclient *spc;
    391  1.24  pooka 	unsigned int i;
    392  1.24  pooka 
    393  1.24  pooka 	for (i = 1; i < MAXCLI; i++) {
    394  1.24  pooka 		spc = &spclist[i];
    395  1.24  pooka 		if (spc->spc_fd == -1)
    396  1.24  pooka 			continue;
    397  1.24  pooka 
    398  1.24  pooka 		shutdown(spc->spc_fd, SHUT_RDWR);
    399  1.24  pooka 		serv_handledisco(i);
    400  1.24  pooka 
    401  1.24  pooka 		spcrelease(spc);
    402  1.24  pooka 	}
    403  1.24  pooka }
    404  1.24  pooka 
    405  1.11  pooka static unsigned
    406  1.11  pooka serv_handleconn(int fd, connecthook_fn connhook, int busy)
    407   1.1  pooka {
    408   1.1  pooka 	struct sockaddr_storage ss;
    409   1.1  pooka 	socklen_t sl = sizeof(ss);
    410  1.11  pooka 	int newfd, flags;
    411   1.1  pooka 	unsigned i;
    412   1.1  pooka 
    413   1.1  pooka 	/*LINTED: cast ok */
    414   1.1  pooka 	newfd = accept(fd, (struct sockaddr *)&ss, &sl);
    415   1.1  pooka 	if (newfd == -1)
    416  1.11  pooka 		return 0;
    417   1.1  pooka 
    418  1.11  pooka 	if (busy) {
    419   1.1  pooka 		close(newfd); /* EBUSY */
    420  1.11  pooka 		return 0;
    421   1.1  pooka 	}
    422   1.1  pooka 
    423   1.1  pooka 	flags = fcntl(newfd, F_GETFL, 0);
    424   1.1  pooka 	if (fcntl(newfd, F_SETFL, flags | O_NONBLOCK) == -1) {
    425   1.1  pooka 		close(newfd);
    426  1.11  pooka 		return 0;
    427   1.1  pooka 	}
    428   1.1  pooka 
    429  1.11  pooka 	if (connhook(newfd) != 0) {
    430   1.1  pooka 		close(newfd);
    431  1.11  pooka 		return 0;
    432   1.1  pooka 	}
    433   1.1  pooka 
    434  1.26  pooka 	/* write out a banner for the client */
    435  1.26  pooka 	if (write(newfd, banner, strlen(banner)) != (ssize_t)strlen(banner)) {
    436  1.26  pooka 		close(newfd);
    437  1.26  pooka 		return 0;
    438  1.26  pooka 	}
    439  1.26  pooka 
    440   1.1  pooka 	/* find empty slot the simple way */
    441   1.1  pooka 	for (i = 0; i < MAXCLI; i++) {
    442  1.12  pooka 		if (pfdlist[i].fd == -1 && spclist[i].spc_dying == 0)
    443   1.2  pooka 			break;
    444   1.1  pooka 	}
    445   1.1  pooka 
    446  1.11  pooka 	if (lwproc_newproc(&spclist[i]) != 0) {
    447  1.10  pooka 		close(newfd);
    448  1.11  pooka 		return 0;
    449  1.10  pooka 	}
    450  1.10  pooka 
    451   1.1  pooka 	assert(i < MAXCLI);
    452   1.1  pooka 
    453   1.1  pooka 	pfdlist[i].fd = newfd;
    454   1.1  pooka 	spclist[i].spc_fd = newfd;
    455   1.8  pooka 	spclist[i].spc_mainlwp = lwproc_curlwp();
    456   1.7  pooka 	spclist[i].spc_istatus = SPCSTATUS_BUSY; /* dedicated receiver */
    457   1.8  pooka 	spclist[i].spc_pid = lwproc_getpid();
    458  1.11  pooka 	spclist[i].spc_refcnt = 1;
    459   1.7  pooka 
    460   1.7  pooka 	TAILQ_INIT(&spclist[i].spc_respwait);
    461   1.1  pooka 
    462  1.13  pooka 	DPRINTF(("rump_sp: added new connection fd %d at idx %u, pid %d\n",
    463  1.13  pooka 	    newfd, i, lwproc_getpid()));
    464   1.2  pooka 
    465   1.3  pooka 	lwproc_switch(NULL);
    466   1.1  pooka 
    467  1.11  pooka 	return i;
    468   1.1  pooka }
    469   1.1  pooka 
    470   1.1  pooka static void
    471   1.1  pooka serv_handlesyscall(struct spclient *spc, struct rsp_hdr *rhdr, uint8_t *data)
    472   1.1  pooka {
    473   1.7  pooka 	register_t retval[2] = {0, 0};
    474   1.1  pooka 	int rv, sysnum;
    475   1.1  pooka 
    476   1.1  pooka 	sysnum = (int)rhdr->rsp_sysnum;
    477   1.1  pooka 	DPRINTF(("rump_sp: handling syscall %d from client %d\n",
    478   1.1  pooka 	    sysnum, 0));
    479   1.1  pooka 
    480   1.8  pooka 	lwproc_newlwp(spc->spc_pid);
    481   1.3  pooka 	rv = rumpsyscall(sysnum, data, retval);
    482  1.16  pooka 	lwproc_release();
    483   1.1  pooka 
    484   1.7  pooka 	DPRINTF(("rump_sp: got return value %d & %d/%d\n",
    485   1.7  pooka 	    rv, retval[0], retval[1]));
    486   1.5  pooka 
    487   1.1  pooka 	send_syscall_resp(spc, rhdr->rsp_reqno, rv, retval);
    488   1.1  pooka }
    489   1.1  pooka 
    490   1.7  pooka struct sysbouncearg {
    491   1.7  pooka 	struct spclient *sba_spc;
    492   1.7  pooka 	struct rsp_hdr sba_hdr;
    493   1.7  pooka 	uint8_t *sba_data;
    494  1.20  pooka 
    495  1.20  pooka 	TAILQ_ENTRY(sysbouncearg) sba_entries;
    496   1.7  pooka };
    497  1.20  pooka static pthread_mutex_t sbamtx;
    498  1.20  pooka static pthread_cond_t sbacv;
    499  1.20  pooka static int nworker, idleworker;
    500  1.20  pooka static TAILQ_HEAD(, sysbouncearg) syslist = TAILQ_HEAD_INITIALIZER(syslist);
    501  1.20  pooka 
    502  1.20  pooka /*ARGSUSED*/
    503   1.7  pooka static void *
    504   1.7  pooka serv_syscallbouncer(void *arg)
    505   1.7  pooka {
    506  1.20  pooka 	struct sysbouncearg *sba;
    507  1.20  pooka 
    508  1.20  pooka 	for (;;) {
    509  1.20  pooka 		pthread_mutex_lock(&sbamtx);
    510  1.20  pooka 		if (idleworker >= rumpsp_idleworker) {
    511  1.20  pooka 			nworker--;
    512  1.20  pooka 			pthread_mutex_unlock(&sbamtx);
    513  1.20  pooka 			break;
    514  1.20  pooka 		}
    515  1.20  pooka 		idleworker++;
    516  1.20  pooka 		while (TAILQ_EMPTY(&syslist)) {
    517  1.20  pooka 			pthread_cond_wait(&sbacv, &sbamtx);
    518  1.20  pooka 		}
    519  1.20  pooka 
    520  1.20  pooka 		sba = TAILQ_FIRST(&syslist);
    521  1.20  pooka 		TAILQ_REMOVE(&syslist, sba, sba_entries);
    522  1.20  pooka 		idleworker--;
    523  1.20  pooka 		pthread_mutex_unlock(&sbamtx);
    524  1.20  pooka 
    525  1.20  pooka 		serv_handlesyscall(sba->sba_spc,
    526  1.20  pooka 		    &sba->sba_hdr, sba->sba_data);
    527  1.20  pooka 		spcrelease(sba->sba_spc);
    528  1.20  pooka 		free(sba->sba_data);
    529  1.20  pooka 		free(sba);
    530  1.20  pooka 	}
    531   1.7  pooka 
    532   1.7  pooka 	return NULL;
    533   1.7  pooka }
    534   1.7  pooka 
    535  1.15  pooka static int
    536  1.15  pooka sp_copyin(void *arg, const void *raddr, void *laddr, size_t *len, int wantstr)
    537   1.1  pooka {
    538  1.10  pooka 	struct spclient *spc = arg;
    539   1.9  pooka 	void *rdata = NULL; /* XXXuninit */
    540  1.13  pooka 	int rv, nlocks;
    541  1.13  pooka 
    542  1.13  pooka 	rumpuser__kunlock(0, &nlocks, NULL);
    543   1.1  pooka 
    544  1.15  pooka 	rv = copyin_req(spc, raddr, len, wantstr, &rdata);
    545  1.12  pooka 	if (rv)
    546  1.13  pooka 		goto out;
    547   1.1  pooka 
    548  1.15  pooka 	memcpy(laddr, rdata, *len);
    549   1.7  pooka 	free(rdata);
    550   1.1  pooka 
    551  1.13  pooka  out:
    552  1.13  pooka 	rumpuser__klock(nlocks, NULL);
    553  1.13  pooka 	if (rv)
    554  1.13  pooka 		return EFAULT;
    555   1.1  pooka 	return 0;
    556   1.1  pooka }
    557   1.1  pooka 
    558   1.1  pooka int
    559  1.15  pooka rumpuser_sp_copyin(void *arg, const void *raddr, void *laddr, size_t len)
    560  1.15  pooka {
    561  1.15  pooka 
    562  1.15  pooka 	return sp_copyin(arg, raddr, laddr, &len, 0);
    563  1.15  pooka }
    564  1.15  pooka 
    565  1.15  pooka int
    566  1.15  pooka rumpuser_sp_copyinstr(void *arg, const void *raddr, void *laddr, size_t *len)
    567  1.15  pooka {
    568  1.15  pooka 
    569  1.15  pooka 	return sp_copyin(arg, raddr, laddr, len, 1);
    570  1.15  pooka }
    571  1.15  pooka 
    572  1.15  pooka static int
    573  1.15  pooka sp_copyout(void *arg, const void *laddr, void *raddr, size_t dlen)
    574   1.1  pooka {
    575  1.10  pooka 	struct spclient *spc = arg;
    576  1.13  pooka 	int nlocks, rv;
    577  1.13  pooka 
    578  1.13  pooka 	rumpuser__kunlock(0, &nlocks, NULL);
    579  1.15  pooka 	rv = send_copyout_req(spc, raddr, laddr, dlen);
    580  1.13  pooka 	rumpuser__klock(nlocks, NULL);
    581   1.1  pooka 
    582  1.13  pooka 	if (rv)
    583   1.7  pooka 		return EFAULT;
    584   1.1  pooka 	return 0;
    585   1.1  pooka }
    586   1.1  pooka 
    587   1.1  pooka int
    588  1.15  pooka rumpuser_sp_copyout(void *arg, const void *laddr, void *raddr, size_t dlen)
    589  1.15  pooka {
    590  1.15  pooka 
    591  1.15  pooka 	return sp_copyout(arg, laddr, raddr, dlen);
    592  1.15  pooka }
    593  1.15  pooka 
    594  1.15  pooka int
    595  1.15  pooka rumpuser_sp_copyoutstr(void *arg, const void *laddr, void *raddr, size_t *dlen)
    596  1.15  pooka {
    597  1.15  pooka 
    598  1.15  pooka 	return sp_copyout(arg, laddr, raddr, *dlen);
    599  1.15  pooka }
    600  1.15  pooka 
    601  1.15  pooka int
    602  1.10  pooka rumpuser_sp_anonmmap(void *arg, size_t howmuch, void **addr)
    603   1.1  pooka {
    604  1.10  pooka 	struct spclient *spc = arg;
    605   1.7  pooka 	void *resp, *rdata;
    606  1.13  pooka 	int nlocks, rv;
    607  1.13  pooka 
    608  1.13  pooka 	rumpuser__kunlock(0, &nlocks, NULL);
    609   1.1  pooka 
    610   1.7  pooka 	rv = anonmmap_req(spc, howmuch, &rdata);
    611  1.13  pooka 	if (rv) {
    612  1.13  pooka 		rv = EFAULT;
    613  1.13  pooka 		goto out;
    614  1.13  pooka 	}
    615   1.1  pooka 
    616   1.7  pooka 	resp = *(void **)rdata;
    617   1.7  pooka 	free(rdata);
    618   1.1  pooka 
    619   1.7  pooka 	if (resp == NULL) {
    620  1.13  pooka 		rv = ENOMEM;
    621   1.1  pooka 	}
    622   1.1  pooka 
    623   1.1  pooka 	*addr = resp;
    624  1.13  pooka 
    625  1.13  pooka  out:
    626  1.13  pooka 	rumpuser__klock(nlocks, NULL);
    627  1.13  pooka 
    628  1.13  pooka 	if (rv)
    629  1.13  pooka 		return rv;
    630   1.1  pooka 	return 0;
    631   1.1  pooka }
    632   1.1  pooka 
    633   1.1  pooka /*
    634   1.1  pooka  *
    635   1.1  pooka  * Startup routines and mainloop for server.
    636   1.1  pooka  *
    637   1.1  pooka  */
    638   1.1  pooka 
    639   1.1  pooka struct spservarg {
    640   1.1  pooka 	int sps_sock;
    641   1.1  pooka 	connecthook_fn sps_connhook;
    642   1.1  pooka };
    643   1.1  pooka 
    644  1.14  pooka static pthread_attr_t pattr_detached;
    645   1.7  pooka static void
    646   1.7  pooka handlereq(struct spclient *spc)
    647   1.7  pooka {
    648   1.7  pooka 	struct sysbouncearg *sba;
    649   1.7  pooka 	pthread_t pt;
    650  1.21  pooka 	int retries;
    651   1.7  pooka 
    652  1.21  pooka 	if (__predict_false(spc->spc_hdr.rsp_type != RUMPSP_SYSCALL)) {
    653  1.21  pooka 		send_error_resp(spc, spc->spc_hdr.rsp_reqno, EINVAL);
    654  1.21  pooka 		spcfreebuf(spc);
    655  1.21  pooka 		return;
    656  1.21  pooka 	}
    657   1.7  pooka 
    658  1.21  pooka 	retries = 0;
    659  1.21  pooka 	while ((sba = malloc(sizeof(*sba))) == NULL) {
    660  1.21  pooka 		if (nworker == 0 || retries > 10) {
    661  1.21  pooka 			send_error_resp(spc, spc->spc_hdr.rsp_reqno, EAGAIN);
    662  1.21  pooka 			spcfreebuf(spc);
    663  1.21  pooka 			return;
    664  1.21  pooka 		}
    665  1.21  pooka 		/* slim chance of more memory? */
    666  1.21  pooka 		usleep(10000);
    667   1.7  pooka 	}
    668   1.7  pooka 
    669   1.7  pooka 	sba->sba_spc = spc;
    670   1.7  pooka 	sba->sba_hdr = spc->spc_hdr;
    671   1.7  pooka 	sba->sba_data = spc->spc_buf;
    672  1.21  pooka 	spcresetbuf(spc);
    673   1.7  pooka 
    674  1.11  pooka 	spcref(spc);
    675  1.20  pooka 
    676  1.20  pooka 	pthread_mutex_lock(&sbamtx);
    677  1.20  pooka 	TAILQ_INSERT_TAIL(&syslist, sba, sba_entries);
    678  1.20  pooka 	if (idleworker > 0) {
    679  1.20  pooka 		/* do we have a daemon's tool (i.e. idle threads)? */
    680  1.20  pooka 		pthread_cond_signal(&sbacv);
    681  1.20  pooka 	} else if (nworker < rumpsp_maxworker) {
    682  1.20  pooka 		/*
    683  1.20  pooka 		 * Else, need to create one
    684  1.20  pooka 		 * (if we can, otherwise just expect another
    685  1.20  pooka 		 * worker to pick up the syscall)
    686  1.20  pooka 		 */
    687  1.20  pooka 		if (pthread_create(&pt, &pattr_detached,
    688  1.20  pooka 		    serv_syscallbouncer, NULL) == 0)
    689  1.20  pooka 			nworker++;
    690   1.7  pooka 	}
    691  1.20  pooka 	pthread_mutex_unlock(&sbamtx);
    692   1.7  pooka }
    693   1.7  pooka 
    694   1.1  pooka static void *
    695   1.1  pooka spserver(void *arg)
    696   1.1  pooka {
    697   1.1  pooka 	struct spservarg *sarg = arg;
    698  1.11  pooka 	struct spclient *spc;
    699   1.1  pooka 	unsigned idx;
    700   1.1  pooka 	int seen;
    701   1.1  pooka 	int rv;
    702  1.11  pooka 	unsigned int nfds, maxidx;
    703   1.1  pooka 
    704  1.11  pooka 	for (idx = 0; idx < MAXCLI; idx++) {
    705   1.1  pooka 		pfdlist[idx].fd = -1;
    706   1.1  pooka 		pfdlist[idx].events = POLLIN;
    707  1.11  pooka 
    708  1.11  pooka 		spc = &spclist[idx];
    709  1.11  pooka 		pthread_mutex_init(&spc->spc_mtx, NULL);
    710  1.11  pooka 		pthread_cond_init(&spc->spc_cv, NULL);
    711  1.24  pooka 		spc->spc_fd = -1;
    712   1.1  pooka 	}
    713  1.24  pooka 	pfdlist[0].fd = spclist[0].spc_fd = sarg->sps_sock;
    714   1.1  pooka 	pfdlist[0].events = POLLIN;
    715   1.1  pooka 	nfds = 1;
    716   1.1  pooka 	maxidx = 0;
    717   1.1  pooka 
    718  1.14  pooka 	pthread_attr_init(&pattr_detached);
    719  1.14  pooka 	pthread_attr_setdetachstate(&pattr_detached, PTHREAD_CREATE_DETACHED);
    720  1.19  pooka 	/* XXX: doesn't stacksize currently work on NetBSD */
    721  1.19  pooka 	pthread_attr_setstacksize(&pattr_detached, 32*1024);
    722  1.14  pooka 
    723  1.20  pooka 	pthread_mutex_init(&sbamtx, NULL);
    724  1.20  pooka 	pthread_cond_init(&sbacv, NULL);
    725  1.20  pooka 
    726   1.1  pooka 	DPRINTF(("rump_sp: server mainloop\n"));
    727   1.1  pooka 
    728   1.1  pooka 	for (;;) {
    729  1.18  pooka 		int discoed;
    730  1.18  pooka 
    731  1.11  pooka 		/* g/c hangarounds (eventually) */
    732  1.18  pooka 		discoed = atomic_swap_uint(&disco, 0);
    733  1.18  pooka 		while (discoed--) {
    734  1.18  pooka 			nfds--;
    735  1.18  pooka 			idx = maxidx;
    736  1.18  pooka 			while (idx) {
    737  1.18  pooka 				if (pfdlist[idx].fd != -1) {
    738  1.18  pooka 					maxidx = idx;
    739  1.18  pooka 					break;
    740  1.11  pooka 				}
    741  1.18  pooka 				idx--;
    742  1.11  pooka 			}
    743  1.18  pooka 			DPRINTF(("rump_sp: set maxidx to [%u]\n",
    744  1.18  pooka 			    maxidx));
    745  1.11  pooka 		}
    746  1.11  pooka 
    747   1.1  pooka 		DPRINTF(("rump_sp: loop nfd %d\n", maxidx+1));
    748   1.1  pooka 		seen = 0;
    749   1.1  pooka 		rv = poll(pfdlist, maxidx+1, INFTIM);
    750   1.1  pooka 		assert(maxidx+1 <= MAXCLI);
    751   1.1  pooka 		assert(rv != 0);
    752   1.1  pooka 		if (rv == -1) {
    753   1.1  pooka 			if (errno == EINTR)
    754   1.1  pooka 				continue;
    755   1.1  pooka 			fprintf(stderr, "rump_spserver: poll returned %d\n",
    756   1.1  pooka 			    errno);
    757   1.1  pooka 			break;
    758   1.1  pooka 		}
    759   1.1  pooka 
    760  1.12  pooka 		for (idx = 0; seen < rv && idx < MAXCLI; idx++) {
    761   1.1  pooka 			if ((pfdlist[idx].revents & POLLIN) == 0)
    762   1.1  pooka 				continue;
    763   1.1  pooka 
    764   1.1  pooka 			seen++;
    765   1.1  pooka 			DPRINTF(("rump_sp: activity at [%u] %d/%d\n",
    766   1.1  pooka 			    idx, seen, rv));
    767   1.1  pooka 			if (idx > 0) {
    768  1.11  pooka 				spc = &spclist[idx];
    769   1.1  pooka 				DPRINTF(("rump_sp: mainloop read [%u]\n", idx));
    770   1.1  pooka 				switch (readframe(spc)) {
    771   1.1  pooka 				case 0:
    772   1.1  pooka 					break;
    773   1.1  pooka 				case -1:
    774   1.2  pooka 					serv_handledisco(idx);
    775   1.1  pooka 					break;
    776   1.1  pooka 				default:
    777   1.7  pooka 					switch (spc->spc_hdr.rsp_class) {
    778   1.7  pooka 					case RUMPSP_RESP:
    779   1.7  pooka 						kickwaiter(spc);
    780   1.7  pooka 						break;
    781   1.7  pooka 					case RUMPSP_REQ:
    782   1.7  pooka 						handlereq(spc);
    783   1.7  pooka 						break;
    784   1.7  pooka 					default:
    785  1.21  pooka 						send_error_resp(spc,
    786  1.21  pooka 						    spc->spc_hdr.rsp_reqno,
    787  1.21  pooka 						    ENOENT);
    788  1.21  pooka 						spcfreebuf(spc);
    789   1.7  pooka 						break;
    790   1.7  pooka 					}
    791   1.1  pooka 					break;
    792   1.1  pooka 				}
    793  1.11  pooka 
    794   1.1  pooka 			} else {
    795   1.1  pooka 				DPRINTF(("rump_sp: mainloop new connection\n"));
    796  1.11  pooka 
    797  1.24  pooka 				if (__predict_false(spfini)) {
    798  1.24  pooka 					close(spclist[0].spc_fd);
    799  1.24  pooka 					serv_shutdown();
    800  1.24  pooka 					goto out;
    801  1.24  pooka 				}
    802  1.24  pooka 
    803  1.11  pooka 				idx = serv_handleconn(pfdlist[0].fd,
    804  1.11  pooka 				    sarg->sps_connhook, nfds == MAXCLI);
    805  1.11  pooka 				if (idx)
    806  1.11  pooka 					nfds++;
    807  1.11  pooka 				if (idx > maxidx)
    808  1.11  pooka 					maxidx = idx;
    809  1.12  pooka 				DPRINTF(("rump_sp: maxid now %d\n", maxidx));
    810   1.1  pooka 			}
    811   1.1  pooka 		}
    812   1.1  pooka 	}
    813   1.1  pooka 
    814  1.24  pooka  out:
    815   1.1  pooka 	return NULL;
    816   1.1  pooka }
    817   1.1  pooka 
    818  1.25  pooka static unsigned cleanupidx;
    819  1.25  pooka static struct sockaddr *cleanupsa;
    820   1.1  pooka int
    821  1.26  pooka rumpuser_sp_init(const char *url, const struct rumpuser_sp_ops *spopsp,
    822  1.26  pooka 	const char *ostype, const char *osrelease, const char *machine)
    823   1.1  pooka {
    824   1.5  pooka 	pthread_t pt;
    825   1.1  pooka 	struct spservarg *sarg;
    826   1.1  pooka 	struct sockaddr *sap;
    827   1.5  pooka 	char *p;
    828   1.5  pooka 	unsigned idx;
    829   1.5  pooka 	int error, s;
    830   1.5  pooka 
    831   1.5  pooka 	p = strdup(url);
    832   1.5  pooka 	if (p == NULL)
    833   1.5  pooka 		return ENOMEM;
    834   1.5  pooka 	error = parseurl(p, &sap, &idx, 1);
    835   1.5  pooka 	free(p);
    836   1.5  pooka 	if (error)
    837   1.5  pooka 		return error;
    838   1.5  pooka 
    839  1.26  pooka 	snprintf(banner, sizeof(banner), "RUMPSP-%d.%d-%s-%s/%s\n",
    840  1.26  pooka 	    PROTOMAJOR, PROTOMINOR, ostype, osrelease, machine);
    841  1.26  pooka 
    842   1.5  pooka 	s = socket(parsetab[idx].domain, SOCK_STREAM, 0);
    843   1.5  pooka 	if (s == -1)
    844   1.5  pooka 		return errno;
    845   1.1  pooka 
    846   1.3  pooka 	spops = *spopsp;
    847   1.5  pooka 	sarg = malloc(sizeof(*sarg));
    848   1.5  pooka 	if (sarg == NULL) {
    849   1.5  pooka 		close(s);
    850   1.5  pooka 		return ENOMEM;
    851   1.5  pooka 	}
    852   1.5  pooka 
    853   1.5  pooka 	sarg->sps_sock = s;
    854   1.5  pooka 	sarg->sps_connhook = parsetab[idx].connhook;
    855   1.1  pooka 
    856  1.25  pooka 	cleanupidx = idx;
    857  1.25  pooka 	cleanupsa = sap;
    858  1.25  pooka 
    859   1.5  pooka 	/* sloppy error recovery */
    860   1.1  pooka 
    861   1.5  pooka 	/*LINTED*/
    862   1.5  pooka 	if (bind(s, sap, sap->sa_len) == -1) {
    863   1.5  pooka 		fprintf(stderr, "rump_sp: server bind failed\n");
    864   1.5  pooka 		return errno;
    865   1.1  pooka 	}
    866  1.25  pooka 
    867  1.18  pooka 	if (listen(s, MAXCLI) == -1) {
    868   1.5  pooka 		fprintf(stderr, "rump_sp: server listen failed\n");
    869   1.5  pooka 		return errno;
    870   1.1  pooka 	}
    871   1.1  pooka 
    872   1.5  pooka 	if ((error = pthread_create(&pt, NULL, spserver, sarg)) != 0) {
    873   1.5  pooka 		fprintf(stderr, "rump_sp: cannot create wrkr thread\n");
    874   1.1  pooka 		return errno;
    875   1.1  pooka 	}
    876   1.5  pooka 	pthread_detach(pt);
    877   1.1  pooka 
    878   1.1  pooka 	return 0;
    879   1.1  pooka }
    880  1.24  pooka 
    881  1.24  pooka void
    882  1.24  pooka rumpuser_sp_fini()
    883  1.24  pooka {
    884  1.24  pooka 
    885  1.24  pooka 	if (spclist[0].spc_fd) {
    886  1.25  pooka 		parsetab[cleanupidx].cleanup(cleanupsa);
    887  1.24  pooka 		shutdown(spclist[0].spc_fd, SHUT_RDWR);
    888  1.24  pooka 		spfini = 1;
    889  1.24  pooka 	}
    890  1.24  pooka }
    891