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