aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/kernel/process_64.c
diff options
context:
space:
mode:
authorErik Bosman <ejbosman@cs.vu.nl>2008-04-13 18:24:18 -0400
committerIngo Molnar <mingo@elte.hu>2008-04-19 13:19:55 -0400
commit529e25f646e08901a6dad5768f681efffd77225e (patch)
treea284d6511b6eef0ab83a3e9ada7a6ae300a33010 /arch/x86/kernel/process_64.c
parent8fb402bccf203ecca8f9e0202b8fd3c937dece6f (diff)
x86: implement prctl PR_GET_TSC and PR_SET_TSC
This patch implements the PR_GET_TSC and PR_SET_TSC prctl() commands on the x86 platform (both 32 and 64 bit.) These commands control the ability to read the timestamp counter from userspace (the RDTSC instruction.) While the RDTSC instuction is a useful profiling tool, it is also the source of some non-determinism in ring-3. For deterministic replay applications it is useful to be able to trap and emulate (and record the outcome of) this instruction. This patch uses code earlier used to disable the timestamp counter for the SECCOMP framework. A side-effect of this patch is that the SECCOMP environment will now also disable the timestamp counter on x86_64 due to the addition of the TIF_NOTSC define on this platform. The code which enables/disables the RDTSC instruction during context switches is in the __switch_to_xtra function, which already handles other unusual conditions, so normal performance should not have to suffer from this change. Signed-off-by: Erik Bosman <ejbosman@cs.vu.nl> Acked-by: Arjan van de Ven <arjan@linux.intel.com> Signed-off-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Diffstat (limited to 'arch/x86/kernel/process_64.c')
-rw-r--r--arch/x86/kernel/process_64.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c
index e75ccc8a2b87..4c13b1406c70 100644
--- a/arch/x86/kernel/process_64.c
+++ b/arch/x86/kernel/process_64.c
@@ -36,6 +36,7 @@
36#include <linux/kprobes.h> 36#include <linux/kprobes.h>
37#include <linux/kdebug.h> 37#include <linux/kdebug.h>
38#include <linux/tick.h> 38#include <linux/tick.h>
39#include <linux/prctl.h>
39 40
40#include <asm/uaccess.h> 41#include <asm/uaccess.h>
41#include <asm/pgtable.h> 42#include <asm/pgtable.h>
@@ -535,6 +536,64 @@ start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
535} 536}
536EXPORT_SYMBOL_GPL(start_thread); 537EXPORT_SYMBOL_GPL(start_thread);
537 538
539static void hard_disable_TSC(void)
540{
541 write_cr4(read_cr4() | X86_CR4_TSD);
542}
543
544void disable_TSC(void)
545{
546 preempt_disable();
547 if (!test_and_set_thread_flag(TIF_NOTSC))
548 /*
549 * Must flip the CPU state synchronously with
550 * TIF_NOTSC in the current running context.
551 */
552 hard_disable_TSC();
553 preempt_enable();
554}
555
556static void hard_enable_TSC(void)
557{
558 write_cr4(read_cr4() & ~X86_CR4_TSD);
559}
560
561void enable_TSC(void)
562{
563 preempt_disable();
564 if (test_and_clear_thread_flag(TIF_NOTSC))
565 /*
566 * Must flip the CPU state synchronously with
567 * TIF_NOTSC in the current running context.
568 */
569 hard_enable_TSC();
570 preempt_enable();
571}
572
573int get_tsc_mode(unsigned long adr)
574{
575 unsigned int val;
576
577 if (test_thread_flag(TIF_NOTSC))
578 val = PR_TSC_SIGSEGV;
579 else
580 val = PR_TSC_ENABLE;
581
582 return put_user(val, (unsigned int __user *)adr);
583}
584
585int set_tsc_mode(unsigned int val)
586{
587 if (val == PR_TSC_SIGSEGV)
588 disable_TSC();
589 else if (val == PR_TSC_ENABLE)
590 enable_TSC();
591 else
592 return -EINVAL;
593
594 return 0;
595}
596
538/* 597/*
539 * This special macro can be used to load a debugging register 598 * This special macro can be used to load a debugging register
540 */ 599 */
@@ -572,6 +631,15 @@ static inline void __switch_to_xtra(struct task_struct *prev_p,
572 loaddebug(next, 7); 631 loaddebug(next, 7);
573 } 632 }
574 633
634 if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
635 test_tsk_thread_flag(next_p, TIF_NOTSC)) {
636 /* prev and next are different */
637 if (test_tsk_thread_flag(next_p, TIF_NOTSC))
638 hard_disable_TSC();
639 else
640 hard_enable_TSC();
641 }
642
575 if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) { 643 if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
576 /* 644 /*
577 * Copy the relevant range of the IO bitmap. 645 * Copy the relevant range of the IO bitmap.