Import 2.3.11pre3
[davej-history.git] / arch / i386 / kernel / ldt.c
blobfa2f06254080f0f963726ff9fff1769203443a3e
1 /*
2 * linux/kernel/ldt.c
4 * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
5 * Copyright (C) 1998 Ingo Molnar
6 */
8 #include <linux/errno.h>
9 #include <linux/sched.h>
10 #include <linux/string.h>
11 #include <linux/mm.h>
12 #include <linux/smp.h>
13 #include <linux/smp_lock.h>
14 #include <linux/vmalloc.h>
16 #include <asm/uaccess.h>
17 #include <asm/system.h>
18 #include <asm/ldt.h>
19 #include <asm/desc.h>
22 * read_ldt() is not really atomic - this is not a problem since
23 * synchronization of reads and writes done to the LDT has to be
24 * assured by user-space anyway. Writes are atomic, to protect
25 * the security checks done on new descriptors.
27 static int read_ldt(void * ptr, unsigned long bytecount)
29 int err;
30 unsigned long size;
31 struct mm_struct * mm = current->mm;
33 err = 0;
34 if (!mm->segments)
35 goto out;
37 size = LDT_ENTRIES*LDT_ENTRY_SIZE;
38 if (size > bytecount)
39 size = bytecount;
41 err = size;
42 if (copy_to_user(ptr, mm->segments, size))
43 err = -EFAULT;
44 out:
45 return err;
48 static int write_ldt(void * ptr, unsigned long bytecount, int oldmode)
50 struct mm_struct * mm = current->mm;
51 __u32 entry_1, entry_2, *lp;
52 int error;
53 struct modify_ldt_ldt_s ldt_info;
55 error = -EINVAL;
56 if (bytecount != sizeof(ldt_info))
57 goto out;
58 error = -EFAULT;
59 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
60 goto out;
62 error = -EINVAL;
63 if (ldt_info.entry_number >= LDT_ENTRIES)
64 goto out;
65 if (ldt_info.contents == 3) {
66 if (oldmode)
67 goto out;
68 if (ldt_info.seg_not_present == 0)
69 goto out;
73 * Horrible dependencies! Try to get rid of this. This is wrong,
74 * as it only reloads the ldt for the first process with this
75 * mm. The implications are that you should really make sure that
76 * you have a ldt before you do the first clone(), otherwise
77 * you get strange behaviour (the kernel is safe, it's just user
78 * space strangeness).
80 * we have two choices: either we preallocate the LDT descriptor
81 * and can do a shared modify_ldt(), or we postallocate it and do
82 * an smp message pass to update it. Currently we are a bit
83 * un-nice to user-space and reload the LDT only on the next
84 * schedule. (only an issue on SMP)
86 * the GDT index of the LDT is allocated dynamically, and is
87 * limited by MAX_LDT_DESCRIPTORS.
89 down(&mm->mmap_sem);
90 if (!mm->segments) {
92 error = -ENOMEM;
93 mm->segments = vmalloc(LDT_ENTRIES*LDT_ENTRY_SIZE);
94 if (!mm->segments)
95 goto out_unlock;
97 if (atomic_read(&mm->count) > 1)
98 printk(KERN_WARNING "LDT allocated for cloned task!\n");
100 * Possibly do an SMP cross-call to other CPUs to reload
101 * their LDTs
105 lp = (__u32 *) ((ldt_info.entry_number << 3) + (char *) mm->segments);
107 /* Allow LDTs to be cleared by the user. */
108 if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
109 if (oldmode ||
110 (ldt_info.contents == 0 &&
111 ldt_info.read_exec_only == 1 &&
112 ldt_info.seg_32bit == 0 &&
113 ldt_info.limit_in_pages == 0 &&
114 ldt_info.seg_not_present == 1 &&
115 ldt_info.useable == 0 )) {
116 entry_1 = 0;
117 entry_2 = 0;
118 goto install;
122 entry_1 = ((ldt_info.base_addr & 0x0000ffff) << 16) |
123 (ldt_info.limit & 0x0ffff);
124 entry_2 = (ldt_info.base_addr & 0xff000000) |
125 ((ldt_info.base_addr & 0x00ff0000) >> 16) |
126 (ldt_info.limit & 0xf0000) |
127 ((ldt_info.read_exec_only ^ 1) << 9) |
128 (ldt_info.contents << 10) |
129 ((ldt_info.seg_not_present ^ 1) << 15) |
130 (ldt_info.seg_32bit << 22) |
131 (ldt_info.limit_in_pages << 23) |
132 0x7000;
133 if (!oldmode)
134 entry_2 |= (ldt_info.useable << 20);
136 /* Install the new entry ... */
137 install:
138 *lp = entry_1;
139 *(lp+1) = entry_2;
140 error = 0;
142 out_unlock:
143 up(&mm->mmap_sem);
144 out:
145 return error;
148 asmlinkage int sys_modify_ldt(int func, void *ptr, unsigned long bytecount)
150 int ret = -ENOSYS;
152 switch (func) {
153 case 0:
154 ret = read_ldt(ptr, bytecount);
155 break;
156 case 1:
157 ret = write_ldt(ptr, bytecount, 1);
158 break;
159 case 0x11:
160 ret = write_ldt(ptr, bytecount, 0);
161 break;
163 return ret;