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