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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
|
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <kmdb/kctl/kctl.h>
#include <kmdb/kctl/kctl_wr.h>
#include <kmdb/kmdb_kctl.h>
#include <kmdb/kmdb_kdi.h>
#include <kmdb/kmdb_auxv.h>
#include <mdb/mdb_errno.h>
#include <sys/sysmacros.h>
#include <sys/reboot.h>
#include <sys/atomic.h>
#include <sys/bootconf.h>
#include <sys/kmdb.h>
#include <sys/kobj.h>
#include <sys/kobj_impl.h>
#include <sys/promimpl.h>
#include <sys/kdi_impl.h>
#include <sys/ctf_api.h>
#include <vm/seg_kmem.h>
kctl_t kctl;
#define KCTL_EXECNAME "/kernel/drv/kmdb"
#if defined(_LP64)
#define KCTL_MEM_GOALSZ (20 * 1024 * 1024)
#else
#define KCTL_MEM_GOALSZ (10 * 1024 * 1024)
#endif
/*
* kmdb will call its own copies of the promif routines during
* initialization. As these routines are intended to be used when the
* world is stopped, they don't attempt to grab the PROM lock. Very
* Bad Things could happen if kmdb called a prom routine while someone
* else was calling the kernel's copy of another prom routine, so we
* grab the PROM lock ourselves before we start initialization.
*/
#ifdef __sparc
#define KCTL_PROM_LOCK promif_preprom()
#define KCTL_PROM_UNLOCK promif_postprom()
#else
#define KCTL_PROM_LOCK
#define KCTL_PROM_UNLOCK
#endif
static int
kctl_init(void)
{
if (kobj_kdi.kdi_version != KDI_VERSION) {
kctl_warn("kmdb/kernel version mismatch (expected %d, "
"found %d)", KDI_VERSION, kobj_kdi.kdi_version);
return (-1);
}
sema_init(&kctl.kctl_wr_avail_sem, 0, NULL, SEMA_DRIVER, NULL);
mutex_init(&kctl.kctl_wr_lock, NULL, MUTEX_DRIVER, NULL);
cv_init(&kctl.kctl_wr_cv, NULL, CV_DRIVER, NULL);
mutex_init(&kctl.kctl_lock, NULL, MUTEX_DRIVER, NULL);
kctl.kctl_execname = KCTL_EXECNAME; /* XXX get from modctl? */
kctl.kctl_state = KCTL_ST_INACTIVE;
kctl.kctl_dseg = kctl.kctl_mrbase = NULL;
kctl.kctl_dseg_size = kctl.kctl_mrsize = 0;
kctl_dmod_init();
return (0);
}
static void
kctl_fini(void)
{
kctl_dmod_fini();
mutex_destroy(&kctl.kctl_lock);
cv_destroy(&kctl.kctl_wr_cv);
mutex_destroy(&kctl.kctl_wr_lock);
sema_destroy(&kctl.kctl_wr_avail_sem);
}
static uint_t
kctl_set_state(uint_t state)
{
uint_t ostate = kctl.kctl_state;
/* forward progess only, please */
if (state > ostate) {
kctl_dprintf("new kctl state: %d", state);
kctl.kctl_state = state;
}
return (ostate);
}
static int
kctl_boot_dseg_alloc(caddr_t dsegaddr, size_t dsegsz)
{
/*
* The Intel boot memory allocator will cleverly map us onto a 4M
* page if we request the whole 4M Intel segment at once. This
* will break physical memory r/w, so we break the request into
* chunks. The allocator isn't smart enough to combine requests,
* so it'll give us a bunch of 4k pages.
*/
while (dsegsz >= 1024*1024) {
size_t sz = MIN(dsegsz, 1024*1024);
if (BOP_ALLOC(kctl.kctl_boot_ops, dsegaddr, sz, BO_NO_ALIGN) !=
dsegaddr)
return (-1);
dsegaddr += sz;
dsegsz -= sz;
}
return (0);
}
static int
kctl_dseg_alloc(caddr_t addr, size_t sz)
{
ASSERT(((uintptr_t)addr & PAGEOFFSET) == 0);
/* make sure there isn't something there already (like kadb) */
if (hat_getpfnum(kas.a_hat, addr) != PFN_INVALID)
return (EAGAIN);
if (segkmem_xalloc(NULL, addr, sz, VM_NOSLEEP, 0, segkmem_page_create,
NULL) == NULL)
return (ENOMEM);
return (0);
}
static void
kctl_dseg_free(caddr_t addr, size_t sz)
{
ASSERT(((uintptr_t)addr & PAGEOFFSET) == 0);
segkmem_free(NULL, addr, sz);
}
static void
kctl_memavail(void)
{
size_t needed;
caddr_t base;
/*
* We're now free to allocate the non-fixed portion of the debugger's
* memory region.
*/
needed = P2ROUNDUP(kctl.kctl_memgoalsz <= kctl.kctl_dseg_size ? 0 :
kctl.kctl_memgoalsz - kctl.kctl_dseg_size, PAGESIZE);
if (needed == 0)
return;
if ((base = kmem_zalloc(needed, KM_NOSLEEP)) == NULL) {
/*
* If we're going to wedge the machine during debugger startup,
* at least let them know why it's going to wedge.
*/
cmn_err(CE_WARN, "retrying of kmdb allocation of 0x%lx bytes",
(ulong_t)needed);
base = kmem_zalloc(needed, KM_SLEEP);
}
kdi_dvec->dv_memavail(base, needed);
kctl.kctl_mrbase = base;
kctl.kctl_mrsize = needed;
}
void
kctl_cleanup(void)
{
uint_t state = kctl_set_state(KCTL_ST_DEACTIVATING);
kctl_dprintf("cleaning up from state %d", state);
ASSERT(kctl.kctl_boot_loaded == 0);
switch (state) {
case KCTL_ST_ACTIVE:
boothowto &= ~RB_DEBUG;
/* XXX there's a race here */
kdi_dvec = NULL;
/*FALLTHROUGH*/
case KCTL_ST_DBG_ACTIVATED:
KCTL_PROM_LOCK;
kmdb_deactivate();
KCTL_PROM_UNLOCK;
/*FALLTHROUGH*/
case KCTL_ST_THREAD_STARTED:
if (curthread != kctl.kctl_wr_thr) {
kctl_wr_thr_stop();
kctl_wr_thr_join();
}
/*FALLTHROUGH*/
case KCTL_ST_MOD_NOTIFIERS:
kctl_mod_notify_unreg();
/*FALLTHROUGH*/
case KCTL_ST_KCTL_PREACTIVATED:
kctl_depreactivate_isadep();
/*FALLTHROUGH*/
case KCTL_ST_INITIALIZED:
/* There's no kmdb_fini */
case KCTL_ST_DSEG_ALLOCED:
kctl_dseg_free(kctl.kctl_dseg, kctl.kctl_dseg_size);
if (kctl.kctl_mrbase != NULL)
kmem_free(kctl.kctl_mrbase, kctl.kctl_mrsize);
/*FALLTHROUGH*/
}
kctl.kctl_state = KCTL_ST_INACTIVE;
}
static void
kctl_startup_modules(void)
{
struct modctl *modp;
/*
* Normal module load and unload is now available. Prior to this point,
* we could only load modules, and that only when the debugger was being
* initialized.
*
* We'll need to prepare the modules we've already loaded (if any) for
* the brave new world in which boot is unmapped.
*/
kctl_dmod_sync();
/*
* Process any outstanding loads or unloads and prepare for automatic
* module loading and unloading.
*/
(void) kctl_wr_process();
kctl_mod_notify_reg();
(void) kctl_set_state(KCTL_ST_MOD_NOTIFIERS);
modp = &modules;
do {
kctl_mod_loaded(modp);
} while ((modp = modp->mod_next) != &modules);
}
static void
kctl_startup_thread(void)
{
/*
* Create the worker thread, which will handle future requests from the
* debugger.
*/
kctl_wr_thr_start();
(void) kctl_set_state(KCTL_ST_THREAD_STARTED);
}
static int
kctl_startup_boot(void)
{
struct modctl_list *lp, **lpp;
int rc;
if (kctl_wr_process() < 0) {
kctl_warn("kmdb: failed to load modules");
return (-1);
}
mutex_enter(&mod_lock);
for (lpp = kobj_linkmaps; *lpp != NULL; lpp++) {
for (lp = *lpp; lp != NULL; lp = lp->modl_next) {
if ((rc = kctl_mod_decompress(lp->modl_modp)) != 0) {
kctl_warn("kmdb: failed to decompress CTF data "
"for %s: %s", lp->modl_modp->mod_modname,
ctf_errmsg(rc));
}
}
}
mutex_exit(&mod_lock);
return (0);
}
static int
kctl_startup_preactivate(void *romp, const char *cfg, const char **argv)
{
kmdb_auxv_t kav;
int rc;
kctl_auxv_init(&kav, cfg, argv, romp);
KCTL_PROM_LOCK;
rc = kmdb_init(kctl.kctl_execname, &kav);
KCTL_PROM_UNLOCK;
kctl_auxv_fini(&kav);
if (rc < 0)
return (EMDB_KNOLOAD);
(void) kctl_set_state(KCTL_ST_INITIALIZED);
if (kctl_preactivate_isadep() != 0)
return (EIO);
(void) kctl_set_state(KCTL_ST_KCTL_PREACTIVATED);
return (0);
}
static int
kctl_startup_activate(uint_t flags)
{
kdi_debugvec_t *dvec;
KCTL_PROM_LOCK;
kmdb_activate(&dvec, flags);
KCTL_PROM_UNLOCK;
(void) kctl_set_state(KCTL_ST_DBG_ACTIVATED);
/*
* fill in a few remaining debugvec entries.
*/
dvec->dv_kctl_modavail = kctl_startup_modules;
dvec->dv_kctl_thravail = kctl_startup_thread;
dvec->dv_kctl_memavail = kctl_memavail;
kctl_activate_isadep(dvec);
kdi_dvec = dvec;
membar_producer();
boothowto |= RB_DEBUG;
(void) kctl_set_state(KCTL_ST_ACTIVE);
return (0);
}
static int
kctl_state_check(uint_t state, uint_t ok_state)
{
if (state == ok_state)
return (0);
if (state == KCTL_ST_INACTIVE)
return (EMDB_KINACTIVE);
else if (kctl.kctl_state > KCTL_ST_INACTIVE &&
kctl.kctl_state < KCTL_ST_ACTIVE)
return (EMDB_KACTIVATING);
else if (kctl.kctl_state == KCTL_ST_ACTIVE)
return (EMDB_KACTIVE);
else if (kctl.kctl_state == KCTL_ST_DEACTIVATING)
return (EMDB_KDEACTIVATING);
else
return (EINVAL);
}
int
kctl_deactivate(void)
{
int rc;
mutex_enter(&kctl.kctl_lock);
if (kctl.kctl_boot_loaded) {
rc = EMDB_KNOUNLOAD;
goto deactivate_done;
}
if ((rc = kctl_state_check(kctl.kctl_state, KCTL_ST_ACTIVE)) != 0)
goto deactivate_done;
kmdb_kdi_set_unload_request();
kmdb_kdi_kmdb_enter();
/*
* The debugger will pass the request to the work thread, which will
* stop itself.
*/
kctl_wr_thr_join();
deactivate_done:
mutex_exit(&kctl.kctl_lock);
return (rc);
}
/*
* Called from krtld, this indicates that the user loaded kmdb at boot. We
* track activation states, but we don't attempt to clean up if activation
* fails, because boot debugger load failures are fatal.
*
* Further complicating matters, various kernel routines, such as bcopy and
* mutex_enter, assume the presence of some basic state. On SPARC, it's the
* presence of a valid curthread pointer. On AMD64, it's a valid curcpu
* pointer in GSBASE. We set up temporary versions of these before beginning
* activation, and tear them down when we're done.
*/
int
kctl_boot_activate(struct bootops *ops, void *romp, size_t memsz,
const char **argv)
{
void *old;
#ifdef __lint
{
/*
* krtld does a name-based symbol lookup to find this routine. It then
* casts the address it gets, calling the result. We want to make sure
* that the call in krtld stays in sync with the prototype for this
* function, so we define a type (kctl_boot_activate_f) that matches the
* current prototype. The following assignment ensures that the type
* still matches the declaration, with lint as the enforcer.
*/
kctl_boot_activate_f *kba = kctl_boot_activate;
if (kba == NULL) /* Make lint think kba is actually used */
return (0);
}
#endif
old = kctl_boot_tmpinit(); /* Set up temporary state */
ASSERT(ops != NULL);
kctl.kctl_boot_ops = ops; /* must be set before kctl_init */
if (kctl_init() < 0)
return (-1);
kctl.kctl_boot_loaded = 1;
kctl_dprintf("beginning kmdb initialization");
if (memsz == 0)
memsz = KCTL_MEM_GOALSZ;
kctl.kctl_dseg = kdi_segdebugbase;
kctl.kctl_dseg_size =
memsz > kdi_segdebugsize ? kdi_segdebugsize : memsz;
kctl.kctl_memgoalsz = memsz;
if (kctl_boot_dseg_alloc(kctl.kctl_dseg, kctl.kctl_dseg_size) < 0) {
kctl_warn("kmdb: failed to allocate %lu-byte debugger area at "
"%p", kctl.kctl_dseg_size, (void *)kctl.kctl_dseg);
return (-1);
}
(void) kctl_set_state(KCTL_ST_DSEG_ALLOCED);
if (kctl_startup_preactivate(romp, NULL, argv) != 0 ||
kctl_startup_activate(KMDB_ACT_F_BOOT)) {
kctl_warn("kmdb: failed to activate");
return (-1);
}
if (kctl_startup_boot() < 0)
return (-1);
kctl_dprintf("finished with kmdb initialization");
kctl_boot_tmpfini(old);
kctl.kctl_boot_ops = NULL;
return (0);
}
int
kctl_modload_activate(size_t memsz, const char *cfg, uint_t flags)
{
int rc;
mutex_enter(&kctl.kctl_lock);
if ((rc = kctl_state_check(kctl.kctl_state, KCTL_ST_INACTIVE)) != 0) {
if ((flags & KMDB_F_AUTO_ENTRY) && rc == EMDB_KACTIVE) {
kmdb_kdi_kmdb_enter();
rc = 0;
}
mutex_exit(&kctl.kctl_lock);
return (rc);
}
kctl.kctl_flags = flags;
if (memsz == 0)
memsz = KCTL_MEM_GOALSZ;
kctl.kctl_dseg = kdi_segdebugbase;
kctl.kctl_dseg_size =
memsz > kdi_segdebugsize ? kdi_segdebugsize : memsz;
kctl.kctl_memgoalsz = memsz;
if ((rc = kctl_dseg_alloc(kctl.kctl_dseg, kctl.kctl_dseg_size)) != 0)
goto activate_fail;
(void) kctl_set_state(KCTL_ST_DSEG_ALLOCED);
if ((rc = kctl_startup_preactivate(NULL, cfg, NULL)) != 0)
goto activate_fail;
kctl_startup_modules();
kctl_startup_thread();
if ((rc = kctl_startup_activate(0)) != 0)
goto activate_fail;
kctl_memavail(); /* Must be after kdi_dvec is set */
if (kctl.kctl_flags & KMDB_F_AUTO_ENTRY)
kmdb_kdi_kmdb_enter();
mutex_exit(&kctl.kctl_lock);
return (0);
activate_fail:
kctl_cleanup();
mutex_exit(&kctl.kctl_lock);
return (rc);
}
/*
* This interface will be called when drv/kmdb loads. When we get the call, one
* of two things will have happened:
*
* 1. The debugger was loaded at boot. We've progressed far enough into boot
* as to allow drv/kmdb to be loaded as a non-primary. Invocation of this
* interface is the signal to the debugger that it can start allowing things
* like dmod loading and automatic CTF decompression - things which require
* the system services that have now been started.
*
* 2. The debugger was loaded after boot. mdb opened /dev/kmdb, causing
* drv/kmdb to load, followed by misc/kmdb. Nothing has been set up yet,
* so we need to initialize. Activation will occur separately, so we don't
* have to worry about that.
*/
int
kctl_attach(dev_info_t *dip)
{
kctl.kctl_drv_dip = dip;
return (0);
}
int
kctl_detach(void)
{
return (kctl.kctl_state == KCTL_ST_INACTIVE ? 0 : EBUSY);
}
static struct modlmisc modlmisc = {
&mod_miscops,
KMDB_VERSION
};
static struct modlinkage modlinkage = {
MODREV_1,
(void *)&modlmisc,
NULL
};
/*
* Invoked only when debugger is loaded via modload - not invoked when debugger
* is loaded at boot. kctl_boot_activate needs to call anything (aside from
* mod_install) this function does.
*/
int
_init(void)
{
if (kctl_init() < 0)
return (EINVAL);
return (mod_install(&modlinkage));
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
int
_fini(void)
{
kctl_fini();
return (mod_remove(&modlinkage));
}
|