syscall_c.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-02-03 lizhirui first version
  9. * 2022-11-10 WangXiaoyao Add readable syscall tracing
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #ifdef RT_USING_SMART
  14. #define DBG_TAG "syscall"
  15. #define DBG_LVL DBG_WARNING
  16. #include <rtdbg.h>
  17. #include <stdint.h>
  18. #include <mmu.h>
  19. #include <page.h>
  20. #include <lwp_user_mm.h>
  21. #include "riscv_mmu.h"
  22. #include "stack.h"
  23. typedef rt_size_t (*syscallfunc_t)(rt_size_t, rt_size_t, rt_size_t, rt_size_t, rt_size_t, rt_size_t, rt_size_t);
  24. void syscall_handler(struct rt_hw_stack_frame *regs)
  25. {
  26. const char *syscall_name;
  27. int syscallid = regs->a7;
  28. if (syscallid == 0)
  29. {
  30. LOG_E("syscall id = 0!\n");
  31. while (1)
  32. ;
  33. }
  34. syscallfunc_t syscallfunc = (syscallfunc_t)lwp_get_sys_api(syscallid);
  35. if (syscallfunc == RT_NULL)
  36. {
  37. LOG_E("unsupported syscall!\n");
  38. sys_exit_group(-1);
  39. }
  40. #if DBG_LVL >= DBG_INFO
  41. syscall_name = lwp_get_syscall_name(syscallid);
  42. #endif
  43. LOG_I("[0x%lx] %s(0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx)", rt_thread_self(), syscall_name,
  44. regs->a0, regs->a1, regs->a2, regs->a3, regs->a4, regs->a5, regs->a6);
  45. regs->a0 = syscallfunc(regs->a0, regs->a1, regs->a2, regs->a3, regs->a4, regs->a5, regs->a6);
  46. regs->a7 = 0;
  47. regs->epc += 4; // skip ecall instruction
  48. LOG_I("[0x%lx] %s ret: 0x%lx", rt_thread_self(), syscall_name, regs->a0);
  49. }
  50. #endif /* RT_USING_SMART */