aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/kernel
diff options
context:
space:
mode:
authorThomas Gleixner <tglx@linutronix.de>2008-01-30 07:30:14 -0500
committerIngo Molnar <mingo@elte.hu>2008-01-30 07:30:14 -0500
commit77e463d1040d6310211ac5162729f5d4afc4dd8c (patch)
tree98b1a10bd33bcee97348bd5e8576b91d4f09d4ec /arch/x86/kernel
parent70f5088dd5e9fbd3a71b3a5b01395c676158194b (diff)
x86: merge arch/x86/kernel/ldt_32/64.c
Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'arch/x86/kernel')
-rw-r--r--arch/x86/kernel/Makefile_322
-rw-r--r--arch/x86/kernel/Makefile_642
-rw-r--r--arch/x86/kernel/ldt.c (renamed from arch/x86/kernel/ldt_64.c)25
-rw-r--r--arch/x86/kernel/ldt_32.c253
4 files changed, 22 insertions, 260 deletions
diff --git a/arch/x86/kernel/Makefile_32 b/arch/x86/kernel/Makefile_32
index 0cc1981d1e38..31ff982bc26b 100644
--- a/arch/x86/kernel/Makefile_32
+++ b/arch/x86/kernel/Makefile_32
@@ -6,7 +6,7 @@ extra-y := head_32.o init_task.o vmlinux.lds
6CPPFLAGS_vmlinux.lds += -Ui386 6CPPFLAGS_vmlinux.lds += -Ui386
7 7
8obj-y := process_32.o signal_32.o entry_32.o traps_32.o irq_32.o \ 8obj-y := process_32.o signal_32.o entry_32.o traps_32.o irq_32.o \
9 ptrace_32.o time_32.o ioport_32.o ldt_32.o setup_32.o i8259_32.o sys_i386_32.o \ 9 ptrace_32.o time_32.o ioport_32.o ldt.o setup_32.o i8259_32.o sys_i386_32.o \
10 pci-dma_32.o i386_ksyms_32.o i387_32.o bootflag.o e820_32.o\ 10 pci-dma_32.o i386_ksyms_32.o i387_32.o bootflag.o e820_32.o\
11 quirks.o i8237.o topology.o alternative.o i8253.o tsc_32.o io_delay.o 11 quirks.o i8237.o topology.o alternative.o i8253.o tsc_32.o io_delay.o
12 12
diff --git a/arch/x86/kernel/Makefile_64 b/arch/x86/kernel/Makefile_64
index 08a68f0d8fda..9cb3df27c413 100644
--- a/arch/x86/kernel/Makefile_64
+++ b/arch/x86/kernel/Makefile_64
@@ -7,7 +7,7 @@ CPPFLAGS_vmlinux.lds += -Ux86_64
7EXTRA_AFLAGS := -traditional 7EXTRA_AFLAGS := -traditional
8 8
9obj-y := process_64.o signal_64.o entry_64.o traps_64.o irq_64.o \ 9obj-y := process_64.o signal_64.o entry_64.o traps_64.o irq_64.o \
10 ptrace_64.o time_64.o ioport_64.o ldt_64.o setup_64.o i8259_64.o sys_x86_64.o \ 10 ptrace_64.o time_64.o ioport_64.o ldt.o setup_64.o i8259_64.o sys_x86_64.o \
11 x8664_ksyms_64.o i387_64.o syscall_64.o vsyscall_64.o \ 11 x8664_ksyms_64.o i387_64.o syscall_64.o vsyscall_64.o \
12 setup64.o bootflag.o e820_64.o reboot_64.o quirks.o i8237.o \ 12 setup64.o bootflag.o e820_64.o reboot_64.o quirks.o i8237.o \
13 pci-dma_64.o pci-nommu_64.o alternative.o hpet.o tsc_64.o bugs_64.o \ 13 pci-dma_64.o pci-nommu_64.o alternative.o hpet.o tsc_64.o bugs_64.o \
diff --git a/arch/x86/kernel/ldt_64.c b/arch/x86/kernel/ldt.c
index 95903938e7ad..a8cdca3615bf 100644
--- a/arch/x86/kernel/ldt_64.c
+++ b/arch/x86/kernel/ldt.c
@@ -50,7 +50,11 @@ static int alloc_ldt(mm_context_t *pc, int mincount, int reload)
50 oldldt = pc->ldt; 50 oldldt = pc->ldt;
51 memset(newldt + oldsize * LDT_ENTRY_SIZE, 0, 51 memset(newldt + oldsize * LDT_ENTRY_SIZE, 0,
52 (mincount - oldsize) * LDT_ENTRY_SIZE); 52 (mincount - oldsize) * LDT_ENTRY_SIZE);
53
54#ifdef CONFIG_X86_64
55 /* CHECKME: Do we really need this ? */
53 wmb(); 56 wmb();
57#endif
54 pc->ldt = newldt; 58 pc->ldt = newldt;
55 wmb(); 59 wmb();
56 pc->size = mincount; 60 pc->size = mincount;
@@ -110,11 +114,18 @@ int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
110} 114}
111 115
112/* 116/*
113 * Don't touch the LDT register - we're already in the next thread. 117 * No need to lock the MM as we are the last user
118 *
119 * 64bit: Don't touch the LDT register - we're already in the next thread.
114 */ 120 */
115void destroy_context(struct mm_struct *mm) 121void destroy_context(struct mm_struct *mm)
116{ 122{
117 if (mm->context.size) { 123 if (mm->context.size) {
124#ifdef CONFIG_X86_32
125 /* CHECKME: Can this ever happen ? */
126 if (mm == current->active_mm)
127 clear_LDT();
128#endif
118 if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE) 129 if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
119 vfree(mm->context.ldt); 130 vfree(mm->context.ldt);
120 else 131 else
@@ -159,10 +170,14 @@ error_return:
159 170
160static int read_default_ldt(void __user *ptr, unsigned long bytecount) 171static int read_default_ldt(void __user *ptr, unsigned long bytecount)
161{ 172{
162 /* Arbitrary number */ 173 /* CHECKME: Can we use _one_ random number ? */
163 /* x86-64 default LDT is all zeros */ 174#ifdef CONFIG_X86_32
164 if (bytecount > 128) 175 unsigned long size = 5 * sizeof(struct desc_struct);
165 bytecount = 128; 176#else
177 unsigned long size = 128;
178#endif
179 if (bytecount > size)
180 bytecount = size;
166 if (clear_user(ptr, bytecount)) 181 if (clear_user(ptr, bytecount))
167 return -EFAULT; 182 return -EFAULT;
168 return bytecount; 183 return bytecount;
diff --git a/arch/x86/kernel/ldt_32.c b/arch/x86/kernel/ldt_32.c
deleted file mode 100644
index bb15753abaf2..000000000000
--- a/arch/x86/kernel/ldt_32.c
+++ /dev/null
@@ -1,253 +0,0 @@
1/*
2 * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
3 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
4 */
5
6#include <linux/errno.h>
7#include <linux/sched.h>
8#include <linux/string.h>
9#include <linux/mm.h>
10#include <linux/smp.h>
11#include <linux/vmalloc.h>
12#include <linux/slab.h>
13
14#include <asm/uaccess.h>
15#include <asm/system.h>
16#include <asm/ldt.h>
17#include <asm/desc.h>
18#include <asm/mmu_context.h>
19
20#ifdef CONFIG_SMP
21static void flush_ldt(void *null)
22{
23 if (current->active_mm)
24 load_LDT(&current->active_mm->context);
25}
26#endif
27
28static int alloc_ldt(mm_context_t *pc, int mincount, int reload)
29{
30 void *oldldt, *newldt;
31 int oldsize;
32
33 if (mincount <= pc->size)
34 return 0;
35 oldsize = pc->size;
36 mincount = (mincount + 511) & (~511);
37 if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE)
38 newldt = vmalloc(mincount * LDT_ENTRY_SIZE);
39 else
40 newldt = kmalloc(mincount * LDT_ENTRY_SIZE, GFP_KERNEL);
41
42 if (!newldt)
43 return -ENOMEM;
44
45 if (oldsize)
46 memcpy(newldt, pc->ldt, oldsize * LDT_ENTRY_SIZE);
47 oldldt = pc->ldt;
48 memset(newldt + oldsize * LDT_ENTRY_SIZE, 0,
49 (mincount - oldsize) * LDT_ENTRY_SIZE);
50 pc->ldt = newldt;
51 wmb();
52 pc->size = mincount;
53 wmb();
54
55 if (reload) {
56#ifdef CONFIG_SMP
57 cpumask_t mask;
58
59 preempt_disable();
60 load_LDT(pc);
61 mask = cpumask_of_cpu(smp_processor_id());
62 if (!cpus_equal(current->mm->cpu_vm_mask, mask))
63 smp_call_function(flush_ldt, NULL, 1, 1);
64 preempt_enable();
65#else
66 load_LDT(pc);
67#endif
68 }
69 if (oldsize) {
70 if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
71 vfree(oldldt);
72 else
73 kfree(oldldt);
74 }
75 return 0;
76}
77
78static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
79{
80 int err = alloc_ldt(new, old->size, 0);
81
82 if (err < 0)
83 return err;
84 memcpy(new->ldt, old->ldt, old->size * LDT_ENTRY_SIZE);
85 return 0;
86}
87
88/*
89 * we do not have to muck with descriptors here, that is
90 * done in switch_mm() as needed.
91 */
92int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
93{
94 struct mm_struct *old_mm;
95 int retval = 0;
96
97 mutex_init(&mm->context.lock);
98 mm->context.size = 0;
99 old_mm = current->mm;
100 if (old_mm && old_mm->context.size > 0) {
101 mutex_lock(&old_mm->context.lock);
102 retval = copy_ldt(&mm->context, &old_mm->context);
103 mutex_unlock(&old_mm->context.lock);
104 }
105 return retval;
106}
107
108/*
109 * No need to lock the MM as we are the last user
110 */
111void destroy_context(struct mm_struct *mm)
112{
113 if (mm->context.size) {
114 if (mm == current->active_mm)
115 clear_LDT();
116 if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
117 vfree(mm->context.ldt);
118 else
119 kfree(mm->context.ldt);
120 mm->context.size = 0;
121 }
122}
123
124static int read_ldt(void __user *ptr, unsigned long bytecount)
125{
126 int err;
127 unsigned long size;
128 struct mm_struct *mm = current->mm;
129
130 if (!mm->context.size)
131 return 0;
132 if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
133 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
134
135 mutex_lock(&mm->context.lock);
136 size = mm->context.size * LDT_ENTRY_SIZE;
137 if (size > bytecount)
138 size = bytecount;
139
140 err = 0;
141 if (copy_to_user(ptr, mm->context.ldt, size))
142 err = -EFAULT;
143 mutex_unlock(&mm->context.lock);
144 if (err < 0)
145 goto error_return;
146 if (size != bytecount) {
147 /* zero-fill the rest */
148 if (clear_user(ptr + size, bytecount - size) != 0) {
149 err = -EFAULT;
150 goto error_return;
151 }
152 }
153 return bytecount;
154error_return:
155 return err;
156}
157
158static int read_default_ldt(void __user *ptr, unsigned long bytecount)
159{
160 int err;
161 unsigned long size;
162
163 err = 0;
164 size = 5 * sizeof(struct desc_struct);
165 if (size > bytecount)
166 size = bytecount;
167
168 err = size;
169 if (clear_user(ptr, size))
170 err = -EFAULT;
171
172 return err;
173}
174
175static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
176{
177 struct mm_struct *mm = current->mm;
178 __u32 entry_1, entry_2;
179 int error;
180 struct user_desc ldt_info;
181
182 error = -EINVAL;
183 if (bytecount != sizeof(ldt_info))
184 goto out;
185 error = -EFAULT;
186 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
187 goto out;
188
189 error = -EINVAL;
190 if (ldt_info.entry_number >= LDT_ENTRIES)
191 goto out;
192 if (ldt_info.contents == 3) {
193 if (oldmode)
194 goto out;
195 if (ldt_info.seg_not_present == 0)
196 goto out;
197 }
198
199 mutex_lock(&mm->context.lock);
200 if (ldt_info.entry_number >= mm->context.size) {
201 error = alloc_ldt(&current->mm->context,
202 ldt_info.entry_number + 1, 1);
203 if (error < 0)
204 goto out_unlock;
205 }
206
207 /* Allow LDTs to be cleared by the user. */
208 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
209 if (oldmode || LDT_empty(&ldt_info)) {
210 entry_1 = 0;
211 entry_2 = 0;
212 goto install;
213 }
214 }
215
216 entry_1 = LDT_entry_a(&ldt_info);
217 entry_2 = LDT_entry_b(&ldt_info);
218 if (oldmode)
219 entry_2 &= ~(1 << 20);
220
221 /* Install the new entry ... */
222install:
223 write_ldt_entry(mm->context.ldt, ldt_info.entry_number, entry_1,
224 entry_2);
225 error = 0;
226
227out_unlock:
228 mutex_unlock(&mm->context.lock);
229out:
230 return error;
231}
232
233asmlinkage int sys_modify_ldt(int func, void __user *ptr,
234 unsigned long bytecount)
235{
236 int ret = -ENOSYS;
237
238 switch (func) {
239 case 0:
240 ret = read_ldt(ptr, bytecount);
241 break;
242 case 1:
243 ret = write_ldt(ptr, bytecount, 1);
244 break;
245 case 2:
246 ret = read_default_ldt(ptr, bytecount);
247 break;
248 case 0x11:
249 ret = write_ldt(ptr, bytecount, 0);
250 break;
251 }
252 return ret;
253}