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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/*
* The PCI Library -- Hurd access via RPCs
*
* Copyright (c) 2017 Joan Lledó <jlledom@member.fsf.org>
*
* Can be freely distributed and used under the terms of the GNU GPL v2+.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#define _GNU_SOURCE
#include "internal.h"
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <string.h>
#include <hurd.h>
#include <hurd/pci.h>
#include <hurd/paths.h>
/* Server path */
#define _SERVERS_BUS_PCI _SERVERS_BUS "/pci"
/* File names */
#define FILE_CONFIG_NAME "config"
#define FILE_ROM_NAME "rom"
/* Level in the fs tree */
typedef enum
{
LEVEL_NONE,
LEVEL_DOMAIN,
LEVEL_BUS,
LEVEL_DEV,
LEVEL_FUNC
} tree_level;
/* Check whether there's a pci server */
static int
hurd_detect(struct pci_access *a)
{
int err;
struct stat st;
err = stat(_SERVERS_BUS_PCI, &st);
if (err)
{
a->error("Could not open file `%s'", _SERVERS_BUS_PCI);
return 0;
}
/* The node must be a directory and a translator */
return S_ISDIR(st.st_mode) && ((st.st_mode & S_ITRANS) == S_IROOT);
}
/* Empty callbacks, we don't need any special init or cleanup */
static void
hurd_init(struct pci_access *a UNUSED)
{
}
static void
hurd_cleanup(struct pci_access *a UNUSED)
{
}
/* Each device has its own server path. Allocate space for the port. */
static void
hurd_init_dev(struct pci_dev *d)
{
d->backend_data = pci_malloc(d->access, sizeof(mach_port_t));
*((mach_port_t *) d->backend_data) = MACH_PORT_NULL;
}
/* Deallocate the port and free its space */
static void
hurd_cleanup_dev(struct pci_dev *d)
{
mach_port_t device_port;
device_port = *((mach_port_t *) d->backend_data);
mach_port_deallocate(mach_task_self(), device_port);
pci_mfree(d->backend_data);
d->backend_data = NULL;
}
static mach_port_t
device_port_lookup(struct pci_dev *d)
{
char server[NAME_MAX];
mach_port_t device_port = *((mach_port_t *) d->backend_data);
if (device_port != MACH_PORT_NULL)
return device_port;
snprintf(server, NAME_MAX, "%s/%04x/%02x/%02x/%01u/%s",
_SERVERS_BUS_PCI, d->domain, d->bus, d->dev, d->func,
FILE_CONFIG_NAME);
device_port = file_name_lookup(server, 0, 0);
if (device_port == MACH_PORT_NULL)
d->access->error("Cannot find the PCI arbiter");
*((mach_port_t *) d->backend_data) = device_port;
return device_port;
}
/* Walk through the FS tree to see what is allowed for us */
static void
enum_devices(const char *parent, struct pci_access *a, int domain, int bus,
int dev, int func, tree_level lev)
{
int ret;
DIR *dir;
struct dirent *entry;
char path[NAME_MAX];
struct pci_dev *d;
dir = opendir(parent);
if (!dir)
{
if (errno == EPERM || errno == EACCES)
/* The client lacks the permissions to access this function, skip */
return;
else
a->error("Cannot open directory: %s (%s)", parent, strerror(errno));
}
while ((entry = readdir(dir)) != 0)
{
snprintf(path, NAME_MAX, "%s/%s", parent, entry->d_name);
if (entry->d_type == DT_DIR)
{
if (!strncmp(entry->d_name, ".", NAME_MAX)
|| !strncmp(entry->d_name, "..", NAME_MAX))
continue;
errno = 0;
ret = strtol(entry->d_name, 0, 16);
if (errno)
{
if (closedir(dir) < 0)
a->warning("Cannot close directory: %s (%s)", parent,
strerror(errno));
a->error("Wrong directory name: %s (number expected) probably "
"not connected to an arbiter", entry->d_name);
}
/*
* We found a valid directory.
* Update the address and switch to the next level.
*/
switch (lev)
{
case LEVEL_DOMAIN:
domain = ret;
break;
case LEVEL_BUS:
bus = ret;
break;
case LEVEL_DEV:
dev = ret;
break;
case LEVEL_FUNC:
func = ret;
break;
default:
if (closedir(dir) < 0)
a->warning("Cannot close directory: %s (%s)", parent,
strerror(errno));
a->error("Wrong directory tree, probably not connected to an arbiter");
}
enum_devices(path, a, domain, bus, dev, func, lev + 1);
}
else
{
if (strncmp(entry->d_name, FILE_CONFIG_NAME, NAME_MAX))
/* We are looking for the config file */
continue;
/* We found an available virtual device, add it to our list */
d = pci_alloc_dev(a);
d->domain = domain;
d->bus = bus;
d->dev = dev;
d->func = func;
pci_link_dev(a, d);
}
}
if (closedir(dir) < 0)
a->error("Cannot close directory: %s (%s)", parent, strerror(errno));
}
/* Enumerate devices */
static void
hurd_scan(struct pci_access *a)
{
enum_devices(_SERVERS_BUS_PCI, a, -1, -1, -1, -1, LEVEL_DOMAIN);
}
/*
* Read `len' bytes to `buf'.
*
* Returns error when the number of read bytes does not match `len'.
*/
static int
hurd_read(struct pci_dev *d, int pos, byte * buf, int len)
{
int err;
size_t nread;
char *data;
mach_port_t device_port = device_port_lookup(d);
if (len > 4)
return pci_generic_block_read(d, pos, buf, len);
data = (char *) buf;
err = pci_conf_read(device_port, pos, &data, &nread, len);
if (data != (char *) buf)
{
if (nread > (size_t) len) /* Sanity check for bogus server. */
{
vm_deallocate(mach_task_self(), (vm_address_t) data, nread);
return 0;
}
memcpy(buf, data, nread);
vm_deallocate(mach_task_self(), (vm_address_t) data, nread);
}
return !err && nread == (size_t) len;
}
/*
* Write `len' bytes from `buf'.
*
* Returns error when the number of written bytes does not match `len'.
*/
static int
hurd_write(struct pci_dev *d, int pos, byte * buf, int len)
{
int err;
size_t nwrote;
mach_port_t device_port = device_port_lookup(d);
if (len > 4)
return pci_generic_block_write(d, pos, buf, len);
err = pci_conf_write(device_port, pos, (char *) buf, len, &nwrote);
return !err && nwrote == (size_t) len;
}
/* Get requested info from the server */
static int
hurd_fill_regions(struct pci_dev *d)
{
mach_port_t device_port = device_port_lookup(d);
struct pci_bar regions[6];
char *buf = (char *) ®ions;
size_t size = sizeof(regions);
int err = pci_get_dev_regions(device_port, &buf, &size);
if (err)
return 0;
if ((char *) ®ions != buf)
{
/* Sanity check for bogus server. */
if (size > sizeof(regions))
{
vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
return 0;
}
memcpy(®ions, buf, size);
vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
}
for (int i = 0; i < 6; i++)
{
if (regions[i].size == 0)
continue;
d->base_addr[i] = regions[i].base_addr;
d->base_addr[i] |= regions[i].is_IO;
d->base_addr[i] |= regions[i].is_64 << 2;
d->base_addr[i] |= regions[i].is_prefetchable << 3;
d->size[i] = regions[i].size;
}
return 1;
}
static int
hurd_fill_rom(struct pci_dev *d)
{
struct pci_xrom_bar rom;
mach_port_t device_port = device_port_lookup(d);
char *buf = (char *) &rom;
size_t size = sizeof(rom);
int err = pci_get_dev_rom(device_port, &buf, &size);
if (err)
return 0;
if ((char *) &rom != buf)
{
/* Sanity check for bogus server. */
if (size > sizeof(rom))
{
vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
return 0;
}
memcpy(&rom, buf, size);
vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
}
d->rom_base_addr = rom.base_addr;
d->rom_size = rom.size;
return 1;
}
static void
hurd_fill_info(struct pci_dev *d, unsigned int flags)
{
if (!d->access->buscentric)
{
if (want_fill(d, flags, PCI_FILL_BASES | PCI_FILL_SIZES))
{
if (hurd_fill_regions(d))
clear_fill(d, PCI_FILL_BASES | PCI_FILL_SIZES);
}
if (want_fill(d, flags, PCI_FILL_ROM_BASE))
{
if (hurd_fill_rom(d))
clear_fill(d, PCI_FILL_ROM_BASE);
}
}
pci_generic_fill_info(d, flags);
}
struct pci_methods pm_hurd = {
"hurd",
"Hurd access using RPCs",
NULL, /* config */
hurd_detect,
hurd_init,
hurd_cleanup,
hurd_scan,
hurd_fill_info,
hurd_read,
hurd_write,
NULL, /* read_vpd */
hurd_init_dev,
hurd_cleanup_dev
};
|