| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* copyright (c) 2006 IBM Corporation |
| 4 |
* Authored by: Mike D. Day <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <linux/slab.h> |
| 8 |
#include <linux/kernel.h> |
| 9 |
#include <linux/init.h> |
| 10 |
#include <linux/kobject.h> |
| 11 |
#include <linux/err.h> |
| 12 |
|
| 13 |
#include <asm/xen/hypervisor.h> |
| 14 |
#include <asm/xen/hypercall.h> |
| 15 |
|
| 16 |
#include <xen/xen.h> |
| 17 |
#include <xen/xenbus.h> |
| 18 |
#include <xen/interface/xen.h> |
| 19 |
#include <xen/interface/version.h> |
| 20 |
#ifdef CONFIG_XEN_HAVE_VPMU |
| 21 |
#include <xen/interface/xenpmu.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#define HYPERVISOR_ATTR_RO(_name) \ |
| 25 |
static struct hyp_sysfs_attr _name##_attr = __ATTR_RO(_name) |
| 26 |
|
| 27 |
#define HYPERVISOR_ATTR_RW(_name) \ |
| 28 |
static struct hyp_sysfs_attr _name##_attr = __ATTR_RW(_name) |
| 29 |
|
| 30 |
struct hyp_sysfs_attr { |
| 31 |
struct attribute attr; |
| 32 |
ssize_t (*show)(struct hyp_sysfs_attr *, char *); |
| 33 |
ssize_t (*store)(struct hyp_sysfs_attr *, const char *, size_t); |
| 34 |
union { |
| 35 |
void *hyp_attr_data; |
| 36 |
unsigned long hyp_attr_value; |
| 37 |
}; |
| 38 |
}; |
| 39 |
|
| 40 |
static ssize_t type_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 41 |
{ |
| 42 |
return sprintf(buffer, "xen\n"); |
| 43 |
} |
| 44 |
|
| 45 |
HYPERVISOR_ATTR_RO(type); |
| 46 |
|
| 47 |
static int __init xen_sysfs_type_init(void) |
| 48 |
{ |
| 49 |
return sysfs_create_file(hypervisor_kobj, &type_attr.attr); |
| 50 |
} |
| 51 |
|
| 52 |
static ssize_t guest_type_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 53 |
{ |
| 54 |
const char *type; |
| 55 |
|
| 56 |
switch (xen_domain_type) { |
| 57 |
case XEN_NATIVE: |
| 58 |
/* ARM only. */ |
| 59 |
type = "Xen"; |
| 60 |
break; |
| 61 |
case XEN_PV_DOMAIN: |
| 62 |
type = "PV"; |
| 63 |
break; |
| 64 |
case XEN_HVM_DOMAIN: |
| 65 |
type = xen_pvh_domain() ? "PVH" : "HVM"; |
| 66 |
break; |
| 67 |
default: |
| 68 |
return -EINVAL; |
| 69 |
} |
| 70 |
|
| 71 |
return sprintf(buffer, "%s\n", type); |
| 72 |
} |
| 73 |
|
| 74 |
HYPERVISOR_ATTR_RO(guest_type); |
| 75 |
|
| 76 |
static int __init xen_sysfs_guest_type_init(void) |
| 77 |
{ |
| 78 |
return sysfs_create_file(hypervisor_kobj, &guest_type_attr.attr); |
| 79 |
} |
| 80 |
|
| 81 |
/* xen version attributes */ |
| 82 |
static ssize_t major_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 83 |
{ |
| 84 |
int version = HYPERVISOR_xen_version(XENVER_version, NULL); |
| 85 |
if (version) |
| 86 |
return sprintf(buffer, "%d\n", version >> 16); |
| 87 |
return -ENODEV; |
| 88 |
} |
| 89 |
|
| 90 |
HYPERVISOR_ATTR_RO(major); |
| 91 |
|
| 92 |
static ssize_t minor_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 93 |
{ |
| 94 |
int version = HYPERVISOR_xen_version(XENVER_version, NULL); |
| 95 |
if (version) |
| 96 |
return sprintf(buffer, "%d\n", version & 0xff); |
| 97 |
return -ENODEV; |
| 98 |
} |
| 99 |
|
| 100 |
HYPERVISOR_ATTR_RO(minor); |
| 101 |
|
| 102 |
static ssize_t extra_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 103 |
{ |
| 104 |
int ret = -ENOMEM; |
| 105 |
char *extra; |
| 106 |
|
| 107 |
extra = kmalloc(XEN_EXTRAVERSION_LEN, GFP_KERNEL); |
| 108 |
if (extra) { |
| 109 |
ret = HYPERVISOR_xen_version(XENVER_extraversion, extra); |
| 110 |
if (!ret) |
| 111 |
ret = sprintf(buffer, "%s\n", extra); |
| 112 |
kfree(extra); |
| 113 |
} |
| 114 |
|
| 115 |
return ret; |
| 116 |
} |
| 117 |
|
| 118 |
HYPERVISOR_ATTR_RO(extra); |
| 119 |
|
| 120 |
static struct attribute *version_attrs[] = { |
| 121 |
&major_attr.attr, |
| 122 |
&minor_attr.attr, |
| 123 |
&extra_attr.attr, |
| 124 |
NULL |
| 125 |
}; |
| 126 |
|
| 127 |
static const struct attribute_group version_group = { |
| 128 |
.name = "version", |
| 129 |
.attrs = version_attrs, |
| 130 |
}; |
| 131 |
|
| 132 |
static int __init xen_sysfs_version_init(void) |
| 133 |
{ |
| 134 |
return sysfs_create_group(hypervisor_kobj, &version_group); |
| 135 |
} |
| 136 |
|
| 137 |
/* UUID */ |
| 138 |
|
| 139 |
static ssize_t uuid_show_fallback(struct hyp_sysfs_attr *attr, char *buffer) |
| 140 |
{ |
| 141 |
char *vm, *val; |
| 142 |
int ret; |
| 143 |
extern int xenstored_ready; |
| 144 |
|
| 145 |
if (!xenstored_ready) |
| 146 |
return -EBUSY; |
| 147 |
|
| 148 |
vm = xenbus_read(XBT_NIL, "vm", "", NULL); |
| 149 |
if (IS_ERR(vm)) |
| 150 |
return PTR_ERR(vm); |
| 151 |
val = xenbus_read(XBT_NIL, vm, "uuid", NULL); |
| 152 |
kfree(vm); |
| 153 |
if (IS_ERR(val)) |
| 154 |
return PTR_ERR(val); |
| 155 |
ret = sprintf(buffer, "%s\n", val); |
| 156 |
kfree(val); |
| 157 |
return ret; |
| 158 |
} |
| 159 |
|
| 160 |
static ssize_t uuid_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 161 |
{ |
| 162 |
xen_domain_handle_t uuid; |
| 163 |
int ret; |
| 164 |
ret = HYPERVISOR_xen_version(XENVER_guest_handle, uuid); |
| 165 |
if (ret) |
| 166 |
return uuid_show_fallback(attr, buffer); |
| 167 |
ret = sprintf(buffer, "%pU\n", uuid); |
| 168 |
return ret; |
| 169 |
} |
| 170 |
|
| 171 |
HYPERVISOR_ATTR_RO(uuid); |
| 172 |
|
| 173 |
static int __init xen_sysfs_uuid_init(void) |
| 174 |
{ |
| 175 |
return sysfs_create_file(hypervisor_kobj, &uuid_attr.attr); |
| 176 |
} |
| 177 |
|
| 178 |
/* xen compilation attributes */ |
| 179 |
|
| 180 |
static ssize_t compiler_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 181 |
{ |
| 182 |
int ret = -ENOMEM; |
| 183 |
struct xen_compile_info *info; |
| 184 |
|
| 185 |
info = kmalloc_obj(struct xen_compile_info); |
| 186 |
if (info) { |
| 187 |
ret = HYPERVISOR_xen_version(XENVER_compile_info, info); |
| 188 |
if (!ret) |
| 189 |
ret = sprintf(buffer, "%s\n", info->compiler); |
| 190 |
kfree(info); |
| 191 |
} |
| 192 |
|
| 193 |
return ret; |
| 194 |
} |
| 195 |
|
| 196 |
HYPERVISOR_ATTR_RO(compiler); |
| 197 |
|
| 198 |
static ssize_t compiled_by_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 199 |
{ |
| 200 |
int ret = -ENOMEM; |
| 201 |
struct xen_compile_info *info; |
| 202 |
|
| 203 |
info = kmalloc_obj(struct xen_compile_info); |
| 204 |
if (info) { |
| 205 |
ret = HYPERVISOR_xen_version(XENVER_compile_info, info); |
| 206 |
if (!ret) |
| 207 |
ret = sprintf(buffer, "%s\n", info->compile_by); |
| 208 |
kfree(info); |
| 209 |
} |
| 210 |
|
| 211 |
return ret; |
| 212 |
} |
| 213 |
|
| 214 |
HYPERVISOR_ATTR_RO(compiled_by); |
| 215 |
|
| 216 |
static ssize_t compile_date_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 217 |
{ |
| 218 |
int ret = -ENOMEM; |
| 219 |
struct xen_compile_info *info; |
| 220 |
|
| 221 |
info = kmalloc_obj(struct xen_compile_info); |
| 222 |
if (info) { |
| 223 |
ret = HYPERVISOR_xen_version(XENVER_compile_info, info); |
| 224 |
if (!ret) |
| 225 |
ret = sprintf(buffer, "%s\n", info->compile_date); |
| 226 |
kfree(info); |
| 227 |
} |
| 228 |
|
| 229 |
return ret; |
| 230 |
} |
| 231 |
|
| 232 |
HYPERVISOR_ATTR_RO(compile_date); |
| 233 |
|
| 234 |
static struct attribute *xen_compile_attrs[] = { |
| 235 |
&compiler_attr.attr, |
| 236 |
&compiled_by_attr.attr, |
| 237 |
&compile_date_attr.attr, |
| 238 |
NULL |
| 239 |
}; |
| 240 |
|
| 241 |
static const struct attribute_group xen_compilation_group = { |
| 242 |
.name = "compilation", |
| 243 |
.attrs = xen_compile_attrs, |
| 244 |
}; |
| 245 |
|
| 246 |
static int __init xen_sysfs_compilation_init(void) |
| 247 |
{ |
| 248 |
return sysfs_create_group(hypervisor_kobj, &xen_compilation_group); |
| 249 |
} |
| 250 |
|
| 251 |
/* xen properties info */ |
| 252 |
|
| 253 |
static ssize_t capabilities_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 254 |
{ |
| 255 |
int ret = -ENOMEM; |
| 256 |
char *caps; |
| 257 |
|
| 258 |
caps = kmalloc(XEN_CAPABILITIES_INFO_LEN, GFP_KERNEL); |
| 259 |
if (caps) { |
| 260 |
ret = HYPERVISOR_xen_version(XENVER_capabilities, caps); |
| 261 |
if (!ret) |
| 262 |
ret = sprintf(buffer, "%s\n", caps); |
| 263 |
kfree(caps); |
| 264 |
} |
| 265 |
|
| 266 |
return ret; |
| 267 |
} |
| 268 |
|
| 269 |
HYPERVISOR_ATTR_RO(capabilities); |
| 270 |
|
| 271 |
static ssize_t changeset_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 272 |
{ |
| 273 |
int ret = -ENOMEM; |
| 274 |
char *cset; |
| 275 |
|
| 276 |
cset = kmalloc(XEN_CHANGESET_INFO_LEN, GFP_KERNEL); |
| 277 |
if (cset) { |
| 278 |
ret = HYPERVISOR_xen_version(XENVER_changeset, cset); |
| 279 |
if (!ret) |
| 280 |
ret = sprintf(buffer, "%s\n", cset); |
| 281 |
kfree(cset); |
| 282 |
} |
| 283 |
|
| 284 |
return ret; |
| 285 |
} |
| 286 |
|
| 287 |
HYPERVISOR_ATTR_RO(changeset); |
| 288 |
|
| 289 |
static ssize_t virtual_start_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 290 |
{ |
| 291 |
int ret = -ENOMEM; |
| 292 |
struct xen_platform_parameters *parms; |
| 293 |
|
| 294 |
parms = kmalloc_obj(struct xen_platform_parameters); |
| 295 |
if (parms) { |
| 296 |
ret = HYPERVISOR_xen_version(XENVER_platform_parameters, |
| 297 |
parms); |
| 298 |
if (!ret) |
| 299 |
ret = sprintf(buffer, "%"PRI_xen_ulong"\n", |
| 300 |
parms->virt_start); |
| 301 |
kfree(parms); |
| 302 |
} |
| 303 |
|
| 304 |
return ret; |
| 305 |
} |
| 306 |
|
| 307 |
HYPERVISOR_ATTR_RO(virtual_start); |
| 308 |
|
| 309 |
static ssize_t pagesize_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 310 |
{ |
| 311 |
int ret; |
| 312 |
|
| 313 |
ret = HYPERVISOR_xen_version(XENVER_pagesize, NULL); |
| 314 |
if (ret > 0) |
| 315 |
ret = sprintf(buffer, "%x\n", ret); |
| 316 |
|
| 317 |
return ret; |
| 318 |
} |
| 319 |
|
| 320 |
HYPERVISOR_ATTR_RO(pagesize); |
| 321 |
|
| 322 |
static ssize_t xen_feature_show(int index, char *buffer) |
| 323 |
{ |
| 324 |
ssize_t ret; |
| 325 |
struct xen_feature_info info; |
| 326 |
|
| 327 |
info.submap_idx = index; |
| 328 |
ret = HYPERVISOR_xen_version(XENVER_get_features, &info); |
| 329 |
if (!ret) |
| 330 |
ret = sprintf(buffer, "%08x", info.submap); |
| 331 |
|
| 332 |
return ret; |
| 333 |
} |
| 334 |
|
| 335 |
static ssize_t features_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 336 |
{ |
| 337 |
ssize_t len; |
| 338 |
int i; |
| 339 |
|
| 340 |
len = 0; |
| 341 |
for (i = XENFEAT_NR_SUBMAPS-1; i >= 0; i--) { |
| 342 |
int ret = xen_feature_show(i, buffer + len); |
| 343 |
if (ret < 0) { |
| 344 |
if (len == 0) |
| 345 |
len = ret; |
| 346 |
break; |
| 347 |
} |
| 348 |
len += ret; |
| 349 |
} |
| 350 |
if (len > 0) |
| 351 |
buffer[len++] = '\n'; |
| 352 |
|
| 353 |
return len; |
| 354 |
} |
| 355 |
|
| 356 |
HYPERVISOR_ATTR_RO(features); |
| 357 |
|
| 358 |
static ssize_t buildid_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 359 |
{ |
| 360 |
ssize_t ret; |
| 361 |
struct xen_build_id *buildid; |
| 362 |
|
| 363 |
ret = HYPERVISOR_xen_version(XENVER_build_id, NULL); |
| 364 |
if (ret < 0) { |
| 365 |
if (ret == -EPERM) |
| 366 |
ret = sprintf(buffer, "<denied>"); |
| 367 |
return ret; |
| 368 |
} |
| 369 |
if (ret > PAGE_SIZE) |
| 370 |
return -ENOSPC; |
| 371 |
|
| 372 |
buildid = kmalloc(sizeof(*buildid) + ret, GFP_KERNEL); |
| 373 |
if (!buildid) |
| 374 |
return -ENOMEM; |
| 375 |
|
| 376 |
buildid->len = ret; |
| 377 |
ret = HYPERVISOR_xen_version(XENVER_build_id, buildid); |
| 378 |
if (ret > 0) { |
| 379 |
/* Build id is binary, not a string. */ |
| 380 |
memcpy(buffer, buildid->buf, ret); |
| 381 |
} |
| 382 |
kfree(buildid); |
| 383 |
|
| 384 |
return ret; |
| 385 |
} |
| 386 |
|
| 387 |
HYPERVISOR_ATTR_RO(buildid); |
| 388 |
|
| 389 |
static struct attribute *xen_properties_attrs[] = { |
| 390 |
&capabilities_attr.attr, |
| 391 |
&changeset_attr.attr, |
| 392 |
&virtual_start_attr.attr, |
| 393 |
&pagesize_attr.attr, |
| 394 |
&features_attr.attr, |
| 395 |
&buildid_attr.attr, |
| 396 |
NULL |
| 397 |
}; |
| 398 |
|
| 399 |
static const struct attribute_group xen_properties_group = { |
| 400 |
.name = "properties", |
| 401 |
.attrs = xen_properties_attrs, |
| 402 |
}; |
| 403 |
|
| 404 |
static int __init xen_sysfs_properties_init(void) |
| 405 |
{ |
| 406 |
return sysfs_create_group(hypervisor_kobj, &xen_properties_group); |
| 407 |
} |
| 408 |
|
| 409 |
#define FLAG_UNAME "unknown" |
| 410 |
#define FLAG_UNAME_FMT FLAG_UNAME "%02u" |
| 411 |
#define FLAG_UNAME_MAX sizeof(FLAG_UNAME "XX") |
| 412 |
#define FLAG_COUNT (sizeof(xen_start_flags) * BITS_PER_BYTE) |
| 413 |
static_assert(sizeof(xen_start_flags) <= |
| 414 |
sizeof_field(struct hyp_sysfs_attr, hyp_attr_value)); |
| 415 |
|
| 416 |
static ssize_t flag_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 417 |
{ |
| 418 |
char *p = buffer; |
| 419 |
|
| 420 |
*p++ = '0' + ((xen_start_flags & attr->hyp_attr_value) != 0); |
| 421 |
*p++ = '\n'; |
| 422 |
return p - buffer; |
| 423 |
} |
| 424 |
|
| 425 |
#define FLAG_NODE(flag, node) \ |
| 426 |
[ilog2(flag)] = { \ |
| 427 |
.attr = { .name = #node, .mode = 0444 },\ |
| 428 |
.show = flag_show, \ |
| 429 |
.hyp_attr_value = flag \ |
| 430 |
} |
| 431 |
|
| 432 |
/* |
| 433 |
* Add new, known flags here. No other changes are required, but |
| 434 |
* note that each known flag wastes one entry in flag_unames[]. |
| 435 |
* The code/complexity machinations to avoid this isn't worth it |
| 436 |
* for a few entries, but keep it in mind. |
| 437 |
*/ |
| 438 |
static struct hyp_sysfs_attr flag_attrs[FLAG_COUNT] = { |
| 439 |
FLAG_NODE(SIF_PRIVILEGED, privileged), |
| 440 |
FLAG_NODE(SIF_INITDOMAIN, initdomain) |
| 441 |
}; |
| 442 |
static struct attribute_group xen_flags_group = { |
| 443 |
.name = "start_flags", |
| 444 |
.attrs = (struct attribute *[FLAG_COUNT + 1]){} |
| 445 |
}; |
| 446 |
static char flag_unames[FLAG_COUNT][FLAG_UNAME_MAX]; |
| 447 |
|
| 448 |
static int __init xen_sysfs_flags_init(void) |
| 449 |
{ |
| 450 |
for (unsigned fnum = 0; fnum != FLAG_COUNT; fnum++) { |
| 451 |
if (likely(flag_attrs[fnum].attr.name == NULL)) { |
| 452 |
sprintf(flag_unames[fnum], FLAG_UNAME_FMT, fnum); |
| 453 |
flag_attrs[fnum].attr.name = flag_unames[fnum]; |
| 454 |
flag_attrs[fnum].attr.mode = 0444; |
| 455 |
flag_attrs[fnum].show = flag_show; |
| 456 |
flag_attrs[fnum].hyp_attr_value = 1 << fnum; |
| 457 |
} |
| 458 |
xen_flags_group.attrs[fnum] = &flag_attrs[fnum].attr; |
| 459 |
} |
| 460 |
return sysfs_create_group(hypervisor_kobj, &xen_flags_group); |
| 461 |
} |
| 462 |
|
| 463 |
#ifdef CONFIG_XEN_HAVE_VPMU |
| 464 |
struct pmu_mode { |
| 465 |
const char *name; |
| 466 |
uint32_t mode; |
| 467 |
}; |
| 468 |
|
| 469 |
static struct pmu_mode pmu_modes[] = { |
| 470 |
{"off", XENPMU_MODE_OFF}, |
| 471 |
{"self", XENPMU_MODE_SELF}, |
| 472 |
{"hv", XENPMU_MODE_HV}, |
| 473 |
{"all", XENPMU_MODE_ALL} |
| 474 |
}; |
| 475 |
|
| 476 |
static ssize_t pmu_mode_store(struct hyp_sysfs_attr *attr, |
| 477 |
const char *buffer, size_t len) |
| 478 |
{ |
| 479 |
int ret; |
| 480 |
struct xen_pmu_params xp; |
| 481 |
int i; |
| 482 |
|
| 483 |
for (i = 0; i < ARRAY_SIZE(pmu_modes); i++) { |
| 484 |
if (strncmp(buffer, pmu_modes[i].name, len - 1) == 0) { |
| 485 |
xp.val = pmu_modes[i].mode; |
| 486 |
break; |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
if (i == ARRAY_SIZE(pmu_modes)) |
| 491 |
return -EINVAL; |
| 492 |
|
| 493 |
xp.version.maj = XENPMU_VER_MAJ; |
| 494 |
xp.version.min = XENPMU_VER_MIN; |
| 495 |
ret = HYPERVISOR_xenpmu_op(XENPMU_mode_set, &xp); |
| 496 |
if (ret) |
| 497 |
return ret; |
| 498 |
|
| 499 |
return len; |
| 500 |
} |
| 501 |
|
| 502 |
static ssize_t pmu_mode_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 503 |
{ |
| 504 |
int ret; |
| 505 |
struct xen_pmu_params xp; |
| 506 |
int i; |
| 507 |
uint32_t mode; |
| 508 |
|
| 509 |
xp.version.maj = XENPMU_VER_MAJ; |
| 510 |
xp.version.min = XENPMU_VER_MIN; |
| 511 |
ret = HYPERVISOR_xenpmu_op(XENPMU_mode_get, &xp); |
| 512 |
if (ret) |
| 513 |
return ret; |
| 514 |
|
| 515 |
mode = (uint32_t)xp.val; |
| 516 |
for (i = 0; i < ARRAY_SIZE(pmu_modes); i++) { |
| 517 |
if (mode == pmu_modes[i].mode) |
| 518 |
return sprintf(buffer, "%s\n", pmu_modes[i].name); |
| 519 |
} |
| 520 |
|
| 521 |
return -EINVAL; |
| 522 |
} |
| 523 |
HYPERVISOR_ATTR_RW(pmu_mode); |
| 524 |
|
| 525 |
static ssize_t pmu_features_store(struct hyp_sysfs_attr *attr, |
| 526 |
const char *buffer, size_t len) |
| 527 |
{ |
| 528 |
int ret; |
| 529 |
uint32_t features; |
| 530 |
struct xen_pmu_params xp; |
| 531 |
|
| 532 |
ret = kstrtou32(buffer, 0, &features); |
| 533 |
if (ret) |
| 534 |
return ret; |
| 535 |
|
| 536 |
xp.val = features; |
| 537 |
xp.version.maj = XENPMU_VER_MAJ; |
| 538 |
xp.version.min = XENPMU_VER_MIN; |
| 539 |
ret = HYPERVISOR_xenpmu_op(XENPMU_feature_set, &xp); |
| 540 |
if (ret) |
| 541 |
return ret; |
| 542 |
|
| 543 |
return len; |
| 544 |
} |
| 545 |
|
| 546 |
static ssize_t pmu_features_show(struct hyp_sysfs_attr *attr, char *buffer) |
| 547 |
{ |
| 548 |
int ret; |
| 549 |
struct xen_pmu_params xp; |
| 550 |
|
| 551 |
xp.version.maj = XENPMU_VER_MAJ; |
| 552 |
xp.version.min = XENPMU_VER_MIN; |
| 553 |
ret = HYPERVISOR_xenpmu_op(XENPMU_feature_get, &xp); |
| 554 |
if (ret) |
| 555 |
return ret; |
| 556 |
|
| 557 |
return sprintf(buffer, "0x%x\n", (uint32_t)xp.val); |
| 558 |
} |
| 559 |
HYPERVISOR_ATTR_RW(pmu_features); |
| 560 |
|
| 561 |
static struct attribute *xen_pmu_attrs[] = { |
| 562 |
&pmu_mode_attr.attr, |
| 563 |
&pmu_features_attr.attr, |
| 564 |
NULL |
| 565 |
}; |
| 566 |
|
| 567 |
static const struct attribute_group xen_pmu_group = { |
| 568 |
.name = "pmu", |
| 569 |
.attrs = xen_pmu_attrs, |
| 570 |
}; |
| 571 |
|
| 572 |
static int __init xen_sysfs_pmu_init(void) |
| 573 |
{ |
| 574 |
return sysfs_create_group(hypervisor_kobj, &xen_pmu_group); |
| 575 |
} |
| 576 |
#endif |
| 577 |
|
| 578 |
static int __init hyper_sysfs_init(void) |
| 579 |
{ |
| 580 |
int ret; |
| 581 |
|
| 582 |
if (!xen_domain()) |
| 583 |
return -ENODEV; |
| 584 |
|
| 585 |
ret = xen_sysfs_type_init(); |
| 586 |
if (ret) |
| 587 |
goto out; |
| 588 |
ret = xen_sysfs_guest_type_init(); |
| 589 |
if (ret) |
| 590 |
goto guest_type_out; |
| 591 |
ret = xen_sysfs_version_init(); |
| 592 |
if (ret) |
| 593 |
goto version_out; |
| 594 |
ret = xen_sysfs_compilation_init(); |
| 595 |
if (ret) |
| 596 |
goto comp_out; |
| 597 |
ret = xen_sysfs_uuid_init(); |
| 598 |
if (ret) |
| 599 |
goto uuid_out; |
| 600 |
ret = xen_sysfs_properties_init(); |
| 601 |
if (ret) |
| 602 |
goto prop_out; |
| 603 |
ret = xen_sysfs_flags_init(); |
| 604 |
if (ret) |
| 605 |
goto flags_out; |
| 606 |
#ifdef CONFIG_XEN_HAVE_VPMU |
| 607 |
if (xen_initial_domain()) { |
| 608 |
ret = xen_sysfs_pmu_init(); |
| 609 |
if (ret) { |
| 610 |
sysfs_remove_group(hypervisor_kobj, &xen_flags_group); |
| 611 |
goto flags_out; |
| 612 |
} |
| 613 |
} |
| 614 |
#endif |
| 615 |
goto out; |
| 616 |
|
| 617 |
flags_out: |
| 618 |
sysfs_remove_group(hypervisor_kobj, &xen_properties_group); |
| 619 |
prop_out: |
| 620 |
sysfs_remove_file(hypervisor_kobj, &uuid_attr.attr); |
| 621 |
uuid_out: |
| 622 |
sysfs_remove_group(hypervisor_kobj, &xen_compilation_group); |
| 623 |
comp_out: |
| 624 |
sysfs_remove_group(hypervisor_kobj, &version_group); |
| 625 |
version_out: |
| 626 |
sysfs_remove_file(hypervisor_kobj, &guest_type_attr.attr); |
| 627 |
guest_type_out: |
| 628 |
sysfs_remove_file(hypervisor_kobj, &type_attr.attr); |
| 629 |
out: |
| 630 |
return ret; |
| 631 |
} |
| 632 |
device_initcall(hyper_sysfs_init); |
| 633 |
|
| 634 |
static ssize_t hyp_sysfs_show(struct kobject *kobj, |
| 635 |
struct attribute *attr, |
| 636 |
char *buffer) |
| 637 |
{ |
| 638 |
struct hyp_sysfs_attr *hyp_attr; |
| 639 |
hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr); |
| 640 |
if (hyp_attr->show) |
| 641 |
return hyp_attr->show(hyp_attr, buffer); |
| 642 |
return 0; |
| 643 |
} |
| 644 |
|
| 645 |
static ssize_t hyp_sysfs_store(struct kobject *kobj, |
| 646 |
struct attribute *attr, |
| 647 |
const char *buffer, |
| 648 |
size_t len) |
| 649 |
{ |
| 650 |
struct hyp_sysfs_attr *hyp_attr; |
| 651 |
hyp_attr = container_of(attr, struct hyp_sysfs_attr, attr); |
| 652 |
if (hyp_attr->store) |
| 653 |
return hyp_attr->store(hyp_attr, buffer, len); |
| 654 |
return 0; |
| 655 |
} |
| 656 |
|
| 657 |
static const struct sysfs_ops hyp_sysfs_ops = { |
| 658 |
.show = hyp_sysfs_show, |
| 659 |
.store = hyp_sysfs_store, |
| 660 |
}; |
| 661 |
|
| 662 |
static const struct kobj_type hyp_sysfs_kobj_type = { |
| 663 |
.sysfs_ops = &hyp_sysfs_ops, |
| 664 |
}; |
| 665 |
|
| 666 |
static int __init hypervisor_subsys_init(void) |
| 667 |
{ |
| 668 |
if (!xen_domain()) |
| 669 |
return -ENODEV; |
| 670 |
|
| 671 |
hypervisor_kobj->ktype = &hyp_sysfs_kobj_type; |
| 672 |
return 0; |
| 673 |
} |
| 674 |
device_initcall(hypervisor_subsys_init); |