ldt.c 6.6 KB

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