blob: 5c0b711d2433d3bb2f67e1f744917bc272bf37c9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Dikeba180fd2007-10-16 01:27:00 -07002 * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Jeff Dike8192ab42008-02-04 22:30:53 -08006#include <linux/mm.h>
7#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Jeff Dike8192ab42008-02-04 22:30:53 -08009#include <asm/unistd.h>
Al Viro37185b32012-10-08 03:27:32 +010010#include <os.h>
Al Viro37185b32012-10-08 03:27:32 +010011#include <skas.h>
Al Viro37185b32012-10-08 03:27:32 +010012#include <sysdep/tls.h>
Jeff Dikeba180fd2007-10-16 01:27:00 -070013
14extern int modify_ldt(int func, void *ptr, unsigned long bytecount);
Bodo Stroesser858259c2005-11-07 00:58:55 -080015
WANG Cong99764fa2008-07-23 21:28:49 -070016static long write_ldt_entry(struct mm_id *mm_idp, int func,
17 struct user_desc *desc, void **addr, int done)
Bodo Stroesser858259c2005-11-07 00:58:55 -080018{
19 long res;
Richard Weinbergerd0b5e152015-03-18 21:31:27 +010020 void *stub_addr;
21 res = syscall_stub_data(mm_idp, (unsigned long *)desc,
22 (sizeof(*desc) + sizeof(long) - 1) &
23 ~(sizeof(long) - 1),
24 addr, &stub_addr);
25 if (!res) {
26 unsigned long args[] = { func,
27 (unsigned long)stub_addr,
28 sizeof(*desc),
29 0, 0, 0 };
30 res = run_syscall_stub(mm_idp, __NR_modify_ldt, args,
31 0, addr, done);
Bodo Stroesser858259c2005-11-07 00:58:55 -080032 }
33
Bodo Stroesser858259c2005-11-07 00:58:55 -080034 return res;
35}
36
37/*
38 * In skas mode, we hold our own ldt data in UML.
39 * Thus, the code implementing sys_modify_ldt_skas
40 * is very similar to (and mostly stolen from) sys_modify_ldt
41 * for arch/i386/kernel/ldt.c
42 * The routines copied and modified in part are:
43 * - read_ldt
44 * - read_default_ldt
45 * - write_ldt
46 * - sys_modify_ldt_skas
47 */
48
49static int read_ldt(void __user * ptr, unsigned long bytecount)
50{
51 int i, err = 0;
52 unsigned long size;
Al Virob3ee5712011-08-18 20:10:49 +010053 uml_ldt_t *ldt = &current->mm->context.arch.ldt;
Bodo Stroesser858259c2005-11-07 00:58:55 -080054
Jeff Dikeba180fd2007-10-16 01:27:00 -070055 if (!ldt->entry_count)
Bodo Stroesser858259c2005-11-07 00:58:55 -080056 goto out;
Jeff Dikeba180fd2007-10-16 01:27:00 -070057 if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
Bodo Stroesser858259c2005-11-07 00:58:55 -080058 bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
59 err = bytecount;
60
Daniel Walker01ac8352008-02-04 22:31:26 -080061 mutex_lock(&ldt->lock);
Jeff Dikeba180fd2007-10-16 01:27:00 -070062 if (ldt->entry_count <= LDT_DIRECT_ENTRIES) {
Bodo Stroesser858259c2005-11-07 00:58:55 -080063 size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
Jeff Dikeba180fd2007-10-16 01:27:00 -070064 if (size > bytecount)
Bodo Stroesser858259c2005-11-07 00:58:55 -080065 size = bytecount;
Jeff Dikeba180fd2007-10-16 01:27:00 -070066 if (copy_to_user(ptr, ldt->u.entries, size))
Bodo Stroesser858259c2005-11-07 00:58:55 -080067 err = -EFAULT;
68 bytecount -= size;
69 ptr += size;
70 }
71 else {
Jeff Dikeba180fd2007-10-16 01:27:00 -070072 for (i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
73 i++) {
Bodo Stroesser858259c2005-11-07 00:58:55 -080074 size = PAGE_SIZE;
Jeff Dikeba180fd2007-10-16 01:27:00 -070075 if (size > bytecount)
Bodo Stroesser858259c2005-11-07 00:58:55 -080076 size = bytecount;
Jeff Dikeba180fd2007-10-16 01:27:00 -070077 if (copy_to_user(ptr, ldt->u.pages[i], size)) {
Bodo Stroesser858259c2005-11-07 00:58:55 -080078 err = -EFAULT;
79 break;
80 }
81 bytecount -= size;
82 ptr += size;
83 }
84 }
Daniel Walker01ac8352008-02-04 22:31:26 -080085 mutex_unlock(&ldt->lock);
Bodo Stroesser858259c2005-11-07 00:58:55 -080086
Jeff Dikeba180fd2007-10-16 01:27:00 -070087 if (bytecount == 0 || err == -EFAULT)
Bodo Stroesser858259c2005-11-07 00:58:55 -080088 goto out;
89
Jeff Dikeba180fd2007-10-16 01:27:00 -070090 if (clear_user(ptr, bytecount))
Bodo Stroesser858259c2005-11-07 00:58:55 -080091 err = -EFAULT;
92
93out:
94 return err;
95}
96
97static int read_default_ldt(void __user * ptr, unsigned long bytecount)
98{
99 int err;
100
Jeff Dikeba180fd2007-10-16 01:27:00 -0700101 if (bytecount > 5*LDT_ENTRY_SIZE)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800102 bytecount = 5*LDT_ENTRY_SIZE;
103
104 err = bytecount;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700105 /*
106 * UML doesn't support lcall7 and lcall27.
Bodo Stroesser858259c2005-11-07 00:58:55 -0800107 * So, we don't really have a default ldt, but emulate
108 * an empty ldt of common host default ldt size.
109 */
Jeff Dikeba180fd2007-10-16 01:27:00 -0700110 if (clear_user(ptr, bytecount))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800111 err = -EFAULT;
112
113 return err;
114}
115
116static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
117{
Al Virob3ee5712011-08-18 20:10:49 +0100118 uml_ldt_t *ldt = &current->mm->context.arch.ldt;
Jeff Dike6c738ff2007-10-16 01:27:06 -0700119 struct mm_id * mm_idp = &current->mm->context.id;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800120 int i, err;
121 struct user_desc ldt_info;
122 struct ldt_entry entry0, *ldt_p;
123 void *addr = NULL;
124
125 err = -EINVAL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700126 if (bytecount != sizeof(ldt_info))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800127 goto out;
128 err = -EFAULT;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700129 if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800130 goto out;
131
132 err = -EINVAL;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700133 if (ldt_info.entry_number >= LDT_ENTRIES)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800134 goto out;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700135 if (ldt_info.contents == 3) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800136 if (func == 1)
137 goto out;
138 if (ldt_info.seg_not_present == 0)
139 goto out;
140 }
141
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100142 mutex_lock(&ldt->lock);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800143
144 err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700145 if (err)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800146 goto out_unlock;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800147
Jeff Dikeba180fd2007-10-16 01:27:00 -0700148 if (ldt_info.entry_number >= ldt->entry_count &&
149 ldt_info.entry_number >= LDT_DIRECT_ENTRIES) {
150 for (i=ldt->entry_count/LDT_ENTRIES_PER_PAGE;
151 i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number;
152 i++) {
153 if (i == 0)
Jeff Dikee23181d2005-11-21 21:32:08 -0800154 memcpy(&entry0, ldt->u.entries,
155 sizeof(entry0));
156 ldt->u.pages[i] = (struct ldt_entry *)
157 __get_free_page(GFP_KERNEL|__GFP_ZERO);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700158 if (!ldt->u.pages[i]) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800159 err = -ENOMEM;
160 /* Undo the change in host */
161 memset(&ldt_info, 0, sizeof(ldt_info));
162 write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1);
163 goto out_unlock;
164 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700165 if (i == 0) {
Jeff Dikee23181d2005-11-21 21:32:08 -0800166 memcpy(ldt->u.pages[0], &entry0,
167 sizeof(entry0));
168 memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
Bodo Stroesser858259c2005-11-07 00:58:55 -0800169 sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
170 }
171 ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
172 }
173 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700174 if (ldt->entry_count <= ldt_info.entry_number)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800175 ldt->entry_count = ldt_info.entry_number + 1;
176
Jeff Dikeba180fd2007-10-16 01:27:00 -0700177 if (ldt->entry_count <= LDT_DIRECT_ENTRIES)
Jeff Dikee23181d2005-11-21 21:32:08 -0800178 ldt_p = ldt->u.entries + ldt_info.entry_number;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800179 else
Jeff Dikee23181d2005-11-21 21:32:08 -0800180 ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
Bodo Stroesser858259c2005-11-07 00:58:55 -0800181 ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
182
Jeff Dikeba180fd2007-10-16 01:27:00 -0700183 if (ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
184 (func == 1 || LDT_empty(&ldt_info))) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800185 ldt_p->a = 0;
186 ldt_p->b = 0;
187 }
188 else{
189 if (func == 1)
190 ldt_info.useable = 0;
191 ldt_p->a = LDT_entry_a(&ldt_info);
192 ldt_p->b = LDT_entry_b(&ldt_info);
193 }
194 err = 0;
195
196out_unlock:
Daniel Walker01ac8352008-02-04 22:31:26 -0800197 mutex_unlock(&ldt->lock);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800198out:
199 return err;
200}
201
202static long do_modify_ldt_skas(int func, void __user *ptr,
203 unsigned long bytecount)
204{
205 int ret = -ENOSYS;
206
207 switch (func) {
208 case 0:
209 ret = read_ldt(ptr, bytecount);
210 break;
211 case 1:
212 case 0x11:
213 ret = write_ldt(ptr, bytecount, func);
214 break;
215 case 2:
216 ret = read_default_ldt(ptr, bytecount);
217 break;
218 }
219 return ret;
220}
221
Jeff Dikeaf727902007-02-28 20:13:06 -0800222static DEFINE_SPINLOCK(host_ldt_lock);
223static short dummy_list[9] = {0, -1};
224static short * host_ldt_entries = NULL;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800225
Jeff Dikeaf727902007-02-28 20:13:06 -0800226static void ldt_get_host_info(void)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800227{
228 long ret;
Jeff Dike622e6962007-03-29 01:20:32 -0700229 struct ldt_entry * ldt;
230 short *tmp;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800231 int i, size, k, order;
232
Jeff Dikeaf727902007-02-28 20:13:06 -0800233 spin_lock(&host_ldt_lock);
234
Jeff Dikeba180fd2007-10-16 01:27:00 -0700235 if (host_ldt_entries != NULL) {
Jeff Dikeaf727902007-02-28 20:13:06 -0800236 spin_unlock(&host_ldt_lock);
237 return;
238 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800239 host_ldt_entries = dummy_list+1;
240
Jeff Dikeaf727902007-02-28 20:13:06 -0800241 spin_unlock(&host_ldt_lock);
242
Jeff Dikeba180fd2007-10-16 01:27:00 -0700243 for (i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++)
244 ;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800245
246 ldt = (struct ldt_entry *)
247 __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700248 if (ldt == NULL) {
249 printk(KERN_ERR "ldt_get_host_info: couldn't allocate buffer "
250 "for host ldt\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800251 return;
252 }
253
254 ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700255 if (ret < 0) {
256 printk(KERN_ERR "ldt_get_host_info: couldn't read host ldt\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800257 goto out_free;
258 }
Jeff Dikeba180fd2007-10-16 01:27:00 -0700259 if (ret == 0) {
Bodo Stroesser858259c2005-11-07 00:58:55 -0800260 /* default_ldt is active, simply write an empty entry 0 */
261 host_ldt_entries = dummy_list;
262 goto out_free;
263 }
264
Jeff Dikeba180fd2007-10-16 01:27:00 -0700265 for (i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++) {
266 if (ldt[i].a != 0 || ldt[i].b != 0)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800267 size++;
268 }
269
Jeff Dikeba180fd2007-10-16 01:27:00 -0700270 if (size < ARRAY_SIZE(dummy_list))
Bodo Stroesser858259c2005-11-07 00:58:55 -0800271 host_ldt_entries = dummy_list;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800272 else {
273 size = (size + 1) * sizeof(dummy_list[0]);
Jeff Dikeaf727902007-02-28 20:13:06 -0800274 tmp = kmalloc(size, GFP_KERNEL);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700275 if (tmp == NULL) {
276 printk(KERN_ERR "ldt_get_host_info: couldn't allocate "
277 "host ldt list\n");
Bodo Stroesser858259c2005-11-07 00:58:55 -0800278 goto out_free;
279 }
Jeff Dikeaf727902007-02-28 20:13:06 -0800280 host_ldt_entries = tmp;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800281 }
282
Jeff Dikeba180fd2007-10-16 01:27:00 -0700283 for (i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++) {
284 if (ldt[i].a != 0 || ldt[i].b != 0)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800285 host_ldt_entries[k++] = i;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800286 }
287 host_ldt_entries[k] = -1;
288
289out_free:
290 free_pages((unsigned long)ldt, order);
291}
292
Jeff Dike6c738ff2007-10-16 01:27:06 -0700293long init_new_ldt(struct mm_context *new_mm, struct mm_context *from_mm)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800294{
295 struct user_desc desc;
296 short * num_p;
297 int i;
298 long page, err=0;
299 void *addr = NULL;
300
Bodo Stroesser858259c2005-11-07 00:58:55 -0800301
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100302 mutex_init(&new_mm->arch.ldt.lock);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800303
Jeff Dikeba180fd2007-10-16 01:27:00 -0700304 if (!from_mm) {
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800305 memset(&desc, 0, sizeof(desc));
Bodo Stroesser858259c2005-11-07 00:58:55 -0800306 /*
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100307 * Now we try to retrieve info about the ldt, we
308 * inherited from the host. All ldt-entries found
309 * will be reset in the following loop
Bodo Stroesser858259c2005-11-07 00:58:55 -0800310 */
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100311 ldt_get_host_info();
312 for (num_p=host_ldt_entries; *num_p != -1; num_p++) {
313 desc.entry_number = *num_p;
314 err = write_ldt_entry(&new_mm->id, 1, &desc,
315 &addr, *(num_p + 1) == -1);
316 if (err)
317 break;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800318 }
Al Virob3ee5712011-08-18 20:10:49 +0100319 new_mm->arch.ldt.entry_count = 0;
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800320
321 goto out;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800322 }
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800323
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100324 /*
325 * Our local LDT is used to supply the data for
326 * modify_ldt(READLDT), if PTRACE_LDT isn't available,
327 * i.e., we have to use the stub for modify_ldt, which
328 * can't handle the big read buffer of up to 64kB.
329 */
330 mutex_lock(&from_mm->arch.ldt.lock);
331 if (from_mm->arch.ldt.entry_count <= LDT_DIRECT_ENTRIES)
332 memcpy(new_mm->arch.ldt.u.entries, from_mm->arch.ldt.u.entries,
333 sizeof(new_mm->arch.ldt.u.entries));
334 else {
335 i = from_mm->arch.ldt.entry_count / LDT_ENTRIES_PER_PAGE;
336 while (i-->0) {
337 page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
338 if (!page) {
339 err = -ENOMEM;
340 break;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800341 }
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100342 new_mm->arch.ldt.u.pages[i] =
343 (struct ldt_entry *) page;
344 memcpy(new_mm->arch.ldt.u.pages[i],
345 from_mm->arch.ldt.u.pages[i], PAGE_SIZE);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800346 }
Bodo Stroesser858259c2005-11-07 00:58:55 -0800347 }
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100348 new_mm->arch.ldt.entry_count = from_mm->arch.ldt.entry_count;
349 mutex_unlock(&from_mm->arch.ldt.lock);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800350
Bodo Stroesser12919aa2006-01-18 17:42:39 -0800351 out:
Bodo Stroesser858259c2005-11-07 00:58:55 -0800352 return err;
353}
354
355
Jeff Dike6c738ff2007-10-16 01:27:06 -0700356void free_ldt(struct mm_context *mm)
Bodo Stroesser858259c2005-11-07 00:58:55 -0800357{
358 int i;
359
Richard Weinbergerd0b5e152015-03-18 21:31:27 +0100360 if (mm->arch.ldt.entry_count > LDT_DIRECT_ENTRIES) {
Al Virob3ee5712011-08-18 20:10:49 +0100361 i = mm->arch.ldt.entry_count / LDT_ENTRIES_PER_PAGE;
Jeff Dikeba180fd2007-10-16 01:27:00 -0700362 while (i-- > 0)
Al Virob3ee5712011-08-18 20:10:49 +0100363 free_page((long) mm->arch.ldt.u.pages[i]);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800364 }
Al Virob3ee5712011-08-18 20:10:49 +0100365 mm->arch.ldt.entry_count = 0;
Bodo Stroesser858259c2005-11-07 00:58:55 -0800366}
Bodo Stroesser858259c2005-11-07 00:58:55 -0800367
368int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
369{
Jeff Dike6aa802c2007-10-16 01:26:56 -0700370 return do_modify_ldt_skas(func, ptr, bytecount);
Bodo Stroesser858259c2005-11-07 00:58:55 -0800371}