xref: /kvm-unit-tests/lib/s390x/sclp-console.c (revision 2c96b77ec9d3b1fcec7525174e23a6240ee05949)
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 void sclp_print_ascii(const char *str)
93 {
94 	int len = strlen(str);
95 	WriteEventData *sccb = (void *)_sccb;
96 
97 	sclp_mark_busy();
98 	memset(sccb, 0, sizeof(*sccb));
99 	sccb->h.length = offsetof(WriteEventData, msg) + len;
100 	sccb->h.function_code = SCLP_FC_NORMAL_WRITE;
101 	sccb->ebh.length = sizeof(EventBufferHeader) + len;
102 	sccb->ebh.type = SCLP_EVENT_ASCII_CONSOLE_DATA;
103 	memcpy(&sccb->msg, str, len);
104 
105 	sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
106 }
107 
108 static void lm_print(const char *buff, int len)
109 {
110 	unsigned char *ptr, *end, ch;
111 	unsigned int count, offset;
112 	struct WriteEventData *sccb;
113 	struct mdb *mdb;
114 	struct mto *mto;
115 	struct go *go;
116 
117 	sclp_mark_busy();
118 	sccb = (struct WriteEventData *) _sccb;
119 	end = (unsigned char *) sccb + 4096 - 1;
120 	memset(sccb, 0, sizeof(*sccb));
121 	ptr = (unsigned char *) &sccb->msg.mdb.mto;
122 	offset = 0;
123 	do {
124 		for (count = sizeof(*mto); offset < len; count++) {
125 			ch = buff[offset++];
126 			if (ch == 0x0a || ptr + count > end)
127 				break;
128 			ptr[count] = _ascebc[ch];
129 		}
130 		mto = (struct mto *) ptr;
131 		mto->length = count;
132 		mto->type = 4;
133 		mto->line_type_flags = LNTPFLGS_ENDTEXT;
134 		ptr += count;
135 	} while (offset < len && ptr + sizeof(*mto) <= end);
136 	len = ptr - (unsigned char *) sccb;
137 	sccb->h.length = len - offsetof(struct WriteEventData, h);
138 	sccb->h.function_code = SCLP_FC_NORMAL_WRITE;
139 	sccb->ebh.type = EVTYP_MSG;
140 	sccb->ebh.length = len - offsetof(struct WriteEventData, ebh);
141 	mdb = &sccb->msg.mdb;
142 	mdb->header.type = 1;
143 	mdb->header.tag = 0xD4C4C240;
144 	mdb->header.revision_code = 1;
145 	mdb->header.length = len - offsetof(struct WriteEventData, msg.mdb.header);
146 	go = &mdb->go;
147 	go->length = sizeof(*go);
148 	go->type = 1;
149 	sclp_service_call(SCLP_CMD_WRITE_EVENT_DATA, sccb);
150 }
151 
152 
153 /*
154  * In contrast to the ascii console, linemode produces a new
155  * line with every write of data. The report() function uses
156  * several printf() calls to generate a line of data which
157  * would all end up on different lines.
158  *
159  * Hence we buffer here until we encounter a \n or the buffer
160  * is full. That means that linemode output can look a bit
161  * different from ascii and that it takes a bit longer for
162  * lines to appear.
163  */
164 static void sclp_print_lm(const char *str)
165 {
166 	int i;
167 	const int len = strlen(str);
168 
169 	spin_lock(&lm_buff_lock);
170 
171 	for (i = 0; i < len; i++) {
172 		lm_buff[lm_buff_off++] = str[i];
173 
174 		/* Buffer full or newline? */
175 		if (str[i] == '\n' || lm_buff_off == (ARRAY_SIZE(lm_buff) - 1)) {
176 			lm_print(lm_buff, lm_buff_off);
177 			lm_buff_off = 0;
178 		}
179 	}
180 	spin_unlock(&lm_buff_lock);
181 }
182 
183 /*
184  * SCLP needs to be initialized by setting a send and receive mask,
185  * indicating which messages the control program (we) want(s) to
186  * send/receive.
187  */
188 static void sclp_set_write_mask(void)
189 {
190 	WriteEventMask *sccb = (void *)_sccb;
191 
192 	sclp_mark_busy();
193 	memset(_sccb, 0, sizeof(*sccb));
194 	sccb->h.length = sizeof(WriteEventMask);
195 	sccb->h.function_code = SCLP_FC_NORMAL_WRITE;
196 	sccb->mask_length = sizeof(sccb_mask_t);
197 
198 	/* For now we don't process sclp input. */
199 	sccb->cp_receive_mask = 0;
200 	/* We send ASCII and line mode. */
201 	sccb->cp_send_mask = SCLP_EVENT_MASK_MSG_ASCII | SCLP_EVENT_MASK_MSG;
202 
203 	sclp_service_call(SCLP_CMD_WRITE_EVENT_MASK, sccb);
204 	assert(sccb->h.response_code == SCLP_RC_NORMAL_COMPLETION);
205 }
206 
207 void sclp_console_setup(void)
208 {
209 	sclp_set_write_mask();
210 }
211 
212 void sclp_print(const char *str)
213 {
214 	/*
215 	 * z/VM advertises a vt220 console which is not functional:
216 	 * (response code 05F0, "not active because of the state of
217 	 * the machine"). Hence testing the masks would only work if
218 	 * we also use stsi data to distinguish z/VM.
219 	 *
220 	 * Let's rather print on all available consoles.
221 	 */
222 	if (strlen(str) > (PAGE_SIZE / 2)) {
223 		sclp_print_ascii("Warning: Printing is limited to 2KB of data.");
224 		sclp_print_lm("Warning: Printing is limited to 2KB of data.");
225 		return;
226 	}
227 	sclp_print_ascii(str);
228 	sclp_print_lm(str);
229 }
230