ldt.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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/slab.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/uaccess.h>
  17. #include <asm/ldt.h>
  18. #include <asm/desc.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/syscalls.h>
  21. /* context.lock is held for us, so we don't need any locking. */
  22. static void flush_ldt(void *current_mm)
  23. {
  24. mm_context_t *pc;
  25. if (current->active_mm != current_mm)
  26. return;
  27. pc = &current->active_mm->context;
  28. set_ldt(pc->ldt->entries, pc->ldt->size);
  29. }
  30. /* The caller must call finalize_ldt_struct on the result. LDT starts zeroed. */
  31. static struct ldt_struct *alloc_ldt_struct(int size)
  32. {
  33. struct ldt_struct *new_ldt;
  34. int alloc_size;
  35. if (size > LDT_ENTRIES)
  36. return NULL;
  37. new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
  38. if (!new_ldt)
  39. return NULL;
  40. BUILD_BUG_ON(LDT_ENTRY_SIZE != sizeof(struct desc_struct));
  41. alloc_size = size * LDT_ENTRY_SIZE;
  42. /*
  43. * Xen is very picky: it requires a page-aligned LDT that has no
  44. * trailing nonzero bytes in any page that contains LDT descriptors.
  45. * Keep it simple: zero the whole allocation and never allocate less
  46. * than PAGE_SIZE.
  47. */
  48. if (alloc_size > PAGE_SIZE)
  49. new_ldt->entries = vzalloc(alloc_size);
  50. else
  51. new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
  52. if (!new_ldt->entries) {
  53. kfree(new_ldt);
  54. return NULL;
  55. }
  56. new_ldt->size = size;
  57. return new_ldt;
  58. }
  59. /* After calling this, the LDT is immutable. */
  60. static void finalize_ldt_struct(struct ldt_struct *ldt)
  61. {
  62. paravirt_alloc_ldt(ldt->entries, ldt->size);
  63. }
  64. /* context.lock is held */
  65. static void install_ldt(struct mm_struct *current_mm,
  66. struct ldt_struct *ldt)
  67. {
  68. /* Synchronizes with lockless_dereference in load_mm_ldt. */
  69. smp_store_release(&current_mm->context.ldt, ldt);
  70. /* Activate the LDT for all CPUs using current_mm. */
  71. on_each_cpu_mask(mm_cpumask(current_mm), flush_ldt, current_mm, true);
  72. }
  73. static void free_ldt_struct(struct ldt_struct *ldt)
  74. {
  75. if (likely(!ldt))
  76. return;
  77. paravirt_free_ldt(ldt->entries, ldt->size);
  78. if (ldt->size * LDT_ENTRY_SIZE > PAGE_SIZE)
  79. vfree(ldt->entries);
  80. else
  81. free_page((unsigned long)ldt->entries);
  82. kfree(ldt);
  83. }
  84. /*
  85. * we do not have to muck with descriptors here, that is
  86. * done in switch_mm() as needed.
  87. */
  88. int init_new_context_ldt(struct task_struct *tsk, struct mm_struct *mm)
  89. {
  90. struct ldt_struct *new_ldt;
  91. struct mm_struct *old_mm;
  92. int retval = 0;
  93. mutex_init(&mm->context.lock);
  94. old_mm = current->mm;
  95. if (!old_mm) {
  96. mm->context.ldt = NULL;
  97. return 0;
  98. }
  99. mutex_lock(&old_mm->context.lock);
  100. if (!old_mm->context.ldt) {
  101. mm->context.ldt = NULL;
  102. goto out_unlock;
  103. }
  104. new_ldt = alloc_ldt_struct(old_mm->context.ldt->size);
  105. if (!new_ldt) {
  106. retval = -ENOMEM;
  107. goto out_unlock;
  108. }
  109. memcpy(new_ldt->entries, old_mm->context.ldt->entries,
  110. new_ldt->size * LDT_ENTRY_SIZE);
  111. finalize_ldt_struct(new_ldt);
  112. mm->context.ldt = new_ldt;
  113. out_unlock:
  114. mutex_unlock(&old_mm->context.lock);
  115. return retval;
  116. }
  117. /*
  118. * No need to lock the MM as we are the last user
  119. *
  120. * 64bit: Don't touch the LDT register - we're already in the next thread.
  121. */
  122. void destroy_context_ldt(struct mm_struct *mm)
  123. {
  124. free_ldt_struct(mm->context.ldt);
  125. mm->context.ldt = NULL;
  126. }
  127. static int read_ldt(void __user *ptr, unsigned long bytecount)
  128. {
  129. int retval;
  130. unsigned long size;
  131. struct mm_struct *mm = current->mm;
  132. mutex_lock(&mm->context.lock);
  133. if (!mm->context.ldt) {
  134. retval = 0;
  135. goto out_unlock;
  136. }
  137. if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
  138. bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
  139. size = mm->context.ldt->size * LDT_ENTRY_SIZE;
  140. if (size > bytecount)
  141. size = bytecount;
  142. if (copy_to_user(ptr, mm->context.ldt->entries, size)) {
  143. retval = -EFAULT;
  144. goto out_unlock;
  145. }
  146. if (size != bytecount) {
  147. /* Zero-fill the rest and pretend we read bytecount bytes. */
  148. if (clear_user(ptr + size, bytecount - size)) {
  149. retval = -EFAULT;
  150. goto out_unlock;
  151. }
  152. }
  153. retval = bytecount;
  154. out_unlock:
  155. mutex_unlock(&mm->context.lock);
  156. return retval;
  157. }
  158. static int read_default_ldt(void __user *ptr, unsigned long bytecount)
  159. {
  160. /* CHECKME: Can we use _one_ random number ? */
  161. #ifdef CONFIG_X86_32
  162. unsigned long size = 5 * sizeof(struct desc_struct);
  163. #else
  164. unsigned long size = 128;
  165. #endif
  166. if (bytecount > size)
  167. bytecount = size;
  168. if (clear_user(ptr, bytecount))
  169. return -EFAULT;
  170. return bytecount;
  171. }
  172. static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
  173. {
  174. struct mm_struct *mm = current->mm;
  175. struct desc_struct ldt;
  176. int error;
  177. struct user_desc ldt_info;
  178. int oldsize, newsize;
  179. struct ldt_struct *new_ldt, *old_ldt;
  180. error = -EINVAL;
  181. if (bytecount != sizeof(ldt_info))
  182. goto out;
  183. error = -EFAULT;
  184. if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
  185. goto out;
  186. error = -EINVAL;
  187. if (ldt_info.entry_number >= LDT_ENTRIES)
  188. goto out;
  189. if (ldt_info.contents == 3) {
  190. if (oldmode)
  191. goto out;
  192. if (ldt_info.seg_not_present == 0)
  193. goto out;
  194. }
  195. if ((oldmode && !ldt_info.base_addr && !ldt_info.limit) ||
  196. LDT_empty(&ldt_info)) {
  197. /* The user wants to clear the entry. */
  198. memset(&ldt, 0, sizeof(ldt));
  199. } else {
  200. if (!IS_ENABLED(CONFIG_X86_16BIT) && !ldt_info.seg_32bit) {
  201. error = -EINVAL;
  202. goto out;
  203. }
  204. fill_ldt(&ldt, &ldt_info);
  205. if (oldmode)
  206. ldt.avl = 0;
  207. }
  208. mutex_lock(&mm->context.lock);
  209. old_ldt = mm->context.ldt;
  210. oldsize = old_ldt ? old_ldt->size : 0;
  211. newsize = max((int)(ldt_info.entry_number + 1), oldsize);
  212. error = -ENOMEM;
  213. new_ldt = alloc_ldt_struct(newsize);
  214. if (!new_ldt)
  215. goto out_unlock;
  216. if (old_ldt)
  217. memcpy(new_ldt->entries, old_ldt->entries, oldsize * LDT_ENTRY_SIZE);
  218. new_ldt->entries[ldt_info.entry_number] = ldt;
  219. finalize_ldt_struct(new_ldt);
  220. install_ldt(mm, new_ldt);
  221. free_ldt_struct(old_ldt);
  222. error = 0;
  223. out_unlock:
  224. mutex_unlock(&mm->context.lock);
  225. out:
  226. return error;
  227. }
  228. asmlinkage int sys_modify_ldt(int func, void __user *ptr,
  229. unsigned long bytecount)
  230. {
  231. int ret = -ENOSYS;
  232. switch (func) {
  233. case 0:
  234. ret = read_ldt(ptr, bytecount);
  235. break;
  236. case 1:
  237. ret = write_ldt(ptr, bytecount, 1);
  238. break;
  239. case 2:
  240. ret = read_default_ldt(ptr, bytecount);
  241. break;
  242. case 0x11:
  243. ret = write_ldt(ptr, bytecount, 0);
  244. break;
  245. }
  246. return ret;
  247. }