2 * Copyright (c) 2006-2007 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * This module implements the core syslink() system call and provides
36 * glue for kernel syslink frontends and backends, creating a intra-host
37 * communications infrastructure and DMA transport abstraction.
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/endian.h>
44 #include <sys/malloc.h>
45 #include <sys/alist.h>
51 #include <sys/objcache.h>
52 #include <sys/queue.h>
53 #include <sys/thread.h>
55 #include <sys/sysctl.h>
56 #include <sys/sysproto.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
60 #include <sys/socketops.h>
61 #include <sys/sysref.h>
62 #include <sys/syslink.h>
63 #include <sys/syslink_msg.h>
64 #include <netinet/in.h>
66 #include <sys/thread2.h>
67 #include <sys/spinlock2.h>
69 #include <sys/mplock2.h>
72 * Syslink Connection abstraction
81 struct slmsg_rb_tree reply_rb_root; /* replies to requests */
83 struct sldesc *peer; /* peer syslink, if any */
84 struct file *xfp; /* external file pointer */
85 struct slcommon *common;
87 int rwaiters; /* number of threads waiting */
88 int wblocked; /* blocked waiting for us to drain */
89 size_t cmdbytes; /* unreplied commands pending */
90 size_t repbytes; /* undrained replies pending */
91 int (*backend_wblocked)(struct sldesc *, int, sl_proto_t);
92 int (*backend_write)(struct sldesc *, struct slmsg *);
93 void (*backend_reply)(struct sldesc *,struct slmsg *,struct slmsg *);
94 void (*backend_dispose)(struct sldesc *, struct slmsg *);
97 #define SLF_RSHUTDOWN 0x0001
98 #define SLF_WSHUTDOWN 0x0002
100 static int syslink_cmd_new(struct syslink_info_new *info, int *result);
101 static struct sldesc *allocsldesc(struct slcommon *common);
102 static void setsldescfp(struct sldesc *sl, struct file *fp);
103 static void shutdownsldesc(struct sldesc *sl, int how);
104 static void shutdownsldesc2(struct sldesc *sl, int how);
105 static void sldrop(struct sldesc *sl);
106 static int syslink_validate_msg(struct syslink_msg *msg, int bytes);
107 static int syslink_validate_elm(struct syslink_elm *elm, sl_reclen_t bytes,
108 int swapit, int depth);
110 static int sl_local_mmap(struct slmsg *slmsg, char *base, size_t len);
111 static void sl_local_munmap(struct slmsg *slmsg);
113 static int backend_wblocked_user(struct sldesc *sl, int nbio, sl_proto_t proto);
114 static int backend_write_user(struct sldesc *sl, struct slmsg *slmsg);
115 static void backend_reply_user(struct sldesc *sl, struct slmsg *slcmd,
116 struct slmsg *slrep);
117 static void backend_dispose_user(struct sldesc *sl, struct slmsg *slmsg);
119 static int backend_wblocked_kern(struct sldesc *sl, int nbio, sl_proto_t proto);
120 static int backend_write_kern(struct sldesc *sl, struct slmsg *slmsg);
121 static void backend_reply_kern(struct sldesc *sl, struct slmsg *slcmd,
122 struct slmsg *slrep);
123 static void backend_dispose_kern(struct sldesc *sl, struct slmsg *slmsg);
124 static void slmsg_put(struct slmsg *slmsg);
127 * Objcache memory backend
129 * All three object caches return slmsg structures but each is optimized
130 * for syslink message buffers of varying sizes. We use the slightly
131 * more complex ctor/dtor API in order to provide ready-to-go slmsg's.
134 static struct objcache *sl_objcache_big;
135 static struct objcache *sl_objcache_small;
136 static struct objcache *sl_objcache_none;
138 MALLOC_DEFINE(M_SYSLINK, "syslink", "syslink manager");
140 static boolean_t slmsg_ctor(void *data, void *private, int ocflags);
141 static void slmsg_dtor(void *data, void *private);
145 syslinkinit(void *dummy __unused)
147 size_t n = sizeof(struct slmsg);
149 sl_objcache_none = objcache_create_mbacked(M_SYSLINK, n, 0, 64,
150 slmsg_ctor, slmsg_dtor,
152 sl_objcache_small= objcache_create_mbacked(M_SYSLINK, n, 0, 64,
153 slmsg_ctor, slmsg_dtor,
155 sl_objcache_big = objcache_create_mbacked(M_SYSLINK, n, 0, 16,
156 slmsg_ctor, slmsg_dtor,
162 slmsg_ctor(void *data, void *private, int ocflags)
164 struct slmsg *slmsg = data;
166 bzero(slmsg, sizeof(*slmsg));
168 slmsg->oc = *(struct objcache **)private;
169 if (slmsg->oc == sl_objcache_none) {
171 } else if (slmsg->oc == sl_objcache_small) {
172 slmsg->maxsize = SLMSG_SMALL;
173 } else if (slmsg->oc == sl_objcache_big) {
174 slmsg->maxsize = SLMSG_BIG;
176 panic("slmsg_ctor: bad objcache?");
178 if (slmsg->maxsize) {
179 slmsg->msg = kmalloc(slmsg->maxsize,
180 M_SYSLINK, M_WAITOK|M_ZERO);
182 xio_init(&slmsg->xio);
188 slmsg_dtor(void *data, void *private)
190 struct slmsg *slmsg = data;
192 if (slmsg->maxsize && slmsg->msg) {
193 kfree(slmsg->msg, M_SYSLINK);
199 SYSINIT(syslink, SI_BOOT2_MACHDEP, SI_ORDER_ANY, syslinkinit, NULL)
201 static int rb_slmsg_compare(struct slmsg *msg1, struct slmsg *msg2);
202 RB_GENERATE2(slmsg_rb_tree, slmsg, rbnode, rb_slmsg_compare,
203 sysid_t, msg->sm_msgid);
208 static int syslink_enabled;
209 SYSCTL_NODE(_kern, OID_AUTO, syslink, CTLFLAG_RW, 0, "Pipe operation");
210 SYSCTL_INT(_kern_syslink, OID_AUTO, enabled,
211 CTLFLAG_RW, &syslink_enabled, 0, "Enable SYSLINK");
212 static size_t syslink_bufsize = 65536;
213 SYSCTL_UINT(_kern_syslink, OID_AUTO, bufsize,
214 CTLFLAG_RW, &syslink_bufsize, 0, "Maximum buffer size");
217 * Fileops API - typically used to glue a userland frontend with a
221 static int slfileop_read(struct file *fp, struct uio *uio,
222 struct ucred *cred, int flags);
223 static int slfileop_write(struct file *fp, struct uio *uio,
224 struct ucred *cred, int flags);
225 static int slfileop_close(struct file *fp);
226 static int slfileop_stat(struct file *fp, struct stat *sb, struct ucred *cred);
227 static int slfileop_shutdown(struct file *fp, int how);
228 static int slfileop_ioctl(struct file *fp, u_long cmd, caddr_t data,
229 struct ucred *cred, struct sysmsg *msg);
230 static int slfileop_kqfilter(struct file *fp, struct knote *kn);
232 static struct fileops syslinkops = {
233 .fo_read = slfileop_read,
234 .fo_write = slfileop_write,
235 .fo_ioctl = slfileop_ioctl,
236 .fo_kqfilter = slfileop_kqfilter,
237 .fo_stat = slfileop_stat,
238 .fo_close = slfileop_close,
239 .fo_shutdown = slfileop_shutdown
242 /************************************************************************
243 * PRIMARY SYSTEM CALL INTERFACE *
244 ************************************************************************
246 * syslink(int cmd, struct syslink_info *info, size_t bytes)
251 sys_syslink(struct syslink_args *uap)
253 union syslink_info_all info;
257 * System call is under construction and disabled by default.
258 * Superuser access is also required for now, but eventually
259 * will not be needed.
261 if (syslink_enabled == 0)
263 error = priv_check(curthread, PRIV_ROOT);
268 * Load and validate the info structure. Unloaded bytes are zerod
269 * out. The label field must always be 0-filled, even if not used
272 bzero(&info, sizeof(info));
273 if ((unsigned)uap->bytes <= sizeof(info)) {
275 error = copyin(uap->info, &info, uap->bytes);
284 * Process the command
287 case SYSLINK_CMD_NEW:
288 error = syslink_cmd_new(&info.cmd_new, &uap->sysmsg_result);
296 if (error == 0 && info.head.wbflag)
297 copyout(&info, uap->info, uap->bytes);
302 * Create a linked pair of descriptors, like a pipe.
306 syslink_cmd_new(struct syslink_info_new *info, int *result)
308 struct thread *td = curthread;
309 struct filedesc *fdp = td->td_proc->p_fd;
313 struct sldesc *slpeer;
317 error = falloc(td->td_lwp, &fp1, &fd1);
320 error = falloc(td->td_lwp, &fp2, &fd2);
322 fsetfd(fdp, NULL, fd1);
326 slpeer = allocsldesc(NULL);
327 slpeer->backend_wblocked = backend_wblocked_user;
328 slpeer->backend_write = backend_write_user;
329 slpeer->backend_reply = backend_reply_user;
330 slpeer->backend_dispose = backend_dispose_user;
331 sl = allocsldesc(slpeer->common);
333 sl->backend_wblocked = backend_wblocked_user;
334 sl->backend_write = backend_write_user;
335 sl->backend_reply = backend_reply_user;
336 sl->backend_dispose = backend_dispose_user;
339 setsldescfp(sl, fp1);
340 setsldescfp(slpeer, fp2);
342 fsetfd(fdp, fp1, fd1);
344 fsetfd(fdp, fp2, fd2);
347 info->head.wbflag = 1; /* write back */
354 /************************************************************************
355 * LOW LEVEL SLDESC SUPPORT *
356 ************************************************************************
362 allocsldesc(struct slcommon *common)
366 sl = kmalloc(sizeof(struct sldesc), M_SYSLINK, M_WAITOK|M_ZERO);
368 common = kmalloc(sizeof(*common), M_SYSLINK, M_WAITOK|M_ZERO);
369 TAILQ_INIT(&sl->inq); /* incoming requests */
370 RB_INIT(&sl->reply_rb_root); /* match incoming replies */
371 spin_init(&sl->spin);
379 setsldescfp(struct sldesc *sl, struct file *fp)
382 fp->f_type = DTYPE_SYSLINK;
383 fp->f_flag = FREAD | FWRITE;
384 fp->f_ops = &syslinkops;
389 * Red-black tree compare function
393 rb_slmsg_compare(struct slmsg *msg1, struct slmsg *msg2)
395 if (msg1->msg->sm_msgid < msg2->msg->sm_msgid)
397 if (msg1->msg->sm_msgid == msg2->msg->sm_msgid)
404 shutdownsldesc(struct sldesc *sl, int how)
409 shutdownsldesc2(sl, how);
412 * Return unread and unreplied messages
414 spin_lock(&sl->spin);
415 while ((slmsg = TAILQ_FIRST(&sl->inq)) != NULL) {
416 TAILQ_REMOVE(&sl->inq, slmsg, tqnode);
417 spin_unlock(&sl->spin);
418 if (slmsg->msg->sm_proto & SM_PROTO_REPLY) {
419 sl->repbytes -= slmsg->maxsize;
420 slmsg->flags &= ~SLMSGF_ONINQ;
421 sl->peer->backend_dispose(sl->peer, slmsg);
423 /* leave ONINQ set for commands, it will cleared below */
424 spin_lock(&sl->spin);
426 while ((slmsg = RB_ROOT(&sl->reply_rb_root)) != NULL) {
427 RB_REMOVE(slmsg_rb_tree, &sl->reply_rb_root, slmsg);
428 sl->cmdbytes -= slmsg->maxsize;
429 spin_unlock(&sl->spin);
430 slmsg->flags &= ~SLMSGF_ONINQ;
431 sl->peer->backend_reply(sl->peer, slmsg, NULL);
432 spin_lock(&sl->spin);
434 spin_unlock(&sl->spin);
437 * Call shutdown on the peer with the opposite flags
451 shutdownsldesc2(sl->peer, rhow);
456 shutdownsldesc2(struct sldesc *sl, int how)
458 spin_lock(&sl->spin);
461 sl->flags |= SLF_RSHUTDOWN;
464 sl->flags |= SLF_WSHUTDOWN;
467 sl->flags |= SLF_RSHUTDOWN | SLF_WSHUTDOWN;
470 spin_unlock(&sl->spin);
473 * Handle signaling on the user side
477 wakeup(&sl->rwaiters);
481 sl->wblocked = 0; /* race ok */
482 wakeup(&sl->wblocked);
489 sldrop(struct sldesc *sl)
491 struct sldesc *slpeer;
493 spin_lock(&sl->common->spin);
494 if (--sl->common->refs == 0) {
495 spin_unlock(&sl->common->spin);
496 if ((slpeer = sl->peer) != NULL) {
499 slpeer->common = NULL;
500 KKASSERT(slpeer->xfp == NULL);
501 KKASSERT(TAILQ_EMPTY(&slpeer->inq));
502 KKASSERT(RB_EMPTY(&slpeer->reply_rb_root));
503 kfree(slpeer, M_SYSLINK);
505 KKASSERT(sl->xfp == NULL);
506 KKASSERT(TAILQ_EMPTY(&sl->inq));
507 KKASSERT(RB_EMPTY(&sl->reply_rb_root));
508 kfree(sl->common, M_SYSLINK);
510 kfree(sl, M_SYSLINK);
512 spin_unlock(&sl->common->spin);
518 slmsg_put(struct slmsg *slmsg)
520 if (slmsg->flags & SLMSGF_HASXIO) {
521 slmsg->flags &= ~SLMSGF_HASXIO;
523 xio_release(&slmsg->xio);
526 slmsg->flags &= ~SLMSGF_LINMAP;
527 objcache_put(slmsg->oc, slmsg);
530 /************************************************************************
532 ************************************************************************
534 * Implement userland fileops.
540 slfileop_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
542 struct sldesc *sl = fp->f_data; /* fp refed on call */
546 struct syslink_msg *wmsg;
551 * Kinda messy. Figure out the non-blocking state
553 if (flags & O_FBLOCKING)
555 else if (flags & O_FNONBLOCKING)
557 else if (fp->f_flag & O_NONBLOCK)
565 * iov0 - message buffer
566 * iov1 - DMA buffer or backup buffer
568 if (uio->uio_iovcnt < 1) {
572 iov0 = &uio->uio_iov[0];
573 if (uio->uio_iovcnt > 2) {
579 * Get a message, blocking if necessary.
581 spin_lock(&sl->spin);
582 while ((slmsg = TAILQ_FIRST(&sl->inq)) == NULL) {
583 if (sl->flags & SLF_RSHUTDOWN) {
592 error = ssleep(&sl->rwaiters, &sl->spin, PCATCH, "slrmsg", 0);
600 * We have a message and still hold the spinlock. Make sure the
601 * uio has enough room to hold the message.
603 * Note that replies do not have XIOs.
605 if (slmsg->msgsize > iov0->iov_len) {
609 if (slmsg->xio.xio_bytes) {
610 if (uio->uio_iovcnt != 2) {
614 iov1 = &uio->uio_iov[1];
615 if (slmsg->xio.xio_bytes > iov1->iov_len) {
624 * Dequeue the message. Adjust repbytes immediately. cmdbytes
625 * are adjusted when the command is replied to, not here.
627 TAILQ_REMOVE(&sl->inq, slmsg, tqnode);
628 if (slmsg->msg->sm_proto & SM_PROTO_REPLY)
629 sl->repbytes -= slmsg->maxsize;
630 spin_unlock(&sl->spin);
633 * Load the message data into the user buffer.
635 * If receiving a command an XIO may exist specifying a DMA buffer.
636 * For commands, if DMAW is set we have to copy or map the buffer
637 * so the caller can access the data being written. If DMAR is set
638 * we do not have to copy but we still must map the buffer so the
639 * caller can directly fill in the data being requested.
641 error = uiomove((void *)slmsg->msg, slmsg->msgsize, uio);
642 if (error == 0 && slmsg->xio.xio_bytes &&
643 (wmsg->sm_head.se_cmd & SE_CMDF_REPLY) == 0) {
644 if (wmsg->sm_head.se_cmd & SE_CMDF_DMAW) {
646 * Data being passed to caller or being passed in both
647 * directions, copy or map.
650 if ((flags & O_MAPONREAD) &&
651 (slmsg->xio.xio_flags & XIOF_VMLINEAR)) {
652 error = sl_local_mmap(slmsg,
656 error = xio_copy_xtou(&slmsg->xio, 0,
658 slmsg->xio.xio_bytes);
660 error = xio_copy_xtou(&slmsg->xio, 0,
662 slmsg->xio.xio_bytes);
665 } else if (wmsg->sm_head.se_cmd & SE_CMDF_DMAR) {
667 * Data will be passed back to originator, map
668 * the buffer if we can, else use the backup
669 * buffer at the same VA supplied by the caller.
672 if ((flags & O_MAPONREAD) &&
673 (slmsg->xio.xio_flags & XIOF_VMLINEAR)) {
674 error = sl_local_mmap(slmsg,
677 error = 0; /* ignore errors */
688 * Requeue the message if we could not read it successfully
690 spin_lock(&sl->spin);
691 TAILQ_INSERT_HEAD(&sl->inq, slmsg, tqnode);
692 slmsg->flags |= SLMSGF_ONINQ;
693 spin_unlock(&sl->spin);
694 } else if (slmsg->msg->sm_proto & SM_PROTO_REPLY) {
696 * Dispose of any received reply after we've copied it
697 * to userland. We don't need the slmsg any more.
699 slmsg->flags &= ~SLMSGF_ONINQ;
700 sl->peer->backend_dispose(sl->peer, slmsg);
701 if (sl->wblocked && sl->repbytes < syslink_bufsize) {
702 sl->wblocked = 0; /* MP race ok here */
703 wakeup(&sl->wblocked);
707 * Leave the command in the RB tree but clear ONINQ now
708 * that we have returned it to userland so userland can
711 slmsg->flags &= ~SLMSGF_ONINQ;
715 spin_unlock(&sl->spin);
721 * Userland writes syslink message (optionally with DMA buffer in iov[1]).
725 slfileop_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
727 struct sldesc *sl = fp->f_data;
730 struct syslink_msg sltmp;
731 struct syslink_msg *wmsg; /* wire message */
740 * Kinda messy. Figure out the non-blocking state
742 if (flags & O_FBLOCKING)
744 else if (flags & O_FNONBLOCKING)
746 else if (fp->f_flag & O_NONBLOCK)
754 if (uio->uio_iovcnt < 1) {
758 iov0 = &uio->uio_iov[0];
759 if (iov0->iov_len > SLMSG_BIG) {
763 if (uio->uio_iovcnt > 2) {
767 if (uio->uio_iovcnt > 1) {
768 iov1 = &uio->uio_iov[1];
769 if (iov1->iov_len > XIO_INTERNAL_SIZE) {
773 if ((intptr_t)iov1->iov_base & PAGE_MASK) {
782 * Handle the buffer-full case. slpeer cmdbytes is managed
783 * by the backend function, not us so if the callback just
784 * directly implements the message and never adjusts cmdbytes,
785 * we will never sleep here.
787 if (sl->flags & SLF_WSHUTDOWN) {
793 * Only commands can block the pipe, not replies. Otherwise a
794 * deadlock is possible.
796 error = copyin(iov0->iov_base, &sltmp, sizeof(sltmp));
799 if ((proto = sltmp.sm_proto) & SM_PROTO_ENDIAN_REV)
800 proto = bswap16(proto);
801 error = sl->peer->backend_wblocked(sl->peer, nbio, proto);
806 * Allocate a slmsg and load the message. Note that the bytes
807 * returned to userland only reflects the primary syslink message
808 * and does not include any DMA buffers.
810 if (iov0->iov_len <= SLMSG_SMALL)
811 slmsg = objcache_get(sl_objcache_small, M_WAITOK);
813 slmsg = objcache_get(sl_objcache_big, M_WAITOK);
814 slmsg->msgsize = iov0->iov_len;
817 error = uiomove((void *)wmsg, iov0->iov_len, uio);
820 error = syslink_validate_msg(wmsg, slmsg->msgsize);
824 if ((wmsg->sm_head.se_cmd & SE_CMDF_REPLY) == 0) {
826 * Install the XIO for commands if any DMA flags are set.
828 * XIOF_VMLINEAR requires that the XIO represent a
829 * contiguous set of pages associated with a single VM
830 * object (so the reader side can mmap it easily).
832 * XIOF_VMLINEAR might not be set when the kernel sends
833 * commands to userland so the reader side backs off to
834 * a backup buffer if it isn't set, but we require it
835 * for userland writes.
837 xflags = XIOF_VMLINEAR;
838 if (wmsg->sm_head.se_cmd & SE_CMDF_DMAR)
839 xflags |= XIOF_READ | XIOF_WRITE;
840 else if (wmsg->sm_head.se_cmd & SE_CMDF_DMAW)
842 if (xflags && iov1) {
844 error = xio_init_ubuf(&slmsg->xio, iov1->iov_base,
845 iov1->iov_len, xflags);
849 slmsg->flags |= SLMSGF_HASXIO;
851 error = sl->peer->backend_write(sl->peer, slmsg);
854 * Replies have to be matched up against received commands.
856 spin_lock(&sl->spin);
857 slcmd = slmsg_rb_tree_RB_LOOKUP(&sl->reply_rb_root,
858 slmsg->msg->sm_msgid);
859 if (slcmd == NULL || (slcmd->flags & SLMSGF_ONINQ)) {
861 spin_unlock(&sl->spin);
864 RB_REMOVE(slmsg_rb_tree, &sl->reply_rb_root, slcmd);
865 sl->cmdbytes -= slcmd->maxsize;
866 spin_unlock(&sl->spin);
869 * If the original command specified DMAR, has an xio, and
870 * our write specifies a DMA buffer, then we can do a
871 * copyback. But if we are linearly mapped and the caller
872 * is using the map base address, then the caller filled in
873 * the data via the direct memory map and no copyback is
876 if ((slcmd->msg->sm_head.se_cmd & SE_CMDF_DMAR) && iov1 &&
877 (slcmd->flags & SLMSGF_HASXIO) &&
878 ((slcmd->flags & SLMSGF_LINMAP) == 0 ||
879 iov1->iov_base != slcmd->vmbase)
882 if (iov1->iov_len > slcmd->xio.xio_bytes)
883 count = slcmd->xio.xio_bytes;
885 count = iov1->iov_len;
887 error = xio_copy_utox(&slcmd->xio, 0, iov1->iov_base,
893 * If we had mapped a DMA buffer, remove it
895 if (slcmd->flags & SLMSGF_LINMAP) {
897 sl_local_munmap(slcmd);
902 * Reply and handle unblocking
904 sl->peer->backend_reply(sl->peer, slcmd, slmsg);
905 if (sl->wblocked && sl->cmdbytes < syslink_bufsize) {
906 sl->wblocked = 0; /* MP race ok here */
907 wakeup(&sl->wblocked);
911 * slmsg has already been dealt with, make sure error is
912 * 0 so we do not double-free it.
926 * Close a syslink descriptor.
928 * Disassociate the syslink from the file descriptor and disconnect from
933 slfileop_close(struct file *fp)
938 * Disassociate the file pointer. Take ownership of the ref on the
943 fp->f_ops = &badfileops;
947 * Shutdown both directions. The other side will not issue API
948 * calls to us after we've shutdown both directions.
950 shutdownsldesc(sl, SHUT_RDWR);
955 KKASSERT(sl->cmdbytes == 0);
956 KKASSERT(sl->repbytes == 0);
966 slfileop_stat (struct file *fp, struct stat *sb, struct ucred *cred)
973 slfileop_shutdown (struct file *fp, int how)
975 shutdownsldesc((struct sldesc *)fp->f_data, how);
981 slfileop_ioctl (struct file *fp, u_long cmd, caddr_t data,
982 struct ucred *cred, struct sysmsg *msg)
989 slfileop_kqfilter(struct file *fp, struct knote *kn)
994 /************************************************************************
995 * LOCAL MEMORY MAPPING *
996 ************************************************************************
998 * This feature is currently not implemented
1004 sl_local_mmap(struct slmsg *slmsg, char *base, size_t len)
1006 return (EOPNOTSUPP);
1011 sl_local_munmap(struct slmsg *slmsg)
1020 sl_local_mmap(struct slmsg *slmsg, char *base, size_t len)
1022 struct vmspace *vms = curproc->p_vmspace;
1023 vm_offset_t addr = (vm_offset_t)base;
1025 /* XXX check user address range */
1026 error = vm_map_replace(
1028 (vm_offset_t)base, (vm_offset_t)base + len,
1029 slmsg->xio.xio_pages[0]->object,
1030 slmsg->xio.xio_pages[0]->pindex << PAGE_SHIFT,
1031 VM_PROT_READ|VM_PROT_WRITE,
1032 VM_PROT_READ|VM_PROT_WRITE,
1033 MAP_DISABLE_SYNCER);
1036 slmsg->flags |= SLMSGF_LINMAP;
1037 slmsg->vmbase = base;
1038 slmsg->vmsize = len;
1045 sl_local_munmap(struct slmsg *slmsg)
1047 if (slmsg->flags & SLMSGF_LINMAP) {
1048 vm_map_remove(&curproc->p_vmspace->vm_map,
1050 slmsg->vmbase + slcmd->vmsize);
1051 slmsg->flags &= ~SLMSGF_LINMAP;
1057 /************************************************************************
1058 * MESSAGE VALIDATION *
1059 ************************************************************************
1061 * Validate that the syslink message. Check that all headers and elements
1062 * conform. Correct the endian if necessary.
1064 * NOTE: If reverse endian needs to be corrected, SE_CMDF_UNTRANSLATED
1065 * is recursively flipped on all syslink_elm's in the message. As the
1066 * message traverses the mesh, multiple flips may occur. It is
1067 * up to the RPC protocol layer to correct opaque data payloads and
1068 * SE_CMDF_UNTRANSLATED prevents the protocol layer from misinterpreting
1069 * a command or reply element which has not been endian-corrected.
1073 syslink_validate_msg(struct syslink_msg *msg, int bytes)
1080 * The raw message must be properly-aligned.
1082 if (bytes & SL_ALIGNMASK)
1087 * The message must at least contain the msgid, bytes, and
1090 if (bytes < SL_MIN_PAD_SIZE)
1094 * Fix the endian if it is reversed.
1096 if (msg->sm_proto & SM_PROTO_ENDIAN_REV) {
1097 msg->sm_msgid = bswap64(msg->sm_msgid);
1098 msg->sm_sessid = bswap64(msg->sm_sessid);
1099 msg->sm_bytes = bswap16(msg->sm_bytes);
1100 msg->sm_proto = bswap16(msg->sm_proto);
1101 msg->sm_rlabel = bswap32(msg->sm_rlabel);
1102 if (msg->sm_proto & SM_PROTO_ENDIAN_REV)
1110 * Validate the contents. For PADs, the entire payload is
1111 * ignored and the minimum message size can be as small as
1114 if (msg->sm_proto == SMPROTO_PAD) {
1115 if (msg->sm_bytes < SL_MIN_PAD_SIZE ||
1116 msg->sm_bytes > bytes) {
1119 /* ignore the entire payload, it can be garbage */
1121 if (msg->sm_bytes < SL_MIN_MSG_SIZE ||
1122 msg->sm_bytes > bytes) {
1125 error = syslink_validate_elm(
1128 offsetof(struct syslink_msg,
1130 swapit, SL_MAXDEPTH);
1136 * The aligned payload size must be used to locate the
1137 * next syslink_msg in the buffer.
1139 aligned_reclen = SL_MSG_ALIGN(msg->sm_bytes);
1140 bytes -= aligned_reclen;
1141 msg = (void *)((char *)msg + aligned_reclen);
1148 syslink_validate_elm(struct syslink_elm *elm, sl_reclen_t bytes,
1149 int swapit, int depth)
1154 * If the buffer isn't big enough to fit the header, stop now!
1156 if (bytes < SL_MIN_ELM_SIZE)
1159 * All syslink_elm headers are recursively endian-adjusted. Opaque
1160 * data payloads are not.
1163 elm->se_cmd = bswap16(elm->se_cmd) ^ SE_CMDF_UNTRANSLATED;
1164 elm->se_bytes = bswap16(elm->se_bytes);
1165 elm->se_aux = bswap32(elm->se_aux);
1169 * Check element size requirements.
1171 if (elm->se_bytes < SL_MIN_ELM_SIZE || elm->se_bytes > bytes)
1175 * Recursively check structured payloads. A structured payload may
1176 * contain as few as 0 recursive elements.
1178 if (elm->se_cmd & SE_CMDF_STRUCTURED) {
1181 bytes -= SL_MIN_ELM_SIZE;
1184 if (syslink_validate_elm(elm, bytes, swapit, depth - 1))
1186 aligned_reclen = SL_MSG_ALIGN(elm->se_bytes);
1187 elm = (void *)((char *)elm + aligned_reclen);
1188 bytes -= aligned_reclen;
1194 /************************************************************************
1195 * BACKEND FUNCTIONS - USER DESCRIPTOR *
1196 ************************************************************************
1198 * Peer backend links are primarily used when userland creates a pair
1199 * of linked descriptors.
1203 * Do any required blocking / nbio handling for attempts to write to
1204 * a sldesc associated with a user descriptor.
1208 backend_wblocked_user(struct sldesc *sl, int nbio, sl_proto_t proto)
1211 int *bytesp = (proto & SM_PROTO_REPLY) ? &sl->repbytes : &sl->cmdbytes;
1214 * Block until sufficient data is drained by the target. It is
1215 * ok to have a MP race against cmdbytes.
1217 if (*bytesp >= syslink_bufsize) {
1218 spin_lock(&sl->spin);
1219 while (*bytesp >= syslink_bufsize) {
1220 if (sl->flags & SLF_WSHUTDOWN) {
1229 error = ssleep(&sl->wblocked, &sl->spin,
1230 PCATCH, "slwmsg", 0);
1234 spin_unlock(&sl->spin);
1240 * Unconditionally write a syslink message to the sldesc associated with
1241 * a user descriptor. Command messages are also placed in a red-black
1242 * tree so their DMA tag (if any) can be accessed and so they can be
1243 * linked to any reply message.
1247 backend_write_user(struct sldesc *sl, struct slmsg *slmsg)
1251 spin_lock(&sl->spin);
1252 if (sl->flags & SLF_RSHUTDOWN) {
1254 * Not accepting new messages
1257 } else if (slmsg->msg->sm_proto & SM_PROTO_REPLY) {
1261 TAILQ_INSERT_TAIL(&sl->inq, slmsg, tqnode);
1262 sl->repbytes += slmsg->maxsize;
1263 slmsg->flags |= SLMSGF_ONINQ;
1265 } else if (RB_INSERT(slmsg_rb_tree, &sl->reply_rb_root, slmsg)) {
1267 * Write a command, but there was a msgid collision when
1268 * we tried to insert it into the RB tree.
1273 * Write a command, successful insertion into the RB tree.
1275 TAILQ_INSERT_TAIL(&sl->inq, slmsg, tqnode);
1276 sl->cmdbytes += slmsg->maxsize;
1277 slmsg->flags |= SLMSGF_ONINQ;
1280 spin_unlock(&sl->spin);
1282 wakeup(&sl->rwaiters);
1287 * Our peer is replying a command we previously sent it back to us, along
1288 * with the reply message (if not NULL). We just queue the reply to
1289 * userland and free of the command.
1293 backend_reply_user(struct sldesc *sl, struct slmsg *slcmd, struct slmsg *slrep)
1299 spin_lock(&sl->spin);
1300 if ((sl->flags & SLF_RSHUTDOWN) == 0) {
1301 TAILQ_INSERT_TAIL(&sl->inq, slrep, tqnode);
1302 sl->repbytes += slrep->maxsize;
1307 spin_unlock(&sl->spin);
1309 sl->peer->backend_dispose(sl->peer, slrep);
1310 else if (sl->rwaiters)
1311 wakeup(&sl->rwaiters);
1317 backend_dispose_user(struct sldesc *sl, struct slmsg *slmsg)
1322 /************************************************************************
1323 * KERNEL DRIVER OR FILESYSTEM API *
1324 ************************************************************************
1329 * Create a user<->kernel link, returning the user descriptor in *fdp
1330 * and the kernel descriptor in *kslp. 0 is returned on success, and an
1331 * error code is returned on failure.
1334 syslink_ukbackend(int *pfd, struct sldesc **kslp)
1336 struct thread *td = curthread;
1337 struct filedesc *fdp = td->td_proc->p_fd;
1347 error = falloc(td->td_lwp, &fp, &fd);
1350 usl = allocsldesc(NULL);
1351 usl->backend_wblocked = backend_wblocked_user;
1352 usl->backend_write = backend_write_user;
1353 usl->backend_reply = backend_reply_user;
1354 usl->backend_dispose = backend_dispose_user;
1356 ksl = allocsldesc(usl->common);
1358 ksl->backend_wblocked = backend_wblocked_kern;
1359 ksl->backend_write = backend_write_kern;
1360 ksl->backend_reply = backend_reply_kern;
1361 ksl->backend_dispose = backend_dispose_kern;
1365 setsldescfp(usl, fp);
1366 fsetfd(fdp, fp, fd);
1375 * Assign a unique message id, issue a syslink message to userland,
1376 * and wait for a reply.
1379 syslink_kdomsg(struct sldesc *ksl, struct slmsg *slmsg)
1381 struct syslink_msg *msg;
1385 * Finish initializing slmsg and post it to the red-black tree for
1386 * reply matching. If the message id is already in use we return
1387 * EEXIST, giving the originator the chance to roll a new msgid.
1390 slmsg->msgsize = msg->sm_bytes;
1391 if ((error = syslink_validate_msg(msg, msg->sm_bytes)) != 0)
1393 msg->sm_msgid = allocsysid();
1396 * Issue the request and wait for a matching reply or failure,
1397 * then remove the message from the matching tree and return.
1399 error = ksl->peer->backend_write(ksl->peer, slmsg);
1400 spin_lock(&ksl->spin);
1402 while (slmsg->rep == NULL) {
1403 error = ssleep(slmsg, &ksl->spin, 0, "kwtmsg", 0);
1404 /* XXX ignore error for now */
1406 if (slmsg->rep == (struct slmsg *)-1) {
1410 error = slmsg->rep->msg->sm_head.se_aux;
1413 spin_unlock(&ksl->spin);
1418 * Similar to syslink_kdomsg but return immediately instead of
1419 * waiting for a reply. The kernel must supply a callback function
1420 * which will be made in the context of the user process replying
1424 syslink_ksendmsg(struct sldesc *ksl, struct slmsg *slmsg,
1425 void (*func)(struct slmsg *, void *, int), void *arg)
1427 struct syslink_msg *msg;
1431 * Finish initializing slmsg and post it to the red-black tree for
1432 * reply matching. If the message id is already in use we return
1433 * EEXIST, giving the originator the chance to roll a new msgid.
1436 slmsg->msgsize = msg->sm_bytes;
1437 slmsg->callback_func = func;
1438 slmsg->callback_data = arg;
1439 if ((error = syslink_validate_msg(msg, msg->sm_bytes)) != 0)
1441 msg->sm_msgid = allocsysid();
1444 * Issue the request. If no error occured the operation will be
1445 * in progress, otherwise the operation is considered to have failed
1446 * and the caller can deallocate the slmsg.
1448 error = ksl->peer->backend_write(ksl->peer, slmsg);
1453 syslink_kwaitmsg(struct sldesc *ksl, struct slmsg *slmsg)
1457 spin_lock(&ksl->spin);
1458 while (slmsg->rep == NULL) {
1459 error = ssleep(slmsg, &ksl->spin, 0, "kwtmsg", 0);
1460 /* XXX ignore error for now */
1462 if (slmsg->rep == (struct slmsg *)-1) {
1466 error = slmsg->rep->msg->sm_head.se_aux;
1468 spin_unlock(&ksl->spin);
1473 syslink_kallocmsg(void)
1475 return(objcache_get(sl_objcache_small, M_WAITOK));
1479 syslink_kfreemsg(struct sldesc *ksl, struct slmsg *slmsg)
1483 if ((rep = slmsg->rep) != NULL) {
1485 ksl->peer->backend_dispose(ksl->peer, rep);
1487 slmsg->callback_func = NULL;
1492 syslink_kshutdown(struct sldesc *ksl, int how)
1494 shutdownsldesc(ksl, how);
1498 syslink_kclose(struct sldesc *ksl)
1500 shutdownsldesc(ksl, SHUT_RDWR);
1505 * Associate a DMA buffer with a kernel syslink message prior to it
1506 * being sent to userland. The DMA buffer is set up from the point
1507 * of view of the target.
1510 syslink_kdmabuf_pages(struct slmsg *slmsg, struct vm_page **mbase, int npages)
1515 xflags = XIOF_VMLINEAR;
1516 if (slmsg->msg->sm_head.se_cmd & SE_CMDF_DMAR)
1517 xflags |= XIOF_READ | XIOF_WRITE;
1518 else if (slmsg->msg->sm_head.se_cmd & SE_CMDF_DMAW)
1519 xflags |= XIOF_READ;
1520 error = xio_init_pages(&slmsg->xio, mbase, npages, xflags);
1521 slmsg->flags |= SLMSGF_HASXIO;
1526 * Associate a DMA buffer with a kernel syslink message prior to it
1527 * being sent to userland. The DMA buffer is set up from the point
1528 * of view of the target.
1531 syslink_kdmabuf_data(struct slmsg *slmsg, char *base, int bytes)
1535 xflags = XIOF_VMLINEAR;
1536 if (slmsg->msg->sm_head.se_cmd & SE_CMDF_DMAR)
1537 xflags |= XIOF_READ | XIOF_WRITE;
1538 else if (slmsg->msg->sm_head.se_cmd & SE_CMDF_DMAW)
1539 xflags |= XIOF_READ;
1540 xio_init_kbuf(&slmsg->xio, base, bytes);
1541 slmsg->xio.xio_flags |= xflags;
1542 slmsg->flags |= SLMSGF_HASXIO;
1546 /************************************************************************
1547 * BACKEND FUNCTIONS FOR KERNEL API *
1548 ************************************************************************
1550 * These are the backend functions for a sldesc associated with a kernel
1555 * Our peer wants to write a syslink message to us and is asking us to
1556 * block if our input queue is full. We don't implement command reception
1557 * so don't block right now.
1561 backend_wblocked_kern(struct sldesc *ksl, int nbio, sl_proto_t proto)
1568 * Our peer is writing a request to the kernel. At the moment we do not
1573 backend_write_kern(struct sldesc *ksl, struct slmsg *slmsg)
1579 * Our peer wants to reply to a syslink message we sent it earlier. The
1580 * original command (that we passed to our peer), and the peer's reply
1581 * is specified. If the peer has failed slrep will be NULL.
1585 backend_reply_kern(struct sldesc *ksl, struct slmsg *slcmd, struct slmsg *slrep)
1589 spin_lock(&ksl->spin);
1590 if (slrep == NULL) {
1591 slcmd->rep = (struct slmsg *)-1;
1595 error = slrep->msg->sm_head.se_aux;
1597 spin_unlock(&ksl->spin);
1600 * Issue callback or wakeup a synchronous waiter.
1602 if (slcmd->callback_func) {
1603 slcmd->callback_func(slcmd, slcmd->callback_data, error);
1610 * Any reply messages we sent to our peer are returned to us for disposal.
1611 * Since we do not currently accept commands from our peer, there will not
1612 * be any replies returned to the peer to dispose of.
1616 backend_dispose_kern(struct sldesc *ksl, struct slmsg *slmsg)
1618 panic("backend_dispose_kern: kernel can't accept commands so it "
1619 "certainly did not reply to one!");