linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Anshuman Khandual <khandual@linux.vnet.ibm.com>
To: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org
Cc: mikey@neuling.org, shuahkh@osg.samsung.com, anton@samba.org
Subject: [PATCH V2 01/12] powerpc: Fix handling of DSCR related facility unavailable exception
Date: Tue, 13 Jan 2015 15:52:29 +0530	[thread overview]
Message-ID: <1421144560-15901-2-git-send-email-khandual@linux.vnet.ibm.com> (raw)
In-Reply-To: <1421144560-15901-1-git-send-email-khandual@linux.vnet.ibm.com>

Currently DSCR (Data Stream Control Register) can be accessed with
mfspr or mtspr instructions inside a thread via two different SPR
numbers. One being the user accessible problem state SPR number 0x03
and the other being the privilege state SPR number 0x11. All access
through the privilege state SPR number get emulated through illegal
instruction exception. Any access through the problem state SPR number
raises one facility unavailable exception which sets the thread based
dscr_inherit bit and enables DSCR facility through FSCR register thus
allowing direct access to DSCR without going through this exception in
the future. We set the thread.dscr_inherit bit whether the access was
with mfspr or mtspr instruction which is neither correct nor does it
match the behaviour through the instruction emulation code path driven
from privilege state SPR number. User currently observes two different
kind of behaviour when accessing the DSCR through these two SPR numbers.
This problem can be observed through these two test cases by replacing
the privilege state SPR number with the problem state SPR number.

	(1) http://ozlabs.org/~anton/junkcode/dscr_default_test.c
	(2) http://ozlabs.org/~anton/junkcode/dscr_explicit_test.c

This patch fixes the problem by making sure that the behaviour visible
to the user remains the same irrespective of which SPR number is being
used. Inside facility unavailable exception, we check whether it was
cuased by a mfspr or a mtspr isntrucction. In case of mfspr instruction,
just emulate the instruction. In case of mtspr instruction, set the
thread based dscr_inherit bit and also enable the facility through FSCR.
All user SPR based mfspr instruction will be emulated till one user SPR
based mtspr has been executed.

Signed-off-by: Anshuman Khandual <khandual@linux.vnet.ibm.com>
---
 arch/powerpc/kernel/traps.c | 45 ++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 40 insertions(+), 5 deletions(-)

diff --git a/arch/powerpc/kernel/traps.c b/arch/powerpc/kernel/traps.c
index e6595b7..cd69340 100644
--- a/arch/powerpc/kernel/traps.c
+++ b/arch/powerpc/kernel/traps.c
@@ -1377,6 +1377,7 @@ void facility_unavailable_exception(struct pt_regs *regs)
 	};
 	char *facility = "unknown";
 	u64 value;
+	u32 instword, rd;
 	u8 status;
 	bool hv;
 
@@ -1388,12 +1389,46 @@ void facility_unavailable_exception(struct pt_regs *regs)
 
 	status = value >> 56;
 	if (status == FSCR_DSCR_LG) {
-		/* User is acessing the DSCR.  Set the inherit bit and allow
-		 * the user to set it directly in future by setting via the
-		 * FSCR DSCR bit.  We always leave HFSCR DSCR set.
+		/*
+		 * User is accessing the DSCR register using the problem
+		 * state only SPR number (0x03) either through a mfspr or
+		 * a mtspr instruction. If it is a write attempt through
+		 * a mtspr, then we set the inherit bit. This also allows
+		 * the user to write or read the register directly in the
+		 * future by setting via the FSCR DSCR bit. But in case it
+		 * is a read DSCR attempt through a mfspr instruction, we
+		 * just emulate the instruction instead. This code path will
+		 * always emulate all the mfspr instructions till the user
+		 * has attempted atleast one mtspr instruction. This way it
+		 * preserves the same behaviour when the user is accessing
+		 * the DSCR through privilege level only SPR number (0x11)
+		 * which is emulated through illegal instruction exception.
+		 * We always leave HFSCR DSCR set.
 		 */
-		current->thread.dscr_inherit = 1;
-		mtspr(SPRN_FSCR, value | FSCR_DSCR);
+		if (get_user(instword, (u32 __user *)(regs->nip))) {
+			pr_err("Failed to fetch the user instruction\n");
+			return;
+		}
+
+		/* Write into DSCR (mtspr 0x03, RS) */
+		if ((instword & PPC_INST_MTSPR_DSCR_USER_MASK)
+				== PPC_INST_MTSPR_DSCR_USER) {
+			rd = (instword >> 21) & 0x1f;
+			current->thread.dscr = regs->gpr[rd];
+			current->thread.dscr_inherit = 1;
+			mtspr(SPRN_FSCR, value | FSCR_DSCR);
+		}
+
+		/* Read from DSCR (mfspr RT, 0x03) */
+		if ((instword & PPC_INST_MFSPR_DSCR_USER_MASK)
+				== PPC_INST_MFSPR_DSCR_USER) {
+			if (emulate_instruction(regs)) {
+				pr_err("DSCR based mfspr emulation failed\n");
+				return;
+			}
+			regs->nip += 4;
+			emulate_single_step(regs);
+		}
 		return;
 	}
 
-- 
1.9.3

  reply	other threads:[~2015-01-13 10:22 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-13 10:22 [PATCH V2 00/12] POWER DSCR fixes, improvements, docs and tests Anshuman Khandual
2015-01-13 10:22 ` Anshuman Khandual [this message]
2015-01-13 10:22 ` [PATCH V2 02/12] powerpc, process: Remove the unused extern dscr_default Anshuman Khandual
2015-01-13 10:22 ` [PATCH V2 03/12] powerpc, offset: Change PACA_DSCR to PACA_DSCR_DEFAULT Anshuman Khandual
2015-01-13 10:22 ` [PATCH V2 04/12] powerpc, dscr: Added some in-code documentation Anshuman Khandual
2015-01-13 10:22 ` [PATCH V2 05/12] documentation, powerpc: Add documentation for DSCR support Anshuman Khandual
2015-01-13 10:22 ` [PATCH V2 06/12] selftests, powerpc: Add test for system wide DSCR default Anshuman Khandual
2015-01-13 15:22   ` Shuah Khan
2015-01-13 23:44     ` Michael Ellerman
2015-01-20 21:40       ` Dave Jones
2015-01-21  6:51         ` Michael Ellerman
2015-03-27 12:01   ` [V2, " Michael Ellerman
2015-04-09 10:31     ` Anshuman Khandual
2015-04-09 12:38       ` Anshuman Khandual
2015-01-13 10:22 ` [PATCH V2 07/12] selftests, powerpc: Add test for explicitly changing DSCR value Anshuman Khandual
2015-01-13 15:23   ` Shuah Khan
2015-01-13 10:22 ` [PATCH V2 08/12] selftests, powerpc: Add test for DSCR SPR numbers Anshuman Khandual
2015-01-13 15:23   ` Shuah Khan
2015-01-13 10:22 ` [PATCH V2 09/12] selftests, powerpc: Add test for DSCR value inheritence across fork Anshuman Khandual
2015-01-13 15:24   ` Shuah Khan
2015-01-13 10:22 ` [PATCH V2 10/12] selftests, powerpc: Add test for DSCR inheritence across fork & exec Anshuman Khandual
2015-01-13 15:24   ` Shuah Khan
2015-01-13 10:22 ` [PATCH V2 11/12] selftests, powerpc: Add test for all DSCR sysfs interfaces Anshuman Khandual
2015-01-13 15:24   ` Shuah Khan
2015-01-13 10:22 ` [PATCH V2 12/12] selftests, powerpc: Add thread based stress test for " Anshuman Khandual
2015-01-13 15:24   ` Shuah Khan
2015-02-04  8:21 ` [PATCH V2 00/12] POWER DSCR fixes, improvements, docs and tests Anshuman Khandual
2015-02-04  8:36   ` Anshuman Khandual

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1421144560-15901-2-git-send-email-khandual@linux.vnet.ibm.com \
    --to=khandual@linux.vnet.ibm.com \
    --cc=anton@samba.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=mikey@neuling.org \
    --cc=shuahkh@osg.samsung.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).