ldt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * linux/kernel/ldt.c
  3. *
  4. * Copyright (C) 1992 Krishna Balasubramanian and Linus Torvalds
  5. * Copyright (C) 1999 Ingo Molnar <mingo@redhat.com>
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/sched.h>
  9. #include <linux/string.h>
  10. #include <linux/mm.h>
  11. #include <linux/smp.h>
  12. #include <linux/smp_lock.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 /* avoids "defined but not used" warnig */
  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;
  30. void *newldt;
  31. int oldsize;
  32. if (mincount <= pc->size)
  33. return 0;
  34. oldsize = pc->size;
  35. mincount = (mincount+511)&(~511);
  36. if (mincount*LDT_ENTRY_SIZE > PAGE_SIZE)
  37. newldt = vmalloc(mincount*LDT_ENTRY_SIZE);
  38. else
  39. newldt = kmalloc(mincount*LDT_ENTRY_SIZE, 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, (mincount-oldsize)*LDT_ENTRY_SIZE);
  46. pc->ldt = newldt;
  47. wmb();
  48. pc->size = mincount;
  49. wmb();
  50. if (reload) {
  51. #ifdef CONFIG_SMP
  52. cpumask_t mask;
  53. preempt_disable();
  54. load_LDT(pc);
  55. mask = cpumask_of_cpu(smp_processor_id());
  56. if (!cpus_equal(current->mm->cpu_vm_mask, mask))
  57. smp_call_function(flush_ldt, NULL, 1, 1);
  58. preempt_enable();
  59. #else
  60. load_LDT(pc);
  61. #endif
  62. }
  63. if (oldsize) {
  64. if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
  65. vfree(oldldt);
  66. else
  67. kfree(oldldt);
  68. }
  69. return 0;
  70. }
  71. static inline int copy_ldt(mm_context_t *new, mm_context_t *old)
  72. {
  73. int err = alloc_ldt(new, old->size, 0);
  74. if (err < 0)
  75. return err;
  76. memcpy(new->ldt, old->ldt, old->size*LDT_ENTRY_SIZE);
  77. return 0;
  78. }
  79. /*
  80. * we do not have to muck with descriptors here, that is
  81. * done in switch_mm() as needed.
  82. */
  83. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  84. {
  85. struct mm_struct * old_mm;
  86. int retval = 0;
  87. init_MUTEX(&mm->context.sem);
  88. mm->context.size = 0;
  89. old_mm = current->mm;
  90. if (old_mm && old_mm->context.size > 0) {
  91. down(&old_mm->context.sem);
  92. retval = copy_ldt(&mm->context, &old_mm->context);
  93. up(&old_mm->context.sem);
  94. }
  95. return retval;
  96. }
  97. /*
  98. * No need to lock the MM as we are the last user
  99. */
  100. void destroy_context(struct mm_struct *mm)
  101. {
  102. if (mm->context.size) {
  103. if (mm == current->active_mm)
  104. clear_LDT();
  105. if (mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE)
  106. vfree(mm->context.ldt);
  107. else
  108. kfree(mm->context.ldt);
  109. mm->context.size = 0;
  110. }
  111. }
  112. static int read_ldt(void __user * ptr, unsigned long bytecount)
  113. {
  114. int err;
  115. unsigned long size;
  116. struct mm_struct * mm = current->mm;
  117. if (!mm->context.size)
  118. return 0;
  119. if (bytecount > LDT_ENTRY_SIZE*LDT_ENTRIES)
  120. bytecount = LDT_ENTRY_SIZE*LDT_ENTRIES;
  121. down(&mm->context.sem);
  122. size = mm->context.size*LDT_ENTRY_SIZE;
  123. if (size > bytecount)
  124. size = bytecount;
  125. err = 0;
  126. if (copy_to_user(ptr, mm->context.ldt, size))
  127. err = -EFAULT;
  128. up(&mm->context.sem);
  129. if (err < 0)
  130. goto error_return;
  131. if (size != bytecount) {
  132. /* zero-fill the rest */
  133. if (clear_user(ptr+size, bytecount-size) != 0) {
  134. err = -EFAULT;
  135. goto error_return;
  136. }
  137. }
  138. return bytecount;
  139. error_return:
  140. return err;
  141. }
  142. static int read_default_ldt(void __user * ptr, unsigned long bytecount)
  143. {
  144. int err;
  145. unsigned long size;
  146. void *address;
  147. err = 0;
  148. address = &default_ldt[0];
  149. size = 5*sizeof(struct desc_struct);
  150. if (size > bytecount)
  151. size = bytecount;
  152. err = size;
  153. if (copy_to_user(ptr, address, size))
  154. err = -EFAULT;
  155. return err;
  156. }
  157. static int write_ldt(void __user * ptr, unsigned long bytecount, int oldmode)
  158. {
  159. struct mm_struct * mm = current->mm;
  160. __u32 entry_1, entry_2;
  161. int error;
  162. struct user_desc ldt_info;
  163. error = -EINVAL;
  164. if (bytecount != sizeof(ldt_info))
  165. goto out;
  166. error = -EFAULT;
  167. if (copy_from_user(&ldt_info, ptr, sizeof(ldt_info)))
  168. goto out;
  169. error = -EINVAL;
  170. if (ldt_info.entry_number >= LDT_ENTRIES)
  171. goto out;
  172. if (ldt_info.contents == 3) {
  173. if (oldmode)
  174. goto out;
  175. if (ldt_info.seg_not_present == 0)
  176. goto out;
  177. }
  178. down(&mm->context.sem);
  179. if (ldt_info.entry_number >= mm->context.size) {
  180. error = alloc_ldt(&current->mm->context, ldt_info.entry_number+1, 1);
  181. if (error < 0)
  182. goto out_unlock;
  183. }
  184. /* Allow LDTs to be cleared by the user. */
  185. if (ldt_info.base_addr == 0 && ldt_info.limit == 0) {
  186. if (oldmode || LDT_empty(&ldt_info)) {
  187. entry_1 = 0;
  188. entry_2 = 0;
  189. goto install;
  190. }
  191. }
  192. entry_1 = LDT_entry_a(&ldt_info);
  193. entry_2 = LDT_entry_b(&ldt_info);
  194. if (oldmode)
  195. entry_2 &= ~(1 << 20);
  196. /* Install the new entry ... */
  197. install:
  198. write_ldt_entry(mm->context.ldt, ldt_info.entry_number, entry_1, entry_2);
  199. error = 0;
  200. out_unlock:
  201. up(&mm->context.sem);
  202. out:
  203. return error;
  204. }
  205. asmlinkage int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount)
  206. {
  207. int ret = -ENOSYS;
  208. switch (func) {
  209. case 0:
  210. ret = read_ldt(ptr, bytecount);
  211. break;
  212. case 1:
  213. ret = write_ldt(ptr, bytecount, 1);
  214. break;
  215. case 2:
  216. ret = read_default_ldt(ptr, bytecount);
  217. break;
  218. case 0x11:
  219. ret = write_ldt(ptr, bytecount, 0);
  220. break;
  221. }
  222. return ret;
  223. }