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