2005-09-26 14:04:21 +08:00
|
|
|
/*
|
|
|
|
* Derived from "arch/i386/kernel/process.c"
|
|
|
|
* Copyright (C) 1995 Linus Torvalds
|
|
|
|
*
|
|
|
|
* Updated and modified by Cort Dougan (cort@cs.nmt.edu) and
|
|
|
|
* Paul Mackerras (paulus@cs.anu.edu.au)
|
|
|
|
*
|
|
|
|
* PowerPC version
|
|
|
|
* Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/smp.h>
|
|
|
|
#include <linux/stddef.h>
|
|
|
|
#include <linux/unistd.h>
|
|
|
|
#include <linux/ptrace.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/user.h>
|
|
|
|
#include <linux/elf.h>
|
|
|
|
#include <linux/prctl.h>
|
|
|
|
#include <linux/init_task.h>
|
2011-07-23 06:24:23 +08:00
|
|
|
#include <linux/export.h>
|
2005-09-26 14:04:21 +08:00
|
|
|
#include <linux/kallsyms.h>
|
|
|
|
#include <linux/mqueue.h>
|
|
|
|
#include <linux/hardirq.h>
|
2005-10-10 20:29:05 +08:00
|
|
|
#include <linux/utsname.h>
|
2009-02-10 13:10:27 +08:00
|
|
|
#include <linux/ftrace.h>
|
2008-12-31 22:11:38 +08:00
|
|
|
#include <linux/kernel_stat.h>
|
2009-02-22 09:50:03 +08:00
|
|
|
#include <linux/personality.h>
|
|
|
|
#include <linux/random.h>
|
2010-06-15 14:05:19 +08:00
|
|
|
#include <linux/hw_breakpoint.h>
|
2014-10-13 17:27:15 +08:00
|
|
|
#include <linux/uaccess.h>
|
2005-09-26 14:04:21 +08:00
|
|
|
|
|
|
|
#include <asm/pgtable.h>
|
|
|
|
#include <asm/io.h>
|
|
|
|
#include <asm/processor.h>
|
|
|
|
#include <asm/mmu.h>
|
|
|
|
#include <asm/prom.h>
|
2005-11-07 10:12:03 +08:00
|
|
|
#include <asm/machdep.h>
|
powerpc: Implement accurate task and CPU time accounting
This implements accurate task and cpu time accounting for 64-bit
powerpc kernels. Instead of accounting a whole jiffy of time to a
task on a timer interrupt because that task happened to be running at
the time, we now account time in units of timebase ticks according to
the actual time spent by the task in user mode and kernel mode. We
also count the time spent processing hardware and software interrupts
accurately. This is conditional on CONFIG_VIRT_CPU_ACCOUNTING. If
that is not set, we do tick-based approximate accounting as before.
To get this accurate information, we read either the PURR (processor
utilization of resources register) on POWER5 machines, or the timebase
on other machines on
* each entry to the kernel from usermode
* each exit to usermode
* transitions between process context, hard irq context and soft irq
context in kernel mode
* context switches.
On POWER5 systems with shared-processor logical partitioning we also
read both the PURR and the timebase at each timer interrupt and
context switch in order to determine how much time has been taken by
the hypervisor to run other partitions ("steal" time). Unfortunately,
since we need values of the PURR on both threads at the same time to
accurately calculate the steal time, and since we can only calculate
steal time on a per-core basis, the apportioning of the steal time
between idle time (time which we ceded to the hypervisor in the idle
loop) and actual stolen time is somewhat approximate at the moment.
This is all based quite heavily on what s390 does, and it uses the
generic interfaces that were added by the s390 developers,
i.e. account_system_time(), account_user_time(), etc.
This patch doesn't add any new interfaces between the kernel and
userspace, and doesn't change the units in which time is reported to
userspace by things such as /proc/stat, /proc/<pid>/stat, getrusage(),
times(), etc. Internally the various task and cpu times are stored in
timebase units, but they are converted to USER_HZ units (1/100th of a
second) when reported to userspace. Some precision is therefore lost
but there should not be any accumulating error, since the internal
accumulation is at full precision.
Signed-off-by: Paul Mackerras <paulus@samba.org>
2006-02-24 07:06:59 +08:00
|
|
|
#include <asm/time.h>
|
2012-03-29 01:30:02 +08:00
|
|
|
#include <asm/runlatch.h>
|
2006-03-23 07:00:08 +08:00
|
|
|
#include <asm/syscalls.h>
|
2012-03-29 01:30:02 +08:00
|
|
|
#include <asm/switch_to.h>
|
2013-02-14 00:21:37 +08:00
|
|
|
#include <asm/tm.h>
|
2012-03-29 01:30:02 +08:00
|
|
|
#include <asm/debug.h>
|
2005-10-10 20:29:05 +08:00
|
|
|
#ifdef CONFIG_PPC64
|
|
|
|
#include <asm/firmware.h>
|
|
|
|
#endif
|
2014-02-04 13:08:51 +08:00
|
|
|
#include <asm/code-patching.h>
|
2008-07-24 00:10:41 +08:00
|
|
|
#include <linux/kprobes.h>
|
|
|
|
#include <linux/kdebug.h>
|
2005-09-26 14:04:21 +08:00
|
|
|
|
2013-02-14 00:21:32 +08:00
|
|
|
/* Transactional Memory debug */
|
|
|
|
#ifdef TM_DEBUG_SW
|
|
|
|
#define TM_DEBUG(x...) printk(KERN_INFO x)
|
|
|
|
#else
|
|
|
|
#define TM_DEBUG(x...) do { } while(0)
|
|
|
|
#endif
|
|
|
|
|
2005-09-26 14:04:21 +08:00
|
|
|
extern unsigned long _get_SP(void);
|
|
|
|
|
|
|
|
#ifndef CONFIG_SMP
|
|
|
|
struct task_struct *last_task_used_math = NULL;
|
|
|
|
struct task_struct *last_task_used_altivec = NULL;
|
2008-06-25 12:07:18 +08:00
|
|
|
struct task_struct *last_task_used_vsx = NULL;
|
2005-09-26 14:04:21 +08:00
|
|
|
struct task_struct *last_task_used_spe = NULL;
|
|
|
|
#endif
|
|
|
|
|
powerpc: Don't corrupt transactional state when using FP/VMX in kernel
Currently, when we have a process using the transactional memory
facilities on POWER8 (that is, the processor is in transactional
or suspended state), and the process enters the kernel and the
kernel then uses the floating-point or vector (VMX/Altivec) facility,
we end up corrupting the user-visible FP/VMX/VSX state. This
happens, for example, if a page fault causes a copy-on-write
operation, because the copy_page function will use VMX to do the
copy on POWER8. The test program below demonstrates the bug.
The bug happens because when FP/VMX state for a transactional process
is stored in the thread_struct, we store the checkpointed state in
.fp_state/.vr_state and the transactional (current) state in
.transact_fp/.transact_vr. However, when the kernel wants to use
FP/VMX, it calls enable_kernel_fp() or enable_kernel_altivec(),
which saves the current state in .fp_state/.vr_state. Furthermore,
when we return to the user process we return with FP/VMX/VSX
disabled. The next time the process uses FP/VMX/VSX, we don't know
which set of state (the current register values, .fp_state/.vr_state,
or .transact_fp/.transact_vr) we should be using, since we have no
way to tell if we are still in the same transaction, and if not,
whether the previous transaction succeeded or failed.
Thus it is necessary to strictly adhere to the rule that if FP has
been enabled at any point in a transaction, we must keep FP enabled
for the user process with the current transactional state in the
FP registers, until we detect that it is no longer in a transaction.
Similarly for VMX; once enabled it must stay enabled until the
process is no longer transactional.
In order to keep this rule, we add a new thread_info flag which we
test when returning from the kernel to userspace, called TIF_RESTORE_TM.
This flag indicates that there is FP/VMX/VSX state to be restored
before entering userspace, and when it is set the .tm_orig_msr field
in the thread_struct indicates what state needs to be restored.
The restoration is done by restore_tm_state(). The TIF_RESTORE_TM
bit is set by new giveup_fpu/altivec_maybe_transactional helpers,
which are called from enable_kernel_fp/altivec, giveup_vsx, and
flush_fp/altivec_to_thread instead of giveup_fpu/altivec.
The other thing to be done is to get the transactional FP/VMX/VSX
state from .fp_state/.vr_state when doing reclaim, if that state
has been saved there by giveup_fpu/altivec_maybe_transactional.
Having done this, we set the FP/VMX bit in the thread's MSR after
reclaim to indicate that that part of the state is now valid
(having been reclaimed from the processor's checkpointed state).
Finally, in the signal handling code, we move the clearing of the
transactional state bits in the thread's MSR a bit earlier, before
calling flush_fp_to_thread(), so that we don't unnecessarily set
the TIF_RESTORE_TM bit.
This is the test program:
/* Michael Neuling 4/12/2013
*
* See if the altivec state is leaked out of an aborted transaction due to
* kernel vmx copy loops.
*
* gcc -m64 htm_vmxcopy.c -o htm_vmxcopy
*
*/
/* We don't use all of these, but for reference: */
int main(int argc, char *argv[])
{
long double vecin = 1.3;
long double vecout;
unsigned long pgsize = getpagesize();
int i;
int fd;
int size = pgsize*16;
char tmpfile[] = "/tmp/page_faultXXXXXX";
char buf[pgsize];
char *a;
uint64_t aborted = 0;
fd = mkstemp(tmpfile);
assert(fd >= 0);
memset(buf, 0, pgsize);
for (i = 0; i < size; i += pgsize)
assert(write(fd, buf, pgsize) == pgsize);
unlink(tmpfile);
a = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
assert(a != MAP_FAILED);
asm __volatile__(
"lxvd2x 40,0,%[vecinptr] ; " // set 40 to initial value
TBEGIN
"beq 3f ;"
TSUSPEND
"xxlxor 40,40,40 ; " // set 40 to 0
"std 5, 0(%[map]) ;" // cause kernel vmx copy page
TABORT
TRESUME
TEND
"li %[res], 0 ;"
"b 5f ;"
"3: ;" // Abort handler
"li %[res], 1 ;"
"5: ;"
"stxvd2x 40,0,%[vecoutptr] ; "
: [res]"=r"(aborted)
: [vecinptr]"r"(&vecin),
[vecoutptr]"r"(&vecout),
[map]"r"(a)
: "memory", "r0", "r3", "r4", "r5", "r6", "r7");
if (aborted && (vecin != vecout)){
printf("FAILED: vector state leaked on abort %f != %f\n",
(double)vecin, (double)vecout);
exit(1);
}
munmap(a, size);
close(fd);
printf("PASSED!\n");
return 0;
}
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2014-01-13 12:56:29 +08:00
|
|
|
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
|
|
|
|
void giveup_fpu_maybe_transactional(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If we are saving the current thread's registers, and the
|
|
|
|
* thread is in a transactional state, set the TIF_RESTORE_TM
|
|
|
|
* bit so that we know to restore the registers before
|
|
|
|
* returning to userspace.
|
|
|
|
*/
|
|
|
|
if (tsk == current && tsk->thread.regs &&
|
|
|
|
MSR_TM_ACTIVE(tsk->thread.regs->msr) &&
|
|
|
|
!test_thread_flag(TIF_RESTORE_TM)) {
|
|
|
|
tsk->thread.tm_orig_msr = tsk->thread.regs->msr;
|
|
|
|
set_thread_flag(TIF_RESTORE_TM);
|
|
|
|
}
|
|
|
|
|
|
|
|
giveup_fpu(tsk);
|
|
|
|
}
|
|
|
|
|
|
|
|
void giveup_altivec_maybe_transactional(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If we are saving the current thread's registers, and the
|
|
|
|
* thread is in a transactional state, set the TIF_RESTORE_TM
|
|
|
|
* bit so that we know to restore the registers before
|
|
|
|
* returning to userspace.
|
|
|
|
*/
|
|
|
|
if (tsk == current && tsk->thread.regs &&
|
|
|
|
MSR_TM_ACTIVE(tsk->thread.regs->msr) &&
|
|
|
|
!test_thread_flag(TIF_RESTORE_TM)) {
|
|
|
|
tsk->thread.tm_orig_msr = tsk->thread.regs->msr;
|
|
|
|
set_thread_flag(TIF_RESTORE_TM);
|
|
|
|
}
|
|
|
|
|
|
|
|
giveup_altivec(tsk);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#define giveup_fpu_maybe_transactional(tsk) giveup_fpu(tsk)
|
|
|
|
#define giveup_altivec_maybe_transactional(tsk) giveup_altivec(tsk)
|
|
|
|
#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
|
|
|
|
|
2013-07-14 17:02:05 +08:00
|
|
|
#ifdef CONFIG_PPC_FPU
|
2005-09-26 14:04:21 +08:00
|
|
|
/*
|
|
|
|
* Make sure the floating-point register state in the
|
|
|
|
* the thread_struct is up to date for task tsk.
|
|
|
|
*/
|
|
|
|
void flush_fp_to_thread(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
if (tsk->thread.regs) {
|
|
|
|
/*
|
|
|
|
* We need to disable preemption here because if we didn't,
|
|
|
|
* another process could get scheduled after the regs->msr
|
|
|
|
* test but before we have finished saving the FP registers
|
|
|
|
* to the thread_struct. That process could take over the
|
|
|
|
* FPU, and then when we get scheduled again we would store
|
|
|
|
* bogus values for the remaining FP registers.
|
|
|
|
*/
|
|
|
|
preempt_disable();
|
|
|
|
if (tsk->thread.regs->msr & MSR_FP) {
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
/*
|
|
|
|
* This should only ever be called for current or
|
|
|
|
* for a stopped child process. Since we save away
|
|
|
|
* the FP register state on context switch on SMP,
|
|
|
|
* there is something wrong if a stopped child appears
|
|
|
|
* to still have its FP state in the CPU registers.
|
|
|
|
*/
|
|
|
|
BUG_ON(tsk != current);
|
|
|
|
#endif
|
powerpc: Don't corrupt transactional state when using FP/VMX in kernel
Currently, when we have a process using the transactional memory
facilities on POWER8 (that is, the processor is in transactional
or suspended state), and the process enters the kernel and the
kernel then uses the floating-point or vector (VMX/Altivec) facility,
we end up corrupting the user-visible FP/VMX/VSX state. This
happens, for example, if a page fault causes a copy-on-write
operation, because the copy_page function will use VMX to do the
copy on POWER8. The test program below demonstrates the bug.
The bug happens because when FP/VMX state for a transactional process
is stored in the thread_struct, we store the checkpointed state in
.fp_state/.vr_state and the transactional (current) state in
.transact_fp/.transact_vr. However, when the kernel wants to use
FP/VMX, it calls enable_kernel_fp() or enable_kernel_altivec(),
which saves the current state in .fp_state/.vr_state. Furthermore,
when we return to the user process we return with FP/VMX/VSX
disabled. The next time the process uses FP/VMX/VSX, we don't know
which set of state (the current register values, .fp_state/.vr_state,
or .transact_fp/.transact_vr) we should be using, since we have no
way to tell if we are still in the same transaction, and if not,
whether the previous transaction succeeded or failed.
Thus it is necessary to strictly adhere to the rule that if FP has
been enabled at any point in a transaction, we must keep FP enabled
for the user process with the current transactional state in the
FP registers, until we detect that it is no longer in a transaction.
Similarly for VMX; once enabled it must stay enabled until the
process is no longer transactional.
In order to keep this rule, we add a new thread_info flag which we
test when returning from the kernel to userspace, called TIF_RESTORE_TM.
This flag indicates that there is FP/VMX/VSX state to be restored
before entering userspace, and when it is set the .tm_orig_msr field
in the thread_struct indicates what state needs to be restored.
The restoration is done by restore_tm_state(). The TIF_RESTORE_TM
bit is set by new giveup_fpu/altivec_maybe_transactional helpers,
which are called from enable_kernel_fp/altivec, giveup_vsx, and
flush_fp/altivec_to_thread instead of giveup_fpu/altivec.
The other thing to be done is to get the transactional FP/VMX/VSX
state from .fp_state/.vr_state when doing reclaim, if that state
has been saved there by giveup_fpu/altivec_maybe_transactional.
Having done this, we set the FP/VMX bit in the thread's MSR after
reclaim to indicate that that part of the state is now valid
(having been reclaimed from the processor's checkpointed state).
Finally, in the signal handling code, we move the clearing of the
transactional state bits in the thread's MSR a bit earlier, before
calling flush_fp_to_thread(), so that we don't unnecessarily set
the TIF_RESTORE_TM bit.
This is the test program:
/* Michael Neuling 4/12/2013
*
* See if the altivec state is leaked out of an aborted transaction due to
* kernel vmx copy loops.
*
* gcc -m64 htm_vmxcopy.c -o htm_vmxcopy
*
*/
/* We don't use all of these, but for reference: */
int main(int argc, char *argv[])
{
long double vecin = 1.3;
long double vecout;
unsigned long pgsize = getpagesize();
int i;
int fd;
int size = pgsize*16;
char tmpfile[] = "/tmp/page_faultXXXXXX";
char buf[pgsize];
char *a;
uint64_t aborted = 0;
fd = mkstemp(tmpfile);
assert(fd >= 0);
memset(buf, 0, pgsize);
for (i = 0; i < size; i += pgsize)
assert(write(fd, buf, pgsize) == pgsize);
unlink(tmpfile);
a = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
assert(a != MAP_FAILED);
asm __volatile__(
"lxvd2x 40,0,%[vecinptr] ; " // set 40 to initial value
TBEGIN
"beq 3f ;"
TSUSPEND
"xxlxor 40,40,40 ; " // set 40 to 0
"std 5, 0(%[map]) ;" // cause kernel vmx copy page
TABORT
TRESUME
TEND
"li %[res], 0 ;"
"b 5f ;"
"3: ;" // Abort handler
"li %[res], 1 ;"
"5: ;"
"stxvd2x 40,0,%[vecoutptr] ; "
: [res]"=r"(aborted)
: [vecinptr]"r"(&vecin),
[vecoutptr]"r"(&vecout),
[map]"r"(a)
: "memory", "r0", "r3", "r4", "r5", "r6", "r7");
if (aborted && (vecin != vecout)){
printf("FAILED: vector state leaked on abort %f != %f\n",
(double)vecin, (double)vecout);
exit(1);
}
munmap(a, size);
close(fd);
printf("PASSED!\n");
return 0;
}
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2014-01-13 12:56:29 +08:00
|
|
|
giveup_fpu_maybe_transactional(tsk);
|
2005-09-26 14:04:21 +08:00
|
|
|
}
|
|
|
|
preempt_enable();
|
|
|
|
}
|
|
|
|
}
|
KVM: PPC: Add support for Book3S processors in hypervisor mode
This adds support for KVM running on 64-bit Book 3S processors,
specifically POWER7, in hypervisor mode. Using hypervisor mode means
that the guest can use the processor's supervisor mode. That means
that the guest can execute privileged instructions and access privileged
registers itself without trapping to the host. This gives excellent
performance, but does mean that KVM cannot emulate a processor
architecture other than the one that the hardware implements.
This code assumes that the guest is running paravirtualized using the
PAPR (Power Architecture Platform Requirements) interface, which is the
interface that IBM's PowerVM hypervisor uses. That means that existing
Linux distributions that run on IBM pSeries machines will also run
under KVM without modification. In order to communicate the PAPR
hypercalls to qemu, this adds a new KVM_EXIT_PAPR_HCALL exit code
to include/linux/kvm.h.
Currently the choice between book3s_hv support and book3s_pr support
(i.e. the existing code, which runs the guest in user mode) has to be
made at kernel configuration time, so a given kernel binary can only
do one or the other.
This new book3s_hv code doesn't support MMIO emulation at present.
Since we are running paravirtualized guests, this isn't a serious
restriction.
With the guest running in supervisor mode, most exceptions go straight
to the guest. We will never get data or instruction storage or segment
interrupts, alignment interrupts, decrementer interrupts, program
interrupts, single-step interrupts, etc., coming to the hypervisor from
the guest. Therefore this introduces a new KVMTEST_NONHV macro for the
exception entry path so that we don't have to do the KVM test on entry
to those exception handlers.
We do however get hypervisor decrementer, hypervisor data storage,
hypervisor instruction storage, and hypervisor emulation assist
interrupts, so we have to handle those.
In hypervisor mode, real-mode accesses can access all of RAM, not just
a limited amount. Therefore we put all the guest state in the vcpu.arch
and use the shadow_vcpu in the PACA only for temporary scratch space.
We allocate the vcpu with kzalloc rather than vzalloc, and we don't use
anything in the kvmppc_vcpu_book3s struct, so we don't allocate it.
We don't have a shared page with the guest, but we still need a
kvm_vcpu_arch_shared struct to store the values of various registers,
so we include one in the vcpu_arch struct.
The POWER7 processor has a restriction that all threads in a core have
to be in the same partition. MMU-on kernel code counts as a partition
(partition 0), so we have to do a partition switch on every entry to and
exit from the guest. At present we require the host and guest to run
in single-thread mode because of this hardware restriction.
This code allocates a hashed page table for the guest and initializes
it with HPTEs for the guest's Virtual Real Memory Area (VRMA). We
require that the guest memory is allocated using 16MB huge pages, in
order to simplify the low-level memory management. This also means that
we can get away without tracking paging activity in the host for now,
since huge pages can't be paged or swapped.
This also adds a few new exports needed by the book3s_hv code.
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Alexander Graf <agraf@suse.de>
2011-06-29 08:21:34 +08:00
|
|
|
EXPORT_SYMBOL_GPL(flush_fp_to_thread);
|
powerpc: Don't corrupt transactional state when using FP/VMX in kernel
Currently, when we have a process using the transactional memory
facilities on POWER8 (that is, the processor is in transactional
or suspended state), and the process enters the kernel and the
kernel then uses the floating-point or vector (VMX/Altivec) facility,
we end up corrupting the user-visible FP/VMX/VSX state. This
happens, for example, if a page fault causes a copy-on-write
operation, because the copy_page function will use VMX to do the
copy on POWER8. The test program below demonstrates the bug.
The bug happens because when FP/VMX state for a transactional process
is stored in the thread_struct, we store the checkpointed state in
.fp_state/.vr_state and the transactional (current) state in
.transact_fp/.transact_vr. However, when the kernel wants to use
FP/VMX, it calls enable_kernel_fp() or enable_kernel_altivec(),
which saves the current state in .fp_state/.vr_state. Furthermore,
when we return to the user process we return with FP/VMX/VSX
disabled. The next time the process uses FP/VMX/VSX, we don't know
which set of state (the current register values, .fp_state/.vr_state,
or .transact_fp/.transact_vr) we should be using, since we have no
way to tell if we are still in the same transaction, and if not,
whether the previous transaction succeeded or failed.
Thus it is necessary to strictly adhere to the rule that if FP has
been enabled at any point in a transaction, we must keep FP enabled
for the user process with the current transactional state in the
FP registers, until we detect that it is no longer in a transaction.
Similarly for VMX; once enabled it must stay enabled until the
process is no longer transactional.
In order to keep this rule, we add a new thread_info flag which we
test when returning from the kernel to userspace, called TIF_RESTORE_TM.
This flag indicates that there is FP/VMX/VSX state to be restored
before entering userspace, and when it is set the .tm_orig_msr field
in the thread_struct indicates what state needs to be restored.
The restoration is done by restore_tm_state(). The TIF_RESTORE_TM
bit is set by new giveup_fpu/altivec_maybe_transactional helpers,
which are called from enable_kernel_fp/altivec, giveup_vsx, and
flush_fp/altivec_to_thread instead of giveup_fpu/altivec.
The other thing to be done is to get the transactional FP/VMX/VSX
state from .fp_state/.vr_state when doing reclaim, if that state
has been saved there by giveup_fpu/altivec_maybe_transactional.
Having done this, we set the FP/VMX bit in the thread's MSR after
reclaim to indicate that that part of the state is now valid
(having been reclaimed from the processor's checkpointed state).
Finally, in the signal handling code, we move the clearing of the
transactional state bits in the thread's MSR a bit earlier, before
calling flush_fp_to_thread(), so that we don't unnecessarily set
the TIF_RESTORE_TM bit.
This is the test program:
/* Michael Neuling 4/12/2013
*
* See if the altivec state is leaked out of an aborted transaction due to
* kernel vmx copy loops.
*
* gcc -m64 htm_vmxcopy.c -o htm_vmxcopy
*
*/
/* We don't use all of these, but for reference: */
int main(int argc, char *argv[])
{
long double vecin = 1.3;
long double vecout;
unsigned long pgsize = getpagesize();
int i;
int fd;
int size = pgsize*16;
char tmpfile[] = "/tmp/page_faultXXXXXX";
char buf[pgsize];
char *a;
uint64_t aborted = 0;
fd = mkstemp(tmpfile);
assert(fd >= 0);
memset(buf, 0, pgsize);
for (i = 0; i < size; i += pgsize)
assert(write(fd, buf, pgsize) == pgsize);
unlink(tmpfile);
a = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
assert(a != MAP_FAILED);
asm __volatile__(
"lxvd2x 40,0,%[vecinptr] ; " // set 40 to initial value
TBEGIN
"beq 3f ;"
TSUSPEND
"xxlxor 40,40,40 ; " // set 40 to 0
"std 5, 0(%[map]) ;" // cause kernel vmx copy page
TABORT
TRESUME
TEND
"li %[res], 0 ;"
"b 5f ;"
"3: ;" // Abort handler
"li %[res], 1 ;"
"5: ;"
"stxvd2x 40,0,%[vecoutptr] ; "
: [res]"=r"(aborted)
: [vecinptr]"r"(&vecin),
[vecoutptr]"r"(&vecout),
[map]"r"(a)
: "memory", "r0", "r3", "r4", "r5", "r6", "r7");
if (aborted && (vecin != vecout)){
printf("FAILED: vector state leaked on abort %f != %f\n",
(double)vecin, (double)vecout);
exit(1);
}
munmap(a, size);
close(fd);
printf("PASSED!\n");
return 0;
}
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2014-01-13 12:56:29 +08:00
|
|
|
#endif /* CONFIG_PPC_FPU */
|
2005-09-26 14:04:21 +08:00
|
|
|
|
|
|
|
void enable_kernel_fp(void)
|
|
|
|
{
|
|
|
|
WARN_ON(preemptible());
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
if (current->thread.regs && (current->thread.regs->msr & MSR_FP))
|
powerpc: Don't corrupt transactional state when using FP/VMX in kernel
Currently, when we have a process using the transactional memory
facilities on POWER8 (that is, the processor is in transactional
or suspended state), and the process enters the kernel and the
kernel then uses the floating-point or vector (VMX/Altivec) facility,
we end up corrupting the user-visible FP/VMX/VSX state. This
happens, for example, if a page fault causes a copy-on-write
operation, because the copy_page function will use VMX to do the
copy on POWER8. The test program below demonstrates the bug.
The bug happens because when FP/VMX state for a transactional process
is stored in the thread_struct, we store the checkpointed state in
.fp_state/.vr_state and the transactional (current) state in
.transact_fp/.transact_vr. However, when the kernel wants to use
FP/VMX, it calls enable_kernel_fp() or enable_kernel_altivec(),
which saves the current state in .fp_state/.vr_state. Furthermore,
when we return to the user process we return with FP/VMX/VSX
disabled. The next time the process uses FP/VMX/VSX, we don't know
which set of state (the current register values, .fp_state/.vr_state,
or .transact_fp/.transact_vr) we should be using, since we have no
way to tell if we are still in the same transaction, and if not,
whether the previous transaction succeeded or failed.
Thus it is necessary to strictly adhere to the rule that if FP has
been enabled at any point in a transaction, we must keep FP enabled
for the user process with the current transactional state in the
FP registers, until we detect that it is no longer in a transaction.
Similarly for VMX; once enabled it must stay enabled until the
process is no longer transactional.
In order to keep this rule, we add a new thread_info flag which we
test when returning from the kernel to userspace, called TIF_RESTORE_TM.
This flag indicates that there is FP/VMX/VSX state to be restored
before entering userspace, and when it is set the .tm_orig_msr field
in the thread_struct indicates what state needs to be restored.
The restoration is done by restore_tm_state(). The TIF_RESTORE_TM
bit is set by new giveup_fpu/altivec_maybe_transactional helpers,
which are called from enable_kernel_fp/altivec, giveup_vsx, and
flush_fp/altivec_to_thread instead of giveup_fpu/altivec.
The other thing to be done is to get the transactional FP/VMX/VSX
state from .fp_state/.vr_state when doing reclaim, if that state
has been saved there by giveup_fpu/altivec_maybe_transactional.
Having done this, we set the FP/VMX bit in the thread's MSR after
reclaim to indicate that that part of the state is now valid
(having been reclaimed from the processor's checkpointed state).
Finally, in the signal handling code, we move the clearing of the
transactional state bits in the thread's MSR a bit earlier, before
calling flush_fp_to_thread(), so that we don't unnecessarily set
the TIF_RESTORE_TM bit.
This is the test program:
/* Michael Neuling 4/12/2013
*
* See if the altivec state is leaked out of an aborted transaction due to
* kernel vmx copy loops.
*
* gcc -m64 htm_vmxcopy.c -o htm_vmxcopy
*
*/
/* We don't use all of these, but for reference: */
int main(int argc, char *argv[])
{
long double vecin = 1.3;
long double vecout;
unsigned long pgsize = getpagesize();
int i;
int fd;
int size = pgsize*16;
char tmpfile[] = "/tmp/page_faultXXXXXX";
char buf[pgsize];
char *a;
uint64_t aborted = 0;
fd = mkstemp(tmpfile);
assert(fd >= 0);
memset(buf, 0, pgsize);
for (i = 0; i < size; i += pgsize)
assert(write(fd, buf, pgsize) == pgsize);
unlink(tmpfile);
a = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
assert(a != MAP_FAILED);
asm __volatile__(
"lxvd2x 40,0,%[vecinptr] ; " // set 40 to initial value
TBEGIN
"beq 3f ;"
TSUSPEND
"xxlxor 40,40,40 ; " // set 40 to 0
"std 5, 0(%[map]) ;" // cause kernel vmx copy page
TABORT
TRESUME
TEND
"li %[res], 0 ;"
"b 5f ;"
"3: ;" // Abort handler
"li %[res], 1 ;"
"5: ;"
"stxvd2x 40,0,%[vecoutptr] ; "
: [res]"=r"(aborted)
: [vecinptr]"r"(&vecin),
[vecoutptr]"r"(&vecout),
[map]"r"(a)
: "memory", "r0", "r3", "r4", "r5", "r6", "r7");
if (aborted && (vecin != vecout)){
printf("FAILED: vector state leaked on abort %f != %f\n",
(double)vecin, (double)vecout);
exit(1);
}
munmap(a, size);
close(fd);
printf("PASSED!\n");
return 0;
}
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2014-01-13 12:56:29 +08:00
|
|
|
giveup_fpu_maybe_transactional(current);
|
2005-09-26 14:04:21 +08:00
|
|
|
else
|
|
|
|
giveup_fpu(NULL); /* just enables FP for kernel */
|
|
|
|
#else
|
powerpc: Don't corrupt transactional state when using FP/VMX in kernel
Currently, when we have a process using the transactional memory
facilities on POWER8 (that is, the processor is in transactional
or suspended state), and the process enters the kernel and the
kernel then uses the floating-point or vector (VMX/Altivec) facility,
we end up corrupting the user-visible FP/VMX/VSX state. This
happens, for example, if a page fault causes a copy-on-write
operation, because the copy_page function will use VMX to do the
copy on POWER8. The test program below demonstrates the bug.
The bug happens because when FP/VMX state for a transactional process
is stored in the thread_struct, we store the checkpointed state in
.fp_state/.vr_state and the transactional (current) state in
.transact_fp/.transact_vr. However, when the kernel wants to use
FP/VMX, it calls enable_kernel_fp() or enable_kernel_altivec(),
which saves the current state in .fp_state/.vr_state. Furthermore,
when we return to the user process we return with FP/VMX/VSX
disabled. The next time the process uses FP/VMX/VSX, we don't know
which set of state (the current register values, .fp_state/.vr_state,
or .transact_fp/.transact_vr) we should be using, since we have no
way to tell if we are still in the same transaction, and if not,
whether the previous transaction succeeded or failed.
Thus it is necessary to strictly adhere to the rule that if FP has
been enabled at any point in a transaction, we must keep FP enabled
for the user process with the current transactional state in the
FP registers, until we detect that it is no longer in a transaction.
Similarly for VMX; once enabled it must stay enabled until the
process is no longer transactional.
In order to keep this rule, we add a new thread_info flag which we
test when returning from the kernel to userspace, called TIF_RESTORE_TM.
This flag indicates that there is FP/VMX/VSX state to be restored
before entering userspace, and when it is set the .tm_orig_msr field
in the thread_struct indicates what state needs to be restored.
The restoration is done by restore_tm_state(). The TIF_RESTORE_TM
bit is set by new giveup_fpu/altivec_maybe_transactional helpers,
which are called from enable_kernel_fp/altivec, giveup_vsx, and
flush_fp/altivec_to_thread instead of giveup_fpu/altivec.
The other thing to be done is to get the transactional FP/VMX/VSX
state from .fp_state/.vr_state when doing reclaim, if that state
has been saved there by giveup_fpu/altivec_maybe_transactional.
Having done this, we set the FP/VMX bit in the thread's MSR after
reclaim to indicate that that part of the state is now valid
(having been reclaimed from the processor's checkpointed state).
Finally, in the signal handling code, we move the clearing of the
transactional state bits in the thread's MSR a bit earlier, before
calling flush_fp_to_thread(), so that we don't unnecessarily set
the TIF_RESTORE_TM bit.
This is the test program:
/* Michael Neuling 4/12/2013
*
* See if the altivec state is leaked out of an aborted transaction due to
* kernel vmx copy loops.
*
* gcc -m64 htm_vmxcopy.c -o htm_vmxcopy
*
*/
/* We don't use all of these, but for reference: */
int main(int argc, char *argv[])
{
long double vecin = 1.3;
long double vecout;
unsigned long pgsize = getpagesize();
int i;
int fd;
int size = pgsize*16;
char tmpfile[] = "/tmp/page_faultXXXXXX";
char buf[pgsize];
char *a;
uint64_t aborted = 0;
fd = mkstemp(tmpfile);
assert(fd >= 0);
memset(buf, 0, pgsize);
for (i = 0; i < size; i += pgsize)
assert(write(fd, buf, pgsize) == pgsize);
unlink(tmpfile);
a = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
assert(a != MAP_FAILED);
asm __volatile__(
"lxvd2x 40,0,%[vecinptr] ; " // set 40 to initial value
TBEGIN
"beq 3f ;"
TSUSPEND
"xxlxor 40,40,40 ; " // set 40 to 0
"std 5, 0(%[map]) ;" // cause kernel vmx copy page
TABORT
TRESUME
TEND
"li %[res], 0 ;"
"b 5f ;"
"3: ;" // Abort handler
"li %[res], 1 ;"
"5: ;"
"stxvd2x 40,0,%[vecoutptr] ; "
: [res]"=r"(aborted)
: [vecinptr]"r"(&vecin),
[vecoutptr]"r"(&vecout),
[map]"r"(a)
: "memory", "r0", "r3", "r4", "r5", "r6", "r7");
if (aborted && (vecin != vecout)){
printf("FAILED: vector state leaked on abort %f != %f\n",
(double)vecin, (double)vecout);
exit(1);
}
munmap(a, size);
close(fd);
printf("PASSED!\n");
return 0;
}
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2014-01-13 12:56:29 +08:00
|
|
|
giveup_fpu_maybe_transactional(last_task_used_math);
|
2005-09-26 14:04:21 +08:00
|
|
|
#endif /* CONFIG_SMP */
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(enable_kernel_fp);
|
|
|
|
|
|
|
|
#ifdef CONFIG_ALTIVEC
|
|
|
|
void enable_kernel_altivec(void)
|
|
|
|
{
|
|
|
|
WARN_ON(preemptible());
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
if (current->thread.regs && (current->thread.regs->msr & MSR_VEC))
|
powerpc: Don't corrupt transactional state when using FP/VMX in kernel
Currently, when we have a process using the transactional memory
facilities on POWER8 (that is, the processor is in transactional
or suspended state), and the process enters the kernel and the
kernel then uses the floating-point or vector (VMX/Altivec) facility,
we end up corrupting the user-visible FP/VMX/VSX state. This
happens, for example, if a page fault causes a copy-on-write
operation, because the copy_page function will use VMX to do the
copy on POWER8. The test program below demonstrates the bug.
The bug happens because when FP/VMX state for a transactional process
is stored in the thread_struct, we store the checkpointed state in
.fp_state/.vr_state and the transactional (current) state in
.transact_fp/.transact_vr. However, when the kernel wants to use
FP/VMX, it calls enable_kernel_fp() or enable_kernel_altivec(),
which saves the current state in .fp_state/.vr_state. Furthermore,
when we return to the user process we return with FP/VMX/VSX
disabled. The next time the process uses FP/VMX/VSX, we don't know
which set of state (the current register values, .fp_state/.vr_state,
or .transact_fp/.transact_vr) we should be using, since we have no
way to tell if we are still in the same transaction, and if not,
whether the previous transaction succeeded or failed.
Thus it is necessary to strictly adhere to the rule that if FP has
been enabled at any point in a transaction, we must keep FP enabled
for the user process with the current transactional state in the
FP registers, until we detect that it is no longer in a transaction.
Similarly for VMX; once enabled it must stay enabled until the
process is no longer transactional.
In order to keep this rule, we add a new thread_info flag which we
test when returning from the kernel to userspace, called TIF_RESTORE_TM.
This flag indicates that there is FP/VMX/VSX state to be restored
before entering userspace, and when it is set the .tm_orig_msr field
in the thread_struct indicates what state needs to be restored.
The restoration is done by restore_tm_state(). The TIF_RESTORE_TM
bit is set by new giveup_fpu/altivec_maybe_transactional helpers,
which are called from enable_kernel_fp/altivec, giveup_vsx, and
flush_fp/altivec_to_thread instead of giveup_fpu/altivec.
The other thing to be done is to get the transactional FP/VMX/VSX
state from .fp_state/.vr_state when doing reclaim, if that state
has been saved there by giveup_fpu/altivec_maybe_transactional.
Having done this, we set the FP/VMX bit in the thread's MSR after
reclaim to indicate that that part of the state is now valid
(having been reclaimed from the processor's checkpointed state).
Finally, in the signal handling code, we move the clearing of the
transactional state bits in the thread's MSR a bit earlier, before
calling flush_fp_to_thread(), so that we don't unnecessarily set
the TIF_RESTORE_TM bit.
This is the test program:
/* Michael Neuling 4/12/2013
*
* See if the altivec state is leaked out of an aborted transaction due to
* kernel vmx copy loops.
*
* gcc -m64 htm_vmxcopy.c -o htm_vmxcopy
*
*/
/* We don't use all of these, but for reference: */
int main(int argc, char *argv[])
{
long double vecin = 1.3;
long double vecout;
unsigned long pgsize = getpagesize();
int i;
int fd;
int size = pgsize*16;
char tmpfile[] = "/tmp/page_faultXXXXXX";
char buf[pgsize];
char *a;
uint64_t aborted = 0;
fd = mkstemp(tmpfile);
assert(fd >= 0);
memset(buf, 0, pgsize);
for (i = 0; i < size; i += pgsize)
assert(write(fd, buf, pgsize) == pgsize);
unlink(tmpfile);
a = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
assert(a != MAP_FAILED);
asm __volatile__(
"lxvd2x 40,0,%[vecinptr] ; " // set 40 to initial value
TBEGIN
"beq 3f ;"
TSUSPEND
"xxlxor 40,40,40 ; " // set 40 to 0
"std 5, 0(%[map]) ;" // cause kernel vmx copy page
TABORT
TRESUME
TEND
"li %[res], 0 ;"
"b 5f ;"
"3: ;" // Abort handler
"li %[res], 1 ;"
"5: ;"
"stxvd2x 40,0,%[vecoutptr] ; "
: [res]"=r"(aborted)
: [vecinptr]"r"(&vecin),
[vecoutptr]"r"(&vecout),
[map]"r"(a)
: "memory", "r0", "r3", "r4", "r5", "r6", "r7");
if (aborted && (vecin != vecout)){
printf("FAILED: vector state leaked on abort %f != %f\n",
(double)vecin, (double)vecout);
exit(1);
}
munmap(a, size);
close(fd);
printf("PASSED!\n");
return 0;
}
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2014-01-13 12:56:29 +08:00
|
|
|
giveup_altivec_maybe_transactional(current);
|
2005-09-26 14:04:21 +08:00
|
|
|
else
|
2012-04-16 04:56:45 +08:00
|
|
|
giveup_altivec_notask();
|
2005-09-26 14:04:21 +08:00
|
|
|
#else
|
powerpc: Don't corrupt transactional state when using FP/VMX in kernel
Currently, when we have a process using the transactional memory
facilities on POWER8 (that is, the processor is in transactional
or suspended state), and the process enters the kernel and the
kernel then uses the floating-point or vector (VMX/Altivec) facility,
we end up corrupting the user-visible FP/VMX/VSX state. This
happens, for example, if a page fault causes a copy-on-write
operation, because the copy_page function will use VMX to do the
copy on POWER8. The test program below demonstrates the bug.
The bug happens because when FP/VMX state for a transactional process
is stored in the thread_struct, we store the checkpointed state in
.fp_state/.vr_state and the transactional (current) state in
.transact_fp/.transact_vr. However, when the kernel wants to use
FP/VMX, it calls enable_kernel_fp() or enable_kernel_altivec(),
which saves the current state in .fp_state/.vr_state. Furthermore,
when we return to the user process we return with FP/VMX/VSX
disabled. The next time the process uses FP/VMX/VSX, we don't know
which set of state (the current register values, .fp_state/.vr_state,
or .transact_fp/.transact_vr) we should be using, since we have no
way to tell if we are still in the same transaction, and if not,
whether the previous transaction succeeded or failed.
Thus it is necessary to strictly adhere to the rule that if FP has
been enabled at any point in a transaction, we must keep FP enabled
for the user process with the current transactional state in the
FP registers, until we detect that it is no longer in a transaction.
Similarly for VMX; once enabled it must stay enabled until the
process is no longer transactional.
In order to keep this rule, we add a new thread_info flag which we
test when returning from the kernel to userspace, called TIF_RESTORE_TM.
This flag indicates that there is FP/VMX/VSX state to be restored
before entering userspace, and when it is set the .tm_orig_msr field
in the thread_struct indicates what state needs to be restored.
The restoration is done by restore_tm_state(). The TIF_RESTORE_TM
bit is set by new giveup_fpu/altivec_maybe_transactional helpers,
which are called from enable_kernel_fp/altivec, giveup_vsx, and
flush_fp/altivec_to_thread instead of giveup_fpu/altivec.
The other thing to be done is to get the transactional FP/VMX/VSX
state from .fp_state/.vr_state when doing reclaim, if that state
has been saved there by giveup_fpu/altivec_maybe_transactional.
Having done this, we set the FP/VMX bit in the thread's MSR after
reclaim to indicate that that part of the state is now valid
(having been reclaimed from the processor's checkpointed state).
Finally, in the signal handling code, we move the clearing of the
transactional state bits in the thread's MSR a bit earlier, before
calling flush_fp_to_thread(), so that we don't unnecessarily set
the TIF_RESTORE_TM bit.
This is the test program:
/* Michael Neuling 4/12/2013
*
* See if the altivec state is leaked out of an aborted transaction due to
* kernel vmx copy loops.
*
* gcc -m64 htm_vmxcopy.c -o htm_vmxcopy
*
*/
/* We don't use all of these, but for reference: */
int main(int argc, char *argv[])
{
long double vecin = 1.3;
long double vecout;
unsigned long pgsize = getpagesize();
int i;
int fd;
int size = pgsize*16;
char tmpfile[] = "/tmp/page_faultXXXXXX";
char buf[pgsize];
char *a;
uint64_t aborted = 0;
fd = mkstemp(tmpfile);
assert(fd >= 0);
memset(buf, 0, pgsize);
for (i = 0; i < size; i += pgsize)
assert(write(fd, buf, pgsize) == pgsize);
unlink(tmpfile);
a = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
assert(a != MAP_FAILED);
asm __volatile__(
"lxvd2x 40,0,%[vecinptr] ; " // set 40 to initial value
TBEGIN
"beq 3f ;"
TSUSPEND
"xxlxor 40,40,40 ; " // set 40 to 0
"std 5, 0(%[map]) ;" // cause kernel vmx copy page
TABORT
TRESUME
TEND
"li %[res], 0 ;"
"b 5f ;"
"3: ;" // Abort handler
"li %[res], 1 ;"
"5: ;"
"stxvd2x 40,0,%[vecoutptr] ; "
: [res]"=r"(aborted)
: [vecinptr]"r"(&vecin),
[vecoutptr]"r"(&vecout),
[map]"r"(a)
: "memory", "r0", "r3", "r4", "r5", "r6", "r7");
if (aborted && (vecin != vecout)){
printf("FAILED: vector state leaked on abort %f != %f\n",
(double)vecin, (double)vecout);
exit(1);
}
munmap(a, size);
close(fd);
printf("PASSED!\n");
return 0;
}
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2014-01-13 12:56:29 +08:00
|
|
|
giveup_altivec_maybe_transactional(last_task_used_altivec);
|
2005-09-26 14:04:21 +08:00
|
|
|
#endif /* CONFIG_SMP */
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(enable_kernel_altivec);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure the VMX/Altivec register state in the
|
|
|
|
* the thread_struct is up to date for task tsk.
|
|
|
|
*/
|
|
|
|
void flush_altivec_to_thread(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
if (tsk->thread.regs) {
|
|
|
|
preempt_disable();
|
|
|
|
if (tsk->thread.regs->msr & MSR_VEC) {
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
BUG_ON(tsk != current);
|
|
|
|
#endif
|
powerpc: Don't corrupt transactional state when using FP/VMX in kernel
Currently, when we have a process using the transactional memory
facilities on POWER8 (that is, the processor is in transactional
or suspended state), and the process enters the kernel and the
kernel then uses the floating-point or vector (VMX/Altivec) facility,
we end up corrupting the user-visible FP/VMX/VSX state. This
happens, for example, if a page fault causes a copy-on-write
operation, because the copy_page function will use VMX to do the
copy on POWER8. The test program below demonstrates the bug.
The bug happens because when FP/VMX state for a transactional process
is stored in the thread_struct, we store the checkpointed state in
.fp_state/.vr_state and the transactional (current) state in
.transact_fp/.transact_vr. However, when the kernel wants to use
FP/VMX, it calls enable_kernel_fp() or enable_kernel_altivec(),
which saves the current state in .fp_state/.vr_state. Furthermore,
when we return to the user process we return with FP/VMX/VSX
disabled. The next time the process uses FP/VMX/VSX, we don't know
which set of state (the current register values, .fp_state/.vr_state,
or .transact_fp/.transact_vr) we should be using, since we have no
way to tell if we are still in the same transaction, and if not,
whether the previous transaction succeeded or failed.
Thus it is necessary to strictly adhere to the rule that if FP has
been enabled at any point in a transaction, we must keep FP enabled
for the user process with the current transactional state in the
FP registers, until we detect that it is no longer in a transaction.
Similarly for VMX; once enabled it must stay enabled until the
process is no longer transactional.
In order to keep this rule, we add a new thread_info flag which we
test when returning from the kernel to userspace, called TIF_RESTORE_TM.
This flag indicates that there is FP/VMX/VSX state to be restored
before entering userspace, and when it is set the .tm_orig_msr field
in the thread_struct indicates what state needs to be restored.
The restoration is done by restore_tm_state(). The TIF_RESTORE_TM
bit is set by new giveup_fpu/altivec_maybe_transactional helpers,
which are called from enable_kernel_fp/altivec, giveup_vsx, and
flush_fp/altivec_to_thread instead of giveup_fpu/altivec.
The other thing to be done is to get the transactional FP/VMX/VSX
state from .fp_state/.vr_state when doing reclaim, if that state
has been saved there by giveup_fpu/altivec_maybe_transactional.
Having done this, we set the FP/VMX bit in the thread's MSR after
reclaim to indicate that that part of the state is now valid
(having been reclaimed from the processor's checkpointed state).
Finally, in the signal handling code, we move the clearing of the
transactional state bits in the thread's MSR a bit earlier, before
calling flush_fp_to_thread(), so that we don't unnecessarily set
the TIF_RESTORE_TM bit.
This is the test program:
/* Michael Neuling 4/12/2013
*
* See if the altivec state is leaked out of an aborted transaction due to
* kernel vmx copy loops.
*
* gcc -m64 htm_vmxcopy.c -o htm_vmxcopy
*
*/
/* We don't use all of these, but for reference: */
int main(int argc, char *argv[])
{
long double vecin = 1.3;
long double vecout;
unsigned long pgsize = getpagesize();
int i;
int fd;
int size = pgsize*16;
char tmpfile[] = "/tmp/page_faultXXXXXX";
char buf[pgsize];
char *a;
uint64_t aborted = 0;
fd = mkstemp(tmpfile);
assert(fd >= 0);
memset(buf, 0, pgsize);
for (i = 0; i < size; i += pgsize)
assert(write(fd, buf, pgsize) == pgsize);
unlink(tmpfile);
a = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
assert(a != MAP_FAILED);
asm __volatile__(
"lxvd2x 40,0,%[vecinptr] ; " // set 40 to initial value
TBEGIN
"beq 3f ;"
TSUSPEND
"xxlxor 40,40,40 ; " // set 40 to 0
"std 5, 0(%[map]) ;" // cause kernel vmx copy page
TABORT
TRESUME
TEND
"li %[res], 0 ;"
"b 5f ;"
"3: ;" // Abort handler
"li %[res], 1 ;"
"5: ;"
"stxvd2x 40,0,%[vecoutptr] ; "
: [res]"=r"(aborted)
: [vecinptr]"r"(&vecin),
[vecoutptr]"r"(&vecout),
[map]"r"(a)
: "memory", "r0", "r3", "r4", "r5", "r6", "r7");
if (aborted && (vecin != vecout)){
printf("FAILED: vector state leaked on abort %f != %f\n",
(double)vecin, (double)vecout);
exit(1);
}
munmap(a, size);
close(fd);
printf("PASSED!\n");
return 0;
}
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2014-01-13 12:56:29 +08:00
|
|
|
giveup_altivec_maybe_transactional(tsk);
|
2005-09-26 14:04:21 +08:00
|
|
|
}
|
|
|
|
preempt_enable();
|
|
|
|
}
|
|
|
|
}
|
KVM: PPC: Add support for Book3S processors in hypervisor mode
This adds support for KVM running on 64-bit Book 3S processors,
specifically POWER7, in hypervisor mode. Using hypervisor mode means
that the guest can use the processor's supervisor mode. That means
that the guest can execute privileged instructions and access privileged
registers itself without trapping to the host. This gives excellent
performance, but does mean that KVM cannot emulate a processor
architecture other than the one that the hardware implements.
This code assumes that the guest is running paravirtualized using the
PAPR (Power Architecture Platform Requirements) interface, which is the
interface that IBM's PowerVM hypervisor uses. That means that existing
Linux distributions that run on IBM pSeries machines will also run
under KVM without modification. In order to communicate the PAPR
hypercalls to qemu, this adds a new KVM_EXIT_PAPR_HCALL exit code
to include/linux/kvm.h.
Currently the choice between book3s_hv support and book3s_pr support
(i.e. the existing code, which runs the guest in user mode) has to be
made at kernel configuration time, so a given kernel binary can only
do one or the other.
This new book3s_hv code doesn't support MMIO emulation at present.
Since we are running paravirtualized guests, this isn't a serious
restriction.
With the guest running in supervisor mode, most exceptions go straight
to the guest. We will never get data or instruction storage or segment
interrupts, alignment interrupts, decrementer interrupts, program
interrupts, single-step interrupts, etc., coming to the hypervisor from
the guest. Therefore this introduces a new KVMTEST_NONHV macro for the
exception entry path so that we don't have to do the KVM test on entry
to those exception handlers.
We do however get hypervisor decrementer, hypervisor data storage,
hypervisor instruction storage, and hypervisor emulation assist
interrupts, so we have to handle those.
In hypervisor mode, real-mode accesses can access all of RAM, not just
a limited amount. Therefore we put all the guest state in the vcpu.arch
and use the shadow_vcpu in the PACA only for temporary scratch space.
We allocate the vcpu with kzalloc rather than vzalloc, and we don't use
anything in the kvmppc_vcpu_book3s struct, so we don't allocate it.
We don't have a shared page with the guest, but we still need a
kvm_vcpu_arch_shared struct to store the values of various registers,
so we include one in the vcpu_arch struct.
The POWER7 processor has a restriction that all threads in a core have
to be in the same partition. MMU-on kernel code counts as a partition
(partition 0), so we have to do a partition switch on every entry to and
exit from the guest. At present we require the host and guest to run
in single-thread mode because of this hardware restriction.
This code allocates a hashed page table for the guest and initializes
it with HPTEs for the guest's Virtual Real Memory Area (VRMA). We
require that the guest memory is allocated using 16MB huge pages, in
order to simplify the low-level memory management. This also means that
we can get away without tracking paging activity in the host for now,
since huge pages can't be paged or swapped.
This also adds a few new exports needed by the book3s_hv code.
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Alexander Graf <agraf@suse.de>
2011-06-29 08:21:34 +08:00
|
|
|
EXPORT_SYMBOL_GPL(flush_altivec_to_thread);
|
2005-09-26 14:04:21 +08:00
|
|
|
#endif /* CONFIG_ALTIVEC */
|
|
|
|
|
2008-06-25 12:07:18 +08:00
|
|
|
#ifdef CONFIG_VSX
|
|
|
|
#if 0
|
|
|
|
/* not currently used, but some crazy RAID module might want to later */
|
|
|
|
void enable_kernel_vsx(void)
|
|
|
|
{
|
|
|
|
WARN_ON(preemptible());
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
if (current->thread.regs && (current->thread.regs->msr & MSR_VSX))
|
|
|
|
giveup_vsx(current);
|
|
|
|
else
|
|
|
|
giveup_vsx(NULL); /* just enable vsx for kernel - force */
|
|
|
|
#else
|
|
|
|
giveup_vsx(last_task_used_vsx);
|
|
|
|
#endif /* CONFIG_SMP */
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(enable_kernel_vsx);
|
|
|
|
#endif
|
|
|
|
|
2008-07-11 14:29:12 +08:00
|
|
|
void giveup_vsx(struct task_struct *tsk)
|
|
|
|
{
|
powerpc: Don't corrupt transactional state when using FP/VMX in kernel
Currently, when we have a process using the transactional memory
facilities on POWER8 (that is, the processor is in transactional
or suspended state), and the process enters the kernel and the
kernel then uses the floating-point or vector (VMX/Altivec) facility,
we end up corrupting the user-visible FP/VMX/VSX state. This
happens, for example, if a page fault causes a copy-on-write
operation, because the copy_page function will use VMX to do the
copy on POWER8. The test program below demonstrates the bug.
The bug happens because when FP/VMX state for a transactional process
is stored in the thread_struct, we store the checkpointed state in
.fp_state/.vr_state and the transactional (current) state in
.transact_fp/.transact_vr. However, when the kernel wants to use
FP/VMX, it calls enable_kernel_fp() or enable_kernel_altivec(),
which saves the current state in .fp_state/.vr_state. Furthermore,
when we return to the user process we return with FP/VMX/VSX
disabled. The next time the process uses FP/VMX/VSX, we don't know
which set of state (the current register values, .fp_state/.vr_state,
or .transact_fp/.transact_vr) we should be using, since we have no
way to tell if we are still in the same transaction, and if not,
whether the previous transaction succeeded or failed.
Thus it is necessary to strictly adhere to the rule that if FP has
been enabled at any point in a transaction, we must keep FP enabled
for the user process with the current transactional state in the
FP registers, until we detect that it is no longer in a transaction.
Similarly for VMX; once enabled it must stay enabled until the
process is no longer transactional.
In order to keep this rule, we add a new thread_info flag which we
test when returning from the kernel to userspace, called TIF_RESTORE_TM.
This flag indicates that there is FP/VMX/VSX state to be restored
before entering userspace, and when it is set the .tm_orig_msr field
in the thread_struct indicates what state needs to be restored.
The restoration is done by restore_tm_state(). The TIF_RESTORE_TM
bit is set by new giveup_fpu/altivec_maybe_transactional helpers,
which are called from enable_kernel_fp/altivec, giveup_vsx, and
flush_fp/altivec_to_thread instead of giveup_fpu/altivec.
The other thing to be done is to get the transactional FP/VMX/VSX
state from .fp_state/.vr_state when doing reclaim, if that state
has been saved there by giveup_fpu/altivec_maybe_transactional.
Having done this, we set the FP/VMX bit in the thread's MSR after
reclaim to indicate that that part of the state is now valid
(having been reclaimed from the processor's checkpointed state).
Finally, in the signal handling code, we move the clearing of the
transactional state bits in the thread's MSR a bit earlier, before
calling flush_fp_to_thread(), so that we don't unnecessarily set
the TIF_RESTORE_TM bit.
This is the test program:
/* Michael Neuling 4/12/2013
*
* See if the altivec state is leaked out of an aborted transaction due to
* kernel vmx copy loops.
*
* gcc -m64 htm_vmxcopy.c -o htm_vmxcopy
*
*/
/* We don't use all of these, but for reference: */
int main(int argc, char *argv[])
{
long double vecin = 1.3;
long double vecout;
unsigned long pgsize = getpagesize();
int i;
int fd;
int size = pgsize*16;
char tmpfile[] = "/tmp/page_faultXXXXXX";
char buf[pgsize];
char *a;
uint64_t aborted = 0;
fd = mkstemp(tmpfile);
assert(fd >= 0);
memset(buf, 0, pgsize);
for (i = 0; i < size; i += pgsize)
assert(write(fd, buf, pgsize) == pgsize);
unlink(tmpfile);
a = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
assert(a != MAP_FAILED);
asm __volatile__(
"lxvd2x 40,0,%[vecinptr] ; " // set 40 to initial value
TBEGIN
"beq 3f ;"
TSUSPEND
"xxlxor 40,40,40 ; " // set 40 to 0
"std 5, 0(%[map]) ;" // cause kernel vmx copy page
TABORT
TRESUME
TEND
"li %[res], 0 ;"
"b 5f ;"
"3: ;" // Abort handler
"li %[res], 1 ;"
"5: ;"
"stxvd2x 40,0,%[vecoutptr] ; "
: [res]"=r"(aborted)
: [vecinptr]"r"(&vecin),
[vecoutptr]"r"(&vecout),
[map]"r"(a)
: "memory", "r0", "r3", "r4", "r5", "r6", "r7");
if (aborted && (vecin != vecout)){
printf("FAILED: vector state leaked on abort %f != %f\n",
(double)vecin, (double)vecout);
exit(1);
}
munmap(a, size);
close(fd);
printf("PASSED!\n");
return 0;
}
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2014-01-13 12:56:29 +08:00
|
|
|
giveup_fpu_maybe_transactional(tsk);
|
|
|
|
giveup_altivec_maybe_transactional(tsk);
|
2008-07-11 14:29:12 +08:00
|
|
|
__giveup_vsx(tsk);
|
|
|
|
}
|
2014-08-20 06:00:02 +08:00
|
|
|
EXPORT_SYMBOL(giveup_vsx);
|
2008-07-11 14:29:12 +08:00
|
|
|
|
2008-06-25 12:07:18 +08:00
|
|
|
void flush_vsx_to_thread(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
if (tsk->thread.regs) {
|
|
|
|
preempt_disable();
|
|
|
|
if (tsk->thread.regs->msr & MSR_VSX) {
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
BUG_ON(tsk != current);
|
|
|
|
#endif
|
|
|
|
giveup_vsx(tsk);
|
|
|
|
}
|
|
|
|
preempt_enable();
|
|
|
|
}
|
|
|
|
}
|
KVM: PPC: Add support for Book3S processors in hypervisor mode
This adds support for KVM running on 64-bit Book 3S processors,
specifically POWER7, in hypervisor mode. Using hypervisor mode means
that the guest can use the processor's supervisor mode. That means
that the guest can execute privileged instructions and access privileged
registers itself without trapping to the host. This gives excellent
performance, but does mean that KVM cannot emulate a processor
architecture other than the one that the hardware implements.
This code assumes that the guest is running paravirtualized using the
PAPR (Power Architecture Platform Requirements) interface, which is the
interface that IBM's PowerVM hypervisor uses. That means that existing
Linux distributions that run on IBM pSeries machines will also run
under KVM without modification. In order to communicate the PAPR
hypercalls to qemu, this adds a new KVM_EXIT_PAPR_HCALL exit code
to include/linux/kvm.h.
Currently the choice between book3s_hv support and book3s_pr support
(i.e. the existing code, which runs the guest in user mode) has to be
made at kernel configuration time, so a given kernel binary can only
do one or the other.
This new book3s_hv code doesn't support MMIO emulation at present.
Since we are running paravirtualized guests, this isn't a serious
restriction.
With the guest running in supervisor mode, most exceptions go straight
to the guest. We will never get data or instruction storage or segment
interrupts, alignment interrupts, decrementer interrupts, program
interrupts, single-step interrupts, etc., coming to the hypervisor from
the guest. Therefore this introduces a new KVMTEST_NONHV macro for the
exception entry path so that we don't have to do the KVM test on entry
to those exception handlers.
We do however get hypervisor decrementer, hypervisor data storage,
hypervisor instruction storage, and hypervisor emulation assist
interrupts, so we have to handle those.
In hypervisor mode, real-mode accesses can access all of RAM, not just
a limited amount. Therefore we put all the guest state in the vcpu.arch
and use the shadow_vcpu in the PACA only for temporary scratch space.
We allocate the vcpu with kzalloc rather than vzalloc, and we don't use
anything in the kvmppc_vcpu_book3s struct, so we don't allocate it.
We don't have a shared page with the guest, but we still need a
kvm_vcpu_arch_shared struct to store the values of various registers,
so we include one in the vcpu_arch struct.
The POWER7 processor has a restriction that all threads in a core have
to be in the same partition. MMU-on kernel code counts as a partition
(partition 0), so we have to do a partition switch on every entry to and
exit from the guest. At present we require the host and guest to run
in single-thread mode because of this hardware restriction.
This code allocates a hashed page table for the guest and initializes
it with HPTEs for the guest's Virtual Real Memory Area (VRMA). We
require that the guest memory is allocated using 16MB huge pages, in
order to simplify the low-level memory management. This also means that
we can get away without tracking paging activity in the host for now,
since huge pages can't be paged or swapped.
This also adds a few new exports needed by the book3s_hv code.
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Alexander Graf <agraf@suse.de>
2011-06-29 08:21:34 +08:00
|
|
|
EXPORT_SYMBOL_GPL(flush_vsx_to_thread);
|
2008-06-25 12:07:18 +08:00
|
|
|
#endif /* CONFIG_VSX */
|
|
|
|
|
2005-09-26 14:04:21 +08:00
|
|
|
#ifdef CONFIG_SPE
|
|
|
|
|
|
|
|
void enable_kernel_spe(void)
|
|
|
|
{
|
|
|
|
WARN_ON(preemptible());
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
if (current->thread.regs && (current->thread.regs->msr & MSR_SPE))
|
|
|
|
giveup_spe(current);
|
|
|
|
else
|
|
|
|
giveup_spe(NULL); /* just enable SPE for kernel - force */
|
|
|
|
#else
|
|
|
|
giveup_spe(last_task_used_spe);
|
|
|
|
#endif /* __SMP __ */
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(enable_kernel_spe);
|
|
|
|
|
|
|
|
void flush_spe_to_thread(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
if (tsk->thread.regs) {
|
|
|
|
preempt_disable();
|
|
|
|
if (tsk->thread.regs->msr & MSR_SPE) {
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
BUG_ON(tsk != current);
|
|
|
|
#endif
|
2011-06-15 07:34:25 +08:00
|
|
|
tsk->thread.spefscr = mfspr(SPRN_SPEFSCR);
|
2007-08-29 10:15:53 +08:00
|
|
|
giveup_spe(tsk);
|
2005-09-26 14:04:21 +08:00
|
|
|
}
|
|
|
|
preempt_enable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_SPE */
|
|
|
|
|
2006-01-11 19:11:39 +08:00
|
|
|
#ifndef CONFIG_SMP
|
2005-11-30 10:20:54 +08:00
|
|
|
/*
|
|
|
|
* If we are doing lazy switching of CPU state (FP, altivec or SPE),
|
|
|
|
* and the current task has some state, discard it.
|
|
|
|
*/
|
2006-01-11 19:11:39 +08:00
|
|
|
void discard_lazy_cpu_state(void)
|
2005-11-30 10:20:54 +08:00
|
|
|
{
|
|
|
|
preempt_disable();
|
|
|
|
if (last_task_used_math == current)
|
|
|
|
last_task_used_math = NULL;
|
|
|
|
#ifdef CONFIG_ALTIVEC
|
|
|
|
if (last_task_used_altivec == current)
|
|
|
|
last_task_used_altivec = NULL;
|
|
|
|
#endif /* CONFIG_ALTIVEC */
|
2008-06-25 12:07:18 +08:00
|
|
|
#ifdef CONFIG_VSX
|
|
|
|
if (last_task_used_vsx == current)
|
|
|
|
last_task_used_vsx = NULL;
|
|
|
|
#endif /* CONFIG_VSX */
|
2005-11-30 10:20:54 +08:00
|
|
|
#ifdef CONFIG_SPE
|
|
|
|
if (last_task_used_spe == current)
|
|
|
|
last_task_used_spe = NULL;
|
|
|
|
#endif
|
|
|
|
preempt_enable();
|
|
|
|
}
|
2006-01-11 19:11:39 +08:00
|
|
|
#endif /* CONFIG_SMP */
|
2005-11-30 10:20:54 +08:00
|
|
|
|
2010-02-08 19:51:18 +08:00
|
|
|
#ifdef CONFIG_PPC_ADV_DEBUG_REGS
|
|
|
|
void do_send_trap(struct pt_regs *regs, unsigned long address,
|
|
|
|
unsigned long error_code, int signal_code, int breakpt)
|
|
|
|
{
|
|
|
|
siginfo_t info;
|
|
|
|
|
2012-08-24 05:27:09 +08:00
|
|
|
current->thread.trap_nr = signal_code;
|
2010-02-08 19:51:18 +08:00
|
|
|
if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
|
|
|
|
11, SIGSEGV) == NOTIFY_STOP)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Deliver the signal to userspace */
|
|
|
|
info.si_signo = SIGTRAP;
|
|
|
|
info.si_errno = breakpt; /* breakpoint or watchpoint id */
|
|
|
|
info.si_code = signal_code;
|
|
|
|
info.si_addr = (void __user *)address;
|
|
|
|
force_sig_info(SIGTRAP, &info, current);
|
|
|
|
}
|
|
|
|
#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
|
2012-12-20 22:06:44 +08:00
|
|
|
void do_break (struct pt_regs *regs, unsigned long address,
|
2008-07-24 00:10:41 +08:00
|
|
|
unsigned long error_code)
|
|
|
|
{
|
|
|
|
siginfo_t info;
|
|
|
|
|
2012-08-24 05:27:09 +08:00
|
|
|
current->thread.trap_nr = TRAP_HWBKPT;
|
2008-07-24 00:10:41 +08:00
|
|
|
if (notify_die(DIE_DABR_MATCH, "dabr_match", regs, error_code,
|
|
|
|
11, SIGSEGV) == NOTIFY_STOP)
|
|
|
|
return;
|
|
|
|
|
2012-12-20 22:06:44 +08:00
|
|
|
if (debugger_break_match(regs))
|
2008-07-24 00:10:41 +08:00
|
|
|
return;
|
|
|
|
|
2012-12-20 22:06:44 +08:00
|
|
|
/* Clear the breakpoint */
|
|
|
|
hw_breakpoint_disable();
|
2008-07-24 00:10:41 +08:00
|
|
|
|
|
|
|
/* Deliver the signal to userspace */
|
|
|
|
info.si_signo = SIGTRAP;
|
|
|
|
info.si_errno = 0;
|
|
|
|
info.si_code = TRAP_HWBKPT;
|
|
|
|
info.si_addr = (void __user *)address;
|
|
|
|
force_sig_info(SIGTRAP, &info, current);
|
|
|
|
}
|
2010-02-08 19:51:18 +08:00
|
|
|
#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
|
2008-07-24 00:10:41 +08:00
|
|
|
|
2012-12-20 22:06:44 +08:00
|
|
|
static DEFINE_PER_CPU(struct arch_hw_breakpoint, current_brk);
|
2008-03-28 16:11:48 +08:00
|
|
|
|
2010-02-08 19:51:18 +08:00
|
|
|
#ifdef CONFIG_PPC_ADV_DEBUG_REGS
|
|
|
|
/*
|
|
|
|
* Set the debug registers back to their default "safe" values.
|
|
|
|
*/
|
|
|
|
static void set_debug_reg_defaults(struct thread_struct *thread)
|
|
|
|
{
|
2013-07-04 14:15:46 +08:00
|
|
|
thread->debug.iac1 = thread->debug.iac2 = 0;
|
2010-02-08 19:51:18 +08:00
|
|
|
#if CONFIG_PPC_ADV_DEBUG_IACS > 2
|
2013-07-04 14:15:46 +08:00
|
|
|
thread->debug.iac3 = thread->debug.iac4 = 0;
|
2010-02-08 19:51:18 +08:00
|
|
|
#endif
|
2013-07-04 14:15:46 +08:00
|
|
|
thread->debug.dac1 = thread->debug.dac2 = 0;
|
2010-02-08 19:51:18 +08:00
|
|
|
#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
|
2013-07-04 14:15:46 +08:00
|
|
|
thread->debug.dvc1 = thread->debug.dvc2 = 0;
|
2010-02-08 19:51:18 +08:00
|
|
|
#endif
|
2013-07-04 14:15:46 +08:00
|
|
|
thread->debug.dbcr0 = 0;
|
2010-02-08 19:51:18 +08:00
|
|
|
#ifdef CONFIG_BOOKE
|
|
|
|
/*
|
|
|
|
* Force User/Supervisor bits to b11 (user-only MSR[PR]=1)
|
|
|
|
*/
|
2013-07-04 14:15:46 +08:00
|
|
|
thread->debug.dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US |
|
2010-02-08 19:51:18 +08:00
|
|
|
DBCR1_IAC3US | DBCR1_IAC4US;
|
|
|
|
/*
|
|
|
|
* Force Data Address Compare User/Supervisor bits to be User-only
|
|
|
|
* (0b11 MSR[PR]=1) and set all other bits in DBCR2 register to be 0.
|
|
|
|
*/
|
2013-07-04 14:15:46 +08:00
|
|
|
thread->debug.dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
|
2010-02-08 19:51:18 +08:00
|
|
|
#else
|
2013-07-04 14:15:46 +08:00
|
|
|
thread->debug.dbcr1 = 0;
|
2010-02-08 19:51:18 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-11-23 05:52:29 +08:00
|
|
|
static void prime_debug_regs(struct debug_reg *debug)
|
2010-02-08 19:51:18 +08:00
|
|
|
{
|
2013-05-13 22:14:53 +08:00
|
|
|
/*
|
|
|
|
* We could have inherited MSR_DE from userspace, since
|
|
|
|
* it doesn't get cleared on exception entry. Make sure
|
|
|
|
* MSR_DE is clear before we enable any debug events.
|
|
|
|
*/
|
|
|
|
mtmsr(mfmsr() & ~MSR_DE);
|
|
|
|
|
2013-11-23 05:52:29 +08:00
|
|
|
mtspr(SPRN_IAC1, debug->iac1);
|
|
|
|
mtspr(SPRN_IAC2, debug->iac2);
|
2010-02-08 19:51:18 +08:00
|
|
|
#if CONFIG_PPC_ADV_DEBUG_IACS > 2
|
2013-11-23 05:52:29 +08:00
|
|
|
mtspr(SPRN_IAC3, debug->iac3);
|
|
|
|
mtspr(SPRN_IAC4, debug->iac4);
|
2010-02-08 19:51:18 +08:00
|
|
|
#endif
|
2013-11-23 05:52:29 +08:00
|
|
|
mtspr(SPRN_DAC1, debug->dac1);
|
|
|
|
mtspr(SPRN_DAC2, debug->dac2);
|
2010-02-08 19:51:18 +08:00
|
|
|
#if CONFIG_PPC_ADV_DEBUG_DVCS > 0
|
2013-11-23 05:52:29 +08:00
|
|
|
mtspr(SPRN_DVC1, debug->dvc1);
|
|
|
|
mtspr(SPRN_DVC2, debug->dvc2);
|
2010-02-08 19:51:18 +08:00
|
|
|
#endif
|
2013-11-23 05:52:29 +08:00
|
|
|
mtspr(SPRN_DBCR0, debug->dbcr0);
|
|
|
|
mtspr(SPRN_DBCR1, debug->dbcr1);
|
2010-02-08 19:51:18 +08:00
|
|
|
#ifdef CONFIG_BOOKE
|
2013-11-23 05:52:29 +08:00
|
|
|
mtspr(SPRN_DBCR2, debug->dbcr2);
|
2010-02-08 19:51:18 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Unless neither the old or new thread are making use of the
|
|
|
|
* debug registers, set the debug registers from the values
|
|
|
|
* stored in the new thread.
|
|
|
|
*/
|
2013-11-23 05:52:29 +08:00
|
|
|
void switch_booke_debug_regs(struct debug_reg *new_debug)
|
2010-02-08 19:51:18 +08:00
|
|
|
{
|
2013-07-04 14:15:46 +08:00
|
|
|
if ((current->thread.debug.dbcr0 & DBCR0_IDM)
|
2013-11-23 05:52:29 +08:00
|
|
|
|| (new_debug->dbcr0 & DBCR0_IDM))
|
|
|
|
prime_debug_regs(new_debug);
|
2010-02-08 19:51:18 +08:00
|
|
|
}
|
2013-07-04 14:57:44 +08:00
|
|
|
EXPORT_SYMBOL_GPL(switch_booke_debug_regs);
|
2010-02-08 19:51:18 +08:00
|
|
|
#else /* !CONFIG_PPC_ADV_DEBUG_REGS */
|
2011-02-10 12:44:35 +08:00
|
|
|
#ifndef CONFIG_HAVE_HW_BREAKPOINT
|
2010-02-08 19:51:18 +08:00
|
|
|
static void set_debug_reg_defaults(struct thread_struct *thread)
|
|
|
|
{
|
2012-12-20 22:06:44 +08:00
|
|
|
thread->hw_brk.address = 0;
|
|
|
|
thread->hw_brk.type = 0;
|
2013-01-10 22:25:34 +08:00
|
|
|
set_breakpoint(&thread->hw_brk);
|
2010-02-08 19:51:18 +08:00
|
|
|
}
|
2011-02-10 12:44:35 +08:00
|
|
|
#endif /* !CONFIG_HAVE_HW_BREAKPOINT */
|
2010-02-08 19:51:18 +08:00
|
|
|
#endif /* CONFIG_PPC_ADV_DEBUG_REGS */
|
|
|
|
|
2010-02-08 19:50:57 +08:00
|
|
|
#ifdef CONFIG_PPC_ADV_DEBUG_REGS
|
2012-12-20 22:06:44 +08:00
|
|
|
static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
|
|
|
|
{
|
2008-07-24 00:10:41 +08:00
|
|
|
mtspr(SPRN_DAC1, dabr);
|
2010-03-05 18:43:24 +08:00
|
|
|
#ifdef CONFIG_PPC_47x
|
|
|
|
isync();
|
|
|
|
#endif
|
2012-12-20 22:06:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2009-09-08 22:16:58 +08:00
|
|
|
#elif defined(CONFIG_PPC_BOOK3S)
|
2012-12-20 22:06:44 +08:00
|
|
|
static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
|
|
|
|
{
|
2009-09-08 22:16:58 +08:00
|
|
|
mtspr(SPRN_DABR, dabr);
|
2013-05-17 04:27:31 +08:00
|
|
|
if (cpu_has_feature(CPU_FTR_DABRX))
|
|
|
|
mtspr(SPRN_DABRX, dabrx);
|
2005-11-03 12:30:49 +08:00
|
|
|
return 0;
|
2005-09-26 14:04:21 +08:00
|
|
|
}
|
2012-12-20 22:06:44 +08:00
|
|
|
#else
|
|
|
|
static inline int __set_dabr(unsigned long dabr, unsigned long dabrx)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline int set_dabr(struct arch_hw_breakpoint *brk)
|
|
|
|
{
|
|
|
|
unsigned long dabr, dabrx;
|
|
|
|
|
|
|
|
dabr = brk->address | (brk->type & HW_BRK_TYPE_DABR);
|
|
|
|
dabrx = ((brk->type >> 3) & 0x7);
|
|
|
|
|
|
|
|
if (ppc_md.set_dabr)
|
|
|
|
return ppc_md.set_dabr(dabr, dabrx);
|
|
|
|
|
|
|
|
return __set_dabr(dabr, dabrx);
|
|
|
|
}
|
|
|
|
|
2012-12-20 22:06:45 +08:00
|
|
|
static inline int set_dawr(struct arch_hw_breakpoint *brk)
|
|
|
|
{
|
2013-01-24 23:02:58 +08:00
|
|
|
unsigned long dawr, dawrx, mrd;
|
2012-12-20 22:06:45 +08:00
|
|
|
|
|
|
|
dawr = brk->address;
|
|
|
|
|
|
|
|
dawrx = (brk->type & (HW_BRK_TYPE_READ | HW_BRK_TYPE_WRITE)) \
|
|
|
|
<< (63 - 58); //* read/write bits */
|
|
|
|
dawrx |= ((brk->type & (HW_BRK_TYPE_TRANSLATE)) >> 2) \
|
|
|
|
<< (63 - 59); //* translate */
|
|
|
|
dawrx |= (brk->type & (HW_BRK_TYPE_PRIV_ALL)) \
|
|
|
|
>> 3; //* PRIM bits */
|
2013-01-24 23:02:58 +08:00
|
|
|
/* dawr length is stored in field MDR bits 48:53. Matches range in
|
|
|
|
doublewords (64 bits) baised by -1 eg. 0b000000=1DW and
|
|
|
|
0b111111=64DW.
|
|
|
|
brk->len is in bytes.
|
|
|
|
This aligns up to double word size, shifts and does the bias.
|
|
|
|
*/
|
|
|
|
mrd = ((brk->len + 7) >> 3) - 1;
|
|
|
|
dawrx |= (mrd & 0x3f) << (63 - 53);
|
2012-12-20 22:06:45 +08:00
|
|
|
|
|
|
|
if (ppc_md.set_dawr)
|
|
|
|
return ppc_md.set_dawr(dawr, dawrx);
|
|
|
|
mtspr(SPRN_DAWR, dawr);
|
|
|
|
mtspr(SPRN_DAWRX, dawrx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-30 03:25:17 +08:00
|
|
|
void __set_breakpoint(struct arch_hw_breakpoint *brk)
|
2012-12-20 22:06:44 +08:00
|
|
|
{
|
powerpc: Replace __get_cpu_var uses
This still has not been merged and now powerpc is the only arch that does
not have this change. Sorry about missing linuxppc-dev before.
V2->V2
- Fix up to work against 3.18-rc1
__get_cpu_var() is used for multiple purposes in the kernel source. One of
them is address calculation via the form &__get_cpu_var(x). This calculates
the address for the instance of the percpu variable of the current processor
based on an offset.
Other use cases are for storing and retrieving data from the current
processors percpu area. __get_cpu_var() can be used as an lvalue when
writing data or on the right side of an assignment.
__get_cpu_var() is defined as :
__get_cpu_var() always only does an address determination. However, store
and retrieve operations could use a segment prefix (or global register on
other platforms) to avoid the address calculation.
this_cpu_write() and this_cpu_read() can directly take an offset into a
percpu area and use optimized assembly code to read and write per cpu
variables.
This patch converts __get_cpu_var into either an explicit address
calculation using this_cpu_ptr() or into a use of this_cpu operations that
use the offset. Thereby address calculations are avoided and less registers
are used when code is generated.
At the end of the patch set all uses of __get_cpu_var have been removed so
the macro is removed too.
The patch set includes passes over all arches as well. Once these operations
are used throughout then specialized macros can be defined in non -x86
arches as well in order to optimize per cpu access by f.e. using a global
register that may be set to the per cpu base.
Transformations done to __get_cpu_var()
1. Determine the address of the percpu instance of the current processor.
DEFINE_PER_CPU(int, y);
int *x = &__get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(&y);
2. Same as #1 but this time an array structure is involved.
DEFINE_PER_CPU(int, y[20]);
int *x = __get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(y);
3. Retrieve the content of the current processors instance of a per cpu
variable.
DEFINE_PER_CPU(int, y);
int x = __get_cpu_var(y)
Converts to
int x = __this_cpu_read(y);
4. Retrieve the content of a percpu struct
DEFINE_PER_CPU(struct mystruct, y);
struct mystruct x = __get_cpu_var(y);
Converts to
memcpy(&x, this_cpu_ptr(&y), sizeof(x));
5. Assignment to a per cpu variable
DEFINE_PER_CPU(int, y)
__get_cpu_var(y) = x;
Converts to
__this_cpu_write(y, x);
6. Increment/Decrement etc of a per cpu variable
DEFINE_PER_CPU(int, y);
__get_cpu_var(y)++
Converts to
__this_cpu_inc(y)
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
CC: Paul Mackerras <paulus@samba.org>
Signed-off-by: Christoph Lameter <cl@linux.com>
[mpe: Fix build errors caused by set/or_softirq_pending(), and rework
assignment in __set_breakpoint() to use memcpy().]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
2014-10-22 04:23:25 +08:00
|
|
|
memcpy(this_cpu_ptr(¤t_brk), brk, sizeof(*brk));
|
2012-12-20 22:06:44 +08:00
|
|
|
|
2012-12-20 22:06:45 +08:00
|
|
|
if (cpu_has_feature(CPU_FTR_DAWR))
|
2014-04-30 03:25:16 +08:00
|
|
|
set_dawr(brk);
|
|
|
|
else
|
|
|
|
set_dabr(brk);
|
2012-12-20 22:06:44 +08:00
|
|
|
}
|
2005-09-26 14:04:21 +08:00
|
|
|
|
2014-04-30 03:25:17 +08:00
|
|
|
void set_breakpoint(struct arch_hw_breakpoint *brk)
|
|
|
|
{
|
|
|
|
preempt_disable();
|
|
|
|
__set_breakpoint(brk);
|
|
|
|
preempt_enable();
|
|
|
|
}
|
|
|
|
|
2005-10-10 20:29:05 +08:00
|
|
|
#ifdef CONFIG_PPC64
|
|
|
|
DEFINE_PER_CPU(struct cpu_usage, cpu_usage_array);
|
|
|
|
#endif
|
2005-09-26 14:04:21 +08:00
|
|
|
|
2012-12-20 22:06:44 +08:00
|
|
|
static inline bool hw_brk_match(struct arch_hw_breakpoint *a,
|
|
|
|
struct arch_hw_breakpoint *b)
|
|
|
|
{
|
|
|
|
if (a->address != b->address)
|
|
|
|
return false;
|
|
|
|
if (a->type != b->type)
|
|
|
|
return false;
|
|
|
|
if (a->len != b->len)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
powerpc: Don't corrupt transactional state when using FP/VMX in kernel
Currently, when we have a process using the transactional memory
facilities on POWER8 (that is, the processor is in transactional
or suspended state), and the process enters the kernel and the
kernel then uses the floating-point or vector (VMX/Altivec) facility,
we end up corrupting the user-visible FP/VMX/VSX state. This
happens, for example, if a page fault causes a copy-on-write
operation, because the copy_page function will use VMX to do the
copy on POWER8. The test program below demonstrates the bug.
The bug happens because when FP/VMX state for a transactional process
is stored in the thread_struct, we store the checkpointed state in
.fp_state/.vr_state and the transactional (current) state in
.transact_fp/.transact_vr. However, when the kernel wants to use
FP/VMX, it calls enable_kernel_fp() or enable_kernel_altivec(),
which saves the current state in .fp_state/.vr_state. Furthermore,
when we return to the user process we return with FP/VMX/VSX
disabled. The next time the process uses FP/VMX/VSX, we don't know
which set of state (the current register values, .fp_state/.vr_state,
or .transact_fp/.transact_vr) we should be using, since we have no
way to tell if we are still in the same transaction, and if not,
whether the previous transaction succeeded or failed.
Thus it is necessary to strictly adhere to the rule that if FP has
been enabled at any point in a transaction, we must keep FP enabled
for the user process with the current transactional state in the
FP registers, until we detect that it is no longer in a transaction.
Similarly for VMX; once enabled it must stay enabled until the
process is no longer transactional.
In order to keep this rule, we add a new thread_info flag which we
test when returning from the kernel to userspace, called TIF_RESTORE_TM.
This flag indicates that there is FP/VMX/VSX state to be restored
before entering userspace, and when it is set the .tm_orig_msr field
in the thread_struct indicates what state needs to be restored.
The restoration is done by restore_tm_state(). The TIF_RESTORE_TM
bit is set by new giveup_fpu/altivec_maybe_transactional helpers,
which are called from enable_kernel_fp/altivec, giveup_vsx, and
flush_fp/altivec_to_thread instead of giveup_fpu/altivec.
The other thing to be done is to get the transactional FP/VMX/VSX
state from .fp_state/.vr_state when doing reclaim, if that state
has been saved there by giveup_fpu/altivec_maybe_transactional.
Having done this, we set the FP/VMX bit in the thread's MSR after
reclaim to indicate that that part of the state is now valid
(having been reclaimed from the processor's checkpointed state).
Finally, in the signal handling code, we move the clearing of the
transactional state bits in the thread's MSR a bit earlier, before
calling flush_fp_to_thread(), so that we don't unnecessarily set
the TIF_RESTORE_TM bit.
This is the test program:
/* Michael Neuling 4/12/2013
*
* See if the altivec state is leaked out of an aborted transaction due to
* kernel vmx copy loops.
*
* gcc -m64 htm_vmxcopy.c -o htm_vmxcopy
*
*/
/* We don't use all of these, but for reference: */
int main(int argc, char *argv[])
{
long double vecin = 1.3;
long double vecout;
unsigned long pgsize = getpagesize();
int i;
int fd;
int size = pgsize*16;
char tmpfile[] = "/tmp/page_faultXXXXXX";
char buf[pgsize];
char *a;
uint64_t aborted = 0;
fd = mkstemp(tmpfile);
assert(fd >= 0);
memset(buf, 0, pgsize);
for (i = 0; i < size; i += pgsize)
assert(write(fd, buf, pgsize) == pgsize);
unlink(tmpfile);
a = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
assert(a != MAP_FAILED);
asm __volatile__(
"lxvd2x 40,0,%[vecinptr] ; " // set 40 to initial value
TBEGIN
"beq 3f ;"
TSUSPEND
"xxlxor 40,40,40 ; " // set 40 to 0
"std 5, 0(%[map]) ;" // cause kernel vmx copy page
TABORT
TRESUME
TEND
"li %[res], 0 ;"
"b 5f ;"
"3: ;" // Abort handler
"li %[res], 1 ;"
"5: ;"
"stxvd2x 40,0,%[vecoutptr] ; "
: [res]"=r"(aborted)
: [vecinptr]"r"(&vecin),
[vecoutptr]"r"(&vecout),
[map]"r"(a)
: "memory", "r0", "r3", "r4", "r5", "r6", "r7");
if (aborted && (vecin != vecout)){
printf("FAILED: vector state leaked on abort %f != %f\n",
(double)vecin, (double)vecout);
exit(1);
}
munmap(a, size);
close(fd);
printf("PASSED!\n");
return 0;
}
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2014-01-13 12:56:29 +08:00
|
|
|
|
2013-02-14 00:21:37 +08:00
|
|
|
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
|
powerpc: Don't corrupt transactional state when using FP/VMX in kernel
Currently, when we have a process using the transactional memory
facilities on POWER8 (that is, the processor is in transactional
or suspended state), and the process enters the kernel and the
kernel then uses the floating-point or vector (VMX/Altivec) facility,
we end up corrupting the user-visible FP/VMX/VSX state. This
happens, for example, if a page fault causes a copy-on-write
operation, because the copy_page function will use VMX to do the
copy on POWER8. The test program below demonstrates the bug.
The bug happens because when FP/VMX state for a transactional process
is stored in the thread_struct, we store the checkpointed state in
.fp_state/.vr_state and the transactional (current) state in
.transact_fp/.transact_vr. However, when the kernel wants to use
FP/VMX, it calls enable_kernel_fp() or enable_kernel_altivec(),
which saves the current state in .fp_state/.vr_state. Furthermore,
when we return to the user process we return with FP/VMX/VSX
disabled. The next time the process uses FP/VMX/VSX, we don't know
which set of state (the current register values, .fp_state/.vr_state,
or .transact_fp/.transact_vr) we should be using, since we have no
way to tell if we are still in the same transaction, and if not,
whether the previous transaction succeeded or failed.
Thus it is necessary to strictly adhere to the rule that if FP has
been enabled at any point in a transaction, we must keep FP enabled
for the user process with the current transactional state in the
FP registers, until we detect that it is no longer in a transaction.
Similarly for VMX; once enabled it must stay enabled until the
process is no longer transactional.
In order to keep this rule, we add a new thread_info flag which we
test when returning from the kernel to userspace, called TIF_RESTORE_TM.
This flag indicates that there is FP/VMX/VSX state to be restored
before entering userspace, and when it is set the .tm_orig_msr field
in the thread_struct indicates what state needs to be restored.
The restoration is done by restore_tm_state(). The TIF_RESTORE_TM
bit is set by new giveup_fpu/altivec_maybe_transactional helpers,
which are called from enable_kernel_fp/altivec, giveup_vsx, and
flush_fp/altivec_to_thread instead of giveup_fpu/altivec.
The other thing to be done is to get the transactional FP/VMX/VSX
state from .fp_state/.vr_state when doing reclaim, if that state
has been saved there by giveup_fpu/altivec_maybe_transactional.
Having done this, we set the FP/VMX bit in the thread's MSR after
reclaim to indicate that that part of the state is now valid
(having been reclaimed from the processor's checkpointed state).
Finally, in the signal handling code, we move the clearing of the
transactional state bits in the thread's MSR a bit earlier, before
calling flush_fp_to_thread(), so that we don't unnecessarily set
the TIF_RESTORE_TM bit.
This is the test program:
/* Michael Neuling 4/12/2013
*
* See if the altivec state is leaked out of an aborted transaction due to
* kernel vmx copy loops.
*
* gcc -m64 htm_vmxcopy.c -o htm_vmxcopy
*
*/
/* We don't use all of these, but for reference: */
int main(int argc, char *argv[])
{
long double vecin = 1.3;
long double vecout;
unsigned long pgsize = getpagesize();
int i;
int fd;
int size = pgsize*16;
char tmpfile[] = "/tmp/page_faultXXXXXX";
char buf[pgsize];
char *a;
uint64_t aborted = 0;
fd = mkstemp(tmpfile);
assert(fd >= 0);
memset(buf, 0, pgsize);
for (i = 0; i < size; i += pgsize)
assert(write(fd, buf, pgsize) == pgsize);
unlink(tmpfile);
a = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
assert(a != MAP_FAILED);
asm __volatile__(
"lxvd2x 40,0,%[vecinptr] ; " // set 40 to initial value
TBEGIN
"beq 3f ;"
TSUSPEND
"xxlxor 40,40,40 ; " // set 40 to 0
"std 5, 0(%[map]) ;" // cause kernel vmx copy page
TABORT
TRESUME
TEND
"li %[res], 0 ;"
"b 5f ;"
"3: ;" // Abort handler
"li %[res], 1 ;"
"5: ;"
"stxvd2x 40,0,%[vecoutptr] ; "
: [res]"=r"(aborted)
: [vecinptr]"r"(&vecin),
[vecoutptr]"r"(&vecout),
[map]"r"(a)
: "memory", "r0", "r3", "r4", "r5", "r6", "r7");
if (aborted && (vecin != vecout)){
printf("FAILED: vector state leaked on abort %f != %f\n",
(double)vecin, (double)vecout);
exit(1);
}
munmap(a, size);
close(fd);
printf("PASSED!\n");
return 0;
}
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2014-01-13 12:56:29 +08:00
|
|
|
static void tm_reclaim_thread(struct thread_struct *thr,
|
|
|
|
struct thread_info *ti, uint8_t cause)
|
|
|
|
{
|
|
|
|
unsigned long msr_diff = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If FP/VSX registers have been already saved to the
|
|
|
|
* thread_struct, move them to the transact_fp array.
|
|
|
|
* We clear the TIF_RESTORE_TM bit since after the reclaim
|
|
|
|
* the thread will no longer be transactional.
|
|
|
|
*/
|
|
|
|
if (test_ti_thread_flag(ti, TIF_RESTORE_TM)) {
|
|
|
|
msr_diff = thr->tm_orig_msr & ~thr->regs->msr;
|
|
|
|
if (msr_diff & MSR_FP)
|
|
|
|
memcpy(&thr->transact_fp, &thr->fp_state,
|
|
|
|
sizeof(struct thread_fp_state));
|
|
|
|
if (msr_diff & MSR_VEC)
|
|
|
|
memcpy(&thr->transact_vr, &thr->vr_state,
|
|
|
|
sizeof(struct thread_vr_state));
|
|
|
|
clear_ti_thread_flag(ti, TIF_RESTORE_TM);
|
|
|
|
msr_diff &= MSR_FP | MSR_VEC | MSR_VSX | MSR_FE0 | MSR_FE1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tm_reclaim(thr, thr->regs->msr, cause);
|
|
|
|
|
|
|
|
/* Having done the reclaim, we now have the checkpointed
|
|
|
|
* FP/VSX values in the registers. These might be valid
|
|
|
|
* even if we have previously called enable_kernel_fp() or
|
|
|
|
* flush_fp_to_thread(), so update thr->regs->msr to
|
|
|
|
* indicate their current validity.
|
|
|
|
*/
|
|
|
|
thr->regs->msr |= msr_diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tm_reclaim_current(uint8_t cause)
|
|
|
|
{
|
|
|
|
tm_enable();
|
|
|
|
tm_reclaim_thread(¤t->thread, current_thread_info(), cause);
|
|
|
|
}
|
|
|
|
|
2013-02-14 00:21:37 +08:00
|
|
|
static inline void tm_reclaim_task(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
/* We have to work out if we're switching from/to a task that's in the
|
|
|
|
* middle of a transaction.
|
|
|
|
*
|
|
|
|
* In switching we need to maintain a 2nd register state as
|
|
|
|
* oldtask->thread.ckpt_regs. We tm_reclaim(oldproc); this saves the
|
|
|
|
* checkpointed (tbegin) state in ckpt_regs and saves the transactional
|
|
|
|
* (current) FPRs into oldtask->thread.transact_fpr[].
|
|
|
|
*
|
|
|
|
* We also context switch (save) TFHAR/TEXASR/TFIAR in here.
|
|
|
|
*/
|
|
|
|
struct thread_struct *thr = &tsk->thread;
|
|
|
|
|
|
|
|
if (!thr->regs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!MSR_TM_ACTIVE(thr->regs->msr))
|
|
|
|
goto out_and_saveregs;
|
|
|
|
|
|
|
|
/* Stash the original thread MSR, as giveup_fpu et al will
|
|
|
|
* modify it. We hold onto it to see whether the task used
|
powerpc: Don't corrupt transactional state when using FP/VMX in kernel
Currently, when we have a process using the transactional memory
facilities on POWER8 (that is, the processor is in transactional
or suspended state), and the process enters the kernel and the
kernel then uses the floating-point or vector (VMX/Altivec) facility,
we end up corrupting the user-visible FP/VMX/VSX state. This
happens, for example, if a page fault causes a copy-on-write
operation, because the copy_page function will use VMX to do the
copy on POWER8. The test program below demonstrates the bug.
The bug happens because when FP/VMX state for a transactional process
is stored in the thread_struct, we store the checkpointed state in
.fp_state/.vr_state and the transactional (current) state in
.transact_fp/.transact_vr. However, when the kernel wants to use
FP/VMX, it calls enable_kernel_fp() or enable_kernel_altivec(),
which saves the current state in .fp_state/.vr_state. Furthermore,
when we return to the user process we return with FP/VMX/VSX
disabled. The next time the process uses FP/VMX/VSX, we don't know
which set of state (the current register values, .fp_state/.vr_state,
or .transact_fp/.transact_vr) we should be using, since we have no
way to tell if we are still in the same transaction, and if not,
whether the previous transaction succeeded or failed.
Thus it is necessary to strictly adhere to the rule that if FP has
been enabled at any point in a transaction, we must keep FP enabled
for the user process with the current transactional state in the
FP registers, until we detect that it is no longer in a transaction.
Similarly for VMX; once enabled it must stay enabled until the
process is no longer transactional.
In order to keep this rule, we add a new thread_info flag which we
test when returning from the kernel to userspace, called TIF_RESTORE_TM.
This flag indicates that there is FP/VMX/VSX state to be restored
before entering userspace, and when it is set the .tm_orig_msr field
in the thread_struct indicates what state needs to be restored.
The restoration is done by restore_tm_state(). The TIF_RESTORE_TM
bit is set by new giveup_fpu/altivec_maybe_transactional helpers,
which are called from enable_kernel_fp/altivec, giveup_vsx, and
flush_fp/altivec_to_thread instead of giveup_fpu/altivec.
The other thing to be done is to get the transactional FP/VMX/VSX
state from .fp_state/.vr_state when doing reclaim, if that state
has been saved there by giveup_fpu/altivec_maybe_transactional.
Having done this, we set the FP/VMX bit in the thread's MSR after
reclaim to indicate that that part of the state is now valid
(having been reclaimed from the processor's checkpointed state).
Finally, in the signal handling code, we move the clearing of the
transactional state bits in the thread's MSR a bit earlier, before
calling flush_fp_to_thread(), so that we don't unnecessarily set
the TIF_RESTORE_TM bit.
This is the test program:
/* Michael Neuling 4/12/2013
*
* See if the altivec state is leaked out of an aborted transaction due to
* kernel vmx copy loops.
*
* gcc -m64 htm_vmxcopy.c -o htm_vmxcopy
*
*/
/* We don't use all of these, but for reference: */
int main(int argc, char *argv[])
{
long double vecin = 1.3;
long double vecout;
unsigned long pgsize = getpagesize();
int i;
int fd;
int size = pgsize*16;
char tmpfile[] = "/tmp/page_faultXXXXXX";
char buf[pgsize];
char *a;
uint64_t aborted = 0;
fd = mkstemp(tmpfile);
assert(fd >= 0);
memset(buf, 0, pgsize);
for (i = 0; i < size; i += pgsize)
assert(write(fd, buf, pgsize) == pgsize);
unlink(tmpfile);
a = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
assert(a != MAP_FAILED);
asm __volatile__(
"lxvd2x 40,0,%[vecinptr] ; " // set 40 to initial value
TBEGIN
"beq 3f ;"
TSUSPEND
"xxlxor 40,40,40 ; " // set 40 to 0
"std 5, 0(%[map]) ;" // cause kernel vmx copy page
TABORT
TRESUME
TEND
"li %[res], 0 ;"
"b 5f ;"
"3: ;" // Abort handler
"li %[res], 1 ;"
"5: ;"
"stxvd2x 40,0,%[vecoutptr] ; "
: [res]"=r"(aborted)
: [vecinptr]"r"(&vecin),
[vecoutptr]"r"(&vecout),
[map]"r"(a)
: "memory", "r0", "r3", "r4", "r5", "r6", "r7");
if (aborted && (vecin != vecout)){
printf("FAILED: vector state leaked on abort %f != %f\n",
(double)vecin, (double)vecout);
exit(1);
}
munmap(a, size);
close(fd);
printf("PASSED!\n");
return 0;
}
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2014-01-13 12:56:29 +08:00
|
|
|
* FP & vector regs. If the TIF_RESTORE_TM flag is set,
|
|
|
|
* tm_orig_msr is already set.
|
2013-02-14 00:21:37 +08:00
|
|
|
*/
|
powerpc: Don't corrupt transactional state when using FP/VMX in kernel
Currently, when we have a process using the transactional memory
facilities on POWER8 (that is, the processor is in transactional
or suspended state), and the process enters the kernel and the
kernel then uses the floating-point or vector (VMX/Altivec) facility,
we end up corrupting the user-visible FP/VMX/VSX state. This
happens, for example, if a page fault causes a copy-on-write
operation, because the copy_page function will use VMX to do the
copy on POWER8. The test program below demonstrates the bug.
The bug happens because when FP/VMX state for a transactional process
is stored in the thread_struct, we store the checkpointed state in
.fp_state/.vr_state and the transactional (current) state in
.transact_fp/.transact_vr. However, when the kernel wants to use
FP/VMX, it calls enable_kernel_fp() or enable_kernel_altivec(),
which saves the current state in .fp_state/.vr_state. Furthermore,
when we return to the user process we return with FP/VMX/VSX
disabled. The next time the process uses FP/VMX/VSX, we don't know
which set of state (the current register values, .fp_state/.vr_state,
or .transact_fp/.transact_vr) we should be using, since we have no
way to tell if we are still in the same transaction, and if not,
whether the previous transaction succeeded or failed.
Thus it is necessary to strictly adhere to the rule that if FP has
been enabled at any point in a transaction, we must keep FP enabled
for the user process with the current transactional state in the
FP registers, until we detect that it is no longer in a transaction.
Similarly for VMX; once enabled it must stay enabled until the
process is no longer transactional.
In order to keep this rule, we add a new thread_info flag which we
test when returning from the kernel to userspace, called TIF_RESTORE_TM.
This flag indicates that there is FP/VMX/VSX state to be restored
before entering userspace, and when it is set the .tm_orig_msr field
in the thread_struct indicates what state needs to be restored.
The restoration is done by restore_tm_state(). The TIF_RESTORE_TM
bit is set by new giveup_fpu/altivec_maybe_transactional helpers,
which are called from enable_kernel_fp/altivec, giveup_vsx, and
flush_fp/altivec_to_thread instead of giveup_fpu/altivec.
The other thing to be done is to get the transactional FP/VMX/VSX
state from .fp_state/.vr_state when doing reclaim, if that state
has been saved there by giveup_fpu/altivec_maybe_transactional.
Having done this, we set the FP/VMX bit in the thread's MSR after
reclaim to indicate that that part of the state is now valid
(having been reclaimed from the processor's checkpointed state).
Finally, in the signal handling code, we move the clearing of the
transactional state bits in the thread's MSR a bit earlier, before
calling flush_fp_to_thread(), so that we don't unnecessarily set
the TIF_RESTORE_TM bit.
This is the test program:
/* Michael Neuling 4/12/2013
*
* See if the altivec state is leaked out of an aborted transaction due to
* kernel vmx copy loops.
*
* gcc -m64 htm_vmxcopy.c -o htm_vmxcopy
*
*/
/* We don't use all of these, but for reference: */
int main(int argc, char *argv[])
{
long double vecin = 1.3;
long double vecout;
unsigned long pgsize = getpagesize();
int i;
int fd;
int size = pgsize*16;
char tmpfile[] = "/tmp/page_faultXXXXXX";
char buf[pgsize];
char *a;
uint64_t aborted = 0;
fd = mkstemp(tmpfile);
assert(fd >= 0);
memset(buf, 0, pgsize);
for (i = 0; i < size; i += pgsize)
assert(write(fd, buf, pgsize) == pgsize);
unlink(tmpfile);
a = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
assert(a != MAP_FAILED);
asm __volatile__(
"lxvd2x 40,0,%[vecinptr] ; " // set 40 to initial value
TBEGIN
"beq 3f ;"
TSUSPEND
"xxlxor 40,40,40 ; " // set 40 to 0
"std 5, 0(%[map]) ;" // cause kernel vmx copy page
TABORT
TRESUME
TEND
"li %[res], 0 ;"
"b 5f ;"
"3: ;" // Abort handler
"li %[res], 1 ;"
"5: ;"
"stxvd2x 40,0,%[vecoutptr] ; "
: [res]"=r"(aborted)
: [vecinptr]"r"(&vecin),
[vecoutptr]"r"(&vecout),
[map]"r"(a)
: "memory", "r0", "r3", "r4", "r5", "r6", "r7");
if (aborted && (vecin != vecout)){
printf("FAILED: vector state leaked on abort %f != %f\n",
(double)vecin, (double)vecout);
exit(1);
}
munmap(a, size);
close(fd);
printf("PASSED!\n");
return 0;
}
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2014-01-13 12:56:29 +08:00
|
|
|
if (!test_ti_thread_flag(task_thread_info(tsk), TIF_RESTORE_TM))
|
|
|
|
thr->tm_orig_msr = thr->regs->msr;
|
2013-02-14 00:21:37 +08:00
|
|
|
|
|
|
|
TM_DEBUG("--- tm_reclaim on pid %d (NIP=%lx, "
|
|
|
|
"ccr=%lx, msr=%lx, trap=%lx)\n",
|
|
|
|
tsk->pid, thr->regs->nip,
|
|
|
|
thr->regs->ccr, thr->regs->msr,
|
|
|
|
thr->regs->trap);
|
|
|
|
|
powerpc: Don't corrupt transactional state when using FP/VMX in kernel
Currently, when we have a process using the transactional memory
facilities on POWER8 (that is, the processor is in transactional
or suspended state), and the process enters the kernel and the
kernel then uses the floating-point or vector (VMX/Altivec) facility,
we end up corrupting the user-visible FP/VMX/VSX state. This
happens, for example, if a page fault causes a copy-on-write
operation, because the copy_page function will use VMX to do the
copy on POWER8. The test program below demonstrates the bug.
The bug happens because when FP/VMX state for a transactional process
is stored in the thread_struct, we store the checkpointed state in
.fp_state/.vr_state and the transactional (current) state in
.transact_fp/.transact_vr. However, when the kernel wants to use
FP/VMX, it calls enable_kernel_fp() or enable_kernel_altivec(),
which saves the current state in .fp_state/.vr_state. Furthermore,
when we return to the user process we return with FP/VMX/VSX
disabled. The next time the process uses FP/VMX/VSX, we don't know
which set of state (the current register values, .fp_state/.vr_state,
or .transact_fp/.transact_vr) we should be using, since we have no
way to tell if we are still in the same transaction, and if not,
whether the previous transaction succeeded or failed.
Thus it is necessary to strictly adhere to the rule that if FP has
been enabled at any point in a transaction, we must keep FP enabled
for the user process with the current transactional state in the
FP registers, until we detect that it is no longer in a transaction.
Similarly for VMX; once enabled it must stay enabled until the
process is no longer transactional.
In order to keep this rule, we add a new thread_info flag which we
test when returning from the kernel to userspace, called TIF_RESTORE_TM.
This flag indicates that there is FP/VMX/VSX state to be restored
before entering userspace, and when it is set the .tm_orig_msr field
in the thread_struct indicates what state needs to be restored.
The restoration is done by restore_tm_state(). The TIF_RESTORE_TM
bit is set by new giveup_fpu/altivec_maybe_transactional helpers,
which are called from enable_kernel_fp/altivec, giveup_vsx, and
flush_fp/altivec_to_thread instead of giveup_fpu/altivec.
The other thing to be done is to get the transactional FP/VMX/VSX
state from .fp_state/.vr_state when doing reclaim, if that state
has been saved there by giveup_fpu/altivec_maybe_transactional.
Having done this, we set the FP/VMX bit in the thread's MSR after
reclaim to indicate that that part of the state is now valid
(having been reclaimed from the processor's checkpointed state).
Finally, in the signal handling code, we move the clearing of the
transactional state bits in the thread's MSR a bit earlier, before
calling flush_fp_to_thread(), so that we don't unnecessarily set
the TIF_RESTORE_TM bit.
This is the test program:
/* Michael Neuling 4/12/2013
*
* See if the altivec state is leaked out of an aborted transaction due to
* kernel vmx copy loops.
*
* gcc -m64 htm_vmxcopy.c -o htm_vmxcopy
*
*/
/* We don't use all of these, but for reference: */
int main(int argc, char *argv[])
{
long double vecin = 1.3;
long double vecout;
unsigned long pgsize = getpagesize();
int i;
int fd;
int size = pgsize*16;
char tmpfile[] = "/tmp/page_faultXXXXXX";
char buf[pgsize];
char *a;
uint64_t aborted = 0;
fd = mkstemp(tmpfile);
assert(fd >= 0);
memset(buf, 0, pgsize);
for (i = 0; i < size; i += pgsize)
assert(write(fd, buf, pgsize) == pgsize);
unlink(tmpfile);
a = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
assert(a != MAP_FAILED);
asm __volatile__(
"lxvd2x 40,0,%[vecinptr] ; " // set 40 to initial value
TBEGIN
"beq 3f ;"
TSUSPEND
"xxlxor 40,40,40 ; " // set 40 to 0
"std 5, 0(%[map]) ;" // cause kernel vmx copy page
TABORT
TRESUME
TEND
"li %[res], 0 ;"
"b 5f ;"
"3: ;" // Abort handler
"li %[res], 1 ;"
"5: ;"
"stxvd2x 40,0,%[vecoutptr] ; "
: [res]"=r"(aborted)
: [vecinptr]"r"(&vecin),
[vecoutptr]"r"(&vecout),
[map]"r"(a)
: "memory", "r0", "r3", "r4", "r5", "r6", "r7");
if (aborted && (vecin != vecout)){
printf("FAILED: vector state leaked on abort %f != %f\n",
(double)vecin, (double)vecout);
exit(1);
}
munmap(a, size);
close(fd);
printf("PASSED!\n");
return 0;
}
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2014-01-13 12:56:29 +08:00
|
|
|
tm_reclaim_thread(thr, task_thread_info(tsk), TM_CAUSE_RESCHED);
|
2013-02-14 00:21:37 +08:00
|
|
|
|
|
|
|
TM_DEBUG("--- tm_reclaim on pid %d complete\n",
|
|
|
|
tsk->pid);
|
|
|
|
|
|
|
|
out_and_saveregs:
|
|
|
|
/* Always save the regs here, even if a transaction's not active.
|
|
|
|
* This context-switches a thread's TM info SPRs. We do it here to
|
|
|
|
* be consistent with the restore path (in recheckpoint) which
|
|
|
|
* cannot happen later in _switch().
|
|
|
|
*/
|
|
|
|
tm_save_sprs(thr);
|
|
|
|
}
|
|
|
|
|
2014-04-04 17:19:48 +08:00
|
|
|
extern void __tm_recheckpoint(struct thread_struct *thread,
|
|
|
|
unsigned long orig_msr);
|
|
|
|
|
|
|
|
void tm_recheckpoint(struct thread_struct *thread,
|
|
|
|
unsigned long orig_msr)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
/* We really can't be interrupted here as the TEXASR registers can't
|
|
|
|
* change and later in the trecheckpoint code, we have a userspace R1.
|
|
|
|
* So let's hard disable over this region.
|
|
|
|
*/
|
|
|
|
local_irq_save(flags);
|
|
|
|
hard_irq_disable();
|
|
|
|
|
|
|
|
/* The TM SPRs are restored here, so that TEXASR.FS can be set
|
|
|
|
* before the trecheckpoint and no explosion occurs.
|
|
|
|
*/
|
|
|
|
tm_restore_sprs(thread);
|
|
|
|
|
|
|
|
__tm_recheckpoint(thread, orig_msr);
|
|
|
|
|
|
|
|
local_irq_restore(flags);
|
|
|
|
}
|
|
|
|
|
2013-02-14 00:21:40 +08:00
|
|
|
static inline void tm_recheckpoint_new_task(struct task_struct *new)
|
2013-02-14 00:21:37 +08:00
|
|
|
{
|
|
|
|
unsigned long msr;
|
|
|
|
|
|
|
|
if (!cpu_has_feature(CPU_FTR_TM))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Recheckpoint the registers of the thread we're about to switch to.
|
|
|
|
*
|
|
|
|
* If the task was using FP, we non-lazily reload both the original and
|
|
|
|
* the speculative FP register states. This is because the kernel
|
|
|
|
* doesn't see if/when a TM rollback occurs, so if we take an FP
|
|
|
|
* unavoidable later, we are unable to determine which set of FP regs
|
|
|
|
* need to be restored.
|
|
|
|
*/
|
|
|
|
if (!new->thread.regs)
|
|
|
|
return;
|
|
|
|
|
2014-04-04 17:19:48 +08:00
|
|
|
if (!MSR_TM_ACTIVE(new->thread.regs->msr)){
|
|
|
|
tm_restore_sprs(&new->thread);
|
2013-02-14 00:21:37 +08:00
|
|
|
return;
|
2014-04-04 17:19:48 +08:00
|
|
|
}
|
2013-02-14 00:21:37 +08:00
|
|
|
msr = new->thread.tm_orig_msr;
|
|
|
|
/* Recheckpoint to restore original checkpointed register state. */
|
|
|
|
TM_DEBUG("*** tm_recheckpoint of pid %d "
|
|
|
|
"(new->msr 0x%lx, new->origmsr 0x%lx)\n",
|
|
|
|
new->pid, new->thread.regs->msr, msr);
|
|
|
|
|
|
|
|
/* This loads the checkpointed FP/VEC state, if used */
|
|
|
|
tm_recheckpoint(&new->thread, msr);
|
|
|
|
|
|
|
|
/* This loads the speculative FP/VEC state, if used */
|
|
|
|
if (msr & MSR_FP) {
|
|
|
|
do_load_up_transact_fpu(&new->thread);
|
|
|
|
new->thread.regs->msr |=
|
|
|
|
(MSR_FP | new->thread.fpexc_mode);
|
|
|
|
}
|
2013-04-09 14:18:55 +08:00
|
|
|
#ifdef CONFIG_ALTIVEC
|
2013-02-14 00:21:37 +08:00
|
|
|
if (msr & MSR_VEC) {
|
|
|
|
do_load_up_transact_altivec(&new->thread);
|
|
|
|
new->thread.regs->msr |= MSR_VEC;
|
|
|
|
}
|
2013-04-09 14:18:55 +08:00
|
|
|
#endif
|
2013-02-14 00:21:37 +08:00
|
|
|
/* We may as well turn on VSX too since all the state is restored now */
|
|
|
|
if (msr & MSR_VSX)
|
|
|
|
new->thread.regs->msr |= MSR_VSX;
|
|
|
|
|
|
|
|
TM_DEBUG("*** tm_recheckpoint of pid %d complete "
|
|
|
|
"(kernel msr 0x%lx)\n",
|
|
|
|
new->pid, mfmsr());
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void __switch_to_tm(struct task_struct *prev)
|
|
|
|
{
|
|
|
|
if (cpu_has_feature(CPU_FTR_TM)) {
|
|
|
|
tm_enable();
|
|
|
|
tm_reclaim_task(prev);
|
|
|
|
}
|
|
|
|
}
|
powerpc: Don't corrupt transactional state when using FP/VMX in kernel
Currently, when we have a process using the transactional memory
facilities on POWER8 (that is, the processor is in transactional
or suspended state), and the process enters the kernel and the
kernel then uses the floating-point or vector (VMX/Altivec) facility,
we end up corrupting the user-visible FP/VMX/VSX state. This
happens, for example, if a page fault causes a copy-on-write
operation, because the copy_page function will use VMX to do the
copy on POWER8. The test program below demonstrates the bug.
The bug happens because when FP/VMX state for a transactional process
is stored in the thread_struct, we store the checkpointed state in
.fp_state/.vr_state and the transactional (current) state in
.transact_fp/.transact_vr. However, when the kernel wants to use
FP/VMX, it calls enable_kernel_fp() or enable_kernel_altivec(),
which saves the current state in .fp_state/.vr_state. Furthermore,
when we return to the user process we return with FP/VMX/VSX
disabled. The next time the process uses FP/VMX/VSX, we don't know
which set of state (the current register values, .fp_state/.vr_state,
or .transact_fp/.transact_vr) we should be using, since we have no
way to tell if we are still in the same transaction, and if not,
whether the previous transaction succeeded or failed.
Thus it is necessary to strictly adhere to the rule that if FP has
been enabled at any point in a transaction, we must keep FP enabled
for the user process with the current transactional state in the
FP registers, until we detect that it is no longer in a transaction.
Similarly for VMX; once enabled it must stay enabled until the
process is no longer transactional.
In order to keep this rule, we add a new thread_info flag which we
test when returning from the kernel to userspace, called TIF_RESTORE_TM.
This flag indicates that there is FP/VMX/VSX state to be restored
before entering userspace, and when it is set the .tm_orig_msr field
in the thread_struct indicates what state needs to be restored.
The restoration is done by restore_tm_state(). The TIF_RESTORE_TM
bit is set by new giveup_fpu/altivec_maybe_transactional helpers,
which are called from enable_kernel_fp/altivec, giveup_vsx, and
flush_fp/altivec_to_thread instead of giveup_fpu/altivec.
The other thing to be done is to get the transactional FP/VMX/VSX
state from .fp_state/.vr_state when doing reclaim, if that state
has been saved there by giveup_fpu/altivec_maybe_transactional.
Having done this, we set the FP/VMX bit in the thread's MSR after
reclaim to indicate that that part of the state is now valid
(having been reclaimed from the processor's checkpointed state).
Finally, in the signal handling code, we move the clearing of the
transactional state bits in the thread's MSR a bit earlier, before
calling flush_fp_to_thread(), so that we don't unnecessarily set
the TIF_RESTORE_TM bit.
This is the test program:
/* Michael Neuling 4/12/2013
*
* See if the altivec state is leaked out of an aborted transaction due to
* kernel vmx copy loops.
*
* gcc -m64 htm_vmxcopy.c -o htm_vmxcopy
*
*/
/* We don't use all of these, but for reference: */
int main(int argc, char *argv[])
{
long double vecin = 1.3;
long double vecout;
unsigned long pgsize = getpagesize();
int i;
int fd;
int size = pgsize*16;
char tmpfile[] = "/tmp/page_faultXXXXXX";
char buf[pgsize];
char *a;
uint64_t aborted = 0;
fd = mkstemp(tmpfile);
assert(fd >= 0);
memset(buf, 0, pgsize);
for (i = 0; i < size; i += pgsize)
assert(write(fd, buf, pgsize) == pgsize);
unlink(tmpfile);
a = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
assert(a != MAP_FAILED);
asm __volatile__(
"lxvd2x 40,0,%[vecinptr] ; " // set 40 to initial value
TBEGIN
"beq 3f ;"
TSUSPEND
"xxlxor 40,40,40 ; " // set 40 to 0
"std 5, 0(%[map]) ;" // cause kernel vmx copy page
TABORT
TRESUME
TEND
"li %[res], 0 ;"
"b 5f ;"
"3: ;" // Abort handler
"li %[res], 1 ;"
"5: ;"
"stxvd2x 40,0,%[vecoutptr] ; "
: [res]"=r"(aborted)
: [vecinptr]"r"(&vecin),
[vecoutptr]"r"(&vecout),
[map]"r"(a)
: "memory", "r0", "r3", "r4", "r5", "r6", "r7");
if (aborted && (vecin != vecout)){
printf("FAILED: vector state leaked on abort %f != %f\n",
(double)vecin, (double)vecout);
exit(1);
}
munmap(a, size);
close(fd);
printf("PASSED!\n");
return 0;
}
Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2014-01-13 12:56:29 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is called if we are on the way out to userspace and the
|
|
|
|
* TIF_RESTORE_TM flag is set. It checks if we need to reload
|
|
|
|
* FP and/or vector state and does so if necessary.
|
|
|
|
* If userspace is inside a transaction (whether active or
|
|
|
|
* suspended) and FP/VMX/VSX instructions have ever been enabled
|
|
|
|
* inside that transaction, then we have to keep them enabled
|
|
|
|
* and keep the FP/VMX/VSX state loaded while ever the transaction
|
|
|
|
* continues. The reason is that if we didn't, and subsequently
|
|
|
|
* got a FP/VMX/VSX unavailable interrupt inside a transaction,
|
|
|
|
* we don't know whether it's the same transaction, and thus we
|
|
|
|
* don't know which of the checkpointed state and the transactional
|
|
|
|
* state to use.
|
|
|
|
*/
|
|
|
|
void restore_tm_state(struct pt_regs *regs)
|
|
|
|
{
|
|
|
|
unsigned long msr_diff;
|
|
|
|
|
|
|
|
clear_thread_flag(TIF_RESTORE_TM);
|
|
|
|
if (!MSR_TM_ACTIVE(regs->msr))
|
|
|
|
return;
|
|
|
|
|
|
|
|
msr_diff = current->thread.tm_orig_msr & ~regs->msr;
|
|
|
|
msr_diff &= MSR_FP | MSR_VEC | MSR_VSX;
|
|
|
|
if (msr_diff & MSR_FP) {
|
|
|
|
fp_enable();
|
|
|
|
load_fp_state(¤t->thread.fp_state);
|
|
|
|
regs->msr |= current->thread.fpexc_mode;
|
|
|
|
}
|
|
|
|
if (msr_diff & MSR_VEC) {
|
|
|
|
vec_enable();
|
|
|
|
load_vr_state(¤t->thread.vr_state);
|
|
|
|
}
|
|
|
|
regs->msr |= msr_diff;
|
|
|
|
}
|
|
|
|
|
2013-02-14 00:21:37 +08:00
|
|
|
#else
|
|
|
|
#define tm_recheckpoint_new_task(new)
|
|
|
|
#define __switch_to_tm(prev)
|
|
|
|
#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
|
2012-12-20 22:06:44 +08:00
|
|
|
|
2005-09-26 14:04:21 +08:00
|
|
|
struct task_struct *__switch_to(struct task_struct *prev,
|
|
|
|
struct task_struct *new)
|
|
|
|
{
|
|
|
|
struct thread_struct *new_thread, *old_thread;
|
|
|
|
struct task_struct *last;
|
2011-05-25 08:11:48 +08:00
|
|
|
#ifdef CONFIG_PPC_BOOK3S_64
|
|
|
|
struct ppc64_tlb_batch *batch;
|
|
|
|
#endif
|
2005-09-26 14:04:21 +08:00
|
|
|
|
2013-10-02 15:15:14 +08:00
|
|
|
WARN_ON(!irqs_disabled());
|
|
|
|
|
2014-06-05 14:19:22 +08:00
|
|
|
/* Back up the TAR and DSCR across context switches.
|
2013-08-09 15:29:30 +08:00
|
|
|
* Note that the TAR is not available for use in the kernel. (To
|
|
|
|
* provide this, the TAR should be backed up/restored on exception
|
|
|
|
* entry/exit instead, and be in pt_regs. FIXME, this should be in
|
|
|
|
* pt_regs anyway (for debug).)
|
2014-06-05 14:19:22 +08:00
|
|
|
* Save the TAR and DSCR here before we do treclaim/trecheckpoint as
|
|
|
|
* these will change them.
|
2013-08-09 15:29:30 +08:00
|
|
|
*/
|
2014-06-05 14:19:22 +08:00
|
|
|
save_early_sprs(&prev->thread);
|
2013-08-09 15:29:30 +08:00
|
|
|
|
2013-02-14 00:21:40 +08:00
|
|
|
__switch_to_tm(prev);
|
|
|
|
|
2005-09-26 14:04:21 +08:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
/* avoid complexity of lazy save/restore of fpu
|
|
|
|
* by just saving it every time we switch out if
|
|
|
|
* this task used the fpu during the last quantum.
|
|
|
|
*
|
|
|
|
* If it tries to use the fpu again, it'll trap and
|
|
|
|
* reload its fp regs. So we don't have to do a restore
|
|
|
|
* every switch, just a save.
|
|
|
|
* -- Cort
|
|
|
|
*/
|
|
|
|
if (prev->thread.regs && (prev->thread.regs->msr & MSR_FP))
|
|
|
|
giveup_fpu(prev);
|
|
|
|
#ifdef CONFIG_ALTIVEC
|
|
|
|
/*
|
|
|
|
* If the previous thread used altivec in the last quantum
|
|
|
|
* (thus changing altivec regs) then save them.
|
|
|
|
* We used to check the VRSAVE register but not all apps
|
|
|
|
* set it, so we don't rely on it now (and in fact we need
|
|
|
|
* to save & restore VSCR even if VRSAVE == 0). -- paulus
|
|
|
|
*
|
|
|
|
* On SMP we always save/restore altivec regs just to avoid the
|
|
|
|
* complexity of changing processors.
|
|
|
|
* -- Cort
|
|
|
|
*/
|
|
|
|
if (prev->thread.regs && (prev->thread.regs->msr & MSR_VEC))
|
|
|
|
giveup_altivec(prev);
|
|
|
|
#endif /* CONFIG_ALTIVEC */
|
2008-06-25 12:07:18 +08:00
|
|
|
#ifdef CONFIG_VSX
|
|
|
|
if (prev->thread.regs && (prev->thread.regs->msr & MSR_VSX))
|
2008-07-11 14:29:12 +08:00
|
|
|
/* VMX and FPU registers are already save here */
|
|
|
|
__giveup_vsx(prev);
|
2008-06-25 12:07:18 +08:00
|
|
|
#endif /* CONFIG_VSX */
|
2005-09-26 14:04:21 +08:00
|
|
|
#ifdef CONFIG_SPE
|
|
|
|
/*
|
|
|
|
* If the previous thread used spe in the last quantum
|
|
|
|
* (thus changing spe regs) then save them.
|
|
|
|
*
|
|
|
|
* On SMP we always save/restore spe regs just to avoid the
|
|
|
|
* complexity of changing processors.
|
|
|
|
*/
|
|
|
|
if ((prev->thread.regs && (prev->thread.regs->msr & MSR_SPE)))
|
|
|
|
giveup_spe(prev);
|
2005-10-01 11:49:08 +08:00
|
|
|
#endif /* CONFIG_SPE */
|
|
|
|
|
|
|
|
#else /* CONFIG_SMP */
|
|
|
|
#ifdef CONFIG_ALTIVEC
|
|
|
|
/* Avoid the trap. On smp this this never happens since
|
|
|
|
* we don't set last_task_used_altivec -- Cort
|
|
|
|
*/
|
|
|
|
if (new->thread.regs && last_task_used_altivec == new)
|
|
|
|
new->thread.regs->msr |= MSR_VEC;
|
|
|
|
#endif /* CONFIG_ALTIVEC */
|
2008-06-25 12:07:18 +08:00
|
|
|
#ifdef CONFIG_VSX
|
|
|
|
if (new->thread.regs && last_task_used_vsx == new)
|
|
|
|
new->thread.regs->msr |= MSR_VSX;
|
|
|
|
#endif /* CONFIG_VSX */
|
2005-10-01 11:49:08 +08:00
|
|
|
#ifdef CONFIG_SPE
|
2005-09-26 14:04:21 +08:00
|
|
|
/* Avoid the trap. On smp this this never happens since
|
|
|
|
* we don't set last_task_used_spe
|
|
|
|
*/
|
|
|
|
if (new->thread.regs && last_task_used_spe == new)
|
|
|
|
new->thread.regs->msr |= MSR_SPE;
|
|
|
|
#endif /* CONFIG_SPE */
|
2005-10-01 11:49:08 +08:00
|
|
|
|
2005-09-26 14:04:21 +08:00
|
|
|
#endif /* CONFIG_SMP */
|
|
|
|
|
2010-02-08 19:50:57 +08:00
|
|
|
#ifdef CONFIG_PPC_ADV_DEBUG_REGS
|
2013-11-23 05:52:29 +08:00
|
|
|
switch_booke_debug_regs(&new->thread.debug);
|
2009-09-08 22:16:58 +08:00
|
|
|
#else
|
2010-06-15 14:05:19 +08:00
|
|
|
/*
|
|
|
|
* For PPC_BOOK3S_64, we use the hw-breakpoint interfaces that would
|
|
|
|
* schedule DABR
|
|
|
|
*/
|
|
|
|
#ifndef CONFIG_HAVE_HW_BREAKPOINT
|
powerpc: Replace __get_cpu_var uses
This still has not been merged and now powerpc is the only arch that does
not have this change. Sorry about missing linuxppc-dev before.
V2->V2
- Fix up to work against 3.18-rc1
__get_cpu_var() is used for multiple purposes in the kernel source. One of
them is address calculation via the form &__get_cpu_var(x). This calculates
the address for the instance of the percpu variable of the current processor
based on an offset.
Other use cases are for storing and retrieving data from the current
processors percpu area. __get_cpu_var() can be used as an lvalue when
writing data or on the right side of an assignment.
__get_cpu_var() is defined as :
__get_cpu_var() always only does an address determination. However, store
and retrieve operations could use a segment prefix (or global register on
other platforms) to avoid the address calculation.
this_cpu_write() and this_cpu_read() can directly take an offset into a
percpu area and use optimized assembly code to read and write per cpu
variables.
This patch converts __get_cpu_var into either an explicit address
calculation using this_cpu_ptr() or into a use of this_cpu operations that
use the offset. Thereby address calculations are avoided and less registers
are used when code is generated.
At the end of the patch set all uses of __get_cpu_var have been removed so
the macro is removed too.
The patch set includes passes over all arches as well. Once these operations
are used throughout then specialized macros can be defined in non -x86
arches as well in order to optimize per cpu access by f.e. using a global
register that may be set to the per cpu base.
Transformations done to __get_cpu_var()
1. Determine the address of the percpu instance of the current processor.
DEFINE_PER_CPU(int, y);
int *x = &__get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(&y);
2. Same as #1 but this time an array structure is involved.
DEFINE_PER_CPU(int, y[20]);
int *x = __get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(y);
3. Retrieve the content of the current processors instance of a per cpu
variable.
DEFINE_PER_CPU(int, y);
int x = __get_cpu_var(y)
Converts to
int x = __this_cpu_read(y);
4. Retrieve the content of a percpu struct
DEFINE_PER_CPU(struct mystruct, y);
struct mystruct x = __get_cpu_var(y);
Converts to
memcpy(&x, this_cpu_ptr(&y), sizeof(x));
5. Assignment to a per cpu variable
DEFINE_PER_CPU(int, y)
__get_cpu_var(y) = x;
Converts to
__this_cpu_write(y, x);
6. Increment/Decrement etc of a per cpu variable
DEFINE_PER_CPU(int, y);
__get_cpu_var(y)++
Converts to
__this_cpu_inc(y)
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
CC: Paul Mackerras <paulus@samba.org>
Signed-off-by: Christoph Lameter <cl@linux.com>
[mpe: Fix build errors caused by set/or_softirq_pending(), and rework
assignment in __set_breakpoint() to use memcpy().]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
2014-10-22 04:23:25 +08:00
|
|
|
if (unlikely(!hw_brk_match(this_cpu_ptr(¤t_brk), &new->thread.hw_brk)))
|
2014-04-30 03:25:17 +08:00
|
|
|
__set_breakpoint(&new->thread.hw_brk);
|
2010-06-15 14:05:19 +08:00
|
|
|
#endif /* CONFIG_HAVE_HW_BREAKPOINT */
|
2008-07-24 00:10:41 +08:00
|
|
|
#endif
|
|
|
|
|
2009-09-08 22:16:58 +08:00
|
|
|
|
2005-09-26 14:04:21 +08:00
|
|
|
new_thread = &new->thread;
|
|
|
|
old_thread = ¤t->thread;
|
2005-10-10 20:29:05 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_PPC64
|
|
|
|
/*
|
|
|
|
* Collect processor utilization data per process
|
|
|
|
*/
|
|
|
|
if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
|
powerpc: Replace __get_cpu_var uses
This still has not been merged and now powerpc is the only arch that does
not have this change. Sorry about missing linuxppc-dev before.
V2->V2
- Fix up to work against 3.18-rc1
__get_cpu_var() is used for multiple purposes in the kernel source. One of
them is address calculation via the form &__get_cpu_var(x). This calculates
the address for the instance of the percpu variable of the current processor
based on an offset.
Other use cases are for storing and retrieving data from the current
processors percpu area. __get_cpu_var() can be used as an lvalue when
writing data or on the right side of an assignment.
__get_cpu_var() is defined as :
__get_cpu_var() always only does an address determination. However, store
and retrieve operations could use a segment prefix (or global register on
other platforms) to avoid the address calculation.
this_cpu_write() and this_cpu_read() can directly take an offset into a
percpu area and use optimized assembly code to read and write per cpu
variables.
This patch converts __get_cpu_var into either an explicit address
calculation using this_cpu_ptr() or into a use of this_cpu operations that
use the offset. Thereby address calculations are avoided and less registers
are used when code is generated.
At the end of the patch set all uses of __get_cpu_var have been removed so
the macro is removed too.
The patch set includes passes over all arches as well. Once these operations
are used throughout then specialized macros can be defined in non -x86
arches as well in order to optimize per cpu access by f.e. using a global
register that may be set to the per cpu base.
Transformations done to __get_cpu_var()
1. Determine the address of the percpu instance of the current processor.
DEFINE_PER_CPU(int, y);
int *x = &__get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(&y);
2. Same as #1 but this time an array structure is involved.
DEFINE_PER_CPU(int, y[20]);
int *x = __get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(y);
3. Retrieve the content of the current processors instance of a per cpu
variable.
DEFINE_PER_CPU(int, y);
int x = __get_cpu_var(y)
Converts to
int x = __this_cpu_read(y);
4. Retrieve the content of a percpu struct
DEFINE_PER_CPU(struct mystruct, y);
struct mystruct x = __get_cpu_var(y);
Converts to
memcpy(&x, this_cpu_ptr(&y), sizeof(x));
5. Assignment to a per cpu variable
DEFINE_PER_CPU(int, y)
__get_cpu_var(y) = x;
Converts to
__this_cpu_write(y, x);
6. Increment/Decrement etc of a per cpu variable
DEFINE_PER_CPU(int, y);
__get_cpu_var(y)++
Converts to
__this_cpu_inc(y)
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
CC: Paul Mackerras <paulus@samba.org>
Signed-off-by: Christoph Lameter <cl@linux.com>
[mpe: Fix build errors caused by set/or_softirq_pending(), and rework
assignment in __set_breakpoint() to use memcpy().]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
2014-10-22 04:23:25 +08:00
|
|
|
struct cpu_usage *cu = this_cpu_ptr(&cpu_usage_array);
|
2005-10-10 20:29:05 +08:00
|
|
|
long unsigned start_tb, current_tb;
|
|
|
|
start_tb = old_thread->start_tb;
|
|
|
|
cu->current_tb = current_tb = mfspr(SPRN_PURR);
|
|
|
|
old_thread->accum_tb += (current_tb - start_tb);
|
|
|
|
new_thread->start_tb = current_tb;
|
|
|
|
}
|
2011-05-25 08:11:48 +08:00
|
|
|
#endif /* CONFIG_PPC64 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_PPC_BOOK3S_64
|
powerpc: Replace __get_cpu_var uses
This still has not been merged and now powerpc is the only arch that does
not have this change. Sorry about missing linuxppc-dev before.
V2->V2
- Fix up to work against 3.18-rc1
__get_cpu_var() is used for multiple purposes in the kernel source. One of
them is address calculation via the form &__get_cpu_var(x). This calculates
the address for the instance of the percpu variable of the current processor
based on an offset.
Other use cases are for storing and retrieving data from the current
processors percpu area. __get_cpu_var() can be used as an lvalue when
writing data or on the right side of an assignment.
__get_cpu_var() is defined as :
__get_cpu_var() always only does an address determination. However, store
and retrieve operations could use a segment prefix (or global register on
other platforms) to avoid the address calculation.
this_cpu_write() and this_cpu_read() can directly take an offset into a
percpu area and use optimized assembly code to read and write per cpu
variables.
This patch converts __get_cpu_var into either an explicit address
calculation using this_cpu_ptr() or into a use of this_cpu operations that
use the offset. Thereby address calculations are avoided and less registers
are used when code is generated.
At the end of the patch set all uses of __get_cpu_var have been removed so
the macro is removed too.
The patch set includes passes over all arches as well. Once these operations
are used throughout then specialized macros can be defined in non -x86
arches as well in order to optimize per cpu access by f.e. using a global
register that may be set to the per cpu base.
Transformations done to __get_cpu_var()
1. Determine the address of the percpu instance of the current processor.
DEFINE_PER_CPU(int, y);
int *x = &__get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(&y);
2. Same as #1 but this time an array structure is involved.
DEFINE_PER_CPU(int, y[20]);
int *x = __get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(y);
3. Retrieve the content of the current processors instance of a per cpu
variable.
DEFINE_PER_CPU(int, y);
int x = __get_cpu_var(y)
Converts to
int x = __this_cpu_read(y);
4. Retrieve the content of a percpu struct
DEFINE_PER_CPU(struct mystruct, y);
struct mystruct x = __get_cpu_var(y);
Converts to
memcpy(&x, this_cpu_ptr(&y), sizeof(x));
5. Assignment to a per cpu variable
DEFINE_PER_CPU(int, y)
__get_cpu_var(y) = x;
Converts to
__this_cpu_write(y, x);
6. Increment/Decrement etc of a per cpu variable
DEFINE_PER_CPU(int, y);
__get_cpu_var(y)++
Converts to
__this_cpu_inc(y)
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
CC: Paul Mackerras <paulus@samba.org>
Signed-off-by: Christoph Lameter <cl@linux.com>
[mpe: Fix build errors caused by set/or_softirq_pending(), and rework
assignment in __set_breakpoint() to use memcpy().]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
2014-10-22 04:23:25 +08:00
|
|
|
batch = this_cpu_ptr(&ppc64_tlb_batch);
|
2011-05-25 08:11:48 +08:00
|
|
|
if (batch->active) {
|
|
|
|
current_thread_info()->local_flags |= _TLF_LAZY_MMU;
|
|
|
|
if (batch->index)
|
|
|
|
__flush_tlb_pending(batch);
|
|
|
|
batch->active = 0;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_PPC_BOOK3S_64 */
|
2005-10-10 20:29:05 +08:00
|
|
|
|
2008-03-17 12:27:09 +08:00
|
|
|
/*
|
|
|
|
* We can't take a PMU exception inside _switch() since there is a
|
|
|
|
* window where the kernel stack SLB and the kernel stack are out
|
|
|
|
* of sync. Hard disable here.
|
|
|
|
*/
|
|
|
|
hard_irq_disable();
|
2013-02-14 00:21:40 +08:00
|
|
|
|
|
|
|
tm_recheckpoint_new_task(new);
|
|
|
|
|
2005-09-26 14:04:21 +08:00
|
|
|
last = _switch(old_thread, new_thread);
|
|
|
|
|
2011-05-25 08:11:48 +08:00
|
|
|
#ifdef CONFIG_PPC_BOOK3S_64
|
|
|
|
if (current_thread_info()->local_flags & _TLF_LAZY_MMU) {
|
|
|
|
current_thread_info()->local_flags &= ~_TLF_LAZY_MMU;
|
powerpc: Replace __get_cpu_var uses
This still has not been merged and now powerpc is the only arch that does
not have this change. Sorry about missing linuxppc-dev before.
V2->V2
- Fix up to work against 3.18-rc1
__get_cpu_var() is used for multiple purposes in the kernel source. One of
them is address calculation via the form &__get_cpu_var(x). This calculates
the address for the instance of the percpu variable of the current processor
based on an offset.
Other use cases are for storing and retrieving data from the current
processors percpu area. __get_cpu_var() can be used as an lvalue when
writing data or on the right side of an assignment.
__get_cpu_var() is defined as :
__get_cpu_var() always only does an address determination. However, store
and retrieve operations could use a segment prefix (or global register on
other platforms) to avoid the address calculation.
this_cpu_write() and this_cpu_read() can directly take an offset into a
percpu area and use optimized assembly code to read and write per cpu
variables.
This patch converts __get_cpu_var into either an explicit address
calculation using this_cpu_ptr() or into a use of this_cpu operations that
use the offset. Thereby address calculations are avoided and less registers
are used when code is generated.
At the end of the patch set all uses of __get_cpu_var have been removed so
the macro is removed too.
The patch set includes passes over all arches as well. Once these operations
are used throughout then specialized macros can be defined in non -x86
arches as well in order to optimize per cpu access by f.e. using a global
register that may be set to the per cpu base.
Transformations done to __get_cpu_var()
1. Determine the address of the percpu instance of the current processor.
DEFINE_PER_CPU(int, y);
int *x = &__get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(&y);
2. Same as #1 but this time an array structure is involved.
DEFINE_PER_CPU(int, y[20]);
int *x = __get_cpu_var(y);
Converts to
int *x = this_cpu_ptr(y);
3. Retrieve the content of the current processors instance of a per cpu
variable.
DEFINE_PER_CPU(int, y);
int x = __get_cpu_var(y)
Converts to
int x = __this_cpu_read(y);
4. Retrieve the content of a percpu struct
DEFINE_PER_CPU(struct mystruct, y);
struct mystruct x = __get_cpu_var(y);
Converts to
memcpy(&x, this_cpu_ptr(&y), sizeof(x));
5. Assignment to a per cpu variable
DEFINE_PER_CPU(int, y)
__get_cpu_var(y) = x;
Converts to
__this_cpu_write(y, x);
6. Increment/Decrement etc of a per cpu variable
DEFINE_PER_CPU(int, y);
__get_cpu_var(y)++
Converts to
__this_cpu_inc(y)
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
CC: Paul Mackerras <paulus@samba.org>
Signed-off-by: Christoph Lameter <cl@linux.com>
[mpe: Fix build errors caused by set/or_softirq_pending(), and rework
assignment in __set_breakpoint() to use memcpy().]
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
2014-10-22 04:23:25 +08:00
|
|
|
batch = this_cpu_ptr(&ppc64_tlb_batch);
|
2011-05-25 08:11:48 +08:00
|
|
|
batch->active = 1;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_PPC_BOOK3S_64 */
|
|
|
|
|
2005-09-26 14:04:21 +08:00
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
2005-10-10 20:29:05 +08:00
|
|
|
static int instructions_to_print = 16;
|
|
|
|
|
|
|
|
static void show_instructions(struct pt_regs *regs)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned long pc = regs->nip - (instructions_to_print * 3 / 4 *
|
|
|
|
sizeof(int));
|
|
|
|
|
|
|
|
printk("Instruction dump:");
|
|
|
|
|
|
|
|
for (i = 0; i < instructions_to_print; i++) {
|
|
|
|
int instr;
|
|
|
|
|
|
|
|
if (!(i % 8))
|
|
|
|
printk("\n");
|
|
|
|
|
2007-09-28 02:38:55 +08:00
|
|
|
#if !defined(CONFIG_BOOKE)
|
|
|
|
/* If executing with the IMMU off, adjust pc rather
|
|
|
|
* than print XXXXXXXX.
|
|
|
|
*/
|
|
|
|
if (!(regs->msr & MSR_IR))
|
|
|
|
pc = (unsigned long)phys_to_virt(pc);
|
|
|
|
#endif
|
|
|
|
|
2006-10-13 10:17:16 +08:00
|
|
|
if (!__kernel_text_address(pc) ||
|
2014-10-13 17:27:15 +08:00
|
|
|
probe_kernel_address((unsigned int __user *)pc, instr)) {
|
2012-01-06 20:34:07 +08:00
|
|
|
printk(KERN_CONT "XXXXXXXX ");
|
2005-10-10 20:29:05 +08:00
|
|
|
} else {
|
|
|
|
if (regs->nip == pc)
|
2012-01-06 20:34:07 +08:00
|
|
|
printk(KERN_CONT "<%08x> ", instr);
|
2005-10-10 20:29:05 +08:00
|
|
|
else
|
2012-01-06 20:34:07 +08:00
|
|
|
printk(KERN_CONT "%08x ", instr);
|
2005-10-10 20:29:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pc += sizeof(int);
|
|
|
|
}
|
|
|
|
|
|
|
|
printk("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct regbit {
|
|
|
|
unsigned long bit;
|
|
|
|
const char *name;
|
|
|
|
} msr_bits[] = {
|
2011-11-25 03:35:57 +08:00
|
|
|
#if defined(CONFIG_PPC64) && !defined(CONFIG_BOOKE)
|
|
|
|
{MSR_SF, "SF"},
|
|
|
|
{MSR_HV, "HV"},
|
|
|
|
#endif
|
|
|
|
{MSR_VEC, "VEC"},
|
|
|
|
{MSR_VSX, "VSX"},
|
|
|
|
#ifdef CONFIG_BOOKE
|
|
|
|
{MSR_CE, "CE"},
|
|
|
|
#endif
|
2005-10-10 20:29:05 +08:00
|
|
|
{MSR_EE, "EE"},
|
|
|
|
{MSR_PR, "PR"},
|
|
|
|
{MSR_FP, "FP"},
|
|
|
|
{MSR_ME, "ME"},
|
2011-11-25 03:35:57 +08:00
|
|
|
#ifdef CONFIG_BOOKE
|
2008-11-19 12:39:53 +08:00
|
|
|
{MSR_DE, "DE"},
|
2011-11-25 03:35:57 +08:00
|
|
|
#else
|
|
|
|
{MSR_SE, "SE"},
|
|
|
|
{MSR_BE, "BE"},
|
|
|
|
#endif
|
2005-10-10 20:29:05 +08:00
|
|
|
{MSR_IR, "IR"},
|
|
|
|
{MSR_DR, "DR"},
|
2011-11-25 03:35:57 +08:00
|
|
|
{MSR_PMM, "PMM"},
|
|
|
|
#ifndef CONFIG_BOOKE
|
|
|
|
{MSR_RI, "RI"},
|
|
|
|
{MSR_LE, "LE"},
|
|
|
|
#endif
|
2005-10-10 20:29:05 +08:00
|
|
|
{0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void printbits(unsigned long val, struct regbit *bits)
|
|
|
|
{
|
|
|
|
const char *sep = "";
|
|
|
|
|
|
|
|
printk("<");
|
|
|
|
for (; bits->bit; ++bits)
|
|
|
|
if (val & bits->bit) {
|
|
|
|
printk("%s%s", sep, bits->name);
|
|
|
|
sep = ",";
|
|
|
|
}
|
|
|
|
printk(">");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_PPC64
|
2007-03-21 09:38:19 +08:00
|
|
|
#define REG "%016lx"
|
2005-10-10 20:29:05 +08:00
|
|
|
#define REGS_PER_LINE 4
|
|
|
|
#define LAST_VOLATILE 13
|
|
|
|
#else
|
2007-03-21 09:38:19 +08:00
|
|
|
#define REG "%08lx"
|
2005-10-10 20:29:05 +08:00
|
|
|
#define REGS_PER_LINE 8
|
|
|
|
#define LAST_VOLATILE 12
|
|
|
|
#endif
|
|
|
|
|
2005-09-26 14:04:21 +08:00
|
|
|
void show_regs(struct pt_regs * regs)
|
|
|
|
{
|
|
|
|
int i, trap;
|
|
|
|
|
dump_stack: unify debug information printed by show_regs()
show_regs() is inherently arch-dependent but it does make sense to print
generic debug information and some archs already do albeit in slightly
different forms. This patch introduces a generic function to print debug
information from show_regs() so that different archs print out the same
information and it's much easier to modify what's printed.
show_regs_print_info() prints out the same debug info as dump_stack()
does plus task and thread_info pointers.
* Archs which didn't print debug info now do.
alpha, arc, blackfin, c6x, cris, frv, h8300, hexagon, ia64, m32r,
metag, microblaze, mn10300, openrisc, parisc, score, sh64, sparc,
um, xtensa
* Already prints debug info. Replaced with show_regs_print_info().
The printed information is superset of what used to be there.
arm, arm64, avr32, mips, powerpc, sh32, tile, unicore32, x86
* s390 is special in that it used to print arch-specific information
along with generic debug info. Heiko and Martin think that the
arch-specific extra isn't worth keeping s390 specfic implementation.
Converted to use the generic version.
Note that now all archs print the debug info before actual register
dumps.
An example BUG() dump follows.
kernel BUG at /work/os/work/kernel/workqueue.c:4841!
invalid opcode: 0000 [#1] PREEMPT SMP DEBUG_PAGEALLOC
Modules linked in:
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 3.9.0-rc1-work+ #7
Hardware name: empty empty/S3992, BIOS 080011 10/26/2007
task: ffff88007c85e040 ti: ffff88007c860000 task.ti: ffff88007c860000
RIP: 0010:[<ffffffff8234a07e>] [<ffffffff8234a07e>] init_workqueues+0x4/0x6
RSP: 0000:ffff88007c861ec8 EFLAGS: 00010246
RAX: ffff88007c861fd8 RBX: ffffffff824466a8 RCX: 0000000000000001
RDX: 0000000000000046 RSI: 0000000000000001 RDI: ffffffff8234a07a
RBP: ffff88007c861ec8 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000001 R11: 0000000000000000 R12: ffffffff8234a07a
R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000
FS: 0000000000000000(0000) GS:ffff88007dc00000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b
CR2: ffff88015f7ff000 CR3: 00000000021f1000 CR4: 00000000000007f0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000ffff0ff0 DR7: 0000000000000400
Stack:
ffff88007c861ef8 ffffffff81000312 ffffffff824466a8 ffff88007c85e650
0000000000000003 0000000000000000 ffff88007c861f38 ffffffff82335e5d
ffff88007c862080 ffffffff8223d8c0 ffff88007c862080 ffffffff81c47760
Call Trace:
[<ffffffff81000312>] do_one_initcall+0x122/0x170
[<ffffffff82335e5d>] kernel_init_freeable+0x9b/0x1c8
[<ffffffff81c47760>] ? rest_init+0x140/0x140
[<ffffffff81c4776e>] kernel_init+0xe/0xf0
[<ffffffff81c6be9c>] ret_from_fork+0x7c/0xb0
[<ffffffff81c47760>] ? rest_init+0x140/0x140
...
v2: Typo fix in x86-32.
v3: CPU number dropped from show_regs_print_info() as
dump_stack_print_info() has been updated to print it. s390
specific implementation dropped as requested by s390 maintainers.
Signed-off-by: Tejun Heo <tj@kernel.org>
Acked-by: David S. Miller <davem@davemloft.net>
Acked-by: Jesper Nilsson <jesper.nilsson@axis.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: Fengguang Wu <fengguang.wu@intel.com>
Cc: Mike Frysinger <vapier@gentoo.org>
Cc: Vineet Gupta <vgupta@synopsys.com>
Cc: Sam Ravnborg <sam@ravnborg.org>
Acked-by: Chris Metcalf <cmetcalf@tilera.com> [tile bits]
Acked-by: Richard Kuo <rkuo@codeaurora.org> [hexagon bits]
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-05-01 06:27:17 +08:00
|
|
|
show_regs_print_info(KERN_DEFAULT);
|
|
|
|
|
2005-10-10 20:29:05 +08:00
|
|
|
printk("NIP: "REG" LR: "REG" CTR: "REG"\n",
|
|
|
|
regs->nip, regs->link, regs->ctr);
|
|
|
|
printk("REGS: %p TRAP: %04lx %s (%s)\n",
|
2006-10-02 17:18:13 +08:00
|
|
|
regs, regs->trap, print_tainted(), init_utsname()->release);
|
2005-10-10 20:29:05 +08:00
|
|
|
printk("MSR: "REG" ", regs->msr);
|
|
|
|
printbits(regs->msr, msr_bits);
|
2007-03-21 09:38:19 +08:00
|
|
|
printk(" CR: %08lx XER: %08lx\n", regs->ccr, regs->xer);
|
2005-09-26 14:04:21 +08:00
|
|
|
trap = TRAP(regs);
|
2011-07-15 03:25:12 +08:00
|
|
|
if ((regs->trap != 0xc00) && cpu_has_feature(CPU_FTR_CFAR))
|
powerpc: Remove a few lines of oops output
We waste quite a few lines in our oops output:
...
MSR: 8000000000009032 <SF,EE,ME,IR,DR,RI> CR: 28044024 XER: 00000000
SOFTE: 0
CFAR: 0000000000009088
DAR: 000000000000001c, DSISR: 40000000
GPR00: c0000000000c74f0 c00000037cc1b010 c000000000d2bb30 0000000000000000
...
We can do a better job here and remove 3 lines:
MSR: 8000000000009032 <SF,EE,ME,IR,DR,RI> CR: 28044024 XER: 00000000
CFAR: 0000000000009088 DAR: 0000000000000010, DSISR: 40000000 SOFTE: 1
GPR00: c0000000000e3d10 c00000037cc2fda0 c000000000d2c3a8 0000000000000001
Also move PACATMSCRATCH up, it doesn't really belong in the stack
trace section.
Signed-off-by: Anton Blanchard <anton@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2013-11-15 12:48:38 +08:00
|
|
|
printk("CFAR: "REG" ", regs->orig_gpr3);
|
2013-11-15 12:41:19 +08:00
|
|
|
if (trap == 0x200 || trap == 0x300 || trap == 0x600)
|
2011-10-06 10:53:38 +08:00
|
|
|
#if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
|
powerpc: Remove a few lines of oops output
We waste quite a few lines in our oops output:
...
MSR: 8000000000009032 <SF,EE,ME,IR,DR,RI> CR: 28044024 XER: 00000000
SOFTE: 0
CFAR: 0000000000009088
DAR: 000000000000001c, DSISR: 40000000
GPR00: c0000000000c74f0 c00000037cc1b010 c000000000d2bb30 0000000000000000
...
We can do a better job here and remove 3 lines:
MSR: 8000000000009032 <SF,EE,ME,IR,DR,RI> CR: 28044024 XER: 00000000
CFAR: 0000000000009088 DAR: 0000000000000010, DSISR: 40000000 SOFTE: 1
GPR00: c0000000000e3d10 c00000037cc2fda0 c000000000d2c3a8 0000000000000001
Also move PACATMSCRATCH up, it doesn't really belong in the stack
trace section.
Signed-off-by: Anton Blanchard <anton@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2013-11-15 12:48:38 +08:00
|
|
|
printk("DEAR: "REG" ESR: "REG" ", regs->dar, regs->dsisr);
|
2007-07-26 13:46:15 +08:00
|
|
|
#else
|
powerpc: Remove a few lines of oops output
We waste quite a few lines in our oops output:
...
MSR: 8000000000009032 <SF,EE,ME,IR,DR,RI> CR: 28044024 XER: 00000000
SOFTE: 0
CFAR: 0000000000009088
DAR: 000000000000001c, DSISR: 40000000
GPR00: c0000000000c74f0 c00000037cc1b010 c000000000d2bb30 0000000000000000
...
We can do a better job here and remove 3 lines:
MSR: 8000000000009032 <SF,EE,ME,IR,DR,RI> CR: 28044024 XER: 00000000
CFAR: 0000000000009088 DAR: 0000000000000010, DSISR: 40000000 SOFTE: 1
GPR00: c0000000000e3d10 c00000037cc2fda0 c000000000d2c3a8 0000000000000001
Also move PACATMSCRATCH up, it doesn't really belong in the stack
trace section.
Signed-off-by: Anton Blanchard <anton@samba.org>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
2013-11-15 12:48:38 +08:00
|
|
|
printk("DAR: "REG" DSISR: %08lx ", regs->dar, regs->dsisr);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_PPC64
|
|
|
|
printk("SOFTE: %ld ", regs->softe);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
|
2013-11-18 10:19:17 +08:00
|
|
|
if (MSR_TM_ACTIVE(regs->msr))
|
|
|
|
printk("\nPACATMSCRATCH: %016llx ", get_paca()->tm_scratch);
|
2007-07-26 13:46:15 +08:00
|
|
|
#endif
|
2005-09-26 14:04:21 +08:00
|
|
|
|
|
|
|
for (i = 0; i < 32; i++) {
|
2005-10-10 20:29:05 +08:00
|
|
|
if ((i % REGS_PER_LINE) == 0)
|
2009-06-19 06:29:55 +08:00
|
|
|
printk("\nGPR%02d: ", i);
|
2005-10-10 20:29:05 +08:00
|
|
|
printk(REG " ", regs->gpr[i]);
|
|
|
|
if (i == LAST_VOLATILE && !FULL_REGS(regs))
|
2005-09-26 14:04:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
printk("\n");
|
|
|
|
#ifdef CONFIG_KALLSYMS
|
|
|
|
/*
|
|
|
|
* Lookup NIP late so we have the best change of getting the
|
|
|
|
* above info out without failing
|
|
|
|
*/
|
2008-07-07 11:44:31 +08:00
|
|
|
printk("NIP ["REG"] %pS\n", regs->nip, (void *)regs->nip);
|
|
|
|
printk("LR ["REG"] %pS\n", regs->link, (void *)regs->link);
|
2013-02-14 00:21:34 +08:00
|
|
|
#endif
|
2005-09-26 14:04:21 +08:00
|
|
|
show_stack(current, (unsigned long *) regs->gpr[1]);
|
2005-10-10 20:29:05 +08:00
|
|
|
if (!user_mode(regs))
|
|
|
|
show_instructions(regs);
|
2005-09-26 14:04:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void exit_thread(void)
|
|
|
|
{
|
2005-11-30 10:20:54 +08:00
|
|
|
discard_lazy_cpu_state();
|
2005-09-26 14:04:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void flush_thread(void)
|
|
|
|
{
|
2005-11-30 10:20:54 +08:00
|
|
|
discard_lazy_cpu_state();
|
2005-09-26 14:04:21 +08:00
|
|
|
|
2011-02-10 12:44:35 +08:00
|
|
|
#ifdef CONFIG_HAVE_HW_BREAKPOINT
|
2010-06-15 14:05:19 +08:00
|
|
|
flush_ptrace_hw_breakpoint(current);
|
2011-02-10 12:44:35 +08:00
|
|
|
#else /* CONFIG_HAVE_HW_BREAKPOINT */
|
2010-02-08 19:51:18 +08:00
|
|
|
set_debug_reg_defaults(¤t->thread);
|
2011-02-10 12:44:35 +08:00
|
|
|
#endif /* CONFIG_HAVE_HW_BREAKPOINT */
|
2005-09-26 14:04:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
release_thread(struct task_struct *t)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-05-17 06:03:51 +08:00
|
|
|
* this gets called so that we can store coprocessor state into memory and
|
|
|
|
* copy the current task into the new thread.
|
2005-09-26 14:04:21 +08:00
|
|
|
*/
|
2012-05-17 06:03:51 +08:00
|
|
|
int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
|
2005-09-26 14:04:21 +08:00
|
|
|
{
|
2012-05-17 06:03:51 +08:00
|
|
|
flush_fp_to_thread(src);
|
|
|
|
flush_altivec_to_thread(src);
|
|
|
|
flush_vsx_to_thread(src);
|
|
|
|
flush_spe_to_thread(src);
|
2014-03-03 11:21:40 +08:00
|
|
|
/*
|
|
|
|
* Flush TM state out so we can copy it. __switch_to_tm() does this
|
|
|
|
* flush but it removes the checkpointed state from the current CPU and
|
|
|
|
* transitions the CPU out of TM mode. Hence we need to call
|
|
|
|
* tm_recheckpoint_new_task() (on the same task) to restore the
|
|
|
|
* checkpointed state back and the TM mode.
|
|
|
|
*/
|
|
|
|
__switch_to_tm(src);
|
|
|
|
tm_recheckpoint_new_task(src);
|
2013-06-28 16:15:16 +08:00
|
|
|
|
2012-05-17 06:03:51 +08:00
|
|
|
*dst = *src;
|
2013-06-28 16:15:16 +08:00
|
|
|
|
|
|
|
clear_task_ebb(dst);
|
|
|
|
|
2012-05-17 06:03:51 +08:00
|
|
|
return 0;
|
2005-09-26 14:04:21 +08:00
|
|
|
}
|
|
|
|
|
2014-07-10 10:29:21 +08:00
|
|
|
static void setup_ksp_vsid(struct task_struct *p, unsigned long sp)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_PPC_STD_MMU_64
|
|
|
|
unsigned long sp_vsid;
|
|
|
|
unsigned long llp = mmu_psize_defs[mmu_linear_psize].sllp;
|
|
|
|
|
|
|
|
if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
|
|
|
|
sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_1T)
|
|
|
|
<< SLB_VSID_SHIFT_1T;
|
|
|
|
else
|
|
|
|
sp_vsid = get_kernel_vsid(sp, MMU_SEGSIZE_256M)
|
|
|
|
<< SLB_VSID_SHIFT;
|
|
|
|
sp_vsid |= SLB_VSID_KERNEL | llp;
|
|
|
|
p->thread.ksp_vsid = sp_vsid;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-09-26 14:04:21 +08:00
|
|
|
/*
|
|
|
|
* Copy a thread..
|
|
|
|
*/
|
2011-03-02 23:18:48 +08:00
|
|
|
extern unsigned long dscr_default; /* defined in arch/powerpc/kernel/sysfs.c */
|
|
|
|
|
2009-04-03 07:56:59 +08:00
|
|
|
int copy_thread(unsigned long clone_flags, unsigned long usp,
|
2012-10-23 10:51:14 +08:00
|
|
|
unsigned long arg, struct task_struct *p)
|
2005-09-26 14:04:21 +08:00
|
|
|
{
|
|
|
|
struct pt_regs *childregs, *kregs;
|
|
|
|
extern void ret_from_fork(void);
|
2012-09-13 06:32:42 +08:00
|
|
|
extern void ret_from_kernel_thread(void);
|
|
|
|
void (*f)(void);
|
2006-01-12 17:06:02 +08:00
|
|
|
unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
|
2005-09-26 14:04:21 +08:00
|
|
|
|
|
|
|
/* Copy registers */
|
|
|
|
sp -= sizeof(struct pt_regs);
|
|
|
|
childregs = (struct pt_regs *) sp;
|
2012-10-22 10:33:39 +08:00
|
|
|
if (unlikely(p->flags & PF_KTHREAD)) {
|
2012-10-11 20:41:43 +08:00
|
|
|
struct thread_info *ti = (void *)task_stack_page(p);
|
2012-09-13 06:32:42 +08:00
|
|
|
memset(childregs, 0, sizeof(struct pt_regs));
|
2005-09-26 14:04:21 +08:00
|
|
|
childregs->gpr[1] = sp + sizeof(struct pt_regs);
|
2014-02-04 13:08:51 +08:00
|
|
|
/* function */
|
|
|
|
if (usp)
|
|
|
|
childregs->gpr[14] = ppc_function_entry((void *)usp);
|
2012-09-13 06:32:42 +08:00
|
|
|
#ifdef CONFIG_PPC64
|
2006-01-12 17:06:01 +08:00
|
|
|
clear_tsk_thread_flag(p, TIF_32BIT);
|
2012-10-11 20:41:43 +08:00
|
|
|
childregs->softe = 1;
|
2005-10-10 20:29:05 +08:00
|
|
|
#endif
|
2012-09-13 06:32:42 +08:00
|
|
|
childregs->gpr[15] = arg;
|
2005-09-26 14:04:21 +08:00
|
|
|
p->thread.regs = NULL; /* no user register state */
|
2012-10-11 20:41:43 +08:00
|
|
|
ti->flags |= _TIF_RESTOREALL;
|
2012-09-13 06:32:42 +08:00
|
|
|
f = ret_from_kernel_thread;
|
2005-09-26 14:04:21 +08:00
|
|
|
} else {
|
2012-10-23 10:51:14 +08:00
|
|
|
struct pt_regs *regs = current_pt_regs();
|
2012-09-13 06:32:42 +08:00
|
|
|
CHECK_FULL_REGS(regs);
|
|
|
|
*childregs = *regs;
|
2012-10-22 10:28:43 +08:00
|
|
|
if (usp)
|
|
|
|
childregs->gpr[1] = usp;
|
2005-09-26 14:04:21 +08:00
|
|
|
p->thread.regs = childregs;
|
2012-09-13 06:32:42 +08:00
|
|
|
childregs->gpr[3] = 0; /* Result from fork() */
|
2005-10-10 20:29:05 +08:00
|
|
|
if (clone_flags & CLONE_SETTLS) {
|
|
|
|
#ifdef CONFIG_PPC64
|
2010-07-30 06:04:39 +08:00
|
|
|
if (!is_32bit_task())
|
2005-10-10 20:29:05 +08:00
|
|
|
childregs->gpr[13] = childregs->gpr[6];
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
childregs->gpr[2] = childregs->gpr[6];
|
|
|
|
}
|
2012-09-13 06:32:42 +08:00
|
|
|
|
|
|
|
f = ret_from_fork;
|
2005-09-26 14:04:21 +08:00
|
|
|
}
|
|
|
|
sp -= STACK_FRAME_OVERHEAD;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The way this works is that at some point in the future
|
|
|
|
* some task will call _switch to switch to the new task.
|
|
|
|
* That will pop off the stack frame created below and start
|
|
|
|
* the new task running at ret_from_fork. The new task will
|
|
|
|
* do some house keeping and then return from the fork or clone
|
|
|
|
* system call, using the stack frame created above.
|
|
|
|
*/
|
2013-05-07 06:44:41 +08:00
|
|
|
((unsigned long *)sp)[0] = 0;
|
2005-09-26 14:04:21 +08:00
|
|
|
sp -= sizeof(struct pt_regs);
|
|
|
|
kregs = (struct pt_regs *) sp;
|
|
|
|
sp -= STACK_FRAME_OVERHEAD;
|
|
|
|
p->thread.ksp = sp;
|
2013-09-24 13:17:21 +08:00
|
|
|
#ifdef CONFIG_PPC32
|
2008-04-28 14:21:22 +08:00
|
|
|
p->thread.ksp_limit = (unsigned long)task_stack_page(p) +
|
|
|
|
_ALIGN_UP(sizeof(struct thread_info), 16);
|
2013-09-24 13:17:21 +08:00
|
|
|
#endif
|
2013-04-21 14:47:59 +08:00
|
|
|
#ifdef CONFIG_HAVE_HW_BREAKPOINT
|
|
|
|
p->thread.ptrace_bps[0] = NULL;
|
|
|
|
#endif
|
|
|
|
|
2013-09-10 18:21:10 +08:00
|
|
|
p->thread.fp_save_area = NULL;
|
|
|
|
#ifdef CONFIG_ALTIVEC
|
|
|
|
p->thread.vr_save_area = NULL;
|
|
|
|
#endif
|
|
|
|
|
2014-07-10 10:29:21 +08:00
|
|
|
setup_ksp_vsid(p, sp);
|
|
|
|
|
2011-03-02 23:18:48 +08:00
|
|
|
#ifdef CONFIG_PPC64
|
|
|
|
if (cpu_has_feature(CPU_FTR_DSCR)) {
|
2012-09-04 00:49:47 +08:00
|
|
|
p->thread.dscr_inherit = current->thread.dscr_inherit;
|
|
|
|
p->thread.dscr = current->thread.dscr;
|
2011-03-02 23:18:48 +08:00
|
|
|
}
|
2012-12-07 05:49:56 +08:00
|
|
|
if (cpu_has_feature(CPU_FTR_HAS_PPR))
|
|
|
|
p->thread.ppr = INIT_PPR;
|
2011-03-02 23:18:48 +08:00
|
|
|
#endif
|
2014-02-04 13:08:51 +08:00
|
|
|
kregs->nip = ppc_function_entry(f);
|
2005-09-26 14:04:21 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up a thread for executing a new program
|
|
|
|
*/
|
2005-10-10 20:29:05 +08:00
|
|
|
void start_thread(struct pt_regs *regs, unsigned long start, unsigned long sp)
|
2005-09-26 14:04:21 +08:00
|
|
|
{
|
2005-10-21 14:01:33 +08:00
|
|
|
#ifdef CONFIG_PPC64
|
|
|
|
unsigned long load_addr = regs->gpr[2]; /* saved by ELF_PLAT_INIT */
|
|
|
|
#endif
|
|
|
|
|
2005-10-10 20:29:05 +08:00
|
|
|
/*
|
|
|
|
* If we exec out of a kernel thread then thread.regs will not be
|
|
|
|
* set. Do it now.
|
|
|
|
*/
|
|
|
|
if (!current->thread.regs) {
|
2006-01-12 17:06:02 +08:00
|
|
|
struct pt_regs *regs = task_stack_page(current) + THREAD_SIZE;
|
|
|
|
current->thread.regs = regs - 1;
|
2005-10-10 20:29:05 +08:00
|
|
|
}
|
|
|
|
|
2005-09-26 14:04:21 +08:00
|
|
|
memset(regs->gpr, 0, sizeof(regs->gpr));
|
|
|
|
regs->ctr = 0;
|
|
|
|
regs->link = 0;
|
|
|
|
regs->xer = 0;
|
|
|
|
regs->ccr = 0;
|
|
|
|
regs->gpr[1] = sp;
|
2005-10-10 20:29:05 +08:00
|
|
|
|
2007-09-25 07:52:44 +08:00
|
|
|
/*
|
|
|
|
* We have just cleared all the nonvolatile GPRs, so make
|
|
|
|
* FULL_REGS(regs) return true. This is necessary to allow
|
|
|
|
* ptrace to examine the thread immediately after exec.
|
|
|
|
*/
|
|
|
|
regs->trap &= ~1UL;
|
|
|
|
|
2005-10-10 20:29:05 +08:00
|
|
|
#ifdef CONFIG_PPC32
|
|
|
|
regs->mq = 0;
|
|
|
|
regs->nip = start;
|
2005-09-26 14:04:21 +08:00
|
|
|
regs->msr = MSR_USER;
|
2005-10-10 20:29:05 +08:00
|
|
|
#else
|
2010-07-30 06:04:39 +08:00
|
|
|
if (!is_32bit_task()) {
|
2013-11-20 19:15:02 +08:00
|
|
|
unsigned long entry;
|
2005-10-10 20:29:05 +08:00
|
|
|
|
2013-11-20 19:15:02 +08:00
|
|
|
if (is_elf2_task()) {
|
|
|
|
/* Look ma, no function descriptors! */
|
|
|
|
entry = start;
|
2005-10-10 20:29:05 +08:00
|
|
|
|
2013-11-20 19:15:02 +08:00
|
|
|
/*
|
|
|
|
* Ulrich says:
|
|
|
|
* The latest iteration of the ABI requires that when
|
|
|
|
* calling a function (at its global entry point),
|
|
|
|
* the caller must ensure r12 holds the entry point
|
|
|
|
* address (so that the function can quickly
|
|
|
|
* establish addressability).
|
|
|
|
*/
|
|
|
|
regs->gpr[12] = start;
|
|
|
|
/* Make sure that's restored on entry to userspace. */
|
|
|
|
set_thread_flag(TIF_RESTOREALL);
|
|
|
|
} else {
|
|
|
|
unsigned long toc;
|
|
|
|
|
|
|
|
/* start is a relocated pointer to the function
|
|
|
|
* descriptor for the elf _start routine. The first
|
|
|
|
* entry in the function descriptor is the entry
|
|
|
|
* address of _start and the second entry is the TOC
|
|
|
|
* value we need to use.
|
|
|
|
*/
|
|
|
|
__get_user(entry, (unsigned long __user *)start);
|
|
|
|
__get_user(toc, (unsigned long __user *)start+1);
|
|
|
|
|
|
|
|
/* Check whether the e_entry function descriptor entries
|
|
|
|
* need to be relocated before we can use them.
|
|
|
|
*/
|
|
|
|
if (load_addr != 0) {
|
|
|
|
entry += load_addr;
|
|
|
|
toc += load_addr;
|
|
|
|
}
|
|
|
|
regs->gpr[2] = toc;
|
2005-10-10 20:29:05 +08:00
|
|
|
}
|
|
|
|
regs->nip = entry;
|
|
|
|
regs->msr = MSR_USER64;
|
2005-10-13 11:40:54 +08:00
|
|
|
} else {
|
|
|
|
regs->nip = start;
|
|
|
|
regs->gpr[2] = 0;
|
|
|
|
regs->msr = MSR_USER32;
|
2005-10-10 20:29:05 +08:00
|
|
|
}
|
|
|
|
#endif
|
2005-11-30 10:20:54 +08:00
|
|
|
discard_lazy_cpu_state();
|
2008-06-25 12:07:18 +08:00
|
|
|
#ifdef CONFIG_VSX
|
|
|
|
current->thread.used_vsr = 0;
|
|
|
|
#endif
|
2013-09-10 18:20:42 +08:00
|
|
|
memset(¤t->thread.fp_state, 0, sizeof(current->thread.fp_state));
|
2013-09-10 18:21:10 +08:00
|
|
|
current->thread.fp_save_area = NULL;
|
2005-09-26 14:04:21 +08:00
|
|
|
#ifdef CONFIG_ALTIVEC
|
2013-09-10 18:20:42 +08:00
|
|
|
memset(¤t->thread.vr_state, 0, sizeof(current->thread.vr_state));
|
|
|
|
current->thread.vr_state.vscr.u[3] = 0x00010000; /* Java mode disabled */
|
2013-09-10 18:21:10 +08:00
|
|
|
current->thread.vr_save_area = NULL;
|
2005-09-26 14:04:21 +08:00
|
|
|
current->thread.vrsave = 0;
|
|
|
|
current->thread.used_vr = 0;
|
|
|
|
#endif /* CONFIG_ALTIVEC */
|
|
|
|
#ifdef CONFIG_SPE
|
|
|
|
memset(current->thread.evr, 0, sizeof(current->thread.evr));
|
|
|
|
current->thread.acc = 0;
|
|
|
|
current->thread.spefscr = 0;
|
|
|
|
current->thread.used_spe = 0;
|
|
|
|
#endif /* CONFIG_SPE */
|
2013-02-14 00:21:40 +08:00
|
|
|
#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
|
|
|
|
if (cpu_has_feature(CPU_FTR_TM))
|
|
|
|
regs->msr |= MSR_TM;
|
|
|
|
current->thread.tm_tfhar = 0;
|
|
|
|
current->thread.tm_texasr = 0;
|
|
|
|
current->thread.tm_tfiar = 0;
|
|
|
|
#endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
|
2005-09-26 14:04:21 +08:00
|
|
|
}
|
2014-08-20 06:00:02 +08:00
|
|
|
EXPORT_SYMBOL(start_thread);
|
2005-09-26 14:04:21 +08:00
|
|
|
|
|
|
|
#define PR_FP_ALL_EXCEPT (PR_FP_EXC_DIV | PR_FP_EXC_OVF | PR_FP_EXC_UND \
|
|
|
|
| PR_FP_EXC_RES | PR_FP_EXC_INV)
|
|
|
|
|
|
|
|
int set_fpexc_mode(struct task_struct *tsk, unsigned int val)
|
|
|
|
{
|
|
|
|
struct pt_regs *regs = tsk->thread.regs;
|
|
|
|
|
|
|
|
/* This is a bit hairy. If we are an SPE enabled processor
|
|
|
|
* (have embedded fp) we store the IEEE exception enable flags in
|
|
|
|
* fpexc_mode. fpexc_mode is also used for setting FP exception
|
|
|
|
* mode (asyn, precise, disabled) for 'Classic' FP. */
|
|
|
|
if (val & PR_FP_EXC_SW_ENABLE) {
|
|
|
|
#ifdef CONFIG_SPE
|
2007-09-13 14:44:20 +08:00
|
|
|
if (cpu_has_feature(CPU_FTR_SPE)) {
|
powerpc: fix exception clearing in e500 SPE float emulation
The e500 SPE floating-point emulation code clears existing exceptions
(__FPU_FPSCR &= ~FP_EX_MASK;) before ORing in the exceptions from the
emulated operation. However, these exception bits are the "sticky",
cumulative exception bits, and should only be cleared by the user
program setting SPEFSCR, not implicitly by any floating-point
instruction (whether executed purely by the hardware or emulated).
The spurious clearing of these bits shows up as missing exceptions in
glibc testing.
Fixing this, however, is not as simple as just not clearing the bits,
because while the bits may be from previous floating-point operations
(in which case they should not be cleared), the processor can also set
the sticky bits itself before the interrupt for an exception occurs,
and this can happen in cases when IEEE 754 semantics are that the
sticky bit should not be set. Specifically, the "invalid" sticky bit
is set in various cases with non-finite operands, where IEEE 754
semantics do not involve raising such an exception, and the
"underflow" sticky bit is set in cases of exact underflow, whereas
IEEE 754 semantics are that this flag is set only for inexact
underflow. Thus, for correct emulation the kernel needs to know the
setting of these two sticky bits before the instruction being
emulated.
When a floating-point operation raises an exception, the kernel can
note the state of the sticky bits immediately afterwards. Some
<fenv.h> functions that affect the state of these bits, such as
fesetenv and feholdexcept, need to use prctl with PR_GET_FPEXC and
PR_SET_FPEXC anyway, and so it is natural to record the state of those
bits during that call into the kernel and so avoid any need for a
separate call into the kernel to inform it of a change to those bits.
Thus, the interface I chose to use (in this patch and the glibc port)
is that one of those prctl calls must be made after any userspace
change to those sticky bits, other than through a floating-point
operation that traps into the kernel anyway. feclearexcept and
fesetexceptflag duly make those calls, which would not be required
were it not for this issue.
The previous EGLIBC port, and the uClibc code copied from it, is
fundamentally broken as regards any use of prctl for floating-point
exceptions because it didn't use the PR_FP_EXC_SW_ENABLE bit in its
prctl calls (and did various worse things, such as passing a pointer
when prctl expected an integer). If you avoid anything where prctl is
used, the clearing of sticky bits still means it will never give
anything approximating correct exception semantics with existing
kernels. I don't believe the patch makes things any worse for
existing code that doesn't try to inform the kernel of changes to
sticky bits - such code may get incorrect exceptions in some cases,
but it would have done so anyway in other cases.
Signed-off-by: Joseph Myers <joseph@codesourcery.com>
Signed-off-by: Scott Wood <scottwood@freescale.com>
2013-12-11 07:07:45 +08:00
|
|
|
/*
|
|
|
|
* When the sticky exception bits are set
|
|
|
|
* directly by userspace, it must call prctl
|
|
|
|
* with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
|
|
|
|
* in the existing prctl settings) or
|
|
|
|
* PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
|
|
|
|
* the bits being set). <fenv.h> functions
|
|
|
|
* saving and restoring the whole
|
|
|
|
* floating-point environment need to do so
|
|
|
|
* anyway to restore the prctl settings from
|
|
|
|
* the saved environment.
|
|
|
|
*/
|
|
|
|
tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
|
2007-09-13 14:44:20 +08:00
|
|
|
tsk->thread.fpexc_mode = val &
|
|
|
|
(PR_FP_EXC_SW_ENABLE | PR_FP_ALL_EXCEPT);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2005-09-26 14:04:21 +08:00
|
|
|
#else
|
|
|
|
return -EINVAL;
|
|
|
|
#endif
|
|
|
|
}
|
2005-10-10 20:29:05 +08:00
|
|
|
|
|
|
|
/* on a CONFIG_SPE this does not hurt us. The bits that
|
|
|
|
* __pack_fe01 use do not overlap with bits used for
|
|
|
|
* PR_FP_EXC_SW_ENABLE. Additionally, the MSR[FE0,FE1] bits
|
|
|
|
* on CONFIG_SPE implementations are reserved so writing to
|
|
|
|
* them does not change anything */
|
|
|
|
if (val > PR_FP_EXC_PRECISE)
|
|
|
|
return -EINVAL;
|
|
|
|
tsk->thread.fpexc_mode = __pack_fe01(val);
|
|
|
|
if (regs != NULL && (regs->msr & MSR_FP) != 0)
|
|
|
|
regs->msr = (regs->msr & ~(MSR_FE0|MSR_FE1))
|
|
|
|
| tsk->thread.fpexc_mode;
|
2005-09-26 14:04:21 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_fpexc_mode(struct task_struct *tsk, unsigned long adr)
|
|
|
|
{
|
|
|
|
unsigned int val;
|
|
|
|
|
|
|
|
if (tsk->thread.fpexc_mode & PR_FP_EXC_SW_ENABLE)
|
|
|
|
#ifdef CONFIG_SPE
|
powerpc: fix exception clearing in e500 SPE float emulation
The e500 SPE floating-point emulation code clears existing exceptions
(__FPU_FPSCR &= ~FP_EX_MASK;) before ORing in the exceptions from the
emulated operation. However, these exception bits are the "sticky",
cumulative exception bits, and should only be cleared by the user
program setting SPEFSCR, not implicitly by any floating-point
instruction (whether executed purely by the hardware or emulated).
The spurious clearing of these bits shows up as missing exceptions in
glibc testing.
Fixing this, however, is not as simple as just not clearing the bits,
because while the bits may be from previous floating-point operations
(in which case they should not be cleared), the processor can also set
the sticky bits itself before the interrupt for an exception occurs,
and this can happen in cases when IEEE 754 semantics are that the
sticky bit should not be set. Specifically, the "invalid" sticky bit
is set in various cases with non-finite operands, where IEEE 754
semantics do not involve raising such an exception, and the
"underflow" sticky bit is set in cases of exact underflow, whereas
IEEE 754 semantics are that this flag is set only for inexact
underflow. Thus, for correct emulation the kernel needs to know the
setting of these two sticky bits before the instruction being
emulated.
When a floating-point operation raises an exception, the kernel can
note the state of the sticky bits immediately afterwards. Some
<fenv.h> functions that affect the state of these bits, such as
fesetenv and feholdexcept, need to use prctl with PR_GET_FPEXC and
PR_SET_FPEXC anyway, and so it is natural to record the state of those
bits during that call into the kernel and so avoid any need for a
separate call into the kernel to inform it of a change to those bits.
Thus, the interface I chose to use (in this patch and the glibc port)
is that one of those prctl calls must be made after any userspace
change to those sticky bits, other than through a floating-point
operation that traps into the kernel anyway. feclearexcept and
fesetexceptflag duly make those calls, which would not be required
were it not for this issue.
The previous EGLIBC port, and the uClibc code copied from it, is
fundamentally broken as regards any use of prctl for floating-point
exceptions because it didn't use the PR_FP_EXC_SW_ENABLE bit in its
prctl calls (and did various worse things, such as passing a pointer
when prctl expected an integer). If you avoid anything where prctl is
used, the clearing of sticky bits still means it will never give
anything approximating correct exception semantics with existing
kernels. I don't believe the patch makes things any worse for
existing code that doesn't try to inform the kernel of changes to
sticky bits - such code may get incorrect exceptions in some cases,
but it would have done so anyway in other cases.
Signed-off-by: Joseph Myers <joseph@codesourcery.com>
Signed-off-by: Scott Wood <scottwood@freescale.com>
2013-12-11 07:07:45 +08:00
|
|
|
if (cpu_has_feature(CPU_FTR_SPE)) {
|
|
|
|
/*
|
|
|
|
* When the sticky exception bits are set
|
|
|
|
* directly by userspace, it must call prctl
|
|
|
|
* with PR_GET_FPEXC (with PR_FP_EXC_SW_ENABLE
|
|
|
|
* in the existing prctl settings) or
|
|
|
|
* PR_SET_FPEXC (with PR_FP_EXC_SW_ENABLE in
|
|
|
|
* the bits being set). <fenv.h> functions
|
|
|
|
* saving and restoring the whole
|
|
|
|
* floating-point environment need to do so
|
|
|
|
* anyway to restore the prctl settings from
|
|
|
|
* the saved environment.
|
|
|
|
*/
|
|
|
|
tsk->thread.spefscr_last = mfspr(SPRN_SPEFSCR);
|
2007-09-13 14:44:20 +08:00
|
|
|
val = tsk->thread.fpexc_mode;
|
powerpc: fix exception clearing in e500 SPE float emulation
The e500 SPE floating-point emulation code clears existing exceptions
(__FPU_FPSCR &= ~FP_EX_MASK;) before ORing in the exceptions from the
emulated operation. However, these exception bits are the "sticky",
cumulative exception bits, and should only be cleared by the user
program setting SPEFSCR, not implicitly by any floating-point
instruction (whether executed purely by the hardware or emulated).
The spurious clearing of these bits shows up as missing exceptions in
glibc testing.
Fixing this, however, is not as simple as just not clearing the bits,
because while the bits may be from previous floating-point operations
(in which case they should not be cleared), the processor can also set
the sticky bits itself before the interrupt for an exception occurs,
and this can happen in cases when IEEE 754 semantics are that the
sticky bit should not be set. Specifically, the "invalid" sticky bit
is set in various cases with non-finite operands, where IEEE 754
semantics do not involve raising such an exception, and the
"underflow" sticky bit is set in cases of exact underflow, whereas
IEEE 754 semantics are that this flag is set only for inexact
underflow. Thus, for correct emulation the kernel needs to know the
setting of these two sticky bits before the instruction being
emulated.
When a floating-point operation raises an exception, the kernel can
note the state of the sticky bits immediately afterwards. Some
<fenv.h> functions that affect the state of these bits, such as
fesetenv and feholdexcept, need to use prctl with PR_GET_FPEXC and
PR_SET_FPEXC anyway, and so it is natural to record the state of those
bits during that call into the kernel and so avoid any need for a
separate call into the kernel to inform it of a change to those bits.
Thus, the interface I chose to use (in this patch and the glibc port)
is that one of those prctl calls must be made after any userspace
change to those sticky bits, other than through a floating-point
operation that traps into the kernel anyway. feclearexcept and
fesetexceptflag duly make those calls, which would not be required
were it not for this issue.
The previous EGLIBC port, and the uClibc code copied from it, is
fundamentally broken as regards any use of prctl for floating-point
exceptions because it didn't use the PR_FP_EXC_SW_ENABLE bit in its
prctl calls (and did various worse things, such as passing a pointer
when prctl expected an integer). If you avoid anything where prctl is
used, the clearing of sticky bits still means it will never give
anything approximating correct exception semantics with existing
kernels. I don't believe the patch makes things any worse for
existing code that doesn't try to inform the kernel of changes to
sticky bits - such code may get incorrect exceptions in some cases,
but it would have done so anyway in other cases.
Signed-off-by: Joseph Myers <joseph@codesourcery.com>
Signed-off-by: Scott Wood <scottwood@freescale.com>
2013-12-11 07:07:45 +08:00
|
|
|
} else
|
2007-09-13 14:44:20 +08:00
|
|
|
return -EINVAL;
|
2005-09-26 14:04:21 +08:00
|
|
|
#else
|
|
|
|
return -EINVAL;
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
val = __unpack_fe01(tsk->thread.fpexc_mode);
|
|
|
|
return put_user(val, (unsigned int __user *) adr);
|
|
|
|
}
|
|
|
|
|
2006-06-07 14:14:40 +08:00
|
|
|
int set_endian(struct task_struct *tsk, unsigned int val)
|
|
|
|
{
|
|
|
|
struct pt_regs *regs = tsk->thread.regs;
|
|
|
|
|
|
|
|
if ((val == PR_ENDIAN_LITTLE && !cpu_has_feature(CPU_FTR_REAL_LE)) ||
|
|
|
|
(val == PR_ENDIAN_PPC_LITTLE && !cpu_has_feature(CPU_FTR_PPC_LE)))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (regs == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (val == PR_ENDIAN_BIG)
|
|
|
|
regs->msr &= ~MSR_LE;
|
|
|
|
else if (val == PR_ENDIAN_LITTLE || val == PR_ENDIAN_PPC_LITTLE)
|
|
|
|
regs->msr |= MSR_LE;
|
|
|
|
else
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_endian(struct task_struct *tsk, unsigned long adr)
|
|
|
|
{
|
|
|
|
struct pt_regs *regs = tsk->thread.regs;
|
|
|
|
unsigned int val;
|
|
|
|
|
|
|
|
if (!cpu_has_feature(CPU_FTR_PPC_LE) &&
|
|
|
|
!cpu_has_feature(CPU_FTR_REAL_LE))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (regs == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (regs->msr & MSR_LE) {
|
|
|
|
if (cpu_has_feature(CPU_FTR_REAL_LE))
|
|
|
|
val = PR_ENDIAN_LITTLE;
|
|
|
|
else
|
|
|
|
val = PR_ENDIAN_PPC_LITTLE;
|
|
|
|
} else
|
|
|
|
val = PR_ENDIAN_BIG;
|
|
|
|
|
|
|
|
return put_user(val, (unsigned int __user *)adr);
|
|
|
|
}
|
|
|
|
|
2006-06-07 14:15:39 +08:00
|
|
|
int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
|
|
|
|
{
|
|
|
|
tsk->thread.align_ctl = val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
|
|
|
|
{
|
|
|
|
return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
|
|
|
|
}
|
|
|
|
|
2007-02-19 08:42:42 +08:00
|
|
|
static inline int valid_irq_stack(unsigned long sp, struct task_struct *p,
|
|
|
|
unsigned long nbytes)
|
|
|
|
{
|
|
|
|
unsigned long stack_page;
|
|
|
|
unsigned long cpu = task_cpu(p);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Avoid crashing if the stack has overflowed and corrupted
|
|
|
|
* task_cpu(p), which is in the thread_info struct.
|
|
|
|
*/
|
|
|
|
if (cpu < NR_CPUS && cpu_possible(cpu)) {
|
|
|
|
stack_page = (unsigned long) hardirq_ctx[cpu];
|
|
|
|
if (sp >= stack_page + sizeof(struct thread_struct)
|
|
|
|
&& sp <= stack_page + THREAD_SIZE - nbytes)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
stack_page = (unsigned long) softirq_ctx[cpu];
|
|
|
|
if (sp >= stack_page + sizeof(struct thread_struct)
|
|
|
|
&& sp <= stack_page + THREAD_SIZE - nbytes)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-27 08:46:18 +08:00
|
|
|
int validate_sp(unsigned long sp, struct task_struct *p,
|
2005-09-26 14:04:21 +08:00
|
|
|
unsigned long nbytes)
|
|
|
|
{
|
2006-01-12 17:06:02 +08:00
|
|
|
unsigned long stack_page = (unsigned long)task_stack_page(p);
|
2005-09-26 14:04:21 +08:00
|
|
|
|
|
|
|
if (sp >= stack_page + sizeof(struct thread_struct)
|
|
|
|
&& sp <= stack_page + THREAD_SIZE - nbytes)
|
|
|
|
return 1;
|
|
|
|
|
2007-02-19 08:42:42 +08:00
|
|
|
return valid_irq_stack(sp, p, nbytes);
|
2005-09-26 14:04:21 +08:00
|
|
|
}
|
|
|
|
|
2006-03-27 08:46:18 +08:00
|
|
|
EXPORT_SYMBOL(validate_sp);
|
|
|
|
|
2005-09-26 14:04:21 +08:00
|
|
|
unsigned long get_wchan(struct task_struct *p)
|
|
|
|
{
|
|
|
|
unsigned long ip, sp;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
if (!p || p == current || p->state == TASK_RUNNING)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
sp = p->thread.ksp;
|
2008-04-17 12:34:59 +08:00
|
|
|
if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
|
2005-09-26 14:04:21 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
sp = *(unsigned long *)sp;
|
2008-04-17 12:34:59 +08:00
|
|
|
if (!validate_sp(sp, p, STACK_FRAME_OVERHEAD))
|
2005-09-26 14:04:21 +08:00
|
|
|
return 0;
|
|
|
|
if (count > 0) {
|
2008-04-17 12:34:59 +08:00
|
|
|
ip = ((unsigned long *)sp)[STACK_FRAME_LR_SAVE];
|
2005-09-26 14:04:21 +08:00
|
|
|
if (!in_sched_functions(ip))
|
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
} while (count++ < 16);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-10-10 20:29:05 +08:00
|
|
|
|
2008-11-20 11:24:07 +08:00
|
|
|
static int kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
|
2005-10-10 20:29:05 +08:00
|
|
|
|
|
|
|
void show_stack(struct task_struct *tsk, unsigned long *stack)
|
|
|
|
{
|
|
|
|
unsigned long sp, ip, lr, newsp;
|
|
|
|
int count = 0;
|
|
|
|
int firstframe = 1;
|
2009-02-10 13:10:27 +08:00
|
|
|
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
|
|
|
|
int curr_frame = current->curr_ret_stack;
|
|
|
|
extern void return_to_handler(void);
|
2009-09-15 23:20:15 +08:00
|
|
|
unsigned long rth = (unsigned long)return_to_handler;
|
2009-02-10 13:10:27 +08:00
|
|
|
#endif
|
2005-10-10 20:29:05 +08:00
|
|
|
|
|
|
|
sp = (unsigned long) stack;
|
|
|
|
if (tsk == NULL)
|
|
|
|
tsk = current;
|
|
|
|
if (sp == 0) {
|
|
|
|
if (tsk == current)
|
2014-10-13 16:41:39 +08:00
|
|
|
sp = current_stack_pointer();
|
2005-10-10 20:29:05 +08:00
|
|
|
else
|
|
|
|
sp = tsk->thread.ksp;
|
|
|
|
}
|
|
|
|
|
|
|
|
lr = 0;
|
|
|
|
printk("Call Trace:\n");
|
|
|
|
do {
|
2008-04-17 12:34:59 +08:00
|
|
|
if (!validate_sp(sp, tsk, STACK_FRAME_OVERHEAD))
|
2005-10-10 20:29:05 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
stack = (unsigned long *) sp;
|
|
|
|
newsp = stack[0];
|
2008-04-17 12:34:59 +08:00
|
|
|
ip = stack[STACK_FRAME_LR_SAVE];
|
2005-10-10 20:29:05 +08:00
|
|
|
if (!firstframe || ip != lr) {
|
2008-07-07 11:44:31 +08:00
|
|
|
printk("["REG"] ["REG"] %pS", sp, ip, (void *)ip);
|
2009-02-10 13:10:27 +08:00
|
|
|
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
|
2014-09-17 15:07:03 +08:00
|
|
|
if ((ip == rth) && curr_frame >= 0) {
|
2009-02-10 13:10:27 +08:00
|
|
|
printk(" (%pS)",
|
|
|
|
(void *)current->ret_stack[curr_frame].ret);
|
|
|
|
curr_frame--;
|
|
|
|
}
|
|
|
|
#endif
|
2005-10-10 20:29:05 +08:00
|
|
|
if (firstframe)
|
|
|
|
printk(" (unreliable)");
|
|
|
|
printk("\n");
|
|
|
|
}
|
|
|
|
firstframe = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if this is an exception frame.
|
|
|
|
* We look for the "regshere" marker in the current frame.
|
|
|
|
*/
|
2008-04-17 12:34:59 +08:00
|
|
|
if (validate_sp(sp, tsk, STACK_INT_FRAME_SIZE)
|
|
|
|
&& stack[STACK_FRAME_MARKER] == STACK_FRAME_REGS_MARKER) {
|
2005-10-10 20:29:05 +08:00
|
|
|
struct pt_regs *regs = (struct pt_regs *)
|
|
|
|
(sp + STACK_FRAME_OVERHEAD);
|
|
|
|
lr = regs->link;
|
2014-06-12 14:53:08 +08:00
|
|
|
printk("--- interrupt: %lx at %pS\n LR = %pS\n",
|
2008-07-07 11:44:31 +08:00
|
|
|
regs->trap, (void *)regs->nip, (void *)lr);
|
2005-10-10 20:29:05 +08:00
|
|
|
firstframe = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sp = newsp;
|
|
|
|
} while (count++ < kstack_depth_to_print);
|
|
|
|
}
|
|
|
|
|
2006-02-13 11:48:35 +08:00
|
|
|
#ifdef CONFIG_PPC64
|
2012-03-01 09:45:27 +08:00
|
|
|
/* Called with hard IRQs off */
|
2013-06-13 19:04:56 +08:00
|
|
|
void notrace __ppc64_runlatch_on(void)
|
2006-02-13 11:48:35 +08:00
|
|
|
{
|
2012-03-01 09:45:27 +08:00
|
|
|
struct thread_info *ti = current_thread_info();
|
2006-02-13 11:48:35 +08:00
|
|
|
unsigned long ctrl;
|
|
|
|
|
2012-03-01 09:45:27 +08:00
|
|
|
ctrl = mfspr(SPRN_CTRLF);
|
|
|
|
ctrl |= CTRL_RUNLATCH;
|
|
|
|
mtspr(SPRN_CTRLT, ctrl);
|
2006-02-13 11:48:35 +08:00
|
|
|
|
2012-04-11 08:42:15 +08:00
|
|
|
ti->local_flags |= _TLF_RUNLATCH;
|
2006-02-13 11:48:35 +08:00
|
|
|
}
|
|
|
|
|
2012-03-01 09:45:27 +08:00
|
|
|
/* Called with hard IRQs off */
|
2013-06-13 19:04:56 +08:00
|
|
|
void notrace __ppc64_runlatch_off(void)
|
2006-02-13 11:48:35 +08:00
|
|
|
{
|
2012-03-01 09:45:27 +08:00
|
|
|
struct thread_info *ti = current_thread_info();
|
2006-02-13 11:48:35 +08:00
|
|
|
unsigned long ctrl;
|
|
|
|
|
2012-04-11 08:42:15 +08:00
|
|
|
ti->local_flags &= ~_TLF_RUNLATCH;
|
2006-02-13 11:48:35 +08:00
|
|
|
|
2010-08-06 11:28:19 +08:00
|
|
|
ctrl = mfspr(SPRN_CTRLF);
|
|
|
|
ctrl &= ~CTRL_RUNLATCH;
|
|
|
|
mtspr(SPRN_CTRLT, ctrl);
|
2006-02-13 11:48:35 +08:00
|
|
|
}
|
2012-03-01 09:45:27 +08:00
|
|
|
#endif /* CONFIG_PPC64 */
|
2008-04-18 14:56:17 +08:00
|
|
|
|
2009-02-22 09:50:03 +08:00
|
|
|
unsigned long arch_align_stack(unsigned long sp)
|
|
|
|
{
|
|
|
|
if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
|
|
|
|
sp -= get_random_int() & ~PAGE_MASK;
|
|
|
|
return sp & ~0xf;
|
|
|
|
}
|
2009-02-22 09:50:04 +08:00
|
|
|
|
|
|
|
static inline unsigned long brk_rnd(void)
|
|
|
|
{
|
|
|
|
unsigned long rnd = 0;
|
|
|
|
|
|
|
|
/* 8MB for 32bit, 1GB for 64bit */
|
|
|
|
if (is_32bit_task())
|
|
|
|
rnd = (long)(get_random_int() % (1<<(23-PAGE_SHIFT)));
|
|
|
|
else
|
|
|
|
rnd = (long)(get_random_int() % (1<<(30-PAGE_SHIFT)));
|
|
|
|
|
|
|
|
return rnd << PAGE_SHIFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long arch_randomize_brk(struct mm_struct *mm)
|
|
|
|
{
|
2009-09-22 00:52:35 +08:00
|
|
|
unsigned long base = mm->brk;
|
|
|
|
unsigned long ret;
|
|
|
|
|
2009-10-16 15:05:17 +08:00
|
|
|
#ifdef CONFIG_PPC_STD_MMU_64
|
2009-09-22 00:52:35 +08:00
|
|
|
/*
|
|
|
|
* If we are using 1TB segments and we are allowed to randomise
|
|
|
|
* the heap, we can put it above 1TB so it is backed by a 1TB
|
|
|
|
* segment. Otherwise the heap will be in the bottom 1TB
|
|
|
|
* which always uses 256MB segments and this may result in a
|
|
|
|
* performance penalty.
|
|
|
|
*/
|
|
|
|
if (!is_32bit_task() && (mmu_highuser_ssize == MMU_SEGSIZE_1T))
|
|
|
|
base = max_t(unsigned long, mm->brk, 1UL << SID_SHIFT_1T);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ret = PAGE_ALIGN(base + brk_rnd());
|
2009-02-22 09:50:04 +08:00
|
|
|
|
|
|
|
if (ret < mm->brk)
|
|
|
|
return mm->brk;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2009-02-22 09:50:07 +08:00
|
|
|
|