ldt.c 7.1 KB

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