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