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>
29 #include <asm/mmu_context.h>
30 #include <asm/syscalls.h>
32 static void refresh_ldt_segments(void)
38 * Make sure that the cached DS and ES descriptors match the updated
42 if ((sel
& SEGMENT_TI_MASK
) == SEGMENT_LDT
)
46 if ((sel
& SEGMENT_TI_MASK
) == SEGMENT_LDT
)
51 /* context.lock is held by the task which issued the smp function call */
52 static void flush_ldt(void *__mm
)
54 struct mm_struct
*mm
= __mm
;
56 if (this_cpu_read(cpu_tlbstate
.loaded_mm
) != mm
)
61 refresh_ldt_segments();
64 /* The caller must call finalize_ldt_struct on the result. LDT starts zeroed. */
65 static struct ldt_struct
*alloc_ldt_struct(unsigned int num_entries
)
67 struct ldt_struct
*new_ldt
;
68 unsigned int alloc_size
;
70 if (num_entries
> LDT_ENTRIES
)
73 new_ldt
= kmalloc(sizeof(struct ldt_struct
), GFP_KERNEL
);
77 BUILD_BUG_ON(LDT_ENTRY_SIZE
!= sizeof(struct desc_struct
));
78 alloc_size
= num_entries
* LDT_ENTRY_SIZE
;
81 * Xen is very picky: it requires a page-aligned LDT that has no
82 * trailing nonzero bytes in any page that contains LDT descriptors.
83 * Keep it simple: zero the whole allocation and never allocate less
86 if (alloc_size
> PAGE_SIZE
)
87 new_ldt
->entries
= vzalloc(alloc_size
);
89 new_ldt
->entries
= (void *)get_zeroed_page(GFP_KERNEL
);
91 if (!new_ldt
->entries
) {
96 /* The new LDT isn't aliased for PTI yet. */
99 new_ldt
->nr_entries
= num_entries
;
104 * If PTI is enabled, this maps the LDT into the kernelmode and
105 * usermode tables for the given mm.
107 * There is no corresponding unmap function. Even if the LDT is freed, we
108 * leave the PTEs around until the slot is reused or the mm is destroyed.
109 * This is harmless: the LDT is always in ordinary memory, and no one will
110 * access the freed slot.
112 * If we wanted to unmap freed LDTs, we'd also need to do a flush to make
113 * it useful, and the flush would slow down modify_ldt().
116 map_ldt_struct(struct mm_struct
*mm
, struct ldt_struct
*ldt
, int slot
)
118 #ifdef CONFIG_PAGE_TABLE_ISOLATION
119 bool is_vmalloc
, had_top_level_entry
;
125 if (!static_cpu_has(X86_FEATURE_PTI
))
129 * Any given ldt_struct should have map_ldt_struct() called at most
132 WARN_ON(ldt
->slot
!= -1);
135 * Did we already have the top level entry allocated? We can't
136 * use pgd_none() for this because it doens't do anything on
137 * 4-level page table kernels.
139 pgd
= pgd_offset(mm
, LDT_BASE_ADDR
);
140 had_top_level_entry
= (pgd
->pgd
!= 0);
142 is_vmalloc
= is_vmalloc_addr(ldt
->entries
);
144 for (i
= 0; i
* PAGE_SIZE
< ldt
->nr_entries
* LDT_ENTRY_SIZE
; i
++) {
145 unsigned long offset
= i
<< PAGE_SHIFT
;
146 const void *src
= (char *)ldt
->entries
+ offset
;
151 va
= (unsigned long)ldt_slot_va(slot
) + offset
;
152 pfn
= is_vmalloc
? vmalloc_to_pfn(src
) :
153 page_to_pfn(virt_to_page(src
));
155 * Treat the PTI LDT range as a *userspace* range.
156 * get_locked_pte() will allocate all needed pagetables
157 * and account for them in this mm.
159 ptep
= get_locked_pte(mm
, va
, &ptl
);
163 * Map it RO so the easy to find address is not a primary
164 * target via some kernel interface which misses a
167 pte_prot
= __pgprot(__PAGE_KERNEL_RO
& ~_PAGE_GLOBAL
);
168 /* Filter out unsuppored __PAGE_KERNEL* bits: */
169 pgprot_val(pte_prot
) &= __supported_pte_mask
;
170 pte
= pfn_pte(pfn
, pte_prot
);
171 set_pte_at(mm
, va
, ptep
, pte
);
172 pte_unmap_unlock(ptep
, ptl
);
175 if (mm
->context
.ldt
) {
177 * We already had an LDT. The top-level entry should already
178 * have been allocated and synchronized with the usermode
181 WARN_ON(!had_top_level_entry
);
182 if (static_cpu_has(X86_FEATURE_PTI
))
183 WARN_ON(!kernel_to_user_pgdp(pgd
)->pgd
);
186 * This is the first time we're mapping an LDT for this process.
187 * Sync the pgd to the usermode tables.
189 WARN_ON(had_top_level_entry
);
190 if (static_cpu_has(X86_FEATURE_PTI
)) {
191 WARN_ON(kernel_to_user_pgdp(pgd
)->pgd
);
192 set_pgd(kernel_to_user_pgdp(pgd
), *pgd
);
196 va
= (unsigned long)ldt_slot_va(slot
);
197 flush_tlb_mm_range(mm
, va
, va
+ LDT_SLOT_STRIDE
, 0);
204 static void free_ldt_pgtables(struct mm_struct
*mm
)
206 #ifdef CONFIG_PAGE_TABLE_ISOLATION
207 struct mmu_gather tlb
;
208 unsigned long start
= LDT_BASE_ADDR
;
209 unsigned long end
= start
+ (1UL << PGDIR_SHIFT
);
211 if (!static_cpu_has(X86_FEATURE_PTI
))
214 tlb_gather_mmu(&tlb
, mm
, start
, end
);
215 free_pgd_range(&tlb
, start
, end
, start
, end
);
216 tlb_finish_mmu(&tlb
, start
, end
);
220 /* After calling this, the LDT is immutable. */
221 static void finalize_ldt_struct(struct ldt_struct
*ldt
)
223 paravirt_alloc_ldt(ldt
->entries
, ldt
->nr_entries
);
226 static void install_ldt(struct mm_struct
*mm
, struct ldt_struct
*ldt
)
228 mutex_lock(&mm
->context
.lock
);
230 /* Synchronizes with READ_ONCE in load_mm_ldt. */
231 smp_store_release(&mm
->context
.ldt
, ldt
);
233 /* Activate the LDT for all CPUs using currents mm. */
234 on_each_cpu_mask(mm_cpumask(mm
), flush_ldt
, mm
, true);
236 mutex_unlock(&mm
->context
.lock
);
239 static void free_ldt_struct(struct ldt_struct
*ldt
)
244 paravirt_free_ldt(ldt
->entries
, ldt
->nr_entries
);
245 if (ldt
->nr_entries
* LDT_ENTRY_SIZE
> PAGE_SIZE
)
246 vfree_atomic(ldt
->entries
);
248 free_page((unsigned long)ldt
->entries
);
253 * Called on fork from arch_dup_mmap(). Just copy the current LDT state,
254 * the new task is not running, so nothing can be installed.
256 int ldt_dup_context(struct mm_struct
*old_mm
, struct mm_struct
*mm
)
258 struct ldt_struct
*new_ldt
;
264 mutex_lock(&old_mm
->context
.lock
);
265 if (!old_mm
->context
.ldt
)
268 new_ldt
= alloc_ldt_struct(old_mm
->context
.ldt
->nr_entries
);
274 memcpy(new_ldt
->entries
, old_mm
->context
.ldt
->entries
,
275 new_ldt
->nr_entries
* LDT_ENTRY_SIZE
);
276 finalize_ldt_struct(new_ldt
);
278 retval
= map_ldt_struct(mm
, new_ldt
, 0);
280 free_ldt_pgtables(mm
);
281 free_ldt_struct(new_ldt
);
284 mm
->context
.ldt
= new_ldt
;
287 mutex_unlock(&old_mm
->context
.lock
);
292 * No need to lock the MM as we are the last user
294 * 64bit: Don't touch the LDT register - we're already in the next thread.
296 void destroy_context_ldt(struct mm_struct
*mm
)
298 free_ldt_struct(mm
->context
.ldt
);
299 mm
->context
.ldt
= NULL
;
302 void ldt_arch_exit_mmap(struct mm_struct
*mm
)
304 free_ldt_pgtables(mm
);
307 static int read_ldt(void __user
*ptr
, unsigned long bytecount
)
309 struct mm_struct
*mm
= current
->mm
;
310 unsigned long entries_size
;
313 down_read(&mm
->context
.ldt_usr_sem
);
315 if (!mm
->context
.ldt
) {
320 if (bytecount
> LDT_ENTRY_SIZE
* LDT_ENTRIES
)
321 bytecount
= LDT_ENTRY_SIZE
* LDT_ENTRIES
;
323 entries_size
= mm
->context
.ldt
->nr_entries
* LDT_ENTRY_SIZE
;
324 if (entries_size
> bytecount
)
325 entries_size
= bytecount
;
327 if (copy_to_user(ptr
, mm
->context
.ldt
->entries
, entries_size
)) {
332 if (entries_size
!= bytecount
) {
333 /* Zero-fill the rest and pretend we read bytecount bytes. */
334 if (clear_user(ptr
+ entries_size
, bytecount
- entries_size
)) {
342 up_read(&mm
->context
.ldt_usr_sem
);
346 static int read_default_ldt(void __user
*ptr
, unsigned long bytecount
)
348 /* CHECKME: Can we use _one_ random number ? */
350 unsigned long size
= 5 * sizeof(struct desc_struct
);
352 unsigned long size
= 128;
354 if (bytecount
> size
)
356 if (clear_user(ptr
, bytecount
))
361 static int write_ldt(void __user
*ptr
, unsigned long bytecount
, int oldmode
)
363 struct mm_struct
*mm
= current
->mm
;
364 struct ldt_struct
*new_ldt
, *old_ldt
;
365 unsigned int old_nr_entries
, new_nr_entries
;
366 struct user_desc ldt_info
;
367 struct desc_struct ldt
;
371 if (bytecount
!= sizeof(ldt_info
))
374 if (copy_from_user(&ldt_info
, ptr
, sizeof(ldt_info
)))
378 if (ldt_info
.entry_number
>= LDT_ENTRIES
)
380 if (ldt_info
.contents
== 3) {
383 if (ldt_info
.seg_not_present
== 0)
387 if ((oldmode
&& !ldt_info
.base_addr
&& !ldt_info
.limit
) ||
388 LDT_empty(&ldt_info
)) {
389 /* The user wants to clear the entry. */
390 memset(&ldt
, 0, sizeof(ldt
));
392 if (!IS_ENABLED(CONFIG_X86_16BIT
) && !ldt_info
.seg_32bit
) {
397 fill_ldt(&ldt
, &ldt_info
);
402 if (down_write_killable(&mm
->context
.ldt_usr_sem
))
405 old_ldt
= mm
->context
.ldt
;
406 old_nr_entries
= old_ldt
? old_ldt
->nr_entries
: 0;
407 new_nr_entries
= max(ldt_info
.entry_number
+ 1, old_nr_entries
);
410 new_ldt
= alloc_ldt_struct(new_nr_entries
);
415 memcpy(new_ldt
->entries
, old_ldt
->entries
, old_nr_entries
* LDT_ENTRY_SIZE
);
417 new_ldt
->entries
[ldt_info
.entry_number
] = ldt
;
418 finalize_ldt_struct(new_ldt
);
421 * If we are using PTI, map the new LDT into the userspace pagetables.
422 * If there is already an LDT, use the other slot so that other CPUs
423 * will continue to use the old LDT until install_ldt() switches
424 * them over to the new LDT.
426 error
= map_ldt_struct(mm
, new_ldt
, old_ldt
? !old_ldt
->slot
: 0);
429 * This only can fail for the first LDT setup. If an LDT is
430 * already installed then the PTE page is already
431 * populated. Mop up a half populated page table.
433 if (!WARN_ON_ONCE(old_ldt
))
434 free_ldt_pgtables(mm
);
435 free_ldt_struct(new_ldt
);
439 install_ldt(mm
, new_ldt
);
440 free_ldt_struct(old_ldt
);
444 up_write(&mm
->context
.ldt_usr_sem
);
449 SYSCALL_DEFINE3(modify_ldt
, int , func
, void __user
* , ptr
,
450 unsigned long , bytecount
)
456 ret
= read_ldt(ptr
, bytecount
);
459 ret
= write_ldt(ptr
, bytecount
, 1);
462 ret
= read_default_ldt(ptr
, bytecount
);
465 ret
= write_ldt(ptr
, bytecount
, 0);
469 * The SYSCALL_DEFINE() macros give us an 'unsigned long'
470 * return type, but tht ABI for sys_modify_ldt() expects
471 * 'int'. This cast gives us an int-sized value in %rax
472 * for the return code. The 'unsigned' is necessary so
473 * the compiler does not try to sign-extend the negative
474 * return codes into the high half of the register when
475 * taking the value from int->long.
477 return (unsigned int)ret
;