2 * linux/arch/i386/kernel/ldt.c
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>
21 #include <asm/mmu_context.h>
23 #ifdef CONFIG_SMP /* avoids "defined but not used" warnig */
24 static void flush_ldt(void *null)
26 if (current->active_mm)
27 load_LDT(¤t->active_mm->context);
31 static int alloc_ldt(mm_context_t *pc, int mincount, int reload)
37 if (mincount <= pc->size)
40 mincount = (mincount+511)&(~511);
41 if (mincount*LDT_ENTRY_SIZE > PAGE_SIZE)
42 newldt = vmalloc(mincount*LDT_ENTRY_SIZE);
44 newldt = kmalloc(mincount*LDT_ENTRY_SIZE, GFP_KERNEL);
50 memcpy(newldt, pc->ldt, oldsize*LDT_ENTRY_SIZE);
52 memset(newldt+oldsize*LDT_ENTRY_SIZE, 0, (mincount-oldsize)*LDT_ENTRY_SIZE);
65 (pc->size * LDT_ENTRY_SIZE) / PAGE_SIZE,
66 XENFEAT_writable_descriptor_tables);
69 mask = cpumask_of_cpu(smp_processor_id());
70 if (!cpus_equal(current->mm->cpu_vm_mask, mask))
71 smp_call_function(flush_ldt, NULL, 1, 1);
78 (oldsize * LDT_ENTRY_SIZE) / PAGE_SIZE,
79 XENFEAT_writable_descriptor_tables);
80 if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
88 static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
90 int err = alloc_ldt(new, old->size, 0);
93 memcpy(new->ldt, old->ldt, old->size*LDT_ENTRY_SIZE);
96 (new->size * LDT_ENTRY_SIZE) / PAGE_SIZE,
97 XENFEAT_writable_descriptor_tables);
102 * we do not have to muck with descriptors here, that is
103 * done in switch_mm() as needed.
105 int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
107 struct mm_struct * old_mm;
110 init_MUTEX(&mm->context.sem);
111 mm->context.size = 0;
112 mm->context.has_foreign_mappings = 0;
113 old_mm = current->mm;
114 if (old_mm && old_mm->context.size > 0) {
115 down(&old_mm->context.sem);
116 retval = copy_ldt(&mm->context, &old_mm->context);
117 up(&old_mm->context.sem);
123 * No need to lock the MM as we are the last user
125 void destroy_context(struct mm_struct *mm)
127 if (mm->context.size) {
128 if (mm == current->active_mm)
132 (mm->context.size * LDT_ENTRY_SIZE) / PAGE_SIZE,
133 XENFEAT_writable_descriptor_tables);
134 if (mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE)
135 vfree(mm->context.ldt);
137 kfree(mm->context.ldt);
138 mm->context.size = 0;
142 static int read_ldt(void __user * ptr, unsigned long bytecount)
146 struct mm_struct * mm = current->mm;
148 if (!mm->context.size)
150 if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
151 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
153 down(&mm->context.sem);
154 size = mm->context.size*LDT_ENTRY_SIZE;
155 if (size > bytecount)
159 if (copy_to_user(ptr, mm->context.ldt, size))
161 up(&mm->context.sem);
164 if (size != bytecount) {
165 /* zero-fill the rest */
166 if (clear_user(ptr+size, bytecount-size) != 0) {
176 static int read_default_ldt(void __user * ptr, unsigned long bytecount)
182 size = 5*sizeof(struct desc_struct);
183 if (size > bytecount)
187 if (clear_user(ptr, size))
193 static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode)
195 struct mm_struct * mm = current->mm;
196 __u32 entry_1, entry_2;
198 struct user_desc ldt_info;
201 if (bytecount != sizeof(ldt_info))
204 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
208 if (ldt_info.entry_number >= LDT_ENTRIES)
210 if (ldt_info.contents == 3) {
213 if (ldt_info.seg_not_present == 0)
217 down(&mm->context.sem);
218 if (ldt_info.entry_number >= mm->context.size) {
219 error = alloc_ldt(¤t->mm->context, ldt_info.entry_number+1, 1);
224 /* Allow LDTs to be cleared by the user. */
225 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
226 if (oldmode || LDT_empty(&ldt_info)) {
233 entry_1 = LDT_entry_a(&ldt_info);
234 entry_2 = LDT_entry_b(&ldt_info);
236 entry_2 &= ~(1 << 20);
238 /* Install the new entry ... */
240 error = write_ldt_entry(mm->context.ldt, ldt_info.entry_number,
244 up(&mm->context.sem);
249 asmlinkage int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
255 ret = read_ldt(ptr, bytecount);
258 ret = write_ldt(ptr, bytecount, 1);
261 ret = read_default_ldt(ptr, bytecount);
264 ret = write_ldt(ptr, bytecount, 0);