Subversion Repositories HelenOS

Rev

Rev 2827 | Rev 2834 | 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.     int rc;
  73.  
  74.     thread_t *t;
  75.     link_t *cur;
  76.  
  77.     klog_printf("debug_begin()");
  78.  
  79.     ipl = interrupts_disable();
  80.     ta = get_lock_callee_task(phone);
  81.     klog_printf("debugging task %llu", ta->taskid);
  82.  
  83.     if (ta->dt_state != UDEBUG_TS_INACTIVE) {
  84.         spinlock_unlock(&ta->lock);
  85.         interrupts_restore(ipl);
  86.         klog_printf("debug_begin(): busy error");
  87.         return EBUSY;
  88.     }
  89.  
  90.     ta->dt_state = UDEBUG_TS_BEGINNING;
  91.     ta->debug_begin_call = call;
  92.  
  93.     if (ta->not_stoppable_count == 0) {
  94.         ta->dt_state = UDEBUG_TS_ACTIVE;
  95.         ta->debug_begin_call = NULL;
  96.         rc = 1; /* actually we need backsend with 0 retval */
  97.     } else {
  98.         rc = 0; /* no backsend */
  99.     }
  100.    
  101.     /* Set debug_active on all of the task's userspace threads */
  102.  
  103.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  104.         t = list_get_instance(cur, thread_t, th_link);
  105.  
  106.         spinlock_lock(&t->lock);
  107.         if ((t->flags & THREAD_FLAG_USPACE) != 0)
  108.             t->debug_active = true;
  109.         spinlock_unlock(&t->lock);
  110.     }
  111.  
  112.     spinlock_unlock(&ta->lock);
  113.     interrupts_restore(ipl);
  114.  
  115.     klog_printf("debug_begin() done (%s)",
  116.         rc ? "backsend" : "stoppability wait");
  117.  
  118.     return rc;
  119. }
  120.  
  121. static int udebug_rp_go(call_t *call, phone_t *phone)
  122. {
  123.     thread_t *t;
  124.     task_t *ta;
  125.     ipl_t ipl;
  126.  
  127.     klog_printf("debug_go()");
  128.     ta = get_lock_callee_task(phone);
  129.     spinlock_unlock(&ta->lock);
  130.     // TODO: don't lock ta
  131.  
  132.     t = (thread_t *) IPC_GET_ARG2(call->data);
  133.  
  134.     ipl = interrupts_disable();
  135.     spinlock_lock(&threads_lock);
  136.  
  137.     /* Verify that 't' exists and belongs to task 'ta' */
  138.     if (!thread_exists(t) || (t->task != ta)) {
  139.         spinlock_unlock(&threads_lock);
  140.         interrupts_restore(ipl);
  141.         return ENOENT;
  142.     }
  143.  
  144.     if ((t->debug_active != true) || (t->debug_stop != true)) {
  145.         /* Not in debugging session or already has GO */
  146.         spinlock_unlock(&threads_lock);
  147.         interrupts_restore(ipl);       
  148.         return EBUSY;
  149.     }
  150.  
  151.     t->debug_go_call = call;
  152.     t->debug_stop = false;
  153.     waitq_wakeup(&t->go_wq, WAKEUP_FIRST);
  154.  
  155.     spinlock_unlock(&threads_lock);
  156.     interrupts_restore(ipl);
  157.  
  158.     return 0; /* no backsend */
  159. }
  160.  
  161. static int udebug_rp_args_read(call_t *call, phone_t *phone)
  162. {
  163.     thread_t *t;
  164.     task_t *ta;
  165.     void *uspace_buffer;
  166.     int rc;
  167.     ipl_t ipl;
  168.     unative_t buffer[6];
  169.  
  170.     klog_printf("debug_args_read()");
  171.  
  172.     ta = get_lock_callee_task(phone);
  173.     klog_printf("task %llu", ta->taskid);
  174.     spinlock_unlock(&ta->lock);
  175.  
  176.     t = (thread_t *) IPC_GET_ARG2(call->data);
  177.  
  178.     ipl = interrupts_disable();
  179.     spinlock_lock(&threads_lock);
  180.  
  181.     /* Verify that 't' exists and belongs to task 'ta' */
  182.     if (!thread_exists(t) || (t->task != ta)) {
  183.         spinlock_unlock(&threads_lock);
  184.         interrupts_restore(ipl);
  185.         return ENOENT;
  186.     }
  187.  
  188.     //FIXME: additionally we need to verify that we are inside a syscall
  189.     if ((t->debug_active != true) || (t->debug_stop != true)) {
  190.         /* Not in debugging session or has GO */
  191.         spinlock_unlock(&threads_lock);
  192.         interrupts_restore(ipl);       
  193.         return EBUSY;
  194.     }
  195.  
  196.     /* Copy to a local buffer before releasing the lock */
  197.     memcpy(buffer, t->syscall_args, 6 * sizeof(unative_t));
  198.  
  199.     spinlock_unlock(&threads_lock);
  200.     interrupts_restore(ipl);
  201.  
  202.     /* Now copy to userspace */
  203.  
  204.     uspace_buffer = (void *)IPC_GET_ARG3(call->data);
  205.  
  206.     rc = copy_to_uspace(uspace_buffer, buffer, 6 * sizeof(unative_t));
  207.     if (rc != 0) {
  208.         spinlock_unlock(&ta->lock);
  209.         klog_printf("debug_args_read() - copy failed");
  210.         return rc;
  211.     }
  212.  
  213.     klog_printf("debug_args_read() done");
  214.     return 1; /* actually need becksend with retval 0 */
  215. }
  216.  
  217. static int udebug_rp_regs_read(call_t *call, phone_t *phone)
  218. {
  219.     thread_t *t;
  220.     task_t *ta;
  221.     void *uspace_buffer;
  222.     unative_t to_copy;
  223.     int rc;
  224.     istate_t *state;
  225.     istate_t state_copy;
  226.     ipl_t ipl;
  227.  
  228.     klog_printf("debug_regs_read()");
  229.  
  230.     ta = get_lock_callee_task(phone);
  231.     spinlock_unlock(&ta->lock);
  232.     //FIXME: don't lock ta
  233.  
  234.     ipl = interrupts_disable();
  235.     spinlock_lock(&threads_lock);
  236.  
  237.     t = (thread_t *) IPC_GET_ARG2(call->data);
  238.  
  239.     /* Verify that 't' exists and belongs to task 'ta' */
  240.     if (!thread_exists(t) || (t->task != ta)) {
  241.         spinlock_unlock(&threads_lock);
  242.         interrupts_restore(ipl);
  243.         return ENOENT;
  244.     }
  245.  
  246.     if ((t->debug_active != true) || (t->debug_stop != true)) {
  247.         /* Not in debugging session or has GO */
  248.         spinlock_unlock(&threads_lock);
  249.         interrupts_restore(ipl);       
  250.         return EBUSY;
  251.     }
  252.  
  253.     state = t->uspace_state;
  254.     if (state == NULL) {
  255.         spinlock_unlock(&threads_lock);
  256.         interrupts_restore(ipl);
  257.         klog_printf("debug_regs_read() - istate not available");
  258.         return EBUSY;
  259.     }
  260.  
  261.     /* Copy to a local buffer so that we can release the lock */
  262.     memcpy(&state_copy, state, sizeof(state_copy));
  263.     spinlock_unlock(&threads_lock);
  264.     interrupts_restore(ipl);
  265.  
  266.     uspace_buffer = (void *)IPC_GET_ARG3(call->data);
  267.     to_copy = IPC_GET_ARG4(call->data);
  268.     if (to_copy > sizeof(istate_t)) to_copy = sizeof(istate_t);
  269.  
  270.     rc = copy_to_uspace(uspace_buffer, &state_copy, to_copy);
  271.     if (rc != 0) {
  272.         spinlock_unlock(&ta->lock);
  273.         klog_printf("debug_regs_read() - copy failed");
  274.         return rc;
  275.     }
  276.  
  277.     IPC_SET_ARG1(call->data, to_copy);
  278.     IPC_SET_ARG2(call->data, sizeof(istate_t));
  279.  
  280.     klog_printf("debug_regs_read() done");
  281.     return 1; /* actually need becksend with retval 0 */
  282. }
  283.  
  284. static int udebug_rp_regs_write(call_t *call, phone_t *phone)
  285. {
  286.     thread_t *t;
  287.     task_t *ta;
  288.     void *uspace_data;
  289.     unative_t to_copy;
  290.     int rc;
  291.     istate_t *state;
  292.     istate_t data_copy;
  293.     ipl_t ipl;
  294.  
  295.     klog_printf("debug_regs_write()");
  296.  
  297.     /* First copy to a local buffer */
  298.  
  299.     uspace_data = (void *)IPC_GET_ARG3(call->data);
  300.     to_copy = IPC_GET_ARG4(call->data);
  301.     if (to_copy > sizeof(istate_t)) to_copy = sizeof(istate_t);
  302.  
  303.     rc = copy_from_uspace(&data_copy, uspace_data, to_copy);
  304.     if (rc != 0) {
  305.         klog_printf("debug_regs_write() - copy failed");
  306.         return rc;
  307.     }
  308.  
  309.     ta = get_lock_callee_task(phone);
  310.     spinlock_unlock(&ta->lock);
  311.     //FIXME: don't lock ta
  312.  
  313.     /* Now try to change the thread's uspace_state */
  314.  
  315.     ipl = interrupts_disable();
  316.     spinlock_lock(&threads_lock);
  317.  
  318.     t = (thread_t *) IPC_GET_ARG2(call->data);
  319.  
  320.     /* Verify that 't' exists and belongs to task 'ta' */
  321.     if (!thread_exists(t) || (t->task != ta)) {
  322.         spinlock_unlock(&threads_lock);
  323.         interrupts_restore(ipl);
  324.         return ENOENT;
  325.     }
  326.  
  327.     if ((t->debug_active != true) || (t->debug_stop != true)) {
  328.         /* Not in debugging session or has GO */
  329.         spinlock_unlock(&threads_lock);
  330.         interrupts_restore(ipl);       
  331.         return EBUSY;
  332.     }
  333.  
  334.     state = t->uspace_state;
  335.     if (state == NULL) {
  336.         spinlock_unlock(&threads_lock);
  337.         interrupts_restore(ipl);
  338.         klog_printf("debug_regs_write() - istate not available");
  339.         return EBUSY;
  340.     }
  341.  
  342.     memcpy(t->uspace_state, &data_copy, sizeof(t->uspace_state));
  343.  
  344.     spinlock_unlock(&threads_lock);
  345.     interrupts_restore(ipl);
  346.  
  347.     /* Set answer values */
  348.  
  349.     IPC_SET_ARG1(call->data, to_copy);
  350.     IPC_SET_ARG2(call->data, sizeof(istate_t));
  351.  
  352.     klog_printf("debug_regs_write() done");
  353.     return 1; /* actually need becksend with retval 0 */
  354. }
  355.  
  356. static int udebug_rp_thread_read(call_t *call, phone_t *phone)
  357. {
  358.     thread_t *t;
  359.     link_t *cur;
  360.     task_t *ta;
  361.     unative_t *uspace_buffer;
  362.     unative_t to_copy;
  363.     int rc;
  364.     unsigned total_bytes;
  365.     unsigned buf_size;
  366.     unative_t tid;
  367.     unsigned num_threads, copied_ids;
  368.     ipl_t ipl;
  369.     unative_t *buffer;
  370.     int flags;
  371.  
  372.     klog_printf("debug_thread_read()");
  373.  
  374.     ipl = interrupts_disable();
  375.     ta = get_lock_callee_task(phone);
  376.  
  377.     /* Verify task state */
  378.     if (ta->dt_state != UDEBUG_TS_ACTIVE) {
  379.         spinlock_unlock(&ta->lock);
  380.         interrupts_restore(ipl);
  381.         return EBUSY;
  382.     }
  383.  
  384.     /* Count the threads first */
  385.  
  386.     num_threads = 0;
  387.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  388.         /* Count all threads, to be on the safe side */
  389.         ++num_threads;
  390.     }
  391.  
  392.     /* Allocate a buffer and copy down the threads' ids */
  393.     buffer = malloc(num_threads * sizeof(unative_t), 0); // ???
  394.  
  395.     copied_ids = 0;
  396.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  397.         t = list_get_instance(cur, thread_t, th_link);
  398.  
  399.         spinlock_lock(&t->lock);
  400.         flags = t->flags;
  401.         spinlock_unlock(&t->lock);
  402.  
  403.         /* Not interested in kernel threads */
  404.         if ((flags & THREAD_FLAG_USPACE) != 0) {
  405.             /* Using thread struct pointer for identification */
  406.             tid = (unative_t) t;
  407.             buffer[copied_ids++] = tid;
  408.         }
  409.     }
  410.  
  411.     spinlock_unlock(&ta->lock);
  412.     interrupts_restore(ipl);
  413.  
  414.     /* Now copy to userspace */
  415.  
  416.     uspace_buffer = (void *)IPC_GET_ARG2(call->data);
  417.     buf_size = IPC_GET_ARG3(call->data);
  418.  
  419.     total_bytes = copied_ids * sizeof(unative_t);
  420.  
  421.     if (buf_size > total_bytes)
  422.         to_copy = total_bytes;
  423.     else
  424.         to_copy = buf_size;
  425.  
  426.     rc = copy_to_uspace(uspace_buffer, buffer, to_copy);
  427.     free(buffer);
  428.  
  429.     if (rc != 0) {
  430.         klog_printf("debug_thread_read() - copy failed");
  431.         return rc;
  432.     }
  433.  
  434.     IPC_SET_ARG1(call->data, to_copy);
  435.     IPC_SET_ARG2(call->data, total_bytes);
  436.  
  437.     klog_printf("debug_thread_read() done");
  438.     return 1; /* actually need becksend with retval 0 */
  439. }
  440.  
  441. static int udebug_rp_mem_write(call_t *call, phone_t *phone)
  442. {
  443.     void *uspace_data;
  444.     unative_t to_copy;
  445.     int rc;
  446.     void *buffer;
  447.  
  448.     klog_printf("udebug_rp_mem_write()");
  449.  
  450.     uspace_data = (void *)IPC_GET_ARG2(call->data);
  451.     to_copy = IPC_GET_ARG4(call->data);
  452.  
  453.     buffer = malloc(to_copy, 0); // ???
  454.  
  455.     rc = copy_from_uspace(buffer, uspace_data, to_copy);
  456.     if (rc != 0) {
  457.         klog_printf(" - copy failed");
  458.         return rc;
  459.     }
  460.  
  461.     call->buffer = buffer;
  462.  
  463.     klog_printf(" - done");
  464.     return 1; /* actually need becksend with retval 0 */
  465. }
  466.  
  467.  
  468. int udebug_request_preprocess(call_t *call, phone_t *phone)
  469. {
  470.     int rc;
  471.  
  472.     switch (IPC_GET_ARG1(call->data)) {
  473.     case UDEBUG_M_BEGIN:
  474.         rc = udebug_rp_begin(call, phone);
  475.         return rc;
  476.     case UDEBUG_M_GO:
  477.         rc = udebug_rp_go(call, phone);
  478.         return rc;
  479.     case UDEBUG_M_ARGS_READ:
  480.         rc = udebug_rp_args_read(call, phone);
  481.         return rc;
  482.     case UDEBUG_M_REGS_READ:
  483.         rc = udebug_rp_regs_read(call, phone);
  484.         return rc;
  485.     case UDEBUG_M_REGS_WRITE:
  486.         rc = udebug_rp_regs_write(call, phone);
  487.         return rc;
  488.     case UDEBUG_M_THREAD_READ:
  489.         rc = udebug_rp_thread_read(call, phone);
  490.         return rc;
  491.     case UDEBUG_M_MEM_WRITE:
  492.         rc = udebug_rp_mem_write(call, phone);
  493.         return rc;
  494.     default:
  495.         break;
  496.     }
  497.  
  498.     return 0;
  499. }
  500.  
  501. static void udebug_receive_mem_read(call_t *call)
  502. {
  503.     unative_t uspace_dst;
  504.     void *uspace_ptr;
  505.     unsigned size;
  506.     void *buffer;
  507.     int rc;
  508.  
  509.     klog_printf("debug_mem_read()");
  510.     uspace_dst = IPC_GET_ARG2(call->data);
  511.     uspace_ptr = (void *)IPC_GET_ARG3(call->data);
  512.     size = IPC_GET_ARG4(call->data);
  513.  
  514.     buffer = malloc(size, 0); // ???
  515.     klog_printf("debug_mem_read: src=%u, size=%u", uspace_ptr, size);
  516.  
  517.     /* NOTE: this is not strictly from a syscall... but that shouldn't
  518.      * be a problem */
  519.     rc = copy_from_uspace(buffer, uspace_ptr, size);
  520.     if (rc) {
  521.         IPC_SET_RETVAL(call->data, rc);
  522.         return;
  523.     }
  524.  
  525.     klog_printf("first word: %u", *((unative_t *)buffer));
  526.  
  527.     IPC_SET_RETVAL(call->data, 0);
  528.     /* Hack: ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
  529.        same code in process_answer() can be used
  530.        (no way to distinguish method in answer) */
  531.     IPC_SET_ARG1(call->data, uspace_dst);
  532.     IPC_SET_ARG2(call->data, size);
  533.     call->buffer = buffer;
  534.  
  535.     ipc_answer(&TASK->kernel_box, call);
  536. }
  537.  
  538. static void udebug_receive_mem_write(call_t *call)
  539. {
  540.     void *uspace_dst;
  541.     unsigned size;
  542.     void *buffer;
  543.     int rc;
  544.     udebug_task_state_t dts;
  545.  
  546.     klog_printf("udebug_receive_mem_write()");
  547.  
  548.     /* Verify task state */
  549.     spinlock_lock(&TASK->lock);
  550.     dts = TASK->dt_state;
  551.     spinlock_unlock(&TASK->lock);
  552.  
  553.     if (dts != UDEBUG_TS_ACTIVE) {
  554.         IPC_SET_RETVAL(call->data, EBUSY);
  555.         ipc_answer(&TASK->kernel_box, call);
  556.         return;
  557.     }
  558.    
  559.     uspace_dst = (void *)IPC_GET_ARG3(call->data);
  560.     size = IPC_GET_ARG4(call->data);
  561.  
  562.     buffer = call->buffer;
  563.     klog_printf("dst=%u, size=%u", uspace_dst, size);
  564.  
  565.     /* NOTE: this is not strictly from a syscall... but that shouldn't
  566.      * be a problem */
  567.     rc = copy_to_uspace(uspace_dst, buffer, size);
  568.     if (rc) {
  569.         IPC_SET_RETVAL(call->data, rc);
  570.         ipc_answer(&TASK->kernel_box, call);
  571.         return;
  572.     }
  573.  
  574.     IPC_SET_RETVAL(call->data, 0);
  575.  
  576.     free(call->buffer);
  577.     call->buffer = NULL;
  578.  
  579.     ipc_answer(&TASK->kernel_box, call);
  580. }
  581.  
  582.  
  583. /**
  584.  * Handle a debug call received on the kernel answerbox.
  585.  *
  586.  * This is called by the kbox servicing thread.
  587.  */
  588. void udebug_call_receive(call_t *call)
  589. {
  590.     int debug_method;
  591.  
  592.     debug_method = IPC_GET_ARG1(call->data);
  593.  
  594.     switch (debug_method) {
  595.     case UDEBUG_M_MEM_READ:
  596.         udebug_receive_mem_read(call);
  597.         break;
  598.     case UDEBUG_M_MEM_WRITE:
  599.         udebug_receive_mem_write(call);
  600.         break;
  601.     }
  602. }
  603.  
  604. /** @}
  605.  */
  606.