Skip to content

Commit

Permalink
[#36] continue ptrace implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
danielinux committed Aug 18, 2016
1 parent 34e187e commit 9d883c5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 21 deletions.
4 changes: 2 additions & 2 deletions kernel/frosted.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ void kernel_task_init(void);
#define task_space_free f_free
#define F_MALLOC_OVERHEAD 24
uint32_t mem_stats_frag(int pool);
int fmalloc_owner(void *ptr);
int fmalloc_chown(void *ptr, uint16_t pid);
int fmalloc_owner(const void *ptr);
int fmalloc_chown(const void *ptr, uint16_t pid);

/* Helper defined by sysfs.c */
int ul_to_str(unsigned long n, char *s);
Expand Down
4 changes: 2 additions & 2 deletions kernel/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ uint32_t mem_stats_frag(int pool)
}


int fmalloc_owner(void *_ptr)
int fmalloc_owner(const void *_ptr)
{
struct f_malloc_block *blk;
uint8_t *ptr = (uint8_t *)_ptr;
Expand All @@ -537,7 +537,7 @@ int fmalloc_owner(void *_ptr)
return -1;
}

int fmalloc_chown(void *ptr, uint16_t pid)
int fmalloc_chown(const void *ptr, uint16_t pid)
{
struct f_malloc_block *blk = (struct f_malloc_block *) ( ((uint8_t *)ptr) - sizeof(struct f_malloc_block));
if (block_valid(blk))
Expand Down
82 changes: 66 additions & 16 deletions kernel/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ volatile struct extra_stack_frame *tramp_extra;
volatile struct nvic_stack_frame *tramp_nvic;
volatile struct extra_stack_frame *extra_usr;


int task_ptr_valid(const void *ptr);

#ifdef CONFIG_SYSCALL_TRACE
#define STRACE_SIZE 10
Expand Down Expand Up @@ -181,6 +181,7 @@ static void * _top_stack;
#define TASK_FLAG_IN_SYSCALL 0x02
#define TASK_FLAG_SIGNALED 0x04
#define TASK_FLAG_INTR 0x40
#define TASK_FLAG_SYSCALL_STOP 0x80


struct filedesc {
Expand Down Expand Up @@ -211,6 +212,8 @@ struct __attribute__((packed)) task_block {
uint16_t ppid;
uint16_t n_files;

uint16_t tracer;

int exitval;
struct fnode *cwd;
struct task_handler *sighdlr;
Expand Down Expand Up @@ -546,6 +549,11 @@ static int catch_signal(volatile struct task *t, int signo, sigset_t orig_mask)
t->tb.sigpend |= (1 << signo);
return 0;
}

/* If process is being traced, deliver SIGTRAP to tracer */
if (t->tb.tracer > 0) {
sys_kill_hdlr(t->tb.tracer, SIGTRAP);
}

/* Reset signal, if pending, as it's going to be handled. */
t->tb.sigpend &= ~(1 << signo);
Expand Down Expand Up @@ -695,7 +703,7 @@ static void sig_trampoline(volatile struct task *t, struct task_handler *h, int
static int catch_signal(volatile struct task *t, int signo, sigset_t orig_mask) {
(void)orig_mask;
if (signo != SIGCHLD)
task_terminate(t);
task_terminate(t->tb.pid);
return 0;
}
#endif
Expand Down Expand Up @@ -726,7 +734,7 @@ int sys_sigaction_hdlr(int arg1, int arg2, int arg3, int arg4, int arg5)
return 0;
}

int sys_sigprocmask_hdlr(int how, const sigset_t *set, sigset_t *oldset)
int sys_sigprocmask_hdlr(int how, const sigset_t * set, sigset_t *oldset)
{
if (set && (
(how != SIG_SETMASK) &&
Expand Down Expand Up @@ -973,10 +981,11 @@ static void task_create_real(volatile struct task *new, struct vfs_info *vfsi, v

/* stack memory */
sp = (((uint8_t *)(&new->stack)) + SCHEDULER_STACK_SIZE - NVIC_FRAME_SIZE);

new->tb.cur_stack = &new->stack;

/* Change relocated section ownership */
fmalloc_chown(vfsi->pic, new->tb.pid);
fmalloc_chown((void *)vfsi->pic, new->tb.pid);

/* Stack frame is at the end of the stack space */
nvic_frame = (struct nvic_stack_frame *) sp;
Expand Down Expand Up @@ -1009,6 +1018,7 @@ int task_create(struct vfs_info *vfsi, void *arg, unsigned int nice)
new->tb.flags = 0;
new->tb.cwd = fno_search("/");
new->tb.vfsi = vfsi;
new->tb.tracer = 0;

/* Inherit cwd, file descriptors from parent */
if (new->tb.ppid > 1) { /* Start from parent #2 */
Expand Down Expand Up @@ -1382,7 +1392,7 @@ void task_terminate(int pid)
}
task_resume_vfork(t->tb.ppid);
}
tasklet_add(task_deliver_sigchld, (void *)t->tb.ppid);
tasklet_add(task_deliver_sigchld, ((void *)(int)t->tb.ppid));
task_preempt();
}
}
Expand Down Expand Up @@ -1503,6 +1513,7 @@ enum __ptrace_request {
PTRACE_ATTACH = 16,
PTRACE_DETACH = 17,
PTRACE_SYSCALL = 24,
PTRACE_SEIZE = 0x4206
};

int sys_ptrace_hdlr(uint32_t arg1, uint32_t arg2, uint32_t arg3, uint32_t arg4, uint32_t arg5)
Expand All @@ -1513,21 +1524,26 @@ int sys_ptrace_hdlr(uint32_t arg1, uint32_t arg2, uint32_t arg3, uint32_t arg4,
uint32_t pid = arg2;
void *addr = (void *)arg3;
void *data = (void *)arg4;
struct task *tracee = NULL;

if (addr && task_ptr_valid(addr))
return -EACCES;

if (data && task_ptr_valid(data))
return -EACCES;
/* Prepare tracee based on pid */
tracee = tasklist_get(&tasks_idling, pid);
if (!tracee)
tracee = tasklist_get(&tasks_running, pid);


switch (request) {
case PTRACE_TRACEME:
_cur_task->tb.tracer = _cur_task->tb.ppid;
break;
case PTRACE_PEEKTEXT:
break;
case PTRACE_PEEKDATA:
break;
return *((uint32_t *)addr);


case PTRACE_PEEKUSER:
break;
case PTRACE_POKETEXT:
Expand All @@ -1537,21 +1553,55 @@ int sys_ptrace_hdlr(uint32_t arg1, uint32_t arg2, uint32_t arg3, uint32_t arg4,
case PTRACE_POKEUSER:
break;
case PTRACE_CONT:
break;
if (!tracee)
return -ENOENT;
if (tracee->tb.tracer != _cur_task->tb.pid)
return -ESRCH;
task_continue(pid);
if ((int)data != 0)
task_kill(pid, (int)data);
return 0;

case PTRACE_KILL:
break;
if (!tracee)
return -ENOENT;
if (tracee->tb.tracer != _cur_task->tb.pid)
return -ESRCH;
task_kill(pid, SIGKILL);
return 0;

case PTRACE_SINGLESTEP:
break;
case PTRACE_GETREGS:
break;
case PTRACE_SETREGS:
break;


case PTRACE_ATTACH:
break;
case PTRACE_SEIZE:
if (!tracee)
return -ENOENT;
tracee->tb.tracer = _cur_task->tb.pid;
if (request == PTRACE_ATTACH)
task_kill(pid, SIGSTOP);
return 0;

case PTRACE_DETACH:
break;
if (!tracee)
return -ENOENT;
if (tracee->tb.tracer != _cur_task->tb.pid)
return -ESRCH;
tracee->tb.tracer = 0;
task_kill(tracee->tb.pid, SIGCONT);
return 0;
case PTRACE_SYSCALL:
break;
if (!tracee)
return -ENOENT;
if (tracee->tb.tracer != _cur_task->tb.pid)
return -ESRCH;
tracee->tb.flags |= TASK_FLAG_SYSCALL_STOP;
return 0;
}
return -1;
}
Expand Down Expand Up @@ -1656,15 +1706,15 @@ int task_segfault(uint32_t address, uint32_t instruction, int flags)
return 0;
}

int task_ptr_valid(void *ptr)
int task_ptr_valid(const void *ptr)
{
struct task *t;
uint8_t *stack_start = (uint8_t *)_cur_task->tb.cur_stack;
uint8_t *stack_end = stack_start + SCHEDULER_STACK_SIZE;

if (_cur_task->tb.pid == 0)
return 0; /* Kernel mode */
if ( (ptr >= stack_start) && (ptr < stack_end) )
if ( ((uint8_t *)ptr >= stack_start) && ((uint8_t *)ptr < stack_end) )
return 0; /* In the process own's stack */
if (fmalloc_owner(ptr) == _cur_task->tb.pid)
return 0; /* In the process own's heap */
Expand Down
2 changes: 1 addition & 1 deletion kernel/unicore-mx
Submodule unicore-mx updated 52 files
+1 −0 Makefile
+1 −0 README.md
+3 −0 include/unicore-mx/dispatch/nvic.h
+25 −0 include/unicore-mx/lm3s/flash.h
+8 −0 include/unicore-mx/lm3s/memorymap.h
+92 −0 include/unicore-mx/nrf/51/clock.h
+84 −0 include/unicore-mx/nrf/51/ficr.h
+187 −0 include/unicore-mx/nrf/51/gpio.h
+33 −0 include/unicore-mx/nrf/51/irq.json
+102 −0 include/unicore-mx/nrf/51/memorymap.h
+93 −0 include/unicore-mx/nrf/51/power.h
+145 −0 include/unicore-mx/nrf/51/ppi.h
+312 −0 include/unicore-mx/nrf/51/radio.h
+141 −0 include/unicore-mx/nrf/51/rtc.h
+208 −0 include/unicore-mx/nrf/51/timer.h
+146 −0 include/unicore-mx/nrf/51/uart.h
+42 −0 include/unicore-mx/nrf/51/uicr.h
+27 −0 include/unicore-mx/nrf/clock.h
+27 −0 include/unicore-mx/nrf/ficr.h
+28 −0 include/unicore-mx/nrf/gpio.h
+28 −0 include/unicore-mx/nrf/memorymap.h
+27 −0 include/unicore-mx/nrf/power.h
+27 −0 include/unicore-mx/nrf/ppi.h
+27 −0 include/unicore-mx/nrf/radio.h
+27 −0 include/unicore-mx/nrf/rtc.h
+28 −0 include/unicore-mx/nrf/timer.h
+27 −0 include/unicore-mx/nrf/uart.h
+27 −0 include/unicore-mx/nrf/uicr.h
+3 −0 lib/dispatch/vector_nvic.c
+1 −1 lib/efm32/lg/Makefile
+1 −1 lib/lm3s/Makefile
+45 −0 lib/lm3s/flash.c
+1 −1 lib/lm4f/Makefile
+37 −0 lib/nrf/51/Makefile
+75 −0 lib/nrf/51/clock.c
+148 −0 lib/nrf/51/gpio.c
+105 −0 lib/nrf/51/libucmx_nrf51.ld
+138 −0 lib/nrf/51/ppi.c
+307 −0 lib/nrf/51/radio.c
+128 −0 lib/nrf/51/rtc.c
+140 −0 lib/nrf/51/timer.c
+129 −0 lib/nrf/51/uart.c
+1 −1 lib/stm32/f0/Makefile
+1 −1 lib/stm32/f1/Makefile
+1 −1 lib/stm32/f2/Makefile
+1 −1 lib/stm32/f3/Makefile
+1 −1 lib/stm32/f4/Makefile
+1 −1 lib/stm32/f7/Makefile
+1 −1 lib/stm32/l0/Makefile
+1 −1 lib/stm32/l1/Makefile
+0 −17 lib/usbd/backend/usbd_stm32_fsdev.c
+0 −0 lib/usbd/usbd_ep0.c

0 comments on commit 9d883c5

Please sign in to comment.