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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* ksyms driver - exports a single symbol/string table for the kernel
* by concatenating all the module symbol/string tables.
*/
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/cmn_err.h>
#include <sys/uio.h>
#include <sys/kmem.h>
#include <sys/cred.h>
#include <sys/mman.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/conf.h>
#include <sys/debug.h>
#include <sys/kobj.h>
#include <sys/ksyms.h>
#include <sys/vmsystm.h>
#include <vm/seg_vn.h>
#include <sys/atomic.h>
#include <sys/compress.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/list.h>
typedef struct ksyms_image {
caddr_t ksyms_base; /* base address of image */
size_t ksyms_size; /* size of image */
} ksyms_image_t;
typedef struct ksyms_buflist {
list_node_t buflist_node;
char buf[1];
} ksyms_buflist_t;
typedef struct ksyms_buflist_hdr {
list_t blist;
int nchunks;
ksyms_buflist_t *cur;
size_t curbuf_off;
} ksyms_buflist_hdr_t;
#define BUF_SIZE (PAGESIZE - (size_t)offsetof(ksyms_buflist_t, buf))
int nksyms_clones; /* tunable: max clones of this device */
static ksyms_image_t *ksyms_clones; /* clone device array */
static dev_info_t *ksyms_devi;
static void
ksyms_bcopy(const void *srcptr, void *ptr, size_t rsize)
{
size_t sz;
const char *src = (const char *)srcptr;
ksyms_buflist_hdr_t *hptr = (ksyms_buflist_hdr_t *)ptr;
if (hptr->cur == NULL)
return;
while (rsize) {
sz = MIN(rsize, (BUF_SIZE - hptr->curbuf_off));
bcopy(src, (hptr->cur->buf + hptr->curbuf_off), sz);
hptr->curbuf_off += sz;
if (hptr->curbuf_off == BUF_SIZE) {
hptr->curbuf_off = 0;
hptr->cur = list_next(&hptr->blist, hptr->cur);
if (hptr->cur == NULL)
break;
}
src += sz;
rsize -= sz;
}
}
static void
ksyms_buflist_free(ksyms_buflist_hdr_t *hdr)
{
ksyms_buflist_t *list;
while (list = list_head(&hdr->blist)) {
list_remove(&hdr->blist, list);
kmem_free(list, PAGESIZE);
}
list_destroy(&hdr->blist);
hdr->cur = NULL;
}
/*
* Allocate 'size'(rounded to BUF_SIZE) bytes in chunks of BUF_SIZE, and
* add it to the buf list.
* Returns the total size rounded to BUF_SIZE.
*/
static size_t
ksyms_buflist_alloc(ksyms_buflist_hdr_t *hdr, size_t size)
{
int chunks, i;
ksyms_buflist_t *list;
chunks = howmany(size, BUF_SIZE);
if (hdr->nchunks >= chunks)
return (hdr->nchunks * BUF_SIZE);
/*
* Allocate chunks - hdr->nchunks buffers and add them to
* the list.
*/
for (i = chunks - hdr->nchunks; i > 0; i--) {
if ((list = kmem_alloc(PAGESIZE, KM_NOSLEEP)) == NULL)
break;
list_insert_tail(&hdr->blist, list);
}
/*
* If we are running short of memory, free memory allocated till now
* and return.
*/
if (i > 0) {
ksyms_buflist_free(hdr);
return (0);
}
hdr->nchunks = chunks;
hdr->cur = list_head(&hdr->blist);
hdr->curbuf_off = 0;
return (chunks * BUF_SIZE);
}
/*
* rlen is in multiples of PAGESIZE
*/
static char *
ksyms_asmap(struct as *as, size_t rlen)
{
char *addr = NULL;
as_rangelock(as);
map_addr(&addr, rlen, 0, 1, 0);
if (addr == NULL || as_map(as, addr, rlen, segvn_create, zfod_argsp)) {
as_rangeunlock(as);
return (NULL);
}
as_rangeunlock(as);
return (addr);
}
static char *
ksyms_mapin(ksyms_buflist_hdr_t *hdr, size_t size)
{
size_t sz, rlen = roundup(size, PAGESIZE);
struct as *as = curproc->p_as;
char *addr, *raddr;
ksyms_buflist_t *list = list_head(&hdr->blist);
if ((addr = ksyms_asmap(as, rlen)) == NULL)
return (NULL);
raddr = addr;
while (size > 0 && list != NULL) {
sz = MIN(size, BUF_SIZE);
if (copyout(list->buf, raddr, sz)) {
(void) as_unmap(as, addr, rlen);
return (NULL);
}
list = list_next(&hdr->blist, list);
raddr += sz;
size -= sz;
}
return (addr);
}
/*
* Copy a snapshot of the kernel symbol table into the user's address space.
* The symbol table is copied in fragments so that we do not have to
* do a large kmem_alloc() which could fail/block if the kernel memory is
* fragmented.
*/
/* ARGSUSED */
static int
ksyms_open(dev_t *devp, int flag, int otyp, struct cred *cred)
{
minor_t clone;
size_t size = 0;
size_t realsize;
char *addr;
void *hptr = NULL;
ksyms_buflist_hdr_t hdr;
bzero(&hdr, sizeof (struct ksyms_buflist_hdr));
list_create(&hdr.blist, PAGESIZE,
offsetof(ksyms_buflist_t, buflist_node));
if (getminor(*devp) != 0)
return (ENXIO);
for (;;) {
realsize = ksyms_snapshot(ksyms_bcopy, hptr, size);
if (realsize <= size)
break;
size = realsize;
size = ksyms_buflist_alloc(&hdr, size);
if (size == 0)
return (ENOMEM);
hptr = (void *)&hdr;
}
addr = ksyms_mapin(&hdr, realsize);
ksyms_buflist_free(&hdr);
if (addr == NULL)
return (EOVERFLOW);
/*
* Reserve a clone entry. Note that we don't use clone 0
* since that's the "real" minor number.
*/
for (clone = 1; clone < nksyms_clones; clone++) {
if (atomic_cas_ptr(&ksyms_clones[clone].ksyms_base, 0, addr) ==
0) {
ksyms_clones[clone].ksyms_size = realsize;
*devp = makedevice(getemajor(*devp), clone);
(void) ddi_prop_update_int(*devp, ksyms_devi,
"size", realsize);
modunload_disable();
return (0);
}
}
cmn_err(CE_NOTE, "ksyms: too many open references");
(void) as_unmap(curproc->p_as, addr, roundup(realsize, PAGESIZE));
return (ENXIO);
}
/* ARGSUSED */
static int
ksyms_close(dev_t dev, int flag, int otyp, struct cred *cred)
{
minor_t clone = getminor(dev);
(void) as_unmap(curproc->p_as, ksyms_clones[clone].ksyms_base,
roundup(ksyms_clones[clone].ksyms_size, PAGESIZE));
ksyms_clones[clone].ksyms_base = 0;
modunload_enable();
(void) ddi_prop_remove(dev, ksyms_devi, "size");
return (0);
}
static int
ksyms_symtbl_copy(ksyms_image_t *kip, struct uio *uio, size_t len)
{
char *buf;
int error = 0;
caddr_t base;
off_t off = uio->uio_offset;
size_t size;
/*
* The symbol table is stored in the user address space,
* so we have to copy it into the kernel first,
* then copy it back out to the specified user address.
*/
buf = kmem_alloc(PAGESIZE, KM_SLEEP);
base = kip->ksyms_base + off;
while (len) {
size = MIN(PAGESIZE, len);
if (copyin(base, buf, size))
error = EFAULT;
else
error = uiomove(buf, size, UIO_READ, uio);
if (error)
break;
len -= size;
base += size;
}
kmem_free(buf, PAGESIZE);
return (error);
}
/* ARGSUSED */
static int
ksyms_read(dev_t dev, struct uio *uio, struct cred *cred)
{
ksyms_image_t *kip = &ksyms_clones[getminor(dev)];
off_t off = uio->uio_offset;
size_t len = uio->uio_resid;
if (off < 0 || off > kip->ksyms_size)
return (EFAULT);
if (len > kip->ksyms_size - off)
len = kip->ksyms_size - off;
if (len == 0)
return (0);
return (ksyms_symtbl_copy(kip, uio, len));
}
/* ARGSUSED */
static int
ksyms_segmap(dev_t dev, off_t off, struct as *as, caddr_t *addrp, off_t len,
uint_t prot, uint_t maxprot, uint_t flags, struct cred *cred)
{
ksyms_image_t *kip = &ksyms_clones[getminor(dev)];
int error = 0;
char *addr = NULL;
size_t rlen = 0;
struct iovec aiov;
struct uio auio;
if (flags & MAP_FIXED)
return (ENOTSUP);
if (off < 0 || len <= 0 || off > kip->ksyms_size ||
len > kip->ksyms_size - off)
return (EINVAL);
rlen = roundup(len, PAGESIZE);
if ((addr = ksyms_asmap(as, rlen)) == NULL)
return (EOVERFLOW);
aiov.iov_base = addr;
aiov.iov_len = len;
auio.uio_offset = off;
auio.uio_iov = &aiov;
auio.uio_iovcnt = 1;
auio.uio_resid = len;
auio.uio_segflg = UIO_USERSPACE;
auio.uio_llimit = MAXOFFSET_T;
auio.uio_fmode = FREAD;
auio.uio_extflg = UIO_COPY_CACHED;
error = ksyms_symtbl_copy(kip, &auio, len);
if (error)
(void) as_unmap(as, addr, rlen);
else
*addrp = addr;
return (error);
}
/* ARGSUSED */
static int
ksyms_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
{
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
*result = ksyms_devi;
return (DDI_SUCCESS);
case DDI_INFO_DEVT2INSTANCE:
*result = 0;
return (DDI_SUCCESS);
}
return (DDI_FAILURE);
}
static int
ksyms_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
{
if (cmd != DDI_ATTACH)
return (DDI_FAILURE);
if (ddi_create_minor_node(devi, "ksyms", S_IFCHR, 0, DDI_PSEUDO, NULL)
== DDI_FAILURE) {
ddi_remove_minor_node(devi, NULL);
return (DDI_FAILURE);
}
ksyms_devi = devi;
return (DDI_SUCCESS);
}
static int
ksyms_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
{
if (cmd != DDI_DETACH)
return (DDI_FAILURE);
ddi_remove_minor_node(devi, NULL);
return (DDI_SUCCESS);
}
static struct cb_ops ksyms_cb_ops = {
ksyms_open, /* open */
ksyms_close, /* close */
nodev, /* strategy */
nodev, /* print */
nodev, /* dump */
ksyms_read, /* read */
nodev, /* write */
nodev, /* ioctl */
nodev, /* devmap */
nodev, /* mmap */
ksyms_segmap, /* segmap */
nochpoll, /* poll */
ddi_prop_op, /* prop_op */
0, /* streamtab */
D_NEW | D_MP /* Driver compatibility flag */
};
static struct dev_ops ksyms_ops = {
DEVO_REV, /* devo_rev, */
0, /* refcnt */
ksyms_info, /* info */
nulldev, /* identify */
nulldev, /* probe */
ksyms_attach, /* attach */
ksyms_detach, /* detach */
nodev, /* reset */
&ksyms_cb_ops, /* driver operations */
(struct bus_ops *)0, /* no bus operations */
NULL, /* power */
ddi_quiesce_not_needed, /* quiesce */
};
static struct modldrv modldrv = {
&mod_driverops, "kernel symbols driver", &ksyms_ops,
};
static struct modlinkage modlinkage = {
MODREV_1, { (void *)&modldrv }
};
int
_init(void)
{
int error;
if (nksyms_clones == 0)
nksyms_clones = maxusers + 50;
ksyms_clones = kmem_zalloc(nksyms_clones *
sizeof (ksyms_image_t), KM_SLEEP);
if ((error = mod_install(&modlinkage)) != 0)
kmem_free(ksyms_clones, nksyms_clones * sizeof (ksyms_image_t));
return (error);
}
int
_fini(void)
{
int error;
if ((error = mod_remove(&modlinkage)) == 0)
kmem_free(ksyms_clones, nksyms_clones * sizeof (ksyms_image_t));
return (error);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
|