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 /*
28 * Debugger co-routine context support: In order to implement the context-
29 * switching necessary for MDB pipes, we need the ability to establish a
30 * co-routine context that has a separate stack. We use this stack to execute
31 * the MDB parser, and then switch back and forth between this code and the
32 * dcmd which is producing output to be consumed. We implement a context by
33 * mapping a few pages of anonymous memory, and then using setcontext(2) to
34 * switch to this stack and begin execution of a new function.
35 */
36
37 #include <mdb/mdb_context_impl.h>
38 #include <mdb/mdb_modapi.h>
39 #include <mdb/mdb_debug.h>
40 #include <mdb/mdb_err.h>
41
42 #include <sys/types.h>
43 #include <sys/mman.h>
44
45 #include <ucontext.h>
46 #include <unistd.h>
47 #include <setjmp.h>
48 #include <fcntl.h>
49 #include <errno.h>
50
51 static void
context_init(mdb_context_t * volatile c)52 context_init(mdb_context_t *volatile c)
53 {
54 c->ctx_status = c->ctx_func();
55 ASSERT(c->ctx_resumes > 0);
56 longjmp(c->ctx_pcb, 1);
57 }
58
59 mdb_context_t *
mdb_context_create(int (* func)(void))60 mdb_context_create(int (*func)(void))
61 {
62 mdb_context_t *c = mdb_zalloc(sizeof (mdb_context_t), UM_NOSLEEP);
63 size_t pagesize = sysconf(_SC_PAGESIZE);
64 int prot = sysconf(_SC_STACK_PROT);
65 static int zfd = -1;
66
67 if (c == NULL)
68 return (NULL);
69
70 if (prot == -1)
71 prot = PROT_READ | PROT_WRITE | PROT_EXEC;
72
73 c->ctx_func = func;
74 c->ctx_stacksize = pagesize * 4;
75 c->ctx_stack = mmap(NULL, c->ctx_stacksize, prot,
76 MAP_PRIVATE | MAP_ANON, -1, 0);
77
78 /*
79 * If the mmap failed with EBADFD, this kernel doesn't have MAP_ANON
80 * support; fall back to opening /dev/zero, caching the fd, and using
81 * that to mmap chunks of anonymous memory.
82 */
83 if (c->ctx_stack == MAP_FAILED && errno == EBADF) {
84 if (zfd == -1 && (zfd = open("/dev/zero", O_RDWR)) >= 0)
85 (void) fcntl(zfd, F_SETFD, FD_CLOEXEC);
86
87 if (zfd >= 0) {
88 c->ctx_stack = mmap(NULL, c->ctx_stacksize, prot,
89 MAP_PRIVATE, zfd, 0);
90 }
91 }
92
93 c->ctx_uc.uc_flags = UC_ALL;
94 if (c->ctx_stack == MAP_FAILED || getcontext(&c->ctx_uc) != 0) {
95 mdb_free(c, sizeof (mdb_context_t));
96 return (NULL);
97 }
98
99 c->ctx_uc.uc_stack.ss_sp = c->ctx_stack;
100 c->ctx_uc.uc_stack.ss_size = c->ctx_stacksize;
101 c->ctx_uc.uc_stack.ss_flags = 0;
102 c->ctx_uc.uc_link = NULL;
103 makecontext(&c->ctx_uc, context_init, 1, c);
104
105 return (c);
106 }
107
108 void
mdb_context_destroy(mdb_context_t * c)109 mdb_context_destroy(mdb_context_t *c)
110 {
111 if (munmap(c->ctx_stack, c->ctx_stacksize) == -1)
112 fail("failed to unmap stack %p", c->ctx_stack);
113
114 mdb_free(c, sizeof (mdb_context_t));
115 }
116
117 void
mdb_context_switch(mdb_context_t * c)118 mdb_context_switch(mdb_context_t *c)
119 {
120 if (setjmp(c->ctx_pcb) == 0 && setcontext(&c->ctx_uc) == -1)
121 fail("failed to change context to %p", (void *)c);
122 else
123 fail("unexpectedly returned from context %p", (void *)c);
124 }
125
126 jmp_buf *
mdb_context_getpcb(mdb_context_t * c)127 mdb_context_getpcb(mdb_context_t *c)
128 {
129 c->ctx_resumes++;
130 return (&c->ctx_pcb);
131 }
132