Import 2.3.11pre1
[davej-history.git] / arch / i386 / kernel / ldt.c
blob25e8deec44e867023cbd875735317238a2eaa60d
1 /*
2 * linux/kernel/ldt.c
4 * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
5 */
7 #include <linux/errno.h>
8 #include <linux/sched.h>
9 #include <linux/string.h>
10 #include <linux/mm.h>
11 #include <linux/smp.h>
12 #include <linux/smp_lock.h>
13 #include <linux/vmalloc.h>
15 #include <asm/uaccess.h>
16 #include <asm/system.h>
17 #include <asm/ldt.h>
18 #include <asm/desc.h>
20 static int read_ldt(void * ptr, unsigned long bytecount)
22 void * address = current->mm->segments;
23 unsigned long size;
25 if (!ptr)
26 return -EINVAL;
27 if (!address)
28 return 0;
29 size = LDT_ENTRIES*LDT_ENTRY_SIZE;
30 if (size > bytecount)
31 size = bytecount;
32 return copy_to_user(ptr, address, size) ? -EFAULT : size;
35 static int write_ldt(void * ptr, unsigned long bytecount, int oldmode)
37 struct mm_struct * mm = current->mm;
38 __u32 entry_1, entry_2, *lp;
39 int error;
40 struct modify_ldt_ldt_s ldt_info;
42 error = -EINVAL;
43 if (bytecount != sizeof(ldt_info))
44 goto out;
45 error = -EFAULT;
46 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
47 goto out;
49 error = -EINVAL;
50 if (ldt_info.entry_number >= LDT_ENTRIES)
51 goto out;
52 if (ldt_info.contents == 3) {
53 if (oldmode)
54 goto out;
55 if (ldt_info.seg_not_present == 0)
56 goto out;
60 * Horrible dependencies! Try to get rid of this. This is wrong,
61 * as it only reloads the ldt for the first process with this
62 * mm. The implications are that you should really make sure that
63 * you have a ldt before you do the first clone(), otherwise
64 * you get strange behaviour (the kernel is safe, it's just user
65 * space strangeness).
67 * For no good reason except historical, the GDT index of the LDT
68 * is chosen to follow the index number in the task[] array.
70 if (!mm->segments) {
71 void * ldt;
72 error = -ENOMEM;
73 ldt = vmalloc(LDT_ENTRIES*LDT_ENTRY_SIZE);
74 if (!ldt)
75 goto out;
76 memset(ldt, 0, LDT_ENTRIES*LDT_ENTRY_SIZE);
78 * Make sure someone else hasn't allocated it for us ...
80 if (!mm->segments) {
81 int i = current->tarray_ptr - &task[0];
82 mm->segments = ldt;
83 set_ldt_desc(i, ldt, LDT_ENTRIES);
84 current->tss.ldt = _LDT(i);
85 load_ldt(i);
86 if (atomic_read(&mm->count) > 1)
87 printk(KERN_WARNING
88 "LDT allocated for cloned task!\n");
89 } else {
90 vfree(ldt);
94 lp = (__u32 *) ((ldt_info.entry_number << 3) + (char *) mm->segments);
96 /* Allow LDTs to be cleared by the user. */
97 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
98 if (oldmode ||
99 (ldt_info.contents == 0 &&
100 ldt_info.read_exec_only == 1 &&
101 ldt_info.seg_32bit == 0 &&
102 ldt_info.limit_in_pages == 0 &&
103 ldt_info.seg_not_present == 1 &&
104 ldt_info.useable == 0 )) {
105 entry_1 = 0;
106 entry_2 = 0;
107 goto install;
111 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
112 (ldt_info.limit & 0x0ffff);
113 entry_2 = (ldt_info.base_addr & 0xff000000) |
114 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
115 (ldt_info.limit & 0xf0000) |
116 ((ldt_info.read_exec_only ^ 1) << 9) |
117 (ldt_info.contents << 10) |
118 ((ldt_info.seg_not_present ^ 1) << 15) |
119 (ldt_info.seg_32bit << 22) |
120 (ldt_info.limit_in_pages << 23) |
121 0x7000;
122 if (!oldmode)
123 entry_2 |= (ldt_info.useable << 20);
125 /* Install the new entry ... */
126 install:
127 *lp = entry_1;
128 *(lp+1) = entry_2;
129 error = 0;
130 out:
131 return error;
134 asmlinkage int sys_modify_ldt(int func, void *ptr, unsigned long bytecount)
136 int ret = -ENOSYS;
138 lock_kernel();
139 switch (func) {
140 case 0:
141 ret = read_ldt(ptr, bytecount);
142 break;
143 case 1:
144 ret = write_ldt(ptr, bytecount, 1);
145 break;
146 case 0x11:
147 ret = write_ldt(ptr, bytecount, 0);
148 break;
150 unlock_kernel();
151 return ret;