ldt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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->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->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->entries, sizeof(entry0));
  278. ldt->pages[i] = (struct ldt_entry *)
  279. __get_free_page(GFP_KERNEL|__GFP_ZERO);
  280. if(!ldt->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->pages[0], &entry0, sizeof(entry0));
  289. memcpy(ldt->pages[0]+1, ldt->entries+1,
  290. sizeof(entry0)*(LDT_DIRECT_ENTRIES-1));
  291. }
  292. ldt->entry_count = (i + 1) * LDT_ENTRIES_PER_PAGE;
  293. }
  294. }
  295. if(ldt->entry_count <= ldt_info.entry_number)
  296. ldt->entry_count = ldt_info.entry_number + 1;
  297. if(ldt->entry_count <= LDT_DIRECT_ENTRIES)
  298. ldt_p = ldt->entries + ldt_info.entry_number;
  299. else
  300. ldt_p = ldt->pages[ldt_info.entry_number/LDT_ENTRIES_PER_PAGE] +
  301. ldt_info.entry_number%LDT_ENTRIES_PER_PAGE;
  302. if(ldt_info.base_addr == 0 && ldt_info.limit == 0 &&
  303. (func == 1 || LDT_empty(&ldt_info))){
  304. ldt_p->a = 0;
  305. ldt_p->b = 0;
  306. }
  307. else{
  308. if (func == 1)
  309. ldt_info.useable = 0;
  310. ldt_p->a = LDT_entry_a(&ldt_info);
  311. ldt_p->b = LDT_entry_b(&ldt_info);
  312. }
  313. err = 0;
  314. out_unlock:
  315. up(&ldt->semaphore);
  316. out:
  317. return err;
  318. }
  319. static long do_modify_ldt_skas(int func, void __user *ptr,
  320. unsigned long bytecount)
  321. {
  322. int ret = -ENOSYS;
  323. switch (func) {
  324. case 0:
  325. ret = read_ldt(ptr, bytecount);
  326. break;
  327. case 1:
  328. case 0x11:
  329. ret = write_ldt(ptr, bytecount, func);
  330. break;
  331. case 2:
  332. ret = read_default_ldt(ptr, bytecount);
  333. break;
  334. }
  335. return ret;
  336. }
  337. short dummy_list[9] = {0, -1};
  338. short * host_ldt_entries = NULL;
  339. void ldt_get_host_info(void)
  340. {
  341. long ret;
  342. struct ldt_entry * ldt;
  343. int i, size, k, order;
  344. host_ldt_entries = dummy_list+1;
  345. for(i = LDT_PAGES_MAX-1, order=0; i; i>>=1, order++);
  346. ldt = (struct ldt_entry *)
  347. __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
  348. if(ldt == NULL) {
  349. printk("ldt_get_host_info: couldn't allocate buffer for host ldt\n");
  350. return;
  351. }
  352. ret = modify_ldt(0, ldt, (1<<order)*PAGE_SIZE);
  353. if(ret < 0) {
  354. printk("ldt_get_host_info: couldn't read host ldt\n");
  355. goto out_free;
  356. }
  357. if(ret == 0) {
  358. /* default_ldt is active, simply write an empty entry 0 */
  359. host_ldt_entries = dummy_list;
  360. goto out_free;
  361. }
  362. for(i=0, size=0; i<ret/LDT_ENTRY_SIZE; i++){
  363. if(ldt[i].a != 0 || ldt[i].b != 0)
  364. size++;
  365. }
  366. if(size < sizeof(dummy_list)/sizeof(dummy_list[0])) {
  367. host_ldt_entries = dummy_list;
  368. }
  369. else {
  370. size = (size + 1) * sizeof(dummy_list[0]);
  371. host_ldt_entries = (short *)kmalloc(size, GFP_KERNEL);
  372. if(host_ldt_entries == NULL) {
  373. printk("ldt_get_host_info: couldn't allocate host ldt list\n");
  374. goto out_free;
  375. }
  376. }
  377. for(i=0, k=0; i<ret/LDT_ENTRY_SIZE; i++){
  378. if(ldt[i].a != 0 || ldt[i].b != 0) {
  379. host_ldt_entries[k++] = i;
  380. }
  381. }
  382. host_ldt_entries[k] = -1;
  383. out_free:
  384. free_pages((unsigned long)ldt, order);
  385. }
  386. long init_new_ldt(struct mmu_context_skas * new_mm,
  387. struct mmu_context_skas * from_mm)
  388. {
  389. struct user_desc desc;
  390. short * num_p;
  391. int i;
  392. long page, err=0;
  393. void *addr = NULL;
  394. memset(&desc, 0, sizeof(desc));
  395. if(!ptrace_ldt)
  396. init_MUTEX(&new_mm->ldt.semaphore);
  397. if(!from_mm){
  398. /*
  399. * We have to initialize a clean ldt.
  400. */
  401. if(proc_mm) {
  402. /*
  403. * If the new mm was created using proc_mm, host's
  404. * default-ldt currently is assigned, which normally
  405. * contains the call-gates for lcall7 and lcall27.
  406. * To remove these gates, we simply write an empty
  407. * entry as number 0 to the host.
  408. */
  409. err = write_ldt_entry(&new_mm->id, 1, &desc,
  410. &addr, 1);
  411. }
  412. else{
  413. /*
  414. * Now we try to retrieve info about the ldt, we
  415. * inherited from the host. All ldt-entries found
  416. * will be reset in the following loop
  417. */
  418. if(host_ldt_entries == NULL)
  419. ldt_get_host_info();
  420. for(num_p=host_ldt_entries; *num_p != -1; num_p++){
  421. desc.entry_number = *num_p;
  422. err = write_ldt_entry(&new_mm->id, 1, &desc,
  423. &addr, *(num_p + 1) == -1);
  424. if(err)
  425. break;
  426. }
  427. }
  428. new_mm->ldt.entry_count = 0;
  429. }
  430. else if (!ptrace_ldt) {
  431. /* Our local LDT is used to supply the data for
  432. * modify_ldt(READLDT), if PTRACE_LDT isn't available,
  433. * i.e., we have to use the stub for modify_ldt, which
  434. * can't handle the big read buffer of up to 64kB.
  435. */
  436. down(&from_mm->ldt.semaphore);
  437. if(from_mm->ldt.entry_count <= LDT_DIRECT_ENTRIES){
  438. memcpy(new_mm->ldt.entries, from_mm->ldt.entries,
  439. sizeof(new_mm->ldt.entries));
  440. }
  441. else{
  442. i = from_mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
  443. while(i-->0){
  444. page = __get_free_page(GFP_KERNEL|__GFP_ZERO);
  445. if (!page){
  446. err = -ENOMEM;
  447. break;
  448. }
  449. new_mm->ldt.pages[i] = (struct ldt_entry*)page;
  450. memcpy(new_mm->ldt.pages[i],
  451. from_mm->ldt.pages[i], PAGE_SIZE);
  452. }
  453. }
  454. new_mm->ldt.entry_count = from_mm->ldt.entry_count;
  455. up(&from_mm->ldt.semaphore);
  456. }
  457. return err;
  458. }
  459. void free_ldt(struct mmu_context_skas * mm)
  460. {
  461. int i;
  462. if(!ptrace_ldt && mm->ldt.entry_count > LDT_DIRECT_ENTRIES){
  463. i = mm->ldt.entry_count / LDT_ENTRIES_PER_PAGE;
  464. while(i-- > 0){
  465. free_page((long )mm->ldt.pages[i]);
  466. }
  467. }
  468. mm->ldt.entry_count = 0;
  469. }
  470. #endif
  471. int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
  472. {
  473. return(CHOOSE_MODE_PROC(do_modify_ldt_tt, do_modify_ldt_skas, func,
  474. ptr, bytecount));
  475. }