Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
2813 svoboda 1
/** @addtogroup generic
2
 * @{
3
 */
4
 
5
/**
6
 * @file
7
 * @brief   Tdebug.
8
 */
9
 
10
#include <console/klog.h>
11
#include <proc/task.h>
12
#include <proc/thread.h>
2815 svoboda 13
#include <arch.h>
2813 svoboda 14
#include <errno.h>
15
#include <ipc/ipc.h>
16
#include <syscall/copy.h>
17
#include <udebug/udebug.h>
18
#include <udebug/udebug_ipc.h>
19
 
2823 svoboda 20
/**
21
 * Get and lock a phone's callee task.
22
 *
23
 * This will return a pointer to the task to which the phone
24
 * is connected. It will lock the task, making sure it exists.
25
 * (TODO: make sure the udebug-cleanup of the task hasn't
26
 * started yet)
27
 */
2813 svoboda 28
static task_t *get_lock_callee_task(phone_t *phone)
29
{
2823 svoboda 30
    answerbox_t *box;
2813 svoboda 31
    task_t *ta;
2823 svoboda 32
    task_id_t taskid;
33
    ipl_t ipl;
2813 svoboda 34
 
2823 svoboda 35
    ipl = interrupts_disable();
36
    spinlock_lock(&phone->lock);
37
    if (phone->state != IPC_PHONE_CONNECTED) {
38
        spinlock_unlock(&phone->lock);
39
        interrupts_restore(ipl);
40
        return NULL;
41
    }
2813 svoboda 42
 
2823 svoboda 43
    box = phone->callee;
44
 
45
    spinlock_lock(&box->lock);
46
    ta = box->task;
47
    taskid = ta->taskid;
48
    spinlock_unlock(&box->lock);
49
    spinlock_unlock(&phone->lock);
2813 svoboda 50
 
2823 svoboda 51
    /* Locking decoupled using taskid */
52
 
53
    spinlock_lock(&tasks_lock);
54
    ta = task_find_by_id(taskid);
55
    if (ta == NULL) {
56
        spinlock_unlock(&tasks_lock);
57
        interrupts_restore(ipl);
58
        return NULL;
59
    }
60
 
2813 svoboda 61
    spinlock_lock(&ta->lock);
2823 svoboda 62
    spinlock_unlock(&tasks_lock);
63
    interrupts_restore(ipl);
2813 svoboda 64
 
65
    return ta;
66
}
67
 
68
static int udebug_rp_begin(call_t *call, phone_t *phone)
69
{
70
    task_t *ta;
2823 svoboda 71
    ipl_t ipl;
2827 svoboda 72
    int rc;
2813 svoboda 73
 
2827 svoboda 74
    thread_t *t;
75
    link_t *cur;
76
 
2813 svoboda 77
    klog_printf("debug_begin()");
78
 
2823 svoboda 79
    ipl = interrupts_disable();
2813 svoboda 80
    ta = get_lock_callee_task(phone);
81
    klog_printf("debugging task %llu", ta->taskid);
82
 
2825 svoboda 83
    if (ta->dt_state != UDEBUG_TS_INACTIVE) {
2813 svoboda 84
        spinlock_unlock(&ta->lock);
2823 svoboda 85
        interrupts_restore(ipl);
2813 svoboda 86
        klog_printf("debug_begin(): busy error");
87
        return EBUSY;
88
    }
89
 
2825 svoboda 90
    ta->dt_state = UDEBUG_TS_BEGINNING;
2813 svoboda 91
    ta->debug_begin_call = call;
92
 
93
    if (ta->not_stoppable_count == 0) {
2825 svoboda 94
        ta->dt_state = UDEBUG_TS_ACTIVE;
2813 svoboda 95
        ta->debug_begin_call = NULL;
2827 svoboda 96
        rc = 1; /* actually we need backsend with 0 retval */
97
    } else {
98
        rc = 0; /* no backsend */
2813 svoboda 99
    }
2827 svoboda 100
 
101
    /* Set debug_active on all of the task's userspace threads */
2813 svoboda 102
 
2827 svoboda 103
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
104
        t = list_get_instance(cur, thread_t, th_link);
105
 
106
        spinlock_lock(&t->lock);
107
        if ((t->flags & THREAD_FLAG_USPACE) != 0)
108
            t->debug_active = true;
109
        spinlock_unlock(&t->lock);
110
    }
111
 
2813 svoboda 112
    spinlock_unlock(&ta->lock);
2823 svoboda 113
    interrupts_restore(ipl);
2813 svoboda 114
 
2827 svoboda 115
    klog_printf("debug_begin() done (%s)",
116
        rc ? "backsend" : "stoppability wait");
117
 
118
    return rc;
2813 svoboda 119
}
120
 
121
static int udebug_rp_go(call_t *call, phone_t *phone)
122
{
123
    thread_t *t;
124
    task_t *ta;
2823 svoboda 125
    ipl_t ipl;
2813 svoboda 126
 
127
    klog_printf("debug_go()");
128
    ta = get_lock_callee_task(phone);
2823 svoboda 129
    spinlock_unlock(&ta->lock);
2826 svoboda 130
    // TODO: don't lock ta
2823 svoboda 131
 
2816 svoboda 132
    t = (thread_t *) IPC_GET_ARG2(call->data);
2823 svoboda 133
 
134
    ipl = interrupts_disable();
135
    spinlock_lock(&threads_lock);
2825 svoboda 136
 
2826 svoboda 137
    /* Verify that 't' exists and belongs to task 'ta' */
138
    if (!thread_exists(t) || (t->task != ta)) {
2823 svoboda 139
        spinlock_unlock(&threads_lock);
140
        interrupts_restore(ipl);
2813 svoboda 141
        return ENOENT;
142
    }
143
 
2827 svoboda 144
    if ((t->debug_active != true) || (t->debug_stop != true)) {
145
        /* Not in debugging session or already has GO */
146
        spinlock_unlock(&threads_lock);
147
        interrupts_restore(ipl);       
148
        return EBUSY;
149
    }
150
 
2826 svoboda 151
    t->debug_go_call = call;
2825 svoboda 152
    t->debug_stop = false;
2813 svoboda 153
    waitq_wakeup(&t->go_wq, WAKEUP_FIRST);
2825 svoboda 154
 
2823 svoboda 155
    spinlock_unlock(&threads_lock);
156
    interrupts_restore(ipl);
2813 svoboda 157
 
158
    return 0; /* no backsend */
159
}
160
 
161
static int udebug_rp_args_read(call_t *call, phone_t *phone)
162
{
163
    thread_t *t;
164
    task_t *ta;
165
    void *uspace_buffer;
166
    int rc;
2823 svoboda 167
    ipl_t ipl;
2827 svoboda 168
    unative_t buffer[6];
2813 svoboda 169
 
170
    klog_printf("debug_args_read()");
171
 
172
    ta = get_lock_callee_task(phone);
173
    klog_printf("task %llu", ta->taskid);
2823 svoboda 174
    spinlock_unlock(&ta->lock);
175
 
2816 svoboda 176
    t = (thread_t *) IPC_GET_ARG2(call->data);
2823 svoboda 177
 
178
    ipl = interrupts_disable();
179
    spinlock_lock(&threads_lock);
180
 
2827 svoboda 181
    /* Verify that 't' exists and belongs to task 'ta' */
182
    if (!thread_exists(t) || (t->task != ta)) {
2823 svoboda 183
        spinlock_unlock(&threads_lock);
184
        interrupts_restore(ipl);
2813 svoboda 185
        return ENOENT;
186
    }
187
 
2827 svoboda 188
    //FIXME: additionally we need to verify that we are inside a syscall
189
    if ((t->debug_active != true) || (t->debug_stop != true)) {
190
        /* Not in debugging session or has GO */
191
        spinlock_unlock(&threads_lock);
192
        interrupts_restore(ipl);       
193
        return EBUSY;
194
    }
195
 
196
    /* Copy to a local buffer before releasing the lock */
197
    memcpy(buffer, t->syscall_args, 6 * sizeof(unative_t));
198
 
2823 svoboda 199
    spinlock_unlock(&threads_lock);
200
    interrupts_restore(ipl);
201
 
2827 svoboda 202
    /* Now copy to userspace */
203
 
2813 svoboda 204
    uspace_buffer = (void *)IPC_GET_ARG3(call->data);
205
 
2833 svoboda 206
    rc = copy_to_uspace(uspace_buffer, buffer, 6 * sizeof(unative_t));
2813 svoboda 207
    if (rc != 0) {
208
        spinlock_unlock(&ta->lock);
209
        klog_printf("debug_args_read() - copy failed");
210
        return rc;
211
    }
212
 
213
    klog_printf("debug_args_read() done");
214
    return 1; /* actually need becksend with retval 0 */
215
}
216
 
2817 svoboda 217
static int udebug_rp_regs_read(call_t *call, phone_t *phone)
218
{
219
    thread_t *t;
220
    task_t *ta;
221
    void *uspace_buffer;
222
    unative_t to_copy;
223
    int rc;
224
    istate_t *state;
2824 svoboda 225
    istate_t state_copy;
226
    ipl_t ipl;
2817 svoboda 227
 
228
    klog_printf("debug_regs_read()");
229
 
2824 svoboda 230
    ta = get_lock_callee_task(phone);
231
    spinlock_unlock(&ta->lock);
2827 svoboda 232
    //FIXME: don't lock ta
2817 svoboda 233
 
2824 svoboda 234
    ipl = interrupts_disable();
235
    spinlock_lock(&threads_lock);
236
 
2817 svoboda 237
    t = (thread_t *) IPC_GET_ARG2(call->data);
2827 svoboda 238
 
239
    /* Verify that 't' exists and belongs to task 'ta' */
240
    if (!thread_exists(t) || (t->task != ta)) {
241
        spinlock_unlock(&threads_lock);
242
        interrupts_restore(ipl);
2817 svoboda 243
        return ENOENT;
244
    }
245
 
2827 svoboda 246
    if ((t->debug_active != true) || (t->debug_stop != true)) {
247
        /* Not in debugging session or has GO */
248
        spinlock_unlock(&threads_lock);
249
        interrupts_restore(ipl);       
250
        return EBUSY;
251
    }
252
 
2824 svoboda 253
    state = t->uspace_state;
254
    if (state == NULL) {
255
        spinlock_unlock(&threads_lock);
256
        interrupts_restore(ipl);
257
        klog_printf("debug_regs_read() - istate not available");
258
        return EBUSY;
259
    }
260
 
261
    /* Copy to a local buffer so that we can release the lock */
262
    memcpy(&state_copy, state, sizeof(state_copy));
263
    spinlock_unlock(&threads_lock);
264
    interrupts_restore(ipl);
265
 
2817 svoboda 266
    uspace_buffer = (void *)IPC_GET_ARG3(call->data);
267
    to_copy = IPC_GET_ARG4(call->data);
268
    if (to_copy > sizeof(istate_t)) to_copy = sizeof(istate_t);
269
 
2824 svoboda 270
    rc = copy_to_uspace(uspace_buffer, &state_copy, to_copy);
2817 svoboda 271
    if (rc != 0) {
272
        spinlock_unlock(&ta->lock);
273
        klog_printf("debug_regs_read() - copy failed");
274
        return rc;
275
    }
276
 
277
    IPC_SET_ARG1(call->data, to_copy);
278
    IPC_SET_ARG2(call->data, sizeof(istate_t));
279
 
280
    klog_printf("debug_regs_read() done");
281
    return 1; /* actually need becksend with retval 0 */
282
}
283
 
284
static int udebug_rp_regs_write(call_t *call, phone_t *phone)
285
{
286
    thread_t *t;
287
    task_t *ta;
288
    void *uspace_data;
289
    unative_t to_copy;
290
    int rc;
291
    istate_t *state;
2824 svoboda 292
    istate_t data_copy;
293
    ipl_t ipl;
2817 svoboda 294
 
295
    klog_printf("debug_regs_write()");
296
 
2824 svoboda 297
    /* First copy to a local buffer */
2817 svoboda 298
 
299
    uspace_data = (void *)IPC_GET_ARG3(call->data);
300
    to_copy = IPC_GET_ARG4(call->data);
301
    if (to_copy > sizeof(istate_t)) to_copy = sizeof(istate_t);
302
 
2824 svoboda 303
    rc = copy_from_uspace(&data_copy, uspace_data, to_copy);
2817 svoboda 304
    if (rc != 0) {
305
        klog_printf("debug_regs_write() - copy failed");
306
        return rc;
307
    }
308
 
2824 svoboda 309
    ta = get_lock_callee_task(phone);
2817 svoboda 310
    spinlock_unlock(&ta->lock);
2827 svoboda 311
    //FIXME: don't lock ta
2817 svoboda 312
 
2824 svoboda 313
    /* Now try to change the thread's uspace_state */
314
 
315
    ipl = interrupts_disable();
316
    spinlock_lock(&threads_lock);
317
 
318
    t = (thread_t *) IPC_GET_ARG2(call->data);
2827 svoboda 319
 
320
    /* Verify that 't' exists and belongs to task 'ta' */
321
    if (!thread_exists(t) || (t->task != ta)) {
2824 svoboda 322
        spinlock_unlock(&threads_lock);
323
        interrupts_restore(ipl);
324
        return ENOENT;
325
    }
326
 
2827 svoboda 327
    if ((t->debug_active != true) || (t->debug_stop != true)) {
328
        /* Not in debugging session or has GO */
329
        spinlock_unlock(&threads_lock);
330
        interrupts_restore(ipl);       
331
        return EBUSY;
332
    }
333
 
2824 svoboda 334
    state = t->uspace_state;
335
    if (state == NULL) {
336
        spinlock_unlock(&threads_lock);
337
        interrupts_restore(ipl);
338
        klog_printf("debug_regs_write() - istate not available");
339
        return EBUSY;
340
    }
341
 
342
    memcpy(t->uspace_state, &data_copy, sizeof(t->uspace_state));
343
 
344
    spinlock_unlock(&threads_lock);
345
    interrupts_restore(ipl);
346
 
347
    /* Set answer values */
348
 
2817 svoboda 349
    IPC_SET_ARG1(call->data, to_copy);
350
    IPC_SET_ARG2(call->data, sizeof(istate_t));
351
 
352
    klog_printf("debug_regs_write() done");
353
    return 1; /* actually need becksend with retval 0 */
354
}
355
 
2813 svoboda 356
static int udebug_rp_thread_read(call_t *call, phone_t *phone)
357
{
358
    thread_t *t;
359
    link_t *cur;
360
    task_t *ta;
361
    unative_t *uspace_buffer;
362
    unative_t to_copy;
363
    int rc;
2824 svoboda 364
    unsigned total_bytes;
2813 svoboda 365
    unsigned buf_size;
366
    unative_t tid;
2824 svoboda 367
    unsigned num_threads, copied_ids;
368
    ipl_t ipl;
369
    unative_t *buffer;
370
    int flags;
2813 svoboda 371
 
372
    klog_printf("debug_thread_read()");
373
 
2824 svoboda 374
    ipl = interrupts_disable();
2813 svoboda 375
    ta = get_lock_callee_task(phone);
376
 
2827 svoboda 377
    /* Verify task state */
378
    if (ta->dt_state != UDEBUG_TS_ACTIVE) {
379
        spinlock_unlock(&ta->lock);
380
        interrupts_restore(ipl);
381
        return EBUSY;
382
    }
383
 
2824 svoboda 384
    /* Count the threads first */
385
 
386
    num_threads = 0;
2813 svoboda 387
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
2824 svoboda 388
        /* Count all threads, to be on the safe side */
389
        ++num_threads;
390
    }
391
 
392
    /* Allocate a buffer and copy down the threads' ids */
393
    buffer = malloc(num_threads * sizeof(unative_t), 0); // ???
394
 
395
    copied_ids = 0;
396
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
2813 svoboda 397
        t = list_get_instance(cur, thread_t, th_link);
398
 
2824 svoboda 399
        spinlock_lock(&t->lock);
400
        flags = t->flags;
401
        spinlock_unlock(&t->lock);
402
 
2813 svoboda 403
        /* Not interested in kernel threads */
2824 svoboda 404
        if ((flags & THREAD_FLAG_USPACE) != 0) {
405
            /* Using thread struct pointer for identification */
406
            tid = (unative_t) t;
407
            buffer[copied_ids++] = tid;
408
        }
409
    }
2813 svoboda 410
 
2824 svoboda 411
    spinlock_unlock(&ta->lock);
412
    interrupts_restore(ipl);
2813 svoboda 413
 
2824 svoboda 414
    /* Now copy to userspace */
2813 svoboda 415
 
2824 svoboda 416
    uspace_buffer = (void *)IPC_GET_ARG2(call->data);
417
    buf_size = IPC_GET_ARG3(call->data);
2813 svoboda 418
 
2824 svoboda 419
    total_bytes = copied_ids * sizeof(unative_t);
420
 
421
    if (buf_size > total_bytes)
422
        to_copy = total_bytes;
423
    else
424
        to_copy = buf_size;
425
 
426
    rc = copy_to_uspace(uspace_buffer, buffer, to_copy);
427
    free(buffer);
428
 
429
    if (rc != 0) {
430
        klog_printf("debug_thread_read() - copy failed");
431
        return rc;
2813 svoboda 432
    }
433
 
2824 svoboda 434
    IPC_SET_ARG1(call->data, to_copy);
435
    IPC_SET_ARG2(call->data, total_bytes);
2813 svoboda 436
 
437
    klog_printf("debug_thread_read() done");
438
    return 1; /* actually need becksend with retval 0 */
439
}
440
 
2818 svoboda 441
static int udebug_rp_mem_write(call_t *call, phone_t *phone)
442
{
443
    void *uspace_data;
444
    unative_t to_copy;
445
    int rc;
446
    void *buffer;
447
 
448
    klog_printf("udebug_rp_mem_write()");
449
 
450
    uspace_data = (void *)IPC_GET_ARG2(call->data);
451
    to_copy = IPC_GET_ARG4(call->data);
452
 
453
    buffer = malloc(to_copy, 0); // ???
454
 
455
    rc = copy_from_uspace(buffer, uspace_data, to_copy);
456
    if (rc != 0) {
457
        klog_printf(" - copy failed");
458
        return rc;
459
    }
460
 
461
    call->buffer = buffer;
462
 
463
    klog_printf(" - done");
464
    return 1; /* actually need becksend with retval 0 */
465
}
466
 
467
 
2813 svoboda 468
int udebug_request_preprocess(call_t *call, phone_t *phone)
469
{
470
    int rc;
471
 
472
    switch (IPC_GET_ARG1(call->data)) {
473
    case UDEBUG_M_BEGIN:
474
        rc = udebug_rp_begin(call, phone);
475
        return rc;
476
    case UDEBUG_M_GO:
477
        rc = udebug_rp_go(call, phone);
478
        return rc;
479
    case UDEBUG_M_ARGS_READ:
480
        rc = udebug_rp_args_read(call, phone);
481
        return rc;
2817 svoboda 482
    case UDEBUG_M_REGS_READ:
483
        rc = udebug_rp_regs_read(call, phone);
484
        return rc;
485
    case UDEBUG_M_REGS_WRITE:
486
        rc = udebug_rp_regs_write(call, phone);
487
        return rc;
2813 svoboda 488
    case UDEBUG_M_THREAD_READ:
489
        rc = udebug_rp_thread_read(call, phone);
2819 svoboda 490
        return rc;
2818 svoboda 491
    case UDEBUG_M_MEM_WRITE:
492
        rc = udebug_rp_mem_write(call, phone);
2813 svoboda 493
        return rc;
494
    default:
495
        break;
496
    }
497
 
498
    return 0;
499
}
500
 
2815 svoboda 501
static void udebug_receive_mem_read(call_t *call)
502
{
503
    unative_t uspace_dst;
504
    void *uspace_ptr;
505
    unsigned size;
506
    void *buffer;
507
    int rc;
2813 svoboda 508
 
2815 svoboda 509
    klog_printf("debug_mem_read()");
510
    uspace_dst = IPC_GET_ARG2(call->data);
511
    uspace_ptr = (void *)IPC_GET_ARG3(call->data);
512
    size = IPC_GET_ARG4(call->data);
513
 
514
    buffer = malloc(size, 0); // ???
515
    klog_printf("debug_mem_read: src=%u, size=%u", uspace_ptr, size);
516
 
517
    /* NOTE: this is not strictly from a syscall... but that shouldn't
518
     * be a problem */
519
    rc = copy_from_uspace(buffer, uspace_ptr, size);
520
    if (rc) {
521
        IPC_SET_RETVAL(call->data, rc);
522
        return;
523
    }
524
 
525
    klog_printf("first word: %u", *((unative_t *)buffer));
526
 
527
    IPC_SET_RETVAL(call->data, 0);
528
    /* Hack: ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
529
       same code in process_answer() can be used
530
       (no way to distinguish method in answer) */
531
    IPC_SET_ARG1(call->data, uspace_dst);
532
    IPC_SET_ARG2(call->data, size);
533
    call->buffer = buffer;
534
 
535
    ipc_answer(&TASK->kernel_box, call);
536
}
537
 
2818 svoboda 538
static void udebug_receive_mem_write(call_t *call)
539
{
540
    void *uspace_dst;
541
    unsigned size;
542
    void *buffer;
543
    int rc;
2827 svoboda 544
    udebug_task_state_t dts;
2818 svoboda 545
 
546
    klog_printf("udebug_receive_mem_write()");
2827 svoboda 547
 
548
    /* Verify task state */
549
    spinlock_lock(&TASK->lock);
550
    dts = TASK->dt_state;
551
    spinlock_unlock(&TASK->lock);
552
 
553
    if (dts != UDEBUG_TS_ACTIVE) {
554
        IPC_SET_RETVAL(call->data, EBUSY);
555
        ipc_answer(&TASK->kernel_box, call);
556
        return;
557
    }
558
 
2818 svoboda 559
    uspace_dst = (void *)IPC_GET_ARG3(call->data);
560
    size = IPC_GET_ARG4(call->data);
561
 
562
    buffer = call->buffer;
563
    klog_printf("dst=%u, size=%u", uspace_dst, size);
564
 
565
    /* NOTE: this is not strictly from a syscall... but that shouldn't
566
     * be a problem */
567
    rc = copy_to_uspace(uspace_dst, buffer, size);
568
    if (rc) {
569
        IPC_SET_RETVAL(call->data, rc);
2827 svoboda 570
        ipc_answer(&TASK->kernel_box, call);
2818 svoboda 571
        return;
572
    }
573
 
574
    IPC_SET_RETVAL(call->data, 0);
575
 
576
    free(call->buffer);
577
    call->buffer = NULL;
578
 
579
    ipc_answer(&TASK->kernel_box, call);
580
}
581
 
582
 
2815 svoboda 583
/**
584
 * Handle a debug call received on the kernel answerbox.
585
 *
586
 * This is called by the kbox servicing thread.
587
 */
588
void udebug_call_receive(call_t *call)
589
{
590
    int debug_method;
591
 
592
    debug_method = IPC_GET_ARG1(call->data);
593
 
594
    switch (debug_method) {
595
    case UDEBUG_M_MEM_READ:
596
        udebug_receive_mem_read(call);
597
        break;
2818 svoboda 598
    case UDEBUG_M_MEM_WRITE:
599
        udebug_receive_mem_write(call);
600
        break;
2815 svoboda 601
    }
602
}
603
 
2813 svoboda 604
/** @}
605
 */