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