2 * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
3 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
4 * Copyright (C) 2002 Andi Kleen
6 * This handles calls from both 32bit and 64bit mode.
9 #include <linux/errno.h>
10 #include <linux/sched.h>
11 #include <linux/string.h>
13 #include <linux/smp.h>
14 #include <linux/vmalloc.h>
16 #include <asm/uaccess.h>
17 #include <asm/system.h>
20 #include <asm/mmu_context.h>
23 static void flush_ldt(void *current_mm
)
25 if (current
->active_mm
== current_mm
)
26 load_LDT(¤t
->active_mm
->context
);
30 static int alloc_ldt(mm_context_t
*pc
, int mincount
, int reload
)
32 void *oldldt
, *newldt
;
35 if (mincount
<= pc
->size
)
38 mincount
= (mincount
+ (PAGE_SIZE
/ LDT_ENTRY_SIZE
- 1)) &
39 (~(PAGE_SIZE
/ LDT_ENTRY_SIZE
- 1));
40 if (mincount
* LDT_ENTRY_SIZE
> PAGE_SIZE
)
41 newldt
= vmalloc(mincount
* LDT_ENTRY_SIZE
);
43 newldt
= (void *)__get_free_page(GFP_KERNEL
);
49 memcpy(newldt
, pc
->ldt
, oldsize
* LDT_ENTRY_SIZE
);
51 memset(newldt
+ oldsize
* LDT_ENTRY_SIZE
, 0,
52 (mincount
- oldsize
) * LDT_ENTRY_SIZE
);
55 /* CHECKME: Do we really need this ? */
67 if (!cpus_equal(current
->mm
->cpu_vm_mask
,
68 cpumask_of_cpu(smp_processor_id())))
69 smp_call_function(flush_ldt
, current
->mm
, 1);
76 if (oldsize
* LDT_ENTRY_SIZE
> PAGE_SIZE
)
79 put_page(virt_to_page(oldldt
));
84 static inline int copy_ldt(mm_context_t
*new, mm_context_t
*old
)
86 int err
= alloc_ldt(new, old
->size
, 0);
90 memcpy(new->ldt
, old
->ldt
, old
->size
* LDT_ENTRY_SIZE
);
95 * we do not have to muck with descriptors here, that is
96 * done in switch_mm() as needed.
98 int init_new_context(struct task_struct
*tsk
, struct mm_struct
*mm
)
100 struct mm_struct
*old_mm
;
103 mutex_init(&mm
->context
.lock
);
104 mm
->context
.size
= 0;
105 old_mm
= current
->mm
;
106 if (old_mm
&& old_mm
->context
.size
> 0) {
107 mutex_lock(&old_mm
->context
.lock
);
108 retval
= copy_ldt(&mm
->context
, &old_mm
->context
);
109 mutex_unlock(&old_mm
->context
.lock
);
115 * No need to lock the MM as we are the last user
117 * 64bit: Don't touch the LDT register - we're already in the next thread.
119 void destroy_context(struct mm_struct
*mm
)
121 if (mm
->context
.size
) {
123 /* CHECKME: Can this ever happen ? */
124 if (mm
== current
->active_mm
)
127 if (mm
->context
.size
* LDT_ENTRY_SIZE
> PAGE_SIZE
)
128 vfree(mm
->context
.ldt
);
130 put_page(virt_to_page(mm
->context
.ldt
));
131 mm
->context
.size
= 0;
135 static int read_ldt(void __user
*ptr
, unsigned long bytecount
)
139 struct mm_struct
*mm
= current
->mm
;
141 if (!mm
->context
.size
)
143 if (bytecount
> LDT_ENTRY_SIZE
* LDT_ENTRIES
)
144 bytecount
= LDT_ENTRY_SIZE
* LDT_ENTRIES
;
146 mutex_lock(&mm
->context
.lock
);
147 size
= mm
->context
.size
* LDT_ENTRY_SIZE
;
148 if (size
> bytecount
)
152 if (copy_to_user(ptr
, mm
->context
.ldt
, size
))
154 mutex_unlock(&mm
->context
.lock
);
157 if (size
!= bytecount
) {
158 /* zero-fill the rest */
159 if (clear_user(ptr
+ size
, bytecount
- size
) != 0) {
169 static int read_default_ldt(void __user
*ptr
, unsigned long bytecount
)
171 /* CHECKME: Can we use _one_ random number ? */
173 unsigned long size
= 5 * sizeof(struct desc_struct
);
175 unsigned long size
= 128;
177 if (bytecount
> size
)
179 if (clear_user(ptr
, bytecount
))
184 static int write_ldt(void __user
*ptr
, unsigned long bytecount
, int oldmode
)
186 struct mm_struct
*mm
= current
->mm
;
187 struct desc_struct ldt
;
189 struct user_desc ldt_info
;
192 if (bytecount
!= sizeof(ldt_info
))
195 if (copy_from_user(&ldt_info
, ptr
, sizeof(ldt_info
)))
199 if (ldt_info
.entry_number
>= LDT_ENTRIES
)
201 if (ldt_info
.contents
== 3) {
204 if (ldt_info
.seg_not_present
== 0)
208 mutex_lock(&mm
->context
.lock
);
209 if (ldt_info
.entry_number
>= mm
->context
.size
) {
210 error
= alloc_ldt(¤t
->mm
->context
,
211 ldt_info
.entry_number
+ 1, 1);
216 /* Allow LDTs to be cleared by the user. */
217 if (ldt_info
.base_addr
== 0 && ldt_info
.limit
== 0) {
218 if (oldmode
|| LDT_empty(&ldt_info
)) {
219 memset(&ldt
, 0, sizeof(ldt
));
224 fill_ldt(&ldt
, &ldt_info
);
228 /* Install the new entry ... */
230 write_ldt_entry(mm
->context
.ldt
, ldt_info
.entry_number
, &ldt
);
234 mutex_unlock(&mm
->context
.lock
);
239 asmlinkage
int sys_modify_ldt(int func
, void __user
*ptr
,
240 unsigned long bytecount
)
246 ret
= read_ldt(ptr
, bytecount
);
249 ret
= write_ldt(ptr
, bytecount
, 1);
252 ret
= read_default_ldt(ptr
, bytecount
);
255 ret
= write_ldt(ptr
, bytecount
, 0);