1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Hypervisor filesystem for Linux on s390. z/VM implementation.
4  *
5  *    Copyright IBM Corp. 2006
6  *    Author(s): Michael Holzheu <holzheu@de.ibm.com>
7  */
8 
9 #include <linux/types.h>
10 #include <linux/errno.h>
11 #include <linux/string.h>
12 #include <linux/vmalloc.h>
13 #include <asm/extable.h>
14 #include <asm/machine.h>
15 #include <asm/diag.h>
16 #include <asm/ebcdic.h>
17 #include <asm/timex.h>
18 #include "hypfs_vm.h"
19 #include "hypfs.h"
20 
21 #define DBFS_D2FC_HDR_VERSION 0
22 
23 static char local_guest[] = "        ";
24 static char all_guests[] = "*       ";
25 static char *all_groups = all_guests;
26 char *diag2fc_guest_query;
27 
diag2fc(int size,char * query,void * addr)28 static int diag2fc(int size, char* query, void *addr)
29 {
30 	unsigned long residual_cnt;
31 	unsigned long rc;
32 	struct diag2fc_parm_list parm_list;
33 
34 	memcpy(parm_list.userid, query, DIAG2FC_NAME_LEN);
35 	ASCEBC(parm_list.userid, DIAG2FC_NAME_LEN);
36 	memcpy(parm_list.aci_grp, all_groups, DIAG2FC_NAME_LEN);
37 	ASCEBC(parm_list.aci_grp, DIAG2FC_NAME_LEN);
38 	parm_list.addr = (unsigned long)addr;
39 	parm_list.size = size;
40 	parm_list.fmt = 0x02;
41 	rc = -1;
42 
43 	diag_stat_inc(DIAG_STAT_X2FC);
44 	asm volatile(
45 		"	diag    %0,%1,0x2fc\n"
46 		"0:	nopr	%%r7\n"
47 		EX_TABLE(0b,0b)
48 		: "=d" (residual_cnt), "+d" (rc) : "0" (&parm_list) : "memory");
49 
50 	if ((rc != 0 ) && (rc != -2))
51 		return rc;
52 	else
53 		return -residual_cnt;
54 }
55 
56 /*
57  * Allocate buffer for "query" and store diag 2fc at "offset"
58  */
diag2fc_store(char * query,unsigned int * count,int offset)59 void *diag2fc_store(char *query, unsigned int *count, int offset)
60 {
61 	void *data;
62 	int size;
63 
64 	do {
65 		size = diag2fc(0, query, NULL);
66 		if (size < 0)
67 			return ERR_PTR(-EACCES);
68 		data = vmalloc(size + offset);
69 		if (!data)
70 			return ERR_PTR(-ENOMEM);
71 		if (diag2fc(size, query, data + offset) == 0)
72 			break;
73 		vfree(data);
74 	} while (1);
75 	*count = (size / sizeof(struct diag2fc_data));
76 
77 	return data;
78 }
79 
diag2fc_free(const void * data)80 void diag2fc_free(const void *data)
81 {
82 	vfree(data);
83 }
84 
85 struct dbfs_d2fc_hdr {
86 	u64	len;		/* Length of d2fc buffer without header */
87 	u16	version;	/* Version of header */
88 	union tod_clock tod_ext; /* TOD clock for d2fc */
89 	u64	count;		/* Number of VM guests in d2fc buffer */
90 	char	reserved[30];
91 } __attribute__ ((packed));
92 
93 struct dbfs_d2fc {
94 	struct dbfs_d2fc_hdr	hdr;	/* 64 byte header */
95 	char			buf[];	/* d2fc buffer */
96 } __attribute__ ((packed));
97 
dbfs_diag2fc_create(void ** data,void ** data_free_ptr,size_t * size)98 static int dbfs_diag2fc_create(void **data, void **data_free_ptr, size_t *size)
99 {
100 	struct dbfs_d2fc *d2fc;
101 	unsigned int count;
102 
103 	d2fc = diag2fc_store(diag2fc_guest_query, &count, sizeof(d2fc->hdr));
104 	if (IS_ERR(d2fc))
105 		return PTR_ERR(d2fc);
106 	store_tod_clock_ext(&d2fc->hdr.tod_ext);
107 	d2fc->hdr.len = count * sizeof(struct diag2fc_data);
108 	d2fc->hdr.version = DBFS_D2FC_HDR_VERSION;
109 	d2fc->hdr.count = count;
110 	memset(&d2fc->hdr.reserved, 0, sizeof(d2fc->hdr.reserved));
111 	*data = d2fc;
112 	*data_free_ptr = d2fc;
113 	*size = d2fc->hdr.len + sizeof(struct dbfs_d2fc_hdr);
114 	return 0;
115 }
116 
117 static struct hypfs_dbfs_file dbfs_file_2fc = {
118 	.name		= "diag_2fc",
119 	.data_create	= dbfs_diag2fc_create,
120 	.data_free	= diag2fc_free,
121 };
122 
hypfs_vm_init(void)123 int hypfs_vm_init(void)
124 {
125 	if (!machine_is_vm())
126 		return 0;
127 	if (diag2fc(0, all_guests, NULL) > 0)
128 		diag2fc_guest_query = all_guests;
129 	else if (diag2fc(0, local_guest, NULL) > 0)
130 		diag2fc_guest_query = local_guest;
131 	else
132 		return -EACCES;
133 	hypfs_dbfs_create_file(&dbfs_file_2fc);
134 	return 0;
135 }
136 
hypfs_vm_exit(void)137 void hypfs_vm_exit(void)
138 {
139 	if (!machine_is_vm())
140 		return;
141 	hypfs_dbfs_remove_file(&dbfs_file_2fc);
142 }
143