Rev 4605 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3438 | 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 | |||
29 | /** @addtogroup generic |
||
30 | * @{ |
||
31 | */ |
||
32 | |||
33 | /** |
||
34 | * @file |
||
35 | * @brief Udebug IPC message handling. |
||
3457 | svoboda | 36 | * |
37 | * This module handles udebug IPC messages and calls the appropriate |
||
38 | * functions from the udebug_ops module which implement them. |
||
3438 | svoboda | 39 | */ |
40 | |||
41 | #include <proc/task.h> |
||
42 | #include <proc/thread.h> |
||
43 | #include <arch.h> |
||
44 | #include <errno.h> |
||
45 | #include <ipc/ipc.h> |
||
46 | #include <syscall/copy.h> |
||
47 | #include <udebug/udebug.h> |
||
48 | #include <udebug/udebug_ops.h> |
||
49 | #include <udebug/udebug_ipc.h> |
||
50 | |||
51 | int udebug_request_preprocess(call_t *call, phone_t *phone) |
||
52 | { |
||
53 | switch (IPC_GET_ARG1(call->data)) { |
||
54 | /* future UDEBUG_M_REGS_WRITE, UDEBUG_M_MEM_WRITE: */ |
||
55 | default: |
||
56 | break; |
||
57 | } |
||
58 | |||
59 | return 0; |
||
60 | } |
||
61 | |||
3468 | svoboda | 62 | /** Process a BEGIN call. |
63 | * |
||
64 | * Initiates a debugging session for the current task. The reply |
||
65 | * to this call may or may not be sent before this function returns. |
||
66 | * |
||
67 | * @param call The call structure. |
||
68 | */ |
||
3438 | svoboda | 69 | static void udebug_receive_begin(call_t *call) |
70 | { |
||
71 | int rc; |
||
72 | |||
73 | rc = udebug_begin(call); |
||
74 | if (rc < 0) { |
||
75 | IPC_SET_RETVAL(call->data, rc); |
||
3620 | svoboda | 76 | ipc_answer(&TASK->kb.box, call); |
3438 | svoboda | 77 | return; |
78 | } |
||
79 | |||
3468 | svoboda | 80 | /* |
81 | * If the initialization of the debugging session has finished, |
||
82 | * send a reply. |
||
83 | */ |
||
3438 | svoboda | 84 | if (rc != 0) { |
85 | IPC_SET_RETVAL(call->data, 0); |
||
3620 | svoboda | 86 | ipc_answer(&TASK->kb.box, call); |
3438 | svoboda | 87 | } |
88 | } |
||
89 | |||
3468 | svoboda | 90 | /** Process an END call. |
91 | * |
||
92 | * Terminates the debugging session for the current task. |
||
93 | * @param call The call structure. |
||
94 | */ |
||
3438 | svoboda | 95 | static void udebug_receive_end(call_t *call) |
96 | { |
||
97 | int rc; |
||
98 | |||
99 | rc = udebug_end(); |
||
100 | |||
101 | IPC_SET_RETVAL(call->data, rc); |
||
3620 | svoboda | 102 | ipc_answer(&TASK->kb.box, call); |
3438 | svoboda | 103 | } |
104 | |||
3468 | svoboda | 105 | /** Process a SET_EVMASK call. |
106 | * |
||
107 | * Sets an event mask for the current debugging session. |
||
108 | * @param call The call structure. |
||
109 | */ |
||
3438 | svoboda | 110 | static void udebug_receive_set_evmask(call_t *call) |
111 | { |
||
112 | int rc; |
||
113 | udebug_evmask_t mask; |
||
114 | |||
115 | mask = IPC_GET_ARG2(call->data); |
||
116 | rc = udebug_set_evmask(mask); |
||
117 | |||
118 | IPC_SET_RETVAL(call->data, rc); |
||
3620 | svoboda | 119 | ipc_answer(&TASK->kb.box, call); |
3438 | svoboda | 120 | } |
121 | |||
122 | |||
3468 | svoboda | 123 | /** Process a GO call. |
124 | * |
||
125 | * Resumes execution of the specified thread. |
||
126 | * @param call The call structure. |
||
127 | */ |
||
3438 | svoboda | 128 | static void udebug_receive_go(call_t *call) |
129 | { |
||
130 | thread_t *t; |
||
131 | int rc; |
||
132 | |||
133 | t = (thread_t *)IPC_GET_ARG2(call->data); |
||
134 | |||
135 | rc = udebug_go(t, call); |
||
136 | if (rc < 0) { |
||
137 | IPC_SET_RETVAL(call->data, rc); |
||
3620 | svoboda | 138 | ipc_answer(&TASK->kb.box, call); |
3438 | svoboda | 139 | return; |
140 | } |
||
141 | } |
||
142 | |||
3468 | svoboda | 143 | /** Process a STOP call. |
144 | * |
||
145 | * Suspends execution of the specified thread. |
||
146 | * @param call The call structure. |
||
147 | */ |
||
3438 | svoboda | 148 | static void udebug_receive_stop(call_t *call) |
149 | { |
||
150 | thread_t *t; |
||
151 | int rc; |
||
152 | |||
153 | t = (thread_t *)IPC_GET_ARG2(call->data); |
||
154 | |||
155 | rc = udebug_stop(t, call); |
||
156 | IPC_SET_RETVAL(call->data, rc); |
||
3620 | svoboda | 157 | ipc_answer(&TASK->kb.box, call); |
3438 | svoboda | 158 | } |
159 | |||
3468 | svoboda | 160 | /** Process a THREAD_READ call. |
161 | * |
||
162 | * Reads the list of hashes of the (userspace) threads in the current task. |
||
163 | * @param call The call structure. |
||
164 | */ |
||
3438 | svoboda | 165 | static void udebug_receive_thread_read(call_t *call) |
166 | { |
||
167 | unative_t uspace_addr; |
||
168 | unative_t to_copy; |
||
169 | unsigned total_bytes; |
||
170 | unsigned buf_size; |
||
171 | void *buffer; |
||
172 | size_t n; |
||
173 | int rc; |
||
174 | |||
175 | uspace_addr = IPC_GET_ARG2(call->data); /* Destination address */ |
||
176 | buf_size = IPC_GET_ARG3(call->data); /* Dest. buffer size */ |
||
177 | |||
178 | /* |
||
179 | * Read thread list. Variable n will be filled with actual number |
||
180 | * of threads times thread-id size. |
||
181 | */ |
||
182 | rc = udebug_thread_read(&buffer, buf_size, &n); |
||
183 | if (rc < 0) { |
||
184 | IPC_SET_RETVAL(call->data, rc); |
||
3620 | svoboda | 185 | ipc_answer(&TASK->kb.box, call); |
3438 | svoboda | 186 | return; |
187 | } |
||
188 | |||
189 | total_bytes = n; |
||
190 | |||
191 | /* Copy MAX(buf_size, total_bytes) bytes */ |
||
192 | |||
193 | if (buf_size > total_bytes) |
||
194 | to_copy = total_bytes; |
||
195 | else |
||
196 | to_copy = buf_size; |
||
197 | |||
198 | /* |
||
199 | * Make use of call->buffer to transfer data to caller's userspace |
||
200 | */ |
||
201 | |||
202 | IPC_SET_RETVAL(call->data, 0); |
||
203 | /* ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that |
||
204 | same code in process_answer() can be used |
||
205 | (no way to distinguish method in answer) */ |
||
206 | IPC_SET_ARG1(call->data, uspace_addr); |
||
207 | IPC_SET_ARG2(call->data, to_copy); |
||
208 | |||
209 | IPC_SET_ARG3(call->data, total_bytes); |
||
210 | call->buffer = buffer; |
||
211 | |||
3620 | svoboda | 212 | ipc_answer(&TASK->kb.box, call); |
3438 | svoboda | 213 | } |
214 | |||
3468 | svoboda | 215 | /** Process an ARGS_READ call. |
216 | * |
||
217 | * Reads the argument of a current syscall event (SYSCALL_B or SYSCALL_E). |
||
218 | * @param call The call structure. |
||
219 | */ |
||
3438 | svoboda | 220 | static void udebug_receive_args_read(call_t *call) |
221 | { |
||
222 | thread_t *t; |
||
223 | unative_t uspace_addr; |
||
224 | int rc; |
||
225 | void *buffer; |
||
226 | |||
227 | t = (thread_t *)IPC_GET_ARG2(call->data); |
||
228 | |||
229 | rc = udebug_args_read(t, &buffer); |
||
230 | if (rc != EOK) { |
||
231 | IPC_SET_RETVAL(call->data, rc); |
||
3620 | svoboda | 232 | ipc_answer(&TASK->kb.box, call); |
3438 | svoboda | 233 | return; |
234 | } |
||
235 | |||
236 | /* |
||
237 | * Make use of call->buffer to transfer data to caller's userspace |
||
238 | */ |
||
239 | |||
240 | uspace_addr = IPC_GET_ARG3(call->data); |
||
241 | |||
242 | IPC_SET_RETVAL(call->data, 0); |
||
243 | /* ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that |
||
244 | same code in process_answer() can be used |
||
245 | (no way to distinguish method in answer) */ |
||
246 | IPC_SET_ARG1(call->data, uspace_addr); |
||
247 | IPC_SET_ARG2(call->data, 6 * sizeof(unative_t)); |
||
248 | call->buffer = buffer; |
||
249 | |||
3620 | svoboda | 250 | ipc_answer(&TASK->kb.box, call); |
3438 | svoboda | 251 | } |
252 | |||
3468 | svoboda | 253 | /** Process an MEM_READ call. |
254 | * |
||
255 | * Reads memory of the current (debugged) task. |
||
256 | * @param call The call structure. |
||
257 | */ |
||
3438 | svoboda | 258 | static void udebug_receive_mem_read(call_t *call) |
259 | { |
||
260 | unative_t uspace_dst; |
||
261 | unative_t uspace_src; |
||
262 | unsigned size; |
||
263 | void *buffer; |
||
264 | int rc; |
||
265 | |||
266 | uspace_dst = IPC_GET_ARG2(call->data); |
||
267 | uspace_src = IPC_GET_ARG3(call->data); |
||
268 | size = IPC_GET_ARG4(call->data); |
||
269 | |||
270 | rc = udebug_mem_read(uspace_src, size, &buffer); |
||
271 | if (rc < 0) { |
||
272 | IPC_SET_RETVAL(call->data, rc); |
||
3620 | svoboda | 273 | ipc_answer(&TASK->kb.box, call); |
3438 | svoboda | 274 | return; |
275 | } |
||
276 | |||
277 | IPC_SET_RETVAL(call->data, 0); |
||
278 | /* ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that |
||
279 | same code in process_answer() can be used |
||
280 | (no way to distinguish method in answer) */ |
||
281 | IPC_SET_ARG1(call->data, uspace_dst); |
||
282 | IPC_SET_ARG2(call->data, size); |
||
283 | call->buffer = buffer; |
||
284 | |||
3620 | svoboda | 285 | ipc_answer(&TASK->kb.box, call); |
3438 | svoboda | 286 | } |
287 | |||
4616 | brambora | 288 | |
289 | |||
290 | /**************************/ |
||
291 | /*** CHECKPOINTING ***/ |
||
292 | /**************************/ |
||
293 | |||
294 | |||
295 | static void udebug_receive_thread_get_thread_struct(call_t *call) |
||
296 | { |
||
297 | unative_t to_copy; |
||
298 | unative_t total_bytes; |
||
299 | thread_t *t = (thread_t *)IPC_GET_ARG3(call->data); |
||
300 | void *buffer; |
||
301 | |||
302 | unative_t uspace_addr = IPC_GET_ARG2(call->data); |
||
303 | size_t buf_size = IPC_GET_ARG4(call->data); |
||
304 | |||
305 | if (buf_size < sizeof(thread_t)) |
||
306 | { |
||
307 | to_copy = 0; |
||
308 | total_bytes = sizeof(thread_t); |
||
309 | } |
||
310 | else |
||
311 | { |
||
312 | udebug_thread_get_thread_struct(t, &buffer); |
||
313 | to_copy = sizeof(thread_t); |
||
314 | total_bytes = sizeof(thread_t); |
||
315 | } |
||
316 | |||
317 | IPC_SET_RETVAL(call->data, 0); |
||
318 | IPC_SET_ARG1(call->data, uspace_addr); |
||
319 | IPC_SET_ARG2(call->data, to_copy); |
||
320 | IPC_SET_ARG3(call->data, total_bytes); |
||
321 | |||
322 | if (to_copy > 0) |
||
323 | call->buffer = buffer; |
||
324 | else |
||
325 | call->buffer = NULL; |
||
326 | |||
327 | ipc_answer(&TASK->kb.box, call); |
||
328 | } |
||
329 | |||
330 | static void udebug_receive_thread_copy_kstack(call_t *call) |
||
331 | { |
||
332 | void *buffer; |
||
333 | unative_t uspace_addr = IPC_GET_ARG2(call->data); |
||
334 | size_t buf_size = IPC_GET_ARG3(call->data); |
||
335 | thread_t *t = (thread_t *)IPC_GET_ARG4(call->data); |
||
336 | size_t copied; |
||
337 | |||
338 | size_t kstack_size = PAGE_SIZE;//(uintptr_t)t->saved_context.sp - (uintptr_t)t->kstack; |
||
339 | |||
340 | if (buf_size >= kstack_size) |
||
341 | { |
||
342 | udebug_copy_kstack(t->kstack, &buffer, buf_size); |
||
343 | copied = kstack_size; |
||
344 | } |
||
345 | else |
||
346 | copied = 0; |
||
347 | |||
348 | IPC_SET_RETVAL(call->data, 0); |
||
349 | IPC_SET_ARG1(call->data, uspace_addr); |
||
350 | IPC_SET_ARG2(call->data, copied); |
||
351 | IPC_SET_ARG3(call->data, kstack_size); // needed |
||
352 | |||
353 | if (copied > 0) |
||
354 | call->buffer = (void *)buffer; |
||
355 | else |
||
356 | call->buffer = NULL; |
||
357 | |||
358 | ipc_answer(&TASK->kb.box, call); |
||
359 | } |
||
360 | |||
361 | static void udebug_receive_task_mem_areas_read(call_t *call) |
||
362 | { |
||
363 | unative_t uspace_addr; |
||
364 | unative_t to_copy; |
||
365 | unsigned total_bytes; |
||
366 | unsigned buf_size; |
||
367 | void *buffer; |
||
368 | size_t n; |
||
369 | int rc; |
||
370 | |||
371 | uspace_addr = IPC_GET_ARG2(call->data); /* Destination address */ |
||
372 | buf_size = IPC_GET_ARG3(call->data); /* Dest. buffer size */ |
||
373 | |||
374 | rc = udebug_task_get_memory_areas(&buffer, buf_size, &n); |
||
375 | if (rc < 0) { |
||
376 | IPC_SET_RETVAL(call->data, rc); |
||
377 | ipc_answer(&TASK->kb.box, call); |
||
378 | return; |
||
379 | } |
||
380 | |||
381 | total_bytes = n; |
||
382 | |||
383 | /* Copy MAX(buf_size, total_bytes) bytes */ |
||
384 | |||
385 | if (buf_size > total_bytes) |
||
386 | to_copy = total_bytes; |
||
387 | else |
||
388 | to_copy = buf_size; |
||
389 | |||
390 | /* |
||
391 | * Make use of call->buffer to transfer data to caller's userspace |
||
392 | */ |
||
393 | |||
394 | IPC_SET_RETVAL(call->data, 0); |
||
395 | /* ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that |
||
396 | same code in process_answer() can be used |
||
397 | (no way to distinguish method in answer) */ |
||
398 | IPC_SET_ARG1(call->data, uspace_addr); |
||
399 | IPC_SET_ARG2(call->data, to_copy); |
||
400 | |||
401 | IPC_SET_ARG3(call->data, total_bytes); |
||
402 | call->buffer = buffer; |
||
403 | |||
404 | ipc_answer(&TASK->kb.box, call); |
||
405 | } |
||
406 | |||
407 | static void udebug_receive_mem_write(call_t *call) |
||
408 | { |
||
409 | size_t size = (size_t)IPC_GET_ARG3(call->data); |
||
410 | void *start = (void *)IPC_GET_ARG4(call->data); |
||
411 | |||
412 | udebug_mem_write(call->buffer, start, size); |
||
413 | |||
414 | IPC_SET_RETVAL(call->data, 0); |
||
415 | ipc_answer(&TASK->kb.box, call); |
||
416 | } |
||
417 | |||
418 | static void udebug_receive_thread_restore_struct(call_t *call) |
||
419 | { |
||
420 | thread_t *t = (thread_t *)IPC_GET_ARG3(call->data); |
||
421 | |||
422 | udebug_restore_thread_struct(call->buffer, t); |
||
423 | |||
424 | IPC_SET_RETVAL(call->data, 0); |
||
425 | |||
426 | ipc_answer(&TASK->kb.box, call); |
||
427 | } |
||
428 | |||
429 | static void udebug_receive_restore_kstack(call_t *call) |
||
430 | { |
||
431 | size_t size = (size_t)IPC_GET_ARG3(call->data); |
||
432 | thread_t *t = (thread_t *)IPC_GET_ARG4(call->data); |
||
433 | |||
434 | udebug_restore_kstack(call->buffer, size, t); |
||
435 | |||
436 | IPC_SET_RETVAL(call->data, 0); |
||
437 | ipc_answer(&TASK->kb.box, call); |
||
438 | |||
439 | } |
||
440 | |||
3468 | svoboda | 441 | /** Handle a debug call received on the kernel answerbox. |
3438 | svoboda | 442 | * |
3468 | svoboda | 443 | * This is called by the kbox servicing thread. Verifies that the sender |
444 | * is indeed the debugger and calls the appropriate processing function. |
||
3438 | svoboda | 445 | */ |
446 | void udebug_call_receive(call_t *call) |
||
447 | { |
||
448 | int debug_method; |
||
449 | |||
450 | debug_method = IPC_GET_ARG1(call->data); |
||
451 | |||
452 | if (debug_method != UDEBUG_M_BEGIN) { |
||
453 | /* |
||
454 | * Verify that the sender is this task's debugger. |
||
455 | * Note that this is the only thread that could change |
||
456 | * TASK->debugger. Therefore no locking is necessary |
||
457 | * and the sender can be safely considered valid until |
||
458 | * control exits this function. |
||
459 | */ |
||
460 | if (TASK->udebug.debugger != call->sender) { |
||
461 | IPC_SET_RETVAL(call->data, EINVAL); |
||
3620 | svoboda | 462 | ipc_answer(&TASK->kb.box, call); |
3438 | svoboda | 463 | return; |
464 | } |
||
465 | } |
||
466 | |||
467 | switch (debug_method) { |
||
468 | case UDEBUG_M_BEGIN: |
||
469 | udebug_receive_begin(call); |
||
470 | break; |
||
471 | case UDEBUG_M_END: |
||
472 | udebug_receive_end(call); |
||
473 | break; |
||
474 | case UDEBUG_M_SET_EVMASK: |
||
475 | udebug_receive_set_evmask(call); |
||
476 | break; |
||
477 | case UDEBUG_M_GO: |
||
478 | udebug_receive_go(call); |
||
479 | break; |
||
480 | case UDEBUG_M_STOP: |
||
481 | udebug_receive_stop(call); |
||
482 | break; |
||
483 | case UDEBUG_M_THREAD_READ: |
||
484 | udebug_receive_thread_read(call); |
||
485 | break; |
||
486 | case UDEBUG_M_ARGS_READ: |
||
487 | udebug_receive_args_read(call); |
||
488 | break; |
||
489 | case UDEBUG_M_MEM_READ: |
||
490 | udebug_receive_mem_read(call); |
||
491 | break; |
||
4616 | brambora | 492 | |
493 | /* CHECKPOINTING */ |
||
494 | case UDEBUG_M_TASK_MEM_AREAS_READ: |
||
495 | udebug_receive_task_mem_areas_read(call); |
||
496 | break; |
||
497 | case UDEBUG_M_MEM_WRITE: |
||
498 | udebug_receive_mem_write(call); |
||
499 | break; |
||
500 | case UDEBUG_M_THREAD_COPY_KSTACK: |
||
501 | udebug_receive_thread_copy_kstack(call); |
||
502 | break; |
||
503 | case UDEBUG_M_RESTORE_KSTACK: |
||
504 | udebug_receive_restore_kstack(call); |
||
505 | break; |
||
506 | case UDEBUG_M_THREAD_GET_THREAD_STRUCT: |
||
507 | udebug_receive_thread_get_thread_struct(call); |
||
508 | break; |
||
509 | case UDEBUG_M_THREAD_RESTORE_THREAD_STRUCT: |
||
510 | udebug_receive_thread_restore_struct(call); |
||
511 | break; |
||
3438 | svoboda | 512 | } |
513 | } |
||
514 | |||
515 | /** @} |
||
516 | */ |