2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * Copyright (c) 1992-1998 Søren Schmidt
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 * without modification, immediately at the beginning of the file.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_syscons.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/consio.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
47 #if defined(__arm__) || defined(__mips__) || \
48 defined(__powerpc__) || defined(__sparc64__)
49 #include <machine/sc_machdep.h>
51 #include <machine/pc/display.h>
54 #include <dev/syscons/syscons.h>
58 * This calculations should be dynamically scaled by number of separate sc
59 * devices. A base value of 'extra_history_size' should be defined for
60 * each syscons unit, and added and subtracted from the dynamic
61 * 'extra_history_size' as units are added and removed. This way, each time
62 * a new syscons unit goes online, extra_history_size is automatically bumped.
66 #if !defined(SC_MAX_HISTORY_SIZE)
67 #define SC_MAX_HISTORY_SIZE (1000 * MAXCONS * MAXSC)
70 #if !defined(SC_HISTORY_SIZE)
71 #define SC_HISTORY_SIZE (ROW * 4)
74 #if (SC_HISTORY_SIZE * MAXCONS * MAXSC) > SC_MAX_HISTORY_SIZE
75 #undef SC_MAX_HISTORY_SIZE
76 #define SC_MAX_HISTORY_SIZE (SC_HISTORY_SIZE * MAXCONS * MAXSC)
80 static int extra_history_size
81 = SC_MAX_HISTORY_SIZE - SC_HISTORY_SIZE*MAXCONS;
84 static void copy_history(sc_vtb_t *from, sc_vtb_t *to);
85 static void history_to_screen(scr_stat *scp);
87 /* allocate a history buffer */
89 sc_alloc_history_buffer(scr_stat *scp, int lines, int prev_ysize, int wait)
92 * syscons unconditionally allocates buffers up to
93 * SC_HISTORY_SIZE lines or scp->ysize lines, whichever
94 * is larger. A value greater than that is allowed,
95 * subject to extra_history_size.
98 sc_vtb_t *prev_history;
99 int cur_lines; /* current buffer size */
100 int min_lines; /* guaranteed buffer size */
101 int delta; /* lines to put back */
104 lines = SC_HISTORY_SIZE; /* use the default value */
106 /* make it at least as large as the screen size */
107 lines = imax(lines, scp->ysize);
109 /* remove the history buffer while we update it */
110 history = prev_history = scp->history;
113 /* calculate the amount of lines to put back to extra_history_size */
116 cur_lines = sc_vtb_rows(history);
117 min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
118 if (cur_lines > min_lines)
119 delta = cur_lines - min_lines;
122 /* lines up to min_lines are always allowed. */
123 min_lines = imax(SC_HISTORY_SIZE, scp->ysize);
124 if (lines > min_lines) {
125 if (lines - min_lines > extra_history_size + delta) {
126 /* too many lines are requested */
127 scp->history = prev_history;
132 /* allocate a new buffer */
133 history = (sc_vtb_t *)malloc(sizeof(*history),
135 (wait) ? M_WAITOK : M_NOWAIT);
136 if (history != NULL) {
137 if (lines > min_lines)
138 extra_history_size -= lines - min_lines;
139 /* XXX error check? */
140 sc_vtb_init(history, VTB_RINGBUFFER, scp->xsize, lines,
142 /* FIXME: XXX no good? */
143 sc_vtb_clear(history, scp->sc->scr_map[0x20],
145 if (prev_history != NULL)
146 copy_history(prev_history, history);
147 scp->history_pos = sc_vtb_tail(history);
149 scp->history_pos = 0;
152 /* destroy the previous buffer */
153 if (prev_history != NULL) {
154 extra_history_size += delta;
155 sc_vtb_destroy(prev_history);
156 free(prev_history, M_DEVBUF);
159 scp->history = history;
165 copy_history(sc_vtb_t *from, sc_vtb_t *to)
174 lines = sc_vtb_rows(from);
175 cols1 = sc_vtb_cols(from);
176 cols2 = sc_vtb_cols(to);
177 cols = imin(cols1, cols2);
178 pos = sc_vtb_tail(from);
179 for (i = 0; i < lines; ++i) {
180 sc_vtb_append(from, pos, to, cols);
182 sc_vtb_seek(to, sc_vtb_pos(to,
185 pos = sc_vtb_pos(from, pos, cols1);
190 sc_free_history_buffer(scr_stat *scp, int prev_ysize)
193 int cur_lines; /* current buffer size */
194 int min_lines; /* guaranteed buffer size */
196 history = scp->history;
201 cur_lines = sc_vtb_rows(history);
202 min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
203 extra_history_size += (cur_lines > min_lines) ?
204 cur_lines - min_lines : 0;
206 sc_vtb_destroy(history);
207 free(history, M_DEVBUF);
210 /* copy entire screen into the top of the history buffer */
212 sc_hist_save(scr_stat *scp)
214 sc_vtb_append(&scp->vtb, 0, scp->history, scp->xsize*scp->ysize);
215 scp->history_pos = sc_vtb_tail(scp->history);
218 /* restore the screen by copying from the history buffer */
220 sc_hist_restore(scr_stat *scp)
224 if (scp->history_pos != sc_vtb_tail(scp->history)) {
225 scp->history_pos = sc_vtb_tail(scp->history);
226 history_to_screen(scp);
231 sc_vtb_seek(scp->history, sc_vtb_pos(scp->history,
232 sc_vtb_tail(scp->history),
233 -scp->xsize*scp->ysize));
237 /* copy screen-full of saved lines */
239 history_to_screen(scr_stat *scp)
244 pos = scp->history_pos;
245 for (i = 1; i <= scp->ysize; ++i) {
246 pos = sc_vtb_pos(scp->history, pos, -scp->xsize);
247 sc_vtb_copy(scp->history, pos,
248 &scp->vtb, scp->xsize*(scp->ysize - i),
254 /* go to the tail of the history buffer */
256 sc_hist_home(scr_stat *scp)
258 scp->history_pos = sc_vtb_tail(scp->history);
259 history_to_screen(scp);
262 /* go to the top of the history buffer */
264 sc_hist_end(scr_stat *scp)
266 scp->history_pos = sc_vtb_pos(scp->history, sc_vtb_tail(scp->history),
267 scp->xsize*scp->ysize);
268 history_to_screen(scp);
271 /* move one line up */
273 sc_hist_up_line(scr_stat *scp)
275 if (sc_vtb_pos(scp->history, scp->history_pos, -(scp->xsize*scp->ysize))
276 == sc_vtb_tail(scp->history))
278 scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
280 history_to_screen(scp);
284 /* move one line down */
286 sc_hist_down_line(scr_stat *scp)
288 if (scp->history_pos == sc_vtb_tail(scp->history))
290 scp->history_pos = sc_vtb_pos(scp->history, scp->history_pos,
292 history_to_screen(scp);
297 sc_hist_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
304 case CONS_HISTORY: /* set history size */
306 if (*(int *)data <= 0)
308 if (scp->status & BUFFER_SAVED)
310 DPRINTF(5, ("lines:%d, ysize:%d, pool:%d\n",
311 *(int *)data, scp->ysize, extra_history_size));
312 error = sc_alloc_history_buffer(scp,
313 imax(*(int *)data, scp->ysize),
315 DPRINTF(5, ("error:%d, rows:%d, pool:%d\n", error,
316 sc_vtb_rows(scp->history), extra_history_size));
321 sc_vtb_clear(scp->history, scp->sc->scr_map[0x20],
329 #endif /* SC_NO_HISTORY */