Subversion Repositories HelenOS

Rev

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

  1. /** @addtogroup generic
  2.  * @{
  3.  */
  4.  
  5. /**
  6.  * @file
  7.  * @brief   Tdebug.
  8.  */
  9.  
  10. #include <console/klog.h>
  11. #include <proc/task.h>
  12. #include <proc/thread.h>
  13. #include <arch.h>
  14. #include <errno.h>
  15. #include <ipc/ipc.h>
  16. #include <syscall/copy.h>
  17. #include <udebug/udebug.h>
  18. #include <udebug/udebug_ipc.h>
  19.  
  20. /**
  21.  * Get and lock a phone's callee task.
  22.  *
  23.  * This will return a pointer to the task to which the phone
  24.  * is connected. It will lock the task, making sure it exists.
  25.  * (TODO: make sure the udebug-cleanup of the task hasn't
  26.  * started yet)
  27.  */
  28. static task_t *get_lock_callee_task(phone_t *phone)
  29. {
  30.     answerbox_t *box;
  31.     task_t *ta;
  32.     task_id_t taskid;
  33.     ipl_t ipl;
  34.  
  35.     ipl = interrupts_disable();
  36.     spinlock_lock(&phone->lock);
  37.     if (phone->state != IPC_PHONE_CONNECTED) {
  38.         spinlock_unlock(&phone->lock);
  39.         interrupts_restore(ipl);
  40.         return NULL;
  41.     }
  42.  
  43.     box = phone->callee;
  44.    
  45.     spinlock_lock(&box->lock);
  46.     ta = box->task;
  47.     taskid = ta->taskid;
  48.     spinlock_unlock(&box->lock);
  49.     spinlock_unlock(&phone->lock);
  50.  
  51.     /* Locking decoupled using taskid */
  52.    
  53.     spinlock_lock(&tasks_lock);
  54.     ta = task_find_by_id(taskid);
  55.     if (ta == NULL) {
  56.         spinlock_unlock(&tasks_lock);
  57.         interrupts_restore(ipl);
  58.         return NULL;
  59.     }
  60.  
  61.     spinlock_lock(&ta->lock);
  62.     spinlock_unlock(&tasks_lock);
  63.     interrupts_restore(ipl);
  64.  
  65.     return ta;
  66. }
  67.  
  68. static int udebug_rp_begin(call_t *call, phone_t *phone)
  69. {
  70.     task_t *ta;
  71.     ipl_t ipl;
  72.  
  73.     klog_printf("debug_begin()");
  74.  
  75.     ipl = interrupts_disable();
  76.     ta = get_lock_callee_task(phone);
  77.     klog_printf("debugging task %llu", ta->taskid);
  78.  
  79.     if (ta->being_debugged != false) {
  80.         spinlock_unlock(&ta->lock);
  81.         interrupts_restore(ipl);
  82.         klog_printf("debug_begin(): busy error");
  83.         return EBUSY;
  84.     }
  85.  
  86.     ta->being_debugged = true;
  87.     ta->stop_request = true;
  88.     ta->debug_begin_call = call;
  89.  
  90.     if (ta->not_stoppable_count == 0) {
  91.         ta->debug_begin_call = NULL;
  92.         ta->stop_request = false;
  93.         spinlock_unlock(&ta->lock);
  94.         interrupts_restore(ipl);
  95.         klog_printf("debug_begin(): immediate backsend");
  96.         return 1; /* actually we need backsend with 0 retval */
  97.     }
  98.  
  99.     spinlock_unlock(&ta->lock);
  100.     interrupts_restore(ipl);
  101.  
  102.     klog_printf("debug_begin() done (wait for stoppability)");
  103.     return 0;
  104. }
  105.  
  106. static int udebug_rp_go(call_t *call, phone_t *phone)
  107. {
  108.     thread_t *t;
  109.     task_t *ta;
  110.     ipl_t ipl;
  111.  
  112.     klog_printf("debug_go()");
  113.     ta = get_lock_callee_task(phone);
  114.  
  115.     // FIXME: must save this in thread struct, not task struct!!!
  116.     ta->debug_go_call = call;
  117.     spinlock_unlock(&ta->lock);
  118.  
  119.     t = (thread_t *) IPC_GET_ARG2(call->data);
  120.  
  121.     ipl = interrupts_disable();
  122.     spinlock_lock(&threads_lock);
  123.     if (!thread_exists(t)) {
  124.         spinlock_unlock(&threads_lock);
  125.         interrupts_restore(ipl);
  126.         return ENOENT;
  127.     }
  128.  
  129.     waitq_wakeup(&t->go_wq, WAKEUP_FIRST);
  130.     spinlock_unlock(&threads_lock);
  131.     interrupts_restore(ipl);
  132.  
  133.     return 0; /* no backsend */
  134. }
  135.  
  136. static int udebug_rp_args_read(call_t *call, phone_t *phone)
  137. {
  138.     thread_t *t;
  139.     task_t *ta;
  140.     void *uspace_buffer;
  141.     unative_t to_copy;
  142.     int rc;
  143.     ipl_t ipl;
  144.  
  145.     klog_printf("debug_args_read()");
  146.     // FIXME: verify task/thread state
  147.  
  148.     ta = get_lock_callee_task(phone);
  149.     klog_printf("task %llu", ta->taskid);
  150.     spinlock_unlock(&ta->lock);
  151.  
  152.     t = (thread_t *) IPC_GET_ARG2(call->data);
  153.  
  154.     ipl = interrupts_disable();
  155.     spinlock_lock(&threads_lock);
  156.  
  157.     if (!thread_exists(t)) {
  158.         spinlock_unlock(&threads_lock);
  159.         interrupts_restore(ipl);
  160.         return ENOENT;
  161.     }
  162.  
  163.     // FIXME: copy to a local buffer before releasing the lock
  164.     spinlock_unlock(&threads_lock);
  165.     interrupts_restore(ipl);
  166.  
  167.     uspace_buffer = (void *)IPC_GET_ARG3(call->data);
  168.     to_copy = IPC_GET_ARG4(call->data);
  169.     if (to_copy > 6 * sizeof(unative_t)) to_copy = 6 * sizeof(unative_t);
  170.  
  171.     rc = copy_to_uspace(uspace_buffer, t->syscall_args, to_copy);
  172.     if (rc != 0) {
  173.         spinlock_unlock(&ta->lock);
  174.         klog_printf("debug_args_read() - copy failed");
  175.         return rc;
  176.     }
  177.  
  178.     IPC_SET_ARG1(call->data, to_copy);
  179.  
  180.     klog_printf("debug_args_read() done");
  181.     return 1; /* actually need becksend with retval 0 */
  182. }
  183.  
  184. static int udebug_rp_regs_read(call_t *call, phone_t *phone)
  185. {
  186.     thread_t *t;
  187.     task_t *ta;
  188.     void *uspace_buffer;
  189.     unative_t to_copy;
  190.     int rc;
  191.     istate_t *state;
  192.  
  193.     klog_printf("debug_regs_read()");
  194.     // FIXME: verify task/thread state
  195.  
  196.     state = THREAD->uspace_state;
  197.     if (state == NULL) {
  198.         klog_printf("debug_regs_read() - istate not available");
  199.         return EBUSY;
  200.     }
  201.  
  202.     ta = get_lock_callee_task(phone);
  203.     t = (thread_t *) IPC_GET_ARG2(call->data);
  204.     if (!thread_exists(t)) {
  205.         spinlock_unlock(&ta->lock);
  206.         return ENOENT;
  207.     }
  208.  
  209.     uspace_buffer = (void *)IPC_GET_ARG3(call->data);
  210.     to_copy = IPC_GET_ARG4(call->data);
  211.     if (to_copy > sizeof(istate_t)) to_copy = sizeof(istate_t);
  212.  
  213.     rc = copy_to_uspace(uspace_buffer, t->uspace_state, to_copy);
  214.     if (rc != 0) {
  215.         spinlock_unlock(&ta->lock);
  216.         klog_printf("debug_regs_read() - copy failed");
  217.         return rc;
  218.     }
  219.  
  220.     spinlock_unlock(&ta->lock);
  221.  
  222.     IPC_SET_ARG1(call->data, to_copy);
  223.     IPC_SET_ARG2(call->data, sizeof(istate_t));
  224.  
  225.     klog_printf("debug_regs_read() done");
  226.     return 1; /* actually need becksend with retval 0 */
  227. }
  228.  
  229. static int udebug_rp_regs_write(call_t *call, phone_t *phone)
  230. {
  231.     thread_t *t;
  232.     task_t *ta;
  233.     void *uspace_data;
  234.     unative_t to_copy;
  235.     int rc;
  236.     istate_t *state;
  237.  
  238.     klog_printf("debug_regs_write()");
  239.     // FIXME: verify task/thread state
  240.  
  241.     state = THREAD->uspace_state;
  242.     if (state == NULL) {
  243.         klog_printf("debug_regs_write() - istate not available");
  244.         return EBUSY;
  245.     }
  246.  
  247.     ta = get_lock_callee_task(phone);
  248.     t = (thread_t *) IPC_GET_ARG2(call->data);
  249.     if (!thread_exists(t)) {
  250.         spinlock_unlock(&ta->lock);
  251.         return ENOENT;
  252.     }
  253.  
  254.     uspace_data = (void *)IPC_GET_ARG3(call->data);
  255.     to_copy = IPC_GET_ARG4(call->data);
  256.     if (to_copy > sizeof(istate_t)) to_copy = sizeof(istate_t);
  257.  
  258.     rc = copy_from_uspace(t->uspace_state, uspace_data, to_copy);
  259.     if (rc != 0) {
  260.         spinlock_unlock(&ta->lock);
  261.         klog_printf("debug_regs_write() - copy failed");
  262.         return rc;
  263.     }
  264.  
  265.     spinlock_unlock(&ta->lock);
  266.  
  267.     IPC_SET_ARG1(call->data, to_copy);
  268.     IPC_SET_ARG2(call->data, sizeof(istate_t));
  269.  
  270.     klog_printf("debug_regs_write() done");
  271.     return 1; /* actually need becksend with retval 0 */
  272. }
  273.  
  274. static int udebug_rp_thread_read(call_t *call, phone_t *phone)
  275. {
  276.     thread_t *t;
  277.     link_t *cur;
  278.     task_t *ta;
  279.     unative_t *uspace_buffer;
  280.     unative_t to_copy;
  281.     int rc;
  282.     unsigned copied, total;
  283.     unsigned buf_size;
  284.     unative_t tid;
  285.  
  286.     klog_printf("debug_thread_read()");
  287.     // FIXME: verify task/thread state
  288.  
  289.     ta = get_lock_callee_task(phone);
  290.     klog_printf("task %llu", ta->taskid);
  291.    
  292.     uspace_buffer = (void *)IPC_GET_ARG2(call->data);
  293.     buf_size = IPC_GET_ARG3(call->data);
  294.  
  295.     copied = total = 0;
  296.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  297.         t = list_get_instance(cur, thread_t, th_link);
  298.  
  299.         /* Not interested in kernel threads */
  300.         if ((t->flags & THREAD_FLAG_USPACE) == 0)
  301.             continue;
  302.  
  303.         /* Using thread struct pointer for identification */
  304.         tid = (unative_t) t;
  305.  
  306.         to_copy = sizeof(unative_t);
  307.         if (copied + to_copy >= buf_size)
  308.             to_copy = buf_size - copied;
  309.  
  310.         if (to_copy > 0) {
  311.             rc = copy_to_uspace(uspace_buffer, &tid, to_copy);
  312.             if (rc != 0) {
  313.                 spinlock_unlock(&ta->lock);
  314.                 klog_printf("debug_thread_read() - copy failed");
  315.                 return rc;
  316.             }
  317.         }
  318.  
  319.         ++uspace_buffer;
  320.         total += sizeof(unative_t);
  321.         copied += to_copy;
  322.     }
  323.  
  324.     spinlock_unlock(&ta->lock);
  325.  
  326.     IPC_SET_ARG1(call->data, copied);
  327.     IPC_SET_ARG2(call->data, total);
  328.  
  329.     klog_printf("debug_thread_read() done");
  330.     return 1; /* actually need becksend with retval 0 */
  331. }
  332.  
  333. static int udebug_rp_mem_write(call_t *call, phone_t *phone)
  334. {
  335.     void *uspace_data;
  336.     unative_t to_copy;
  337.     int rc;
  338.     void *buffer;
  339.  
  340.     klog_printf("udebug_rp_mem_write()");
  341.     // FIXME: verify task/thread state
  342.  
  343.     uspace_data = (void *)IPC_GET_ARG2(call->data);
  344.     to_copy = IPC_GET_ARG4(call->data);
  345.  
  346.     buffer = malloc(to_copy, 0); // ???
  347.  
  348.     rc = copy_from_uspace(buffer, uspace_data, to_copy);
  349.     if (rc != 0) {
  350.         klog_printf(" - copy failed");
  351.         return rc;
  352.     }
  353.  
  354.     call->buffer = buffer;
  355.  
  356.     klog_printf(" - done");
  357.     return 1; /* actually need becksend with retval 0 */
  358. }
  359.  
  360.  
  361. int udebug_request_preprocess(call_t *call, phone_t *phone)
  362. {
  363.     int rc;
  364.  
  365.     switch (IPC_GET_ARG1(call->data)) {
  366.     case UDEBUG_M_BEGIN:
  367.         rc = udebug_rp_begin(call, phone);
  368.         return rc;
  369.     case UDEBUG_M_GO:
  370.         rc = udebug_rp_go(call, phone);
  371.         return rc;
  372.     case UDEBUG_M_ARGS_READ:
  373.         rc = udebug_rp_args_read(call, phone);
  374.         return rc;
  375.     case UDEBUG_M_REGS_READ:
  376.         rc = udebug_rp_regs_read(call, phone);
  377.         return rc;
  378.     case UDEBUG_M_REGS_WRITE:
  379.         rc = udebug_rp_regs_write(call, phone);
  380.         return rc;
  381.     case UDEBUG_M_THREAD_READ:
  382.         rc = udebug_rp_thread_read(call, phone);
  383.         return rc;
  384.     case UDEBUG_M_MEM_WRITE:
  385.         rc = udebug_rp_mem_write(call, phone);
  386.         return rc;
  387.     default:
  388.         break;
  389.     }
  390.  
  391.     return 0;
  392. }
  393.  
  394. static void udebug_receive_mem_read(call_t *call)
  395. {
  396.     unative_t uspace_dst;
  397.     void *uspace_ptr;
  398.     unsigned size;
  399.     void *buffer;
  400.     int rc;
  401.  
  402.     klog_printf("debug_mem_read()");
  403.     uspace_dst = IPC_GET_ARG2(call->data);
  404.     uspace_ptr = (void *)IPC_GET_ARG3(call->data);
  405.     size = IPC_GET_ARG4(call->data);
  406.  
  407.     buffer = malloc(size, 0); // ???
  408.     klog_printf("debug_mem_read: src=%u, size=%u", uspace_ptr, size);
  409.  
  410.     /* NOTE: this is not strictly from a syscall... but that shouldn't
  411.      * be a problem */
  412.     rc = copy_from_uspace(buffer, uspace_ptr, size);
  413.     if (rc) {
  414.         IPC_SET_RETVAL(call->data, rc);
  415.         return;
  416.     }
  417.  
  418.     klog_printf("first word: %u", *((unative_t *)buffer));
  419.  
  420.     IPC_SET_RETVAL(call->data, 0);
  421.     /* Hack: ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
  422.        same code in process_answer() can be used
  423.        (no way to distinguish method in answer) */
  424.     IPC_SET_ARG1(call->data, uspace_dst);
  425.     IPC_SET_ARG2(call->data, size);
  426.     call->buffer = buffer;
  427.  
  428.     ipc_answer(&TASK->kernel_box, call);
  429. }
  430.  
  431. static void udebug_receive_mem_write(call_t *call)
  432. {
  433.     void *uspace_dst;
  434.     unsigned size;
  435.     void *buffer;
  436.     int rc;
  437.  
  438.     klog_printf("udebug_receive_mem_write()");
  439.     uspace_dst = (void *)IPC_GET_ARG3(call->data);
  440.     size = IPC_GET_ARG4(call->data);
  441.  
  442.     buffer = call->buffer;
  443.     klog_printf("dst=%u, size=%u", uspace_dst, size);
  444.  
  445.     /* NOTE: this is not strictly from a syscall... but that shouldn't
  446.      * be a problem */
  447.     rc = copy_to_uspace(uspace_dst, buffer, size);
  448.     if (rc) {
  449.         IPC_SET_RETVAL(call->data, rc);
  450.         return;
  451.     }
  452.  
  453.     IPC_SET_RETVAL(call->data, 0);
  454.  
  455.     free(call->buffer);
  456.     call->buffer = NULL;
  457.  
  458.     ipc_answer(&TASK->kernel_box, call);
  459. }
  460.  
  461.  
  462. /**
  463.  * Handle a debug call received on the kernel answerbox.
  464.  *
  465.  * This is called by the kbox servicing thread.
  466.  */
  467. void udebug_call_receive(call_t *call)
  468. {
  469.     int debug_method;
  470.  
  471.     debug_method = IPC_GET_ARG1(call->data);
  472.  
  473.     switch (debug_method) {
  474.     case UDEBUG_M_MEM_READ:
  475.         udebug_receive_mem_read(call);
  476.         break;
  477.     case UDEBUG_M_MEM_WRITE:
  478.         udebug_receive_mem_write(call);
  479.         break;
  480.     }
  481. }
  482.  
  483. /** @}
  484.  */
  485.