From: Andi Kleen <andi@firstfloor.org>
To: x86@kernel.org
Cc: luto@amacapital.net, linux-kernel@vger.kernel.org,
Andi Kleen <ak@linux.intel.com>
Subject: [PATCH 8/9] x86: Support arbitrary fs/gs base in getregs
Date: Mon, 21 Mar 2016 09:16:08 -0700 [thread overview]
Message-ID: <1458576969-13309-9-git-send-email-andi@firstfloor.org> (raw)
In-Reply-To: <1458576969-13309-1-git-send-email-andi@firstfloor.org>
From: Andi Kleen <ak@linux.intel.com>
The ptrace code for fs/gs base made some assumptions on
the state of fs/gs which are not true anymore on kernels
running with FSGSBASE.
With the new instructions it is very easy to access
the values, and they are always stored in the thread
struct. So just implement the straight forward code
to access it directly.
Note the direct access code path is only used for core dumps,
as with real ptrace the process is always blocked
and the state can be read from memory.
Signed-off-by: Andi Kleen <ak@linux.intel.com>
---
arch/x86/include/asm/proto.h | 1 +
arch/x86/kernel/process_64.c | 15 +++++++++++++--
arch/x86/kernel/ptrace.c | 15 ++++++++++++++-
3 files changed, 28 insertions(+), 3 deletions(-)
diff --git a/arch/x86/include/asm/proto.h b/arch/x86/include/asm/proto.h
index 9b9b30b..9f235e0 100644
--- a/arch/x86/include/asm/proto.h
+++ b/arch/x86/include/asm/proto.h
@@ -31,5 +31,6 @@ void x86_report_nx(void);
extern int reboot_force;
long do_arch_prctl(struct task_struct *task, int code, unsigned long addr);
+unsigned long read_user_gsbase(void);
#endif /* _ASM_X86_PROTO_H */
diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index 5f40517..d7674d9 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -272,7 +272,7 @@ static noinline __kprobes void switch_gs_base(unsigned long gs)
}
/* Interrupts are disabled here. */
-static noinline __kprobes unsigned long read_user_gsbase(void)
+static noinline __kprobes unsigned long __read_user_gsbase(void)
{
unsigned long gs;
@@ -282,6 +282,17 @@ static noinline __kprobes unsigned long read_user_gsbase(void)
return gs;
}
+unsigned long read_user_gsbase(void)
+{
+ unsigned long flags;
+ unsigned long gs;
+
+ local_irq_save(flags);
+ gs = __read_user_gsbase();
+ local_irq_restore(flags);
+ return gs;
+}
+
/*
* switch_to(x,y) should switch tasks from x to y.
*
@@ -315,7 +326,7 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
savesegment(gs, gsindex);
if (static_cpu_has(X86_FEATURE_FSGSBASE)) {
prev->fs = rdfsbase();
- prev->gs = read_user_gsbase();
+ prev->gs = __read_user_gsbase();
}
/*
diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
index 32e9d9c..b68b15b 100644
--- a/arch/x86/kernel/ptrace.c
+++ b/arch/x86/kernel/ptrace.c
@@ -38,6 +38,7 @@
#include <asm/hw_breakpoint.h>
#include <asm/traps.h>
#include <asm/syscall.h>
+#include <asm/fsgs.h>
#include "tls.h"
@@ -452,12 +453,18 @@ static unsigned long getreg(struct task_struct *task, unsigned long offset)
#ifdef CONFIG_X86_64
case offsetof(struct user_regs_struct, fs_base): {
+ unsigned int seg = task->thread.fsindex;
+ if (boot_cpu_has(X86_FEATURE_FSGSBASE)) {
+ if (task == current)
+ return rdfsbase();
+ else
+ return task->thread.fs;
+ }
/*
* do_arch_prctl may have used a GDT slot instead of
* the MSR. To userland, it appears the same either
* way, except the %fs segment selector might not be 0.
*/
- unsigned int seg = task->thread.fsindex;
if (task->thread.fs != 0)
return task->thread.fs;
if (task == current)
@@ -471,6 +478,12 @@ static unsigned long getreg(struct task_struct *task, unsigned long offset)
* Exactly the same here as the %fs handling above.
*/
unsigned int seg = task->thread.gsindex;
+ if (boot_cpu_has(X86_FEATURE_FSGSBASE)) {
+ if (task == current)
+ return read_user_gsbase();
+ else
+ return task->thread.gs;
+ }
if (task->thread.gs != 0)
return task->thread.gs;
if (task == current)
--
2.5.5
next prev parent reply other threads:[~2016-03-21 16:18 UTC|newest]
Thread overview: 39+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-21 16:16 Updated version of RD/WR FS/GS BASE patchkit Andi Kleen
2016-03-21 16:16 ` [PATCH 1/9] x86: Add intrinsics/macros for new rd/wr fs/gs base instructions Andi Kleen
2016-03-21 18:14 ` Andy Lutomirski
2016-03-21 16:16 ` [PATCH 2/9] x86: Add support for rd/wr fs/gs base Andi Kleen
2016-03-21 18:13 ` Andy Lutomirski
2016-03-21 19:05 ` Andi Kleen
2016-03-21 19:22 ` Andy Lutomirski
2016-03-21 22:05 ` Andi Kleen
2016-03-21 22:08 ` Andy Lutomirski
2016-03-21 22:15 ` Andi Kleen
2016-03-22 8:36 ` Thomas Gleixner
2016-03-22 14:40 ` Brian Gerst
2016-04-15 0:06 ` Andy Lutomirski
2016-03-21 16:16 ` [PATCH 3/9] x86: Make old K8 swapgs workaround conditional Andi Kleen
2016-03-21 16:16 ` [PATCH 4/9] x86: Enumerate kernel FSGS capability in AT_HWCAP2 Andi Kleen
2016-03-21 18:49 ` Brian Gerst
2016-03-21 18:54 ` Andi Kleen
2016-03-21 19:32 ` Brian Gerst
2016-03-21 19:43 ` Andi Kleen
2016-03-21 22:10 ` Andy Lutomirski
2016-03-21 16:16 ` [PATCH 5/9] x86: Add documentation for rd/wr fs/gs base Andi Kleen
2016-03-23 19:14 ` Valdis.Kletnieks
2016-03-21 16:16 ` [PATCH 6/9] x86: Use rd/wr fs/gs base in arch_prctl Andi Kleen
2016-03-21 18:17 ` Andy Lutomirski
2016-03-21 16:16 ` [PATCH 7/9] x86: Add self test code for fsgsbase Andi Kleen
2016-03-21 16:16 ` Andi Kleen [this message]
2016-03-21 16:16 ` [PATCH 9/9] x86: Save FS/GS base in core dump Andi Kleen
2016-03-21 18:39 ` Updated version of RD/WR FS/GS BASE patchkit Andy Lutomirski
2016-03-21 19:03 ` Andi Kleen
2016-03-21 19:23 ` Andy Lutomirski
2016-03-21 19:40 ` Andi Kleen
2016-03-21 22:05 ` Andy Lutomirski
2016-03-21 22:11 ` Andi Kleen
2016-03-21 22:27 ` Andy Lutomirski
2016-03-21 22:41 ` Andi Kleen
2016-03-21 22:47 ` Andy Lutomirski
2016-03-21 22:52 ` Andi Kleen
2016-03-21 22:57 ` Andy Lutomirski
2016-03-21 23:02 ` Andi Kleen
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=1458576969-13309-9-git-send-email-andi@firstfloor.org \
--to=andi@firstfloor.org \
--cc=ak@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@amacapital.net \
--cc=x86@kernel.org \
/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