2 * linux/arch/x86_64/kernel/ldt.c
4 * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
5 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
6 * Copyright (C) 2002 Andi Kleen
8 * This handles calls from both 32bit and 64bit mode.
11 #include <linux/errno.h>
12 #include <linux/sched.h>
13 #include <linux/string.h>
15 #include <linux/smp.h>
16 #include <linux/smp_lock.h>
17 #include <linux/vmalloc.h>
18 #include <linux/slab.h>
20 #include <asm/uaccess.h>
21 #include <asm/system.h>
24 #include <asm/proto.h>
26 #ifdef CONFIG_SMP /* avoids "defined but not used" warnig */
27 static void flush_ldt(void *null
)
29 if (current
->active_mm
)
30 load_LDT(¤t
->active_mm
->context
);
34 static int alloc_ldt(mm_context_t
*pc
, unsigned mincount
, int reload
)
40 if (mincount
<= (unsigned)pc
->size
)
43 mincount
= (mincount
+511)&(~511);
44 if (mincount
*LDT_ENTRY_SIZE
> PAGE_SIZE
)
45 newldt
= vmalloc(mincount
*LDT_ENTRY_SIZE
);
47 newldt
= kmalloc(mincount
*LDT_ENTRY_SIZE
, GFP_KERNEL
);
53 memcpy(newldt
, pc
->ldt
, oldsize
*LDT_ENTRY_SIZE
);
55 memset(newldt
+oldsize
*LDT_ENTRY_SIZE
, 0, (mincount
-oldsize
)*LDT_ENTRY_SIZE
);
66 mask
= cpumask_of_cpu(smp_processor_id());
68 if (!cpus_equal(current
->mm
->cpu_vm_mask
, mask
))
69 smp_call_function(flush_ldt
, NULL
, 1, 1);
76 if (oldsize
*LDT_ENTRY_SIZE
> PAGE_SIZE
)
84 static inline int copy_ldt(mm_context_t
*new, mm_context_t
*old
)
86 int err
= alloc_ldt(new, old
->size
, 0);
89 memcpy(new->ldt
, old
->ldt
, old
->size
*LDT_ENTRY_SIZE
);
94 * we do not have to muck with descriptors here, that is
95 * done in switch_mm() as needed.
97 int init_new_context(struct task_struct
*tsk
, struct mm_struct
*mm
)
99 struct mm_struct
* old_mm
;
102 init_MUTEX(&mm
->context
.sem
);
103 mm
->context
.size
= 0;
104 old_mm
= current
->mm
;
105 if (old_mm
&& old_mm
->context
.size
> 0) {
106 down(&old_mm
->context
.sem
);
107 retval
= copy_ldt(&mm
->context
, &old_mm
->context
);
108 up(&old_mm
->context
.sem
);
115 * Don't touch the LDT register - we're already in the next thread.
117 void destroy_context(struct mm_struct
*mm
)
119 if (mm
->context
.size
) {
120 if ((unsigned)mm
->context
.size
*LDT_ENTRY_SIZE
> PAGE_SIZE
)
121 vfree(mm
->context
.ldt
);
123 kfree(mm
->context
.ldt
);
124 mm
->context
.size
= 0;
128 static int read_ldt(void __user
* ptr
, unsigned long bytecount
)
132 struct mm_struct
* mm
= current
->mm
;
134 if (!mm
->context
.size
)
136 if (bytecount
> LDT_ENTRY_SIZE
*LDT_ENTRIES
)
137 bytecount
= LDT_ENTRY_SIZE
*LDT_ENTRIES
;
139 down(&mm
->context
.sem
);
140 size
= mm
->context
.size
*LDT_ENTRY_SIZE
;
141 if (size
> bytecount
)
145 if (copy_to_user(ptr
, mm
->context
.ldt
, size
))
147 up(&mm
->context
.sem
);
150 if (size
!= bytecount
) {
151 /* zero-fill the rest */
152 if (clear_user(ptr
+size
, bytecount
-size
) != 0) {
162 static int read_default_ldt(void __user
* ptr
, unsigned long bytecount
)
164 /* Arbitrary number */
165 /* x86-64 default LDT is all zeros */
168 if (clear_user(ptr
, bytecount
))
173 static int write_ldt(void __user
* ptr
, unsigned long bytecount
, int oldmode
)
175 struct task_struct
*me
= current
;
176 struct mm_struct
* mm
= me
->mm
;
177 __u32 entry_1
, entry_2
, *lp
;
179 struct user_desc ldt_info
;
183 if (bytecount
!= sizeof(ldt_info
))
186 if (copy_from_user(&ldt_info
, ptr
, bytecount
))
190 if (ldt_info
.entry_number
>= LDT_ENTRIES
)
192 if (ldt_info
.contents
== 3) {
195 if (ldt_info
.seg_not_present
== 0)
199 down(&mm
->context
.sem
);
200 if (ldt_info
.entry_number
>= (unsigned)mm
->context
.size
) {
201 error
= alloc_ldt(¤t
->mm
->context
, ldt_info
.entry_number
+1, 1);
206 lp
= (__u32
*) ((ldt_info
.entry_number
<< 3) + (char *) mm
->context
.ldt
);
208 /* Allow LDTs to be cleared by the user. */
209 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
210 if (oldmode
|| LDT_empty(&ldt_info
)) {
217 entry_1
= LDT_entry_a(&ldt_info
);
218 entry_2
= LDT_entry_b(&ldt_info
);
220 entry_2
&= ~(1 << 20);
222 /* Install the new entry ... */
229 up(&mm
->context
.sem
);
234 asmlinkage
int sys_modify_ldt(int func
, void __user
*ptr
, unsigned long bytecount
)
240 ret
= read_ldt(ptr
, bytecount
);
243 ret
= write_ldt(ptr
, bytecount
, 1);
246 ret
= read_default_ldt(ptr
, bytecount
);
249 ret
= write_ldt(ptr
, bytecount
, 0);