162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2016 Imagination Technologies
462306a36Sopenharmony_ci * Author: Paul Burton <paul.burton@mips.com>
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#ifndef __MIPS_ASM_DSEMUL_H__
862306a36Sopenharmony_ci#define __MIPS_ASM_DSEMUL_H__
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <asm/break.h>
1162306a36Sopenharmony_ci#include <asm/inst.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci/* Break instruction with special math emu break code set */
1462306a36Sopenharmony_ci#define BREAK_MATH(micromips)	(((micromips) ? 0x7 : 0xd) | (BRK_MEMU << 16))
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/* When used as a frame index, indicates the lack of a frame */
1762306a36Sopenharmony_ci#define BD_EMUFRAME_NONE	((int)BIT(31))
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistruct mm_struct;
2062306a36Sopenharmony_cistruct pt_regs;
2162306a36Sopenharmony_cistruct task_struct;
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/**
2462306a36Sopenharmony_ci * mips_dsemul() - 'Emulate' an instruction from a branch delay slot
2562306a36Sopenharmony_ci * @regs:	User thread register context.
2662306a36Sopenharmony_ci * @ir:		The instruction to be 'emulated'.
2762306a36Sopenharmony_ci * @branch_pc:	The PC of the branch instruction.
2862306a36Sopenharmony_ci * @cont_pc:	The PC to continue at following 'emulation'.
2962306a36Sopenharmony_ci *
3062306a36Sopenharmony_ci * Emulate or execute an arbitrary MIPS instruction within the context of
3162306a36Sopenharmony_ci * the current user thread. This is used primarily to handle instructions
3262306a36Sopenharmony_ci * in the delay slots of emulated branch instructions, for example FP
3362306a36Sopenharmony_ci * branch instructions on systems without an FPU.
3462306a36Sopenharmony_ci *
3562306a36Sopenharmony_ci * Return: Zero on success, negative if ir is a NOP, signal number on failure.
3662306a36Sopenharmony_ci */
3762306a36Sopenharmony_ciextern int mips_dsemul(struct pt_regs *regs, mips_instruction ir,
3862306a36Sopenharmony_ci		       unsigned long branch_pc, unsigned long cont_pc);
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci/**
4162306a36Sopenharmony_ci * do_dsemulret() - Return from a delay slot 'emulation' frame
4262306a36Sopenharmony_ci * @xcp:	User thread register context.
4362306a36Sopenharmony_ci *
4462306a36Sopenharmony_ci * Call in response to the BRK_MEMU break instruction used to return to
4562306a36Sopenharmony_ci * the kernel from branch delay slot 'emulation' frames following a call
4662306a36Sopenharmony_ci * to mips_dsemul(). Restores the user thread PC to the value that was
4762306a36Sopenharmony_ci * passed as the cpc parameter to mips_dsemul().
4862306a36Sopenharmony_ci *
4962306a36Sopenharmony_ci * Return: True if an emulation frame was returned from, else false.
5062306a36Sopenharmony_ci */
5162306a36Sopenharmony_ci#ifdef CONFIG_MIPS_FP_SUPPORT
5262306a36Sopenharmony_ciextern bool do_dsemulret(struct pt_regs *xcp);
5362306a36Sopenharmony_ci#else
5462306a36Sopenharmony_cistatic inline bool do_dsemulret(struct pt_regs *xcp)
5562306a36Sopenharmony_ci{
5662306a36Sopenharmony_ci	return false;
5762306a36Sopenharmony_ci}
5862306a36Sopenharmony_ci#endif
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci/**
6162306a36Sopenharmony_ci * dsemul_thread_cleanup() - Cleanup thread 'emulation' frame
6262306a36Sopenharmony_ci * @tsk: The task structure associated with the thread
6362306a36Sopenharmony_ci *
6462306a36Sopenharmony_ci * If the thread @tsk has a branch delay slot 'emulation' frame
6562306a36Sopenharmony_ci * allocated to it then free that frame.
6662306a36Sopenharmony_ci *
6762306a36Sopenharmony_ci * Return: True if a frame was freed, else false.
6862306a36Sopenharmony_ci */
6962306a36Sopenharmony_ci#ifdef CONFIG_MIPS_FP_SUPPORT
7062306a36Sopenharmony_ciextern bool dsemul_thread_cleanup(struct task_struct *tsk);
7162306a36Sopenharmony_ci#else
7262306a36Sopenharmony_cistatic inline bool dsemul_thread_cleanup(struct task_struct *tsk)
7362306a36Sopenharmony_ci{
7462306a36Sopenharmony_ci	return false;
7562306a36Sopenharmony_ci}
7662306a36Sopenharmony_ci#endif
7762306a36Sopenharmony_ci/**
7862306a36Sopenharmony_ci * dsemul_thread_rollback() - Rollback from an 'emulation' frame
7962306a36Sopenharmony_ci * @regs:	User thread register context.
8062306a36Sopenharmony_ci *
8162306a36Sopenharmony_ci * If the current thread, whose register context is represented by @regs,
8262306a36Sopenharmony_ci * is executing within a delay slot 'emulation' frame then exit that
8362306a36Sopenharmony_ci * frame. The PC will be rolled back to the branch if the instruction
8462306a36Sopenharmony_ci * that was being 'emulated' has not yet executed, or advanced to the
8562306a36Sopenharmony_ci * continuation PC if it has.
8662306a36Sopenharmony_ci *
8762306a36Sopenharmony_ci * Return: True if a frame was exited, else false.
8862306a36Sopenharmony_ci */
8962306a36Sopenharmony_ci#ifdef CONFIG_MIPS_FP_SUPPORT
9062306a36Sopenharmony_ciextern bool dsemul_thread_rollback(struct pt_regs *regs);
9162306a36Sopenharmony_ci#else
9262306a36Sopenharmony_cistatic inline bool dsemul_thread_rollback(struct pt_regs *regs)
9362306a36Sopenharmony_ci{
9462306a36Sopenharmony_ci	return false;
9562306a36Sopenharmony_ci}
9662306a36Sopenharmony_ci#endif
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci/**
9962306a36Sopenharmony_ci * dsemul_mm_cleanup() - Cleanup per-mm delay slot 'emulation' state
10062306a36Sopenharmony_ci * @mm:		The struct mm_struct to cleanup state for.
10162306a36Sopenharmony_ci *
10262306a36Sopenharmony_ci * Cleanup state for the given @mm, ensuring that any memory allocated
10362306a36Sopenharmony_ci * for delay slot 'emulation' book-keeping is freed. This is to be called
10462306a36Sopenharmony_ci * before @mm is freed in order to avoid memory leaks.
10562306a36Sopenharmony_ci */
10662306a36Sopenharmony_ci#ifdef CONFIG_MIPS_FP_SUPPORT
10762306a36Sopenharmony_ciextern void dsemul_mm_cleanup(struct mm_struct *mm);
10862306a36Sopenharmony_ci#else
10962306a36Sopenharmony_cistatic inline void dsemul_mm_cleanup(struct mm_struct *mm)
11062306a36Sopenharmony_ci{
11162306a36Sopenharmony_ci	/* no-op */
11262306a36Sopenharmony_ci}
11362306a36Sopenharmony_ci#endif
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci#endif /* __MIPS_ASM_DSEMUL_H__ */
116