ldt.c 13 KB

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