1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
4 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
5 * Copyright (C) 2002 Andi Kleen
7 * This handles calls from both 32bit and 64bit mode.
15 #include <linux/errno.h>
16 #include <linux/gfp.h>
17 #include <linux/sched.h>
18 #include <linux/string.h>
20 #include <linux/smp.h>
21 #include <linux/syscalls.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
24 #include <linux/uaccess.h>
28 #include <asm/mmu_context.h>
29 #include <asm/syscalls.h>
31 static void refresh_ldt_segments(void)
37 * Make sure that the cached DS and ES descriptors match the updated
41 if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
45 if ((sel & SEGMENT_TI_MASK) == SEGMENT_LDT)
50 /* context.lock is held by the task which issued the smp function call */
51 static void flush_ldt(void *__mm)
53 struct mm_struct *mm = __mm;
56 if (this_cpu_read(cpu_tlbstate.loaded_mm) != mm)
60 set_ldt(pc->ldt->entries, pc->ldt->nr_entries);
62 refresh_ldt_segments();
65 /* The caller must call finalize_ldt_struct on the result. LDT starts zeroed. */
66 static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
68 struct ldt_struct *new_ldt;
69 unsigned int alloc_size;
71 if (num_entries > LDT_ENTRIES)
74 new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
78 BUILD_BUG_ON(LDT_ENTRY_SIZE != sizeof(struct desc_struct));
79 alloc_size = num_entries * LDT_ENTRY_SIZE;
82 * Xen is very picky: it requires a page-aligned LDT that has no
83 * trailing nonzero bytes in any page that contains LDT descriptors.
84 * Keep it simple: zero the whole allocation and never allocate less
87 if (alloc_size > PAGE_SIZE)
88 new_ldt->entries = vzalloc(alloc_size);
90 new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
92 if (!new_ldt->entries) {
97 new_ldt->nr_entries = num_entries;
101 /* After calling this, the LDT is immutable. */
102 static void finalize_ldt_struct(struct ldt_struct *ldt)
104 paravirt_alloc_ldt(ldt->entries, ldt->nr_entries);
107 static void install_ldt(struct mm_struct *mm, struct ldt_struct *ldt)
109 mutex_lock(&mm->context.lock);
111 /* Synchronizes with READ_ONCE in load_mm_ldt. */
112 smp_store_release(&mm->context.ldt, ldt);
114 /* Activate the LDT for all CPUs using currents mm. */
115 on_each_cpu_mask(mm_cpumask(mm), flush_ldt, mm, true);
117 mutex_unlock(&mm->context.lock);
120 static void free_ldt_struct(struct ldt_struct *ldt)
125 paravirt_free_ldt(ldt->entries, ldt->nr_entries);
126 if (ldt->nr_entries * LDT_ENTRY_SIZE > PAGE_SIZE)
127 vfree_atomic(ldt->entries);
129 free_page((unsigned long)ldt->entries);
134 * we do not have to muck with descriptors here, that is
135 * done in switch_mm() as needed.
137 int init_new_context_ldt(struct task_struct *tsk, struct mm_struct *mm)
139 struct ldt_struct *new_ldt;
140 struct mm_struct *old_mm;
143 init_rwsem(&mm->context.ldt_usr_sem);
145 old_mm = current->mm;
147 mm->context.ldt = NULL;
151 mutex_lock(&old_mm->context.lock);
152 if (!old_mm->context.ldt) {
153 mm->context.ldt = NULL;
157 new_ldt = alloc_ldt_struct(old_mm->context.ldt->nr_entries);
163 memcpy(new_ldt->entries, old_mm->context.ldt->entries,
164 new_ldt->nr_entries * LDT_ENTRY_SIZE);
165 finalize_ldt_struct(new_ldt);
167 mm->context.ldt = new_ldt;
170 mutex_unlock(&old_mm->context.lock);
175 * No need to lock the MM as we are the last user
177 * 64bit: Don't touch the LDT register - we're already in the next thread.
179 void destroy_context_ldt(struct mm_struct *mm)
181 free_ldt_struct(mm->context.ldt);
182 mm->context.ldt = NULL;
185 static int read_ldt(void __user *ptr, unsigned long bytecount)
187 struct mm_struct *mm = current->mm;
188 unsigned long entries_size;
191 down_read(&mm->context.ldt_usr_sem);
193 if (!mm->context.ldt) {
198 if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
199 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
201 entries_size = mm->context.ldt->nr_entries * LDT_ENTRY_SIZE;
202 if (entries_size > bytecount)
203 entries_size = bytecount;
205 if (copy_to_user(ptr, mm->context.ldt->entries, entries_size)) {
210 if (entries_size != bytecount) {
211 /* Zero-fill the rest and pretend we read bytecount bytes. */
212 if (clear_user(ptr + entries_size, bytecount - entries_size)) {
220 up_read(&mm->context.ldt_usr_sem);
224 static int read_default_ldt(void __user *ptr, unsigned long bytecount)
226 /* CHECKME: Can we use _one_ random number ? */
228 unsigned long size = 5 * sizeof(struct desc_struct);
230 unsigned long size = 128;
232 if (bytecount > size)
234 if (clear_user(ptr, bytecount))
239 static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
241 struct mm_struct *mm = current->mm;
242 struct ldt_struct *new_ldt, *old_ldt;
243 unsigned int old_nr_entries, new_nr_entries;
244 struct user_desc ldt_info;
245 struct desc_struct ldt;
249 if (bytecount != sizeof(ldt_info))
252 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
256 if (ldt_info.entry_number >= LDT_ENTRIES)
258 if (ldt_info.contents == 3) {
261 if (ldt_info.seg_not_present == 0)
265 if ((oldmode && !ldt_info.base_addr && !ldt_info.limit) ||
266 LDT_empty(&ldt_info)) {
267 /* The user wants to clear the entry. */
268 memset(&ldt, 0, sizeof(ldt));
270 if (!IS_ENABLED(CONFIG_X86_16BIT) && !ldt_info.seg_32bit) {
275 fill_ldt(&ldt, &ldt_info);
280 if (down_write_killable(&mm->context.ldt_usr_sem))
283 old_ldt = mm->context.ldt;
284 old_nr_entries = old_ldt ? old_ldt->nr_entries : 0;
285 new_nr_entries = max(ldt_info.entry_number + 1, old_nr_entries);
288 new_ldt = alloc_ldt_struct(new_nr_entries);
293 memcpy(new_ldt->entries, old_ldt->entries, old_nr_entries * LDT_ENTRY_SIZE);
295 new_ldt->entries[ldt_info.entry_number] = ldt;
296 finalize_ldt_struct(new_ldt);
298 install_ldt(mm, new_ldt);
299 free_ldt_struct(old_ldt);
303 up_write(&mm->context.ldt_usr_sem);
308 SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
309 unsigned long , bytecount)
315 ret = read_ldt(ptr, bytecount);
318 ret = write_ldt(ptr, bytecount, 1);
321 ret = read_default_ldt(ptr, bytecount);
324 ret = write_ldt(ptr, bytecount, 0);
328 * The SYSCALL_DEFINE() macros give us an 'unsigned long'
329 * return type, but tht ABI for sys_modify_ldt() expects
330 * 'int'. This cast gives us an int-sized value in %rax
331 * for the return code. The 'unsigned' is necessary so
332 * the compiler does not try to sign-extend the negative
333 * return codes into the high half of the register when
334 * taking the value from int->long.
336 return (unsigned int)ret;