2005-04-16 16:20:36 -06:00
|
|
|
/*
|
2007-10-16 02:27:00 -06:00
|
|
|
* Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
|
2005-04-16 16:20:36 -06:00
|
|
|
* Licensed under the GPL
|
|
|
|
*/
|
|
|
|
|
uml: header untangling
Untangle UML headers somewhat and add some includes where they were
needed explicitly, but gotten accidentally via some other header.
arch/um/include/um_uaccess.h loses asm/fixmap.h because it uses no
fixmap stuff and gains elf.h, because it needs FIXADDR_USER_*, and
archsetjmp.h, because it needs jmp_buf.
pmd_alloc_one is uninlined because it needs mm_struct, and that's
inconvenient to provide in asm-um/pgtable-3level.h.
elf_core_copy_fpregs is also uninlined from elf-i386.h and
elf-x86_64.h, which duplicated the code anyway, to
arch/um/kernel/process.c, so that the reference to current_thread
doesn't pull sched.h or anything related into asm/elf.h.
arch/um/sys-i386/ldt.c, arch/um/kernel/tlb.c and
arch/um/kernel/skas/uaccess.c got sched.h because they dereference
task_structs. Its includes of linux and asm headers got turned from
"" to <>.
arch/um/sys-i386/bug.c gets asm/errno.h because it needs errno
constants.
asm/elf-i386 gets asm/user.h because it needs user_regs_struct.
asm/fixmap.h gets page.h because it needs PAGE_SIZE and PAGE_MASK and
system.h for BUG_ON.
asm/pgtable doesn't need sched.h.
asm/processor-generic.h defined mm_segment_t, but didn't use it. So,
that definition is moved to uaccess.h, which defines a bunch of
mm_segment_t-related stuff. thread_info.h uses mm_segment_t, and
includes uaccess.h, which causes a recursion. So, the definition is
placed above the include of thread_info. in uaccess.h. thread_info.h
also gets page.h because it needs PAGE_SIZE.
ObCheckpatchViolationJustification - I'm not adding a typedef; I'm
moving mm_segment_t from one place to another.
Signed-off-by: Jeff Dike <jdike@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-02-04 23:30:53 -07:00
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <asm/unistd.h>
|
2006-01-18 18:42:39 -07:00
|
|
|
#include "os.h"
|
2007-10-16 02:27:00 -06:00
|
|
|
#include "proc_mm.h"
|
2005-11-07 01:58:55 -07:00
|
|
|
#include "skas.h"
|
|
|
|
#include "skas_ptrace.h"
|
2007-10-16 02:27:00 -06:00
|
|
|
#include "sysdep/tls.h"
|
|
|
|
|
|
|
|
extern int modify_ldt(int func, void *ptr, unsigned long bytecount);
|
2005-11-07 01:58:55 -07:00
|
|
|
|
2008-07-23 22:28:49 -06:00
|
|
|
static long write_ldt_entry(struct mm_id *mm_idp, int func,
|
|
|
|
struct user_desc *desc, void **addr, int done)
|
2005-11-07 01:58:55 -07:00
|
|
|
{
|
|
|
|
long res;
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (proc_mm) {
|
|
|
|
/*
|
|
|
|
* This is a special handling for the case, that the mm to
|
2005-11-07 01:58:55 -07:00
|
|
|
* modify isn't current->active_mm.
|
|
|
|
* If this is called directly by modify_ldt,
|
|
|
|
* (current->active_mm->context.skas.u == mm_idp)
|
2007-10-16 02:26:58 -06:00
|
|
|
* will be true. So no call to __switch_mm(mm_idp) is done.
|
2005-11-07 01:58:55 -07:00
|
|
|
* If this is called in case of init_new_ldt or PTRACE_LDT,
|
|
|
|
* mm_idp won't belong to current->active_mm, but child->mm.
|
|
|
|
* So we need to switch child's mm into our userspace, then
|
|
|
|
* later switch back.
|
|
|
|
*
|
2006-02-24 14:03:55 -07:00
|
|
|
* Note: I'm unsure: should interrupts be disabled here?
|
2005-11-07 01:58:55 -07:00
|
|
|
*/
|
2007-10-16 02:27:00 -06:00
|
|
|
if (!current->active_mm || current->active_mm == &init_mm ||
|
2007-10-16 02:27:06 -06:00
|
|
|
mm_idp != ¤t->active_mm->context.id)
|
2007-10-16 02:26:58 -06:00
|
|
|
__switch_mm(mm_idp);
|
2005-11-07 01:58:55 -07:00
|
|
|
}
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (ptrace_ldt) {
|
2005-11-07 01:58:55 -07:00
|
|
|
struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
|
|
|
|
.func = func,
|
|
|
|
.ptr = desc,
|
|
|
|
.bytecount = sizeof(*desc)};
|
|
|
|
u32 cpu;
|
|
|
|
int pid;
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (!proc_mm)
|
2005-11-07 01:58:55 -07:00
|
|
|
pid = mm_idp->u.pid;
|
|
|
|
else {
|
|
|
|
cpu = get_cpu();
|
|
|
|
pid = userspace_pid[cpu];
|
|
|
|
}
|
|
|
|
|
2006-02-24 14:03:55 -07:00
|
|
|
res = os_ptrace_ldt(pid, 0, (unsigned long) &ldt_op);
|
2005-11-07 01:58:55 -07:00
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (proc_mm)
|
2005-11-07 01:58:55 -07:00
|
|
|
put_cpu();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
void *stub_addr;
|
|
|
|
res = syscall_stub_data(mm_idp, (unsigned long *)desc,
|
|
|
|
(sizeof(*desc) + sizeof(long) - 1) &
|
|
|
|
~(sizeof(long) - 1),
|
|
|
|
addr, &stub_addr);
|
2007-10-16 02:27:00 -06:00
|
|
|
if (!res) {
|
2005-11-07 01:58:55 -07:00
|
|
|
unsigned long args[] = { func,
|
|
|
|
(unsigned long)stub_addr,
|
|
|
|
sizeof(*desc),
|
|
|
|
0, 0, 0 };
|
|
|
|
res = run_syscall_stub(mm_idp, __NR_modify_ldt, args,
|
|
|
|
0, addr, done);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (proc_mm) {
|
|
|
|
/*
|
|
|
|
* This is the second part of special handling, that makes
|
2005-11-07 01:58:55 -07:00
|
|
|
* PTRACE_LDT possible to implement.
|
|
|
|
*/
|
2007-10-16 02:27:00 -06:00
|
|
|
if (current->active_mm && current->active_mm != &init_mm &&
|
2007-10-16 02:27:06 -06:00
|
|
|
mm_idp != ¤t->active_mm->context.id)
|
|
|
|
__switch_mm(¤t->active_mm->context.id);
|
2005-11-07 01:58:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long read_ldt_from_host(void __user * ptr, unsigned long bytecount)
|
|
|
|
{
|
|
|
|
int res, n;
|
|
|
|
struct ptrace_ldt ptrace_ldt = (struct ptrace_ldt) {
|
|
|
|
.func = 0,
|
|
|
|
.bytecount = bytecount,
|
2006-12-13 01:35:56 -07:00
|
|
|
.ptr = kmalloc(bytecount, GFP_KERNEL)};
|
2005-11-07 01:58:55 -07:00
|
|
|
u32 cpu;
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (ptrace_ldt.ptr == NULL)
|
2005-11-07 01:58:55 -07:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
/*
|
|
|
|
* This is called from sys_modify_ldt only, so userspace_pid gives
|
2005-11-07 01:58:55 -07:00
|
|
|
* us the right number
|
|
|
|
*/
|
|
|
|
|
|
|
|
cpu = get_cpu();
|
2006-02-24 14:03:55 -07:00
|
|
|
res = os_ptrace_ldt(userspace_pid[cpu], 0, (unsigned long) &ptrace_ldt);
|
2005-11-07 01:58:55 -07:00
|
|
|
put_cpu();
|
2007-10-16 02:27:00 -06:00
|
|
|
if (res < 0)
|
2005-11-07 01:58:55 -07:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
n = copy_to_user(ptr, ptrace_ldt.ptr, res);
|
2007-10-16 02:27:00 -06:00
|
|
|
if (n != 0)
|
2005-11-07 01:58:55 -07:00
|
|
|
res = -EFAULT;
|
|
|
|
|
|
|
|
out:
|
|
|
|
kfree(ptrace_ldt.ptr);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In skas mode, we hold our own ldt data in UML.
|
|
|
|
* Thus, the code implementing sys_modify_ldt_skas
|
|
|
|
* is very similar to (and mostly stolen from) sys_modify_ldt
|
|
|
|
* for arch/i386/kernel/ldt.c
|
|
|
|
* The routines copied and modified in part are:
|
|
|
|
* - read_ldt
|
|
|
|
* - read_default_ldt
|
|
|
|
* - write_ldt
|
|
|
|
* - sys_modify_ldt_skas
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int read_ldt(void __user * ptr, unsigned long bytecount)
|
|
|
|
{
|
|
|
|
int i, err = 0;
|
|
|
|
unsigned long size;
|
2007-10-16 02:27:06 -06:00
|
|
|
uml_ldt_t * ldt = ¤t->mm->context.ldt;
|
2005-11-07 01:58:55 -07:00
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (!ldt->entry_count)
|
2005-11-07 01:58:55 -07:00
|
|
|
goto out;
|
2007-10-16 02:27:00 -06:00
|
|
|
if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
|
2005-11-07 01:58:55 -07:00
|
|
|
bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
|
|
|
|
err = bytecount;
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (ptrace_ldt)
|
2005-11-07 01:58:55 -07:00
|
|
|
return read_ldt_from_host(ptr, bytecount);
|
|
|
|
|
2008-02-04 23:31:26 -07:00
|
|
|
mutex_lock(&ldt->lock);
|
2007-10-16 02:27:00 -06:00
|
|
|
if (ldt->entry_count <= LDT_DIRECT_ENTRIES) {
|
2005-11-07 01:58:55 -07:00
|
|
|
size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
|
2007-10-16 02:27:00 -06:00
|
|
|
if (size > bytecount)
|
2005-11-07 01:58:55 -07:00
|
|
|
size = bytecount;
|
2007-10-16 02:27:00 -06:00
|
|
|
if (copy_to_user(ptr, ldt->u.entries, size))
|
2005-11-07 01:58:55 -07:00
|
|
|
err = -EFAULT;
|
|
|
|
bytecount -= size;
|
|
|
|
ptr += size;
|
|
|
|
}
|
|
|
|
else {
|
2007-10-16 02:27:00 -06:00
|
|
|
for (i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
|
|
|
|
i++) {
|
2005-11-07 01:58:55 -07:00
|
|
|
size = PAGE_SIZE;
|
2007-10-16 02:27:00 -06:00
|
|
|
if (size > bytecount)
|
2005-11-07 01:58:55 -07:00
|
|
|
size = bytecount;
|
2007-10-16 02:27:00 -06:00
|
|
|
if (copy_to_user(ptr, ldt->u.pages[i], size)) {
|
2005-11-07 01:58:55 -07:00
|
|
|
err = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bytecount -= size;
|
|
|
|
ptr += size;
|
|
|
|
}
|
|
|
|
}
|
2008-02-04 23:31:26 -07:00
|
|
|
mutex_unlock(&ldt->lock);
|
2005-11-07 01:58:55 -07:00
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (bytecount == 0 || err == -EFAULT)
|
2005-11-07 01:58:55 -07:00
|
|
|
goto out;
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (clear_user(ptr, bytecount))
|
2005-11-07 01:58:55 -07:00
|
|
|
err = -EFAULT;
|
|
|
|
|
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_default_ldt(void __user * ptr, unsigned long bytecount)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (bytecount > 5*LDT_ENTRY_SIZE)
|
2005-11-07 01:58:55 -07:00
|
|
|
bytecount = 5*LDT_ENTRY_SIZE;
|
|
|
|
|
|
|
|
err = bytecount;
|
2007-10-16 02:27:00 -06:00
|
|
|
/*
|
|
|
|
* UML doesn't support lcall7 and lcall27.
|
2005-11-07 01:58:55 -07:00
|
|
|
* So, we don't really have a default ldt, but emulate
|
|
|
|
* an empty ldt of common host default ldt size.
|
|
|
|
*/
|
2007-10-16 02:27:00 -06:00
|
|
|
if (clear_user(ptr, bytecount))
|
2005-11-07 01:58:55 -07:00
|
|
|
err = -EFAULT;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
|
|
|
|
{
|
2007-10-16 02:27:06 -06:00
|
|
|
uml_ldt_t * ldt = ¤t->mm->context.ldt;
|
|
|
|
struct mm_id * mm_idp = ¤t->mm->context.id;
|
2005-11-07 01:58:55 -07:00
|
|
|
int i, err;
|
|
|
|
struct user_desc ldt_info;
|
|
|
|
struct ldt_entry entry0, *ldt_p;
|
|
|
|
void *addr = NULL;
|
|
|
|
|
|
|
|
err = -EINVAL;
|
2007-10-16 02:27:00 -06:00
|
|
|
if (bytecount != sizeof(ldt_info))
|
2005-11-07 01:58:55 -07:00
|
|
|
goto out;
|
|
|
|
err = -EFAULT;
|
2007-10-16 02:27:00 -06:00
|
|
|
if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
|
2005-11-07 01:58:55 -07:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = -EINVAL;
|
2007-10-16 02:27:00 -06:00
|
|
|
if (ldt_info.entry_number >= LDT_ENTRIES)
|
2005-11-07 01:58:55 -07:00
|
|
|
goto out;
|
2007-10-16 02:27:00 -06:00
|
|
|
if (ldt_info.contents == 3) {
|
2005-11-07 01:58:55 -07:00
|
|
|
if (func == 1)
|
|
|
|
goto out;
|
|
|
|
if (ldt_info.seg_not_present == 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (!ptrace_ldt)
|
2008-02-04 23:31:26 -07:00
|
|
|
mutex_lock(&ldt->lock);
|
2005-11-07 01:58:55 -07:00
|
|
|
|
|
|
|
err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
|
2007-10-16 02:27:00 -06:00
|
|
|
if (err)
|
2005-11-07 01:58:55 -07:00
|
|
|
goto out_unlock;
|
2007-10-16 02:27:00 -06:00
|
|
|
else if (ptrace_ldt) {
|
|
|
|
/* With PTRACE_LDT available, this is used as a flag only */
|
|
|
|
ldt->entry_count = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ldt_info.entry_number >= ldt->entry_count &&
|
|
|
|
ldt_info.entry_number >= LDT_DIRECT_ENTRIES) {
|
|
|
|
for (i=ldt->entry_count/LDT_ENTRIES_PER_PAGE;
|
|
|
|
i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number;
|
|
|
|
i++) {
|
|
|
|
if (i == 0)
|
2005-11-21 22:32:08 -07:00
|
|
|
memcpy(&entry0, ldt->u.entries,
|
|
|
|
sizeof(entry0));
|
|
|
|
ldt->u.pages[i] = (struct ldt_entry *)
|
|
|
|
__get_free_page(GFP_KERNEL|__GFP_ZERO);
|
2007-10-16 02:27:00 -06:00
|
|
|
if (!ldt->u.pages[i]) {
|
2005-11-07 01:58:55 -07:00
|
|
|
err = -ENOMEM;
|
|
|
|
/* Undo the change in host */
|
|
|
|
memset(&ldt_info, 0, sizeof(ldt_info));
|
|
|
|
write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1);
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
2007-10-16 02:27:00 -06:00
|
|
|
if (i == 0) {
|
2005-11-21 22:32:08 -07:00
|
|
|
memcpy(ldt->u.pages[0], &entry0,
|
|
|
|
sizeof(entry0));
|
|
|
|
memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
|
2005-11-07 01:58:55 -07:00
|
|
|
sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
|
|
|
|
}
|
|
|
|
ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
|
|
|
|
}
|
|
|
|
}
|
2007-10-16 02:27:00 -06:00
|
|
|
if (ldt->entry_count <= ldt_info.entry_number)
|
2005-11-07 01:58:55 -07:00
|
|
|
ldt->entry_count = ldt_info.entry_number + 1;
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (ldt->entry_count <= LDT_DIRECT_ENTRIES)
|
2005-11-21 22:32:08 -07:00
|
|
|
ldt_p = ldt->u.entries + ldt_info.entry_number;
|
2005-11-07 01:58:55 -07:00
|
|
|
else
|
2005-11-21 22:32:08 -07:00
|
|
|
ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
|
2005-11-07 01:58:55 -07:00
|
|
|
ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
|
|
|
|
(func == 1 || LDT_empty(&ldt_info))) {
|
2005-11-07 01:58:55 -07:00
|
|
|
ldt_p->a = 0;
|
|
|
|
ldt_p->b = 0;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
if (func == 1)
|
|
|
|
ldt_info.useable = 0;
|
|
|
|
ldt_p->a = LDT_entry_a(&ldt_info);
|
|
|
|
ldt_p->b = LDT_entry_b(&ldt_info);
|
|
|
|
}
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
out_unlock:
|
2008-02-04 23:31:26 -07:00
|
|
|
mutex_unlock(&ldt->lock);
|
2005-11-07 01:58:55 -07:00
|
|
|
out:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long do_modify_ldt_skas(int func, void __user *ptr,
|
|
|
|
unsigned long bytecount)
|
|
|
|
{
|
|
|
|
int ret = -ENOSYS;
|
|
|
|
|
|
|
|
switch (func) {
|
|
|
|
case 0:
|
|
|
|
ret = read_ldt(ptr, bytecount);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
case 0x11:
|
|
|
|
ret = write_ldt(ptr, bytecount, func);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ret = read_default_ldt(ptr, bytecount);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-02-28 21:13:06 -07:00
|
|
|
static DEFINE_SPINLOCK(host_ldt_lock);
|
|
|
|
static short dummy_list[9] = {0, -1};
|
|
|
|
static short * host_ldt_entries = NULL;
|
2005-11-07 01:58:55 -07:00
|
|
|
|
2007-02-28 21:13:06 -07:00
|
|
|
static void ldt_get_host_info(void)
|
2005-11-07 01:58:55 -07:00
|
|
|
{
|
|
|
|
long ret;
|
2007-03-29 02:20:32 -06:00
|
|
|
struct ldt_entry * ldt;
|
|
|
|
short *tmp;
|
2005-11-07 01:58:55 -07:00
|
|
|
int i, size, k, order;
|
|
|
|
|
2007-02-28 21:13:06 -07:00
|
|
|
spin_lock(&host_ldt_lock);
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (host_ldt_entries != NULL) {
|
2007-02-28 21:13:06 -07:00
|
|
|
spin_unlock(&host_ldt_lock);
|
|
|
|
return;
|
|
|
|
}
|
2005-11-07 01:58:55 -07:00
|
|
|
host_ldt_entries = dummy_list+1;
|
|
|
|
|
2007-02-28 21:13:06 -07:00
|
|
|
spin_unlock(&host_ldt_lock);
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
for (i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++)
|
|
|
|
;
|
2005-11-07 01:58:55 -07:00
|
|
|
|
|
|
|
ldt = (struct ldt_entry *)
|
|
|
|
__get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
|
2007-10-16 02:27:00 -06:00
|
|
|
if (ldt == NULL) {
|
|
|
|
printk(KERN_ERR "ldt_get_host_info: couldn't allocate buffer "
|
|
|
|
"for host ldt\n");
|
2005-11-07 01:58:55 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
|
2007-10-16 02:27:00 -06:00
|
|
|
if (ret < 0) {
|
|
|
|
printk(KERN_ERR "ldt_get_host_info: couldn't read host ldt\n");
|
2005-11-07 01:58:55 -07:00
|
|
|
goto out_free;
|
|
|
|
}
|
2007-10-16 02:27:00 -06:00
|
|
|
if (ret == 0) {
|
2005-11-07 01:58:55 -07:00
|
|
|
/* default_ldt is active, simply write an empty entry 0 */
|
|
|
|
host_ldt_entries = dummy_list;
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
for (i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++) {
|
|
|
|
if (ldt[i].a != 0 || ldt[i].b != 0)
|
2005-11-07 01:58:55 -07:00
|
|
|
size++;
|
|
|
|
}
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (size < ARRAY_SIZE(dummy_list))
|
2005-11-07 01:58:55 -07:00
|
|
|
host_ldt_entries = dummy_list;
|
|
|
|
else {
|
|
|
|
size = (size + 1) * sizeof(dummy_list[0]);
|
2007-02-28 21:13:06 -07:00
|
|
|
tmp = kmalloc(size, GFP_KERNEL);
|
2007-10-16 02:27:00 -06:00
|
|
|
if (tmp == NULL) {
|
|
|
|
printk(KERN_ERR "ldt_get_host_info: couldn't allocate "
|
|
|
|
"host ldt list\n");
|
2005-11-07 01:58:55 -07:00
|
|
|
goto out_free;
|
|
|
|
}
|
2007-02-28 21:13:06 -07:00
|
|
|
host_ldt_entries = tmp;
|
2005-11-07 01:58:55 -07:00
|
|
|
}
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
for (i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++) {
|
|
|
|
if (ldt[i].a != 0 || ldt[i].b != 0)
|
2005-11-07 01:58:55 -07:00
|
|
|
host_ldt_entries[k++] = i;
|
|
|
|
}
|
|
|
|
host_ldt_entries[k] = -1;
|
|
|
|
|
|
|
|
out_free:
|
|
|
|
free_pages((unsigned long)ldt, order);
|
|
|
|
}
|
|
|
|
|
2007-10-16 02:27:06 -06:00
|
|
|
long init_new_ldt(struct mm_context *new_mm, struct mm_context *from_mm)
|
2005-11-07 01:58:55 -07:00
|
|
|
{
|
|
|
|
struct user_desc desc;
|
|
|
|
short * num_p;
|
|
|
|
int i;
|
|
|
|
long page, err=0;
|
|
|
|
void *addr = NULL;
|
2006-01-18 18:42:39 -07:00
|
|
|
struct proc_mm_op copy;
|
2005-11-07 01:58:55 -07:00
|
|
|
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (!ptrace_ldt)
|
2008-02-04 23:31:26 -07:00
|
|
|
mutex_init(&new_mm->ldt.lock);
|
2005-11-07 01:58:55 -07:00
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (!from_mm) {
|
2006-01-18 18:42:39 -07:00
|
|
|
memset(&desc, 0, sizeof(desc));
|
2005-11-07 01:58:55 -07:00
|
|
|
/*
|
|
|
|
* We have to initialize a clean ldt.
|
|
|
|
*/
|
2007-10-16 02:27:00 -06:00
|
|
|
if (proc_mm) {
|
2005-11-07 01:58:55 -07:00
|
|
|
/*
|
|
|
|
* If the new mm was created using proc_mm, host's
|
|
|
|
* default-ldt currently is assigned, which normally
|
|
|
|
* contains the call-gates for lcall7 and lcall27.
|
|
|
|
* To remove these gates, we simply write an empty
|
|
|
|
* entry as number 0 to the host.
|
|
|
|
*/
|
2007-10-16 02:27:00 -06:00
|
|
|
err = write_ldt_entry(&new_mm->id, 1, &desc, &addr, 1);
|
2005-11-07 01:58:55 -07:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
/*
|
|
|
|
* Now we try to retrieve info about the ldt, we
|
|
|
|
* inherited from the host. All ldt-entries found
|
|
|
|
* will be reset in the following loop
|
|
|
|
*/
|
2007-02-28 21:13:06 -07:00
|
|
|
ldt_get_host_info();
|
2007-10-16 02:27:00 -06:00
|
|
|
for (num_p=host_ldt_entries; *num_p != -1; num_p++) {
|
2005-11-07 01:58:55 -07:00
|
|
|
desc.entry_number = *num_p;
|
|
|
|
err = write_ldt_entry(&new_mm->id, 1, &desc,
|
|
|
|
&addr, *(num_p + 1) == -1);
|
2007-10-16 02:27:00 -06:00
|
|
|
if (err)
|
2005-11-07 01:58:55 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_mm->ldt.entry_count = 0;
|
2006-01-18 18:42:39 -07:00
|
|
|
|
|
|
|
goto out;
|
2005-11-07 01:58:55 -07:00
|
|
|
}
|
2006-01-18 18:42:39 -07:00
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (proc_mm) {
|
|
|
|
/*
|
|
|
|
* We have a valid from_mm, so we now have to copy the LDT of
|
2006-01-18 18:42:39 -07:00
|
|
|
* from_mm to new_mm, because using proc_mm an new mm with
|
|
|
|
* an empty/default LDT was created in new_mm()
|
|
|
|
*/
|
|
|
|
copy = ((struct proc_mm_op) { .op = MM_COPY_SEGMENTS,
|
|
|
|
.u =
|
|
|
|
{ .copy_segments =
|
|
|
|
from_mm->id.u.mm_fd } } );
|
2007-05-06 15:51:43 -06:00
|
|
|
i = os_write_file(new_mm->id.u.mm_fd, ©, sizeof(copy));
|
2007-10-16 02:27:00 -06:00
|
|
|
if (i != sizeof(copy))
|
|
|
|
printk(KERN_ERR "new_mm : /proc/mm copy_segments "
|
|
|
|
"failed, err = %d\n", -i);
|
2006-01-18 18:42:39 -07:00
|
|
|
}
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (!ptrace_ldt) {
|
|
|
|
/*
|
|
|
|
* Our local LDT is used to supply the data for
|
2005-11-07 01:58:55 -07:00
|
|
|
* modify_ldt(READLDT), if PTRACE_LDT isn't available,
|
|
|
|
* i.e., we have to use the stub for modify_ldt, which
|
|
|
|
* can't handle the big read buffer of up to 64kB.
|
|
|
|
*/
|
2008-02-04 23:31:26 -07:00
|
|
|
mutex_lock(&from_mm->ldt.lock);
|
2007-10-16 02:27:00 -06:00
|
|
|
if (from_mm->ldt.entry_count <= LDT_DIRECT_ENTRIES)
|
2005-11-21 22:32:08 -07:00
|
|
|
memcpy(new_mm->ldt.u.entries, from_mm->ldt.u.entries,
|
|
|
|
sizeof(new_mm->ldt.u.entries));
|
2007-10-16 02:27:00 -06:00
|
|
|
else {
|
2005-11-07 01:58:55 -07:00
|
|
|
i = from_mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
|
2007-10-16 02:27:00 -06:00
|
|
|
while (i-->0) {
|
2005-11-07 01:58:55 -07:00
|
|
|
page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
|
2007-10-16 02:27:00 -06:00
|
|
|
if (!page) {
|
2005-11-07 01:58:55 -07:00
|
|
|
err = -ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
2005-11-21 22:32:08 -07:00
|
|
|
new_mm->ldt.u.pages[i] =
|
|
|
|
(struct ldt_entry *) page;
|
|
|
|
memcpy(new_mm->ldt.u.pages[i],
|
|
|
|
from_mm->ldt.u.pages[i], PAGE_SIZE);
|
2005-11-07 01:58:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
new_mm->ldt.entry_count = from_mm->ldt.entry_count;
|
2008-02-04 23:31:26 -07:00
|
|
|
mutex_unlock(&from_mm->ldt.lock);
|
2005-11-07 01:58:55 -07:00
|
|
|
}
|
|
|
|
|
2006-01-18 18:42:39 -07:00
|
|
|
out:
|
2005-11-07 01:58:55 -07:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-16 02:27:06 -06:00
|
|
|
void free_ldt(struct mm_context *mm)
|
2005-11-07 01:58:55 -07:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2007-10-16 02:27:00 -06:00
|
|
|
if (!ptrace_ldt && mm->ldt.entry_count > LDT_DIRECT_ENTRIES) {
|
2005-11-07 01:58:55 -07:00
|
|
|
i = mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
|
2007-10-16 02:27:00 -06:00
|
|
|
while (i-- > 0)
|
|
|
|
free_page((long) mm->ldt.u.pages[i]);
|
2005-11-07 01:58:55 -07:00
|
|
|
}
|
|
|
|
mm->ldt.entry_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
|
|
|
|
{
|
2007-10-16 02:26:56 -06:00
|
|
|
return do_modify_ldt_skas(func, ptr, bytecount);
|
2005-11-07 01:58:55 -07:00
|
|
|
}
|