xref: /kvm-unit-tests/lib/s390x/sclp-console.c (revision 4c8a99ca02252d4a2bee43f4558fe47ce5ab7ec0)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * SCLP ASCII access driver
4  *
5  * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
6  */
7 
8 #include <libcflat.h>
9 #include <string.h>
10 #include <asm/page.h>
11 #include <asm/arch_def.h>
12 #include <asm/io.h>
13 #include <asm/spinlock.h>
14 #include "sclp.h"
15 
16 /*
17  * ASCII (IBM PC 437) -> EBCDIC 037
18  */
19 static uint8_t _ascebc[256] = {
20  /*00 NUL   SOH   STX   ETX   EOT   ENQ   ACK   BEL */
21      0x00, 0x01, 0x02, 0x03, 0x37, 0x2D, 0x2E, 0x2F,
22  /*08  BS    HT    LF    VT    FF    CR    SO    SI */
23  /*              ->NL                               */
24      0x16, 0x05, 0x15, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
25  /*10 DLE   DC1   DC2   DC3   DC4   NAK   SYN   ETB */
26      0x10, 0x11, 0x12, 0x13, 0x3C, 0x3D, 0x32, 0x26,
27  /*18 CAN    EM   SUB   ESC    FS    GS    RS    US */
28  /*                               ->IGS ->IRS ->IUS */
29      0x18, 0x19, 0x3F, 0x27, 0x22, 0x1D, 0x1E, 0x1F,
30  /*20  SP     !     "     #     $     %     &     ' */
31      0x40, 0x5A, 0x7F, 0x7B, 0x5B, 0x6C, 0x50, 0x7D,
32  /*28   (     )     *     +     ,     -    .      / */
33      0x4D, 0x5D, 0x5C, 0x4E, 0x6B, 0x60, 0x4B, 0x61,
34  /*30   0     1     2     3     4     5     6     7 */
35      0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
36  /*38   8     9     :     ;     <     =     >     ? */
37      0xF8, 0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F,
38  /*40   @     A     B     C     D     E     F     G */
39      0x7C, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
40  /*48   H     I     J     K     L     M     N     O */
41      0xC8, 0xC9, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
42  /*50   P     Q     R     S     T     U     V     W */
43      0xD7, 0xD8, 0xD9, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6,
44  /*58   X     Y     Z     [     \     ]     ^     _ */
45      0xE7, 0xE8, 0xE9, 0xBA, 0xE0, 0xBB, 0xB0, 0x6D,
46  /*60   `     a     b     c     d     e     f     g */
47      0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
48  /*68   h     i     j     k     l     m     n     o */
49      0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
50  /*70   p     q     r     s     t     u     v     w */
51      0x97, 0x98, 0x99, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
52  /*78   x     y     z     {     |     }     ~    DL */
53      0xA7, 0xA8, 0xA9, 0xC0, 0x4F, 0xD0, 0xA1, 0x07,
54  /*80*/
55      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
56  /*88*/
57      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
58  /*90*/
59      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
60  /*98*/
61      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
62  /*A0*/
63      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
64  /*A8*/
65      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
66  /*B0*/
67      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
68  /*B8*/
69      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
70  /*C0*/
71      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
72  /*C8*/
73      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
74  /*D0*/
75      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
76  /*D8*/
77      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
78  /*E0        sz	*/
79      0x3F, 0x59, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
80  /*E8*/
81      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
82  /*F0*/
83      0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
84  /*F8*/
85      0x90, 0x3F, 0x3F, 0x3F, 0x3F, 0xEA, 0x3F, 0xFF
86 };
87 
88 static char lm_buff[120];
89 static unsigned char lm_buff_off;
90 static struct spinlock lm_buff_lock;
91 
92 static char read_buf[4096];
93 static int read_index = sizeof(read_buf) - 1;
94 static int read_buf_length = 0;
95 
96 static void sclp_print_ascii(const char *str)
97 {
98 	int len = strlen(str);
99 	WriteEventData *sccb = (void *)_sccb;
100 
101 	sclp_mark_busy();
102 	memset(sccb, 0, sizeof(*sccb));
103 	sccb->h.length = offsetof(WriteEventData, msg) + len;
104 	sccb->h.function_code = SCLP_FC_NORMAL_WRITE;
105 	sccb->ebh.length = sizeof(EventBufferHeader) + len;
106 	sccb->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
107 	memcpy(&sccb->msg, str, len);
108 
109 	sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
110 }
111 
112 static void lm_print(const char *buff, int len)
113 {
114 	unsigned char *ptr, *end, ch;
115 	unsigned int count, offset;
116 	struct WriteEventData *sccb;
117 	struct mdb *mdb;
118 	struct mto *mto;
119 	struct go *go;
120 
121 	sclp_mark_busy();
122 	sccb = (struct WriteEventData *) _sccb;
123 	end = (unsigned char *) sccb + 4096 - 1;
124 	memset(sccb, 0, sizeof(*sccb));
125 	ptr = (unsigned char *) &sccb->msg.mdb.mto;
126 	offset = 0;
127 	do {
128 		for (count = sizeof(*mto); offset < len; count++) {
129 			ch = buff[offset++];
130 			if (ch == 0x0a || ptr + count > end)
131 				break;
132 			ptr[count] = _ascebc[ch];
133 		}
134 		mto = (struct mto *) ptr;
135 		mto->length = count;
136 		mto->type = 4;
137 		mto->line_type_flags = LNTPFLGS_ENDTEXT;
138 		ptr += count;
139 	} while (offset < len && ptr + sizeof(*mto) <= end);
140 	len = ptr - (unsigned char *) sccb;
141 	sccb->h.length = len - offsetof(struct WriteEventData, h);
142 	sccb->h.function_code = SCLP_FC_NORMAL_WRITE;
143 	sccb->ebh.type = EVTYP_MSG;
144 	sccb->ebh.length = len - offsetof(struct WriteEventData, ebh);
145 	mdb = &sccb->msg.mdb;
146 	mdb->header.type = 1;
147 	mdb->header.tag = 0xD4C4C240;
148 	mdb->header.revision_code = 1;
149 	mdb->header.length = len - offsetof(struct WriteEventData, msg.mdb.header);
150 	go = &mdb->go;
151 	go->length = sizeof(*go);
152 	go->type = 1;
153 	sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
154 }
155 
156 
157 /*
158  * In contrast to the ascii console, linemode produces a new
159  * line with every write of data. The report() function uses
160  * several printf() calls to generate a line of data which
161  * would all end up on different lines.
162  *
163  * Hence we buffer here until we encounter a \n or the buffer
164  * is full. That means that linemode output can look a bit
165  * different from ascii and that it takes a bit longer for
166  * lines to appear.
167  */
168 static void sclp_print_lm(const char *str)
169 {
170 	int i;
171 	const int len = strlen(str);
172 
173 	spin_lock(&lm_buff_lock);
174 
175 	for (i = 0; i < len; i++) {
176 		lm_buff[lm_buff_off++] = str[i];
177 
178 		/* Buffer full or newline? */
179 		if (str[i] == '\n' || lm_buff_off == (ARRAY_SIZE(lm_buff) - 1)) {
180 			lm_print(lm_buff, lm_buff_off);
181 			lm_buff_off = 0;
182 		}
183 	}
184 	spin_unlock(&lm_buff_lock);
185 }
186 
187 /*
188  * SCLP needs to be initialized by setting a send and receive mask,
189  * indicating which messages the control program (we) want(s) to
190  * send/receive.
191  */
192 static void sclp_write_event_mask(int receive_mask, int send_mask)
193 {
194 	WriteEventMask *sccb = (void *)_sccb;
195 
196 	sclp_mark_busy();
197 	memset(_sccb, 0, sizeof(*sccb));
198 	sccb->h.length = sizeof(WriteEventMask);
199 	sccb->h.function_code = SCLP_FC_NORMAL_WRITE;
200 	sccb->mask_length = sizeof(sccb_mask_t);
201 
202 	sccb->cp_receive_mask = receive_mask;
203 	sccb->cp_send_mask = send_mask;
204 
205 	sclp_service_call(SCLP_CMD_WRITE_EVENT_MASK, sccb);
206 	assert(sccb->h.response_code == SCLP_RC_NORMAL_COMPLETION);
207 }
208 
209 static void sclp_console_enable_read(void)
210 {
211 	sclp_write_event_mask(SCLP_EVENT_MASK_MSG_ASCII, SCLP_EVENT_MASK_MSG_ASCII | SCLP_EVENT_MASK_MSG);
212 }
213 
214 static void sclp_console_disable_read(void)
215 {
216 	sclp_write_event_mask(0, SCLP_EVENT_MASK_MSG_ASCII | SCLP_EVENT_MASK_MSG);
217 }
218 
219 void sclp_console_setup(void)
220 {
221 	/* We send ASCII and line mode. */
222 	sclp_write_event_mask(0, SCLP_EVENT_MASK_MSG_ASCII | SCLP_EVENT_MASK_MSG);
223 }
224 
225 void sclp_print(const char *str)
226 {
227 	/*
228 	 * z/VM advertises a vt220 console which is not functional:
229 	 * (response code 05F0, "not active because of the state of
230 	 * the machine"). Hence testing the masks would only work if
231 	 * we also use stsi data to distinguish z/VM.
232 	 *
233 	 * Let's rather print on all available consoles.
234 	 */
235 	if (strlen(str) > (PAGE_SIZE / 2)) {
236 		sclp_print_ascii("Warning: Printing is limited to 2KB of data.");
237 		sclp_print_lm("Warning: Printing is limited to 2KB of data.");
238 		return;
239 	}
240 	sclp_print_ascii(str);
241 	sclp_print_lm(str);
242 }
243 
244 static int console_refill_read_buffer(void)
245 {
246 	const int max_event_buffer_len = SCCB_SIZE - offsetof(ReadEventDataAsciiConsole, ebh);
247 	ReadEventDataAsciiConsole *sccb = (void *)_sccb;
248 	const int event_buffer_ascii_recv_header_len = sizeof(sccb->ebh) + sizeof(sccb->type);
249 	int ret = -1;
250 
251 	sclp_console_enable_read();
252 
253 	sclp_mark_busy();
254 	memset(sccb, 0, SCCB_SIZE);
255 	sccb->h.length = PAGE_SIZE;
256 	sccb->h.function_code = SCLP_UNCONDITIONAL_READ;
257 	sccb->h.control_mask[2] = SCLP_CM2_VARIABLE_LENGTH_RESPONSE;
258 
259 	sclp_service_call(SCLP_CMD_READ_EVENT_DATA, sccb);
260 
261 	if (sccb->h.response_code == SCLP_RC_NO_EVENT_BUFFERS_STORED ||
262 	    sccb->ebh.type != SCLP_EVENT_ASCII_CONSOLE_DATA ||
263 	    sccb->type != SCLP_EVENT_ASCII_TYPE_DATA_STREAM_FOLLOWS) {
264 		ret = -1;
265 		goto out;
266 	}
267 
268 	assert(sccb->ebh.length <= max_event_buffer_len);
269 	assert(sccb->ebh.length > event_buffer_ascii_recv_header_len);
270 
271 	read_buf_length = sccb->ebh.length - event_buffer_ascii_recv_header_len;
272 
273 	assert(read_buf_length <= sizeof(read_buf));
274 	memcpy(read_buf, sccb->data, read_buf_length);
275 
276 	read_index = 0;
277 	ret = 0;
278 
279 out:
280 	sclp_console_disable_read();
281 
282 	return ret;
283 }
284 
285 int __getchar(void)
286 {
287 	int ret;
288 
289 	if (read_index >= read_buf_length) {
290 		ret = console_refill_read_buffer();
291 		if (ret < 0)
292 			return ret;
293 	}
294 
295 	return read_buf[read_index++];
296 }
297