]>
git.proxmox.com Git - mirror_ubuntu-focal-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/slab.h>
17 #include <linux/vmalloc.h>
18 #include <linux/uaccess.h>
22 #include <asm/mmu_context.h>
23 #include <asm/syscalls.h>
25 static void refresh_ldt_segments(void)
31 * Make sure that the cached DS and ES descriptors match the updated
35 if ((sel
& SEGMENT_TI_MASK
) == SEGMENT_LDT
)
39 if ((sel
& SEGMENT_TI_MASK
) == SEGMENT_LDT
)
44 /* context.lock is held for us, so we don't need any locking. */
45 static void flush_ldt(void *__mm
)
47 struct mm_struct
*mm
= __mm
;
50 if (this_cpu_read(cpu_tlbstate
.loaded_mm
) != mm
)
54 set_ldt(pc
->ldt
->entries
, pc
->ldt
->nr_entries
);
56 refresh_ldt_segments();
59 /* The caller must call finalize_ldt_struct on the result. LDT starts zeroed. */
60 static struct ldt_struct
*alloc_ldt_struct(unsigned int num_entries
)
62 struct ldt_struct
*new_ldt
;
63 unsigned int alloc_size
;
65 if (num_entries
> LDT_ENTRIES
)
68 new_ldt
= kmalloc(sizeof(struct ldt_struct
), GFP_KERNEL
);
72 BUILD_BUG_ON(LDT_ENTRY_SIZE
!= sizeof(struct desc_struct
));
73 alloc_size
= num_entries
* LDT_ENTRY_SIZE
;
76 * Xen is very picky: it requires a page-aligned LDT that has no
77 * trailing nonzero bytes in any page that contains LDT descriptors.
78 * Keep it simple: zero the whole allocation and never allocate less
81 if (alloc_size
> PAGE_SIZE
)
82 new_ldt
->entries
= vzalloc(alloc_size
);
84 new_ldt
->entries
= (void *)get_zeroed_page(GFP_KERNEL
);
86 if (!new_ldt
->entries
) {
91 new_ldt
->nr_entries
= num_entries
;
95 /* After calling this, the LDT is immutable. */
96 static void finalize_ldt_struct(struct ldt_struct
*ldt
)
98 paravirt_alloc_ldt(ldt
->entries
, ldt
->nr_entries
);
101 /* context.lock is held */
102 static void install_ldt(struct mm_struct
*current_mm
,
103 struct ldt_struct
*ldt
)
105 /* Synchronizes with lockless_dereference in load_mm_ldt. */
106 smp_store_release(¤t_mm
->context
.ldt
, ldt
);
108 /* Activate the LDT for all CPUs using current_mm. */
109 on_each_cpu_mask(mm_cpumask(current_mm
), flush_ldt
, current_mm
, true);
112 static void free_ldt_struct(struct ldt_struct
*ldt
)
117 paravirt_free_ldt(ldt
->entries
, ldt
->nr_entries
);
118 if (ldt
->nr_entries
* LDT_ENTRY_SIZE
> PAGE_SIZE
)
119 vfree_atomic(ldt
->entries
);
121 free_page((unsigned long)ldt
->entries
);
126 * we do not have to muck with descriptors here, that is
127 * done in switch_mm() as needed.
129 int init_new_context_ldt(struct task_struct
*tsk
, struct mm_struct
*mm
)
131 struct ldt_struct
*new_ldt
;
132 struct mm_struct
*old_mm
;
135 mutex_init(&mm
->context
.lock
);
136 old_mm
= current
->mm
;
138 mm
->context
.ldt
= NULL
;
142 mutex_lock(&old_mm
->context
.lock
);
143 if (!old_mm
->context
.ldt
) {
144 mm
->context
.ldt
= NULL
;
148 new_ldt
= alloc_ldt_struct(old_mm
->context
.ldt
->nr_entries
);
154 memcpy(new_ldt
->entries
, old_mm
->context
.ldt
->entries
,
155 new_ldt
->nr_entries
* LDT_ENTRY_SIZE
);
156 finalize_ldt_struct(new_ldt
);
158 mm
->context
.ldt
= new_ldt
;
161 mutex_unlock(&old_mm
->context
.lock
);
166 * No need to lock the MM as we are the last user
168 * 64bit: Don't touch the LDT register - we're already in the next thread.
170 void destroy_context_ldt(struct mm_struct
*mm
)
172 free_ldt_struct(mm
->context
.ldt
);
173 mm
->context
.ldt
= NULL
;
176 static int read_ldt(void __user
*ptr
, unsigned long bytecount
)
178 struct mm_struct
*mm
= current
->mm
;
179 unsigned long entries_size
;
182 mutex_lock(&mm
->context
.lock
);
184 if (!mm
->context
.ldt
) {
189 if (bytecount
> LDT_ENTRY_SIZE
* LDT_ENTRIES
)
190 bytecount
= LDT_ENTRY_SIZE
* LDT_ENTRIES
;
192 entries_size
= mm
->context
.ldt
->nr_entries
* LDT_ENTRY_SIZE
;
193 if (entries_size
> bytecount
)
194 entries_size
= bytecount
;
196 if (copy_to_user(ptr
, mm
->context
.ldt
->entries
, entries_size
)) {
201 if (entries_size
!= bytecount
) {
202 /* Zero-fill the rest and pretend we read bytecount bytes. */
203 if (clear_user(ptr
+ entries_size
, bytecount
- entries_size
)) {
211 mutex_unlock(&mm
->context
.lock
);
215 static int read_default_ldt(void __user
*ptr
, unsigned long bytecount
)
217 /* CHECKME: Can we use _one_ random number ? */
219 unsigned long size
= 5 * sizeof(struct desc_struct
);
221 unsigned long size
= 128;
223 if (bytecount
> size
)
225 if (clear_user(ptr
, bytecount
))
230 static int write_ldt(void __user
*ptr
, unsigned long bytecount
, int oldmode
)
232 struct mm_struct
*mm
= current
->mm
;
233 struct ldt_struct
*new_ldt
, *old_ldt
;
234 unsigned int old_nr_entries
, new_nr_entries
;
235 struct user_desc ldt_info
;
236 struct desc_struct ldt
;
240 if (bytecount
!= sizeof(ldt_info
))
243 if (copy_from_user(&ldt_info
, ptr
, sizeof(ldt_info
)))
247 if (ldt_info
.entry_number
>= LDT_ENTRIES
)
249 if (ldt_info
.contents
== 3) {
252 if (ldt_info
.seg_not_present
== 0)
256 if ((oldmode
&& !ldt_info
.base_addr
&& !ldt_info
.limit
) ||
257 LDT_empty(&ldt_info
)) {
258 /* The user wants to clear the entry. */
259 memset(&ldt
, 0, sizeof(ldt
));
261 if (!IS_ENABLED(CONFIG_X86_16BIT
) && !ldt_info
.seg_32bit
) {
266 fill_ldt(&ldt
, &ldt_info
);
271 mutex_lock(&mm
->context
.lock
);
273 old_ldt
= mm
->context
.ldt
;
274 old_nr_entries
= old_ldt
? old_ldt
->nr_entries
: 0;
275 new_nr_entries
= max(ldt_info
.entry_number
+ 1, old_nr_entries
);
278 new_ldt
= alloc_ldt_struct(new_nr_entries
);
283 memcpy(new_ldt
->entries
, old_ldt
->entries
, old_nr_entries
* LDT_ENTRY_SIZE
);
285 new_ldt
->entries
[ldt_info
.entry_number
] = ldt
;
286 finalize_ldt_struct(new_ldt
);
288 install_ldt(mm
, new_ldt
);
289 free_ldt_struct(old_ldt
);
293 mutex_unlock(&mm
->context
.lock
);
298 asmlinkage
int sys_modify_ldt(int func
, void __user
*ptr
,
299 unsigned long bytecount
)
305 ret
= read_ldt(ptr
, bytecount
);
308 ret
= write_ldt(ptr
, bytecount
, 1);
311 ret
= read_default_ldt(ptr
, bytecount
);
314 ret
= write_ldt(ptr
, bytecount
, 0);