4 * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
5 * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
8 #include <linux/errno.h>
9 #include <linux/sched.h>
10 #include <linux/string.h>
12 #include <linux/smp.h>
13 #include <linux/smp_lock.h>
14 #include <linux/vmalloc.h>
15 #include <linux/slab.h>
17 #include <asm/uaccess.h>
18 #include <asm/system.h>
22 #ifdef CONFIG_SMP /* avoids "defined but not used" warnig */
23 static void flush_ldt(void *null)
25 if (current->active_mm)
26 load_LDT(¤t->active_mm->context);
30 static int alloc_ldt(mm_context_t *pc, int mincount, int reload)
36 if (mincount <= pc->size)
39 mincount = (mincount+511)&(~511);
40 if (mincount*LDT_ENTRY_SIZE > PAGE_SIZE)
41 newldt = vmalloc(mincount*LDT_ENTRY_SIZE);
43 newldt = kmalloc(mincount*LDT_ENTRY_SIZE, GFP_KERNEL);
49 memcpy(newldt, pc->ldt, oldsize*LDT_ENTRY_SIZE);
51 memset(newldt+oldsize*LDT_ENTRY_SIZE, 0, (mincount-oldsize)*LDT_ENTRY_SIZE);
62 mask = cpumask_of_cpu(smp_processor_id());
63 if (!cpus_equal(current->mm->cpu_vm_mask, mask))
64 smp_call_function(flush_ldt, NULL, 1, 1);
71 if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
79 static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
81 int err = alloc_ldt(new, old->size, 0);
84 memcpy(new->ldt, old->ldt, old->size*LDT_ENTRY_SIZE);
89 * we do not have to muck with descriptors here, that is
90 * done in switch_mm() as needed.
92 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
94 struct mm_struct * old_mm;
97 init_MUTEX(&mm->context.sem);
100 if (old_mm && old_mm->context.size > 0) {
101 down(&old_mm->context.sem);
102 retval = copy_ldt(&mm->context, &old_mm->context);
103 up(&old_mm->context.sem);
109 * No need to lock the MM as we are the last user
111 void destroy_context(struct mm_struct *mm)
113 if (mm->context.size) {
114 if (mm == current->active_mm)
116 if (mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE)
117 vfree(mm->context.ldt);
119 kfree(mm->context.ldt);
120 mm->context.size = 0;
124 static int read_ldt(void __user * ptr, unsigned long bytecount)
128 struct mm_struct * mm = current->mm;
130 if (!mm->context.size)
132 if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
133 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
135 down(&mm->context.sem);
136 size = mm->context.size*LDT_ENTRY_SIZE;
137 if (size > bytecount)
141 if (copy_to_user(ptr, mm->context.ldt, size))
143 up(&mm->context.sem);
146 if (size != bytecount) {
147 /* zero-fill the rest */
148 if (clear_user(ptr+size, bytecount-size) != 0) {
158 static int read_default_ldt(void __user * ptr, unsigned long bytecount)
165 address = &default_ldt[0];
166 size = 5*sizeof(struct desc_struct);
167 if (size > bytecount)
171 if (copy_to_user(ptr, address, size))
177 static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode)
179 struct mm_struct * mm = current->mm;
180 __u32 entry_1, entry_2, *lp;
182 struct user_desc ldt_info;
185 if (bytecount != sizeof(ldt_info))
188 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
192 if (ldt_info.entry_number >= LDT_ENTRIES)
194 if (ldt_info.contents == 3) {
197 if (ldt_info.seg_not_present == 0)
201 down(&mm->context.sem);
202 if (ldt_info.entry_number >= mm->context.size) {
203 error = alloc_ldt(¤t->mm->context, ldt_info.entry_number+1, 1);
208 lp = (__u32 *) ((ldt_info.entry_number << 3) + (char *) mm->context.ldt);
210 /* Allow LDTs to be cleared by the user. */
211 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
212 if (oldmode || LDT_empty(&ldt_info)) {
219 entry_1 = LDT_entry_a(&ldt_info);
220 entry_2 = LDT_entry_b(&ldt_info);
222 entry_2 &= ~(1 << 20);
224 /* Install the new entry ... */
231 up(&mm->context.sem);
236 asmlinkage int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
242 ret = read_ldt(ptr, bytecount);
245 ret = write_ldt(ptr, bytecount, 1);
248 ret = read_default_ldt(ptr, bytecount);
251 ret = write_ldt(ptr, bytecount, 0);