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