ldt.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #include <asm/syscalls.h>
  20. #ifdef CONFIG_SMP
  21. static void flush_ldt(void *current_mm)
  22. {
  23. if (current->active_mm == current_mm)
  24. load_LDT(&current->active_mm->context);
  25. }
  26. #endif
  27. static int alloc_ldt(mm_context_t *pc, int mincount, int reload)
  28. {
  29. void *oldldt, *newldt;
  30. int oldsize;
  31. if (mincount <= pc->size)
  32. return 0;
  33. oldsize = pc->size;
  34. mincount = (mincount + (PAGE_SIZE / LDT_ENTRY_SIZE - 1)) &
  35. (~(PAGE_SIZE / LDT_ENTRY_SIZE - 1));
  36. if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE)
  37. newldt = vmalloc(mincount * LDT_ENTRY_SIZE);
  38. else
  39. newldt = (void *)__get_free_page(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. #ifdef CONFIG_X86_64
  48. /* CHECKME: Do we really need this ? */
  49. wmb();
  50. #endif
  51. pc->ldt = newldt;
  52. wmb();
  53. pc->size = mincount;
  54. wmb();
  55. if (reload) {
  56. #ifdef CONFIG_SMP
  57. preempt_disable();
  58. load_LDT(pc);
  59. if (!cpus_equal(current->mm->cpu_vm_mask,
  60. cpumask_of_cpu(smp_processor_id())))
  61. smp_call_function(flush_ldt, current->mm, 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. put_page(virt_to_page(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. mutex_init(&mm->context.lock);
  92. mm->context.size = 0;
  93. old_mm = current->mm;
  94. if (old_mm && old_mm->context.size > 0) {
  95. mutex_lock(&old_mm->context.lock);
  96. retval = copy_ldt(&mm->context, &old_mm->context);
  97. mutex_unlock(&old_mm->context.lock);
  98. }
  99. return retval;
  100. }
  101. /*
  102. * No need to lock the MM as we are the last user
  103. *
  104. * 64bit: Don't touch the LDT register - we're already in the next thread.
  105. */
  106. void destroy_context(struct mm_struct *mm)
  107. {
  108. if (mm->context.size) {
  109. #ifdef CONFIG_X86_32
  110. /* CHECKME: Can this ever happen ? */
  111. if (mm == current->active_mm)
  112. clear_LDT();
  113. #endif
  114. if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
  115. vfree(mm->context.ldt);
  116. else
  117. put_page(virt_to_page(mm->context.ldt));
  118. mm->context.size = 0;
  119. }
  120. }
  121. static int read_ldt(void __user *ptr, unsigned long bytecount)
  122. {
  123. int err;
  124. unsigned long size;
  125. struct mm_struct *mm = current->mm;
  126. if (!mm->context.size)
  127. return 0;
  128. if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
  129. bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
  130. mutex_lock(&mm->context.lock);
  131. size = mm->context.size * LDT_ENTRY_SIZE;
  132. if (size > bytecount)
  133. size = bytecount;
  134. err = 0;
  135. if (copy_to_user(ptr, mm->context.ldt, size))
  136. err = -EFAULT;
  137. mutex_unlock(&mm->context.lock);
  138. if (err < 0)
  139. goto error_return;
  140. if (size != bytecount) {
  141. /* zero-fill the rest */
  142. if (clear_user(ptr + size, bytecount - size) != 0) {
  143. err = -EFAULT;
  144. goto error_return;
  145. }
  146. }
  147. return bytecount;
  148. error_return:
  149. return err;
  150. }
  151. static int read_default_ldt(void __user *ptr, unsigned long bytecount)
  152. {
  153. /* CHECKME: Can we use _one_ random number ? */
  154. #ifdef CONFIG_X86_32
  155. unsigned long size = 5 * sizeof(struct desc_struct);
  156. #else
  157. unsigned long size = 128;
  158. #endif
  159. if (bytecount > size)
  160. bytecount = size;
  161. if (clear_user(ptr, bytecount))
  162. return -EFAULT;
  163. return bytecount;
  164. }
  165. static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
  166. {
  167. struct mm_struct *mm = current->mm;
  168. struct desc_struct ldt;
  169. int error;
  170. struct user_desc ldt_info;
  171. error = -EINVAL;
  172. if (bytecount != sizeof(ldt_info))
  173. goto out;
  174. error = -EFAULT;
  175. if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
  176. goto out;
  177. error = -EINVAL;
  178. if (ldt_info.entry_number >= LDT_ENTRIES)
  179. goto out;
  180. if (ldt_info.contents == 3) {
  181. if (oldmode)
  182. goto out;
  183. if (ldt_info.seg_not_present == 0)
  184. goto out;
  185. }
  186. mutex_lock(&mm->context.lock);
  187. if (ldt_info.entry_number >= mm->context.size) {
  188. error = alloc_ldt(&current->mm->context,
  189. ldt_info.entry_number + 1, 1);
  190. if (error < 0)
  191. goto out_unlock;
  192. }
  193. /* Allow LDTs to be cleared by the user. */
  194. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  195. if (oldmode || LDT_empty(&ldt_info)) {
  196. memset(&ldt, 0, sizeof(ldt));
  197. goto install;
  198. }
  199. }
  200. fill_ldt(&ldt, &ldt_info);
  201. if (oldmode)
  202. ldt.avl = 0;
  203. /* Install the new entry ... */
  204. install:
  205. write_ldt_entry(mm->context.ldt, ldt_info.entry_number, &ldt);
  206. error = 0;
  207. out_unlock:
  208. mutex_unlock(&mm->context.lock);
  209. out:
  210. return error;
  211. }
  212. asmlinkage int sys_modify_ldt(int func, void __user *ptr,
  213. unsigned long bytecount)
  214. {
  215. int ret = -ENOSYS;
  216. switch (func) {
  217. case 0:
  218. ret = read_ldt(ptr, bytecount);
  219. break;
  220. case 1:
  221. ret = write_ldt(ptr, bytecount, 1);
  222. break;
  223. case 2:
  224. ret = read_default_ldt(ptr, bytecount);
  225. break;
  226. case 0x11:
  227. ret = write_ldt(ptr, bytecount, 0);
  228. break;
  229. }
  230. return ret;
  231. }