Merge commit 'upstream-x86-entry' into WIP.x86/mm
[linux-2.6-microblaze.git] / arch / x86 / kernel / ldt.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
4  * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
5  * Copyright (C) 2002 Andi Kleen
6  *
7  * This handles calls from both 32bit and 64bit mode.
8  */
9
10 #include <linux/errno.h>
11 #include <linux/gfp.h>
12 #include <linux/sched.h>
13 #include <linux/string.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #include <linux/syscalls.h>
17 #include <linux/slab.h>
18 #include <linux/vmalloc.h>
19 #include <linux/uaccess.h>
20
21 #include <asm/ldt.h>
22 #include <asm/desc.h>
23 #include <asm/mmu_context.h>
24 #include <asm/syscalls.h>
25
26 static void refresh_ldt_segments(void)
27 {
28 #ifdef CONFIG_X86_64
29         unsigned short sel;
30
31         /*
32          * Make sure that the cached DS and ES descriptors match the updated
33          * LDT.
34          */
35         savesegment(ds, sel);
36         if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
37                 loadsegment(ds, sel);
38
39         savesegment(es, sel);
40         if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
41                 loadsegment(es, sel);
42 #endif
43 }
44
45 /* context.lock is held for us, so we don't need any locking. */
46 static void flush_ldt(void *__mm)
47 {
48         struct mm_struct *mm = __mm;
49         mm_context_t *pc;
50
51         if (this_cpu_read(cpu_tlbstate.loaded_mm) != mm)
52                 return;
53
54         pc = &mm->context;
55         set_ldt(pc->ldt->entries, pc->ldt->nr_entries);
56
57         refresh_ldt_segments();
58 }
59
60 /* The caller must call finalize_ldt_struct on the result. LDT starts zeroed. */
61 static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
62 {
63         struct ldt_struct *new_ldt;
64         unsigned int alloc_size;
65
66         if (num_entries > LDT_ENTRIES)
67                 return NULL;
68
69         new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
70         if (!new_ldt)
71                 return NULL;
72
73         BUILD_BUG_ON(LDT_ENTRY_SIZE != sizeof(struct desc_struct));
74         alloc_size = num_entries * LDT_ENTRY_SIZE;
75
76         /*
77          * Xen is very picky: it requires a page-aligned LDT that has no
78          * trailing nonzero bytes in any page that contains LDT descriptors.
79          * Keep it simple: zero the whole allocation and never allocate less
80          * than PAGE_SIZE.
81          */
82         if (alloc_size > PAGE_SIZE)
83                 new_ldt->entries = vzalloc(alloc_size);
84         else
85                 new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
86
87         if (!new_ldt->entries) {
88                 kfree(new_ldt);
89                 return NULL;
90         }
91
92         new_ldt->nr_entries = num_entries;
93         return new_ldt;
94 }
95
96 /* After calling this, the LDT is immutable. */
97 static void finalize_ldt_struct(struct ldt_struct *ldt)
98 {
99         paravirt_alloc_ldt(ldt->entries, ldt->nr_entries);
100 }
101
102 /* context.lock is held */
103 static void install_ldt(struct mm_struct *current_mm,
104                         struct ldt_struct *ldt)
105 {
106         /* Synchronizes with lockless_dereference in load_mm_ldt. */
107         smp_store_release(&current_mm->context.ldt, ldt);
108
109         /* Activate the LDT for all CPUs using current_mm. */
110         on_each_cpu_mask(mm_cpumask(current_mm), flush_ldt, current_mm, true);
111 }
112
113 static void free_ldt_struct(struct ldt_struct *ldt)
114 {
115         if (likely(!ldt))
116                 return;
117
118         paravirt_free_ldt(ldt->entries, ldt->nr_entries);
119         if (ldt->nr_entries * LDT_ENTRY_SIZE > PAGE_SIZE)
120                 vfree_atomic(ldt->entries);
121         else
122                 free_page((unsigned long)ldt->entries);
123         kfree(ldt);
124 }
125
126 /*
127  * we do not have to muck with descriptors here, that is
128  * done in switch_mm() as needed.
129  */
130 int init_new_context_ldt(struct task_struct *tsk, struct mm_struct *mm)
131 {
132         struct ldt_struct *new_ldt;
133         struct mm_struct *old_mm;
134         int retval = 0;
135
136         mutex_init(&mm->context.lock);
137         old_mm = current->mm;
138         if (!old_mm) {
139                 mm->context.ldt = NULL;
140                 return 0;
141         }
142
143         mutex_lock(&old_mm->context.lock);
144         if (!old_mm->context.ldt) {
145                 mm->context.ldt = NULL;
146                 goto out_unlock;
147         }
148
149         new_ldt = alloc_ldt_struct(old_mm->context.ldt->nr_entries);
150         if (!new_ldt) {
151                 retval = -ENOMEM;
152                 goto out_unlock;
153         }
154
155         memcpy(new_ldt->entries, old_mm->context.ldt->entries,
156                new_ldt->nr_entries * LDT_ENTRY_SIZE);
157         finalize_ldt_struct(new_ldt);
158
159         mm->context.ldt = new_ldt;
160
161 out_unlock:
162         mutex_unlock(&old_mm->context.lock);
163         return retval;
164 }
165
166 /*
167  * No need to lock the MM as we are the last user
168  *
169  * 64bit: Don't touch the LDT register - we're already in the next thread.
170  */
171 void destroy_context_ldt(struct mm_struct *mm)
172 {
173         free_ldt_struct(mm->context.ldt);
174         mm->context.ldt = NULL;
175 }
176
177 static int read_ldt(void __user *ptr, unsigned long bytecount)
178 {
179         struct mm_struct *mm = current->mm;
180         unsigned long entries_size;
181         int retval;
182
183         mutex_lock(&mm->context.lock);
184
185         if (!mm->context.ldt) {
186                 retval = 0;
187                 goto out_unlock;
188         }
189
190         if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
191                 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
192
193         entries_size = mm->context.ldt->nr_entries * LDT_ENTRY_SIZE;
194         if (entries_size > bytecount)
195                 entries_size = bytecount;
196
197         if (copy_to_user(ptr, mm->context.ldt->entries, entries_size)) {
198                 retval = -EFAULT;
199                 goto out_unlock;
200         }
201
202         if (entries_size != bytecount) {
203                 /* Zero-fill the rest and pretend we read bytecount bytes. */
204                 if (clear_user(ptr + entries_size, bytecount - entries_size)) {
205                         retval = -EFAULT;
206                         goto out_unlock;
207                 }
208         }
209         retval = bytecount;
210
211 out_unlock:
212         mutex_unlock(&mm->context.lock);
213         return retval;
214 }
215
216 static int read_default_ldt(void __user *ptr, unsigned long bytecount)
217 {
218         /* CHECKME: Can we use _one_ random number ? */
219 #ifdef CONFIG_X86_32
220         unsigned long size = 5 * sizeof(struct desc_struct);
221 #else
222         unsigned long size = 128;
223 #endif
224         if (bytecount > size)
225                 bytecount = size;
226         if (clear_user(ptr, bytecount))
227                 return -EFAULT;
228         return bytecount;
229 }
230
231 static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
232 {
233         struct mm_struct *mm = current->mm;
234         struct ldt_struct *new_ldt, *old_ldt;
235         unsigned int old_nr_entries, new_nr_entries;
236         struct user_desc ldt_info;
237         struct desc_struct ldt;
238         int error;
239
240         error = -EINVAL;
241         if (bytecount != sizeof(ldt_info))
242                 goto out;
243         error = -EFAULT;
244         if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
245                 goto out;
246
247         error = -EINVAL;
248         if (ldt_info.entry_number >= LDT_ENTRIES)
249                 goto out;
250         if (ldt_info.contents == 3) {
251                 if (oldmode)
252                         goto out;
253                 if (ldt_info.seg_not_present == 0)
254                         goto out;
255         }
256
257         if ((oldmode && !ldt_info.base_addr && !ldt_info.limit) ||
258             LDT_empty(&ldt_info)) {
259                 /* The user wants to clear the entry. */
260                 memset(&ldt, 0, sizeof(ldt));
261         } else {
262                 if (!IS_ENABLED(CONFIG_X86_16BIT) && !ldt_info.seg_32bit) {
263                         error = -EINVAL;
264                         goto out;
265                 }
266
267                 fill_ldt(&ldt, &ldt_info);
268                 if (oldmode)
269                         ldt.avl = 0;
270         }
271
272         mutex_lock(&mm->context.lock);
273
274         old_ldt       = mm->context.ldt;
275         old_nr_entries = old_ldt ? old_ldt->nr_entries : 0;
276         new_nr_entries = max(ldt_info.entry_number + 1, old_nr_entries);
277
278         error = -ENOMEM;
279         new_ldt = alloc_ldt_struct(new_nr_entries);
280         if (!new_ldt)
281                 goto out_unlock;
282
283         if (old_ldt)
284                 memcpy(new_ldt->entries, old_ldt->entries, old_nr_entries * LDT_ENTRY_SIZE);
285
286         new_ldt->entries[ldt_info.entry_number] = ldt;
287         finalize_ldt_struct(new_ldt);
288
289         install_ldt(mm, new_ldt);
290         free_ldt_struct(old_ldt);
291         error = 0;
292
293 out_unlock:
294         mutex_unlock(&mm->context.lock);
295 out:
296         return error;
297 }
298
299 SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
300                 unsigned long , bytecount)
301 {
302         int ret = -ENOSYS;
303
304         switch (func) {
305         case 0:
306                 ret = read_ldt(ptr, bytecount);
307                 break;
308         case 1:
309                 ret = write_ldt(ptr, bytecount, 1);
310                 break;
311         case 2:
312                 ret = read_default_ldt(ptr, bytecount);
313                 break;
314         case 0x11:
315                 ret = write_ldt(ptr, bytecount, 0);
316                 break;
317         }
318         /*
319          * The SYSCALL_DEFINE() macros give us an 'unsigned long'
320          * return type, but tht ABI for sys_modify_ldt() expects
321          * 'int'.  This cast gives us an int-sized value in %rax
322          * for the return code.  The 'unsigned' is necessary so
323          * the compiler does not try to sign-extend the negative
324          * return codes into the high half of the register when
325          * taking the value from int->long.
326          */
327         return (unsigned int)ret;
328 }