Subversion Repositories HelenOS

Rev

Rev 3014 | Rev 3471 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2894 svoboda 1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
2813 svoboda 29
/** @addtogroup generic
30
 * @{
31
 */
32
 
33
/**
34
 * @file
2894 svoboda 35
 * @brief   Udebug IPC message handling.
2813 svoboda 36
 */
37
 
3424 svoboda 38
#include <print.h>
2813 svoboda 39
#include <proc/task.h>
40
#include <proc/thread.h>
2815 svoboda 41
#include <arch.h>
2813 svoboda 42
#include <errno.h>
43
#include <ipc/ipc.h>
44
#include <syscall/copy.h>
45
#include <udebug/udebug.h>
2887 svoboda 46
#include <udebug/udebug_ops.h>
2813 svoboda 47
#include <udebug/udebug_ipc.h>
48
 
2817 svoboda 49
static int udebug_rp_regs_write(call_t *call, phone_t *phone)
50
{
51
    void *uspace_data;
52
    unative_t to_copy;
53
    int rc;
2886 svoboda 54
    void *buffer;
2817 svoboda 55
 
3424 svoboda 56
    printf("debug_regs_write()\n");
2817 svoboda 57
 
58
    uspace_data = (void *)IPC_GET_ARG3(call->data);
2919 svoboda 59
    to_copy = sizeof(istate_t);
2896 svoboda 60
    buffer = malloc(to_copy, 0);
2886 svoboda 61
 
62
    rc = copy_from_uspace(buffer, uspace_data, to_copy);
2817 svoboda 63
    if (rc != 0) {
3424 svoboda 64
        printf("debug_regs_write() - copy failed\n");
2817 svoboda 65
        return rc;
66
    }
67
 
2886 svoboda 68
    call->buffer = buffer;
2824 svoboda 69
 
3424 svoboda 70
    printf(" - done\n");
2892 svoboda 71
    return 0;
2817 svoboda 72
}
73
 
2885 svoboda 74
static int udebug_rp_mem_write(call_t *call, phone_t *phone)
2813 svoboda 75
{
2885 svoboda 76
    void *uspace_data;
77
    unative_t to_copy;
78
    int rc;
79
    void *buffer;
80
 
3424 svoboda 81
    printf("udebug_rp_mem_write()\n");
2885 svoboda 82
 
83
    uspace_data = (void *)IPC_GET_ARG2(call->data);
84
    to_copy = IPC_GET_ARG4(call->data);
85
 
2896 svoboda 86
    buffer = malloc(to_copy, 0);
2885 svoboda 87
 
88
    rc = copy_from_uspace(buffer, uspace_data, to_copy);
89
    if (rc != 0) {
3424 svoboda 90
        printf(" - copy failed\n");
2885 svoboda 91
        return rc;
92
    }
93
 
94
    call->buffer = buffer;
95
 
3424 svoboda 96
    printf(" - done\n");
2892 svoboda 97
    return 0;
2885 svoboda 98
}
99
 
100
 
101
int udebug_request_preprocess(call_t *call, phone_t *phone)
102
{
103
    int rc;
104
 
105
    switch (IPC_GET_ARG1(call->data)) {
106
    case UDEBUG_M_REGS_WRITE:
107
        rc = udebug_rp_regs_write(call, phone);
108
        return rc;
109
    case UDEBUG_M_MEM_WRITE:
110
        rc = udebug_rp_mem_write(call, phone);
111
        return rc;
112
    default:
113
        break;
114
    }
115
 
116
    return 0;
117
}
118
 
119
static void udebug_receive_begin(call_t *call)
120
{
2887 svoboda 121
    int rc;
2885 svoboda 122
 
2887 svoboda 123
    rc = udebug_begin(call);
124
    if (rc < 0) {
125
        IPC_SET_RETVAL(call->data, rc);
2885 svoboda 126
        ipc_answer(&TASK->kernel_box, call);
2887 svoboda 127
        return;
2885 svoboda 128
    }
129
 
2887 svoboda 130
    if (rc != 0) {
131
        IPC_SET_RETVAL(call->data, 0);
132
        ipc_answer(&TASK->kernel_box, call);
2885 svoboda 133
    }
134
}
135
 
136
static void udebug_receive_end(call_t *call)
137
{
138
    int rc;
139
 
2887 svoboda 140
    rc = udebug_end();
2885 svoboda 141
 
2887 svoboda 142
    IPC_SET_RETVAL(call->data, rc);
2885 svoboda 143
    ipc_answer(&TASK->kernel_box, call);
144
}
145
 
2899 svoboda 146
static void udebug_receive_set_evmask(call_t *call)
147
{
148
    int rc;
149
    udebug_evmask_t mask;
150
 
151
    mask = IPC_GET_ARG2(call->data);
152
    rc = udebug_set_evmask(mask);
153
 
154
    IPC_SET_RETVAL(call->data, rc);
155
    ipc_answer(&TASK->kernel_box, call);
156
}
157
 
158
 
2886 svoboda 159
static void udebug_receive_go(call_t *call)
160
{
161
    thread_t *t;
162
    int rc;
163
 
3424 svoboda 164
    //printf("debug_go()\n");
2886 svoboda 165
 
166
    t = (thread_t *)IPC_GET_ARG2(call->data);
167
 
2887 svoboda 168
    rc = udebug_go(t, call);
169
    if (rc < 0) {
2886 svoboda 170
        IPC_SET_RETVAL(call->data, rc);
171
        ipc_answer(&TASK->kernel_box, call);
172
        return;
173
    }
174
}
175
 
2898 svoboda 176
static void udebug_receive_stop(call_t *call)
177
{
178
    thread_t *t;
179
    int rc;
2886 svoboda 180
 
3424 svoboda 181
    printf("debug_stop()\n");
2898 svoboda 182
 
183
    t = (thread_t *)IPC_GET_ARG2(call->data);
184
 
185
    rc = udebug_stop(t, call);
186
    IPC_SET_RETVAL(call->data, rc);
187
    ipc_answer(&TASK->kernel_box, call);
188
}
189
 
2885 svoboda 190
static void udebug_receive_thread_read(call_t *call)
191
{
192
    unative_t uspace_addr;
2813 svoboda 193
    unative_t to_copy;
2824 svoboda 194
    unsigned total_bytes;
2813 svoboda 195
    unsigned buf_size;
2887 svoboda 196
    void *buffer;
197
    size_t n;
198
    int rc;
2813 svoboda 199
 
2897 svoboda 200
    uspace_addr = IPC_GET_ARG2(call->data); /* Destination address */
201
    buf_size = IPC_GET_ARG3(call->data);    /* Dest. buffer size */
202
 
203
    /*
204
     * Read thread list. Variable n will be filled with actual number
205
     * of threads times thread-id size.
206
     */
207
    rc = udebug_thread_read(&buffer, buf_size, &n);
2887 svoboda 208
    if (rc < 0) {
209
        IPC_SET_RETVAL(call->data, rc);
2885 svoboda 210
        ipc_answer(&TASK->kernel_box, call);
211
        return;
2827 svoboda 212
    }
213
 
2897 svoboda 214
    total_bytes = n;
2813 svoboda 215
 
2897 svoboda 216
    /* Copy MAX(buf_size, total_bytes) bytes */
2813 svoboda 217
 
2824 svoboda 218
    if (buf_size > total_bytes)
219
        to_copy = total_bytes;
220
    else
221
        to_copy = buf_size;
222
 
2897 svoboda 223
    /*
224
     * Make use of call->buffer to transfer data to caller's userspace
225
     */
226
 
2885 svoboda 227
    IPC_SET_RETVAL(call->data, 0);
228
    /* ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
229
       same code in process_answer() can be used
230
       (no way to distinguish method in answer) */
231
    IPC_SET_ARG1(call->data, uspace_addr);
232
    IPC_SET_ARG2(call->data, to_copy);
2824 svoboda 233
 
2885 svoboda 234
    IPC_SET_ARG3(call->data, total_bytes);
2887 svoboda 235
    call->buffer = buffer;
2813 svoboda 236
 
2885 svoboda 237
    ipc_answer(&TASK->kernel_box, call);
2813 svoboda 238
}
239
 
2886 svoboda 240
static void udebug_receive_args_read(call_t *call)
241
{
242
    thread_t *t;
243
    unative_t uspace_addr;
244
    int rc;
2887 svoboda 245
    void *buffer;
2818 svoboda 246
 
2886 svoboda 247
    t = (thread_t *)IPC_GET_ARG2(call->data);
248
 
2887 svoboda 249
    rc = udebug_args_read(t, &buffer);
2886 svoboda 250
    if (rc != EOK) {
251
        IPC_SET_RETVAL(call->data, rc);
252
        ipc_answer(&TASK->kernel_box, call);
253
        return;
254
    }
255
 
256
    /*
257
     * Make use of call->buffer to transfer data to caller's userspace
258
     */
259
 
260
    uspace_addr = IPC_GET_ARG3(call->data);
261
 
262
    IPC_SET_RETVAL(call->data, 0);
263
    /* ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
264
       same code in process_answer() can be used
265
       (no way to distinguish method in answer) */
266
    IPC_SET_ARG1(call->data, uspace_addr);
267
    IPC_SET_ARG2(call->data, 6 * sizeof(unative_t));
2887 svoboda 268
    call->buffer = buffer;
2886 svoboda 269
 
270
    ipc_answer(&TASK->kernel_box, call);
271
}
272
 
273
static void udebug_receive_regs_read(call_t *call)
274
{
275
    thread_t *t;
276
    unative_t uspace_addr;
277
    unative_t to_copy;
278
    void *buffer;
279
    int rc;
280
 
3424 svoboda 281
    printf("debug_regs_read()\n");
2886 svoboda 282
 
283
    t = (thread_t *) IPC_GET_ARG2(call->data);
2919 svoboda 284
    buffer = malloc(sizeof(istate_t), 0);
2886 svoboda 285
 
2919 svoboda 286
    rc = udebug_regs_read(t, buffer);
2887 svoboda 287
    if (rc < 0) {
2886 svoboda 288
        IPC_SET_RETVAL(call->data, rc);
289
        ipc_answer(&TASK->kernel_box, call);
290
        return;
291
    }
292
 
293
    /*
294
     * Make use of call->buffer to transfer data to caller's userspace
295
     */
296
 
297
    uspace_addr = IPC_GET_ARG3(call->data);
2919 svoboda 298
    to_copy = sizeof(istate_t);
2886 svoboda 299
 
300
    IPC_SET_RETVAL(call->data, 0);
301
    /* ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
302
       same code in process_answer() can be used
303
       (no way to distinguish method in answer) */
304
    IPC_SET_ARG1(call->data, uspace_addr);
305
    IPC_SET_ARG2(call->data, to_copy);
306
 
2887 svoboda 307
    call->buffer = buffer;
2886 svoboda 308
 
309
    ipc_answer(&TASK->kernel_box, call);
310
}
311
 
312
static void udebug_receive_regs_write(call_t *call)
313
{
314
    thread_t *t;
315
    void *uspace_data;
316
    int rc;
317
 
2919 svoboda 318
    t = (thread_t *) IPC_GET_ARG2(call->data);
2886 svoboda 319
    uspace_data = (void *)IPC_GET_ARG3(call->data);
320
 
2887 svoboda 321
    rc = udebug_regs_write(t, call->buffer);
322
    if (rc < 0) {
2886 svoboda 323
        IPC_SET_RETVAL(call->data, rc);
324
        ipc_answer(&TASK->kernel_box, call);
325
        return;
326
    }
327
 
328
    /* Set answer values */
329
 
330
    IPC_SET_RETVAL(call->data, 0);
2887 svoboda 331
    free(call->buffer);
332
    call->buffer = NULL;
333
 
2886 svoboda 334
    ipc_answer(&TASK->kernel_box, call);
335
}
336
 
337
 
2815 svoboda 338
static void udebug_receive_mem_read(call_t *call)
339
{
340
    unative_t uspace_dst;
2887 svoboda 341
    unative_t uspace_src;
2815 svoboda 342
    unsigned size;
343
    void *buffer;
344
    int rc;
2813 svoboda 345
 
2815 svoboda 346
    uspace_dst = IPC_GET_ARG2(call->data);
2887 svoboda 347
    uspace_src = IPC_GET_ARG3(call->data);
2815 svoboda 348
    size = IPC_GET_ARG4(call->data);
349
 
2887 svoboda 350
    rc = udebug_mem_read(uspace_src, size, &buffer);
351
    if (rc < 0) {
2815 svoboda 352
        IPC_SET_RETVAL(call->data, rc);
2886 svoboda 353
        ipc_answer(&TASK->kernel_box, call);
2815 svoboda 354
        return;
355
    }
356
 
357
    IPC_SET_RETVAL(call->data, 0);
2887 svoboda 358
    /* ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
2815 svoboda 359
       same code in process_answer() can be used
360
       (no way to distinguish method in answer) */
361
    IPC_SET_ARG1(call->data, uspace_dst);
362
    IPC_SET_ARG2(call->data, size);
363
    call->buffer = buffer;
364
 
365
    ipc_answer(&TASK->kernel_box, call);
366
}
367
 
2818 svoboda 368
static void udebug_receive_mem_write(call_t *call)
369
{
2887 svoboda 370
    unative_t uspace_dst;
2818 svoboda 371
    unsigned size;
372
    int rc;
373
 
3424 svoboda 374
    printf("udebug_receive_mem_write()\n");
2827 svoboda 375
 
2887 svoboda 376
    uspace_dst = IPC_GET_ARG3(call->data);
2818 svoboda 377
    size = IPC_GET_ARG4(call->data);
378
 
2887 svoboda 379
    rc = udebug_mem_write(uspace_dst, call->buffer, size);
380
    if (rc < 0) {
2818 svoboda 381
        IPC_SET_RETVAL(call->data, rc);
2827 svoboda 382
        ipc_answer(&TASK->kernel_box, call);
2818 svoboda 383
        return;
384
    }
385
 
386
    IPC_SET_RETVAL(call->data, 0);
387
    free(call->buffer);
388
    call->buffer = NULL;
389
 
390
    ipc_answer(&TASK->kernel_box, call);
391
}
392
 
393
 
2815 svoboda 394
/**
395
 * Handle a debug call received on the kernel answerbox.
396
 *
397
 * This is called by the kbox servicing thread.
398
 */
399
void udebug_call_receive(call_t *call)
400
{
401
    int debug_method;
402
 
403
    debug_method = IPC_GET_ARG1(call->data);
404
 
2888 svoboda 405
    if (debug_method != UDEBUG_M_BEGIN) {
406
        /*
407
         * Verify that the sender is this task's debugger.
408
         * Note that this is the only thread that could change
409
         * TASK->debugger. Therefore no locking is necessary
410
         * and the sender can be safely considered valid until
411
         * control exits this function.
412
         */
3014 svoboda 413
        if (TASK->udebug.debugger != call->sender) {
2888 svoboda 414
            IPC_SET_RETVAL(call->data, EINVAL);
415
            ipc_answer(&TASK->kernel_box, call);
416
            return;
417
        }
418
    }
419
 
2815 svoboda 420
    switch (debug_method) {
2885 svoboda 421
    case UDEBUG_M_BEGIN:
422
        udebug_receive_begin(call);
423
        break;
424
    case UDEBUG_M_END:
425
        udebug_receive_end(call);
426
        break;
2899 svoboda 427
    case UDEBUG_M_SET_EVMASK:
428
        udebug_receive_set_evmask(call);
429
        break;
2886 svoboda 430
    case UDEBUG_M_GO:
431
        udebug_receive_go(call);
432
        break;
2898 svoboda 433
    case UDEBUG_M_STOP:
434
        udebug_receive_stop(call);
435
        break;
2885 svoboda 436
    case UDEBUG_M_THREAD_READ:
437
        udebug_receive_thread_read(call);
438
        break;
2886 svoboda 439
    case UDEBUG_M_ARGS_READ:
440
        udebug_receive_args_read(call);
441
        break;
442
    case UDEBUG_M_REGS_READ:
443
        udebug_receive_regs_read(call);
444
        break;
445
    case UDEBUG_M_REGS_WRITE:
446
        udebug_receive_regs_write(call);
447
        break;
2815 svoboda 448
    case UDEBUG_M_MEM_READ:
449
        udebug_receive_mem_read(call);
450
        break;
2818 svoboda 451
    case UDEBUG_M_MEM_WRITE:
452
        udebug_receive_mem_write(call);
453
        break;
2815 svoboda 454
    }
455
}
456
 
2813 svoboda 457
/** @}
458
 */