ldt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. extern int modify_ldt(int func, void *ptr, unsigned long bytecount);
  19. #ifdef CONFIG_MODE_TT
  20. static long do_modify_ldt_tt(int func, void __user *ptr,
  21. unsigned long bytecount)
  22. {
  23. struct user_desc info;
  24. int res = 0;
  25. void *buf = NULL;
  26. void *p = NULL; /* What we pass to host. */
  27. switch(func){
  28. case 1:
  29. case 0x11: /* write_ldt */
  30. /* Do this check now to avoid overflows. */
  31. if (bytecount != sizeof(struct user_desc)) {
  32. res = -EINVAL;
  33. goto out;
  34. }
  35. if(copy_from_user(&info, ptr, sizeof(info))) {
  36. res = -EFAULT;
  37. goto out;
  38. }
  39. p = &info;
  40. break;
  41. case 0:
  42. case 2: /* read_ldt */
  43. /* The use of info avoids kmalloc on the write case, not on the
  44. * read one. */
  45. buf = kmalloc(bytecount, GFP_KERNEL);
  46. if (!buf) {
  47. res = -ENOMEM;
  48. goto out;
  49. }
  50. p = buf;
  51. break;
  52. default:
  53. res = -ENOSYS;
  54. goto out;
  55. }
  56. res = modify_ldt(func, p, bytecount);
  57. if(res < 0)
  58. goto out;
  59. switch(func){
  60. case 0:
  61. case 2:
  62. /* Modify_ldt was for reading and returned the number of read
  63. * bytes.*/
  64. if(copy_to_user(ptr, p, res))
  65. res = -EFAULT;
  66. break;
  67. }
  68. out:
  69. kfree(buf);
  70. return res;
  71. }
  72. #endif
  73. #ifdef CONFIG_MODE_SKAS
  74. #include "skas.h"
  75. #include "skas_ptrace.h"
  76. #include "asm/mmu_context.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 unshure: 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 = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
  112. if(res)
  113. res = errno;
  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 = ptrace(PTRACE_LDT, userspace_pid[cpu], 0,
  157. (unsigned long) &ptrace_ldt);
  158. put_cpu();
  159. if(res < 0)
  160. goto out;
  161. n = copy_to_user(ptr, ptrace_ldt.ptr, res);
  162. if(n != 0)
  163. res = -EFAULT;
  164. out:
  165. kfree(ptrace_ldt.ptr);
  166. return res;
  167. }
  168. /*
  169. * In skas mode, we hold our own ldt data in UML.
  170. * Thus, the code implementing sys_modify_ldt_skas
  171. * is very similar to (and mostly stolen from) sys_modify_ldt
  172. * for arch/i386/kernel/ldt.c
  173. * The routines copied and modified in part are:
  174. * - read_ldt
  175. * - read_default_ldt
  176. * - write_ldt
  177. * - sys_modify_ldt_skas
  178. */
  179. static int read_ldt(void __user * ptr, unsigned long bytecount)
  180. {
  181. int i, err = 0;
  182. unsigned long size;
  183. uml_ldt_t * ldt = &current->mm->context.skas.ldt;
  184. if(!ldt->entry_count)
  185. goto out;
  186. if(bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
  187. bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
  188. err = bytecount;
  189. if(ptrace_ldt){
  190. return read_ldt_from_host(ptr, bytecount);
  191. }
  192. down(&ldt->semaphore);
  193. if(ldt->entry_count <= LDT_DIRECT_ENTRIES){
  194. size = LDT_ENTRY_SIZE*LDT_DIRECT_ENTRIES;
  195. if(size > bytecount)
  196. size = bytecount;
  197. if(copy_to_user(ptr, ldt->u.entries, size))
  198. err = -EFAULT;
  199. bytecount -= size;
  200. ptr += size;
  201. }
  202. else {
  203. for(i=0; i<ldt->entry_count/LDT_ENTRIES_PER_PAGE && bytecount;
  204. i++){
  205. size = PAGE_SIZE;
  206. if(size > bytecount)
  207. size = bytecount;
  208. if(copy_to_user(ptr, ldt->u.pages[i], size)){
  209. err = -EFAULT;
  210. break;
  211. }
  212. bytecount -= size;
  213. ptr += size;
  214. }
  215. }
  216. up(&ldt->semaphore);
  217. if(bytecount == 0 || err == -EFAULT)
  218. goto out;
  219. if(clear_user(ptr, bytecount))
  220. err = -EFAULT;
  221. out:
  222. return err;
  223. }
  224. static int read_default_ldt(void __user * ptr, unsigned long bytecount)
  225. {
  226. int err;
  227. if(bytecount > 5*LDT_ENTRY_SIZE)
  228. bytecount = 5*LDT_ENTRY_SIZE;
  229. err = bytecount;
  230. /* UML doesn't support lcall7 and lcall27.
  231. * So, we don't really have a default ldt, but emulate
  232. * an empty ldt of common host default ldt size.
  233. */
  234. if(clear_user(ptr, bytecount))
  235. err = -EFAULT;
  236. return err;
  237. }
  238. static int write_ldt(void __user * ptr, unsigned long bytecount, int func)
  239. {
  240. uml_ldt_t * ldt = &current->mm->context.skas.ldt;
  241. struct mm_id * mm_idp = &current->mm->context.skas.id;
  242. int i, err;
  243. struct user_desc ldt_info;
  244. struct ldt_entry entry0, *ldt_p;
  245. void *addr = NULL;
  246. err = -EINVAL;
  247. if(bytecount != sizeof(ldt_info))
  248. goto out;
  249. err = -EFAULT;
  250. if(copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
  251. goto out;
  252. err = -EINVAL;
  253. if(ldt_info.entry_number >= LDT_ENTRIES)
  254. goto out;
  255. if(ldt_info.contents == 3){
  256. if (func == 1)
  257. goto out;
  258. if (ldt_info.seg_not_present == 0)
  259. goto out;
  260. }
  261. if(!ptrace_ldt)
  262. down(&ldt->semaphore);
  263. err = write_ldt_entry(mm_idp, func, &ldt_info, &addr, 1);
  264. if(err)
  265. goto out_unlock;
  266. else if(ptrace_ldt) {
  267. /* With PTRACE_LDT available, this is used as a flag only */
  268. ldt->entry_count = 1;
  269. goto out;
  270. }
  271. if(ldt_info.entry_number >= ldt->entry_count &&
  272. ldt_info.entry_number >= LDT_DIRECT_ENTRIES){
  273. for(i=ldt->entry_count/LDT_ENTRIES_PER_PAGE;
  274. i*LDT_ENTRIES_PER_PAGE <= ldt_info.entry_number;
  275. i++){
  276. if(i == 0)
  277. memcpy(&entry0, ldt->u.entries,
  278. sizeof(entry0));
  279. ldt->u.pages[i] = (struct ldt_entry *)
  280. __get_free_page(GFP_KERNEL|__GFP_ZERO);
  281. if(!ldt->u.pages[i]){
  282. err = -ENOMEM;
  283. /* Undo the change in host */
  284. memset(&ldt_info, 0, sizeof(ldt_info));
  285. write_ldt_entry(mm_idp, 1, &ldt_info, &addr, 1);
  286. goto out_unlock;
  287. }
  288. if(i == 0) {
  289. memcpy(ldt->u.pages[0], &entry0,
  290. sizeof(entry0));
  291. memcpy(ldt->u.pages[0]+1, ldt->u.entries+1,
  292. sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
  293. }
  294. ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
  295. }
  296. }
  297. if(ldt->entry_count <= ldt_info.entry_number)
  298. ldt->entry_count = ldt_info.entry_number + 1;
  299. if(ldt->entry_count <= LDT_DIRECT_ENTRIES)
  300. ldt_p = ldt->u.entries + ldt_info.entry_number;
  301. else
  302. ldt_p = ldt->u.pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
  303. ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
  304. if(ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
  305. (func == 1 || LDT_empty(&ldt_info))){
  306. ldt_p->a = 0;
  307. ldt_p->b = 0;
  308. }
  309. else{
  310. if (func == 1)
  311. ldt_info.useable = 0;
  312. ldt_p->a = LDT_entry_a(&ldt_info);
  313. ldt_p->b = LDT_entry_b(&ldt_info);
  314. }
  315. err = 0;
  316. out_unlock:
  317. up(&ldt->semaphore);
  318. out:
  319. return err;
  320. }
  321. static long do_modify_ldt_skas(int func, void __user *ptr,
  322. unsigned long bytecount)
  323. {
  324. int ret = -ENOSYS;
  325. switch (func) {
  326. case 0:
  327. ret = read_ldt(ptr, bytecount);
  328. break;
  329. case 1:
  330. case 0x11:
  331. ret = write_ldt(ptr, bytecount, func);
  332. break;
  333. case 2:
  334. ret = read_default_ldt(ptr, bytecount);
  335. break;
  336. }
  337. return ret;
  338. }
  339. short dummy_list[9] = {0, -1};
  340. short * host_ldt_entries = NULL;
  341. void ldt_get_host_info(void)
  342. {
  343. long ret;
  344. struct ldt_entry * ldt;
  345. int i, size, k, order;
  346. host_ldt_entries = dummy_list+1;
  347. for(i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++);
  348. ldt = (struct ldt_entry *)
  349. __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
  350. if(ldt == NULL) {
  351. printk("ldt_get_host_info: couldn't allocate buffer for host ldt\n");
  352. return;
  353. }
  354. ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
  355. if(ret < 0) {
  356. printk("ldt_get_host_info: couldn't read host ldt\n");
  357. goto out_free;
  358. }
  359. if(ret == 0) {
  360. /* default_ldt is active, simply write an empty entry 0 */
  361. host_ldt_entries = dummy_list;
  362. goto out_free;
  363. }
  364. for(i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++){
  365. if(ldt[i].a != 0 || ldt[i].b != 0)
  366. size++;
  367. }
  368. if(size < sizeof(dummy_list)/sizeof(dummy_list[0])) {
  369. host_ldt_entries = dummy_list;
  370. }
  371. else {
  372. size = (size + 1) * sizeof(dummy_list[0]);
  373. host_ldt_entries = (short *)kmalloc(size, GFP_KERNEL);
  374. if(host_ldt_entries == NULL) {
  375. printk("ldt_get_host_info: couldn't allocate host ldt list\n");
  376. goto out_free;
  377. }
  378. }
  379. for(i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++){
  380. if(ldt[i].a != 0 || ldt[i].b != 0) {
  381. host_ldt_entries[k++] = i;
  382. }
  383. }
  384. host_ldt_entries[k] = -1;
  385. out_free:
  386. free_pages((unsigned long)ldt, order);
  387. }
  388. long init_new_ldt(struct mmu_context_skas * new_mm,
  389. struct mmu_context_skas * from_mm)
  390. {
  391. struct user_desc desc;
  392. short * num_p;
  393. int i;
  394. long page, err=0;
  395. void *addr = NULL;
  396. memset(&desc, 0, sizeof(desc));
  397. if(!ptrace_ldt)
  398. init_MUTEX(&new_mm->ldt.semaphore);
  399. if(!from_mm){
  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. }
  432. else if (!ptrace_ldt) {
  433. /* Our local LDT is used to supply the data for
  434. * modify_ldt(READLDT), if PTRACE_LDT isn't available,
  435. * i.e., we have to use the stub for modify_ldt, which
  436. * can't handle the big read buffer of up to 64kB.
  437. */
  438. down(&from_mm->ldt.semaphore);
  439. if(from_mm->ldt.entry_count <= LDT_DIRECT_ENTRIES){
  440. memcpy(new_mm->ldt.u.entries, from_mm->ldt.u.entries,
  441. sizeof(new_mm->ldt.u.entries));
  442. }
  443. else{
  444. i = from_mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
  445. while(i-->0){
  446. page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
  447. if (!page){
  448. err = -ENOMEM;
  449. break;
  450. }
  451. new_mm->ldt.u.pages[i] =
  452. (struct ldt_entry *) page;
  453. memcpy(new_mm->ldt.u.pages[i],
  454. from_mm->ldt.u.pages[i], PAGE_SIZE);
  455. }
  456. }
  457. new_mm->ldt.entry_count = from_mm->ldt.entry_count;
  458. up(&from_mm->ldt.semaphore);
  459. }
  460. return err;
  461. }
  462. void free_ldt(struct mmu_context_skas * mm)
  463. {
  464. int i;
  465. if(!ptrace_ldt && mm->ldt.entry_count > LDT_DIRECT_ENTRIES){
  466. i = mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
  467. while(i-- > 0){
  468. free_page((long )mm->ldt.u.pages[i]);
  469. }
  470. }
  471. mm->ldt.entry_count = 0;
  472. }
  473. #endif
  474. int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
  475. {
  476. return(CHOOSE_MODE_PROC(do_modify_ldt_tt, do_modify_ldt_skas, func,
  477. ptr, bytecount));
  478. }