signal.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  4. * Chen Liqin <liqin.chen@sunplusct.com>
  5. * Lennox Wu <lennox.wu@sunplusct.com>
  6. * Copyright (C) 2012 Regents of the University of California
  7. */
  8. #include <linux/compat.h>
  9. #include <linux/signal.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/syscalls.h>
  12. #include <linux/resume_user_mode.h>
  13. #include <linux/linkage.h>
  14. #include <linux/entry-common.h>
  15. #include <asm/ucontext.h>
  16. #include <asm/vdso.h>
  17. #include <asm/signal.h>
  18. #include <asm/signal32.h>
  19. #include <asm/switch_to.h>
  20. #include <asm/vector.h>
  21. #include <asm/csr.h>
  22. #include <asm/cacheflush.h>
  23. unsigned long signal_minsigstksz __ro_after_init;
  24. extern u32 __user_rt_sigreturn[2];
  25. static size_t riscv_v_sc_size __ro_after_init;
  26. #define DEBUG_SIG 0
  27. struct rt_sigframe {
  28. struct siginfo info;
  29. struct ucontext uc;
  30. #ifndef CONFIG_MMU
  31. u32 sigreturn_code[2];
  32. #endif
  33. };
  34. #ifdef CONFIG_FPU
  35. static long restore_fp_state(struct pt_regs *regs,
  36. union __riscv_fp_state __user *sc_fpregs)
  37. {
  38. long err;
  39. struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
  40. err = __copy_from_user(&current->thread.fstate, state, sizeof(*state));
  41. if (unlikely(err))
  42. return err;
  43. fstate_restore(current, regs);
  44. return 0;
  45. }
  46. static long save_fp_state(struct pt_regs *regs,
  47. union __riscv_fp_state __user *sc_fpregs)
  48. {
  49. long err;
  50. struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
  51. fstate_save(current, regs);
  52. err = __copy_to_user(state, &current->thread.fstate, sizeof(*state));
  53. return err;
  54. }
  55. #else
  56. #define save_fp_state(task, regs) (0)
  57. #define restore_fp_state(task, regs) (0)
  58. #endif
  59. #ifdef CONFIG_RISCV_ISA_V
  60. static long save_v_state(struct pt_regs *regs, void __user **sc_vec)
  61. {
  62. struct __riscv_ctx_hdr __user *hdr;
  63. struct __sc_riscv_v_state __user *state;
  64. void __user *datap;
  65. long err;
  66. hdr = *sc_vec;
  67. /* Place state to the user's signal context space after the hdr */
  68. state = (struct __sc_riscv_v_state __user *)(hdr + 1);
  69. /* Point datap right after the end of __sc_riscv_v_state */
  70. datap = state + 1;
  71. /* datap is designed to be 16 byte aligned for better performance */
  72. WARN_ON(!IS_ALIGNED((unsigned long)datap, 16));
  73. get_cpu_vector_context();
  74. riscv_v_vstate_save(&current->thread.vstate, regs);
  75. put_cpu_vector_context();
  76. /* Copy everything of vstate but datap. */
  77. err = __copy_to_user(&state->v_state, &current->thread.vstate,
  78. offsetof(struct __riscv_v_ext_state, datap));
  79. /* Copy the pointer datap itself. */
  80. err |= __put_user((__force void *)datap, &state->v_state.datap);
  81. /* Copy the whole vector content to user space datap. */
  82. err |= __copy_to_user(datap, current->thread.vstate.datap, riscv_v_vsize);
  83. /* Copy magic to the user space after saving all vector conetext */
  84. err |= __put_user(RISCV_V_MAGIC, &hdr->magic);
  85. err |= __put_user(riscv_v_sc_size, &hdr->size);
  86. if (unlikely(err))
  87. return err;
  88. /* Only progress the sv_vec if everything has done successfully */
  89. *sc_vec += riscv_v_sc_size;
  90. return 0;
  91. }
  92. /*
  93. * Restore Vector extension context from the user's signal frame. This function
  94. * assumes a valid extension header. So magic and size checking must be done by
  95. * the caller.
  96. */
  97. static long __restore_v_state(struct pt_regs *regs, void __user *sc_vec)
  98. {
  99. long err;
  100. struct __sc_riscv_v_state __user *state = sc_vec;
  101. void __user *datap;
  102. /*
  103. * Mark the vstate as clean prior performing the actual copy,
  104. * to avoid getting the vstate incorrectly clobbered by the
  105. * discarded vector state.
  106. */
  107. riscv_v_vstate_set_restore(current, regs);
  108. /* Copy everything of __sc_riscv_v_state except datap. */
  109. err = __copy_from_user(&current->thread.vstate, &state->v_state,
  110. offsetof(struct __riscv_v_ext_state, datap));
  111. if (unlikely(err))
  112. return err;
  113. /* Copy the pointer datap itself. */
  114. err = __get_user(datap, &state->v_state.datap);
  115. if (unlikely(err))
  116. return err;
  117. /*
  118. * Copy the whole vector content from user space datap. Use
  119. * copy_from_user to prevent information leak.
  120. */
  121. return copy_from_user(current->thread.vstate.datap, datap, riscv_v_vsize);
  122. }
  123. #else
  124. #define save_v_state(task, regs) (0)
  125. #define __restore_v_state(task, regs) (0)
  126. #endif
  127. static long restore_sigcontext(struct pt_regs *regs,
  128. struct sigcontext __user *sc)
  129. {
  130. void __user *sc_ext_ptr = &sc->sc_extdesc.hdr;
  131. __u32 rsvd;
  132. long err;
  133. /* sc_regs is structured the same as the start of pt_regs */
  134. err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
  135. if (unlikely(err))
  136. return err;
  137. /* Restore the floating-point state. */
  138. if (has_fpu()) {
  139. err = restore_fp_state(regs, &sc->sc_fpregs);
  140. if (unlikely(err))
  141. return err;
  142. }
  143. /* Check the reserved word before extensions parsing */
  144. err = __get_user(rsvd, &sc->sc_extdesc.reserved);
  145. if (unlikely(err))
  146. return err;
  147. if (unlikely(rsvd))
  148. return -EINVAL;
  149. while (!err) {
  150. __u32 magic, size;
  151. struct __riscv_ctx_hdr __user *head = sc_ext_ptr;
  152. err |= __get_user(magic, &head->magic);
  153. err |= __get_user(size, &head->size);
  154. if (unlikely(err))
  155. return err;
  156. sc_ext_ptr += sizeof(*head);
  157. switch (magic) {
  158. case END_MAGIC:
  159. if (size != END_HDR_SIZE)
  160. return -EINVAL;
  161. return 0;
  162. case RISCV_V_MAGIC:
  163. if (!has_vector() || !riscv_v_vstate_query(regs) ||
  164. size != riscv_v_sc_size)
  165. return -EINVAL;
  166. err = __restore_v_state(regs, sc_ext_ptr);
  167. break;
  168. default:
  169. return -EINVAL;
  170. }
  171. sc_ext_ptr = (void __user *)head + size;
  172. }
  173. return err;
  174. }
  175. static size_t get_rt_frame_size(bool cal_all)
  176. {
  177. struct rt_sigframe __user *frame;
  178. size_t frame_size;
  179. size_t total_context_size = 0;
  180. frame_size = sizeof(*frame);
  181. if (has_vector()) {
  182. if (cal_all || riscv_v_vstate_query(task_pt_regs(current)))
  183. total_context_size += riscv_v_sc_size;
  184. }
  185. /*
  186. * Preserved a __riscv_ctx_hdr for END signal context header if an
  187. * extension uses __riscv_extra_ext_header
  188. */
  189. if (total_context_size)
  190. total_context_size += sizeof(struct __riscv_ctx_hdr);
  191. frame_size += total_context_size;
  192. frame_size = round_up(frame_size, 16);
  193. return frame_size;
  194. }
  195. SYSCALL_DEFINE0(rt_sigreturn)
  196. {
  197. struct pt_regs *regs = current_pt_regs();
  198. struct rt_sigframe __user *frame;
  199. struct task_struct *task;
  200. sigset_t set;
  201. size_t frame_size = get_rt_frame_size(false);
  202. /* Always make any pending restarted system calls return -EINTR */
  203. current->restart_block.fn = do_no_restart_syscall;
  204. frame = (struct rt_sigframe __user *)regs->sp;
  205. if (!access_ok(frame, frame_size))
  206. goto badframe;
  207. if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
  208. goto badframe;
  209. set_current_blocked(&set);
  210. if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
  211. goto badframe;
  212. if (restore_altstack(&frame->uc.uc_stack))
  213. goto badframe;
  214. regs->cause = -1UL;
  215. return regs->a0;
  216. badframe:
  217. task = current;
  218. if (show_unhandled_signals) {
  219. pr_info_ratelimited(
  220. "%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n",
  221. task->comm, task_pid_nr(task), __func__,
  222. frame, (void *)regs->epc, (void *)regs->sp);
  223. }
  224. force_sig(SIGSEGV);
  225. return 0;
  226. }
  227. static long setup_sigcontext(struct rt_sigframe __user *frame,
  228. struct pt_regs *regs)
  229. {
  230. struct sigcontext __user *sc = &frame->uc.uc_mcontext;
  231. struct __riscv_ctx_hdr __user *sc_ext_ptr = &sc->sc_extdesc.hdr;
  232. long err;
  233. /* sc_regs is structured the same as the start of pt_regs */
  234. err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
  235. /* Save the floating-point state. */
  236. if (has_fpu())
  237. err |= save_fp_state(regs, &sc->sc_fpregs);
  238. /* Save the vector state. */
  239. if (has_vector() && riscv_v_vstate_query(regs))
  240. err |= save_v_state(regs, (void __user **)&sc_ext_ptr);
  241. /* Write zero to fp-reserved space and check it on restore_sigcontext */
  242. err |= __put_user(0, &sc->sc_extdesc.reserved);
  243. /* And put END __riscv_ctx_hdr at the end. */
  244. err |= __put_user(END_MAGIC, &sc_ext_ptr->magic);
  245. err |= __put_user(END_HDR_SIZE, &sc_ext_ptr->size);
  246. return err;
  247. }
  248. static inline void __user *get_sigframe(struct ksignal *ksig,
  249. struct pt_regs *regs, size_t framesize)
  250. {
  251. unsigned long sp;
  252. /* Default to using normal stack */
  253. sp = regs->sp;
  254. /*
  255. * If we are on the alternate signal stack and would overflow it, don't.
  256. * Return an always-bogus address instead so we will die with SIGSEGV.
  257. */
  258. if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
  259. return (void __user __force *)(-1UL);
  260. /* This is the X/Open sanctioned signal stack switching. */
  261. sp = sigsp(sp, ksig) - framesize;
  262. /* Align the stack frame. */
  263. sp &= ~0xfUL;
  264. return (void __user *)sp;
  265. }
  266. static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
  267. struct pt_regs *regs)
  268. {
  269. struct rt_sigframe __user *frame;
  270. long err = 0;
  271. unsigned long __maybe_unused addr;
  272. size_t frame_size = get_rt_frame_size(false);
  273. frame = get_sigframe(ksig, regs, frame_size);
  274. if (!access_ok(frame, frame_size))
  275. return -EFAULT;
  276. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  277. /* Create the ucontext. */
  278. err |= __put_user(0, &frame->uc.uc_flags);
  279. err |= __put_user(NULL, &frame->uc.uc_link);
  280. err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
  281. err |= setup_sigcontext(frame, regs);
  282. err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
  283. if (err)
  284. return -EFAULT;
  285. /* Set up to return from userspace. */
  286. #ifdef CONFIG_MMU
  287. regs->ra = (unsigned long)VDSO_SYMBOL(
  288. current->mm->context.vdso, rt_sigreturn);
  289. #else
  290. /*
  291. * For the nommu case we don't have a VDSO. Instead we push two
  292. * instructions to call the rt_sigreturn syscall onto the user stack.
  293. */
  294. if (copy_to_user(&frame->sigreturn_code, __user_rt_sigreturn,
  295. sizeof(frame->sigreturn_code)))
  296. return -EFAULT;
  297. addr = (unsigned long)&frame->sigreturn_code;
  298. /* Make sure the two instructions are pushed to icache. */
  299. flush_icache_range(addr, addr + sizeof(frame->sigreturn_code));
  300. regs->ra = addr;
  301. #endif /* CONFIG_MMU */
  302. /*
  303. * Set up registers for signal handler.
  304. * Registers that we don't modify keep the value they had from
  305. * user-space at the time we took the signal.
  306. * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
  307. * since some things rely on this (e.g. glibc's debug/segfault.c).
  308. */
  309. regs->epc = (unsigned long)ksig->ka.sa.sa_handler;
  310. regs->sp = (unsigned long)frame;
  311. regs->a0 = ksig->sig; /* a0: signal number */
  312. regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */
  313. regs->a2 = (unsigned long)(&frame->uc); /* a2: ucontext pointer */
  314. #if DEBUG_SIG
  315. pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n",
  316. current->comm, task_pid_nr(current), ksig->sig,
  317. (void *)regs->epc, (void *)regs->ra, frame);
  318. #endif
  319. return 0;
  320. }
  321. static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  322. {
  323. sigset_t *oldset = sigmask_to_save();
  324. int ret;
  325. rseq_signal_deliver(ksig, regs);
  326. /* Set up the stack frame */
  327. if (is_compat_task())
  328. ret = compat_setup_rt_frame(ksig, oldset, regs);
  329. else
  330. ret = setup_rt_frame(ksig, oldset, regs);
  331. signal_setup_done(ret, ksig, 0);
  332. }
  333. void arch_do_signal_or_restart(struct pt_regs *regs)
  334. {
  335. unsigned long continue_addr = 0, restart_addr = 0;
  336. int retval = 0;
  337. struct ksignal ksig;
  338. bool syscall = (regs->cause == EXC_SYSCALL);
  339. /* If we were from a system call, check for system call restarting */
  340. if (syscall) {
  341. continue_addr = regs->epc;
  342. restart_addr = continue_addr - 4;
  343. retval = regs->a0;
  344. /* Avoid additional syscall restarting via ret_from_exception */
  345. regs->cause = -1UL;
  346. /*
  347. * Prepare for system call restart. We do this here so that a
  348. * debugger will see the already changed PC.
  349. */
  350. switch (retval) {
  351. case -ERESTARTNOHAND:
  352. case -ERESTARTSYS:
  353. case -ERESTARTNOINTR:
  354. case -ERESTART_RESTARTBLOCK:
  355. regs->a0 = regs->orig_a0;
  356. regs->epc = restart_addr;
  357. break;
  358. }
  359. }
  360. /*
  361. * Get the signal to deliver. When running under ptrace, at this point
  362. * the debugger may change all of our registers.
  363. */
  364. if (get_signal(&ksig)) {
  365. /*
  366. * Depending on the signal settings, we may need to revert the
  367. * decision to restart the system call, but skip this if a
  368. * debugger has chosen to restart at a different PC.
  369. */
  370. if (regs->epc == restart_addr &&
  371. (retval == -ERESTARTNOHAND ||
  372. retval == -ERESTART_RESTARTBLOCK ||
  373. (retval == -ERESTARTSYS &&
  374. !(ksig.ka.sa.sa_flags & SA_RESTART)))) {
  375. regs->a0 = -EINTR;
  376. regs->epc = continue_addr;
  377. }
  378. /* Actually deliver the signal */
  379. handle_signal(&ksig, regs);
  380. return;
  381. }
  382. /*
  383. * Handle restarting a different system call. As above, if a debugger
  384. * has chosen to restart at a different PC, ignore the restart.
  385. */
  386. if (syscall && regs->epc == restart_addr && retval == -ERESTART_RESTARTBLOCK)
  387. regs->a7 = __NR_restart_syscall;
  388. /*
  389. * If there is no signal to deliver, we just put the saved
  390. * sigmask back.
  391. */
  392. restore_saved_sigmask();
  393. }
  394. void init_rt_signal_env(void);
  395. void __init init_rt_signal_env(void)
  396. {
  397. riscv_v_sc_size = sizeof(struct __riscv_ctx_hdr) +
  398. sizeof(struct __sc_riscv_v_state) + riscv_v_vsize;
  399. /*
  400. * Determine the stack space required for guaranteed signal delivery.
  401. * The signal_minsigstksz will be populated into the AT_MINSIGSTKSZ entry
  402. * in the auxiliary array at process startup.
  403. */
  404. signal_minsigstksz = get_rt_frame_size(true);
  405. }
  406. #ifdef CONFIG_DYNAMIC_SIGFRAME
  407. bool sigaltstack_size_valid(size_t ss_size)
  408. {
  409. return ss_size > get_rt_frame_size(false);
  410. }
  411. #endif /* CONFIG_DYNAMIC_SIGFRAME */