blob: 42f2375c203edc9a44016d6f42dee30c8b232ad9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Michael Holzheu31cb4bd2007-02-05 21:18:29 +01002/*
3 * Hypervisor filesystem for Linux on s390. z/VM implementation.
4 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02005 * Copyright IBM Corp. 2006
Michael Holzheu31cb4bd2007-02-05 21:18:29 +01006 * 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>
Martin Schwidefsky1ec27722015-08-20 17:28:44 +020013#include <asm/diag.h>
Michael Holzheu31cb4bd2007-02-05 21:18:29 +010014#include <asm/ebcdic.h>
Michael Holzheu57b28f62010-05-17 10:00:20 +020015#include <asm/timex.h>
Michael Holzheu31cb4bd2007-02-05 21:18:29 +010016#include "hypfs.h"
17
18#define NAME_LEN 8
Michael Holzheu57b28f62010-05-17 10:00:20 +020019#define DBFS_D2FC_HDR_VERSION 0
Michael Holzheu31cb4bd2007-02-05 21:18:29 +010020
21static char local_guest[] = " ";
22static char all_guests[] = "* ";
23static char *guest_query;
24
25struct 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;
Michael Holzheu36a55402014-03-17 11:31:28 +010037 __u32 ocpus;
Michael Holzheu31cb4bd2007-02-05 21:18:29 +010038 __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
49struct 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
57static 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 parm_list.addr = (unsigned long) addr ;
66 parm_list.size = size;
67 parm_list.fmt = 0x02;
68 memset(parm_list.aci_grp, 0x40, NAME_LEN);
69 rc = -1;
70
Martin Schwidefsky1ec27722015-08-20 17:28:44 +020071 diag_stat_inc(DIAG_STAT_X2FC);
Michael Holzheu31cb4bd2007-02-05 21:18:29 +010072 asm volatile(
73 " diag %0,%1,0x2fc\n"
Heiko Carstens6c22c982016-06-10 09:57:05 +020074 "0: nopr %%r7\n"
Michael Holzheu31cb4bd2007-02-05 21:18:29 +010075 EX_TABLE(0b,0b)
76 : "=d" (residual_cnt), "+d" (rc) : "0" (&parm_list) : "memory");
77
78 if ((rc != 0 ) && (rc != -2))
79 return rc;
80 else
81 return -residual_cnt;
82}
83
Michael Holzheu57b28f62010-05-17 10:00:20 +020084/*
85 * Allocate buffer for "query" and store diag 2fc at "offset"
86 */
87static void *diag2fc_store(char *query, unsigned int *count, int offset)
Michael Holzheu31cb4bd2007-02-05 21:18:29 +010088{
Michael Holzheu57b28f62010-05-17 10:00:20 +020089 void *data;
Michael Holzheu31cb4bd2007-02-05 21:18:29 +010090 int size;
Michael Holzheu31cb4bd2007-02-05 21:18:29 +010091
92 do {
93 size = diag2fc(0, query, NULL);
94 if (size < 0)
95 return ERR_PTR(-EACCES);
Michael Holzheu57b28f62010-05-17 10:00:20 +020096 data = vmalloc(size + offset);
Michael Holzheu31cb4bd2007-02-05 21:18:29 +010097 if (!data)
98 return ERR_PTR(-ENOMEM);
Michael Holzheu57b28f62010-05-17 10:00:20 +020099 if (diag2fc(size, query, data + offset) == 0)
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100100 break;
101 vfree(data);
102 } while (1);
Michael Holzheu57b28f62010-05-17 10:00:20 +0200103 *count = (size / sizeof(struct diag2fc_data));
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100104
105 return data;
106}
107
Michael Holzheu2fcb3682011-01-05 12:47:43 +0100108static void diag2fc_free(const void *data)
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100109{
110 vfree(data);
111}
112
Al Virof78e41e2013-07-19 17:03:49 +0400113#define ATTRIBUTE(dir, name, member) \
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100114do { \
115 void *rc; \
Al Viroa118cfd2013-07-19 18:05:21 +0400116 rc = hypfs_create_u64(dir, name, member); \
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100117 if (IS_ERR(rc)) \
118 return PTR_ERR(rc); \
119} while(0)
120
Al Virof78e41e2013-07-19 17:03:49 +0400121static int hpyfs_vm_create_guest(struct dentry *systems_dir,
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100122 struct diag2fc_data *data)
123{
124 char guest_name[NAME_LEN + 1] = {};
125 struct dentry *guest_dir, *cpus_dir, *samples_dir, *mem_dir;
126 int dedicated_flag, capped_value;
127
128 capped_value = (data->flags & 0x00000006) >> 1;
129 dedicated_flag = (data->flags & 0x00000008) >> 3;
130
131 /* guest dir */
132 memcpy(guest_name, data->guest_name, NAME_LEN);
133 EBCASC(guest_name, NAME_LEN);
Heiko Carstens1d802e22009-12-18 17:43:27 +0100134 strim(guest_name);
Al Viroa118cfd2013-07-19 18:05:21 +0400135 guest_dir = hypfs_mkdir(systems_dir, guest_name);
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100136 if (IS_ERR(guest_dir))
137 return PTR_ERR(guest_dir);
Al Virof78e41e2013-07-19 17:03:49 +0400138 ATTRIBUTE(guest_dir, "onlinetime_us", data->el_time);
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100139
140 /* logical cpu information */
Al Viroa118cfd2013-07-19 18:05:21 +0400141 cpus_dir = hypfs_mkdir(guest_dir, "cpus");
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100142 if (IS_ERR(cpus_dir))
143 return PTR_ERR(cpus_dir);
Al Virof78e41e2013-07-19 17:03:49 +0400144 ATTRIBUTE(cpus_dir, "cputime_us", data->used_cpu);
145 ATTRIBUTE(cpus_dir, "capped", capped_value);
146 ATTRIBUTE(cpus_dir, "dedicated", dedicated_flag);
147 ATTRIBUTE(cpus_dir, "count", data->vcpus);
Michael Holzheu36a55402014-03-17 11:31:28 +0100148 /*
149 * Note: The "weight_min" attribute got the wrong name.
150 * The value represents the number of non-stopped (operating)
151 * CPUS.
152 */
153 ATTRIBUTE(cpus_dir, "weight_min", data->ocpus);
Al Virof78e41e2013-07-19 17:03:49 +0400154 ATTRIBUTE(cpus_dir, "weight_max", data->cpu_max);
155 ATTRIBUTE(cpus_dir, "weight_cur", data->cpu_shares);
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100156
157 /* memory information */
Al Viroa118cfd2013-07-19 18:05:21 +0400158 mem_dir = hypfs_mkdir(guest_dir, "mem");
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100159 if (IS_ERR(mem_dir))
160 return PTR_ERR(mem_dir);
Al Virof78e41e2013-07-19 17:03:49 +0400161 ATTRIBUTE(mem_dir, "min_KiB", data->mem_min_kb);
162 ATTRIBUTE(mem_dir, "max_KiB", data->mem_max_kb);
163 ATTRIBUTE(mem_dir, "used_KiB", data->mem_used_kb);
164 ATTRIBUTE(mem_dir, "share_KiB", data->mem_share_kb);
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100165
166 /* samples */
Al Viroa118cfd2013-07-19 18:05:21 +0400167 samples_dir = hypfs_mkdir(guest_dir, "samples");
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100168 if (IS_ERR(samples_dir))
169 return PTR_ERR(samples_dir);
Al Virof78e41e2013-07-19 17:03:49 +0400170 ATTRIBUTE(samples_dir, "cpu_using", data->cpu_use_samp);
171 ATTRIBUTE(samples_dir, "cpu_delay", data->cpu_delay_samp);
172 ATTRIBUTE(samples_dir, "mem_delay", data->page_wait_samp);
173 ATTRIBUTE(samples_dir, "idle", data->idle_samp);
174 ATTRIBUTE(samples_dir, "other", data->other_samp);
175 ATTRIBUTE(samples_dir, "total", data->total_samp);
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100176 return 0;
177}
178
Al Virof78e41e2013-07-19 17:03:49 +0400179int hypfs_vm_create_files(struct dentry *root)
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100180{
181 struct dentry *dir, *file;
182 struct diag2fc_data *data;
Michael Holzheu57b28f62010-05-17 10:00:20 +0200183 unsigned int count = 0;
184 int rc, i;
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100185
Michael Holzheu57b28f62010-05-17 10:00:20 +0200186 data = diag2fc_store(guest_query, &count, 0);
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100187 if (IS_ERR(data))
188 return PTR_ERR(data);
189
190 /* Hpervisor Info */
Al Viroa118cfd2013-07-19 18:05:21 +0400191 dir = hypfs_mkdir(root, "hyp");
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100192 if (IS_ERR(dir)) {
193 rc = PTR_ERR(dir);
194 goto failed;
195 }
Al Viroa118cfd2013-07-19 18:05:21 +0400196 file = hypfs_create_str(dir, "type", "z/VM Hypervisor");
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100197 if (IS_ERR(file)) {
198 rc = PTR_ERR(file);
199 goto failed;
200 }
201
202 /* physical cpus */
Al Viroa118cfd2013-07-19 18:05:21 +0400203 dir = hypfs_mkdir(root, "cpus");
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100204 if (IS_ERR(dir)) {
205 rc = PTR_ERR(dir);
206 goto failed;
207 }
Al Viroa118cfd2013-07-19 18:05:21 +0400208 file = hypfs_create_u64(dir, "count", data->lcpus);
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100209 if (IS_ERR(file)) {
210 rc = PTR_ERR(file);
211 goto failed;
212 }
213
214 /* guests */
Al Viroa118cfd2013-07-19 18:05:21 +0400215 dir = hypfs_mkdir(root, "systems");
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100216 if (IS_ERR(dir)) {
217 rc = PTR_ERR(dir);
218 goto failed;
219 }
220
221 for (i = 0; i < count; i++) {
Al Virof78e41e2013-07-19 17:03:49 +0400222 rc = hpyfs_vm_create_guest(dir, &(data[i]));
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100223 if (rc)
224 goto failed;
225 }
226 diag2fc_free(data);
227 return 0;
228
229failed:
230 diag2fc_free(data);
231 return rc;
232}
233
Michael Holzheu57b28f62010-05-17 10:00:20 +0200234struct dbfs_d2fc_hdr {
235 u64 len; /* Length of d2fc buffer without header */
236 u16 version; /* Version of header */
Chen Gange38f9782015-01-01 22:27:32 +0800237 char tod_ext[STORE_CLOCK_EXT_SIZE]; /* TOD clock for d2fc */
Michael Holzheu57b28f62010-05-17 10:00:20 +0200238 u64 count; /* Number of VM guests in d2fc buffer */
239 char reserved[30];
240} __attribute__ ((packed));
241
242struct dbfs_d2fc {
243 struct dbfs_d2fc_hdr hdr; /* 64 byte header */
244 char buf[]; /* d2fc buffer */
245} __attribute__ ((packed));
246
Michael Holzheu2fcb3682011-01-05 12:47:43 +0100247static int dbfs_diag2fc_create(void **data, void **data_free_ptr, size_t *size)
Michael Holzheu57b28f62010-05-17 10:00:20 +0200248{
Michael Holzheu2fcb3682011-01-05 12:47:43 +0100249 struct dbfs_d2fc *d2fc;
Michael Holzheu57b28f62010-05-17 10:00:20 +0200250 unsigned int count;
251
Michael Holzheu2fcb3682011-01-05 12:47:43 +0100252 d2fc = diag2fc_store(guest_query, &count, sizeof(d2fc->hdr));
253 if (IS_ERR(d2fc))
254 return PTR_ERR(d2fc);
Heiko Carstens1aae0562013-01-30 09:49:40 +0100255 get_tod_clock_ext(d2fc->hdr.tod_ext);
Michael Holzheu2fcb3682011-01-05 12:47:43 +0100256 d2fc->hdr.len = count * sizeof(struct diag2fc_data);
257 d2fc->hdr.version = DBFS_D2FC_HDR_VERSION;
258 d2fc->hdr.count = count;
259 memset(&d2fc->hdr.reserved, 0, sizeof(d2fc->hdr.reserved));
260 *data = d2fc;
261 *data_free_ptr = d2fc;
262 *size = d2fc->hdr.len + sizeof(struct dbfs_d2fc_hdr);
Michael Holzheu57b28f62010-05-17 10:00:20 +0200263 return 0;
264}
265
Michael Holzheu2fcb3682011-01-05 12:47:43 +0100266static struct hypfs_dbfs_file dbfs_file_2fc = {
267 .name = "diag_2fc",
268 .data_create = dbfs_diag2fc_create,
269 .data_free = diag2fc_free,
Michael Holzheu57b28f62010-05-17 10:00:20 +0200270};
271
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100272int hypfs_vm_init(void)
273{
Michael Holzheu57b28f62010-05-17 10:00:20 +0200274 if (!MACHINE_IS_VM)
275 return 0;
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100276 if (diag2fc(0, all_guests, NULL) > 0)
277 guest_query = all_guests;
278 else if (diag2fc(0, local_guest, NULL) > 0)
279 guest_query = local_guest;
280 else
281 return -EACCES;
Greg Kroah-Hartmanf36108c2019-01-22 16:21:00 +0100282 hypfs_dbfs_create_file(&dbfs_file_2fc);
283 return 0;
Michael Holzheu31cb4bd2007-02-05 21:18:29 +0100284}
Michael Holzheu57b28f62010-05-17 10:00:20 +0200285
286void hypfs_vm_exit(void)
287{
288 if (!MACHINE_IS_VM)
289 return;
Michael Holzheu2fcb3682011-01-05 12:47:43 +0100290 hypfs_dbfs_remove_file(&dbfs_file_2fc);
Michael Holzheu57b28f62010-05-17 10:00:20 +0200291}