ldt_64.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
  3. * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
  4. * Copyright (C) 2002 Andi Kleen
  5. *
  6. * This handles calls from both 32bit and 64bit mode.
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/sched.h>
  10. #include <linux/string.h>
  11. #include <linux/mm.h>
  12. #include <linux/smp.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/slab.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/system.h>
  17. #include <asm/ldt.h>
  18. #include <asm/desc.h>
  19. #include <asm/proto.h>
  20. #ifdef CONFIG_SMP /* avoids "defined but not used" warnig */
  21. static void flush_ldt(void *null)
  22. {
  23. if (current->active_mm)
  24. load_LDT(&current->active_mm->context);
  25. }
  26. #endif
  27. static int alloc_ldt(mm_context_t *pc, unsigned mincount, int reload)
  28. {
  29. void *oldldt;
  30. void *newldt;
  31. unsigned oldsize;
  32. if (mincount <= (unsigned)pc->size)
  33. return 0;
  34. oldsize = pc->size;
  35. mincount = (mincount+511)&(~511);
  36. if (mincount*LDT_ENTRY_SIZE > PAGE_SIZE)
  37. newldt = vmalloc(mincount*LDT_ENTRY_SIZE);
  38. else
  39. newldt = kmalloc(mincount*LDT_ENTRY_SIZE, GFP_KERNEL);
  40. if (!newldt)
  41. return -ENOMEM;
  42. if (oldsize)
  43. memcpy(newldt, pc->ldt, oldsize*LDT_ENTRY_SIZE);
  44. oldldt = pc->ldt;
  45. memset(newldt+oldsize*LDT_ENTRY_SIZE, 0, (mincount-oldsize)*LDT_ENTRY_SIZE);
  46. wmb();
  47. pc->ldt = newldt;
  48. wmb();
  49. pc->size = mincount;
  50. wmb();
  51. if (reload) {
  52. #ifdef CONFIG_SMP
  53. cpumask_t mask;
  54. preempt_disable();
  55. mask = cpumask_of_cpu(smp_processor_id());
  56. load_LDT(pc);
  57. if (!cpus_equal(current->mm->cpu_vm_mask, mask))
  58. smp_call_function(flush_ldt, NULL, 1, 1);
  59. preempt_enable();
  60. #else
  61. load_LDT(pc);
  62. #endif
  63. }
  64. if (oldsize) {
  65. if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
  66. vfree(oldldt);
  67. else
  68. kfree(oldldt);
  69. }
  70. return 0;
  71. }
  72. static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
  73. {
  74. int err = alloc_ldt(new, old->size, 0);
  75. if (err < 0)
  76. return err;
  77. memcpy(new->ldt, old->ldt, old->size*LDT_ENTRY_SIZE);
  78. return 0;
  79. }
  80. /*
  81. * we do not have to muck with descriptors here, that is
  82. * done in switch_mm() as needed.
  83. */
  84. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  85. {
  86. struct mm_struct * old_mm;
  87. int retval = 0;
  88. init_MUTEX(&mm->context.sem);
  89. mm->context.size = 0;
  90. old_mm = current->mm;
  91. if (old_mm && old_mm->context.size > 0) {
  92. down(&old_mm->context.sem);
  93. retval = copy_ldt(&mm->context, &old_mm->context);
  94. up(&old_mm->context.sem);
  95. }
  96. return retval;
  97. }
  98. /*
  99. *
  100. * Don't touch the LDT register - we're already in the next thread.
  101. */
  102. void destroy_context(struct mm_struct *mm)
  103. {
  104. if (mm->context.size) {
  105. if ((unsigned)mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE)
  106. vfree(mm->context.ldt);
  107. else
  108. kfree(mm->context.ldt);
  109. mm->context.size = 0;
  110. }
  111. }
  112. static int read_ldt(void __user * ptr, unsigned long bytecount)
  113. {
  114. int err;
  115. unsigned long size;
  116. struct mm_struct * mm = current->mm;
  117. if (!mm->context.size)
  118. return 0;
  119. if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
  120. bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
  121. down(&mm->context.sem);
  122. size = mm->context.size*LDT_ENTRY_SIZE;
  123. if (size > bytecount)
  124. size = bytecount;
  125. err = 0;
  126. if (copy_to_user(ptr, mm->context.ldt, size))
  127. err = -EFAULT;
  128. up(&mm->context.sem);
  129. if (err < 0)
  130. goto error_return;
  131. if (size != bytecount) {
  132. /* zero-fill the rest */
  133. if (clear_user(ptr+size, bytecount-size) != 0) {
  134. err = -EFAULT;
  135. goto error_return;
  136. }
  137. }
  138. return bytecount;
  139. error_return:
  140. return err;
  141. }
  142. static int read_default_ldt(void __user * ptr, unsigned long bytecount)
  143. {
  144. /* Arbitrary number */
  145. /* x86-64 default LDT is all zeros */
  146. if (bytecount > 128)
  147. bytecount = 128;
  148. if (clear_user(ptr, bytecount))
  149. return -EFAULT;
  150. return bytecount;
  151. }
  152. static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode)
  153. {
  154. struct task_struct *me = current;
  155. struct mm_struct * mm = me->mm;
  156. __u32 entry_1, entry_2, *lp;
  157. int error;
  158. struct user_desc ldt_info;
  159. error = -EINVAL;
  160. if (bytecount != sizeof(ldt_info))
  161. goto out;
  162. error = -EFAULT;
  163. if (copy_from_user(&ldt_info, ptr, bytecount))
  164. goto out;
  165. error = -EINVAL;
  166. if (ldt_info.entry_number >= LDT_ENTRIES)
  167. goto out;
  168. if (ldt_info.contents == 3) {
  169. if (oldmode)
  170. goto out;
  171. if (ldt_info.seg_not_present == 0)
  172. goto out;
  173. }
  174. down(&mm->context.sem);
  175. if (ldt_info.entry_number >= (unsigned)mm->context.size) {
  176. error = alloc_ldt(&current->mm->context, ldt_info.entry_number+1, 1);
  177. if (error < 0)
  178. goto out_unlock;
  179. }
  180. lp = (__u32 *) ((ldt_info.entry_number << 3) + (char *) mm->context.ldt);
  181. /* Allow LDTs to be cleared by the user. */
  182. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  183. if (oldmode || LDT_empty(&ldt_info)) {
  184. entry_1 = 0;
  185. entry_2 = 0;
  186. goto install;
  187. }
  188. }
  189. entry_1 = LDT_entry_a(&ldt_info);
  190. entry_2 = LDT_entry_b(&ldt_info);
  191. if (oldmode)
  192. entry_2 &= ~(1 << 20);
  193. /* Install the new entry ... */
  194. install:
  195. *lp = entry_1;
  196. *(lp+1) = entry_2;
  197. error = 0;
  198. out_unlock:
  199. up(&mm->context.sem);
  200. out:
  201. return error;
  202. }
  203. asmlinkage int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
  204. {
  205. int ret = -ENOSYS;
  206. switch (func) {
  207. case 0:
  208. ret = read_ldt(ptr, bytecount);
  209. break;
  210. case 1:
  211. ret = write_ldt(ptr, bytecount, 1);
  212. break;
  213. case 2:
  214. ret = read_default_ldt(ptr, bytecount);
  215. break;
  216. case 0x11:
  217. ret = write_ldt(ptr, bytecount, 0);
  218. break;
  219. }
  220. return ret;
  221. }