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 *mm)
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);
52 memset(newldt+oldsize*LDT_ENTRY_SIZE, 0, (mincount-oldsize)*LDT_ENTRY_SIZE);
59 if (current->mm->cpu_vm_mask != (1<<smp_processor_id()))
60 smp_call_function(flush_ldt, 0, 1, 1);
65 if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
73 static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
75 int err = alloc_ldt(new, old->size, 0);
77 printk(KERN_WARNING "ldt allocation failed\n");
81 memcpy(new->ldt, old->ldt, old->size*LDT_ENTRY_SIZE);
86 * we do not have to muck with descriptors here, that is
87 * done in switch_mm() as needed.
89 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
91 struct mm_struct * old_mm;
94 init_MUTEX(&mm->context.sem);
97 if (old_mm && old_mm->context.size > 0) {
98 down(&old_mm->context.sem);
99 retval = copy_ldt(&mm->context, &old_mm->context);
100 up(&old_mm->context.sem);
106 * No need to lock the MM as we are the last user
107 * Do not touch the ldt register, we are already
108 * in the next thread.
110 void destroy_context(struct mm_struct *mm)
112 if (mm->context.size) {
113 if (mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE)
114 vfree(mm->context.ldt);
116 kfree(mm->context.ldt);
117 mm->context.size = 0;
121 static int read_ldt(void * ptr, unsigned long bytecount)
125 struct mm_struct * mm = current->mm;
127 if (!mm->context.size)
129 if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
130 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
132 down(&mm->context.sem);
133 size = mm->context.size*LDT_ENTRY_SIZE;
134 if (size > bytecount)
138 if (copy_to_user(ptr, mm->context.ldt, size))
140 up(&mm->context.sem);
143 if (size != bytecount) {
144 /* zero-fill the rest */
145 clear_user(ptr+size, bytecount-size);
150 static int read_default_ldt(void * ptr, unsigned long bytecount)
157 address = &default_ldt[0];
158 size = 5*sizeof(struct desc_struct);
159 if (size > bytecount)
163 if (copy_to_user(ptr, address, size))
169 static int write_ldt(void * ptr, unsigned long bytecount, int oldmode)
171 struct mm_struct * mm = current->mm;
172 __u32 entry_1, entry_2, *lp;
174 struct modify_ldt_ldt_s ldt_info;
177 if (bytecount != sizeof(ldt_info))
180 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
184 if (ldt_info.entry_number >= LDT_ENTRIES)
186 if (ldt_info.contents == 3) {
189 if (ldt_info.seg_not_present == 0)
193 down(&mm->context.sem);
194 if (ldt_info.entry_number >= mm->context.size) {
195 error = alloc_ldt(¤t->mm->context, ldt_info.entry_number+1, 1);
200 lp = (__u32 *) ((ldt_info.entry_number << 3) + (char *) mm->context.ldt);
202 /* Allow LDTs to be cleared by the user. */
203 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
205 (ldt_info.contents == 0 &&
206 ldt_info.read_exec_only == 1 &&
207 ldt_info.seg_32bit == 0 &&
208 ldt_info.limit_in_pages == 0 &&
209 ldt_info.seg_not_present == 1 &&
210 ldt_info.useable == 0 )) {
217 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
218 (ldt_info.limit & 0x0ffff);
219 entry_2 = (ldt_info.base_addr & 0xff000000) |
220 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
221 (ldt_info.limit & 0xf0000) |
222 ((ldt_info.read_exec_only ^ 1) << 9) |
223 (ldt_info.contents << 10) |
224 ((ldt_info.seg_not_present ^ 1) << 15) |
225 (ldt_info.seg_32bit << 22) |
226 (ldt_info.limit_in_pages << 23) |
229 entry_2 |= (ldt_info.useable << 20);
231 /* Install the new entry ... */
238 up(&mm->context.sem);
243 asmlinkage int sys_modify_ldt(int func, void *ptr, unsigned long bytecount)
249 ret = read_ldt(ptr, bytecount);
252 ret = write_ldt(ptr, bytecount, 1);
255 ret = read_default_ldt(ptr, bytecount);
258 ret = write_ldt(ptr, bytecount, 0);