signal.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Based on arch/arm/kernel/signal.c
  4. *
  5. * Copyright (C) 1995-2009 Russell King
  6. * Copyright (C) 2012 ARM Ltd.
  7. */
  8. #include <linux/cache.h>
  9. #include <linux/compat.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <linux/signal.h>
  13. #include <linux/freezer.h>
  14. #include <linux/stddef.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/sizes.h>
  17. #include <linux/string.h>
  18. #include <linux/ratelimit.h>
  19. #include <linux/rseq.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/pkeys.h>
  22. #include <asm/daifflags.h>
  23. #include <asm/debug-monitors.h>
  24. #include <asm/elf.h>
  25. #include <asm/exception.h>
  26. #include <asm/cacheflush.h>
  27. #include <asm/ucontext.h>
  28. #include <asm/unistd.h>
  29. #include <asm/fpsimd.h>
  30. #include <asm/ptrace.h>
  31. #include <asm/syscall.h>
  32. #include <asm/signal32.h>
  33. #include <asm/traps.h>
  34. #include <asm/vdso.h>
  35. /*
  36. * Do a signal return; undo the signal stack. These are aligned to 128-bit.
  37. */
  38. struct rt_sigframe {
  39. struct siginfo info;
  40. struct ucontext uc;
  41. };
  42. struct frame_record {
  43. u64 fp;
  44. u64 lr;
  45. };
  46. struct rt_sigframe_user_layout {
  47. struct rt_sigframe __user *sigframe;
  48. struct frame_record __user *next_frame;
  49. unsigned long size; /* size of allocated sigframe data */
  50. unsigned long limit; /* largest allowed size */
  51. unsigned long fpsimd_offset;
  52. unsigned long esr_offset;
  53. unsigned long sve_offset;
  54. unsigned long tpidr2_offset;
  55. unsigned long za_offset;
  56. unsigned long zt_offset;
  57. unsigned long fpmr_offset;
  58. unsigned long poe_offset;
  59. unsigned long extra_offset;
  60. unsigned long end_offset;
  61. };
  62. /*
  63. * Holds any EL0-controlled state that influences unprivileged memory accesses.
  64. * This includes both accesses done in userspace and uaccess done in the kernel.
  65. *
  66. * This state needs to be carefully managed to ensure that it doesn't cause
  67. * uaccess to fail when setting up the signal frame, and the signal handler
  68. * itself also expects a well-defined state when entered.
  69. */
  70. struct user_access_state {
  71. u64 por_el0;
  72. };
  73. #define BASE_SIGFRAME_SIZE round_up(sizeof(struct rt_sigframe), 16)
  74. #define TERMINATOR_SIZE round_up(sizeof(struct _aarch64_ctx), 16)
  75. #define EXTRA_CONTEXT_SIZE round_up(sizeof(struct extra_context), 16)
  76. /*
  77. * Save the user access state into ua_state and reset it to disable any
  78. * restrictions.
  79. */
  80. static void save_reset_user_access_state(struct user_access_state *ua_state)
  81. {
  82. if (system_supports_poe()) {
  83. u64 por_enable_all = 0;
  84. for (int pkey = 0; pkey < arch_max_pkey(); pkey++)
  85. por_enable_all |= POE_RXW << (pkey * POR_BITS_PER_PKEY);
  86. ua_state->por_el0 = read_sysreg_s(SYS_POR_EL0);
  87. write_sysreg_s(por_enable_all, SYS_POR_EL0);
  88. /* Ensure that any subsequent uaccess observes the updated value */
  89. isb();
  90. }
  91. }
  92. /*
  93. * Set the user access state for invoking the signal handler.
  94. *
  95. * No uaccess should be done after that function is called.
  96. */
  97. static void set_handler_user_access_state(void)
  98. {
  99. if (system_supports_poe())
  100. write_sysreg_s(POR_EL0_INIT, SYS_POR_EL0);
  101. }
  102. /*
  103. * Restore the user access state to the values saved in ua_state.
  104. *
  105. * No uaccess should be done after that function is called.
  106. */
  107. static void restore_user_access_state(const struct user_access_state *ua_state)
  108. {
  109. if (system_supports_poe())
  110. write_sysreg_s(ua_state->por_el0, SYS_POR_EL0);
  111. }
  112. static void init_user_layout(struct rt_sigframe_user_layout *user)
  113. {
  114. const size_t reserved_size =
  115. sizeof(user->sigframe->uc.uc_mcontext.__reserved);
  116. memset(user, 0, sizeof(*user));
  117. user->size = offsetof(struct rt_sigframe, uc.uc_mcontext.__reserved);
  118. user->limit = user->size + reserved_size;
  119. user->limit -= TERMINATOR_SIZE;
  120. user->limit -= EXTRA_CONTEXT_SIZE;
  121. /* Reserve space for extension and terminator ^ */
  122. }
  123. static size_t sigframe_size(struct rt_sigframe_user_layout const *user)
  124. {
  125. return round_up(max(user->size, sizeof(struct rt_sigframe)), 16);
  126. }
  127. /*
  128. * Sanity limit on the approximate maximum size of signal frame we'll
  129. * try to generate. Stack alignment padding and the frame record are
  130. * not taken into account. This limit is not a guarantee and is
  131. * NOT ABI.
  132. */
  133. #define SIGFRAME_MAXSZ SZ_256K
  134. static int __sigframe_alloc(struct rt_sigframe_user_layout *user,
  135. unsigned long *offset, size_t size, bool extend)
  136. {
  137. size_t padded_size = round_up(size, 16);
  138. if (padded_size > user->limit - user->size &&
  139. !user->extra_offset &&
  140. extend) {
  141. int ret;
  142. user->limit += EXTRA_CONTEXT_SIZE;
  143. ret = __sigframe_alloc(user, &user->extra_offset,
  144. sizeof(struct extra_context), false);
  145. if (ret) {
  146. user->limit -= EXTRA_CONTEXT_SIZE;
  147. return ret;
  148. }
  149. /* Reserve space for the __reserved[] terminator */
  150. user->size += TERMINATOR_SIZE;
  151. /*
  152. * Allow expansion up to SIGFRAME_MAXSZ, ensuring space for
  153. * the terminator:
  154. */
  155. user->limit = SIGFRAME_MAXSZ - TERMINATOR_SIZE;
  156. }
  157. /* Still not enough space? Bad luck! */
  158. if (padded_size > user->limit - user->size)
  159. return -ENOMEM;
  160. *offset = user->size;
  161. user->size += padded_size;
  162. return 0;
  163. }
  164. /*
  165. * Allocate space for an optional record of <size> bytes in the user
  166. * signal frame. The offset from the signal frame base address to the
  167. * allocated block is assigned to *offset.
  168. */
  169. static int sigframe_alloc(struct rt_sigframe_user_layout *user,
  170. unsigned long *offset, size_t size)
  171. {
  172. return __sigframe_alloc(user, offset, size, true);
  173. }
  174. /* Allocate the null terminator record and prevent further allocations */
  175. static int sigframe_alloc_end(struct rt_sigframe_user_layout *user)
  176. {
  177. int ret;
  178. /* Un-reserve the space reserved for the terminator: */
  179. user->limit += TERMINATOR_SIZE;
  180. ret = sigframe_alloc(user, &user->end_offset,
  181. sizeof(struct _aarch64_ctx));
  182. if (ret)
  183. return ret;
  184. /* Prevent further allocation: */
  185. user->limit = user->size;
  186. return 0;
  187. }
  188. static void __user *apply_user_offset(
  189. struct rt_sigframe_user_layout const *user, unsigned long offset)
  190. {
  191. char __user *base = (char __user *)user->sigframe;
  192. return base + offset;
  193. }
  194. struct user_ctxs {
  195. struct fpsimd_context __user *fpsimd;
  196. u32 fpsimd_size;
  197. struct sve_context __user *sve;
  198. u32 sve_size;
  199. struct tpidr2_context __user *tpidr2;
  200. u32 tpidr2_size;
  201. struct za_context __user *za;
  202. u32 za_size;
  203. struct zt_context __user *zt;
  204. u32 zt_size;
  205. struct fpmr_context __user *fpmr;
  206. u32 fpmr_size;
  207. struct poe_context __user *poe;
  208. u32 poe_size;
  209. };
  210. static int preserve_fpsimd_context(struct fpsimd_context __user *ctx)
  211. {
  212. struct user_fpsimd_state const *fpsimd =
  213. &current->thread.uw.fpsimd_state;
  214. int err;
  215. /* copy the FP and status/control registers */
  216. err = __copy_to_user(ctx->vregs, fpsimd->vregs, sizeof(fpsimd->vregs));
  217. __put_user_error(fpsimd->fpsr, &ctx->fpsr, err);
  218. __put_user_error(fpsimd->fpcr, &ctx->fpcr, err);
  219. /* copy the magic/size information */
  220. __put_user_error(FPSIMD_MAGIC, &ctx->head.magic, err);
  221. __put_user_error(sizeof(struct fpsimd_context), &ctx->head.size, err);
  222. return err ? -EFAULT : 0;
  223. }
  224. static int restore_fpsimd_context(struct user_ctxs *user)
  225. {
  226. struct user_fpsimd_state fpsimd;
  227. int err = 0;
  228. /* check the size information */
  229. if (user->fpsimd_size != sizeof(struct fpsimd_context))
  230. return -EINVAL;
  231. /* copy the FP and status/control registers */
  232. err = __copy_from_user(fpsimd.vregs, &(user->fpsimd->vregs),
  233. sizeof(fpsimd.vregs));
  234. __get_user_error(fpsimd.fpsr, &(user->fpsimd->fpsr), err);
  235. __get_user_error(fpsimd.fpcr, &(user->fpsimd->fpcr), err);
  236. clear_thread_flag(TIF_SVE);
  237. current->thread.fp_type = FP_STATE_FPSIMD;
  238. /* load the hardware registers from the fpsimd_state structure */
  239. if (!err)
  240. fpsimd_update_current_state(&fpsimd);
  241. return err ? -EFAULT : 0;
  242. }
  243. static int preserve_fpmr_context(struct fpmr_context __user *ctx)
  244. {
  245. int err = 0;
  246. current->thread.uw.fpmr = read_sysreg_s(SYS_FPMR);
  247. __put_user_error(FPMR_MAGIC, &ctx->head.magic, err);
  248. __put_user_error(sizeof(*ctx), &ctx->head.size, err);
  249. __put_user_error(current->thread.uw.fpmr, &ctx->fpmr, err);
  250. return err;
  251. }
  252. static int restore_fpmr_context(struct user_ctxs *user)
  253. {
  254. u64 fpmr;
  255. int err = 0;
  256. if (user->fpmr_size != sizeof(*user->fpmr))
  257. return -EINVAL;
  258. __get_user_error(fpmr, &user->fpmr->fpmr, err);
  259. if (!err)
  260. write_sysreg_s(fpmr, SYS_FPMR);
  261. return err;
  262. }
  263. static int preserve_poe_context(struct poe_context __user *ctx,
  264. const struct user_access_state *ua_state)
  265. {
  266. int err = 0;
  267. __put_user_error(POE_MAGIC, &ctx->head.magic, err);
  268. __put_user_error(sizeof(*ctx), &ctx->head.size, err);
  269. __put_user_error(ua_state->por_el0, &ctx->por_el0, err);
  270. return err;
  271. }
  272. static int restore_poe_context(struct user_ctxs *user,
  273. struct user_access_state *ua_state)
  274. {
  275. u64 por_el0;
  276. int err = 0;
  277. if (user->poe_size != sizeof(*user->poe))
  278. return -EINVAL;
  279. __get_user_error(por_el0, &(user->poe->por_el0), err);
  280. if (!err)
  281. ua_state->por_el0 = por_el0;
  282. return err;
  283. }
  284. #ifdef CONFIG_ARM64_SVE
  285. static int preserve_sve_context(struct sve_context __user *ctx)
  286. {
  287. int err = 0;
  288. u16 reserved[ARRAY_SIZE(ctx->__reserved)];
  289. u16 flags = 0;
  290. unsigned int vl = task_get_sve_vl(current);
  291. unsigned int vq = 0;
  292. if (thread_sm_enabled(&current->thread)) {
  293. vl = task_get_sme_vl(current);
  294. vq = sve_vq_from_vl(vl);
  295. flags |= SVE_SIG_FLAG_SM;
  296. } else if (current->thread.fp_type == FP_STATE_SVE) {
  297. vq = sve_vq_from_vl(vl);
  298. }
  299. memset(reserved, 0, sizeof(reserved));
  300. __put_user_error(SVE_MAGIC, &ctx->head.magic, err);
  301. __put_user_error(round_up(SVE_SIG_CONTEXT_SIZE(vq), 16),
  302. &ctx->head.size, err);
  303. __put_user_error(vl, &ctx->vl, err);
  304. __put_user_error(flags, &ctx->flags, err);
  305. BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved));
  306. err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved));
  307. if (vq) {
  308. /*
  309. * This assumes that the SVE state has already been saved to
  310. * the task struct by calling the function
  311. * fpsimd_signal_preserve_current_state().
  312. */
  313. err |= __copy_to_user((char __user *)ctx + SVE_SIG_REGS_OFFSET,
  314. current->thread.sve_state,
  315. SVE_SIG_REGS_SIZE(vq));
  316. }
  317. return err ? -EFAULT : 0;
  318. }
  319. static int restore_sve_fpsimd_context(struct user_ctxs *user)
  320. {
  321. int err = 0;
  322. unsigned int vl, vq;
  323. struct user_fpsimd_state fpsimd;
  324. u16 user_vl, flags;
  325. if (user->sve_size < sizeof(*user->sve))
  326. return -EINVAL;
  327. __get_user_error(user_vl, &(user->sve->vl), err);
  328. __get_user_error(flags, &(user->sve->flags), err);
  329. if (err)
  330. return err;
  331. if (flags & SVE_SIG_FLAG_SM) {
  332. if (!system_supports_sme())
  333. return -EINVAL;
  334. vl = task_get_sme_vl(current);
  335. } else {
  336. /*
  337. * A SME only system use SVE for streaming mode so can
  338. * have a SVE formatted context with a zero VL and no
  339. * payload data.
  340. */
  341. if (!system_supports_sve() && !system_supports_sme())
  342. return -EINVAL;
  343. vl = task_get_sve_vl(current);
  344. }
  345. if (user_vl != vl)
  346. return -EINVAL;
  347. if (user->sve_size == sizeof(*user->sve)) {
  348. clear_thread_flag(TIF_SVE);
  349. current->thread.svcr &= ~SVCR_SM_MASK;
  350. current->thread.fp_type = FP_STATE_FPSIMD;
  351. goto fpsimd_only;
  352. }
  353. vq = sve_vq_from_vl(vl);
  354. if (user->sve_size < SVE_SIG_CONTEXT_SIZE(vq))
  355. return -EINVAL;
  356. /*
  357. * Careful: we are about __copy_from_user() directly into
  358. * thread.sve_state with preemption enabled, so protection is
  359. * needed to prevent a racing context switch from writing stale
  360. * registers back over the new data.
  361. */
  362. fpsimd_flush_task_state(current);
  363. /* From now, fpsimd_thread_switch() won't touch thread.sve_state */
  364. sve_alloc(current, true);
  365. if (!current->thread.sve_state) {
  366. clear_thread_flag(TIF_SVE);
  367. return -ENOMEM;
  368. }
  369. err = __copy_from_user(current->thread.sve_state,
  370. (char __user const *)user->sve +
  371. SVE_SIG_REGS_OFFSET,
  372. SVE_SIG_REGS_SIZE(vq));
  373. if (err)
  374. return -EFAULT;
  375. if (flags & SVE_SIG_FLAG_SM)
  376. current->thread.svcr |= SVCR_SM_MASK;
  377. else
  378. set_thread_flag(TIF_SVE);
  379. current->thread.fp_type = FP_STATE_SVE;
  380. fpsimd_only:
  381. /* copy the FP and status/control registers */
  382. /* restore_sigframe() already checked that user->fpsimd != NULL. */
  383. err = __copy_from_user(fpsimd.vregs, user->fpsimd->vregs,
  384. sizeof(fpsimd.vregs));
  385. __get_user_error(fpsimd.fpsr, &user->fpsimd->fpsr, err);
  386. __get_user_error(fpsimd.fpcr, &user->fpsimd->fpcr, err);
  387. /* load the hardware registers from the fpsimd_state structure */
  388. if (!err)
  389. fpsimd_update_current_state(&fpsimd);
  390. return err ? -EFAULT : 0;
  391. }
  392. #else /* ! CONFIG_ARM64_SVE */
  393. static int restore_sve_fpsimd_context(struct user_ctxs *user)
  394. {
  395. WARN_ON_ONCE(1);
  396. return -EINVAL;
  397. }
  398. /* Turn any non-optimised out attempts to use this into a link error: */
  399. extern int preserve_sve_context(void __user *ctx);
  400. #endif /* ! CONFIG_ARM64_SVE */
  401. #ifdef CONFIG_ARM64_SME
  402. static int preserve_tpidr2_context(struct tpidr2_context __user *ctx)
  403. {
  404. int err = 0;
  405. current->thread.tpidr2_el0 = read_sysreg_s(SYS_TPIDR2_EL0);
  406. __put_user_error(TPIDR2_MAGIC, &ctx->head.magic, err);
  407. __put_user_error(sizeof(*ctx), &ctx->head.size, err);
  408. __put_user_error(current->thread.tpidr2_el0, &ctx->tpidr2, err);
  409. return err;
  410. }
  411. static int restore_tpidr2_context(struct user_ctxs *user)
  412. {
  413. u64 tpidr2_el0;
  414. int err = 0;
  415. if (user->tpidr2_size != sizeof(*user->tpidr2))
  416. return -EINVAL;
  417. __get_user_error(tpidr2_el0, &user->tpidr2->tpidr2, err);
  418. if (!err)
  419. write_sysreg_s(tpidr2_el0, SYS_TPIDR2_EL0);
  420. return err;
  421. }
  422. static int preserve_za_context(struct za_context __user *ctx)
  423. {
  424. int err = 0;
  425. u16 reserved[ARRAY_SIZE(ctx->__reserved)];
  426. unsigned int vl = task_get_sme_vl(current);
  427. unsigned int vq;
  428. if (thread_za_enabled(&current->thread))
  429. vq = sve_vq_from_vl(vl);
  430. else
  431. vq = 0;
  432. memset(reserved, 0, sizeof(reserved));
  433. __put_user_error(ZA_MAGIC, &ctx->head.magic, err);
  434. __put_user_error(round_up(ZA_SIG_CONTEXT_SIZE(vq), 16),
  435. &ctx->head.size, err);
  436. __put_user_error(vl, &ctx->vl, err);
  437. BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved));
  438. err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved));
  439. if (vq) {
  440. /*
  441. * This assumes that the ZA state has already been saved to
  442. * the task struct by calling the function
  443. * fpsimd_signal_preserve_current_state().
  444. */
  445. err |= __copy_to_user((char __user *)ctx + ZA_SIG_REGS_OFFSET,
  446. current->thread.sme_state,
  447. ZA_SIG_REGS_SIZE(vq));
  448. }
  449. return err ? -EFAULT : 0;
  450. }
  451. static int restore_za_context(struct user_ctxs *user)
  452. {
  453. int err = 0;
  454. unsigned int vq;
  455. u16 user_vl;
  456. if (user->za_size < sizeof(*user->za))
  457. return -EINVAL;
  458. __get_user_error(user_vl, &(user->za->vl), err);
  459. if (err)
  460. return err;
  461. if (user_vl != task_get_sme_vl(current))
  462. return -EINVAL;
  463. if (user->za_size == sizeof(*user->za)) {
  464. current->thread.svcr &= ~SVCR_ZA_MASK;
  465. return 0;
  466. }
  467. vq = sve_vq_from_vl(user_vl);
  468. if (user->za_size < ZA_SIG_CONTEXT_SIZE(vq))
  469. return -EINVAL;
  470. /*
  471. * Careful: we are about __copy_from_user() directly into
  472. * thread.sme_state with preemption enabled, so protection is
  473. * needed to prevent a racing context switch from writing stale
  474. * registers back over the new data.
  475. */
  476. fpsimd_flush_task_state(current);
  477. /* From now, fpsimd_thread_switch() won't touch thread.sve_state */
  478. sme_alloc(current, true);
  479. if (!current->thread.sme_state) {
  480. current->thread.svcr &= ~SVCR_ZA_MASK;
  481. clear_thread_flag(TIF_SME);
  482. return -ENOMEM;
  483. }
  484. err = __copy_from_user(current->thread.sme_state,
  485. (char __user const *)user->za +
  486. ZA_SIG_REGS_OFFSET,
  487. ZA_SIG_REGS_SIZE(vq));
  488. if (err)
  489. return -EFAULT;
  490. set_thread_flag(TIF_SME);
  491. current->thread.svcr |= SVCR_ZA_MASK;
  492. return 0;
  493. }
  494. static int preserve_zt_context(struct zt_context __user *ctx)
  495. {
  496. int err = 0;
  497. u16 reserved[ARRAY_SIZE(ctx->__reserved)];
  498. if (WARN_ON(!thread_za_enabled(&current->thread)))
  499. return -EINVAL;
  500. memset(reserved, 0, sizeof(reserved));
  501. __put_user_error(ZT_MAGIC, &ctx->head.magic, err);
  502. __put_user_error(round_up(ZT_SIG_CONTEXT_SIZE(1), 16),
  503. &ctx->head.size, err);
  504. __put_user_error(1, &ctx->nregs, err);
  505. BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved));
  506. err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved));
  507. /*
  508. * This assumes that the ZT state has already been saved to
  509. * the task struct by calling the function
  510. * fpsimd_signal_preserve_current_state().
  511. */
  512. err |= __copy_to_user((char __user *)ctx + ZT_SIG_REGS_OFFSET,
  513. thread_zt_state(&current->thread),
  514. ZT_SIG_REGS_SIZE(1));
  515. return err ? -EFAULT : 0;
  516. }
  517. static int restore_zt_context(struct user_ctxs *user)
  518. {
  519. int err;
  520. u16 nregs;
  521. /* ZA must be restored first for this check to be valid */
  522. if (!thread_za_enabled(&current->thread))
  523. return -EINVAL;
  524. if (user->zt_size != ZT_SIG_CONTEXT_SIZE(1))
  525. return -EINVAL;
  526. if (__copy_from_user(&nregs, &(user->zt->nregs), sizeof(nregs)))
  527. return -EFAULT;
  528. if (nregs != 1)
  529. return -EINVAL;
  530. /*
  531. * Careful: we are about __copy_from_user() directly into
  532. * thread.zt_state with preemption enabled, so protection is
  533. * needed to prevent a racing context switch from writing stale
  534. * registers back over the new data.
  535. */
  536. fpsimd_flush_task_state(current);
  537. /* From now, fpsimd_thread_switch() won't touch ZT in thread state */
  538. err = __copy_from_user(thread_zt_state(&current->thread),
  539. (char __user const *)user->zt +
  540. ZT_SIG_REGS_OFFSET,
  541. ZT_SIG_REGS_SIZE(1));
  542. if (err)
  543. return -EFAULT;
  544. return 0;
  545. }
  546. #else /* ! CONFIG_ARM64_SME */
  547. /* Turn any non-optimised out attempts to use these into a link error: */
  548. extern int preserve_tpidr2_context(void __user *ctx);
  549. extern int restore_tpidr2_context(struct user_ctxs *user);
  550. extern int preserve_za_context(void __user *ctx);
  551. extern int restore_za_context(struct user_ctxs *user);
  552. extern int preserve_zt_context(void __user *ctx);
  553. extern int restore_zt_context(struct user_ctxs *user);
  554. #endif /* ! CONFIG_ARM64_SME */
  555. static int parse_user_sigframe(struct user_ctxs *user,
  556. struct rt_sigframe __user *sf)
  557. {
  558. struct sigcontext __user *const sc = &sf->uc.uc_mcontext;
  559. struct _aarch64_ctx __user *head;
  560. char __user *base = (char __user *)&sc->__reserved;
  561. size_t offset = 0;
  562. size_t limit = sizeof(sc->__reserved);
  563. bool have_extra_context = false;
  564. char const __user *const sfp = (char const __user *)sf;
  565. user->fpsimd = NULL;
  566. user->sve = NULL;
  567. user->tpidr2 = NULL;
  568. user->za = NULL;
  569. user->zt = NULL;
  570. user->fpmr = NULL;
  571. user->poe = NULL;
  572. if (!IS_ALIGNED((unsigned long)base, 16))
  573. goto invalid;
  574. while (1) {
  575. int err = 0;
  576. u32 magic, size;
  577. char const __user *userp;
  578. struct extra_context const __user *extra;
  579. u64 extra_datap;
  580. u32 extra_size;
  581. struct _aarch64_ctx const __user *end;
  582. u32 end_magic, end_size;
  583. if (limit - offset < sizeof(*head))
  584. goto invalid;
  585. if (!IS_ALIGNED(offset, 16))
  586. goto invalid;
  587. head = (struct _aarch64_ctx __user *)(base + offset);
  588. __get_user_error(magic, &head->magic, err);
  589. __get_user_error(size, &head->size, err);
  590. if (err)
  591. return err;
  592. if (limit - offset < size)
  593. goto invalid;
  594. switch (magic) {
  595. case 0:
  596. if (size)
  597. goto invalid;
  598. goto done;
  599. case FPSIMD_MAGIC:
  600. if (!system_supports_fpsimd())
  601. goto invalid;
  602. if (user->fpsimd)
  603. goto invalid;
  604. user->fpsimd = (struct fpsimd_context __user *)head;
  605. user->fpsimd_size = size;
  606. break;
  607. case ESR_MAGIC:
  608. /* ignore */
  609. break;
  610. case POE_MAGIC:
  611. if (!system_supports_poe())
  612. goto invalid;
  613. if (user->poe)
  614. goto invalid;
  615. user->poe = (struct poe_context __user *)head;
  616. user->poe_size = size;
  617. break;
  618. case SVE_MAGIC:
  619. if (!system_supports_sve() && !system_supports_sme())
  620. goto invalid;
  621. if (user->sve)
  622. goto invalid;
  623. user->sve = (struct sve_context __user *)head;
  624. user->sve_size = size;
  625. break;
  626. case TPIDR2_MAGIC:
  627. if (!system_supports_tpidr2())
  628. goto invalid;
  629. if (user->tpidr2)
  630. goto invalid;
  631. user->tpidr2 = (struct tpidr2_context __user *)head;
  632. user->tpidr2_size = size;
  633. break;
  634. case ZA_MAGIC:
  635. if (!system_supports_sme())
  636. goto invalid;
  637. if (user->za)
  638. goto invalid;
  639. user->za = (struct za_context __user *)head;
  640. user->za_size = size;
  641. break;
  642. case ZT_MAGIC:
  643. if (!system_supports_sme2())
  644. goto invalid;
  645. if (user->zt)
  646. goto invalid;
  647. user->zt = (struct zt_context __user *)head;
  648. user->zt_size = size;
  649. break;
  650. case FPMR_MAGIC:
  651. if (!system_supports_fpmr())
  652. goto invalid;
  653. if (user->fpmr)
  654. goto invalid;
  655. user->fpmr = (struct fpmr_context __user *)head;
  656. user->fpmr_size = size;
  657. break;
  658. case EXTRA_MAGIC:
  659. if (have_extra_context)
  660. goto invalid;
  661. if (size < sizeof(*extra))
  662. goto invalid;
  663. userp = (char const __user *)head;
  664. extra = (struct extra_context const __user *)userp;
  665. userp += size;
  666. __get_user_error(extra_datap, &extra->datap, err);
  667. __get_user_error(extra_size, &extra->size, err);
  668. if (err)
  669. return err;
  670. /* Check for the dummy terminator in __reserved[]: */
  671. if (limit - offset - size < TERMINATOR_SIZE)
  672. goto invalid;
  673. end = (struct _aarch64_ctx const __user *)userp;
  674. userp += TERMINATOR_SIZE;
  675. __get_user_error(end_magic, &end->magic, err);
  676. __get_user_error(end_size, &end->size, err);
  677. if (err)
  678. return err;
  679. if (end_magic || end_size)
  680. goto invalid;
  681. /* Prevent looping/repeated parsing of extra_context */
  682. have_extra_context = true;
  683. base = (__force void __user *)extra_datap;
  684. if (!IS_ALIGNED((unsigned long)base, 16))
  685. goto invalid;
  686. if (!IS_ALIGNED(extra_size, 16))
  687. goto invalid;
  688. if (base != userp)
  689. goto invalid;
  690. /* Reject "unreasonably large" frames: */
  691. if (extra_size > sfp + SIGFRAME_MAXSZ - userp)
  692. goto invalid;
  693. /*
  694. * Ignore trailing terminator in __reserved[]
  695. * and start parsing extra data:
  696. */
  697. offset = 0;
  698. limit = extra_size;
  699. if (!access_ok(base, limit))
  700. goto invalid;
  701. continue;
  702. default:
  703. goto invalid;
  704. }
  705. if (size < sizeof(*head))
  706. goto invalid;
  707. if (limit - offset < size)
  708. goto invalid;
  709. offset += size;
  710. }
  711. done:
  712. return 0;
  713. invalid:
  714. return -EINVAL;
  715. }
  716. static int restore_sigframe(struct pt_regs *regs,
  717. struct rt_sigframe __user *sf,
  718. struct user_access_state *ua_state)
  719. {
  720. sigset_t set;
  721. int i, err;
  722. struct user_ctxs user;
  723. err = __copy_from_user(&set, &sf->uc.uc_sigmask, sizeof(set));
  724. if (err == 0)
  725. set_current_blocked(&set);
  726. for (i = 0; i < 31; i++)
  727. __get_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
  728. err);
  729. __get_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
  730. __get_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
  731. __get_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
  732. /*
  733. * Avoid sys_rt_sigreturn() restarting.
  734. */
  735. forget_syscall(regs);
  736. err |= !valid_user_regs(&regs->user_regs, current);
  737. if (err == 0)
  738. err = parse_user_sigframe(&user, sf);
  739. if (err == 0 && system_supports_fpsimd()) {
  740. if (!user.fpsimd)
  741. return -EINVAL;
  742. if (user.sve)
  743. err = restore_sve_fpsimd_context(&user);
  744. else
  745. err = restore_fpsimd_context(&user);
  746. }
  747. if (err == 0 && system_supports_tpidr2() && user.tpidr2)
  748. err = restore_tpidr2_context(&user);
  749. if (err == 0 && system_supports_fpmr() && user.fpmr)
  750. err = restore_fpmr_context(&user);
  751. if (err == 0 && system_supports_sme() && user.za)
  752. err = restore_za_context(&user);
  753. if (err == 0 && system_supports_sme2() && user.zt)
  754. err = restore_zt_context(&user);
  755. if (err == 0 && system_supports_poe() && user.poe)
  756. err = restore_poe_context(&user, ua_state);
  757. return err;
  758. }
  759. SYSCALL_DEFINE0(rt_sigreturn)
  760. {
  761. struct pt_regs *regs = current_pt_regs();
  762. struct rt_sigframe __user *frame;
  763. struct user_access_state ua_state;
  764. /* Always make any pending restarted system calls return -EINTR */
  765. current->restart_block.fn = do_no_restart_syscall;
  766. /*
  767. * Since we stacked the signal on a 128-bit boundary, then 'sp' should
  768. * be word aligned here.
  769. */
  770. if (regs->sp & 15)
  771. goto badframe;
  772. frame = (struct rt_sigframe __user *)regs->sp;
  773. if (!access_ok(frame, sizeof (*frame)))
  774. goto badframe;
  775. if (restore_sigframe(regs, frame, &ua_state))
  776. goto badframe;
  777. if (restore_altstack(&frame->uc.uc_stack))
  778. goto badframe;
  779. restore_user_access_state(&ua_state);
  780. return regs->regs[0];
  781. badframe:
  782. arm64_notify_segfault(regs->sp);
  783. return 0;
  784. }
  785. /*
  786. * Determine the layout of optional records in the signal frame
  787. *
  788. * add_all: if true, lays out the biggest possible signal frame for
  789. * this task; otherwise, generates a layout for the current state
  790. * of the task.
  791. */
  792. static int setup_sigframe_layout(struct rt_sigframe_user_layout *user,
  793. bool add_all)
  794. {
  795. int err;
  796. if (system_supports_fpsimd()) {
  797. err = sigframe_alloc(user, &user->fpsimd_offset,
  798. sizeof(struct fpsimd_context));
  799. if (err)
  800. return err;
  801. }
  802. /* fault information, if valid */
  803. if (add_all || current->thread.fault_code) {
  804. err = sigframe_alloc(user, &user->esr_offset,
  805. sizeof(struct esr_context));
  806. if (err)
  807. return err;
  808. }
  809. if (system_supports_sve() || system_supports_sme()) {
  810. unsigned int vq = 0;
  811. if (add_all || current->thread.fp_type == FP_STATE_SVE ||
  812. thread_sm_enabled(&current->thread)) {
  813. int vl = max(sve_max_vl(), sme_max_vl());
  814. if (!add_all)
  815. vl = thread_get_cur_vl(&current->thread);
  816. vq = sve_vq_from_vl(vl);
  817. }
  818. err = sigframe_alloc(user, &user->sve_offset,
  819. SVE_SIG_CONTEXT_SIZE(vq));
  820. if (err)
  821. return err;
  822. }
  823. if (system_supports_tpidr2()) {
  824. err = sigframe_alloc(user, &user->tpidr2_offset,
  825. sizeof(struct tpidr2_context));
  826. if (err)
  827. return err;
  828. }
  829. if (system_supports_sme()) {
  830. unsigned int vl;
  831. unsigned int vq = 0;
  832. if (add_all)
  833. vl = sme_max_vl();
  834. else
  835. vl = task_get_sme_vl(current);
  836. if (thread_za_enabled(&current->thread))
  837. vq = sve_vq_from_vl(vl);
  838. err = sigframe_alloc(user, &user->za_offset,
  839. ZA_SIG_CONTEXT_SIZE(vq));
  840. if (err)
  841. return err;
  842. }
  843. if (system_supports_sme2()) {
  844. if (add_all || thread_za_enabled(&current->thread)) {
  845. err = sigframe_alloc(user, &user->zt_offset,
  846. ZT_SIG_CONTEXT_SIZE(1));
  847. if (err)
  848. return err;
  849. }
  850. }
  851. if (system_supports_fpmr()) {
  852. err = sigframe_alloc(user, &user->fpmr_offset,
  853. sizeof(struct fpmr_context));
  854. if (err)
  855. return err;
  856. }
  857. if (system_supports_poe()) {
  858. err = sigframe_alloc(user, &user->poe_offset,
  859. sizeof(struct poe_context));
  860. if (err)
  861. return err;
  862. }
  863. return sigframe_alloc_end(user);
  864. }
  865. static int setup_sigframe(struct rt_sigframe_user_layout *user,
  866. struct pt_regs *regs, sigset_t *set,
  867. const struct user_access_state *ua_state)
  868. {
  869. int i, err = 0;
  870. struct rt_sigframe __user *sf = user->sigframe;
  871. /* set up the stack frame for unwinding */
  872. __put_user_error(regs->regs[29], &user->next_frame->fp, err);
  873. __put_user_error(regs->regs[30], &user->next_frame->lr, err);
  874. for (i = 0; i < 31; i++)
  875. __put_user_error(regs->regs[i], &sf->uc.uc_mcontext.regs[i],
  876. err);
  877. __put_user_error(regs->sp, &sf->uc.uc_mcontext.sp, err);
  878. __put_user_error(regs->pc, &sf->uc.uc_mcontext.pc, err);
  879. __put_user_error(regs->pstate, &sf->uc.uc_mcontext.pstate, err);
  880. __put_user_error(current->thread.fault_address, &sf->uc.uc_mcontext.fault_address, err);
  881. err |= __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set));
  882. if (err == 0 && system_supports_fpsimd()) {
  883. struct fpsimd_context __user *fpsimd_ctx =
  884. apply_user_offset(user, user->fpsimd_offset);
  885. err |= preserve_fpsimd_context(fpsimd_ctx);
  886. }
  887. /* fault information, if valid */
  888. if (err == 0 && user->esr_offset) {
  889. struct esr_context __user *esr_ctx =
  890. apply_user_offset(user, user->esr_offset);
  891. __put_user_error(ESR_MAGIC, &esr_ctx->head.magic, err);
  892. __put_user_error(sizeof(*esr_ctx), &esr_ctx->head.size, err);
  893. __put_user_error(current->thread.fault_code, &esr_ctx->esr, err);
  894. }
  895. /* Scalable Vector Extension state (including streaming), if present */
  896. if ((system_supports_sve() || system_supports_sme()) &&
  897. err == 0 && user->sve_offset) {
  898. struct sve_context __user *sve_ctx =
  899. apply_user_offset(user, user->sve_offset);
  900. err |= preserve_sve_context(sve_ctx);
  901. }
  902. /* TPIDR2 if supported */
  903. if (system_supports_tpidr2() && err == 0) {
  904. struct tpidr2_context __user *tpidr2_ctx =
  905. apply_user_offset(user, user->tpidr2_offset);
  906. err |= preserve_tpidr2_context(tpidr2_ctx);
  907. }
  908. /* FPMR if supported */
  909. if (system_supports_fpmr() && err == 0) {
  910. struct fpmr_context __user *fpmr_ctx =
  911. apply_user_offset(user, user->fpmr_offset);
  912. err |= preserve_fpmr_context(fpmr_ctx);
  913. }
  914. if (system_supports_poe() && err == 0 && user->poe_offset) {
  915. struct poe_context __user *poe_ctx =
  916. apply_user_offset(user, user->poe_offset);
  917. err |= preserve_poe_context(poe_ctx, ua_state);
  918. }
  919. /* ZA state if present */
  920. if (system_supports_sme() && err == 0 && user->za_offset) {
  921. struct za_context __user *za_ctx =
  922. apply_user_offset(user, user->za_offset);
  923. err |= preserve_za_context(za_ctx);
  924. }
  925. /* ZT state if present */
  926. if (system_supports_sme2() && err == 0 && user->zt_offset) {
  927. struct zt_context __user *zt_ctx =
  928. apply_user_offset(user, user->zt_offset);
  929. err |= preserve_zt_context(zt_ctx);
  930. }
  931. if (err == 0 && user->extra_offset) {
  932. char __user *sfp = (char __user *)user->sigframe;
  933. char __user *userp =
  934. apply_user_offset(user, user->extra_offset);
  935. struct extra_context __user *extra;
  936. struct _aarch64_ctx __user *end;
  937. u64 extra_datap;
  938. u32 extra_size;
  939. extra = (struct extra_context __user *)userp;
  940. userp += EXTRA_CONTEXT_SIZE;
  941. end = (struct _aarch64_ctx __user *)userp;
  942. userp += TERMINATOR_SIZE;
  943. /*
  944. * extra_datap is just written to the signal frame.
  945. * The value gets cast back to a void __user *
  946. * during sigreturn.
  947. */
  948. extra_datap = (__force u64)userp;
  949. extra_size = sfp + round_up(user->size, 16) - userp;
  950. __put_user_error(EXTRA_MAGIC, &extra->head.magic, err);
  951. __put_user_error(EXTRA_CONTEXT_SIZE, &extra->head.size, err);
  952. __put_user_error(extra_datap, &extra->datap, err);
  953. __put_user_error(extra_size, &extra->size, err);
  954. /* Add the terminator */
  955. __put_user_error(0, &end->magic, err);
  956. __put_user_error(0, &end->size, err);
  957. }
  958. /* set the "end" magic */
  959. if (err == 0) {
  960. struct _aarch64_ctx __user *end =
  961. apply_user_offset(user, user->end_offset);
  962. __put_user_error(0, &end->magic, err);
  963. __put_user_error(0, &end->size, err);
  964. }
  965. return err;
  966. }
  967. static int get_sigframe(struct rt_sigframe_user_layout *user,
  968. struct ksignal *ksig, struct pt_regs *regs)
  969. {
  970. unsigned long sp, sp_top;
  971. int err;
  972. init_user_layout(user);
  973. err = setup_sigframe_layout(user, false);
  974. if (err)
  975. return err;
  976. sp = sp_top = sigsp(regs->sp, ksig);
  977. sp = round_down(sp - sizeof(struct frame_record), 16);
  978. user->next_frame = (struct frame_record __user *)sp;
  979. sp = round_down(sp, 16) - sigframe_size(user);
  980. user->sigframe = (struct rt_sigframe __user *)sp;
  981. /*
  982. * Check that we can actually write to the signal frame.
  983. */
  984. if (!access_ok(user->sigframe, sp_top - sp))
  985. return -EFAULT;
  986. return 0;
  987. }
  988. static void setup_return(struct pt_regs *regs, struct k_sigaction *ka,
  989. struct rt_sigframe_user_layout *user, int usig)
  990. {
  991. __sigrestore_t sigtramp;
  992. regs->regs[0] = usig;
  993. regs->sp = (unsigned long)user->sigframe;
  994. regs->regs[29] = (unsigned long)&user->next_frame->fp;
  995. regs->pc = (unsigned long)ka->sa.sa_handler;
  996. /*
  997. * Signal delivery is a (wacky) indirect function call in
  998. * userspace, so simulate the same setting of BTYPE as a BLR
  999. * <register containing the signal handler entry point>.
  1000. * Signal delivery to a location in a PROT_BTI guarded page
  1001. * that is not a function entry point will now trigger a
  1002. * SIGILL in userspace.
  1003. *
  1004. * If the signal handler entry point is not in a PROT_BTI
  1005. * guarded page, this is harmless.
  1006. */
  1007. if (system_supports_bti()) {
  1008. regs->pstate &= ~PSR_BTYPE_MASK;
  1009. regs->pstate |= PSR_BTYPE_C;
  1010. }
  1011. /* TCO (Tag Check Override) always cleared for signal handlers */
  1012. regs->pstate &= ~PSR_TCO_BIT;
  1013. /* Signal handlers are invoked with ZA and streaming mode disabled */
  1014. if (system_supports_sme()) {
  1015. /*
  1016. * If we were in streaming mode the saved register
  1017. * state was SVE but we will exit SM and use the
  1018. * FPSIMD register state - flush the saved FPSIMD
  1019. * register state in case it gets loaded.
  1020. */
  1021. if (current->thread.svcr & SVCR_SM_MASK) {
  1022. memset(&current->thread.uw.fpsimd_state, 0,
  1023. sizeof(current->thread.uw.fpsimd_state));
  1024. current->thread.fp_type = FP_STATE_FPSIMD;
  1025. }
  1026. current->thread.svcr &= ~(SVCR_ZA_MASK |
  1027. SVCR_SM_MASK);
  1028. sme_smstop();
  1029. }
  1030. if (ka->sa.sa_flags & SA_RESTORER)
  1031. sigtramp = ka->sa.sa_restorer;
  1032. else
  1033. sigtramp = VDSO_SYMBOL(current->mm->context.vdso, sigtramp);
  1034. regs->regs[30] = (unsigned long)sigtramp;
  1035. }
  1036. static int setup_rt_frame(int usig, struct ksignal *ksig, sigset_t *set,
  1037. struct pt_regs *regs)
  1038. {
  1039. struct rt_sigframe_user_layout user;
  1040. struct rt_sigframe __user *frame;
  1041. struct user_access_state ua_state;
  1042. int err = 0;
  1043. fpsimd_signal_preserve_current_state();
  1044. if (get_sigframe(&user, ksig, regs))
  1045. return 1;
  1046. save_reset_user_access_state(&ua_state);
  1047. frame = user.sigframe;
  1048. __put_user_error(0, &frame->uc.uc_flags, err);
  1049. __put_user_error(NULL, &frame->uc.uc_link, err);
  1050. err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
  1051. err |= setup_sigframe(&user, regs, set, &ua_state);
  1052. if (err == 0) {
  1053. setup_return(regs, &ksig->ka, &user, usig);
  1054. if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
  1055. err |= copy_siginfo_to_user(&frame->info, &ksig->info);
  1056. regs->regs[1] = (unsigned long)&frame->info;
  1057. regs->regs[2] = (unsigned long)&frame->uc;
  1058. }
  1059. }
  1060. if (err == 0)
  1061. set_handler_user_access_state();
  1062. else
  1063. restore_user_access_state(&ua_state);
  1064. return err;
  1065. }
  1066. static void setup_restart_syscall(struct pt_regs *regs)
  1067. {
  1068. if (is_compat_task())
  1069. compat_setup_restart_syscall(regs);
  1070. else
  1071. regs->regs[8] = __NR_restart_syscall;
  1072. }
  1073. /*
  1074. * OK, we're invoking a handler
  1075. */
  1076. static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
  1077. {
  1078. sigset_t *oldset = sigmask_to_save();
  1079. int usig = ksig->sig;
  1080. int ret;
  1081. rseq_signal_deliver(ksig, regs);
  1082. /*
  1083. * Set up the stack frame
  1084. */
  1085. if (is_compat_task()) {
  1086. if (ksig->ka.sa.sa_flags & SA_SIGINFO)
  1087. ret = compat_setup_rt_frame(usig, ksig, oldset, regs);
  1088. else
  1089. ret = compat_setup_frame(usig, ksig, oldset, regs);
  1090. } else {
  1091. ret = setup_rt_frame(usig, ksig, oldset, regs);
  1092. }
  1093. /*
  1094. * Check that the resulting registers are actually sane.
  1095. */
  1096. ret |= !valid_user_regs(&regs->user_regs, current);
  1097. /* Step into the signal handler if we are stepping */
  1098. signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
  1099. }
  1100. /*
  1101. * Note that 'init' is a special process: it doesn't get signals it doesn't
  1102. * want to handle. Thus you cannot kill init even with a SIGKILL even by
  1103. * mistake.
  1104. *
  1105. * Note that we go through the signals twice: once to check the signals that
  1106. * the kernel can handle, and then we build all the user-level signal handling
  1107. * stack-frames in one go after that.
  1108. */
  1109. void do_signal(struct pt_regs *regs)
  1110. {
  1111. unsigned long continue_addr = 0, restart_addr = 0;
  1112. int retval = 0;
  1113. struct ksignal ksig;
  1114. bool syscall = in_syscall(regs);
  1115. /*
  1116. * If we were from a system call, check for system call restarting...
  1117. */
  1118. if (syscall) {
  1119. continue_addr = regs->pc;
  1120. restart_addr = continue_addr - (compat_thumb_mode(regs) ? 2 : 4);
  1121. retval = regs->regs[0];
  1122. /*
  1123. * Avoid additional syscall restarting via ret_to_user.
  1124. */
  1125. forget_syscall(regs);
  1126. /*
  1127. * Prepare for system call restart. We do this here so that a
  1128. * debugger will see the already changed PC.
  1129. */
  1130. switch (retval) {
  1131. case -ERESTARTNOHAND:
  1132. case -ERESTARTSYS:
  1133. case -ERESTARTNOINTR:
  1134. case -ERESTART_RESTARTBLOCK:
  1135. regs->regs[0] = regs->orig_x0;
  1136. regs->pc = restart_addr;
  1137. break;
  1138. }
  1139. }
  1140. /*
  1141. * Get the signal to deliver. When running under ptrace, at this point
  1142. * the debugger may change all of our registers.
  1143. */
  1144. if (get_signal(&ksig)) {
  1145. /*
  1146. * Depending on the signal settings, we may need to revert the
  1147. * decision to restart the system call, but skip this if a
  1148. * debugger has chosen to restart at a different PC.
  1149. */
  1150. if (regs->pc == restart_addr &&
  1151. (retval == -ERESTARTNOHAND ||
  1152. retval == -ERESTART_RESTARTBLOCK ||
  1153. (retval == -ERESTARTSYS &&
  1154. !(ksig.ka.sa.sa_flags & SA_RESTART)))) {
  1155. syscall_set_return_value(current, regs, -EINTR, 0);
  1156. regs->pc = continue_addr;
  1157. }
  1158. handle_signal(&ksig, regs);
  1159. return;
  1160. }
  1161. /*
  1162. * Handle restarting a different system call. As above, if a debugger
  1163. * has chosen to restart at a different PC, ignore the restart.
  1164. */
  1165. if (syscall && regs->pc == restart_addr) {
  1166. if (retval == -ERESTART_RESTARTBLOCK)
  1167. setup_restart_syscall(regs);
  1168. user_rewind_single_step(current);
  1169. }
  1170. restore_saved_sigmask();
  1171. }
  1172. unsigned long __ro_after_init signal_minsigstksz;
  1173. /*
  1174. * Determine the stack space required for guaranteed signal devliery.
  1175. * This function is used to populate AT_MINSIGSTKSZ at process startup.
  1176. * cpufeatures setup is assumed to be complete.
  1177. */
  1178. void __init minsigstksz_setup(void)
  1179. {
  1180. struct rt_sigframe_user_layout user;
  1181. init_user_layout(&user);
  1182. /*
  1183. * If this fails, SIGFRAME_MAXSZ needs to be enlarged. It won't
  1184. * be big enough, but it's our best guess:
  1185. */
  1186. if (WARN_ON(setup_sigframe_layout(&user, true)))
  1187. return;
  1188. signal_minsigstksz = sigframe_size(&user) +
  1189. round_up(sizeof(struct frame_record), 16) +
  1190. 16; /* max alignment padding */
  1191. }
  1192. /*
  1193. * Compile-time assertions for siginfo_t offsets. Check NSIG* as well, as
  1194. * changes likely come with new fields that should be added below.
  1195. */
  1196. static_assert(NSIGILL == 11);
  1197. static_assert(NSIGFPE == 15);
  1198. static_assert(NSIGSEGV == 10);
  1199. static_assert(NSIGBUS == 5);
  1200. static_assert(NSIGTRAP == 6);
  1201. static_assert(NSIGCHLD == 6);
  1202. static_assert(NSIGSYS == 2);
  1203. static_assert(sizeof(siginfo_t) == 128);
  1204. static_assert(__alignof__(siginfo_t) == 8);
  1205. static_assert(offsetof(siginfo_t, si_signo) == 0x00);
  1206. static_assert(offsetof(siginfo_t, si_errno) == 0x04);
  1207. static_assert(offsetof(siginfo_t, si_code) == 0x08);
  1208. static_assert(offsetof(siginfo_t, si_pid) == 0x10);
  1209. static_assert(offsetof(siginfo_t, si_uid) == 0x14);
  1210. static_assert(offsetof(siginfo_t, si_tid) == 0x10);
  1211. static_assert(offsetof(siginfo_t, si_overrun) == 0x14);
  1212. static_assert(offsetof(siginfo_t, si_status) == 0x18);
  1213. static_assert(offsetof(siginfo_t, si_utime) == 0x20);
  1214. static_assert(offsetof(siginfo_t, si_stime) == 0x28);
  1215. static_assert(offsetof(siginfo_t, si_value) == 0x18);
  1216. static_assert(offsetof(siginfo_t, si_int) == 0x18);
  1217. static_assert(offsetof(siginfo_t, si_ptr) == 0x18);
  1218. static_assert(offsetof(siginfo_t, si_addr) == 0x10);
  1219. static_assert(offsetof(siginfo_t, si_addr_lsb) == 0x18);
  1220. static_assert(offsetof(siginfo_t, si_lower) == 0x20);
  1221. static_assert(offsetof(siginfo_t, si_upper) == 0x28);
  1222. static_assert(offsetof(siginfo_t, si_pkey) == 0x20);
  1223. static_assert(offsetof(siginfo_t, si_perf_data) == 0x18);
  1224. static_assert(offsetof(siginfo_t, si_perf_type) == 0x20);
  1225. static_assert(offsetof(siginfo_t, si_perf_flags) == 0x24);
  1226. static_assert(offsetof(siginfo_t, si_band) == 0x10);
  1227. static_assert(offsetof(siginfo_t, si_fd) == 0x18);
  1228. static_assert(offsetof(siginfo_t, si_call_addr) == 0x10);
  1229. static_assert(offsetof(siginfo_t, si_syscall) == 0x18);
  1230. static_assert(offsetof(siginfo_t, si_arch) == 0x1c);