ldt.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/gfp.h>
  10. #include <linux/sched.h>
  11. #include <linux/string.h>
  12. #include <linux/mm.h>
  13. #include <linux/smp.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/uaccess.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. paravirt_alloc_ldt(newldt, mincount);
  48. #ifdef CONFIG_X86_64
  49. /* CHECKME: Do we really need this ? */
  50. wmb();
  51. #endif
  52. pc->ldt = newldt;
  53. wmb();
  54. pc->size = mincount;
  55. wmb();
  56. if (reload) {
  57. #ifdef CONFIG_SMP
  58. preempt_disable();
  59. load_LDT(pc);
  60. if (!cpumask_equal(mm_cpumask(current->mm),
  61. cpumask_of(smp_processor_id())))
  62. smp_call_function(flush_ldt, current->mm, 1);
  63. preempt_enable();
  64. #else
  65. load_LDT(pc);
  66. #endif
  67. }
  68. if (oldsize) {
  69. paravirt_free_ldt(oldldt, oldsize);
  70. if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
  71. vfree(oldldt);
  72. else
  73. put_page(virt_to_page(oldldt));
  74. }
  75. return 0;
  76. }
  77. static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
  78. {
  79. int err = alloc_ldt(new, old->size, 0);
  80. int i;
  81. if (err < 0)
  82. return err;
  83. for (i = 0; i < old->size; i++)
  84. write_ldt_entry(new->ldt, i, old->ldt + i * LDT_ENTRY_SIZE);
  85. return 0;
  86. }
  87. /*
  88. * we do not have to muck with descriptors here, that is
  89. * done in switch_mm() as needed.
  90. */
  91. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  92. {
  93. struct mm_struct *old_mm;
  94. int retval = 0;
  95. mutex_init(&mm->context.lock);
  96. mm->context.size = 0;
  97. old_mm = current->mm;
  98. if (old_mm && old_mm->context.size > 0) {
  99. mutex_lock(&old_mm->context.lock);
  100. retval = copy_ldt(&mm->context, &old_mm->context);
  101. mutex_unlock(&old_mm->context.lock);
  102. }
  103. return retval;
  104. }
  105. /*
  106. * No need to lock the MM as we are the last user
  107. *
  108. * 64bit: Don't touch the LDT register - we're already in the next thread.
  109. */
  110. void destroy_context(struct mm_struct *mm)
  111. {
  112. if (mm->context.size) {
  113. #ifdef CONFIG_X86_32
  114. /* CHECKME: Can this ever happen ? */
  115. if (mm == current->active_mm)
  116. clear_LDT();
  117. #endif
  118. paravirt_free_ldt(mm->context.ldt, mm->context.size);
  119. if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
  120. vfree(mm->context.ldt);
  121. else
  122. put_page(virt_to_page(mm->context.ldt));
  123. mm->context.size = 0;
  124. }
  125. }
  126. static int read_ldt(void __user *ptr, unsigned long bytecount)
  127. {
  128. int err;
  129. unsigned long size;
  130. struct mm_struct *mm = current->mm;
  131. if (!mm->context.size)
  132. return 0;
  133. if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
  134. bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
  135. mutex_lock(&mm->context.lock);
  136. size = mm->context.size * LDT_ENTRY_SIZE;
  137. if (size > bytecount)
  138. size = bytecount;
  139. err = 0;
  140. if (copy_to_user(ptr, mm->context.ldt, size))
  141. err = -EFAULT;
  142. mutex_unlock(&mm->context.lock);
  143. if (err < 0)
  144. goto error_return;
  145. if (size != bytecount) {
  146. /* zero-fill the rest */
  147. if (clear_user(ptr + size, bytecount - size) != 0) {
  148. err = -EFAULT;
  149. goto error_return;
  150. }
  151. }
  152. return bytecount;
  153. error_return:
  154. return err;
  155. }
  156. static int read_default_ldt(void __user *ptr, unsigned long bytecount)
  157. {
  158. /* CHECKME: Can we use _one_ random number ? */
  159. #ifdef CONFIG_X86_32
  160. unsigned long size = 5 * sizeof(struct desc_struct);
  161. #else
  162. unsigned long size = 128;
  163. #endif
  164. if (bytecount > size)
  165. bytecount = size;
  166. if (clear_user(ptr, bytecount))
  167. return -EFAULT;
  168. return bytecount;
  169. }
  170. static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
  171. {
  172. struct mm_struct *mm = current->mm;
  173. struct desc_struct ldt;
  174. int error;
  175. struct user_desc ldt_info;
  176. error = -EINVAL;
  177. if (bytecount != sizeof(ldt_info))
  178. goto out;
  179. error = -EFAULT;
  180. if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
  181. goto out;
  182. error = -EINVAL;
  183. if (ldt_info.entry_number >= LDT_ENTRIES)
  184. goto out;
  185. if (ldt_info.contents == 3) {
  186. if (oldmode)
  187. goto out;
  188. if (ldt_info.seg_not_present == 0)
  189. goto out;
  190. }
  191. mutex_lock(&mm->context.lock);
  192. if (ldt_info.entry_number >= mm->context.size) {
  193. error = alloc_ldt(&current->mm->context,
  194. ldt_info.entry_number + 1, 1);
  195. if (error < 0)
  196. goto out_unlock;
  197. }
  198. /* Allow LDTs to be cleared by the user. */
  199. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  200. if (oldmode || LDT_empty(&ldt_info)) {
  201. memset(&ldt, 0, sizeof(ldt));
  202. goto install;
  203. }
  204. }
  205. if (!IS_ENABLED(CONFIG_X86_16BIT) && !ldt_info.seg_32bit) {
  206. error = -EINVAL;
  207. goto out_unlock;
  208. }
  209. fill_ldt(&ldt, &ldt_info);
  210. if (oldmode)
  211. ldt.avl = 0;
  212. /* Install the new entry ... */
  213. install:
  214. write_ldt_entry(mm->context.ldt, ldt_info.entry_number, &ldt);
  215. error = 0;
  216. out_unlock:
  217. mutex_unlock(&mm->context.lock);
  218. out:
  219. return error;
  220. }
  221. asmlinkage int sys_modify_ldt(int func, void __user *ptr,
  222. unsigned long bytecount)
  223. {
  224. int ret = -ENOSYS;
  225. switch (func) {
  226. case 0:
  227. ret = read_ldt(ptr, bytecount);
  228. break;
  229. case 1:
  230. ret = write_ldt(ptr, bytecount, 1);
  231. break;
  232. case 2:
  233. ret = read_default_ldt(ptr, bytecount);
  234. break;
  235. case 0x11:
  236. ret = write_ldt(ptr, bytecount, 0);
  237. break;
  238. }
  239. return ret;
  240. }