Subversion Repositories HelenOS

Rev

Rev 2894 | Rev 2897 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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.  
  38. #include <console/klog.h>
  39. #include <proc/task.h>
  40. #include <proc/thread.h>
  41. #include <arch.h>
  42. #include <errno.h>
  43. #include <ipc/ipc.h>
  44. #include <syscall/copy.h>
  45. #include <udebug/udebug.h>
  46. #include <udebug/udebug_ops.h>
  47. #include <udebug/udebug_ipc.h>
  48.  
  49. static int udebug_rp_regs_write(call_t *call, phone_t *phone)
  50. {
  51.     void *uspace_data;
  52.     unative_t to_copy;
  53.     int rc;
  54.     void *buffer;
  55.  
  56.     klog_printf("debug_regs_write()");
  57.  
  58.     uspace_data = (void *)IPC_GET_ARG3(call->data);
  59.     to_copy = IPC_GET_ARG4(call->data);
  60.     if (to_copy > sizeof(istate_t)) to_copy = sizeof(istate_t);
  61.  
  62.     buffer = malloc(to_copy, 0);
  63.     if (!buffer) return ENOMEM;
  64.  
  65.     rc = copy_from_uspace(buffer, uspace_data, to_copy);
  66.     if (rc != 0) {
  67.         klog_printf("debug_regs_write() - copy failed");
  68.         return rc;
  69.     }
  70.  
  71.     call->buffer = buffer;
  72.  
  73.     klog_printf(" - done");
  74.     return 0;
  75. }
  76.  
  77. static int udebug_rp_mem_write(call_t *call, phone_t *phone)
  78. {
  79.     void *uspace_data;
  80.     unative_t to_copy;
  81.     int rc;
  82.     void *buffer;
  83.  
  84.     klog_printf("udebug_rp_mem_write()");
  85.  
  86.     uspace_data = (void *)IPC_GET_ARG2(call->data);
  87.     to_copy = IPC_GET_ARG4(call->data);
  88.  
  89.     buffer = malloc(to_copy, 0);
  90.     if (!buffer) return ENOMEM;
  91.  
  92.     rc = copy_from_uspace(buffer, uspace_data, to_copy);
  93.     if (rc != 0) {
  94.         klog_printf(" - copy failed");
  95.         return rc;
  96.     }
  97.  
  98.     call->buffer = buffer;
  99.  
  100.     klog_printf(" - done");
  101.     return 0;
  102. }
  103.  
  104.  
  105. int udebug_request_preprocess(call_t *call, phone_t *phone)
  106. {
  107.     int rc;
  108.  
  109.     switch (IPC_GET_ARG1(call->data)) {
  110.     case UDEBUG_M_REGS_WRITE:
  111.         rc = udebug_rp_regs_write(call, phone);
  112.         return rc;
  113.     case UDEBUG_M_MEM_WRITE:
  114.         rc = udebug_rp_mem_write(call, phone);
  115.         return rc;
  116.     default:
  117.         break;
  118.     }
  119.  
  120.     return 0;
  121. }
  122.  
  123. static void udebug_receive_begin(call_t *call)
  124. {
  125.     int rc;
  126.  
  127.     rc = udebug_begin(call);
  128.     if (rc < 0) {
  129.         IPC_SET_RETVAL(call->data, rc);
  130.         ipc_answer(&TASK->kernel_box, call);
  131.         return;
  132.     }
  133.  
  134.     if (rc != 0) {
  135.         IPC_SET_RETVAL(call->data, 0);
  136.         ipc_answer(&TASK->kernel_box, call);
  137.     }
  138. }
  139.  
  140. static void udebug_receive_end(call_t *call)
  141. {
  142.     int rc;
  143.  
  144.     rc = udebug_end();
  145.  
  146.     IPC_SET_RETVAL(call->data, rc);
  147.     ipc_answer(&TASK->kernel_box, call);
  148. }
  149.  
  150. static void udebug_receive_go(call_t *call)
  151. {
  152.     thread_t *t;
  153.     int rc;
  154.  
  155.     klog_printf("debug_go()");
  156.  
  157.     t = (thread_t *)IPC_GET_ARG2(call->data);
  158.  
  159.     rc = udebug_go(t, call);
  160.     if (rc < 0) {
  161.         IPC_SET_RETVAL(call->data, rc);
  162.         ipc_answer(&TASK->kernel_box, call);
  163.         return;
  164.     }
  165. }
  166.  
  167.  
  168. static void udebug_receive_thread_read(call_t *call)
  169. {
  170.     unative_t uspace_addr;
  171.     unative_t to_copy;
  172.     unsigned total_bytes;
  173.     unsigned buf_size;
  174.     void *buffer;
  175.     size_t n;
  176.     int rc;
  177.  
  178.     rc = udebug_thread_read(&buffer, &n);
  179.     if (rc < 0) {
  180.         IPC_SET_RETVAL(call->data, rc);
  181.         ipc_answer(&TASK->kernel_box, call);
  182.         return;
  183.     }
  184.  
  185.     /*
  186.      * Make use of call->buffer to transfer data to caller's userspace
  187.      */
  188.  
  189.     uspace_addr = IPC_GET_ARG2(call->data);
  190.     buf_size = IPC_GET_ARG3(call->data);
  191.  
  192.     total_bytes = n;
  193.  
  194.     if (buf_size > total_bytes)
  195.         to_copy = total_bytes;
  196.     else
  197.         to_copy = buf_size;
  198.  
  199.     IPC_SET_RETVAL(call->data, 0);
  200.     /* ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
  201.        same code in process_answer() can be used
  202.        (no way to distinguish method in answer) */
  203.     IPC_SET_ARG1(call->data, uspace_addr);
  204.     IPC_SET_ARG2(call->data, to_copy);
  205.  
  206.     IPC_SET_ARG3(call->data, total_bytes);
  207.     call->buffer = buffer;
  208.  
  209.     ipc_answer(&TASK->kernel_box, call);
  210. }
  211.  
  212. static void udebug_receive_args_read(call_t *call)
  213. {
  214.     thread_t *t;
  215.     unative_t uspace_addr;
  216.     int rc;
  217.     void *buffer;
  218.  
  219.     t = (thread_t *)IPC_GET_ARG2(call->data);
  220.  
  221.     rc = udebug_args_read(t, &buffer);
  222.     if (rc != EOK) {
  223.         IPC_SET_RETVAL(call->data, rc);
  224.         ipc_answer(&TASK->kernel_box, call);
  225.         return;
  226.     }
  227.  
  228.     /*
  229.      * Make use of call->buffer to transfer data to caller's userspace
  230.      */
  231.  
  232.     uspace_addr = IPC_GET_ARG3(call->data);
  233.  
  234.     IPC_SET_RETVAL(call->data, 0);
  235.     /* ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
  236.        same code in process_answer() can be used
  237.        (no way to distinguish method in answer) */
  238.     IPC_SET_ARG1(call->data, uspace_addr);
  239.     IPC_SET_ARG2(call->data, 6 * sizeof(unative_t));
  240.     call->buffer = buffer;
  241.  
  242.     ipc_answer(&TASK->kernel_box, call);
  243. }
  244.  
  245. static void udebug_receive_regs_read(call_t *call)
  246. {
  247.     thread_t *t;
  248.     unative_t uspace_addr;
  249.     unative_t to_copy;
  250.     unative_t buf_size;
  251.     unative_t total_bytes;
  252.     void *buffer;
  253.     int rc;
  254.     size_t n;
  255.  
  256.     klog_printf("debug_regs_read()");
  257.  
  258.     t = (thread_t *) IPC_GET_ARG2(call->data);
  259.  
  260.     rc = udebug_regs_read(t, &buffer, &n);
  261.     if (rc < 0) {
  262.         IPC_SET_RETVAL(call->data, rc);
  263.         ipc_answer(&TASK->kernel_box, call);
  264.         return;
  265.     }
  266.  
  267.     /*
  268.      * Make use of call->buffer to transfer data to caller's userspace
  269.      */
  270.  
  271.     uspace_addr = IPC_GET_ARG3(call->data);
  272.     buf_size = IPC_GET_ARG4(call->data);
  273.  
  274.     total_bytes = n;
  275.  
  276.     if (buf_size > total_bytes)
  277.         to_copy = total_bytes;
  278.     else
  279.         to_copy = buf_size;
  280.  
  281.     IPC_SET_RETVAL(call->data, 0);
  282.     /* ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
  283.        same code in process_answer() can be used
  284.        (no way to distinguish method in answer) */
  285.     IPC_SET_ARG1(call->data, uspace_addr);
  286.     IPC_SET_ARG2(call->data, to_copy);
  287.  
  288.     IPC_SET_ARG3(call->data, total_bytes);
  289.     call->buffer = buffer;
  290.  
  291.     ipc_answer(&TASK->kernel_box, call);
  292. }
  293.  
  294. static void udebug_receive_regs_write(call_t *call)
  295. {
  296.     thread_t *t;
  297.     void *uspace_data;
  298.     unative_t to_copy;
  299.     int rc;
  300.  
  301.     uspace_data = (void *)IPC_GET_ARG3(call->data);
  302.     to_copy = IPC_GET_ARG4(call->data);
  303.  
  304.     t = (thread_t *) IPC_GET_ARG2(call->data);
  305.  
  306.     rc = udebug_regs_write(t, call->buffer);
  307.     if (rc < 0) {
  308.         IPC_SET_RETVAL(call->data, rc);
  309.         ipc_answer(&TASK->kernel_box, call);
  310.         return;
  311.     }
  312.  
  313.     /* Set answer values */
  314.  
  315.     IPC_SET_ARG1(call->data, to_copy);
  316.     IPC_SET_ARG2(call->data, sizeof(istate_t));
  317.  
  318.     IPC_SET_RETVAL(call->data, 0);
  319.     free(call->buffer);
  320.     call->buffer = NULL;
  321.  
  322.     ipc_answer(&TASK->kernel_box, call);
  323. }
  324.  
  325.  
  326. static void udebug_receive_mem_read(call_t *call)
  327. {
  328.     unative_t uspace_dst;
  329.     unative_t uspace_src;
  330.     unsigned size;
  331.     void *buffer;
  332.     int rc;
  333.  
  334.     uspace_dst = IPC_GET_ARG2(call->data);
  335.     uspace_src = IPC_GET_ARG3(call->data);
  336.     size = IPC_GET_ARG4(call->data);
  337.  
  338.     rc = udebug_mem_read(uspace_src, size, &buffer);
  339.     if (rc < 0) {
  340.         IPC_SET_RETVAL(call->data, rc);
  341.         ipc_answer(&TASK->kernel_box, call);
  342.         return;
  343.     }
  344.  
  345.     IPC_SET_RETVAL(call->data, 0);
  346.     /* ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
  347.        same code in process_answer() can be used
  348.        (no way to distinguish method in answer) */
  349.     IPC_SET_ARG1(call->data, uspace_dst);
  350.     IPC_SET_ARG2(call->data, size);
  351.     call->buffer = buffer;
  352.  
  353.     ipc_answer(&TASK->kernel_box, call);
  354. }
  355.  
  356. static void udebug_receive_mem_write(call_t *call)
  357. {
  358.     unative_t uspace_dst;
  359.     unsigned size;
  360.     int rc;
  361.  
  362.     klog_printf("udebug_receive_mem_write()");
  363.  
  364.     uspace_dst = IPC_GET_ARG3(call->data);
  365.     size = IPC_GET_ARG4(call->data);
  366.  
  367.     rc = udebug_mem_write(uspace_dst, call->buffer, size);
  368.     if (rc < 0) {
  369.         IPC_SET_RETVAL(call->data, rc);
  370.         ipc_answer(&TASK->kernel_box, call);
  371.         return;
  372.     }
  373.  
  374.     IPC_SET_RETVAL(call->data, 0);
  375.     free(call->buffer);
  376.     call->buffer = NULL;
  377.  
  378.     ipc_answer(&TASK->kernel_box, call);
  379. }
  380.  
  381.  
  382. /**
  383.  * Handle a debug call received on the kernel answerbox.
  384.  *
  385.  * This is called by the kbox servicing thread.
  386.  */
  387. void udebug_call_receive(call_t *call)
  388. {
  389.     int debug_method;
  390.  
  391.     debug_method = IPC_GET_ARG1(call->data);
  392.  
  393.     if (debug_method != UDEBUG_M_BEGIN) {
  394.         /*
  395.          * Verify that the sender is this task's debugger.
  396.          * Note that this is the only thread that could change
  397.          * TASK->debugger. Therefore no locking is necessary
  398.          * and the sender can be safely considered valid until
  399.          * control exits this function.
  400.          */
  401.         if (TASK->debugger != call->sender) {
  402.             IPC_SET_RETVAL(call->data, EINVAL);
  403.             ipc_answer(&TASK->kernel_box, call);
  404.             return;
  405.         }
  406.     }
  407.  
  408.     switch (debug_method) {
  409.     case UDEBUG_M_BEGIN:
  410.         udebug_receive_begin(call);
  411.         break;
  412.     case UDEBUG_M_END:
  413.         udebug_receive_end(call);
  414.         break;
  415.     case UDEBUG_M_GO:
  416.         udebug_receive_go(call);
  417.         break;
  418.     case UDEBUG_M_THREAD_READ:
  419.         udebug_receive_thread_read(call);
  420.         break;
  421.     case UDEBUG_M_ARGS_READ:
  422.         udebug_receive_args_read(call);
  423.         break;
  424.     case UDEBUG_M_REGS_READ:
  425.         udebug_receive_regs_read(call);
  426.         break;
  427.     case UDEBUG_M_REGS_WRITE:
  428.         udebug_receive_regs_write(call);
  429.         break;
  430.     case UDEBUG_M_MEM_READ:
  431.         udebug_receive_mem_read(call);
  432.         break;
  433.     case UDEBUG_M_MEM_WRITE:
  434.         udebug_receive_mem_write(call);
  435.         break;
  436.     }
  437. }
  438.  
  439. /** @}
  440.  */
  441.