2 * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
3 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
6 #include <linux/errno.h>
7 #include <linux/sched.h>
8 #include <linux/string.h>
10 #include <linux/smp.h>
11 #include <linux/vmalloc.h>
12 #include <linux/slab.h>
14 #include <asm/uaccess.h>
15 #include <asm/system.h>
18 #include <asm/mmu_context.h>
20 #ifdef CONFIG_SMP /* avoids "defined but not used" warnig */
21 static void flush_ldt(void *null
)
23 if (current
->active_mm
)
24 load_LDT(¤t
->active_mm
->context
);
28 static int alloc_ldt(mm_context_t
*pc
, int mincount
, int reload
)
34 if (mincount
<= pc
->size
)
37 mincount
= (mincount
+511)&(~511);
38 if (mincount
*LDT_ENTRY_SIZE
> PAGE_SIZE
)
39 newldt
= vmalloc(mincount
*LDT_ENTRY_SIZE
);
41 newldt
= kmalloc(mincount
*LDT_ENTRY_SIZE
, GFP_KERNEL
);
47 memcpy(newldt
, pc
->ldt
, oldsize
*LDT_ENTRY_SIZE
);
49 memset(newldt
+oldsize
*LDT_ENTRY_SIZE
, 0, (mincount
-oldsize
)*LDT_ENTRY_SIZE
);
60 mask
= cpumask_of_cpu(smp_processor_id());
61 if (!cpus_equal(current
->mm
->cpu_vm_mask
, mask
))
62 smp_call_function(flush_ldt
, NULL
, 1, 1);
69 if (oldsize
*LDT_ENTRY_SIZE
> PAGE_SIZE
)
77 static inline int copy_ldt(mm_context_t
*new, mm_context_t
*old
)
79 int err
= alloc_ldt(new, old
->size
, 0);
82 memcpy(new->ldt
, old
->ldt
, old
->size
*LDT_ENTRY_SIZE
);
87 * we do not have to muck with descriptors here, that is
88 * done in switch_mm() as needed.
90 int init_new_context(struct task_struct
*tsk
, struct mm_struct
*mm
)
92 struct mm_struct
* old_mm
;
95 mutex_init(&mm
->context
.lock
);
98 if (old_mm
&& old_mm
->context
.size
> 0) {
99 mutex_lock(&old_mm
->context
.lock
);
100 retval
= copy_ldt(&mm
->context
, &old_mm
->context
);
101 mutex_unlock(&old_mm
->context
.lock
);
107 * No need to lock the MM as we are the last user
109 void destroy_context(struct mm_struct
*mm
)
111 if (mm
->context
.size
) {
112 if (mm
== current
->active_mm
)
114 if (mm
->context
.size
*LDT_ENTRY_SIZE
> PAGE_SIZE
)
115 vfree(mm
->context
.ldt
);
117 kfree(mm
->context
.ldt
);
118 mm
->context
.size
= 0;
122 static int read_ldt(void __user
* ptr
, unsigned long bytecount
)
126 struct mm_struct
* mm
= current
->mm
;
128 if (!mm
->context
.size
)
130 if (bytecount
> LDT_ENTRY_SIZE
*LDT_ENTRIES
)
131 bytecount
= LDT_ENTRY_SIZE
*LDT_ENTRIES
;
133 mutex_lock(&mm
->context
.lock
);
134 size
= mm
->context
.size
*LDT_ENTRY_SIZE
;
135 if (size
> bytecount
)
139 if (copy_to_user(ptr
, mm
->context
.ldt
, size
))
141 mutex_unlock(&mm
->context
.lock
);
144 if (size
!= bytecount
) {
145 /* zero-fill the rest */
146 if (clear_user(ptr
+size
, bytecount
-size
) != 0) {
156 static int read_default_ldt(void __user
* ptr
, unsigned long bytecount
)
162 size
= 5*sizeof(struct desc_struct
);
163 if (size
> bytecount
)
167 if (clear_user(ptr
, size
))
173 static int write_ldt(void __user
* ptr
, unsigned long bytecount
, int oldmode
)
175 struct mm_struct
* mm
= current
->mm
;
176 __u32 entry_1
, entry_2
;
178 struct user_desc ldt_info
;
181 if (bytecount
!= sizeof(ldt_info
))
184 if (copy_from_user(&ldt_info
, ptr
, sizeof(ldt_info
)))
188 if (ldt_info
.entry_number
>= LDT_ENTRIES
)
190 if (ldt_info
.contents
== 3) {
193 if (ldt_info
.seg_not_present
== 0)
197 mutex_lock(&mm
->context
.lock
);
198 if (ldt_info
.entry_number
>= mm
->context
.size
) {
199 error
= alloc_ldt(¤t
->mm
->context
, ldt_info
.entry_number
+1, 1);
204 /* Allow LDTs to be cleared by the user. */
205 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
206 if (oldmode
|| LDT_empty(&ldt_info
)) {
213 entry_1
= LDT_entry_a(&ldt_info
);
214 entry_2
= LDT_entry_b(&ldt_info
);
216 entry_2
&= ~(1 << 20);
218 /* Install the new entry ... */
220 write_ldt_entry(mm
->context
.ldt
, ldt_info
.entry_number
, entry_1
, entry_2
);
224 mutex_unlock(&mm
->context
.lock
);
229 asmlinkage
int sys_modify_ldt(int func
, void __user
*ptr
, unsigned long bytecount
)
235 ret
= read_ldt(ptr
, bytecount
);
238 ret
= write_ldt(ptr
, bytecount
, 1);
241 ret
= read_default_ldt(ptr
, bytecount
);
244 ret
= write_ldt(ptr
, bytecount
, 0);