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