162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci#include <linux/regset.h>
462306a36Sopenharmony_ci#include <linux/elf.h>
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <asm/switch_to.h>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include "ptrace-decl.h"
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci/*
1162306a36Sopenharmony_ci * Get/set all the altivec registers vr0..vr31, vscr, vrsave, in one go.
1262306a36Sopenharmony_ci * The transfer totals 34 quadword.  Quadwords 0-31 contain the
1362306a36Sopenharmony_ci * corresponding vector registers.  Quadword 32 contains the vscr as the
1462306a36Sopenharmony_ci * last word (offset 12) within that quadword.  Quadword 33 contains the
1562306a36Sopenharmony_ci * vrsave as the first word (offset 0) within the quadword.
1662306a36Sopenharmony_ci *
1762306a36Sopenharmony_ci * This definition of the VMX state is compatible with the current PPC32
1862306a36Sopenharmony_ci * ptrace interface.  This allows signal handling and ptrace to use the
1962306a36Sopenharmony_ci * same structures.  This also simplifies the implementation of a bi-arch
2062306a36Sopenharmony_ci * (combined (32- and 64-bit) gdb.
2162306a36Sopenharmony_ci */
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ciint vr_active(struct task_struct *target, const struct user_regset *regset)
2462306a36Sopenharmony_ci{
2562306a36Sopenharmony_ci	flush_altivec_to_thread(target);
2662306a36Sopenharmony_ci	return target->thread.used_vr ? regset->n : 0;
2762306a36Sopenharmony_ci}
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/*
3062306a36Sopenharmony_ci * Regardless of transactions, 'vr_state' holds the current running
3162306a36Sopenharmony_ci * value of all the VMX registers and 'ckvr_state' holds the last
3262306a36Sopenharmony_ci * checkpointed value of all the VMX registers for the current
3362306a36Sopenharmony_ci * transaction to fall back on in case it aborts.
3462306a36Sopenharmony_ci *
3562306a36Sopenharmony_ci * Userspace interface buffer layout:
3662306a36Sopenharmony_ci *
3762306a36Sopenharmony_ci * struct data {
3862306a36Sopenharmony_ci *	vector128	vr[32];
3962306a36Sopenharmony_ci *	vector128	vscr;
4062306a36Sopenharmony_ci *	vector128	vrsave;
4162306a36Sopenharmony_ci * };
4262306a36Sopenharmony_ci */
4362306a36Sopenharmony_ciint vr_get(struct task_struct *target, const struct user_regset *regset,
4462306a36Sopenharmony_ci	   struct membuf to)
4562306a36Sopenharmony_ci{
4662306a36Sopenharmony_ci	union {
4762306a36Sopenharmony_ci		elf_vrreg_t reg;
4862306a36Sopenharmony_ci		u32 word;
4962306a36Sopenharmony_ci	} vrsave;
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	flush_altivec_to_thread(target);
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	BUILD_BUG_ON(offsetof(struct thread_vr_state, vscr) !=
5462306a36Sopenharmony_ci		     offsetof(struct thread_vr_state, vr[32]));
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	membuf_write(&to, &target->thread.vr_state, 33 * sizeof(vector128));
5762306a36Sopenharmony_ci	/*
5862306a36Sopenharmony_ci	 * Copy out only the low-order word of vrsave.
5962306a36Sopenharmony_ci	 */
6062306a36Sopenharmony_ci	memset(&vrsave, 0, sizeof(vrsave));
6162306a36Sopenharmony_ci	vrsave.word = target->thread.vrsave;
6262306a36Sopenharmony_ci	return membuf_write(&to, &vrsave, sizeof(vrsave));
6362306a36Sopenharmony_ci}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci/*
6662306a36Sopenharmony_ci * Regardless of transactions, 'vr_state' holds the current running
6762306a36Sopenharmony_ci * value of all the VMX registers and 'ckvr_state' holds the last
6862306a36Sopenharmony_ci * checkpointed value of all the VMX registers for the current
6962306a36Sopenharmony_ci * transaction to fall back on in case it aborts.
7062306a36Sopenharmony_ci *
7162306a36Sopenharmony_ci * Userspace interface buffer layout:
7262306a36Sopenharmony_ci *
7362306a36Sopenharmony_ci * struct data {
7462306a36Sopenharmony_ci *	vector128	vr[32];
7562306a36Sopenharmony_ci *	vector128	vscr;
7662306a36Sopenharmony_ci *	vector128	vrsave;
7762306a36Sopenharmony_ci * };
7862306a36Sopenharmony_ci */
7962306a36Sopenharmony_ciint vr_set(struct task_struct *target, const struct user_regset *regset,
8062306a36Sopenharmony_ci	   unsigned int pos, unsigned int count,
8162306a36Sopenharmony_ci	   const void *kbuf, const void __user *ubuf)
8262306a36Sopenharmony_ci{
8362306a36Sopenharmony_ci	int ret;
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	flush_altivec_to_thread(target);
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	BUILD_BUG_ON(offsetof(struct thread_vr_state, vscr) !=
8862306a36Sopenharmony_ci		     offsetof(struct thread_vr_state, vr[32]));
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
9162306a36Sopenharmony_ci				 &target->thread.vr_state, 0,
9262306a36Sopenharmony_ci				 33 * sizeof(vector128));
9362306a36Sopenharmony_ci	if (!ret && count > 0) {
9462306a36Sopenharmony_ci		/*
9562306a36Sopenharmony_ci		 * We use only the first word of vrsave.
9662306a36Sopenharmony_ci		 */
9762306a36Sopenharmony_ci		int start, end;
9862306a36Sopenharmony_ci		union {
9962306a36Sopenharmony_ci			elf_vrreg_t reg;
10062306a36Sopenharmony_ci			u32 word;
10162306a36Sopenharmony_ci		} vrsave;
10262306a36Sopenharmony_ci		memset(&vrsave, 0, sizeof(vrsave));
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci		vrsave.word = target->thread.vrsave;
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci		start = 33 * sizeof(vector128);
10762306a36Sopenharmony_ci		end = start + sizeof(vrsave);
10862306a36Sopenharmony_ci		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &vrsave,
10962306a36Sopenharmony_ci					 start, end);
11062306a36Sopenharmony_ci		if (!ret)
11162306a36Sopenharmony_ci			target->thread.vrsave = vrsave.word;
11262306a36Sopenharmony_ci	}
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	return ret;
11562306a36Sopenharmony_ci}
116