summaryrefslogtreecommitdiff
path: root/drivers/xen/xenfs/xensyms.c
blob: c6c73a33c44d556c0ac5ebf53d6b5e078bfb6dc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// SPDX-License-Identifier: GPL-2.0
#include <linux/init.h>
#include <linux/seq_file.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/proc_fs.h>
#include <linux/slab.h>
#include <xen/interface/platform.h>
#include <asm/xen/hypercall.h>
#include <xen/xen-ops.h>
#include "xenfs.h"


#define XEN_KSYM_NAME_LEN 127 /* Hypervisor may have different name length */

struct xensyms {
	struct xen_platform_op op;
	char *name;
	uint32_t namelen;
};

/* Grab next output page from the hypervisor */
static int xensyms_next_sym(struct xensyms *xs)
{
	int ret;
	struct xenpf_symdata *symdata = &xs->op.u.symdata;
	uint64_t symnum;

	memset(xs->name, 0, xs->namelen);
	symdata->namelen = xs->namelen;

	symnum = symdata->symnum;

	ret = HYPERVISOR_platform_op(&xs->op);
	if (ret < 0)
		return ret;

	/*
	 * If hypervisor's symbol didn't fit into the buffer then allocate
	 * a larger buffer and try again.
	 */
	if (unlikely(symdata->namelen > xs->namelen)) {
		kfree(xs->name);

		xs->namelen = symdata->namelen;
		xs->name = kzalloc(xs->namelen, GFP_KERNEL);
		if (!xs->name)
			return -ENOMEM;

		set_xen_guest_handle(symdata->name, xs->name);
		symdata->symnum--; /* Rewind */

		ret = HYPERVISOR_platform_op(&xs->op);
		if (ret < 0)
			return ret;
	}

	if (symdata->symnum == symnum)
		/* End of symbols */
		return 1;

	return 0;
}

static void *xensyms_start(struct seq_file *m, loff_t *pos)
{
	struct xensyms *xs = (struct xensyms *)m->private;

	xs->op.u.symdata.symnum = *pos;

	if (xensyms_next_sym(xs))
		return NULL;

	return m->private;
}

static void *xensyms_next(struct seq_file *m, void *p, loff_t *pos)
{
	struct xensyms *xs = (struct xensyms *)m->private;

	xs->op.u.symdata.symnum = ++(*pos);

	if (xensyms_next_sym(xs))
		return NULL;

	return p;
}

static int xensyms_show(struct seq_file *m, void *p)
{
	struct xensyms *xs = (struct xensyms *)m->private;
	struct xenpf_symdata *symdata = &xs->op.u.symdata;

	seq_printf(m, "%016llx %c %s\n", symdata->address,
		   symdata->type, xs->name);

	return 0;
}

static void xensyms_stop(struct seq_file *m, void *p)
{
}

static const struct seq_operations xensyms_seq_ops = {
	.start = xensyms_start,
	.next = xensyms_next,
	.show = xensyms_show,
	.stop = xensyms_stop,
};

static int xensyms_open(struct inode *inode, struct file *file)
{
	struct seq_file *m;
	struct xensyms *xs;
	int ret;

	ret = seq_open_private(file, &xensyms_seq_ops,
			       sizeof(struct xensyms));
	if (ret)
		return ret;

	m = file->private_data;
	xs = (struct xensyms *)m->private;

	xs->namelen = XEN_KSYM_NAME_LEN + 1;
	xs->name = kzalloc(xs->namelen, GFP_KERNEL);
	if (!xs->name) {
		seq_release_private(inode, file);
		return -ENOMEM;
	}
	set_xen_guest_handle(xs->op.u.symdata.name, xs->name);
	xs->op.cmd = XENPF_get_symbol;
	xs->op.u.symdata.namelen = xs->namelen;

	return 0;
}

static int xensyms_release(struct inode *inode, struct file *file)
{
	struct seq_file *m = file->private_data;
	struct xensyms *xs = (struct xensyms *)m->private;

	kfree(xs->name);
	return seq_release_private(inode, file);
}

const struct file_operations xensyms_ops = {
	.open = xensyms_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = xensyms_release
};