ldt.c 12 KB

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