1181905Sed/*-
2181905Sed * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
3181905Sed * All rights reserved.
4181905Sed *
5181905Sed * Portions of this software were developed under sponsorship from Snow
6181905Sed * B.V., the Netherlands.
7181905Sed *
8181905Sed * Redistribution and use in source and binary forms, with or without
9181905Sed * modification, are permitted provided that the following conditions
10181905Sed * are met:
11181905Sed * 1. Redistributions of source code must retain the above copyright
12181905Sed *    notice, this list of conditions and the following disclaimer.
13181905Sed * 2. Redistributions in binary form must reproduce the above copyright
14181905Sed *    notice, this list of conditions and the following disclaimer in the
15181905Sed *    documentation and/or other materials provided with the distribution.
16181905Sed *
17181905Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18181905Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19181905Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20181905Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21181905Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22181905Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23181905Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24181905Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25181905Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26181905Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27181905Sed * SUCH DAMAGE.
28181905Sed */
29181905Sed
30181905Sed#include <sys/cdefs.h>
31181905Sed__FBSDID("$FreeBSD: stable/11/sys/kern/tty_outq.c 314538 2017-03-02 04:23:53Z ian $");
32181905Sed
33181905Sed#include <sys/param.h>
34181905Sed#include <sys/kernel.h>
35181905Sed#include <sys/lock.h>
36181905Sed#include <sys/queue.h>
37181905Sed#include <sys/systm.h>
38181905Sed#include <sys/tty.h>
39181905Sed#include <sys/uio.h>
40181905Sed
41181905Sed#include <vm/uma.h>
42181905Sed
43181905Sed/*
44181905Sed * TTY output queue buffering.
45181905Sed *
46181905Sed * The previous design of the TTY layer offered the so-called clists.
47181905Sed * These clists were used for both the input queues and the output
48181905Sed * queue. We don't use certain features on the output side, like quoting
49181905Sed * bits for parity marking and such. This mechanism is similar to the
50181905Sed * old clists, but only contains the features we need to buffer the
51181905Sed * output.
52181905Sed */
53181905Sed
54181905Sedstruct ttyoutq_block {
55188096Sed	struct ttyoutq_block	*tob_next;
56188096Sed	char			tob_data[TTYOUTQ_DATASIZE];
57181905Sed};
58181905Sed
59181905Sedstatic uma_zone_t ttyoutq_zone;
60181905Sed
61188096Sed#define	TTYOUTQ_INSERT_TAIL(to, tob) do {				\
62188096Sed	if (to->to_end == 0) {						\
63188096Sed		tob->tob_next = to->to_firstblock;			\
64188096Sed		to->to_firstblock = tob;				\
65188096Sed	} else {							\
66188096Sed		tob->tob_next = to->to_lastblock->tob_next;		\
67188096Sed		to->to_lastblock->tob_next = tob;			\
68188096Sed	}								\
69188096Sed	to->to_nblocks++;						\
70188096Sed} while (0)
71188096Sed
72188096Sed#define	TTYOUTQ_REMOVE_HEAD(to) do {					\
73188096Sed	to->to_firstblock = to->to_firstblock->tob_next;		\
74188096Sed	to->to_nblocks--;						\
75188096Sed} while (0)
76188096Sed
77188096Sed#define	TTYOUTQ_RECYCLE(to, tob) do {					\
78188096Sed	if (to->to_quota <= to->to_nblocks)				\
79188096Sed		uma_zfree(ttyoutq_zone, tob);				\
80188096Sed	else								\
81188096Sed		TTYOUTQ_INSERT_TAIL(to, tob);				\
82188096Sed} while(0)
83188096Sed
84181905Sedvoid
85181905Sedttyoutq_flush(struct ttyoutq *to)
86181905Sed{
87181905Sed
88181905Sed	to->to_begin = 0;
89181905Sed	to->to_end = 0;
90181905Sed}
91181905Sed
92314538Sianint
93181905Sedttyoutq_setsize(struct ttyoutq *to, struct tty *tp, size_t size)
94181905Sed{
95181905Sed	struct ttyoutq_block *tob;
96181905Sed
97182471Sed	to->to_quota = howmany(size, TTYOUTQ_DATASIZE);
98181905Sed
99182471Sed	while (to->to_quota > to->to_nblocks) {
100181905Sed		/*
101181905Sed		 * List is getting bigger.
102181905Sed		 * Add new blocks to the tail of the list.
103181905Sed		 *
104181905Sed		 * We must unlock the TTY temporarily, because we need
105181905Sed		 * to allocate memory. This won't be a problem, because
106181905Sed		 * in the worst case, another thread ends up here, which
107181905Sed		 * may cause us to allocate too many blocks, but this
108181905Sed		 * will be caught by the loop below.
109181905Sed		 */
110181905Sed		tty_unlock(tp);
111181905Sed		tob = uma_zalloc(ttyoutq_zone, M_WAITOK);
112181905Sed		tty_lock(tp);
113181905Sed
114314538Sian		if (tty_gone(tp)) {
115314538Sian			uma_zfree(ttyoutq_zone, tob);
116314538Sian			return (ENXIO);
117314538Sian		}
118314538Sian
119188096Sed		TTYOUTQ_INSERT_TAIL(to, tob);
120181905Sed	}
121314538Sian	return (0);
122182471Sed}
123181905Sed
124182471Sedvoid
125182471Sedttyoutq_free(struct ttyoutq *to)
126182471Sed{
127182471Sed	struct ttyoutq_block *tob;
128223575Sed
129182471Sed	ttyoutq_flush(to);
130182471Sed	to->to_quota = 0;
131181905Sed
132188096Sed	while ((tob = to->to_firstblock) != NULL) {
133188096Sed		TTYOUTQ_REMOVE_HEAD(to);
134181905Sed		uma_zfree(ttyoutq_zone, tob);
135181905Sed	}
136182471Sed
137182471Sed	MPASS(to->to_nblocks == 0);
138181905Sed}
139181905Sed
140181905Sedsize_t
141181905Sedttyoutq_read(struct ttyoutq *to, void *buf, size_t len)
142181905Sed{
143181905Sed	char *cbuf = buf;
144181905Sed
145181905Sed	while (len > 0) {
146181905Sed		struct ttyoutq_block *tob;
147181905Sed		size_t cbegin, cend, clen;
148181905Sed
149181905Sed		/* See if there still is data. */
150181905Sed		if (to->to_begin == to->to_end)
151181905Sed			break;
152188096Sed		tob = to->to_firstblock;
153181905Sed		if (tob == NULL)
154181905Sed			break;
155181905Sed
156181905Sed		/*
157181905Sed		 * The end address should be the lowest of these three:
158181905Sed		 * - The write pointer
159181905Sed		 * - The blocksize - we can't read beyond the block
160181905Sed		 * - The end address if we could perform the full read
161181905Sed		 */
162181905Sed		cbegin = to->to_begin;
163181905Sed		cend = MIN(MIN(to->to_end, to->to_begin + len),
164181905Sed		    TTYOUTQ_DATASIZE);
165181905Sed		clen = cend - cbegin;
166181905Sed
167188096Sed		/* Copy the data out of the buffers. */
168188096Sed		memcpy(cbuf, tob->tob_data + cbegin, clen);
169188096Sed		cbuf += clen;
170188096Sed		len -= clen;
171188096Sed
172188096Sed		if (cend == to->to_end) {
173188096Sed			/* Read the complete queue. */
174188096Sed			to->to_begin = 0;
175188096Sed			to->to_end = 0;
176188096Sed		} else if (cend == TTYOUTQ_DATASIZE) {
177181905Sed			/* Read the block until the end. */
178188096Sed			TTYOUTQ_REMOVE_HEAD(to);
179181905Sed			to->to_begin = 0;
180188096Sed			to->to_end -= TTYOUTQ_DATASIZE;
181188096Sed			TTYOUTQ_RECYCLE(to, tob);
182181905Sed		} else {
183181905Sed			/* Read the block partially. */
184181905Sed			to->to_begin += clen;
185181905Sed		}
186181905Sed	}
187181905Sed
188181905Sed	return (cbuf - (char *)buf);
189181905Sed}
190181905Sed
191181905Sed/*
192181905Sed * An optimized version of ttyoutq_read() which can be used in pseudo
193181905Sed * TTY drivers to directly copy data from the outq to userspace, instead
194181905Sed * of buffering it.
195181905Sed *
196181905Sed * We can only copy data directly if we need to read the entire block
197181905Sed * back to the user, because we temporarily remove the block from the
198181905Sed * queue. Otherwise we need to copy it to a temporary buffer first, to
199181905Sed * make sure data remains in the correct order.
200181905Sed */
201181905Sedint
202181905Sedttyoutq_read_uio(struct ttyoutq *to, struct tty *tp, struct uio *uio)
203181905Sed{
204181905Sed
205181905Sed	while (uio->uio_resid > 0) {
206181905Sed		int error;
207181905Sed		struct ttyoutq_block *tob;
208181905Sed		size_t cbegin, cend, clen;
209181905Sed
210181905Sed		/* See if there still is data. */
211181905Sed		if (to->to_begin == to->to_end)
212181905Sed			return (0);
213188096Sed		tob = to->to_firstblock;
214181905Sed		if (tob == NULL)
215181905Sed			return (0);
216181905Sed
217181905Sed		/*
218181905Sed		 * The end address should be the lowest of these three:
219181905Sed		 * - The write pointer
220181905Sed		 * - The blocksize - we can't read beyond the block
221181905Sed		 * - The end address if we could perform the full read
222181905Sed		 */
223181905Sed		cbegin = to->to_begin;
224181905Sed		cend = MIN(MIN(to->to_end, to->to_begin + uio->uio_resid),
225181905Sed		    TTYOUTQ_DATASIZE);
226181905Sed		clen = cend - cbegin;
227181905Sed
228181905Sed		/*
229181905Sed		 * We can prevent buffering in some cases:
230181905Sed		 * - We need to read the block until the end.
231181905Sed		 * - We don't need to read the block until the end, but
232181905Sed		 *   there is no data beyond it, which allows us to move
233181905Sed		 *   the write pointer to a new block.
234181905Sed		 */
235181905Sed		if (cend == TTYOUTQ_DATASIZE || cend == to->to_end) {
236181905Sed			/*
237181905Sed			 * Fast path: zero copy. Remove the first block,
238181905Sed			 * so we can unlock the TTY temporarily.
239181905Sed			 */
240188096Sed			TTYOUTQ_REMOVE_HEAD(to);
241181905Sed			to->to_begin = 0;
242188096Sed			if (to->to_end <= TTYOUTQ_DATASIZE)
243181905Sed				to->to_end = 0;
244188096Sed			else
245181905Sed				to->to_end -= TTYOUTQ_DATASIZE;
246181905Sed
247181905Sed			/* Temporary unlock and copy the data to userspace. */
248181905Sed			tty_unlock(tp);
249181905Sed			error = uiomove(tob->tob_data + cbegin, clen, uio);
250181905Sed			tty_lock(tp);
251181905Sed
252181905Sed			/* Block can now be readded to the list. */
253188096Sed			TTYOUTQ_RECYCLE(to, tob);
254181905Sed		} else {
255181905Sed			char ob[TTYOUTQ_DATASIZE - 1];
256181905Sed
257181905Sed			/*
258181905Sed			 * Slow path: store data in a temporary buffer.
259181905Sed			 */
260181905Sed			memcpy(ob, tob->tob_data + cbegin, clen);
261181905Sed			to->to_begin += clen;
262181905Sed			MPASS(to->to_begin < TTYOUTQ_DATASIZE);
263181905Sed
264181905Sed			/* Temporary unlock and copy the data to userspace. */
265181905Sed			tty_unlock(tp);
266181905Sed			error = uiomove(ob, clen, uio);
267181905Sed			tty_lock(tp);
268182471Sed		}
269181905Sed
270182471Sed		if (error != 0)
271182471Sed			return (error);
272181905Sed	}
273181905Sed
274181905Sed	return (0);
275181905Sed}
276181905Sed
277181905Sedsize_t
278181905Sedttyoutq_write(struct ttyoutq *to, const void *buf, size_t nbytes)
279181905Sed{
280181905Sed	const char *cbuf = buf;
281181905Sed	struct ttyoutq_block *tob;
282181905Sed	unsigned int boff;
283181905Sed	size_t l;
284181905Sed
285181905Sed	while (nbytes > 0) {
286181905Sed		boff = to->to_end % TTYOUTQ_DATASIZE;
287181905Sed
288181905Sed		if (to->to_end == 0) {
289181905Sed			/* First time we're being used or drained. */
290181905Sed			MPASS(to->to_begin == 0);
291188096Sed			tob = to->to_firstblock;
292181905Sed			if (tob == NULL) {
293181905Sed				/* Queue has no blocks. */
294181905Sed				break;
295181905Sed			}
296188096Sed			to->to_lastblock = tob;
297181905Sed		} else if (boff == 0) {
298181905Sed			/* We reached the end of this block on last write. */
299188096Sed			tob = to->to_lastblock->tob_next;
300181905Sed			if (tob == NULL) {
301181905Sed				/* We've reached the watermark. */
302181905Sed				break;
303181905Sed			}
304181905Sed			to->to_lastblock = tob;
305188096Sed		} else {
306188096Sed			tob = to->to_lastblock;
307181905Sed		}
308181905Sed
309181905Sed		/* Don't copy more than was requested. */
310181905Sed		l = MIN(nbytes, TTYOUTQ_DATASIZE - boff);
311181905Sed		MPASS(l > 0);
312181905Sed		memcpy(tob->tob_data + boff, cbuf, l);
313181905Sed
314181905Sed		cbuf += l;
315181905Sed		nbytes -= l;
316181905Sed		to->to_end += l;
317181905Sed	}
318181905Sed
319181905Sed	return (cbuf - (const char *)buf);
320181905Sed}
321181905Sed
322181905Sedint
323181905Sedttyoutq_write_nofrag(struct ttyoutq *to, const void *buf, size_t nbytes)
324181905Sed{
325181905Sed	size_t ret;
326181905Sed
327181905Sed	if (ttyoutq_bytesleft(to) < nbytes)
328181905Sed		return (-1);
329181905Sed
330181905Sed	/* We should always be able to write it back. */
331181905Sed	ret = ttyoutq_write(to, buf, nbytes);
332181905Sed	MPASS(ret == nbytes);
333181905Sed
334181905Sed	return (0);
335181905Sed}
336181905Sed
337181905Sedstatic void
338181905Sedttyoutq_startup(void *dummy)
339181905Sed{
340181905Sed
341181905Sed	ttyoutq_zone = uma_zcreate("ttyoutq", sizeof(struct ttyoutq_block),
342181905Sed	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
343181905Sed}
344181905Sed
345181905SedSYSINIT(ttyoutq, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyoutq_startup, NULL);
346