ldt_64.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <linux/slab.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/system.h>
  17. #include <asm/ldt.h>
  18. #include <asm/desc.h>
  19. #include <asm/mmu_context.h>
  20. #ifdef CONFIG_SMP
  21. static void flush_ldt(void *null)
  22. {
  23. if (current->active_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 + 511) & (~511);
  35. if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE)
  36. newldt = vmalloc(mincount * LDT_ENTRY_SIZE);
  37. else
  38. newldt = kmalloc(mincount * LDT_ENTRY_SIZE, GFP_KERNEL);
  39. if (!newldt)
  40. return -ENOMEM;
  41. if (oldsize)
  42. memcpy(newldt, pc->ldt, oldsize * LDT_ENTRY_SIZE);
  43. oldldt = pc->ldt;
  44. memset(newldt + oldsize * LDT_ENTRY_SIZE, 0,
  45. (mincount - oldsize) * LDT_ENTRY_SIZE);
  46. wmb();
  47. pc->ldt = newldt;
  48. wmb();
  49. pc->size = mincount;
  50. wmb();
  51. if (reload) {
  52. #ifdef CONFIG_SMP
  53. cpumask_t mask;
  54. preempt_disable();
  55. load_LDT(pc);
  56. mask = cpumask_of_cpu(smp_processor_id());
  57. if (!cpus_equal(current->mm->cpu_vm_mask, mask))
  58. smp_call_function(flush_ldt, NULL, 1, 1);
  59. preempt_enable();
  60. #else
  61. load_LDT(pc);
  62. #endif
  63. }
  64. if (oldsize) {
  65. if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
  66. vfree(oldldt);
  67. else
  68. kfree(oldldt);
  69. }
  70. return 0;
  71. }
  72. static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
  73. {
  74. int err = alloc_ldt(new, old->size, 0);
  75. if (err < 0)
  76. return err;
  77. memcpy(new->ldt, old->ldt, old->size * LDT_ENTRY_SIZE);
  78. return 0;
  79. }
  80. /*
  81. * we do not have to muck with descriptors here, that is
  82. * done in switch_mm() as needed.
  83. */
  84. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  85. {
  86. struct mm_struct *old_mm;
  87. int retval = 0;
  88. mutex_init(&mm->context.lock);
  89. mm->context.size = 0;
  90. old_mm = current->mm;
  91. if (old_mm && old_mm->context.size > 0) {
  92. mutex_lock(&old_mm->context.lock);
  93. retval = copy_ldt(&mm->context, &old_mm->context);
  94. mutex_unlock(&old_mm->context.lock);
  95. }
  96. return retval;
  97. }
  98. /*
  99. * Don't touch the LDT register - we're already in the next thread.
  100. */
  101. void destroy_context(struct mm_struct *mm)
  102. {
  103. if (mm->context.size) {
  104. if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
  105. vfree(mm->context.ldt);
  106. else
  107. kfree(mm->context.ldt);
  108. mm->context.size = 0;
  109. }
  110. }
  111. static int read_ldt(void __user *ptr, unsigned long bytecount)
  112. {
  113. int err;
  114. unsigned long size;
  115. struct mm_struct *mm = current->mm;
  116. if (!mm->context.size)
  117. return 0;
  118. if (bytecount > LDT_ENTRY_SIZE * LDT_ENTRIES)
  119. bytecount = LDT_ENTRY_SIZE * LDT_ENTRIES;
  120. mutex_lock(&mm->context.lock);
  121. size = mm->context.size * LDT_ENTRY_SIZE;
  122. if (size > bytecount)
  123. size = bytecount;
  124. err = 0;
  125. if (copy_to_user(ptr, mm->context.ldt, size))
  126. err = -EFAULT;
  127. mutex_unlock(&mm->context.lock);
  128. if (err < 0)
  129. goto error_return;
  130. if (size != bytecount) {
  131. /* zero-fill the rest */
  132. if (clear_user(ptr + size, bytecount - size) != 0) {
  133. err = -EFAULT;
  134. goto error_return;
  135. }
  136. }
  137. return bytecount;
  138. error_return:
  139. return err;
  140. }
  141. static int read_default_ldt(void __user *ptr, unsigned long bytecount)
  142. {
  143. /* Arbitrary number */
  144. /* x86-64 default LDT is all zeros */
  145. if (bytecount > 128)
  146. bytecount = 128;
  147. if (clear_user(ptr, bytecount))
  148. return -EFAULT;
  149. return bytecount;
  150. }
  151. static int write_ldt(void __user *ptr, unsigned long bytecount, int oldmode)
  152. {
  153. struct mm_struct *mm = current->mm;
  154. __u32 entry_1, entry_2;
  155. int error;
  156. struct user_desc ldt_info;
  157. error = -EINVAL;
  158. if (bytecount != sizeof(ldt_info))
  159. goto out;
  160. error = -EFAULT;
  161. if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
  162. goto out;
  163. error = -EINVAL;
  164. if (ldt_info.entry_number >= LDT_ENTRIES)
  165. goto out;
  166. if (ldt_info.contents == 3) {
  167. if (oldmode)
  168. goto out;
  169. if (ldt_info.seg_not_present == 0)
  170. goto out;
  171. }
  172. mutex_lock(&mm->context.lock);
  173. if (ldt_info.entry_number >= mm->context.size) {
  174. error = alloc_ldt(&current->mm->context,
  175. ldt_info.entry_number + 1, 1);
  176. if (error < 0)
  177. goto out_unlock;
  178. }
  179. /* Allow LDTs to be cleared by the user. */
  180. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  181. if (oldmode || LDT_empty(&ldt_info)) {
  182. entry_1 = 0;
  183. entry_2 = 0;
  184. goto install;
  185. }
  186. }
  187. entry_1 = LDT_entry_a(&ldt_info);
  188. entry_2 = LDT_entry_b(&ldt_info);
  189. if (oldmode)
  190. entry_2 &= ~(1 << 20);
  191. /* Install the new entry ... */
  192. install:
  193. write_ldt_entry(mm->context.ldt, ldt_info.entry_number, entry_1,
  194. entry_2);
  195. error = 0;
  196. out_unlock:
  197. mutex_unlock(&mm->context.lock);
  198. out:
  199. return error;
  200. }
  201. asmlinkage int sys_modify_ldt(int func, void __user *ptr,
  202. unsigned long bytecount)
  203. {
  204. int ret = -ENOSYS;
  205. switch (func) {
  206. case 0:
  207. ret = read_ldt(ptr, bytecount);
  208. break;
  209. case 1:
  210. ret = write_ldt(ptr, bytecount, 1);
  211. break;
  212. case 2:
  213. ret = read_default_ldt(ptr, bytecount);
  214. break;
  215. case 0x11:
  216. ret = write_ldt(ptr, bytecount, 0);
  217. break;
  218. }
  219. return ret;
  220. }