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