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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
#include <Python.h>
#include "muser.h"
#include <assert.h>
static PyObject *region_access_callbacks[LM_DEV_NUM_REGS] = { 0 };
static int
handle_read(char *dst, PyObject *result, int count)
{
int i;
PyArg_Parse(result, "i", &i);
Py_DECREF(result);
memcpy(dst, &i, count);
return 0;
}
/*
* Function callback called by libmuser. We then call the Python function.
*
* FIXME need a way to provide private pointer.
*/
static ssize_t
region_access_wrap(void *pvt, char * const buf, size_t count, loff_t offset,
const bool is_write, int region)
{
PyObject *arglist;
PyObject *result = NULL;
uint64_t _buf = { 0 };
if (region_access_callbacks[region] == NULL)
{
fprintf(stderr, "FIXME no callback for region %d\n", region);
return -1;
}
if (is_write)
{
memcpy(&_buf, buf, count);
}
/* FIXME not sure about type of count and offset */
arglist = Py_BuildValue("IKIKi", pvt, _buf, count, offset, is_write);
if (arglist == NULL)
{
fprintf(stderr, "FIXME failed to build func args\n");
return -1;
}
result = PyEval_CallObject(region_access_callbacks[region], arglist);
Py_DECREF(arglist);
if (result == NULL)
{
fprintf(stderr, "FIXME failed to call fn for region %d\n", region);
return -1;
}
if (!is_write)
{
if (handle_read(buf, result, count))
{
return -1;
}
}
return count;
}
#define REGION_WRAP(region) \
static ssize_t \
r_ ## region ## _wrap(void *p, char * const b, size_t c, loff_t o, \
const bool w) \
{ \
return region_access_wrap(p, b, c, o, w, region); \
}
REGION_WRAP(0)
REGION_WRAP(1)
REGION_WRAP(2)
REGION_WRAP(3)
REGION_WRAP(4)
REGION_WRAP(5)
REGION_WRAP(6)
REGION_WRAP(7)
REGION_WRAP(8)
static ssize_t (*region_access_wraps[LM_DEV_NUM_REGS])(void*, char * const, size_t, loff_t, const bool) = {
r_0_wrap,
r_1_wrap,
r_2_wrap,
r_3_wrap,
r_4_wrap,
r_5_wrap,
r_6_wrap,
r_7_wrap,
r_8_wrap};
struct _region_info {
char *perm;
unsigned int size;
PyObject *fn;
};
static const struct _region_info _0_ri = { 0 };
static PyObject *log_fn = NULL;
static lm_log_lvl_t log_lvl = LM_ERR;
static void _log_fn(void *pvt, const char *const msg)
{
PyObject *arglist;
PyObject *result = NULL;
arglist = Py_BuildValue("(s)", msg);
result = PyEval_CallObject(log_fn, arglist);
Py_DECREF(arglist);
if (result != NULL)
{
Py_DECREF(result);
}
}
static PyObject *
libmuser_run(PyObject *self, PyObject *args, PyObject *kwargs)
{
static char *kwlist[] = {"vid", "did", "uuid", "log", "log_lvl",
"bar0", "bar1", "bar2", "bar3", "bar4", "bar5", "rom", "cfg", "vga",
"intx", "msi", "msix", "err", "req",
NULL};
int err;
lm_dev_info_t dev_info = { 0 };
int i;
struct _region_info _ri[LM_DEV_NUM_REGS] = { 0 };
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "HHs|Oi(sIO)(sIO)(sIO)(sIO)(sIO)(sIO)(sIO)(sIO)(sIO)IIIII", kwlist,
&dev_info.pci_info.id.vid,
&dev_info.pci_info.id.did,
&dev_info.uuid,
&log_fn, &log_lvl,
&_ri[0].perm, &_ri[0].size, &_ri[0].fn,
&_ri[1].perm, &_ri[1].size, &_ri[1].fn,
&_ri[2].perm, &_ri[2].size, &_ri[2].fn,
&_ri[3].perm, &_ri[3].size, &_ri[3].fn,
&_ri[4].perm, &_ri[4].size, &_ri[4].fn,
&_ri[5].perm, &_ri[5].size, &_ri[5].fn,
&_ri[6].perm, &_ri[6].size, &_ri[6].fn,
&_ri[7].perm, &_ri[7].size, &_ri[7].fn,
&_ri[8].perm, &_ri[8].size, &_ri[8].fn,
&dev_info.pci_info.irq_count[0],
&dev_info.pci_info.irq_count[1],
&dev_info.pci_info.irq_count[2],
&dev_info.pci_info.irq_count[3],
&dev_info.pci_info.irq_count[4]))
{
return NULL;
}
for (i = 0; i < LM_DEV_NUM_REGS; i++)
{
int j;
uint32_t flags = 0;
if (i == LM_DEV_CFG_REG_IDX && !memcmp(&_0_ri, &_ri[i], sizeof _0_ri))
{
continue;
}
if (_ri[i].perm != NULL)
{
for (j = 0; j < strlen(_ri[i].perm); j++)
{
if (_ri[i].perm[j] == 'r')
{
flags |= LM_REG_FLAG_READ;
}
else if (_ri[i].perm[j] == 'w')
{
flags |= LM_REG_FLAG_WRITE;
}
else
{
/* FIXME shouldn't print to stderr */
fprintf(stderr, "bad permission '%c'\n", _ri[i].perm[j]);
return NULL;
}
}
}
region_access_callbacks[i] = _ri[i].fn;
dev_info.pci_info.reg_info[i].flags = flags;
dev_info.pci_info.reg_info[i].size = _ri[i].size;
dev_info.pci_info.reg_info[i].fn = region_access_wraps[i];
}
if (log_fn != NULL)
{
if (!PyCallable_Check(log_fn))
{
return NULL;
}
dev_info.log = _log_fn;
dev_info.log_lvl = log_lvl;
}
err = lm_ctx_run(&dev_info);
return Py_BuildValue("i", err);
}
static PyMethodDef LibmuserMethods[] = {
{"run", (PyCFunction)libmuser_run, METH_VARARGS | METH_KEYWORDS, "runs a device"},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
initmuser(void)
{
(void) Py_InitModule("muser", LibmuserMethods);
}
/* ex: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab: */
|