xref: /freebsd/sys/kern/tty_outq.c (revision 13464e4a44fc58490a03bb8bfc7e3c972e9c30b2)
1 /*-
2  * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Portions of this software were developed under sponsorship from Snow
6  * B.V., the Netherlands.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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 the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/queue.h>
37 #include <sys/systm.h>
38 #include <sys/tty.h>
39 #include <sys/uio.h>
40 
41 #include <vm/uma.h>
42 
43 /*
44  * TTY output queue buffering.
45  *
46  * The previous design of the TTY layer offered the so-called clists.
47  * These clists were used for both the input queues and the output
48  * queue. We don't use certain features on the output side, like quoting
49  * bits for parity marking and such. This mechanism is similar to the
50  * old clists, but only contains the features we need to buffer the
51  * output.
52  */
53 
54 struct ttyoutq_block {
55 	struct ttyoutq_block	*tob_next;
56 	char			tob_data[TTYOUTQ_DATASIZE];
57 };
58 
59 static uma_zone_t ttyoutq_zone;
60 
61 #define	TTYOUTQ_INSERT_TAIL(to, tob) do {				\
62 	if (to->to_end == 0) {						\
63 		tob->tob_next = to->to_firstblock;			\
64 		to->to_firstblock = tob;				\
65 	} else {							\
66 		tob->tob_next = to->to_lastblock->tob_next;		\
67 		to->to_lastblock->tob_next = tob;			\
68 	}								\
69 	to->to_nblocks++;						\
70 } while (0)
71 
72 #define	TTYOUTQ_REMOVE_HEAD(to) do {					\
73 	to->to_firstblock = to->to_firstblock->tob_next;		\
74 	to->to_nblocks--;						\
75 } while (0)
76 
77 #define	TTYOUTQ_RECYCLE(to, tob) do {					\
78 	if (to->to_quota <= to->to_nblocks)				\
79 		uma_zfree(ttyoutq_zone, tob);				\
80 	else								\
81 		TTYOUTQ_INSERT_TAIL(to, tob);				\
82 } while(0)
83 
84 void
85 ttyoutq_flush(struct ttyoutq *to)
86 {
87 
88 	to->to_begin = 0;
89 	to->to_end = 0;
90 }
91 
92 int
93 ttyoutq_setsize(struct ttyoutq *to, struct tty *tp, size_t size)
94 {
95 	struct ttyoutq_block *tob;
96 
97 	to->to_quota = howmany(size, TTYOUTQ_DATASIZE);
98 
99 	while (to->to_quota > to->to_nblocks) {
100 		/*
101 		 * List is getting bigger.
102 		 * Add new blocks to the tail of the list.
103 		 *
104 		 * We must unlock the TTY temporarily, because we need
105 		 * to allocate memory. This won't be a problem, because
106 		 * in the worst case, another thread ends up here, which
107 		 * may cause us to allocate too many blocks, but this
108 		 * will be caught by the loop below.
109 		 */
110 		tty_unlock(tp);
111 		tob = uma_zalloc(ttyoutq_zone, M_WAITOK);
112 		tty_lock(tp);
113 
114 		if (tty_gone(tp)) {
115 			uma_zfree(ttyoutq_zone, tob);
116 			return (ENXIO);
117 		}
118 
119 		TTYOUTQ_INSERT_TAIL(to, tob);
120 	}
121 	return (0);
122 }
123 
124 void
125 ttyoutq_free(struct ttyoutq *to)
126 {
127 	struct ttyoutq_block *tob;
128 
129 	ttyoutq_flush(to);
130 	to->to_quota = 0;
131 
132 	while ((tob = to->to_firstblock) != NULL) {
133 		TTYOUTQ_REMOVE_HEAD(to);
134 		uma_zfree(ttyoutq_zone, tob);
135 	}
136 
137 	MPASS(to->to_nblocks == 0);
138 }
139 
140 size_t
141 ttyoutq_read(struct ttyoutq *to, void *buf, size_t len)
142 {
143 	char *cbuf = buf;
144 
145 	while (len > 0) {
146 		struct ttyoutq_block *tob;
147 		size_t cbegin, cend, clen;
148 
149 		/* See if there still is data. */
150 		if (to->to_begin == to->to_end)
151 			break;
152 		tob = to->to_firstblock;
153 		if (tob == NULL)
154 			break;
155 
156 		/*
157 		 * The end address should be the lowest of these three:
158 		 * - The write pointer
159 		 * - The blocksize - we can't read beyond the block
160 		 * - The end address if we could perform the full read
161 		 */
162 		cbegin = to->to_begin;
163 		cend = MIN(MIN(to->to_end, to->to_begin + len),
164 		    TTYOUTQ_DATASIZE);
165 		clen = cend - cbegin;
166 
167 		/* Copy the data out of the buffers. */
168 		memcpy(cbuf, tob->tob_data + cbegin, clen);
169 		cbuf += clen;
170 		len -= clen;
171 
172 		if (cend == to->to_end) {
173 			/* Read the complete queue. */
174 			to->to_begin = 0;
175 			to->to_end = 0;
176 		} else if (cend == TTYOUTQ_DATASIZE) {
177 			/* Read the block until the end. */
178 			TTYOUTQ_REMOVE_HEAD(to);
179 			to->to_begin = 0;
180 			to->to_end -= TTYOUTQ_DATASIZE;
181 			TTYOUTQ_RECYCLE(to, tob);
182 		} else {
183 			/* Read the block partially. */
184 			to->to_begin += clen;
185 		}
186 	}
187 
188 	return (cbuf - (char *)buf);
189 }
190 
191 /*
192  * An optimized version of ttyoutq_read() which can be used in pseudo
193  * TTY drivers to directly copy data from the outq to userspace, instead
194  * of buffering it.
195  *
196  * We can only copy data directly if we need to read the entire block
197  * back to the user, because we temporarily remove the block from the
198  * queue. Otherwise we need to copy it to a temporary buffer first, to
199  * make sure data remains in the correct order.
200  */
201 int
202 ttyoutq_read_uio(struct ttyoutq *to, struct tty *tp, struct uio *uio)
203 {
204 
205 	while (uio->uio_resid > 0) {
206 		int error;
207 		struct ttyoutq_block *tob;
208 		size_t cbegin, cend, clen;
209 
210 		/* See if there still is data. */
211 		if (to->to_begin == to->to_end)
212 			return (0);
213 		tob = to->to_firstblock;
214 		if (tob == NULL)
215 			return (0);
216 
217 		/*
218 		 * The end address should be the lowest of these three:
219 		 * - The write pointer
220 		 * - The blocksize - we can't read beyond the block
221 		 * - The end address if we could perform the full read
222 		 */
223 		cbegin = to->to_begin;
224 		cend = MIN(MIN(to->to_end, to->to_begin + uio->uio_resid),
225 		    TTYOUTQ_DATASIZE);
226 		clen = cend - cbegin;
227 
228 		/*
229 		 * We can prevent buffering in some cases:
230 		 * - We need to read the block until the end.
231 		 * - We don't need to read the block until the end, but
232 		 *   there is no data beyond it, which allows us to move
233 		 *   the write pointer to a new block.
234 		 */
235 		if (cend == TTYOUTQ_DATASIZE || cend == to->to_end) {
236 			/*
237 			 * Fast path: zero copy. Remove the first block,
238 			 * so we can unlock the TTY temporarily.
239 			 */
240 			TTYOUTQ_REMOVE_HEAD(to);
241 			to->to_begin = 0;
242 			if (to->to_end <= TTYOUTQ_DATASIZE)
243 				to->to_end = 0;
244 			else
245 				to->to_end -= TTYOUTQ_DATASIZE;
246 
247 			/* Temporary unlock and copy the data to userspace. */
248 			tty_unlock(tp);
249 			error = uiomove(tob->tob_data + cbegin, clen, uio);
250 			tty_lock(tp);
251 
252 			/* Block can now be readded to the list. */
253 			TTYOUTQ_RECYCLE(to, tob);
254 		} else {
255 			char ob[TTYOUTQ_DATASIZE - 1];
256 
257 			/*
258 			 * Slow path: store data in a temporary buffer.
259 			 */
260 			memcpy(ob, tob->tob_data + cbegin, clen);
261 			to->to_begin += clen;
262 			MPASS(to->to_begin < TTYOUTQ_DATASIZE);
263 
264 			/* Temporary unlock and copy the data to userspace. */
265 			tty_unlock(tp);
266 			error = uiomove(ob, clen, uio);
267 			tty_lock(tp);
268 		}
269 
270 		if (error != 0)
271 			return (error);
272 	}
273 
274 	return (0);
275 }
276 
277 size_t
278 ttyoutq_write(struct ttyoutq *to, const void *buf, size_t nbytes)
279 {
280 	const char *cbuf = buf;
281 	struct ttyoutq_block *tob;
282 	unsigned int boff;
283 	size_t l;
284 
285 	while (nbytes > 0) {
286 		boff = to->to_end % TTYOUTQ_DATASIZE;
287 
288 		if (to->to_end == 0) {
289 			/* First time we're being used or drained. */
290 			MPASS(to->to_begin == 0);
291 			tob = to->to_firstblock;
292 			if (tob == NULL) {
293 				/* Queue has no blocks. */
294 				break;
295 			}
296 			to->to_lastblock = tob;
297 		} else if (boff == 0) {
298 			/* We reached the end of this block on last write. */
299 			tob = to->to_lastblock->tob_next;
300 			if (tob == NULL) {
301 				/* We've reached the watermark. */
302 				break;
303 			}
304 			to->to_lastblock = tob;
305 		} else {
306 			tob = to->to_lastblock;
307 		}
308 
309 		/* Don't copy more than was requested. */
310 		l = MIN(nbytes, TTYOUTQ_DATASIZE - boff);
311 		MPASS(l > 0);
312 		memcpy(tob->tob_data + boff, cbuf, l);
313 
314 		cbuf += l;
315 		nbytes -= l;
316 		to->to_end += l;
317 	}
318 
319 	return (cbuf - (const char *)buf);
320 }
321 
322 int
323 ttyoutq_write_nofrag(struct ttyoutq *to, const void *buf, size_t nbytes)
324 {
325 	size_t ret;
326 
327 	if (ttyoutq_bytesleft(to) < nbytes)
328 		return (-1);
329 
330 	/* We should always be able to write it back. */
331 	ret = ttyoutq_write(to, buf, nbytes);
332 	MPASS(ret == nbytes);
333 
334 	return (0);
335 }
336 
337 static void
338 ttyoutq_startup(void *dummy)
339 {
340 
341 	ttyoutq_zone = uma_zcreate("ttyoutq", sizeof(struct ttyoutq_block),
342 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
343 }
344 
345 SYSINIT(ttyoutq, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyoutq_startup, NULL);
346