ldt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 unsure: 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 = os_ptrace_ldt(pid, 0, (unsigned long) &ldt_op);
  114. if(proc_mm)
  115. put_cpu();
  116. }
  117. else {
  118. void *stub_addr;
  119. res = syscall_stub_data(mm_idp, (unsigned long *)desc,
  120. (sizeof(*desc) + sizeof(long) - 1) &
  121. ~(sizeof(long) - 1),
  122. addr, &stub_addr);
  123. if(!res){
  124. unsigned long args[] = { func,
  125. (unsigned long)stub_addr,
  126. sizeof(*desc),
  127. 0, 0, 0 };
  128. res = run_syscall_stub(mm_idp, __NR_modify_ldt, args,
  129. 0, addr, done);
  130. }
  131. }
  132. if(proc_mm){
  133. /* This is the second part of special handling, that makes
  134. * PTRACE_LDT possible to implement.
  135. */
  136. if(current->active_mm && current->active_mm != &init_mm &&
  137. mm_idp != &current->active_mm->context.skas.id)
  138. switch_mm_skas(&current->active_mm->context.skas.id);
  139. }
  140. return res;
  141. }
  142. static long read_ldt_from_host(void __user * ptr, unsigned long bytecount)
  143. {
  144. int res, n;
  145. struct ptrace_ldt ptrace_ldt = (struct ptrace_ldt) {
  146. .func = 0,
  147. .bytecount = bytecount,
  148. .ptr = (void *)kmalloc(bytecount, GFP_KERNEL)};
  149. u32 cpu;
  150. if(ptrace_ldt.ptr == NULL)
  151. return -ENOMEM;
  152. /* This is called from sys_modify_ldt only, so userspace_pid gives
  153. * us the right number
  154. */
  155. cpu = get_cpu();
  156. res = os_ptrace_ldt(userspace_pid[cpu], 0, (unsigned long) &ptrace_ldt);
  157. put_cpu();
  158. if(res < 0)
  159. goto out;
  160. n = copy_to_user(ptr, ptrace_ldt.ptr, res);
  161. if(n != 0)
  162. res = -EFAULT;
  163. out:
  164. kfree(ptrace_ldt.ptr);
  165. return res;
  166. }
  167. /*
  168. * In skas mode, we hold our own ldt data in UML.
  169. * Thus, the code implementing sys_modify_ldt_skas
  170. * is very similar to (and mostly stolen from) sys_modify_ldt
  171. * for arch/i386/kernel/ldt.c
  172. * The routines copied and modified in part are:
  173. * - read_ldt
  174. * - read_default_ldt
  175. * - write_ldt
  176. * - sys_modify_ldt_skas
  177. */
  178. static int read_ldt(void __user * ptr, unsigned long bytecount)
  179. {
  180. int i, err = 0;
  181. unsigned long size;
  182. uml_ldt_t * ldt = &current->mm->context.skas.ldt;
  183. if(!ldt->entry_count)
  184. goto out;
  185. if(bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
  186. bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
  187. err = bytecount;
  188. if(ptrace_ldt){
  189. return read_ldt_from_host(ptr, bytecount);
  190. }
  191. down(&ldt->semaphore);
  192. if(ldt->entry_count <= LDT_DIRECT_ENTRIES){
  193. size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
  194. if(size > bytecount)
  195. size = bytecount;
  196. if(copy_to_user(ptr, ldt->u.entries, size))
  197. err = -EFAULT;
  198. bytecount -= size;
  199. ptr += size;
  200. }
  201. else {
  202. for(i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
  203. i++){
  204. size = PAGE_SIZE;
  205. if(size > bytecount)
  206. size = bytecount;
  207. if(copy_to_user(ptr, ldt->u.pages[i], size)){
  208. err = -EFAULT;
  209. break;
  210. }
  211. bytecount -= size;
  212. ptr += size;
  213. }
  214. }
  215. up(&ldt->semaphore);
  216. if(bytecount == 0 || err == -EFAULT)
  217. goto out;
  218. if(clear_user(ptr, bytecount))
  219. err = -EFAULT;
  220. out:
  221. return err;
  222. }
  223. static int read_default_ldt(void __user * ptr, unsigned long bytecount)
  224. {
  225. int err;
  226. if(bytecount > 5*LDT_ENTRY_SIZE)
  227. bytecount = 5*LDT_ENTRY_SIZE;
  228. err = bytecount;
  229. /* UML doesn't support lcall7 and lcall27.
  230. * So, we don't really have a default ldt, but emulate
  231. * an empty ldt of common host default ldt size.
  232. */
  233. if(clear_user(ptr, bytecount))
  234. err = -EFAULT;
  235. return err;
  236. }
  237. static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
  238. {
  239. uml_ldt_t * ldt = &current->mm->context.skas.ldt;
  240. struct mm_id * mm_idp = &current->mm->context.skas.id;
  241. int i, err;
  242. struct user_desc ldt_info;
  243. struct ldt_entry entry0, *ldt_p;
  244. void *addr = NULL;
  245. err = -EINVAL;
  246. if(bytecount != sizeof(ldt_info))
  247. goto out;
  248. err = -EFAULT;
  249. if(copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
  250. goto out;
  251. err = -EINVAL;
  252. if(ldt_info.entry_number >= LDT_ENTRIES)
  253. goto out;
  254. if(ldt_info.contents == 3){
  255. if (func == 1)
  256. goto out;
  257. if (ldt_info.seg_not_present == 0)
  258. goto out;
  259. }
  260. if(!ptrace_ldt)
  261. down(&ldt->semaphore);
  262. err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
  263. if(err)
  264. goto out_unlock;
  265. else if(ptrace_ldt) {
  266. /* With PTRACE_LDT available, this is used as a flag only */
  267. ldt->entry_count = 1;
  268. goto out;
  269. }
  270. if(ldt_info.entry_number >= ldt->entry_count &&
  271. ldt_info.entry_number >= LDT_DIRECT_ENTRIES){
  272. for(i=ldt->entry_count/LDT_ENTRIES_PER_PAGE;
  273. i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number;
  274. i++){
  275. if(i == 0)
  276. memcpy(&entry0, ldt->u.entries,
  277. sizeof(entry0));
  278. ldt->u.pages[i] = (struct ldt_entry *)
  279. __get_free_page(GFP_KERNEL|__GFP_ZERO);
  280. if(!ldt->u.pages[i]){
  281. err = -ENOMEM;
  282. /* Undo the change in host */
  283. memset(&ldt_info, 0, sizeof(ldt_info));
  284. write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1);
  285. goto out_unlock;
  286. }
  287. if(i == 0) {
  288. memcpy(ldt->u.pages[0], &entry0,
  289. sizeof(entry0));
  290. memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
  291. sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
  292. }
  293. ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
  294. }
  295. }
  296. if(ldt->entry_count <= ldt_info.entry_number)
  297. ldt->entry_count = ldt_info.entry_number + 1;
  298. if(ldt->entry_count <= LDT_DIRECT_ENTRIES)
  299. ldt_p = ldt->u.entries + ldt_info.entry_number;
  300. else
  301. ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
  302. ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
  303. if(ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
  304. (func == 1 || LDT_empty(&ldt_info))){
  305. ldt_p->a = 0;
  306. ldt_p->b = 0;
  307. }
  308. else{
  309. if (func == 1)
  310. ldt_info.useable = 0;
  311. ldt_p->a = LDT_entry_a(&ldt_info);
  312. ldt_p->b = LDT_entry_b(&ldt_info);
  313. }
  314. err = 0;
  315. out_unlock:
  316. up(&ldt->semaphore);
  317. out:
  318. return err;
  319. }
  320. static long do_modify_ldt_skas(int func, void __user *ptr,
  321. unsigned long bytecount)
  322. {
  323. int ret = -ENOSYS;
  324. switch (func) {
  325. case 0:
  326. ret = read_ldt(ptr, bytecount);
  327. break;
  328. case 1:
  329. case 0x11:
  330. ret = write_ldt(ptr, bytecount, func);
  331. break;
  332. case 2:
  333. ret = read_default_ldt(ptr, bytecount);
  334. break;
  335. }
  336. return ret;
  337. }
  338. short dummy_list[9] = {0, -1};
  339. short * host_ldt_entries = NULL;
  340. void ldt_get_host_info(void)
  341. {
  342. long ret;
  343. struct ldt_entry * ldt;
  344. int i, size, k, order;
  345. host_ldt_entries = dummy_list+1;
  346. for(i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++);
  347. ldt = (struct ldt_entry *)
  348. __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
  349. if(ldt == NULL) {
  350. printk("ldt_get_host_info: couldn't allocate buffer for host ldt\n");
  351. return;
  352. }
  353. ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
  354. if(ret < 0) {
  355. printk("ldt_get_host_info: couldn't read host ldt\n");
  356. goto out_free;
  357. }
  358. if(ret == 0) {
  359. /* default_ldt is active, simply write an empty entry 0 */
  360. host_ldt_entries = dummy_list;
  361. goto out_free;
  362. }
  363. for(i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++){
  364. if(ldt[i].a != 0 || ldt[i].b != 0)
  365. size++;
  366. }
  367. if(size < sizeof(dummy_list)/sizeof(dummy_list[0])) {
  368. host_ldt_entries = dummy_list;
  369. }
  370. else {
  371. size = (size + 1) * sizeof(dummy_list[0]);
  372. host_ldt_entries = (short *)kmalloc(size, GFP_KERNEL);
  373. if(host_ldt_entries == NULL) {
  374. printk("ldt_get_host_info: couldn't allocate host ldt list\n");
  375. goto out_free;
  376. }
  377. }
  378. for(i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++){
  379. if(ldt[i].a != 0 || ldt[i].b != 0) {
  380. host_ldt_entries[k++] = i;
  381. }
  382. }
  383. host_ldt_entries[k] = -1;
  384. out_free:
  385. free_pages((unsigned long)ldt, order);
  386. }
  387. long init_new_ldt(struct mmu_context_skas * new_mm,
  388. struct mmu_context_skas * from_mm)
  389. {
  390. struct user_desc desc;
  391. short * num_p;
  392. int i;
  393. long page, err=0;
  394. void *addr = NULL;
  395. struct proc_mm_op copy;
  396. if(!ptrace_ldt)
  397. init_MUTEX(&new_mm->ldt.semaphore);
  398. if(!from_mm){
  399. memset(&desc, 0, sizeof(desc));
  400. /*
  401. * We have to initialize a clean ldt.
  402. */
  403. if(proc_mm) {
  404. /*
  405. * If the new mm was created using proc_mm, host's
  406. * default-ldt currently is assigned, which normally
  407. * contains the call-gates for lcall7 and lcall27.
  408. * To remove these gates, we simply write an empty
  409. * entry as number 0 to the host.
  410. */
  411. err = write_ldt_entry(&new_mm->id, 1, &desc,
  412. &addr, 1);
  413. }
  414. else{
  415. /*
  416. * Now we try to retrieve info about the ldt, we
  417. * inherited from the host. All ldt-entries found
  418. * will be reset in the following loop
  419. */
  420. if(host_ldt_entries == NULL)
  421. ldt_get_host_info();
  422. for(num_p=host_ldt_entries; *num_p != -1; num_p++){
  423. desc.entry_number = *num_p;
  424. err = write_ldt_entry(&new_mm->id, 1, &desc,
  425. &addr, *(num_p + 1) == -1);
  426. if(err)
  427. break;
  428. }
  429. }
  430. new_mm->ldt.entry_count = 0;
  431. goto out;
  432. }
  433. if(proc_mm){
  434. /* We have a valid from_mm, so we now have to copy the LDT of
  435. * from_mm to new_mm, because using proc_mm an new mm with
  436. * an empty/default LDT was created in new_mm()
  437. */
  438. copy = ((struct proc_mm_op) { .op = MM_COPY_SEGMENTS,
  439. .u =
  440. { .copy_segments =
  441. from_mm->id.u.mm_fd } } );
  442. i = os_write_file(new_mm->id.u.mm_fd, &copy, sizeof(copy));
  443. if(i != sizeof(copy))
  444. printk("new_mm : /proc/mm copy_segments failed, "
  445. "err = %d\n", -i);
  446. }
  447. if(!ptrace_ldt) {
  448. /* Our local LDT is used to supply the data for
  449. * modify_ldt(READLDT), if PTRACE_LDT isn't available,
  450. * i.e., we have to use the stub for modify_ldt, which
  451. * can't handle the big read buffer of up to 64kB.
  452. */
  453. down(&from_mm->ldt.semaphore);
  454. if(from_mm->ldt.entry_count <= LDT_DIRECT_ENTRIES){
  455. memcpy(new_mm->ldt.u.entries, from_mm->ldt.u.entries,
  456. sizeof(new_mm->ldt.u.entries));
  457. }
  458. else{
  459. i = from_mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
  460. while(i-->0){
  461. page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
  462. if (!page){
  463. err = -ENOMEM;
  464. break;
  465. }
  466. new_mm->ldt.u.pages[i] =
  467. (struct ldt_entry *) page;
  468. memcpy(new_mm->ldt.u.pages[i],
  469. from_mm->ldt.u.pages[i], PAGE_SIZE);
  470. }
  471. }
  472. new_mm->ldt.entry_count = from_mm->ldt.entry_count;
  473. up(&from_mm->ldt.semaphore);
  474. }
  475. out:
  476. return err;
  477. }
  478. void free_ldt(struct mmu_context_skas * mm)
  479. {
  480. int i;
  481. if(!ptrace_ldt && mm->ldt.entry_count > LDT_DIRECT_ENTRIES){
  482. i = mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
  483. while(i-- > 0){
  484. free_page((long )mm->ldt.u.pages[i]);
  485. }
  486. }
  487. mm->ldt.entry_count = 0;
  488. }
  489. #endif
  490. int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
  491. {
  492. return(CHOOSE_MODE_PROC(do_modify_ldt_tt, do_modify_ldt_skas, func,
  493. ptr, bytecount));
  494. }