Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds |
3 | * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com> | |
4 | * Copyright (C) 2002 Andi Kleen | |
78aa1f66 | 5 | * |
1da177e4 LT |
6 | * This handles calls from both 32bit and 64bit mode. |
7 | */ | |
8 | ||
9 | #include <linux/errno.h> | |
10 | #include <linux/sched.h> | |
11 | #include <linux/string.h> | |
12 | #include <linux/mm.h> | |
13 | #include <linux/smp.h> | |
1da177e4 | 14 | #include <linux/vmalloc.h> |
1da177e4 LT |
15 | |
16 | #include <asm/uaccess.h> | |
17 | #include <asm/system.h> | |
18 | #include <asm/ldt.h> | |
19 | #include <asm/desc.h> | |
70f5088d | 20 | #include <asm/mmu_context.h> |
1da177e4 | 21 | |
78aa1f66 | 22 | #ifdef CONFIG_SMP |
b478458a | 23 | static void flush_ldt(void *current_mm) |
1da177e4 | 24 | { |
b478458a | 25 | if (current->active_mm == current_mm) |
78aa1f66 | 26 | load_LDT(¤t->active_mm->context); |
1da177e4 LT |
27 | } |
28 | #endif | |
29 | ||
70f5088d | 30 | static int alloc_ldt(mm_context_t *pc, int mincount, int reload) |
1da177e4 | 31 | { |
70f5088d TG |
32 | void *oldldt, *newldt; |
33 | int oldsize; | |
1da177e4 | 34 | |
70f5088d | 35 | if (mincount <= pc->size) |
1da177e4 LT |
36 | return 0; |
37 | oldsize = pc->size; | |
fa0c864d CG |
38 | mincount = (mincount + (PAGE_SIZE / LDT_ENTRY_SIZE - 1)) & |
39 | (~(PAGE_SIZE / LDT_ENTRY_SIZE - 1)); | |
78aa1f66 TG |
40 | if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE) |
41 | newldt = vmalloc(mincount * LDT_ENTRY_SIZE); | |
1da177e4 | 42 | else |
4dbf7af6 | 43 | newldt = (void *)__get_free_page(GFP_KERNEL); |
1da177e4 LT |
44 | |
45 | if (!newldt) | |
46 | return -ENOMEM; | |
47 | ||
48 | if (oldsize) | |
78aa1f66 | 49 | memcpy(newldt, pc->ldt, oldsize * LDT_ENTRY_SIZE); |
1da177e4 | 50 | oldldt = pc->ldt; |
78aa1f66 TG |
51 | memset(newldt + oldsize * LDT_ENTRY_SIZE, 0, |
52 | (mincount - oldsize) * LDT_ENTRY_SIZE); | |
77e463d1 TG |
53 | |
54 | #ifdef CONFIG_X86_64 | |
55 | /* CHECKME: Do we really need this ? */ | |
1da177e4 | 56 | wmb(); |
77e463d1 | 57 | #endif |
1da177e4 LT |
58 | pc->ldt = newldt; |
59 | wmb(); | |
60 | pc->size = mincount; | |
61 | wmb(); | |
70f5088d | 62 | |
1da177e4 LT |
63 | if (reload) { |
64 | #ifdef CONFIG_SMP | |
c42f4f4c | 65 | cpumask_of_cpu_ptr_declare(mask); |
1da177e4 LT |
66 | |
67 | preempt_disable(); | |
1da177e4 | 68 | load_LDT(pc); |
c42f4f4c MT |
69 | cpumask_of_cpu_ptr_next(mask, smp_processor_id()); |
70 | if (!cpus_equal(current->mm->cpu_vm_mask, *mask)) | |
1a781a77 | 71 | smp_call_function(flush_ldt, current->mm, 1); |
1da177e4 LT |
72 | preempt_enable(); |
73 | #else | |
74 | load_LDT(pc); | |
75 | #endif | |
76 | } | |
77 | if (oldsize) { | |
78aa1f66 | 78 | if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE) |
1da177e4 LT |
79 | vfree(oldldt); |
80 | else | |
4dbf7af6 | 81 | put_page(virt_to_page(oldldt)); |
1da177e4 LT |
82 | } |
83 | return 0; | |
84 | } | |
85 | ||
86 | static inline int copy_ldt(mm_context_t *new, mm_context_t *old) | |
87 | { | |
88 | int err = alloc_ldt(new, old->size, 0); | |
78aa1f66 | 89 | |
1da177e4 LT |
90 | if (err < 0) |
91 | return err; | |
78aa1f66 | 92 | memcpy(new->ldt, old->ldt, old->size * LDT_ENTRY_SIZE); |
1da177e4 LT |
93 | return 0; |
94 | } | |
95 | ||
96 | /* | |
97 | * we do not have to muck with descriptors here, that is | |
98 | * done in switch_mm() as needed. | |
99 | */ | |
100 | int init_new_context(struct task_struct *tsk, struct mm_struct *mm) | |
101 | { | |
78aa1f66 | 102 | struct mm_struct *old_mm; |
1da177e4 LT |
103 | int retval = 0; |
104 | ||
c7537ab2 | 105 | mutex_init(&mm->context.lock); |
1da177e4 LT |
106 | mm->context.size = 0; |
107 | old_mm = current->mm; | |
108 | if (old_mm && old_mm->context.size > 0) { | |
c7537ab2 | 109 | mutex_lock(&old_mm->context.lock); |
1da177e4 | 110 | retval = copy_ldt(&mm->context, &old_mm->context); |
c7537ab2 | 111 | mutex_unlock(&old_mm->context.lock); |
1da177e4 LT |
112 | } |
113 | return retval; | |
114 | } | |
115 | ||
116 | /* | |
77e463d1 TG |
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. | |
1da177e4 LT |
120 | */ |
121 | void destroy_context(struct mm_struct *mm) | |
122 | { | |
123 | if (mm->context.size) { | |
77e463d1 TG |
124 | #ifdef CONFIG_X86_32 |
125 | /* CHECKME: Can this ever happen ? */ | |
126 | if (mm == current->active_mm) | |
127 | clear_LDT(); | |
128 | #endif | |
70f5088d | 129 | if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE) |
1da177e4 LT |
130 | vfree(mm->context.ldt); |
131 | else | |
4dbf7af6 | 132 | put_page(virt_to_page(mm->context.ldt)); |
1da177e4 LT |
133 | mm->context.size = 0; |
134 | } | |
135 | } | |
136 | ||
78aa1f66 | 137 | static int read_ldt(void __user *ptr, unsigned long bytecount) |
1da177e4 LT |
138 | { |
139 | int err; | |
140 | unsigned long size; | |
78aa1f66 | 141 | struct mm_struct *mm = current->mm; |
1da177e4 LT |
142 | |
143 | if (!mm->context.size) | |
144 | return 0; | |
78aa1f66 TG |
145 | if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES) |
146 | bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES; | |
1da177e4 | 147 | |
c7537ab2 | 148 | mutex_lock(&mm->context.lock); |
78aa1f66 | 149 | size = mm->context.size * LDT_ENTRY_SIZE; |
1da177e4 LT |
150 | if (size > bytecount) |
151 | size = bytecount; | |
152 | ||
153 | err = 0; | |
154 | if (copy_to_user(ptr, mm->context.ldt, size)) | |
155 | err = -EFAULT; | |
c7537ab2 | 156 | mutex_unlock(&mm->context.lock); |
1da177e4 LT |
157 | if (err < 0) |
158 | goto error_return; | |
159 | if (size != bytecount) { | |
160 | /* zero-fill the rest */ | |
78aa1f66 | 161 | if (clear_user(ptr + size, bytecount - size) != 0) { |
1da177e4 LT |
162 | err = -EFAULT; |
163 | goto error_return; | |
164 | } | |
165 | } | |
166 | return bytecount; | |
167 | error_return: | |
168 | return err; | |
169 | } | |
170 | ||
78aa1f66 | 171 | static int read_default_ldt(void __user *ptr, unsigned long bytecount) |
1da177e4 | 172 | { |
77e463d1 TG |
173 | /* CHECKME: Can we use _one_ random number ? */ |
174 | #ifdef CONFIG_X86_32 | |
175 | unsigned long size = 5 * sizeof(struct desc_struct); | |
176 | #else | |
177 | unsigned long size = 128; | |
178 | #endif | |
179 | if (bytecount > size) | |
180 | bytecount = size; | |
1da177e4 LT |
181 | if (clear_user(ptr, bytecount)) |
182 | return -EFAULT; | |
78aa1f66 | 183 | return bytecount; |
1da177e4 LT |
184 | } |
185 | ||
78aa1f66 | 186 | static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode) |
1da177e4 | 187 | { |
70f5088d | 188 | struct mm_struct *mm = current->mm; |
5af72502 | 189 | struct desc_struct ldt; |
1da177e4 LT |
190 | int error; |
191 | struct user_desc ldt_info; | |
192 | ||
193 | error = -EINVAL; | |
1da177e4 LT |
194 | if (bytecount != sizeof(ldt_info)) |
195 | goto out; | |
78aa1f66 | 196 | error = -EFAULT; |
70f5088d | 197 | if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info))) |
1da177e4 LT |
198 | goto out; |
199 | ||
200 | error = -EINVAL; | |
201 | if (ldt_info.entry_number >= LDT_ENTRIES) | |
202 | goto out; | |
203 | if (ldt_info.contents == 3) { | |
204 | if (oldmode) | |
205 | goto out; | |
206 | if (ldt_info.seg_not_present == 0) | |
207 | goto out; | |
208 | } | |
209 | ||
c7537ab2 | 210 | mutex_lock(&mm->context.lock); |
70f5088d | 211 | if (ldt_info.entry_number >= mm->context.size) { |
78aa1f66 TG |
212 | error = alloc_ldt(¤t->mm->context, |
213 | ldt_info.entry_number + 1, 1); | |
1da177e4 LT |
214 | if (error < 0) |
215 | goto out_unlock; | |
216 | } | |
217 | ||
78aa1f66 TG |
218 | /* Allow LDTs to be cleared by the user. */ |
219 | if (ldt_info.base_addr == 0 && ldt_info.limit == 0) { | |
1da177e4 | 220 | if (oldmode || LDT_empty(&ldt_info)) { |
5af72502 | 221 | memset(&ldt, 0, sizeof(ldt)); |
1da177e4 LT |
222 | goto install; |
223 | } | |
224 | } | |
225 | ||
80fbb69a | 226 | fill_ldt(&ldt, &ldt_info); |
1da177e4 | 227 | if (oldmode) |
5af72502 | 228 | ldt.avl = 0; |
1da177e4 LT |
229 | |
230 | /* Install the new entry ... */ | |
231 | install: | |
75b8bb3e | 232 | write_ldt_entry(mm->context.ldt, ldt_info.entry_number, &ldt); |
1da177e4 LT |
233 | error = 0; |
234 | ||
235 | out_unlock: | |
c7537ab2 | 236 | mutex_unlock(&mm->context.lock); |
1da177e4 LT |
237 | out: |
238 | return error; | |
239 | } | |
240 | ||
78aa1f66 TG |
241 | asmlinkage int sys_modify_ldt(int func, void __user *ptr, |
242 | unsigned long bytecount) | |
1da177e4 LT |
243 | { |
244 | int ret = -ENOSYS; | |
245 | ||
246 | switch (func) { | |
247 | case 0: | |
248 | ret = read_ldt(ptr, bytecount); | |
249 | break; | |
250 | case 1: | |
251 | ret = write_ldt(ptr, bytecount, 1); | |
252 | break; | |
253 | case 2: | |
254 | ret = read_default_ldt(ptr, bytecount); | |
255 | break; | |
256 | case 0x11: | |
257 | ret = write_ldt(ptr, bytecount, 0); | |
258 | break; | |
259 | } | |
260 | return ret; | |
261 | } |