| @@ -57,6 +57,7 @@ struct rt_sigframe_user_layout { · 56 unmodified lines |
| 57 | 57 | unsigned long esr_offset; |
| 58 | 58 | unsigned long sve_offset; |
| 59 | 59 | unsigned long za_offset; |
| 60 | + unsigned long zt_offset; |
| 60 | 61 | unsigned long extra_offset; |
| 61 | 62 | unsigned long end_offset; |
| 62 | 63 | }; |
| @@ -221,6 +222,7 @@ struct user_ctxs { · 158 unmodified lines |
| 221 | 222 | struct fpsimd_context __user *fpsimd; |
| 222 | 223 | struct sve_context __user *sve; |
| 223 | 224 | struct za_context __user *za; |
| 225 | + struct zt_context __user *zt; |
| 224 | 226 | }; |
| 225 | 227 | |
| 226 | 228 | #ifdef CONFIG_ARM64_SVE |
| @@ -447,11 +449,81 @@ static int restore_za_context(struct user_ctxs *user) · 220 unmodified lines |
| 447 | 449 | |
| 448 | 450 | return 0; |
| 449 | 451 | } |
| 452 | + |
| 453 | +static int preserve_zt_context(struct zt_context __user *ctx) |
| 454 | +{ |
| 455 | + int err = 0; |
| 456 | + u16 reserved[ARRAY_SIZE(ctx->__reserved)]; |
| 457 | + |
| 458 | + if (WARN_ON(!thread_za_enabled(¤t->thread))) |
| 459 | + return -EINVAL; |
| 460 | + |
| 461 | + memset(reserved, 0, sizeof(reserved)); |
| 462 | + |
| 463 | + __put_user_error(ZT_MAGIC, &ctx->head.magic, err); |
| 464 | + __put_user_error(round_up(ZT_SIG_CONTEXT_SIZE(1), 16), |
| 465 | + &ctx->head.size, err); |
| 466 | + __put_user_error(1, &ctx->nregs, err); |
| 467 | + BUILD_BUG_ON(sizeof(ctx->__reserved) != sizeof(reserved)); |
| 468 | + err |= __copy_to_user(&ctx->__reserved, reserved, sizeof(reserved)); |
| 469 | + |
| 470 | + /* |
| 471 | + * This assumes that the ZT state has already been saved to |
| 472 | + * the task struct by calling the function |
| 473 | + * fpsimd_signal_preserve_current_state(). |
| 474 | + */ |
| 475 | + err |= __copy_to_user((char __user *)ctx + ZT_SIG_REGS_OFFSET, |
| 476 | + thread_zt_state(¤t->thread), |
| 477 | + ZT_SIG_REGS_SIZE(1)); |
| 478 | + |
| 479 | + return err ? -EFAULT : 0; |
| 480 | +} |
| 481 | + |
| 482 | +static int restore_zt_context(struct user_ctxs *user) |
| 483 | +{ |
| 484 | + int err; |
| 485 | + struct zt_context zt; |
| 486 | + |
| 487 | + /* ZA must be restored first for this check to be valid */ |
| 488 | + if (!thread_za_enabled(¤t->thread)) |
| 489 | + return -EINVAL; |
| 490 | + |
| 491 | + if (__copy_from_user(&zt, user->zt, sizeof(zt))) |
| 492 | + return -EFAULT; |
| 493 | + |
| 494 | + if (zt.nregs != 1) |
| 495 | + return -EINVAL; |
| 496 | + |
| 497 | + if (zt.head.size != ZT_SIG_CONTEXT_SIZE(zt.nregs)) |
| 498 | + return -EINVAL; |
| 499 | + |
| 500 | + /* |
| 501 | + * Careful: we are about __copy_from_user() directly into |
| 502 | + * thread.zt_state with preemption enabled, so protection is |
| 503 | + * needed to prevent a racing context switch from writing stale |
| 504 | + * registers back over the new data. |
| 505 | + */ |
| 506 | + |
| 507 | + fpsimd_flush_task_state(current); |
| 508 | + /* From now, fpsimd_thread_switch() won't touch ZT in thread state */ |
| 509 | + |
| 510 | + err = __copy_from_user(thread_zt_state(¤t->thread), |
| 511 | + (char __user const *)user->zt + |
| 512 | + ZT_SIG_REGS_OFFSET, |
| 513 | + ZT_SIG_REGS_SIZE(1)); |
| 514 | + if (err) |
| 515 | + return -EFAULT; |
| 516 | + |
| 517 | + return 0; |
| 518 | +} |
| 519 | + |
| 450 | 520 | #else /* ! CONFIG_ARM64_SME */ |
| 451 | 521 | |
| 452 | 522 | /* Turn any non-optimised out attempts to use these into a link error: */ |
| 453 | 523 | extern int preserve_za_context(void __user *ctx); |
| 454 | 524 | extern int restore_za_context(struct user_ctxs *user); |
| 525 | +extern int preserve_zt_context(void __user *ctx); |
| 526 | +extern int restore_zt_context(struct user_ctxs *user); |
| 455 | 527 | |
| 456 | 528 | #endif /* ! CONFIG_ARM64_SME */ |
| 457 | 529 | |
| @@ -469,6 +541,7 @@ static int parse_user_sigframe(struct user_ctxs *user, · 11 unmodified lines |
| 469 | 541 | user->fpsimd = NULL; |
| 470 | 542 | user->sve = NULL; |
| 471 | 543 | user->za = NULL; |
| 544 | + user->zt = NULL; |
| 472 | 545 | |
| 473 | 546 | if (!IS_ALIGNED((unsigned long)base, 16)) |
| 474 | 547 | goto invalid; |
| @@ -547,6 +620,19 @@ static int parse_user_sigframe(struct user_ctxs *user, · 72 unmodified lines |
| 547 | 620 | user->za = (struct za_context __user *)head; |
| 548 | 621 | break; |
| 549 | 622 | |
| 623 | + case ZT_MAGIC: |
| 624 | + if (!system_supports_sme2()) |
| 625 | + goto invalid; |
| 626 | + |
| 627 | + if (user->zt) |
| 628 | + goto invalid; |
| 629 | + |
| 630 | + if (size < sizeof(*user->zt)) |
| 631 | + goto invalid; |
| 632 | + |
| 633 | + user->zt = (struct zt_context __user *)head; |
| 634 | + break; |
| 635 | + |
| 550 | 636 | case EXTRA_MAGIC: |
| 551 | 637 | if (have_extra_context) |
| 552 | 638 | goto invalid; |
| @@ -669,6 +755,9 @@ static int restore_sigframe(struct pt_regs *regs, · 116 unmodified lines |
| 669 | 755 | if (err == 0 && system_supports_sme() && user.za) |
| 670 | 756 | err = restore_za_context(&user); |
| 671 | 757 | |
| 758 | + if (err == 0 && system_supports_sme2() && user.zt) |
| 759 | + err = restore_zt_context(&user); |
| 760 | + |
| 672 | 761 | return err; |
| 673 | 762 | } |
| 674 | 763 | |
| @@ -769,6 +858,15 @@ static int setup_sigframe_layout(struct rt_sigframe_user_layout *user, · 94 unmodified lines |
| 769 | 858 | return err; |
| 770 | 859 | } |
| 771 | 860 | |
| 861 | + if (system_supports_sme2()) { |
| 862 | + if (add_all || thread_za_enabled(¤t->thread)) { |
| 863 | + err = sigframe_alloc(user, &user->zt_offset, |
| 864 | + ZT_SIG_CONTEXT_SIZE(1)); |
| 865 | + if (err) |
| 866 | + return err; |
| 867 | + } |
| 868 | + } |
| 869 | + |
| 772 | 870 | return sigframe_alloc_end(user); |
| 773 | 871 | } |
| 774 | 872 | |
| @@ -824,6 +922,13 @@ static int setup_sigframe(struct rt_sigframe_user_layout *user, · 49 unmodified lines |
| 824 | 922 | err |= preserve_za_context(za_ctx); |
| 825 | 923 | } |
| 826 | 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 | + |
| 827 | 932 | if (err == 0 && user->extra_offset) { |
| 828 | 933 | char __user *sfp = (char __user *)user->sigframe; |
| 829 | 934 | char __user *userp = |