2 * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
3 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
4 * Copyright (C) 2002 Andi Kleen
6 * This handles calls from both 32bit and 64bit mode.
9 #include <linux/errno.h>
10 #include <linux/gfp.h>
11 #include <linux/sched.h>
12 #include <linux/string.h>
14 #include <linux/smp.h>
15 #include <linux/syscalls.h>
16 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
18 #include <linux/uaccess.h>
19 #include <linux/kaiser.h>
23 #include <asm/mmu_context.h>
24 #include <asm/syscalls.h>
26 /* context.lock is held for us, so we don't need any locking. */
27 static void flush_ldt(void *current_mm)
31 if (current->active_mm != current_mm)
34 pc = ¤t->active_mm->context;
35 set_ldt(pc->ldt->entries, pc->ldt->size);
38 static void __free_ldt_struct(struct ldt_struct *ldt)
40 if (ldt->size * LDT_ENTRY_SIZE > PAGE_SIZE)
43 free_page((unsigned long)ldt->entries);
47 /* The caller must call finalize_ldt_struct on the result. LDT starts zeroed. */
48 static struct ldt_struct *alloc_ldt_struct(int size)
50 struct ldt_struct *new_ldt;
54 if (size > LDT_ENTRIES)
57 new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
61 BUILD_BUG_ON(LDT_ENTRY_SIZE != sizeof(struct desc_struct));
62 alloc_size = size * LDT_ENTRY_SIZE;
65 * Xen is very picky: it requires a page-aligned LDT that has no
66 * trailing nonzero bytes in any page that contains LDT descriptors.
67 * Keep it simple: zero the whole allocation and never allocate less
70 if (alloc_size > PAGE_SIZE)
71 new_ldt->entries = vzalloc(alloc_size);
73 new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
75 if (!new_ldt->entries) {
80 ret = kaiser_add_mapping((unsigned long)new_ldt->entries, alloc_size,
84 __free_ldt_struct(new_ldt);
90 /* After calling this, the LDT is immutable. */
91 static void finalize_ldt_struct(struct ldt_struct *ldt)
93 paravirt_alloc_ldt(ldt->entries, ldt->size);
96 /* context.lock is held */
97 static void install_ldt(struct mm_struct *current_mm,
98 struct ldt_struct *ldt)
100 /* Synchronizes with lockless_dereference in load_mm_ldt. */
101 smp_store_release(¤t_mm->context.ldt, ldt);
103 /* Activate the LDT for all CPUs using current_mm. */
104 on_each_cpu_mask(mm_cpumask(current_mm), flush_ldt, current_mm, true);
107 static void free_ldt_struct(struct ldt_struct *ldt)
112 kaiser_remove_mapping((unsigned long)ldt->entries,
113 ldt->size * LDT_ENTRY_SIZE);
114 paravirt_free_ldt(ldt->entries, ldt->size);
115 __free_ldt_struct(ldt);
119 * we do not have to muck with descriptors here, that is
120 * done in switch_mm() as needed.
122 int init_new_context_ldt(struct task_struct *tsk, struct mm_struct *mm)
124 struct ldt_struct *new_ldt;
125 struct mm_struct *old_mm;
128 mutex_init(&mm->context.lock);
129 old_mm = current->mm;
131 mm->context.ldt = NULL;
135 mutex_lock(&old_mm->context.lock);
136 if (!old_mm->context.ldt) {
137 mm->context.ldt = NULL;
141 new_ldt = alloc_ldt_struct(old_mm->context.ldt->size);
147 memcpy(new_ldt->entries, old_mm->context.ldt->entries,
148 new_ldt->size * LDT_ENTRY_SIZE);
149 finalize_ldt_struct(new_ldt);
151 mm->context.ldt = new_ldt;
154 mutex_unlock(&old_mm->context.lock);
159 * No need to lock the MM as we are the last user
161 * 64bit: Don't touch the LDT register - we're already in the next thread.
163 void destroy_context_ldt(struct mm_struct *mm)
165 free_ldt_struct(mm->context.ldt);
166 mm->context.ldt = NULL;
169 static int read_ldt(void __user *ptr, unsigned long bytecount)
173 struct mm_struct *mm = current->mm;
175 mutex_lock(&mm->context.lock);
177 if (!mm->context.ldt) {
182 if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
183 bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
185 size = mm->context.ldt->size * LDT_ENTRY_SIZE;
186 if (size > bytecount)
189 if (copy_to_user(ptr, mm->context.ldt->entries, size)) {
194 if (size != bytecount) {
195 /* Zero-fill the rest and pretend we read bytecount bytes. */
196 if (clear_user(ptr + size, bytecount - size)) {
204 mutex_unlock(&mm->context.lock);
208 static int read_default_ldt(void __user *ptr, unsigned long bytecount)
210 /* CHECKME: Can we use _one_ random number ? */
212 unsigned long size = 5 * sizeof(struct desc_struct);
214 unsigned long size = 128;
216 if (bytecount > size)
218 if (clear_user(ptr, bytecount))
223 static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
225 struct mm_struct *mm = current->mm;
226 struct desc_struct ldt;
228 struct user_desc ldt_info;
229 int oldsize, newsize;
230 struct ldt_struct *new_ldt, *old_ldt;
233 if (bytecount != sizeof(ldt_info))
236 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
240 if (ldt_info.entry_number >= LDT_ENTRIES)
242 if (ldt_info.contents == 3) {
245 if (ldt_info.seg_not_present == 0)
249 if ((oldmode && !ldt_info.base_addr && !ldt_info.limit) ||
250 LDT_empty(&ldt_info)) {
251 /* The user wants to clear the entry. */
252 memset(&ldt, 0, sizeof(ldt));
254 if (!IS_ENABLED(CONFIG_X86_16BIT) && !ldt_info.seg_32bit) {
259 fill_ldt(&ldt, &ldt_info);
264 mutex_lock(&mm->context.lock);
266 old_ldt = mm->context.ldt;
267 oldsize = old_ldt ? old_ldt->size : 0;
268 newsize = max((int)(ldt_info.entry_number + 1), oldsize);
271 new_ldt = alloc_ldt_struct(newsize);
276 memcpy(new_ldt->entries, old_ldt->entries, oldsize * LDT_ENTRY_SIZE);
277 new_ldt->entries[ldt_info.entry_number] = ldt;
278 finalize_ldt_struct(new_ldt);
280 install_ldt(mm, new_ldt);
281 free_ldt_struct(old_ldt);
285 mutex_unlock(&mm->context.lock);
290 SYSCALL_DEFINE3(modify_ldt, int , func , void __user * , ptr ,
291 unsigned long , bytecount)
297 ret = read_ldt(ptr, bytecount);
300 ret = write_ldt(ptr, bytecount, 1);
303 ret = read_default_ldt(ptr, bytecount);
306 ret = write_ldt(ptr, bytecount, 0);
310 * The SYSCALL_DEFINE() macros give us an 'unsigned long'
311 * return type, but tht ABI for sys_modify_ldt() expects
312 * 'int'. This cast gives us an int-sized value in %rax
313 * for the return code. The 'unsigned' is necessary so
314 * the compiler does not try to sign-extend the negative
315 * return codes into the high half of the register when
316 * taking the value from int->long.
318 return (unsigned int)ret;