aboutsummaryrefslogtreecommitdiffstats
path: root/tools/debugger/pdb/linux-2.6-module/module.c
blob: c75b9cd64727f39d1b2f7438f9c06e5f34cfcffe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * module.c
 *
 * Handles initial registration with pdb when the pdb module starts up
 * and cleanup when the module goes away (sortof :)
 * Also receives each request from pdb in domain 0 and dispatches to the
 * appropriate debugger function.
 */

#include <linux/module.h>
#include <linux/interrupt.h>

#include <asm-i386/kdebug.h>

#include <asm-xen/evtchn.h>
#include <asm-xen/ctrl_if.h>
#include <asm-xen/hypervisor.h>
#include <asm-xen/xen-public/io/domain_controller.h>
#include <asm-xen/xen-public/xen.h>

#include <asm-xen/xen-public/io/ring.h>

#include "pdb_module.h"
#include "pdb_debug.h"

#define PDB_RING_SIZE __RING_SIZE((pdb_sring_t *)0, PAGE_SIZE)

static pdb_back_ring_t pdb_ring;
static unsigned int    pdb_evtchn;
static unsigned int    pdb_irq;
static unsigned int    pdb_domain;

/* work queue */
static void pdb_work_handler(void *unused);
static DECLARE_WORK(pdb_deferred_work, pdb_work_handler, NULL);

/*
 * send response to a pdb request
 */
void
pdb_send_response (pdb_response_t *response)
{
    pdb_response_t *resp;

    resp = RING_GET_RESPONSE(&pdb_ring, pdb_ring.rsp_prod_pvt);

    memcpy(resp, response, sizeof(pdb_response_t));
    resp->domain = pdb_domain;
    
    wmb();                 /* Ensure other side can see the response fields. */
    pdb_ring.rsp_prod_pvt++;
    RING_PUSH_RESPONSES(&pdb_ring);
    notify_via_evtchn(pdb_evtchn);
    return;
}

/*
 * handle a debug command from the front end
 */
static void
pdb_process_request (pdb_request_t *request)
{
    pdb_response_t resp;
    struct task_struct *target;

    read_lock(&tasklist_lock);
    target = find_task_by_pid(request->process);
    if (target)
        get_task_struct(target);
    read_unlock(&tasklist_lock);

    resp.operation = request->operation;
    resp.process   = request->process;

    if (!target)
    {
        printk ("(linux) target not found 0x%x\n", request->process);
        resp.status = PDB_RESPONSE_ERROR;
        goto response;
    }

    switch (request->operation)
    {
    case PDB_OPCODE_PAUSE :
        pdb_suspend(target);
        resp.status = PDB_RESPONSE_OKAY;
        break;
    case PDB_OPCODE_ATTACH :
        pdb_suspend(target);
        pdb_domain = request->u.attach.domain;
        printk("(linux) attach  dom:0x%x pid:0x%x\n",
               pdb_domain, request->process);
        resp.status = PDB_RESPONSE_OKAY;
        break;
    case PDB_OPCODE_DETACH :
        pdb_resume(target);
        printk("(linux) detach 0x%x\n", request->process);
        resp.status = PDB_RESPONSE_OKAY;
        break;
    case PDB_OPCODE_RD_REGS :
        pdb_read_registers(target, &resp.u.rd_regs);
        resp.status = PDB_RESPONSE_OKAY;
        break;
    case PDB_OPCODE_WR_REG :
        pdb_write_register(target, &request->u.wr_reg);
        resp.status = PDB_RESPONSE_OKAY;
        break;
    case PDB_OPCODE_RD_MEM :
        pdb_access_memory(target, request->u.rd_mem.address,
                          &resp.u.rd_mem.data, request->u.rd_mem.length, 0);
        resp.u.rd_mem.address = request->u.rd_mem.address;
        resp.u.rd_mem.length  = request->u.rd_mem.length;
        resp.status = PDB_RESPONSE_OKAY;
        break;
    case PDB_OPCODE_WR_MEM :
        pdb_access_memory(target, request->u.wr_mem.address,
                         &request->u.wr_mem.data, request->u.wr_mem.length, 1);
        resp.status = PDB_RESPONSE_OKAY;
        break;
    case PDB_OPCODE_CONTINUE :
        pdb_continue(target);
        goto no_response;
        break;
    case PDB_OPCODE_STEP :
        pdb_step(target);
        resp.status = PDB_RESPONSE_OKAY;
        goto no_response;
        break;
    case PDB_OPCODE_SET_BKPT :
        pdb_insert_memory_breakpoint(target, request->u.bkpt.address,
                                     request->u.bkpt.length);
        resp.status = PDB_RESPONSE_OKAY;
        break;
    case PDB_OPCODE_CLR_BKPT :
        pdb_remove_memory_breakpoint(target, request->u.bkpt.address,
                                     request->u.bkpt.length);
        resp.status = PDB_RESPONSE_OKAY;
        break;
    default:
        printk("(pdb) unknown request operation %d\n", request->operation);
        resp.status = PDB_RESPONSE_ERROR;
    }

 response:        
    pdb_send_response (&resp);

 no_response:
    return;
}

/*
 * work queue
 */
static void
pdb_work_handler (void *unused)
{
    pdb_request_t *req;
    RING_IDX i, rp;

    rp = pdb_ring.sring->req_prod;
    rmb();

    for ( i = pdb_ring.req_cons; 
          (i != rp) && !RING_REQUEST_CONS_OVERFLOW(&pdb_ring, i);
          i++ )
    {
        req = RING_GET_REQUEST(&pdb_ring, i);
        pdb_process_request(req);

    }
    pdb_ring.req_cons = i;
}

/*
 * receive a pdb request
 */
static irqreturn_t
pdb_interrupt (int irq, void *dev_id, struct pt_regs *ptregs)
{
    schedule_work(&pdb_deferred_work);

    return IRQ_HANDLED;
}

static void
pdb_send_connection_status(int status, memory_t ring)
{
    ctrl_msg_t cmsg = 
    {
        .type = CMSG_DEBUG,
        .subtype = CMSG_DEBUG_CONNECTION_STATUS,
        .length  = sizeof(pdb_connection_t),
    };
    pdb_connection_t *conn = (pdb_connection_t *)cmsg.msg;

    conn->status = status;
    conn->ring = ring;
    conn->evtchn = 0;

    ctrl_if_send_message_block(&cmsg, NULL, 0, TASK_UNINTERRUPTIBLE);
}


/*
 * this is called each time a message is received on the control channel
 */
static void
pdb_ctrlif_rx(ctrl_msg_t *msg, unsigned long id)
{
    switch (msg->subtype)
    {
    case CMSG_DEBUG_CONNECTION_STATUS:
        /* initialize event channel created by the pdb server */

        pdb_evtchn = ((pdb_connection_p) msg->msg)->evtchn;
        pdb_irq = bind_evtchn_to_irq(pdb_evtchn);

        if ( request_irq(pdb_irq, pdb_interrupt, 
                         SA_SAMPLE_RANDOM, "pdb", NULL) )
        {
            printk("(pdb) request irq failed: %d %d\n", pdb_evtchn, pdb_irq);
        }
        break;

    default:
        printk ("(pdb) unknown xcs control message: %d\n", msg->subtype);
        break;
    }

    return;
}


/********************************************************************/

static struct notifier_block pdb_exceptions_nb =
{
    .notifier_call = pdb_exceptions_notify,
    .priority = 0x1                                          /* low priority */
};


static int __init 
pdb_initialize (void)
{
    int err;
    pdb_sring_t *sring;

    printk("----\npdb initialize   %s %s\n", __DATE__, __TIME__);

    pdb_initialize_bwcpoint();

    /*
    if ( xen_start_info.flags & SIF_INITDOMAIN )
        return 1;
    */

    pdb_evtchn = 0;
    pdb_irq    = 0;
    pdb_domain = 0;

    (void)ctrl_if_register_receiver(CMSG_DEBUG, pdb_ctrlif_rx,
                                    CALLBACK_IN_BLOCKING_CONTEXT);

    /* rings */
    sring = (pdb_sring_t *)__get_free_page(GFP_KERNEL);
    SHARED_RING_INIT(sring);
    BACK_RING_INIT(&pdb_ring, sring, PAGE_SIZE);
 
    /* notify pdb in dom 0 */
    pdb_send_connection_status(PDB_CONNECTION_STATUS_UP, 
                               virt_to_machine(pdb_ring.sring) >> PAGE_SHIFT);

    /* handler for int1 & int3 */
    err = register_die_notifier(&pdb_exceptions_nb);

    return err;
}

static void __exit
pdb_terminate(void)
{
    int err = 0;

    printk("pdb cleanup\n");

    (void)ctrl_if_unregister_receiver(CMSG_DEBUG, pdb_ctrlif_rx);

    if (pdb_irq)
    {
        free_irq(pdb_irq, NULL);
        pdb_irq = 0;
    }

    if (pdb_evtchn)
    {
        unbind_evtchn_from_irq(pdb_evtchn); 
        pdb_evtchn = 0;
    }

    pdb_send_connection_status(PDB_CONNECTION_STATUS_DOWN, 0);

    /* handler for int1 & int3 */
    err = unregister_die_notifier(&pdb_exceptions_nb);

	return;
}


module_init(pdb_initialize);
module_exit(pdb_terminate);


/*
 * Local variables:
 * mode: C
 * c-set-style: "BSD"
 * c-basic-offset: 4
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */