]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - arch/x86/kernel/ldt.c
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.
10 #include <linux/errno.h>
11 #include <linux/gfp.h>
12 #include <linux/sched.h>
13 #include <linux/string.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>
23 #include <asm/mmu_context.h>
24 #include <asm/syscalls.h>
26 static void refresh_ldt_segments(void)
32 * Make sure that the cached DS and ES descriptors match the updated
36 if ((sel
& SEGMENT_TI_MASK
) == SEGMENT_LDT
)
40 if ((sel
& SEGMENT_TI_MASK
) == SEGMENT_LDT
)
45 /* context.lock is held for us, so we don't need any locking. */
46 static void flush_ldt(void *__mm
)
48 struct mm_struct
*mm
= __mm
;
51 if (this_cpu_read(cpu_tlbstate
.loaded_mm
) != mm
)
55 set_ldt(pc
->ldt
->entries
, pc
->ldt
->nr_entries
);
57 refresh_ldt_segments();
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
)
63 struct ldt_struct
*new_ldt
;
64 unsigned int alloc_size
;
66 if (num_entries
> LDT_ENTRIES
)
69 new_ldt
= kmalloc(sizeof(struct ldt_struct
), GFP_KERNEL
);
73 BUILD_BUG_ON(LDT_ENTRY_SIZE
!= sizeof(struct desc_struct
));
74 alloc_size
= num_entries
* LDT_ENTRY_SIZE
;
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
82 if (alloc_size
> PAGE_SIZE
)
83 new_ldt
->entries
= vzalloc(alloc_size
);
85 new_ldt
->entries
= (void *)get_zeroed_page(GFP_KERNEL
);
87 if (!new_ldt
->entries
) {
92 new_ldt
->nr_entries
= num_entries
;
96 /* After calling this, the LDT is immutable. */
97 static void finalize_ldt_struct(struct ldt_struct
*ldt
)
99 paravirt_alloc_ldt(ldt
->entries
, ldt
->nr_entries
);
102 /* context.lock is held */
103 static void install_ldt(struct mm_struct
*current_mm
,
104 struct ldt_struct
*ldt
)
106 /* Synchronizes with READ_ONCE in load_mm_ldt. */
107 smp_store_release(¤t_mm
->context
.ldt
, ldt
);
109 /* Activate the LDT for all CPUs using current_mm. */
110 on_each_cpu_mask(mm_cpumask(current_mm
), flush_ldt
, current_mm
, true);
113 static void free_ldt_struct(struct ldt_struct
*ldt
)
118 paravirt_free_ldt(ldt
->entries
, ldt
->nr_entries
);
119 if (ldt
->nr_entries
* LDT_ENTRY_SIZE
> PAGE_SIZE
)
120 vfree_atomic(ldt
->entries
);
122 free_page((unsigned long)ldt
->entries
);
127 * we do not have to muck with descriptors here, that is
128 * done in switch_mm() as needed.
130 int init_new_context_ldt(struct task_struct
*tsk
, struct mm_struct
*mm
)
132 struct ldt_struct
*new_ldt
;
133 struct mm_struct
*old_mm
;
136 mutex_init(&mm
->context
.lock
);
137 old_mm
= current
->mm
;
139 mm
->context
.ldt
= NULL
;
143 mutex_lock(&old_mm
->context
.lock
);
144 if (!old_mm
->context
.ldt
) {
145 mm
->context
.ldt
= NULL
;
149 new_ldt
= alloc_ldt_struct(old_mm
->context
.ldt
->nr_entries
);
155 memcpy(new_ldt
->entries
, old_mm
->context
.ldt
->entries
,
156 new_ldt
->nr_entries
* LDT_ENTRY_SIZE
);
157 finalize_ldt_struct(new_ldt
);
159 mm
->context
.ldt
= new_ldt
;
162 mutex_unlock(&old_mm
->context
.lock
);
167 * No need to lock the MM as we are the last user
169 * 64bit: Don't touch the LDT register - we're already in the next thread.
171 void destroy_context_ldt(struct mm_struct
*mm
)
173 free_ldt_struct(mm
->context
.ldt
);
174 mm
->context
.ldt
= NULL
;
177 static int read_ldt(void __user
*ptr
, unsigned long bytecount
)
179 struct mm_struct
*mm
= current
->mm
;
180 unsigned long entries_size
;
183 mutex_lock(&mm
->context
.lock
);
185 if (!mm
->context
.ldt
) {
190 if (bytecount
> LDT_ENTRY_SIZE
* LDT_ENTRIES
)
191 bytecount
= LDT_ENTRY_SIZE
* LDT_ENTRIES
;
193 entries_size
= mm
->context
.ldt
->nr_entries
* LDT_ENTRY_SIZE
;
194 if (entries_size
> bytecount
)
195 entries_size
= bytecount
;
197 if (copy_to_user(ptr
, mm
->context
.ldt
->entries
, entries_size
)) {
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
)) {
212 mutex_unlock(&mm
->context
.lock
);
216 static int read_default_ldt(void __user
*ptr
, unsigned long bytecount
)
218 /* CHECKME: Can we use _one_ random number ? */
220 unsigned long size
= 5 * sizeof(struct desc_struct
);
222 unsigned long size
= 128;
224 if (bytecount
> size
)
226 if (clear_user(ptr
, bytecount
))
231 static int write_ldt(void __user
*ptr
, unsigned long bytecount
, int oldmode
)
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
;
241 if (bytecount
!= sizeof(ldt_info
))
244 if (copy_from_user(&ldt_info
, ptr
, sizeof(ldt_info
)))
248 if (ldt_info
.entry_number
>= LDT_ENTRIES
)
250 if (ldt_info
.contents
== 3) {
253 if (ldt_info
.seg_not_present
== 0)
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
));
262 if (!IS_ENABLED(CONFIG_X86_16BIT
) && !ldt_info
.seg_32bit
) {
267 fill_ldt(&ldt
, &ldt_info
);
272 mutex_lock(&mm
->context
.lock
);
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
);
279 new_ldt
= alloc_ldt_struct(new_nr_entries
);
284 memcpy(new_ldt
->entries
, old_ldt
->entries
, old_nr_entries
* LDT_ENTRY_SIZE
);
286 new_ldt
->entries
[ldt_info
.entry_number
] = ldt
;
287 finalize_ldt_struct(new_ldt
);
289 install_ldt(mm
, new_ldt
);
290 free_ldt_struct(old_ldt
);
294 mutex_unlock(&mm
->context
.lock
);
299 SYSCALL_DEFINE3(modify_ldt
, int , func
, void __user
* , ptr
,
300 unsigned long , bytecount
)
306 ret
= read_ldt(ptr
, bytecount
);
309 ret
= write_ldt(ptr
, bytecount
, 1);
312 ret
= read_default_ldt(ptr
, bytecount
);
315 ret
= write_ldt(ptr
, bytecount
, 0);
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.
327 return (unsigned int)ret
;