ldt.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 <asm/uaccess.h>
  15. #include <asm/system.h>
  16. #include <asm/ldt.h>
  17. #include <asm/desc.h>
  18. #include <asm/mmu_context.h>
  19. #ifdef CONFIG_SMP
  20. static void flush_ldt(void *current_mm)
  21. {
  22. if (current->active_mm == current_mm)
  23. load_LDT(&current->active_mm->context);
  24. }
  25. #endif
  26. static int alloc_ldt(mm_context_t *pc, int mincount, int reload)
  27. {
  28. void *oldldt, *newldt;
  29. int oldsize;
  30. if (mincount <= pc->size)
  31. return 0;
  32. oldsize = pc->size;
  33. mincount = (mincount + (PAGE_SIZE / LDT_ENTRY_SIZE - 1)) &
  34. (~(PAGE_SIZE / LDT_ENTRY_SIZE - 1));
  35. if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE)
  36. newldt = vmalloc(mincount * LDT_ENTRY_SIZE);
  37. else
  38. newldt = (void *)__get_free_page(GFP_KERNEL);
  39. if (!newldt)
  40. return -ENOMEM;
  41. if (oldsize)
  42. memcpy(newldt, pc->ldt, oldsize * LDT_ENTRY_SIZE);
  43. oldldt = pc->ldt;
  44. memset(newldt + oldsize * LDT_ENTRY_SIZE, 0,
  45. (mincount - oldsize) * LDT_ENTRY_SIZE);
  46. #ifdef CONFIG_X86_64
  47. /* CHECKME: Do we really need this ? */
  48. wmb();
  49. #endif
  50. pc->ldt = newldt;
  51. wmb();
  52. pc->size = mincount;
  53. wmb();
  54. if (reload) {
  55. #ifdef CONFIG_SMP
  56. preempt_disable();
  57. load_LDT(pc);
  58. if (!cpus_equal(current->mm->cpu_vm_mask,
  59. cpumask_of_cpu(smp_processor_id())))
  60. smp_call_function(flush_ldt, current->mm, 1);
  61. preempt_enable();
  62. #else
  63. load_LDT(pc);
  64. #endif
  65. }
  66. if (oldsize) {
  67. if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
  68. vfree(oldldt);
  69. else
  70. put_page(virt_to_page(oldldt));
  71. }
  72. return 0;
  73. }
  74. static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
  75. {
  76. int err = alloc_ldt(new, old->size, 0);
  77. if (err < 0)
  78. return err;
  79. memcpy(new->ldt, old->ldt, old->size * LDT_ENTRY_SIZE);
  80. return 0;
  81. }
  82. /*
  83. * we do not have to muck with descriptors here, that is
  84. * done in switch_mm() as needed.
  85. */
  86. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  87. {
  88. struct mm_struct *old_mm;
  89. int retval = 0;
  90. mutex_init(&mm->context.lock);
  91. mm->context.size = 0;
  92. old_mm = current->mm;
  93. if (old_mm && old_mm->context.size > 0) {
  94. mutex_lock(&old_mm->context.lock);
  95. retval = copy_ldt(&mm->context, &old_mm->context);
  96. mutex_unlock(&old_mm->context.lock);
  97. }
  98. return retval;
  99. }
  100. /*
  101. * No need to lock the MM as we are the last user
  102. *
  103. * 64bit: 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. #ifdef CONFIG_X86_32
  109. /* CHECKME: Can this ever happen ? */
  110. if (mm == current->active_mm)
  111. clear_LDT();
  112. #endif
  113. if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
  114. vfree(mm->context.ldt);
  115. else
  116. put_page(virt_to_page(mm->context.ldt));
  117. mm->context.size = 0;
  118. }
  119. }
  120. static int read_ldt(void __user *ptr, unsigned long bytecount)
  121. {
  122. int err;
  123. unsigned long size;
  124. struct mm_struct *mm = current->mm;
  125. if (!mm->context.size)
  126. return 0;
  127. if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
  128. bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
  129. mutex_lock(&mm->context.lock);
  130. size = mm->context.size * LDT_ENTRY_SIZE;
  131. if (size > bytecount)
  132. size = bytecount;
  133. err = 0;
  134. if (copy_to_user(ptr, mm->context.ldt, size))
  135. err = -EFAULT;
  136. mutex_unlock(&mm->context.lock);
  137. if (err < 0)
  138. goto error_return;
  139. if (size != bytecount) {
  140. /* zero-fill the rest */
  141. if (clear_user(ptr + size, bytecount - size) != 0) {
  142. err = -EFAULT;
  143. goto error_return;
  144. }
  145. }
  146. return bytecount;
  147. error_return:
  148. return err;
  149. }
  150. static int read_default_ldt(void __user *ptr, unsigned long bytecount)
  151. {
  152. /* CHECKME: Can we use _one_ random number ? */
  153. #ifdef CONFIG_X86_32
  154. unsigned long size = 5 * sizeof(struct desc_struct);
  155. #else
  156. unsigned long size = 128;
  157. #endif
  158. if (bytecount > size)
  159. bytecount = size;
  160. if (clear_user(ptr, bytecount))
  161. return -EFAULT;
  162. return bytecount;
  163. }
  164. static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
  165. {
  166. struct mm_struct *mm = current->mm;
  167. struct desc_struct ldt;
  168. int error;
  169. struct user_desc ldt_info;
  170. error = -EINVAL;
  171. if (bytecount != sizeof(ldt_info))
  172. goto out;
  173. error = -EFAULT;
  174. if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
  175. goto out;
  176. error = -EINVAL;
  177. if (ldt_info.entry_number >= LDT_ENTRIES)
  178. goto out;
  179. if (ldt_info.contents == 3) {
  180. if (oldmode)
  181. goto out;
  182. if (ldt_info.seg_not_present == 0)
  183. goto out;
  184. }
  185. mutex_lock(&mm->context.lock);
  186. if (ldt_info.entry_number >= mm->context.size) {
  187. error = alloc_ldt(&current->mm->context,
  188. ldt_info.entry_number + 1, 1);
  189. if (error < 0)
  190. goto out_unlock;
  191. }
  192. /* Allow LDTs to be cleared by the user. */
  193. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  194. if (oldmode || LDT_empty(&ldt_info)) {
  195. memset(&ldt, 0, sizeof(ldt));
  196. goto install;
  197. }
  198. }
  199. fill_ldt(&ldt, &ldt_info);
  200. if (oldmode)
  201. ldt.avl = 0;
  202. /* Install the new entry ... */
  203. install:
  204. write_ldt_entry(mm->context.ldt, ldt_info.entry_number, &ldt);
  205. error = 0;
  206. out_unlock:
  207. mutex_unlock(&mm->context.lock);
  208. out:
  209. return error;
  210. }
  211. asmlinkage int sys_modify_ldt(int func, void __user *ptr,
  212. unsigned long bytecount)
  213. {
  214. int ret = -ENOSYS;
  215. switch (func) {
  216. case 0:
  217. ret = read_ldt(ptr, bytecount);
  218. break;
  219. case 1:
  220. ret = write_ldt(ptr, bytecount, 1);
  221. break;
  222. case 2:
  223. ret = read_default_ldt(ptr, bytecount);
  224. break;
  225. case 0x11:
  226. ret = write_ldt(ptr, bytecount, 0);
  227. break;
  228. }
  229. return ret;
  230. }