Rev 2825 | Rev 2827 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2825 | Rev 2826 | ||
---|---|---|---|
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 | 72 | ||
73 | klog_printf("debug_begin()"); |
73 | klog_printf("debug_begin()"); |
74 | 74 | ||
75 | ipl = interrupts_disable(); |
75 | ipl = interrupts_disable(); |
76 | ta = get_lock_callee_task(phone); |
76 | ta = get_lock_callee_task(phone); |
77 | klog_printf("debugging task %llu", ta->taskid); |
77 | klog_printf("debugging task %llu", ta->taskid); |
78 | 78 | ||
79 | if (ta->dt_state != UDEBUG_TS_INACTIVE) { |
79 | if (ta->dt_state != UDEBUG_TS_INACTIVE) { |
80 | spinlock_unlock(&ta->lock); |
80 | spinlock_unlock(&ta->lock); |
81 | interrupts_restore(ipl); |
81 | interrupts_restore(ipl); |
82 | klog_printf("debug_begin(): busy error"); |
82 | klog_printf("debug_begin(): busy error"); |
83 | return EBUSY; |
83 | return EBUSY; |
84 | } |
84 | } |
85 | 85 | ||
86 | ta->dt_state = UDEBUG_TS_BEGINNING; |
86 | ta->dt_state = UDEBUG_TS_BEGINNING; |
87 | ta->debug_begin_call = call; |
87 | ta->debug_begin_call = call; |
88 | 88 | ||
89 | if (ta->not_stoppable_count == 0) { |
89 | if (ta->not_stoppable_count == 0) { |
90 | ta->dt_state = UDEBUG_TS_ACTIVE; |
90 | ta->dt_state = UDEBUG_TS_ACTIVE; |
91 | ta->debug_begin_call = NULL; |
91 | ta->debug_begin_call = NULL; |
92 | spinlock_unlock(&ta->lock); |
92 | spinlock_unlock(&ta->lock); |
93 | interrupts_restore(ipl); |
93 | interrupts_restore(ipl); |
94 | klog_printf("debug_begin(): immediate backsend"); |
94 | klog_printf("debug_begin(): immediate backsend"); |
95 | return 1; /* actually we need backsend with 0 retval */ |
95 | return 1; /* actually we need backsend with 0 retval */ |
96 | } |
96 | } |
97 | 97 | ||
98 | spinlock_unlock(&ta->lock); |
98 | spinlock_unlock(&ta->lock); |
99 | interrupts_restore(ipl); |
99 | interrupts_restore(ipl); |
100 | 100 | ||
101 | klog_printf("debug_begin() done (wait for stoppability)"); |
101 | klog_printf("debug_begin() done (wait for stoppability)"); |
102 | return 0; |
102 | return 0; |
103 | } |
103 | } |
104 | 104 | ||
105 | static int udebug_rp_go(call_t *call, phone_t *phone) |
105 | static int udebug_rp_go(call_t *call, phone_t *phone) |
106 | { |
106 | { |
107 | thread_t *t; |
107 | thread_t *t; |
108 | task_t *ta; |
108 | task_t *ta; |
109 | ipl_t ipl; |
109 | ipl_t ipl; |
110 | 110 | ||
111 | klog_printf("debug_go()"); |
111 | klog_printf("debug_go()"); |
112 | ta = get_lock_callee_task(phone); |
112 | ta = get_lock_callee_task(phone); |
113 | - | ||
114 | // FIXME: must save this in thread struct, not task struct!!! |
- | |
115 | ta->debug_go_call = call; |
- | |
116 | spinlock_unlock(&ta->lock); |
113 | spinlock_unlock(&ta->lock); |
- | 114 | // TODO: don't lock ta |
|
117 | 115 | ||
118 | t = (thread_t *) IPC_GET_ARG2(call->data); |
116 | t = (thread_t *) IPC_GET_ARG2(call->data); |
119 | 117 | ||
120 | ipl = interrupts_disable(); |
118 | ipl = interrupts_disable(); |
121 | spinlock_lock(&threads_lock); |
119 | spinlock_lock(&threads_lock); |
122 | 120 | ||
- | 121 | /* Verify that 't' exists and belongs to task 'ta' */ |
|
123 | if (!thread_exists(t)) { |
122 | if (!thread_exists(t) || (t->task != ta)) { |
124 | spinlock_unlock(&threads_lock); |
123 | spinlock_unlock(&threads_lock); |
125 | interrupts_restore(ipl); |
124 | interrupts_restore(ipl); |
126 | return ENOENT; |
125 | return ENOENT; |
127 | } |
126 | } |
128 | 127 | ||
- | 128 | t->debug_go_call = call; |
|
129 | t->debug_stop = false; |
129 | t->debug_stop = false; |
130 | waitq_wakeup(&t->go_wq, WAKEUP_FIRST); |
130 | waitq_wakeup(&t->go_wq, WAKEUP_FIRST); |
131 | 131 | ||
132 | spinlock_unlock(&threads_lock); |
132 | spinlock_unlock(&threads_lock); |
133 | interrupts_restore(ipl); |
133 | interrupts_restore(ipl); |
134 | 134 | ||
135 | return 0; /* no backsend */ |
135 | return 0; /* no backsend */ |
136 | } |
136 | } |
137 | 137 | ||
138 | static int udebug_rp_args_read(call_t *call, phone_t *phone) |
138 | static int udebug_rp_args_read(call_t *call, phone_t *phone) |
139 | { |
139 | { |
140 | thread_t *t; |
140 | thread_t *t; |
141 | task_t *ta; |
141 | task_t *ta; |
142 | void *uspace_buffer; |
142 | void *uspace_buffer; |
143 | unative_t to_copy; |
143 | unative_t to_copy; |
144 | int rc; |
144 | int rc; |
145 | ipl_t ipl; |
145 | ipl_t ipl; |
146 | 146 | ||
147 | klog_printf("debug_args_read()"); |
147 | klog_printf("debug_args_read()"); |
148 | // FIXME: verify task/thread state |
148 | // FIXME: verify task/thread state |
149 | 149 | ||
150 | ta = get_lock_callee_task(phone); |
150 | ta = get_lock_callee_task(phone); |
151 | klog_printf("task %llu", ta->taskid); |
151 | klog_printf("task %llu", ta->taskid); |
152 | spinlock_unlock(&ta->lock); |
152 | spinlock_unlock(&ta->lock); |
153 | 153 | ||
154 | t = (thread_t *) IPC_GET_ARG2(call->data); |
154 | t = (thread_t *) IPC_GET_ARG2(call->data); |
155 | 155 | ||
156 | ipl = interrupts_disable(); |
156 | ipl = interrupts_disable(); |
157 | spinlock_lock(&threads_lock); |
157 | spinlock_lock(&threads_lock); |
158 | 158 | ||
159 | if (!thread_exists(t)) { |
159 | if (!thread_exists(t)) { |
160 | spinlock_unlock(&threads_lock); |
160 | spinlock_unlock(&threads_lock); |
161 | interrupts_restore(ipl); |
161 | interrupts_restore(ipl); |
162 | return ENOENT; |
162 | return ENOENT; |
163 | } |
163 | } |
164 | 164 | ||
165 | // FIXME: copy to a local buffer before releasing the lock |
165 | // FIXME: copy to a local buffer before releasing the lock |
166 | spinlock_unlock(&threads_lock); |
166 | spinlock_unlock(&threads_lock); |
167 | interrupts_restore(ipl); |
167 | interrupts_restore(ipl); |
168 | 168 | ||
169 | uspace_buffer = (void *)IPC_GET_ARG3(call->data); |
169 | uspace_buffer = (void *)IPC_GET_ARG3(call->data); |
170 | to_copy = IPC_GET_ARG4(call->data); |
170 | to_copy = IPC_GET_ARG4(call->data); |
171 | if (to_copy > 6 * sizeof(unative_t)) to_copy = 6 * sizeof(unative_t); |
171 | if (to_copy > 6 * sizeof(unative_t)) to_copy = 6 * sizeof(unative_t); |
172 | 172 | ||
173 | rc = copy_to_uspace(uspace_buffer, t->syscall_args, to_copy); |
173 | rc = copy_to_uspace(uspace_buffer, t->syscall_args, to_copy); |
174 | if (rc != 0) { |
174 | if (rc != 0) { |
175 | spinlock_unlock(&ta->lock); |
175 | spinlock_unlock(&ta->lock); |
176 | klog_printf("debug_args_read() - copy failed"); |
176 | klog_printf("debug_args_read() - copy failed"); |
177 | return rc; |
177 | return rc; |
178 | } |
178 | } |
179 | 179 | ||
180 | IPC_SET_ARG1(call->data, to_copy); |
180 | IPC_SET_ARG1(call->data, to_copy); |
181 | 181 | ||
182 | klog_printf("debug_args_read() done"); |
182 | klog_printf("debug_args_read() done"); |
183 | return 1; /* actually need becksend with retval 0 */ |
183 | return 1; /* actually need becksend with retval 0 */ |
184 | } |
184 | } |
185 | 185 | ||
186 | static int udebug_rp_regs_read(call_t *call, phone_t *phone) |
186 | static int udebug_rp_regs_read(call_t *call, phone_t *phone) |
187 | { |
187 | { |
188 | thread_t *t; |
188 | thread_t *t; |
189 | task_t *ta; |
189 | task_t *ta; |
190 | void *uspace_buffer; |
190 | void *uspace_buffer; |
191 | unative_t to_copy; |
191 | unative_t to_copy; |
192 | int rc; |
192 | int rc; |
193 | istate_t *state; |
193 | istate_t *state; |
194 | istate_t state_copy; |
194 | istate_t state_copy; |
195 | ipl_t ipl; |
195 | ipl_t ipl; |
196 | 196 | ||
197 | klog_printf("debug_regs_read()"); |
197 | klog_printf("debug_regs_read()"); |
198 | // FIXME: verify task/thread state |
198 | // FIXME: verify task/thread state |
199 | 199 | ||
200 | ta = get_lock_callee_task(phone); |
200 | ta = get_lock_callee_task(phone); |
201 | spinlock_unlock(&ta->lock); |
201 | spinlock_unlock(&ta->lock); |
202 | 202 | ||
203 | ipl = interrupts_disable(); |
203 | ipl = interrupts_disable(); |
204 | spinlock_lock(&threads_lock); |
204 | spinlock_lock(&threads_lock); |
205 | 205 | ||
206 | t = (thread_t *) IPC_GET_ARG2(call->data); |
206 | t = (thread_t *) IPC_GET_ARG2(call->data); |
207 | if (!thread_exists(t)) { |
207 | if (!thread_exists(t)) { |
208 | return ENOENT; |
208 | return ENOENT; |
209 | } |
209 | } |
210 | 210 | ||
211 | state = t->uspace_state; |
211 | state = t->uspace_state; |
212 | if (state == NULL) { |
212 | if (state == NULL) { |
213 | spinlock_unlock(&threads_lock); |
213 | spinlock_unlock(&threads_lock); |
214 | interrupts_restore(ipl); |
214 | interrupts_restore(ipl); |
215 | klog_printf("debug_regs_read() - istate not available"); |
215 | klog_printf("debug_regs_read() - istate not available"); |
216 | return EBUSY; |
216 | return EBUSY; |
217 | } |
217 | } |
218 | 218 | ||
219 | /* Copy to a local buffer so that we can release the lock */ |
219 | /* Copy to a local buffer so that we can release the lock */ |
220 | memcpy(&state_copy, state, sizeof(state_copy)); |
220 | memcpy(&state_copy, state, sizeof(state_copy)); |
221 | spinlock_unlock(&threads_lock); |
221 | spinlock_unlock(&threads_lock); |
222 | interrupts_restore(ipl); |
222 | interrupts_restore(ipl); |
223 | 223 | ||
224 | uspace_buffer = (void *)IPC_GET_ARG3(call->data); |
224 | uspace_buffer = (void *)IPC_GET_ARG3(call->data); |
225 | to_copy = IPC_GET_ARG4(call->data); |
225 | to_copy = IPC_GET_ARG4(call->data); |
226 | if (to_copy > sizeof(istate_t)) to_copy = sizeof(istate_t); |
226 | if (to_copy > sizeof(istate_t)) to_copy = sizeof(istate_t); |
227 | 227 | ||
228 | rc = copy_to_uspace(uspace_buffer, &state_copy, to_copy); |
228 | rc = copy_to_uspace(uspace_buffer, &state_copy, to_copy); |
229 | if (rc != 0) { |
229 | if (rc != 0) { |
230 | spinlock_unlock(&ta->lock); |
230 | spinlock_unlock(&ta->lock); |
231 | klog_printf("debug_regs_read() - copy failed"); |
231 | klog_printf("debug_regs_read() - copy failed"); |
232 | return rc; |
232 | return rc; |
233 | } |
233 | } |
234 | 234 | ||
235 | IPC_SET_ARG1(call->data, to_copy); |
235 | IPC_SET_ARG1(call->data, to_copy); |
236 | IPC_SET_ARG2(call->data, sizeof(istate_t)); |
236 | IPC_SET_ARG2(call->data, sizeof(istate_t)); |
237 | 237 | ||
238 | klog_printf("debug_regs_read() done"); |
238 | klog_printf("debug_regs_read() done"); |
239 | return 1; /* actually need becksend with retval 0 */ |
239 | return 1; /* actually need becksend with retval 0 */ |
240 | } |
240 | } |
241 | 241 | ||
242 | static int udebug_rp_regs_write(call_t *call, phone_t *phone) |
242 | static int udebug_rp_regs_write(call_t *call, phone_t *phone) |
243 | { |
243 | { |
244 | thread_t *t; |
244 | thread_t *t; |
245 | task_t *ta; |
245 | task_t *ta; |
246 | void *uspace_data; |
246 | void *uspace_data; |
247 | unative_t to_copy; |
247 | unative_t to_copy; |
248 | int rc; |
248 | int rc; |
249 | istate_t *state; |
249 | istate_t *state; |
250 | istate_t data_copy; |
250 | istate_t data_copy; |
251 | ipl_t ipl; |
251 | ipl_t ipl; |
252 | 252 | ||
253 | klog_printf("debug_regs_write()"); |
253 | klog_printf("debug_regs_write()"); |
254 | 254 | ||
255 | /* First copy to a local buffer */ |
255 | /* First copy to a local buffer */ |
256 | 256 | ||
257 | uspace_data = (void *)IPC_GET_ARG3(call->data); |
257 | uspace_data = (void *)IPC_GET_ARG3(call->data); |
258 | to_copy = IPC_GET_ARG4(call->data); |
258 | to_copy = IPC_GET_ARG4(call->data); |
259 | if (to_copy > sizeof(istate_t)) to_copy = sizeof(istate_t); |
259 | if (to_copy > sizeof(istate_t)) to_copy = sizeof(istate_t); |
260 | 260 | ||
261 | rc = copy_from_uspace(&data_copy, uspace_data, to_copy); |
261 | rc = copy_from_uspace(&data_copy, uspace_data, to_copy); |
262 | if (rc != 0) { |
262 | if (rc != 0) { |
263 | klog_printf("debug_regs_write() - copy failed"); |
263 | klog_printf("debug_regs_write() - copy failed"); |
264 | return rc; |
264 | return rc; |
265 | } |
265 | } |
266 | 266 | ||
267 | // FIXME: verify task/thread state |
267 | // FIXME: verify task/thread state |
268 | 268 | ||
269 | ta = get_lock_callee_task(phone); |
269 | ta = get_lock_callee_task(phone); |
270 | spinlock_unlock(&ta->lock); |
270 | spinlock_unlock(&ta->lock); |
271 | 271 | ||
272 | /* Now try to change the thread's uspace_state */ |
272 | /* Now try to change the thread's uspace_state */ |
273 | 273 | ||
274 | ipl = interrupts_disable(); |
274 | ipl = interrupts_disable(); |
275 | spinlock_lock(&threads_lock); |
275 | spinlock_lock(&threads_lock); |
276 | 276 | ||
277 | t = (thread_t *) IPC_GET_ARG2(call->data); |
277 | t = (thread_t *) IPC_GET_ARG2(call->data); |
278 | if (!thread_exists(t)) { |
278 | if (!thread_exists(t)) { |
279 | spinlock_unlock(&threads_lock); |
279 | spinlock_unlock(&threads_lock); |
280 | interrupts_restore(ipl); |
280 | interrupts_restore(ipl); |
281 | return ENOENT; |
281 | return ENOENT; |
282 | } |
282 | } |
283 | 283 | ||
284 | state = t->uspace_state; |
284 | state = t->uspace_state; |
285 | if (state == NULL) { |
285 | if (state == NULL) { |
286 | spinlock_unlock(&threads_lock); |
286 | spinlock_unlock(&threads_lock); |
287 | interrupts_restore(ipl); |
287 | interrupts_restore(ipl); |
288 | klog_printf("debug_regs_write() - istate not available"); |
288 | klog_printf("debug_regs_write() - istate not available"); |
289 | return EBUSY; |
289 | return EBUSY; |
290 | } |
290 | } |
291 | 291 | ||
292 | memcpy(t->uspace_state, &data_copy, sizeof(t->uspace_state)); |
292 | memcpy(t->uspace_state, &data_copy, sizeof(t->uspace_state)); |
293 | 293 | ||
294 | spinlock_unlock(&threads_lock); |
294 | spinlock_unlock(&threads_lock); |
295 | interrupts_restore(ipl); |
295 | interrupts_restore(ipl); |
296 | 296 | ||
297 | /* Set answer values */ |
297 | /* Set answer values */ |
298 | 298 | ||
299 | IPC_SET_ARG1(call->data, to_copy); |
299 | IPC_SET_ARG1(call->data, to_copy); |
300 | IPC_SET_ARG2(call->data, sizeof(istate_t)); |
300 | IPC_SET_ARG2(call->data, sizeof(istate_t)); |
301 | 301 | ||
302 | klog_printf("debug_regs_write() done"); |
302 | klog_printf("debug_regs_write() done"); |
303 | return 1; /* actually need becksend with retval 0 */ |
303 | return 1; /* actually need becksend with retval 0 */ |
304 | } |
304 | } |
305 | 305 | ||
306 | static int udebug_rp_thread_read(call_t *call, phone_t *phone) |
306 | static int udebug_rp_thread_read(call_t *call, phone_t *phone) |
307 | { |
307 | { |
308 | thread_t *t; |
308 | thread_t *t; |
309 | link_t *cur; |
309 | link_t *cur; |
310 | task_t *ta; |
310 | task_t *ta; |
311 | unative_t *uspace_buffer; |
311 | unative_t *uspace_buffer; |
312 | unative_t to_copy; |
312 | unative_t to_copy; |
313 | int rc; |
313 | int rc; |
314 | unsigned total_bytes; |
314 | unsigned total_bytes; |
315 | unsigned buf_size; |
315 | unsigned buf_size; |
316 | unative_t tid; |
316 | unative_t tid; |
317 | unsigned num_threads, copied_ids; |
317 | unsigned num_threads, copied_ids; |
318 | ipl_t ipl; |
318 | ipl_t ipl; |
319 | unative_t *buffer; |
319 | unative_t *buffer; |
320 | int flags; |
320 | int flags; |
321 | 321 | ||
322 | klog_printf("debug_thread_read()"); |
322 | klog_printf("debug_thread_read()"); |
323 | // FIXME: verify task/thread state |
323 | // FIXME: verify task/thread state |
324 | 324 | ||
325 | ipl = interrupts_disable(); |
325 | ipl = interrupts_disable(); |
326 | ta = get_lock_callee_task(phone); |
326 | ta = get_lock_callee_task(phone); |
327 | 327 | ||
328 | /* Count the threads first */ |
328 | /* Count the threads first */ |
329 | 329 | ||
330 | num_threads = 0; |
330 | num_threads = 0; |
331 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
331 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
332 | /* Count all threads, to be on the safe side */ |
332 | /* Count all threads, to be on the safe side */ |
333 | ++num_threads; |
333 | ++num_threads; |
334 | } |
334 | } |
335 | 335 | ||
336 | /* Allocate a buffer and copy down the threads' ids */ |
336 | /* Allocate a buffer and copy down the threads' ids */ |
337 | buffer = malloc(num_threads * sizeof(unative_t), 0); // ??? |
337 | buffer = malloc(num_threads * sizeof(unative_t), 0); // ??? |
338 | 338 | ||
339 | copied_ids = 0; |
339 | copied_ids = 0; |
340 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
340 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
341 | t = list_get_instance(cur, thread_t, th_link); |
341 | t = list_get_instance(cur, thread_t, th_link); |
342 | 342 | ||
343 | spinlock_lock(&t->lock); |
343 | spinlock_lock(&t->lock); |
344 | flags = t->flags; |
344 | flags = t->flags; |
345 | spinlock_unlock(&t->lock); |
345 | spinlock_unlock(&t->lock); |
346 | 346 | ||
347 | /* Not interested in kernel threads */ |
347 | /* Not interested in kernel threads */ |
348 | if ((flags & THREAD_FLAG_USPACE) != 0) { |
348 | if ((flags & THREAD_FLAG_USPACE) != 0) { |
349 | /* Using thread struct pointer for identification */ |
349 | /* Using thread struct pointer for identification */ |
350 | tid = (unative_t) t; |
350 | tid = (unative_t) t; |
351 | buffer[copied_ids++] = tid; |
351 | buffer[copied_ids++] = tid; |
352 | } |
352 | } |
353 | } |
353 | } |
354 | 354 | ||
355 | spinlock_unlock(&ta->lock); |
355 | spinlock_unlock(&ta->lock); |
356 | interrupts_restore(ipl); |
356 | interrupts_restore(ipl); |
357 | 357 | ||
358 | /* Now copy to userspace */ |
358 | /* Now copy to userspace */ |
359 | 359 | ||
360 | uspace_buffer = (void *)IPC_GET_ARG2(call->data); |
360 | uspace_buffer = (void *)IPC_GET_ARG2(call->data); |
361 | buf_size = IPC_GET_ARG3(call->data); |
361 | buf_size = IPC_GET_ARG3(call->data); |
362 | 362 | ||
363 | total_bytes = copied_ids * sizeof(unative_t); |
363 | total_bytes = copied_ids * sizeof(unative_t); |
364 | 364 | ||
365 | if (buf_size > total_bytes) |
365 | if (buf_size > total_bytes) |
366 | to_copy = total_bytes; |
366 | to_copy = total_bytes; |
367 | else |
367 | else |
368 | to_copy = buf_size; |
368 | to_copy = buf_size; |
369 | 369 | ||
370 | rc = copy_to_uspace(uspace_buffer, buffer, to_copy); |
370 | rc = copy_to_uspace(uspace_buffer, buffer, to_copy); |
371 | free(buffer); |
371 | free(buffer); |
372 | 372 | ||
373 | if (rc != 0) { |
373 | if (rc != 0) { |
374 | klog_printf("debug_thread_read() - copy failed"); |
374 | klog_printf("debug_thread_read() - copy failed"); |
375 | return rc; |
375 | return rc; |
376 | } |
376 | } |
377 | 377 | ||
378 | IPC_SET_ARG1(call->data, to_copy); |
378 | IPC_SET_ARG1(call->data, to_copy); |
379 | IPC_SET_ARG2(call->data, total_bytes); |
379 | IPC_SET_ARG2(call->data, total_bytes); |
380 | 380 | ||
381 | klog_printf("debug_thread_read() done"); |
381 | klog_printf("debug_thread_read() done"); |
382 | return 1; /* actually need becksend with retval 0 */ |
382 | return 1; /* actually need becksend with retval 0 */ |
383 | } |
383 | } |
384 | 384 | ||
385 | static int udebug_rp_mem_write(call_t *call, phone_t *phone) |
385 | static int udebug_rp_mem_write(call_t *call, phone_t *phone) |
386 | { |
386 | { |
387 | void *uspace_data; |
387 | void *uspace_data; |
388 | unative_t to_copy; |
388 | unative_t to_copy; |
389 | int rc; |
389 | int rc; |
390 | void *buffer; |
390 | void *buffer; |
391 | 391 | ||
392 | klog_printf("udebug_rp_mem_write()"); |
392 | klog_printf("udebug_rp_mem_write()"); |
393 | // FIXME: verify task/thread state |
393 | // FIXME: verify task/thread state |
394 | 394 | ||
395 | uspace_data = (void *)IPC_GET_ARG2(call->data); |
395 | uspace_data = (void *)IPC_GET_ARG2(call->data); |
396 | to_copy = IPC_GET_ARG4(call->data); |
396 | to_copy = IPC_GET_ARG4(call->data); |
397 | 397 | ||
398 | buffer = malloc(to_copy, 0); // ??? |
398 | buffer = malloc(to_copy, 0); // ??? |
399 | 399 | ||
400 | rc = copy_from_uspace(buffer, uspace_data, to_copy); |
400 | rc = copy_from_uspace(buffer, uspace_data, to_copy); |
401 | if (rc != 0) { |
401 | if (rc != 0) { |
402 | klog_printf(" - copy failed"); |
402 | klog_printf(" - copy failed"); |
403 | return rc; |
403 | return rc; |
404 | } |
404 | } |
405 | 405 | ||
406 | call->buffer = buffer; |
406 | call->buffer = buffer; |
407 | 407 | ||
408 | klog_printf(" - done"); |
408 | klog_printf(" - done"); |
409 | return 1; /* actually need becksend with retval 0 */ |
409 | return 1; /* actually need becksend with retval 0 */ |
410 | } |
410 | } |
411 | 411 | ||
412 | 412 | ||
413 | int udebug_request_preprocess(call_t *call, phone_t *phone) |
413 | int udebug_request_preprocess(call_t *call, phone_t *phone) |
414 | { |
414 | { |
415 | int rc; |
415 | int rc; |
416 | 416 | ||
417 | switch (IPC_GET_ARG1(call->data)) { |
417 | switch (IPC_GET_ARG1(call->data)) { |
418 | case UDEBUG_M_BEGIN: |
418 | case UDEBUG_M_BEGIN: |
419 | rc = udebug_rp_begin(call, phone); |
419 | rc = udebug_rp_begin(call, phone); |
420 | return rc; |
420 | return rc; |
421 | case UDEBUG_M_GO: |
421 | case UDEBUG_M_GO: |
422 | rc = udebug_rp_go(call, phone); |
422 | rc = udebug_rp_go(call, phone); |
423 | return rc; |
423 | return rc; |
424 | case UDEBUG_M_ARGS_READ: |
424 | case UDEBUG_M_ARGS_READ: |
425 | rc = udebug_rp_args_read(call, phone); |
425 | rc = udebug_rp_args_read(call, phone); |
426 | return rc; |
426 | return rc; |
427 | case UDEBUG_M_REGS_READ: |
427 | case UDEBUG_M_REGS_READ: |
428 | rc = udebug_rp_regs_read(call, phone); |
428 | rc = udebug_rp_regs_read(call, phone); |
429 | return rc; |
429 | return rc; |
430 | case UDEBUG_M_REGS_WRITE: |
430 | case UDEBUG_M_REGS_WRITE: |
431 | rc = udebug_rp_regs_write(call, phone); |
431 | rc = udebug_rp_regs_write(call, phone); |
432 | return rc; |
432 | return rc; |
433 | case UDEBUG_M_THREAD_READ: |
433 | case UDEBUG_M_THREAD_READ: |
434 | rc = udebug_rp_thread_read(call, phone); |
434 | rc = udebug_rp_thread_read(call, phone); |
435 | return rc; |
435 | return rc; |
436 | case UDEBUG_M_MEM_WRITE: |
436 | case UDEBUG_M_MEM_WRITE: |
437 | rc = udebug_rp_mem_write(call, phone); |
437 | rc = udebug_rp_mem_write(call, phone); |
438 | return rc; |
438 | return rc; |
439 | default: |
439 | default: |
440 | break; |
440 | break; |
441 | } |
441 | } |
442 | 442 | ||
443 | return 0; |
443 | return 0; |
444 | } |
444 | } |
445 | 445 | ||
446 | static void udebug_receive_mem_read(call_t *call) |
446 | static void udebug_receive_mem_read(call_t *call) |
447 | { |
447 | { |
448 | unative_t uspace_dst; |
448 | unative_t uspace_dst; |
449 | void *uspace_ptr; |
449 | void *uspace_ptr; |
450 | unsigned size; |
450 | unsigned size; |
451 | void *buffer; |
451 | void *buffer; |
452 | int rc; |
452 | int rc; |
453 | 453 | ||
454 | klog_printf("debug_mem_read()"); |
454 | klog_printf("debug_mem_read()"); |
455 | uspace_dst = IPC_GET_ARG2(call->data); |
455 | uspace_dst = IPC_GET_ARG2(call->data); |
456 | uspace_ptr = (void *)IPC_GET_ARG3(call->data); |
456 | uspace_ptr = (void *)IPC_GET_ARG3(call->data); |
457 | size = IPC_GET_ARG4(call->data); |
457 | size = IPC_GET_ARG4(call->data); |
458 | 458 | ||
459 | buffer = malloc(size, 0); // ??? |
459 | buffer = malloc(size, 0); // ??? |
460 | klog_printf("debug_mem_read: src=%u, size=%u", uspace_ptr, size); |
460 | klog_printf("debug_mem_read: src=%u, size=%u", uspace_ptr, size); |
461 | 461 | ||
462 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
462 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
463 | * be a problem */ |
463 | * be a problem */ |
464 | rc = copy_from_uspace(buffer, uspace_ptr, size); |
464 | rc = copy_from_uspace(buffer, uspace_ptr, size); |
465 | if (rc) { |
465 | if (rc) { |
466 | IPC_SET_RETVAL(call->data, rc); |
466 | IPC_SET_RETVAL(call->data, rc); |
467 | return; |
467 | return; |
468 | } |
468 | } |
469 | 469 | ||
470 | klog_printf("first word: %u", *((unative_t *)buffer)); |
470 | klog_printf("first word: %u", *((unative_t *)buffer)); |
471 | 471 | ||
472 | IPC_SET_RETVAL(call->data, 0); |
472 | IPC_SET_RETVAL(call->data, 0); |
473 | /* Hack: ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that |
473 | /* Hack: ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that |
474 | same code in process_answer() can be used |
474 | same code in process_answer() can be used |
475 | (no way to distinguish method in answer) */ |
475 | (no way to distinguish method in answer) */ |
476 | IPC_SET_ARG1(call->data, uspace_dst); |
476 | IPC_SET_ARG1(call->data, uspace_dst); |
477 | IPC_SET_ARG2(call->data, size); |
477 | IPC_SET_ARG2(call->data, size); |
478 | call->buffer = buffer; |
478 | call->buffer = buffer; |
479 | 479 | ||
480 | ipc_answer(&TASK->kernel_box, call); |
480 | ipc_answer(&TASK->kernel_box, call); |
481 | } |
481 | } |
482 | 482 | ||
483 | static void udebug_receive_mem_write(call_t *call) |
483 | static void udebug_receive_mem_write(call_t *call) |
484 | { |
484 | { |
485 | void *uspace_dst; |
485 | void *uspace_dst; |
486 | unsigned size; |
486 | unsigned size; |
487 | void *buffer; |
487 | void *buffer; |
488 | int rc; |
488 | int rc; |
489 | 489 | ||
490 | klog_printf("udebug_receive_mem_write()"); |
490 | klog_printf("udebug_receive_mem_write()"); |
491 | uspace_dst = (void *)IPC_GET_ARG3(call->data); |
491 | uspace_dst = (void *)IPC_GET_ARG3(call->data); |
492 | size = IPC_GET_ARG4(call->data); |
492 | size = IPC_GET_ARG4(call->data); |
493 | 493 | ||
494 | buffer = call->buffer; |
494 | buffer = call->buffer; |
495 | klog_printf("dst=%u, size=%u", uspace_dst, size); |
495 | klog_printf("dst=%u, size=%u", uspace_dst, size); |
496 | 496 | ||
497 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
497 | /* NOTE: this is not strictly from a syscall... but that shouldn't |
498 | * be a problem */ |
498 | * be a problem */ |
499 | rc = copy_to_uspace(uspace_dst, buffer, size); |
499 | rc = copy_to_uspace(uspace_dst, buffer, size); |
500 | if (rc) { |
500 | if (rc) { |
501 | IPC_SET_RETVAL(call->data, rc); |
501 | IPC_SET_RETVAL(call->data, rc); |
502 | return; |
502 | return; |
503 | } |
503 | } |
504 | 504 | ||
505 | IPC_SET_RETVAL(call->data, 0); |
505 | IPC_SET_RETVAL(call->data, 0); |
506 | 506 | ||
507 | free(call->buffer); |
507 | free(call->buffer); |
508 | call->buffer = NULL; |
508 | call->buffer = NULL; |
509 | 509 | ||
510 | ipc_answer(&TASK->kernel_box, call); |
510 | ipc_answer(&TASK->kernel_box, call); |
511 | } |
511 | } |
512 | 512 | ||
513 | 513 | ||
514 | /** |
514 | /** |
515 | * Handle a debug call received on the kernel answerbox. |
515 | * Handle a debug call received on the kernel answerbox. |
516 | * |
516 | * |
517 | * This is called by the kbox servicing thread. |
517 | * This is called by the kbox servicing thread. |
518 | */ |
518 | */ |
519 | void udebug_call_receive(call_t *call) |
519 | void udebug_call_receive(call_t *call) |
520 | { |
520 | { |
521 | int debug_method; |
521 | int debug_method; |
522 | 522 | ||
523 | debug_method = IPC_GET_ARG1(call->data); |
523 | debug_method = IPC_GET_ARG1(call->data); |
524 | 524 | ||
525 | switch (debug_method) { |
525 | switch (debug_method) { |
526 | case UDEBUG_M_MEM_READ: |
526 | case UDEBUG_M_MEM_READ: |
527 | udebug_receive_mem_read(call); |
527 | udebug_receive_mem_read(call); |
528 | break; |
528 | break; |
529 | case UDEBUG_M_MEM_WRITE: |
529 | case UDEBUG_M_MEM_WRITE: |
530 | udebug_receive_mem_write(call); |
530 | udebug_receive_mem_write(call); |
531 | break; |
531 | break; |
532 | } |
532 | } |
533 | } |
533 | } |
534 | 534 | ||
535 | /** @} |
535 | /** @} |
536 | */ |
536 | */ |
537 | 537 |