Subversion Repositories HelenOS

Rev

Rev 2894 | Rev 2897 | 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
 
38
#include <console/klog.h>
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
 
56
    klog_printf("debug_regs_write()");
57
 
58
    uspace_data = (void *)IPC_GET_ARG3(call->data);
59
    to_copy = IPC_GET_ARG4(call->data);
60
    if (to_copy > sizeof(istate_t)) to_copy = sizeof(istate_t);
61
 
2896 svoboda 62
    buffer = malloc(to_copy, 0);
63
    if (!buffer) return ENOMEM;
2886 svoboda 64
 
65
    rc = copy_from_uspace(buffer, uspace_data, to_copy);
2817 svoboda 66
    if (rc != 0) {
67
        klog_printf("debug_regs_write() - copy failed");
68
        return rc;
69
    }
70
 
2886 svoboda 71
    call->buffer = buffer;
2824 svoboda 72
 
2886 svoboda 73
    klog_printf(" - done");
2892 svoboda 74
    return 0;
2817 svoboda 75
}
76
 
2885 svoboda 77
static int udebug_rp_mem_write(call_t *call, phone_t *phone)
2813 svoboda 78
{
2885 svoboda 79
    void *uspace_data;
80
    unative_t to_copy;
81
    int rc;
82
    void *buffer;
83
 
84
    klog_printf("udebug_rp_mem_write()");
85
 
86
    uspace_data = (void *)IPC_GET_ARG2(call->data);
87
    to_copy = IPC_GET_ARG4(call->data);
88
 
2896 svoboda 89
    buffer = malloc(to_copy, 0);
90
    if (!buffer) return ENOMEM;
2885 svoboda 91
 
92
    rc = copy_from_uspace(buffer, uspace_data, to_copy);
93
    if (rc != 0) {
94
        klog_printf(" - copy failed");
95
        return rc;
96
    }
97
 
98
    call->buffer = buffer;
99
 
100
    klog_printf(" - done");
2892 svoboda 101
    return 0;
2885 svoboda 102
}
103
 
104
 
105
int udebug_request_preprocess(call_t *call, phone_t *phone)
106
{
107
    int rc;
108
 
109
    switch (IPC_GET_ARG1(call->data)) {
110
    case UDEBUG_M_REGS_WRITE:
111
        rc = udebug_rp_regs_write(call, phone);
112
        return rc;
113
    case UDEBUG_M_MEM_WRITE:
114
        rc = udebug_rp_mem_write(call, phone);
115
        return rc;
116
    default:
117
        break;
118
    }
119
 
120
    return 0;
121
}
122
 
123
static void udebug_receive_begin(call_t *call)
124
{
2887 svoboda 125
    int rc;
2885 svoboda 126
 
2887 svoboda 127
    rc = udebug_begin(call);
128
    if (rc < 0) {
129
        IPC_SET_RETVAL(call->data, rc);
2885 svoboda 130
        ipc_answer(&TASK->kernel_box, call);
2887 svoboda 131
        return;
2885 svoboda 132
    }
133
 
2887 svoboda 134
    if (rc != 0) {
135
        IPC_SET_RETVAL(call->data, 0);
136
        ipc_answer(&TASK->kernel_box, call);
2885 svoboda 137
    }
138
}
139
 
140
static void udebug_receive_end(call_t *call)
141
{
142
    int rc;
143
 
2887 svoboda 144
    rc = udebug_end();
2885 svoboda 145
 
2887 svoboda 146
    IPC_SET_RETVAL(call->data, rc);
2885 svoboda 147
    ipc_answer(&TASK->kernel_box, call);
148
}
149
 
2886 svoboda 150
static void udebug_receive_go(call_t *call)
151
{
152
    thread_t *t;
153
    int rc;
154
 
155
    klog_printf("debug_go()");
156
 
157
    t = (thread_t *)IPC_GET_ARG2(call->data);
158
 
2887 svoboda 159
    rc = udebug_go(t, call);
160
    if (rc < 0) {
2886 svoboda 161
        IPC_SET_RETVAL(call->data, rc);
162
        ipc_answer(&TASK->kernel_box, call);
163
        return;
164
    }
165
}
166
 
167
 
2885 svoboda 168
static void udebug_receive_thread_read(call_t *call)
169
{
170
    unative_t uspace_addr;
2813 svoboda 171
    unative_t to_copy;
2824 svoboda 172
    unsigned total_bytes;
2813 svoboda 173
    unsigned buf_size;
2887 svoboda 174
    void *buffer;
175
    size_t n;
176
    int rc;
2813 svoboda 177
 
2887 svoboda 178
    rc = udebug_thread_read(&buffer, &n);
179
    if (rc < 0) {
180
        IPC_SET_RETVAL(call->data, rc);
2885 svoboda 181
        ipc_answer(&TASK->kernel_box, call);
182
        return;
2827 svoboda 183
    }
184
 
2885 svoboda 185
    /*
2887 svoboda 186
     * Make use of call->buffer to transfer data to caller's userspace
2885 svoboda 187
     */
2813 svoboda 188
 
2885 svoboda 189
    uspace_addr = IPC_GET_ARG2(call->data);
2824 svoboda 190
    buf_size = IPC_GET_ARG3(call->data);
2813 svoboda 191
 
2887 svoboda 192
    total_bytes = n;
2824 svoboda 193
 
194
    if (buf_size > total_bytes)
195
        to_copy = total_bytes;
196
    else
197
        to_copy = buf_size;
198
 
2885 svoboda 199
    IPC_SET_RETVAL(call->data, 0);
200
    /* ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
201
       same code in process_answer() can be used
202
       (no way to distinguish method in answer) */
203
    IPC_SET_ARG1(call->data, uspace_addr);
204
    IPC_SET_ARG2(call->data, to_copy);
2824 svoboda 205
 
2885 svoboda 206
    IPC_SET_ARG3(call->data, total_bytes);
2887 svoboda 207
    call->buffer = buffer;
2813 svoboda 208
 
2885 svoboda 209
    ipc_answer(&TASK->kernel_box, call);
2813 svoboda 210
}
211
 
2886 svoboda 212
static void udebug_receive_args_read(call_t *call)
213
{
214
    thread_t *t;
215
    unative_t uspace_addr;
216
    int rc;
2887 svoboda 217
    void *buffer;
2818 svoboda 218
 
2886 svoboda 219
    t = (thread_t *)IPC_GET_ARG2(call->data);
220
 
2887 svoboda 221
    rc = udebug_args_read(t, &buffer);
2886 svoboda 222
    if (rc != EOK) {
223
        IPC_SET_RETVAL(call->data, rc);
224
        ipc_answer(&TASK->kernel_box, call);
225
        return;
226
    }
227
 
228
    /*
229
     * Make use of call->buffer to transfer data to caller's userspace
230
     */
231
 
232
    uspace_addr = IPC_GET_ARG3(call->data);
233
 
234
    IPC_SET_RETVAL(call->data, 0);
235
    /* ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
236
       same code in process_answer() can be used
237
       (no way to distinguish method in answer) */
238
    IPC_SET_ARG1(call->data, uspace_addr);
239
    IPC_SET_ARG2(call->data, 6 * sizeof(unative_t));
2887 svoboda 240
    call->buffer = buffer;
2886 svoboda 241
 
242
    ipc_answer(&TASK->kernel_box, call);
243
}
244
 
245
static void udebug_receive_regs_read(call_t *call)
246
{
247
    thread_t *t;
248
    unative_t uspace_addr;
249
    unative_t to_copy;
250
    unative_t buf_size;
251
    unative_t total_bytes;
252
    void *buffer;
253
    int rc;
2887 svoboda 254
    size_t n;
2886 svoboda 255
 
256
    klog_printf("debug_regs_read()");
257
 
258
    t = (thread_t *) IPC_GET_ARG2(call->data);
259
 
2887 svoboda 260
    rc = udebug_regs_read(t, &buffer, &n);
261
    if (rc < 0) {
2886 svoboda 262
        IPC_SET_RETVAL(call->data, rc);
263
        ipc_answer(&TASK->kernel_box, call);
264
        return;
265
    }
266
 
267
    /*
268
     * Make use of call->buffer to transfer data to caller's userspace
269
     */
270
 
271
    uspace_addr = IPC_GET_ARG3(call->data);
272
    buf_size = IPC_GET_ARG4(call->data);
273
 
2887 svoboda 274
    total_bytes = n;
2886 svoboda 275
 
276
    if (buf_size > total_bytes)
277
        to_copy = total_bytes;
278
    else
279
        to_copy = buf_size;
280
 
281
    IPC_SET_RETVAL(call->data, 0);
282
    /* ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
283
       same code in process_answer() can be used
284
       (no way to distinguish method in answer) */
285
    IPC_SET_ARG1(call->data, uspace_addr);
286
    IPC_SET_ARG2(call->data, to_copy);
287
 
288
    IPC_SET_ARG3(call->data, total_bytes);
2887 svoboda 289
    call->buffer = buffer;
2886 svoboda 290
 
291
    ipc_answer(&TASK->kernel_box, call);
292
}
293
 
294
static void udebug_receive_regs_write(call_t *call)
295
{
296
    thread_t *t;
297
    void *uspace_data;
298
    unative_t to_copy;
299
    int rc;
300
 
301
    uspace_data = (void *)IPC_GET_ARG3(call->data);
302
    to_copy = IPC_GET_ARG4(call->data);
303
 
304
    t = (thread_t *) IPC_GET_ARG2(call->data);
305
 
2887 svoboda 306
    rc = udebug_regs_write(t, call->buffer);
307
    if (rc < 0) {
2886 svoboda 308
        IPC_SET_RETVAL(call->data, rc);
309
        ipc_answer(&TASK->kernel_box, call);
310
        return;
311
    }
312
 
313
    /* Set answer values */
314
 
315
    IPC_SET_ARG1(call->data, to_copy);
316
    IPC_SET_ARG2(call->data, sizeof(istate_t));
317
 
318
    IPC_SET_RETVAL(call->data, 0);
2887 svoboda 319
    free(call->buffer);
320
    call->buffer = NULL;
321
 
2886 svoboda 322
    ipc_answer(&TASK->kernel_box, call);
323
}
324
 
325
 
2815 svoboda 326
static void udebug_receive_mem_read(call_t *call)
327
{
328
    unative_t uspace_dst;
2887 svoboda 329
    unative_t uspace_src;
2815 svoboda 330
    unsigned size;
331
    void *buffer;
332
    int rc;
2813 svoboda 333
 
2815 svoboda 334
    uspace_dst = IPC_GET_ARG2(call->data);
2887 svoboda 335
    uspace_src = IPC_GET_ARG3(call->data);
2815 svoboda 336
    size = IPC_GET_ARG4(call->data);
337
 
2887 svoboda 338
    rc = udebug_mem_read(uspace_src, size, &buffer);
339
    if (rc < 0) {
2815 svoboda 340
        IPC_SET_RETVAL(call->data, rc);
2886 svoboda 341
        ipc_answer(&TASK->kernel_box, call);
2815 svoboda 342
        return;
343
    }
344
 
345
    IPC_SET_RETVAL(call->data, 0);
2887 svoboda 346
    /* ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
2815 svoboda 347
       same code in process_answer() can be used
348
       (no way to distinguish method in answer) */
349
    IPC_SET_ARG1(call->data, uspace_dst);
350
    IPC_SET_ARG2(call->data, size);
351
    call->buffer = buffer;
352
 
353
    ipc_answer(&TASK->kernel_box, call);
354
}
355
 
2818 svoboda 356
static void udebug_receive_mem_write(call_t *call)
357
{
2887 svoboda 358
    unative_t uspace_dst;
2818 svoboda 359
    unsigned size;
360
    int rc;
361
 
362
    klog_printf("udebug_receive_mem_write()");
2827 svoboda 363
 
2887 svoboda 364
    uspace_dst = IPC_GET_ARG3(call->data);
2818 svoboda 365
    size = IPC_GET_ARG4(call->data);
366
 
2887 svoboda 367
    rc = udebug_mem_write(uspace_dst, call->buffer, size);
368
    if (rc < 0) {
2818 svoboda 369
        IPC_SET_RETVAL(call->data, rc);
2827 svoboda 370
        ipc_answer(&TASK->kernel_box, call);
2818 svoboda 371
        return;
372
    }
373
 
374
    IPC_SET_RETVAL(call->data, 0);
375
    free(call->buffer);
376
    call->buffer = NULL;
377
 
378
    ipc_answer(&TASK->kernel_box, call);
379
}
380
 
381
 
2815 svoboda 382
/**
383
 * Handle a debug call received on the kernel answerbox.
384
 *
385
 * This is called by the kbox servicing thread.
386
 */
387
void udebug_call_receive(call_t *call)
388
{
389
    int debug_method;
390
 
391
    debug_method = IPC_GET_ARG1(call->data);
392
 
2888 svoboda 393
    if (debug_method != UDEBUG_M_BEGIN) {
394
        /*
395
         * Verify that the sender is this task's debugger.
396
         * Note that this is the only thread that could change
397
         * TASK->debugger. Therefore no locking is necessary
398
         * and the sender can be safely considered valid until
399
         * control exits this function.
400
         */
401
        if (TASK->debugger != call->sender) {
402
            IPC_SET_RETVAL(call->data, EINVAL);
403
            ipc_answer(&TASK->kernel_box, call);
404
            return;
405
        }
406
    }
407
 
2815 svoboda 408
    switch (debug_method) {
2885 svoboda 409
    case UDEBUG_M_BEGIN:
410
        udebug_receive_begin(call);
411
        break;
412
    case UDEBUG_M_END:
413
        udebug_receive_end(call);
414
        break;
2886 svoboda 415
    case UDEBUG_M_GO:
416
        udebug_receive_go(call);
417
        break;
2885 svoboda 418
    case UDEBUG_M_THREAD_READ:
419
        udebug_receive_thread_read(call);
420
        break;
2886 svoboda 421
    case UDEBUG_M_ARGS_READ:
422
        udebug_receive_args_read(call);
423
        break;
424
    case UDEBUG_M_REGS_READ:
425
        udebug_receive_regs_read(call);
426
        break;
427
    case UDEBUG_M_REGS_WRITE:
428
        udebug_receive_regs_write(call);
429
        break;
2815 svoboda 430
    case UDEBUG_M_MEM_READ:
431
        udebug_receive_mem_read(call);
432
        break;
2818 svoboda 433
    case UDEBUG_M_MEM_WRITE:
434
        udebug_receive_mem_write(call);
435
        break;
2815 svoboda 436
    }
437
}
438
 
2813 svoboda 439
/** @}
440
 */