ldt.c 12 KB

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