source: mainline/kernel/generic/src/udebug/udebug_ipc.c

Last change on this file was fa603e99, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Fix unhandled error condition in udebug_name_read

Detected by GCC when LTO is enabled.
Also changed a bunch of literal zeros in the file to EOK.

  • Property mode set to 100644
File size: 11.6 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 kernel_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 EOK;
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, EOK);
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 errno_t rc;
221
222 uspace_addr = ipc_get_arg2(&call->data); /* Destination address */
223 buf_size = ipc_get_arg3(&call->data); /* Dest. buffer size */
224
225 /*
226 * Read task name.
227 */
228 rc = udebug_name_read((char **) &data, &data_size);
229 if (rc != EOK) {
230 ipc_set_retval(&call->data, rc);
231 ipc_answer(&TASK->kb.box, call);
232 return;
233 }
234
235 /* Copy MAX(buf_size, data_size) bytes */
236
237 if (buf_size > data_size)
238 to_copy = data_size;
239 else
240 to_copy = buf_size;
241
242 /*
243 * Make use of call->buffer to transfer data to caller's userspace
244 */
245
246 ipc_set_retval(&call->data, EOK);
247 /*
248 * ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
249 * same code in process_answer() can be used
250 * (no way to distinguish method in answer)
251 */
252 ipc_set_arg1(&call->data, uspace_addr);
253 ipc_set_arg2(&call->data, to_copy);
254
255 ipc_set_arg3(&call->data, data_size);
256 call->buffer = data;
257
258 ipc_answer(&TASK->kb.box, call);
259}
260
261/** Process an AREAS_READ call.
262 *
263 * Returns a list of address space areas in the current task, as an array
264 * of as_area_info_t structures.
265 *
266 * @param call The call structure.
267 */
268static void udebug_receive_areas_read(call_t *call)
269{
270 sysarg_t uspace_addr;
271 sysarg_t to_copy;
272 size_t data_size;
273 size_t buf_size;
274 as_area_info_t *data;
275
276 uspace_addr = ipc_get_arg2(&call->data); /* Destination address */
277 buf_size = ipc_get_arg3(&call->data); /* Dest. buffer size */
278
279 /*
280 * Read area list.
281 */
282 data = as_get_area_info(AS, &data_size);
283 if (!data) {
284 ipc_set_retval(&call->data, ENOMEM);
285 ipc_answer(&TASK->kb.box, call);
286 return;
287 }
288
289 /* Copy MAX(buf_size, data_size) bytes */
290
291 if (buf_size > data_size)
292 to_copy = data_size;
293 else
294 to_copy = buf_size;
295
296 /*
297 * Make use of call->buffer to transfer data to caller's userspace
298 */
299
300 ipc_set_retval(&call->data, EOK);
301 /*
302 * ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
303 * same code in process_answer() can be used
304 * (no way to distinguish method in answer)
305 */
306 ipc_set_arg1(&call->data, uspace_addr);
307 ipc_set_arg2(&call->data, to_copy);
308
309 ipc_set_arg3(&call->data, data_size);
310 call->buffer = (uint8_t *) data;
311
312 ipc_answer(&TASK->kb.box, call);
313}
314
315/** Process an ARGS_READ call.
316 *
317 * Reads the argument of a current syscall event (SYSCALL_B or SYSCALL_E).
318 * @param call The call structure.
319 */
320static void udebug_receive_args_read(call_t *call)
321{
322 thread_t *t;
323 sysarg_t uspace_addr;
324 errno_t rc;
325 void *buffer;
326
327 t = (thread_t *)ipc_get_arg2(&call->data);
328
329 rc = udebug_args_read(t, &buffer);
330 if (rc != EOK) {
331 ipc_set_retval(&call->data, rc);
332 ipc_answer(&TASK->kb.box, call);
333 return;
334 }
335
336 /*
337 * Make use of call->buffer to transfer data to caller's userspace
338 */
339
340 uspace_addr = ipc_get_arg3(&call->data);
341
342 ipc_set_retval(&call->data, EOK);
343 /*
344 * ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
345 * same code in process_answer() can be used
346 * (no way to distinguish method in answer)
347 */
348 ipc_set_arg1(&call->data, uspace_addr);
349 ipc_set_arg2(&call->data, 6 * sizeof(sysarg_t));
350 call->buffer = buffer;
351
352 ipc_answer(&TASK->kb.box, call);
353}
354
355/** Receive a REGS_READ call.
356 *
357 * Reads the thread's register state (istate structure).
358 */
359static void udebug_receive_regs_read(call_t *call)
360{
361 thread_t *t;
362 sysarg_t uspace_addr;
363 sysarg_t to_copy;
364 void *buffer = NULL;
365 errno_t rc;
366
367 t = (thread_t *) ipc_get_arg2(&call->data);
368
369 rc = udebug_regs_read(t, &buffer);
370 if (rc != EOK) {
371 ipc_set_retval(&call->data, rc);
372 ipc_answer(&TASK->kb.box, call);
373 return;
374 }
375
376 assert(buffer != NULL);
377
378 /*
379 * Make use of call->buffer to transfer data to caller's userspace
380 */
381
382 uspace_addr = ipc_get_arg3(&call->data);
383 to_copy = sizeof(istate_t);
384
385 ipc_set_retval(&call->data, EOK);
386 /*
387 * ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
388 * same code in process_answer() can be used
389 * (no way to distinguish method in answer)
390 */
391 ipc_set_arg1(&call->data, uspace_addr);
392 ipc_set_arg2(&call->data, to_copy);
393
394 call->buffer = buffer;
395
396 ipc_answer(&TASK->kb.box, call);
397}
398
399/** Process an MEM_READ call.
400 *
401 * Reads memory of the current (debugged) task.
402 * @param call The call structure.
403 */
404static void udebug_receive_mem_read(call_t *call)
405{
406 uspace_addr_t uspace_dst;
407 uspace_addr_t uspace_src;
408 unsigned size;
409 void *buffer = NULL;
410 errno_t rc;
411
412 uspace_dst = ipc_get_arg2(&call->data);
413 uspace_src = ipc_get_arg3(&call->data);
414 size = ipc_get_arg4(&call->data);
415
416 rc = udebug_mem_read(uspace_src, size, &buffer);
417 if (rc != EOK) {
418 ipc_set_retval(&call->data, rc);
419 ipc_answer(&TASK->kb.box, call);
420 return;
421 }
422
423 assert(buffer != NULL);
424
425 ipc_set_retval(&call->data, EOK);
426 /*
427 * ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
428 * same code in process_answer() can be used
429 * (no way to distinguish method in answer)
430 */
431 ipc_set_arg1(&call->data, uspace_dst);
432 ipc_set_arg2(&call->data, size);
433 call->buffer = buffer;
434
435 ipc_answer(&TASK->kb.box, call);
436}
437
438/** Handle a debug call received on the kernel answerbox.
439 *
440 * This is called by the kbox servicing thread. Verifies that the sender
441 * is indeed the debugger and calls the appropriate processing function.
442 */
443void udebug_call_receive(call_t *call)
444{
445 int debug_method;
446
447 debug_method = ipc_get_arg1(&call->data);
448
449 if (debug_method != UDEBUG_M_BEGIN) {
450 /*
451 * Verify that the sender is this task's debugger.
452 * Note that this is the only thread that could change
453 * TASK->debugger. Therefore no locking is necessary
454 * and the sender can be safely considered valid until
455 * control exits this function.
456 */
457 if (TASK->udebug.debugger != call->sender) {
458 ipc_set_retval(&call->data, EINVAL);
459 ipc_answer(&TASK->kb.box, call);
460 return;
461 }
462 }
463
464 switch (debug_method) {
465 case UDEBUG_M_BEGIN:
466 udebug_receive_begin(call);
467 break;
468 case UDEBUG_M_END:
469 udebug_receive_end(call);
470 break;
471 case UDEBUG_M_SET_EVMASK:
472 udebug_receive_set_evmask(call);
473 break;
474 case UDEBUG_M_GO:
475 udebug_receive_go(call);
476 break;
477 case UDEBUG_M_STOP:
478 udebug_receive_stop(call);
479 break;
480 case UDEBUG_M_THREAD_READ:
481 udebug_receive_thread_read(call);
482 break;
483 case UDEBUG_M_NAME_READ:
484 udebug_receive_name_read(call);
485 break;
486 case UDEBUG_M_AREAS_READ:
487 udebug_receive_areas_read(call);
488 break;
489 case UDEBUG_M_ARGS_READ:
490 udebug_receive_args_read(call);
491 break;
492 case UDEBUG_M_REGS_READ:
493 udebug_receive_regs_read(call);
494 break;
495 case UDEBUG_M_MEM_READ:
496 udebug_receive_mem_read(call);
497 break;
498 }
499}
500
501/** @}
502 */
Note: See TracBrowser for help on using the repository browser.