1 /*
2 * Hypervisor filesystem for Linux on s390. z/VM implementation.
3 *
4 * Copyright IBM Corp. 2006
5 * Author(s): Michael Holzheu <holzheu@de.ibm.com>
6 */
7
8 #include <linux/types.h>
9 #include <linux/errno.h>
10 #include <linux/string.h>
11 #include <linux/vmalloc.h>
12 #include <asm/diag.h>
13 #include <asm/ebcdic.h>
14 #include <asm/timex.h>
15 #include "hypfs.h"
16
17 #define NAME_LEN 8
18 #define DBFS_D2FC_HDR_VERSION 0
19
20 static char local_guest[] = " ";
21 static char all_guests[] = "* ";
22 static char *all_groups = all_guests;
23 static char *guest_query;
24
25 struct diag2fc_data {
26 __u32 version;
27 __u32 flags;
28 __u64 used_cpu;
29 __u64 el_time;
30 __u64 mem_min_kb;
31 __u64 mem_max_kb;
32 __u64 mem_share_kb;
33 __u64 mem_used_kb;
34 __u32 pcpus;
35 __u32 lcpus;
36 __u32 vcpus;
37 __u32 ocpus;
38 __u32 cpu_max;
39 __u32 cpu_shares;
40 __u32 cpu_use_samp;
41 __u32 cpu_delay_samp;
42 __u32 page_wait_samp;
43 __u32 idle_samp;
44 __u32 other_samp;
45 __u32 total_samp;
46 char guest_name[NAME_LEN];
47 };
48
49 struct diag2fc_parm_list {
50 char userid[NAME_LEN];
51 char aci_grp[NAME_LEN];
52 __u64 addr;
53 __u32 size;
54 __u32 fmt;
55 };
56
diag2fc(int size,char * query,void * addr)57 static int diag2fc(int size, char* query, void *addr)
58 {
59 unsigned long residual_cnt;
60 unsigned long rc;
61 struct diag2fc_parm_list parm_list;
62
63 memcpy(parm_list.userid, query, NAME_LEN);
64 ASCEBC(parm_list.userid, NAME_LEN);
65 memcpy(parm_list.aci_grp, all_groups, NAME_LEN);
66 ASCEBC(parm_list.aci_grp, NAME_LEN);
67 parm_list.addr = (unsigned long)addr;
68 parm_list.size = size;
69 parm_list.fmt = 0x02;
70 rc = -1;
71
72 diag_stat_inc(DIAG_STAT_X2FC);
73 asm volatile(
74 " diag %0,%1,0x2fc\n"
75 "0:\n"
76 EX_TABLE(0b,0b)
77 : "=d" (residual_cnt), "+d" (rc) : "0" (&parm_list) : "memory");
78
79 if ((rc != 0 ) && (rc != -2))
80 return rc;
81 else
82 return -residual_cnt;
83 }
84
85 /*
86 * Allocate buffer for "query" and store diag 2fc at "offset"
87 */
diag2fc_store(char * query,unsigned int * count,int offset)88 static void *diag2fc_store(char *query, unsigned int *count, int offset)
89 {
90 void *data;
91 int size;
92
93 do {
94 size = diag2fc(0, query, NULL);
95 if (size < 0)
96 return ERR_PTR(-EACCES);
97 data = vmalloc(size + offset);
98 if (!data)
99 return ERR_PTR(-ENOMEM);
100 if (diag2fc(size, query, data + offset) == 0)
101 break;
102 vfree(data);
103 } while (1);
104 *count = (size / sizeof(struct diag2fc_data));
105
106 return data;
107 }
108
diag2fc_free(const void * data)109 static void diag2fc_free(const void *data)
110 {
111 vfree(data);
112 }
113
114 #define ATTRIBUTE(dir, name, member) \
115 do { \
116 void *rc; \
117 rc = hypfs_create_u64(dir, name, member); \
118 if (IS_ERR(rc)) \
119 return PTR_ERR(rc); \
120 } while(0)
121
hpyfs_vm_create_guest(struct dentry * systems_dir,struct diag2fc_data * data)122 static int hpyfs_vm_create_guest(struct dentry *systems_dir,
123 struct diag2fc_data *data)
124 {
125 char guest_name[NAME_LEN + 1] = {};
126 struct dentry *guest_dir, *cpus_dir, *samples_dir, *mem_dir;
127 int dedicated_flag, capped_value;
128
129 capped_value = (data->flags & 0x00000006) >> 1;
130 dedicated_flag = (data->flags & 0x00000008) >> 3;
131
132 /* guest dir */
133 memcpy(guest_name, data->guest_name, NAME_LEN);
134 EBCASC(guest_name, NAME_LEN);
135 strim(guest_name);
136 guest_dir = hypfs_mkdir(systems_dir, guest_name);
137 if (IS_ERR(guest_dir))
138 return PTR_ERR(guest_dir);
139 ATTRIBUTE(guest_dir, "onlinetime_us", data->el_time);
140
141 /* logical cpu information */
142 cpus_dir = hypfs_mkdir(guest_dir, "cpus");
143 if (IS_ERR(cpus_dir))
144 return PTR_ERR(cpus_dir);
145 ATTRIBUTE(cpus_dir, "cputime_us", data->used_cpu);
146 ATTRIBUTE(cpus_dir, "capped", capped_value);
147 ATTRIBUTE(cpus_dir, "dedicated", dedicated_flag);
148 ATTRIBUTE(cpus_dir, "count", data->vcpus);
149 /*
150 * Note: The "weight_min" attribute got the wrong name.
151 * The value represents the number of non-stopped (operating)
152 * CPUS.
153 */
154 ATTRIBUTE(cpus_dir, "weight_min", data->ocpus);
155 ATTRIBUTE(cpus_dir, "weight_max", data->cpu_max);
156 ATTRIBUTE(cpus_dir, "weight_cur", data->cpu_shares);
157
158 /* memory information */
159 mem_dir = hypfs_mkdir(guest_dir, "mem");
160 if (IS_ERR(mem_dir))
161 return PTR_ERR(mem_dir);
162 ATTRIBUTE(mem_dir, "min_KiB", data->mem_min_kb);
163 ATTRIBUTE(mem_dir, "max_KiB", data->mem_max_kb);
164 ATTRIBUTE(mem_dir, "used_KiB", data->mem_used_kb);
165 ATTRIBUTE(mem_dir, "share_KiB", data->mem_share_kb);
166
167 /* samples */
168 samples_dir = hypfs_mkdir(guest_dir, "samples");
169 if (IS_ERR(samples_dir))
170 return PTR_ERR(samples_dir);
171 ATTRIBUTE(samples_dir, "cpu_using", data->cpu_use_samp);
172 ATTRIBUTE(samples_dir, "cpu_delay", data->cpu_delay_samp);
173 ATTRIBUTE(samples_dir, "mem_delay", data->page_wait_samp);
174 ATTRIBUTE(samples_dir, "idle", data->idle_samp);
175 ATTRIBUTE(samples_dir, "other", data->other_samp);
176 ATTRIBUTE(samples_dir, "total", data->total_samp);
177 return 0;
178 }
179
hypfs_vm_create_files(struct dentry * root)180 int hypfs_vm_create_files(struct dentry *root)
181 {
182 struct dentry *dir, *file;
183 struct diag2fc_data *data;
184 unsigned int count = 0;
185 int rc, i;
186
187 data = diag2fc_store(guest_query, &count, 0);
188 if (IS_ERR(data))
189 return PTR_ERR(data);
190
191 /* Hpervisor Info */
192 dir = hypfs_mkdir(root, "hyp");
193 if (IS_ERR(dir)) {
194 rc = PTR_ERR(dir);
195 goto failed;
196 }
197 file = hypfs_create_str(dir, "type", "z/VM Hypervisor");
198 if (IS_ERR(file)) {
199 rc = PTR_ERR(file);
200 goto failed;
201 }
202
203 /* physical cpus */
204 dir = hypfs_mkdir(root, "cpus");
205 if (IS_ERR(dir)) {
206 rc = PTR_ERR(dir);
207 goto failed;
208 }
209 file = hypfs_create_u64(dir, "count", data->lcpus);
210 if (IS_ERR(file)) {
211 rc = PTR_ERR(file);
212 goto failed;
213 }
214
215 /* guests */
216 dir = hypfs_mkdir(root, "systems");
217 if (IS_ERR(dir)) {
218 rc = PTR_ERR(dir);
219 goto failed;
220 }
221
222 for (i = 0; i < count; i++) {
223 rc = hpyfs_vm_create_guest(dir, &(data[i]));
224 if (rc)
225 goto failed;
226 }
227 diag2fc_free(data);
228 return 0;
229
230 failed:
231 diag2fc_free(data);
232 return rc;
233 }
234
235 struct dbfs_d2fc_hdr {
236 u64 len; /* Length of d2fc buffer without header */
237 u16 version; /* Version of header */
238 char tod_ext[STORE_CLOCK_EXT_SIZE]; /* TOD clock for d2fc */
239 u64 count; /* Number of VM guests in d2fc buffer */
240 char reserved[30];
241 } __attribute__ ((packed));
242
243 struct dbfs_d2fc {
244 struct dbfs_d2fc_hdr hdr; /* 64 byte header */
245 char buf[]; /* d2fc buffer */
246 } __attribute__ ((packed));
247
dbfs_diag2fc_create(void ** data,void ** data_free_ptr,size_t * size)248 static int dbfs_diag2fc_create(void **data, void **data_free_ptr, size_t *size)
249 {
250 struct dbfs_d2fc *d2fc;
251 unsigned int count;
252
253 d2fc = diag2fc_store(guest_query, &count, sizeof(d2fc->hdr));
254 if (IS_ERR(d2fc))
255 return PTR_ERR(d2fc);
256 get_tod_clock_ext(d2fc->hdr.tod_ext);
257 d2fc->hdr.len = count * sizeof(struct diag2fc_data);
258 d2fc->hdr.version = DBFS_D2FC_HDR_VERSION;
259 d2fc->hdr.count = count;
260 memset(&d2fc->hdr.reserved, 0, sizeof(d2fc->hdr.reserved));
261 *data = d2fc;
262 *data_free_ptr = d2fc;
263 *size = d2fc->hdr.len + sizeof(struct dbfs_d2fc_hdr);
264 return 0;
265 }
266
267 static struct hypfs_dbfs_file dbfs_file_2fc = {
268 .name = "diag_2fc",
269 .data_create = dbfs_diag2fc_create,
270 .data_free = diag2fc_free,
271 };
272
hypfs_vm_init(void)273 int hypfs_vm_init(void)
274 {
275 if (!MACHINE_IS_VM)
276 return 0;
277 if (diag2fc(0, all_guests, NULL) > 0)
278 guest_query = all_guests;
279 else if (diag2fc(0, local_guest, NULL) > 0)
280 guest_query = local_guest;
281 else
282 return -EACCES;
283 return hypfs_dbfs_create_file(&dbfs_file_2fc);
284 }
285
hypfs_vm_exit(void)286 void hypfs_vm_exit(void)
287 {
288 if (!MACHINE_IS_VM)
289 return;
290 hypfs_dbfs_remove_file(&dbfs_file_2fc);
291 }
292