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