2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995 Linus Torvalds
|
|
|
|
*
|
|
|
|
* Pentium III FXSR, SSE support
|
|
|
|
* Gareth Hughes <gareth@valinux.com>, May 2000
|
2008-01-30 20:31:03 +08:00
|
|
|
*
|
2005-04-17 06:20:36 +08:00
|
|
|
* X86-64 port
|
|
|
|
* Andi Kleen.
|
2005-06-26 05:55:00 +08:00
|
|
|
*
|
|
|
|
* CPU hotplug support - ashok.raj@intel.com
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file handles the architecture-dependent parts of process handling..
|
|
|
|
*/
|
|
|
|
|
2005-06-26 05:55:00 +08:00
|
|
|
#include <linux/cpu.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/sched.h>
|
2008-01-30 20:31:03 +08:00
|
|
|
#include <linux/fs.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/elfcore.h>
|
|
|
|
#include <linux/smp.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/user.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/delay.h>
|
2008-01-30 20:31:03 +08:00
|
|
|
#include <linux/module.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/ptrace.h>
|
2006-01-12 05:44:36 +08:00
|
|
|
#include <linux/notifier.h>
|
2006-03-26 17:38:20 +08:00
|
|
|
#include <linux/kprobes.h>
|
2007-05-08 15:27:03 +08:00
|
|
|
#include <linux/kdebug.h>
|
2008-04-14 06:24:18 +08:00
|
|
|
#include <linux/prctl.h>
|
2008-07-29 13:48:51 +08:00
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <linux/io.h>
|
2008-12-06 10:40:00 +08:00
|
|
|
#include <linux/ftrace.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#include <asm/pgtable.h>
|
|
|
|
#include <asm/processor.h>
|
|
|
|
#include <asm/i387.h>
|
2012-02-22 05:19:22 +08:00
|
|
|
#include <asm/fpu-internal.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <asm/mmu_context.h>
|
|
|
|
#include <asm/prctl.h>
|
|
|
|
#include <asm/desc.h>
|
|
|
|
#include <asm/proto.h>
|
|
|
|
#include <asm/ia32.h>
|
2006-01-12 05:44:36 +08:00
|
|
|
#include <asm/idle.h>
|
2008-07-22 00:04:13 +08:00
|
|
|
#include <asm/syscalls.h>
|
2009-06-02 02:14:55 +08:00
|
|
|
#include <asm/debugreg.h>
|
2012-03-29 01:11:12 +08:00
|
|
|
#include <asm/switch_to.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
asmlinkage extern void ret_from_fork(void);
|
|
|
|
|
2014-05-02 06:44:37 +08:00
|
|
|
__visible DEFINE_PER_CPU(unsigned long, old_rsp);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-30 20:31:03 +08:00
|
|
|
/* Prints also some state that isn't saved in the pt_regs */
|
2008-04-03 21:40:48 +08:00
|
|
|
void __show_regs(struct pt_regs *regs, int all)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
unsigned long cr0 = 0L, cr2 = 0L, cr3 = 0L, cr4 = 0L, fs, gs, shadowgs;
|
2007-07-21 23:10:42 +08:00
|
|
|
unsigned long d0, d1, d2, d3, d6, d7;
|
2008-01-30 20:31:03 +08:00
|
|
|
unsigned int fsindex, gsindex;
|
|
|
|
unsigned int ds, cs, es;
|
2009-12-08 16:29:42 +08:00
|
|
|
|
2009-12-28 16:26:59 +08:00
|
|
|
printk(KERN_DEFAULT "RIP: %04lx:[<%016lx>] ", regs->cs & 0xffff, regs->ip);
|
2013-10-25 21:06:58 +08:00
|
|
|
printk_address(regs->ip);
|
2009-12-28 16:26:59 +08:00
|
|
|
printk(KERN_DEFAULT "RSP: %04lx:%016lx EFLAGS: %08lx\n", regs->ss,
|
2008-07-29 13:48:52 +08:00
|
|
|
regs->sp, regs->flags);
|
2009-12-28 16:26:59 +08:00
|
|
|
printk(KERN_DEFAULT "RAX: %016lx RBX: %016lx RCX: %016lx\n",
|
2008-01-30 20:30:56 +08:00
|
|
|
regs->ax, regs->bx, regs->cx);
|
2009-12-28 16:26:59 +08:00
|
|
|
printk(KERN_DEFAULT "RDX: %016lx RSI: %016lx RDI: %016lx\n",
|
2008-01-30 20:30:56 +08:00
|
|
|
regs->dx, regs->si, regs->di);
|
2009-12-28 16:26:59 +08:00
|
|
|
printk(KERN_DEFAULT "RBP: %016lx R08: %016lx R09: %016lx\n",
|
2008-01-30 20:30:56 +08:00
|
|
|
regs->bp, regs->r8, regs->r9);
|
2009-12-28 16:26:59 +08:00
|
|
|
printk(KERN_DEFAULT "R10: %016lx R11: %016lx R12: %016lx\n",
|
2008-07-29 13:48:51 +08:00
|
|
|
regs->r10, regs->r11, regs->r12);
|
2009-12-28 16:26:59 +08:00
|
|
|
printk(KERN_DEFAULT "R13: %016lx R14: %016lx R15: %016lx\n",
|
2008-07-29 13:48:51 +08:00
|
|
|
regs->r13, regs->r14, regs->r15);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-07-29 13:48:51 +08:00
|
|
|
asm("movl %%ds,%0" : "=r" (ds));
|
|
|
|
asm("movl %%cs,%0" : "=r" (cs));
|
|
|
|
asm("movl %%es,%0" : "=r" (es));
|
2005-04-17 06:20:36 +08:00
|
|
|
asm("movl %%fs,%0" : "=r" (fsindex));
|
|
|
|
asm("movl %%gs,%0" : "=r" (gsindex));
|
|
|
|
|
|
|
|
rdmsrl(MSR_FS_BASE, fs);
|
2008-07-29 13:48:51 +08:00
|
|
|
rdmsrl(MSR_GS_BASE, gs);
|
|
|
|
rdmsrl(MSR_KERNEL_GS_BASE, shadowgs);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-04-03 21:40:48 +08:00
|
|
|
if (!all)
|
|
|
|
return;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-07-22 17:12:29 +08:00
|
|
|
cr0 = read_cr0();
|
|
|
|
cr2 = read_cr2();
|
|
|
|
cr3 = read_cr3();
|
2014-10-25 06:58:08 +08:00
|
|
|
cr4 = __read_cr4();
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-12-28 16:26:59 +08:00
|
|
|
printk(KERN_DEFAULT "FS: %016lx(%04x) GS:%016lx(%04x) knlGS:%016lx\n",
|
2008-07-29 13:48:51 +08:00
|
|
|
fs, fsindex, gs, gsindex, shadowgs);
|
2009-12-28 16:26:59 +08:00
|
|
|
printk(KERN_DEFAULT "CS: %04x DS: %04x ES: %04x CR0: %016lx\n", cs, ds,
|
2008-07-29 13:48:52 +08:00
|
|
|
es, cr0);
|
2009-12-28 16:26:59 +08:00
|
|
|
printk(KERN_DEFAULT "CR2: %016lx CR3: %016lx CR4: %016lx\n", cr2, cr3,
|
2008-07-29 13:48:52 +08:00
|
|
|
cr4);
|
2007-07-21 23:10:42 +08:00
|
|
|
|
|
|
|
get_debugreg(d0, 0);
|
|
|
|
get_debugreg(d1, 1);
|
|
|
|
get_debugreg(d2, 2);
|
|
|
|
get_debugreg(d3, 3);
|
|
|
|
get_debugreg(d6, 6);
|
|
|
|
get_debugreg(d7, 7);
|
2013-06-19 00:09:11 +08:00
|
|
|
|
|
|
|
/* Only print out debug registers if they are in their non-default state. */
|
|
|
|
if ((d0 == 0) && (d1 == 0) && (d2 == 0) && (d3 == 0) &&
|
|
|
|
(d6 == DR6_RESERVED) && (d7 == 0x400))
|
|
|
|
return;
|
|
|
|
|
|
|
|
printk(KERN_DEFAULT "DR0: %016lx DR1: %016lx DR2: %016lx\n", d0, d1, d2);
|
2009-12-28 16:26:59 +08:00
|
|
|
printk(KERN_DEFAULT "DR3: %016lx DR6: %016lx DR7: %016lx\n", d3, d6, d7);
|
2013-06-19 00:09:11 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void release_thread(struct task_struct *dead_task)
|
|
|
|
{
|
|
|
|
if (dead_task->mm) {
|
|
|
|
if (dead_task->mm->context.size) {
|
2012-11-06 14:45:46 +08:00
|
|
|
pr_warn("WARNING: dead process %s still has LDT? <%p/%d>\n",
|
2012-05-22 10:50:07 +08:00
|
|
|
dead_task->comm,
|
|
|
|
dead_task->mm->context.ldt,
|
|
|
|
dead_task->mm->context.size);
|
2005-04-17 06:20:36 +08:00
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void set_32bit_tls(struct task_struct *t, int tls, u32 addr)
|
|
|
|
{
|
2008-01-30 20:31:03 +08:00
|
|
|
struct user_desc ud = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.base_addr = addr,
|
|
|
|
.limit = 0xfffff,
|
|
|
|
.seg_32bit = 1,
|
|
|
|
.limit_in_pages = 1,
|
|
|
|
.useable = 1,
|
|
|
|
};
|
2008-01-30 20:33:23 +08:00
|
|
|
struct desc_struct *desc = t->thread.tls_array;
|
2005-04-17 06:20:36 +08:00
|
|
|
desc += tls;
|
2008-01-30 20:31:13 +08:00
|
|
|
fill_ldt(desc, &ud);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 read_32bit_tls(struct task_struct *t, int tls)
|
|
|
|
{
|
2008-01-30 20:30:45 +08:00
|
|
|
return get_desc_base(&t->thread.tls_array[tls]);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2009-04-03 07:56:59 +08:00
|
|
|
int copy_thread(unsigned long clone_flags, unsigned long sp,
|
2012-10-23 10:51:14 +08:00
|
|
|
unsigned long arg, struct task_struct *p)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int err;
|
2008-07-29 13:48:51 +08:00
|
|
|
struct pt_regs *childregs;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct task_struct *me = current;
|
|
|
|
|
2012-09-11 04:44:54 +08:00
|
|
|
p->thread.sp0 = (unsigned long)task_stack_page(p) + THREAD_SIZE;
|
|
|
|
childregs = task_pt_regs(p);
|
2008-01-30 20:31:02 +08:00
|
|
|
p->thread.sp = (unsigned long) childregs;
|
|
|
|
p->thread.usersp = me->thread.usersp;
|
2006-01-12 17:05:38 +08:00
|
|
|
set_tsk_thread_flag(p, TIF_FORK);
|
2009-06-02 02:14:55 +08:00
|
|
|
p->thread.io_bitmap_ptr = NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-06-25 12:19:00 +08:00
|
|
|
savesegment(gs, p->thread.gsindex);
|
2010-04-24 07:17:40 +08:00
|
|
|
p->thread.gs = p->thread.gsindex ? 0 : me->thread.gs;
|
2008-06-25 12:19:00 +08:00
|
|
|
savesegment(fs, p->thread.fsindex);
|
2010-04-24 07:17:40 +08:00
|
|
|
p->thread.fs = p->thread.fsindex ? 0 : me->thread.fs;
|
2008-06-25 12:19:00 +08:00
|
|
|
savesegment(es, p->thread.es);
|
|
|
|
savesegment(ds, p->thread.ds);
|
2012-09-11 04:44:54 +08:00
|
|
|
memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
|
|
|
|
|
2012-10-23 10:34:11 +08:00
|
|
|
if (unlikely(p->flags & PF_KTHREAD)) {
|
2012-09-11 04:44:54 +08:00
|
|
|
/* kernel thread */
|
|
|
|
memset(childregs, 0, sizeof(struct pt_regs));
|
|
|
|
childregs->sp = (unsigned long)childregs;
|
|
|
|
childregs->ss = __KERNEL_DS;
|
|
|
|
childregs->bx = sp; /* function */
|
|
|
|
childregs->bp = arg;
|
|
|
|
childregs->orig_ax = -1;
|
|
|
|
childregs->cs = __KERNEL_CS | get_kernel_rpl();
|
2013-04-28 07:10:11 +08:00
|
|
|
childregs->flags = X86_EFLAGS_IF | X86_EFLAGS_FIXED;
|
2012-09-11 04:44:54 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-10-23 10:34:11 +08:00
|
|
|
*childregs = *current_pt_regs();
|
2012-09-11 04:44:54 +08:00
|
|
|
|
|
|
|
childregs->ax = 0;
|
2012-10-23 10:34:11 +08:00
|
|
|
if (sp)
|
|
|
|
childregs->sp = sp;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-06-02 02:14:55 +08:00
|
|
|
err = -ENOMEM;
|
2006-09-26 16:52:28 +08:00
|
|
|
if (unlikely(test_tsk_thread_flag(me, TIF_IO_BITMAP))) {
|
2011-11-18 06:43:40 +08:00
|
|
|
p->thread.io_bitmap_ptr = kmemdup(me->thread.io_bitmap_ptr,
|
|
|
|
IO_BITMAP_BYTES, GFP_KERNEL);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!p->thread.io_bitmap_ptr) {
|
|
|
|
p->thread.io_bitmap_max = 0;
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2006-09-26 16:52:28 +08:00
|
|
|
set_tsk_thread_flag(p, TIF_IO_BITMAP);
|
2008-01-30 20:31:03 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set a new TLS for the child thread?
|
|
|
|
*/
|
|
|
|
if (clone_flags & CLONE_SETTLS) {
|
|
|
|
#ifdef CONFIG_IA32_EMULATION
|
2015-02-25 08:01:39 +08:00
|
|
|
if (is_ia32_task())
|
2008-01-30 20:30:46 +08:00
|
|
|
err = do_set_thread_area(p, -1,
|
2008-01-30 20:30:56 +08:00
|
|
|
(struct user_desc __user *)childregs->si, 0);
|
2008-07-29 13:48:51 +08:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
err = do_arch_prctl(p, ARCH_SET_FS, childregs->r8);
|
|
|
|
if (err)
|
2005-04-17 06:20:36 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
err = 0;
|
|
|
|
out:
|
|
|
|
if (err && p->thread.io_bitmap_ptr) {
|
|
|
|
kfree(p->thread.io_bitmap_ptr);
|
|
|
|
p->thread.io_bitmap_max = 0;
|
|
|
|
}
|
2009-06-02 02:14:55 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2009-10-10 06:56:53 +08:00
|
|
|
static void
|
|
|
|
start_thread_common(struct pt_regs *regs, unsigned long new_ip,
|
|
|
|
unsigned long new_sp,
|
|
|
|
unsigned int _cs, unsigned int _ss, unsigned int _ds)
|
2008-02-21 12:18:40 +08:00
|
|
|
{
|
2008-06-25 12:19:00 +08:00
|
|
|
loadsegment(fs, 0);
|
2009-10-10 06:56:53 +08:00
|
|
|
loadsegment(es, _ds);
|
|
|
|
loadsegment(ds, _ds);
|
2008-02-21 12:18:40 +08:00
|
|
|
load_gs_index(0);
|
2012-02-27 00:17:55 +08:00
|
|
|
current->thread.usersp = new_sp;
|
2008-02-21 12:18:40 +08:00
|
|
|
regs->ip = new_ip;
|
|
|
|
regs->sp = new_sp;
|
2012-05-11 15:35:27 +08:00
|
|
|
this_cpu_write(old_rsp, new_sp);
|
2009-10-10 06:56:53 +08:00
|
|
|
regs->cs = _cs;
|
|
|
|
regs->ss = _ss;
|
2009-10-09 09:02:54 +08:00
|
|
|
regs->flags = X86_EFLAGS_IF;
|
2008-02-21 12:18:40 +08:00
|
|
|
}
|
2009-10-10 06:56:53 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
|
|
|
|
{
|
|
|
|
start_thread_common(regs, new_ip, new_sp,
|
|
|
|
__USER_CS, __USER_DS, 0);
|
|
|
|
}
|
2008-02-21 12:18:40 +08:00
|
|
|
|
2009-10-09 09:02:54 +08:00
|
|
|
#ifdef CONFIG_IA32_EMULATION
|
|
|
|
void start_thread_ia32(struct pt_regs *regs, u32 new_ip, u32 new_sp)
|
|
|
|
{
|
2009-10-10 06:56:53 +08:00
|
|
|
start_thread_common(regs, new_ip, new_sp,
|
2012-02-20 02:06:34 +08:00
|
|
|
test_thread_flag(TIF_X32)
|
|
|
|
? __USER_CS : __USER32_CS,
|
|
|
|
__USER_DS, __USER_DS);
|
2009-10-09 09:02:54 +08:00
|
|
|
}
|
|
|
|
#endif
|
2008-02-21 12:18:40 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* switch_to(x,y) should switch tasks from x to y.
|
|
|
|
*
|
2008-01-30 20:31:03 +08:00
|
|
|
* This could still be optimized:
|
2005-04-17 06:20:36 +08:00
|
|
|
* - fold all the options into a flag word and test it with a single test.
|
|
|
|
* - could test fs/gs bitsliced
|
2006-02-04 04:51:38 +08:00
|
|
|
*
|
|
|
|
* Kprobes not supported here. Set the probe on schedule instead.
|
2008-12-06 10:40:00 +08:00
|
|
|
* Function graph tracer not supported too.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2013-08-06 06:02:39 +08:00
|
|
|
__visible __notrace_funcgraph struct task_struct *
|
2005-11-06 00:25:54 +08:00
|
|
|
__switch_to(struct task_struct *prev_p, struct task_struct *next_p)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-07-09 06:06:26 +08:00
|
|
|
struct thread_struct *prev = &prev_p->thread;
|
|
|
|
struct thread_struct *next = &next_p->thread;
|
2008-01-30 20:31:03 +08:00
|
|
|
int cpu = smp_processor_id();
|
2015-03-06 11:19:05 +08:00
|
|
|
struct tss_struct *tss = &per_cpu(cpu_tss, cpu);
|
2008-06-25 12:19:24 +08:00
|
|
|
unsigned fsindex, gsindex;
|
i387: re-introduce FPU state preloading at context switch time
After all the FPU state cleanups and finally finding the problem that
caused all our FPU save/restore problems, this re-introduces the
preloading of FPU state that was removed in commit b3b0870ef3ff ("i387:
do not preload FPU state at task switch time").
However, instead of simply reverting the removal, this reimplements
preloading with several fixes, most notably
- properly abstracted as a true FPU state switch, rather than as
open-coded save and restore with various hacks.
In particular, implementing it as a proper FPU state switch allows us
to optimize the CR0.TS flag accesses: there is no reason to set the
TS bit only to then almost immediately clear it again. CR0 accesses
are quite slow and expensive, don't flip the bit back and forth for
no good reason.
- Make sure that the same model works for both x86-32 and x86-64, so
that there are no gratuitous differences between the two due to the
way they save and restore segment state differently due to
architectural differences that really don't matter to the FPU state.
- Avoid exposing the "preload" state to the context switch routines,
and in particular allow the concept of lazy state restore: if nothing
else has used the FPU in the meantime, and the process is still on
the same CPU, we can avoid restoring state from memory entirely, just
re-expose the state that is still in the FPU unit.
That optimized lazy restore isn't actually implemented here, but the
infrastructure is set up for it. Of course, older CPU's that use
'fnsave' to save the state cannot take advantage of this, since the
state saving also trashes the state.
In other words, there is now an actual _design_ to the FPU state saving,
rather than just random historical baggage. Hopefully it's easier to
follow as a result.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-02-19 04:56:35 +08:00
|
|
|
fpu_switch_t fpu;
|
2006-09-26 16:52:36 +08:00
|
|
|
|
i387: support lazy restore of FPU state
This makes us recognize when we try to restore FPU state that matches
what we already have in the FPU on this CPU, and avoids the restore
entirely if so.
To do this, we add two new data fields:
- a percpu 'fpu_owner_task' variable that gets written any time we
update the "has_fpu" field, and thus acts as a kind of back-pointer
to the task that owns the CPU. The exception is when we save the FPU
state as part of a context switch - if the save can keep the FPU
state around, we leave the 'fpu_owner_task' variable pointing at the
task whose FP state still remains on the CPU.
- a per-thread 'last_cpu' field, that indicates which CPU that thread
used its FPU on last. We update this on every context switch
(writing an invalid CPU number if the last context switch didn't
leave the FPU in a lazily usable state), so we know that *that*
thread has done nothing else with the FPU since.
These two fields together can be used when next switching back to the
task to see if the CPU still matches: if 'fpu_owner_task' matches the
task we are switching to, we know that no other task (or kernel FPU
usage) touched the FPU on this CPU in the meantime, and if the current
CPU number matches the 'last_cpu' field, we know that this thread did no
other FP work on any other CPU, so the FPU state on the CPU must match
what was saved on last context switch.
In that case, we can avoid the 'f[x]rstor' entirely, and just clear the
CR0.TS bit.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-02-20 05:27:00 +08:00
|
|
|
fpu = switch_fpu_prepare(prev_p, next_p, cpu);
|
2012-02-17 11:11:15 +08:00
|
|
|
|
x86_64, switch_to(): Load TLS descriptors before switching DS and ES
Otherwise, if buggy user code points DS or ES into the TLS
array, they would be corrupted after a context switch.
This also significantly improves the comments and documents some
gotchas in the code.
Before this patch, the both tests below failed. With this
patch, the es test passes, although the gsbase test still fails.
----- begin es test -----
/*
* Copyright (c) 2014 Andy Lutomirski
* GPL v2
*/
static unsigned short GDT3(int idx)
{
return (idx << 3) | 3;
}
static int create_tls(int idx, unsigned int base)
{
struct user_desc desc = {
.entry_number = idx,
.base_addr = base,
.limit = 0xfffff,
.seg_32bit = 1,
.contents = 0, /* Data, grow-up */
.read_exec_only = 0,
.limit_in_pages = 1,
.seg_not_present = 0,
.useable = 0,
};
if (syscall(SYS_set_thread_area, &desc) != 0)
err(1, "set_thread_area");
return desc.entry_number;
}
int main()
{
int idx = create_tls(-1, 0);
printf("Allocated GDT index %d\n", idx);
unsigned short orig_es;
asm volatile ("mov %%es,%0" : "=rm" (orig_es));
int errors = 0;
int total = 1000;
for (int i = 0; i < total; i++) {
asm volatile ("mov %0,%%es" : : "rm" (GDT3(idx)));
usleep(100);
unsigned short es;
asm volatile ("mov %%es,%0" : "=rm" (es));
asm volatile ("mov %0,%%es" : : "rm" (orig_es));
if (es != GDT3(idx)) {
if (errors == 0)
printf("[FAIL]\tES changed from 0x%hx to 0x%hx\n",
GDT3(idx), es);
errors++;
}
}
if (errors) {
printf("[FAIL]\tES was corrupted %d/%d times\n", errors, total);
return 1;
} else {
printf("[OK]\tES was preserved\n");
return 0;
}
}
----- end es test -----
----- begin gsbase test -----
/*
* gsbase.c, a gsbase test
* Copyright (c) 2014 Andy Lutomirski
* GPL v2
*/
static unsigned char *testptr, *testptr2;
static unsigned char read_gs_testvals(void)
{
unsigned char ret;
asm volatile ("movb %%gs:%1, %0" : "=r" (ret) : "m" (*testptr));
return ret;
}
int main()
{
int errors = 0;
testptr = mmap((void *)0x200000000UL, 1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (testptr == MAP_FAILED)
err(1, "mmap");
testptr2 = mmap((void *)0x300000000UL, 1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (testptr2 == MAP_FAILED)
err(1, "mmap");
*testptr = 0;
*testptr2 = 1;
if (syscall(SYS_arch_prctl, ARCH_SET_GS,
(unsigned long)testptr2 - (unsigned long)testptr) != 0)
err(1, "ARCH_SET_GS");
usleep(100);
if (read_gs_testvals() == 1) {
printf("[OK]\tARCH_SET_GS worked\n");
} else {
printf("[FAIL]\tARCH_SET_GS failed\n");
errors++;
}
asm volatile ("mov %0,%%gs" : : "r" (0));
if (read_gs_testvals() == 0) {
printf("[OK]\tWriting 0 to gs worked\n");
} else {
printf("[FAIL]\tWriting 0 to gs failed\n");
errors++;
}
usleep(100);
if (read_gs_testvals() == 0) {
printf("[OK]\tgsbase is still zero\n");
} else {
printf("[FAIL]\tgsbase was corrupted\n");
errors++;
}
return errors == 0 ? 0 : 1;
}
----- end gsbase test -----
Signed-off-by: Andy Lutomirski <luto@amacapital.net>
Cc: <stable@vger.kernel.org>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/509d27c9fec78217691c3dad91cec87e1006b34a.1418075657.git.luto@amacapital.net
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2014-12-09 05:55:20 +08:00
|
|
|
/* Reload esp0 and ss1. */
|
2008-01-30 20:31:31 +08:00
|
|
|
load_sp0(tss, next);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-06-25 12:19:24 +08:00
|
|
|
/* We must save %fs and %gs before load_TLS() because
|
|
|
|
* %fs and %gs may be cleared by load_TLS().
|
|
|
|
*
|
|
|
|
* (e.g. xen_load_tls())
|
|
|
|
*/
|
|
|
|
savesegment(fs, fsindex);
|
|
|
|
savesegment(gs, gsindex);
|
|
|
|
|
x86_64, switch_to(): Load TLS descriptors before switching DS and ES
Otherwise, if buggy user code points DS or ES into the TLS
array, they would be corrupted after a context switch.
This also significantly improves the comments and documents some
gotchas in the code.
Before this patch, the both tests below failed. With this
patch, the es test passes, although the gsbase test still fails.
----- begin es test -----
/*
* Copyright (c) 2014 Andy Lutomirski
* GPL v2
*/
static unsigned short GDT3(int idx)
{
return (idx << 3) | 3;
}
static int create_tls(int idx, unsigned int base)
{
struct user_desc desc = {
.entry_number = idx,
.base_addr = base,
.limit = 0xfffff,
.seg_32bit = 1,
.contents = 0, /* Data, grow-up */
.read_exec_only = 0,
.limit_in_pages = 1,
.seg_not_present = 0,
.useable = 0,
};
if (syscall(SYS_set_thread_area, &desc) != 0)
err(1, "set_thread_area");
return desc.entry_number;
}
int main()
{
int idx = create_tls(-1, 0);
printf("Allocated GDT index %d\n", idx);
unsigned short orig_es;
asm volatile ("mov %%es,%0" : "=rm" (orig_es));
int errors = 0;
int total = 1000;
for (int i = 0; i < total; i++) {
asm volatile ("mov %0,%%es" : : "rm" (GDT3(idx)));
usleep(100);
unsigned short es;
asm volatile ("mov %%es,%0" : "=rm" (es));
asm volatile ("mov %0,%%es" : : "rm" (orig_es));
if (es != GDT3(idx)) {
if (errors == 0)
printf("[FAIL]\tES changed from 0x%hx to 0x%hx\n",
GDT3(idx), es);
errors++;
}
}
if (errors) {
printf("[FAIL]\tES was corrupted %d/%d times\n", errors, total);
return 1;
} else {
printf("[OK]\tES was preserved\n");
return 0;
}
}
----- end es test -----
----- begin gsbase test -----
/*
* gsbase.c, a gsbase test
* Copyright (c) 2014 Andy Lutomirski
* GPL v2
*/
static unsigned char *testptr, *testptr2;
static unsigned char read_gs_testvals(void)
{
unsigned char ret;
asm volatile ("movb %%gs:%1, %0" : "=r" (ret) : "m" (*testptr));
return ret;
}
int main()
{
int errors = 0;
testptr = mmap((void *)0x200000000UL, 1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (testptr == MAP_FAILED)
err(1, "mmap");
testptr2 = mmap((void *)0x300000000UL, 1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (testptr2 == MAP_FAILED)
err(1, "mmap");
*testptr = 0;
*testptr2 = 1;
if (syscall(SYS_arch_prctl, ARCH_SET_GS,
(unsigned long)testptr2 - (unsigned long)testptr) != 0)
err(1, "ARCH_SET_GS");
usleep(100);
if (read_gs_testvals() == 1) {
printf("[OK]\tARCH_SET_GS worked\n");
} else {
printf("[FAIL]\tARCH_SET_GS failed\n");
errors++;
}
asm volatile ("mov %0,%%gs" : : "r" (0));
if (read_gs_testvals() == 0) {
printf("[OK]\tWriting 0 to gs worked\n");
} else {
printf("[FAIL]\tWriting 0 to gs failed\n");
errors++;
}
usleep(100);
if (read_gs_testvals() == 0) {
printf("[OK]\tgsbase is still zero\n");
} else {
printf("[FAIL]\tgsbase was corrupted\n");
errors++;
}
return errors == 0 ? 0 : 1;
}
----- end gsbase test -----
Signed-off-by: Andy Lutomirski <luto@amacapital.net>
Cc: <stable@vger.kernel.org>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/509d27c9fec78217691c3dad91cec87e1006b34a.1418075657.git.luto@amacapital.net
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2014-12-09 05:55:20 +08:00
|
|
|
/*
|
|
|
|
* Load TLS before restoring any segments so that segment loads
|
|
|
|
* reference the correct GDT entries.
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
load_TLS(next, cpu);
|
|
|
|
|
2008-06-25 12:19:23 +08:00
|
|
|
/*
|
x86_64, switch_to(): Load TLS descriptors before switching DS and ES
Otherwise, if buggy user code points DS or ES into the TLS
array, they would be corrupted after a context switch.
This also significantly improves the comments and documents some
gotchas in the code.
Before this patch, the both tests below failed. With this
patch, the es test passes, although the gsbase test still fails.
----- begin es test -----
/*
* Copyright (c) 2014 Andy Lutomirski
* GPL v2
*/
static unsigned short GDT3(int idx)
{
return (idx << 3) | 3;
}
static int create_tls(int idx, unsigned int base)
{
struct user_desc desc = {
.entry_number = idx,
.base_addr = base,
.limit = 0xfffff,
.seg_32bit = 1,
.contents = 0, /* Data, grow-up */
.read_exec_only = 0,
.limit_in_pages = 1,
.seg_not_present = 0,
.useable = 0,
};
if (syscall(SYS_set_thread_area, &desc) != 0)
err(1, "set_thread_area");
return desc.entry_number;
}
int main()
{
int idx = create_tls(-1, 0);
printf("Allocated GDT index %d\n", idx);
unsigned short orig_es;
asm volatile ("mov %%es,%0" : "=rm" (orig_es));
int errors = 0;
int total = 1000;
for (int i = 0; i < total; i++) {
asm volatile ("mov %0,%%es" : : "rm" (GDT3(idx)));
usleep(100);
unsigned short es;
asm volatile ("mov %%es,%0" : "=rm" (es));
asm volatile ("mov %0,%%es" : : "rm" (orig_es));
if (es != GDT3(idx)) {
if (errors == 0)
printf("[FAIL]\tES changed from 0x%hx to 0x%hx\n",
GDT3(idx), es);
errors++;
}
}
if (errors) {
printf("[FAIL]\tES was corrupted %d/%d times\n", errors, total);
return 1;
} else {
printf("[OK]\tES was preserved\n");
return 0;
}
}
----- end es test -----
----- begin gsbase test -----
/*
* gsbase.c, a gsbase test
* Copyright (c) 2014 Andy Lutomirski
* GPL v2
*/
static unsigned char *testptr, *testptr2;
static unsigned char read_gs_testvals(void)
{
unsigned char ret;
asm volatile ("movb %%gs:%1, %0" : "=r" (ret) : "m" (*testptr));
return ret;
}
int main()
{
int errors = 0;
testptr = mmap((void *)0x200000000UL, 1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (testptr == MAP_FAILED)
err(1, "mmap");
testptr2 = mmap((void *)0x300000000UL, 1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (testptr2 == MAP_FAILED)
err(1, "mmap");
*testptr = 0;
*testptr2 = 1;
if (syscall(SYS_arch_prctl, ARCH_SET_GS,
(unsigned long)testptr2 - (unsigned long)testptr) != 0)
err(1, "ARCH_SET_GS");
usleep(100);
if (read_gs_testvals() == 1) {
printf("[OK]\tARCH_SET_GS worked\n");
} else {
printf("[FAIL]\tARCH_SET_GS failed\n");
errors++;
}
asm volatile ("mov %0,%%gs" : : "r" (0));
if (read_gs_testvals() == 0) {
printf("[OK]\tWriting 0 to gs worked\n");
} else {
printf("[FAIL]\tWriting 0 to gs failed\n");
errors++;
}
usleep(100);
if (read_gs_testvals() == 0) {
printf("[OK]\tgsbase is still zero\n");
} else {
printf("[FAIL]\tgsbase was corrupted\n");
errors++;
}
return errors == 0 ? 0 : 1;
}
----- end gsbase test -----
Signed-off-by: Andy Lutomirski <luto@amacapital.net>
Cc: <stable@vger.kernel.org>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/509d27c9fec78217691c3dad91cec87e1006b34a.1418075657.git.luto@amacapital.net
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2014-12-09 05:55:20 +08:00
|
|
|
* Leave lazy mode, flushing any hypercalls made here. This
|
|
|
|
* must be done after loading TLS entries in the GDT but before
|
|
|
|
* loading segments that might reference them, and and it must
|
|
|
|
* be done before math_state_restore, so the TS bit is up to
|
|
|
|
* date.
|
2008-06-25 12:19:23 +08:00
|
|
|
*/
|
2009-02-19 03:18:57 +08:00
|
|
|
arch_end_context_switch(next_p);
|
2008-06-25 12:19:23 +08:00
|
|
|
|
x86_64, switch_to(): Load TLS descriptors before switching DS and ES
Otherwise, if buggy user code points DS or ES into the TLS
array, they would be corrupted after a context switch.
This also significantly improves the comments and documents some
gotchas in the code.
Before this patch, the both tests below failed. With this
patch, the es test passes, although the gsbase test still fails.
----- begin es test -----
/*
* Copyright (c) 2014 Andy Lutomirski
* GPL v2
*/
static unsigned short GDT3(int idx)
{
return (idx << 3) | 3;
}
static int create_tls(int idx, unsigned int base)
{
struct user_desc desc = {
.entry_number = idx,
.base_addr = base,
.limit = 0xfffff,
.seg_32bit = 1,
.contents = 0, /* Data, grow-up */
.read_exec_only = 0,
.limit_in_pages = 1,
.seg_not_present = 0,
.useable = 0,
};
if (syscall(SYS_set_thread_area, &desc) != 0)
err(1, "set_thread_area");
return desc.entry_number;
}
int main()
{
int idx = create_tls(-1, 0);
printf("Allocated GDT index %d\n", idx);
unsigned short orig_es;
asm volatile ("mov %%es,%0" : "=rm" (orig_es));
int errors = 0;
int total = 1000;
for (int i = 0; i < total; i++) {
asm volatile ("mov %0,%%es" : : "rm" (GDT3(idx)));
usleep(100);
unsigned short es;
asm volatile ("mov %%es,%0" : "=rm" (es));
asm volatile ("mov %0,%%es" : : "rm" (orig_es));
if (es != GDT3(idx)) {
if (errors == 0)
printf("[FAIL]\tES changed from 0x%hx to 0x%hx\n",
GDT3(idx), es);
errors++;
}
}
if (errors) {
printf("[FAIL]\tES was corrupted %d/%d times\n", errors, total);
return 1;
} else {
printf("[OK]\tES was preserved\n");
return 0;
}
}
----- end es test -----
----- begin gsbase test -----
/*
* gsbase.c, a gsbase test
* Copyright (c) 2014 Andy Lutomirski
* GPL v2
*/
static unsigned char *testptr, *testptr2;
static unsigned char read_gs_testvals(void)
{
unsigned char ret;
asm volatile ("movb %%gs:%1, %0" : "=r" (ret) : "m" (*testptr));
return ret;
}
int main()
{
int errors = 0;
testptr = mmap((void *)0x200000000UL, 1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (testptr == MAP_FAILED)
err(1, "mmap");
testptr2 = mmap((void *)0x300000000UL, 1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (testptr2 == MAP_FAILED)
err(1, "mmap");
*testptr = 0;
*testptr2 = 1;
if (syscall(SYS_arch_prctl, ARCH_SET_GS,
(unsigned long)testptr2 - (unsigned long)testptr) != 0)
err(1, "ARCH_SET_GS");
usleep(100);
if (read_gs_testvals() == 1) {
printf("[OK]\tARCH_SET_GS worked\n");
} else {
printf("[FAIL]\tARCH_SET_GS failed\n");
errors++;
}
asm volatile ("mov %0,%%gs" : : "r" (0));
if (read_gs_testvals() == 0) {
printf("[OK]\tWriting 0 to gs worked\n");
} else {
printf("[FAIL]\tWriting 0 to gs failed\n");
errors++;
}
usleep(100);
if (read_gs_testvals() == 0) {
printf("[OK]\tgsbase is still zero\n");
} else {
printf("[FAIL]\tgsbase was corrupted\n");
errors++;
}
return errors == 0 ? 0 : 1;
}
----- end gsbase test -----
Signed-off-by: Andy Lutomirski <luto@amacapital.net>
Cc: <stable@vger.kernel.org>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/509d27c9fec78217691c3dad91cec87e1006b34a.1418075657.git.luto@amacapital.net
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2014-12-09 05:55:20 +08:00
|
|
|
/* Switch DS and ES.
|
|
|
|
*
|
|
|
|
* Reading them only returns the selectors, but writing them (if
|
|
|
|
* nonzero) loads the full descriptor from the GDT or LDT. The
|
|
|
|
* LDT for next is loaded in switch_mm, and the GDT is loaded
|
|
|
|
* above.
|
|
|
|
*
|
|
|
|
* We therefore need to write new values to the segment
|
|
|
|
* registers on every context switch unless both the new and old
|
|
|
|
* values are zero.
|
|
|
|
*
|
|
|
|
* Note that we don't need to do anything for CS and SS, as
|
|
|
|
* those are saved and restored as part of pt_regs.
|
|
|
|
*/
|
|
|
|
savesegment(es, prev->es);
|
|
|
|
if (unlikely(next->es | prev->es))
|
|
|
|
loadsegment(es, next->es);
|
|
|
|
|
|
|
|
savesegment(ds, prev->ds);
|
|
|
|
if (unlikely(next->ds | prev->ds))
|
|
|
|
loadsegment(ds, next->ds);
|
|
|
|
|
2008-07-29 13:48:51 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Switch FS and GS.
|
2008-07-09 06:06:26 +08:00
|
|
|
*
|
x86_64, switch_to(): Load TLS descriptors before switching DS and ES
Otherwise, if buggy user code points DS or ES into the TLS
array, they would be corrupted after a context switch.
This also significantly improves the comments and documents some
gotchas in the code.
Before this patch, the both tests below failed. With this
patch, the es test passes, although the gsbase test still fails.
----- begin es test -----
/*
* Copyright (c) 2014 Andy Lutomirski
* GPL v2
*/
static unsigned short GDT3(int idx)
{
return (idx << 3) | 3;
}
static int create_tls(int idx, unsigned int base)
{
struct user_desc desc = {
.entry_number = idx,
.base_addr = base,
.limit = 0xfffff,
.seg_32bit = 1,
.contents = 0, /* Data, grow-up */
.read_exec_only = 0,
.limit_in_pages = 1,
.seg_not_present = 0,
.useable = 0,
};
if (syscall(SYS_set_thread_area, &desc) != 0)
err(1, "set_thread_area");
return desc.entry_number;
}
int main()
{
int idx = create_tls(-1, 0);
printf("Allocated GDT index %d\n", idx);
unsigned short orig_es;
asm volatile ("mov %%es,%0" : "=rm" (orig_es));
int errors = 0;
int total = 1000;
for (int i = 0; i < total; i++) {
asm volatile ("mov %0,%%es" : : "rm" (GDT3(idx)));
usleep(100);
unsigned short es;
asm volatile ("mov %%es,%0" : "=rm" (es));
asm volatile ("mov %0,%%es" : : "rm" (orig_es));
if (es != GDT3(idx)) {
if (errors == 0)
printf("[FAIL]\tES changed from 0x%hx to 0x%hx\n",
GDT3(idx), es);
errors++;
}
}
if (errors) {
printf("[FAIL]\tES was corrupted %d/%d times\n", errors, total);
return 1;
} else {
printf("[OK]\tES was preserved\n");
return 0;
}
}
----- end es test -----
----- begin gsbase test -----
/*
* gsbase.c, a gsbase test
* Copyright (c) 2014 Andy Lutomirski
* GPL v2
*/
static unsigned char *testptr, *testptr2;
static unsigned char read_gs_testvals(void)
{
unsigned char ret;
asm volatile ("movb %%gs:%1, %0" : "=r" (ret) : "m" (*testptr));
return ret;
}
int main()
{
int errors = 0;
testptr = mmap((void *)0x200000000UL, 1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (testptr == MAP_FAILED)
err(1, "mmap");
testptr2 = mmap((void *)0x300000000UL, 1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (testptr2 == MAP_FAILED)
err(1, "mmap");
*testptr = 0;
*testptr2 = 1;
if (syscall(SYS_arch_prctl, ARCH_SET_GS,
(unsigned long)testptr2 - (unsigned long)testptr) != 0)
err(1, "ARCH_SET_GS");
usleep(100);
if (read_gs_testvals() == 1) {
printf("[OK]\tARCH_SET_GS worked\n");
} else {
printf("[FAIL]\tARCH_SET_GS failed\n");
errors++;
}
asm volatile ("mov %0,%%gs" : : "r" (0));
if (read_gs_testvals() == 0) {
printf("[OK]\tWriting 0 to gs worked\n");
} else {
printf("[FAIL]\tWriting 0 to gs failed\n");
errors++;
}
usleep(100);
if (read_gs_testvals() == 0) {
printf("[OK]\tgsbase is still zero\n");
} else {
printf("[FAIL]\tgsbase was corrupted\n");
errors++;
}
return errors == 0 ? 0 : 1;
}
----- end gsbase test -----
Signed-off-by: Andy Lutomirski <luto@amacapital.net>
Cc: <stable@vger.kernel.org>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/509d27c9fec78217691c3dad91cec87e1006b34a.1418075657.git.luto@amacapital.net
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2014-12-09 05:55:20 +08:00
|
|
|
* These are even more complicated than FS and GS: they have
|
|
|
|
* 64-bit bases are that controlled by arch_prctl. Those bases
|
|
|
|
* only differ from the values in the GDT or LDT if the selector
|
|
|
|
* is 0.
|
|
|
|
*
|
|
|
|
* Loading the segment register resets the hidden base part of
|
|
|
|
* the register to 0 or the value from the GDT / LDT. If the
|
|
|
|
* next base address zero, writing 0 to the segment register is
|
|
|
|
* much faster than using wrmsr to explicitly zero the base.
|
|
|
|
*
|
|
|
|
* The thread_struct.fs and thread_struct.gs values are 0
|
|
|
|
* if the fs and gs bases respectively are not overridden
|
|
|
|
* from the values implied by fsindex and gsindex. They
|
|
|
|
* are nonzero, and store the nonzero base addresses, if
|
|
|
|
* the bases are overridden.
|
|
|
|
*
|
|
|
|
* (fs != 0 && fsindex != 0) || (gs != 0 && gsindex != 0) should
|
|
|
|
* be impossible.
|
|
|
|
*
|
|
|
|
* Therefore we need to reload the segment registers if either
|
|
|
|
* the old or new selector is nonzero, and we need to override
|
|
|
|
* the base address if next thread expects it to be overridden.
|
|
|
|
*
|
|
|
|
* This code is unnecessarily slow in the case where the old and
|
|
|
|
* new indexes are zero and the new base is nonzero -- it will
|
|
|
|
* unnecessarily write 0 to the selector before writing the new
|
|
|
|
* base address.
|
|
|
|
*
|
|
|
|
* Note: This all depends on arch_prctl being the only way that
|
|
|
|
* user code can override the segment base. Once wrfsbase and
|
|
|
|
* wrgsbase are enabled, most of this code will need to change.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2008-07-09 06:06:26 +08:00
|
|
|
if (unlikely(fsindex | next->fsindex | prev->fs)) {
|
|
|
|
loadsegment(fs, next->fsindex);
|
x86_64, switch_to(): Load TLS descriptors before switching DS and ES
Otherwise, if buggy user code points DS or ES into the TLS
array, they would be corrupted after a context switch.
This also significantly improves the comments and documents some
gotchas in the code.
Before this patch, the both tests below failed. With this
patch, the es test passes, although the gsbase test still fails.
----- begin es test -----
/*
* Copyright (c) 2014 Andy Lutomirski
* GPL v2
*/
static unsigned short GDT3(int idx)
{
return (idx << 3) | 3;
}
static int create_tls(int idx, unsigned int base)
{
struct user_desc desc = {
.entry_number = idx,
.base_addr = base,
.limit = 0xfffff,
.seg_32bit = 1,
.contents = 0, /* Data, grow-up */
.read_exec_only = 0,
.limit_in_pages = 1,
.seg_not_present = 0,
.useable = 0,
};
if (syscall(SYS_set_thread_area, &desc) != 0)
err(1, "set_thread_area");
return desc.entry_number;
}
int main()
{
int idx = create_tls(-1, 0);
printf("Allocated GDT index %d\n", idx);
unsigned short orig_es;
asm volatile ("mov %%es,%0" : "=rm" (orig_es));
int errors = 0;
int total = 1000;
for (int i = 0; i < total; i++) {
asm volatile ("mov %0,%%es" : : "rm" (GDT3(idx)));
usleep(100);
unsigned short es;
asm volatile ("mov %%es,%0" : "=rm" (es));
asm volatile ("mov %0,%%es" : : "rm" (orig_es));
if (es != GDT3(idx)) {
if (errors == 0)
printf("[FAIL]\tES changed from 0x%hx to 0x%hx\n",
GDT3(idx), es);
errors++;
}
}
if (errors) {
printf("[FAIL]\tES was corrupted %d/%d times\n", errors, total);
return 1;
} else {
printf("[OK]\tES was preserved\n");
return 0;
}
}
----- end es test -----
----- begin gsbase test -----
/*
* gsbase.c, a gsbase test
* Copyright (c) 2014 Andy Lutomirski
* GPL v2
*/
static unsigned char *testptr, *testptr2;
static unsigned char read_gs_testvals(void)
{
unsigned char ret;
asm volatile ("movb %%gs:%1, %0" : "=r" (ret) : "m" (*testptr));
return ret;
}
int main()
{
int errors = 0;
testptr = mmap((void *)0x200000000UL, 1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (testptr == MAP_FAILED)
err(1, "mmap");
testptr2 = mmap((void *)0x300000000UL, 1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (testptr2 == MAP_FAILED)
err(1, "mmap");
*testptr = 0;
*testptr2 = 1;
if (syscall(SYS_arch_prctl, ARCH_SET_GS,
(unsigned long)testptr2 - (unsigned long)testptr) != 0)
err(1, "ARCH_SET_GS");
usleep(100);
if (read_gs_testvals() == 1) {
printf("[OK]\tARCH_SET_GS worked\n");
} else {
printf("[FAIL]\tARCH_SET_GS failed\n");
errors++;
}
asm volatile ("mov %0,%%gs" : : "r" (0));
if (read_gs_testvals() == 0) {
printf("[OK]\tWriting 0 to gs worked\n");
} else {
printf("[FAIL]\tWriting 0 to gs failed\n");
errors++;
}
usleep(100);
if (read_gs_testvals() == 0) {
printf("[OK]\tgsbase is still zero\n");
} else {
printf("[FAIL]\tgsbase was corrupted\n");
errors++;
}
return errors == 0 ? 0 : 1;
}
----- end gsbase test -----
Signed-off-by: Andy Lutomirski <luto@amacapital.net>
Cc: <stable@vger.kernel.org>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/509d27c9fec78217691c3dad91cec87e1006b34a.1418075657.git.luto@amacapital.net
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2014-12-09 05:55:20 +08:00
|
|
|
|
2008-07-29 13:48:51 +08:00
|
|
|
/*
|
x86_64, switch_to(): Load TLS descriptors before switching DS and ES
Otherwise, if buggy user code points DS or ES into the TLS
array, they would be corrupted after a context switch.
This also significantly improves the comments and documents some
gotchas in the code.
Before this patch, the both tests below failed. With this
patch, the es test passes, although the gsbase test still fails.
----- begin es test -----
/*
* Copyright (c) 2014 Andy Lutomirski
* GPL v2
*/
static unsigned short GDT3(int idx)
{
return (idx << 3) | 3;
}
static int create_tls(int idx, unsigned int base)
{
struct user_desc desc = {
.entry_number = idx,
.base_addr = base,
.limit = 0xfffff,
.seg_32bit = 1,
.contents = 0, /* Data, grow-up */
.read_exec_only = 0,
.limit_in_pages = 1,
.seg_not_present = 0,
.useable = 0,
};
if (syscall(SYS_set_thread_area, &desc) != 0)
err(1, "set_thread_area");
return desc.entry_number;
}
int main()
{
int idx = create_tls(-1, 0);
printf("Allocated GDT index %d\n", idx);
unsigned short orig_es;
asm volatile ("mov %%es,%0" : "=rm" (orig_es));
int errors = 0;
int total = 1000;
for (int i = 0; i < total; i++) {
asm volatile ("mov %0,%%es" : : "rm" (GDT3(idx)));
usleep(100);
unsigned short es;
asm volatile ("mov %%es,%0" : "=rm" (es));
asm volatile ("mov %0,%%es" : : "rm" (orig_es));
if (es != GDT3(idx)) {
if (errors == 0)
printf("[FAIL]\tES changed from 0x%hx to 0x%hx\n",
GDT3(idx), es);
errors++;
}
}
if (errors) {
printf("[FAIL]\tES was corrupted %d/%d times\n", errors, total);
return 1;
} else {
printf("[OK]\tES was preserved\n");
return 0;
}
}
----- end es test -----
----- begin gsbase test -----
/*
* gsbase.c, a gsbase test
* Copyright (c) 2014 Andy Lutomirski
* GPL v2
*/
static unsigned char *testptr, *testptr2;
static unsigned char read_gs_testvals(void)
{
unsigned char ret;
asm volatile ("movb %%gs:%1, %0" : "=r" (ret) : "m" (*testptr));
return ret;
}
int main()
{
int errors = 0;
testptr = mmap((void *)0x200000000UL, 1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (testptr == MAP_FAILED)
err(1, "mmap");
testptr2 = mmap((void *)0x300000000UL, 1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (testptr2 == MAP_FAILED)
err(1, "mmap");
*testptr = 0;
*testptr2 = 1;
if (syscall(SYS_arch_prctl, ARCH_SET_GS,
(unsigned long)testptr2 - (unsigned long)testptr) != 0)
err(1, "ARCH_SET_GS");
usleep(100);
if (read_gs_testvals() == 1) {
printf("[OK]\tARCH_SET_GS worked\n");
} else {
printf("[FAIL]\tARCH_SET_GS failed\n");
errors++;
}
asm volatile ("mov %0,%%gs" : : "r" (0));
if (read_gs_testvals() == 0) {
printf("[OK]\tWriting 0 to gs worked\n");
} else {
printf("[FAIL]\tWriting 0 to gs failed\n");
errors++;
}
usleep(100);
if (read_gs_testvals() == 0) {
printf("[OK]\tgsbase is still zero\n");
} else {
printf("[FAIL]\tgsbase was corrupted\n");
errors++;
}
return errors == 0 ? 0 : 1;
}
----- end gsbase test -----
Signed-off-by: Andy Lutomirski <luto@amacapital.net>
Cc: <stable@vger.kernel.org>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/509d27c9fec78217691c3dad91cec87e1006b34a.1418075657.git.luto@amacapital.net
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2014-12-09 05:55:20 +08:00
|
|
|
* If user code wrote a nonzero value to FS, then it also
|
|
|
|
* cleared the overridden base address.
|
|
|
|
*
|
|
|
|
* XXX: if user code wrote 0 to FS and cleared the base
|
|
|
|
* address itself, we won't notice and we'll incorrectly
|
|
|
|
* restore the prior base address next time we reschdule
|
|
|
|
* the process.
|
2008-07-09 06:06:26 +08:00
|
|
|
*/
|
|
|
|
if (fsindex)
|
2008-07-29 13:48:51 +08:00
|
|
|
prev->fs = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2008-07-09 06:06:26 +08:00
|
|
|
if (next->fs)
|
|
|
|
wrmsrl(MSR_FS_BASE, next->fs);
|
|
|
|
prev->fsindex = fsindex;
|
|
|
|
|
|
|
|
if (unlikely(gsindex | next->gsindex | prev->gs)) {
|
|
|
|
load_gs_index(next->gsindex);
|
x86_64, switch_to(): Load TLS descriptors before switching DS and ES
Otherwise, if buggy user code points DS or ES into the TLS
array, they would be corrupted after a context switch.
This also significantly improves the comments and documents some
gotchas in the code.
Before this patch, the both tests below failed. With this
patch, the es test passes, although the gsbase test still fails.
----- begin es test -----
/*
* Copyright (c) 2014 Andy Lutomirski
* GPL v2
*/
static unsigned short GDT3(int idx)
{
return (idx << 3) | 3;
}
static int create_tls(int idx, unsigned int base)
{
struct user_desc desc = {
.entry_number = idx,
.base_addr = base,
.limit = 0xfffff,
.seg_32bit = 1,
.contents = 0, /* Data, grow-up */
.read_exec_only = 0,
.limit_in_pages = 1,
.seg_not_present = 0,
.useable = 0,
};
if (syscall(SYS_set_thread_area, &desc) != 0)
err(1, "set_thread_area");
return desc.entry_number;
}
int main()
{
int idx = create_tls(-1, 0);
printf("Allocated GDT index %d\n", idx);
unsigned short orig_es;
asm volatile ("mov %%es,%0" : "=rm" (orig_es));
int errors = 0;
int total = 1000;
for (int i = 0; i < total; i++) {
asm volatile ("mov %0,%%es" : : "rm" (GDT3(idx)));
usleep(100);
unsigned short es;
asm volatile ("mov %%es,%0" : "=rm" (es));
asm volatile ("mov %0,%%es" : : "rm" (orig_es));
if (es != GDT3(idx)) {
if (errors == 0)
printf("[FAIL]\tES changed from 0x%hx to 0x%hx\n",
GDT3(idx), es);
errors++;
}
}
if (errors) {
printf("[FAIL]\tES was corrupted %d/%d times\n", errors, total);
return 1;
} else {
printf("[OK]\tES was preserved\n");
return 0;
}
}
----- end es test -----
----- begin gsbase test -----
/*
* gsbase.c, a gsbase test
* Copyright (c) 2014 Andy Lutomirski
* GPL v2
*/
static unsigned char *testptr, *testptr2;
static unsigned char read_gs_testvals(void)
{
unsigned char ret;
asm volatile ("movb %%gs:%1, %0" : "=r" (ret) : "m" (*testptr));
return ret;
}
int main()
{
int errors = 0;
testptr = mmap((void *)0x200000000UL, 1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (testptr == MAP_FAILED)
err(1, "mmap");
testptr2 = mmap((void *)0x300000000UL, 1, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
if (testptr2 == MAP_FAILED)
err(1, "mmap");
*testptr = 0;
*testptr2 = 1;
if (syscall(SYS_arch_prctl, ARCH_SET_GS,
(unsigned long)testptr2 - (unsigned long)testptr) != 0)
err(1, "ARCH_SET_GS");
usleep(100);
if (read_gs_testvals() == 1) {
printf("[OK]\tARCH_SET_GS worked\n");
} else {
printf("[FAIL]\tARCH_SET_GS failed\n");
errors++;
}
asm volatile ("mov %0,%%gs" : : "r" (0));
if (read_gs_testvals() == 0) {
printf("[OK]\tWriting 0 to gs worked\n");
} else {
printf("[FAIL]\tWriting 0 to gs failed\n");
errors++;
}
usleep(100);
if (read_gs_testvals() == 0) {
printf("[OK]\tgsbase is still zero\n");
} else {
printf("[FAIL]\tgsbase was corrupted\n");
errors++;
}
return errors == 0 ? 0 : 1;
}
----- end gsbase test -----
Signed-off-by: Andy Lutomirski <luto@amacapital.net>
Cc: <stable@vger.kernel.org>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/509d27c9fec78217691c3dad91cec87e1006b34a.1418075657.git.luto@amacapital.net
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2014-12-09 05:55:20 +08:00
|
|
|
|
|
|
|
/* This works (and fails) the same way as fsindex above. */
|
2008-07-09 06:06:26 +08:00
|
|
|
if (gsindex)
|
2008-07-29 13:48:51 +08:00
|
|
|
prev->gs = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2008-07-09 06:06:26 +08:00
|
|
|
if (next->gs)
|
|
|
|
wrmsrl(MSR_KERNEL_GS_BASE, next->gs);
|
|
|
|
prev->gsindex = gsindex;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
i387: re-introduce FPU state preloading at context switch time
After all the FPU state cleanups and finally finding the problem that
caused all our FPU save/restore problems, this re-introduces the
preloading of FPU state that was removed in commit b3b0870ef3ff ("i387:
do not preload FPU state at task switch time").
However, instead of simply reverting the removal, this reimplements
preloading with several fixes, most notably
- properly abstracted as a true FPU state switch, rather than as
open-coded save and restore with various hacks.
In particular, implementing it as a proper FPU state switch allows us
to optimize the CR0.TS flag accesses: there is no reason to set the
TS bit only to then almost immediately clear it again. CR0 accesses
are quite slow and expensive, don't flip the bit back and forth for
no good reason.
- Make sure that the same model works for both x86-32 and x86-64, so
that there are no gratuitous differences between the two due to the
way they save and restore segment state differently due to
architectural differences that really don't matter to the FPU state.
- Avoid exposing the "preload" state to the context switch routines,
and in particular allow the concept of lazy state restore: if nothing
else has used the FPU in the meantime, and the process is still on
the same CPU, we can avoid restoring state from memory entirely, just
re-expose the state that is still in the FPU unit.
That optimized lazy restore isn't actually implemented here, but the
infrastructure is set up for it. Of course, older CPU's that use
'fnsave' to save the state cannot take advantage of this, since the
state saving also trashes the state.
In other words, there is now an actual _design_ to the FPU state saving,
rather than just random historical baggage. Hopefully it's easier to
follow as a result.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-02-19 04:56:35 +08:00
|
|
|
switch_fpu_finish(next_p, fpu);
|
|
|
|
|
2008-07-29 13:48:51 +08:00
|
|
|
/*
|
2006-03-25 23:29:25 +08:00
|
|
|
* Switch the PDA and FPU contexts.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2012-05-11 15:35:27 +08:00
|
|
|
prev->usersp = this_cpu_read(old_rsp);
|
|
|
|
this_cpu_write(old_rsp, next->usersp);
|
|
|
|
this_cpu_write(current_task, next_p);
|
2006-04-20 08:36:45 +08:00
|
|
|
|
2013-08-14 20:51:00 +08:00
|
|
|
/*
|
|
|
|
* If it were not for PREEMPT_ACTIVE we could guarantee that the
|
|
|
|
* preempt_count of all tasks was equal here and this would not be
|
|
|
|
* needed.
|
|
|
|
*/
|
|
|
|
task_thread_info(prev_p)->saved_preempt_count = this_cpu_read(__preempt_count);
|
|
|
|
this_cpu_write(__preempt_count, task_thread_info(next_p)->saved_preempt_count);
|
|
|
|
|
2012-05-11 15:35:27 +08:00
|
|
|
this_cpu_write(kernel_stack,
|
2008-07-09 06:06:26 +08:00
|
|
|
(unsigned long)task_stack_page(next_p) +
|
2009-01-18 23:38:58 +08:00
|
|
|
THREAD_SIZE - KERNEL_STACK_OFFSET);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
2006-09-26 16:52:28 +08:00
|
|
|
* Now maybe reload the debug registers and handle I/O bitmaps
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2008-01-30 20:31:09 +08:00
|
|
|
if (unlikely(task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT ||
|
|
|
|
task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV))
|
2006-09-26 16:52:28 +08:00
|
|
|
__switch_to_xtra(prev_p, next_p, tss);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return prev_p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_personality_64bit(void)
|
|
|
|
{
|
|
|
|
/* inherit personality from parent */
|
|
|
|
|
|
|
|
/* Make sure to be in 64bit mode */
|
2008-01-30 20:31:03 +08:00
|
|
|
clear_thread_flag(TIF_IA32);
|
2012-02-07 05:03:09 +08:00
|
|
|
clear_thread_flag(TIF_ADDR32);
|
2012-02-15 05:56:49 +08:00
|
|
|
clear_thread_flag(TIF_X32);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-03-14 03:49:14 +08:00
|
|
|
/* Ensure the corresponding mm is not marked. */
|
|
|
|
if (current->mm)
|
|
|
|
current->mm->context.ia32_compat = 0;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* TBD: overwrites user setup. Should have two bits.
|
|
|
|
But 64bit processes have always behaved this way,
|
|
|
|
so it's not too bad. The main problem is just that
|
2008-01-30 20:31:03 +08:00
|
|
|
32bit childs are affected again. */
|
2005-04-17 06:20:36 +08:00
|
|
|
current->personality &= ~READ_IMPLIES_EXEC;
|
|
|
|
}
|
|
|
|
|
2012-02-20 02:06:34 +08:00
|
|
|
void set_personality_ia32(bool x32)
|
2010-01-29 14:14:43 +08:00
|
|
|
{
|
|
|
|
/* inherit personality from parent */
|
|
|
|
|
|
|
|
/* Make sure to be in 32bit mode */
|
2012-02-07 05:03:09 +08:00
|
|
|
set_thread_flag(TIF_ADDR32);
|
2010-01-29 14:14:43 +08:00
|
|
|
|
2011-03-14 03:49:14 +08:00
|
|
|
/* Mark the associated mm as containing 32-bit tasks. */
|
2012-02-20 02:06:34 +08:00
|
|
|
if (x32) {
|
|
|
|
clear_thread_flag(TIF_IA32);
|
|
|
|
set_thread_flag(TIF_X32);
|
2014-04-20 00:10:09 +08:00
|
|
|
if (current->mm)
|
|
|
|
current->mm->context.ia32_compat = TIF_X32;
|
2012-02-20 02:06:34 +08:00
|
|
|
current->personality &= ~READ_IMPLIES_EXEC;
|
2012-02-26 12:25:38 +08:00
|
|
|
/* is_compat_task() uses the presence of the x32
|
|
|
|
syscall bit flag to determine compat status */
|
|
|
|
current_thread_info()->status &= ~TS_COMPAT;
|
2012-02-20 02:06:34 +08:00
|
|
|
} else {
|
|
|
|
set_thread_flag(TIF_IA32);
|
|
|
|
clear_thread_flag(TIF_X32);
|
2014-04-20 00:10:09 +08:00
|
|
|
if (current->mm)
|
|
|
|
current->mm->context.ia32_compat = TIF_IA32;
|
2012-02-20 02:06:34 +08:00
|
|
|
current->personality |= force_personality32;
|
|
|
|
/* Prepare the first "return" to user space */
|
|
|
|
current_thread_info()->status |= TS_COMPAT;
|
|
|
|
}
|
2010-01-29 14:14:43 +08:00
|
|
|
}
|
2012-05-07 08:40:03 +08:00
|
|
|
EXPORT_SYMBOL_GPL(set_personality_ia32);
|
2010-01-29 14:14:43 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long get_wchan(struct task_struct *p)
|
|
|
|
{
|
|
|
|
unsigned long stack;
|
2008-07-29 13:48:51 +08:00
|
|
|
u64 fp, ip;
|
2005-04-17 06:20:36 +08:00
|
|
|
int count = 0;
|
|
|
|
|
2008-07-29 13:48:51 +08:00
|
|
|
if (!p || p == current || p->state == TASK_RUNNING)
|
|
|
|
return 0;
|
2006-01-12 17:05:39 +08:00
|
|
|
stack = (unsigned long)task_stack_page(p);
|
2008-10-08 05:15:11 +08:00
|
|
|
if (p->thread.sp < stack || p->thread.sp >= stack+THREAD_SIZE)
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
2008-01-30 20:31:02 +08:00
|
|
|
fp = *(u64 *)(p->thread.sp);
|
2008-07-29 13:48:51 +08:00
|
|
|
do {
|
2005-11-06 00:25:54 +08:00
|
|
|
if (fp < (unsigned long)stack ||
|
2008-10-08 05:15:11 +08:00
|
|
|
fp >= (unsigned long)stack+THREAD_SIZE)
|
2008-07-29 13:48:51 +08:00
|
|
|
return 0;
|
2008-01-30 20:30:56 +08:00
|
|
|
ip = *(u64 *)(fp+8);
|
|
|
|
if (!in_sched_functions(ip))
|
|
|
|
return ip;
|
2008-07-29 13:48:51 +08:00
|
|
|
fp = *(u64 *)fp;
|
|
|
|
} while (count++ < 16);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
long do_arch_prctl(struct task_struct *task, int code, unsigned long addr)
|
2008-07-29 13:48:51 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
int doit = task == current;
|
|
|
|
int cpu;
|
|
|
|
|
2008-07-29 13:48:51 +08:00
|
|
|
switch (code) {
|
2005-04-17 06:20:36 +08:00
|
|
|
case ARCH_SET_GS:
|
[PATCH] x86_64: TASK_SIZE fixes for compatibility mode processes
Appended patch will setup compatibility mode TASK_SIZE properly. This will
fix atleast three known bugs that can be encountered while running
compatibility mode apps.
a) A malicious 32bit app can have an elf section at 0xffffe000. During
exec of this app, we will have a memory leak as insert_vm_struct() is
not checking for return value in syscall32_setup_pages() and thus not
freeing the vma allocated for the vsyscall page. And instead of exec
failing (as it has addresses > TASK_SIZE), we were allowing it to
succeed previously.
b) With a 32bit app, hugetlb_get_unmapped_area/arch_get_unmapped_area
may return addresses beyond 32bits, ultimately causing corruption
because of wrap-around and resulting in SEGFAULT, instead of returning
ENOMEM.
c) 32bit app doing this below mmap will now fail.
mmap((void *)(0xFFFFE000UL), 0x10000UL, PROT_READ|PROT_WRITE,
MAP_FIXED|MAP_PRIVATE|MAP_ANON, 0, 0);
Signed-off-by: Zou Nan hai <nanhai.zou@intel.com>
Signed-off-by: Suresh Siddha <suresh.b.siddha@intel.com>
Cc: Andi Kleen <ak@muc.de>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-06-22 08:14:32 +08:00
|
|
|
if (addr >= TASK_SIZE_OF(task))
|
2008-07-29 13:48:51 +08:00
|
|
|
return -EPERM;
|
2005-04-17 06:20:36 +08:00
|
|
|
cpu = get_cpu();
|
2008-07-29 13:48:51 +08:00
|
|
|
/* handle small bases via the GDT because that's faster to
|
2005-04-17 06:20:36 +08:00
|
|
|
switch. */
|
2008-07-29 13:48:51 +08:00
|
|
|
if (addr <= 0xffffffff) {
|
|
|
|
set_32bit_tls(task, GS_TLS, addr);
|
|
|
|
if (doit) {
|
2005-04-17 06:20:36 +08:00
|
|
|
load_TLS(&task->thread, cpu);
|
2008-07-29 13:48:51 +08:00
|
|
|
load_gs_index(GS_TLS_SEL);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2008-07-29 13:48:51 +08:00
|
|
|
task->thread.gsindex = GS_TLS_SEL;
|
2005-04-17 06:20:36 +08:00
|
|
|
task->thread.gs = 0;
|
2008-07-29 13:48:51 +08:00
|
|
|
} else {
|
2005-04-17 06:20:36 +08:00
|
|
|
task->thread.gsindex = 0;
|
|
|
|
task->thread.gs = addr;
|
|
|
|
if (doit) {
|
2005-11-06 00:25:54 +08:00
|
|
|
load_gs_index(0);
|
2012-06-08 04:32:04 +08:00
|
|
|
ret = wrmsrl_safe(MSR_KERNEL_GS_BASE, addr);
|
2008-07-29 13:48:51 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
put_cpu();
|
|
|
|
break;
|
|
|
|
case ARCH_SET_FS:
|
|
|
|
/* Not strictly needed for fs, but do it for symmetry
|
|
|
|
with gs */
|
[PATCH] x86_64: TASK_SIZE fixes for compatibility mode processes
Appended patch will setup compatibility mode TASK_SIZE properly. This will
fix atleast three known bugs that can be encountered while running
compatibility mode apps.
a) A malicious 32bit app can have an elf section at 0xffffe000. During
exec of this app, we will have a memory leak as insert_vm_struct() is
not checking for return value in syscall32_setup_pages() and thus not
freeing the vma allocated for the vsyscall page. And instead of exec
failing (as it has addresses > TASK_SIZE), we were allowing it to
succeed previously.
b) With a 32bit app, hugetlb_get_unmapped_area/arch_get_unmapped_area
may return addresses beyond 32bits, ultimately causing corruption
because of wrap-around and resulting in SEGFAULT, instead of returning
ENOMEM.
c) 32bit app doing this below mmap will now fail.
mmap((void *)(0xFFFFE000UL), 0x10000UL, PROT_READ|PROT_WRITE,
MAP_FIXED|MAP_PRIVATE|MAP_ANON, 0, 0);
Signed-off-by: Zou Nan hai <nanhai.zou@intel.com>
Signed-off-by: Suresh Siddha <suresh.b.siddha@intel.com>
Cc: Andi Kleen <ak@muc.de>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-06-22 08:14:32 +08:00
|
|
|
if (addr >= TASK_SIZE_OF(task))
|
2008-01-30 20:31:03 +08:00
|
|
|
return -EPERM;
|
2005-04-17 06:20:36 +08:00
|
|
|
cpu = get_cpu();
|
2008-01-30 20:31:03 +08:00
|
|
|
/* handle small bases via the GDT because that's faster to
|
2005-04-17 06:20:36 +08:00
|
|
|
switch. */
|
2008-01-30 20:31:03 +08:00
|
|
|
if (addr <= 0xffffffff) {
|
2005-04-17 06:20:36 +08:00
|
|
|
set_32bit_tls(task, FS_TLS, addr);
|
2008-01-30 20:31:03 +08:00
|
|
|
if (doit) {
|
|
|
|
load_TLS(&task->thread, cpu);
|
2008-06-25 12:19:00 +08:00
|
|
|
loadsegment(fs, FS_TLS_SEL);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
task->thread.fsindex = FS_TLS_SEL;
|
|
|
|
task->thread.fs = 0;
|
2008-01-30 20:31:03 +08:00
|
|
|
} else {
|
2005-04-17 06:20:36 +08:00
|
|
|
task->thread.fsindex = 0;
|
|
|
|
task->thread.fs = addr;
|
|
|
|
if (doit) {
|
|
|
|
/* set the selector to 0 to not confuse
|
|
|
|
__switch_to */
|
2008-06-25 12:19:00 +08:00
|
|
|
loadsegment(fs, 0);
|
2012-06-08 04:32:04 +08:00
|
|
|
ret = wrmsrl_safe(MSR_FS_BASE, addr);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
put_cpu();
|
|
|
|
break;
|
2008-01-30 20:31:03 +08:00
|
|
|
case ARCH_GET_FS: {
|
|
|
|
unsigned long base;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (task->thread.fsindex == FS_TLS_SEL)
|
|
|
|
base = read_32bit_tls(task, FS_TLS);
|
2005-11-06 00:25:54 +08:00
|
|
|
else if (doit)
|
2005-04-17 06:20:36 +08:00
|
|
|
rdmsrl(MSR_FS_BASE, base);
|
2005-11-06 00:25:54 +08:00
|
|
|
else
|
2005-04-17 06:20:36 +08:00
|
|
|
base = task->thread.fs;
|
2008-01-30 20:31:03 +08:00
|
|
|
ret = put_user(base, (unsigned long __user *)addr);
|
|
|
|
break;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2008-01-30 20:31:03 +08:00
|
|
|
case ARCH_GET_GS: {
|
2005-04-17 06:20:36 +08:00
|
|
|
unsigned long base;
|
[PATCH] x86_64: Plug GS leak in arch_prctl()
In linux-2.6.16, we have noticed a problem where the gs base value
returned from an arch_prtcl(ARCH_GET_GS, ...) call will be incorrect if:
- the current/calling task has NOT set its own gs base yet to a
non-zero value,
- some other task that ran on the same processor previously set their
own gs base to a non-zero value.
In this situation, the ARCH_GET_GS code will read and return the
MSR_KERNEL_GS_BASE msr register.
However, since the __switch_to() code does NOT load/zero the
MSR_KERNEL_GS_BASE register when the task that is switched IN has a zero
next->gs value, the caller of arch_prctl(ARCH_GET_GS, ...) will get back
the value of some previous tasks's gs base value instead of 0.
Change the arch_prctl() ARCH_GET_GS code to only read and return
the MSR_KERNEL_GS_BASE msr register if the 'gs' register of the calling
task is non-zero.
Side note: Since in addition to using arch_prctl(ARCH_SET_GS, ...),
a task can also setup a gs base value by using modify_ldt() and write
an index value into 'gs' from user space, the patch below reads
'gs' instead of using thread.gs, since in the modify_ldt() case,
the thread.gs value will be 0, and incorrect value would be returned
(the task->thread.gs value).
When the user has not set its own gs base value and the 'gs'
register is zero, then the MSR_KERNEL_GS_BASE register will not be
read and a value of zero will be returned by reading and returning
'task->thread.gs'.
The first patch shown below is an attempt at implementing this
approach.
Signed-off-by: Andi Kleen <ak@suse.de>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-04-08 01:50:25 +08:00
|
|
|
unsigned gsindex;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (task->thread.gsindex == GS_TLS_SEL)
|
|
|
|
base = read_32bit_tls(task, GS_TLS);
|
[PATCH] x86_64: Plug GS leak in arch_prctl()
In linux-2.6.16, we have noticed a problem where the gs base value
returned from an arch_prtcl(ARCH_GET_GS, ...) call will be incorrect if:
- the current/calling task has NOT set its own gs base yet to a
non-zero value,
- some other task that ran on the same processor previously set their
own gs base to a non-zero value.
In this situation, the ARCH_GET_GS code will read and return the
MSR_KERNEL_GS_BASE msr register.
However, since the __switch_to() code does NOT load/zero the
MSR_KERNEL_GS_BASE register when the task that is switched IN has a zero
next->gs value, the caller of arch_prctl(ARCH_GET_GS, ...) will get back
the value of some previous tasks's gs base value instead of 0.
Change the arch_prctl() ARCH_GET_GS code to only read and return
the MSR_KERNEL_GS_BASE msr register if the 'gs' register of the calling
task is non-zero.
Side note: Since in addition to using arch_prctl(ARCH_SET_GS, ...),
a task can also setup a gs base value by using modify_ldt() and write
an index value into 'gs' from user space, the patch below reads
'gs' instead of using thread.gs, since in the modify_ldt() case,
the thread.gs value will be 0, and incorrect value would be returned
(the task->thread.gs value).
When the user has not set its own gs base value and the 'gs'
register is zero, then the MSR_KERNEL_GS_BASE register will not be
read and a value of zero will be returned by reading and returning
'task->thread.gs'.
The first patch shown below is an attempt at implementing this
approach.
Signed-off-by: Andi Kleen <ak@suse.de>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-04-08 01:50:25 +08:00
|
|
|
else if (doit) {
|
2008-06-25 12:19:00 +08:00
|
|
|
savesegment(gs, gsindex);
|
[PATCH] x86_64: Plug GS leak in arch_prctl()
In linux-2.6.16, we have noticed a problem where the gs base value
returned from an arch_prtcl(ARCH_GET_GS, ...) call will be incorrect if:
- the current/calling task has NOT set its own gs base yet to a
non-zero value,
- some other task that ran on the same processor previously set their
own gs base to a non-zero value.
In this situation, the ARCH_GET_GS code will read and return the
MSR_KERNEL_GS_BASE msr register.
However, since the __switch_to() code does NOT load/zero the
MSR_KERNEL_GS_BASE register when the task that is switched IN has a zero
next->gs value, the caller of arch_prctl(ARCH_GET_GS, ...) will get back
the value of some previous tasks's gs base value instead of 0.
Change the arch_prctl() ARCH_GET_GS code to only read and return
the MSR_KERNEL_GS_BASE msr register if the 'gs' register of the calling
task is non-zero.
Side note: Since in addition to using arch_prctl(ARCH_SET_GS, ...),
a task can also setup a gs base value by using modify_ldt() and write
an index value into 'gs' from user space, the patch below reads
'gs' instead of using thread.gs, since in the modify_ldt() case,
the thread.gs value will be 0, and incorrect value would be returned
(the task->thread.gs value).
When the user has not set its own gs base value and the 'gs'
register is zero, then the MSR_KERNEL_GS_BASE register will not be
read and a value of zero will be returned by reading and returning
'task->thread.gs'.
The first patch shown below is an attempt at implementing this
approach.
Signed-off-by: Andi Kleen <ak@suse.de>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-04-08 01:50:25 +08:00
|
|
|
if (gsindex)
|
|
|
|
rdmsrl(MSR_KERNEL_GS_BASE, base);
|
|
|
|
else
|
|
|
|
base = task->thread.gs;
|
2008-07-29 13:48:51 +08:00
|
|
|
} else
|
2005-04-17 06:20:36 +08:00
|
|
|
base = task->thread.gs;
|
2008-01-30 20:31:03 +08:00
|
|
|
ret = put_user(base, (unsigned long __user *)addr);
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
2008-01-30 20:31:03 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-01-30 20:31:03 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
long sys_arch_prctl(int code, unsigned long addr)
|
|
|
|
{
|
|
|
|
return do_arch_prctl(current, code, addr);
|
|
|
|
}
|
|
|
|
|
2009-11-03 17:22:40 +08:00
|
|
|
unsigned long KSTK_ESP(struct task_struct *task)
|
|
|
|
{
|
|
|
|
return (test_tsk_thread_flag(task, TIF_IA32)) ?
|
|
|
|
(task_pt_regs(task)->sp) : ((task)->thread.usersp);
|
|
|
|
}
|