ldt.c 5.3 KB

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