ldt_64.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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
  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,
  46. (mincount - oldsize) * LDT_ENTRY_SIZE);
  47. wmb();
  48. pc->ldt = newldt;
  49. wmb();
  50. pc->size = mincount;
  51. wmb();
  52. if (reload) {
  53. #ifdef CONFIG_SMP
  54. cpumask_t mask;
  55. preempt_disable();
  56. mask = cpumask_of_cpu(smp_processor_id());
  57. load_LDT(pc);
  58. if (!cpus_equal(current->mm->cpu_vm_mask, mask))
  59. smp_call_function(flush_ldt, NULL, 1, 1);
  60. preempt_enable();
  61. #else
  62. load_LDT(pc);
  63. #endif
  64. }
  65. if (oldsize) {
  66. if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
  67. vfree(oldldt);
  68. else
  69. kfree(oldldt);
  70. }
  71. return 0;
  72. }
  73. static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
  74. {
  75. int err = alloc_ldt(new, old->size, 0);
  76. if (err < 0)
  77. return err;
  78. memcpy(new->ldt, old->ldt, old->size * LDT_ENTRY_SIZE);
  79. return 0;
  80. }
  81. /*
  82. * we do not have to muck with descriptors here, that is
  83. * done in switch_mm() as needed.
  84. */
  85. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  86. {
  87. struct mm_struct *old_mm;
  88. int retval = 0;
  89. mutex_init(&mm->context.lock);
  90. mm->context.size = 0;
  91. old_mm = current->mm;
  92. if (old_mm && old_mm->context.size > 0) {
  93. mutex_lock(&old_mm->context.lock);
  94. retval = copy_ldt(&mm->context, &old_mm->context);
  95. mutex_unlock(&old_mm->context.lock);
  96. }
  97. return retval;
  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. mutex_lock(&mm->context.lock);
  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. mutex_unlock(&mm->context.lock);
  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;
  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. mutex_lock(&mm->context.lock);
  175. if (ldt_info.entry_number >= (unsigned)mm->context.size) {
  176. error = alloc_ldt(&current->mm->context,
  177. ldt_info.entry_number + 1, 1);
  178. if (error < 0)
  179. goto out_unlock;
  180. }
  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. write_ldt_entry(mm->context.ldt, ldt_info.entry_number, entry_1,
  196. entry_2);
  197. error = 0;
  198. out_unlock:
  199. mutex_unlock(&mm->context.lock);
  200. out:
  201. return error;
  202. }
  203. asmlinkage int sys_modify_ldt(int func, void __user *ptr,
  204. unsigned long bytecount)
  205. {
  206. int ret = -ENOSYS;
  207. switch (func) {
  208. case 0:
  209. ret = read_ldt(ptr, bytecount);
  210. break;
  211. case 1:
  212. ret = write_ldt(ptr, bytecount, 1);
  213. break;
  214. case 2:
  215. ret = read_default_ldt(ptr, bytecount);
  216. break;
  217. case 0x11:
  218. ret = write_ldt(ptr, bytecount, 0);
  219. break;
  220. }
  221. return ret;
  222. }