2 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
5 * Portions of this software were developed under sponsorship from Snow
6 * B.V., the Netherlands.
8 * Redistribution and use in source and binary forms, with or without
9 * 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 the
15 * documentation and/or other materials provided with the distribution.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/kernel.h>
36 #include <sys/queue.h>
37 #include <sys/sysctl.h>
38 #include <sys/systm.h>
45 * TTY output queue buffering.
47 * The previous design of the TTY layer offered the so-called clists.
48 * These clists were used for both the input queues and the output
49 * queue. We don't use certain features on the output side, like quoting
50 * bits for parity marking and such. This mechanism is similar to the
51 * old clists, but only contains the features we need to buffer the
56 static long ttyoutq_nfast
= 0;
57 SYSCTL_LONG(_kern
, OID_AUTO
, tty_outq_nfast
, CTLFLAG_RD
,
58 &ttyoutq_nfast
, 0, "Unbuffered reads to userspace on output");
59 static long ttyoutq_nslow
= 0;
60 SYSCTL_LONG(_kern
, OID_AUTO
, tty_outq_nslow
, CTLFLAG_RD
,
61 &ttyoutq_nslow
, 0, "Buffered reads to userspace on output");
63 struct ttyoutq_block
{
64 STAILQ_ENTRY(ttyoutq_block
) tob_list
;
65 char tob_data
[TTYOUTQ_DATASIZE
];
68 static uma_zone_t ttyoutq_zone
;
71 ttyoutq_flush(struct ttyoutq
*to
)
79 ttyoutq_setsize(struct ttyoutq
*to
, struct tty
*tp
, size_t size
)
81 struct ttyoutq_block
*tob
;
83 to
->to_quota
= howmany(size
, TTYOUTQ_DATASIZE
);
85 while (to
->to_quota
> to
->to_nblocks
) {
87 * List is getting bigger.
88 * Add new blocks to the tail of the list.
90 * We must unlock the TTY temporarily, because we need
91 * to allocate memory. This won't be a problem, because
92 * in the worst case, another thread ends up here, which
93 * may cause us to allocate too many blocks, but this
94 * will be caught by the loop below.
97 tob
= uma_zalloc(ttyoutq_zone
, M_WAITOK
);
100 STAILQ_INSERT_TAIL(&to
->to_list
, tob
, tob_list
);
106 ttyoutq_free(struct ttyoutq
*to
)
108 struct ttyoutq_block
*tob
;
113 while ((tob
= STAILQ_FIRST(&to
->to_list
)) != NULL
) {
114 STAILQ_REMOVE_HEAD(&to
->to_list
, tob_list
);
115 uma_zfree(ttyoutq_zone
, tob
);
119 MPASS(to
->to_nblocks
== 0);
123 ttyoutq_read(struct ttyoutq
*to
, void *buf
, size_t len
)
128 struct ttyoutq_block
*tob
;
129 size_t cbegin
, cend
, clen
;
131 /* See if there still is data. */
132 if (to
->to_begin
== to
->to_end
)
134 tob
= STAILQ_FIRST(&to
->to_list
);
139 * The end address should be the lowest of these three:
140 * - The write pointer
141 * - The blocksize - we can't read beyond the block
142 * - The end address if we could perform the full read
144 cbegin
= to
->to_begin
;
145 cend
= MIN(MIN(to
->to_end
, to
->to_begin
+ len
),
147 clen
= cend
- cbegin
;
149 if (cend
== TTYOUTQ_DATASIZE
|| cend
== to
->to_end
) {
150 /* Read the block until the end. */
151 STAILQ_REMOVE_HEAD(&to
->to_list
, tob_list
);
152 if (to
->to_quota
< to
->to_nblocks
) {
153 uma_zfree(ttyoutq_zone
, tob
);
156 STAILQ_INSERT_TAIL(&to
->to_list
, tob
, tob_list
);
159 if (to
->to_end
<= TTYOUTQ_DATASIZE
) {
162 to
->to_end
-= TTYOUTQ_DATASIZE
;
165 /* Read the block partially. */
166 to
->to_begin
+= clen
;
169 /* Copy the data out of the buffers. */
170 memcpy(cbuf
, tob
->tob_data
+ cbegin
, clen
);
175 return (cbuf
- (char *)buf
);
179 * An optimized version of ttyoutq_read() which can be used in pseudo
180 * TTY drivers to directly copy data from the outq to userspace, instead
183 * We can only copy data directly if we need to read the entire block
184 * back to the user, because we temporarily remove the block from the
185 * queue. Otherwise we need to copy it to a temporary buffer first, to
186 * make sure data remains in the correct order.
189 ttyoutq_read_uio(struct ttyoutq
*to
, struct tty
*tp
, struct uio
*uio
)
192 while (uio
->uio_resid
> 0) {
194 struct ttyoutq_block
*tob
;
195 size_t cbegin
, cend
, clen
;
197 /* See if there still is data. */
198 if (to
->to_begin
== to
->to_end
)
200 tob
= STAILQ_FIRST(&to
->to_list
);
205 * The end address should be the lowest of these three:
206 * - The write pointer
207 * - The blocksize - we can't read beyond the block
208 * - The end address if we could perform the full read
210 cbegin
= to
->to_begin
;
211 cend
= MIN(MIN(to
->to_end
, to
->to_begin
+ uio
->uio_resid
),
213 clen
= cend
- cbegin
;
216 * We can prevent buffering in some cases:
217 * - We need to read the block until the end.
218 * - We don't need to read the block until the end, but
219 * there is no data beyond it, which allows us to move
220 * the write pointer to a new block.
222 if (cend
== TTYOUTQ_DATASIZE
|| cend
== to
->to_end
) {
223 atomic_add_long(&ttyoutq_nfast
, 1);
226 * Fast path: zero copy. Remove the first block,
227 * so we can unlock the TTY temporarily.
229 STAILQ_REMOVE_HEAD(&to
->to_list
, tob_list
);
232 if (to
->to_end
<= TTYOUTQ_DATASIZE
) {
235 to
->to_end
-= TTYOUTQ_DATASIZE
;
238 /* Temporary unlock and copy the data to userspace. */
240 error
= uiomove(tob
->tob_data
+ cbegin
, clen
, uio
);
243 /* Block can now be readded to the list. */
244 if (to
->to_quota
<= to
->to_nblocks
) {
245 uma_zfree(ttyoutq_zone
, tob
);
247 STAILQ_INSERT_TAIL(&to
->to_list
, tob
, tob_list
);
251 char ob
[TTYOUTQ_DATASIZE
- 1];
252 atomic_add_long(&ttyoutq_nslow
, 1);
255 * Slow path: store data in a temporary buffer.
257 memcpy(ob
, tob
->tob_data
+ cbegin
, clen
);
258 to
->to_begin
+= clen
;
259 MPASS(to
->to_begin
< TTYOUTQ_DATASIZE
);
261 /* Temporary unlock and copy the data to userspace. */
263 error
= uiomove(ob
, clen
, uio
);
275 ttyoutq_write(struct ttyoutq
*to
, const void *buf
, size_t nbytes
)
277 const char *cbuf
= buf
;
278 struct ttyoutq_block
*tob
;
283 /* Offset in current block. */
284 tob
= to
->to_lastblock
;
285 boff
= to
->to_end
% TTYOUTQ_DATASIZE
;
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_lastblock
= STAILQ_FIRST(&to
->to_list
);
292 /* Queue has no blocks. */
295 } else if (boff
== 0) {
296 /* We reached the end of this block on last write. */
297 tob
= STAILQ_NEXT(tob
, tob_list
);
299 /* We've reached the watermark. */
302 to
->to_lastblock
= tob
;
305 /* Don't copy more than was requested. */
306 l
= MIN(nbytes
, TTYOUTQ_DATASIZE
- boff
);
308 memcpy(tob
->tob_data
+ boff
, cbuf
, l
);
315 return (cbuf
- (const char *)buf
);
319 ttyoutq_write_nofrag(struct ttyoutq
*to
, const void *buf
, size_t nbytes
)
323 if (ttyoutq_bytesleft(to
) < nbytes
)
326 /* We should always be able to write it back. */
327 ret
= ttyoutq_write(to
, buf
, nbytes
);
328 MPASS(ret
== nbytes
);
334 ttyoutq_startup(void *dummy
)
337 ttyoutq_zone
= uma_zcreate("ttyoutq", sizeof(struct ttyoutq_block
),
338 NULL
, NULL
, NULL
, NULL
, UMA_ALIGN_PTR
, 0);
341 SYSINIT(ttyoutq
, SI_SUB_DRIVERS
, SI_ORDER_FIRST
, ttyoutq_startup
, NULL
);