ldt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/mm.h"
  6. #include "asm/unistd.h"
  7. #include "os.h"
  8. #include "proc_mm.h"
  9. #include "skas.h"
  10. #include "skas_ptrace.h"
  11. #include "sysdep/tls.h"
  12. extern int modify_ldt(int func, void *ptr, unsigned long bytecount);
  13. long write_ldt_entry(struct mm_id * mm_idp, int func, struct user_desc * desc,
  14. void **addr, int done)
  15. {
  16. long res;
  17. if (proc_mm) {
  18. /*
  19. * This is a special handling for the case, that the mm to
  20. * modify isn't current->active_mm.
  21. * If this is called directly by modify_ldt,
  22. * (current->active_mm->context.skas.u == mm_idp)
  23. * will be true. So no call to __switch_mm(mm_idp) is done.
  24. * If this is called in case of init_new_ldt or PTRACE_LDT,
  25. * mm_idp won't belong to current->active_mm, but child->mm.
  26. * So we need to switch child's mm into our userspace, then
  27. * later switch back.
  28. *
  29. * Note: I'm unsure: should interrupts be disabled here?
  30. */
  31. if (!current->active_mm || current->active_mm == &init_mm ||
  32. mm_idp != &current->active_mm->context.id)
  33. __switch_mm(mm_idp);
  34. }
  35. if (ptrace_ldt) {
  36. struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
  37. .func = func,
  38. .ptr = desc,
  39. .bytecount = sizeof(*desc)};
  40. u32 cpu;
  41. int pid;
  42. if (!proc_mm)
  43. pid = mm_idp->u.pid;
  44. else {
  45. cpu = get_cpu();
  46. pid = userspace_pid[cpu];
  47. }
  48. res = os_ptrace_ldt(pid, 0, (unsigned long) &ldt_op);
  49. if (proc_mm)
  50. put_cpu();
  51. }
  52. else {
  53. void *stub_addr;
  54. res = syscall_stub_data(mm_idp, (unsigned long *)desc,
  55. (sizeof(*desc) + sizeof(long) - 1) &
  56. ~(sizeof(long) - 1),
  57. addr, &stub_addr);
  58. if (!res) {
  59. unsigned long args[] = { func,
  60. (unsigned long)stub_addr,
  61. sizeof(*desc),
  62. 0, 0, 0 };
  63. res = run_syscall_stub(mm_idp, __NR_modify_ldt, args,
  64. 0, addr, done);
  65. }
  66. }
  67. if (proc_mm) {
  68. /*
  69. * This is the second part of special handling, that makes
  70. * PTRACE_LDT possible to implement.
  71. */
  72. if (current->active_mm && current->active_mm != &init_mm &&
  73. mm_idp != &current->active_mm->context.id)
  74. __switch_mm(&current->active_mm->context.id);
  75. }
  76. return res;
  77. }
  78. static long read_ldt_from_host(void __user * ptr, unsigned long bytecount)
  79. {
  80. int res, n;
  81. struct ptrace_ldt ptrace_ldt = (struct ptrace_ldt) {
  82. .func = 0,
  83. .bytecount = bytecount,
  84. .ptr = kmalloc(bytecount, GFP_KERNEL)};
  85. u32 cpu;
  86. if (ptrace_ldt.ptr == NULL)
  87. return -ENOMEM;
  88. /*
  89. * This is called from sys_modify_ldt only, so userspace_pid gives
  90. * us the right number
  91. */
  92. cpu = get_cpu();
  93. res = os_ptrace_ldt(userspace_pid[cpu], 0, (unsigned long) &ptrace_ldt);
  94. put_cpu();
  95. if (res < 0)
  96. goto out;
  97. n = copy_to_user(ptr, ptrace_ldt.ptr, res);
  98. if (n != 0)
  99. res = -EFAULT;
  100. out:
  101. kfree(ptrace_ldt.ptr);
  102. return res;
  103. }
  104. /*
  105. * In skas mode, we hold our own ldt data in UML.
  106. * Thus, the code implementing sys_modify_ldt_skas
  107. * is very similar to (and mostly stolen from) sys_modify_ldt
  108. * for arch/i386/kernel/ldt.c
  109. * The routines copied and modified in part are:
  110. * - read_ldt
  111. * - read_default_ldt
  112. * - write_ldt
  113. * - sys_modify_ldt_skas
  114. */
  115. static int read_ldt(void __user * ptr, unsigned long bytecount)
  116. {
  117. int i, err = 0;
  118. unsigned long size;
  119. uml_ldt_t * ldt = &current->mm->context.ldt;
  120. if (!ldt->entry_count)
  121. goto out;
  122. if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
  123. bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
  124. err = bytecount;
  125. if (ptrace_ldt)
  126. return read_ldt_from_host(ptr, bytecount);
  127. down(&ldt->semaphore);
  128. if (ldt->entry_count <= LDT_DIRECT_ENTRIES) {
  129. size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
  130. if (size > bytecount)
  131. size = bytecount;
  132. if (copy_to_user(ptr, ldt->u.entries, size))
  133. err = -EFAULT;
  134. bytecount -= size;
  135. ptr += size;
  136. }
  137. else {
  138. for (i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
  139. i++) {
  140. size = PAGE_SIZE;
  141. if (size > bytecount)
  142. size = bytecount;
  143. if (copy_to_user(ptr, ldt->u.pages[i], size)) {
  144. err = -EFAULT;
  145. break;
  146. }
  147. bytecount -= size;
  148. ptr += size;
  149. }
  150. }
  151. up(&ldt->semaphore);
  152. if (bytecount == 0 || err == -EFAULT)
  153. goto out;
  154. if (clear_user(ptr, bytecount))
  155. err = -EFAULT;
  156. out:
  157. return err;
  158. }
  159. static int read_default_ldt(void __user * ptr, unsigned long bytecount)
  160. {
  161. int err;
  162. if (bytecount > 5*LDT_ENTRY_SIZE)
  163. bytecount = 5*LDT_ENTRY_SIZE;
  164. err = bytecount;
  165. /*
  166. * UML doesn't support lcall7 and lcall27.
  167. * So, we don't really have a default ldt, but emulate
  168. * an empty ldt of common host default ldt size.
  169. */
  170. if (clear_user(ptr, bytecount))
  171. err = -EFAULT;
  172. return err;
  173. }
  174. static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
  175. {
  176. uml_ldt_t * ldt = &current->mm->context.ldt;
  177. struct mm_id * mm_idp = &current->mm->context.id;
  178. int i, err;
  179. struct user_desc ldt_info;
  180. struct ldt_entry entry0, *ldt_p;
  181. void *addr = NULL;
  182. err = -EINVAL;
  183. if (bytecount != sizeof(ldt_info))
  184. goto out;
  185. err = -EFAULT;
  186. if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
  187. goto out;
  188. err = -EINVAL;
  189. if (ldt_info.entry_number >= LDT_ENTRIES)
  190. goto out;
  191. if (ldt_info.contents == 3) {
  192. if (func == 1)
  193. goto out;
  194. if (ldt_info.seg_not_present == 0)
  195. goto out;
  196. }
  197. if (!ptrace_ldt)
  198. down(&ldt->semaphore);
  199. err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
  200. if (err)
  201. goto out_unlock;
  202. else if (ptrace_ldt) {
  203. /* With PTRACE_LDT available, this is used as a flag only */
  204. ldt->entry_count = 1;
  205. goto out;
  206. }
  207. if (ldt_info.entry_number >= ldt->entry_count &&
  208. ldt_info.entry_number >= LDT_DIRECT_ENTRIES) {
  209. for (i=ldt->entry_count/LDT_ENTRIES_PER_PAGE;
  210. i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number;
  211. i++) {
  212. if (i == 0)
  213. memcpy(&entry0, ldt->u.entries,
  214. sizeof(entry0));
  215. ldt->u.pages[i] = (struct ldt_entry *)
  216. __get_free_page(GFP_KERNEL|__GFP_ZERO);
  217. if (!ldt->u.pages[i]) {
  218. err = -ENOMEM;
  219. /* Undo the change in host */
  220. memset(&ldt_info, 0, sizeof(ldt_info));
  221. write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1);
  222. goto out_unlock;
  223. }
  224. if (i == 0) {
  225. memcpy(ldt->u.pages[0], &entry0,
  226. sizeof(entry0));
  227. memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
  228. sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
  229. }
  230. ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
  231. }
  232. }
  233. if (ldt->entry_count <= ldt_info.entry_number)
  234. ldt->entry_count = ldt_info.entry_number + 1;
  235. if (ldt->entry_count <= LDT_DIRECT_ENTRIES)
  236. ldt_p = ldt->u.entries + ldt_info.entry_number;
  237. else
  238. ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
  239. ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
  240. if (ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
  241. (func == 1 || LDT_empty(&ldt_info))) {
  242. ldt_p->a = 0;
  243. ldt_p->b = 0;
  244. }
  245. else{
  246. if (func == 1)
  247. ldt_info.useable = 0;
  248. ldt_p->a = LDT_entry_a(&ldt_info);
  249. ldt_p->b = LDT_entry_b(&ldt_info);
  250. }
  251. err = 0;
  252. out_unlock:
  253. up(&ldt->semaphore);
  254. out:
  255. return err;
  256. }
  257. static long do_modify_ldt_skas(int func, void __user *ptr,
  258. unsigned long bytecount)
  259. {
  260. int ret = -ENOSYS;
  261. switch (func) {
  262. case 0:
  263. ret = read_ldt(ptr, bytecount);
  264. break;
  265. case 1:
  266. case 0x11:
  267. ret = write_ldt(ptr, bytecount, func);
  268. break;
  269. case 2:
  270. ret = read_default_ldt(ptr, bytecount);
  271. break;
  272. }
  273. return ret;
  274. }
  275. static DEFINE_SPINLOCK(host_ldt_lock);
  276. static short dummy_list[9] = {0, -1};
  277. static short * host_ldt_entries = NULL;
  278. static void ldt_get_host_info(void)
  279. {
  280. long ret;
  281. struct ldt_entry * ldt;
  282. short *tmp;
  283. int i, size, k, order;
  284. spin_lock(&host_ldt_lock);
  285. if (host_ldt_entries != NULL) {
  286. spin_unlock(&host_ldt_lock);
  287. return;
  288. }
  289. host_ldt_entries = dummy_list+1;
  290. spin_unlock(&host_ldt_lock);
  291. for (i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++)
  292. ;
  293. ldt = (struct ldt_entry *)
  294. __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
  295. if (ldt == NULL) {
  296. printk(KERN_ERR "ldt_get_host_info: couldn't allocate buffer "
  297. "for host ldt\n");
  298. return;
  299. }
  300. ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
  301. if (ret < 0) {
  302. printk(KERN_ERR "ldt_get_host_info: couldn't read host ldt\n");
  303. goto out_free;
  304. }
  305. if (ret == 0) {
  306. /* default_ldt is active, simply write an empty entry 0 */
  307. host_ldt_entries = dummy_list;
  308. goto out_free;
  309. }
  310. for (i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++) {
  311. if (ldt[i].a != 0 || ldt[i].b != 0)
  312. size++;
  313. }
  314. if (size < ARRAY_SIZE(dummy_list))
  315. host_ldt_entries = dummy_list;
  316. else {
  317. size = (size + 1) * sizeof(dummy_list[0]);
  318. tmp = kmalloc(size, GFP_KERNEL);
  319. if (tmp == NULL) {
  320. printk(KERN_ERR "ldt_get_host_info: couldn't allocate "
  321. "host ldt list\n");
  322. goto out_free;
  323. }
  324. host_ldt_entries = tmp;
  325. }
  326. for (i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++) {
  327. if (ldt[i].a != 0 || ldt[i].b != 0)
  328. host_ldt_entries[k++] = i;
  329. }
  330. host_ldt_entries[k] = -1;
  331. out_free:
  332. free_pages((unsigned long)ldt, order);
  333. }
  334. long init_new_ldt(struct mm_context *new_mm, struct mm_context *from_mm)
  335. {
  336. struct user_desc desc;
  337. short * num_p;
  338. int i;
  339. long page, err=0;
  340. void *addr = NULL;
  341. struct proc_mm_op copy;
  342. if (!ptrace_ldt)
  343. init_MUTEX(&new_mm->ldt.semaphore);
  344. if (!from_mm) {
  345. memset(&desc, 0, sizeof(desc));
  346. /*
  347. * We have to initialize a clean ldt.
  348. */
  349. if (proc_mm) {
  350. /*
  351. * If the new mm was created using proc_mm, host's
  352. * default-ldt currently is assigned, which normally
  353. * contains the call-gates for lcall7 and lcall27.
  354. * To remove these gates, we simply write an empty
  355. * entry as number 0 to the host.
  356. */
  357. err = write_ldt_entry(&new_mm->id, 1, &desc, &addr, 1);
  358. }
  359. else{
  360. /*
  361. * Now we try to retrieve info about the ldt, we
  362. * inherited from the host. All ldt-entries found
  363. * will be reset in the following loop
  364. */
  365. ldt_get_host_info();
  366. for (num_p=host_ldt_entries; *num_p != -1; num_p++) {
  367. desc.entry_number = *num_p;
  368. err = write_ldt_entry(&new_mm->id, 1, &desc,
  369. &addr, *(num_p + 1) == -1);
  370. if (err)
  371. break;
  372. }
  373. }
  374. new_mm->ldt.entry_count = 0;
  375. goto out;
  376. }
  377. if (proc_mm) {
  378. /*
  379. * We have a valid from_mm, so we now have to copy the LDT of
  380. * from_mm to new_mm, because using proc_mm an new mm with
  381. * an empty/default LDT was created in new_mm()
  382. */
  383. copy = ((struct proc_mm_op) { .op = MM_COPY_SEGMENTS,
  384. .u =
  385. { .copy_segments =
  386. from_mm->id.u.mm_fd } } );
  387. i = os_write_file(new_mm->id.u.mm_fd, &copy, sizeof(copy));
  388. if (i != sizeof(copy))
  389. printk(KERN_ERR "new_mm : /proc/mm copy_segments "
  390. "failed, err = %d\n", -i);
  391. }
  392. if (!ptrace_ldt) {
  393. /*
  394. * Our local LDT is used to supply the data for
  395. * modify_ldt(READLDT), if PTRACE_LDT isn't available,
  396. * i.e., we have to use the stub for modify_ldt, which
  397. * can't handle the big read buffer of up to 64kB.
  398. */
  399. down(&from_mm->ldt.semaphore);
  400. if (from_mm->ldt.entry_count <= LDT_DIRECT_ENTRIES)
  401. memcpy(new_mm->ldt.u.entries, from_mm->ldt.u.entries,
  402. sizeof(new_mm->ldt.u.entries));
  403. else {
  404. i = from_mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
  405. while (i-->0) {
  406. page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
  407. if (!page) {
  408. err = -ENOMEM;
  409. break;
  410. }
  411. new_mm->ldt.u.pages[i] =
  412. (struct ldt_entry *) page;
  413. memcpy(new_mm->ldt.u.pages[i],
  414. from_mm->ldt.u.pages[i], PAGE_SIZE);
  415. }
  416. }
  417. new_mm->ldt.entry_count = from_mm->ldt.entry_count;
  418. up(&from_mm->ldt.semaphore);
  419. }
  420. out:
  421. return err;
  422. }
  423. void free_ldt(struct mm_context *mm)
  424. {
  425. int i;
  426. if (!ptrace_ldt && mm->ldt.entry_count > LDT_DIRECT_ENTRIES) {
  427. i = mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
  428. while (i-- > 0)
  429. free_page((long) mm->ldt.u.pages[i]);
  430. }
  431. mm->ldt.entry_count = 0;
  432. }
  433. int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
  434. {
  435. return do_modify_ldt_skas(func, ptr, bytecount);
  436. }