cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

sclp_early_core.c (9414B)


      1// SPDX-License-Identifier: GPL-2.0
      2/*
      3 *    Copyright IBM Corp. 2015
      4 *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
      5 */
      6
      7#include <linux/kernel.h>
      8#include <asm/processor.h>
      9#include <asm/lowcore.h>
     10#include <asm/ebcdic.h>
     11#include <asm/irq.h>
     12#include <asm/sections.h>
     13#include <asm/mem_detect.h>
     14#include <asm/facility.h>
     15#include "sclp.h"
     16#include "sclp_rw.h"
     17
     18static struct read_info_sccb __bootdata(sclp_info_sccb);
     19static int __bootdata(sclp_info_sccb_valid);
     20char *__bootdata(sclp_early_sccb);
     21int sclp_init_state = sclp_init_state_uninitialized;
     22/*
     23 * Used to keep track of the size of the event masks. Qemu until version 2.11
     24 * only supports 4 and needs a workaround.
     25 */
     26bool sclp_mask_compat_mode;
     27
     28void sclp_early_wait_irq(void)
     29{
     30	unsigned long psw_mask, addr;
     31	psw_t psw_ext_save, psw_wait;
     32	union ctlreg0 cr0, cr0_new;
     33
     34	__ctl_store(cr0.val, 0, 0);
     35	cr0_new.val = cr0.val & ~CR0_IRQ_SUBCLASS_MASK;
     36	cr0_new.lap = 0;
     37	cr0_new.sssm = 1;
     38	__ctl_load(cr0_new.val, 0, 0);
     39
     40	psw_ext_save = S390_lowcore.external_new_psw;
     41	psw_mask = __extract_psw();
     42	S390_lowcore.external_new_psw.mask = psw_mask;
     43	psw_wait.mask = psw_mask | PSW_MASK_EXT | PSW_MASK_WAIT;
     44	S390_lowcore.ext_int_code = 0;
     45
     46	do {
     47		asm volatile(
     48			"	larl	%[addr],0f\n"
     49			"	stg	%[addr],%[psw_wait_addr]\n"
     50			"	stg	%[addr],%[psw_ext_addr]\n"
     51			"	lpswe	%[psw_wait]\n"
     52			"0:\n"
     53			: [addr] "=&d" (addr),
     54			  [psw_wait_addr] "=Q" (psw_wait.addr),
     55			  [psw_ext_addr] "=Q" (S390_lowcore.external_new_psw.addr)
     56			: [psw_wait] "Q" (psw_wait)
     57			: "cc", "memory");
     58	} while (S390_lowcore.ext_int_code != EXT_IRQ_SERVICE_SIG);
     59
     60	S390_lowcore.external_new_psw = psw_ext_save;
     61	__ctl_load(cr0.val, 0, 0);
     62}
     63
     64int sclp_early_cmd(sclp_cmdw_t cmd, void *sccb)
     65{
     66	unsigned long flags;
     67	int rc;
     68
     69	flags = arch_local_irq_save();
     70	rc = sclp_service_call(cmd, sccb);
     71	if (rc)
     72		goto out;
     73	sclp_early_wait_irq();
     74out:
     75	arch_local_irq_restore(flags);
     76	return rc;
     77}
     78
     79struct write_sccb {
     80	struct sccb_header header;
     81	struct msg_buf msg;
     82} __packed;
     83
     84/* Output multi-line text using SCLP Message interface. */
     85static void sclp_early_print_lm(const char *str, unsigned int len)
     86{
     87	unsigned char *ptr, *end, ch;
     88	unsigned int count, offset;
     89	struct write_sccb *sccb;
     90	struct msg_buf *msg;
     91	struct mdb *mdb;
     92	struct mto *mto;
     93	struct go *go;
     94
     95	sccb = (struct write_sccb *) sclp_early_sccb;
     96	end = (unsigned char *) sccb + EARLY_SCCB_SIZE - 1;
     97	memset(sccb, 0, sizeof(*sccb));
     98	ptr = (unsigned char *) &sccb->msg.mdb.mto;
     99	offset = 0;
    100	do {
    101		for (count = sizeof(*mto); offset < len; count++) {
    102			ch = str[offset++];
    103			if ((ch == 0x0a) || (ptr + count > end))
    104				break;
    105			ptr[count] = _ascebc[ch];
    106		}
    107		mto = (struct mto *) ptr;
    108		memset(mto, 0, sizeof(*mto));
    109		mto->length = count;
    110		mto->type = 4;
    111		mto->line_type_flags = LNTPFLGS_ENDTEXT;
    112		ptr += count;
    113	} while ((offset < len) && (ptr + sizeof(*mto) <= end));
    114	len = ptr - (unsigned char *) sccb;
    115	sccb->header.length = len - offsetof(struct write_sccb, header);
    116	msg = &sccb->msg;
    117	msg->header.type = EVTYP_MSG;
    118	msg->header.length = len - offsetof(struct write_sccb, msg.header);
    119	mdb = &msg->mdb;
    120	mdb->header.type = 1;
    121	mdb->header.tag = 0xD4C4C240;
    122	mdb->header.revision_code = 1;
    123	mdb->header.length = len - offsetof(struct write_sccb, msg.mdb.header);
    124	go = &mdb->go;
    125	go->length = sizeof(*go);
    126	go->type = 1;
    127	sclp_early_cmd(SCLP_CMDW_WRITE_EVENT_DATA, sccb);
    128}
    129
    130struct vt220_sccb {
    131	struct sccb_header header;
    132	struct {
    133		struct evbuf_header header;
    134		char data[];
    135	} msg;
    136} __packed;
    137
    138/* Output multi-line text using SCLP VT220 interface. */
    139static void sclp_early_print_vt220(const char *str, unsigned int len)
    140{
    141	struct vt220_sccb *sccb;
    142
    143	sccb = (struct vt220_sccb *) sclp_early_sccb;
    144	if (sizeof(*sccb) + len >= EARLY_SCCB_SIZE)
    145		len = EARLY_SCCB_SIZE - sizeof(*sccb);
    146	memset(sccb, 0, sizeof(*sccb));
    147	memcpy(&sccb->msg.data, str, len);
    148	sccb->header.length = sizeof(*sccb) + len;
    149	sccb->msg.header.length = sizeof(sccb->msg) + len;
    150	sccb->msg.header.type = EVTYP_VT220MSG;
    151	sclp_early_cmd(SCLP_CMDW_WRITE_EVENT_DATA, sccb);
    152}
    153
    154int sclp_early_set_event_mask(struct init_sccb *sccb,
    155			      sccb_mask_t receive_mask,
    156			      sccb_mask_t send_mask)
    157{
    158retry:
    159	memset(sccb, 0, sizeof(*sccb));
    160	sccb->header.length = sizeof(*sccb);
    161	if (sclp_mask_compat_mode)
    162		sccb->mask_length = SCLP_MASK_SIZE_COMPAT;
    163	else
    164		sccb->mask_length = sizeof(sccb_mask_t);
    165	sccb_set_recv_mask(sccb, receive_mask);
    166	sccb_set_send_mask(sccb, send_mask);
    167	if (sclp_early_cmd(SCLP_CMDW_WRITE_EVENT_MASK, sccb))
    168		return -EIO;
    169	if ((sccb->header.response_code == 0x74f0) && !sclp_mask_compat_mode) {
    170		sclp_mask_compat_mode = true;
    171		goto retry;
    172	}
    173	if (sccb->header.response_code != 0x20)
    174		return -EIO;
    175	return 0;
    176}
    177
    178unsigned int sclp_early_con_check_linemode(struct init_sccb *sccb)
    179{
    180	if (!(sccb_get_sclp_send_mask(sccb) & EVTYP_OPCMD_MASK))
    181		return 0;
    182	if (!(sccb_get_sclp_recv_mask(sccb) & (EVTYP_MSG_MASK | EVTYP_PMSGCMD_MASK)))
    183		return 0;
    184	return 1;
    185}
    186
    187unsigned int sclp_early_con_check_vt220(struct init_sccb *sccb)
    188{
    189	if (sccb_get_sclp_send_mask(sccb) & EVTYP_VT220MSG_MASK)
    190		return 1;
    191	return 0;
    192}
    193
    194static int sclp_early_setup(int disable, int *have_linemode, int *have_vt220)
    195{
    196	unsigned long receive_mask, send_mask;
    197	struct init_sccb *sccb;
    198	int rc;
    199
    200	BUILD_BUG_ON(sizeof(struct init_sccb) > PAGE_SIZE);
    201
    202	*have_linemode = *have_vt220 = 0;
    203	sccb = (struct init_sccb *) sclp_early_sccb;
    204	receive_mask = disable ? 0 : EVTYP_OPCMD_MASK;
    205	send_mask = disable ? 0 : EVTYP_VT220MSG_MASK | EVTYP_MSG_MASK;
    206	rc = sclp_early_set_event_mask(sccb, receive_mask, send_mask);
    207	if (rc)
    208		return rc;
    209	*have_linemode = sclp_early_con_check_linemode(sccb);
    210	*have_vt220 = !!(sccb_get_send_mask(sccb) & EVTYP_VT220MSG_MASK);
    211	return rc;
    212}
    213
    214void sclp_early_set_buffer(void *sccb)
    215{
    216	sclp_early_sccb = sccb;
    217}
    218
    219/*
    220 * Output one or more lines of text on the SCLP console (VT220 and /
    221 * or line-mode).
    222 */
    223void __sclp_early_printk(const char *str, unsigned int len)
    224{
    225	int have_linemode, have_vt220;
    226
    227	if (sclp_init_state != sclp_init_state_uninitialized)
    228		return;
    229	if (sclp_early_setup(0, &have_linemode, &have_vt220) != 0)
    230		return;
    231	if (have_linemode)
    232		sclp_early_print_lm(str, len);
    233	if (have_vt220)
    234		sclp_early_print_vt220(str, len);
    235	sclp_early_setup(1, &have_linemode, &have_vt220);
    236}
    237
    238void sclp_early_printk(const char *str)
    239{
    240	__sclp_early_printk(str, strlen(str));
    241}
    242
    243/*
    244 * We can't pass sclp_info_sccb to sclp_early_cmd() here directly,
    245 * because it might not fulfil the requiremets for a SCLP communication buffer:
    246 *   - lie below 2G in memory
    247 *   - be page-aligned
    248 * Therefore, we use the buffer sclp_early_sccb (which fulfils all those
    249 * requirements) temporarily for communication and copy a received response
    250 * back into the buffer sclp_info_sccb upon successful completion.
    251 */
    252int __init sclp_early_read_info(void)
    253{
    254	int i;
    255	int length = test_facility(140) ? EXT_SCCB_READ_SCP : PAGE_SIZE;
    256	struct read_info_sccb *sccb = (struct read_info_sccb *)sclp_early_sccb;
    257	sclp_cmdw_t commands[] = {SCLP_CMDW_READ_SCP_INFO_FORCED,
    258				  SCLP_CMDW_READ_SCP_INFO};
    259
    260	for (i = 0; i < ARRAY_SIZE(commands); i++) {
    261		memset(sccb, 0, length);
    262		sccb->header.length = length;
    263		sccb->header.function_code = 0x80;
    264		sccb->header.control_mask[2] = 0x80;
    265		if (sclp_early_cmd(commands[i], sccb))
    266			break;
    267		if (sccb->header.response_code == 0x10) {
    268			memcpy(&sclp_info_sccb, sccb, length);
    269			sclp_info_sccb_valid = 1;
    270			return 0;
    271		}
    272		if (sccb->header.response_code != 0x1f0)
    273			break;
    274	}
    275	return -EIO;
    276}
    277
    278struct read_info_sccb * __init sclp_early_get_info(void)
    279{
    280	if (!sclp_info_sccb_valid)
    281		return NULL;
    282
    283	return &sclp_info_sccb;
    284}
    285
    286int __init sclp_early_get_memsize(unsigned long *mem)
    287{
    288	unsigned long rnmax;
    289	unsigned long rnsize;
    290	struct read_info_sccb *sccb = &sclp_info_sccb;
    291
    292	if (!sclp_info_sccb_valid)
    293		return -EIO;
    294
    295	rnmax = sccb->rnmax ? sccb->rnmax : sccb->rnmax2;
    296	rnsize = sccb->rnsize ? sccb->rnsize : sccb->rnsize2;
    297	rnsize <<= 20;
    298	*mem = rnsize * rnmax;
    299	return 0;
    300}
    301
    302int __init sclp_early_get_hsa_size(unsigned long *hsa_size)
    303{
    304	if (!sclp_info_sccb_valid)
    305		return -EIO;
    306
    307	*hsa_size = 0;
    308	if (sclp_info_sccb.hsa_size)
    309		*hsa_size = (sclp_info_sccb.hsa_size - 1) * PAGE_SIZE;
    310	return 0;
    311}
    312
    313#define SCLP_STORAGE_INFO_FACILITY     0x0000400000000000UL
    314
    315void __weak __init add_mem_detect_block(u64 start, u64 end) {}
    316int __init sclp_early_read_storage_info(void)
    317{
    318	struct read_storage_sccb *sccb = (struct read_storage_sccb *)sclp_early_sccb;
    319	int rc, id, max_id = 0;
    320	unsigned long rn, rzm;
    321	sclp_cmdw_t command;
    322	u16 sn;
    323
    324	if (!sclp_info_sccb_valid)
    325		return -EIO;
    326
    327	if (!(sclp_info_sccb.facilities & SCLP_STORAGE_INFO_FACILITY))
    328		return -EOPNOTSUPP;
    329
    330	rzm = sclp_info_sccb.rnsize ?: sclp_info_sccb.rnsize2;
    331	rzm <<= 20;
    332
    333	for (id = 0; id <= max_id; id++) {
    334		memset(sclp_early_sccb, 0, EARLY_SCCB_SIZE);
    335		sccb->header.length = EARLY_SCCB_SIZE;
    336		command = SCLP_CMDW_READ_STORAGE_INFO | (id << 8);
    337		rc = sclp_early_cmd(command, sccb);
    338		if (rc)
    339			goto fail;
    340
    341		max_id = sccb->max_id;
    342		switch (sccb->header.response_code) {
    343		case 0x0010:
    344			for (sn = 0; sn < sccb->assigned; sn++) {
    345				if (!sccb->entries[sn])
    346					continue;
    347				rn = sccb->entries[sn] >> 16;
    348				add_mem_detect_block((rn - 1) * rzm, rn * rzm);
    349			}
    350			break;
    351		case 0x0310:
    352		case 0x0410:
    353			break;
    354		default:
    355			goto fail;
    356		}
    357	}
    358
    359	return 0;
    360fail:
    361	mem_detect.count = 0;
    362	return -EIO;
    363}