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