Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 2886 → Rev 2887

/branches/tracing/kernel/generic/src/udebug/udebug_ipc.c
15,86 → 15,9
#include <ipc/ipc.h>
#include <syscall/copy.h>
#include <udebug/udebug.h>
#include <udebug/udebug_ops.h>
#include <udebug/udebug_ipc.h>
 
/**
* Prepare a thread for a debugging operation.
*
* Simply put, return thread t with t->debug_lock held,
* but only if it verifies all conditions.
*
* Specifically, verifies that thread t exists, is a userspace thread,
* and belongs to the current task (TASK). It also locks t->debug_lock,
* making sure that t->debug_active is true - that the thread is
* in a valid debugging session.
*
* Returns EOK if all went well, or an error code otherwise.
* Interrupts must be already disabled when calling this function.
*
* Note: This function sports complicated locking.
*/
static int _thread_op_begin(thread_t *t)
{
int rc;
task_id_t taskid;
 
taskid = TASK->taskid;
 
/* Must lock threads_lock to ensure continued existence of the thread */
spinlock_lock(&threads_lock);
 
if (!thread_exists(t)) {
spinlock_unlock(&threads_lock);
return ENOENT;
}
 
spinlock_lock(&t->debug_lock);
spinlock_lock(&t->lock);
/* Now verify that it's the current task */
if (t->task != TASK) {
/* No such thread belonging to callee */
rc = ENOENT;
goto error_exit;
}
 
/* Verify that 't' is a userspace thread */
if ((t->flags & THREAD_FLAG_USPACE) == 0) {
/* It's not, deny its existence */
rc = ENOENT;
goto error_exit;
}
 
if ((t->debug_active != true) || (t->debug_stop != true)) {
/* Not in debugging session or already has GO */
rc = ENOENT;
goto error_exit;
}
 
spinlock_unlock(&threads_lock);
spinlock_unlock(&t->lock);
 
/* Only t->debug_lock left */
 
return EOK; /* All went well */
 
 
/* Executed when a check on the thread fails */
error_exit:
spinlock_unlock(&t->lock);
spinlock_unlock(&t->debug_lock);
spinlock_unlock(&threads_lock);
 
/* No locks left here */
return rc; /* Some errors occured */
}
 
 
static void _thread_op_end(thread_t *t)
{
spinlock_unlock(&t->debug_lock);
}
 
static int udebug_rp_regs_write(call_t *call, phone_t *phone)
{
void *uspace_data;
169,84 → 92,28
 
static void udebug_receive_begin(call_t *call)
{
ipl_t ipl;
int reply;
int rc;
 
thread_t *t;
link_t *cur;
 
klog_printf("debug_begin()");
 
ipl = interrupts_disable();
klog_printf("debugging task %llu", TASK->taskid);
 
spinlock_lock(&TASK->lock);
 
if (TASK->dt_state != UDEBUG_TS_INACTIVE) {
spinlock_unlock(&TASK->lock);
interrupts_restore(ipl);
klog_printf("debug_begin(): busy error");
 
IPC_SET_RETVAL(call->data, EBUSY);
rc = udebug_begin(call);
if (rc < 0) {
IPC_SET_RETVAL(call->data, rc);
ipc_answer(&TASK->kernel_box, call);
return;
}
 
TASK->dt_state = UDEBUG_TS_BEGINNING;
TASK->debug_begin_call = call;
TASK->debugger = call->sender;
 
if (TASK->not_stoppable_count == 0) {
TASK->dt_state = UDEBUG_TS_ACTIVE;
TASK->debug_begin_call = NULL;
reply = 1; /* immediate reply */
} else {
reply = 0; /* no reply */
if (rc != 0) {
IPC_SET_RETVAL(call->data, 0);
ipc_answer(&TASK->kernel_box, call);
}
/* Set debug_active on all of the task's userspace threads */
 
for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
t = list_get_instance(cur, thread_t, th_link);
 
spinlock_lock(&t->debug_lock);
if ((t->flags & THREAD_FLAG_USPACE) != 0)
t->debug_active = true;
spinlock_unlock(&t->debug_lock);
}
 
spinlock_unlock(&TASK->lock);
interrupts_restore(ipl);
 
klog_printf("debug_begin() done (%s)",
reply ? "reply" : "stoppability wait");
 
if (reply) ipc_answer(&TASK->kernel_box, call);
}
 
static void udebug_receive_end(call_t *call)
{
ipl_t ipl;
int rc;
 
klog_printf("udebug_receive_end()");
rc = udebug_end();
 
ipl = interrupts_disable();
spinlock_lock(&TASK->lock);
 
rc = udebug_task_cleanup(TASK);
 
klog_printf("task %llu", TASK->taskid);
 
spinlock_unlock(&TASK->lock);
interrupts_restore(ipl);
 
if (rc < 0) {
IPC_SET_RETVAL(call->data, EINVAL);
ipc_answer(&TASK->kernel_box, call);
return;
}
 
IPC_SET_RETVAL(call->data, 0);
IPC_SET_RETVAL(call->data, rc);
ipc_answer(&TASK->kernel_box, call);
}
 
253,7 → 120,6
static void udebug_receive_go(call_t *call)
{
thread_t *t;
ipl_t ipl;
int rc;
 
klog_printf("debug_go()");
260,101 → 126,40
 
t = (thread_t *)IPC_GET_ARG2(call->data);
 
ipl = interrupts_disable();
 
/* On success, this will lock t->debug_lock */
rc = _thread_op_begin(t);
if (rc != EOK) {
interrupts_restore(ipl);
 
rc = udebug_go(t, call);
if (rc < 0) {
IPC_SET_RETVAL(call->data, rc);
ipc_answer(&TASK->kernel_box, call);
return;
}
 
t->debug_go_call = call;
t->debug_stop = false;
t->cur_event = 0; /* none */
 
/*
* Neither t's lock nor threads_lock may be held during wakeup
*/
waitq_wakeup(&t->go_wq, WAKEUP_FIRST);
 
_thread_op_end(t);
interrupts_restore(ipl);
 
/* No reply */
}
 
 
static void udebug_receive_thread_read(call_t *call)
{
thread_t *t;
link_t *cur;
unative_t uspace_addr;
unative_t to_copy;
unsigned total_bytes;
unsigned buf_size;
unative_t tid;
unsigned num_threads, copied_ids;
ipl_t ipl;
unative_t *buffer;
int flags;
void *buffer;
size_t n;
int rc;
 
klog_printf("debug_thread_read()");
 
ipl = interrupts_disable();
spinlock_lock(&TASK->lock);
 
/* Verify task state */
if (TASK->dt_state != UDEBUG_TS_ACTIVE) {
spinlock_unlock(&TASK->lock);
interrupts_restore(ipl);
 
IPC_SET_RETVAL(call->data, EINVAL);
rc = udebug_thread_read(&buffer, &n);
if (rc < 0) {
IPC_SET_RETVAL(call->data, rc);
ipc_answer(&TASK->kernel_box, call);
return;
}
 
/* Count the threads first */
 
num_threads = 0;
for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
/* Count all threads, to be on the safe side */
++num_threads;
}
 
/* Allocate a buffer and copy down the threads' ids */
buffer = malloc(num_threads * sizeof(unative_t), 0); // ???
 
copied_ids = 0;
for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
t = list_get_instance(cur, thread_t, th_link);
 
spinlock_lock(&t->lock);
flags = t->flags;
spinlock_unlock(&t->lock);
 
/* Not interested in kernel threads */
if ((flags & THREAD_FLAG_USPACE) != 0) {
/* Using thread struct pointer for identification */
tid = (unative_t) t;
buffer[copied_ids++] = tid;
}
}
 
spinlock_unlock(&TASK->lock);
interrupts_restore(ipl);
 
/*
* Prepare data and send it back through call->buffer
* Make use of call->buffer to transfer data to caller's userspace
*/
 
uspace_addr = IPC_GET_ARG2(call->data);
buf_size = IPC_GET_ARG3(call->data);
 
total_bytes = copied_ids * sizeof(unative_t);
total_bytes = n;
 
if (buf_size > total_bytes)
to_copy = total_bytes;
369,7 → 174,7
IPC_SET_ARG2(call->data, to_copy);
 
IPC_SET_ARG3(call->data, total_bytes);
call->buffer = (void *)buffer;
call->buffer = buffer;
 
ipc_answer(&TASK->kernel_box, call);
}
379,41 → 184,17
thread_t *t;
unative_t uspace_addr;
int rc;
ipl_t ipl;
unative_t *buffer;
void *buffer;
 
klog_printf("debug_args_read()");
 
t = (thread_t *)IPC_GET_ARG2(call->data);
 
ipl = interrupts_disable();
 
/* On success, this will lock t->debug_lock */
rc = _thread_op_begin(t);
rc = udebug_args_read(t, &buffer);
if (rc != EOK) {
interrupts_restore(ipl);
IPC_SET_RETVAL(call->data, rc);
ipc_answer(&TASK->kernel_box, call);
return;
}
 
/* Additionally we need to verify that we are inside a syscall */
if (t->cur_event != UDEBUG_EVENT_SYSCALL) {
_thread_op_end(t);
interrupts_restore(ipl);
 
IPC_SET_RETVAL(call->data, EINVAL);
ipc_answer(&TASK->kernel_box, call);
return;
}
 
/* Copy to a local buffer before releasing the lock */
buffer = malloc(6 * sizeof(unative_t), 0); // ???
memcpy(buffer, t->syscall_args, 6 * sizeof(unative_t));
 
_thread_op_end(t);
interrupts_restore(ipl);
 
/*
* Make use of call->buffer to transfer data to caller's userspace
*/
426,7 → 207,7
(no way to distinguish method in answer) */
IPC_SET_ARG1(call->data, uspace_addr);
IPC_SET_ARG2(call->data, 6 * sizeof(unative_t));
call->buffer = (void *)buffer;
call->buffer = buffer;
 
ipc_answer(&TASK->kernel_box, call);
}
438,45 → 219,21
unative_t to_copy;
unative_t buf_size;
unative_t total_bytes;
istate_t *state;
void *buffer;
int rc;
ipl_t ipl;
size_t n;
 
klog_printf("debug_regs_read()");
 
t = (thread_t *) IPC_GET_ARG2(call->data);
 
ipl = interrupts_disable();
 
/* On success, this will lock t->debug_lock */
rc = _thread_op_begin(t);
if (rc != EOK) {
interrupts_restore(ipl);
 
rc = udebug_regs_read(t, &buffer, &n);
if (rc < 0) {
IPC_SET_RETVAL(call->data, rc);
ipc_answer(&TASK->kernel_box, call);
return;
}
 
state = t->uspace_state;
if (state == NULL) {
_thread_op_end(t);
interrupts_restore(ipl);
klog_printf("debug_regs_read() - istate not available");
 
IPC_SET_RETVAL(call->data, EBUSY);
ipc_answer(&TASK->kernel_box, call);
return;
}
 
/* Copy to an allocated buffer */
buffer = malloc(sizeof(istate_t), 0); // ???
memcpy(buffer, state, sizeof(istate_t));
 
_thread_op_end(t);
interrupts_restore(ipl);
 
/*
* Make use of call->buffer to transfer data to caller's userspace
*/
484,7 → 241,7
uspace_addr = IPC_GET_ARG3(call->data);
buf_size = IPC_GET_ARG4(call->data);
 
total_bytes = sizeof(istate_t);
total_bytes = n;
 
if (buf_size > total_bytes)
to_copy = total_bytes;
499,7 → 256,7
IPC_SET_ARG2(call->data, to_copy);
 
IPC_SET_ARG3(call->data, total_bytes);
call->buffer = (void *)buffer;
call->buffer = buffer;
 
ipc_answer(&TASK->kernel_box, call);
}
510,45 → 267,19
void *uspace_data;
unative_t to_copy;
int rc;
istate_t *state;
ipl_t ipl;
 
klog_printf("debug_regs_write()");
 
uspace_data = (void *)IPC_GET_ARG3(call->data);
to_copy = IPC_GET_ARG4(call->data);
 
/* Try to change the thread's uspace_state */
 
ipl = interrupts_disable();
t = (thread_t *) IPC_GET_ARG2(call->data);
 
/* On success, this will lock t->debug_lock */
rc = _thread_op_begin(t);
if (rc != EOK) {
interrupts_restore(ipl);
 
rc = udebug_regs_write(t, call->buffer);
if (rc < 0) {
IPC_SET_RETVAL(call->data, rc);
ipc_answer(&TASK->kernel_box, call);
return;
}
 
state = t->uspace_state;
if (state == NULL) {
_thread_op_end(t);
interrupts_restore(ipl);
klog_printf("debug_regs_write() - istate not available");
 
IPC_SET_RETVAL(call->data, EBUSY);
ipc_answer(&TASK->kernel_box, call);
return;
}
 
memcpy(t->uspace_state, call->buffer, sizeof(t->uspace_state));
 
_thread_op_end(t);
interrupts_restore(ipl);
 
/* Set answer values */
 
IPC_SET_ARG1(call->data, to_copy);
555,9 → 286,10
IPC_SET_ARG2(call->data, sizeof(istate_t));
 
IPC_SET_RETVAL(call->data, 0);
free(call->buffer);
call->buffer = NULL;
 
ipc_answer(&TASK->kernel_box, call);
 
klog_printf("debug_regs_write() done");
}
 
 
564,32 → 296,24
static void udebug_receive_mem_read(call_t *call)
{
unative_t uspace_dst;
void *uspace_ptr;
unative_t uspace_src;
unsigned size;
void *buffer;
int rc;
 
klog_printf("debug_mem_read()");
uspace_dst = IPC_GET_ARG2(call->data);
uspace_ptr = (void *)IPC_GET_ARG3(call->data);
uspace_src = IPC_GET_ARG3(call->data);
size = IPC_GET_ARG4(call->data);
 
buffer = malloc(size, 0); // ???
klog_printf("debug_mem_read: src=%u, size=%u", uspace_ptr, size);
 
/* NOTE: this is not strictly from a syscall... but that shouldn't
* be a problem */
rc = copy_from_uspace(buffer, uspace_ptr, size);
if (rc) {
rc = udebug_mem_read(uspace_src, size, &buffer);
if (rc < 0) {
IPC_SET_RETVAL(call->data, rc);
ipc_answer(&TASK->kernel_box, call);
return;
}
 
klog_printf("first word: %u", *((unative_t *)buffer));
 
IPC_SET_RETVAL(call->data, 0);
/* Hack: ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
/* ARG1=dest, ARG2=size as in IPC_M_DATA_READ so that
same code in process_answer() can be used
(no way to distinguish method in answer) */
IPC_SET_ARG1(call->data, uspace_dst);
601,35 → 325,17
 
static void udebug_receive_mem_write(call_t *call)
{
void *uspace_dst;
unative_t uspace_dst;
unsigned size;
void *buffer;
int rc;
udebug_task_state_t dts;
 
klog_printf("udebug_receive_mem_write()");
 
/* Verify task state */
spinlock_lock(&TASK->lock);
dts = TASK->dt_state;
spinlock_unlock(&TASK->lock);
 
if (dts != UDEBUG_TS_ACTIVE) {
IPC_SET_RETVAL(call->data, EBUSY);
ipc_answer(&TASK->kernel_box, call);
return;
}
uspace_dst = (void *)IPC_GET_ARG3(call->data);
uspace_dst = IPC_GET_ARG3(call->data);
size = IPC_GET_ARG4(call->data);
 
buffer = call->buffer;
klog_printf("dst=%u, size=%u", uspace_dst, size);
 
/* NOTE: this is not strictly from a syscall... but that shouldn't
* be a problem */
rc = copy_to_uspace(uspace_dst, buffer, size);
if (rc) {
rc = udebug_mem_write(uspace_dst, call->buffer, size);
if (rc < 0) {
IPC_SET_RETVAL(call->data, rc);
ipc_answer(&TASK->kernel_box, call);
return;
636,7 → 342,6
}
 
IPC_SET_RETVAL(call->data, 0);
 
free(call->buffer);
call->buffer = NULL;