Linux Audio

Check our new training course

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