1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #pragma ident "%Z%%M% %I% %E% SMI"
28
29 /*
30 * Debugger co-routine context support. kmdb co-routines are essentially the
31 * same as the ones used by mdb, with the exception that we allocate the stack
32 * for the co-routine from our heap.
33 */
34
35 #include <kmdb/kmdb_context_impl.h>
36 #include <mdb/mdb_modapi.h>
37 #include <mdb/mdb_debug.h>
38 #include <mdb/mdb_err.h>
39 #include <mdb/mdb_umem.h>
40 #include <mdb/mdb.h>
41
42 #include <sys/types.h>
43
44 #include <ucontext.h>
45 #include <setjmp.h>
46
47 static void
context_init(mdb_context_t * volatile c)48 context_init(mdb_context_t *volatile c)
49 {
50 c->ctx_status = c->ctx_func();
51 ASSERT(c->ctx_resumes > 0);
52 longjmp(c->ctx_pcb, 1);
53 }
54
55 mdb_context_t *
mdb_context_create(int (* func)(void))56 mdb_context_create(int (*func)(void))
57 {
58 mdb_context_t *c = mdb_zalloc(sizeof (mdb_context_t), UM_NOSLEEP);
59 size_t pagesize = mdb.m_pagesize;
60
61 if (c == NULL)
62 return (NULL);
63
64 c->ctx_func = func;
65 c->ctx_stacksize = pagesize * 4;
66 c->ctx_stack = mdb_alloc_align(c->ctx_stacksize, pagesize, UM_NOSLEEP);
67
68 if (c->ctx_stack == NULL) {
69 mdb_free(c, sizeof (mdb_context_t));
70 return (NULL);
71 }
72
73 kmdb_makecontext(&c->ctx_uc, (void (*)(void *))context_init, c,
74 c->ctx_stack, c->ctx_stacksize);
75
76 return (c);
77 }
78
79 void
mdb_context_destroy(mdb_context_t * c)80 mdb_context_destroy(mdb_context_t *c)
81 {
82 mdb_free_align(c->ctx_stack, c->ctx_stacksize);
83 mdb_free(c, sizeof (mdb_context_t));
84 }
85
86 void
mdb_context_switch(mdb_context_t * c)87 mdb_context_switch(mdb_context_t *c)
88 {
89 if (setjmp(c->ctx_pcb) == 0 && kmdb_setcontext(&c->ctx_uc) == -1)
90 fail("failed to change context to %p", (void *)c);
91 else
92 fail("unexpectedly returned from context %p", (void *)c);
93 }
94
95 jmp_buf *
mdb_context_getpcb(mdb_context_t * c)96 mdb_context_getpcb(mdb_context_t *c)
97 {
98 c->ctx_resumes++;
99 return (&c->ctx_pcb);
100 }
101