source: mainline/kernel/generic/src/udebug/udebug_ipc.c@ 09ab0a9a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 09ab0a9a was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

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