ldt.c 13 KB

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