1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
33#include <sys/kernel.h>
34#include <sys/lock.h>
35#include <sys/queue.h>
36#include <sys/systm.h>
37#include <sys/tty.h>
38#include <sys/uio.h>
39
40#include <vm/uma.h>
41
42/*
43 * TTY output queue buffering.
44 *
45 * The previous design of the TTY layer offered the so-called clists.
46 * These clists were used for both the input queues and the output
47 * queue. We don't use certain features on the output side, like quoting
48 * bits for parity marking and such. This mechanism is similar to the
49 * old clists, but only contains the features we need to buffer the
50 * output.
51 */
52
53struct ttyoutq_block {
54	struct ttyoutq_block	*tob_next;
55	char			tob_data[TTYOUTQ_DATASIZE];
56};
57
58static uma_zone_t ttyoutq_zone;
59
60#define	TTYOUTQ_INSERT_TAIL(to, tob) do {				\
61	if (to->to_end == 0) {						\
62		tob->tob_next = to->to_firstblock;			\
63		to->to_firstblock = tob;				\
64	} else {							\
65		tob->tob_next = to->to_lastblock->tob_next;		\
66		to->to_lastblock->tob_next = tob;			\
67	}								\
68	to->to_nblocks++;						\
69} while (0)
70
71#define	TTYOUTQ_REMOVE_HEAD(to) do {					\
72	to->to_firstblock = to->to_firstblock->tob_next;		\
73	to->to_nblocks--;						\
74} while (0)
75
76#define	TTYOUTQ_RECYCLE(to, tob) do {					\
77	if (to->to_quota <= to->to_nblocks)				\
78		uma_zfree(ttyoutq_zone, tob);				\
79	else								\
80		TTYOUTQ_INSERT_TAIL(to, tob);				\
81} while (0)
82
83void
84ttyoutq_flush(struct ttyoutq *to)
85{
86
87	to->to_begin = 0;
88	to->to_end = 0;
89}
90
91int
92ttyoutq_setsize(struct ttyoutq *to, struct tty *tp, size_t size)
93{
94	struct ttyoutq_block *tob;
95
96	to->to_quota = howmany(size, TTYOUTQ_DATASIZE);
97
98	while (to->to_quota > to->to_nblocks) {
99		/*
100		 * List is getting bigger.
101		 * Add new blocks to the tail of the list.
102		 *
103		 * We must unlock the TTY temporarily, because we need
104		 * to allocate memory. This won't be a problem, because
105		 * in the worst case, another thread ends up here, which
106		 * may cause us to allocate too many blocks, but this
107		 * will be caught by the loop below.
108		 */
109		tty_unlock(tp);
110		tob = uma_zalloc(ttyoutq_zone, M_WAITOK);
111		tty_lock(tp);
112
113		if (tty_gone(tp)) {
114			uma_zfree(ttyoutq_zone, tob);
115			return (ENXIO);
116		}
117
118		TTYOUTQ_INSERT_TAIL(to, tob);
119	}
120	return (0);
121}
122
123void
124ttyoutq_free(struct ttyoutq *to)
125{
126	struct ttyoutq_block *tob;
127
128	ttyoutq_flush(to);
129	to->to_quota = 0;
130
131	while ((tob = to->to_firstblock) != NULL) {
132		TTYOUTQ_REMOVE_HEAD(to);
133		uma_zfree(ttyoutq_zone, tob);
134	}
135
136	MPASS(to->to_nblocks == 0);
137}
138
139size_t
140ttyoutq_read(struct ttyoutq *to, void *buf, size_t len)
141{
142	char *cbuf = buf;
143
144	while (len > 0) {
145		struct ttyoutq_block *tob;
146		size_t cbegin, cend, clen;
147
148		/* See if there still is data. */
149		if (to->to_begin == to->to_end)
150			break;
151		tob = to->to_firstblock;
152		if (tob == NULL)
153			break;
154
155		/*
156		 * The end address should be the lowest of these three:
157		 * - The write pointer
158		 * - The blocksize - we can't read beyond the block
159		 * - The end address if we could perform the full read
160		 */
161		cbegin = to->to_begin;
162		cend = MIN(MIN(to->to_end, to->to_begin + len),
163		    TTYOUTQ_DATASIZE);
164		clen = cend - cbegin;
165
166		/* Copy the data out of the buffers. */
167		memcpy(cbuf, tob->tob_data + cbegin, clen);
168		cbuf += clen;
169		len -= clen;
170
171		if (cend == to->to_end) {
172			/* Read the complete queue. */
173			to->to_begin = 0;
174			to->to_end = 0;
175		} else if (cend == TTYOUTQ_DATASIZE) {
176			/* Read the block until the end. */
177			TTYOUTQ_REMOVE_HEAD(to);
178			to->to_begin = 0;
179			to->to_end -= TTYOUTQ_DATASIZE;
180			TTYOUTQ_RECYCLE(to, tob);
181		} else {
182			/* Read the block partially. */
183			to->to_begin += clen;
184		}
185	}
186
187	return (cbuf - (char *)buf);
188}
189
190/*
191 * An optimized version of ttyoutq_read() which can be used in pseudo
192 * TTY drivers to directly copy data from the outq to userspace, instead
193 * of buffering it.
194 *
195 * We can only copy data directly if we need to read the entire block
196 * back to the user, because we temporarily remove the block from the
197 * queue. Otherwise we need to copy it to a temporary buffer first, to
198 * make sure data remains in the correct order.
199 */
200int
201ttyoutq_read_uio(struct ttyoutq *to, struct tty *tp, struct uio *uio)
202{
203
204	while (uio->uio_resid > 0) {
205		int error;
206		struct ttyoutq_block *tob;
207		size_t cbegin, cend, clen;
208
209		/* See if there still is data. */
210		if (to->to_begin == to->to_end)
211			return (0);
212		tob = to->to_firstblock;
213		if (tob == NULL)
214			return (0);
215
216		/*
217		 * The end address should be the lowest of these three:
218		 * - The write pointer
219		 * - The blocksize - we can't read beyond the block
220		 * - The end address if we could perform the full read
221		 */
222		cbegin = to->to_begin;
223		cend = MIN(MIN(to->to_end, to->to_begin + uio->uio_resid),
224		    TTYOUTQ_DATASIZE);
225		clen = cend - cbegin;
226
227		/*
228		 * We can prevent buffering in some cases:
229		 * - We need to read the block until the end.
230		 * - We don't need to read the block until the end, but
231		 *   there is no data beyond it, which allows us to move
232		 *   the write pointer to a new block.
233		 */
234		if (cend == TTYOUTQ_DATASIZE || cend == to->to_end) {
235			/*
236			 * Fast path: zero copy. Remove the first block,
237			 * so we can unlock the TTY temporarily.
238			 */
239			TTYOUTQ_REMOVE_HEAD(to);
240			to->to_begin = 0;
241			if (to->to_end <= TTYOUTQ_DATASIZE)
242				to->to_end = 0;
243			else
244				to->to_end -= TTYOUTQ_DATASIZE;
245
246			/* Temporary unlock and copy the data to userspace. */
247			tty_unlock(tp);
248			error = uiomove(tob->tob_data + cbegin, clen, uio);
249			tty_lock(tp);
250
251			/* Block can now be readded to the list. */
252			TTYOUTQ_RECYCLE(to, tob);
253		} else {
254			char ob[TTYOUTQ_DATASIZE - 1];
255
256			/*
257			 * Slow path: store data in a temporary buffer.
258			 */
259			memcpy(ob, tob->tob_data + cbegin, clen);
260			to->to_begin += clen;
261			MPASS(to->to_begin < TTYOUTQ_DATASIZE);
262
263			/* Temporary unlock and copy the data to userspace. */
264			tty_unlock(tp);
265			error = uiomove(ob, clen, uio);
266			tty_lock(tp);
267		}
268
269		if (error != 0)
270			return (error);
271	}
272
273	return (0);
274}
275
276size_t
277ttyoutq_write(struct ttyoutq *to, const void *buf, size_t nbytes)
278{
279	const char *cbuf = buf;
280	struct ttyoutq_block *tob;
281	unsigned int boff;
282	size_t l;
283
284	while (nbytes > 0) {
285		boff = to->to_end % TTYOUTQ_DATASIZE;
286
287		if (to->to_end == 0) {
288			/* First time we're being used or drained. */
289			MPASS(to->to_begin == 0);
290			tob = to->to_firstblock;
291			if (tob == NULL) {
292				/* Queue has no blocks. */
293				break;
294			}
295			to->to_lastblock = tob;
296		} else if (boff == 0) {
297			/* We reached the end of this block on last write. */
298			tob = to->to_lastblock->tob_next;
299			if (tob == NULL) {
300				/* We've reached the watermark. */
301				break;
302			}
303			to->to_lastblock = tob;
304		} else {
305			tob = to->to_lastblock;
306		}
307
308		/* Don't copy more than was requested. */
309		l = MIN(nbytes, TTYOUTQ_DATASIZE - boff);
310		MPASS(l > 0);
311		memcpy(tob->tob_data + boff, cbuf, l);
312
313		cbuf += l;
314		nbytes -= l;
315		to->to_end += l;
316	}
317
318	return (cbuf - (const char *)buf);
319}
320
321int
322ttyoutq_write_nofrag(struct ttyoutq *to, const void *buf, size_t nbytes)
323{
324	size_t ret __unused;
325
326	if (ttyoutq_bytesleft(to) < nbytes)
327		return (-1);
328
329	/* We should always be able to write it back. */
330	ret = ttyoutq_write(to, buf, nbytes);
331	MPASS(ret == nbytes);
332
333	return (0);
334}
335
336static void
337ttyoutq_startup(void *dummy)
338{
339
340	ttyoutq_zone = uma_zcreate("ttyoutq", sizeof(struct ttyoutq_block),
341	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
342}
343
344SYSINIT(ttyoutq, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyoutq_startup, NULL);
345