Subversion Repositories HelenOS

Rev

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