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 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /*
28 * The communication mechanism for requesting that the driver perform work on
29 * behalf of the debugger. Messages are passed and processed in FIFO order,
30 * with no provision for high priority messages. High priority messages, such
31 * as debugger termination requests, should be passed using a different
32 * mechanism.
33 *
34 * Two FIFO queues are used for communication - one from the debugger to the
35 * driver, known as the driver_notify queue, and one from the driver to the
36 * debugger, known as the debugger_notify queue. Messages are added to one
37 * queue, processed by the party on the other end, and are sent back as
38 * acknowledgements on the other queue. All messages must be acknowledged, in
39 * part because the party who sent the message is the only one who can free it.
40 *
41 * Debugger-initiated work requests are usually triggered by dcmds such as
42 * ::load. In the case of a ::load, the debugger adds a load request to the
43 * driver_notify queue. The driver removes the request from the queue and
44 * processes it. When processing is complete, the message is turned into an
45 * acknowledgement, and completion status is added. The message is then added
46 * to the debugger_notify queue. Upon receipt, the debugger removes the
47 * message from the queue, notes the completion status, and frees it.
48 *
49 * The driver can itself initiate unsolicited work, such as the automatic
50 * loading of a dmod in response to a krtld module load notification. In this
51 * case, the driver loads the module and creates a work-completion message.
52 * This completion is identical to the one sent in the solicited load case
53 * above, with the exception of the acknowledgement bit, which isn't be set.
54 * When the debugger receives the completion message, it notes the completion
55 * status, and sends the message back to the driver via the driver_notify queue,
56 * this time with the acknowledgement bit set.
57 */
58
59 #include <sys/types.h>
60
61 #include <kmdb/kmdb_asmutil.h>
62 #include <kmdb/kmdb_wr_impl.h>
63 #include <mdb/mdb_debug.h>
64 #include <mdb/mdb.h>
65
66 /*
67 * Called by the driver to pass a message to the debugger. The debugger could
68 * start running at any time. Nodes are added to the queue in FIFO order, but
69 * with links pointing in reverse order.
70 */
71 void
kmdb_wr_debugger_notify(void * arg)72 kmdb_wr_debugger_notify(void *arg)
73 {
74 kmdb_wr_t *new = arg;
75 kmdb_wr_t *curtail;
76
77 new->wn_next = new->wn_prev = NULL;
78 membar_producer();
79
80 do {
81 if ((curtail = mdb.m_dbgwrtail) == NULL) {
82 /*
83 * The queue is empty, because tail will only be NULL if
84 * head is NULL too. We're the only one who can add
85 * to the queue, so we can blindly add our node. The
86 * debugger can't look at tail until head is non-NULL,
87 * so we set tail first.
88 */
89 mdb.m_dbgwrtail = new;
90 membar_producer();
91 mdb.m_dbgwrhead = new;
92 membar_producer();
93 break;
94 }
95
96 /*
97 * Point the new node at the current tail. Attempt to set tail
98 * to point to our new node, but only as long as tail is what
99 * we think it is.
100 */
101 new->wn_prev = curtail;
102 membar_producer();
103 } while (cas((uintptr_t *)&mdb.m_dbgwrtail, (uintptr_t)curtail,
104 (uintptr_t)new) != (uintptr_t)curtail);
105 }
106
107 /*
108 * Called by the debugger to receive messages from the driver. The driver
109 * has added the nodes in FIFO order, but has only set the prev pointers. We
110 * have to correct that before processing the nodes. This routine will not
111 * be preempted.
112 */
113 int
kmdb_wr_debugger_process(int (* cb)(kmdb_wr_t *,void *),void * arg)114 kmdb_wr_debugger_process(int (*cb)(kmdb_wr_t *, void *), void *arg)
115 {
116 kmdb_wr_t *wn, *wnn;
117 int i;
118
119 if (mdb.m_dbgwrhead == NULL)
120 return (0); /* The queue is empty, so there's nothing to do */
121
122 /* Re-establish the next links so we can traverse in FIFO order */
123 mdb.m_dbgwrtail->wn_next = NULL;
124 for (wn = mdb.m_dbgwrtail; wn->wn_prev != NULL;
125 wn = wn->wn_prev)
126 wn->wn_prev->wn_next = wn;
127
128 /* We don't own wn after we've invoked the callback */
129 wn = mdb.m_dbgwrhead;
130 i = 0;
131 do {
132 wnn = wn->wn_next;
133 i += cb(wn, arg);
134 } while ((wn = wnn) != NULL);
135
136 mdb.m_dbgwrhead = mdb.m_dbgwrtail = NULL;
137
138 return (i);
139 }
140
141 /*
142 * Called by the debugger to check queue status.
143 */
144 int
kmdb_wr_debugger_notify_isempty(void)145 kmdb_wr_debugger_notify_isempty(void)
146 {
147 return (mdb.m_dbgwrhead == NULL);
148 }
149
150 /*
151 * Called by the debugger to pass a message to the driver. This routine will
152 * not be preempted.
153 */
154 void
kmdb_wr_driver_notify(void * arg)155 kmdb_wr_driver_notify(void *arg)
156 {
157 kmdb_wr_t *new = arg;
158
159 /*
160 * We restrict ourselves to manipulating the rear of the queue. We
161 * don't look at the head unless the tail is NULL.
162 */
163 if (mdb.m_drvwrtail == NULL) {
164 new->wn_next = new->wn_prev = NULL;
165 mdb.m_drvwrhead = mdb.m_drvwrtail = new;
166 } else {
167 mdb.m_drvwrtail->wn_next = new;
168 new->wn_prev = mdb.m_drvwrtail;
169 new->wn_next = NULL;
170 mdb.m_drvwrtail = new;
171 }
172 }
173
174 /*
175 * Called by the driver to receive messages from the debugger. The debugger
176 * could start running at any time.
177 *
178 * NOTE: This routine may run *after* mdb_destroy(), and may *NOT* use any MDB
179 * services.
180 */
181 int
kmdb_wr_driver_process(int (* cb)(kmdb_wr_t *,void *),void * arg)182 kmdb_wr_driver_process(int (*cb)(kmdb_wr_t *, void *), void *arg)
183 {
184 kmdb_wr_t *worklist, *wn, *wnn;
185 int rc, rv, i;
186
187 if ((worklist = mdb.m_drvwrhead) == NULL) {
188 return (0); /* The queue is empty, so there's nothing to do */
189 }
190
191 mdb.m_drvwrhead = NULL;
192 /* The debugger uses tail, so enqueues still work */
193 membar_producer();
194 mdb.m_drvwrtail = NULL;
195 membar_producer();
196
197 /*
198 * The current set of messages has been removed from the queue, so
199 * we can process them at our leisure.
200 */
201
202 wn = worklist;
203 rc = i = 0;
204 do {
205 wnn = wn->wn_next;
206 if ((rv = cb(wn, arg)) < 0)
207 rc = -1;
208 else
209 i += rv;
210 } while ((wn = wnn) != NULL);
211
212 return (rc == 0 ? i : -1);
213 }
214
215 /*
216 * Called by the debugger to check queue status
217 */
218 int
kmdb_wr_driver_notify_isempty(void)219 kmdb_wr_driver_notify_isempty(void)
220 {
221 return (mdb.m_drvwrhead == NULL);
222 }
223