Subversion Repositories HelenOS

Rev

Rev 2826 | Rev 2833 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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